]> Dogcows Code - chaz/openbox/blob - src/bindings.cc
42a92591724249b2779f9ec2f931a2b4b384bf1b
[chaz/openbox] / src / bindings.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include "bindings.hh"
8 #include "screen.hh"
9 #include "openbox.hh"
10 #include "client.hh"
11 #include "frame.hh"
12 #include "python.hh"
13 #include "otk/display.hh"
14
15 extern "C" {
16 #include <X11/Xlib.h>
17
18 #include "gettext.h"
19 #define _(str) gettext(str)
20 }
21
22 namespace ob {
23
24 static bool buttonvalue(const std::string &button, unsigned int *val)
25 {
26 if (button == "1" || button == "Button1") {
27 *val |= Button1;
28 } else if (button == "2" || button == "Button2") {
29 *val |= Button2;
30 } else if (button == "3" || button == "Button3") {
31 *val |= Button3;
32 } else if (button == "4" || button == "Button4") {
33 *val |= Button4;
34 } else if (button == "5" || button == "Button5") {
35 *val |= Button5;
36 } else
37 return false;
38 return true;
39 }
40
41 static bool modvalue(const std::string &mod, unsigned int *val)
42 {
43 if (mod == "C") { // control
44 *val |= ControlMask;
45 } else if (mod == "S") { // shift
46 *val |= ShiftMask;
47 } else if (mod == "A" || // alt/mod1
48 mod == "M" ||
49 mod == "Mod1" ||
50 mod == "M1") {
51 *val |= Mod1Mask;
52 } else if (mod == "Mod2" || // mod2
53 mod == "M2") {
54 *val |= Mod2Mask;
55 } else if (mod == "Mod3" || // mod3
56 mod == "M3") {
57 *val |= Mod3Mask;
58 } else if (mod == "W" || // windows/mod4
59 mod == "Mod4" ||
60 mod == "M4") {
61 *val |= Mod4Mask;
62 } else if (mod == "Mod5" || // mod5
63 mod == "M5") {
64 *val |= Mod5Mask;
65 } else { // invalid
66 return false;
67 }
68 return true;
69 }
70
71 bool OBBindings::translate(const std::string &str, Binding &b) const
72 {
73 // parse out the base key name
74 std::string::size_type keybegin = str.find_last_of('-');
75 keybegin = (keybegin == std::string::npos) ? 0 : keybegin + 1;
76 std::string key(str, keybegin);
77
78 // parse out the requested modifier keys
79 unsigned int modval = 0;
80 std::string::size_type begin = 0, end;
81 while (begin != keybegin) {
82 end = str.find_first_of('-', begin);
83
84 std::string mod(str, begin, end-begin);
85 if (!modvalue(mod, &modval)) {
86 printf(_("Invalid modifier element in key binding: %s\n"), mod.c_str());
87 return false;
88 }
89
90 begin = end + 1;
91 }
92
93 // set the binding
94 b.modifiers = modval;
95 KeySym sym = XStringToKeysym(const_cast<char *>(key.c_str()));
96 if (sym == NoSymbol) {
97 printf(_("Invalid Key name in key binding: %s\n"), key.c_str());
98 return false;
99 }
100 if (!(b.key = XKeysymToKeycode(otk::OBDisplay::display, sym)))
101 printf(_("No valid keycode for Key in key binding: %s\n"), key.c_str());
102 return b.key != 0;
103 }
104
105 static void destroytree(BindingTree *tree)
106 {
107 while (tree) {
108 BindingTree *c = tree->first_child;
109 delete tree;
110 tree = c;
111 }
112 }
113
114 BindingTree *OBBindings::buildtree(const StringVect &keylist, int id) const
115 {
116 if (keylist.empty()) return 0; // nothing in the list.. return 0
117
118 BindingTree *ret = 0, *p;
119
120 StringVect::const_reverse_iterator it, end = keylist.rend();
121 for (it = keylist.rbegin(); it != end; ++it) {
122 p = ret;
123 ret = new BindingTree(id);
124 if (!p) ret->chain = false; // only the first built node
125 ret->first_child = p;
126 if (!translate(*it, ret->binding)) {
127 destroytree(ret);
128 ret = 0;
129 break;
130 }
131 }
132 return ret;
133 }
134
135
136 OBBindings::OBBindings()
137 : _curpos(&_tree),
138 _resetkey(0,0),
139 _timer(Openbox::instance->timerManager(),
140 (otk::OBTimeoutHandler)reset, this)
141 {
142 _timer.setTimeout(5000); // chains reset after 5 seconds
143
144 setResetKey("C-g"); // set the default reset key
145 }
146
147
148 OBBindings::~OBBindings()
149 {
150 grabKeys(false);
151 remove_all();
152 }
153
154
155 void OBBindings::assimilate(BindingTree *node)
156 {
157 BindingTree *a, *b, *tmp, *last;
158
159 if (!_tree.first_child) {
160 // there are no nodes at this level yet
161 _tree.first_child = node;
162 } else {
163 a = _tree.first_child;
164 last = a;
165 b = node;
166 while (a) {
167 last = a;
168 if (a->binding != b->binding) {
169 a = a->next_sibling;
170 } else {
171 tmp = b;
172 b = b->first_child;
173 delete tmp;
174 a = a->first_child;
175 }
176 }
177 if (last->binding != b->binding)
178 last->next_sibling = b;
179 else {
180 last->first_child = b->first_child;
181 delete b;
182 }
183 }
184 }
185
186
187 int OBBindings::find(BindingTree *search) const {
188 BindingTree *a, *b;
189 a = _tree.first_child;
190 b = search;
191 while (a && b) {
192 if (a->binding != b->binding) {
193 a = a->next_sibling;
194 } else {
195 if (a->chain == b->chain) {
196 if (!a->chain) {
197 return a->id; // found it! (return the actual id, not the search's)
198 }
199 } else {
200 return -2; // the chain status' don't match (conflict!)
201 }
202 b = b->first_child;
203 a = a->first_child;
204 }
205 }
206 return -1; // it just isn't in here
207 }
208
209
210 bool OBBindings::add(const StringVect &keylist, int id)
211 {
212 BindingTree *tree;
213
214 if (!(tree = buildtree(keylist, id)))
215 return false; // invalid binding requested
216
217 if (find(tree) != -1) {
218 // conflicts with another binding
219 destroytree(tree);
220 return false;
221 }
222
223 grabKeys(false);
224
225 // assimilate this built tree into the main tree
226 assimilate(tree); // assimilation destroys/uses the tree
227
228 grabKeys(true);
229
230 return true;
231 }
232
233
234 int OBBindings::find(const StringVect &keylist)
235 {
236 BindingTree *tree;
237 bool ret;
238
239 if (!(tree = buildtree(keylist, 0)))
240 return false; // invalid binding requested
241
242 ret = find(tree) >= 0;
243
244 destroytree(tree);
245
246 return ret;
247 }
248
249
250 int OBBindings::remove(const StringVect &keylist)
251 {
252 (void)keylist;
253 assert(false); // XXX: function not implemented yet
254
255 grabKeys(false);
256 _curpos = &_tree;
257
258 // do shit here...
259
260 grabKeys(true);
261
262 }
263
264
265 void OBBindings::setResetKey(const std::string &key)
266 {
267 Binding b(0, 0);
268 if (translate(key, b)) {
269 grabKeys(false);
270 _resetkey.key = b.key;
271 _resetkey.modifiers = b.modifiers;
272 grabKeys(true);
273 }
274 }
275
276
277 static void remove_branch(BindingTree *first)
278 {
279 BindingTree *p = first;
280
281 while (p) {
282 if (p->first_child)
283 remove_branch(p->first_child);
284 BindingTree *s = p->next_sibling;
285 delete p;
286 p = s;
287 }
288 }
289
290
291 void OBBindings::remove_all()
292 {
293 if (_tree.first_child) {
294 remove_branch(_tree.first_child);
295 _tree.first_child = 0;
296 }
297 }
298
299
300 void OBBindings::grabKeys(bool grab)
301 {
302 for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
303 Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
304
305 BindingTree *p = _curpos->first_child;
306 while (p) {
307 if (grab) {
308 otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
309 root, false, GrabModeAsync, GrabModeAsync,
310 false);
311 }
312 else
313 otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
314 root);
315 p = p->next_sibling;
316 }
317
318 if (grab)
319 otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
320 root, true, GrabModeAsync, GrabModeAsync,
321 false);
322 else
323 otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
324 root);
325 }
326 }
327
328
329 void OBBindings::fire(Window window, unsigned int modifiers, unsigned int key,
330 Time time)
331 {
332 if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
333 reset(this);
334 } else {
335 BindingTree *p = _curpos->first_child;
336 while (p) {
337 if (p->binding.key == key && p->binding.modifiers == modifiers) {
338 if (p->chain) {
339 _timer.start(); // start/restart the timer
340 grabKeys(false);
341 _curpos = p;
342 grabKeys(true);
343 } else {
344 python_callback_binding(p->id, window, modifiers, key, time);
345 reset(this);
346 }
347 break;
348 }
349 p = p->next_sibling;
350 }
351 }
352 }
353
354 void OBBindings::reset(OBBindings *self)
355 {
356 self->_timer.stop();
357 self->grabKeys(false);
358 self->_curpos = &self->_tree;
359 self->grabKeys(true);
360 }
361
362 }
This page took 0.049308 seconds and 4 git commands to generate.