]>
Dogcows Code - chaz/openbox/blob - src/bindings.cc
3e65c049eb79dc23a59d97c8ddba8f1360795f49
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
8 #include "otk/display.hh"
17 static void print_branch(BindingTree
*first
, std::string str
)
19 BindingTree
*p
= first
;
23 print_branch(p
->first_child
, str
+ " " + p
->text
);
25 printf("%d%s\n", p
->id
, (str
+ " " + p
->text
).c_str());
26 BindingTree
*s
= p
->next_sibling
;
33 void OBBindings::display()
35 if (_bindings
.first_child
)
36 print_branch(_bindings
.first_child
, "");
41 static bool translate(const std::string str
, Binding
&b
)
43 std::string::size_type keybegin
= str
.find_last_of('-');
44 std::string
key(str
, keybegin
!= std::string::npos
? keybegin
+ 1 : 0);
46 // XXX: get some modifiers up in the hizzie
48 KeySym sym
= XStringToKeysym(const_cast<char *>(key
.c_str()));
49 if (sym
== NoSymbol
) return false;
50 b
.modifiers
= Mod1Mask
; // XXX: no way
51 b
.key
= XKeysymToKeycode(otk::OBDisplay::display
, sym
);
55 static BindingTree
*buildtree(const OBBindings::StringVect
&keylist
, int id
)
57 if (keylist
.empty()) return 0; // nothing in the list.. return 0
59 BindingTree
*ret
= new BindingTree(id
), *p
= 0;
61 OBBindings::StringVect::const_iterator it
, end
= keylist
.end();
62 for (it
= keylist
.begin(); it
!= end
; ++it
) {
64 p
= p
->first_child
= new BindingTree(id
);
66 p
= ret
; // the first node
68 if (!translate(*it
, p
->binding
))
73 // build failed.. clean up and return 0
75 while (p
->first_child
) {
76 BindingTree
*c
= p
->first_child
;
83 // set the proper chain status on the last node
87 printf("BUILDING:\n");
88 print_branch(ret
, "");
90 // successfully built a tree
94 static void destroytree(BindingTree
*tree
)
97 BindingTree
*c
= tree
->first_child
;
103 OBBindings::OBBindings()
108 OBBindings::~OBBindings()
114 static void assimilate(BindingTree
*parent
, BindingTree
*node
)
116 BindingTree
*p
, *lastsib
, *nextparent
, *nextnode
= node
->first_child
;
118 if (!parent
->first_child
) {
119 // there are no nodes at this level yet
120 parent
->first_child
= node
;
123 p
= lastsib
= parent
->first_child
;
125 while (p
->next_sibling
) {
127 lastsib
= p
; // finds the last sibling
128 if (p
->binding
== node
->binding
) {
129 // found an identical binding..
130 assert(node
->chain
&& p
->chain
);
131 delete node
; // kill the one we aren't using
136 // couldn't find an existing binding, use this new one, and insert it
138 p
= lastsib
->next_sibling
= node
;
144 assimilate(nextparent
, nextnode
);
148 static int find_bind(BindingTree
*tree
, BindingTree
*search
) {
153 if (a
->binding
!= b
->binding
) {
156 if (a
->chain
== b
->chain
) {
158 return a
->id
; // found it! (return the actual id, not the search's)
160 return -2; // the chain status' don't match (conflict!)
165 return -1; // it just isn't in here
169 static int find(BindingTree *parent, BindingTree *node) {
170 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
172 if (!parent->first_child)
175 p = parent->first_child;
177 if (node->binding == p->binding) {
178 if (node->chain == p->chain) {
180 return p->id; // found it! (return the actual id, not the search's)
182 break; // go on to the next child in the chain
185 return -2; // the chain status' don't match (conflict!)
190 if (!p) return -1; // doesn't exist
193 assert(node->first_child);
194 return find(p, node->first_child);
196 return -1; // it just isnt in here
200 bool OBBindings::add(const StringVect
&keylist
, int id
)
204 if (!(tree
= buildtree(keylist
, id
)))
205 return false; // invalid binding requested
207 if (find_bind(_bindings
.first_child
, tree
) < -1) {
208 // conflicts with another binding
213 // assimilate this built tree into the main tree
214 assimilate(&_bindings
, tree
); // assimilation destroys/uses the tree
219 int OBBindings::find(const StringVect
&keylist
)
224 if (!(tree
= buildtree(keylist
, 0)))
225 return false; // invalid binding requested
227 ret
= find_bind(_bindings
.first_child
, tree
) >= 0;
235 int OBBindings::remove(const StringVect
&keylist
)
238 assert(false); // XXX: function not implemented yet
242 static void remove_branch(BindingTree
*first
)
244 BindingTree
*p
= first
;
248 remove_branch(p
->first_child
);
249 BindingTree
*s
= p
->next_sibling
;
256 void OBBindings::remove_all()
258 if (_bindings
.first_child
)
259 remove_branch(_bindings
.first_child
);
This page took 0.043166 seconds and 4 git commands to generate.