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