1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // keytree.cc for Epistrophy - 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.
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
35 keytree::keytree(Display *display, epist *ep)
36 : _display(display), _timeout_screen(NULL), _timer(NULL), _epist(ep)
40 _head->action = NULL; // head's action is always NULL
42 // for complete initialization, initialize() has to be called as well. We
43 // call initialize() when we are certain that the config object (which the
44 // timer uses) has been fully initialized. (see parser::parse())
53 void keytree::unloadBindings()
55 ChildList::iterator it, end = _head->children.end();
56 for (it = _head->children.begin(); it != end; ++it)
59 _head->children.clear();
63 void keytree::clearTree(keynode *node)
68 ChildList::iterator it, end = node->children.end();
69 for (it = node->children.begin(); it != end; ++it)
72 node->children.clear();
80 void keytree::grabDefaults(screen *scr)
82 grabChildren(_head, scr);
85 void keytree::ungrabDefaults(screen *scr)
87 ChildList::const_iterator it, end = _head->children.end();
88 for (it = _head->children.begin(); it != end; ++it)
89 if ( (*it)->action && (*it)->action->type() != Action::toggleGrabs)
90 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
93 void keytree::grabChildren(keynode *node, screen *scr)
95 ChildList::const_iterator it, end = node->children.end();
96 for (it = node->children.begin(); it != end; ++it)
98 scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
101 void keytree::ungrabChildren(keynode *node, screen *scr)
103 ChildList::const_iterator head_it, head_end = _head->children.end();
104 ChildList::const_iterator it, end = node->children.end();
107 // when ungrabbing children, make sure that we don't ungrab any topmost keys
108 // (children of the head node) This would render those topmost keys useless.
109 // Topmost keys are _never_ ungrabbed, since they are only grabbed at startup
111 for (it = node->children.begin(); it != end; ++it) {
112 if ( (*it)->action ) {
113 for (head_it = _head->children.begin(); head_it != head_end; ++head_it) {
114 if ( (*it)->action->modifierMask() == (*head_it)->action->modifierMask() &&
115 (*it)->action->keycode() == (*head_it)->action->keycode())
123 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
130 const Action * keytree::getAction(const XEvent &e, unsigned int state,
135 // we're done with the children. ungrab them
136 if (_current != _head)
137 ungrabChildren(_current, scr);
139 ChildList::const_iterator it, end = _current->children.end();
140 for (it = _current->children.begin(); it != end; ++it) {
142 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
143 if (act->type() == Action::cancelChain) {
144 // user is cancelling the chain explicitly
146 return (const Action *)NULL;
148 else if ( isLeaf(*it) ) {
149 // node is a leaf, so an action will be executed
150 if (_timer->isTiming()) {
152 _timeout_screen = NULL;
159 // node is not a leaf, so we advance down the tree, and grab the
160 // children of the new current node. no action is executed
161 if (_timer->isTiming())
164 _timeout_screen = scr;
167 grabChildren(_current, scr);
168 return (const Action *)NULL;
173 // action not found. back to the head
175 return (const Action *)NULL;
178 void keytree::addAction(Action::ActionType action, unsigned int mask,
179 string key, string arg)
181 keynode *tmp = new keynode;
183 if (action == Action::toggleGrabs && _current != _head) {
184 // the toggleGrabs key can only be set up as a root key, since if
185 // it was a chain key, we'd have to not ungrab the whole chain up
186 // to that key. which kinda defeats the purpose of this function.
190 tmp->action = new Action(action,
191 XKeysymToKeycode(_display,
192 XStringToKeysym(key.c_str())),
194 tmp->parent = _current;
195 _current->children.push_back(tmp);
198 void keytree::advanceOnNewNode()
200 keynode *tmp = new keynode;
202 tmp->parent = _current;
203 _current->children.push_back(tmp);
207 void keytree::retract()
209 if (_current != _head)
210 _current = _current->parent;
213 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
214 string key, string arg)
216 if (_current->action)
217 delete _current->action;
219 _current->action = new Action(action,
220 XKeysymToKeycode(_display,
221 XStringToKeysym(key.c_str())),
225 void keytree::initialize(void)
229 _epist->getConfig()->getValue(Config::chainTimeout, tval);
230 _timer = new BTimer(_epist, this);
233 tval = 3000; // set default timeout to 3 seconds
235 _timer->setTimeout(tval);
238 void keytree::timeout(void)
240 assert(_timeout_screen != NULL);
242 if (_current != _head) {
243 ungrabChildren(_current, _timeout_screen);
246 _timeout_screen = NULL;