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