]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/keytree.cc
Big fat merge for epist.
[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 keytree::keytree(Display *display) : _display(display)
26 {
27   _head = new keynode;
28   _head->parent = NULL;
29   _head->action = NULL; // head's action is always NULL
30   _current = _head;
31 }
32
33 keytree::~keytree()
34 {
35   clearTree(_head);
36 }
37
38 void keytree::clearTree(keynode *node)
39 {
40   if (!node)
41     return;
42
43   ChildList::iterator it, end = node->children.end();
44   for (it = node->children.begin(); it != end; ++it)
45     clearTree(*it);
46
47   if (node->action)
48     delete node->action;
49   delete node;
50 }
51
52 void keytree::grabDefaults(screen *scr)
53 {
54   grabChildren(_head, scr);
55 }
56
57 void keytree::grabChildren(keynode *node, screen *scr)
58 {
59   ChildList::const_iterator it, end = node->children.end();
60   for (it = node->children.begin(); it != end; ++it)
61     if ( (*it)->action )
62       scr->grabKey( (*it)->action->keycode(), (*it)->action->modifierMask() );
63 }
64
65 void keytree::ungrabChildren(keynode *node, screen *scr)
66 {
67   ChildList::const_iterator it, end = node->children.end();
68   for (it = node->children.begin(); it != end; ++it)
69     if ( (*it)->action )
70       scr->ungrabKey( (*it)->action->keycode(), (*it)->action->modifierMask());
71 }
72
73 const Action * keytree::getAction(const XEvent &e, unsigned int state,
74                                   screen *scr)
75 {
76   Action *act;
77
78   if (_current != _head)
79     ungrabChildren(_current, scr);
80   
81   ChildList::const_iterator it, end = _current->children.end();
82   for (it = _current->children.begin(); it != end; ++it) {
83     act = (*it)->action;
84     if (e.xkey.keycode == act->keycode() && state == act->modifierMask()) {
85       if ( isLeaf(*it) ) {
86         if (_current != _head)
87           ungrabChildren(_current, scr);
88         _current = _head;
89         return act;
90       }
91       else {
92         _current = *it;
93         grabChildren(_current, scr);
94         return (const Action *)NULL;
95       }
96     }
97   }
98
99   // action not found. back to the head
100   _current = _head;
101   return (const Action *)NULL;
102 }
103
104 void keytree::addAction(Action::ActionType action, unsigned int mask,
105                         string key, string arg)
106 {
107   // can't grab non-modifier as topmost key
108   if (_current == _head && (mask == 0 || mask == ShiftMask))
109     return;
110
111   keynode *tmp = new keynode;
112   tmp->action = new Action(action,
113                            XKeysymToKeycode(_display,
114                                             XStringToKeysym(key.c_str())),
115                            mask, arg);
116   tmp->parent = _current;
117   _current->children.push_back(tmp);
118 }
119
120 void keytree::advanceOnNewNode()
121 {
122   keynode *tmp = new keynode;
123   tmp->action = NULL;
124   tmp->parent = _current;
125   _current->children.push_back(tmp);
126   _current = tmp;
127 }
128
129 void keytree::retract()
130 {
131   if (_current != _head)
132     _current = _current->parent;
133 }
134
135 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
136                                   string key, string arg)
137 {
138   if (_current->action)
139     delete _current->action;
140   _current->action = new Action(action,
141                                 XKeysymToKeycode(_display,
142                                                  XStringToKeysym(key.c_str())),
143                                 mask, arg);
144 }