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),
151 setResetKey("C-g"); // set the default reset key
155 Bindings::~Bindings()
161 XUngrabKeyboard(**otk::display
, CurrentTime
);
164 //removeAllButtons(); // this is done by each client as they are unmanaged
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
, KeyCallback callback
,
231 KeyBindingTree
*tree
, *t
;
234 if (!(tree
= buildtree(keylist
, callback
, data
)))
235 return false; // invalid binding requested
237 t
= find(tree
, &conflict
);
239 // conflicts with another binding
245 // already bound to something
246 t
->callbacks
.push_back(KeyCallbackData(callback
, data
));
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();
264 bool Bindings::removeKey(const StringVect &keylist, KeyCallback callback, void *data)
266 assert(false); // XXX: function not implemented yet
268 KeyBindingTree *tree;
271 if (!(tree = buildtree(keylist, 0)))
272 return false; // invalid binding requested
274 KeyBindingTree *t = find(tree, &conflict);
276 KeyCallbackList::iterator it = std::find(t->callbacks.begin(),
279 if (it != t->callbacks.end()) {
280 // grab the server here to make sure no key pressed go missed
281 otk::display->grab();
286 // XXX do shit here ...
290 otk::display->ungrab();
299 void Bindings::setResetKey(const std::string
&key
)
302 if (translate(key
, b
)) {
303 _resetkey
.key
= b
.key
;
304 _resetkey
.modifiers
= b
.modifiers
;
309 static void remove_branch(KeyBindingTree
*first
)
311 KeyBindingTree
*p
= first
;
315 remove_branch(p
->first_child
);
316 KeyBindingTree
*s
= p
->next_sibling
;
317 while(!p
->callbacks
.empty()) {
318 p
->callbacks
.pop_front();
326 void Bindings::removeAllKeys()
329 if (_keytree
.first_child
) {
330 remove_branch(_keytree
.first_child
);
331 _keytree
.first_child
= 0;
337 void Bindings::grabKeys(bool grab
)
339 for (int i
= 0; i
< ScreenCount(**otk::display
); ++i
) {
340 Screen
*sc
= openbox
->screen(i
);
341 if (!sc
) continue; // not a managed screen
342 Window root
= otk::display
->screenInfo(i
)->rootWindow();
344 otk::display
->ungrabAllKeys(root
);
347 KeyBindingTree
*p
= _keytree
.first_child
;
349 otk::display
->grabKey(p
->binding
.key
, p
->binding
.modifiers
,
350 root
, false, GrabModeAsync
, GrabModeSync
,
358 bool Bindings::grabKeyboard(int screen
, KeyCallback callback
, void *data
)
361 if (_keybgrab_callback
.callback
) return false; // already grabbed
363 if (!openbox
->screen(screen
))
364 return false; // the screen is not managed
366 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
367 if (XGrabKeyboard(**otk::display
, root
, false, GrabModeAsync
,
368 GrabModeAsync
, CurrentTime
))
370 _keybgrab_callback
.callback
= callback
;
371 _keybgrab_callback
.data
= data
;
376 void Bindings::ungrabKeyboard()
378 if (!_keybgrab_callback
.callback
) return; // not grabbed
380 _keybgrab_callback
= KeyCallbackData(0, 0);
381 if (!_grabbed
) /* don't release out from under keychains */
382 XUngrabKeyboard(**otk::display
, CurrentTime
);
383 XUngrabPointer(**otk::display
, CurrentTime
);
387 bool Bindings::grabPointer(int screen
)
389 if (!openbox
->screen(screen
))
390 return false; // the screen is not managed
392 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
393 XGrabPointer(**otk::display
, root
, false, 0, GrabModeAsync
,
394 GrabModeAsync
, None
, None
, CurrentTime
);
399 void Bindings::ungrabPointer()
401 XUngrabPointer(**otk::display
, CurrentTime
);
405 void Bindings::fireKey(int screen
, unsigned int modifiers
, unsigned int key
,
406 Time time
, KeyAction::KA action
)
408 if (_keybgrab_callback
.callback
) {
409 Client
*c
= openbox
->focusedClient();
410 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
411 _keybgrab_callback
.fire(&data
);
414 // KeyRelease events only occur during keyboard grabs
415 if (action
== KeyAction::Release
) return;
417 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
419 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
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 if (!_grabbed
&& !_keybgrab_callback
.callback
) {
431 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
432 //grab should never fail because we should have a sync grab at
434 XGrabKeyboard(**otk::display
, root
, 0, GrabModeAsync
,
435 GrabModeSync
, CurrentTime
);
439 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
441 Client
*c
= openbox
->focusedClient();
442 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
443 KeyCallbackList::iterator it
, end
= p
->callbacks
.end();
444 for (it
= p
->callbacks
.begin(); it
!= end
; ++it
)
446 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
456 void Bindings::resetChains(Bindings
*self
)
460 self
->_timer
= (otk::Timer
*) 0;
462 self
->_curpos
= &self
->_keytree
;
463 if (self
->_grabbed
) {
464 self
->_grabbed
= false;
465 if (!self
->_keybgrab_callback
.callback
)
466 XUngrabKeyboard(**otk::display
, CurrentTime
);
471 bool Bindings::addButton(const std::string
&but
, MouseContext::MC context
,
472 MouseAction::MA action
, MouseCallback callback
,
475 assert(context
>= 0 && context
< MouseContext::NUM_MOUSE_CONTEXT
);
476 assert(action
>= 0 && action
< MouseAction::NUM_MOUSE_ACTION
);
479 if (!translate(but
, b
, false))
482 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
484 // look for a duplicate binding
485 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
486 if ((*it
)->binding
.key
== b
.key
&&
487 (*it
)->binding
.modifiers
== b
.modifiers
) {
493 // the binding didnt exist yet, add it
495 bind
= new ButtonBinding();
496 bind
->binding
.key
= b
.key
;
497 bind
->binding
.modifiers
= b
.modifiers
;
498 _buttons
[context
].push_back(bind
);
499 // grab the button on all clients
500 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
501 Screen
*s
= openbox
->screen(sn
);
502 if (!s
) continue; // not managed
503 Client::List::iterator c_it
, c_end
= s
->clients
.end();
504 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
505 grabButton(true, bind
->binding
, context
, *c_it
);
510 bind
->callbacks
[action
].push_back(MouseCallbackData(callback
, data
));
514 void Bindings::removeAllButtons()
516 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
517 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
518 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
) {
519 for (int a
= 0; a
< MouseAction::NUM_MOUSE_ACTION
; ++a
) {
520 while (!(*it
)->callbacks
[a
].empty()) {
521 (*it
)->callbacks
[a
].pop_front();
524 // ungrab the button on all clients
525 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
526 Screen
*s
= openbox
->screen(sn
);
527 if (!s
) continue; // not managed
528 Client::List::iterator c_it
, c_end
= s
->clients
.end();
529 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
530 grabButton(false, (*it
)->binding
, (MouseContext::MC
)i
, *c_it
);
537 void Bindings::grabButton(bool grab
, const Binding
&b
,
538 MouseContext::MC context
, Client
*client
)
541 int mode
= GrabModeAsync
;
544 case MouseContext::Frame
:
545 win
= client
->frame
->window();
546 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
548 case MouseContext::Window
:
549 win
= client
->frame
->plate();
550 mode
= GrabModeSync
; // this is handled in fireButton
551 mask
= ButtonPressMask
; // can't catch more than this with Sync mode
552 // the release event is manufactured by the
553 // master buttonPressHandler
556 // any other elements already get button events, don't grab on them
560 otk::display
->grabButton(b
.key
, b
.modifiers
, win
, false, mask
, mode
,
561 GrabModeAsync
, None
, None
, false);
563 otk::display
->ungrabButton(b
.key
, b
.modifiers
, win
);
566 void Bindings::grabButtons(bool grab
, Client
*client
)
568 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
569 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
570 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
571 grabButton(grab
, (*it
)->binding
, (MouseContext::MC
)i
, client
);
575 void Bindings::fireButton(MouseData
*data
)
577 if (data
->context
== MouseContext::Window
) {
578 // Replay the event, so it goes to the client
579 XAllowEvents(**otk::display
, ReplayPointer
, data
->time
);
582 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
583 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
584 if ((*it
)->binding
.key
== data
->button
&&
585 (*it
)->binding
.modifiers
== data
->state
) {
586 MouseCallbackList::iterator c_it
,
587 c_end
= (*it
)->callbacks
[data
->action
].end();
588 for (c_it
= (*it
)->callbacks
[data
->action
].begin();
589 c_it
!= c_end
; ++c_it
)
595 bool Bindings::addEvent(EventAction::EA action
, EventCallback callback
,
598 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
602 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
603 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
604 XkbBellNotifyMask
, XkbBellNotifyMask
);
606 _eventlist
[action
].push_back(EventCallbackData(callback
, data
));
610 bool Bindings::removeEvent(EventAction::EA action
, EventCallback callback
,
613 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
617 EventCallbackList::iterator it
= std::find(_eventlist
[action
].begin(),
618 _eventlist
[action
].end(),
619 EventCallbackData(callback
,
621 if (it
!= _eventlist
[action
].end()) {
622 _eventlist
[action
].erase(it
);
624 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
625 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
626 XkbBellNotifyMask
, 0);
633 void Bindings::removeAllEvents()
635 for (int i
= 0; i
< EventAction::NUM_EVENT_ACTION
; ++i
) {
636 while (!_eventlist
[i
].empty()) {
637 _eventlist
[i
].pop_front();
642 void Bindings::fireEvent(EventData
*data
)
644 EventCallbackList::iterator c_it
, c_end
= _eventlist
[data
->action
].end();
645 for (c_it
= _eventlist
[data
->action
].begin(); c_it
!= c_end
; ++c_it
)