]>
Dogcows Code - chaz/openbox/blob - src/bindings.cc
f34b9af3d682e0d521bbe8c1cc2e7b191080119c
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "otk/display.hh"
14 #define _(str) gettext(str)
20 static void print_branch(BindingTree
*first
, std::string str
)
22 BindingTree
*p
= first
;
26 print_branch(p
->first_child
, str
+ " " + p
->text
);
28 printf("%d%s\n", p
->id
, (str
+ " " + p
->text
).c_str());
34 void OBBindings::display()
36 if (_tree
.first_child
)
37 print_branch(_tree
.first_child
, "");
42 bool OBBindings::translate(const std::string
&str
, Binding
&b
)
44 // parse out the base key name
45 std::string::size_type keybegin
= str
.find_last_of('-');
46 keybegin
= (keybegin
== std::string::npos
) ? 0 : keybegin
+ 1;
47 std::string
key(str
, keybegin
);
49 // parse out the requested modifier keys
50 unsigned int mods
= 0;
51 std::string::size_type begin
= 0, end
;
52 while (begin
!= keybegin
) {
53 end
= str
.find_first_of('-', begin
);
55 std::string
mod(str
, begin
, end
-begin
);
57 if (mod
== "C") { // control
59 } else if (mod
== "S") { // shift
61 } else if (mod
== "A" || // alt/mod1
66 } else if (mod
== "M2" || // mod2
69 } else if (mod
== "M3" || // mod3
72 } else if (mod
== "W" || // windows/mod4
76 } else if (mod
== "M5" || // mod5
80 printf(_("Invalid modifier element in key binding: %s\n"), mod
.c_str());
86 KeySym sym
= XStringToKeysym(const_cast<char *>(key
.c_str()));
87 if (sym
== NoSymbol
) return false;
89 b
.key
= XKeysymToKeycode(otk::OBDisplay::display
, sym
);
93 BindingTree
*OBBindings::buildtree(const StringVect
&keylist
, int id
)
95 if (keylist
.empty()) return 0; // nothing in the list.. return 0
97 BindingTree
*ret
= new BindingTree(id
), *p
= 0;
99 StringVect::const_iterator it
, end
= keylist
.end();
100 for (it
= keylist
.begin(); it
!= end
; ++it
) {
102 p
= p
->first_child
= new BindingTree(id
);
104 p
= ret
; // the first node
106 if (!translate(*it
, p
->binding
))
111 // build failed.. clean up and return 0
113 while (p
->first_child
) {
114 BindingTree
*c
= p
->first_child
;
121 // set the proper chain status on the last node
125 // successfully built a tree
129 static void destroytree(BindingTree
*tree
)
132 BindingTree
*c
= tree
->first_child
;
138 OBBindings::OBBindings()
143 OBBindings::~OBBindings()
149 void OBBindings::assimilate(BindingTree
*node
)
151 BindingTree
*a
, *b
, *tmp
, *last
;
153 if (!_tree
.first_child
) {
154 // there are no nodes at this level yet
155 _tree
.first_child
= node
;
158 a
= _tree
.first_child
;
163 if (a
->binding
!= b
->binding
) {
172 if (last
->binding
!= b
->binding
)
173 last
->next_sibling
= b
;
175 last
->first_child
= b
->first_child
;
181 int OBBindings::find(BindingTree
*search
) {
183 a
= _tree
.first_child
;
186 if (a
->binding
!= b
->binding
) {
189 if (a
->chain
== b
->chain
) {
191 return a
->id
; // found it! (return the actual id, not the search's)
193 return -2; // the chain status' don't match (conflict!)
198 return -1; // it just isn't in here
202 static int find(BindingTree *parent, BindingTree *node) {
203 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
205 if (!parent->first_child)
208 p = parent->first_child;
210 if (node->binding == p->binding) {
211 if (node->chain == p->chain) {
213 return p->id; // found it! (return the actual id, not the search's)
215 break; // go on to the next child in the chain
218 return -2; // the chain status' don't match (conflict!)
223 if (!p) return -1; // doesn't exist
226 assert(node->first_child);
227 return find(p, node->first_child);
229 return -1; // it just isnt in here
233 bool OBBindings::add(const StringVect
&keylist
, int id
)
237 if (!(tree
= buildtree(keylist
, id
)))
238 return false; // invalid binding requested
240 if (find(tree
) < -1) {
241 // conflicts with another binding
246 // assimilate this built tree into the main tree
247 assimilate(tree
); // assimilation destroys/uses the tree
252 int OBBindings::find(const StringVect
&keylist
)
257 if (!(tree
= buildtree(keylist
, 0)))
258 return false; // invalid binding requested
260 ret
= find(tree
) >= 0;
268 int OBBindings::remove(const StringVect
&keylist
)
271 assert(false); // XXX: function not implemented yet
275 static void remove_branch(BindingTree
*first
)
277 BindingTree
*p
= first
;
281 remove_branch(p
->first_child
);
282 BindingTree
*s
= p
->next_sibling
;
289 void OBBindings::remove_all()
291 if (_tree
.first_child
)
292 remove_branch(_tree
.first_child
);
This page took 0.045573 seconds and 4 git commands to generate.