]> Dogcows Code - chaz/openbox/blob - util/epist/keytree.cc
make epist compile with the new code base
[chaz/openbox] / util / epist / keytree.cc
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>
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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.
22
23 #ifdef HAVE_CONFIG_H
24 # include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 #include "keytree.hh"
28 #include "epist.hh"
29 #include "config.hh"
30
31 #include <string>
32 #include <iostream>
33
34 using std::string;
35
36 keytree::keytree(Display *display, epist *ep)
37 : _display(display), _timeout_screen(NULL), _timer(NULL), _epist(ep)
38 {
39 _head = new keynode;
40 _head->parent = NULL;
41 _head->action = NULL; // head's action is always NULL
42 _current = _head;
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())
46 }
47
48 keytree::~keytree()
49 {
50 clearTree(_head);
51 delete _timer;
52 }
53
54 void keytree::unloadBindings()
55 {
56 ChildList::iterator it, end = _head->children.end();
57 for (it = _head->children.begin(); it != end; ++it)
58 clearTree(*it);
59
60 _head->children.clear();
61 reset();
62 }
63
64 void keytree::clearTree(keynode *node)
65 {
66 if (!node)
67 return;
68
69 ChildList::iterator it, end = node->children.end();
70 for (it = node->children.begin(); it != end; ++it)
71 clearTree(*it);
72
73 node->children.clear();
74
75 if (node->action)
76 delete node->action;
77 delete node;
78 node = NULL;
79 }
80
81 void keytree::grabDefaults(screen *scr)
82 {
83 grabChildren(_head, scr);
84 }
85
86 void keytree::ungrabDefaults(screen *scr)
87 {
88 Action *act;
89
90 ChildList::const_iterator it, end = _head->children.end();
91 for (it = _head->children.begin(); it != end; ++it) {
92 act = (*it)->action;
93 if (act && act->type() != Action::toggleGrabs)
94 scr->ungrabKey(act->keycode(), act->modifierMask());
95 }
96 }
97
98 void keytree::grabChildren(keynode *node, screen *scr)
99 {
100 Action *act;
101
102 ChildList::const_iterator it, end = node->children.end();
103 for (it = node->children.begin(); it != end; ++it) {
104 act = (*it)->action;
105 if (act)
106 scr->grabKey(act->keycode(), act->modifierMask());
107 }
108 }
109
110 void keytree::ungrabChildren(keynode *node, screen *scr)
111 {
112 ChildList::const_iterator head_it, head_end = _head->children.end();
113 ChildList::const_iterator it, end = node->children.end();
114 bool ungrab = true;
115
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
119
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())
125 {
126 ungrab = false;
127 break;
128 }
129 }
130
131 if (ungrab)
132 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
133
134 ungrab = true;
135 }
136 }
137 }
138
139 const Action * keytree::getAction(const XEvent &e, unsigned int state,
140 screen *scr)
141 {
142 Action *act;
143
144 // we're done with the children. ungrab them
145 if (_current != _head)
146 ungrabChildren(_current, scr);
147
148 ChildList::const_iterator it, end = _current->children.end();
149 for (it = _current->children.begin(); it != end; ++it) {
150 act = (*it)->action;
151 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
152 if (act->type() == Action::cancelChain) {
153 // user is cancelling the chain explicitly
154 _current = _head;
155 return (const Action *)NULL;
156 }
157 else if ( isLeaf(*it) ) {
158 // node is a leaf, so an action will be executed
159 if (_timer->isTiming()) {
160 _timer->stop();
161 _timeout_screen = NULL;
162 }
163
164 _current = _head;
165 return act;
166 }
167 else {
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())
171 _timer->stop();
172 _timer->start();
173 _timeout_screen = scr;
174
175 _current = *it;
176 grabChildren(_current, scr);
177 return (const Action *)NULL;
178 }
179 }
180 }
181
182 // action not found. back to the head
183 _current = _head;
184 return (const Action *)NULL;
185 }
186
187 void keytree::addAction(Action::ActionType action, unsigned int mask,
188 string key, string arg)
189 {
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.
194 return;
195 }
196
197 KeySym sym = XStringToKeysym(key.c_str());
198 keynode *tmp = new keynode;
199
200 tmp->action = new Action(action,
201 XKeysymToKeycode(_display, sym),
202 mask, arg);
203 tmp->parent = _current;
204 _current->children.push_back(tmp);
205 }
206
207 void keytree::advanceOnNewNode()
208 {
209 keynode *tmp = new keynode;
210 tmp->action = NULL;
211 tmp->parent = _current;
212 _current->children.push_back(tmp);
213 _current = tmp;
214 }
215
216 void keytree::retract()
217 {
218 if (_current != _head)
219 _current = _current->parent;
220 }
221
222 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
223 string key, string arg)
224 {
225 if (_current->action)
226 delete _current->action;
227
228 KeySym sym = XStringToKeysym(key.c_str());
229 _current->action = new Action(action,
230 XKeysymToKeycode(_display, sym),
231 mask, arg);
232 }
233
234 void keytree::initialize(void)
235 {
236 int tval = 0;
237
238 _epist->getConfig()->getValue(Config::chainTimeout, tval);
239 _timer = new BTimer(_epist, this);
240
241 if (tval <= 0)
242 tval = 3000; // set default timeout to 3 seconds
243
244 _timer->setTimeout(tval);
245 }
246
247 void keytree::timeout(void)
248 {
249 assert(_timeout_screen != NULL);
250
251 if (_current != _head) {
252 ungrabChildren(_current, _timeout_screen);
253 _current = _head;
254 }
255 _timeout_screen = NULL;
256 }
This page took 0.050537 seconds and 4 git commands to generate.