]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
new mouse button code is seeming to work. you can move windows
[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 python_callback(p->callback, win, modifiers, key, time);
354 reset(this);
355 }
356 break;
357 }
358 p = p->next_sibling;
359 }
360 }
361 }
362
363 void OBBindings::reset(OBBindings *self)
364 {
365 self->_timer.stop();
366 self->grabKeys(false);
367 self->_curpos = &self->_keytree;
368 self->grabKeys(true);
369 }
370
371
372 bool OBBindings::addButton(const std::string &but, MouseContext context,
373 MouseAction action, PyObject *callback)
374 {
375 assert(context >= 0 && context < NUM_MOUSE_CONTEXT);
376
377 Binding b(0,0);
378 if (!translate(but, b, false))
379 return false;
380
381 ButtonBindingList::iterator it, end = _buttons[context].end();
382
383 // look for a duplicate binding
384 for (it = _buttons[context].begin(); it != end; ++it)
385 if ((*it)->binding.key == b.key &&
386 (*it)->binding.modifiers == b.modifiers) {
387 ButtonBinding::CallbackList::iterator c_it,
388 c_end = (*it)->callback[action].end();
389 for (c_it = (*it)->callback[action].begin(); c_it != c_end; ++c_it)
390 if (*c_it == callback)
391 return true; // already bound
392 break;
393 }
394
395 ButtonBinding *bind;
396
397 // the binding didnt exist yet, add it
398 if (it == end) {
399 bind = new ButtonBinding();
400 bind->binding.key = b.key;
401 bind->binding.modifiers = b.modifiers;
402 _buttons[context].push_back(bind);
403 printf("adding %d.%d to %d\n", b.key, b.modifiers, context);
404 // XXX GRAB the new button everywhere!
405 } else
406 bind = *it;
407 bind->callback[action].push_back(callback);
408 Py_INCREF(callback);
409 return true;
410 }
411
412 void OBBindings::grabButtons(bool grab, OBClient *client)
413 {
414 for (int i = 0; i < NUM_MOUSE_CONTEXT; ++i) {
415 Window win[3] = {0, 0, 0}; // at most 2 windows
416 switch (i) {
417 case MC_Frame:
418 win[0] = client->frame->window();
419 break;
420 case MC_Titlebar:
421 win[0] = client->frame->titlebar();
422 win[1] = client->frame->label();
423 break;
424 case MC_Window:
425 win[0] = client->frame->plate();
426 break;
427 case MC_MaximizeButton:
428 // win[0] = client->frame->button_max();
429 break;
430 case MC_CloseButton:
431 // win[0] = client->frame->button_close();
432 break;
433 case MC_IconifyButton:
434 // win[0] = client->frame->button_iconify();
435 break;
436 case MC_StickyButton:
437 // win[0] = client->frame->button_stick();
438 break;
439 case MC_Grip:
440 // win[0] = client->frame->grip_left();
441 // win[1] = client->frame->grip_right();
442 break;
443 case MC_Root:
444 // win[0] = otk::OBDisplay::screenInfo(client->screen())->rootWindow();
445 break;
446 case MC_MenuItem:
447 break;
448 default:
449 assert(false); // invalid mouse context
450 }
451 ButtonBindingList::iterator it, end = _buttons[i].end();
452 for (it = _buttons[i].begin(); it != end; ++it)
453 for (Window *w = win; *w; ++w) {
454 if (grab) {
455 otk::OBDisplay::grabButton((*it)->binding.key,
456 (*it)->binding.modifiers, *w, false,
457 ButtonPressMask | ButtonMotionMask |
458 ButtonReleaseMask, GrabModeAsync,
459 GrabModeAsync, None, None, false);
460 }
461 else
462 otk::OBDisplay::ungrabButton((*it)->binding.key,
463 (*it)->binding.modifiers, *w);
464 }
465 }
466 }
467
468 void OBBindings::fire(MouseAction action, OBWidget::WidgetType type,
469 Window win, unsigned int modifiers, unsigned int button,
470 int xroot, int yroot, Time time)
471 {
472 assert(action >= 0 && action < NUM_MOUSE_ACTION);
473
474 MouseContext context;
475 switch (type) {
476 case OBWidget::Type_Frame:
477 context = MC_Frame; break;
478 case OBWidget::Type_Titlebar:
479 context = MC_Titlebar; break;
480 case OBWidget::Type_Handle:
481 context = MC_Frame; break;
482 case OBWidget::Type_Plate:
483 context = MC_Window; break;
484 case OBWidget::Type_Label:
485 context = MC_Titlebar; break;
486 case OBWidget::Type_MaximizeButton:
487 context = MC_MaximizeButton; break;
488 case OBWidget::Type_CloseButton:
489 context = MC_CloseButton; break;
490 case OBWidget::Type_IconifyButton:
491 context = MC_IconifyButton; break;
492 case OBWidget::Type_StickyButton:
493 context = MC_StickyButton; break;
494 case OBWidget::Type_LeftGrip:
495 context = MC_Grip; break;
496 case OBWidget::Type_RightGrip:
497 context = MC_Grip; break;
498 case OBWidget::Type_Client:
499 context = MC_Window; break;
500 case OBWidget::Type_Root:
501 context = MC_Root; break;
502 default:
503 assert(false); // unhandled type
504 }
505
506 modifiers &= (ControlMask | ShiftMask | Mod1Mask | Mod2Mask | Mod3Mask |
507 Mod4Mask | Mod5Mask);
508
509 printf("but.mods %d.%d\n", button, modifiers);
510
511 ButtonBindingList::iterator it, end = _buttons[context].end();
512 for (it = _buttons[context].begin(); it != end; ++it)
513 if ((*it)->binding.key == button &&
514 (*it)->binding.modifiers == modifiers) {
515 ButtonBinding::CallbackList::iterator c_it,
516 c_end = (*it)->callback[action].end();
517 for (c_it = (*it)->callback[action].begin(); c_it != c_end; ++c_it)
518 python_callback(*c_it, action, win, context, modifiers,
519 button, xroot, yroot, time);
520 }
521 }
522
523 }
This page took 0.054733 seconds and 4 git commands to generate.