-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpd-notification.c
325 lines (275 loc) · 7.8 KB
/
mpd-notification.c
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
/* See LICENSE file for licensing details. */
/* Includes */
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <mpd/client.h>
#include <libnotify/notify.h>
#include <glib.h>
/* Macro definitions */
/* Get array length on compile time */
#define LENGTH(a) (sizeof(a) / sizeof(a[0]))
/* Compile-time configuration */
#include "config.h"
/* Global variable definitions */
static struct mpd_connection *connection = NULL;
static bool verbose = false;
static NotifyNotification *notification = NULL;
static char *notification_body = NULL, *notification_body_escaped = NULL;
static char *notification_head = NULL, *notification_head_escaped = NULL;
static struct mpd_song *song = NULL;
static enum mpd_state state;
/* Function declarations */
static const char *get_mpd_song_tag(const struct mpd_song *song, enum mpd_tag_type type);
static enum mpd_state get_mpd_state(struct mpd_connection *connection);
static const char *get_tag_token_replacement(const char token, const struct mpd_song *song);
static NotifyNotification *init_notify(const char *name);
static size_t get_replaced_format_string_length(const char *format, const struct mpd_song *song);
static char *replace_tag_tokens_all(const char *format, const struct mpd_song *song);
static const bool show_notify_notification(NotifyNotification *notification);
void die(const char *errstr, ...);
static void usage(const char *name);
/* Implementation */
/* Retrives a tag from a mpd song object */
static const char *
get_mpd_song_tag(const struct mpd_song *song, enum mpd_tag_type type)
{
const char *tag;
tag = mpd_song_get_tag(song, type, 0);
if (tag == NULL) {
tag = strunknown;
}
return tag;
}
/* Retrives the current status of mpd */
static enum mpd_state
get_mpd_state(struct mpd_connection *connection)
{
struct mpd_status *status;
enum mpd_state state;
status = mpd_run_status(connection);
if (status == NULL) {
return -1;
}
state = mpd_status_get_state(status);
mpd_status_free(status);
return state;
}
/* Replace all tokens in the format string. */
static const char *
get_tag_token_replacement(const char token, const struct mpd_song *song)
{
unsigned int i;
struct mpd_tag_token {
char token;
enum mpd_tag_type tag;
};
const struct mpd_tag_token table[] = {
{ 'a', MPD_TAG_ARTIST },
{ 'b', MPD_TAG_ALBUM },
{ 'A', MPD_TAG_ALBUM_ARTIST },
{ 't', MPD_TAG_TITLE },
{ 'T', MPD_TAG_TRACK },
{ 'n', MPD_TAG_NAME },
{ 'g', MPD_TAG_GENRE },
{ 'd', MPD_TAG_DATE },
{ 'c', MPD_TAG_COMPOSER },
{ 'p', MPD_TAG_PERFORMER },
{ 'C', MPD_TAG_COMMENT },
{ 'D', MPD_TAG_DISC }
};
for (i = 0; i < LENGTH(table); i++) {
if (table[i].token == token) {
return get_mpd_song_tag(song, table[i].tag);
}
}
return NULL;
}
/* Initialize libnotify; return a fresh notification object */
static NotifyNotification *
init_notify(const char *name)
{
NotifyNotification *notification;
if (!notify_init(name)) {
return NULL;
}
notification = notify_notification_new(name, NULL, "sound");
notify_notification_set_urgency(notification, urgency);
return notification;
}
static size_t
get_replaced_format_string_length(const char *format, const struct mpd_song *song)
{
size_t size = strlen(format);
while ((format = strchr(format, '%')) != NULL) {
if (format[1] == '\0') break;
else if (format[1] == '%') size--;
else {
const char *replacement;
replacement = get_tag_token_replacement(format[1], song);
if (replacement == NULL) continue;
/* Subtract the token itself. */
size += strlen(replacement) - 2;
}
format += 2;
}
return size;
}
/* Replaces all tokens in a given format string with the tags of the currently
* playing song. */
static char *
replace_tag_tokens_all(const char *format, const struct mpd_song *song)
{
char *format_position = NULL;
char *new, *new_end;
size_t offset;
size_t size = get_replaced_format_string_length(format, song);
new = calloc(size+1, sizeof(char));
if (new == NULL) return NULL;
new_end = new;
while ((format_position = strchr(format, '%')) != NULL) {
offset = format_position - format;
strncpy(new_end, format, offset);
new_end += offset;
if (format_position[1] == '\0') break;
else if (format_position[1] == '%') {
new_end[0] = '%';
new_end++;
}
else {
const char *replacement;
replacement = get_tag_token_replacement(format_position[1], song);
if (replacement != NULL) {
size_t replacement_size = strlen(replacement);
strncpy(new_end, replacement, replacement_size);
new_end += replacement_size;
}
}
/* Skip the token. */
format = format_position + 2;
}
/* Copy the rest. */
strcpy(new_end, format);
new[size] = '\0';
return new;
}
/* Send the notification to the notification daemon. */
static const bool
show_notify_notification(NotifyNotification *notification)
{
GError *error = NULL;
return notify_notification_show(notification, &error);
}
/* Print error message and exit. */
void
die(const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
/* Print program usage string. */
static void
usage(const char *name)
{
die("Usage: %s [-V] [-h HOST] [-p PORT] [-P PASSWORD] [-v] [-H HEADING] [-B BODY]\n", name);
}
/* Main procedure */
int
main(int argc, char *argv[])
{
int opt;
/* Parse commandline options */
while ((opt = getopt(argc, argv, "Vh:p:P:vH:B:")) != -1) {
switch (opt) {
case 'V':
printf("%s, version %s\n", NAME, VERSION);
exit(EXIT_SUCCESS);
break;
case 'h':
host = optarg;
break;
case 'p':
port = strtol(optarg, NULL, 10);
if ((errno == ERANGE && (port == LONG_MAX || port == LONG_MIN))
|| (errno != 0 && port == 0)
|| (port < 0 || port > 65535)) {
die("Invalid port number. Exiting.\n");
}
break;
case 'P':
password = optarg;
break;
case 'v':
verbose = true;
break;
case 'B':
body = optarg;
break;
case 'H':
head = optarg;
break;
default:
usage(argv[0]);
break;
}
}
connection = mpd_connection_new(host, port, timeout);
if (connection == NULL) {
die("%s: Cannot connect to mpd - Out of memory. Exiting.\n", argv[0]);
}
if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
fprintf(stderr, "%s: %s\n", argv[0], mpd_connection_get_error_message(connection));
mpd_connection_free(connection);
exit(EXIT_FAILURE);
}
if (password != NULL) {
if (!mpd_run_password(connection, password)) {
die("%s: Wrong password. Exiting.\n", argv[0]);
}
}
notification = init_notify(NAME);
/* Main loop */
while (mpd_run_idle_mask(connection, MPD_IDLE_PLAYER)) {
if ((state = get_mpd_state(connection)) == -1) {
die("%s: Cannot get mpd status. Exiting.\n", argv[0]);
}
if (state == MPD_STATE_PLAY) {
song = mpd_run_current_song(connection);
if (song == NULL) continue;
notification_head = replace_tag_tokens_all(head, song);
if (notification_head == NULL) goto free;
if (verbose) {
printf("Head: %s\n", notification_head);
}
notification_head_escaped = g_markup_escape_text(notification_head, -1);
free(notification_head);
if (body != NULL) {
notification_body = replace_tag_tokens_all(body, song);
if (notification_body == NULL) goto free;
if (verbose) {
printf("Body: %s\n", notification_body);
}
notification_body_escaped = g_markup_escape_text(notification_body, -1);
free(notification_body);
}
notify_notification_update(notification, notification_head_escaped, notification_body_escaped, NULL);
if (!show_notify_notification(notification)) {
fprintf(stderr, "%s: Cannot display notification.\n", argv[0]);
}
g_free(notification_head_escaped);
g_free(notification_body_escaped);
free:
mpd_song_free(song);
}
}
mpd_connection_free(connection);
g_object_unref(G_OBJECT(notification));
notify_uninit();
}