]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
rm old bb shit, and the old xeventhandler
[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)reset, this)
146 {
147 _timer.setTimeout(5000); // chains reset after 5 seconds
148
149 setResetKey("C-g"); // set the default reset key
150 }
151
152
153 OBBindings::~OBBindings()
154 {
155 grabKeys(false);
156 removeAllKeys();
157 removeAllButtons();
158 }
159
160
161 void OBBindings::assimilate(KeyBindingTree *node)
162 {
163 KeyBindingTree *a, *b, *tmp, *last;
164
165 if (!_keytree.first_child) {
166 // there are no nodes at this level yet
167 _keytree.first_child = node;
168 } else {
169 a = _keytree.first_child;
170 last = a;
171 b = node;
172 while (a) {
173 last = a;
174 if (a->binding != b->binding) {
175 a = a->next_sibling;
176 } else {
177 tmp = b;
178 b = b->first_child;
179 delete tmp;
180 a = a->first_child;
181 }
182 }
183 if (last->binding != b->binding)
184 last->next_sibling = b;
185 else {
186 last->first_child = b->first_child;
187 delete b;
188 }
189 }
190 }
191
192
193 PyObject *OBBindings::find(KeyBindingTree *search, bool *conflict) const {
194 *conflict = false;
195 KeyBindingTree *a, *b;
196 a = _keytree.first_child;
197 b = search;
198 while (a && b) {
199 if (a->binding != b->binding) {
200 a = a->next_sibling;
201 } else {
202 if (a->chain == b->chain) {
203 if (!a->chain) {
204 // found it! (return the actual id, not the search's)
205 return a->callback;
206 }
207 } else {
208 *conflict = true;
209 return 0; // the chain status' don't match (conflict!)
210 }
211 b = b->first_child;
212 a = a->first_child;
213 }
214 }
215 return 0; // it just isn't in here
216 }
217
218
219 bool OBBindings::addKey(const StringVect &keylist, PyObject *callback)
220 {
221 KeyBindingTree *tree;
222 bool conflict;
223
224 if (!(tree = buildtree(keylist, callback)))
225 return false; // invalid binding requested
226
227 if (find(tree, &conflict) || conflict) {
228 // conflicts with another binding
229 destroytree(tree);
230 return false;
231 }
232
233 grabKeys(false);
234
235 // assimilate this built tree into the main tree
236 assimilate(tree); // assimilation destroys/uses the tree
237
238 Py_INCREF(callback);
239
240 grabKeys(true);
241
242 return true;
243 }
244
245
246 bool OBBindings::removeKey(const StringVect &keylist)
247 {
248 assert(false); // XXX: function not implemented yet
249
250 KeyBindingTree *tree;
251 bool conflict;
252
253 if (!(tree = buildtree(keylist, 0)))
254 return false; // invalid binding requested
255
256 PyObject *func = find(tree, &conflict);
257 if (func) {
258 grabKeys(false);
259
260 _curpos = &_keytree;
261
262 // XXX do shit here ...
263 Py_DECREF(func);
264
265 grabKeys(true);
266 return true;
267 }
268 return false;
269 }
270
271
272 void OBBindings::setResetKey(const std::string &key)
273 {
274 Binding b(0, 0);
275 if (translate(key, b)) {
276 grabKeys(false);
277 _resetkey.key = b.key;
278 _resetkey.modifiers = b.modifiers;
279 grabKeys(true);
280 }
281 }
282
283
284 static void remove_branch(KeyBindingTree *first)
285 {
286 KeyBindingTree *p = first;
287
288 while (p) {
289 if (p->first_child)
290 remove_branch(p->first_child);
291 KeyBindingTree *s = p->next_sibling;
292 Py_XDECREF(p->callback);
293 delete p;
294 p = s;
295 }
296 }
297
298
299 void OBBindings::removeAllKeys()
300 {
301 grabKeys(false);
302 if (_keytree.first_child) {
303 remove_branch(_keytree.first_child);
304 _keytree.first_child = 0;
305 }
306 grabKeys(true);
307 }
308
309
310 void OBBindings::grabKeys(bool grab)
311 {
312 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
313 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
314
315 KeyBindingTree *p = _curpos->first_child;
316 while (p) {
317 if (grab) {
318 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
319 root, false, GrabModeAsync, GrabModeAsync,
320 false);
321 }
322 else
323 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
324 root);
325 p = p->next_sibling;
326 }
327
328 if (grab)
329 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
330 root, true, GrabModeAsync, GrabModeAsync,
331 false);
332 else
333 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
334 root);
335 }
336 }
337
338
339 void OBBindings::fireKey(unsigned int modifiers, unsigned int key, Time time)
340 {
341 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
342 reset(this);
343 } else {
344 KeyBindingTree *p = _curpos->first_child;
345 while (p) {
346 if (p->binding.key == key && p->binding.modifiers == modifiers) {
347 if (p->chain) {
348 _timer.start(); // start/restart the timer
349 grabKeys(false);
350 _curpos = p;
351 grabKeys(true);
352 } else {
353 Window win = None;
354 OBClient *c = Openbox::instance->focusedClient();
355 if (c) win = c->window();
356 KeyData *data = new_key_data(win, time, modifiers, key);
357 python_callback(p->callback, (PyObject*)data);
358 Py_DECREF((PyObject*)data);
359 reset(this);
360 }
361 break;
362 }
363 p = p->next_sibling;
364 }
365 }
366 }
367
368 void OBBindings::reset(OBBindings *self)
369 {
370 self->_timer.stop();
371 self->grabKeys(false);
372 self->_curpos = &self->_keytree;
373 self->grabKeys(true);
374 }
375
376
377 bool OBBindings::addButton(const std::string &but, MouseContext context,
378 MouseAction action, PyObject *callback)
379 {
380 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
381
382 Binding b(0,0);
383 if (!translate(but, b, false))
384 return false;
385
386 ButtonBindingList::iterator it, end = _buttons[context].end();
387
388 // look for a duplicate binding
389 for (it = _buttons[context].begin(); it != end; ++it)
390 if ((*it)->binding.key == b.key &&
391 (*it)->binding.modifiers == b.modifiers) {
392 if ((*it)->callback[action] == callback)
393 return true; // already bound
394 break;
395 }
396
397 ButtonBinding *bind;
398
399 // the binding didnt exist yet, add it
400 if (it == end) {
401 bind = new ButtonBinding();
402 bind->binding.key = b.key;
403 bind->binding.modifiers = b.modifiers;
404 _buttons[context].push_back(bind);
405 // XXX GRAB the new button everywhere!
406 } else
407 bind = *it;
408 Py_XDECREF(bind->callback[action]); // if it was already bound, unbind it
409 bind->callback[action] = callback;
410 Py_INCREF(callback);
411 return true;
412 }
413
414 void OBBindings::removeAllButtons()
415 {
416 // XXX: UNGRAB shits
417 for (int i = i; i < NUM_MOUSE_CONTEXT; ++i) {
418 ButtonBindingList::iterator it, end = _buttons[i].end();
419 for (it = _buttons[i].begin(); it != end; ++it)
420 for (int a = 0; a < NUM_MOUSE_ACTION; ++a) {
421 Py_XDECREF((*it)->callback[a]);
422 (*it)->callback[a] = 0;
423 }
424 }
425 }
426
427 void OBBindings::grabButtons(bool grab, OBClient *client)
428 {
429 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
430 Window win;
431 int mode = GrabModeAsync;
432 switch (i) {
433 case MC_Frame:
434 win = client->frame->window();
435 break;
436 case MC_Window:
437 win = client->frame->plate();
438 mode = GrabModeSync; // this is handled in the plate's buttonPressHandler
439 break;
440 default:
441 // any other elements already get button events, don't grab on them
442 continue;
443 }
444 ButtonBindingList::iterator it, end = _buttons[i].end();
445 for (it = _buttons[i].begin(); it != end; ++it)
446 if (grab)
447 otk::OBDisplay::grabButton((*it)->binding.key,
448 (*it)->binding.modifiers, win, false,
449 ButtonPressMask | ButtonMotionMask |
450 ButtonReleaseMask, mode, GrabModeAsync,
451 None, None, false);
452 else
453 otk::OBDisplay::ungrabButton((*it)->binding.key,
454 (*it)->binding.modifiers, win);
455 }
456 }
457
458 void OBBindings::fireButton(ButtonData *data)
459 {
460 printf("but.mods %d.%d\n", data->button, data->state);
461
462 if (data->context == MC_Window) {
463 // these are grabbed in Sync mode to allow the press to be normal to the
464 // client
465 XAllowEvents(otk::OBDisplay::display, ReplayPointer, data->time);
466 }
467
468 ButtonBindingList::iterator it, end = _buttons[data->context].end();
469 for (it = _buttons[data->context].begin(); it != end; ++it)
470 if ((*it)->binding.key == data->button &&
471 (*it)->binding.modifiers == data->state) {
472 if ((*it)->callback[data->action])
473 python_callback((*it)->callback[data->action], (PyObject*)data);
474 }
475 }
476
477 }
This page took 0.059135 seconds and 4 git commands to generate.