]> icculus.org git repositories - manmower/openbox.git/blob - openbox/keyboard.c
allow noninteractive directional focus.
[manmower/openbox.git] / openbox / keyboard.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    keyboard.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "mainloop.h"
21 #include "focus.h"
22 #include "screen.h"
23 #include "frame.h"
24 #include "openbox.h"
25 #include "event.h"
26 #include "grab.h"
27 #include "client.h"
28 #include "action.h"
29 #include "prop.h"
30 #include "menuframe.h"
31 #include "config.h"
32 #include "keytree.h"
33 #include "keyboard.h"
34 #include "translate.h"
35 #include "moveresize.h"
36 #include "popup.h"
37 #include "gettext.h"
38
39 #include <glib.h>
40
41 typedef struct {
42     gboolean active;
43     guint state;
44     ObClient *client;
45     ObAction *action;
46 } ObInteractiveState;
47
48 KeyBindingTree *keyboard_firstnode = NULL;
49 static ObPopup *popup = NULL;
50 static ObInteractiveState istate;
51 static KeyBindingTree *curpos;
52
53 static void grab_keys(gboolean grab)
54 {
55     KeyBindingTree *p;
56
57     ungrab_all_keys(RootWindow(ob_display, ob_screen));
58
59     if (grab) {
60         p = curpos ? curpos->first_child : keyboard_firstnode;
61         while (p) {
62             grab_key(p->key, p->state, RootWindow(ob_display, ob_screen),
63                      GrabModeAsync);
64             p = p->next_sibling;
65         }
66         if (curpos)
67             grab_key(config_keyboard_reset_keycode,
68                      config_keyboard_reset_state,
69                      RootWindow(ob_display, ob_screen), GrabModeAsync);
70     }
71 }
72
73 static gboolean chain_timeout(gpointer data)
74 {
75     keyboard_reset_chains(0);
76     return FALSE; /* don't repeat */
77 }
78
79 static void set_curpos(KeyBindingTree *newpos)
80 {
81     if (curpos == newpos) return;
82
83     grab_keys(FALSE);
84     curpos = newpos;
85     grab_keys(TRUE);
86
87     if (curpos != NULL) {
88         gchar *text = NULL;
89         GList *it;
90         Rect *a;
91
92         for (it = curpos->keylist; it; it = g_list_next(it)) {
93             gchar *oldtext = text;
94             if (text == NULL)
95                 text = g_strdup(it->data);
96             else
97                 text = g_strconcat(text, " - ", it->data, NULL);
98             g_free(oldtext);
99         }
100
101         a = screen_physical_area_monitor_active();
102         popup_position(popup, NorthWestGravity, a->x + 10, a->y + 10);
103         /* 1 second delay for the popup to show */
104         popup_delay_show(popup, G_USEC_PER_SEC, text);
105         g_free(text);
106     } else {
107         popup_hide(popup);
108     }
109 }
110
111 void keyboard_reset_chains(gint break_chroots)
112 {
113     KeyBindingTree *p;
114
115     for (p = curpos; p; p = p->parent) {
116         if (p->chroot) {
117             if (break_chroots == 0) break; /* stop here */
118             if (break_chroots > 0)
119                 --break_chroots;
120         }
121     }
122     set_curpos(p);
123 }
124
125 void keyboard_unbind_all()
126 {
127     tree_destroy(keyboard_firstnode);
128     keyboard_firstnode = NULL;
129 }
130
131 void keyboard_chroot(GList *keylist)
132 {
133     /* try do it in the existing tree. if we can't that means it is an empty
134        chroot binding. so add it to the tree then. */
135     if (!tree_chroot(keyboard_firstnode, keylist)) {
136         KeyBindingTree *tree;
137         if (!(tree = tree_build(keylist)))
138             return;
139         tree_chroot(tree, keylist);
140         tree_assimilate(tree);
141     }
142 }
143
144 gboolean keyboard_bind(GList *keylist, ObAction *action)
145 {
146     KeyBindingTree *tree, *t;
147     gboolean conflict;
148
149     g_assert(keylist != NULL);
150     g_assert(action != NULL);
151
152     if (!(tree = tree_build(keylist)))
153         return FALSE;
154
155     if ((t = tree_find(tree, &conflict)) != NULL) {
156         /* already bound to something, use the existing tree */
157         tree_destroy(tree);
158         tree = NULL;
159     } else
160         t = tree;
161
162     if (conflict) {
163         g_message(_("Conflict with key binding in config file"));
164         tree_destroy(tree);
165         return FALSE;
166     }
167
168     /* find the bottom node */
169     for (; t->first_child; t = t->first_child);
170
171     /* when there are no modifiers in the binding, then the action cannot
172        be interactive */
173     if (!t->state && action->data.any.interactive) {
174         g_print("not interactive\n");
175         action->data.any.interactive = FALSE;
176         action->data.inter.final = TRUE;
177     }
178
179     /* set the action */
180     t->actions = g_slist_append(t->actions, action);
181     /* assimilate this built tree into the main tree. assimilation
182        destroys/uses the tree */
183     if (tree) tree_assimilate(tree);
184
185     return TRUE;
186 }
187
188 static void keyboard_interactive_end(guint state, gboolean cancel, Time time,
189                                      gboolean ungrab)
190 {
191     GSList *alist;
192
193     g_assert(istate.active);
194
195     /* ungrab first so they won't be NotifyWhileGrabbed */
196     if (ungrab)
197         ungrab_keyboard();
198
199     /* set this before running the actions so they know the keyboard is not
200        grabbed */
201     istate.active = FALSE;
202
203     alist = g_slist_append(NULL, istate.action);
204     action_run_interactive(alist, istate.client, state, time, cancel, TRUE);
205     g_slist_free(alist);
206
207     keyboard_reset_chains(0);
208 }
209
210 static void keyboard_interactive_end_client(ObClient *client, gpointer data)
211 {
212     if (istate.active && istate.client == client)
213         istate.client = NULL;
214 }
215
216
217 void keyboard_interactive_cancel()
218 {
219     keyboard_interactive_end(0, TRUE, event_curtime, TRUE);
220 }
221
222 gboolean keyboard_interactive_grab(guint state, ObClient *client,
223                                    ObAction *action)
224 {
225     g_assert(action->data.any.interactive);
226
227     if (!istate.active) {
228         if (!grab_keyboard())
229             return FALSE;
230     } else if (action->func != istate.action->func) {
231         keyboard_interactive_end(state, TRUE, action->data.any.time, FALSE);
232     }
233
234     istate.active = TRUE;
235     istate.state = state;
236     istate.client = client;
237     istate.action = action;
238
239     return TRUE;
240 }
241
242 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
243 {
244     gboolean handled = FALSE;
245     gboolean done = FALSE;
246     gboolean cancel = FALSE;
247
248     if (istate.active) {
249         if ((e->type == KeyRelease && !(istate.state & e->xkey.state))) {
250             done = TRUE;
251             handled = TRUE;
252         } else if (e->type == KeyPress) {
253             /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
254               done = TRUE;
255               else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE)) {
256                   cancel = done = TRUE;
257                   handled = TRUE;
258               }
259         } else if (e->type == ButtonPress) {
260             cancel = TRUE;
261             done = TRUE;
262             handled = FALSE;
263         }
264
265         if (done)
266             keyboard_interactive_end(e->xkey.state, cancel, e->xkey.time,TRUE);
267
268         if (handled)
269             *client = istate.client;
270     }
271
272     return handled;
273 }
274
275 void keyboard_event(ObClient *client, const XEvent *e)
276 {
277     KeyBindingTree *p;
278
279     g_assert(e->type == KeyPress);
280
281     if (e->xkey.keycode == config_keyboard_reset_keycode &&
282         e->xkey.state == config_keyboard_reset_state)
283     {
284         ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
285         keyboard_reset_chains(-1);
286         return;
287     }
288
289     if (curpos == NULL)
290         p = keyboard_firstnode;
291     else
292         p = curpos->first_child;
293     while (p) {
294         if (p->key == e->xkey.keycode &&
295             p->state == e->xkey.state)
296         {
297             /* if we hit a key binding, then close any open menus and run it */
298             if (menu_frame_visible)
299                 menu_frame_hide_all();
300
301             if (p->first_child != NULL) { /* part of a chain */
302                 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
303                 /* 3 second timeout for chains */
304                 ob_main_loop_timeout_add(ob_main_loop, 3 * G_USEC_PER_SEC,
305                                          chain_timeout, NULL,
306                                          g_direct_equal, NULL);
307                 set_curpos(p);
308             } else if (p->chroot)         /* an empty chroot */
309                 set_curpos(p);
310             else {
311                 GSList *it;
312                 gboolean inter = FALSE;
313
314                 for (it = p->actions; it && !inter; it = g_slist_next(it))
315                     if (((ObAction*)it->data)->data.any.interactive)
316                         inter = TRUE;
317                 if (!inter) /* don't reset if the action is interactive */
318                     keyboard_reset_chains(0);
319
320                 action_run_key(p->actions, client, e->xkey.state,
321                                e->xkey.x_root, e->xkey.y_root,
322                                e->xkey.time);
323             }
324             break;
325         }
326         p = p->next_sibling;
327     }
328 }
329
330 gboolean keyboard_interactively_grabbed()
331 {
332     return istate.active;
333 }
334
335 void keyboard_startup(gboolean reconfig)
336 {
337     grab_keys(TRUE);
338     popup = popup_new(FALSE);
339     popup_set_text_align(popup, RR_JUSTIFY_CENTER);
340
341     if (!reconfig)
342         client_add_destroy_notify(keyboard_interactive_end_client, NULL);
343 }
344
345 void keyboard_shutdown(gboolean reconfig)
346 {
347     if (!reconfig)
348         client_remove_destroy_notify(keyboard_interactive_end_client);
349
350     if (istate.active)
351         keyboard_interactive_cancel();
352
353     ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
354
355     keyboard_unbind_all();
356     set_curpos(NULL);
357
358     popup_free(popup);
359     popup = NULL;
360 }
361