2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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 /** @file extmidi.cpp Playing music via an external player. */
10 #include "../stdafx.h"
12 #include "../string_func.h"
13 #include "../core/alloc_func.hpp"
14 #include "../sound/sound_driver.hpp"
15 #include "../video/video_driver.hpp"
16 #include "../gfx_func.h"
18 #include "../base_media_base.h"
19 #include "../thread.h"
20 #include "midifile.hpp"
22 #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 std::optional
<std::string_view
> MusicDriver_ExtMidi::Start(const StringList
&parm
)
40 if (VideoDriver::GetInstance()->GetName() == "allegro" ||
41 SoundDriver::GetInstance()->GetName() == "allegro") {
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 this->command_tokens
.clear();
54 std::string_view view
= command
;
56 auto pos
= view
.find(' ');
57 this->command_tokens
.emplace_back(view
.substr(0, pos
));
59 if (pos
== std::string_view::npos
) break;
60 view
.remove_prefix(pos
+ 1);
68 void MusicDriver_ExtMidi::Stop()
74 void MusicDriver_ExtMidi::PlaySong(const MusicSongInfo
&song
)
76 std::string filename
= MidiFile::GetSMFFile(song
);
77 if (!filename
.empty()) {
78 this->song
= std::move(filename
);
83 void MusicDriver_ExtMidi::StopSong()
89 bool MusicDriver_ExtMidi::IsSongPlaying()
91 if (this->pid
!= -1 && waitpid(this->pid
, nullptr, WNOHANG
) == this->pid
) {
94 if (this->pid
== -1 && !this->song
.empty()) this->DoPlay();
95 return this->pid
!= -1;
98 void MusicDriver_ExtMidi::SetVolume(uint8_t)
100 Debug(driver
, 1, "extmidi: set volume not implemented");
103 void MusicDriver_ExtMidi::DoPlay()
109 int d
= open("/dev/null", O_RDONLY
);
110 if (d
!= -1 && dup2(d
, 1) != -1 && dup2(d
, 2) != -1) {
111 /* execvp is nasty as it *allows* the passed parameters to be written
112 * for backward compatibility, however we are a fork so do not care. */
113 std::vector
<char *> parameters
;
114 for (auto &token
: this->command_tokens
) parameters
.emplace_back(token
.data());
115 parameters
.emplace_back(this->song
.data());
116 parameters
.emplace_back(nullptr);
118 execvp(parameters
[0], parameters
.data());
124 Debug(driver
, 0, "extmidi: couldn't fork: {}", strerror(errno
));
134 * Try to end child process with kill/waitpid for up to 1 second.
135 * @param pid The process ID to end.
136 * @param signal The signal type to send.
137 * @return True if the process has been ended.
139 static bool KillWait(pid_t
&pid
, int signal
)
141 /* First try to stop for about a second;
142 * 1 seconds = 1000 milliseconds, 50 ms per cycle => 20 cycles. */
143 for (int i
= 0; i
< 20; i
++) {
145 if (waitpid(pid
, nullptr, WNOHANG
) == pid
) {
146 /* It has shut down, so we are done */
150 /* Wait 50 milliseconds. */
157 void MusicDriver_ExtMidi::DoStop()
159 if (this->pid
<= 0) return;
161 if (KillWait(this->pid
, SIGINT
)) return;
163 if (KillWait(this->pid
, SIGTERM
)) return;
165 Debug(driver
, 0, "extmidi: gracefully stopping failed, trying the hard way");
166 /* Gracefully stopping failed. Do it the hard way
167 * and wait till the process finally died. */
168 kill(this->pid
, SIGKILL
);
169 waitpid(this->pid
, nullptr, 0);