]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
save teh client for interactive actions cuz after teh keyboard is grabbed there is...
[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 #include "moveresize.h"
16
17 #include <glib.h>
18
19 KeyBindingTree *keyboard_firstnode;
20
21 typedef struct {
22 guint state;
23 ObClient *client;
24 ObAction *action;
25 ObFrameContext context;
26 } ObInteractiveState;
27
28 static GSList *interactive_states;
29
30 static KeyBindingTree *curpos;
31
32 static void grab_for_window(Window win, gboolean grab)
33 {
34 KeyBindingTree *p;
35
36 ungrab_all_keys(win);
37
38 if (grab) {
39 p = curpos ? curpos->first_child : keyboard_firstnode;
40 while (p) {
41 grab_key(p->key, p->state, win, GrabModeAsync);
42 p = p->next_sibling;
43 }
44 if (curpos)
45 grab_key(config_keyboard_reset_keycode,
46 config_keyboard_reset_state,
47 win, GrabModeAsync);
48 }
49 }
50
51 void keyboard_grab_for_client(ObClient *c, gboolean grab)
52 {
53 grab_for_window(c->window, grab);
54 }
55
56 static void grab_keys(gboolean grab)
57 {
58 GList *it;
59
60 grab_for_window(screen_support_win, grab);
61 for (it = client_list; it; it = g_list_next(it))
62 grab_for_window(((ObClient*)it->data)->window, grab);
63 }
64
65 static gboolean chain_timeout(gpointer data)
66 {
67 keyboard_reset_chains();
68
69 return FALSE; /* don't repeat */
70 }
71
72 void keyboard_reset_chains()
73 {
74 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
75
76 if (curpos) {
77 grab_keys(FALSE);
78 curpos = NULL;
79 grab_keys(TRUE);
80 }
81 }
82
83 gboolean keyboard_bind(GList *keylist, ObAction *action)
84 {
85 KeyBindingTree *tree, *t;
86 gboolean conflict;
87 gboolean mods = TRUE;
88
89 g_assert(keylist != NULL);
90 g_assert(action != NULL);
91
92 if (!(tree = tree_build(keylist)))
93 return FALSE;
94
95 if ((t = tree_find(tree, &conflict)) != NULL) {
96 /* already bound to something, use the existing tree */
97 tree_destroy(tree);
98 tree = NULL;
99 } else
100 t = tree;
101
102 if (conflict) {
103 g_warning("conflict with binding");
104 tree_destroy(tree);
105 return FALSE;
106 }
107
108 /* find if every key in this chain has modifiers, and also find the
109 bottom node of the tree */
110 while (t->first_child) {
111 if (!t->state)
112 mods = FALSE;
113 t = t->first_child;
114 }
115
116 /* when there are no modifiers in the binding, then the action cannot
117 be interactive */
118 if (!mods && action->data.any.interactive) {
119 action->data.any.interactive = FALSE;
120 action->data.inter.final = TRUE;
121 }
122
123 /* set the action */
124 t->actions = g_slist_append(t->actions, action);
125 /* assimilate this built tree into the main tree. assimilation
126 destroys/uses the tree */
127 if (tree) tree_assimilate(tree);
128
129 return TRUE;
130 }
131
132 void keyboard_interactive_grab(guint state, ObClient *client,
133 ObAction *action)
134 {
135 ObInteractiveState *s;
136
137 g_assert(action->data.any.interactive);
138
139 if (moveresize_in_progress)
140 moveresize_end(FALSE);
141
142 if (!interactive_states) {
143 if (!grab_keyboard(TRUE))
144 return;
145 if (!grab_pointer(TRUE, OB_CURSOR_NONE)) {
146 grab_keyboard(FALSE);
147 return;
148 }
149 }
150
151 s = g_new(ObInteractiveState, 1);
152
153 s->state = state;
154 s->client = client;
155 s->action = action;
156
157 interactive_states = g_slist_append(interactive_states, s);
158 }
159
160 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
161 {
162 GSList *it, *next;
163 gboolean handled = FALSE;
164 gboolean done = FALSE;
165 gboolean cancel = FALSE;
166
167 for (it = interactive_states; it; it = next) {
168 ObInteractiveState *s = it->data;
169
170 next = g_slist_next(it);
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 action_run_interactive(s->action, s->client,
183 e->xkey.state, cancel, TRUE);
184
185 g_free(s);
186
187 interactive_states = g_slist_delete_link(interactive_states, it);
188 if (!interactive_states) {
189 grab_keyboard(FALSE);
190 grab_pointer(FALSE, OB_CURSOR_NONE);
191 keyboard_reset_chains();
192 }
193
194 handled = TRUE;
195 } else
196 *client = s->client;
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 {
223 if (p->first_child != NULL) { /* part of a chain */
224 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
225 /* 5 second timeout for chains */
226 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
227 chain_timeout, NULL, NULL);
228 grab_keys(FALSE);
229 curpos = p;
230 grab_keys(TRUE);
231 } else {
232 GSList *it;
233
234 for (it = p->actions; it; it = it->next)
235 action_run_key(it->data, client, e->xkey.state,
236 e->xkey.x_root, e->xkey.y_root);
237
238 keyboard_reset_chains();
239 }
240 break;
241 }
242 p = p->next_sibling;
243 }
244 }
245
246 void keyboard_startup(gboolean reconfig)
247 {
248 grab_keys(TRUE);
249 }
250
251 void keyboard_shutdown(gboolean reconfig)
252 {
253 GSList *it;
254
255 tree_destroy(keyboard_firstnode);
256 keyboard_firstnode = NULL;
257
258 for (it = interactive_states; it; it = g_slist_next(it))
259 g_free(it->data);
260 g_slist_free(interactive_states);
261 interactive_states = NULL;
262
263 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
264 grab_keys(FALSE);
265 curpos = NULL;
266 }
267
This page took 0.049077 seconds and 5 git commands to generate.