OpenTTD Source  12.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 "../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 #include <errno.h>
28 
29 #include "../safeguards.h"
30 
31 #ifndef EXTERNAL_PLAYER
32 
33 #define EXTERNAL_PLAYER "timidity"
34 #endif
35 
38 
39 const char *MusicDriver_ExtMidi::Start(const StringList &parm)
40 {
41  if (strcmp(VideoDriver::GetInstance()->GetName(), "allegro") == 0 ||
42  strcmp(SoundDriver::GetInstance()->GetName(), "allegro") == 0) {
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  /* Count number of arguments, but include 3 extra slots: 1st for command, 2nd for song title, and 3rd for terminating nullptr. */
54  uint num_args = 3;
55  for (const char *t = command; *t != '\0'; t++) if (*t == ' ') num_args++;
56 
57  this->params = CallocT<char *>(num_args);
58  this->params[0] = stredup(command);
59 
60  /* Replace space with \0 and add next arg to params */
61  uint p = 1;
62  while (true) {
63  this->params[p] = strchr(this->params[p - 1], ' ');
64  if (this->params[p] == nullptr) break;
65 
66  this->params[p][0] = '\0';
67  this->params[p]++;
68  p++;
69  }
70 
71  /* Last parameter is the song file. */
72  this->params[p] = this->song;
73 
74  this->song[0] = '\0';
75  this->pid = -1;
76  return nullptr;
77 }
78 
80 {
81  free(params[0]);
82  free(params);
83  this->song[0] = '\0';
84  this->DoStop();
85 }
86 
88 {
89  std::string filename = MidiFile::GetSMFFile(song);
90  if (!filename.empty()) {
91  strecpy(this->song, filename.c_str(), lastof(this->song));
92  this->DoStop();
93  }
94 }
95 
97 {
98  this->song[0] = '\0';
99  this->DoStop();
100 }
101 
103 {
104  if (this->pid != -1 && waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
105  this->pid = -1;
106  }
107  if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
108  return this->pid != -1;
109 }
110 
112 {
113  Debug(driver, 1, "extmidi: set volume not implemented");
114 }
115 
116 void MusicDriver_ExtMidi::DoPlay()
117 {
118  this->pid = fork();
119  switch (this->pid) {
120  case 0: {
121  close(0);
122  int d = open("/dev/null", O_RDONLY);
123  if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
124  execvp(this->params[0], this->params);
125  }
126  _exit(1);
127  }
128 
129  case -1:
130  Debug(driver, 0, "extmidi: couldn't fork: {}", strerror(errno));
131  FALLTHROUGH;
132 
133  default:
134  this->song[0] = '\0';
135  break;
136  }
137 }
138 
139 void MusicDriver_ExtMidi::DoStop()
140 {
141  if (this->pid <= 0) return;
142 
143  /* First try to gracefully stop for about five seconds;
144  * 5 seconds = 5000 milliseconds, 10 ms per cycle => 500 cycles. */
145  for (int i = 0; i < 500; i++) {
146  kill(this->pid, SIGTERM);
147  if (waitpid(this->pid, nullptr, WNOHANG) == this->pid) {
148  /* It has shut down, so we are done */
149  this->pid = -1;
150  return;
151  }
152  /* Wait 10 milliseconds. */
153  CSleep(10);
154  }
155 
156  Debug(driver, 0, "extmidi: gracefully stopping failed, trying the hard way");
157  /* Gracefully stopping failed. Do it the hard way
158  * and wait till the process finally died. */
159  kill(this->pid, SIGKILL);
160  waitpid(this->pid, nullptr, 0);
161  this->pid = -1;
162 }
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:23
MusicDriver_ExtMidi::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: extmidi.cpp:39
MusicDriver_ExtMidi::SetVolume
void SetVolume(byte vol) override
Set the volume, if possible.
Definition: extmidi.cpp:111
extmidi.h
GetDriverParam
const char * GetDriverParam(const StringList &parm, const char *name)
Get a string parameter the list of parameters.
Definition: driver.cpp:41
MusicDriver_ExtMidi::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
Definition: extmidi.cpp:102
MusicDriver_ExtMidi::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Definition: extmidi.cpp:87
FMusicDriver_ExtMidi
Definition: extmidi.h:39
MusicSongInfo
Metadata about a music track.
Definition: base_media_base.h:291
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:64
MusicDriver_ExtMidi::StopSong
void StopSong() override
Stop playing the current song.
Definition: extmidi.cpp:96
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:199
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1047
iFMusicDriver_ExtMidi
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi
Factory for the midi player that uses external players.
Definition: extmidi.cpp:37
MusicDriver_ExtMidi::Stop
void Stop() override
Stop this driver.
Definition: extmidi.cpp:79
MusicDriver_ExtMidi::GetName
const char * GetName() const override
Get the name of this driver.
Definition: extmidi.h:36
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
SoundDriver::GetInstance
static SoundDriver * GetInstance()
Get the currently active instance of the sound driver.
Definition: sound_driver.hpp:35
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
EXTERNAL_PLAYER
#define EXTERNAL_PLAYER
The default external midi player.
Definition: extmidi.cpp:33
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394