]> icculus.org git repositories - mikachu/openbox.git/blob - util/epist/parser.cc
A few bug fixes and change the config to case insensitive and be more
[mikachu/openbox.git] / util / epist / parser.cc
1 extern "C" {
2 #include <stdio.h>
3 #include <string.h>
4 }
5
6 #include "parser.hh"
7 #include <string>
8
9 using std::string;
10
11 parser::parser(keytree *kt)
12     : _kt(kt), _mask(0), _action(Action::noaction), _key(""), _arg("") 
13 {
14 }
15
16 parser::~parser()
17 {
18     // nothing to see here. move along.
19 }
20
21 void parser::parse(string rc_file)
22 {
23     extern int yyparse(void *);
24     extern FILE *yyin;
25
26     yyin = fopen(rc_file.c_str(), "r");
27
28     yyparse(this);
29
30     fclose(yyin);
31     _kt->reset();
32 }
33
34 void parser::setAction(string act)
35 {
36     struct {
37         const char* str;
38         Action::ActionType act;
39     }
40     actions[] = {
41         { "noaction", Action::noaction },
42         { "execute", Action::execute },
43         { "iconify", Action::iconify },
44         { "raise", Action::raise },
45         { "lower", Action::lower },
46         { "close", Action::close },
47         { "toggleshade", Action::toggleshade },
48         { "toggleomnipresent", Action::toggleomnipresent },
49         { "movewindowup", Action::moveWindowUp },
50         { "movewindowdown", Action::moveWindowDown },
51         { "movewindowleft", Action::moveWindowLeft },
52         { "movewindowright", Action::moveWindowRight },
53         { "resizewindowwidth", Action::resizeWindowWidth },
54         { "resizewindowheight", Action::resizeWindowHeight },
55         { "togglemaximizefull", Action::toggleMaximizeFull },
56         { "togglemaximizevertical", Action::toggleMaximizeVertical },
57         { "togglemaximizehorizontal", Action::toggleMaximizeHorizontal },
58         { "sendtoworkspace", Action::sendToWorkspace },
59         { "nextwindow", Action::nextWindow },
60         { "prevwindow", Action::prevWindow },
61         { "nextwindowonallworkspaces", Action::nextWindowOnAllWorkspaces },
62         { "prevwindowonallworkspaces", Action::prevWindowOnAllWorkspaces },
63         { "nextwindowonallscreens", Action::nextWindowOnAllScreens },
64         { "prevwindowonallscreens", Action::prevWindowOnAllScreens },
65         { "nextwindowofclass", Action::nextWindowOfClass },
66         { "prevwindowofclass", Action::prevWindowOfClass },
67         { "nextwindowofclassonallworkspaces", Action::nextWindowOfClassOnAllWorkspaces },
68         { "prevwindowofclassonallworkspaces", Action::prevWindowOfClassOnAllWorkspaces },
69         { "changeworkspace", Action::changeWorkspace },
70         { "nextworkspace", Action::nextWorkspace },
71         { "prevworkspace", Action::prevWorkspace },
72         { "nextscreen", Action::nextScreen },
73         { "prevscreen", Action::prevScreen },
74         { "showrootmenu", Action::showRootMenu },
75         { "showworkspacemenu", Action::showWorkspaceMenu },
76         { "stringchain", Action::stringChain },
77         { "keychain", Action::keyChain },
78         { "numberchain", Action::numberChain },
79         { "cancel", Action::cancel },
80         { "", Action::noaction }
81     };
82
83     bool found = false;
84
85     for (int i = 0; actions[i].str != ""; ++i) {
86         if ( strcasecmp(actions[i].str, act.c_str()) == 0 ) {
87             _action = actions[i].act;
88             found = true;
89         }
90     }
91
92     if (!found)
93         _action = Action::noaction;
94 }
95
96 void parser::addModifier(string mod)
97 {
98     struct {
99         string str;
100         unsigned int mask;
101     }
102     modifiers[] = {
103         { "Mod1", Mod1Mask },
104         { "Mod2", Mod2Mask },
105         { "Mod3", Mod3Mask },
106         { "Mod4", Mod4Mask },
107         { "Control", ControlMask },
108         { "Shift", ShiftMask },
109         { "", 0 }
110     };
111
112     for (int i = 0; modifiers[i].str != ""; ++i) {
113         if (modifiers[i].str == mod)
114             _mask |= modifiers[i].mask;
115     }
116 }
117
118 void parser::endAction()
119 {
120     _kt->addAction(_action, _mask, _key, _arg);
121     reset();
122 }
123
124 void parser::startChain()
125 {
126     _kt->advanceOnNewNode();
127     setChainBinding();
128     reset();
129 }
130
131 void parser::endChain()
132 {
133     _kt->retract();
134     reset();
135 }
136
137 void parser::setChainBinding()
138 {
139     if (_mask != 0 && _key != "") {
140         _kt->setCurrentNodeProps(Action::noaction, _mask, _key, "");
141         reset();
142     }
143 }
144
145 void parser::reset()
146 {
147     _mask = 0;
148     _action = Action::noaction;
149     _key = "";
150     _arg = "";
151 }