news download themes documentation links










FbCommandFactory.cc

00001 // FbCommandFactory.cc for Fluxbox Window manager
00002 // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
00003 //                and Simon Bowden (rathnor at users.sourceforge.net)
00004 //
00005 // Permission is hereby granted, free of charge, to any person obtaining a
00006 // copy of this software and associated documentation files (the "Software"),
00007 // to deal in the Software without restriction, including without limitation
00008 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 // and/or sell copies of the Software, and to permit persons to whom the
00010 // Software is furnished to do so, subject to the following conditions:
00011 //
00012 // The above copyright notice and this permission notice shall be included in
00013 // all copies or substantial portions of the Software.
00014 //
00015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00018 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00021 // DEALINGS IN THE SOFTWARE.
00022 
00023 // $Id: FbCommandFactory.cc,v 1.24 2003/12/20 17:42:04 fluxgen Exp $
00024 
00025 #include "FbCommandFactory.hh"
00026 
00027 #include "CurrentWindowCmd.hh"
00028 #include "FbCommands.hh"
00029 #include "Window.hh"
00030 #include "WorkspaceCmd.hh"
00031 #include "fluxbox.hh"
00032 #include "SimpleCommand.hh"
00033 #include "Screen.hh"
00034 
00035 #include "FbTk/StringUtil.hh"
00036 #include "FbTk/MacroCommand.hh"
00037 
00038 #include <string>
00039 
00040 #ifdef HAVE_CONFIG_H
00041 #include "config.h"
00042 #endif // HAVE_CONFIG_H
00043 
00044 #ifdef HAVE_SSTREAM
00045 #include <sstream>
00046 #define FB_istringstream istringstream
00047 #elif HAVE_STRSTREAM 
00048 #include <strstream>
00049 #define FB_istringstream istrstream
00050 #else
00051 #error "You dont have sstream or strstream headers!"
00052 #endif // HAVE_STRSTREAM
00053 
00054 using namespace std;
00055 
00056 // autoregister this module to command parser
00057 FbCommandFactory FbCommandFactory::s_autoreg;
00058 
00059 
00060 FbCommandFactory::FbCommandFactory() {
00061     // setup commands that we can handle
00062     const char* commands[] = {
00063         "arrangewindows",
00064         "bindkey",
00065         "close",
00066         "commanddialog",
00067         "detachclient",
00068         "exec",
00069         "execcommand",
00070         "execute",
00071         "iconfiy",
00072         "killwindow",
00073         "leftworkspace",
00074         "lower",
00075         "macrocmd",
00076         "maximize",
00077         "maximizehorizontal",
00078         "maximizevertical",
00079         "maximizewindow",
00080         "minimize",
00081         "minimizewindow",
00082         "moveto",
00083         "move",
00084         "movedown",
00085         "moveleft",
00086         "moveright",
00087         "movetableft",
00088         "movetabright",
00089         "moveup",
00090         "nextgroup",
00091         "nexttab",
00092         "nextwindow",
00093         "nextworkspace",
00094         "prevgroup",
00095         "prevtab",
00096         "prevwindow",
00097         "prevworkspace",
00098         "quit",
00099         "raise",
00100         "reconfigure",
00101         "reloadstyle",
00102         "resizeto",
00103         "resize",
00104         "resizehorizontal",
00105         "resizevertical",
00106         "restart",
00107         "rightworkspace",
00108         "rootmenu",
00109         "saverc",
00110         "sendtoworkspace",
00111         "setstyle",
00112         "setworkspacename",
00113         "setworkspacenamedialog",
00114         "setresourcevalue",
00115         "setresourcevaluedialog",
00116         "shade",
00117         "shadewindow",
00118         "showdesktop",
00119         "stick",
00120         "stickwindow",
00121         "toggledecor",
00122         "workspace",
00123         "workspacemenu",
00124         ""
00125     };
00126 
00127     for (int i=0;; ++i) {
00128         if (strcmp(commands[i], "") == 0)
00129             break;
00130         addCommand(commands[i]);
00131     }
00132                      
00133 }
00134 
00135 FbTk::Command *FbCommandFactory::stringToCommand(const std::string &command,
00136                                                  const std::string &arguments) {
00137     using namespace FbCommands;
00138     //
00139     // WM commands
00140     //
00141     if (command == "restart")
00142         return new RestartFluxboxCmd(arguments);
00143     else if (command == "reconfigure")
00144         return new ReconfigureFluxboxCmd();
00145     else if (command == "setstyle")
00146         return new SetStyleCmd(arguments);
00147     else if (command == "reloadstyle")
00148         return new ReloadStyleCmd();
00149     else if (command == "saverc")
00150         return new SaveResources();
00151     else if (command == "execcommand" || command == "execute" || command == "exec")
00152         return new ExecuteCmd(arguments); // execute command on key screen
00153     else if (command == "quit")
00154         return new FbTk::SimpleCommand<Fluxbox>(*Fluxbox::instance(), &Fluxbox::shutdown);
00155     else if (command == "commanddialog") // run specified fluxbox command
00156         return new CommandDialogCmd();
00157     else if (command == "bindkey")
00158         return new BindKeyCmd(arguments);
00159     else if (command == "setresourcevalue") {
00160         // we need to parse arguments as:
00161         // <remove whitespace here><resname><one whitespace><value>
00162         string name = arguments;
00163         FbTk::StringUtil::removeFirstWhitespace(name);
00164         size_t pos = name.find_first_of(" \t");
00165         // we need an argument to resource name
00166         if (pos == std::string::npos || pos == name.size())
00167             return 0;
00168         // +1 so we only remove the first whitespace
00169         // i.e so users can set space before workspace name and so on
00170         string value = name.substr(pos + 1);
00171         name = name.substr(0, pos);
00172         cerr<<"Creating SetResourceValue "<<name<<" "<<value<<endl;
00173         return new SetResourceValueCmd(name, value);
00174     } else if (command == "setresourcevaluedialog")
00175         return new SetResourceValueDialogCmd();
00176     //
00177     // Current focused window commands
00178     //
00179     else if (command == "minimizewindow" || command == "minimize" || command == "iconify")
00180         return new CurrentWindowCmd(&FluxboxWindow::iconify);
00181     else if (command == "maximizewindow" || command == "maximize")
00182         return new CurrentWindowCmd(&FluxboxWindow::maximizeFull);
00183     else if (command == "maximizevertical")
00184         return new CurrentWindowCmd(&FluxboxWindow::maximizeVertical);
00185     else if (command == "maximizehorizontal")
00186         return new CurrentWindowCmd(&FluxboxWindow::maximizeHorizontal);
00187     else if (command == "resize") {
00188         FB_istringstream is(arguments.c_str()); 
00189         int dx = 0, dy = 0;
00190         is >> dx >> dy;
00191         return new ResizeCmd(dx, dy);
00192     }
00193     else if (command == "resizeto") {
00194         FB_istringstream is(arguments.c_str());
00195         int dx = 0, dy = 0;
00196         is >> dx >> dy;
00197         return new ResizeToCmd(dx, dy);
00198     }
00199     else if (command == "resizehorizontal")
00200         return new ResizeCmd(atoi(arguments.c_str()),0);
00201     else if (command == "resizevertical")
00202         return new ResizeCmd(0,atoi(arguments.c_str()));
00203     else if (command == "moveto") {
00204        FB_istringstream is(arguments.c_str());
00205        int dx = 0, dy = 0;
00206        is >> dx >> dy;
00207        return new MoveToCmd(dx,dy);    
00208     }
00209     else if (command == "move") {
00210         FB_istringstream is(arguments.c_str());
00211         int dx = 0, dy = 0;
00212         is >> dx >> dy;
00213         return new MoveCmd(dx, dy);
00214     }
00215     else if (command == "moveright")
00216         return new MoveCmd(atoi(arguments.c_str()),0);
00217     else if (command == "moveleft")
00218         return new MoveCmd(-atoi(arguments.c_str()),0);
00219     else if (command == "moveup")
00220         return new MoveCmd(0,-atoi(arguments.c_str()));
00221     else if (command == "movedown")
00222         return new MoveCmd(0,atoi(arguments.c_str()));
00223     else if (command == "raise")
00224         return new CurrentWindowCmd(&FluxboxWindow::raise);
00225     else if (command == "lower")
00226         return new CurrentWindowCmd(&FluxboxWindow::lower);
00227     else if (command == "close")
00228         return new CurrentWindowCmd(&FluxboxWindow::close);
00229     else if (command == "shade" || command == "shadewindow")
00230         return new CurrentWindowCmd(&FluxboxWindow::shade);
00231     else if (command == "stick" || command == "stickwindow")
00232         return new CurrentWindowCmd(&FluxboxWindow::stick);
00233     else if (command == "toggledecor")
00234         return new CurrentWindowCmd(&FluxboxWindow::toggleDecoration);
00235     else if (command == "sendtoworkspace")
00236         return new SendToWorkspaceCmd(atoi(arguments.c_str()) - 1); // make 1-indexed to user
00237     else if (command == "killwindow")
00238         return new KillWindowCmd();
00239      else if (command == "nexttab")
00240         return new CurrentWindowCmd(&FluxboxWindow::nextClient);
00241     else if (command == "prevtab")
00242         return new CurrentWindowCmd(&FluxboxWindow::prevClient);
00243     else if (command == "movetableft")
00244         return new CurrentWindowCmd(&FluxboxWindow::moveClientLeft);
00245     else if (command == "movetabright")
00246         return new CurrentWindowCmd(&FluxboxWindow::moveClientRight);
00247     else if (command == "detachclient")
00248         return new CurrentWindowCmd(&FluxboxWindow::detachCurrentClient);
00249     // 
00250     // Workspace commands
00251     //
00252     else if (command == "nextworkspace" && arguments.size() == 0)
00253         return new NextWorkspaceCmd();
00254     else if (command == "prevworkspace" && arguments.size() == 0)
00255         return new PrevWorkspaceCmd();
00256     else if (command == "rightworkspace")
00257         return new RightWorkspaceCmd(atoi(arguments.c_str()));
00258     else if (command == "leftworkspace")
00259         return new LeftWorkspaceCmd(atoi(arguments.c_str()));
00260     else if (command == "workspace") {
00261         int num = 1; // workspaces appear 1-indexed to the user
00262         if (!arguments.empty())
00263             num = atoi(arguments.c_str());
00264         return new JumpToWorkspaceCmd(num-1);
00265     } else if (command == "nextwindow")
00266         return new NextWindowCmd(atoi(arguments.c_str()));
00267     else if (command == "prevwindow")
00268         return new PrevWindowCmd(atoi(arguments.c_str()));
00269     else if (command == "nextgroup")
00270         return new NextWindowCmd(atoi(arguments.c_str()) ^ BScreen::CYCLEGROUPS);
00271     else if (command == "prevgroup")
00272         return new PrevWindowCmd(atoi(arguments.c_str()) ^ BScreen::CYCLEGROUPS);
00273     else if (command == "arrangewindows")
00274         return new ArrangeWindowsCmd();
00275     else if (command == "showdesktop")
00276         return new ShowDesktopCmd();
00277     else if (command == "rootmenu")
00278         return new ShowRootMenuCmd();
00279     else if (command == "workspacemenu")
00280         return new ShowWorkspaceMenuCmd();
00281     else if (command == "setworkspacename") {
00282         if (arguments.empty())
00283             return new SetWorkspaceNameCmd("empty");
00284         else
00285             return new SetWorkspaceNameCmd(arguments);
00286     }
00287     else if (command == "setworkspacenamedialog")
00288         return new WorkspaceNameDialogCmd();
00289     //
00290     // special commands
00291     //
00292     else if (command == "macrocmd") {
00293       std::string cmd;
00294       int   err= 0;
00295       int   parse_pos= 0;
00296       FbTk::MacroCommand* macro= new FbTk::MacroCommand();
00297 
00298       while (true) {
00299         parse_pos+= err;
00300         err= FbTk::StringUtil::getStringBetween(cmd, arguments.c_str() + parse_pos,
00301                                               '{', '}', " \t\n", true);
00302         if ( err > 0 ) {
00303           std::string c, a;
00304           std::string::size_type first_pos = FbTk::StringUtil::removeFirstWhitespace(cmd);
00305           std::string::size_type second_pos= cmd.find_first_of(" \t", first_pos);
00306           if (second_pos != std::string::npos) {
00307             a= cmd.substr(second_pos);
00308             FbTk::StringUtil::removeFirstWhitespace(a);
00309             cmd.erase(second_pos);
00310           }
00311           c= FbTk::StringUtil::toLower(cmd);
00312 
00313           FbTk::Command* fbcmd= stringToCommand(c,a);
00314           if ( fbcmd ) {
00315             FbTk::RefCount<FbTk::Command> rfbcmd(fbcmd);
00316             macro->add(rfbcmd);
00317           }
00318         }
00319         else
00320           break;
00321       }
00322 
00323       if ( macro->size() > 0 )
00324         return macro;
00325 
00326       delete macro;
00327     }
00328     return 0;
00329 }

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA