more using g_slice_new() instead of g_new()
[dana/openbox.git] / openbox / actions / movetoedge.c
1 #include "openbox/actions.h"
2 #include "openbox/misc.h"
3 #include "openbox/client.h"
4 #include "openbox/frame.h"
5 #include "openbox/geom.h"
6 #include <glib.h>
7
8 typedef struct {
9     ObDirection dir;
10 } Options;
11
12 static gpointer setup_func(xmlNodePtr node);
13 static void free_func(gpointer o);
14 static gboolean run_func(ObActionsData *data, gpointer options);
15 /* 3.4-compatibility */
16 static gpointer setup_north_func(xmlNodePtr node);
17 static gpointer setup_south_func(xmlNodePtr node);
18 static gpointer setup_east_func(xmlNodePtr node);
19 static gpointer setup_west_func(xmlNodePtr node);
20
21 void action_movetoedge_startup(void)
22 {
23     actions_register("MoveToEdge", setup_func, free_func, run_func);
24     /* 3.4-compatibility */
25     actions_register("MoveToEdgeNorth", setup_north_func, free_func, run_func);
26     actions_register("MoveToEdgeSouth", setup_south_func, free_func, run_func);
27     actions_register("MoveToEdgeEast", setup_east_func, free_func, run_func);
28     actions_register("MoveToEdgeWest", setup_west_func, free_func, run_func);
29 }
30
31 static gpointer setup_func(xmlNodePtr node)
32 {
33     xmlNodePtr n;
34     Options *o;
35
36     o = g_slice_new0(Options);
37     o->dir = OB_DIRECTION_NORTH;
38
39     if ((n = obt_xml_find_node(node, "direction"))) {
40         gchar *s = obt_xml_node_string(n);
41         if (!g_ascii_strcasecmp(s, "north") ||
42             !g_ascii_strcasecmp(s, "up"))
43             o->dir = OB_DIRECTION_NORTH;
44         else if (!g_ascii_strcasecmp(s, "south") ||
45                  !g_ascii_strcasecmp(s, "down"))
46             o->dir = OB_DIRECTION_SOUTH;
47         else if (!g_ascii_strcasecmp(s, "west") ||
48                  !g_ascii_strcasecmp(s, "left"))
49             o->dir = OB_DIRECTION_WEST;
50         else if (!g_ascii_strcasecmp(s, "east") ||
51                  !g_ascii_strcasecmp(s, "right"))
52             o->dir = OB_DIRECTION_EAST;
53         g_free(s);
54     }
55
56     return o;
57 }
58
59 static void free_func(gpointer o)
60 {
61     g_slice_free(Options, o);
62 }
63
64 /* Always return FALSE because its not interactive */
65 static gboolean run_func(ObActionsData *data, gpointer options)
66 {
67     Options *o = options;
68
69     if (data->client) {
70         gint x, y;
71
72         client_find_move_directional(data->client, o->dir, &x, &y);
73         if (x != data->client->area.x || y != data->client->area.y) {
74             actions_client_move(data, TRUE);
75             client_move(data->client, x, y);
76             actions_client_move(data, FALSE);
77         }
78     }
79
80     return FALSE;
81 }
82
83 /* 3.4-compatibility */
84 static gpointer setup_north_func(xmlNodePtr node)
85 {
86     Options *o = g_slice_new0(Options);
87     o->dir = OB_DIRECTION_NORTH;
88     return o;
89 }
90
91 static gpointer setup_south_func(xmlNodePtr node)
92 {
93     Options *o = g_slice_new0(Options);
94     o->dir = OB_DIRECTION_SOUTH;
95     return o;
96 }
97
98 static gpointer setup_east_func(xmlNodePtr node)
99 {
100     Options *o = g_slice_new0(Options);
101     o->dir = OB_DIRECTION_EAST;
102     return o;
103 }
104
105 static gpointer setup_west_func(xmlNodePtr node)
106 {
107     Options *o = g_slice_new0(Options);
108     o->dir = OB_DIRECTION_WEST;
109     return o;
110 }
111