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.
31 keytree::keytree(Display *display, epist *ep)
32 : _display(display), _timeout_screen(NULL), _timer(NULL), _epist(ep)
36 _head->action = NULL; // head's action is always NULL
38 // for complete initialization, initialize() has to be called as well. We
39 // call initialize() when we are certain that the config object (which the
40 // timer uses) has been fully initialized. (see parser::parse())
49 void keytree::unloadBindings()
51 ChildList::iterator it, end = _head->children.end();
52 for (it = _head->children.begin(); it != end; ++it)
55 _head->children.clear();
59 void keytree::clearTree(keynode *node)
64 ChildList::iterator it, end = node->children.end();
65 for (it = node->children.begin(); it != end; ++it)
68 node->children.clear();
76 void keytree::grabDefaults(screen *scr)
78 grabChildren(_head, scr);
81 void keytree::ungrabDefaults(screen *scr)
83 ChildList::const_iterator it, end = _head->children.end();
84 for (it = _head->children.begin(); it != end; ++it)
85 if ( (*it)->action && (*it)->action->type() != Action::toggleGrabs)
86 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
89 void keytree::grabChildren(keynode *node, screen *scr)
91 ChildList::const_iterator it, end = node->children.end();
92 for (it = node->children.begin(); it != end; ++it)
94 scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
97 void keytree::ungrabChildren(keynode *node, screen *scr)
99 ChildList::const_iterator head_it, head_end = _head->children.end();
100 ChildList::const_iterator it, end = node->children.end();
103 // when ungrabbing children, make sure that we don't ungrab any topmost keys
104 // (children of the head node) This would render those topmost keys useless.
105 // Topmost keys are _never_ ungrabbed, since they are only grabbed at startup
107 for (it = node->children.begin(); it != end; ++it) {
108 if ( (*it)->action ) {
109 for (head_it = _head->children.begin(); head_it != head_end; ++head_it) {
110 if ( (*it)->action->modifierMask() == (*head_it)->action->modifierMask() &&
111 (*it)->action->keycode() == (*head_it)->action->keycode())
119 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
126 const Action * keytree::getAction(const XEvent &e, unsigned int state,
131 // we're done with the children. ungrab them
132 if (_current != _head)
133 ungrabChildren(_current, scr);
135 ChildList::const_iterator it, end = _current->children.end();
136 for (it = _current->children.begin(); it != end; ++it) {
138 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
139 if (act->type() == Action::cancelChain) {
140 // user is cancelling the chain explicitly
142 return (const Action *)NULL;
144 else if ( isLeaf(*it) ) {
145 // node is a leaf, so an action will be executed
146 if (_timer->isTiming()) {
148 _timeout_screen = NULL;
155 // node is not a leaf, so we advance down the tree, and grab the
156 // children of the new current node. no action is executed
157 if (_timer->isTiming())
160 _timeout_screen = scr;
163 grabChildren(_current, scr);
164 return (const Action *)NULL;
169 // action not found. back to the head
171 return (const Action *)NULL;
174 void keytree::addAction(Action::ActionType action, unsigned int mask,
175 string key, string arg)
177 keynode *tmp = new keynode;
179 if (action == Action::toggleGrabs && _current != _head) {
180 // the toggleGrabs key can only be set up as a root key, since if
181 // it was a chain key, we'd have to not ungrab the whole chain up
182 // to that key. which kinda defeats the purpose of this function.
186 tmp->action = new Action(action,
187 XKeysymToKeycode(_display,
188 XStringToKeysym(key.c_str())),
190 tmp->parent = _current;
191 _current->children.push_back(tmp);
194 void keytree::advanceOnNewNode()
196 keynode *tmp = new keynode;
198 tmp->parent = _current;
199 _current->children.push_back(tmp);
203 void keytree::retract()
205 if (_current != _head)
206 _current = _current->parent;
209 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
210 string key, string arg)
212 if (_current->action)
213 delete _current->action;
215 _current->action = new Action(action,
216 XKeysymToKeycode(_display,
217 XStringToKeysym(key.c_str())),
221 void keytree::initialize(void)
225 _epist->getConfig()->getNumberValue(Config::chainTimeout, tval);
226 _timer = new BTimer(_epist, this);
229 tval = 3000; // set default timeout to 3 seconds
231 _timer->setTimeout(tval);
234 void keytree::timeout(void)
236 assert(_timeout_screen != NULL);
238 if (_current != _head) {
239 ungrabChildren(_current, _timeout_screen);
242 _timeout_screen = NULL;