summaryrefslogtreecommitdiffstats
path: root/mplay.h
blob: 0a666d9aeabff6c0f44bf3d413aa174a59bacc6f (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#pragma once

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define MPLAY_API_VERSION "0.0.1"

#define MPLAY_FLAG_HEADLESS "-d"

#define MPLAY_INFO_STR_VERSION "version"
#define MPLAY_ACTION_KEY_VERSION '?'

#define MPLAY_INFO_STR_STATUS "status"
#define MPLAY_ACTION_KEY_STATUS 'S'

#define MPLAY_INFO_STR_VOLUME "volume"
#define MPLAY_ACTION_KEY_VOLUME 'v'
#define MPLAY_ACTION_KEY_VOLUME_UP '+'
#define MPLAY_ACTION_KEY_VOLUME_DOWN '-'

#define MPLAY_INFO_STR_SEEK "seek"
#define MPLAY_ACTION_KEY_SEEK 's'
#define MPLAY_ACTION_KEY_SEEK_FWD '>'
#define MPLAY_ACTION_KEY_SEEK_BWD '<'

#define MPLAY_INFO_STR_PAUSE "pause"
#define MPLAY_ACTION_KEY_PAUSE 'p'

#define MPLAY_INFO_STR_EXIT "exit"
#define MPLAY_ACTION_KEY_EXIT 'q'

#define MPLAY_INFO_STR_READY "ready"

#define MPLAY_INFO_STR_INPUT "input"

enum mplay_key {
	MPLAY_KEY_INV = 256,
	MPLAY_KEY_EOF,
	MPLAY_KEY_UP,
	MPLAY_KEY_DOWN,
	MPLAY_KEY_LEFT,
	MPLAY_KEY_RIGHT,
};

enum mplay_action {
	MPLAY_ACTION_NONE,
	MPLAY_ACTION_VERSION,
	MPLAY_ACTION_STATUS,
	MPLAY_ACTION_SEEK,
	MPLAY_ACTION_SEEK_FWD,
	MPLAY_ACTION_SEEK_BWD,
	MPLAY_ACTION_VOLUME,
	MPLAY_ACTION_VOLUME_UP,
	MPLAY_ACTION_VOLUME_DOWN,
	MPLAY_ACTION_PAUSE,
	MPLAY_ACTION_EXIT,
};

enum mplay_info {
	MPLAY_INFO_NONE,
	MPLAY_INFO_VERSION,
	MPLAY_INFO_STATUS,
	MPLAY_INFO_SEEK,
	MPLAY_INFO_VOLUME,
	MPLAY_INFO_PAUSE,
	MPLAY_INFO_READY,
	MPLAY_INFO_INPUT,
	MPLAY_INFO_EXIT	
};

static int
__attribute__((unused))
mplay__getkey_esc(void)
{
	int c;

	c = getchar();
	switch (c) {
	case 'A':
		return MPLAY_KEY_UP;
	case 'B':
		return MPLAY_KEY_DOWN;
	case 'C':
		return MPLAY_KEY_RIGHT;
	case 'D':
		return MPLAY_KEY_LEFT;
	case EOF:
		return MPLAY_KEY_EOF;
	default:
		return MPLAY_KEY_INV;
	}
}

static int
__attribute__((unused))
mplay_getkey(void)
{
	int c;

	c = getchar();
	switch (c) {
	case 0x1b:
		c = getchar();
		switch (c) {
		case '[':
			return mplay__getkey_esc();
		default:
			return MPLAY_KEY_INV;
		}
	case EOF:
		return MPLAY_KEY_EOF;
	default:
		return c > 0 ? (c & 0xff) : c;
	}
}

static enum mplay_action
__attribute__((unused))
mplay_action(int key)
{
	switch (key) {
	case MPLAY_ACTION_KEY_VERSION:
		return MPLAY_ACTION_VERSION;
	case MPLAY_ACTION_KEY_VOLUME:
		return MPLAY_ACTION_VOLUME;
	case MPLAY_KEY_UP:
	case MPLAY_ACTION_KEY_VOLUME_UP:
		return MPLAY_ACTION_VOLUME_UP;
	case MPLAY_KEY_DOWN:
	case MPLAY_ACTION_KEY_VOLUME_DOWN:
		return MPLAY_ACTION_VOLUME_DOWN;
	case MPLAY_ACTION_KEY_SEEK:
		return MPLAY_ACTION_SEEK;
	case MPLAY_KEY_LEFT:
	case MPLAY_ACTION_KEY_SEEK_BWD:
		return MPLAY_ACTION_SEEK_BWD;
	case MPLAY_KEY_RIGHT:
	case MPLAY_ACTION_KEY_SEEK_FWD:
		return MPLAY_ACTION_SEEK_FWD;
	case MPLAY_ACTION_KEY_PAUSE:
		return MPLAY_ACTION_PAUSE;
	case MPLAY_ACTION_KEY_STATUS:
		return MPLAY_ACTION_STATUS;
	default:
		return MPLAY_ACTION_NONE;
	}
}

static const char *
__attribute__((unused))
mplay_info_str(enum mplay_info info)
{
	switch (info) {
	case MPLAY_INFO_VERSION:
		return MPLAY_INFO_STR_VERSION;
	case MPLAY_INFO_SEEK:
		return MPLAY_INFO_STR_SEEK;
	case MPLAY_INFO_STATUS:
		return MPLAY_INFO_STR_STATUS;
	case MPLAY_INFO_VOLUME:
		return MPLAY_INFO_STR_VOLUME;
	case MPLAY_INFO_EXIT:
		return MPLAY_INFO_STR_EXIT;
	case MPLAY_INFO_INPUT:
		return MPLAY_INFO_STR_INPUT;
	case MPLAY_INFO_READY:
		return MPLAY_INFO_STR_READY;
	case MPLAY_INFO_PAUSE:
		return MPLAY_INFO_STR_PAUSE;
	default:
		abort();
	}
}

static enum mplay_info
__attribute__((unused))
mplay_info_parse(const char *str)
{
	if (!strcmp(str, MPLAY_INFO_STR_VERSION))
		return MPLAY_INFO_VERSION;
	else if (!strcmp(str, MPLAY_INFO_STR_SEEK))
		return MPLAY_INFO_SEEK;
	else if (!strcmp(str, MPLAY_INFO_STR_STATUS))
		return MPLAY_INFO_STATUS;
	else if (!strcmp(str, MPLAY_INFO_STR_VOLUME))
		return MPLAY_INFO_VOLUME;
	else if (!strcmp(str, MPLAY_INFO_STR_EXIT))
		return MPLAY_INFO_EXIT;
	else if (!strcmp(str, MPLAY_INFO_STR_INPUT))
		return MPLAY_INFO_INPUT;
	else if (!strcmp(str, MPLAY_INFO_STR_READY))
		return MPLAY_INFO_READY;
	else
		abort();
}

static void
__attribute__((unused))
__attribute__((format(printf, 2, 3)))
mplay_status(enum mplay_info info, const char *fmt, ...)
{
	va_list ap;

	fputs("mplay!", stdout);
	fputs(mplay_info_str(info), stdout);
	if (fmt) {
		fputc(':', stdout);
		va_start(ap, fmt);
		vfprintf(stdout, fmt, ap);
		va_end(ap);
	}
	fputc('\n', stdout);
}