]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
update to the new strings. and add menu shortcuts for the client menu to them all
[chaz/openbox] / 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 "config.h"
31 #include "keytree.h"
32 #include "keyboard.h"
33 #include "translate.h"
34 #include "moveresize.h"
35 #include "gettext.h"
36
37 #include <glib.h>
38
39 KeyBindingTree *keyboard_firstnode;
40
41 typedef struct {
42 guint state;
43 ObClient *client;
44 GSList *actions;
45 ObFrameContext context;
46 } ObInteractiveState;
47
48 static GSList *interactive_states;
49
50 static KeyBindingTree *curpos;
51
52 static void grab_for_window(Window win, gboolean grab)
53 {
54 KeyBindingTree *p;
55
56 ungrab_all_keys(win);
57
58 if (grab) {
59 p = curpos ? curpos->first_child : keyboard_firstnode;
60 while (p) {
61 grab_key(p->key, p->state, win, GrabModeAsync);
62 p = p->next_sibling;
63 }
64 if (curpos)
65 grab_key(config_keyboard_reset_keycode,
66 config_keyboard_reset_state,
67 win, GrabModeAsync);
68 }
69 }
70
71 static void grab_keys(gboolean grab)
72 {
73 GList *it;
74
75 grab_for_window(screen_support_win, grab);
76 grab_for_window(RootWindow(ob_display, ob_screen), grab);
77 }
78
79 static gboolean chain_timeout(gpointer data)
80 {
81 keyboard_reset_chains();
82
83 return FALSE; /* don't repeat */
84 }
85
86 void keyboard_reset_chains()
87 {
88 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
89
90 if (curpos) {
91 grab_keys(FALSE);
92 curpos = NULL;
93 grab_keys(TRUE);
94 }
95 }
96
97 void keyboard_unbind_all()
98 {
99 tree_destroy(keyboard_firstnode);
100 keyboard_firstnode = NULL;
101 grab_keys(FALSE);
102 curpos = NULL;
103 }
104
105 gboolean keyboard_bind(GList *keylist, ObAction *action)
106 {
107 KeyBindingTree *tree, *t;
108 gboolean conflict;
109 gboolean mods = TRUE;
110
111 g_assert(keylist != NULL);
112 g_assert(action != NULL);
113
114 if (!(tree = tree_build(keylist)))
115 return FALSE;
116
117 if ((t = tree_find(tree, &conflict)) != NULL) {
118 /* already bound to something, use the existing tree */
119 tree_destroy(tree);
120 tree = NULL;
121 } else
122 t = tree;
123
124 if (conflict) {
125 g_message(_("Conflict with key binding in config file"));
126 tree_destroy(tree);
127 return FALSE;
128 }
129
130 /* find if every key in this chain has modifiers, and also find the
131 bottom node of the tree */
132 while (t->first_child) {
133 if (!t->state)
134 mods = FALSE;
135 t = t->first_child;
136 }
137
138 /* when there are no modifiers in the binding, then the action cannot
139 be interactive */
140 if (!mods && action->data.any.interactive) {
141 action->data.any.interactive = FALSE;
142 action->data.inter.final = TRUE;
143 }
144
145 /* set the action */
146 t->actions = g_slist_append(t->actions, action);
147 /* assimilate this built tree into the main tree. assimilation
148 destroys/uses the tree */
149 if (tree) tree_assimilate(tree);
150
151 return TRUE;
152 }
153
154 gboolean keyboard_interactive_grab(guint state, ObClient *client,
155 ObAction *action)
156 {
157 ObInteractiveState *s;
158
159 g_assert(action->data.any.interactive);
160
161 if (!interactive_states) {
162 if (!grab_keyboard(TRUE))
163 return FALSE;
164 }
165
166 s = g_new(ObInteractiveState, 1);
167
168 s->state = state;
169 s->client = client;
170 s->actions = g_slist_append(NULL, action);
171
172 interactive_states = g_slist_append(interactive_states, s);
173
174 return TRUE;
175 }
176
177 void keyboard_interactive_end(ObInteractiveState *s,
178 guint state, gboolean cancel, Time time)
179 {
180 action_run_interactive(s->actions, s->client, state, time, cancel, TRUE);
181
182 g_slist_free(s->actions);
183 g_free(s);
184
185 interactive_states = g_slist_remove(interactive_states, s);
186
187 if (!interactive_states) {
188 grab_keyboard(FALSE);
189 keyboard_reset_chains();
190 }
191 }
192
193 void keyboard_interactive_end_client(ObClient *client, gpointer data)
194 {
195 GSList *it, *next;
196
197 for (it = interactive_states; it; it = next) {
198 ObInteractiveState *s = it->data;
199
200 next = g_slist_next(it);
201
202 if (s->client == client)
203 s->client = NULL;
204 }
205 }
206
207 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
208 {
209 GSList *it, *next;
210 gboolean handled = FALSE;
211 gboolean done = FALSE;
212 gboolean cancel = FALSE;
213
214 for (it = interactive_states; it; it = next) {
215 ObInteractiveState *s = it->data;
216
217 next = g_slist_next(it);
218
219 if ((e->type == KeyRelease &&
220 !(s->state & e->xkey.state)))
221 done = TRUE;
222 else if (e->type == KeyPress) {
223 /*if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
224 done = TRUE;
225 else */if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
226 cancel = done = TRUE;
227 }
228 if (done) {
229 keyboard_interactive_end(s, e->xkey.state, cancel, e->xkey.time);
230
231 handled = TRUE;
232 } else
233 *client = s->client;
234 }
235
236 return handled;
237 }
238
239 void keyboard_event(ObClient *client, const XEvent *e)
240 {
241 KeyBindingTree *p;
242
243 g_assert(e->type == KeyPress);
244
245 if (e->xkey.keycode == config_keyboard_reset_keycode &&
246 e->xkey.state == config_keyboard_reset_state)
247 {
248 keyboard_reset_chains();
249 return;
250 }
251
252 if (curpos == NULL)
253 p = keyboard_firstnode;
254 else
255 p = curpos->first_child;
256 while (p) {
257 if (p->key == e->xkey.keycode &&
258 p->state == e->xkey.state)
259 {
260 if (p->first_child != NULL) { /* part of a chain */
261 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
262 /* 5 second timeout for chains */
263 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
264 chain_timeout, NULL,
265 g_direct_equal, NULL);
266 grab_keys(FALSE);
267 curpos = p;
268 grab_keys(TRUE);
269 } else {
270
271 keyboard_reset_chains();
272
273 action_run_key(p->actions, client, e->xkey.state,
274 e->xkey.x_root, e->xkey.y_root,
275 e->xkey.time);
276 }
277 break;
278 }
279 p = p->next_sibling;
280 }
281 }
282
283 gboolean keyboard_interactively_grabbed()
284 {
285 return !!interactive_states;
286 }
287
288 void keyboard_startup(gboolean reconfig)
289 {
290 grab_keys(TRUE);
291
292 if (!reconfig)
293 client_add_destructor(keyboard_interactive_end_client, NULL);
294 }
295
296 void keyboard_shutdown(gboolean reconfig)
297 {
298 GSList *it;
299
300 if (!reconfig)
301 client_remove_destructor(keyboard_interactive_end_client);
302
303 for (it = interactive_states; it; it = g_slist_next(it))
304 g_free(it->data);
305 g_slist_free(interactive_states);
306 interactive_states = NULL;
307
308 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
309
310 keyboard_unbind_all();
311 }
312
This page took 0.051975 seconds and 4 git commands to generate.