File indexing completed on 2024-05-12 16:23:35
0001 /*************************************************************************** 0002 * Copyright (C) 2005-2008 by Bjoern Erik Nilsen & Fredrik Berg Kjoelstad* 0003 * bjoern.nilsen@bjoernen.com & fredrikbk@hotmail.com * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 #include "commandlinegrabber.h" 0021 0022 #include <cstdlib> 0023 #include <cstring> 0024 0025 #include "imagegrabber.h" 0026 #include "logger.h" 0027 #include "src/technical/util.h" 0028 0029 enum WarnIfExitOne { noWarn, doWarn }; 0030 0031 bool callSystem(const char* task, const char* commandLine, 0032 WarnIfExitOne warn = doWarn) { 0033 if (!commandLine || strlen(commandLine) == 0) { 0034 Logger::get().logDebug("No process defined for '%s'", task); 0035 return true; 0036 } 0037 Logger::get().logDebug("Attempting task '%s'", task); 0038 int r = system(commandLine); 0039 if (!WIFEXITED(r)) { 0040 Logger::get().logFatal("Could not start task '%s': %s", task, 0041 commandLine); 0042 return false; 0043 } 0044 int code = WEXITSTATUS(r); 0045 if (code == 0 || code == 256) { 0046 // vgrabberj in daemon mode uses return code 256 0047 Logger::get().logDebug("Task '%s' returned code %d: %s", task, code, 0048 commandLine); 0049 return true; 0050 } 0051 if (code == 1) { 0052 if (warn == doWarn) 0053 Logger::get().logWarning("Task '%s' returned code %d: %s", task, code, 0054 commandLine); 0055 else 0056 Logger::get().logDebug("Task '%s' returned code %d: %s", task, code, 0057 commandLine); 0058 return true; 0059 } 0060 Logger::get().logFatal( 0061 "Task '%s' returned code %d: %s", 0062 task, code, commandLine); 0063 return false; 0064 } 0065 0066 0067 CommandLineGrabber::CommandLineGrabber(const char* path) 0068 : ImageGrabber(path) { 0069 isInitSuccess = false; 0070 this->prePoll = ""; 0071 this->startProcess = ""; 0072 this->stopProcess = ""; 0073 } 0074 0075 0076 CommandLineGrabber::~CommandLineGrabber() { 0077 }; 0078 0079 0080 bool CommandLineGrabber::setPrePollCommand(const char *command) { 0081 // This happens if the user doesn't uses pre poll 0082 if ( strcmp(command, "") == 0) { 0083 return true; 0084 } 0085 prePoll = parseCommand(command); 0086 if (prePoll != "") { 0087 return true; 0088 } 0089 return false; 0090 } 0091 0092 0093 bool CommandLineGrabber::setStartCommand(const char *command) { 0094 // This happens if the user doesn't uses start command 0095 if ( strcmp(command, "") == 0) { 0096 return true; 0097 } 0098 startProcess = parseCommand(command); 0099 if (startProcess != "") { 0100 return true; 0101 } 0102 return false; 0103 } 0104 0105 0106 bool CommandLineGrabber::setStopCommand(const char *command) { 0107 stopProcess = parseCommand(command); 0108 return true; 0109 } 0110 0111 0112 bool CommandLineGrabber::init() { 0113 if (!isGrabberProcess()) 0114 return true; 0115 return callSystem("start grabber", startProcess.c_str()); 0116 } 0117 0118 0119 bool CommandLineGrabber::tearDown() { 0120 return callSystem("stop grabber", stopProcess.c_str()); 0121 } 0122 0123 0124 bool CommandLineGrabber::grab() { 0125 if ( !callSystem("grab", prePoll.c_str(), noWarn) ) { 0126 isInitSuccess = false; 0127 return false; 0128 } 0129 return true; 0130 } 0131 0132 0133 std::string CommandLineGrabber::parseCommand(const char * command) { 0134 std::string tmp = command; 0135 int spaceIdx = Util::endOfArgument(command) - command; 0136 std::string commandName = tmp.substr(0, spaceIdx); 0137 std::string path; 0138 if (Util::checkCommand(&path, commandName.c_str())) { 0139 tmp.replace(0, spaceIdx, path); 0140 int index = tmp.find("$IMAGEFILE"); 0141 if (index != -1) { 0142 tmp.replace(index, (int) strlen("$IMAGEFILE"), string(filePath())); 0143 } 0144 return tmp; 0145 } 0146 return ""; 0147 } 0148 0149 bool CommandLineGrabber::isGrabberProcess() { 0150 return !startProcess.empty(); 0151 }