1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
10 #include "otk/display.hh"
16 #define _(str) gettext(str)
23 static bool buttonvalue(const std::string
&button
, unsigned int *val
)
25 if (button
== "Left" || button
== "1" || button
== "Button1") {
27 } else if (button
== "Middle" || button
== "2" || button
== "Button2") {
29 } else if (button
== "Right" || button
== "3" || button
== "Button3") {
31 } else if (button
== "Up" || button
== "4" || button
== "Button4") {
33 } else if (button
== "Down" || button
== "5" || button
== "Button5") {
40 static bool modvalue(const std::string
&mod
, unsigned int *val
)
42 if (mod
== "C") { // control
44 } else if (mod
== "S") { // shift
46 } else if (mod
== "A" || // alt/mod1
51 } else if (mod
== "Mod2" || // mod2
54 } else if (mod
== "Mod3" || // mod3
57 } else if (mod
== "W" || // windows/mod4
61 } else if (mod
== "Mod5" || // mod5
70 bool Bindings::translate(const std::string
&str
, Binding
&b
,bool askey
) const
72 // parse out the base key name
73 std::string::size_type keybegin
= str
.find_last_of('-');
74 keybegin
= (keybegin
== std::string::npos
) ? 0 : keybegin
+ 1;
75 std::string
key(str
, keybegin
);
77 // parse out the requested modifier keys
78 unsigned int modval
= 0;
79 std::string::size_type begin
= 0, end
;
80 while (begin
!= keybegin
) {
81 end
= str
.find_first_of('-', begin
);
83 std::string
mod(str
, begin
, end
-begin
);
84 if (!modvalue(mod
, &modval
)) {
85 printf(_("Invalid modifier element in key binding: %s\n"), mod
.c_str());
95 KeySym sym
= XStringToKeysym(const_cast<char *>(key
.c_str()));
96 if (sym
== NoSymbol
) {
97 printf(_("Invalid Key name in key binding: %s\n"), key
.c_str());
100 if (!(b
.key
= XKeysymToKeycode(**otk::display
, sym
)))
101 printf(_("No valid keycode for Key in key binding: %s\n"), key
.c_str());
104 return buttonvalue(key
, &b
.key
);
108 static void destroytree(KeyBindingTree
*tree
)
111 KeyBindingTree
*c
= tree
->first_child
;
117 KeyBindingTree
*Bindings::buildtree(const StringVect
&keylist
,
118 KeyCallback callback
, void *data
) const
120 if (keylist
.empty()) return 0; // nothing in the list.. return 0
122 KeyBindingTree
*ret
= 0, *p
;
124 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
125 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
127 ret
= new KeyBindingTree();
129 // this is the first built node, the bottom node of the tree
131 ret
->callbacks
.push_back(KeyCallbackData(callback
, data
));
133 ret
->first_child
= p
;
134 if (!translate(*it
, ret
->binding
)) {
145 : _curpos(&_keytree
),
147 _timer((otk::Timer
*) 0),
148 _keybgrab_callback(0, 0)
150 // setResetKey("C-g"); // set the default reset key
154 Bindings::~Bindings()
160 //removeAllButtons(); // this is done by each client as they are unmanaged
165 void Bindings::assimilate(KeyBindingTree
*node
)
167 KeyBindingTree
*a
, *b
, *tmp
, *last
;
169 if (!_keytree
.first_child
) {
170 // there are no nodes at this level yet
171 _keytree
.first_child
= node
;
173 a
= _keytree
.first_child
;
178 if (a
->binding
!= b
->binding
) {
187 if (last
->binding
!= b
->binding
)
188 last
->next_sibling
= b
;
190 last
->first_child
= b
->first_child
;
197 KeyBindingTree
*Bindings::find(KeyBindingTree
*search
,
198 bool *conflict
) const {
200 KeyBindingTree
*a
, *b
;
201 a
= _keytree
.first_child
;
204 if (a
->binding
!= b
->binding
) {
207 if (a
->chain
== b
->chain
) {
209 // found it! (return the actual id, not the search's)
214 return 0; // the chain status' don't match (conflict!)
220 return 0; // it just isn't in here
224 bool Bindings::addKey(const StringVect
&keylist
, KeyCallback callback
,
227 KeyBindingTree
*tree
, *t
;
230 if (!(tree
= buildtree(keylist
, callback
, data
)))
231 return false; // invalid binding requested
233 t
= find(tree
, &conflict
);
235 // conflicts with another binding
241 // already bound to something
242 t
->callbacks
.push_back(KeyCallbackData(callback
, data
));
245 // grab the server here to make sure no key pressed go missed
246 otk::display
->grab();
249 // assimilate this built tree into the main tree
250 assimilate(tree
); // assimilation destroys/uses the tree
253 otk::display
->ungrab();
260 bool Bindings::removeKey(const StringVect &keylist, KeyCallback callback, void *data)
262 assert(false); // XXX: function not implemented yet
264 KeyBindingTree *tree;
267 if (!(tree = buildtree(keylist, 0)))
268 return false; // invalid binding requested
270 KeyBindingTree *t = find(tree, &conflict);
272 KeyCallbackList::iterator it = std::find(t->callbacks.begin(),
275 if (it != t->callbacks.end()) {
276 // grab the server here to make sure no key pressed go missed
277 otk::display->grab();
282 // XXX do shit here ...
286 otk::display->ungrab();
295 void Bindings::setResetKey(const std::string
&key
)
298 if (translate(key
, b
)) {
299 // grab the server here to make sure no key pressed go missed
300 otk::display
->grab();
302 _resetkey
.key
= b
.key
;
303 _resetkey
.modifiers
= b
.modifiers
;
305 otk::display
->ungrab();
310 static void remove_branch(KeyBindingTree
*first
)
312 KeyBindingTree
*p
= first
;
316 remove_branch(p
->first_child
);
317 KeyBindingTree
*s
= p
->next_sibling
;
318 while(!p
->callbacks
.empty()) {
319 p
->callbacks
.pop_front();
327 void Bindings::removeAllKeys()
330 if (_keytree
.first_child
) {
331 remove_branch(_keytree
.first_child
);
332 _keytree
.first_child
= 0;
338 void Bindings::grabKeys(bool grab
)
340 for (int i
= 0; i
< ScreenCount(**otk::display
); ++i
) {
341 Screen
*sc
= openbox
->screen(i
);
342 if (!sc
) continue; // not a managed screen
343 Window root
= otk::display
->screenInfo(i
)->rootWindow();
345 KeyBindingTree
*p
= _curpos
->first_child
;
348 otk::display
->grabKey(p
->binding
.key
, p
->binding
.modifiers
,
349 root
, false, GrabModeAsync
, GrabModeAsync
,
353 otk::display
->ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
360 otk::display
->grabKey(_resetkey
.key
, _resetkey
.modifiers
,
361 root
, false, GrabModeAsync
, GrabModeAsync
,
364 otk::display
->ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
370 bool Bindings::grabKeyboard(int screen
, KeyCallback callback
, void *data
)
373 if (_keybgrab_callback
.callback
) return false; // already grabbed
375 if (!openbox
->screen(screen
))
376 return false; // the screen is not managed
378 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
379 if (XGrabKeyboard(**otk::display
, root
, false, GrabModeAsync
,
380 GrabModeAsync
, CurrentTime
))
382 _keybgrab_callback
.callback
= callback
;
383 _keybgrab_callback
.data
= data
;
388 void Bindings::ungrabKeyboard()
390 if (!_keybgrab_callback
.callback
) return; // not grabbed
392 _keybgrab_callback
= KeyCallbackData(0, 0);
393 XUngrabKeyboard(**otk::display
, CurrentTime
);
394 XUngrabPointer(**otk::display
, CurrentTime
);
398 bool Bindings::grabPointer(int screen
)
400 if (!openbox
->screen(screen
))
401 return false; // the screen is not managed
403 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
404 XGrabPointer(**otk::display
, root
, false, 0, GrabModeAsync
,
405 GrabModeAsync
, None
, None
, CurrentTime
);
410 void Bindings::ungrabPointer()
412 XUngrabPointer(**otk::display
, CurrentTime
);
416 void Bindings::fireKey(int screen
, unsigned int modifiers
, unsigned int key
,
417 Time time
, KeyAction::KA action
)
419 if (_keybgrab_callback
.callback
) {
420 Client
*c
= openbox
->focusedClient();
421 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
422 _keybgrab_callback
.fire(&data
);
425 // KeyRelease events only occur during keyboard grabs
426 if (action
== KeyAction::Release
) return;
428 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
431 KeyBindingTree
*p
= _curpos
->first_child
;
433 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
437 _timer
= new otk::Timer(5000, // 5 second timeout
438 (otk::Timer::TimeoutHandler
)resetChains
,
440 // grab the server here to make sure no key presses get missed
441 otk::display
->grab();
445 otk::display
->ungrab();
447 Client
*c
= openbox
->focusedClient();
448 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
449 KeyCallbackList::iterator it
, end
= p
->callbacks
.end();
450 for (it
= p
->callbacks
.begin(); it
!= end
; ++it
)
461 void Bindings::resetChains(Bindings
*self
)
465 self
->_timer
= (otk::Timer
*) 0;
467 // grab the server here to make sure no key pressed go missed
468 otk::display
->grab();
469 self
->grabKeys(false);
470 self
->_curpos
= &self
->_keytree
;
471 self
->grabKeys(true);
472 otk::display
->ungrab();
476 bool Bindings::addButton(const std::string
&but
, MouseContext::MC context
,
477 MouseAction::MA action
, MouseCallback callback
,
480 assert(context
>= 0 && context
< MouseContext::NUM_MOUSE_CONTEXT
);
481 assert(action
>= 0 && action
< MouseAction::NUM_MOUSE_ACTION
);
484 if (!translate(but
, b
, false))
487 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
489 // look for a duplicate binding
490 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
491 if ((*it
)->binding
.key
== b
.key
&&
492 (*it
)->binding
.modifiers
== b
.modifiers
) {
498 // the binding didnt exist yet, add it
500 bind
= new ButtonBinding();
501 bind
->binding
.key
= b
.key
;
502 bind
->binding
.modifiers
= b
.modifiers
;
503 _buttons
[context
].push_back(bind
);
504 // grab the button on all clients
505 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
506 Screen
*s
= openbox
->screen(sn
);
507 if (!s
) continue; // not managed
508 Client::List::iterator c_it
, c_end
= s
->clients
.end();
509 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
510 grabButton(true, bind
->binding
, context
, *c_it
);
515 bind
->callbacks
[action
].push_back(MouseCallbackData(callback
, data
));
519 void Bindings::removeAllButtons()
521 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
522 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
523 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
) {
524 for (int a
= 0; a
< MouseAction::NUM_MOUSE_ACTION
; ++a
) {
525 while (!(*it
)->callbacks
[a
].empty()) {
526 (*it
)->callbacks
[a
].pop_front();
529 // ungrab the button on all clients
530 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
531 Screen
*s
= openbox
->screen(sn
);
532 if (!s
) continue; // not managed
533 Client::List::iterator c_it
, c_end
= s
->clients
.end();
534 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
535 grabButton(false, (*it
)->binding
, (MouseContext::MC
)i
, *c_it
);
542 void Bindings::grabButton(bool grab
, const Binding
&b
,
543 MouseContext::MC context
, Client
*client
)
546 int mode
= GrabModeAsync
;
549 case MouseContext::Frame
:
550 win
= client
->frame
->window();
551 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
553 case MouseContext::Window
:
554 win
= client
->frame
->plate();
555 mode
= GrabModeSync
; // this is handled in fireButton
556 mask
= ButtonPressMask
; // can't catch more than this with Sync mode
557 // the release event is manufactured by the
558 // master buttonPressHandler
561 // any other elements already get button events, don't grab on them
565 otk::display
->grabButton(b
.key
, b
.modifiers
, win
, false, mask
, mode
,
566 GrabModeAsync
, None
, None
, false);
568 otk::display
->ungrabButton(b
.key
, b
.modifiers
, win
);
571 void Bindings::grabButtons(bool grab
, Client
*client
)
573 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
574 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
575 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
576 grabButton(grab
, (*it
)->binding
, (MouseContext::MC
)i
, client
);
580 void Bindings::fireButton(MouseData
*data
)
582 if (data
->context
== MouseContext::Window
) {
583 // Replay the event, so it goes to the client
584 XAllowEvents(**otk::display
, ReplayPointer
, data
->time
);
587 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
588 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
589 if ((*it
)->binding
.key
== data
->button
&&
590 (*it
)->binding
.modifiers
== data
->state
) {
591 MouseCallbackList::iterator c_it
,
592 c_end
= (*it
)->callbacks
[data
->action
].end();
593 for (c_it
= (*it
)->callbacks
[data
->action
].begin();
594 c_it
!= c_end
; ++c_it
)
600 bool Bindings::addEvent(EventAction::EA action
, EventCallback callback
,
603 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
607 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
608 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
609 XkbBellNotifyMask
, XkbBellNotifyMask
);
611 _eventlist
[action
].push_back(EventCallbackData(callback
, data
));
615 bool Bindings::removeEvent(EventAction::EA action
, EventCallback callback
,
618 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
622 EventCallbackList::iterator it
= std::find(_eventlist
[action
].begin(),
623 _eventlist
[action
].end(),
624 EventCallbackData(callback
,
626 if (it
!= _eventlist
[action
].end()) {
627 _eventlist
[action
].erase(it
);
629 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
630 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
631 XkbBellNotifyMask
, 0);
638 void Bindings::removeAllEvents()
640 for (int i
= 0; i
< EventAction::NUM_EVENT_ACTION
; ++i
) {
641 while (!_eventlist
[i
].empty()) {
642 _eventlist
[i
].pop_front();
647 void Bindings::fireEvent(EventData
*data
)
649 EventCallbackList::iterator c_it
, c_end
= _eventlist
[data
->action
].end();
650 for (c_it
= _eventlist
[data
->action
].begin(); c_it
!= c_end
; ++c_it
)