]> icculus.org git repositories - dana/openbox.git/blob - util/epist/parser.cc
more fixes, better sanity detection and error messages. it shouldn't hog the keyboard...
[dana/openbox.git] / util / epist / parser.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // parser.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 #ifdef    HAVE_CONFIG_H
24 #  include "../../config.h"
25 #endif // HAVE_CONFIG_H
26
27 extern "C" {
28 #include <stdio.h>
29 #include <string.h>
30 }
31
32 #include "parser.hh"
33 #include <string>
34 #include <iostream>
35
36 using std::string;
37 using std::cout;
38
39 parser::parser(keytree *kt, Config *conf)
40   : _kt(kt), _config(conf), _mask(0), _action(Action::noaction),
41     _key(""), _arg(""), _add(true)
42 {
43 }
44
45 parser::~parser()
46 {
47   // nothing to see here. move along.
48 }
49
50 void parser::parse(string rc_file)
51 {
52   extern int yyparse(void *);
53   extern FILE *yyin;
54
55   yyin = fopen(rc_file.c_str(), "r");
56
57   yyparse(this);
58
59   fclose(yyin);
60   _kt->reset();
61   _kt->initialize();
62 }
63
64 void parser::setKey(string key)
65
66   KeySym sym = XStringToKeysym(key.c_str());
67
68   if (sym == 0) {
69     std::cerr << "ERROR: Invalid key (" << key << ")! This may cause odd behavior.\n";
70     _add = false;
71   } else {
72     _key = key;
73   }
74 }
75
76 void parser::setAction(string act)
77 {
78   struct {
79     const char* str;
80     Action::ActionType act;
81   }
82   actions[] = {
83     { "noaction", Action::noaction },
84     { "execute", Action::execute },
85     { "iconify", Action::iconify },
86     { "raise", Action::raise },
87     { "lower", Action::lower },
88     { "close", Action::close },
89     { "toggleShade", Action::toggleShade },
90     { "toggleOmnipresent", Action::toggleOmnipresent },
91     { "movewindowup", Action::moveWindowUp },
92     { "movewindowdown", Action::moveWindowDown },
93     { "movewindowleft", Action::moveWindowLeft },
94     { "movewindowright", Action::moveWindowRight },
95     { "resizewindowwidth", Action::resizeWindowWidth },
96     { "resizewindowheight", Action::resizeWindowHeight },
97     { "togglemaximizefull", Action::toggleMaximizeFull },
98     { "togglemaximizevertical", Action::toggleMaximizeVertical },
99     { "togglemaximizehorizontal", Action::toggleMaximizeHorizontal },
100     { "sendtoworkspace", Action::sendToWorkspace },
101     { "nextwindow", Action::nextWindow },
102     { "prevwindow", Action::prevWindow },
103     { "nextwindowonallworkspaces", Action::nextWindowOnAllWorkspaces },
104     { "prevwindowonallworkspaces", Action::prevWindowOnAllWorkspaces },
105     { "nextwindowonallscreens", Action::nextWindowOnAllScreens },
106     { "prevwindowonallscreens", Action::prevWindowOnAllScreens },
107     { "nextwindowofclass", Action::nextWindowOfClass },
108     { "prevwindowofclass", Action::prevWindowOfClass },
109     { "nextwindowofclassonallworkspaces", Action::nextWindowOfClassOnAllWorkspaces },
110     { "prevwindowofclassonallworkspaces", Action::prevWindowOfClassOnAllWorkspaces },
111     { "changeworkspace", Action::changeWorkspace },
112     { "nextworkspace", Action::nextWorkspace },
113     { "prevworkspace", Action::prevWorkspace },
114     { "nextworkspacerow", Action::upWorkspace },
115     { "prevworkspacerow", Action::downWorkspace },
116     { "prevworkspacecolumn", Action::leftWorkspace },
117     { "nextworkspacecolumn", Action::rightWorkspace },
118     { "nextscreen", Action::nextScreen },
119     { "prevscreen", Action::prevScreen },
120     { "showrootmenu", Action::showRootMenu },
121     { "showworkspacemenu", Action::showWorkspaceMenu },
122     { "toggledecorations", Action::toggleDecorations },
123     { "togglegrabs", Action::toggleGrabs },
124     { "stringchain", Action::stringChain },
125     { "keychain", Action::keyChain },
126     { "numberchain", Action::numberChain },
127     { "cancelchain", Action::cancelChain },
128     { "", Action::noaction }
129   };
130
131   bool found = false;
132
133   for (int i = 0; actions[i].str != ""; ++i) {
134     if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
135       _action = actions[i].act;
136       found = true;
137       break;
138     }
139   }
140
141   if (!found) {
142     cout << "ERROR: Invalid action (" << act << "). Binding ignored.\n";
143     _add = false;
144   }
145 }
146
147 void parser::addModifier(string mod)
148 {
149   struct {
150     const char *str;
151     unsigned int mask;
152   }
153   modifiers[] = {
154     { "mod1", Mod1Mask },
155     { "mod2", Mod2Mask },
156     { "mod3", Mod3Mask },
157     { "mod4", Mod4Mask },
158     { "mod5", Mod5Mask },
159     { "control", ControlMask },
160     { "shift", ShiftMask },
161     { "", 0 }
162   };
163
164   bool found = false;
165
166   for (int i = 0; modifiers[i].str != ""; ++i) {
167     if ( strcasecmp(modifiers[i].str, mod.c_str()) == 0 ) {
168       _mask |= modifiers[i].mask;
169       found = true;
170       break;
171     }
172   }
173
174   if (!found) {
175     cout << "ERROR: Invalid modifier (" << mod << "). Binding ignored.\n";
176     _add = false;
177   }
178 }
179
180 void parser::endAction()
181 {
182   if (_add)
183     _kt->addAction(_action, _mask, _key, _arg);
184   reset();
185
186   _add = true;
187 }
188
189 void parser::startChain()
190 {
191   _kt->advanceOnNewNode();
192   setChainBinding();
193   reset();
194 }
195
196 void parser::endChain()
197 {
198   _kt->retract();
199   reset();
200 }
201
202 void parser::setChainBinding()
203 {
204   if (_add)
205     _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
206   
207   _add = true;
208   reset();
209 }
210
211 void parser::reset()
212 {
213   _mask = 0;
214   _action = Action::noaction;
215   _key = "";
216   _arg = "";
217 }