]> Dogcows Code - chaz/openbox/blob - openbox/actions.c
create fake enter events for button press bindings. only swallow enter events when...
[chaz/openbox] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 actions.h for the Openbox window manager
4 Copyright (c) 2007 Dana Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "actions.h"
20 #include "gettext.h"
21 #include "grab.h"
22 #include "screen.h"
23 #include "event.h"
24 #include "config.h"
25 #include "client.h"
26 #include "debug.h"
27
28 #include "actions/all.h"
29
30 static void actions_definition_ref(ObActionsDefinition *def);
31 static void actions_definition_unref(ObActionsDefinition *def);
32 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
33 static void actions_interactive_end_act();
34 static ObActionsAct* actions_build_act_from_string(const gchar *name);
35
36 static ObActionsAct *interactive_act = NULL;
37 static guint interactive_initial_state = 0;
38
39 struct _ObActionsDefinition {
40 guint ref;
41
42 gchar *name;
43
44 ObActionsDataSetupFunc setup;
45 ObActionsDataFreeFunc free;
46 ObActionsRunFunc run;
47 ObActionsInteractiveInputFunc i_input;
48 ObActionsInteractiveCancelFunc i_cancel;
49 };
50
51 struct _ObActionsAct {
52 guint ref;
53
54 ObActionsDefinition *def;
55 gpointer options;
56 };
57
58 static GSList *registered = NULL;
59
60
61 void actions_startup(gboolean reconfig)
62 {
63 if (reconfig) return;
64
65 action_all_startup();
66 }
67
68 void actions_shutdown(gboolean reconfig)
69 {
70 actions_interactive_cancel_act();
71
72 if (reconfig) return;
73
74 /* free all the registered actions */
75 while (registered) {
76 actions_definition_unref(registered->data);
77 registered = g_slist_delete_link(registered, registered);
78 }
79 }
80
81 gboolean actions_register(const gchar *name,
82 ObActionsDataSetupFunc setup,
83 ObActionsDataFreeFunc free,
84 ObActionsRunFunc run,
85 ObActionsInteractiveInputFunc i_input,
86 ObActionsInteractiveCancelFunc i_cancel)
87 {
88 GSList *it;
89 ObActionsDefinition *def;
90
91 g_assert(run != NULL);
92 g_assert((i_input == NULL) == (i_cancel == NULL));
93
94 for (it = registered; it; it = g_slist_next(it)) {
95 def = it->data;
96 if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
97 return FALSE;
98 }
99
100 def = g_new(ObActionsDefinition, 1);
101 def->ref = 1;
102 def->name = g_strdup(name);
103 def->setup = setup;
104 def->free = free;
105 def->run = run;
106 def->i_input = i_input;
107 def->i_cancel = i_cancel;
108
109 registered = g_slist_prepend(registered, def);
110
111 return TRUE;
112 }
113
114 static void actions_definition_ref(ObActionsDefinition *def)
115 {
116 ++def->ref;
117 }
118
119 static void actions_definition_unref(ObActionsDefinition *def)
120 {
121 if (def && --def->ref == 0) {
122 g_free(def->name);
123 g_free(def);
124 }
125 }
126
127 static ObActionsAct* actions_build_act_from_string(const gchar *name)
128 {
129 GSList *it;
130 ObActionsDefinition *def = NULL;
131 ObActionsAct *act = NULL;
132
133 /* find the requested action */
134 for (it = registered; it; it = g_slist_next(it)) {
135 def = it->data;
136 if (!g_ascii_strcasecmp(name, def->name))
137 break;
138 def = NULL;
139 }
140
141 /* if we found the action */
142 if (def) {
143 act = g_new(ObActionsAct, 1);
144 act->ref = 1;
145 act->def = def;
146 actions_definition_ref(act->def);
147 act->options = NULL;
148 } else
149 g_message(_("Invalid action '%s' requested. No such action exists."),
150 name);
151
152 return act;
153 }
154
155 ObActionsAct* actions_parse_string(const gchar *name)
156 {
157 ObActionsAct *act = NULL;
158
159 if ((act = actions_build_act_from_string(name)))
160 if (act->def->setup)
161 act->options = act->def->setup(NULL, NULL, NULL);
162
163 return act;
164 }
165
166 ObActionsAct* actions_parse(ObParseInst *i,
167 xmlDocPtr doc,
168 xmlNodePtr node)
169 {
170 gchar *name;
171 ObActionsAct *act = NULL;
172
173 if (parse_attr_string("name", node, &name)) {
174 if ((act = actions_build_act_from_string(name)))
175 /* there is more stuff to parse here */
176 if (act->def->setup)
177 act->options = act->def->setup(i, doc, node->xmlChildrenNode);
178
179 g_free(name);
180 }
181
182 return act;
183 }
184
185 gboolean actions_act_is_interactive(ObActionsAct *act)
186 {
187 return act->def->i_cancel != NULL;
188 }
189
190 void actions_act_ref(ObActionsAct *act)
191 {
192 ++act->ref;
193 }
194
195 void actions_act_unref(ObActionsAct *act)
196 {
197 if (act && --act->ref == 0) {
198 /* free the action specific options */
199 if (act->def->free)
200 act->def->free(act->options);
201 /* unref the definition */
202 actions_definition_unref(act->def);
203 g_free(act);
204 }
205 }
206
207 static void actions_setup_data(ObActionsData *data,
208 ObUserAction uact,
209 guint state,
210 gint x,
211 gint y,
212 gint button,
213 ObFrameContext con,
214 struct _ObClient *client)
215 {
216 data->uact = uact;
217 data->state = state;
218 data->x = x;
219 data->y = y;
220 data->button = button;
221 data->context = con;
222 data->client = client;
223 }
224
225 void actions_run_acts(GSList *acts,
226 ObUserAction uact,
227 guint state,
228 gint x,
229 gint y,
230 gint button,
231 ObFrameContext con,
232 struct _ObClient *client)
233 {
234 GSList *it;
235
236 /* Don't allow saving the initial state when running things from the
237 menu */
238 if (uact == OB_USER_ACTION_MENU_SELECTION)
239 state = 0;
240 /* If x and y are < 0 then use the current pointer position */
241 if (x < 0 && y < 0)
242 screen_pointer_pos(&x, &y);
243
244 for (it = acts; it; it = g_slist_next(it)) {
245 ObActionsData data;
246 ObActionsAct *act = it->data;
247 gboolean ok = TRUE;
248
249 actions_setup_data(&data, uact, state, x, y, button, con, client);
250
251 /* if they have the same run function, then we'll assume they are
252 cooperating and not cancel eachother out */
253 if (!interactive_act || interactive_act->def->run != act->def->run) {
254 if (actions_act_is_interactive(act)) {
255 /* cancel the old one */
256 if (interactive_act)
257 actions_interactive_cancel_act();
258 ok = actions_interactive_begin_act(act, state);
259 }
260 }
261
262 /* fire the action's run function with this data */
263 if (ok) {
264 if (!act->def->run(&data, act->options)) {
265 if (actions_act_is_interactive(act))
266 actions_interactive_end_act();
267 } else {
268 /* make sure its interactive if it returned TRUE */
269 g_assert(act->def->i_cancel && act->def->i_input);
270
271 /* no actions are run after the interactive one */
272 break;
273 }
274 }
275 }
276 }
277
278 gboolean actions_interactive_act_running(void)
279 {
280 return interactive_act != NULL;
281 }
282
283 void actions_interactive_cancel_act(void)
284 {
285 if (interactive_act) {
286 interactive_act->def->i_cancel(interactive_act->options);
287 actions_interactive_end_act();
288 }
289 }
290
291 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
292 {
293 if (grab_keyboard()) {
294 interactive_act = act;
295 actions_act_ref(interactive_act);
296
297 interactive_initial_state = state;
298
299 /* if using focus_delay, stop the timer now so that focus doesn't go
300 moving on us, which would kill the action */
301 event_halt_focus_delay();
302
303 return TRUE;
304 }
305 else
306 return FALSE;
307 }
308
309 static void actions_interactive_end_act(void)
310 {
311 if (interactive_act) {
312 ungrab_keyboard();
313
314 actions_act_unref(interactive_act);
315 interactive_act = NULL;
316 }
317 }
318
319 gboolean actions_interactive_input_event(XEvent *e)
320 {
321 gboolean used = FALSE;
322 if (interactive_act) {
323 if (!interactive_act->def->i_input(interactive_initial_state, e,
324 interactive_act->options, &used))
325 {
326 used = TRUE; /* if it cancelled the action then it has to of
327 been used */
328 actions_interactive_end_act();
329 }
330 }
331 return used;
332 }
333
334 void actions_client_move(ObActionsData *data, gboolean start)
335 {
336 static gulong ignore_start = 0;
337 if (start)
338 ignore_start = event_start_ignore_all_enters();
339 else if (config_focus_follow &&
340 data->context != OB_FRAME_CONTEXT_CLIENT)
341 {
342 if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
343 struct _ObClient *c;
344
345 /* usually this is sorta redundant, but with a press action
346 that moves windows our from under the cursor, the enter
347 event will come as a GrabNotify which is ignored, so this
348 makes a fake enter event
349 */
350 if ((c = client_under_pointer()) && c != data->client) {
351 ob_debug_type(OB_DEBUG_FOCUS,
352 "Generating fake enter because we did a "
353 "mouse-event action");
354 event_enter_client(c);
355 }
356 }
357 else if (!data->button && !config_focus_under_mouse)
358 event_end_ignore_all_enters(ignore_start);
359 }
360 }
This page took 0.049412 seconds and 5 git commands to generate.