2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#ifndef __MORPHOS__
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../sound.h"
|
|
|
|
#include "../string.h"
|
|
|
|
#include "../variables.h"
|
2006-05-02 19:09:49 +00:00
|
|
|
#include "../debug.h"
|
2005-07-25 07:16:10 +00:00
|
|
|
#include "extmidi.h"
|
2005-03-30 19:52:26 +00:00
|
|
|
#include <fcntl.h>
|
2004-08-09 17:04:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static struct {
|
|
|
|
char song[MAX_PATH];
|
2005-09-25 09:04:59 +00:00
|
|
|
pid_t pid;
|
2005-03-30 19:52:26 +00:00
|
|
|
} _midi;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void DoPlay(void);
|
|
|
|
static void DoStop(void);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static const char* ExtMidiStart(const char* const * parm)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2005-03-30 19:52:26 +00:00
|
|
|
_midi.song[0] = '\0';
|
|
|
|
_midi.pid = -1;
|
2004-11-06 23:24:59 +00:00
|
|
|
return NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void ExtMidiStop(void)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2005-03-30 19:52:26 +00:00
|
|
|
_midi.song[0] = '\0';
|
|
|
|
DoStop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void ExtMidiPlaySong(const char* filename)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2005-03-30 19:52:26 +00:00
|
|
|
ttd_strlcpy(_midi.song, filename, lengthof(_midi.song));
|
|
|
|
DoStop();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void ExtMidiStopSong(void)
|
|
|
|
{
|
|
|
|
_midi.song[0] = '\0';
|
|
|
|
DoStop();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static bool ExtMidiIsPlaying(void)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2005-03-30 19:52:26 +00:00
|
|
|
if (_midi.pid != -1 && waitpid(_midi.pid, NULL, WNOHANG) == _midi.pid)
|
|
|
|
_midi.pid = -1;
|
|
|
|
if (_midi.pid == -1 && _midi.song[0] != '\0') DoPlay();
|
|
|
|
return _midi.pid != -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void ExtMidiSetVolume(byte vol)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2006-05-02 19:09:49 +00:00
|
|
|
DEBUG(driver, 1) ("extmidi: set volume not implemented");
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void DoPlay(void)
|
|
|
|
{
|
|
|
|
_midi.pid = fork();
|
|
|
|
switch (_midi.pid) {
|
|
|
|
case 0: {
|
|
|
|
int d;
|
|
|
|
|
|
|
|
close(0);
|
|
|
|
d = open("/dev/null", O_RDONLY);
|
2005-09-12 09:53:56 +00:00
|
|
|
if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
|
|
|
|
#if defined(MIDI_ARG)
|
|
|
|
execlp(msf.extmidi, "extmidi", MIDI_ARG, _midi.song, (char*)0);
|
|
|
|
#else
|
|
|
|
execlp(msf.extmidi, "extmidi", _midi.song, (char*)0);
|
|
|
|
#endif
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
2005-09-11 09:24:17 +00:00
|
|
|
_exit(1);
|
2005-03-30 19:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case -1:
|
2006-05-02 19:09:49 +00:00
|
|
|
DEBUG(driver, 0) ("extmidi: couldn't fork: %s", strerror(errno));
|
2005-03-30 19:52:26 +00:00
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
_midi.song[0] = '\0';
|
|
|
|
break;
|
2004-11-06 23:24:59 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 19:52:26 +00:00
|
|
|
static void DoStop(void)
|
2004-11-06 23:24:59 +00:00
|
|
|
{
|
2005-03-30 19:52:26 +00:00
|
|
|
if (_midi.pid != -1) kill(_midi.pid, SIGTERM);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const HalMusicDriver _extmidi_music_driver = {
|
2005-03-30 19:52:26 +00:00
|
|
|
ExtMidiStart,
|
|
|
|
ExtMidiStop,
|
|
|
|
ExtMidiPlaySong,
|
|
|
|
ExtMidiStopSong,
|
|
|
|
ExtMidiIsPlaying,
|
|
|
|
ExtMidiSetVolume,
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2004-11-06 23:24:59 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* __MORPHOS__ */
|