00001
00002
00003
00004
00005
00006 extern "C" {
00007 #include <assert.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <sys/types.h>
00011 #include <sys/wait.h>
00012 #include <unistd.h>
00013 #include <X11/Xlib.h>
00014 }
00015
00016 #include <map>
00017
00018 #include "launcher.h"
00019
00020 #include "action.h"
00021 #include "logger.h"
00022 #include "kernel.h"
00023 #include "monitor.h"
00024
00025 Launcher::Launcher() {}
00026 Launcher::~Launcher() {}
00027
00028 void Launcher::exec(string command) {
00029
00030 Monitor *focusedMonitor = Kernel::instance()->focusedMonitor();
00031
00032 if (fork() == 0) {
00033 if (fork() == 0) {
00034 setsid();
00035
00036 close(ConnectionNumber(Kernel::instance()->display()));
00037
00038 putenv((char *)focusedMonitor->displayString().c_str());
00039
00040 char **cmdArgs = Util::arguments(command);
00041
00042 LOGDEBUG("executing '" + command + "'");
00043 execvp(cmdArgs[0], cmdArgs);
00044 LOGERROR("cannot execute '" + command + "'", true);
00045
00046 }
00047 exit(0);
00048 }
00049 wait(0);
00050 }
00051
00052 void Launcher::execSeq(Action *caller, string command) {
00053
00054 MBindings *actionBindings = KERNEL->actionBindings();
00055
00056 string cmd = command;
00057 string actionName = "";
00058 string arguments = "";
00059
00060 LOGDEBUG("sequence: got: " + cmd);
00061 bool proceed = true;
00062 for (string::iterator it = cmd.begin(); proceed; it++) {
00063
00064 proceed = it != cmd.end();
00065 if (!proceed || (*it == ',') ) {
00066 if (caller->id() == actionName) {
00067 Logger::instance()->warning(
00068 "sequence '" + actionName +
00069 "' was called recursivly, stopped");
00070 return;
00071
00072 }
00073
00074 unsigned int argCount = 0;
00075 unsigned int argDelim = actionName.find_first_of('+');
00076
00077 if (argDelim != string::npos) {
00078 argCount++;
00079 arguments = actionName.substr(argDelim + 1);
00080 LOGDEBUG("sequence: got arguments: " + arguments);
00081 actionName = actionName.substr(0, argDelim);
00082 LOGDEBUG("sequence: found arguments: " + arguments);
00083 LOGDEBUG("sequence: for action: " + actionName);
00084 }
00085
00086 Action *action = Util::get(actionBindings, actionName);
00087 if (!action) {
00088 LOGWARN("unknown action '" + actionName +
00089 "' cannot be performed sequentially");
00090 return;
00091 }
00092 if (action->promptsCount() != argCount) {
00093 LOGWARN("action '" + actionName +
00094 "' cannot be performed sequentially, because "
00095 "to much or less given arguments.");
00096 return;
00097 }
00098 if (!action->isValid()) {
00099 LOGWARN("action '" + actionName +
00100 "' cannot be performed because it"
00101 " won't have any effect");
00102 return;
00103 }
00104 if (argCount) {
00105 action->setArgument((char *)arguments.c_str());
00106 }
00107 action->perform();
00108 actionName = "";
00109 }
00110 else {
00111 actionName += *it;
00112 }
00113 }
00114 }