OpenTTD Source: src/music/extmidi.cpp Source File

OpenTTD Source 14.1

extmidi.cpp
Go to the documentation of this file.
1 /*
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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "../debug.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"
17 #include "extmidi.h"
18 #include "../base_media_base.h"
19 #include "../thread.h"
20 #include "midifile.hpp"
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/stat.h>
27
28 #include "../safeguards.h"
29
30 #ifndef EXTERNAL_PLAYER
31
32 #define EXTERNAL_PLAYER "timidity"
33 #endif
34
36 static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
37
38 const char *MusicDriver_ExtMidi::Start(const StringList &parm)
39 {
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.";
43 }
44
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
51
52 this->command_tokens.clear();
53
54 std::string_view view = command;
55 for (;;) {
56 auto pos = view.find(' ');
57 this->command_tokens.emplace_back(view.substr(0, pos));
58
59 if (pos == std::string_view::npos) break;
60 view.remove_prefix(pos + 1);
61 }
62
63 this->song.clear();
64 this->pid = -1;
65 return nullptr;
66 }
67
68 void MusicDriver_ExtMidi::Stop()
69 {
70 this->song.clear();
71 this->DoStop();
72 }
73
74 void MusicDriver_ExtMidi::PlaySong(const MusicSongInfo &song)
75 {
76 std::string filename = MidiFile::GetSMFFile(song);
77 if (!filename.empty()) {
78 this->song = std::move(filename);
79 this->DoStop();
80 }
81 }
82
83 void MusicDriver_ExtMidi::StopSong()
84 {
85 this->song.clear();
86 this->DoStop();
87 }
88
89 bool MusicDriver_ExtMidi::IsSongPlaying()
90 {
91 if (this->pid != -1 && waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
92 this->pid = -1;
93 }
94 if (this->pid == -1 && !this->song.empty()) this->DoPlay();
95 return this->pid != -1;
96 }
97
98 void MusicDriver_ExtMidi::SetVolume(byte)
99 {
100 Debug(driver, 1, "extmidi: set volume not implemented");
101 }
102
103 void MusicDriver_ExtMidi::DoPlay()
104 {
105 this->pid = fork();
106 switch (this->pid) {
107 case 0: {
108 close(0);
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);
117
118 execvp(parameters[0], parameters.data());
119 }
120 _exit(1);
121 }
122
123 case -1:
124 Debug(driver, 0, "extmidi: couldn't fork: {}", strerror(errno));
125 [[fallthrough]];
126
127 default:
128 this->song.clear();
129 break;
130 }
131 }
132
139 static bool KillWait(pid_t &pid, int signal)
140 {
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++) {
144 kill(pid, signal);
145 if (waitpid(pid, nullptr, WNOHANG) == pid) {
146 /* It has shut down, so we are done */
147 pid = -1;
148 return true;
149 }
150 /* Wait 50 milliseconds. */
151 CSleep(50);
152 }
153
154 return false;
155 }
156
157 void MusicDriver_ExtMidi::DoStop()
158 {
159 if (this->pid <= 0) return;
160
161 if (KillWait(this->pid, SIGINT)) return;
162
163 if (KillWait(this->pid, SIGTERM)) return;
164
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);
170 this->pid = -1;
171 }
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:24
MusicDriver_ExtMidi::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: extmidi.cpp:38
MusicDriver_ExtMidi::SetVolume
void SetVolume(byte vol) override
Set the volume, if possible.
Definition: extmidi.cpp:98
extmidi.h
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:50
MusicDriver_ExtMidi::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
Definition: extmidi.cpp:89
StrEmpty
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:56
KillWait
static bool KillWait(pid_t &pid, int signal)
Try to end child process with kill/waitpid for up to 1 second.
Definition: extmidi.cpp:139
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
MusicDriver_ExtMidi::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition: extmidi.cpp:74
FMusicDriver_ExtMidi
Definition: extmidi.h:39
MusicSongInfo
Metadata about a music track.
Definition: base_media_base.h:325
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
MusicDriver_ExtMidi::StopSong
void StopSong() override
Stop playing the current song.
Definition: extmidi.cpp:83
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:201
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1039
iFMusicDriver_ExtMidi
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
Factory for the midi player that uses external players.
Definition: extmidi.cpp:36
MusicDriver_ExtMidi::Stop
void Stop() override
Stop this driver.
Definition: extmidi.cpp:68
MusicDriver_ExtMidi::GetName
const char * GetName() const override
Get the name of this driver.
Definition: extmidi.h:36
SoundDriver::GetInstance
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
Definition: sound_driver.hpp:35
EXTERNAL_PLAYER
#define EXTERNAL_PLAYER
The default external midi player.
Definition: extmidi.cpp:32