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