]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/parser.cc
spit an error when no config file is found, to avoid confusing lex and the user.
[mikachu/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   if (yyin) {
58     yyparse(this);
59     fclose(yyin);
60   } else {
61     std::cerr << "ERROR: Configuration file could not be opened/found.\n";
62   }
63
64   _kt->reset();
65   _kt->initialize();
66 }
67
68 void parser::setKey(string key)
69
70   KeySym sym = XStringToKeysym(key.c_str());
71
72   if (sym == 0) {
73     std::cerr << "ERROR: Invalid key (" << key << ")! This may cause odd behavior.\n";
74     _add = false;
75   } else {
76     _key = key;
77   }
78 }
79
80 void parser::setAction(string act)
81 {
82   struct {
83     const char* str;
84     Action::ActionType act;
85   }
86   actions[] = {
87     { "noaction", Action::noaction },
88     { "execute", Action::execute },
89     { "iconify", Action::iconify },
90     { "raise", Action::raise },
91     { "lower", Action::lower },
92     { "close", Action::close },
93     { "toggleShade", Action::toggleShade },
94     { "toggleOmnipresent", Action::toggleOmnipresent },
95     { "movewindowup", Action::moveWindowUp },
96     { "movewindowdown", Action::moveWindowDown },
97     { "movewindowleft", Action::moveWindowLeft },
98     { "movewindowright", Action::moveWindowRight },
99     { "resizewindowwidth", Action::resizeWindowWidth },
100     { "resizewindowheight", Action::resizeWindowHeight },
101     { "togglemaximizefull", Action::toggleMaximizeFull },
102     { "togglemaximizevertical", Action::toggleMaximizeVertical },
103     { "togglemaximizehorizontal", Action::toggleMaximizeHorizontal },
104     { "sendtoworkspace", Action::sendToWorkspace },
105     { "nextwindow", Action::nextWindow },
106     { "prevwindow", Action::prevWindow },
107     { "nextwindowonallworkspaces", Action::nextWindowOnAllWorkspaces },
108     { "prevwindowonallworkspaces", Action::prevWindowOnAllWorkspaces },
109     { "nextwindowonallscreens", Action::nextWindowOnAllScreens },
110     { "prevwindowonallscreens", Action::prevWindowOnAllScreens },
111     { "nextwindowofclass", Action::nextWindowOfClass },
112     { "prevwindowofclass", Action::prevWindowOfClass },
113     { "nextwindowofclassonallworkspaces", Action::nextWindowOfClassOnAllWorkspaces },
114     { "prevwindowofclassonallworkspaces", Action::prevWindowOfClassOnAllWorkspaces },
115     { "changeworkspace", Action::changeWorkspace },
116     { "nextworkspace", Action::nextWorkspace },
117     { "prevworkspace", Action::prevWorkspace },
118     { "nextworkspacerow", Action::upWorkspace },
119     { "prevworkspacerow", Action::downWorkspace },
120     { "prevworkspacecolumn", Action::leftWorkspace },
121     { "nextworkspacecolumn", Action::rightWorkspace },
122     { "nextscreen", Action::nextScreen },
123     { "prevscreen", Action::prevScreen },
124     { "showrootmenu", Action::showRootMenu },
125     { "showworkspacemenu", Action::showWorkspaceMenu },
126     { "toggledecorations", Action::toggleDecorations },
127     { "togglegrabs", Action::toggleGrabs },
128     { "stringchain", Action::stringChain },
129     { "keychain", Action::keyChain },
130     { "numberchain", Action::numberChain },
131     { "cancelchain", Action::cancelChain },
132     { "", Action::noaction }
133   };
134
135   bool found = false;
136
137   for (int i = 0; actions[i].str != ""; ++i) {
138     if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
139       _action = actions[i].act;
140       found = true;
141       break;
142     }
143   }
144
145   if (!found) {
146     cout << "ERROR: Invalid action (" << act << "). Binding ignored.\n";
147     _add = false;
148   }
149 }
150
151 void parser::addModifier(string mod)
152 {
153   struct {
154     const char *str;
155     unsigned int mask;
156   }
157   modifiers[] = {
158     { "mod1", Mod1Mask },
159     { "mod2", Mod2Mask },
160     { "mod3", Mod3Mask },
161     { "mod4", Mod4Mask },
162     { "mod5", Mod5Mask },
163     { "control", ControlMask },
164     { "shift", ShiftMask },
165     { "", 0 }
166   };
167
168   bool found = false;
169
170   for (int i = 0; modifiers[i].str != ""; ++i) {
171     if ( strcasecmp(modifiers[i].str, mod.c_str()) == 0 ) {
172       _mask |= modifiers[i].mask;
173       found = true;
174       break;
175     }
176   }
177
178   if (!found) {
179     cout << "ERROR: Invalid modifier (" << mod << "). Binding ignored.\n";
180     _add = false;
181   }
182 }
183
184 void parser::endAction()
185 {
186   if (_add)
187     _kt->addAction(_action, _mask, _key, _arg);
188   reset();
189
190   _add = true;
191 }
192
193 void parser::startChain()
194 {
195   _kt->advanceOnNewNode();
196   setChainBinding();
197   reset();
198 }
199
200 void parser::endChain()
201 {
202   _kt->retract();
203   reset();
204 }
205
206 void parser::setChainBinding()
207 {
208   if (_add)
209     _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
210   
211   _add = true;
212   reset();
213 }
214
215 void parser::reset()
216 {
217   _mask = 0;
218   _action = Action::noaction;
219   _key = "";
220   _arg = "";
221 }