]> icculus.org git repositories - dana/openbox.git/blob - src/bindings.cc
add click_raise global var
[dana/openbox.git] / 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), _resetkey(0,0)
138 {
139   setResetKey("C-g"); // set the default reset key
140 }
141
142
143 OBBindings::~OBBindings()
144 {
145   grabKeys(false);
146   remove_all();
147 }
148
149
150 void OBBindings::assimilate(BindingTree *node)
151 {
152   BindingTree *a, *b, *tmp, *last;
153
154   if (!_tree.first_child) {
155     // there are no nodes at this level yet
156     _tree.first_child = node;
157   } else {
158     a = _tree.first_child;
159     last = a;
160     b = node;
161     while (a) {
162       last = a;
163       if (a->binding != b->binding) {
164         a = a->next_sibling;
165       } else {
166         tmp = b;
167         b = b->first_child;
168         delete tmp;
169         a = a->first_child;
170       }
171     }
172     if (last->binding != b->binding)
173       last->next_sibling = b;
174     else {
175       last->first_child = b->first_child;
176       delete b;
177     }
178   }
179 }
180
181
182 int OBBindings::find(BindingTree *search) const {
183   BindingTree *a, *b;
184   a = _tree.first_child;
185   b = search;
186   while (a && b) {
187     if (a->binding != b->binding) {
188       a = a->next_sibling;
189     } else {
190       if (a->chain == b->chain) {
191         if (!a->chain) {
192           return a->id; // found it! (return the actual id, not the search's)
193         }
194       } else {
195         return -2; // the chain status' don't match (conflict!)
196       }
197       b = b->first_child;
198       a = a->first_child;
199     }
200   }
201   return -1; // it just isn't in here
202 }
203
204
205 bool OBBindings::add(const StringVect &keylist, int id)
206 {
207   BindingTree *tree;
208
209   if (!(tree = buildtree(keylist, id)))
210     return false; // invalid binding requested
211
212   if (find(tree) != -1) {
213     // conflicts with another binding
214     destroytree(tree);
215     return false;
216   }
217
218   grabKeys(false);
219   
220   // assimilate this built tree into the main tree
221   assimilate(tree); // assimilation destroys/uses the tree
222
223   grabKeys(true); 
224  
225   return true;
226 }
227
228
229 int OBBindings::find(const StringVect &keylist)
230 {
231   BindingTree *tree;
232   bool ret;
233
234   if (!(tree = buildtree(keylist, 0)))
235     return false; // invalid binding requested
236
237   ret = find(tree) >= 0;
238
239   destroytree(tree);
240
241   return ret;
242 }
243
244
245 int OBBindings::remove(const StringVect &keylist)
246 {
247   (void)keylist;
248   assert(false); // XXX: function not implemented yet
249
250   grabKeys(false);
251   _curpos = &_tree;
252
253   // do shit here...
254   
255   grabKeys(true);
256
257 }
258
259
260 void OBBindings::setResetKey(const std::string &key)
261 {
262   Binding b(0, 0);
263   if (translate(key, b)) {
264     grabKeys(false);
265     _resetkey.key = b.key;
266     _resetkey.modifiers = b.modifiers;
267     grabKeys(true);
268   }
269 }
270
271
272 static void remove_branch(BindingTree *first)
273 {
274   BindingTree *p = first;
275
276   while (p) {
277     if (p->first_child)
278       remove_branch(p->first_child);
279     BindingTree *s = p->next_sibling;
280     delete p;
281     p = s;
282   }
283 }
284
285
286 void OBBindings::remove_all()
287 {
288   if (_tree.first_child) {
289     remove_branch(_tree.first_child);
290     _tree.first_child = 0;
291   }
292 }
293
294
295 void OBBindings::grabKeys(bool grab)
296 {
297   for (int i = 0; i < Openbox::instance->screenCount(); ++i) {
298     Window root = otk::OBDisplay::screenInfo(i)->rootWindow();
299
300     BindingTree *p = _curpos->first_child;
301     while (p) {
302       if (grab)
303         otk::OBDisplay::grabKey(p->binding.key, p->binding.modifiers,
304                                 root, false, GrabModeAsync, GrabModeAsync,
305                                 false);
306       else
307         otk::OBDisplay::ungrabKey(p->binding.key, p->binding.modifiers,
308                                   root);
309       p = p->next_sibling;
310     }
311
312     if (grab)
313       otk::OBDisplay::grabKey(_resetkey.key, _resetkey.modifiers,
314                               root, true, GrabModeAsync, GrabModeAsync,
315                               false);
316     else
317       otk::OBDisplay::ungrabKey(_resetkey.key, _resetkey.modifiers,
318                                 root);
319   }
320 }
321
322
323 void OBBindings::fire(Window window, unsigned int modifiers, unsigned int key,
324                       Time time)
325 {
326   if (key == _resetkey.key && modifiers == _resetkey.modifiers) {
327     grabKeys(false);
328     _curpos = &_tree;
329     grabKeys(true);
330   } else {
331     BindingTree *p = _curpos->first_child;
332     while (p) {
333       if (p->binding.key == key && p->binding.modifiers == modifiers) {
334         if (p->chain) {
335           grabKeys(false);
336           _curpos = p;
337           grabKeys(true);
338         } else {
339           python_callback_binding(p->id, window, modifiers, key, time);
340           grabKeys(false);
341           _curpos = &_tree;
342           grabKeys(true);
343         }
344         break;
345       }
346       p = p->next_sibling;
347     }
348   }
349 }
350
351 }