Update readme and changelog for v1.27.0
[openttd-joker.git] / src / music / extmidi.cpp
blob86f664d3cc0d7cd1495a00a7ba54fac6aabf67df
1 /* $Id: extmidi.cpp 22410 2011-05-02 16:14:23Z rubidium $ */
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 "../core/alloc_func.hpp"
16 #include "../sound/sound_driver.hpp"
17 #include "../video/video_driver.hpp"
18 #include "../gfx_func.h"
19 #include "extmidi.h"
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24 #include <signal.h>
25 #include <sys/stat.h>
26 #include <errno.h>
28 #include "../safeguards.h"
30 #ifndef EXTERNAL_PLAYER
31 /** The default external midi player. */
32 #define EXTERNAL_PLAYER "timidity"
33 #endif
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");
46 #ifndef MIDI_ARG
47 if (StrEmpty(command)) command = EXTERNAL_PLAYER;
48 #else
49 if (StrEmpty(command)) command = EXTERNAL_PLAYER " " MIDI_ARG;
50 #endif
52 /* Count number of arguments, but include 3 extra slots: 1st for command, 2nd for song title, and 3rd for terminating NULL. */
53 uint num_args = 3;
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 */
60 uint p = 1;
61 while (true) {
62 this->params[p] = strchr(this->params[p - 1], ' ');
63 if (this->params[p] == NULL) break;
65 this->params[p][0] = '\0';
66 this->params[p]++;
67 p++;
70 /* Last parameter is the song file. */
71 this->params[p] = this->song;
73 this->song[0] = '\0';
74 this->pid = -1;
75 return NULL;
78 void MusicDriver_ExtMidi::Stop()
80 free(params[0]);
81 free(params);
82 this->song[0] = '\0';
83 this->DoStop();
86 void MusicDriver_ExtMidi::PlaySong(const char *filename)
88 strecpy(this->song, filename, lastof(this->song));
89 this->DoStop();
92 void MusicDriver_ExtMidi::StopSong()
94 this->song[0] = '\0';
95 this->DoStop();
98 bool MusicDriver_ExtMidi::IsSongPlaying()
100 if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) {
101 this->pid = -1;
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()
114 this->pid = fork();
115 switch (this->pid) {
116 case 0: {
117 close(0);
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);
122 _exit(1);
125 case -1:
126 DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
127 FALLTHROUGH;
129 default:
130 this->song[0] = '\0';
131 break;
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 */
145 this->pid = -1;
146 return;
148 /* Wait 10 milliseconds. */
149 CSleep(10);
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);
157 this->pid = -1;