1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
10 #include "otk/display.hh"
16 #define _(str) gettext(str)
24 static bool buttonvalue(const std::string
&button
, unsigned int *val
)
28 else if (button
== "Middle")
30 else if (button
== "Right")
32 else if (button
== "Up")
34 else if (button
== "Down")
37 // try convert to number
38 int i
= atoi(button
.c_str());
46 static bool modvalue(const std::string
&mod
, unsigned int *val
)
48 if (mod
== "C") { // control
50 } else if (mod
== "S") { // shift
52 } else if (mod
== "A" || // alt/mod1
57 } else if (mod
== "Mod2" || // mod2
60 } else if (mod
== "Mod3" || // mod3
63 } else if (mod
== "W" || // windows/mod4
67 } else if (mod
== "Mod5" || // mod5
76 bool Bindings::translate(const std::string
&str
, Binding
&b
,bool askey
) const
78 // parse out the base key name
79 std::string::size_type keybegin
= str
.find_last_of('-');
80 keybegin
= (keybegin
== std::string::npos
) ? 0 : keybegin
+ 1;
81 std::string
key(str
, keybegin
);
83 // parse out the requested modifier keys
84 unsigned int modval
= 0;
85 std::string::size_type begin
= 0, end
;
86 while (begin
!= keybegin
) {
87 end
= str
.find_first_of('-', begin
);
89 std::string
mod(str
, begin
, end
-begin
);
90 if (!modvalue(mod
, &modval
)) {
91 printf(_("Invalid modifier element in key binding: %s\n"), mod
.c_str());
101 KeySym sym
= XStringToKeysym(const_cast<char *>(key
.c_str()));
102 if (sym
== NoSymbol
) {
103 printf(_("Invalid Key name in key binding: %s\n"), key
.c_str());
106 if (!(b
.key
= XKeysymToKeycode(**otk::display
, sym
)))
107 printf(_("No valid keycode for Key in key binding: %s\n"), key
.c_str());
110 return buttonvalue(key
, &b
.key
);
114 static void destroytree(KeyBindingTree
*tree
)
117 KeyBindingTree
*c
= tree
->first_child
;
123 KeyBindingTree
*Bindings::buildtree(const StringVect
&keylist
,
124 KeyCallback callback
, void *data
) const
126 if (keylist
.empty()) return 0; // nothing in the list.. return 0
128 KeyBindingTree
*ret
= 0, *p
;
130 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
131 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
133 ret
= new KeyBindingTree();
135 // this is the first built node, the bottom node of the tree
137 ret
->callbacks
.push_back(KeyCallbackData(callback
, data
));
139 ret
->first_child
= p
;
140 if (!translate(*it
, ret
->binding
)) {
151 : _curpos(&_keytree
),
153 _timer((otk::Timer
*) 0),
154 _keybgrab_callback(0, 0),
157 setResetKey("C-g"); // set the default reset key
161 Bindings::~Bindings()
167 XUngrabKeyboard(**otk::display
, CurrentTime
);
170 //removeAllButtons(); // this is done by each client as they are unmanaged
175 void Bindings::assimilate(KeyBindingTree
*node
)
177 KeyBindingTree
*a
, *b
, *tmp
, *last
;
179 if (!_keytree
.first_child
) {
180 // there are no nodes at this level yet
181 _keytree
.first_child
= node
;
183 a
= _keytree
.first_child
;
188 if (a
->binding
!= b
->binding
) {
197 if (last
->binding
!= b
->binding
)
198 last
->next_sibling
= b
;
200 last
->first_child
= b
->first_child
;
207 KeyBindingTree
*Bindings::find(KeyBindingTree
*search
,
208 bool *conflict
) const {
210 KeyBindingTree
*a
, *b
;
211 a
= _keytree
.first_child
;
214 if (a
->binding
!= b
->binding
) {
217 if (a
->chain
== b
->chain
) {
219 // found it! (return the actual id, not the search's)
224 return 0; // the chain status' don't match (conflict!)
230 return 0; // it just isn't in here
234 bool Bindings::addKey(const StringVect
&keylist
, KeyCallback callback
,
237 KeyBindingTree
*tree
, *t
;
240 if (!(tree
= buildtree(keylist
, callback
, data
)))
241 return false; // invalid binding requested
243 t
= find(tree
, &conflict
);
245 // conflicts with another binding
251 // already bound to something
252 t
->callbacks
.push_back(KeyCallbackData(callback
, data
));
255 // grab the server here to make sure no key pressed go missed
256 otk::display
->grab();
259 // assimilate this built tree into the main tree
260 assimilate(tree
); // assimilation destroys/uses the tree
263 otk::display
->ungrab();
270 bool Bindings::removeKey(const StringVect &keylist, KeyCallback callback, void *data)
272 assert(false); // XXX: function not implemented yet
274 KeyBindingTree *tree;
277 if (!(tree = buildtree(keylist, 0)))
278 return false; // invalid binding requested
280 KeyBindingTree *t = find(tree, &conflict);
282 KeyCallbackList::iterator it = std::find(t->callbacks.begin(),
285 if (it != t->callbacks.end()) {
286 // grab the server here to make sure no key pressed go missed
287 otk::display->grab();
292 // XXX do shit here ...
296 otk::display->ungrab();
305 void Bindings::setResetKey(const std::string
&key
)
308 if (translate(key
, b
)) {
309 _resetkey
.key
= b
.key
;
310 _resetkey
.modifiers
= b
.modifiers
;
315 static void remove_branch(KeyBindingTree
*first
)
317 KeyBindingTree
*p
= first
;
321 remove_branch(p
->first_child
);
322 KeyBindingTree
*s
= p
->next_sibling
;
323 while(!p
->callbacks
.empty()) {
324 p
->callbacks
.pop_front();
332 void Bindings::removeAllKeys()
335 if (_keytree
.first_child
) {
336 remove_branch(_keytree
.first_child
);
337 _keytree
.first_child
= 0;
343 void Bindings::grabKeys(bool grab
)
345 for (int i
= 0; i
< ScreenCount(**otk::display
); ++i
) {
346 Screen
*sc
= openbox
->screen(i
);
347 if (!sc
) continue; // not a managed screen
348 Window root
= otk::display
->screenInfo(i
)->rootWindow();
350 otk::display
->ungrabAllKeys(root
);
353 KeyBindingTree
*p
= _keytree
.first_child
;
355 otk::display
->grabKey(p
->binding
.key
, p
->binding
.modifiers
,
356 root
, false, GrabModeAsync
, GrabModeSync
,
364 bool Bindings::grabKeyboard(int screen
, KeyCallback callback
, void *data
)
367 if (_keybgrab_callback
.callback
) return false; // already grabbed
369 if (!openbox
->screen(screen
))
370 return false; // the screen is not managed
372 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
373 if (XGrabKeyboard(**otk::display
, root
, false, GrabModeAsync
,
374 GrabModeAsync
, CurrentTime
))
376 _keybgrab_callback
.callback
= callback
;
377 _keybgrab_callback
.data
= data
;
382 void Bindings::ungrabKeyboard()
384 if (!_keybgrab_callback
.callback
) return; // not grabbed
386 _keybgrab_callback
= KeyCallbackData(0, 0);
387 if (!_grabbed
) /* don't release out from under keychains */
388 XUngrabKeyboard(**otk::display
, CurrentTime
);
389 XUngrabPointer(**otk::display
, CurrentTime
);
393 bool Bindings::grabPointer(int screen
)
395 if (!openbox
->screen(screen
))
396 return false; // the screen is not managed
398 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
399 XGrabPointer(**otk::display
, root
, false, 0, GrabModeAsync
,
400 GrabModeAsync
, None
, None
, CurrentTime
);
405 void Bindings::ungrabPointer()
407 XUngrabPointer(**otk::display
, CurrentTime
);
411 void Bindings::fireKey(int screen
, unsigned int modifiers
, unsigned int key
,
412 Time time
, KeyAction::KA action
)
414 if (_keybgrab_callback
.callback
) {
415 Client
*c
= openbox
->focusedClient();
416 KeyData
data(screen
, c
, time
, modifiers
, key
, action
);
417 _keybgrab_callback
.fire(&data
);
420 // KeyRelease events only occur during keyboard grabs
421 if (action
== KeyAction::Release
) return;
423 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
425 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
427 KeyBindingTree
*p
= _curpos
->first_child
;
429 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
433 _timer
= new otk::Timer(5000, // 5 second timeout
434 (otk::Timer::TimeoutHandler
)resetChains
,
436 if (!_grabbed
&& !_keybgrab_callback
.callback
) {
437 Window root
= otk::display
->screenInfo(screen
)->rootWindow();
438 //grab should never fail because we should have a sync grab at
440 XGrabKeyboard(**otk::display
, root
, 0, GrabModeAsync
,
441 GrabModeSync
, CurrentTime
);
445 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
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
)
452 XAllowEvents(**otk::display
, AsyncKeyboard
, CurrentTime
);
462 void Bindings::resetChains(Bindings
*self
)
466 self
->_timer
= (otk::Timer
*) 0;
468 self
->_curpos
= &self
->_keytree
;
469 if (self
->_grabbed
) {
470 self
->_grabbed
= false;
471 if (!self
->_keybgrab_callback
.callback
)
472 XUngrabKeyboard(**otk::display
, CurrentTime
);
477 bool Bindings::addButton(const std::string
&but
, MouseContext::MC context
,
478 MouseAction::MA action
, MouseCallback callback
,
481 assert(context
>= 0 && context
< MouseContext::NUM_MOUSE_CONTEXT
);
482 assert(action
>= 0 && action
< MouseAction::NUM_MOUSE_ACTION
);
485 if (!translate(but
, b
, false))
488 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
490 // look for a duplicate binding
491 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
492 if ((*it
)->binding
.key
== b
.key
&&
493 (*it
)->binding
.modifiers
== b
.modifiers
) {
499 // the binding didnt exist yet, add it
501 bind
= new ButtonBinding();
502 bind
->binding
.key
= b
.key
;
503 bind
->binding
.modifiers
= b
.modifiers
;
504 _buttons
[context
].push_back(bind
);
505 // grab the button on all clients
506 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
507 Screen
*s
= openbox
->screen(sn
);
508 if (!s
) continue; // not managed
509 Client::List::iterator c_it
, c_end
= s
->clients
.end();
510 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
511 grabButton(true, bind
->binding
, context
, *c_it
);
516 bind
->callbacks
[action
].push_back(MouseCallbackData(callback
, data
));
520 void Bindings::removeAllButtons()
522 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
523 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
524 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
) {
525 for (int a
= 0; a
< MouseAction::NUM_MOUSE_ACTION
; ++a
) {
526 while (!(*it
)->callbacks
[a
].empty()) {
527 (*it
)->callbacks
[a
].pop_front();
530 // ungrab the button on all clients
531 for (int sn
= 0; sn
< ScreenCount(**otk::display
); ++sn
) {
532 Screen
*s
= openbox
->screen(sn
);
533 if (!s
) continue; // not managed
534 Client::List::iterator c_it
, c_end
= s
->clients
.end();
535 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
536 grabButton(false, (*it
)->binding
, (MouseContext::MC
)i
, *c_it
);
543 void Bindings::grabButton(bool grab
, const Binding
&b
,
544 MouseContext::MC context
, Client
*client
)
547 int mode
= GrabModeAsync
;
550 case MouseContext::Frame
:
551 win
= client
->frame
->window();
552 mask
= ButtonPressMask
| ButtonMotionMask
| ButtonReleaseMask
;
554 case MouseContext::Window
:
555 win
= client
->frame
->plate();
556 mode
= GrabModeSync
; // this is handled in fireButton
557 mask
= ButtonPressMask
; // can't catch more than this with Sync mode
558 // the release event is manufactured by the
559 // master buttonPressHandler
562 // any other elements already get button events, don't grab on them
566 otk::display
->grabButton(b
.key
, b
.modifiers
, win
, false, mask
, mode
,
567 GrabModeAsync
, None
, None
, false);
569 otk::display
->ungrabButton(b
.key
, b
.modifiers
, win
);
572 void Bindings::grabButtons(bool grab
, Client
*client
)
574 for (int i
= 0; i
< MouseContext::NUM_MOUSE_CONTEXT
; ++i
) {
575 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
576 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
577 grabButton(grab
, (*it
)->binding
, (MouseContext::MC
)i
, client
);
581 void Bindings::fireButton(MouseData
*data
)
583 if (data
->context
== MouseContext::Window
) {
584 // Replay the event, so it goes to the client
585 XAllowEvents(**otk::display
, ReplayPointer
, data
->time
);
588 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
589 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
590 if ((*it
)->binding
.key
== data
->button
&&
591 (*it
)->binding
.modifiers
== data
->state
) {
592 MouseCallbackList::iterator c_it
,
593 c_end
= (*it
)->callbacks
[data
->action
].end();
594 for (c_it
= (*it
)->callbacks
[data
->action
].begin();
595 c_it
!= c_end
; ++c_it
)
601 bool Bindings::addEvent(EventAction::EA action
, EventCallback callback
,
604 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
608 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
609 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
610 XkbBellNotifyMask
, XkbBellNotifyMask
);
612 _eventlist
[action
].push_back(EventCallbackData(callback
, data
));
616 bool Bindings::removeEvent(EventAction::EA action
, EventCallback callback
,
619 if (action
< 0 || action
>= EventAction::NUM_EVENT_ACTION
) {
623 EventCallbackList::iterator it
= std::find(_eventlist
[action
].begin(),
624 _eventlist
[action
].end(),
625 EventCallbackData(callback
,
627 if (it
!= _eventlist
[action
].end()) {
628 _eventlist
[action
].erase(it
);
630 if (action
== EventAction::Bell
&& _eventlist
[action
].empty())
631 XkbSelectEvents(**otk::display
, XkbUseCoreKbd
,
632 XkbBellNotifyMask
, 0);
639 void Bindings::removeAllEvents()
641 for (int i
= 0; i
< EventAction::NUM_EVENT_ACTION
; ++i
) {
642 while (!_eventlist
[i
].empty()) {
643 _eventlist
[i
].pop_front();
648 void Bindings::fireEvent(EventData
*data
)
650 EventCallbackList::iterator c_it
, c_end
= _eventlist
[data
->action
].end();
651 for (c_it
= _eventlist
[data
->action
].begin(); c_it
!= c_end
; ++c_it
)