author | peter1138 |
Thu, 16 Feb 2006 20:51:06 +0000 | |
changeset 3027 | 43ffdf4d6a1f |
parent 2458 | c95808cd2ac9 |
child 3721 | 200e79fe4502 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 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" |
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
|
9 |
#include "extmidi.h" |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
10 |
#include <fcntl.h> |
0 | 11 |
#include <sys/types.h> |
12 |
#include <sys/wait.h> |
|
13 |
#include <unistd.h> |
|
14 |
#include <signal.h> |
|
15 |
#include <sys/stat.h> |
|
16 |
#include <errno.h> |
|
17 |
||
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
18 |
static struct { |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
19 |
char song[MAX_PATH]; |
2458
c95808cd2ac9
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2420
diff
changeset
|
20 |
pid_t pid; |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
21 |
} _midi; |
0 | 22 |
|
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
23 |
static void DoPlay(void); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
24 |
static void DoStop(void); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
25 |
|
074dff2e4890
(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 | 27 |
{ |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
28 |
_midi.song[0] = '\0'; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
29 |
_midi.pid = -1; |
342 | 30 |
return NULL; |
0 | 31 |
} |
32 |
||
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
33 |
static void ExtMidiStop(void) |
342 | 34 |
{ |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
35 |
_midi.song[0] = '\0'; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
36 |
DoStop(); |
0 | 37 |
} |
38 |
||
1608
074dff2e4890
(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 | 40 |
{ |
1608
074dff2e4890
(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)); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
42 |
DoStop(); |
0 | 43 |
} |
44 |
||
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
45 |
static void ExtMidiStopSong(void) |
342 | 46 |
{ |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
47 |
_midi.song[0] = '\0'; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
48 |
DoStop(); |
342 | 49 |
} |
50 |
||
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
51 |
static bool ExtMidiIsPlaying(void) |
342 | 52 |
{ |
1608
074dff2e4890
(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) |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
54 |
_midi.pid = -1; |
074dff2e4890
(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(); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
56 |
return _midi.pid != -1; |
342 | 57 |
} |
58 |
||
1608
074dff2e4890
(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 | 60 |
{ |
61 |
fprintf(stderr, "extmidi: set volume not implemented\n"); |
|
0 | 62 |
} |
63 |
||
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
64 |
static void DoPlay(void) |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
65 |
{ |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
66 |
_midi.pid = fork(); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
67 |
switch (_midi.pid) { |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
68 |
case 0: { |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
69 |
int d; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
70 |
|
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
71 |
close(0); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
72 |
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
|
73 |
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
|
74 |
#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
|
75 |
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
|
76 |
#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
|
77 |
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
|
78 |
#endif |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
79 |
} |
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
|
80 |
_exit(1); |
1608
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
81 |
} |
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 |
case -1: |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
84 |
fprintf(stderr, "extmidi: couldn't fork: %s\n", strerror(errno)); |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
85 |
/* FALLTHROUGH */ |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
86 |
|
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
87 |
default: |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
88 |
_midi.song[0] = '\0'; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
89 |
break; |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
90 |
} |
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 |
static void DoStop(void) |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
94 |
{ |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
95 |
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
|
96 |
} |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
97 |
|
0 | 98 |
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
|
99 |
ExtMidiStart, |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
100 |
ExtMidiStop, |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
101 |
ExtMidiPlaySong, |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
102 |
ExtMidiStopSong, |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
103 |
ExtMidiIsPlaying, |
074dff2e4890
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
104 |
ExtMidiSetVolume, |
0 | 105 |
}; |
342 | 106 |
|
0 | 107 |
#endif /* __MORPHOS__ */ |