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)
25 static void print_branch(const BindingTree *first, std::string str)
27 const BindingTree *p = first;
31 print_branch(p->first_child, str + " " + p->text);
33 printf("%d%s\n", p->id, (str + " " + p->text).c_str());
39 void OBBindings::display()
41 if (_keytree.first_child) {
42 printf("Key Tree:\n");
43 print_branch(_keytree.first_child, "");
46 printf("Mouse Tree:\n");
47 BindingTree *p = _mousetree;
49 printf("%d %s\n", p->id, p->text.c_str());
56 static bool buttonvalue(const std::string &button, unsigned int *val)
58 if (button == "1" || button == "Button1") {
60 } else if (button == "2" || button == "Button2") {
62 } else if (button == "3" || button == "Button3") {
64 } else if (button == "4" || button == "Button4") {
66 } else if (button == "5" || button == "Button5") {
73 static bool modvalue(const std::string &mod, unsigned int *val)
75 if (mod == "C") { // control
77 } else if (mod == "S") { // shift
79 } else if (mod == "A" || // alt/mod1
84 } else if (mod == "Mod2" || // mod2
87 } else if (mod == "Mod3" || // mod3
90 } else if (mod == "W" || // windows/mod4
94 } else if (mod == "Mod5" || // mod5
103 bool OBBindings::translate(const std::string &str, Binding &b,
106 // parse out the base key name
107 std::string::size_type keybegin = str.find_last_of('-');
108 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
109 std::string key(str, keybegin);
111 // parse out the requested modifier keys
112 unsigned int modval = 0;
113 std::string::size_type begin = 0, end;
114 while (begin != keybegin) {
115 end = str.find_first_of('-', begin);
117 std::string mod(str, begin, end-begin);
118 if (!modvalue(mod, &modval)) {
119 // printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
127 b.modifiers = modval;
129 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
130 if (sym == NoSymbol) return false;
131 b.key = XKeysymToKeycode(otk::OBDisplay::display, sym);
134 return buttonvalue(key, &b.key);
138 static void destroytree(BindingTree *tree)
141 BindingTree *c = tree->first_child;
147 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
149 if (keylist.empty()) return 0; // nothing in the list.. return 0
151 BindingTree *ret = 0, *p;
153 StringVect::const_reverse_iterator it, end = keylist.rend();
154 for (it = keylist.rbegin(); it != end; ++it) {
156 ret = new BindingTree(id);
157 if (!p) ret->chain = false; // only the first built node
158 ret->first_child = p;
159 if (!translate(*it, ret->binding, true)) {
164 ret->text = *it; // XXX: rm me
170 OBBindings::OBBindings()
171 : _curpos(&_keytree), _mousetree(0)
176 OBBindings::~OBBindings()
178 grabMouseOnAll(false); // ungrab everything
184 bool OBBindings::add_mouse(const std::string &button, int id)
188 if (!translate(button, n.binding, false))
191 BindingTree *p = _mousetree, **newp = &_mousetree;
193 if (p->binding == n.binding)
194 return false; // conflict
196 newp = &p->next_sibling;
199 grabMouseOnAll(false); // ungrab everything
201 *newp = new BindingTree(id);
202 (*newp)->text = button;
203 (*newp)->chain = false;
204 (*newp)->binding.key = n.binding.key;
205 (*newp)->binding.modifiers = n.binding.modifiers;
207 grabMouseOnAll(true);
213 int OBBindings::remove_mouse(const std::string &button)
216 assert(false); // XXX: function not implemented yet
218 grabMouseOnAll(false); // ungrab everything
222 grabMouseOnAll(true);
226 void OBBindings::assimilate(BindingTree *node)
228 BindingTree *a, *b, *tmp, *last;
230 if (!_keytree.first_child) {
231 // there are no nodes at this level yet
232 _keytree.first_child = node;
234 a = _keytree.first_child;
239 if (a->binding != b->binding) {
248 if (last->binding != b->binding)
249 last->next_sibling = b;
251 last->first_child = b->first_child;
258 int OBBindings::find_key(BindingTree *search) const {
260 a = _keytree.first_child;
263 if (a->binding != b->binding) {
266 if (a->chain == b->chain) {
268 return a->id; // found it! (return the actual id, not the search's)
271 return -2; // the chain status' don't match (conflict!)
277 return -1; // it just isn't in here
281 bool OBBindings::add_key(const StringVect &keylist, int id)
285 if (!(tree = buildtree(keylist, id)))
286 return false; // invalid binding requested
288 if (find_key(tree) != -1) {
289 // conflicts with another binding
296 // assimilate this built tree into the main tree
297 assimilate(tree); // assimilation destroys/uses the tree
305 int OBBindings::find_key(const StringVect &keylist)
310 if (!(tree = buildtree(keylist, 0)))
311 return false; // invalid binding requested
313 ret = find_key(tree) >= 0;
321 int OBBindings::remove_key(const StringVect &keylist)
324 assert(false); // XXX: function not implemented yet
336 static void remove_branch(BindingTree *first)
338 BindingTree *p = first;
342 remove_branch(p->first_child);
343 BindingTree *s = p->next_sibling;
350 void OBBindings::remove_all()
352 if (_keytree.first_child) {
353 remove_branch(_keytree.first_child);
354 _keytree.first_child = 0;
356 BindingTree *p = _mousetree;
358 BindingTree *n = p->next_sibling;
366 void OBBindings::process(unsigned int modifiers, unsigned int key)
368 BindingTree *c = _curpos->first_child;
371 if (c->binding.key == key && c->binding.modifiers == modifiers) {
377 if (!_curpos->chain) {
378 // XXX execute command for _curpos->id
379 _curpos = &_keytree; // back to the start
385 void OBBindings::grabMouse(bool grab, const OBClient *client)
387 BindingTree *p = _mousetree;
390 otk::OBDisplay::grabButton(p->binding.key, p->binding.modifiers,
391 client->frame->window(), false,
392 ButtonMotionMask | ButtonPressMask |
393 ButtonReleaseMask, GrabModeAsync,
394 GrabModeAsync, None, None, false);
396 otk::OBDisplay::ungrabButton(p->binding.key, p->binding.modifiers,
397 client->frame->window());
403 void OBBindings::grabMouseOnAll(bool grab)
405 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
406 OBScreen *s = Openbox::instance->screen(i);
408 OBScreen::ClientList::iterator it, end = s->clients.end();
409 for (it = s->clients.begin(); it != end; ++it)
410 grabMouse(grab, *it);
415 void OBBindings::grabKeys(bool grab)
417 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
418 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
420 BindingTree *p = _curpos->first_child;
423 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
424 root, false, GrabModeAsync, GrabModeAsync,
427 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
435 void OBBindings::fire(OBActions::ActionType type, Window window,
436 unsigned int modifiers, unsigned int key, Time time)
438 if (type == OBActions::Action_KeyPress) {
439 BindingTree *p = _curpos->first_child;
441 if (p->binding.key == key && p->binding.modifiers == modifiers) {
447 python_callback_binding(p->id, type, window, modifiers, key, time);
457 BindingTree *p = _mousetree;
459 if (p->binding.key == key && p->binding.modifiers == modifiers) {
460 python_callback_binding(p->id, type, window, modifiers, key, time);