summaryrefslogtreecommitdiff
path: root/extmidi.c
blob: a03d4cf48f2bc47a4ffa793b60903554c6d0af98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __BEOS__
#ifndef __MORPHOS__
#include "stdafx.h"

#include "ttd.h"
#include "hal.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <sys/stat.h>
#include <errno.h>

#ifndef EXTERNAL_PLAYER
#define EXTERNAL_PLAYER "timidity"
#endif

static pid_t _pid;

static void extmidi_kill(void)
{
	if (_pid > 0) {
		kill(_pid, SIGKILL);
		while (waitpid(_pid, NULL, WNOHANG) != _pid);
	}
	_pid = 0;
}

static const char *extmidi_start(const char * const *parm)
{
	_pid = 0;
	return NULL;
}

static void extmidi_stop(void)
{
	extmidi_kill();
}

static void extmidi_play_song(const char *filename)
{
	extmidi_kill();

	_pid = fork();
	if (_pid < 0) {
		fprintf(stderr, "extmidi: couldn't fork: %s\n", strerror(errno));
		_pid = 0;
		return;
	}

	if (_pid == 0) {
		#if defined(MIDI_ARG)
			execlp(EXTERNAL_PLAYER, "extmidi", MIDI_ARG, filename, NULL);
		#else
			execlp(EXTERNAL_PLAYER, "extmidi", filename, NULL);
		#endif
		fprintf(stderr, "extmidi: couldn't execl: %s\n", strerror(errno));
		exit(0);
	}

	usleep(500);

	if (_pid == waitpid(_pid, NULL, WNOHANG)) {
		fprintf(stderr, "extmidi: play song failed\n");
		_pid = 0;

		usleep(5000);
	}
}

static void extmidi_stop_song(void)
{
	extmidi_kill();
}

static bool extmidi_is_playing(void)
{
	if (_pid == 0)
		return 0;

	if (waitpid(_pid, NULL, WNOHANG) == _pid) {
		_pid = 0;
		return 0;
	}

	return 1;
}

static void extmidi_set_volume(byte vol)
{
	fprintf(stderr, "extmidi: set volume not implemented\n");
}

const HalMusicDriver _extmidi_music_driver = {
	extmidi_start,
	extmidi_stop,
	extmidi_play_song,
	extmidi_stop_song,
	extmidi_is_playing,
	extmidi_set_volume,
};

#endif /* __MORPHOS__ */
#endif /* __BEOS__ */