1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
13 #include "otk/display.hh"
19 #define _(str) gettext(str)
26 static bool buttonvalue(const std::string
&button
, unsigned int *val
)
28 if (button
== "Left" || button
== "1" || button
== "Button1") {
30 } else if (button
== "Middle" || button
== "2" || button
== "Button2") {
32 } else if (button
== "Right" || button
== "3" || button
== "Button3") {
34 } else if (button
== "Up" || button
== "4" || button
== "Button4") {
36 } else if (button
== "Down" || button
== "5" || button
== "Button5") {
43 static bool modvalue(const std::string
&mod
, unsigned int *val
)
45 if (mod
== "C") { // control
47 } else if (mod
== "S") { // shift
49 } else if (mod
== "A" || // alt/mod1
54 } else if (mod
== "Mod2" || // mod2
57 } else if (mod
== "Mod3" || // mod3
60 } else if (mod
== "W" || // windows/mod4
64 } else if (mod
== "Mod5" || // mod5
73 bool Bindings::translate(const std::string
&str
, Binding
&b
,bool askey
) const
75 // parse out the base key name
76 std::string::size_type keybegin
= str
.find_last_of('-');
77 keybegin
= (keybegin
== std::string::npos
) ? 0 : keybegin
+ 1;
78 std::string
key(str
, keybegin
);
80 // parse out the requested modifier keys
81 unsigned int modval
= 0;
82 std::string::size_type begin
= 0, end
;
83 while (begin
!= keybegin
) {
84 end
= str
.find_first_of('-', begin
);
86 std::string
mod(str
, begin
, end
-begin
);
87 if (!modvalue(mod
, &modval
)) {
88 printf(_("Invalid modifier element in key binding: %s\n"), mod
.c_str());
98 KeySym sym
= XStringToKeysym(const_cast<char *>(key
.c_str()));
99 if (sym
== NoSymbol
) {
100 printf(_("Invalid Key name in key binding: %s\n"), key
.c_str());
103 if (!(b
.key
= XKeysymToKeycode(**otk::display
, sym
)))
104 printf(_("No valid keycode for Key in key binding: %s\n"), key
.c_str());
107 return buttonvalue(key
, &b
.key
);
111 static void destroytree(KeyBindingTree
*tree
)
114 KeyBindingTree
*c
= tree
->first_child
;
120 KeyBindingTree
*Bindings::buildtree(const StringVect
&keylist
,
121 PyObject
*callback
) const
123 if (keylist
.empty()) return 0; // nothing in the list.. return 0
125 KeyBindingTree
*ret
= 0, *p
;
127 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
128 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
130 ret
= new KeyBindingTree();
132 // this is the first built node, the bottom node of the tree
134 ret
->callbacks
.push_back(callback
);
136 ret
->first_child
= p
;
137 if (!translate(*it
, ret
->binding
)) {
148 : _curpos(&_keytree
),
150 _timer((otk::Timer
*) 0),
151 _keybgrab_callback(0)
153 // setResetKey("C-g"); // set the default reset key
157 Bindings::~Bindings()
164 // removeAllButtons(); XXX
169 void Bindings::assimilate(KeyBindingTree
*node
)
171 KeyBindingTree
*a
, *b
, *tmp
, *last
;
173 if (!_keytree
.first_child
) {
174 // there are no nodes at this level yet
175 _keytree
.first_child
= node
;
177 a
= _keytree
.first_child
;
182 if (a
->binding
!= b
->binding
) {
191 if (last
->binding
!= b
->binding
)
192 last
->next_sibling
= b
;
194 last
->first_child
= b
->first_child
;
201 KeyBindingTree
*Bindings::find(KeyBindingTree
*search
,
202 bool *conflict
) const {
204 KeyBindingTree
*a
, *b
;
205 a
= _keytree
.first_child
;
208 if (a
->binding
!= b
->binding
) {
211 if (a
->chain
== b
->chain
) {
213 // found it! (return the actual id, not the search's)
218 return 0; // the chain status' don't match (conflict!)
224 return 0; // it just isn't in here
228 bool Bindings::addKey(const StringVect
&keylist
, PyObject
*callback
)
230 KeyBindingTree
*tree
, *t
;
233 if (!(tree
= buildtree(keylist
, callback
)))
234 return false; // invalid binding requested
236 t
= find(tree
, &conflict
);
238 // conflicts with another binding
244 // already bound to something
245 // XXX: look if callback is already bound to this key?
246 t
->callbacks
.push_back(callback
);
249 // grab the server here to make sure no key pressed go missed
250 otk::display
->grab();
253 // assimilate this built tree into the main tree
254 assimilate(tree
); // assimilation destroys/uses the tree
257 otk::display
->ungrab();
266 bool Bindings::removeKey(const StringVect
&keylist
, PyObject
*callback
)
268 assert(false); // XXX: function not implemented yet
270 KeyBindingTree
*tree
;
273 if (!(tree
= buildtree(keylist
, 0)))
274 return false; // invalid binding requested
276 KeyBindingTree
*t
= find(tree
, &conflict
);
278 CallbackList::iterator it
= std::find(t
->callbacks
.begin(),
281 if (it
!= t
->callbacks
.end()) {
282 // grab the server here to make sure no key pressed go missed
283 otk::display
->grab();
288 // XXX do shit here ...
292 otk::display
->ungrab();
301 void Bindings::setResetKey(const std::string
&key
)
304 if (translate(key
, b
)) {
305 // grab the server here to make sure no key pressed go missed
306 otk::display
->grab();
308 _resetkey
.key
= b
.key
;
309 _resetkey
.modifiers
= b
.modifiers
;
311 otk::display
->ungrab();
316 static void remove_branch(KeyBindingTree
*first
)
318 KeyBindingTree
*p
= first
;
322 remove_branch(p
->first_child
);
323 KeyBindingTree
*s
= p
->next_sibling
;
324 while(!p
->callbacks
.empty()) {
325 Py_XDECREF(p
->callbacks
.front());
326 p
->callbacks
.pop_front();
334 void Bindings::removeAllKeys()
337 if (_keytree
.first_child
) {
338 remove_branch(_keytree
.first_child
);
339 _keytree
.first_child
= 0;
345 void Bindings::grabKeys(bool grab
)
347 for (int i
= 0; i
< openbox
->screenCount(); ++i
) {
348 Window root
= otk::display
->screenInfo(i
)->rootWindow();
350 KeyBindingTree
*p
= _curpos
->first_child
;
353 otk::display
->grabKey(p
->binding
.key
, p
->binding
.modifiers
,
354 root
, false, GrabModeAsync
, GrabModeAsync
,
358 otk::display
->ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
365 otk::display
->grabKey(_resetkey
.key
, _resetkey
.modifiers
,
366 root
, false, GrabModeAsync
, GrabModeAsync
,
369 otk::display
->ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
375 bool Bindings::grabKeyboard(int screen
, PyObject
*callback
)
378 if (_keybgrab_callback
) return false; // already grabbed
381 for (i
= 0; i
< openbox
->screenCount(); ++i
)
382 if (openbox
->screen(screen
)->number() == screen
)
384 if (i
>= openbox
->screenCount())
385 return false; // couldn't find the screen.. it's not managed
387 Window root
= otk::display
->screenInfo(i
)->rootWindow();
388 if (XGrabKeyboard(**otk::display
, root
, false, GrabModeAsync
,
389 GrabModeSync
, CurrentTime
))
391 printf("****GRABBED****\n");
392 _keybgrab_callback
= callback
;
397 void Bindings::ungrabKeyboard()
399 if (!_keybgrab_callback
) return; // not grabbed
401 _keybgrab_callback
= 0;
402 XUngrabKeyboard(**otk::display
, CurrentTime
);
403 printf("****UNGRABBED****\n");
407 void Bindings::fireKey(int screen
, unsigned int modifiers
, unsigned int key
,
408 Time time
, KeyAction action
)
410 if (_keybgrab_callback
) {
411 Client
*c
= openbox
->focusedClient();
412 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
413 python_callback(_keybgrab_callback
, &data
);
415 // KeyRelease events only occur during keyboard grabs
416 if (action
== EventKeyRelease
) return;
418 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
421 KeyBindingTree
*p
= _curpos
->first_child
;
423 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
427 _timer
= new otk::Timer(5000, // 5 second timeout
428 (otk::Timer::TimeoutHandler
)resetChains
,
430 // grab the server here to make sure no key pressed go missed
431 otk::display
->grab();
435 otk::display
->ungrab();
437 Client
*c
= openbox
->focusedClient();
438 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
439 CallbackList::iterator it
, end
= p
->callbacks
.end();
440 for (it
= p
->callbacks
.begin(); it
!= end
; ++it
)
441 python_callback(*it
, &data
);
452 void Bindings::resetChains(Bindings
*self
)
456 self
->_timer
= (otk::Timer
*) 0;
458 // grab the server here to make sure no key pressed go missed
459 otk::display
->grab();
460 self
->grabKeys(false);
461 self
->_curpos
= &self
->_keytree
;
462 self
->grabKeys(true);
463 otk::display
->ungrab();
467 bool Bindings::addButton(const std::string
&but
, MouseContext context
,
468 MouseAction action
, PyObject
*callback
)
470 assert(context
>= 0 && context
< NUM_MOUSE_CONTEXT
);
473 if (!translate(but
, b
, false))
476 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
478 // look for a duplicate binding
479 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
480 if ((*it
)->binding
.key
== b
.key
&&
481 (*it
)->binding
.modifiers
== b
.modifiers
) {
487 // the binding didnt exist yet, add it
489 bind
= new ButtonBinding();
490 bind
->binding
.key
= b
.key
;
491 bind
->binding
.modifiers
= b
.modifiers
;
492 _buttons
[context
].push_back(bind
);
493 // grab the button on all clients
494 for (int sn
= 0; sn
< openbox
->screenCount(); ++sn
) {
495 Screen
*s
= openbox
->screen(sn
);
496 Client::List::iterator c_it
, c_end
= s
->clients
.end();
497 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
498 grabButton(true, bind
->binding
, context
, *c_it
);
503 bind
->callbacks
[action
].push_back(callback
);
508 void Bindings::removeAllButtons()
510 for (int i
= 0; i
< NUM_MOUSE_CONTEXT
; ++i
) {
511 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
512 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
) {
513 for (int a
= 0; a
< NUM_MOUSE_ACTION
; ++a
) {
514 while (!(*it
)->callbacks
[a
].empty()) {
515 Py_XDECREF((*it
)->callbacks
[a
].front());
516 (*it
)->callbacks
[a
].pop_front();
519 // ungrab the button on all clients
520 for (int sn
= 0; sn
< openbox
->screenCount(); ++sn
) {
521 Screen
*s
= openbox
->screen(sn
);
522 Client::List::iterator c_it
, c_end
= s
->clients
.end();
523 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
524 grabButton(false, (*it
)->binding
, (MouseContext
)i
, *c_it
);
531 void Bindings::grabButton(bool grab
, const Binding
&b
, MouseContext context
,
535 int mode
= GrabModeAsync
;
539 win
= client
->frame
->window();
540 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
543 win
= client
->frame
->plate();
544 mode
= GrabModeSync
; // this is handled in fireButton
545 mask
= ButtonPressMask
; // can't catch more than this with Sync mode
546 // the release event is manufactured by the
547 // master buttonPressHandler
550 // any other elements already get button events, don't grab on them
554 otk::display
->grabButton(b
.key
, b
.modifiers
, win
, false, mask
, mode
,
555 GrabModeAsync
, None
, None
, false);
557 otk::display
->ungrabButton(b
.key
, b
.modifiers
, win
);
560 void Bindings::grabButtons(bool grab
, Client
*client
)
562 for (int i
= 0; i
< NUM_MOUSE_CONTEXT
; ++i
) {
563 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
564 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
565 grabButton(grab
, (*it
)->binding
, (MouseContext
)i
, client
);
569 void Bindings::fireButton(MouseData
*data
)
571 if (data
->context
== MC_Window
) {
572 // Replay the event, so it goes to the client
573 XAllowEvents(**otk::display
, ReplayPointer
, data
->time
);
576 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
577 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
578 if ((*it
)->binding
.key
== data
->button
&&
579 (*it
)->binding
.modifiers
== data
->state
) {
580 CallbackList::iterator c_it
,c_end
= (*it
)->callbacks
[data
->action
].end();
581 for (c_it
= (*it
)->callbacks
[data
->action
].begin();
582 c_it
!= c_end
; ++c_it
)
583 python_callback(*c_it
, data
);
588 bool Bindings::addEvent(EventAction action
, PyObject
*callback
)
590 if (action
< 0 || action
>= NUM_EVENTS
) {
594 if (action
== EventBell
&& _eventlist
[action
].empty())
595 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
596 XkbBellNotifyMask
, XkbBellNotifyMask
);
598 _eventlist
[action
].push_back(callback
);
603 bool Bindings::removeEvent(EventAction action
, PyObject
*callback
)
605 if (action
< 0 || action
>= NUM_EVENTS
) {
609 CallbackList::iterator it
= std::find(_eventlist
[action
].begin(),
610 _eventlist
[action
].end(),
612 if (it
!= _eventlist
[action
].end()) {
614 _eventlist
[action
].erase(it
);
616 if (action
== EventBell
&& _eventlist
[action
].empty())
617 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
618 XkbBellNotifyMask
, 0);
625 void Bindings::removeAllEvents()
627 for (int i
= 0; i
< NUM_EVENTS
; ++i
) {
628 while (!_eventlist
[i
].empty()) {
629 Py_XDECREF(_eventlist
[i
].front());
630 _eventlist
[i
].pop_front();
635 void Bindings::fireEvent(EventData
*data
)
637 CallbackList::iterator c_it
, c_end
= _eventlist
[data
->action
].end();
638 for (c_it
= _eventlist
[data
->action
].begin(); c_it
!= c_end
; ++c_it
)
639 python_callback(*c_it
, data
);