]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
let the user remove client decorations.
[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 grabKeys(false);
163 removeAllKeys();
164 // removeAllButtons(); XXX
165 removeAllEvents();
166 }
167
168
169 void Bindings::assimilate(KeyBindingTree *node)
170 {
171 KeyBindingTree *a, *b, *tmp, *last;
172
173 if (!_keytree.first_child) {
174 // there are no nodes at this level yet
175 _keytree.first_child = node;
176 } else {
177 a = _keytree.first_child;
178 last = a;
179 b = node;
180 while (a) {
181 last = a;
182 if (a->binding != b->binding) {
183 a = a->next_sibling;
184 } else {
185 tmp = b;
186 b = b->first_child;
187 delete tmp;
188 a = a->first_child;
189 }
190 }
191 if (last->binding != b->binding)
192 last->next_sibling = b;
193 else {
194 last->first_child = b->first_child;
195 delete b;
196 }
197 }
198 }
199
200
201 KeyBindingTree *Bindings::find(KeyBindingTree *search,
202 bool *conflict) const {
203 *conflict = false;
204 KeyBindingTree *a, *b;
205 a = _keytree.first_child;
206 b = search;
207 while (a && b) {
208 if (a->binding != b->binding) {
209 a = a->next_sibling;
210 } else {
211 if (a->chain == b->chain) {
212 if (!a->chain) {
213 // found it! (return the actual id, not the search's)
214 return a;
215 }
216 } else {
217 *conflict = true;
218 return 0; // the chain status' don't match (conflict!)
219 }
220 b = b->first_child;
221 a = a->first_child;
222 }
223 }
224 return 0; // it just isn't in here
225 }
226
227
228 bool Bindings::addKey(const StringVect &keylist, PyObject *callback)
229 {
230 KeyBindingTree *tree, *t;
231 bool conflict;
232
233 if (!(tree = buildtree(keylist, callback)))
234 return false; // invalid binding requested
235
236 t = find(tree, &conflict);
237 if (conflict) {
238 // conflicts with another binding
239 destroytree(tree);
240 return false;
241 }
242
243 if (t) {
244 // already bound to something
245 // XXX: look if callback is already bound to this key?
246 t->callbacks.push_back(callback);
247 destroytree(tree);
248 } else {
249 // grab the server here to make sure no key pressed go missed
250 otk::display->grab();
251 grabKeys(false);
252
253 // assimilate this built tree into the main tree
254 assimilate(tree); // assimilation destroys/uses the tree
255
256 grabKeys(true);
257 otk::display->ungrab();
258 }
259
260 Py_INCREF(callback);
261
262 return true;
263 }
264
265
266 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
267 {
268 assert(false); // XXX: function not implemented yet
269
270 KeyBindingTree *tree;
271 bool conflict;
272
273 if (!(tree = buildtree(keylist, 0)))
274 return false; // invalid binding requested
275
276 KeyBindingTree *t = find(tree, &conflict);
277 if (t) {
278 CallbackList::iterator it = std::find(t->callbacks.begin(),
279 t->callbacks.end(),
280 callback);
281 if (it != t->callbacks.end()) {
282 // grab the server here to make sure no key pressed go missed
283 otk::display->grab();
284 grabKeys(false);
285
286 _curpos = &_keytree;
287
288 // XXX do shit here ...
289 Py_XDECREF(*it);
290
291 grabKeys(true);
292 otk::display->ungrab();
293
294 return true;
295 }
296 }
297 return false;
298 }
299
300
301 void Bindings::setResetKey(const std::string &key)
302 {
303 Binding b(0, 0);
304 if (translate(key, b)) {
305 // grab the server here to make sure no key pressed go missed
306 otk::display->grab();
307 grabKeys(false);
308 _resetkey.key = b.key;
309 _resetkey.modifiers = b.modifiers;
310 grabKeys(true);
311 otk::display->ungrab();
312 }
313 }
314
315
316 static void remove_branch(KeyBindingTree *first)
317 {
318 KeyBindingTree *p = first;
319
320 while (p) {
321 if (p->first_child)
322 remove_branch(p->first_child);
323 KeyBindingTree *s = p->next_sibling;
324 while(!p->callbacks.empty()) {
325 Py_XDECREF(p->callbacks.front());
326 p->callbacks.pop_front();
327 }
328 delete p;
329 p = s;
330 }
331 }
332
333
334 void Bindings::removeAllKeys()
335 {
336 grabKeys(false);
337 if (_keytree.first_child) {
338 remove_branch(_keytree.first_child);
339 _keytree.first_child = 0;
340 }
341 grabKeys(true);
342 }
343
344
345 void Bindings::grabKeys(bool grab)
346 {
347 for (int i = 0; i < ScreenCount(**otk::display); ++i) {
348 Screen *sc = openbox->screen(i);
349 if (!sc) continue; // not a managed screen
350 Window root = otk::display->screenInfo(i)->rootWindow();
351
352 KeyBindingTree *p = _curpos->first_child;
353 while (p) {
354 if (grab) {
355 otk::display->grabKey(p->binding.key, p->binding.modifiers,
356 root, false, GrabModeAsync, GrabModeAsync,
357 false);
358 }
359 else
360 otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
361 root);
362 p = p->next_sibling;
363 }
364
365 if (_resetkey.key)
366 if (grab)
367 otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
368 root, false, GrabModeAsync, GrabModeAsync,
369 false);
370 else
371 otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
372 root);
373 }
374 }
375
376
377 bool Bindings::grabKeyboard(int screen, PyObject *callback)
378 {
379 assert(callback);
380 if (_keybgrab_callback) return false; // already grabbed
381
382 if (!openbox->screen(screen))
383 return false; // the screen is not managed
384
385 Window root = otk::display->screenInfo(screen)->rootWindow();
386 if (XGrabKeyboard(**otk::display, root, false, GrabModeAsync,
387 GrabModeAsync, CurrentTime))
388 return false;
389 // the pointer grab causes pointer events during the keyboard grab to go away
390 XGrabPointer(**otk::display, root, false, 0, GrabModeAsync,
391 GrabModeAsync, None, None, CurrentTime);
392 _keybgrab_callback = callback;
393 return true;
394 }
395
396
397 void Bindings::ungrabKeyboard()
398 {
399 if (!_keybgrab_callback) return; // not grabbed
400
401 _keybgrab_callback = 0;
402 XUngrabKeyboard(**otk::display, CurrentTime);
403 XUngrabPointer(**otk::display, CurrentTime);
404 }
405
406
407 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
408 Time time, KeyAction::KA action)
409 {
410 if (_keybgrab_callback) {
411 Client *c = openbox->focusedClient();
412 KeyData data(screen, c, time, modifiers, key, action);
413 python_callback(_keybgrab_callback, &data);
414 }
415
416 // KeyRelease events only occur during keyboard grabs
417 if (action == KeyAction::Release) return;
418
419 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
420 resetChains(this);
421 } else {
422 KeyBindingTree *p = _curpos->first_child;
423 while (p) {
424 if (p->binding.key == key && p->binding.modifiers == modifiers) {
425 if (p->chain) {
426 if (_timer)
427 delete _timer;
428 _timer = new otk::Timer(5000, // 5 second timeout
429 (otk::Timer::TimeoutHandler)resetChains,
430 this);
431 // grab the server here to make sure no key pressed go missed
432 otk::display->grab();
433 grabKeys(false);
434 _curpos = p;
435 grabKeys(true);
436 otk::display->ungrab();
437 } else {
438 Client *c = openbox->focusedClient();
439 KeyData data(screen, c, time, modifiers, key, action);
440 CallbackList::iterator it, end = p->callbacks.end();
441 for (it = p->callbacks.begin(); it != end; ++it)
442 python_callback(*it, &data);
443 resetChains(this);
444 }
445 break;
446 }
447 p = p->next_sibling;
448 }
449 }
450 }
451
452 void Bindings::resetChains(Bindings *self)
453 {
454 if (self->_timer) {
455 delete self->_timer;
456 self->_timer = (otk::Timer *) 0;
457 }
458 // grab the server here to make sure no key pressed go missed
459 otk::display->grab();
460 self->grabKeys(false);
461 self->_curpos = &self->_keytree;
462 self->grabKeys(true);
463 otk::display->ungrab();
464 }
465
466
467 bool Bindings::addButton(const std::string &but, MouseContext::MC context,
468 MouseAction::MA action, PyObject *callback)
469 {
470 assert(context >= 0 && context < MouseContext::NUM_MOUSE_CONTEXT);
471
472 Binding b(0,0);
473 if (!translate(but, b, false))
474 return false;
475
476 ButtonBindingList::iterator it, end = _buttons[context].end();
477
478 // look for a duplicate binding
479 for (it = _buttons[context].begin(); it != end; ++it)
480 if ((*it)->binding.key == b.key &&
481 (*it)->binding.modifiers == b.modifiers) {
482 break;
483 }
484
485 ButtonBinding *bind;
486
487 // the binding didnt exist yet, add it
488 if (it == end) {
489 bind = new ButtonBinding();
490 bind->binding.key = b.key;
491 bind->binding.modifiers = b.modifiers;
492 _buttons[context].push_back(bind);
493 // grab the button on all clients
494 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
495 Screen *s = openbox->screen(sn);
496 if (!s) continue; // not managed
497 Client::List::iterator c_it, c_end = s->clients.end();
498 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
499 grabButton(true, bind->binding, context, *c_it);
500 }
501 }
502 } else
503 bind = *it;
504 bind->callbacks[action].push_back(callback);
505 Py_INCREF(callback);
506 return true;
507 }
508
509 void Bindings::removeAllButtons()
510 {
511 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
512 ButtonBindingList::iterator it, end = _buttons[i].end();
513 for (it = _buttons[i].begin(); it != end; ++it) {
514 for (int a = 0; a < MouseAction::NUM_MOUSE_ACTION; ++a) {
515 while (!(*it)->callbacks[a].empty()) {
516 Py_XDECREF((*it)->callbacks[a].front());
517 (*it)->callbacks[a].pop_front();
518 }
519 }
520 // ungrab the button on all clients
521 for (int sn = 0; sn < ScreenCount(**otk::display); ++sn) {
522 Screen *s = openbox->screen(sn);
523 if (!s) continue; // not managed
524 Client::List::iterator c_it, c_end = s->clients.end();
525 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
526 grabButton(false, (*it)->binding, (MouseContext::MC)i, *c_it);
527 }
528 }
529 }
530 }
531 }
532
533 void Bindings::grabButton(bool grab, const Binding &b,
534 MouseContext::MC context, Client *client)
535 {
536 Window win;
537 int mode = GrabModeAsync;
538 unsigned int mask;
539 switch(context) {
540 case MouseContext::Frame:
541 win = client->frame->window();
542 mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
543 break;
544 case MouseContext::Window:
545 win = client->frame->plate();
546 mode = GrabModeSync; // this is handled in fireButton
547 mask = ButtonPressMask; // can't catch more than this with Sync mode
548 // the release event is manufactured by the
549 // master buttonPressHandler
550 break;
551 default:
552 // any other elements already get button events, don't grab on them
553 return;
554 }
555 if (grab)
556 otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
557 GrabModeAsync, None, None, false);
558 else
559 otk::display->ungrabButton(b.key, b.modifiers, win);
560 }
561
562 void Bindings::grabButtons(bool grab, Client *client)
563 {
564 for (int i = 0; i < MouseContext::NUM_MOUSE_CONTEXT; ++i) {
565 ButtonBindingList::iterator it, end = _buttons[i].end();
566 for (it = _buttons[i].begin(); it != end; ++it)
567 grabButton(grab, (*it)->binding, (MouseContext::MC)i, client);
568 }
569 }
570
571 void Bindings::fireButton(MouseData *data)
572 {
573 if (data->context == MouseContext::Window) {
574 // Replay the event, so it goes to the client
575 XAllowEvents(**otk::display, ReplayPointer, data->time);
576 }
577
578 ButtonBindingList::iterator it, end = _buttons[data->context].end();
579 for (it = _buttons[data->context].begin(); it != end; ++it)
580 if ((*it)->binding.key == data->button &&
581 (*it)->binding.modifiers == data->state) {
582 CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
583 for (c_it = (*it)->callbacks[data->action].begin();
584 c_it != c_end; ++c_it)
585 python_callback(*c_it, data);
586 }
587 }
588
589
590 bool Bindings::addEvent(EventAction::EA action, PyObject *callback)
591 {
592 if (action < 0 || action >= EventAction::NUM_EVENTS) {
593 return false;
594 }
595 #ifdef XKB
596 if (action == EventAction::Bell && _eventlist[action].empty())
597 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
598 XkbBellNotifyMask, XkbBellNotifyMask);
599 #endif // XKB
600 _eventlist[action].push_back(callback);
601 Py_INCREF(callback);
602 return true;
603 }
604
605 bool Bindings::removeEvent(EventAction::EA action, PyObject *callback)
606 {
607 if (action < 0 || action >= EventAction::NUM_EVENTS) {
608 return false;
609 }
610
611 CallbackList::iterator it = std::find(_eventlist[action].begin(),
612 _eventlist[action].end(),
613 callback);
614 if (it != _eventlist[action].end()) {
615 Py_XDECREF(*it);
616 _eventlist[action].erase(it);
617 #ifdef XKB
618 if (action == EventAction::Bell && _eventlist[action].empty())
619 XkbSelectEvents(**otk::display, XkbUseCoreKbd,
620 XkbBellNotifyMask, 0);
621 #endif // XKB
622 return true;
623 }
624 return false;
625 }
626
627 void Bindings::removeAllEvents()
628 {
629 for (int i = 0; i < EventAction::NUM_EVENTS; ++i) {
630 while (!_eventlist[i].empty()) {
631 Py_XDECREF(_eventlist[i].front());
632 _eventlist[i].pop_front();
633 }
634 }
635 }
636
637 void Bindings::fireEvent(EventData *data)
638 {
639 CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
640 for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
641 python_callback(*c_it, data);
642 }
643
644 }
This page took 0.06503 seconds and 4 git commands to generate.