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