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,
74 // parse out the base key name
75 std::string::size_type keybegin = str.find_last_of('-');
76 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
77 std::string key(str, keybegin);
79 // parse out the requested modifier keys
80 unsigned int modval = 0;
81 std::string::size_type begin = 0, end;
82 while (begin != keybegin) {
83 end = str.find_first_of('-', begin);
85 std::string mod(str, begin, end-begin);
86 if (!modvalue(mod, &modval)) {
87 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
97 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
98 if (sym == NoSymbol) {
99 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
102 if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
103 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
106 if (!buttonvalue(key, &b.key)) {
107 printf(_("Invalid Button name in mouse binding: %s\n"), key.c_str());
114 static void destroytree(BindingTree *tree)
117 BindingTree *c = tree->first_child;
123 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
125 if (keylist.empty()) return 0; // nothing in the list.. return 0
127 BindingTree *ret = 0, *p;
129 StringVect::const_reverse_iterator it, end = keylist.rend();
130 for (it = keylist.rbegin(); it != end; ++it) {
132 ret = new BindingTree(id);
133 if (!p) ret->chain = false; // only the first built node
134 ret->first_child = p;
135 if (!translate(*it, ret->binding, true)) {
145 OBBindings::OBBindings()
146 : _curpos(&_keytree), _mousetree(0), _resetkey(0,0)
148 setResetKey("C-g"); // set the default reset key
152 OBBindings::~OBBindings()
154 grabMouseOnAll(false); // ungrab everything
160 bool OBBindings::add_mouse(const std::string &button, int id)
164 if (!translate(button, n.binding, false))
167 BindingTree *p = _mousetree, **newp = &_mousetree;
169 if (p->binding == n.binding)
170 return false; // conflict
172 newp = &p->next_sibling;
175 grabMouseOnAll(false); // ungrab everything
177 *newp = new BindingTree(id);
178 (*newp)->chain = false;
179 (*newp)->binding.key = n.binding.key;
180 (*newp)->binding.modifiers = n.binding.modifiers;
182 grabMouseOnAll(true);
188 int OBBindings::remove_mouse(const std::string &button)
191 assert(false); // XXX: function not implemented yet
193 grabMouseOnAll(false); // ungrab everything
197 grabMouseOnAll(true);
201 void OBBindings::assimilate(BindingTree *node)
203 BindingTree *a, *b, *tmp, *last;
205 if (!_keytree.first_child) {
206 // there are no nodes at this level yet
207 _keytree.first_child = node;
209 a = _keytree.first_child;
214 if (a->binding != b->binding) {
223 if (last->binding != b->binding)
224 last->next_sibling = b;
226 last->first_child = b->first_child;
233 int OBBindings::find_key(BindingTree *search) const {
235 a = _keytree.first_child;
238 if (a->binding != b->binding) {
241 if (a->chain == b->chain) {
243 return a->id; // found it! (return the actual id, not the search's)
246 return -2; // the chain status' don't match (conflict!)
252 return -1; // it just isn't in here
256 bool OBBindings::add_key(const StringVect &keylist, int id)
260 if (!(tree = buildtree(keylist, id)))
261 return false; // invalid binding requested
263 if (find_key(tree) != -1) {
264 // conflicts with another binding
271 // assimilate this built tree into the main tree
272 assimilate(tree); // assimilation destroys/uses the tree
280 int OBBindings::find_key(const StringVect &keylist)
285 if (!(tree = buildtree(keylist, 0)))
286 return false; // invalid binding requested
288 ret = find_key(tree) >= 0;
296 int OBBindings::remove_key(const StringVect &keylist)
299 assert(false); // XXX: function not implemented yet
311 void OBBindings::setResetKey(const std::string &key)
314 if (translate(key, b, true)) {
316 _resetkey.key = b.key;
317 _resetkey.modifiers = b.modifiers;
323 static void remove_branch(BindingTree *first)
325 BindingTree *p = first;
329 remove_branch(p->first_child);
330 BindingTree *s = p->next_sibling;
337 void OBBindings::remove_all()
339 if (_keytree.first_child) {
340 remove_branch(_keytree.first_child);
341 _keytree.first_child = 0;
343 BindingTree *p = _mousetree;
345 BindingTree *n = p->next_sibling;
353 void OBBindings::grabMouse(bool grab, const OBClient *client)
355 BindingTree *p = _mousetree;
358 otk::OBDisplay::grabButton(p->binding.key, p->binding.modifiers,
359 client->frame->window(), false,
360 ButtonMotionMask | ButtonPressMask |
361 ButtonReleaseMask, GrabModeAsync,
362 GrabModeAsync, None, None, false);
364 otk::OBDisplay::ungrabButton(p->binding.key, p->binding.modifiers,
365 client->frame->window());
371 void OBBindings::grabMouseOnAll(bool grab)
373 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
374 OBScreen *s = Openbox::instance->screen(i);
376 OBScreen::ClientList::iterator it, end = s->clients.end();
377 for (it = s->clients.begin(); it != end; ++it)
378 grabMouse(grab, *it);
383 void OBBindings::grabKeys(bool grab)
385 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
386 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
388 BindingTree *p = _curpos->first_child;
391 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
392 root, false, GrabModeAsync, GrabModeAsync,
395 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
401 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
402 root, true, GrabModeAsync, GrabModeAsync,
405 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
411 void OBBindings::fire(OBActions::ActionType type, Window window,
412 unsigned int modifiers, unsigned int key, Time time)
414 if (type == OBActions::Action_KeyPress) {
415 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
420 BindingTree *p = _curpos->first_child;
422 if (p->binding.key == key && p->binding.modifiers == modifiers) {
428 python_callback_binding(p->id, type, window, modifiers, key, time);
439 BindingTree *p = _mousetree;
441 if (p->binding.key == key && p->binding.modifiers == modifiers) {
442 python_callback_binding(p->id, type, window, modifiers, key, time);