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