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