OpenTTD Source 20250312-master-gcdcc6b491d
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 "../sound/sound_driver.hpp"
14#include "../video/video_driver.hpp"
15#include "../gfx_func.h"
16#include "extmidi.h"
17#include "../base_media_base.h"
18#include "../thread.h"
19#include "midifile.hpp"
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
27#include "../safeguards.h"
28
29#ifndef EXTERNAL_PLAYER
31#define EXTERNAL_PLAYER "timidity"
32#endif
33
36
37std::optional<std::string_view> MusicDriver_ExtMidi::Start(const StringList &parm)
38{
39 if (VideoDriver::GetInstance()->GetName() == "allegro" ||
40 SoundDriver::GetInstance()->GetName() == "allegro") {
41 return "the extmidi driver does not work when Allegro is loaded.";
42 }
43
44 const char *command = GetDriverParam(parm, "cmd");
45#ifndef MIDI_ARG
46 if (StrEmpty(command)) command = EXTERNAL_PLAYER;
47#else
48 if (StrEmpty(command)) command = EXTERNAL_PLAYER " " MIDI_ARG;
49#endif
50
51 this->command_tokens.clear();
52
53 std::string_view view = command;
54 for (;;) {
55 auto pos = view.find(' ');
56 this->command_tokens.emplace_back(view.substr(0, pos));
57
58 if (pos == std::string_view::npos) break;
59 view.remove_prefix(pos + 1);
60 }
61
62 this->song.clear();
63 this->pid = -1;
64 return std::nullopt;
65}
66
68{
69 this->song.clear();
70 this->DoStop();
71}
72
74{
75 std::string filename = MidiFile::GetSMFFile(song);
76 if (!filename.empty()) {
77 this->song = std::move(filename);
78 this->DoStop();
79 }
80}
81
83{
84 this->song.clear();
85 this->DoStop();
86}
87
89{
90 if (this->pid != -1 && waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
91 this->pid = -1;
92 }
93 if (this->pid == -1 && !this->song.empty()) this->DoPlay();
94 return this->pid != -1;
95}
96
98{
99 Debug(driver, 1, "extmidi: set volume not implemented");
100}
101
102void MusicDriver_ExtMidi::DoPlay()
103{
104 this->pid = fork();
105 switch (this->pid) {
106 case 0: {
107 close(0);
108 int d = open("/dev/null", O_RDONLY);
109 if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
110 /* execvp is nasty as it *allows* the passed parameters to be written
111 * for backward compatibility, however we are a fork so do not care. */
112 std::vector<char *> parameters;
113 for (auto &token : this->command_tokens) parameters.emplace_back(token.data());
114 parameters.emplace_back(this->song.data());
115 parameters.emplace_back(nullptr);
116
117 execvp(parameters[0], parameters.data());
118 }
119 _exit(1);
120 }
121
122 case -1:
123 Debug(driver, 0, "extmidi: couldn't fork: {}", strerror(errno));
124 [[fallthrough]];
125
126 default:
127 this->song.clear();
128 break;
129 }
130}
131
138static bool KillWait(pid_t &pid, int signal)
139{
140 /* First try to stop for about a second;
141 * 1 seconds = 1000 milliseconds, 50 ms per cycle => 20 cycles. */
142 for (int i = 0; i < 20; i++) {
143 kill(pid, signal);
144 if (waitpid(pid, nullptr, WNOHANG) == pid) {
145 /* It has shut down, so we are done */
146 pid = -1;
147 return true;
148 }
149 /* Wait 50 milliseconds. */
150 CSleep(50);
151 }
152
153 return false;
154}
155
156void MusicDriver_ExtMidi::DoStop()
157{
158 if (this->pid <= 0) return;
159
160 if (KillWait(this->pid, SIGINT)) return;
161
162 if (KillWait(this->pid, SIGTERM)) return;
163
164 Debug(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
165 /* Gracefully stopping failed. Do it the hard way
166 * and wait till the process finally died. */
167 kill(this->pid, SIGKILL);
168 waitpid(this->pid, nullptr, 0);
169 this->pid = -1;
170}
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition extmidi.cpp:73
void Stop() override
Stop this driver.
Definition extmidi.cpp:67
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition extmidi.cpp:37
std::string_view GetName() const override
Get the name of this driver.
Definition extmidi.h:36
void StopSong() override
Stop playing the current song.
Definition extmidi.cpp:82
bool IsSongPlaying() override
Are we currently playing a song?
Definition extmidi.cpp:88
void SetVolume(uint8_t vol) override
Set the volume, if possible.
Definition extmidi.cpp:97
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition driver.cpp:45
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
Factory for the midi player that uses external players.
Definition extmidi.cpp:35
#define EXTERNAL_PLAYER
The default external midi player.
Definition extmidi.cpp:31
static bool KillWait(pid_t &pid, int signal)
Try to end child process with kill/waitpid for up to 1 second.
Definition extmidi.cpp:138
Base support for playing music via an external application.
bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition string_func.h:57
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Metadata about a music track.
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition thread.h:24