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