extmidi.c
author tron
Mon, 20 Jun 2005 20:09:46 +0000
changeset 1959 c2c3a9850c2e
parent 1892 8717d92b32b1
child 2153 ecfc674410b4
permissions -rw-r--r--
(svn r2465) Remove some unreachable code
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#ifndef __BEOS__
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     2
#ifndef __MORPHOS__
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     4
1892
8717d92b32b1 (svn r2398) - CodeChange: forgot to update unix and os2 specific files as well.
Darkvater
parents: 1608
diff changeset
     5
#include "openttd.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#include "hal.h"
1584
7122e759d56c (svn r2088) - Fix: [ 1155158 ] Make extmidi command a config option in addition to the compile-time MIDI switch. Patch by macbaine.
pasky
parents: 1301
diff changeset
     7
#include "sound.h"
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
     8
#include "string.h"
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
     9
#include <fcntl.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include <sys/types.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include <sys/wait.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include <unistd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include <signal.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <sys/stat.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <errno.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    17
static struct {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    18
	char song[MAX_PATH];
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    19
	int pid;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    20
} _midi;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    22
static void DoPlay(void);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    23
static void DoStop(void);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    24
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    25
static const char* ExtMidiStart(const char* const * parm)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    26
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    27
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    28
	_midi.pid = -1;
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    29
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    32
static void ExtMidiStop(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    33
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    34
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    35
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    38
static void ExtMidiPlaySong(const char* filename)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    39
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    40
	ttd_strlcpy(_midi.song, filename, lengthof(_midi.song));
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    41
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    44
static void ExtMidiStopSong(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    45
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    46
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    47
	DoStop();
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    48
}
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    49
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    50
static bool ExtMidiIsPlaying(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    51
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    52
	if (_midi.pid != -1 && waitpid(_midi.pid, NULL, WNOHANG) == _midi.pid)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    53
		_midi.pid = -1;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    54
	if (_midi.pid == -1 && _midi.song[0] != '\0') DoPlay();
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    55
	return _midi.pid != -1;
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    56
}
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    57
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    58
static void ExtMidiSetVolume(byte vol)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    59
{
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    60
	fprintf(stderr, "extmidi: set volume not implemented\n");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    63
static void DoPlay(void)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    64
{
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    65
	_midi.pid = fork();
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    66
	switch (_midi.pid) {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    67
		case 0: {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    68
			int d;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    69
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    70
			close(0);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    71
			close(1);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    72
			close(2);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    73
			d = open("/dev/null", O_RDONLY);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    74
			if (d != -1) {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    75
				if (dup2(d, 1) != -1 && dup2(d, 2) != -1) {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    76
					#if defined(MIDI_ARG)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    77
						execlp(msf.extmidi, "extmidi", MIDI_ARG, _midi.song, NULL);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    78
					#else
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    79
						execlp(msf.extmidi, "extmidi", _midi.song, NULL);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    80
					#endif
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    81
				}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    82
			}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    83
			exit(1);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    84
		}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    85
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    86
		case -1:
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    87
			fprintf(stderr, "extmidi: couldn't fork: %s\n", strerror(errno));
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    88
			/* FALLTHROUGH */
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    89
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    90
		default:
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    91
			_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    92
			break;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    93
	}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    94
}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    95
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    96
static void DoStop(void)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    97
{
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    98
	if (_midi.pid != -1) kill(_midi.pid, SIGTERM);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    99
}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   100
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
const HalMusicDriver _extmidi_music_driver = {
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   102
	ExtMidiStart,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   103
	ExtMidiStop,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   104
	ExtMidiPlaySong,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   105
	ExtMidiStopSong,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   106
	ExtMidiIsPlaying,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   107
	ExtMidiSetVolume,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
};
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
   109
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
#endif /* __MORPHOS__ */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
#endif /* __BEOS__ */