]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/keytree.cc
bad patch
[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         _current = _head;
87         return act;
88       }
89       else {
90         _current = *it;
91         grabChildren(_current, scr);
92         return (const Action *)NULL;
93       }
94     }
95   }
96
97   // action not found. back to the head
98   _current = _head;
99   return (const Action *)NULL;
100 }
101
102 void keytree::addAction(Action::ActionType action, unsigned int mask,
103                         string key, string arg)
104 {
105   // can't grab non-modifier as topmost key
106   if (_current == _head && (mask == 0 || mask == ShiftMask))
107     return;
108
109   keynode *tmp = new keynode;
110   tmp->action = new Action(action,
111                            XKeysymToKeycode(_display,
112                                             XStringToKeysym(key.c_str())),
113                            mask, arg);
114   tmp->parent = _current;
115   _current->children.push_back(tmp);
116 }
117
118 void keytree::advanceOnNewNode()
119 {
120   keynode *tmp = new keynode;
121   tmp->action = NULL;
122   tmp->parent = _current;
123   _current->children.push_back(tmp);
124   _current = tmp;
125 }
126
127 void keytree::retract()
128 {
129   if (_current != _head)
130     _current = _current->parent;
131 }
132
133 void keytree::setCurrentNodeProps(Action::ActionType action, unsigned int mask,
134                                   string key, string arg)
135 {
136   if (_current->action)
137     delete _current->action;
138   _current->action = new Action(action,
139                                 XKeysymToKeycode(_display,
140                                                  XStringToKeysym(key.c_str())),
141                                 mask, arg);
142 }