]>
Dogcows Code - chaz/openbox/blob - src/bindings.cc
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
);
24 printf("%d%s\n", p
->id
, (str
+ " " + p
->text
).c_str());
25 BindingTree
*s
= p
->next_sibling
;
32 void OBBindings::display()
34 if (_bindings
.first_child
)
35 print_branch(_bindings
.first_child
, "");
40 static bool translate(const std::string str
, Binding
&b
)
42 KeySym sym
= XStringToKeysym(const_cast<char *>(str
.c_str()));
43 if (sym
== NoSymbol
) return false;
44 b
.modifiers
= Mod1Mask
;
45 b
.key
= XKeysymToKeycode(otk::OBDisplay::display
, sym
);
49 static BindingTree
*buildtree(const OBBindings::StringVect
&keylist
, int id
)
51 if (keylist
.empty()) return 0; // nothing in the list.. return 0
53 BindingTree
*ret
= new BindingTree(id
), *p
= 0;
55 OBBindings::StringVect::const_iterator it
, end
= keylist
.end();
56 for (it
= keylist
.begin(); it
!= end
; ++it
) {
58 p
= p
->first_child
= new BindingTree(id
);
60 p
= ret
; // the first node
62 if (!translate(*it
, p
->binding
))
67 // build failed.. clean up and return 0
69 while (p
->first_child
) {
70 BindingTree
*c
= p
->first_child
;
77 // set the proper chain status on the last node
81 printf("<BUILDING>\n");
83 printf("</BUILDING>\n");
85 // successfully built a tree
89 static void destroytree(BindingTree
*tree
)
92 BindingTree
*c
= tree
->first_child
;
98 OBBindings::OBBindings()
103 OBBindings::~OBBindings()
109 static void assimilate(BindingTree
*parent
, BindingTree
*node
)
111 BindingTree
*p
, *lastsib
, *nextparent
, *nextnode
= node
->first_child
;
113 if (!parent
->first_child
) {
114 // there are no nodes at this level yet
115 parent
->first_child
= node
;
118 p
= lastsib
= parent
->first_child
;
120 while (p
->next_sibling
) {
122 lastsib
= p
; // finds the last sibling
123 if (p
->binding
== node
->binding
) {
124 // found an identical binding..
125 assert(node
->chain
&& p
->chain
);
126 delete node
; // kill the one we aren't using
131 // couldn't find an existing binding, use this new one, and insert it
133 p
= lastsib
->next_sibling
= node
;
139 assimilate(nextparent
, nextnode
);
143 static int find_bind(BindingTree
*tree
, BindingTree
*search
) {
148 if (a
->binding
!= b
->binding
) {
151 if (a
->chain
== b
->chain
) {
153 return a
->id
; // found it! (return the actual id, not the search's)
155 return -2; // the chain status' don't match (conflict!)
160 return -1; // it just isn't in here
164 static int find(BindingTree *parent, BindingTree *node) {
165 BindingTree *p, *lastsib, *nextparent, *nextnode = node->first_child;
167 if (!parent->first_child)
170 p = parent->first_child;
172 if (node->binding == p->binding) {
173 if (node->chain == p->chain) {
175 return p->id; // found it! (return the actual id, not the search's)
177 break; // go on to the next child in the chain
180 return -2; // the chain status' don't match (conflict!)
185 if (!p) return -1; // doesn't exist
188 assert(node->first_child);
189 return find(p, node->first_child);
191 return -1; // it just isnt in here
195 bool OBBindings::add(const StringVect
&keylist
, int id
)
199 if (!(tree
= buildtree(keylist
, id
)))
200 return false; // invalid binding requested
202 if (find_bind(_bindings
.first_child
, tree
) < -1) {
203 // conflicts with another binding
208 // assimilate this built tree into the main tree
209 assimilate(&_bindings
, tree
); // assimilation destroys/uses the tree
214 int OBBindings::find(const StringVect
&keylist
)
219 if (!(tree
= buildtree(keylist
, 0)))
220 return false; // invalid binding requested
222 ret
= find_bind(_bindings
.first_child
, tree
) >= 0;
230 int OBBindings::remove(const StringVect
&keylist
)
233 assert(false); // XXX: function not implemented yet
237 static void remove_branch(BindingTree
*first
)
239 BindingTree
*p
= first
;
243 remove_branch(p
->first_child
);
244 BindingTree
*s
= p
->next_sibling
;
251 void OBBindings::remove_all()
253 if (_bindings
.first_child
)
254 remove_branch(_bindings
.first_child
);
This page took 0.043386 seconds and 4 git commands to generate.