summaryrefslogtreecommitdiffstats
path: root/player.c
blob: c5724ab7115bce2acba502dfd5ac5e1890fa3211 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
#include "player.h"

#include "portaudio.h"
#include "sndfile.h"

#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>

static struct player player_static;
struct player *player;

struct player *
player_thread()
{
	struct player *player;
	pid_t pid;

	player = mmap(&player_static, sizeof(struct player),
		PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);
	player->action = PLAYER_NONE;
	player->resp = PLAYER_NOTSET;
	player->alive = 1;

	pid = fork();
	if (!pid) {
		player_main();
		player->alive = 0;
		exit(0);
	}

	player->pid = pid;

	return player;
}

void
player_main(void)
{
	PaStream *stream;
	int status;

	if (Pa_Initialize() != paNoError)
		return;

	while (player->action != PLAYER_EXIT) {
		player->resp = PLAYER_NOTSET;
		switch (player->action) {
		case PLAYER_PLAY:
			player->resp = player_play();
			break;
		case PLAYER_PAUSE:
			player->resp = player_pause();
			break;
		case PLAYER_SKIP:
			player->resp = player_skip();
			break;
		case PLAYER_PREV:
			player->resp = player_prev();
			break;
		}
		Pa_Sleep(100);
	}

	Pa_Terminate();
}

int
player_alive(void)
{
	return player->alive && !kill(player->pid, 0);
}

void
player_loadfile(const char *file)
{
	ASSERT(player_alive());
	player_action(PLAYER_STOP);
}

void
player_action(int action)
{
	ASSERT(player_alive());
	player->action = action;
}

int
player_play(void)
{
	return PLAYER_OK;
}

int
player_pause(void)
{
	return PLAYER_OK;
}

int
player_skip(void)
{
	return PLAYER_OK;
}

int
player_prev(void)
{
	return PLAYER_OK;
}