1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // keytree.cc for Epistophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
25 keytree::keytree(Display *display) : _display(display)
29 _head->action = NULL; // head's action is always NULL
38 void keytree::clearTree(keynode *node)
43 ChildList::iterator it, end = node->children.end();
44 for (it = node->children.begin(); it != end; ++it)
52 void keytree::grabDefaults(screen *scr)
54 grabChildren(_head, scr);
57 void keytree::grabChildren(keynode *node, screen *scr)
59 ChildList::const_iterator it, end = node->children.end();
60 for (it = node->children.begin(); it != end; ++it)
62 scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
65 void keytree::ungrabChildren(keynode *node, screen *scr)
67 ChildList::const_iterator it, end = node->children.end();
68 for (it = node->children.begin(); it != end; ++it)
70 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
73 const Action * keytree::getAction(const XEvent &e, unsigned int state,
78 if (_current != _head)
79 ungrabChildren(_current, scr);
81 ChildList::const_iterator it, end = _current->children.end();
82 for (it = _current->children.begin(); it != end; ++it) {
84 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
86 if (_current != _head)
87 ungrabChildren(_current, scr);
93 grabChildren(_current, scr);
94 return (const Action *)NULL;
99 // action not found. back to the head
101 return (const Action *)NULL;
104 void keytree::addAction(Action::ActionType action, unsigned int mask,
105 string key, string arg)
107 // can't grab non-modifier as topmost key
108 if (_current == _head && (mask == 0 || mask == ShiftMask))
111 keynode *tmp = new keynode;
112 tmp->action = new Action(action,
113 XKeysymToKeycode(_display,
114 XStringToKeysym(key.c_str())),
116 tmp->parent = _current;
117 _current->children.push_back(tmp);
120 void keytree::advanceOnNewNode()
122 keynode *tmp = new keynode;
124 tmp->parent = _current;
125 _current->children.push_back(tmp);
129 void keytree::retract()
131 if (_current != _head)
132 _current = _current->parent;
135 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
136 string key, string arg)
138 if (_current->action)
139 delete _current->action;
140 _current->action = new Action(action,
141 XKeysymToKeycode(_display,
142 XStringToKeysym(key.c_str())),