]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/actions/directionalwindows.c
Merge branch 'work' into wip/mikabox
[mikachu/openbox.git] / openbox / actions / directionalwindows.c
1 #include "openbox/actions.h"
2 #include "openbox/event.h"
3 #include "openbox/stacking.h"
4 #include "openbox/window.h"
5 #include "openbox/focus_cycle.h"
6 #include "openbox/openbox.h"
7 #include "openbox/misc.h"
8 #include "openbox/client.h"
9 #include "gettext.h"
10
11 typedef struct {
12     gboolean interactive;
13     gboolean dialog;
14     gboolean dock_windows;
15     gboolean desktop_windows;
16     ObDirection direction;
17     gboolean bar;
18     gboolean raise;
19     GSList *actions;
20 } Options;
21
22 static gboolean cycling = FALSE;
23
24 static gpointer setup_func(xmlNodePtr node);
25 static gpointer setup_cycle_func(xmlNodePtr node);
26 static gpointer setup_target_func(xmlNodePtr node);
27 static void     free_func(gpointer options);
28 static gboolean run_func(ObActionsData *data, gpointer options);
29 static gboolean i_input_func(guint initial_state,
30                              XEvent *e,
31                              gpointer options,
32                              gboolean *used);
33 static void     i_cancel_func(gpointer options);
34
35 static void     end_cycle(gboolean cancel, guint state, Options *o);
36
37 void action_directionalwindows_startup(void)
38 {
39     actions_register("DirectionalCycleWindows", setup_cycle_func, free_func,
40                      run_func, i_input_func, i_cancel_func);
41     actions_register("DirectionalTargetWindow", setup_target_func, free_func,
42                      run_func, NULL, NULL);
43 }
44
45 static gpointer setup_func(xmlNodePtr node)
46 {
47     xmlNodePtr n;
48     Options *o;
49
50     o = g_new0(Options, 1);
51     o->dialog = TRUE;
52     o->bar = TRUE;
53
54     if ((n = obt_parse_find_node(node, "dialog")))
55         o->dialog = obt_parse_node_bool(n);
56     if ((n = obt_parse_find_node(node, "bar")))
57         o->bar = obt_parse_node_bool(n);
58     if ((n = obt_parse_find_node(node, "raise")))
59         o->raise = obt_parse_node_bool(n);
60     if ((n = obt_parse_find_node(node, "panels")))
61         o->dock_windows = obt_parse_node_bool(n);
62     if ((n = obt_parse_find_node(node, "desktop")))
63         o->desktop_windows = obt_parse_node_bool(n);
64     if ((n = obt_parse_find_node(node, "direction"))) {
65         gchar *s = obt_parse_node_string(n);
66         if (!g_ascii_strcasecmp(s, "north") ||
67             !g_ascii_strcasecmp(s, "up"))
68             o->direction = OB_DIRECTION_NORTH;
69         else if (!g_ascii_strcasecmp(s, "northwest"))
70             o->direction = OB_DIRECTION_NORTHWEST;
71         else if (!g_ascii_strcasecmp(s, "northeast"))
72             o->direction = OB_DIRECTION_NORTHEAST;
73         else if (!g_ascii_strcasecmp(s, "west") ||
74                  !g_ascii_strcasecmp(s, "left"))
75             o->direction = OB_DIRECTION_WEST;
76         else if (!g_ascii_strcasecmp(s, "east") ||
77                  !g_ascii_strcasecmp(s, "right"))
78             o->direction = OB_DIRECTION_EAST;
79         else if (!g_ascii_strcasecmp(s, "south") ||
80                  !g_ascii_strcasecmp(s, "down"))
81             o->direction = OB_DIRECTION_SOUTH;
82         else if (!g_ascii_strcasecmp(s, "southwest"))
83             o->direction = OB_DIRECTION_SOUTHWEST;
84         else if (!g_ascii_strcasecmp(s, "southeast"))
85             o->direction = OB_DIRECTION_SOUTHEAST;
86         g_free(s);
87     }
88
89     if ((n = obt_parse_find_node(node, "finalactions"))) {
90         xmlNodePtr m;
91
92         m = obt_parse_find_node(n->children, "action");
93         while (m) {
94             ObActionsAct *action = actions_parse(m);
95             if (action) o->actions = g_slist_append(o->actions, action);
96             m = obt_parse_find_node(m->next, "action");
97         }
98     }
99     else {
100         o->actions = g_slist_prepend(o->actions,
101                                      actions_parse_string("Focus"));
102         o->actions = g_slist_prepend(o->actions,
103                                      actions_parse_string("Raise"));
104         o->actions = g_slist_prepend(o->actions,
105                                      actions_parse_string("Unshade"));
106     }
107
108     return o;
109 }
110
111 static gpointer setup_cycle_func(xmlNodePtr node)
112 {
113     Options *o = setup_func(node);
114     o->interactive = TRUE;
115     return o;
116 }
117
118 static gpointer setup_target_func(xmlNodePtr node)
119 {
120     Options *o = setup_func(node);
121     o->interactive = FALSE;
122     return o;
123 }
124
125 static void free_func(gpointer options)
126 {
127     Options *o = options;
128
129     while (o->actions) {
130         actions_act_unref(o->actions->data);
131         o->actions = g_slist_delete_link(o->actions, o->actions);
132     }
133
134     g_free(o);
135 }
136
137 static gboolean run_func(ObActionsData *data, gpointer options)
138 {
139     Options *o = options;
140
141     if (!o->interactive)
142         end_cycle(FALSE, data->state, o);
143     else {
144         struct _ObClient *ft;
145
146         ft = focus_directional_cycle(o->direction,
147                                      o->dock_windows,
148                                      o->desktop_windows,
149                                      TRUE,
150                                      o->bar,
151                                      o->dialog,
152                                      FALSE, FALSE);
153         cycling = TRUE;
154
155         stacking_restore();
156         if (o->raise && ft) stacking_temp_raise(CLIENT_AS_WINDOW(ft));
157     }
158
159     return o->interactive;
160 }
161
162 static gboolean i_input_func(guint initial_state,
163                              XEvent *e,
164                              gpointer options,
165                              gboolean *used)
166 {
167     if (e->type == KeyPress) {
168         /* Escape cancels no matter what */
169         if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
170             end_cycle(TRUE, e->xkey.state, options);
171             return FALSE;
172         }
173
174         /* There were no modifiers and they pressed enter */
175         else if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN) &&
176                  !initial_state)
177         {
178             end_cycle(FALSE, e->xkey.state, options);
179             return FALSE;
180         }
181     }
182     /* They released the modifiers */
183     else if (e->type == KeyRelease && initial_state &&
184              (e->xkey.state & initial_state) == 0)
185     {
186         end_cycle(FALSE, e->xkey.state, options);
187         return FALSE;
188     }
189
190     return TRUE;
191 }
192
193 static void i_cancel_func(gpointer options)
194 {
195     /* we get cancelled when we move focus, but we're not cycling anymore, so
196        just ignore that */
197     if (cycling)
198         end_cycle(TRUE, 0, options);
199 }
200
201 static void end_cycle(gboolean cancel, guint state, Options *o)
202 {
203     struct _ObClient *ft;
204
205     ft = focus_directional_cycle(o->direction,
206                                  o->dock_windows,
207                                  o->desktop_windows,
208                                  o->interactive,
209                                  o->bar,
210                                  o->dialog,
211                                  TRUE, cancel);
212     cycling = FALSE;
213
214     if (g_list_find(client_list, ft))
215         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
216                          state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
217
218     stacking_restore();
219 }