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(BindingTree
*tree
)
112 BindingTree
*c
= tree
->first_child
;
118 BindingTree
*OBBindings::buildtree(const StringVect
&keylist
,
119 PyObject
*callback
) const
121 if (keylist
.empty()) return 0; // nothing in the list.. return 0
123 BindingTree
*ret
= 0, *p
;
125 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
126 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
128 ret
= new BindingTree(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()
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(BindingTree
*node
)
162 BindingTree
*a
, *b
, *tmp
, *last
;
164 if (!_tree
.first_child
) {
165 // there are no nodes at this level yet
166 _tree
.first_child
= node
;
168 a
= _tree
.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(BindingTree
*search
, bool *conflict
) const {
195 a
= _tree
.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
)
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
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(BindingTree
*first
)
285 BindingTree
*p
= first
;
289 remove_branch(p
->first_child
);
290 BindingTree
*s
= p
->next_sibling
;
291 Py_XDECREF(p
->callback
);
298 void OBBindings::removeAll()
300 if (_tree
.first_child
) {
301 remove_branch(_tree
.first_child
);
302 _tree
.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 BindingTree
*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 BindingTree
*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 python_callback(p
->callback
, win
, modifiers
, key
, time
);
363 void OBBindings::reset(OBBindings
*self
)
366 self
->grabKeys(false);
367 self
->_curpos
= &self
->_tree
;
368 self
->grabKeys(true);