4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file extmidi.cpp Playing music via an external player. */
12 #include "../stdafx.h"
14 #include "../string_func.h"
15 #include "../sound/sound_driver.hpp"
16 #include "../video/video_driver.hpp"
17 #include "../gfx_func.h"
20 #include <sys/types.h>
27 #include "../safeguards.h"
29 #ifndef EXTERNAL_PLAYER
30 /** The default external midi player. */
31 #define EXTERNAL_PLAYER "timidity"
34 /** Factory for the midi player that uses external players. */
35 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
;
37 const char *MusicDriver_ExtMidi::Start(const char * const * parm
)
39 if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
40 strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
41 return "the extmidi driver does not work when Allegro is loaded.";
44 const char *command
= GetDriverParam(parm
, "cmd");
45 if (StrEmpty(command
)) command
= EXTERNAL_PLAYER
;
47 this->command
= stredup(command
);
53 void MusicDriver_ExtMidi::Stop()
60 void MusicDriver_ExtMidi::PlaySong(const char *filename
)
62 strecpy(this->song
, filename
, lastof(this->song
));
66 void MusicDriver_ExtMidi::StopSong()
72 bool MusicDriver_ExtMidi::IsSongPlaying()
74 if (this->pid
!= -1 && waitpid(this->pid
, NULL
, WNOHANG
) == this->pid
) {
77 if (this->pid
== -1 && this->song
[0] != '\0') this->DoPlay();
78 return this->pid
!= -1;
81 void MusicDriver_ExtMidi::SetVolume(byte vol
)
83 DEBUG(driver
, 1, "extmidi: set volume not implemented");
86 void MusicDriver_ExtMidi::DoPlay()
92 int d
= open("/dev/null", O_RDONLY
);
93 if (d
!= -1 && dup2(d
, 1) != -1 && dup2(d
, 2) != -1) {
95 execlp(this->command
, "extmidi", MIDI_ARG
, this->song
, (char*)0);
97 execlp(this->command
, "extmidi", this->song
, (char*)0);
104 DEBUG(driver
, 0, "extmidi: couldn't fork: %s", strerror(errno
));
108 this->song
[0] = '\0';
113 void MusicDriver_ExtMidi::DoStop()
115 if (this->pid
<= 0) return;
117 /* First try to gracefully stop for about five seconds;
118 * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
119 for (int i
= 0; i
< 500; i
++) {
120 kill(this->pid
, SIGTERM
);
121 if (waitpid(this->pid
, NULL
, WNOHANG
) == this->pid
) {
122 /* It has shut down, so we are done */
126 /* Wait 10 milliseconds. */
130 DEBUG(driver
, 0, "extmidi: gracefully stopping failed, trying the hard way");
131 /* Gracefully stopping failed. Do it the hard way
132 * and wait till the process finally died. */
133 kill(this->pid
, SIGKILL
);
134 waitpid(this->pid
, NULL
, 0);