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