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()
161 void OBBindings::assimilate(KeyBindingTree
*node
)
163 KeyBindingTree
*a
, *b
, *tmp
, *last
;
165 if (!_keytree
.first_child
) {
166 // there are no nodes at this level yet
167 _keytree
.first_child
= node
;
169 a
= _keytree
.first_child
;
174 if (a
->binding
!= b
->binding
) {
183 if (last
->binding
!= b
->binding
)
184 last
->next_sibling
= b
;
186 last
->first_child
= b
->first_child
;
193 PyObject
*OBBindings::find(KeyBindingTree
*search
, bool *conflict
) const {
195 KeyBindingTree
*a
, *b
;
196 a
= _keytree
.first_child
;
199 if (a
->binding
!= b
->binding
) {
202 if (a
->chain
== b
->chain
) {
204 // found it! (return the actual id, not the search's)
209 return 0; // the chain status' don't match (conflict!)
215 return 0; // it just isn't in here
219 bool OBBindings::addKey(const StringVect
&keylist
, PyObject
*callback
)
221 KeyBindingTree
*tree
;
224 if (!(tree
= buildtree(keylist
, callback
)))
225 return false; // invalid binding requested
227 if (find(tree
, &conflict
) || conflict
) {
228 // conflicts with another binding
235 // assimilate this built tree into the main tree
236 assimilate(tree
); // assimilation destroys/uses the tree
246 bool OBBindings::removeKey(const StringVect
&keylist
)
248 assert(false); // XXX: function not implemented yet
250 KeyBindingTree
*tree
;
253 if (!(tree
= buildtree(keylist
, 0)))
254 return false; // invalid binding requested
256 PyObject
*func
= find(tree
, &conflict
);
262 // XXX do shit here ...
272 void OBBindings::setResetKey(const std::string
&key
)
275 if (translate(key
, b
)) {
277 _resetkey
.key
= b
.key
;
278 _resetkey
.modifiers
= b
.modifiers
;
284 static void remove_branch(KeyBindingTree
*first
)
286 KeyBindingTree
*p
= first
;
290 remove_branch(p
->first_child
);
291 KeyBindingTree
*s
= p
->next_sibling
;
292 Py_XDECREF(p
->callback
);
299 void OBBindings::removeAllKeys()
302 if (_keytree
.first_child
) {
303 remove_branch(_keytree
.first_child
);
304 _keytree
.first_child
= 0;
310 void OBBindings::grabKeys(bool grab
)
312 for (int i
= 0; i
< Openbox::instance
->screenCount(); ++i
) {
313 Window root
= otk::OBDisplay::screenInfo(i
)->rootWindow();
315 KeyBindingTree
*p
= _curpos
->first_child
;
318 otk::OBDisplay::grabKey(p
->binding
.key
, p
->binding
.modifiers
,
319 root
, false, GrabModeAsync
, GrabModeAsync
,
323 otk::OBDisplay::ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
329 otk::OBDisplay::grabKey(_resetkey
.key
, _resetkey
.modifiers
,
330 root
, true, GrabModeAsync
, GrabModeAsync
,
333 otk::OBDisplay::ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
339 void OBBindings::fireKey(unsigned int modifiers
, unsigned int key
, Time time
)
341 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
344 KeyBindingTree
*p
= _curpos
->first_child
;
346 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
348 _timer
.start(); // start/restart the timer
354 OBClient
*c
= Openbox::instance
->focusedClient();
355 if (c
) win
= c
->window();
356 KeyData
*data
= new_key_data(win
, time
, modifiers
, key
);
357 python_callback(p
->callback
, (PyObject
*)data
);
358 Py_DECREF((PyObject
*)data
);
368 void OBBindings::reset(OBBindings
*self
)
371 self
->grabKeys(false);
372 self
->_curpos
= &self
->_keytree
;
373 self
->grabKeys(true);
377 bool OBBindings::addButton(const std::string
&but
, MouseContext context
,
378 MouseAction action
, PyObject
*callback
)
380 assert(context
>= 0 && context
< NUM_MOUSE_CONTEXT
);
383 if (!translate(but
, b
, false))
386 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
388 // look for a duplicate binding
389 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
390 if ((*it
)->binding
.key
== b
.key
&&
391 (*it
)->binding
.modifiers
== b
.modifiers
) {
392 if ((*it
)->callback
[action
] == 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 // XXX GRAB the new button everywhere!
408 Py_XDECREF(bind
->callback
[action
]); // if it was already bound, unbind it
409 bind
->callback
[action
] = callback
;
414 void OBBindings::removeAllButtons()
417 for (int i
= i
; i
< NUM_MOUSE_CONTEXT
; ++i
) {
418 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
419 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
420 for (int a
= 0; a
< NUM_MOUSE_ACTION
; ++a
) {
421 Py_XDECREF((*it
)->callback
[a
]);
422 (*it
)->callback
[a
] = 0;
427 void OBBindings::grabButtons(bool grab
, OBClient
*client
)
429 for (int i
= 0; i
< NUM_MOUSE_CONTEXT
; ++i
) {
431 int mode
= GrabModeAsync
;
434 win
= client
->frame
->window();
437 win
= client
->frame
->plate();
438 mode
= GrabModeSync
; // this is handled in the plate's buttonPressHandler
441 // any other elements already get button events, don't grab on them
444 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
445 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
447 otk::OBDisplay::grabButton((*it
)->binding
.key
,
448 (*it
)->binding
.modifiers
, win
, false,
449 ButtonPressMask
| ButtonMotionMask
|
450 ButtonReleaseMask
, mode
, GrabModeAsync
,
453 otk::OBDisplay::ungrabButton((*it
)->binding
.key
,
454 (*it
)->binding
.modifiers
, win
);
458 void OBBindings::fireButton(ButtonData
*data
)
460 printf("but.mods %d.%d\n", data
->button
, data
->state
);
462 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
463 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
464 if ((*it
)->binding
.key
== data
->button
&&
465 (*it
)->binding
.modifiers
== data
->state
) {
466 if ((*it
)->callback
[data
->action
])
467 python_callback((*it
)->callback
[data
->action
], (PyObject
*)data
);