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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/**
* mpd_trigger: Execute whatever you want when MPD (Music Player Daemon) changes its state
* Copyright (C) 2014 Ted Yin
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <getopt.h>
#include <mpd/idle.h>
#include <mpd/tag.h>
#include <mpd/status.h>
#include <mpd/client.h>
struct mpd_connection *conn;
typedef struct mpd_song mpd_song_t;
typedef struct mpd_status mpd_status_t;
#define MAINLOOP_ERROR do { handle_error(conn); return; } while (0)
#define MAX_HASH 1021
#define MAX_INFO_BUFF 256
#define MAX_OUTPUT_BUFF 2048
const char *host = "localhost";
unsigned int port = 6600;
unsigned int reconnect_time = 3;
const char *state_name[4] = {"unknown", "stopped", "now playing", "paused"};
const char *shell = "bash";
const char *trigger_command = "terminal-notifier -title \"{title}: {state} ({elapsed_pct}%)\" "
"-subtitle \"{artist}\" -message \"{album} @ {track?{track}:unknown track}\" -sender com.apple.iTunes";
typedef const char *(*Hook_t)(mpd_status_t status, mpd_song_t song);
typedef struct Entry {
const char *name;
const char **content_ref;
struct Entry *next;
} Entry;
typedef struct {
Entry *head[MAX_HASH];
} HashTable;
HashTable *hash_table_create() {
HashTable *ht = (HashTable *)malloc(sizeof(HashTable));
memset(ht->head, sizeof(ht->head), 0);
return ht;
}
unsigned hash_table_hash_func(const char *name) {
unsigned int seed = 131, res = 0;
while (*name)
res = res * seed + (*name++ - 'a');
return res % MAX_HASH;
}
void hash_table_destroy(HashTable *ht) {
unsigned int i;
for (i = 0; i < MAX_HASH; i++)
{
Entry *e, *ne;
for (e = ht->head[i]; e; e = ne)
{
ne = e->next;
free(e);
}
}
free(ht);
}
void hash_table_register(HashTable *ht, const char *name, const char **content_ref) {
unsigned int hv = hash_table_hash_func(name);
Entry *e = (Entry *)malloc(sizeof(Entry));
e->name = name;
e->content_ref = content_ref;
e->next = ht->head[hv];
ht->head[hv] = e;
}
const char *hash_table_lookup(HashTable *ht, const char *name) {
unsigned int hv = hash_table_hash_func(name);
Entry *e;
for (e = ht->head[hv]; e; e = e->next)
if (!strcmp(e->name, name))
return *(e->content_ref);
return NULL;
}
char etime_buff[MAX_INFO_BUFF], ttime_buff[MAX_INFO_BUFF], epct_buff[MAX_INFO_BUFF];
const char *title, *artist, *album, *track, *state,
*elapsed_time = etime_buff, *total_time = ttime_buff, *elapsed_pct = epct_buff;
HashTable *dict;
void handle_error(struct mpd_connection *conn) {
fprintf(stderr, "[mpd] error: %s\n", mpd_connection_get_error_message(conn));
mpd_connection_free(conn);
}
#define CHECK_OVERFLOW(ptr, buff, limit) \
do { \
if (ptr >= buff + limit) \
{ \
fprintf(stderr, "[main] string is too long to be stored"); \
exit(1); \
} \
} while (0)
const char *substitution(const char *exp, size_t *size) {
static char output_buff[MAX_OUTPUT_BUFF];
const char *content;
char *optr = output_buff;
char *cond_pos = NULL, *sep_pos = NULL;
*size = 0;
while (*exp)
{
CHECK_OVERFLOW(optr, output_buff, MAX_OUTPUT_BUFF);
if (*exp == '?') cond_pos = optr;
else if (*exp == ':') sep_pos = optr;
*optr++ = *exp++;
(*size)++;
}
CHECK_OVERFLOW(optr, output_buff, MAX_OUTPUT_BUFF);
*optr = '\0';
if (cond_pos && sep_pos) /* a substitution */
{
char *start;
*cond_pos = '\0';
*sep_pos = '\0';
content = hash_table_lookup(dict, output_buff);
start = (content && *content) ? cond_pos + 1: sep_pos + 1;
*size = strlen(start);
memmove(output_buff, start, *size);
output_buff[*size] = '\0';
}
else
{
if ((content = hash_table_lookup(dict, output_buff)))
{
*size = strlen(content);
memmove(output_buff, content, *size);
output_buff[*size] = '\0';
}
}
return output_buff;
}
const char *filter(const char *input) {
static char output_buff[MAX_OUTPUT_BUFF];
static char *pos[MAX_OUTPUT_BUFF];
char *optr = output_buff;
char **pptr = pos - 1;
enum {
ESCAPE,
NORMAL
} state = NORMAL;
while (*input)
{
CHECK_OVERFLOW(optr, output_buff, MAX_OUTPUT_BUFF);
if (state == NORMAL)
{
if (*input == '\\')
state = ESCAPE;
else if (*input == '}')
{
size_t csize;
const char *content;
char *bptr = optr - 1;
if (pptr >= pos)
{
while (bptr != *pptr) bptr--;
*optr = '\0';
content = substitution(bptr + 1, &csize);
memmove(bptr, content, csize);
optr = bptr + csize;
pptr--;
}
else *optr++ = '}';
state = NORMAL;
}
else
{
if (*input == '{')
*(++pptr) = optr;
*optr++ = *input;
}
}
else
{
*optr++ = *input;
state = NORMAL;
}
input++;
}
CHECK_OVERFLOW(optr, output_buff, MAX_OUTPUT_BUFF);
*optr = '\0';
return output_buff;
}
void trigger(const char *filtered_cmd) {
pid_t pid;
int fd[2];
pipe(fd);
fprintf(stderr, "[trigger] executing command: %s\n", filtered_cmd);
if ((pid = fork()) == -1)
{
fprintf(stderr, "[trigger] failed to fork\n");
return;
}
else if (!pid)
{
dup2(fd[0], 0); /* override stdin */
close(fd[0]);
close(fd[1]);
execlp(shell, shell, (char *)NULL);
}
close(fd[0]);
write(fd[1], filtered_cmd, strlen(filtered_cmd));
close(fd[1]);
waitpid(pid, NULL, 0);
}
void main_loop() {
for (;;)
{
int idle_info;
mpd_song_t *song;
mpd_status_t *status;
if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS)
MAINLOOP_ERROR;
mpd_send_idle_mask(conn, MPD_IDLE_PLAYER);
idle_info = mpd_recv_idle(conn, 1);
if (!idle_info) MAINLOOP_ERROR;
if (idle_info == MPD_IDLE_PLAYER)
{
int et, tt;
mpd_send_status(conn);
status = mpd_recv_status(conn);
if (!status) MAINLOOP_ERROR;
state = state_name[mpd_status_get_state(status)];
fprintf(stderr, "[mpd] new event: %s\n", state);
et = mpd_status_get_elapsed_time(status);
tt = mpd_status_get_total_time(status);
snprintf(etime_buff, sizeof(etime_buff), "%i", et);
snprintf(ttime_buff, sizeof(ttime_buff), "%i", tt);
snprintf(epct_buff, sizeof(epct_buff), "%d", tt ? et * 100 / tt : 0);
mpd_status_free(status);
mpd_send_current_song(conn);
while ((song = mpd_recv_song(conn)) != NULL)
{
title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
album = mpd_song_get_tag(song, MPD_TAG_ALBUM, 0);
track = mpd_song_get_tag(song, MPD_TAG_TRACK, 0);
trigger(filter(trigger_command));
mpd_song_free(song);
}
}
}
}
struct option long_options[] = {
{"port", required_argument, NULL, 'p'},
{"execute", required_argument, NULL, 'e'},
{"help", no_argument, NULL, 'h'},
{"shell", required_argument, NULL, 's'},
{"retry", required_argument, NULL, 'r'},
{0, 0, 0, 0}
};
int str_to_int(char *repr, int *flag) {
char *endptr;
int val = (int)strtol(repr, &endptr, 10);
if (endptr == repr || endptr != repr + strlen(repr))
{
*flag = 0;
return 0;
}
*flag = 1;
return val;
}
int main(int argc, char **argv) {
int opt, ind = 0;
dict = hash_table_create();
hash_table_register(dict, "title", &title);
hash_table_register(dict, "artist", &artist);
hash_table_register(dict, "album", &album);
hash_table_register(dict, "track", &track);
hash_table_register(dict, "state", &state);
hash_table_register(dict, "elapsed_time", &elapsed_time);
hash_table_register(dict, "total_time", &total_time);
hash_table_register(dict, "elapsed_pct", &elapsed_pct);
while ((opt = getopt_long(argc, argv, "p:e:s:r:h", long_options, &ind)) != -1)
{
int flag;
switch (opt)
{
case 'p':
port = str_to_int(optarg, &flag);
if (!flag)
return fprintf(stderr, "Port number should be an integer.\n"), 1;
break;
case 'r':
reconnect_time = str_to_int(optarg, &flag);
if (!flag)
return fprintf(stderr, "reconnect time should be an integer.\n"), 1;
break;
case 'e':
trigger_command = optarg;
break;
case 's':
shell = optarg;
break;
case 'h':
fprintf(stderr,
"mpd_trigger: Execute whatever you want when MPD "
"(Music Player Daemon) changes its state \n\n"
"Usage: mpd_trigger [OPTION]... [HOST]\n\n"
" -p, --port\t\tspecify a port number (6600 by default)\n"
" -e, --execute\t\tspecify the command to trigger "
"(special patterns are supported: {title} {track} ... {str1?str2:str3})\n"
" -s, --shell\t\tspecify shell used to interpret the command\n"
" -r, --retry\t\tspecify reconnect time (in seconds)\n"
" -h, --help\t\tshow this info\n"
"\nAuthor: Ted Yin <[email protected]>\n");
return 0;
}
}
if (optind < argc) host = argv[optind];
for (;;)
{
fprintf(stderr, "[mpd] trying to connect %s:%d\n", host, port);
conn = mpd_connection_new(host, port, 0);
main_loop();
fprintf(stderr, "[mpd] reconnecting\n");
sleep(reconnect_time);
}
return 0;
}
|