]> icculus.org git repositories - mikachu/openbox.git/blob - src/bindings.cc
use the new non-static display
[mikachu/openbox.git] / 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 == "Left" || button == "1" || button == "Button1") {
27     *val |= Button1;
28   } else if (button == "Middle" || button == "2" || button == "Button2") {
29     *val |= Button2;
30   } else if (button == "Right" || button == "3" || button == "Button3") {
31     *val |= Button3;
32   } else if (button == "Up" || button == "4" || button == "Button4") {
33     *val |= Button4;
34   } else if (button == "Down" || 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 Bindings::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::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 *Bindings::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();
129     if (!p) {
130       // this is the first built node, the bottom node of the tree
131       ret->chain = false;
132       ret->callbacks.push_back(callback);
133     }
134     ret->first_child = p;
135     if (!translate(*it, ret->binding)) {
136       destroytree(ret);
137       ret = 0;
138       break;
139     }
140   }
141   return ret;
142 }
143
144
145 Bindings::Bindings()
146   : _curpos(&_keytree),
147     _resetkey(0,0),
148     _timer(openbox->timerManager(),
149            (otk::TimeoutHandler)resetChains, this)
150 {
151   _timer.setTimeout(5000); // chains reset after 5 seconds
152   
153 //  setResetKey("C-g"); // set the default reset key
154 }
155
156
157 Bindings::~Bindings()
158 {
159   grabKeys(false);
160   removeAllKeys();
161 //  removeAllButtons(); XXX
162   removeAllEvents();
163 }
164
165
166 void Bindings::assimilate(KeyBindingTree *node)
167 {
168   KeyBindingTree *a, *b, *tmp, *last;
169
170   if (!_keytree.first_child) {
171     // there are no nodes at this level yet
172     _keytree.first_child = node;
173   } else {
174     a = _keytree.first_child;
175     last = a;
176     b = node;
177     while (a) {
178       last = a;
179       if (a->binding != b->binding) {
180         a = a->next_sibling;
181       } else {
182         tmp = b;
183         b = b->first_child;
184         delete tmp;
185         a = a->first_child;
186       }
187     }
188     if (last->binding != b->binding)
189       last->next_sibling = b;
190     else {
191       last->first_child = b->first_child;
192       delete b;
193     }
194   }
195 }
196
197
198 KeyBindingTree *Bindings::find(KeyBindingTree *search,
199                                  bool *conflict) const {
200   *conflict = false;
201   KeyBindingTree *a, *b;
202   a = _keytree.first_child;
203   b = search;
204   while (a && b) {
205     if (a->binding != b->binding) {
206       a = a->next_sibling;
207     } else {
208       if (a->chain == b->chain) {
209         if (!a->chain) {
210           // found it! (return the actual id, not the search's)
211           return a;
212         }
213       } else {
214         *conflict = true;
215         return 0; // the chain status' don't match (conflict!)
216       }
217       b = b->first_child;
218       a = a->first_child;
219     }
220   }
221   return 0; // it just isn't in here
222 }
223
224
225 bool Bindings::addKey(const StringVect &keylist, PyObject *callback)
226 {
227   KeyBindingTree *tree, *t;
228   bool conflict;
229
230   if (!(tree = buildtree(keylist, callback)))
231     return false; // invalid binding requested
232
233   t = find(tree, &conflict);
234   if (conflict) {
235     // conflicts with another binding
236     destroytree(tree);
237     return false;
238   }
239
240   if (t) {
241     // already bound to something
242     // XXX: look if callback is already bound to this key?
243     t->callbacks.push_back(callback);
244     destroytree(tree);
245   } else {
246     // grab the server here to make sure no key pressed go missed
247     otk::display->grab();
248     grabKeys(false);
249
250     // assimilate this built tree into the main tree
251     assimilate(tree); // assimilation destroys/uses the tree
252
253     grabKeys(true); 
254     otk::display->ungrab();
255   }
256  
257   Py_INCREF(callback);
258
259   return true;
260 }
261
262
263 bool Bindings::removeKey(const StringVect &keylist, PyObject *callback)
264 {
265   assert(false); // XXX: function not implemented yet
266
267   KeyBindingTree *tree;
268   bool conflict;
269
270   if (!(tree = buildtree(keylist, 0)))
271     return false; // invalid binding requested
272
273   KeyBindingTree *t = find(tree, &conflict);
274   if (t) {
275     CallbackList::iterator it = std::find(t->callbacks.begin(),
276                                           t->callbacks.end(),
277                                           callback);
278     if (it != t->callbacks.end()) {
279       // grab the server here to make sure no key pressed go missed
280       otk::display->grab();
281       grabKeys(false);
282       
283       _curpos = &_keytree;
284       
285       // XXX do shit here ...
286       Py_XDECREF(*it);
287       
288       grabKeys(true);
289       otk::display->ungrab();
290       
291       return true;
292     }
293   }
294   return false;
295 }
296
297
298 void Bindings::setResetKey(const std::string &key)
299 {
300   Binding b(0, 0);
301   if (translate(key, b)) {
302     // grab the server here to make sure no key pressed go missed
303     otk::display->grab();
304     grabKeys(false);
305     _resetkey.key = b.key;
306     _resetkey.modifiers = b.modifiers;
307     grabKeys(true);
308     otk::display->ungrab();
309   }
310 }
311
312
313 static void remove_branch(KeyBindingTree *first)
314 {
315   KeyBindingTree *p = first;
316
317   while (p) {
318     if (p->first_child)
319       remove_branch(p->first_child);
320     KeyBindingTree *s = p->next_sibling;
321     while(!p->callbacks.empty()) {
322       Py_XDECREF(p->callbacks.front());
323       p->callbacks.pop_front();
324     }
325     delete p;
326     p = s;
327   }
328 }
329
330
331 void Bindings::removeAllKeys()
332 {
333   grabKeys(false);
334   if (_keytree.first_child) {
335     remove_branch(_keytree.first_child);
336     _keytree.first_child = 0;
337   }
338   grabKeys(true);
339 }
340
341
342 void Bindings::grabKeys(bool grab)
343 {
344   for (int i = 0; i < openbox->screenCount(); ++i) {
345     Window root = otk::display->screenInfo(i)->rootWindow();
346
347     KeyBindingTree *p = _curpos->first_child;
348     while (p) {
349       if (grab) {
350         otk::display->grabKey(p->binding.key, p->binding.modifiers,
351                                 root, false, GrabModeAsync, GrabModeAsync,
352                                 false);
353       }
354       else
355         otk::display->ungrabKey(p->binding.key, p->binding.modifiers,
356                                   root);
357       p = p->next_sibling;
358     }
359
360     if (_resetkey.key)
361       if (grab)
362         otk::display->grabKey(_resetkey.key, _resetkey.modifiers,
363                                 root, false, GrabModeAsync, GrabModeAsync,
364                                 false);
365       else
366         otk::display->ungrabKey(_resetkey.key, _resetkey.modifiers,
367                                   root);
368   }
369 }
370
371
372 void Bindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
373                          Time time)
374 {
375   if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
376     resetChains(this);
377   } else {
378     KeyBindingTree *p = _curpos->first_child;
379     while (p) {
380       if (p->binding.key == key && p->binding.modifiers == modifiers) {
381         if (p->chain) {
382           _timer.start(); // start/restart the timer
383           // grab the server here to make sure no key pressed go missed
384           otk::display->grab();
385           grabKeys(false);
386           _curpos = p;
387           grabKeys(true);
388           otk::display->ungrab();
389         } else {
390           Client *c = openbox->focusedClient();
391           KeyData data(screen, c, time, modifiers, key);
392           CallbackList::iterator it, end = p->callbacks.end();
393           for (it = p->callbacks.begin(); it != end; ++it)
394             python_callback(*it, &data);
395           resetChains(this);
396         }
397         break;
398       }
399       p = p->next_sibling;
400     }
401   }
402 }
403
404 void Bindings::resetChains(Bindings *self)
405 {
406   self->_timer.stop();
407   // grab the server here to make sure no key pressed go missed
408   otk::display->grab();
409   self->grabKeys(false);
410   self->_curpos = &self->_keytree;
411   self->grabKeys(true);
412   otk::display->ungrab();
413 }
414
415
416 bool Bindings::addButton(const std::string &but, MouseContext context,
417                            MouseAction action, PyObject *callback)
418 {
419   assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
420   
421   Binding b(0,0);
422   if (!translate(but, b, false))
423     return false;
424
425   ButtonBindingList::iterator it, end = _buttons[context].end();
426
427   // look for a duplicate binding
428   for (it = _buttons[context].begin(); it != end; ++it)
429     if ((*it)->binding.key == b.key &&
430         (*it)->binding.modifiers == b.modifiers) {
431       break;
432     }
433
434   ButtonBinding *bind;
435   
436   // the binding didnt exist yet, add it
437   if (it == end) {
438     bind = new ButtonBinding();
439     bind->binding.key = b.key;
440     bind->binding.modifiers = b.modifiers;
441     _buttons[context].push_back(bind);
442     // grab the button on all clients
443     for (int sn = 0; sn < openbox->screenCount(); ++sn) {
444       Screen *s = openbox->screen(sn);
445       Client::List::iterator c_it, c_end = s->clients.end();
446       for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
447         grabButton(true, bind->binding, context, *c_it);
448       }
449     }
450   } else
451     bind = *it;
452   bind->callbacks[action].push_back(callback);
453   Py_INCREF(callback);
454   return true;
455 }
456
457 void Bindings::removeAllButtons()
458 {
459   for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
460     ButtonBindingList::iterator it, end = _buttons[i].end();
461     for (it = _buttons[i].begin(); it != end; ++it) {
462       for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
463         while (!(*it)->callbacks[a].empty()) {
464           Py_XDECREF((*it)->callbacks[a].front());
465           (*it)->callbacks[a].pop_front();
466         }
467       }
468       // ungrab the button on all clients
469       for (int sn = 0; sn < openbox->screenCount(); ++sn) {
470         Screen *s = openbox->screen(sn);
471         Client::List::iterator c_it, c_end = s->clients.end();
472         for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
473           grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
474         }
475       }
476     }
477   }
478 }
479
480 void Bindings::grabButton(bool grab, const Binding &b, MouseContext context,
481                             Client *client)
482 {
483   Window win;
484   int mode = GrabModeAsync;
485   unsigned int mask;
486   switch(context) {
487   case MC_Frame:
488     win = client->frame->window();
489     mask = ButtonPressMask | ButtonMotionMask | ButtonReleaseMask;
490     break;
491   case MC_Window:
492     win = client->frame->plate();
493     mode = GrabModeSync; // this is handled in fireButton
494     mask = ButtonPressMask; // can't catch more than this with Sync mode
495                             // the release event is manufactured by the
496                             // master buttonPressHandler
497     break;
498   default:
499     // any other elements already get button events, don't grab on them
500     return;
501   }
502   if (grab)
503     otk::display->grabButton(b.key, b.modifiers, win, false, mask, mode,
504                              GrabModeAsync, None, None, false);
505   else
506     otk::display->ungrabButton(b.key, b.modifiers, win);
507 }
508
509 void Bindings::grabButtons(bool grab, Client *client)
510 {
511   for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
512     ButtonBindingList::iterator it, end = _buttons[i].end();
513     for (it = _buttons[i].begin(); it != end; ++it)
514       grabButton(grab, (*it)->binding, (MouseContext)i, client);
515   }
516 }
517
518 void Bindings::fireButton(MouseData *data)
519 {
520   if (data->context == MC_Window) {
521     // Replay the event, so it goes to the client, and ungrab the device.
522     XAllowEvents(**otk::display, ReplayPointer, data->time);
523   }
524   
525   ButtonBindingList::iterator it, end = _buttons[data->context].end();
526   for (it = _buttons[data->context].begin(); it != end; ++it)
527     if ((*it)->binding.key == data->button &&
528         (*it)->binding.modifiers == data->state) {
529       CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
530       for (c_it = (*it)->callbacks[data->action].begin();
531            c_it != c_end; ++c_it)
532         python_callback(*c_it, data);
533     }
534 }
535
536
537 bool Bindings::addEvent(EventAction action, PyObject *callback)
538 {
539   if (action < 0 || action >= NUM_EVENTS) {
540     return false;
541   }
542 #ifdef    XKB
543   if (action == EventBell && _eventlist[action].empty())
544     XkbSelectEvents(**otk::display, XkbUseCoreKbd,
545                     XkbBellNotifyMask, XkbBellNotifyMask);
546 #endif // XKB
547   _eventlist[action].push_back(callback);
548   Py_INCREF(callback);
549   return true;
550 }
551
552 bool Bindings::removeEvent(EventAction action, PyObject *callback)
553 {
554   if (action < 0 || action >= NUM_EVENTS) {
555     return false;
556   }
557   
558   CallbackList::iterator it = std::find(_eventlist[action].begin(),
559                                         _eventlist[action].end(),
560                                         callback);
561   if (it != _eventlist[action].end()) {
562     Py_XDECREF(*it);
563     _eventlist[action].erase(it);
564 #ifdef    XKB
565     if (action == EventBell && _eventlist[action].empty())
566       XkbSelectEvents(**otk::display, XkbUseCoreKbd,
567                       XkbBellNotifyMask, 0);
568 #endif // XKB
569     return true;
570   }
571   return false;
572 }
573
574 void Bindings::removeAllEvents()
575 {
576   for (int i = 0; i < NUM_EVENTS; ++i) {
577     while (!_eventlist[i].empty()) {
578       Py_XDECREF(_eventlist[i].front());
579       _eventlist[i].pop_front();
580     }
581   }
582 }
583
584 void Bindings::fireEvent(EventData *data)
585 {
586   CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
587   for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
588     python_callback(*c_it, data);
589 }
590
591 }