]> Dogcows Code - chaz/openbox/blob - util/epist/keytree.cc
b3be1de341ed21026cb45608f7de64f5bb36c319
[chaz/openbox] / util / epist / keytree.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // keytree.cc for Epistrophy - a key handler for NETWM/EWMH window managers.
3 // Copyright (c) 2002 - 2002 Ben Jansens <ben at orodu.net>
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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.
22
23 #include "keytree.hh"
24
25 #include <string>
26
27 using std::string;
28
29
30 keytree::keytree(Display *display) : _display(display)
31 {
32 _head = new keynode;
33 _head->parent = NULL;
34 _head->action = NULL; // head's action is always NULL
35 _current = _head;
36 }
37
38 keytree::~keytree()
39 {
40 clearTree(_head);
41 }
42
43 void keytree::clearTree(keynode *node)
44 {
45 if (!node)
46 return;
47
48 ChildList::iterator it, end = node->children.end();
49 for (it = node->children.begin(); it != end; ++it)
50 clearTree(*it);
51
52 if (node->action)
53 delete node->action;
54 delete node;
55 }
56
57 void keytree::grabDefaults(screen *scr)
58 {
59 grabChildren(_head, scr);
60 }
61
62 void keytree::grabChildren(keynode *node, screen *scr)
63 {
64 ChildList::const_iterator it, end = node->children.end();
65 for (it = node->children.begin(); it != end; ++it)
66 if ( (*it)->action )
67 scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
68 }
69
70 void keytree::ungrabChildren(keynode *node, screen *scr)
71 {
72 ChildList::const_iterator head_it, head_end = _head->children.end();
73 ChildList::const_iterator it, end = node->children.end();
74 bool ungrab = true;
75
76 // when ungrabbing children, make sure that we don't ungrab any topmost keys
77 // (children of the head node) This would render those topmost keys useless.
78 // Topmost keys are _never_ ungrabbed, since they are only grabbed at startup
79
80 for (it = node->children.begin(); it != end; ++it) {
81 if ( (*it)->action ) {
82 for (head_it = _head->children.begin(); head_it != head_end; ++head_it) {
83 if ( (*it)->action->modifierMask() == (*head_it)->action->modifierMask() &&
84 (*it)->action->keycode() == (*head_it)->action->keycode())
85 {
86 ungrab = false;
87 break;
88 }
89 }
90
91 if (ungrab)
92 scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
93
94 ungrab = true;
95 }
96 }
97 }
98
99 const Action * keytree::getAction(const XEvent &e, unsigned int state,
100 screen *scr)
101 {
102 Action *act;
103
104 // we're done with the children. ungrab them
105 if (_current != _head)
106 ungrabChildren(_current, scr);
107
108 ChildList::const_iterator it, end = _current->children.end();
109 for (it = _current->children.begin(); it != end; ++it) {
110 act = (*it)->action;
111 if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
112 if ( isLeaf(*it) ) {
113 // node is a leaf, so an action will be executed
114 _current = _head;
115 return act;
116 }
117 else {
118 // node is not a leaf, so we advance down the tree, and grab the
119 // children of the new current node. no action is executed
120 _current = *it;
121 grabChildren(_current, scr);
122 return (const Action *)NULL;
123 }
124 }
125 }
126
127 // action not found. back to the head
128 _current = _head;
129 return (const Action *)NULL;
130 }
131
132 void keytree::addAction(Action::ActionType action, unsigned int mask,
133 string key, string arg)
134 {
135 keynode *tmp = new keynode;
136 tmp->action = new Action(action,
137 XKeysymToKeycode(_display,
138 XStringToKeysym(key.c_str())),
139 mask, arg);
140 tmp->parent = _current;
141 _current->children.push_back(tmp);
142 }
143
144 void keytree::advanceOnNewNode()
145 {
146 keynode *tmp = new keynode;
147 tmp->action = NULL;
148 tmp->parent = _current;
149 _current->children.push_back(tmp);
150 _current = tmp;
151 }
152
153 void keytree::retract()
154 {
155 if (_current != _head)
156 _current = _current->parent;
157 }
158
159 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
160 string key, string arg)
161 {
162 if (_current->action)
163 delete _current->action;
164
165 _current->action = new Action(action,
166 XKeysymToKeycode(_display,
167 XStringToKeysym(key.c_str())),
168 mask, arg);
169 }
This page took 0.040364 seconds and 3 git commands to generate.