]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
783b6479c2b2d03cdea56828dc80f423346c2405
[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 #include <algorithm>
23
24 namespace ob {
25
26 static bool buttonvalue(const std::string &button, unsigned int *val)
27 {
28 if (button == "Left" || button == "1" || button == "Button1") {
29 *val |= Button1;
30 } else if (button == "Middle" || button == "2" || button == "Button2") {
31 *val |= Button2;
32 } else if (button == "Right" || button == "3" || button == "Button3") {
33 *val |= Button3;
34 } else if (button == "Up" || button == "4" || button == "Button4") {
35 *val |= Button4;
36 } else if (button == "Down" || button == "5" || button == "Button5") {
37 *val |= Button5;
38 } else
39 return false;
40 return true;
41 }
42
43 static bool modvalue(const std::string &mod, unsigned int *val)
44 {
45 if (mod == "C") { // control
46 *val |= ControlMask;
47 } else if (mod == "S") { // shift
48 *val |= ShiftMask;
49 } else if (mod == "A" || // alt/mod1
50 mod == "M" ||
51 mod == "Mod1" ||
52 mod == "M1") {
53 *val |= Mod1Mask;
54 } else if (mod == "Mod2" || // mod2
55 mod == "M2") {
56 *val |= Mod2Mask;
57 } else if (mod == "Mod3" || // mod3
58 mod == "M3") {
59 *val |= Mod3Mask;
60 } else if (mod == "W" || // windows/mod4
61 mod == "Mod4" ||
62 mod == "M4") {
63 *val |= Mod4Mask;
64 } else if (mod == "Mod5" || // mod5
65 mod == "M5") {
66 *val |= Mod5Mask;
67 } else { // invalid
68 return false;
69 }
70 return true;
71 }
72
73 bool Bindings::translate(const std::string &str, Binding &b,bool askey) const
74 {
75 // parse out the base key name
76 std::string::size_type keybegin = str.find_last_of('-');
77 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
78 std::string key(str, keybegin);
79
80 // parse out the requested modifier keys
81 unsigned int modval = 0;
82 std::string::size_type begin = 0, end;
83 while (begin != keybegin) {
84 end = str.find_first_of('-', begin);
85
86 std::string mod(str, begin, end-begin);
87 if (!modvalue(mod, &modval)) {
88 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
89 return false;
90 }
91
92 begin = end + 1;
93 }
94
95 // set the binding
96 b.modifiers = modval;
97 if (askey) {
98 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
99 if (sym == NoSymbol) {
100 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
101 return false;
102 }
103 if (!(b.key = XKeysymToKeycode(**otk::display, sym)))
104 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
105 return b.key != 0;
106 } else {
107 return buttonvalue(key, &b.key);
108 }
109 }
110
111 static void destroytree(KeyBindingTree *tree)
112 {
113 while (tree) {
114 KeyBindingTree *c = tree->first_child;
115 delete tree;
116 tree = c;
117 }
118 }
119
120 KeyBindingTree *Bindings::buildtree(const StringVect &keylist,
121 PyObject *callback) const
122 {
123 if (keylist.empty()) return 0; // nothing in the list.. return 0
124
125 KeyBindingTree *ret = 0, *p;
126
127 StringVect::const_reverse_iterator it, end = keylist.rend();
128 for (it = keylist.rbegin(); it != end; ++it) {
129 p = ret;
130 ret = new KeyBindingTree();
131 if (!p) {
132 // this is the first built node, the bottom node of the tree
133 ret->chain = false;
134 ret->callbacks.push_back(callback);
135 }
136 ret->first_child = p;
137 if (!translate(*it, ret->binding)) {
138 destroytree(ret);
139 ret = 0;
140 break;
141 }
142 }
143 return ret;
144 }
145
146
147 Bindings::Bindings()
148 : _curpos(&_keytree),
149 _resetkey(0,0),
150 _timer((otk::Timer *) 0),
151 _keybgrab_callback(0)
152 {
153 // setResetKey("C-g"); // set the default reset key
154 }
155
156
157 Bindings::~Bindings()
158 {
159 if (_timer)
160 delete _timer;
161
162 removeAllKeys();
163 //removeAllButtons(); // this is done by each client as they are unmanaged
164 removeAllEvents();
165 }
166
167
168 void Bindings::assimilate(KeyBindingTree *node)
169 {
170 KeyBindingTree *a, *b, *tmp, *last;
171
172 if (!_keytree.first_child) {
173 // there are no nodes at this level yet
174 _keytree.first_child = node;
175 } else {
176 a = _keytree.first_child;
177 last = a;
178 b = node;
179 while (a) {
180 last = a;
181 if (a->binding != b->binding) {
182 a = a->next_sibling;
183 } else {
184 tmp = b;
185 b = b->first_child;
186 delete tmp;
187 a = a->first_child;
188 }
189 }
190 if (last->binding != b->binding)
191 last->next_sibling = b;
192 else {
193 last->first_child = b->first_child;
194 delete b;
195 }
196 }
197 }
198
199
200 KeyBindingTree *Bindings::find(KeyBindingTree *search,
201 bool *conflict) const {
202 *conflict = false;
203 KeyBindingTree *a, *b;
204 a = _keytree.first_child;
205 b = search;
206 while (a && b) {
207 if (a->binding != b->binding) {
208 a = a->next_sibling;
209 } else {
210 if (a->chain == b->chain) {
211 if (!a->chain) {
212 // found it! (return the actual id, not the search's)
213 return a;
214 }
215 } else {
216 *conflict = true;
217 return 0; // the chain status' don't match (conflict!)
218 }
219 b = b->first_child;
220 a = a->first_child;
221 }
222 }
223 return 0; // it just isn't in here
224 }
225
226
227 bool Bindings::addKey(const StringVect &keylist, PyObject *callback)
228 {
229 KeyBindingTree *tree, *t;
230 bool conflict;
231
232 if (!(tree = buildtree(keylist, callback)))
233 return false; // invalid binding requested
234
235 t = find(tree, &conflict);
236 if (conflict) {
237 // conflicts with another binding
238 destroytree(tree);
239 return false;
240 }
241
242 if (t) {
243 // already bound to something
244 // XXX: look if callback is already bound to this key?
245 t->callbacks.push_back(callback);
246 destroytree(tree);
247 } else {
248 // grab the server here to make sure no key pressed go missed
249 otk::display->grab();
250 grabKeys(false);
251
252 // assimilate this built tree into the main tree
253 assimilate(tree); // assimilation destroys/uses the tree
254
255 grabKeys(true);
256 otk::display->ungrab();
257 }
258
259 Py_INCREF(callback);
260
261 return true;
262 }
263
264
265 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
266 {
267 assert(false); // XXX: function not implemented yet
268
269 KeyBindingTree *tree;
270 bool conflict;
271
272 if (!(tree = buildtree(keylist, 0)))
273 return false; // invalid binding requested
274
275 KeyBindingTree *t = find(tree, &conflict);
276 if (t) {
277 CallbackList::iterator it = std::find(t->callbacks.begin(),
278 t->callbacks.end(),
279 callback);
280 if (it != t->callbacks.end()) {
281 // grab the server here to make sure no key pressed go missed
282 otk::display->grab();
283 grabKeys(false);
284
285 _curpos = &_keytree;
286
287 // XXX do shit here ...
288 Py_XDECREF(*it);
289
290 grabKeys(true);
291 otk::display->ungrab();
292
293 return true;
294 }
295 }
296 return false;
297 }
298
299
300 void Bindings::setResetKey(const std::string &key)
301 {
302 Binding b(0, 0);
303 if (translate(key, b)) {
304 // grab the server here to make sure no key pressed go missed
305 otk::display->grab();
306 grabKeys(false);
307 _resetkey.key = b.key;
308 _resetkey.modifiers = b.modifiers;
309 grabKeys(true);
310 otk::display->ungrab();
311 }
312 }
313
314
315 static void remove_branch(KeyBindingTree *first)
316 {
317 KeyBindingTree *p = first;
318
319 while (p) {
320 if (p->first_child)
321 remove_branch(p->first_child);
322 KeyBindingTree *s = p->next_sibling;
323 while(!p->callbacks.empty()) {
324 Py_XDECREF(p->callbacks.front());
325 p->callbacks.pop_front();
326 }
327 delete p;
328 p = s;
329 }
330 }
331
332
333 void Bindings::removeAllKeys()
334 {
335 grabKeys(false);
336 if (_keytree.first_child) {
337 remove_branch(_keytree.first_child);
338 _keytree.first_child = 0;
339 }
340 grabKeys(true);
341 }
342
343
344 void Bindings::grabKeys(bool grab)
345 {
346 for (int i = 0; i < ScreenCount(**otk::display); ++i) {
347 Screen *sc = openbox->screen(i);
348 if (!sc) continue; // not a managed screen
349 Window root = otk::display->screenInfo(i)->rootWindow();
350
351 KeyBindingTree *p = _curpos->first_child;
352 while (p) {
353 if (grab) {
354 otk::display->grabKey(p->binding.key, p->binding.modifiers,
355 root, false, GrabModeAsync, GrabModeAsync,
356 false);
357 }
358 else
359 otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
360 root);
361 p = p->next_sibling;
362 }
363
364 if (_resetkey.key)
365 if (grab)
366 otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
367 root, false, GrabModeAsync, GrabModeAsync,
368 false);
369 else
370 otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
371 root);
372 }
373 }
374
375
376 bool Bindings::grabKeyboard(int screen, PyObject *callback)
377 {
378 assert(callback);
379 if (_keybgrab_callback) return false; // already grabbed
380
381 if (!openbox->screen(screen))
382 return false; // the screen is not managed
383
384 Window root = otk::display->screenInfo(screen)->rootWindow();
385 if (XGrabKeyboard(**otk::display, root, false, GrabModeAsync,
386 GrabModeAsync, CurrentTime))
387 return false;
388 // the pointer grab causes pointer events during the keyboard grab to go away
389 XGrabPointer(**otk::display, root, false, 0, GrabModeAsync,
390 GrabModeAsync, None, None, CurrentTime);
391 _keybgrab_callback = callback;
392 return true;
393 }
394
395
396 void Bindings::ungrabKeyboard()
397 {
398 if (!_keybgrab_callback) return; // not grabbed
399
400 _keybgrab_callback = 0;
401 XUngrabKeyboard(**otk::display, CurrentTime);
402 XUngrabPointer(**otk::display, CurrentTime);
403 }
404
405
406 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
407 Time time, KeyAction::KA action)
408 {
409 if (_keybgrab_callback) {
410 Client *c = openbox->focusedClient();
411 KeyData data(screen, c, time, modifiers, key, action);
412 python_callback(_keybgrab_callback, &data);
413 }
414
415 // KeyRelease events only occur during keyboard grabs
416 if (action == KeyAction::Release) return;
417
418 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
419 resetChains(this);
420 } else {
421 KeyBindingTree *p = _curpos->first_child;
422 while (p) {
423 if (p->binding.key == key && p->binding.modifiers == modifiers) {
424 if (p->chain) {
425 if (_timer)
426 delete _timer;
427 _timer = new otk::Timer(5000, // 5 second timeout
428 (otk::Timer::TimeoutHandler)resetChains,
429 this);
430 // grab the server here to make sure no key pressed go missed
431 otk::display->grab();
432 grabKeys(false);
433 _curpos = p;
434 grabKeys(true);
435 otk::display->ungrab();
436 } else {
437 Client *c = openbox->focusedClient();
438 KeyData data(screen, c, time, modifiers, key, action);
439 CallbackList::iterator it, end = p->callbacks.end();
440 for (it = p->callbacks.begin(); it != end; ++it)
441 python_callback(*it, &data);
442 resetChains(this);
443 }
444 break;
445 }
446 p = p->next_sibling;
447 }
448 }
449 }
450
451 void Bindings::resetChains(Bindings *self)
452 {
453 if (self->_timer) {
454 delete self->_timer;
455 self->_timer = (otk::Timer *) 0;
456 }
457 // grab the server here to make sure no key pressed go missed
458 otk::display->grab();
459 self->grabKeys(false);
460 self->_curpos = &self->_keytree;
461 self->grabKeys(true);
462 otk::display->ungrab();
463 }
464
465
466 bool Bindings::addButton(const std::string &but, MouseContext::MC context,
467 MouseAction::MA action, PyObject *callback)
468 {
469 assert(context >= 0 && context < MouseContext::NUM_MOUSE_CONTEXT);
470
471 Binding b(0,0);
472 if (!translate(but, b, false))
473 return false;
474
475 ButtonBindingList::iterator it, end = _buttons[context].end();
476
477 // look for a duplicate binding
478 for (it = _buttons[context].begin(); it != end; ++it)
479 if ((*it)->binding.key == b.key &&
480 (*it)->binding.modifiers == b.modifiers) {
481 break;
482 }
483
484 ButtonBinding *bind;
485
486 // the binding didnt exist yet, add it
487 if (it == end) {
488 bind = new ButtonBinding();
489 bind->binding.key = b.key;
490 bind->binding.modifiers = b.modifiers;
491 _buttons[context].push_back(bind);
492 // grab the button on all clients
493 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
494 Screen *s = openbox->screen(sn);
495 if (!s) continue; // not managed
496 Client::List::iterator c_it, c_end = s->clients.end();
497 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
498 grabButton(true, bind->binding, context, *c_it);
499 }
500 }
501 } else
502 bind = *it;
503 bind->callbacks[action].push_back(callback);
504 Py_INCREF(callback);
505 return true;
506 }
507
508 void Bindings::removeAllButtons()
509 {
510 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
511 ButtonBindingList::iterator it, end = _buttons[i].end();
512 for (it = _buttons[i].begin(); it != end; ++it) {
513 for (int a = 0; a < MouseAction::NUM_MOUSE_ACTION; ++a) {
514 while (!(*it)->callbacks[a].empty()) {
515 Py_XDECREF((*it)->callbacks[a].front());
516 (*it)->callbacks[a].pop_front();
517 }
518 }
519 // ungrab the button on all clients
520 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
521 Screen *s = openbox->screen(sn);
522 if (!s) continue; // not managed
523 Client::List::iterator c_it, c_end = s->clients.end();
524 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
525 grabButton(false, (*it)->binding, (MouseContext::MC)i, *c_it);
526 }
527 }
528 }
529 }
530 }
531
532 void Bindings::grabButton(bool grab, const Binding &b,
533 MouseContext::MC context, Client *client)
534 {
535 Window win;
536 int mode = GrabModeAsync;
537 unsigned int mask;
538 switch(context) {
539 case MouseContext::Frame:
540 win = client->frame->window();
541 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
542 break;
543 case MouseContext::Window:
544 win = client->frame->plate();
545 mode = GrabModeSync; // this is handled in fireButton
546 mask = ButtonPressMask; // can't catch more than this with Sync mode
547 // the release event is manufactured by the
548 // master buttonPressHandler
549 break;
550 default:
551 // any other elements already get button events, don't grab on them
552 return;
553 }
554 if (grab)
555 otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
556 GrabModeAsync, None, None, false);
557 else
558 otk::display->ungrabButton(b.key, b.modifiers, win);
559 }
560
561 void Bindings::grabButtons(bool grab, Client *client)
562 {
563 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
564 ButtonBindingList::iterator it, end = _buttons[i].end();
565 for (it = _buttons[i].begin(); it != end; ++it)
566 grabButton(grab, (*it)->binding, (MouseContext::MC)i, client);
567 }
568 }
569
570 void Bindings::fireButton(MouseData *data)
571 {
572 if (data->context == MouseContext::Window) {
573 // Replay the event, so it goes to the client
574 XAllowEvents(**otk::display, ReplayPointer, data->time);
575 }
576
577 ButtonBindingList::iterator it, end = _buttons[data->context].end();
578 for (it = _buttons[data->context].begin(); it != end; ++it)
579 if ((*it)->binding.key == data->button &&
580 (*it)->binding.modifiers == data->state) {
581 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
582 for (c_it = (*it)->callbacks[data->action].begin();
583 c_it != c_end; ++c_it)
584 python_callback(*c_it, data);
585 }
586 }
587
588
589 bool Bindings::addEvent(EventAction::EA action, PyObject *callback)
590 {
591 if (action < 0 || action >= EventAction::NUM_EVENTS) {
592 return false;
593 }
594 #ifdef XKB
595 if (action == EventAction::Bell && _eventlist[action].empty())
596 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
597 XkbBellNotifyMask, XkbBellNotifyMask);
598 #endif // XKB
599 _eventlist[action].push_back(callback);
600 Py_INCREF(callback);
601 return true;
602 }
603
604 bool Bindings::removeEvent(EventAction::EA action, PyObject *callback)
605 {
606 if (action < 0 || action >= EventAction::NUM_EVENTS) {
607 return false;
608 }
609
610 CallbackList::iterator it = std::find(_eventlist[action].begin(),
611 _eventlist[action].end(),
612 callback);
613 if (it != _eventlist[action].end()) {
614 Py_XDECREF(*it);
615 _eventlist[action].erase(it);
616 #ifdef XKB
617 if (action == EventAction::Bell && _eventlist[action].empty())
618 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
619 XkbBellNotifyMask, 0);
620 #endif // XKB
621 return true;
622 }
623 return false;
624 }
625
626 void Bindings::removeAllEvents()
627 {
628 for (int i = 0; i < EventAction::NUM_EVENTS; ++i) {
629 while (!_eventlist[i].empty()) {
630 Py_XDECREF(_eventlist[i].front());
631 _eventlist[i].pop_front();
632 }
633 }
634 }
635
636 void Bindings::fireEvent(EventData *data)
637 {
638 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
639 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
640 python_callback(*c_it, data);
641 }
642
643 }
This page took 0.064622 seconds and 4 git commands to generate.