]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
no default reset key
[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 (_resetkey.key)
333 if (grab)
334 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
335 root, false, GrabModeAsync, GrabModeAsync,
336 false);
337 else
338 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
339 root);
340 }
341 }
342
343
344 void OBBindings::fireKey(unsigned int modifiers, unsigned int key, Time time)
345 {
346 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
347 resetChains(this);
348 } else {
349 KeyBindingTree *p = _curpos->first_child;
350 while (p) {
351 if (p->binding.key == key && p->binding.modifiers == modifiers) {
352 if (p->chain) {
353 _timer.start(); // start/restart the timer
354 grabKeys(false);
355 _curpos = p;
356 grabKeys(true);
357 } else {
358 Window win = None;
359 OBClient *c = Openbox::instance->focusedClient();
360 if (c) win = c->window();
361 KeyData *data = new_key_data(win, time, modifiers, key);
362 python_callback(p->callback, (PyObject*)data);
363 Py_DECREF((PyObject*)data);
364 resetChains(this);
365 }
366 break;
367 }
368 p = p->next_sibling;
369 }
370 }
371 }
372
373 void OBBindings::resetChains(OBBindings *self)
374 {
375 self->_timer.stop();
376 self->grabKeys(false);
377 self->_curpos = &self->_keytree;
378 self->grabKeys(true);
379 }
380
381
382 bool OBBindings::addButton(const std::string &but, MouseContext context,
383 MouseAction action, PyObject *callback)
384 {
385 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
386
387 Binding b(0,0);
388 if (!translate(but, b, false))
389 return false;
390
391 ButtonBindingList::iterator it, end = _buttons[context].end();
392
393 // look for a duplicate binding
394 for (it = _buttons[context].begin(); it != end; ++it)
395 if ((*it)->binding.key == b.key &&
396 (*it)->binding.modifiers == b.modifiers) {
397 if ((*it)->callback[action] == callback)
398 return true; // already bound
399 break;
400 }
401
402 ButtonBinding *bind;
403
404 // the binding didnt exist yet, add it
405 if (it == end) {
406 bind = new ButtonBinding();
407 bind->binding.key = b.key;
408 bind->binding.modifiers = b.modifiers;
409 _buttons[context].push_back(bind);
410 // grab the button on all clients
411 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
412 OBScreen *s = Openbox::instance->screen(sn);
413 OBScreen::ClientList::iterator c_it, c_end = s->clients.end();
414 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
415 grabButton(true, bind->binding, context, *c_it);
416 }
417 }
418 } else
419 bind = *it;
420 Py_XDECREF(bind->callback[action]); // if it was already bound, unbind it
421 bind->callback[action] = callback;
422 Py_INCREF(callback);
423 return true;
424 }
425
426 void OBBindings::removeAllButtons()
427 {
428 for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
429 ButtonBindingList::iterator it, end = _buttons[i].end();
430 for (it = _buttons[i].begin(); it != end; ++it) {
431 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
432 Py_XDECREF((*it)->callback[a]);
433 (*it)->callback[a] = 0;
434 }
435 // ungrab the button on all clients
436 for (int sn = 0; sn < Openbox::instance->screenCount(); ++sn) {
437 OBScreen *s = Openbox::instance->screen(sn);
438 OBScreen::ClientList::iterator c_it, c_end = s->clients.end();
439 for (c_it = s->clients.begin(); c_it != c_end; ++c_it) {
440 grabButton(false, (*it)->binding, (MouseContext)i, *c_it);
441 }
442 }
443 }
444 }
445 }
446
447 void OBBindings::grabButton(bool grab, const Binding &b, MouseContext context,
448 OBClient *client)
449 {
450 Window win;
451 int mode = GrabModeAsync;
452 switch(context) {
453 case MC_Frame:
454 win = client->frame->window();
455 break;
456 case MC_Window:
457 win = client->frame->plate();
458 mode = GrabModeSync; // this is handled in fireButton
459 break;
460 default:
461 // any other elements already get button events, don't grab on them
462 return;
463 }
464 if (grab)
465 otk::OBDisplay::grabButton(b.key, b.modifiers, win, false,
466 ButtonPressMask | ButtonMotionMask |
467 ButtonReleaseMask, mode, GrabModeAsync,
468 None, None, false);
469 else
470 otk::OBDisplay::ungrabButton(b.key, b.modifiers, win);
471 }
472
473 void OBBindings::grabButtons(bool grab, OBClient *client)
474 {
475 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
476 ButtonBindingList::iterator it, end = _buttons[i].end();
477 for (it = _buttons[i].begin(); it != end; ++it)
478 grabButton(grab, (*it)->binding, (MouseContext)i, client);
479 }
480 }
481
482 void OBBindings::fireButton(ButtonData *data)
483 {
484 printf("but.mods %d.%d\n", data->button, data->state);
485
486 if (data->context == MC_Window) {
487 // these are grabbed in Sync mode to allow the press to be normal to the
488 // client
489 XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
490 }
491
492 ButtonBindingList::iterator it, end = _buttons[data->context].end();
493 for (it = _buttons[data->context].begin(); it != end; ++it)
494 if ((*it)->binding.key == data->button &&
495 (*it)->binding.modifiers == data->state) {
496 if ((*it)->callback[data->action])
497 python_callback((*it)->callback[data->action], (PyObject*)data);
498 }
499 }
500
501
502 bool OBBindings::addEvent(EventAction action, PyObject *callback)
503 {
504 if (action < 0 || action >= NUM_EVENTS) {
505 return false;
506 }
507
508 Py_XDECREF(_events[action]);
509 _events[action] = callback;
510 Py_INCREF(callback);
511 return true;
512 }
513
514 bool OBBindings::removeEvent(EventAction action)
515 {
516 if (action < 0 || action >= NUM_EVENTS) {
517 return false;
518 }
519
520 Py_XDECREF(_events[action]);
521 _events[action] = 0;
522 return true;
523 }
524
525 void OBBindings::removeAllEvents()
526 {
527 for (int i = 0; i < NUM_EVENTS; ++i) {
528 Py_XDECREF(_events[i]);
529 _events[i] = 0;
530 }
531 }
532
533 void OBBindings::fireEvent(EventData *data)
534 {
535 if (_events[data->action])
536 python_callback(_events[data->action], (PyObject*)data);
537 }
538
539 }
This page took 0.057947 seconds and 4 git commands to generate.