12 #include "../stdafx.h"
14 #include "../string_func.h"
15 #include "../core/alloc_func.hpp"
16 #include "../sound/sound_driver.hpp"
17 #include "../video/video_driver.hpp"
18 #include "../gfx_func.h"
21 #include <sys/types.h>
28 #include "../safeguards.h"
30 #ifndef EXTERNAL_PLAYER
32 #define EXTERNAL_PLAYER "timidity"
42 return "the extmidi driver does not work when Allegro is loaded.";
54 for (
const char *t = command; *t !=
'\0'; t++)
if (*t ==
' ') num_args++;
56 this->params = CallocT<char *>(num_args);
57 this->params[0] =
stredup(command);
62 this->params[p] = strchr(this->params[p - 1],
' ');
63 if (this->params[p] == NULL)
break;
65 this->params[p][0] =
'\0';
71 this->params[p] = this->song;
100 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
103 if (this->pid == -1 && this->song[0] !=
'\0') this->DoPlay();
104 return this->pid != -1;
109 DEBUG(driver, 1,
"extmidi: set volume not implemented");
112 void MusicDriver_ExtMidi::DoPlay()
118 int d = open(
"/dev/null", O_RDONLY);
119 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
120 execvp(this->params[0], this->params);
126 DEBUG(driver, 0,
"extmidi: couldn't fork: %s", strerror(errno));
130 this->song[0] =
'\0';
135 void MusicDriver_ExtMidi::DoStop()
137 if (this->pid <= 0)
return;
141 for (
int i = 0; i < 500; i++) {
142 kill(this->pid, SIGTERM);
143 if (waitpid(this->pid, NULL, WNOHANG) == this->pid) {
152 DEBUG(driver, 0,
"extmidi: gracefully stopping failed, trying the hard way");
155 kill(this->pid, SIGKILL);
156 waitpid(this->pid, NULL, 0);