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