don't strip the state for keyboard events
[dana/openbox.git] / openbox / actions / cyclewindows.c
1 #include "openbox/actions.h"
2 #include "openbox/stacking.h"
3 #include "openbox/window.h"
4 #include "openbox/event.h"
5 #include "openbox/focus_cycle.h"
6 #include "openbox/openbox.h"
7 #include "gettext.h"
8 #include "obt/keyboard.h"
9
10 typedef struct {
11     gboolean linear;
12     gboolean dock_windows;
13     gboolean desktop_windows;
14     gboolean all_desktops;
15     gboolean forward;
16     gboolean bar;
17     gboolean raise;
18     ObFocusCyclePopupMode dialog_mode;
19     GSList *actions;
20
21
22     /* options for after we're done */
23     gboolean cancel; /* did the user cancel or not */
24     guint state;     /* keyboard state when finished */
25 } Options;
26
27 static gpointer setup_func(xmlNodePtr node,
28                            ObActionsIPreFunc *pre,
29                            ObActionsIInputFunc *in,
30                            ObActionsICancelFunc *c,
31                            ObActionsIPostFunc *post);
32 static gpointer setup_forward_func(xmlNodePtr node,
33                                    ObActionsIPreFunc *pre,
34                                    ObActionsIInputFunc *in,
35                                    ObActionsICancelFunc *c,
36                                    ObActionsIPostFunc *post);
37 static gpointer setup_backward_func(xmlNodePtr node,
38                                     ObActionsIPreFunc *pre,
39                                     ObActionsIInputFunc *in,
40                                     ObActionsICancelFunc *c,
41                                     ObActionsIPostFunc *post);
42 static void     free_func(gpointer options);
43 static gboolean run_func(ObActionsData *data, gpointer options);
44 static gboolean i_input_func(guint initial_state,
45                              XEvent *e,
46                              gpointer options,
47                              gboolean *used);
48 static void     i_cancel_func(gpointer options);
49 static void     i_post_func(gpointer options);
50
51 void action_cyclewindows_startup(void)
52 {
53     actions_register_i("NextWindow", setup_forward_func, free_func, run_func);
54     actions_register_i("PreviousWindow", setup_backward_func, free_func,
55                        run_func);
56 }
57
58 static gpointer setup_func(xmlNodePtr node,
59                            ObActionsIPreFunc *pre,
60                            ObActionsIInputFunc *input,
61                            ObActionsICancelFunc *cancel,
62                            ObActionsIPostFunc *post)
63 {
64     xmlNodePtr n;
65     Options *o;
66
67     o = g_new0(Options, 1);
68     o->bar = TRUE;
69     o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_LIST;
70
71     if ((n = obt_xml_find_node(node, "linear")))
72         o->linear = obt_xml_node_bool(n);
73     if ((n = obt_xml_find_node(node, "dialog"))) {
74         if (obt_xml_node_contains(n, "none"))
75             o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_NONE;
76         else if (obt_xml_node_contains(n, "icons"))
77             o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_ICONS;
78     }
79     if ((n = obt_xml_find_node(node, "bar")))
80         o->bar = obt_xml_node_bool(n);
81     if ((n = obt_xml_find_node(node, "raise")))
82         o->raise = obt_xml_node_bool(n);
83     if ((n = obt_xml_find_node(node, "panels")))
84         o->dock_windows = obt_xml_node_bool(n);
85     if ((n = obt_xml_find_node(node, "desktop")))
86         o->desktop_windows = obt_xml_node_bool(n);
87     if ((n = obt_xml_find_node(node, "allDesktops")))
88         o->all_desktops = obt_xml_node_bool(n);
89
90     if ((n = obt_xml_find_node(node, "finalactions"))) {
91         xmlNodePtr m;
92
93         m = obt_xml_find_node(n->children, "action");
94         while (m) {
95             ObActionsAct *action = actions_parse(m);
96             if (action) o->actions = g_slist_append(o->actions, action);
97             m = obt_xml_find_node(m->next, "action");
98         }
99     }
100     else {
101         o->actions = g_slist_prepend(o->actions,
102                                      actions_parse_string("Focus"));
103         o->actions = g_slist_prepend(o->actions,
104                                      actions_parse_string("Raise"));
105         o->actions = g_slist_prepend(o->actions,
106                                      actions_parse_string("Unshade"));
107     }
108
109     *input = i_input_func;
110     *cancel = i_cancel_func;
111     *post = i_post_func;
112     return o;
113 }
114
115 static gpointer setup_forward_func(xmlNodePtr node,
116                                    ObActionsIPreFunc *pre,
117                                    ObActionsIInputFunc *input,
118                                    ObActionsICancelFunc *cancel,
119                                    ObActionsIPostFunc *post)
120 {
121     Options *o = setup_func(node, pre, input, cancel, post);
122     o->forward = TRUE;
123     return o;
124 }
125
126 static gpointer setup_backward_func(xmlNodePtr node,
127                                     ObActionsIPreFunc *pre,
128                                     ObActionsIInputFunc *input,
129                                     ObActionsICancelFunc *cancel,
130                                     ObActionsIPostFunc *post)
131 {
132     Options *o = setup_func(node, pre, input, cancel, post);
133     o->forward = FALSE;
134     return o;
135 }
136
137 static void free_func(gpointer options)
138 {
139     Options *o = options;
140
141     while (o->actions) {
142         actions_act_unref(o->actions->data);
143         o->actions = g_slist_delete_link(o->actions, o->actions);
144     }
145
146     g_free(o);
147 }
148
149 static gboolean run_func(ObActionsData *data, gpointer options)
150 {
151     Options *o = options;
152     struct _ObClient *ft;
153
154     ft = focus_cycle(o->forward,
155                      o->all_desktops,
156                      o->dock_windows,
157                      o->desktop_windows,
158                      o->linear,
159                      TRUE,
160                      o->bar,
161                      o->dialog_mode,
162                      FALSE, FALSE);
163
164     stacking_restore();
165     if (o->raise && ft) stacking_temp_raise(CLIENT_AS_WINDOW(ft));
166
167     return TRUE;
168 }
169
170 static gboolean i_input_func(guint initial_state,
171                              XEvent *e,
172                              gpointer options,
173                              gboolean *used)
174 {
175     Options *o = options;
176     guint mods;
177
178     mods = obt_keyboard_only_modmasks(e->xkey.state);
179     if (e->type == KeyRelease) {
180         /* remove from the state the mask of the modifier key being
181            released, if it is a modifier key being released that is */
182         mods &= ~obt_keyboard_keycode_to_modmask(e->xkey.keycode);
183     }
184
185     if (e->type == KeyPress) {
186         /* Escape cancels no matter what */
187         if (ob_keycode_match(e->xkey.keycode, OB_KEY_ESCAPE)) {
188             o->cancel = TRUE;
189             o->state = e->xkey.state;
190             return FALSE;
191         }
192
193         /* There were no modifiers and they pressed enter */
194         else if (ob_keycode_match(e->xkey.keycode, OB_KEY_RETURN) &&
195                  !initial_state)
196         {
197             o->cancel = FALSE;
198             o->state = e->xkey.state;
199             return FALSE;
200         }
201     }
202     /* They released the modifiers */
203     else if (e->type == KeyRelease && initial_state && !(mods & initial_state))
204     {
205         o->cancel = FALSE;
206         o->state = e->xkey.state;
207         return FALSE;
208     }
209
210     return TRUE;
211 }
212
213 static void i_cancel_func(gpointer options)
214 {
215     Options *o = options;
216     o->cancel = TRUE;
217     o->state = 0;
218 }
219
220 static void i_post_func(gpointer options)
221 {
222     Options *o = options;
223     struct _ObClient *ft;
224
225     ft = focus_cycle(o->forward,
226                      o->all_desktops,
227                      o->dock_windows,
228                      o->desktop_windows,
229                      o->linear,
230                      TRUE,
231                      o->bar,
232                      o->dialog_mode,
233                      TRUE, o->cancel);
234
235     if (ft)
236         actions_run_acts(o->actions, OB_USER_ACTION_KEYBOARD_KEY,
237                          o->state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, ft);
238
239     stacking_restore();
240 }