summaryrefslogtreecommitdiff
path: root/src/FbCommandFactory.cc
blob: 1382c8917ffae245ad17e9416b7d4f9ba9371655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// FbCommandFactory.cc for Fluxbox Window manager
// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//                and Simon Bowden (rathnor at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id$

#include "FbCommandFactory.hh"

#include "CurrentWindowCmd.hh"
#include "FbCommands.hh"
#include "Window.hh"
#include "WorkspaceCmd.hh"
#include "fluxbox.hh"
#include "SimpleCommand.hh"
#include "Screen.hh"

#include "FbTk/StringUtil.hh"
#include "FbTk/MacroCommand.hh"
#include "FbTk/stringstream.hh"

#include <string>

#ifdef HAVE_CSTDIO
  #include <cstdio>
#else
  #include <stdio.h>
#endif

using std::string;
using std::vector;
using std::cerr;
using std::endl;

// autoregister this module to command parser
FbCommandFactory FbCommandFactory::s_autoreg;

static int getint(const char *str, int defaultvalue) {
    sscanf(str, "%d", &defaultvalue);
    return defaultvalue;
}

FbCommandFactory::FbCommandFactory() {
    // setup commands that we can handle
    const char* commands[] = {
        "addworkspace",
        "arrangewindows",
        "bindkey",
        "close",
        "closeallwindows",
        "commanddialog",
        "custommenu",
        "deiconify",
        "detachclient",
        "export",
        "exec",
        "execcommand",
        "execute",
        "exit",
        "focusup",
        "focusdown",
        "focusleft",
        "focusright",
        "fullscreen",
        "hidemenus",
        "iconify",
        "keymode",
        "kill",
        "killwindow",
        "leftworkspace",
        "lower",
        "lowerlayer",
        "macrocmd",
        "maximize",
        "maximizehorizontal",
        "maximizevertical",
        "maximizewindow",
        "minimize",
        "minimizewindow",
        "moveto",
        "move",
        "movedown",
        "moveleft",
        "moveright",
        "movetableft",
        "movetabright",
        "moveup",
        "nextgroup",
        "nexttab",
        "nextwindow",
        "nextworkspace",
        "prevgroup",
        "prevtab",
        "prevwindow",
        "prevworkspace",
        "quit",
        "raise",
        "raiselayer",
        "reconfig",
        "reconfigure",
        "reloadstyle",
        "removelastworkspace",
        "resizeto",
        "resize",
        "resizehorizontal",
        "resizevertical",
        "restart",
        "rightworkspace",
        "rootmenu",
        "saverc",
        "sendtoworkspace",
        "sendtonextworkspace",
        "sendtoprevworkspace",
        "setalpha",
        "setenv",
        "sethead",
        "setmodkey",
        "setstyle",
        "setworkspacename",
        "setworkspacenamedialog",
        "setresourcevalue",
        "setresourcevaluedialog",
        "shade",
        "shadewindow",
        "showdesktop",
        "stick",
        "stickwindow",
        "tab",
        "taketoworkspace",
        "taketonextworkspace",
        "taketoprevworkspace",
        "togglecmd",
        "toggledecor",
        "windowmenu",
        "workspace",
        /* NOTE: The following are DEPRECATED and subject to removal */
        "workspace1",
        "workspace2",
        "workspace3",
        "workspace4",
        "workspace5",
        "workspace6",
        "workspace7",
        "workspace8",
        "workspace9",
        "workspace10",
        "workspace11",
        "workspace12",
        /* end note */
        "workspacemenu",
        0
    };

    for (int i=0; commands[i]; ++i)
        addCommand(commands[i]);
}

FbTk::Command *FbCommandFactory::stringToCommand(const std::string &command,
                                                 const std::string &arguments) {
    using namespace FbCommands;
    //
    // WM commands
    //
    if (command == "restart")
        return new RestartFluxboxCmd(arguments);
    else if (command == "reconfigure" || command == "reconfig")
        return new ReconfigureFluxboxCmd();
    else if (command == "setstyle")
        return new SetStyleCmd(arguments);
    else if (command == "reloadstyle")
        return new ReloadStyleCmd();
    else if (command == "keymode")
        return new KeyModeCmd(arguments);
    else if (command == "saverc")
        return new SaveResources();
    else if (command == "execcommand" || command == "execute" || command == "exec")
        return new ExecuteCmd(arguments); // execute command on key screen
    else if (command == "exit" || command == "quit")
        return new ExitFluxboxCmd();
    else if (command == "setenv" || command == "export") {

        string name = arguments;
        FbTk::StringUtil::removeFirstWhitespace(name);
        FbTk::StringUtil::removeTrailingWhitespace(name);
        size_t pos = name.find_first_of(command == "setenv" ? " \t" : "=");
        if (pos == string::npos || pos == name.size())
            return 0;

        string value = name.substr(pos + 1);
        name = name.substr(0, pos);
        return new ExportCmd(name, value);
    }
    else if (command == "setmodkey") {
        string modkey(arguments);
        FbTk::StringUtil::removeFirstWhitespace(modkey);
        FbTk::StringUtil::removeTrailingWhitespace(modkey);

        return new SetModKeyCmd(modkey);
    }
    else if (command == "commanddialog") // run specified fluxbox command
        return new CommandDialogCmd();
    else if (command == "bindkey")
        return new BindKeyCmd(arguments);
    else if (command == "setresourcevalue") {
        // we need to parse arguments as:
        // <remove whitespace here><resname><one whitespace><value>
        string name = arguments;
        FbTk::StringUtil::removeFirstWhitespace(name);
        size_t pos = name.find_first_of(" \t");
        // we need an argument to resource name
        if (pos == std::string::npos || pos == name.size())
            return 0;
        // +1 so we only remove the first whitespace
        // i.e so users can set space before workspace name and so on
        string value = name.substr(pos + 1);
        name = name.substr(0, pos);
        return new SetResourceValueCmd(name, value);
    } else if (command == "setresourcevaluedialog")
        return new SetResourceValueDialogCmd();
    else if (command == "addworkspace")
        return new AddWorkspaceCmd();
    else if (command == "removelastworkspace")
        return new RemoveLastWorkspaceCmd();
    //
    // Current focused window commands
    //
    else if (command == "fullscreen")
        return new FullscreenCmd();
    else if (command == "minimizewindow" || command == "minimize" || command == "iconify") {
        string cmd;
        if (FbTk::StringUtil::getStringBetween(cmd, arguments.c_str() +
                                               0, '(', ')', " \t\n", true)
            && cmd == "layer")
            return new MinimizeLayerCmd();
        else
            return new CurrentWindowCmd(&FluxboxWindow::iconify);
    } else if (command == "maximizewindow" || command == "maximize")
        return new CurrentWindowCmd(&FluxboxWindow::maximizeFull);
    else if (command == "maximizevertical")
        return new CurrentWindowCmd(&FluxboxWindow::maximizeVertical);
    else if (command == "maximizehorizontal")
        return new CurrentWindowCmd(&FluxboxWindow::maximizeHorizontal);
    else if (command == "setalpha") {
        typedef vector<string> StringTokens;
        StringTokens tokens;
        FbTk::StringUtil::stringtok<StringTokens>(tokens, arguments);

        int focused, unfocused;
        bool relative, un_rel;

        if (tokens.empty()) { // set default alpha
            focused = unfocused = 256;
            relative = un_rel = false;
        } else {
            relative = un_rel = (tokens[0][0] == '+' || tokens[0][0] == '-');
            focused = unfocused = atoi(tokens[0].c_str());
        }

        if (tokens.size() > 1) { // set different unfocused alpha
            un_rel = (tokens[1][0] == '+' || tokens[1][0] == '-');
            unfocused = atoi(tokens[1].c_str());
        }

        return new SetAlphaCmd(focused, relative, unfocused, un_rel);
    } else if (command == "resize") {
        FbTk_istringstream is(arguments.c_str());
        int dx = 0, dy = 0;
        is >> dx >> dy;
        return new ResizeCmd(dx, dy);
    }
    else if (command == "resizeto") {
        FbTk_istringstream is(arguments.c_str());
        int dx = 0, dy = 0;
        is >> dx >> dy;
        return new ResizeToCmd(dx, dy);
    }
    else if (command == "resizehorizontal")
        return new ResizeCmd(atoi(arguments.c_str()),0);
    else if (command == "resizevertical")
        return new ResizeCmd(0,atoi(arguments.c_str()));
    else if (command == "moveto") {
        typedef vector<string> StringTokens;
        StringTokens tokens;
        FbTk::StringUtil::stringtok<StringTokens>(tokens, arguments);

        if (tokens.size() < 2) {
            cerr<<"*** WARNING: missing arguments for MoveTo\n";
            return NULL;
        }

        unsigned int refc = MoveToCmd::UPPER|MoveToCmd::LEFT;
        int dx = 0;
        int dy = 0;

        if (tokens[0][0] == '*')
            refc |= MoveToCmd::IGNORE_X;
        else
            dx = atoi(tokens[0].c_str());

        if (tokens[1][0] == '*' && ! (refc & MoveToCmd::IGNORE_X))
            refc |= MoveToCmd::IGNORE_Y;
        else
            dy = atoi(tokens[1].c_str());

        if (tokens.size() >= 3) {
            tokens[2] = FbTk::StringUtil::toLower(tokens[2]);
            if (tokens[2] == "left" || tokens[2] == "upperleft" || tokens[2] == "lowerleft") {
                refc |= MoveToCmd::LEFT;
                refc &= ~MoveToCmd::RIGHT;
            } else if (tokens[2] == "right" || tokens[2] == "upperright" || tokens[2] == "lowerright") {
                refc |= MoveToCmd::RIGHT;
                refc &= ~MoveToCmd::LEFT;
            }

            if (tokens[2] == "upper" || tokens[2] == "upperleft" || tokens[2] == "upperright") {
                refc |= MoveToCmd::UPPER;
                refc &= ~MoveToCmd::LOWER;
            } else if (tokens[2] == "lower" || tokens[2] == "lowerleft" || tokens[2] == "lowerright") {
                refc |= MoveToCmd::LOWER;
                refc &= ~MoveToCmd::UPPER;
            }
        }

        return new MoveToCmd(dx, dy, refc);
    }
    else if (command == "move") {
        FbTk_istringstream is(arguments.c_str());
        int dx = 0, dy = 0;
        is >> dx >> dy;
        return new MoveCmd(dx, dy);
    }
    else if (command == "moveright")
        return new MoveCmd(atoi(arguments.c_str()),0);
    else if (command == "moveleft")
        return new MoveCmd(-atoi(arguments.c_str()),0);
    else if (command == "moveup")
        return new MoveCmd(0,-atoi(arguments.c_str()));
    else if (command == "movedown")
        return new MoveCmd(0,atoi(arguments.c_str()));
    else if (command == "raise")
        return new CurrentWindowCmd(&FluxboxWindow::raise);
    else if (command == "raiselayer")
        return new CurrentWindowCmd(&FluxboxWindow::raiseLayer);
    else if (command == "lower")
        return new CurrentWindowCmd(&FluxboxWindow::lower);
    else if (command == "lowerlayer")
        return new CurrentWindowCmd(&FluxboxWindow::lowerLayer);
    else if (command == "close")
        return new CurrentWindowCmd(&FluxboxWindow::close);
    else if (command == "closeallwindows")
        return new CloseAllWindowsCmd();
    else if (command == "shade" || command == "shadewindow")
        return new CurrentWindowCmd(&FluxboxWindow::shade);
    else if (command == "stick" || command == "stickwindow")
        return new CurrentWindowCmd(&FluxboxWindow::stick);
    else if (command == "toggledecor")
        return new CurrentWindowCmd(&FluxboxWindow::toggleDecoration);
    else if (command == "sethead")
        return new SetHeadCmd(atoi(arguments.c_str()));
    else if (command == "sendtoworkspace")
        // workspaces appear 1-indexed to the user, hence the minus 1
        return new SendToWorkspaceCmd(getint(arguments.c_str(), 1) - 1);
    else if (command == "sendtonextworkspace")
        return new SendToNextWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "sendtoprevworkspace")
        return new SendToPrevWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "taketoworkspace")
        // workspaces appear 1-indexed to the user, hence the minus 1
        return new TakeToWorkspaceCmd(getint(arguments.c_str(), 1) - 1);
    else if (command == "taketonextworkspace")
        return new TakeToNextWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "taketoprevworkspace")
        return new TakeToPrevWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "killwindow" || command == "kill")
        return new KillWindowCmd();
    else if (command == "tab")
        return new GoToTabCmd(getint(arguments.c_str(), 1));
    else if (command == "nexttab")
        return new CurrentWindowCmd(&FluxboxWindow::nextClient);
    else if (command == "prevtab")
        return new CurrentWindowCmd(&FluxboxWindow::prevClient);
    else if (command == "movetableft")
        return new CurrentWindowCmd(&FluxboxWindow::moveClientLeft);
    else if (command == "movetabright")
        return new CurrentWindowCmd(&FluxboxWindow::moveClientRight);
    else if (command == "detachclient")
        return new CurrentWindowCmd(&FluxboxWindow::detachCurrentClient);
    else if (command == "windowmenu")
        return new CurrentWindowCmd(&FluxboxWindow::popupMenu);
    //
    // Workspace commands
    //
    else if (command == "nextworkspace")
        return new NextWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "prevworkspace")
        return new PrevWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "rightworkspace")
        return new RightWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "leftworkspace")
        return new LeftWorkspaceCmd(getint(arguments.c_str(), 1));
    else if (command == "workspace")
        // workspaces appear 1-indexed to the user, hence the minus 1
        return new JumpToWorkspaceCmd(getint(arguments.c_str(), 1) - 1);
    else if (command.substr(0, 9) == "workspace" && command[9] >= '0' && command[9] <= '9') {
        cerr<<"*** WARNING: 'Workspace<n>' actions are deprecated! Use 'Workspace <n>' instead"<<endl;
        return new JumpToWorkspaceCmd(getint(command.substr(9).c_str(), 1) - 1);

    } else if (command == "nextwindow")
        return new NextWindowCmd(atoi(arguments.c_str()));
    else if (command == "prevwindow")
        return new PrevWindowCmd(atoi(arguments.c_str()));
    else if (command == "focusup")
        return new DirFocusCmd(FocusControl::FOCUSUP);
    else if (command == "focusdown")
        return new DirFocusCmd(FocusControl::FOCUSDOWN);
    else if (command == "focusleft")
        return new DirFocusCmd(FocusControl::FOCUSLEFT);
    else if (command == "focusright")
        return new DirFocusCmd(FocusControl::FOCUSRIGHT);
    else if (command == "nextgroup")
        return new NextWindowCmd(atoi(arguments.c_str()) ^ FocusControl::CYCLEGROUPS);
    else if (command == "prevgroup")
        return new PrevWindowCmd(atoi(arguments.c_str()) ^ FocusControl::CYCLEGROUPS);
    else if (command == "arrangewindows")
        return new ArrangeWindowsCmd();
    else if (command == "showdesktop")
        return new ShowDesktopCmd();
    else if (command == "hidemenus")
        return new HideMenuCmd();
    else if (command == "rootmenu")
        return new ShowRootMenuCmd();
    else if (command == "custommenu")
        return new ShowCustomMenuCmd(arguments.c_str());
    else if (command == "workspacemenu")
        return new ShowWorkspaceMenuCmd();
    else if (command == "setworkspacename") {
        if (arguments.empty())
            return new SetWorkspaceNameCmd("empty");
        else
            return new SetWorkspaceNameCmd(arguments);
    }
    else if (command == "setworkspacenamedialog")
        return new WorkspaceNameDialogCmd();
    //
    // special commands
    //
    else if (command == "deiconify") {

        FbTk_istringstream iss(arguments.c_str());
        string mode;
        string d;
        DeiconifyCmd::Destination dest;

        iss >> mode;
        if (iss.fail())
            mode="lastworkspace";
        mode= FbTk::StringUtil::toLower(mode);

        iss >> d;
        if (iss.fail())
            d="current";
        d= FbTk::StringUtil::toLower(d);
        if (d == "origin" )
          dest= DeiconifyCmd::ORIGIN;
        else if (d == "originquiet")
          dest= DeiconifyCmd::ORIGINQUIET;
        else
          dest= DeiconifyCmd::CURRENT;

        if ( mode == "all" )
            return new DeiconifyCmd(DeiconifyCmd::ALL, dest);
        else if ( mode == "allworkspace" )
            return new DeiconifyCmd(DeiconifyCmd::ALLWORKSPACE, dest);
        else if ( mode == "last" )
            return new DeiconifyCmd(DeiconifyCmd::LAST, dest);
        else // lastworkspace, default
            return new DeiconifyCmd(DeiconifyCmd::LASTWORKSPACE, dest);

    } else if (command == "macrocmd") {
      string cmd;
      int   err= 0;
      int   parse_pos= 0;
      FbTk::MacroCommand* macro= new FbTk::MacroCommand();

      while (true) {
        parse_pos+= err;
        err= FbTk::StringUtil::getStringBetween(cmd, arguments.c_str() +
                                                parse_pos,
                                                '{', '}', " \t\n", true);
        if ( err > 0 ) {
          string c, a;
          string::size_type first_pos =
              FbTk::StringUtil::removeFirstWhitespace(cmd);
          string::size_type second_pos =
              cmd.find_first_of(" \t", first_pos);
          if (second_pos != string::npos) {
            a= cmd.substr(second_pos);
            FbTk::StringUtil::removeFirstWhitespace(a);
            cmd.erase(second_pos);
          }
          c= FbTk::StringUtil::toLower(cmd);

          FbTk::Command* fbcmd= stringToCommand(c,a);
          if (fbcmd) {
            FbTk::RefCount<FbTk::Command> rfbcmd(fbcmd);
            macro->add(rfbcmd);
          }
        } else
          break;
      }

      if ( macro->size() > 0 )
        return macro;

      delete macro;
    } else if (command == "togglecmd") {
      string cmd;
      int   err= 0;
      int   parse_pos= 0;
      FbTk::ToggleCommand* macro= new FbTk::ToggleCommand();

      while (true) {
        parse_pos+= err;
        err= FbTk::StringUtil::getStringBetween(cmd, arguments.c_str() +
                                                parse_pos,
                                                '{', '}', " \t\n", true);
        if ( err > 0 ) {
          string c, a;
          string::size_type first_pos =
              FbTk::StringUtil::removeFirstWhitespace(cmd);
          string::size_type second_pos=
              cmd.find_first_of(" \t", first_pos);
          if (second_pos != string::npos) {
            a= cmd.substr(second_pos);
            FbTk::StringUtil::removeFirstWhitespace(a);
            cmd.erase(second_pos);
          }
          c= FbTk::StringUtil::toLower(cmd);

          FbTk::Command* fbcmd= stringToCommand(c,a);
          if (fbcmd) {
            FbTk::RefCount<FbTk::Command> rfbcmd(fbcmd);
            macro->add(rfbcmd);
          }
        } else
          break;
      }

      if ( macro->size() > 0 )
        return macro;

      delete macro;
    }
    return 0;
}