author | rubidium |
Tue, 02 Jan 2007 19:19:48 +0000 | |
changeset 5475 | 2e6990a8c7c4 |
parent 5380 | music/extmidi.c@8ea58542b6e0 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 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" |
3721
170325136e34
(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
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
|
10 |
#include "extmidi.h" |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
11 |
#include <fcntl.h> |
0 | 12 |
#include <sys/types.h> |
13 |
#include <sys/wait.h> |
|
14 |
#include <unistd.h> |
|
15 |
#include <signal.h> |
|
16 |
#include <sys/stat.h> |
|
17 |
#include <errno.h> |
|
18 |
||
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
19 |
static struct { |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
20 |
char song[MAX_PATH]; |
2458
dbaf5dffbb28
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2420
diff
changeset
|
21 |
pid_t pid; |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
22 |
} _midi; |
0 | 23 |
|
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
24 |
static void DoPlay(void); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
25 |
static void DoStop(void); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
26 |
|
70a3c1a6576d
(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 | 28 |
{ |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
29 |
_midi.song[0] = '\0'; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
30 |
_midi.pid = -1; |
342 | 31 |
return NULL; |
0 | 32 |
} |
33 |
||
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
34 |
static void ExtMidiStop(void) |
342 | 35 |
{ |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
36 |
_midi.song[0] = '\0'; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
37 |
DoStop(); |
0 | 38 |
} |
39 |
||
1608
70a3c1a6576d
(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 | 41 |
{ |
1608
70a3c1a6576d
(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)); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
43 |
DoStop(); |
0 | 44 |
} |
45 |
||
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
46 |
static void ExtMidiStopSong(void) |
342 | 47 |
{ |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
48 |
_midi.song[0] = '\0'; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
49 |
DoStop(); |
342 | 50 |
} |
51 |
||
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
52 |
static bool ExtMidiIsPlaying(void) |
342 | 53 |
{ |
1608
70a3c1a6576d
(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) |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
55 |
_midi.pid = -1; |
70a3c1a6576d
(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(); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
57 |
return _midi.pid != -1; |
342 | 58 |
} |
59 |
||
1608
70a3c1a6576d
(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 | 61 |
{ |
5380
8ea58542b6e0
(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 | 63 |
} |
64 |
||
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
65 |
static void DoPlay(void) |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
66 |
{ |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
67 |
_midi.pid = fork(); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
68 |
switch (_midi.pid) { |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
69 |
case 0: { |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
70 |
int d; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
71 |
|
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
72 |
close(0); |
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); |
2420
0c1ee5dc7072
(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) { |
0c1ee5dc7072
(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) |
0c1ee5dc7072
(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); |
0c1ee5dc7072
(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 |
0c1ee5dc7072
(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); |
0c1ee5dc7072
(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
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
80 |
} |
2412
7c89a94e1be5
(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
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 |
case -1: |
5380
8ea58542b6e0
(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
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
86 |
/* FALLTHROUGH */ |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
87 |
|
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
88 |
default: |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
89 |
_midi.song[0] = '\0'; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
90 |
break; |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
91 |
} |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
92 |
} |
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 |
static void DoStop(void) |
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 |
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
|
97 |
} |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
98 |
|
0 | 99 |
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
|
100 |
ExtMidiStart, |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
101 |
ExtMidiStop, |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
102 |
ExtMidiPlaySong, |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
103 |
ExtMidiStopSong, |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
104 |
ExtMidiIsPlaying, |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
105 |
ExtMidiSetVolume, |
0 | 106 |
}; |
342 | 107 |
|
0 | 108 |
#endif /* __MORPHOS__ */ |