src/music/extmidi.cpp
author smatz
Sun, 15 Jun 2008 13:52:51 +0000
changeset 10966 3de9939c8a91
parent 10429 1b99254f9607
permissions -rw-r--r--
(svn r13520) -Fix (r13375): pointer has to be valid before it can be loaded to (SLE_STR)
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
10429
1b99254f9607 (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: 8710
diff changeset
     3
/** @file extmidi.cpp Playing music via an external player. */
1b99254f9607 (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: 8710
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#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
     6
#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
     7
#include "../openttd.h"
8653
a83f7a536919 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 7668
diff changeset
     8
#include "../sound_func.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
     9
#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
    10
#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
    11
#include "extmidi.h"
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    12
#include <fcntl.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include <sys/types.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include <sys/wait.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include <unistd.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include <signal.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include <sys/stat.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include <errno.h>
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    20
static FMusicDriver_ExtMidi iFMusicDriver_ExtMidi;
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    21
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    22
const char* MusicDriver_ExtMidi::Start(const char* const * parm)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    23
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    24
	this->song[0] = '\0';
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    25
	this->pid = -1;
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    26
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    29
void MusicDriver_ExtMidi::Stop()
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    30
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    31
	this->song[0] = '\0';
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    32
	this->DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    35
void MusicDriver_ExtMidi::PlaySong(const char* filename)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    36
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    37
	ttd_strlcpy(this->song, filename, lengthof(this->song));
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    38
	this->DoStop();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    41
void MusicDriver_ExtMidi::StopSong()
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    42
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    43
	this->song[0] = '\0';
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    44
	this->DoStop();
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    45
}
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    46
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    47
bool MusicDriver_ExtMidi::IsSongPlaying()
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    48
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    49
	if (this->pid != -1 && waitpid(this->pid, NULL, WNOHANG) == this->pid)
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    50
		this->pid = -1;
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    51
	if (this->pid == -1 && this->song[0] != '\0') this->DoPlay();
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    52
	return this->pid != -1;
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    53
}
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    54
7666
a5fccd76176a (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6573
diff changeset
    55
void MusicDriver_ExtMidi::SetVolume(byte vol)
342
a97783c4a357 (svn r518) Merge extmidi.c r441 to trunk:
tron
parents: 0
diff changeset
    56
{
5568
75f13d7bfaed (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    60
void MusicDriver_ExtMidi::DoPlay()
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    61
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    62
	this->pid = fork();
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    63
	switch (this->pid) {
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    64
		case 0: {
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    65
			int d;
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
			close(0);
074dff2e4890 (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
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
    69
			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
    70
				#if defined(MIDI_ARG)
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    71
					execlp(msf.extmidi, "extmidi", MIDI_ARG, this->song, (char*)0);
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
    72
				#else
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    73
					execlp(msf.extmidi, "extmidi", this->song, (char*)0);
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
				#endif
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    75
			}
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
    76
			_exit(1);
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    77
		}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    78
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    79
		case -1:
5568
75f13d7bfaed (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
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    81
			/* FALLTHROUGH */
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
		default:
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    84
			this->song[0] = '\0';
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    85
			break;
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
}
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    88
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    89
void MusicDriver_ExtMidi::DoStop()
1608
074dff2e4890 (svn r2112) -Fix: ExtMidi no longer halts the game while starting a song
tron
parents: 1584
diff changeset
    90
{
7668
293aec8511ae (svn r10446) -Codechange: Move extmidi's global data into its class.
peter1138
parents: 7666
diff changeset
    91
	if (this->pid != -1) kill(this->pid, SIGTERM);
1608
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
#endif /* __MORPHOS__ */