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
36 keytree::keytree(Display *display, epist *ep)
37 : _display(display), _timeout_screen(NULL), _timer(NULL), _epist(ep)
41 _head->action = NULL; // head's action is always NULL
43 // for complete initialization, initialize() has to be called as well. We
44 // call initialize() when we are certain that the config object (which the
45 // timer uses) has been fully initialized. (see parser::parse())
54 void keytree::unloadBindings()
56 ChildList::iterator it, end = _head->children.end();
57 for (it = _head->children.begin(); it != end; ++it)
60 _head->children.clear();
64 void keytree::clearTree(keynode *node)
69 ChildList::iterator it, end = node->children.end();
70 for (it = node->children.begin(); it != end; ++it)
73 node->children.clear();
81 void keytree::grabDefaults(screen *scr)
83 grabChildren(_head, scr);
86 void keytree::ungrabDefaults(screen *scr)
90 ChildList::const_iterator it, end = _head->children.end();
91 for (it = _head->children.begin(); it != end; ++it) {
93 if (act && act->type() != Action::toggleGrabs)
94 scr->ungrabKey(act->keycode(), act->modifierMask());
98 void keytree::grabChildren(keynode *node, screen *scr)
102 ChildList::const_iterator it, end = node->children.end();
103 for (it = node->children.begin(); it != end; ++it) {
106 scr->grabKey(act->keycode(), act->modifierMask());
110 void keytree::ungrabChildren(keynode *node, screen *scr)
112 ChildList::const_iterator head_it, head_end = _head->children.end();
113 ChildList::const_iterator it, end = node->children.end();
116 // when ungrabbing children, make sure that we don't ungrab any topmost keys
117 // (children of the head node) This would render those topmost keys useless.
118 // Topmost keys are _never_ ungrabbed, since they are only grabbed at startup
120 for (it = node->children.begin(); it != end; ++it) {
121 if ( (*it)->action ) {
122 for (head_it = _head->children.begin(); head_it != head_end; ++head_it) {
123 if ( (*it)->action->modifierMask() == (*head_it)->action->modifierMask() &&
124 (*it)->action->keycode() == (*head_it)->action->keycode())
132 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
139 const Action * keytree::getAction(const XEvent &e, unsigned int state,
144 // we're done with the children. ungrab them
145 if (_current != _head)
146 ungrabChildren(_current, scr);
148 ChildList::const_iterator it, end = _current->children.end();
149 for (it = _current->children.begin(); it != end; ++it) {
151 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
152 if (act->type() == Action::cancelChain) {
153 // user is cancelling the chain explicitly
155 return (const Action *)NULL;
157 else if ( isLeaf(*it) ) {
158 // node is a leaf, so an action will be executed
159 if (_timer->isTiming()) {
161 _timeout_screen = NULL;
168 // node is not a leaf, so we advance down the tree, and grab the
169 // children of the new current node. no action is executed
170 if (_timer->isTiming())
173 _timeout_screen = scr;
176 grabChildren(_current, scr);
177 return (const Action *)NULL;
182 // action not found. back to the head
184 return (const Action *)NULL;
187 void keytree::addAction(Action::ActionType action, unsigned int mask,
188 string key, string arg)
190 if (action == Action::toggleGrabs && _current != _head) {
191 // the toggleGrabs key can only be set up as a root key, since if
192 // it was a chain key, we'd have to not ungrab the whole chain up
193 // to that key. which kinda defeats the purpose of this function.
197 KeySym sym = XStringToKeysym(key.c_str());
198 keynode *tmp = new keynode;
200 tmp->action = new Action(action,
201 XKeysymToKeycode(_display, sym),
203 tmp->parent = _current;
204 _current->children.push_back(tmp);
207 void keytree::advanceOnNewNode()
209 keynode *tmp = new keynode;
211 tmp->parent = _current;
212 _current->children.push_back(tmp);
216 void keytree::retract()
218 if (_current != _head)
219 _current = _current->parent;
222 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
223 string key, string arg)
225 if (_current->action)
226 delete _current->action;
228 KeySym sym = XStringToKeysym(key.c_str());
229 _current->action = new Action(action,
230 XKeysymToKeycode(_display, sym),
234 void keytree::initialize(void)
238 _epist->getConfig()->getValue(Config::chainTimeout, tval);
239 _timer = new BTimer(_epist, this);
242 tval = 3000; // set default timeout to 3 seconds
244 _timer->setTimeout(tval);
247 void keytree::timeout(void)
249 assert(_timeout_screen != NULL);
251 if (_current != _head) {
252 ungrabChildren(_current, _timeout_screen);
255 _timeout_screen = NULL;