fixing parse error in execcommand
[bbkeys.git] / src / bbkeysconf.pl.in
blobb263e0bd42bd42e0e4d4c809e5f3162f64d827f4
1 #!@PERL@
3 # Copyright (c) 2001 Damien Tougas <damien@tougas.net>. USA.
4 # Copyright (c) 2001 Jason 'vanRijn' Kasper <vR at movingparts dot net>
5 # All rights reserved
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
28 # $Id$
30 sub usage() {
31 print "\nbbkeysconf.pl version @VERSION@\n";
32 print "Usage: bbkeysconf.pl [options]\n";
33 print "Options:\n";
34 print " -rc[file] <filename> Alternate keygrab definition file.\n";
35 print " (default is ~/.bbkeysrc)\n";
36 print " -h[elp] Display this help\n";
37 print "\n";
40 # do math in integer rather than double mode--we rely on this for rounding
41 use integer;
43 # prototypes
44 sub trim;
46 my $bbkeysrc = $ENV{HOME} . "/.bbkeysrc";
48 # get the keyconfig file from the commandline, or use the default
49 for ( $i=0; $i< @ARGV; $i++) {
50 if ($ARGV[$i] =~ /^-rc(|file)$/) {
51 $bbkeysrc = $ARGV[++$i];
52 } elsif ($_ =~ /^-h(|elp)$/) {
53 usage();
54 exit;
55 } else {
56 usage();
57 die "unknown command. exiting\n\n";
61 my @modifiers = (
62 "None",
63 "Control",
64 "Shift",
65 "Mod1",
66 "Control+Shift",
67 "Control+Mod1",
68 "Shift+Mod1",
69 "Control+Shift+Mod1"
72 my @actions = (
73 "BigNudgeDown",
74 "BigNudgeLeft",
75 "BigNudgeRight",
76 "BigNudgeUp",
77 "Close",
78 "ExecCommand",
79 "HorizontalDecrement",
80 "HorizontalIncrement",
81 "Lower",
82 "MaximizeHorizontal",
83 "MaximizeVertical",
84 "MaximizeWindow",
85 "Minimize",
86 "NextWindow",
87 "NextWorkspace",
88 "NudgeDown",
89 "NudgeLeft",
90 "NudgeUp",
91 "NudgeRight",
92 "PrevWindow",
93 "PrevWorkspace",
94 "Raise",
95 "ShadeWindow",
96 "StickWindow",
97 "ToggleDecor",
98 "Workspace1",
99 "Workspace2",
100 "Workspace3",
101 "Workspace4",
102 "Workspace5",
103 "Workspace6",
104 "Workspace7",
105 "Workspace8",
106 "Workspace9",
107 "Workspace10",
108 "Workspace11",
109 "Workspace12",
110 "VerticalIncrement",
111 "VerticalDecrement"
115 my @config;
116 my $count = 0;
117 if (open(R, "$bbkeysrc")) {
118 while (<R>) {
119 # skip blank lines or comments
120 $_ = trim($_);
121 next if ($_ =~ /^$/ or $_ =~ /^#/) ;
122 $config[$count++] = $_;
124 close(R);
127 my $save_state = 0;
128 my $exit = 0;
129 my $command = "";
131 print "\n*** BBKeys Configuration Tool ***\n";
132 &list;
133 while ($exit == 0) {
135 print "\n[A]dd [D]elete [L]ist [S]ave [Q]uit\n";
136 print "Menu: Select> ";
137 $command = <STDIN>;
138 chomp($command);
139 $command =~ s/\s*//g;
141 &add if ($command =~ /^a$/i);
142 &delete if ($command =~ /^d$/i);
143 &list if ($command =~ /^l$/i);
144 &save if ($command =~ /^s$/i);
145 &quit if ($command =~ /^q$/i);
149 exit(0);
151 sub trim {
152 local $_ = shift;
154 s/^\s*//;
155 s/\s*$//;
157 return $_;
160 sub list {
162 print "\nList:> Current Configuration\n";
163 print "-----------------------------------------------------------------\n";
165 open(CONFIG_LIST, ">-");
166 for ($i = 0; $i < $count; $i++) {
168 if ($config[$i] =~ /KeyToGrab.*?Action\(ExecCommand\)/) {
169 $config[$i] =~ m/Grab\((.*)\).*Modifier\((.*)\).*Action\((.*)\).*This\((.*)\)/;
170 $key_seq = "$2+$1";
171 $result = "$3 $4";
172 } elsif ($config[$i] =~/KeyToGrab/ ) {
173 $config[$i] =~ m/Grab\((.*)\).*Modifier\((.*)\).*Action\((.*)\)/;
174 $key_seq = "$2+$1";
175 $result = "$3";
177 write(CONFIG_LIST);
180 close(CONFIG_LIST);
182 print "-----------------------------------------------------------------\n";
186 sub add {
188 print "\nAdd: Choose Key> Up, Down, R, L, etc.\n";
189 print "Add: Choose Key> ";
190 my $grab = <STDIN>;
191 chomp($grab);
192 $grab =~ s/\s*//g;
193 if ($grab eq "") {
194 print ("\nERROR: Must enter valid key.\n");
195 return;
198 print "Add: Select Modifier> Modifier List\n";
199 print "-----------------------------------------------------------------\n";
200 open(MODIFIER_LIST, ">-");
201 for ($i = 0; $i <= $#modifiers; $i++) {
202 write(MODIFIER_LIST);
204 close(MODIFIER_LIST);
205 print "-----------------------------------------------------------------\n";
206 my $mod = $modifiers[&get_num("Add: Select Modifier> ", $#modifiers)];
208 print "Add: Choose Action> Action List\n";
209 print "-----------------------------------------------------------------\n";
210 # okay. $#actions refers to the last index of the array, not the
211 # count. so add 1 more to it and divide by 3 and with the "user
212 # integer" line up time, we get an even number of rows
213 $action_rows = ($#actions +1)/ 3;
214 open(ACTION_LIST, ">-");
215 for ($i = 0; $i <= $action_rows; $i++) {
216 $index1=$i;
217 $index2=$i + $action_rows +1;
218 $index3=$index2 + $action_rows +1;
219 write(ACTION_LIST);
221 close(ACTION_LIST);
222 print "-----------------------------------------------------------------\n";
223 my $action = $actions[&get_num("Add: Select Action> ", $#actions)];
225 $config[$count] = "KeyToGrab($grab), WithModifier($mod), " .
226 "WithAction($action)";
227 if ($action =~ /ExecCommand/) {
228 print "Add: Command To Execute> ";
229 my $dothis = <STDIN>;
230 chomp($dothis);
231 $config[$count] .= ", DoThis($dothis)";
234 $count++;
235 $save_state = 1;
236 print "Add:> Finished\n";
240 sub delete {
242 print "\n";
243 $item = &get_num("Delete: Select Item> ", $count - 1);
245 for ($i = 0; $i < $count - 1; $i++) {
246 $config[$i] = $config[$i + 1] if ($i >= $item);
249 $count--;
250 $save_state = 1;
251 print "Delete:> Finished\n";
255 sub save {
257 print "Save: Confirm> Save the current configuration?\n";
258 print "Save: Confirm> ";
259 my $confirm = <STDIN>;
260 chomp($confirm);
261 $confirm =~ s/\s*//g;
263 if ($confirm =~ /^(y|yes)$/i) {
264 open(W, ">$bbkeysrc");
265 print W "#~/.bbkeysrc, automagically configured by bbkeysconf.pl\n\n\n";
266 for ($i = 0; $i < $count; $i++) {
267 print W $config[$i] . "\n";
269 close(W);
270 $save_state = 0;
271 print "Save:> Finished\n";
273 else {
274 print "Save:> Aborted\n";
279 sub quit {
281 if ($save_state == 1) {
282 print "Quit: Notice> Configuration has been changed\n";
283 &save;
285 print "Quit:> Finished\n";
286 $exit = 1;
290 sub get_num {
292 my $prompt = $_[0];
293 my $maxnum = $_[1];
294 my $validnum = 0;
295 my $input;
297 print $prompt . "Enter a number between 0 and $maxnum\n";
298 while ($validnum == 0) {
299 print $prompt;
300 $input = <STDIN>;
301 chomp($input);
302 $input =~ s/\s*//g;
303 if (($input =~ /^\d+$/) && ($input <= $maxnum)) {
304 $validnum = 1;
306 else {
307 print "\nERROR: Invalid selection, please try again\n\n";
311 return($input);
315 format CONFIG_LIST =
316 @> @<<<<<<<<<<<<<<<<<<<<<<<<<<< = @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
317 $i, $key_seq, $result
320 format MODIFIER_LIST =
321 @> @<<<<<<<<<<<<<<<<<<<<<<
322 $i, $modifiers[$i]
325 format ACTION_LIST =
326 @> @<<<<<<<<<<<<<<<<<<<< @> @<<<<<<<<<<<<<<<<<<< @> @<<<<<<<<<<<<<<<<<<<
327 $index1, $actions[$index1], {$actions[$index2] ? ($index2) : ""}, {$actions[$index2] ? $actions[$index2] : ""}, {$actions[$index3] ? ($index3) : ""}, {$actions[$index3] ? $actions[$index3] : ""}