]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
3df98da92056b77b320bb85f57b3331aeb487e52
[chaz/openbox] / src / bindings.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "bindings.hh"
8 #include "screen.hh"
9 #include "openbox.hh"
10 #include "client.hh"
11 #include "frame.hh"
12 #include "python.hh"
13 #include "otk/display.hh"
14
15 extern "C" {
16 #include <X11/Xlib.h>
17
18 #include "gettext.h"
19 #define _(str) gettext(str)
20 }
21
22 namespace ob {
23
24 static bool buttonvalue(const std::string &button, unsigned int *val)
25 {
26 if (button == "1" || button == "Button1") {
27 *val |= Button1;
28 } else if (button == "2" || button == "Button2") {
29 *val |= Button2;
30 } else if (button == "3" || button == "Button3") {
31 *val |= Button3;
32 } else if (button == "4" || button == "Button4") {
33 *val |= Button4;
34 } else if (button == "5" || button == "Button5") {
35 *val |= Button5;
36 } else
37 return false;
38 return true;
39 }
40
41 static bool modvalue(const std::string &mod, unsigned int *val)
42 {
43 if (mod == "C") { // control
44 *val |= ControlMask;
45 } else if (mod == "S") { // shift
46 *val |= ShiftMask;
47 } else if (mod == "A" || // alt/mod1
48 mod == "M" ||
49 mod == "Mod1" ||
50 mod == "M1") {
51 *val |= Mod1Mask;
52 } else if (mod == "Mod2" || // mod2
53 mod == "M2") {
54 *val |= Mod2Mask;
55 } else if (mod == "Mod3" || // mod3
56 mod == "M3") {
57 *val |= Mod3Mask;
58 } else if (mod == "W" || // windows/mod4
59 mod == "Mod4" ||
60 mod == "M4") {
61 *val |= Mod4Mask;
62 } else if (mod == "Mod5" || // mod5
63 mod == "M5") {
64 *val |= Mod5Mask;
65 } else { // invalid
66 return false;
67 }
68 return true;
69 }
70
71 bool OBBindings::translate(const std::string &str, Binding &b,bool askey) const
72 {
73 // parse out the base key name
74 std::string::size_type keybegin = str.find_last_of('-');
75 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
76 std::string key(str, keybegin);
77
78 // parse out the requested modifier keys
79 unsigned int modval = 0;
80 std::string::size_type begin = 0, end;
81 while (begin != keybegin) {
82 end = str.find_first_of('-', begin);
83
84 std::string mod(str, begin, end-begin);
85 if (!modvalue(mod, &modval)) {
86 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
87 return false;
88 }
89
90 begin = end + 1;
91 }
92
93 // set the binding
94 b.modifiers = modval;
95 if (askey) {
96 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
97 if (sym == NoSymbol) {
98 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
99 return false;
100 }
101 if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
102 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
103 return b.key != 0;
104 } else {
105 return buttonvalue(key, &b.key);
106 }
107 }
108
109 static void destroytree(KeyBindingTree *tree)
110 {
111 while (tree) {
112 KeyBindingTree *c = tree->first_child;
113 delete tree;
114 tree = c;
115 }
116 }
117
118 KeyBindingTree *OBBindings::buildtree(const StringVect &keylist,
119 PyObject *callback) const
120 {
121 if (keylist.empty()) return 0; // nothing in the list.. return 0
122
123 KeyBindingTree *ret = 0, *p;
124
125 StringVect::const_reverse_iterator it, end = keylist.rend();
126 for (it = keylist.rbegin(); it != end; ++it) {
127 p = ret;
128 ret = new KeyBindingTree(callback);
129 if (!p) ret->chain = false; // only the first built node
130 ret->first_child = p;
131 if (!translate(*it, ret->binding)) {
132 destroytree(ret);
133 ret = 0;
134 break;
135 }
136 }
137 return ret;
138 }
139
140
141 OBBindings::OBBindings()
142 : _curpos(&_keytree),
143 _resetkey(0,0),
144 _timer(Openbox::instance->timerManager(),
145 (otk::OBTimeoutHandler)resetChains, this)
146 {
147 _timer.setTimeout(5000); // chains reset after 5 seconds
148
149 setResetKey("C-g"); // set the default reset key
150
151 for (int i = 0; i < NUM_EVENTS; ++i)
152 _events[i] = 0;
153 }
154
155
156 OBBindings::~OBBindings()
157 {
158 grabKeys(false);
159 removeAllKeys();
160 removeAllButtons();
161 removeAllEvents();
162 }
163
164
165 void OBBindings::assimilate(KeyBindingTree *node)
166 {
167 KeyBindingTree *a, *b, *tmp, *last;
168
169 if (!_keytree.first_child) {
170 // there are no nodes at this level yet
171 _keytree.first_child = node;
172 } else {
173 a = _keytree.first_child;
174 last = a;
175 b = node;
176 while (a) {
177 last = a;
178 if (a->binding != b->binding) {
179 a = a->next_sibling;
180 } else {
181 tmp = b;
182 b = b->first_child;
183 delete tmp;
184 a = a->first_child;
185 }
186 }
187 if (last->binding != b->binding)
188 last->next_sibling = b;
189 else {
190 last->first_child = b->first_child;
191 delete b;
192 }
193 }
194 }
195
196
197 PyObject *OBBindings::find(KeyBindingTree *search, bool *conflict) const {
198 *conflict = false;
199 KeyBindingTree *a, *b;
200 a = _keytree.first_child;
201 b = search;
202 while (a && b) {
203 if (a->binding != b->binding) {
204 a = a->next_sibling;
205 } else {
206 if (a->chain == b->chain) {
207 if (!a->chain) {
208 // found it! (return the actual id, not the search's)
209 return a->callback;
210 }
211 } else {
212 *conflict = true;
213 return 0; // the chain status' don't match (conflict!)
214 }
215 b = b->first_child;
216 a = a->first_child;
217 }
218 }
219 return 0; // it just isn't in here
220 }
221
222
223 bool OBBindings::addKey(const StringVect &keylist, PyObject *callback)
224 {
225 KeyBindingTree *tree;
226 bool conflict;
227
228 if (!(tree = buildtree(keylist, callback)))
229 return false; // invalid binding requested
230
231 if (find(tree, &conflict) || conflict) {
232 // conflicts with another binding
233 destroytree(tree);
234 return false;
235 }
236
237 grabKeys(false);
238
239 // assimilate this built tree into the main tree
240 assimilate(tree); // assimilation destroys/uses the tree
241
242 Py_INCREF(callback);
243
244 grabKeys(true);
245
246 return true;
247 }
248
249
250 bool OBBindings::removeKey(const StringVect &keylist)
251 {
252 assert(false); // XXX: function not implemented yet
253
254 KeyBindingTree *tree;
255 bool conflict;
256
257 if (!(tree = buildtree(keylist, 0)))
258 return false; // invalid binding requested
259
260 PyObject *func = find(tree, &conflict);
261 if (func) {
262 grabKeys(false);
263
264 _curpos = &_keytree;
265
266 // XXX do shit here ...
267 Py_DECREF(func);
268
269 grabKeys(true);
270 return true;
271 }
272 return false;
273 }
274
275
276 void OBBindings::setResetKey(const std::string &key)
277 {
278 Binding b(0, 0);
279 if (translate(key, b)) {
280 grabKeys(false);
281 _resetkey.key = b.key;
282 _resetkey.modifiers = b.modifiers;
283 grabKeys(true);
284 }
285 }
286
287
288 static void remove_branch(KeyBindingTree *first)
289 {
290 KeyBindingTree *p = first;
291
292 while (p) {
293 if (p->first_child)
294 remove_branch(p->first_child);
295 KeyBindingTree *s = p->next_sibling;
296 Py_XDECREF(p->callback);
297 delete p;
298 p = s;
299 }
300 }
301
302
303 void OBBindings::removeAllKeys()
304 {
305 grabKeys(false);
306 if (_keytree.first_child) {
307 remove_branch(_keytree.first_child);
308 _keytree.first_child = 0;
309 }
310 grabKeys(true);
311 }
312
313
314 void OBBindings::grabKeys(bool grab)
315 {
316 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
317 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
318
319 KeyBindingTree *p = _curpos->first_child;
320 while (p) {
321 if (grab) {
322 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
323 root, false, GrabModeAsync, GrabModeAsync,
324 false);
325 }
326 else
327 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
328 root);
329 p = p->next_sibling;
330 }
331
332 if (grab)
333 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
334 root, true, GrabModeAsync, GrabModeAsync,
335 false);
336 else
337 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
338 root);
339 }
340 }
341
342
343 void OBBindings::fireKey(unsigned int modifiers, unsigned int key, Time time)
344 {
345 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
346 resetChains(this);
347 } else {
348 KeyBindingTree *p = _curpos->first_child;
349 while (p) {
350 if (p->binding.key == key && p->binding.modifiers == modifiers) {
351 if (p->chain) {
352 _timer.start(); // start/restart the timer
353 grabKeys(false);
354 _curpos = p;
355 grabKeys(true);
356 } else {
357 Window win = None;
358 OBClient *c = Openbox::instance->focusedClient();
359 if (c) win = c->window();
360 KeyData *data = new_key_data(win, time, modifiers, key);
361 python_callback(p->callback, (PyObject*)data);
362 Py_DECREF((PyObject*)data);
363 resetChains(this);
364 }
365 break;
366 }
367 p = p->next_sibling;
368 }
369 }
370 }
371
372 void OBBindings::resetChains(OBBindings *self)
373 {
374 self->_timer.stop();
375 self->grabKeys(false);
376 self->_curpos = &self->_keytree;
377 self->grabKeys(true);
378 }
379
380
381 bool OBBindings::addButton(const std::string &but, MouseContext context,
382 MouseAction action, PyObject *callback)
383 {
384 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
385
386 Binding b(0,0);
387 if (!translate(but, b, false))
388 return false;
389
390 ButtonBindingList::iterator it, end = _buttons[context].end();
391
392 // look for a duplicate binding
393 for (it = _buttons[context].begin(); it != end; ++it)
394 if ((*it)->binding.key == b.key &&
395 (*it)->binding.modifiers == b.modifiers) {
396 if ((*it)->callback[action] == callback)
397 return true; // already bound
398 break;
399 }
400
401 ButtonBinding *bind;
402
403 // the binding didnt exist yet, add it
404 if (it == end) {
405 bind = new ButtonBinding();
406 bind->binding.key = b.key;
407 bind->binding.modifiers = b.modifiers;
408 _buttons[context].push_back(bind);
409 // grab the button on all clients
410 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
411 OBScreen *s = Openbox::instance->screen(sn);
412 OBScreen::ClientList::iterator c_it, c_end = s->clients.end();
413 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
414 grabButton(true, bind->binding, context, *c_it);
415 }
416 }
417 } else
418 bind = *it;
419 Py_XDECREF(bind->callback[action]); // if it was already bound, unbind it
420 bind->callback[action] = callback;
421 Py_INCREF(callback);
422 return true;
423 }
424
425 void OBBindings::removeAllButtons()
426 {
427 for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
428 ButtonBindingList::iterator it, end = _buttons[i].end();
429 for (it = _buttons[i].begin(); it != end; ++it) {
430 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
431 Py_XDECREF((*it)->callback[a]);
432 (*it)->callback[a] = 0;
433 }
434 // ungrab the button on all clients
435 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
436 OBScreen *s = Openbox::instance->screen(sn);
437 OBScreen::ClientList::iterator c_it, c_end = s->clients.end();
438 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
439 grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
440 }
441 }
442 }
443 }
444 }
445
446 void OBBindings::grabButton(bool grab, const Binding &b, MouseContext context,
447 OBClient *client)
448 {
449 Window win;
450 int mode = GrabModeAsync;
451 switch(context) {
452 case MC_Frame:
453 win = client->frame->window();
454 break;
455 case MC_Window:
456 win = client->frame->plate();
457 mode = GrabModeSync; // this is handled in fireButton
458 break;
459 default:
460 // any other elements already get button events, don't grab on them
461 return;
462 }
463 if (grab)
464 otk::OBDisplay::grabButton(b.key, b.modifiers, win, false,
465 ButtonPressMask | ButtonMotionMask |
466 ButtonReleaseMask, mode, GrabModeAsync,
467 None, None, false);
468 else
469 otk::OBDisplay::ungrabButton(b.key, b.modifiers, win);
470 }
471
472 void OBBindings::grabButtons(bool grab, OBClient *client)
473 {
474 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
475 ButtonBindingList::iterator it, end = _buttons[i].end();
476 for (it = _buttons[i].begin(); it != end; ++it)
477 grabButton(grab, (*it)->binding, (MouseContext)i, client);
478 }
479 }
480
481 void OBBindings::fireButton(ButtonData *data)
482 {
483 printf("but.mods %d.%d\n", data->button, data->state);
484
485 if (data->context == MC_Window) {
486 // these are grabbed in Sync mode to allow the press to be normal to the
487 // client
488 XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
489 }
490
491 ButtonBindingList::iterator it, end = _buttons[data->context].end();
492 for (it = _buttons[data->context].begin(); it != end; ++it)
493 if ((*it)->binding.key == data->button &&
494 (*it)->binding.modifiers == data->state) {
495 if ((*it)->callback[data->action])
496 python_callback((*it)->callback[data->action], (PyObject*)data);
497 }
498 }
499
500
501 bool OBBindings::addEvent(EventAction action, PyObject *callback)
502 {
503 if (action < 0 || action >= NUM_EVENTS) {
504 return false;
505 }
506
507 Py_XDECREF(_events[action]);
508 _events[action] = callback;
509 Py_INCREF(callback);
510 return true;
511 }
512
513 bool OBBindings::removeEvent(EventAction action)
514 {
515 if (action < 0 || action >= NUM_EVENTS) {
516 return false;
517 }
518
519 Py_XDECREF(_events[action]);
520 _events[action] = 0;
521 return true;
522 }
523
524 void OBBindings::removeAllEvents()
525 {
526 for (int i = 0; i < NUM_EVENTS; ++i) {
527 Py_XDECREF(_events[i]);
528 _events[i] = 0;
529 }
530 }
531
532 void OBBindings::fireEvent(EventData *data)
533 {
534 if (_events[data->action])
535 python_callback(_events[data->action], (PyObject*)data);
536 }
537
538 }
This page took 0.060151 seconds and 4 git commands to generate.