author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9111 | 48ce04029fe4 |
child 10310 | ca2eb5811a07 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8214
diff
changeset
|
3 |
/** @file extmidi.cpp Playing music via an external player. */ |
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8214
diff
changeset
|
4 |
|
0 | 5 |
#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
|
6 |
#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
|
7 |
#include "../openttd.h" |
8157
1505def01945
(svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents:
7172
diff
changeset
|
8 |
#include "../sound_func.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
|
9 |
#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
|
10 |
#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
|
11 |
#include "extmidi.h" |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
12 |
#include <fcntl.h> |
0 | 13 |
#include <sys/types.h> |
14 |
#include <sys/wait.h> |
|
15 |
#include <unistd.h> |
|
16 |
#include <signal.h> |
|
17 |
#include <sys/stat.h> |
|
18 |
#include <errno.h> |
|
19 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
20 |
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi; |
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
21 |
|
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
22 |
const char* MusicDriver_ExtMidi::Start(const char* const * parm) |
342 | 23 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
24 |
this->song[0] = '\0'; |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
25 |
this->pid = -1; |
342 | 26 |
return NULL; |
0 | 27 |
} |
28 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
29 |
void MusicDriver_ExtMidi::Stop() |
342 | 30 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
31 |
this->song[0] = '\0'; |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
32 |
this->DoStop(); |
0 | 33 |
} |
34 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
35 |
void MusicDriver_ExtMidi::PlaySong(const char* filename) |
342 | 36 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
37 |
ttd_strlcpy(this->song, filename, lengthof(this->song)); |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
38 |
this->DoStop(); |
0 | 39 |
} |
40 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
41 |
void MusicDriver_ExtMidi::StopSong() |
342 | 42 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
43 |
this->song[0] = '\0'; |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
44 |
this->DoStop(); |
342 | 45 |
} |
46 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
47 |
bool MusicDriver_ExtMidi::IsSongPlaying() |
342 | 48 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
49 |
if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid) |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
50 |
this->pid = -1; |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
51 |
if (this->pid == -1 && this->song[0] != '\0') this->DoPlay(); |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
52 |
return this->pid != -1; |
342 | 53 |
} |
54 |
||
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
6247
diff
changeset
|
55 |
void MusicDriver_ExtMidi::SetVolume(byte vol) |
342 | 56 |
{ |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
3721
diff
changeset
|
57 |
DEBUG(driver, 1, "extmidi: set volume not implemented"); |
0 | 58 |
} |
59 |
||
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
60 |
void MusicDriver_ExtMidi::DoPlay() |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
61 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
62 |
this->pid = fork(); |
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
63 |
switch (this->pid) { |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
64 |
case 0: { |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
65 |
int d; |
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 |
close(0); |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
68 |
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
|
69 |
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
|
70 |
#if defined(MIDI_ARG) |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
71 |
execlp(msf.extmidi, "extmidi", MIDI_ARG, this->song, (char*)0); |
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
|
72 |
#else |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
73 |
execlp(msf.extmidi, "extmidi", this->song, (char*)0); |
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 |
#endif |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
75 |
} |
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
|
76 |
_exit(1); |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
77 |
} |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
78 |
|
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
79 |
case -1: |
5380
8ea58542b6e0
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
3721
diff
changeset
|
80 |
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
|
81 |
/* FALLTHROUGH */ |
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 |
default: |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
84 |
this->song[0] = '\0'; |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
85 |
break; |
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 |
} |
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
88 |
|
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
89 |
void MusicDriver_ExtMidi::DoStop() |
1608
70a3c1a6576d
(svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents:
1584
diff
changeset
|
90 |
{ |
7172
ca55dbe776a3
(svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents:
7170
diff
changeset
|
91 |
if (this->pid != -1) kill(this->pid, SIGTERM); |
1608
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 |
|
0 | 94 |
#endif /* __MORPHOS__ */ |