music/extmidi.c
author tron
Sun, 21 Aug 2005 17:32:43 +0000
changeset 2358 f6d863c533f2
parent 2303 559e782ebc25
child 2412 7c89a94e1be5
permissions -rw-r--r--
(svn r2884) Fix and improve the look of the new letters (Hadez)
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2171
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2171
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#ifndef __MORPHOS__
2189
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     4
#include "../stdafx.h"
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     5
#include "../openttd.h"
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     6
#include "../sound.h"
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     7
#include "../string.h"
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     8
#include "../variables.h"
5cdc11ffeaa4 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     9
#include "extmidi.h"
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    10
#include <fcntl.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include <sys/types.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include <sys/wait.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include <unistd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <signal.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <sys/stat.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include <errno.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    18
static struct {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    19
	char song[MAX_PATH];
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    20
	int pid;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    21
} _midi;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    23
static void DoPlay(void);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    24
static void DoStop(void);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    25
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    26
static const char* ExtMidiStart(const char* const * parm)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    27
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    28
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    29
	_midi.pid = -1;
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    30
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    33
static void ExtMidiStop(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    34
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    35
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    36
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    39
static void ExtMidiPlaySong(const char* filename)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    40
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    41
	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
    42
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    45
static void ExtMidiStopSong(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    46
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    47
	_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    48
	DoStop();
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    49
}
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    50
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    51
static bool ExtMidiIsPlaying(void)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    52
{
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    53
	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
    54
		_midi.pid = -1;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    55
	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
    56
	return _midi.pid != -1;
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    57
}
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    58
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    59
static void ExtMidiSetVolume(byte vol)
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    60
{
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    61
	fprintf(stderr, "extmidi: set volume not implemented\n");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
1608
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    64
static void DoPlay(void)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    65
{
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    66
	_midi.pid = fork();
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    67
	switch (_midi.pid) {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    68
		case 0: {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    69
			int d;
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    70
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    71
			close(0);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    72
			close(1);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    73
			close(2);
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    74
			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
    75
			if (d != -1) {
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    76
				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
    77
					#if defined(MIDI_ARG)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    78
						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
    79
					#else
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    80
						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
    81
					#endif
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
			}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    84
			exit(1);
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
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    87
		case -1:
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    88
			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
    89
			/* FALLTHROUGH */
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    90
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    91
		default:
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    92
			_midi.song[0] = '\0';
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    93
			break;
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
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    97
static void DoStop(void)
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    98
{
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    99
	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
   100
}
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   101
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
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
   103
	ExtMidiStart,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   104
	ExtMidiStop,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   105
	ExtMidiPlaySong,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   106
	ExtMidiStopSong,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   107
	ExtMidiIsPlaying,
70a3c1a6576d (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   108
	ExtMidiSetVolume,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
};
342
c16e965ab592 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
   110
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
#endif /* __MORPHOS__ */