Skip to content

Commit

Permalink
can now run external commands without spawning a window on win32
Browse files Browse the repository at this point in the history
git-svn-id: file:///home/behr_mi/git/sumo_synched/trunk@15974 afbd958f-9f77-42d5-a016-97a22340ccf4
  • Loading branch information
namdre committed Mar 19, 2014
1 parent 2d1b1d7 commit 8abeacf
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
4 changes: 1 addition & 3 deletions sumo/src/gui/GUIApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,9 +1201,7 @@ GUIApplicationWindow::checkGamingEvents() {
const std::string cmd = myJamSounds.get(&myGamingRNG);
if (cmd != "")
// yay! fun with dangerous commands... Never use this over the internet
system(cmd.c_str());
//system("play /home/erdm_ja/Downloads/vehicle040.wav &"); // linux
//system("start /min vlc -Idummy D:\\erdm_ja\\Downloads\\vehicle040.wav &"); // windows
SysUtils::runHiddenCommand(cmd);
}
// one sound per simulation step is enough
break;
Expand Down
37 changes: 37 additions & 0 deletions sumo/src/utils/common/SysUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,43 @@ SysUtils::getWindowsTicks() {
#endif


unsigned long
SysUtils::runHiddenCommand(const std::string& cmd) {
#ifndef WIN32
return (unsigned long)system(cmd.c_str());
#else
// code inspired by http://www.codeproject.com/Articles/2537/Running-console-applications-silently
STARTUPINFO StartupInfo;
PROCESS_INFORMATION ProcessInfo;
unsigned long rc;

memset(&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_HIDE;

// "/c" option - Do the command then terminate the command window
std::string winCmd = "CMD.exe /c " + cmd;
char* args = new char[winCmd.size()];
args[0] = 0;
strcpy(args, winCmd.c_str());
if (!CreateProcess( NULL, args, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo)) {
delete args;
return (unsigned long)GetLastError();
}

WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc))
rc = 0;

CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);

delete args;
return rc;
#endif
}

/****************************************************************************/

5 changes: 4 additions & 1 deletion sumo/src/utils/common/SysUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <config.h>
#endif

#include <string>

// ===========================================================================
// class definitions
Expand All @@ -54,8 +55,10 @@ class SysUtils {
static long getWindowsTicks();
#endif

};

/// @brief run a shell command without popping up any windows (particuarly on win32)
static unsigned long runHiddenCommand(const std::string& cmd);
};

#endif

Expand Down
2 changes: 1 addition & 1 deletion sumo/tools/game/cross/cross.rou.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/routes_file.xsd">
<routes>
<vTypeDistribution id="DEFAULT_VEHTYPE">
<vType id="passenger" length="5.0" minGap="2.5" guiShape="passenger" osgFile="vw_golf.3ds" probability="20" />
<vType id="bus" length="12.5" minGap="2.5" guiShape="bus" osgFile="tour_bus.3ds" width="2.4" probability="2"/>
Expand Down
6 changes: 3 additions & 3 deletions sumo/tools/game/cross/cross.settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
<legend showSizeLegend="1"/>
</scheme>

<event id="jam" commandPosix="play sounds/car_horn1.wav &#x26;" commandWindows="start /min vlc -Idummy sounds/car_horn1.wav &#x26;" probability="0.1"/>
<event id="jam" commandPosix="play sounds/car_horn2.wav &#x26;" commandWindows="start /min vlc -Idummy sounds/car_horn2.wav &#x26;" probability="0.1"/>
<event id="jam" commandPosix="play sounds/car_horn3.wav &#x26;" commandWindows="start /min vlc -Idummy sounds/car_horn3.wav &#x26;" probability="0.1"/>
<event id="jam" commandPosix="play sounds/car_horn1.wav &#x26;" commandWindows="start /min vlc.exe -Idummy --play-and-exit sounds/car_horn1.wav" probability="0.1"/>
<event id="jam" commandPosix="play sounds/car_horn2.wav &#x26;" commandWindows="start /min vlc.exe -Idummy --play-and-exit sounds/car_horn2.wav" probability="0.1"/>
<event id="jam" commandPosix="play sounds/car_horn3.wav &#x26;" commandWindows="start /min vlc.exe -Idummy --play-and-exit sounds/car_horn3.wav" probability="0.1"/>
<jamTime value="40"/>

</viewsettings>
2 changes: 1 addition & 1 deletion sumo/tools/game/square/square.rou.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo.sf.net/xsd/routes_file.xsd">
<routes>
<routeDistribution id="horizontal">
<route edges="left0to0/0 0/0to1/0 1/0toright0"/>
<route edges="left1to0/1 0/1to1/1 1/1toright1"/>
Expand Down

0 comments on commit 8abeacf

Please sign in to comment.