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
) 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());
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::OBDisplay::display
, sym
)))
101 printf(_("No valid keycode for Key in key binding: %s\n"), key
.c_str());
105 static void destroytree(BindingTree
*tree
)
108 BindingTree
*c
= tree
->first_child
;
114 BindingTree
*OBBindings::buildtree(const StringVect
&keylist
, int id
) const
116 if (keylist
.empty()) return 0; // nothing in the list.. return 0
118 BindingTree
*ret
= 0, *p
;
120 StringVect::const_reverse_iterator it
, end
= keylist
.rend();
121 for (it
= keylist
.rbegin(); it
!= end
; ++it
) {
123 ret
= new BindingTree(id
);
124 if (!p
) ret
->chain
= false; // only the first built node
125 ret
->first_child
= p
;
126 if (!translate(*it
, ret
->binding
, true)) {
136 OBBindings::OBBindings()
137 : _curpos(&_keytree
), _resetkey(0,0)
139 setResetKey("C-g"); // set the default reset key
143 OBBindings::~OBBindings()
150 void OBBindings::assimilate(BindingTree
*node
)
152 BindingTree
*a
, *b
, *tmp
, *last
;
154 if (!_keytree
.first_child
) {
155 // there are no nodes at this level yet
156 _keytree
.first_child
= node
;
158 a
= _keytree
.first_child
;
163 if (a
->binding
!= b
->binding
) {
172 if (last
->binding
!= b
->binding
)
173 last
->next_sibling
= b
;
175 last
->first_child
= b
->first_child
;
182 int OBBindings::find_key(BindingTree
*search
) const {
184 a
= _keytree
.first_child
;
187 if (a
->binding
!= b
->binding
) {
190 if (a
->chain
== b
->chain
) {
192 return a
->id
; // found it! (return the actual id, not the search's)
195 return -2; // the chain status' don't match (conflict!)
201 return -1; // it just isn't in here
205 bool OBBindings::add_key(const StringVect
&keylist
, int id
)
209 if (!(tree
= buildtree(keylist
, id
)))
210 return false; // invalid binding requested
212 if (find_key(tree
) != -1) {
213 // conflicts with another binding
220 // assimilate this built tree into the main tree
221 assimilate(tree
); // assimilation destroys/uses the tree
229 int OBBindings::find_key(const StringVect
&keylist
)
234 if (!(tree
= buildtree(keylist
, 0)))
235 return false; // invalid binding requested
237 ret
= find_key(tree
) >= 0;
245 int OBBindings::remove_key(const StringVect
&keylist
)
248 assert(false); // XXX: function not implemented yet
260 void OBBindings::setResetKey(const std::string
&key
)
263 if (translate(key
, b
, true)) {
265 _resetkey
.key
= b
.key
;
266 _resetkey
.modifiers
= b
.modifiers
;
272 static void remove_branch(BindingTree
*first
)
274 BindingTree
*p
= first
;
278 remove_branch(p
->first_child
);
279 BindingTree
*s
= p
->next_sibling
;
286 void OBBindings::remove_all()
288 if (_keytree
.first_child
) {
289 remove_branch(_keytree
.first_child
);
290 _keytree
.first_child
= 0;
295 void OBBindings::grabKeys(bool grab
)
297 for (int i
= 0; i
< Openbox::instance
->screenCount(); ++i
) {
298 Window root
= otk::OBDisplay::screenInfo(i
)->rootWindow();
300 BindingTree
*p
= _curpos
->first_child
;
303 otk::OBDisplay::grabKey(p
->binding
.key
, p
->binding
.modifiers
,
304 root
, false, GrabModeAsync
, GrabModeAsync
,
307 otk::OBDisplay::ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
313 otk::OBDisplay::grabKey(_resetkey
.key
, _resetkey
.modifiers
,
314 root
, true, GrabModeAsync
, GrabModeAsync
,
317 otk::OBDisplay::ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
323 void OBBindings::fire(Window window
, unsigned int modifiers
, unsigned int key
,
326 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
331 BindingTree
*p
= _curpos
->first_child
;
333 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
339 python_callback_binding(p
->id
, type
, window
, modifiers
, key
, time
);