]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/parser.cc
uber patch.
[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 extern "C" {
24 #include <stdio.h>
25 #include <string.h>
26 }
27
28 #include "parser.hh"
29 #include <string>
30
31 using std::string;
32
33 parser::parser(keytree *kt, Config *conf)
34   : _kt(kt), _config(conf), _mask(0), _action(Action::noaction),
35     _key(""), _arg("")
36 {
37 }
38
39 parser::~parser()
40 {
41   // nothing to see here. move along.
42 }
43
44 void parser::parse(string rc_file)
45 {
46   extern int yyparse(void *);
47   extern FILE *yyin;
48
49   yyin = fopen(rc_file.c_str(), "r");
50
51   yyparse(this);
52
53   fclose(yyin);
54   _kt->reset();
55   _kt->initialize();
56 }
57
58 void parser::setAction(string act)
59 {
60   struct {
61     const char* str;
62     Action::ActionType act;
63   }
64   actions[] = {
65     { "noaction", Action::noaction },
66     { "execute", Action::execute },
67     { "iconify", Action::iconify },
68     { "raise", Action::raise },
69     { "lower", Action::lower },
70     { "close", Action::close },
71     { "toggleshade", Action::toggleshade },
72     { "toggleomnipresent", Action::toggleomnipresent },
73     { "movewindowup", Action::moveWindowUp },
74     { "movewindowdown", Action::moveWindowDown },
75     { "movewindowleft", Action::moveWindowLeft },
76     { "movewindowright", Action::moveWindowRight },
77     { "resizewindowwidth", Action::resizeWindowWidth },
78     { "resizewindowheight", Action::resizeWindowHeight },
79     { "togglemaximizefull", Action::toggleMaximizeFull },
80     { "togglemaximizevertical", Action::toggleMaximizeVertical },
81     { "togglemaximizehorizontal", Action::toggleMaximizeHorizontal },
82     { "sendtoworkspace", Action::sendToWorkspace },
83     { "nextwindow", Action::nextWindow },
84     { "prevwindow", Action::prevWindow },
85     { "nextwindowonallworkspaces", Action::nextWindowOnAllWorkspaces },
86     { "prevwindowonallworkspaces", Action::prevWindowOnAllWorkspaces },
87     { "nextwindowonallscreens", Action::nextWindowOnAllScreens },
88     { "prevwindowonallscreens", Action::prevWindowOnAllScreens },
89     { "nextwindowofclass", Action::nextWindowOfClass },
90     { "prevwindowofclass", Action::prevWindowOfClass },
91     { "nextwindowofclassonallworkspaces", Action::nextWindowOfClassOnAllWorkspaces },
92     { "prevwindowofclassonallworkspaces", Action::prevWindowOfClassOnAllWorkspaces },
93     { "changeworkspace", Action::changeWorkspace },
94     { "nextworkspace", Action::nextWorkspace },
95     { "prevworkspace", Action::prevWorkspace },
96     { "nextworkspacerow", Action::upWorkspace },
97     { "prevworkspacerow", Action::downWorkspace },
98     { "prevworkspacecolumn", Action::leftWorkspace },
99     { "nextworkspacecolumn", Action::rightWorkspace },
100     { "nextscreen", Action::nextScreen },
101     { "prevscreen", Action::prevScreen },
102     { "showrootmenu", Action::showRootMenu },
103     { "showworkspacemenu", Action::showWorkspaceMenu },
104     { "stringchain", Action::stringChain },
105     { "keychain", Action::keyChain },
106     { "numberchain", Action::numberChain },
107     { "cancelchain", Action::cancelChain },
108     { "", Action::noaction }
109   };
110
111   bool found = false;
112
113   for (int i = 0; actions[i].str != ""; ++i) {
114     if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
115       _action = actions[i].act;
116       found = true;
117     }
118   }
119
120   if (!found)
121     _action = Action::noaction;
122 }
123
124 void parser::addModifier(string mod)
125 {
126   struct {
127     string str;
128     unsigned int mask;
129   }
130   modifiers[] = {
131     { "Mod1", Mod1Mask },
132     { "Mod2", Mod2Mask },
133     { "Mod3", Mod3Mask },
134     { "Mod4", Mod4Mask },
135     { "Control", ControlMask },
136     { "Shift", ShiftMask },
137     { "", 0 }
138   };
139
140   for (int i = 0; modifiers[i].str != ""; ++i) {
141     if (modifiers[i].str == mod)
142       _mask |= modifiers[i].mask;
143   }
144 }
145
146 void parser::endAction()
147 {
148   _kt->addAction(_action, _mask, _key, _arg);
149   reset();
150 }
151
152 void parser::startChain()
153 {
154   _kt->advanceOnNewNode();
155   setChainBinding();
156   reset();
157 }
158
159 void parser::endChain()
160 {
161   _kt->retract();
162   reset();
163 }
164
165 void parser::setChainBinding()
166 {
167   if (_mask != 0 && _key != "") {
168     _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
169     reset();
170   }
171 }
172
173 void parser::reset()
174 {
175   _mask = 0;
176   _action = Action::noaction;
177   _key = "";
178   _arg = "";
179 }