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