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