1 /* $Id: extmidi.cpp 22410 2011-05-02 16:14:23Z rubidium $ */
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 "../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
31 /** The default external midi player. */
32 #define EXTERNAL_PLAYER "timidity"
35 /** Factory for the midi player that uses external players. */
36 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
;
38 const char *MusicDriver_ExtMidi::Start(const char * const * parm
)
40 if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
41 strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
42 return "the extmidi driver does not work when Allegro is loaded.";
45 const char *command
= GetDriverParam(parm
, "cmd");
47 if (StrEmpty(command
)) command
= EXTERNAL_PLAYER
;
49 if (StrEmpty(command
)) command
= EXTERNAL_PLAYER
" " MIDI_ARG
;
52 /* Count number of arguments, but include 3 extra slots: 1st for command, 2nd for song title, and 3rd for terminating NULL. */
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
);
59 /* Replace space with \0 and add next arg to params */
62 this->params
[p
] = strchr(this->params
[p
- 1], ' ');
63 if (this->params
[p
] == NULL
) break;
65 this->params
[p
][0] = '\0';
70 /* Last parameter is the song file. */
71 this->params
[p
] = this->song
;
78 void MusicDriver_ExtMidi::Stop()
86 void MusicDriver_ExtMidi::PlaySong(const char *filename
)
88 strecpy(this->song
, filename
, lastof(this->song
));
92 void MusicDriver_ExtMidi::StopSong()
98 bool MusicDriver_ExtMidi::IsSongPlaying()
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;
107 void MusicDriver_ExtMidi::SetVolume(byte vol
)
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;
139 /* First try to gracefully stop for about five seconds;
140 * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
141 for (int i
= 0; i
< 500; i
++) {
142 kill(this->pid
, SIGTERM
);
143 if (waitpid(this->pid
, NULL
, WNOHANG
) == this->pid
) {
144 /* It has shut down, so we are done */
148 /* Wait 10 milliseconds. */
152 DEBUG(driver
, 0, "extmidi: gracefully stopping failed, trying the hard way");
153 /* Gracefully stopping failed. Do it the hard way
154 * and wait till the process finally died. */
155 kill(this->pid
, SIGKILL
);
156 waitpid(this->pid
, NULL
, 0);