]> Dogcows Code - chaz/openbox/blob - openbox/keyboard.c
when the client for an interactive action is closed, kill the action
[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 void keyboard_interactive_end(ObInteractiveState *s,
161 guint state, gboolean cancel)
162 {
163 action_run_interactive(s->action, s->client, state, cancel, TRUE);
164
165 g_free(s);
166
167 interactive_states = g_slist_remove(interactive_states, s);
168
169 if (!interactive_states) {
170 grab_keyboard(FALSE);
171 grab_pointer(FALSE, OB_CURSOR_NONE);
172 keyboard_reset_chains();
173 }
174 }
175
176 void keyboard_interactive_end_client(gpointer data)
177 {
178 GSList *it, *next;
179 ObClient *c = data;
180
181 for (it = interactive_states; it; it = next) {
182 ObInteractiveState *s = it->data;
183
184 next = g_slist_next(it);
185
186 if (s->client == c)
187 keyboard_interactive_end(s, 0, FALSE);
188 }
189 }
190
191 gboolean keyboard_process_interactive_grab(const XEvent *e, ObClient **client)
192 {
193 GSList *it, *next;
194 gboolean handled = FALSE;
195 gboolean done = FALSE;
196 gboolean cancel = FALSE;
197
198 for (it = interactive_states; it; it = next) {
199 ObInteractiveState *s = it->data;
200
201 next = g_slist_next(it);
202
203 if ((e->type == KeyRelease &&
204 !(s->state & e->xkey.state)))
205 done = TRUE;
206 else if (e->type == KeyPress) {
207 if (e->xkey.keycode == ob_keycode(OB_KEY_RETURN))
208 done = TRUE;
209 else if (e->xkey.keycode == ob_keycode(OB_KEY_ESCAPE))
210 cancel = done = TRUE;
211 }
212 if (done) {
213 keyboard_interactive_end(s, e->xkey.state, cancel);
214
215 handled = TRUE;
216 } else
217 *client = s->client;
218 }
219
220 return handled;
221 }
222
223 void keyboard_event(ObClient *client, const XEvent *e)
224 {
225 KeyBindingTree *p;
226
227 g_assert(e->type == KeyPress);
228
229 if (e->xkey.keycode == config_keyboard_reset_keycode &&
230 e->xkey.state == config_keyboard_reset_state)
231 {
232 keyboard_reset_chains();
233 return;
234 }
235
236 if (curpos == NULL)
237 p = keyboard_firstnode;
238 else
239 p = curpos->first_child;
240 while (p) {
241 if (p->key == e->xkey.keycode &&
242 p->state == e->xkey.state)
243 {
244 if (p->first_child != NULL) { /* part of a chain */
245 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
246 /* 5 second timeout for chains */
247 ob_main_loop_timeout_add(ob_main_loop, 5 * G_USEC_PER_SEC,
248 chain_timeout, NULL, NULL);
249 grab_keys(FALSE);
250 curpos = p;
251 grab_keys(TRUE);
252 } else {
253 GSList *it;
254
255 for (it = p->actions; it; it = it->next)
256 action_run_key(it->data, client, e->xkey.state,
257 e->xkey.x_root, e->xkey.y_root);
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 if (!reconfig)
272 client_add_destructor(keyboard_interactive_end_client);
273 }
274
275 void keyboard_shutdown(gboolean reconfig)
276 {
277 GSList *it;
278
279 if (!reconfig)
280 client_remove_destructor(keyboard_interactive_end_client);
281
282 tree_destroy(keyboard_firstnode);
283 keyboard_firstnode = NULL;
284
285 for (it = interactive_states; it; it = g_slist_next(it))
286 g_free(it->data);
287 g_slist_free(interactive_states);
288 interactive_states = NULL;
289
290 ob_main_loop_timeout_remove(ob_main_loop, chain_timeout);
291 grab_keys(FALSE);
292 curpos = NULL;
293 }
294
This page took 0.046225 seconds and 5 git commands to generate.