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)
24 static bool buttonvalue(const std::string
&button
, unsigned int *val
)
26 if (button
== "1" || button
== "Button1") {
28 } else if (button
== "2" || button
== "Button2") {
30 } else if (button
== "3" || button
== "Button3") {
32 } else if (button
== "4" || button
== "Button4") {
34 } else if (button
== "5" || button
== "Button5") {
41 static bool modvalue(const std::string
&mod
, unsigned int *val
)
43 if (mod
== "C") { // control
45 } else if (mod
== "S") { // shift
47 } else if (mod
== "A" || // alt/mod1
52 } else if (mod
== "Mod2" || // mod2
55 } else if (mod
== "Mod3" || // mod3
58 } else if (mod
== "W" || // windows/mod4
62 } else if (mod
== "Mod5" || // mod5
71 bool OBBindings::translate(const std::string
&str
, Binding
&b
,bool askey
) const
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
);
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
);
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());
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());
101 if (!(b
.key
= XKeysymToKeycode(otk::OBDisplay::display
, sym
)))
102 printf(_("No valid keycode for Key in key binding: %s\n"), key
.c_str());
105 return buttonvalue(key
, &b
.key
);
109 static void destroytree(KeyBindingTree
*tree
)
112 KeyBindingTree
*c
= tree
->first_child
;
118 KeyBindingTree
*OBBindings::buildtree(const StringVect
&keylist
,
119 PyObject
*callback
) const
121 if (keylist
.empty()) return 0; // nothing in the list.. return 0
123 KeyBindingTree
*ret
= 0, *p
;
125 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
126 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
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
)) {
141 OBBindings::OBBindings()
142 : _curpos(&_keytree
),
144 _timer(Openbox::instance
->timerManager(),
145 (otk::OBTimeoutHandler
)reset
, this)
147 _timer
.setTimeout(5000); // chains reset after 5 seconds
149 setResetKey("C-g"); // set the default reset key
153 OBBindings::~OBBindings()
160 void OBBindings::assimilate(KeyBindingTree
*node
)
162 KeyBindingTree
*a
, *b
, *tmp
, *last
;
164 if (!_keytree
.first_child
) {
165 // there are no nodes at this level yet
166 _keytree
.first_child
= node
;
168 a
= _keytree
.first_child
;
173 if (a
->binding
!= b
->binding
) {
182 if (last
->binding
!= b
->binding
)
183 last
->next_sibling
= b
;
185 last
->first_child
= b
->first_child
;
192 PyObject
*OBBindings::find(KeyBindingTree
*search
, bool *conflict
) const {
194 KeyBindingTree
*a
, *b
;
195 a
= _keytree
.first_child
;
198 if (a
->binding
!= b
->binding
) {
201 if (a
->chain
== b
->chain
) {
203 // found it! (return the actual id, not the search's)
208 return 0; // the chain status' don't match (conflict!)
214 return 0; // it just isn't in here
218 bool OBBindings::add(const StringVect
&keylist
, PyObject
*callback
)
220 KeyBindingTree
*tree
;
223 if (!(tree
= buildtree(keylist
, callback
)))
224 return false; // invalid binding requested
226 if (find(tree
, &conflict
) || conflict
) {
227 // conflicts with another binding
234 // assimilate this built tree into the main tree
235 assimilate(tree
); // assimilation destroys/uses the tree
245 bool OBBindings::remove(const StringVect
&keylist
)
247 assert(false); // XXX: function not implemented yet
249 KeyBindingTree
*tree
;
252 if (!(tree
= buildtree(keylist
, 0)))
253 return false; // invalid binding requested
255 PyObject
*func
= find(tree
, &conflict
);
261 // XXX do shit here ...
271 void OBBindings::setResetKey(const std::string
&key
)
274 if (translate(key
, b
)) {
276 _resetkey
.key
= b
.key
;
277 _resetkey
.modifiers
= b
.modifiers
;
283 static void remove_branch(KeyBindingTree
*first
)
285 KeyBindingTree
*p
= first
;
289 remove_branch(p
->first_child
);
290 KeyBindingTree
*s
= p
->next_sibling
;
291 Py_XDECREF(p
->callback
);
298 void OBBindings::removeAll()
300 if (_keytree
.first_child
) {
301 remove_branch(_keytree
.first_child
);
302 _keytree
.first_child
= 0;
307 void OBBindings::grabKeys(bool grab
)
309 for (int i
= 0; i
< Openbox::instance
->screenCount(); ++i
) {
310 Window root
= otk::OBDisplay::screenInfo(i
)->rootWindow();
312 KeyBindingTree
*p
= _curpos
->first_child
;
315 otk::OBDisplay::grabKey(p
->binding
.key
, p
->binding
.modifiers
,
316 root
, false, GrabModeAsync
, GrabModeAsync
,
320 otk::OBDisplay::ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
326 otk::OBDisplay::grabKey(_resetkey
.key
, _resetkey
.modifiers
,
327 root
, true, GrabModeAsync
, GrabModeAsync
,
330 otk::OBDisplay::ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
336 void OBBindings::fire(unsigned int modifiers
, unsigned int key
, Time time
)
338 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
341 KeyBindingTree
*p
= _curpos
->first_child
;
343 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
345 _timer
.start(); // start/restart the timer
351 OBClient
*c
= Openbox::instance
->focusedClient();
352 if (c
) win
= c
->window();
353 KeyData
*data
= new_key_data(win
, time
, modifiers
, key
);
354 python_callback(p
->callback
, (PyObject
*)data
);
355 Py_DECREF((PyObject
*)data
);
365 void OBBindings::reset(OBBindings
*self
)
368 self
->grabKeys(false);
369 self
->_curpos
= &self
->_keytree
;
370 self
->grabKeys(true);
374 bool OBBindings::addButton(const std::string
&but
, MouseContext context
,
375 MouseAction action
, PyObject
*callback
)
377 assert(context
>= 0 && context
< NUM_MOUSE_CONTEXT
);
380 if (!translate(but
, b
, false))
383 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
385 // look for a duplicate binding
386 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
387 if ((*it
)->binding
.key
== b
.key
&&
388 (*it
)->binding
.modifiers
== b
.modifiers
) {
389 ButtonBinding::CallbackList::iterator c_it
,
390 c_end
= (*it
)->callback
[action
].end();
391 for (c_it
= (*it
)->callback
[action
].begin(); c_it
!= c_end
; ++c_it
)
392 if (*c_it
== callback
)
393 return true; // already bound
399 // the binding didnt exist yet, add it
401 bind
= new ButtonBinding();
402 bind
->binding
.key
= b
.key
;
403 bind
->binding
.modifiers
= b
.modifiers
;
404 _buttons
[context
].push_back(bind
);
405 printf("adding %d.%d to %d\n", b
.key
, b
.modifiers
, context
);
406 // XXX GRAB the new button everywhere!
409 bind
->callback
[action
].push_back(callback
);
414 void OBBindings::grabButtons(bool grab
, OBClient
*client
)
416 for (int i
= 0; i
< NUM_MOUSE_CONTEXT
; ++i
) {
417 Window win
[3] = {0, 0, 0}; // at most 2 windows
420 win
[0] = client
->frame
->window();
423 win
[0] = client
->frame
->titlebar();
424 win
[1] = client
->frame
->label();
427 win
[0] = client
->frame
->plate();
430 win
[0] = client
->frame
->handle();
432 case MC_MaximizeButton
:
434 case MC_IconifyButton
:
435 case MC_StickyButton
:
441 assert(false); // invalid mouse context
443 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
444 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
445 for (Window
*w
= win
; *w
; ++w
) {
447 otk::OBDisplay::grabButton((*it
)->binding
.key
,
448 (*it
)->binding
.modifiers
, *w
, false,
449 ButtonPressMask
| ButtonMotionMask
|
450 ButtonReleaseMask
, GrabModeAsync
,
451 GrabModeAsync
, None
, None
, false);
454 otk::OBDisplay::ungrabButton((*it
)->binding
.key
,
455 (*it
)->binding
.modifiers
, *w
);
460 void OBBindings::fire(ButtonData
*data
)
462 printf("but.mods %d.%d\n", data
->button
, data
->state
);
464 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
465 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
466 if ((*it
)->binding
.key
== data
->button
&&
467 (*it
)->binding
.modifiers
== data
->state
) {
468 ButtonBinding::CallbackList::iterator c_it
,
469 c_end
= (*it
)->callback
[data
->action
].end();
470 for (c_it
= (*it
)->callback
[data
->action
].begin(); c_it
!= c_end
; ++c_it
)
471 python_callback(*c_it
, (PyObject
*)data
);