]> icculus.org git repositories - mikachu/openbox.git/blob - src/bindings.cc
supply python routines for next/prev workspace
[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 == "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();
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 OBBindings::OBBindings()
146   : _curpos(&_keytree),
147     _resetkey(0,0),
148     _timer(Openbox::instance->timerManager(),
149            (otk::OBTimeoutHandler)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 OBBindings::~OBBindings()
158 {
159   grabKeys(false);
160   removeAllKeys();
161   removeAllButtons();
162   removeAllEvents();
163 }
164
165
166 void OBBindings::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 *OBBindings::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 OBBindings::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     grabKeys(false);
247
248     // assimilate this built tree into the main tree
249     assimilate(tree); // assimilation destroys/uses the tree
250
251     grabKeys(true); 
252   }
253  
254   Py_INCREF(callback);
255
256   return true;
257 }
258
259
260 bool OBBindings::removeKey(const StringVect &keylist, PyObject *callback)
261 {
262   assert(false); // XXX: function not implemented yet
263
264   KeyBindingTree *tree;
265   bool conflict;
266
267   if (!(tree = buildtree(keylist, 0)))
268     return false; // invalid binding requested
269
270   KeyBindingTree *t = find(tree, &conflict);
271   if (t) {
272     CallbackList::iterator it = std::find(t->callbacks.begin(),
273                                           t->callbacks.end(),
274                                           callback);
275     if (it != t->callbacks.end()) {
276       grabKeys(false);
277       
278       _curpos = &_keytree;
279       
280       // XXX do shit here ...
281       Py_XDECREF(*it);
282       
283       grabKeys(true);
284       return true;
285     }
286   }
287   return false;
288 }
289
290
291 void OBBindings::setResetKey(const std::string &key)
292 {
293   Binding b(0, 0);
294   if (translate(key, b)) {
295     grabKeys(false);
296     _resetkey.key = b.key;
297     _resetkey.modifiers = b.modifiers;
298     grabKeys(true);
299   }
300 }
301
302
303 static void remove_branch(KeyBindingTree *first)
304 {
305   KeyBindingTree *p = first;
306
307   while (p) {
308     if (p->first_child)
309       remove_branch(p->first_child);
310     KeyBindingTree *s = p->next_sibling;
311     while(!p->callbacks.empty()) {
312       Py_XDECREF(p->callbacks.front());
313       p->callbacks.pop_front();
314     }
315     delete p;
316     p = s;
317   }
318 }
319
320
321 void OBBindings::removeAllKeys()
322 {
323   grabKeys(false);
324   if (_keytree.first_child) {
325     remove_branch(_keytree.first_child);
326     _keytree.first_child = 0;
327   }
328   grabKeys(true);
329 }
330
331
332 void OBBindings::grabKeys(bool grab)
333 {
334   for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
335     Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
336
337     KeyBindingTree *p = _curpos->first_child;
338     while (p) {
339       if (grab) {
340         otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
341                                 root, false, GrabModeAsync, GrabModeAsync,
342                                 false);
343       }
344       else
345         otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
346                                   root);
347       p = p->next_sibling;
348     }
349
350     if (_resetkey.key)
351       if (grab)
352         otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
353                                 root, false, GrabModeAsync, GrabModeAsync,
354                                 false);
355       else
356         otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
357                                   root);
358   }
359 }
360
361
362 void OBBindings::fireKey(int screen, unsigned int modifiers, unsigned int key,
363                          Time time)
364 {
365   if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
366     resetChains(this);
367   } else {
368     KeyBindingTree *p = _curpos->first_child;
369     while (p) {
370       if (p->binding.key == key && p->binding.modifiers == modifiers) {
371         if (p->chain) {
372           _timer.start(); // start/restart the timer
373           grabKeys(false);
374           _curpos = p;
375           grabKeys(true);
376         } else {
377           Window win = None;
378           OBClient *c = Openbox::instance->focusedClient();
379           if (c) win = c->window();
380           KeyData *data = new_key_data(screen, win, time, modifiers, key);
381           CallbackList::iterator it, end = p->callbacks.end();
382           for (it = p->callbacks.begin(); it != end; ++it)
383             python_callback(*it, (PyObject*)data);
384           Py_XDECREF((PyObject*)data);
385           resetChains(this);
386         }
387         break;
388       }
389       p = p->next_sibling;
390     }
391   }
392 }
393
394 void OBBindings::resetChains(OBBindings *self)
395 {
396   self->_timer.stop();
397   self->grabKeys(false);
398   self->_curpos = &self->_keytree;
399   self->grabKeys(true);
400 }
401
402
403 bool OBBindings::addButton(const std::string &but, MouseContext context,
404                            MouseAction action, PyObject *callback)
405 {
406   assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
407   
408   Binding b(0,0);
409   if (!translate(but, b, false))
410     return false;
411
412   ButtonBindingList::iterator it, end = _buttons[context].end();
413
414   // look for a duplicate binding
415   for (it = _buttons[context].begin(); it != end; ++it)
416     if ((*it)->binding.key == b.key &&
417         (*it)->binding.modifiers == b.modifiers) {
418       break;
419     }
420
421   ButtonBinding *bind;
422   
423   // the binding didnt exist yet, add it
424   if (it == end) {
425     bind = new ButtonBinding();
426     bind->binding.key = b.key;
427     bind->binding.modifiers = b.modifiers;
428     _buttons[context].push_back(bind);
429     // grab the button on all clients
430     for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
431       OBScreen *s = Openbox::instance->screen(sn);
432       OBClient::List::iterator c_it, c_end = s->clients.end();
433       for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
434         grabButton(true, bind->binding, context, *c_it);
435       }
436     }
437   } else
438     bind = *it;
439   bind->callbacks[action].push_back(callback);
440   Py_INCREF(callback);
441   return true;
442 }
443
444 void OBBindings::removeAllButtons()
445 {
446   for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
447     ButtonBindingList::iterator it, end = _buttons[i].end();
448     for (it = _buttons[i].begin(); it != end; ++it) {
449       for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
450         while (!(*it)->callbacks[a].empty()) {
451           Py_XDECREF((*it)->callbacks[a].front());
452           (*it)->callbacks[a].pop_front();
453         }
454       }
455       // ungrab the button on all clients
456       for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
457         OBScreen *s = Openbox::instance->screen(sn);
458         OBClient::List::iterator c_it, c_end = s->clients.end();
459         for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
460           grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
461         }
462       }
463     }
464   }
465 }
466
467 void OBBindings::grabButton(bool grab, const Binding &b, MouseContext context,
468                             OBClient *client)
469 {
470   Window win;
471   int mode = GrabModeAsync;
472   switch(context) {
473   case MC_Frame:
474     win = client->frame->window();
475     break;
476   case MC_Window:
477     win = client->frame->plate();
478     mode = GrabModeSync; // this is handled in fireButton
479     break;
480   default:
481     // any other elements already get button events, don't grab on them
482     return;
483   }
484   if (grab)
485     otk::OBDisplay::grabButton(b.key, b.modifiers, win, false,
486                                ButtonPressMask | ButtonMotionMask |
487                                ButtonReleaseMask, mode, GrabModeAsync,
488                                None, None, false);
489   else
490     otk::OBDisplay::ungrabButton(b.key, b.modifiers, win);
491 }
492
493 void OBBindings::grabButtons(bool grab, OBClient *client)
494 {
495   for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
496     ButtonBindingList::iterator it, end = _buttons[i].end();
497     for (it = _buttons[i].begin(); it != end; ++it)
498       grabButton(grab, (*it)->binding, (MouseContext)i, client);
499   }
500 }
501
502 void OBBindings::fireButton(ButtonData *data)
503 {
504   if (data->context == MC_Window) {
505     // these are grabbed in Sync mode to allow the press to be normal to the
506     // client
507     XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
508   }
509   
510   ButtonBindingList::iterator it, end = _buttons[data->context].end();
511   for (it = _buttons[data->context].begin(); it != end; ++it)
512     if ((*it)->binding.key == data->button &&
513         (*it)->binding.modifiers == data->state) {
514       CallbackList::iterator c_it,c_end = (*it)->callbacks[data->action].end();
515       for (c_it = (*it)->callbacks[data->action].begin();
516            c_it != c_end; ++c_it)
517         python_callback(*c_it, (PyObject*)data);
518     }
519 }
520
521
522 bool OBBindings::addEvent(EventAction action, PyObject *callback)
523 {
524   if (action < 0 || action >= NUM_EVENTS) {
525     return false;
526   }
527
528   _eventlist[action].push_back(callback);
529   Py_INCREF(callback);
530   return true;
531 }
532
533 bool OBBindings::removeEvent(EventAction action, PyObject *callback)
534 {
535   if (action < 0 || action >= NUM_EVENTS) {
536     return false;
537   }
538   
539   CallbackList::iterator it = std::find(_eventlist[action].begin(),
540                                         _eventlist[action].end(),
541                                         callback);
542   if (it != _eventlist[action].end()) {
543     Py_XDECREF(*it);
544     _eventlist[action].erase(it);
545     return true;
546   }
547   return false;
548 }
549
550 void OBBindings::removeAllEvents()
551 {
552   for (int i = 0; i < NUM_EVENTS; ++i) {
553     while (!_eventlist[i].empty()) {
554       Py_XDECREF(_eventlist[i].front());
555       _eventlist[i].pop_front();
556     }
557   }
558 }
559
560 void OBBindings::fireEvent(EventData *data)
561 {
562   CallbackList::iterator c_it, c_end = _eventlist[data->action].end();
563   for (c_it = _eventlist[data->action].begin(); c_it != c_end; ++c_it)
564     python_callback(*c_it, (PyObject*)data);
565 }
566
567 }