Add 'last' as a desktop target for if/foreach
[openbox.git] / openbox / actions / cyclewindows.c
blobf834951502a0db96393ee3b21ccbd64071a9ee59
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"
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;
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;
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);
54 void action_cyclewindows_startup(void)
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);
61 static gpointer setup_func(xmlNodePtr node,
62 ObActionsIPreFunc *pre,
63 ObActionsIInputFunc *input,
64 ObActionsICancelFunc *cancel,
65 ObActionsIPostFunc *post)
67 xmlNodePtr n;
68 Options *o;
70 o = g_slice_new0(Options);
71 o->bar = TRUE;
72 o->dialog_mode = OB_FOCUS_CYCLE_POPUP_MODE_LIST;
73 o->interactive = TRUE;
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;
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);
100 if ((n = obt_xml_find_node(node, "finalactions"))) {
101 xmlNodePtr m;
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");
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"));
119 *input = i_input_func;
120 *cancel = i_cancel_func;
121 *post = i_post_func;
122 return o;
125 static gpointer setup_forward_func(xmlNodePtr node,
126 ObActionsIPreFunc *pre,
127 ObActionsIInputFunc *input,
128 ObActionsICancelFunc *cancel,
129 ObActionsIPostFunc *post)
131 Options *o = setup_func(node, pre, input, cancel, post);
132 o->forward = TRUE;
133 return o;
136 static gpointer setup_backward_func(xmlNodePtr node,
137 ObActionsIPreFunc *pre,
138 ObActionsIInputFunc *input,
139 ObActionsICancelFunc *cancel,
140 ObActionsIPostFunc *post)
142 Options *o = setup_func(node, pre, input, cancel, post);
143 o->forward = FALSE;
144 return o;
147 static void free_func(gpointer options)
149 Options *o = options;
151 while (o->actions) {
152 actions_act_unref(o->actions->data);
153 o->actions = g_slist_delete_link(o->actions, o->actions);
156 g_slice_free(Options, o);
159 static gboolean run_func(ObActionsData *data, gpointer options)
161 Options *o = options;
162 struct _ObClient *ft;
164 gboolean done = FALSE;
165 gboolean cancel = FALSE;
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);
178 stacking_restore();
179 if (o->raise && ft) stacking_temp_raise(CLIENT_AS_WINDOW(ft));
181 return o->interactive;
184 static gboolean i_input_func(guint initial_state,
185 XEvent *e,
186 ObtIC *ic,
187 gpointer options,
188 gboolean *used)
190 Options *o = options;
191 guint mods, initial_mods;
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);
201 if (e->type == KeyPress) {
202 KeySym sym = obt_keyboard_keypress_to_keysym(e);
204 /* Escape cancels no matter what */
205 if (sym == XK_Escape) {
206 o->cancel = TRUE;
207 o->state = e->xkey.state;
208 return FALSE;
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;
218 /* They released the modifiers */
219 else if (e->type == KeyRelease && initial_mods && !(mods & initial_mods))
221 o->cancel = FALSE;
222 o->state = e->xkey.state;
223 return FALSE;
226 return TRUE;
229 static void i_cancel_func(gpointer options)
231 Options *o = options;
232 o->cancel = TRUE;
233 o->state = 0;
236 static void i_post_func(gpointer options)
238 Options *o = options;
239 struct _ObClient *ft;
241 gboolean done = TRUE;
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);
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);
257 stacking_restore();