]> icculus.org git repositories - dana/openbox.git/blob - openbox/actions.c
rm some unused fn defns
[dana/openbox.git] / openbox / actions.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    actions.c 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 "actions_list.h"
21 #include "gettext.h"
22 #include "grab.h"
23 #include "screen.h"
24 #include "event.h"
25 #include "config.h"
26 #include "client.h"
27 #include "focus.h"
28 #include "openbox.h"
29 #include "debug.h"
30
31 #include "actions/all.h"
32
33 static void     actions_definition_ref(ObActionsDefinition *def);
34 static void     actions_definition_unref(ObActionsDefinition *def);
35 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state);
36 static void     actions_interactive_end_act();
37 static ObActionsAct* actions_act_find_name(const gchar *name);
38
39 static ObActionsAct *interactive_act = NULL;
40 static guint         interactive_initial_state = 0;
41
42 struct _ObActionsDefinition {
43     guint ref;
44
45     gchar *name;
46
47     gboolean canbeinteractive;
48     union {
49         ObActionsIDataSetupFunc i;
50         ObActionsDataSetupFunc n;
51     } setup;
52     ObActionsDataFreeFunc free;
53     ObActionsRunFunc run;
54     ObActionsShutdownFunc shutdown;
55 };
56
57 struct _ObActionsAct {
58     guint ref;
59
60     ObActionsDefinition *def;
61     ObActionsIPreFunc i_pre;
62     ObActionsIInputFunc i_input;
63     ObActionsICancelFunc i_cancel;
64     ObActionsIPostFunc i_post;
65     gpointer options;
66 };
67
68 static GSList *registered = NULL;
69
70 void actions_startup(gboolean reconfig)
71 {
72     if (reconfig) return;
73
74     action_all_startup();
75 }
76
77 void actions_shutdown(gboolean reconfig)
78 {
79     actions_interactive_cancel_act();
80
81     if (reconfig) return;
82
83     /* free all the registered actions */
84     while (registered) {
85         ObActionsDefinition *d = registered->data;
86         if (d->shutdown) d->shutdown();
87         actions_definition_unref(d);
88         registered = g_slist_delete_link(registered, registered);
89     }
90 }
91
92 ObActionsDefinition* do_register(const gchar *name,
93                                  ObActionsDataFreeFunc free,
94                                  ObActionsRunFunc run)
95 {
96     GSList *it;
97     ObActionsDefinition *def;
98
99     g_assert(run != NULL);
100
101     for (it = registered; it; it = g_slist_next(it)) {
102         def = it->data;
103         if (!g_ascii_strcasecmp(name, def->name)) /* already registered */
104             return NULL;
105     }
106
107     def = g_slice_new(ObActionsDefinition);
108     def->ref = 1;
109     def->name = g_strdup(name);
110     def->free = free;
111     def->run = run;
112     def->shutdown = NULL;
113
114     registered = g_slist_prepend(registered, def);
115     return def;
116 }
117
118 gboolean actions_register_i(const gchar *name,
119                             ObActionsIDataSetupFunc setup,
120                             ObActionsDataFreeFunc free,
121                             ObActionsRunFunc run)
122 {
123     ObActionsDefinition *def = do_register(name, free, run);
124     if (def) {
125         def->canbeinteractive = TRUE;
126         def->setup.i = setup;
127     }
128     return def != NULL;
129 }
130
131 gboolean actions_register(const gchar *name,
132                           ObActionsDataSetupFunc setup,
133                           ObActionsDataFreeFunc free,
134                           ObActionsRunFunc run)
135 {
136     ObActionsDefinition *def = do_register(name, free, run);
137     if (def) {
138         def->canbeinteractive = FALSE;
139         def->setup.n = setup;
140     }
141     return def != NULL;
142 }
143
144 gboolean actions_set_shutdown(const gchar *name,
145                               ObActionsShutdownFunc shutdown)
146 {
147     GSList *it;
148     ObActionsDefinition *def;
149
150     for (it = registered; it; it = g_slist_next(it)) {
151         def = it->data;
152         if (!g_ascii_strcasecmp(name, def->name)) {
153             def->shutdown = shutdown;
154             return TRUE;
155         }
156     }
157     return FALSE;
158 }
159
160 static void actions_definition_ref(ObActionsDefinition *def)
161 {
162     ++def->ref;
163 }
164
165 static void actions_definition_unref(ObActionsDefinition *def)
166 {
167     if (def && --def->ref == 0) {
168         g_free(def->name);
169         g_slice_free(ObActionsDefinition, def);
170     }
171 }
172
173 static ObActionsAct* actions_act_find_name(const gchar *name)
174 {
175     GSList *it;
176     ObActionsDefinition *def = NULL;
177     ObActionsAct *act = NULL;
178
179     /* find the requested action */
180     for (it = registered; it; it = g_slist_next(it)) {
181         def = it->data;
182         if (!g_ascii_strcasecmp(name, def->name))
183             break;
184         def = NULL;
185     }
186
187     /* if we found the action */
188     if (def) {
189         act = g_slice_new(ObActionsAct);
190         act->ref = 1;
191         act->def = def;
192         actions_definition_ref(act->def);
193         act->i_pre = NULL;
194         act->i_input = NULL;
195         act->i_cancel = NULL;
196         act->i_post = NULL;
197         act->options = NULL;
198     } else
199         g_message(_("Invalid action \"%s\" requested. No such action exists."),
200                   name);
201
202     return act;
203 }
204
205 ObActionsAct* actions_act_new(const gchar *name, GHashTable *config)
206 {
207     ObActionsAct *act = NULL;
208
209     act = actions_act_find_name(name);
210     if (act) {
211         /* there is more stuff to parse here */
212         if (act->def->canbeinteractive) {
213             if (act->def->setup.i)
214                 act->options = act->def->setup.i(config,
215                                                  &act->i_pre,
216                                                  &act->i_input,
217                                                  &act->i_cancel,
218                                                  &act->i_post);
219         }
220         else {
221             if (act->def->setup.n)
222                 act->options = act->def->setup.n(config);
223         }
224     }
225
226     return act;
227 }
228
229 gboolean actions_act_is_interactive(ObActionsAct *act)
230 {
231     return act->i_input != NULL;
232 }
233
234 void actions_act_ref(ObActionsAct *act)
235 {
236     ++act->ref;
237 }
238
239 void actions_act_unref(ObActionsAct *act)
240 {
241     if (act && --act->ref == 0) {
242         /* free the action specific options */
243         if (act->def->free)
244             act->def->free(act->options);
245         /* unref the definition */
246         actions_definition_unref(act->def);
247         g_slice_free(ObActionsAct, act);
248     }
249 }
250
251 static void actions_setup_data(ObActionsData *data,
252                                ObUserAction uact,
253                                guint state,
254                                gint x,
255                                gint y,
256                                gint button,
257                                ObFrameContext con,
258                                struct _ObClient *client)
259 {
260     data->uact = uact;
261     data->state = state;
262     data->x = x;
263     data->y = y;
264     data->button = button;
265     data->context = con;
266     data->client = client;
267 }
268
269 gboolean actions_run_acts(ObActionsList *acts,
270                           ObUserAction uact,
271                           guint state,
272                           gint x,
273                           gint y,
274                           gint button,
275                           ObFrameContext con,
276                           struct _ObClient *client)
277 {
278     gboolean ran_interactive;
279     gboolean update_user_time;
280
281     /* Don't allow saving the initial state when running things from the
282        menu */
283     if (uact == OB_USER_ACTION_MENU_SELECTION)
284         state = 0;
285     /* If x and y are < 0 then use the current pointer position */
286     if (x < 0 && y < 0)
287         screen_pointer_pos(&x, &y);
288
289     ran_interactive = FALSE;
290     update_user_time = FALSE;
291     while (acts) {
292         ObActionsAct *act;
293         ObActionsData data;
294         gboolean ok = TRUE;
295
296         if (acts->isfilter) {
297             g_warning("filters not implemented!");
298             acts = acts->next;
299             continue;
300         }
301         else {
302             act = acts->u.action;
303         }
304
305         actions_setup_data(&data, uact, state, x, y, button, con, client);
306
307         /* if they have the same run function, then we'll assume they are
308            cooperating and not cancel eachother out */
309         if (!interactive_act || interactive_act->def->run != act->def->run) {
310             if (actions_act_is_interactive(act)) {
311                 /* cancel the old one */
312                 if (interactive_act)
313                     actions_interactive_cancel_act();
314                 if (act->i_pre)
315                     if (!act->i_pre(state, act->options))
316                         act->i_input = NULL; /* remove the interactivity */
317                 ran_interactive = TRUE;
318             }
319             /* check again cuz it might have been cancelled */
320             if (actions_act_is_interactive(act)) {
321                 ok = actions_interactive_begin_act(act, state);
322                 ran_interactive = TRUE;
323             }
324         }
325
326         /* fire the action's run function with this data */
327         if (ok) {
328             if (!act->def->run(&data, act->options)) {
329                 if (actions_act_is_interactive(act))
330                     actions_interactive_end_act();
331                 if (client && client == focus_client)
332                     update_user_time = TRUE;
333             } else {
334                 /* make sure its interactive if it returned TRUE */
335                 g_assert(act->i_input);
336
337                 /* no actions are run after the interactive one */
338                 break;
339             }
340         }
341         acts = acts->next;
342     }
343     if (update_user_time)
344         event_update_user_time();
345     return ran_interactive;
346 }
347
348 gboolean actions_interactive_act_running(void)
349 {
350     return interactive_act != NULL;
351 }
352
353 void actions_interactive_cancel_act(void)
354 {
355     if (interactive_act) {
356         if (interactive_act->i_cancel)
357             interactive_act->i_cancel(interactive_act->options);
358         actions_interactive_end_act();
359     }
360 }
361
362 static gboolean actions_interactive_begin_act(ObActionsAct *act, guint state)
363 {
364     if (grab_keyboard()) {
365         interactive_act = act;
366         actions_act_ref(interactive_act);
367
368         interactive_initial_state = state;
369
370         /* if using focus_delay, stop the timer now so that focus doesn't go
371            moving on us, which would kill the action */
372         event_halt_focus_delay();
373
374         return TRUE;
375     }
376     else
377         return FALSE;
378 }
379
380 static void actions_interactive_end_act(void)
381 {
382     if (interactive_act) {
383         ObActionsAct *ia = interactive_act;
384
385         /* set this to NULL first so the i_post() function can't cause this to
386            get called again (if it decides it wants to cancel any ongoing
387            interactive action). */
388         interactive_act = NULL;
389
390         ungrab_keyboard();
391
392         if (ia->i_post)
393             ia->i_post(ia->options);
394
395         actions_act_unref(ia);
396     }
397 }
398
399 gboolean actions_interactive_input_event(XEvent *e)
400 {
401     gboolean used = FALSE;
402     if (interactive_act) {
403         if (!interactive_act->i_input(interactive_initial_state, e,
404                                       grab_input_context(),
405                                       interactive_act->options, &used))
406         {
407             used = TRUE; /* if it cancelled the action then it has to of
408                             been used */
409             actions_interactive_end_act();
410         }
411     }
412     return used;
413 }
414
415 void actions_client_move(ObActionsData *data, gboolean start)
416 {
417     static gulong ignore_start = 0;
418     if (start)
419         ignore_start = event_start_ignore_all_enters();
420     else if (config_focus_follow &&
421              data->context != OB_FRAME_CONTEXT_CLIENT)
422     {
423         if (data->uact == OB_USER_ACTION_MOUSE_PRESS) {
424             struct _ObClient *c;
425
426             /* usually this is sorta redundant, but with a press action
427                that moves windows our from under the cursor, the enter
428                event will come as a GrabNotify which is ignored, so this
429                makes a fake enter event
430
431                don't do this if there is a grab on the pointer.  enter events
432                are ignored during a grab, so don't force fake ones when they
433                should be ignored
434             */
435             if (!grab_on_pointer()) {
436                 if ((c = client_under_pointer()) && c != data->client) {
437                     ob_debug_type(OB_DEBUG_FOCUS,
438                                   "Generating fake enter because we did a "
439                                   "mouse-event action");
440                     event_enter_client(c);
441                 }
442                 else if (!c && c != data->client) {
443                     ob_debug_type(OB_DEBUG_FOCUS,
444                                   "Generating fake leave because we did a "
445                                   "mouse-event action");
446                     event_enter_client(data->client);
447                 }
448             }
449         }
450         else if (!data->button && !config_focus_under_mouse)
451             event_end_ignore_all_enters(ignore_start);
452     }
453 }