]>
Dogcows Code - chaz/openbox/blob - util/epist/keytree.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // keytree.cc for Epistrophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
31 keytree::keytree(Display
*display
, epist
*ep
)
32 : _display(display
), _timeout_screen(NULL
), _timer(NULL
), _epist(ep
)
36 _head
->action
= NULL
; // head's action is always NULL
38 // for complete initialization, initialize() has to be called as well. We
39 // call initialize() when we are certain that the config object (which the
40 // timer uses) has been fully initialized. (see parser::parse())
49 void keytree::unloadBindings()
51 ChildList::iterator it
, end
= _head
->children
.end();
52 for (it
= _head
->children
.begin(); it
!= end
; ++it
)
55 _head
->children
.clear();
59 void keytree::clearTree(keynode
*node
)
64 ChildList::iterator it
, end
= node
->children
.end();
65 for (it
= node
->children
.begin(); it
!= end
; ++it
)
68 node
->children
.clear();
76 void keytree::grabDefaults(screen
*scr
)
78 grabChildren(_head
, scr
);
81 void keytree::ungrabDefaults(screen
*scr
)
83 ChildList::const_iterator it
, end
= _head
->children
.end();
84 for (it
= _head
->children
.begin(); it
!= end
; ++it
)
86 scr
->ungrabKey( (*it
)->action
->keycode(), (*it
)->action
->modifierMask() );
89 void keytree::grabChildren(keynode
*node
, screen
*scr
)
91 ChildList::const_iterator it
, end
= node
->children
.end();
92 for (it
= node
->children
.begin(); it
!= end
; ++it
)
94 scr
->grabKey( (*it
)->action
->keycode(), (*it
)->action
->modifierMask() );
97 void keytree::ungrabChildren(keynode
*node
, screen
*scr
)
99 ChildList::const_iterator head_it
, head_end
= _head
->children
.end();
100 ChildList::const_iterator it
, end
= node
->children
.end();
103 // when ungrabbing children, make sure that we don't ungrab any topmost keys
104 // (children of the head node) This would render those topmost keys useless.
105 // Topmost keys are _never_ ungrabbed, since they are only grabbed at startup
107 for (it
= node
->children
.begin(); it
!= end
; ++it
) {
108 if ( (*it
)->action
) {
109 for (head_it
= _head
->children
.begin(); head_it
!= head_end
; ++head_it
) {
110 if ( (*it
)->action
->modifierMask() == (*head_it
)->action
->modifierMask() &&
111 (*it
)->action
->keycode() == (*head_it
)->action
->keycode())
119 scr
->ungrabKey( (*it
)->action
->keycode(), (*it
)->action
->modifierMask());
126 const Action
* keytree::getAction(const XEvent
&e
, unsigned int state
,
131 // we're done with the children. ungrab them
132 if (_current
!= _head
)
133 ungrabChildren(_current
, scr
);
135 ChildList::const_iterator it
, end
= _current
->children
.end();
136 for (it
= _current
->children
.begin(); it
!= end
; ++it
) {
138 if (e
.xkey
.keycode
== act
->keycode() && state
== act
->modifierMask()) {
139 if (act
->type() == Action::cancelChain
) {
140 // user is cancelling the chain explicitly
142 return (const Action
*)NULL
;
144 else if ( isLeaf(*it
) ) {
145 // node is a leaf, so an action will be executed
146 if (_timer
->isTiming()) {
148 _timeout_screen
= NULL
;
155 // node is not a leaf, so we advance down the tree, and grab the
156 // children of the new current node. no action is executed
157 if (_timer
->isTiming())
160 _timeout_screen
= scr
;
163 grabChildren(_current
, scr
);
164 return (const Action
*)NULL
;
169 // action not found. back to the head
171 return (const Action
*)NULL
;
174 void keytree::addAction(Action::ActionType action
, unsigned int mask
,
175 string key
, string arg
)
177 keynode
*tmp
= new keynode
;
178 tmp
->action
= new Action(action
,
179 XKeysymToKeycode(_display
,
180 XStringToKeysym(key
.c_str())),
182 tmp
->parent
= _current
;
183 _current
->children
.push_back(tmp
);
186 void keytree::advanceOnNewNode()
188 keynode
*tmp
= new keynode
;
190 tmp
->parent
= _current
;
191 _current
->children
.push_back(tmp
);
195 void keytree::retract()
197 if (_current
!= _head
)
198 _current
= _current
->parent
;
201 void keytree::setCurrentNodeProps(Action::ActionType action
, unsigned int mask
,
202 string key
, string arg
)
204 if (_current
->action
)
205 delete _current
->action
;
207 _current
->action
= new Action(action
,
208 XKeysymToKeycode(_display
,
209 XStringToKeysym(key
.c_str())),
213 void keytree::initialize(void)
215 int tval
= _epist
->getConfig()->getNumberValue(Config::chainTimeout
);
216 _timer
= new BTimer(_epist
, this);
219 tval
= 3000; // set default timeout to 3 seconds
221 _timer
->setTimeout(tval
);
224 void keytree::timeout(void)
226 assert(_timeout_screen
!= NULL
);
228 if (_current
!= _head
) {
229 ungrabChildren(_current
, _timeout_screen
);
232 _timeout_screen
= NULL
;
This page took 0.042458 seconds and 4 git commands to generate.