]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/keytree.cc
fix std:: namespace problems
[mikachu/openbox.git] / util / epist / keytree.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // keytree.cc for Epistophy - 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 it, end = node->children.end();
73   for (it = node->children.begin(); it != end; ++it)
74     if ( (*it)->action )
75       scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
76 }
77
78 const Action * keytree::getAction(const XEvent &e, unsigned int state,
79                                   screen *scr)
80 {
81   Action *act;
82
83   if (_current != _head)
84     ungrabChildren(_current, scr);
85   
86   ChildList::const_iterator it, end = _current->children.end();
87   for (it = _current->children.begin(); it != end; ++it) {
88     act = (*it)->action;
89     if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
90       if ( isLeaf(*it) ) {
91         _current = _head;
92         return act;
93       }
94       else {
95         _current = *it;
96         grabChildren(_current, scr);
97         return (const Action *)NULL;
98       }
99     }
100   }
101
102   // action not found. back to the head
103   _current = _head;
104   return (const Action *)NULL;
105 }
106
107 void keytree::addAction(Action::ActionType action, unsigned int mask,
108                         string key, string arg)
109 {
110   // can't grab non-modifier as topmost key
111   if (_current == _head && (mask == 0 || mask == ShiftMask))
112     return;
113
114   keynode *tmp = new keynode;
115   tmp->action = new Action(action,
116                            XKeysymToKeycode(_display,
117                                             XStringToKeysym(key.c_str())),
118                            mask, arg);
119   tmp->parent = _current;
120   _current->children.push_back(tmp);
121 }
122
123 void keytree::advanceOnNewNode()
124 {
125   keynode *tmp = new keynode;
126   tmp->action = NULL;
127   tmp->parent = _current;
128   _current->children.push_back(tmp);
129   _current = tmp;
130 }
131
132 void keytree::retract()
133 {
134   if (_current != _head)
135     _current = _current->parent;
136 }
137
138 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
139                                   string key, string arg)
140 {
141   if (_current->action)
142     delete _current->action;
143   _current->action = new Action(action,
144                                 XKeysymToKeycode(_display,
145                                                  XStringToKeysym(key.c_str())),
146                                 mask, arg);
147 }