(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / music / extmidi.cpp
blobf4a35360c09e29d3446c0e2111d9293290d09f8e
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file extmidi.cpp Playing music via an external player. */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../string_func.h"
15 #include "../sound/sound_driver.hpp"
16 #include "../video/video_driver.hpp"
17 #include "../gfx_func.h"
18 #include "extmidi.h"
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #include <sys/stat.h>
25 #include <errno.h>
27 #include "../safeguards.h"
29 #ifndef EXTERNAL_PLAYER
30 /** The default external midi player. */
31 #define EXTERNAL_PLAYER "timidity"
32 #endif
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);
48 this->song[0] = '\0';
49 this->pid = -1;
50 return NULL;
53 void MusicDriver_ExtMidi::Stop()
55 free(command);
56 this->song[0] = '\0';
57 this->DoStop();
60 void MusicDriver_ExtMidi::PlaySong(const char *filename)
62 strecpy(this->song, filename, lastof(this->song));
63 this->DoStop();
66 void MusicDriver_ExtMidi::StopSong()
68 this->song[0] = '\0';
69 this->DoStop();
72 bool MusicDriver_ExtMidi::IsSongPlaying()
74 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
75 this->pid = -1;
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()
88 this->pid = fork();
89 switch (this->pid) {
90 case 0: {
91 close(0);
92 int d = open("/dev/null", O_RDONLY);
93 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
94 #if defined(MIDI_ARG)
95 execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0);
96 #else
97 execlp(this->command, "extmidi", this->song, (char*)0);
98 #endif
100 _exit(1);
103 case -1:
104 DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
105 /* FALL THROUGH */
107 default:
108 this->song[0] = '\0';
109 break;
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 */
123 this->pid = -1;
124 return;
126 /* Wait 10 milliseconds. */
127 CSleep(10);
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);
135 this->pid = -1;