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