]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/parser.cc
toggledecor almost done
[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     { "toggledecorations", Action::toggleDecorations },
105     { "stringchain", Action::stringChain },
106     { "keychain", Action::keyChain },
107     { "numberchain", Action::numberChain },
108     { "cancelchain", Action::cancelChain },
109     { "", Action::noaction }
110   };
111
112   bool found = false;
113
114   for (int i = 0; actions[i].str != ""; ++i) {
115     if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
116       _action = actions[i].act;
117       found = true;
118     }
119   }
120
121   if (!found)
122     _action = Action::noaction;
123 }
124
125 void parser::addModifier(string mod)
126 {
127   struct {
128     string str;
129     unsigned int mask;
130   }
131   modifiers[] = {
132     { "Mod1", Mod1Mask },
133     { "Mod2", Mod2Mask },
134     { "Mod3", Mod3Mask },
135     { "Mod4", Mod4Mask },
136     { "Control", ControlMask },
137     { "Shift", ShiftMask },
138     { "", 0 }
139   };
140
141   for (int i = 0; modifiers[i].str != ""; ++i) {
142     if (modifiers[i].str == mod)
143       _mask |= modifiers[i].mask;
144   }
145 }
146
147 void parser::endAction()
148 {
149   _kt->addAction(_action, _mask, _key, _arg);
150   reset();
151 }
152
153 void parser::startChain()
154 {
155   _kt->advanceOnNewNode();
156   setChainBinding();
157   reset();
158 }
159
160 void parser::endChain()
161 {
162   _kt->retract();
163   reset();
164 }
165
166 void parser::setChainBinding()
167 {
168   if (_mask != 0 && _key != "") {
169     _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
170     reset();
171   }
172 }
173
174 void parser::reset()
175 {
176   _mask = 0;
177   _action = Action::noaction;
178   _key = "";
179   _arg = "";
180 }