]> Dogcows Code - chaz/openbox/blob - util/epist/keytree.cc
uber patch.
[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 #include "keytree.hh"
24 #include "epist.hh"
25 #include "config.hh"
26
27 #include <string>
28
29 using std::string;
30
31 keytree::keytree(Display *display, epist *ep)
32 : _display(display), _timeout_screen(NULL), _timer(NULL), _epist(ep)
33 {
34 _head = new keynode;
35 _head->parent = NULL;
36 _head->action = NULL; // head's action is always NULL
37 _current = _head;
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())
41 }
42
43 keytree::~keytree()
44 {
45 clearTree(_head);
46 delete _timer;
47 }
48
49 void keytree::unloadBindings()
50 {
51 ChildList::iterator it, end = _head->children.end();
52 for (it = _head->children.begin(); it != end; ++it)
53 clearTree(*it);
54
55 _head->children.clear();
56 reset();
57 }
58
59 void keytree::clearTree(keynode *node)
60 {
61 if (!node)
62 return;
63
64 ChildList::iterator it, end = node->children.end();
65 for (it = node->children.begin(); it != end; ++it)
66 clearTree(*it);
67
68 node->children.clear();
69
70 if (node->action)
71 delete node->action;
72 delete node;
73 node = NULL;
74 }
75
76 void keytree::grabDefaults(screen *scr)
77 {
78 grabChildren(_head, scr);
79 }
80
81 void keytree::ungrabDefaults(screen *scr)
82 {
83 ChildList::const_iterator it, end = _head->children.end();
84 for (it = _head->children.begin(); it != end; ++it)
85 if ( (*it)->action )
86 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
87 }
88
89 void keytree::grabChildren(keynode *node, screen *scr)
90 {
91 ChildList::const_iterator it, end = node->children.end();
92 for (it = node->children.begin(); it != end; ++it)
93 if ( (*it)->action )
94 scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
95 }
96
97 void keytree::ungrabChildren(keynode *node, screen *scr)
98 {
99 ChildList::const_iterator head_it, head_end = _head->children.end();
100 ChildList::const_iterator it, end = node->children.end();
101 bool ungrab = true;
102
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
106
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())
112 {
113 ungrab = false;
114 break;
115 }
116 }
117
118 if (ungrab)
119 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
120
121 ungrab = true;
122 }
123 }
124 }
125
126 const Action * keytree::getAction(const XEvent &e, unsigned int state,
127 screen *scr)
128 {
129 Action *act;
130
131 // we're done with the children. ungrab them
132 if (_current != _head)
133 ungrabChildren(_current, scr);
134
135 ChildList::const_iterator it, end = _current->children.end();
136 for (it = _current->children.begin(); it != end; ++it) {
137 act = (*it)->action;
138 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
139 if (act->type() == Action::cancelChain) {
140 // user is cancelling the chain explicitly
141 _current = _head;
142 return (const Action *)NULL;
143 }
144 else if ( isLeaf(*it) ) {
145 // node is a leaf, so an action will be executed
146 if (_timer->isTiming()) {
147 _timer->stop();
148 _timeout_screen = NULL;
149 }
150
151 _current = _head;
152 return act;
153 }
154 else {
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())
158 _timer->stop();
159 _timer->start();
160 _timeout_screen = scr;
161
162 _current = *it;
163 grabChildren(_current, scr);
164 return (const Action *)NULL;
165 }
166 }
167 }
168
169 // action not found. back to the head
170 _current = _head;
171 return (const Action *)NULL;
172 }
173
174 void keytree::addAction(Action::ActionType action, unsigned int mask,
175 string key, string arg)
176 {
177 keynode *tmp = new keynode;
178 tmp->action = new Action(action,
179 XKeysymToKeycode(_display,
180 XStringToKeysym(key.c_str())),
181 mask, arg);
182 tmp->parent = _current;
183 _current->children.push_back(tmp);
184 }
185
186 void keytree::advanceOnNewNode()
187 {
188 keynode *tmp = new keynode;
189 tmp->action = NULL;
190 tmp->parent = _current;
191 _current->children.push_back(tmp);
192 _current = tmp;
193 }
194
195 void keytree::retract()
196 {
197 if (_current != _head)
198 _current = _current->parent;
199 }
200
201 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
202 string key, string arg)
203 {
204 if (_current->action)
205 delete _current->action;
206
207 _current->action = new Action(action,
208 XKeysymToKeycode(_display,
209 XStringToKeysym(key.c_str())),
210 mask, arg);
211 }
212
213 void keytree::initialize(void)
214 {
215 int tval = _epist->getConfig()->getNumberValue(Config::chainTimeout);
216 _timer = new BTimer(_epist, this);
217
218 if (tval <= 0)
219 tval = 3000; // set default timeout to 3 seconds
220
221 _timer->setTimeout(tval);
222 }
223
224 void keytree::timeout(void)
225 {
226 assert(_timeout_screen != NULL);
227
228 if (_current != _head) {
229 ungrabChildren(_current, _timeout_screen);
230 _current = _head;
231 }
232 _timeout_screen = NULL;
233 }
This page took 0.043271 seconds and 4 git commands to generate.