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
)resetChains
, this)
147 _timer
.setTimeout(5000); // chains reset after 5 seconds
149 // setResetKey("C-g"); // set the default reset key
151 for (int i
= 0; i
< NUM_EVENTS
; ++i
)
156 OBBindings::~OBBindings()
165 void OBBindings::assimilate(KeyBindingTree
*node
)
167 KeyBindingTree
*a
, *b
, *tmp
, *last
;
169 if (!_keytree
.first_child
) {
170 // there are no nodes at this level yet
171 _keytree
.first_child
= node
;
173 a
= _keytree
.first_child
;
178 if (a
->binding
!= b
->binding
) {
187 if (last
->binding
!= b
->binding
)
188 last
->next_sibling
= b
;
190 last
->first_child
= b
->first_child
;
197 PyObject
*OBBindings::find(KeyBindingTree
*search
, bool *conflict
) const {
199 KeyBindingTree
*a
, *b
;
200 a
= _keytree
.first_child
;
203 if (a
->binding
!= b
->binding
) {
206 if (a
->chain
== b
->chain
) {
208 // found it! (return the actual id, not the search's)
213 return 0; // the chain status' don't match (conflict!)
219 return 0; // it just isn't in here
223 bool OBBindings::addKey(const StringVect
&keylist
, PyObject
*callback
)
225 KeyBindingTree
*tree
;
228 if (!(tree
= buildtree(keylist
, callback
)))
229 return false; // invalid binding requested
231 if (find(tree
, &conflict
) || conflict
) {
232 // conflicts with another binding
239 // assimilate this built tree into the main tree
240 assimilate(tree
); // assimilation destroys/uses the tree
250 bool OBBindings::removeKey(const StringVect
&keylist
)
252 assert(false); // XXX: function not implemented yet
254 KeyBindingTree
*tree
;
257 if (!(tree
= buildtree(keylist
, 0)))
258 return false; // invalid binding requested
260 PyObject
*func
= find(tree
, &conflict
);
266 // XXX do shit here ...
276 void OBBindings::setResetKey(const std::string
&key
)
279 if (translate(key
, b
)) {
281 _resetkey
.key
= b
.key
;
282 _resetkey
.modifiers
= b
.modifiers
;
288 static void remove_branch(KeyBindingTree
*first
)
290 KeyBindingTree
*p
= first
;
294 remove_branch(p
->first_child
);
295 KeyBindingTree
*s
= p
->next_sibling
;
296 Py_XDECREF(p
->callback
);
303 void OBBindings::removeAllKeys()
306 if (_keytree
.first_child
) {
307 remove_branch(_keytree
.first_child
);
308 _keytree
.first_child
= 0;
314 void OBBindings::grabKeys(bool grab
)
316 for (int i
= 0; i
< Openbox::instance
->screenCount(); ++i
) {
317 Window root
= otk::OBDisplay::screenInfo(i
)->rootWindow();
319 KeyBindingTree
*p
= _curpos
->first_child
;
322 otk::OBDisplay::grabKey(p
->binding
.key
, p
->binding
.modifiers
,
323 root
, false, GrabModeAsync
, GrabModeAsync
,
327 otk::OBDisplay::ungrabKey(p
->binding
.key
, p
->binding
.modifiers
,
334 otk::OBDisplay::grabKey(_resetkey
.key
, _resetkey
.modifiers
,
335 root
, false, GrabModeAsync
, GrabModeAsync
,
338 otk::OBDisplay::ungrabKey(_resetkey
.key
, _resetkey
.modifiers
,
344 void OBBindings::fireKey(unsigned int modifiers
, unsigned int key
, Time time
)
346 if (key
== _resetkey
.key
&& modifiers
== _resetkey
.modifiers
) {
349 KeyBindingTree
*p
= _curpos
->first_child
;
351 if (p
->binding
.key
== key
&& p
->binding
.modifiers
== modifiers
) {
353 _timer
.start(); // start/restart the timer
359 OBClient
*c
= Openbox::instance
->focusedClient();
360 if (c
) win
= c
->window();
361 KeyData
*data
= new_key_data(win
, time
, modifiers
, key
);
362 python_callback(p
->callback
, (PyObject
*)data
);
363 Py_DECREF((PyObject
*)data
);
373 void OBBindings::resetChains(OBBindings
*self
)
376 self
->grabKeys(false);
377 self
->_curpos
= &self
->_keytree
;
378 self
->grabKeys(true);
382 bool OBBindings::addButton(const std::string
&but
, MouseContext context
,
383 MouseAction action
, PyObject
*callback
)
385 assert(context
>= 0 && context
< NUM_MOUSE_CONTEXT
);
388 if (!translate(but
, b
, false))
391 ButtonBindingList::iterator it
, end
= _buttons
[context
].end();
393 // look for a duplicate binding
394 for (it
= _buttons
[context
].begin(); it
!= end
; ++it
)
395 if ((*it
)->binding
.key
== b
.key
&&
396 (*it
)->binding
.modifiers
== b
.modifiers
) {
397 if ((*it
)->callback
[action
] == callback
)
398 return true; // already bound
404 // the binding didnt exist yet, add it
406 bind
= new ButtonBinding();
407 bind
->binding
.key
= b
.key
;
408 bind
->binding
.modifiers
= b
.modifiers
;
409 _buttons
[context
].push_back(bind
);
410 // grab the button on all clients
411 for (int sn
= 0; sn
< Openbox::instance
->screenCount(); ++sn
) {
412 OBScreen
*s
= Openbox::instance
->screen(sn
);
413 OBClient::List::iterator c_it
, c_end
= s
->clients
.end();
414 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
415 grabButton(true, bind
->binding
, context
, *c_it
);
420 Py_XDECREF(bind
->callback
[action
]); // if it was already bound, unbind it
421 bind
->callback
[action
] = callback
;
426 void OBBindings::removeAllButtons()
428 for (int i
= i
; i
< NUM_MOUSE_CONTEXT
; ++i
) {
429 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
430 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
) {
431 for (int a
= 0; a
< NUM_MOUSE_ACTION
; ++a
) {
432 Py_XDECREF((*it
)->callback
[a
]);
433 (*it
)->callback
[a
] = 0;
435 // ungrab the button on all clients
436 for (int sn
= 0; sn
< Openbox::instance
->screenCount(); ++sn
) {
437 OBScreen
*s
= Openbox::instance
->screen(sn
);
438 OBClient::List::iterator c_it
, c_end
= s
->clients
.end();
439 for (c_it
= s
->clients
.begin(); c_it
!= c_end
; ++c_it
) {
440 grabButton(false, (*it
)->binding
, (MouseContext
)i
, *c_it
);
447 void OBBindings::grabButton(bool grab
, const Binding
&b
, MouseContext context
,
451 int mode
= GrabModeAsync
;
454 win
= client
->frame
->window();
457 win
= client
->frame
->plate();
458 mode
= GrabModeSync
; // this is handled in fireButton
461 // any other elements already get button events, don't grab on them
465 otk::OBDisplay::grabButton(b
.key
, b
.modifiers
, win
, false,
466 ButtonPressMask
| ButtonMotionMask
|
467 ButtonReleaseMask
, mode
, GrabModeAsync
,
470 otk::OBDisplay::ungrabButton(b
.key
, b
.modifiers
, win
);
473 void OBBindings::grabButtons(bool grab
, OBClient
*client
)
475 for (int i
= 0; i
< NUM_MOUSE_CONTEXT
; ++i
) {
476 ButtonBindingList::iterator it
, end
= _buttons
[i
].end();
477 for (it
= _buttons
[i
].begin(); it
!= end
; ++it
)
478 grabButton(grab
, (*it
)->binding
, (MouseContext
)i
, client
);
482 void OBBindings::fireButton(ButtonData
*data
)
484 if (data
->context
== MC_Window
) {
485 // these are grabbed in Sync mode to allow the press to be normal to the
487 XAllowEvents(otk::OBDisplay::display
, ReplayPointer
, data
->time
);
490 ButtonBindingList::iterator it
, end
= _buttons
[data
->context
].end();
491 for (it
= _buttons
[data
->context
].begin(); it
!= end
; ++it
)
492 if ((*it
)->binding
.key
== data
->button
&&
493 (*it
)->binding
.modifiers
== data
->state
) {
494 if ((*it
)->callback
[data
->action
])
495 python_callback((*it
)->callback
[data
->action
], (PyObject
*)data
);
500 bool OBBindings::addEvent(EventAction action
, PyObject
*callback
)
502 if (action
< 0 || action
>= NUM_EVENTS
) {
506 Py_XDECREF(_events
[action
]);
507 _events
[action
] = callback
;
512 bool OBBindings::removeEvent(EventAction action
)
514 if (action
< 0 || action
>= NUM_EVENTS
) {
518 Py_XDECREF(_events
[action
]);
523 void OBBindings::removeAllEvents()
525 for (int i
= 0; i
< NUM_EVENTS
; ++i
) {
526 Py_XDECREF(_events
[i
]);
531 void OBBindings::fireEvent(EventData
*data
)
533 if (_events
[data
->action
])
534 python_callback(_events
[data
->action
], (PyObject
*)data
);