music/extmidi.c
author KUDr
Sun, 31 Dec 2006 02:53:23 +0000
branchcustombridgeheads
changeset 5611 11da6bafbfb9
parent 5568 75f13d7bfaed
permissions -rw-r--r--
(svn r7687) [cbh] - Fix: trains can now enter the bridge from side. They still can't leave it from side (pathfinder will need to be invoked when the other ramp is entered). Also the code is not very clear and needs review. It is more proof of concept than final solution. I hope that somebody smarter (Celestar) can do it better.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2171
diff changeset
     1
/* $Id$ */
461a2aff3486 (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
d240b9097139 (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"
d240b9097139 (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"
d240b9097139 (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"
d240b9097139 (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"
d240b9097139 (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"
3721
200e79fe4502 (svn r4692) - Use DEBUG() instead of direct fprintf in extmidi music driver, and silence 'set volume not implemented' warning at the default debug level.
peter1138
parents: 2458
diff changeset
     9
#include "../debug.h"
2189
d240b9097139 (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
    10
#include "extmidi.h"
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    11
#include <fcntl.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include <sys/types.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include <sys/wait.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <unistd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <signal.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include <sys/stat.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include <errno.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    19
static struct {
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    20
	char song[MAX_PATH];
2458
c95808cd2ac9 (svn r2984) Use adequate types, this should aid portability a bit
tron
parents: 2420
diff changeset
    21
	pid_t pid;
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    22
} _midi;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    24
static void DoPlay(void);
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    25
static void DoStop(void);
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    26
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    27
static const char* ExtMidiStart(const char* const * parm)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    28
{
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    29
	_midi.song[0] = '\0';
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    30
	_midi.pid = -1;
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    31
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    34
static void ExtMidiStop(void)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    35
{
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    36
	_midi.song[0] = '\0';
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    37
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    40
static void ExtMidiPlaySong(const char* filename)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    41
{
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    42
	ttd_strlcpy(_midi.song, filename, lengthof(_midi.song));
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    43
	DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    46
static void ExtMidiStopSong(void)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    47
{
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    48
	_midi.song[0] = '\0';
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    49
	DoStop();
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    50
}
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    51
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    52
static bool ExtMidiIsPlaying(void)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    53
{
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    54
	if (_midi.pid != -1 && waitpid(_midi.pid, NULL, WNOHANG) == _midi.pid)
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    55
		_midi.pid = -1;
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    56
	if (_midi.pid == -1 && _midi.song[0] != '\0') DoPlay();
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    57
	return _midi.pid != -1;
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    58
}
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    59
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    60
static void ExtMidiSetVolume(byte vol)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    61
{
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 3721
diff changeset
    62
	DEBUG(driver, 1, "extmidi: set volume not implemented");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    65
static void DoPlay(void)
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    66
{
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    67
	_midi.pid = fork();
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    68
	switch (_midi.pid) {
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    69
		case 0: {
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    70
			int d;
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    71
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    72
			close(0);
074dff2e4890 (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);
2420
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    74
			if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) {
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    75
				#if defined(MIDI_ARG)
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    76
					execlp(msf.extmidi, "extmidi", MIDI_ARG, _midi.song, (char*)0);
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    77
				#else
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    78
					execlp(msf.extmidi, "extmidi", _midi.song, (char*)0);
e885ba7ba1e2 (svn r2946) Remove redundant calls, simplify a check and terminate the argument list of execlp() as suggested by the manpage to make it correctly work on 64bit platforms
tron
parents: 2412
diff changeset
    79
				#endif
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    80
			}
2412
67e06432921e (svn r2938) -Fix: Exit the child of the extmidi backend with _exit() instead of exit(), because we don't want any atexit handlers - especially flushing output streams - to run, if exec() fails
tron
parents: 2303
diff changeset
    81
			_exit(1);
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    82
		}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    83
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    84
		case -1:
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 3721
diff changeset
    85
			DEBUG(driver, 0, "extmidi: couldn't fork: %s", strerror(errno));
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    86
			/* FALLTHROUGH */
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    87
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    88
		default:
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    89
			_midi.song[0] = '\0';
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    90
			break;
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    91
	}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    92
}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    93
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    94
static void DoStop(void)
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    95
{
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    96
	if (_midi.pid != -1) kill(_midi.pid, SIGTERM);
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    97
}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    98
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
const HalMusicDriver _extmidi_music_driver = {
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   100
	ExtMidiStart,
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   101
	ExtMidiStop,
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   102
	ExtMidiPlaySong,
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   103
	ExtMidiStopSong,
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   104
	ExtMidiIsPlaying,
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
   105
	ExtMidiSetVolume,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
};
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
   107
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
#endif /* __MORPHOS__ */