(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song

-Fix: Redirect stdin/stdout/stderr of the ExtMidi process to /dev/null, to prevent it from writing to the terminal
While here give the ExtMidi functions canonical names
replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
tron 20 years ago
parent 16a64d7000
commit 66989faab6

@ -5,6 +5,8 @@
#include "ttd.h" #include "ttd.h"
#include "hal.h" #include "hal.h"
#include "sound.h" #include "sound.h"
#include "string.h"
#include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
@ -12,89 +14,97 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
static pid_t _pid; static struct {
char song[MAX_PATH];
int pid;
} _midi;
static void extmidi_kill(void) static void DoPlay(void);
static void DoStop(void);
static const char* ExtMidiStart(const char* const * parm)
{ {
if (_pid > 0) { _midi.song[0] = '\0';
kill(_pid, SIGKILL); _midi.pid = -1;
while (waitpid(_pid, NULL, WNOHANG) != _pid); return NULL;
}
_pid = 0;
} }
static const char *extmidi_start(const char * const *parm) static void ExtMidiStop(void)
{ {
_pid = 0; _midi.song[0] = '\0';
return NULL; DoStop();
} }
static void extmidi_stop(void) static void ExtMidiPlaySong(const char* filename)
{ {
extmidi_kill(); ttd_strlcpy(_midi.song, filename, lengthof(_midi.song));
DoStop();
} }
static void extmidi_play_song(const char *filename) static void ExtMidiStopSong(void)
{ {
extmidi_kill(); _midi.song[0] = '\0';
DoStop();
}
_pid = fork(); static bool ExtMidiIsPlaying(void)
if (_pid < 0) { {
fprintf(stderr, "extmidi: couldn't fork: %s\n", strerror(errno)); if (_midi.pid != -1 && waitpid(_midi.pid, NULL, WNOHANG) == _midi.pid)
_pid = 0; _midi.pid = -1;
return; if (_midi.pid == -1 && _midi.song[0] != '\0') DoPlay();
} return _midi.pid != -1;
}
static void ExtMidiSetVolume(byte vol)
{
fprintf(stderr, "extmidi: set volume not implemented\n");
}
if (_pid == 0) { static void DoPlay(void)
{
_midi.pid = fork();
switch (_midi.pid) {
case 0: {
int d;
close(0);
close(1);
close(2);
d = open("/dev/null", O_RDONLY);
if (d != -1) {
if (dup2(d, 1) != -1 && dup2(d, 2) != -1) {
#if defined(MIDI_ARG) #if defined(MIDI_ARG)
execlp(msf.extmidi, "extmidi", MIDI_ARG, filename, NULL); execlp(msf.extmidi, "extmidi", MIDI_ARG, _midi.song, NULL);
#else #else
execlp(msf.extmidi, "extmidi", filename, NULL); execlp(msf.extmidi, "extmidi", _midi.song, NULL);
#endif #endif
fprintf(stderr, "extmidi: couldn't execl: %s\n", strerror(errno));
exit(0);
} }
usleep(500);
if (_pid == waitpid(_pid, NULL, WNOHANG)) {
fprintf(stderr, "extmidi: play song failed\n");
_pid = 0;
usleep(5000);
} }
} exit(1);
}
static void extmidi_stop_song(void)
{
extmidi_kill();
}
static bool extmidi_is_playing(void) case -1:
{ fprintf(stderr, "extmidi: couldn't fork: %s\n", strerror(errno));
if (_pid == 0) /* FALLTHROUGH */
return 0;
if (waitpid(_pid, NULL, WNOHANG) == _pid) { default:
_pid = 0; _midi.song[0] = '\0';
return 0; break;
} }
return 1;
} }
static void extmidi_set_volume(byte vol) static void DoStop(void)
{ {
fprintf(stderr, "extmidi: set volume not implemented\n"); if (_midi.pid != -1) kill(_midi.pid, SIGTERM);
} }
const HalMusicDriver _extmidi_music_driver = { const HalMusicDriver _extmidi_music_driver = {
extmidi_start, ExtMidiStart,
extmidi_stop, ExtMidiStop,
extmidi_play_song, ExtMidiPlaySong,
extmidi_stop_song, ExtMidiStopSong,
extmidi_is_playing, ExtMidiIsPlaying,
extmidi_set_volume, ExtMidiSetVolume,
}; };
#endif /* __MORPHOS__ */ #endif /* __MORPHOS__ */

@ -181,7 +181,7 @@ void MusicLoop(void)
if (_song_is_active == false) if (_song_is_active == false)
return; return;
if (!_music_driver->is_song_playing()) { if (!_music_driver->is_song_playing() && _game_mode != GM_MENU) {
StopMusic(); StopMusic();
SkipToNextSong(); SkipToNextSong();
PlayPlaylistSong(); PlayPlaylistSong();

@ -641,7 +641,6 @@ int ttd_main(int argc, char* argv[])
LoadDriver(SOUND_DRIVER, _ini_sounddriver); LoadDriver(SOUND_DRIVER, _ini_sounddriver);
LoadDriver(MUSIC_DRIVER, _ini_musicdriver); LoadDriver(MUSIC_DRIVER, _ini_musicdriver);
LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads LoadDriver(VIDEO_DRIVER, _ini_videodriver); // load video last, to prevent an empty window while sound and music loads
MusicLoop();
_savegame_sort_order = 1; // default sorting of savegames is by date, newest first _savegame_sort_order = 1; // default sorting of savegames is by date, newest first
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK
@ -1165,7 +1164,6 @@ void GameLoop(void)
InputLoop(); InputLoop();
if (_game_mode != GM_MENU)
MusicLoop(); MusicLoop();
} }

Loading…
Cancel
Save