-
Notifications
You must be signed in to change notification settings - Fork 9
/
spotify-listener.c
360 lines (298 loc) · 10.8 KB
/
spotify-listener.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
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
#include "../include/spotify-listener.h"
#include <dbus-1.0/dbus/dbus.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../include/utils.h"
#ifdef VERBOSE
const dbus_bool_t VERBOSE = TRUE;
#else
const dbus_bool_t VERBOSE = FALSE;
#endif
const char *POLYBAR_IPC_DIRECTORY = "/tmp";
// Used to check if track has changed
char *last_trackid = NULL;
// Used to identify spotify for Play/Pause
char *dbus_senderid = NULL;
// Current state of spotify
typedef enum { PLAYING, PAUSED, EXITED } SpotifyState;
SpotifyState CURRENT_SPOTIFY_STATE = EXITED;
// DBus signals to listen for
const char *PROPERTIES_CHANGED_MATCH =
"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
"path='/org/mpris/MediaPlayer2'";
const char *NAME_OWNER_CHANGED_MATCH =
"interface='org.freedesktop.DBus',member='NameOwnerChanged',path='/org/"
"freedesktop/DBus'";
dbus_bool_t update_last_trackid(const char *trackid) {
if (trackid != NULL) {
// +1 for null char
size_t size = strlen(trackid) + 1;
last_trackid = (char *)realloc(last_trackid, size);
last_trackid[0] = '\0';
strcpy(last_trackid, trackid);
return TRUE;
} else {
return FALSE;
}
}
dbus_bool_t spotify_update_track(const char *current_trackid) {
// If trackid didn't change
if (last_trackid != NULL && strcmp(current_trackid, last_trackid) != 0) {
puts("Track Changed");
// Send message to update track name
if (send_ipc_polybar(1, "hook:module/spotify2")) return TRUE;
}
return FALSE;
}
dbus_bool_t spotify_update_sender(const char *senderid) {
if (senderid != NULL) {
// +1 for null char
size_t size = strlen(senderid) + 1;
dbus_senderid = (char *)realloc(dbus_senderid, size);
dbus_senderid[0] = '\0';
strcpy(dbus_senderid, senderid);
return TRUE;
} else {
return FALSE;
}
}
dbus_bool_t spotify_playing() {
if (CURRENT_SPOTIFY_STATE != PLAYING) {
puts("Song is playing");
// Show pause, next, and previous button on polybar
if (send_ipc_polybar(4, "hook:module/playpause2",
"hook:module/previous2", "hook:module/next2",
"hook:module/spotify2")) {
CURRENT_SPOTIFY_STATE = PLAYING;
return TRUE;
}
}
return FALSE;
}
dbus_bool_t spotify_paused() {
if (CURRENT_SPOTIFY_STATE != PAUSED) {
puts("Song is paused");
// Show play, next, and previous button on polybar
if (send_ipc_polybar(4, "hook:module/playpause3",
"hook:module/previous2", "hook:module/next2",
"hook:module/spotify2")) {
CURRENT_SPOTIFY_STATE = PAUSED;
return TRUE;
}
}
return FALSE;
}
dbus_bool_t spotify_exited() {
if (CURRENT_SPOTIFY_STATE != EXITED) {
// Hide all buttons and track display on polybar
if (send_ipc_polybar(4, "hook:module/playpause1",
"hook:module/previous1", "hook:module/next1",
"hook:module/spotify1")) {
CURRENT_SPOTIFY_STATE = EXITED;
return TRUE;
}
}
return FALSE;
}
dbus_bool_t send_ipc_polybar(int numOfMsgs, ...) {
char **paths;
size_t num_of_paths;
va_list args;
// Pass address of pointer to array of strings
get_polybar_ipc_paths(POLYBAR_IPC_DIRECTORY, &paths, &num_of_paths);
for (size_t p = 0; p < num_of_paths; p++) {
FILE *fp;
va_start(args, numOfMsgs);
for (int m = 0; m < numOfMsgs; m++) {
const char *message = va_arg(args, char *);
fp = fopen(paths[p], "w");
fputs(message, fp);
printf("%s%s%s%s%s\n", "Sending the message '", message, "' to '",
paths[p], "'");
fclose(fp);
// Without sleep, requests are sometimes ignored
msleep(10);
}
va_end(args);
free(paths[p]);
}
free(paths);
return TRUE;
}
DBusHandlerResult properties_changed_handler(DBusConnection *connection,
DBusMessage *message,
void *user_data) {
if (VERBOSE) puts("Running properties_changed_handler");
DBusMessageIter iter;
DBusMessageIter sub_iter;
dbus_bool_t is_spotify = FALSE;
dbus_message_iter_init(message, &iter);
/**
* Format of PropertiesChanged signal
* string "org.mpris.MediaPlayer2.Player"
* array [
* dict entry(
* string "Metadata"
* variant array [
* dict entry(
* string "mpris:trackid"
* variant string "spotify:track:xxxxxxxxxxxxx"
* )
* .
* .
* .
* ]
* )
* dict entry(
* string "PlaybackStatus"
* variant string "Paused"
* )
* ]
*
*/
char *interface_name = iter_get_string(&iter);
// Check if interface is correct
if (interface_name != NULL &&
strcmp(interface_name, "org.mpris.MediaPlayer2.Player") != 0) {
if (VERBOSE)
puts(
"Interface of PropertiesChanged signal not "
"org.mpris.MediaPlayer2.Player");
free(interface_name);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
free(interface_name);
dbus_message_iter_next(&iter);
// Recurse into array
if (!(recurse_iter_of_type(&iter, &sub_iter, DBUS_TYPE_ARRAY) &&
// Go to value with Metadata key
iter_try_step_to_key(&sub_iter, "Metadata") &&
// Step into variant value
iter_try_step_into_type(&sub_iter, DBUS_TYPE_VARIANT) &&
// Step into array of metadata
iter_try_step_into_signature(&sub_iter, "a{sv}") &&
// Go to value with key mpris:trackid
iter_try_step_to_key(&sub_iter, "mpris:trackid") &&
// Step into container
iter_try_step_into_type(&sub_iter, DBUS_TYPE_VARIANT) &&
// Verify string type
dbus_message_iter_get_arg_type(&sub_iter) == DBUS_TYPE_STRING) &&
!(recurse_iter_of_type(&iter, &sub_iter, DBUS_TYPE_ARRAY) &&
// Go to value with Metadata key
iter_try_step_to_key(&sub_iter, "PlaybackStatus") &&
// Step into variant value
iter_try_step_into_type(&sub_iter, DBUS_TYPE_VARIANT) &&
// Verify string type
dbus_message_iter_get_arg_type(&sub_iter) == DBUS_TYPE_STRING)) {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
// Make sure trackid begins with spotify
char *trackid = iter_get_string(&sub_iter);
if (trackid != NULL && strncmp(trackid, "/com/spotify", 12) == 0) {
spotify_update_sender(dbus_message_get_sender(message));
spotify_update_track(trackid);
update_last_trackid(trackid);
is_spotify = TRUE;
if (VERBOSE) puts("Spotify Detected");
}
free(trackid);
if (!is_spotify && dbus_senderid && strcmp(dbus_senderid, dbus_message_get_sender(message)) == 0) {
is_spotify = TRUE;
}
if (is_spotify) {
// Recurse into array
if (!(recurse_iter_of_type(&iter, &sub_iter, DBUS_TYPE_ARRAY) &&
// Step to PlaybackStatus key
iter_try_step_to_key(&sub_iter, "PlaybackStatus") &&
// Recurse into variant value
iter_try_step_into_type(&sub_iter, DBUS_TYPE_VARIANT) &&
// Verify string type
dbus_message_iter_get_arg_type(&sub_iter) == DBUS_TYPE_STRING)) {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
// Update polybar modules
char *status = iter_get_string(&sub_iter);
if (strcmp(status, "Paused") == 0) {
spotify_paused();
} else if (strcmp(status, "Playing") == 0) {
spotify_playing();
}
free(status);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult name_owner_changed_handler(DBusConnection *connection,
DBusMessage *message,
void *user_data) {
if (VERBOSE) puts("Starting handler for name owner changed");
const char *name;
const char *old_owner;
const char *new_owner;
/**
* Format of NameOwnerChanged signal
* string "name"
* string "old owner"
* string "new owner"
*
*/
// Try to get message arguments
if (!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old_owner, DBUS_TYPE_STRING,
&new_owner, DBUS_TYPE_INVALID)) {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
// If name matches spotify and new owner is "", spotify disconnected
if (strcmp(name, "org.mpris.MediaPlayer2.spotify") == 0 &&
strcmp(new_owner, "") == 0) {
puts("Spotify disconnected");
spotify_exited();
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
void free_user_data(void *memory) {}
int main() {
DBusConnection *connection;
DBusError err;
dbus_error_init(&err);
// Connect to session bus
if (!(connection = dbus_bus_get(DBUS_BUS_SESSION, &err))) {
fputs(err.message, stderr);
return 1;
}
// Receive messages for PropertiesChanged signal to detect track changes
// or spotify launching
dbus_bus_add_match(connection, PROPERTIES_CHANGED_MATCH, &err);
if (dbus_error_is_set(&err)) {
fputs(err.message, stderr);
return 1;
}
// Receive messages for NameOwnerChanged signal to detect spotify exiting
dbus_bus_add_match(connection, NAME_OWNER_CHANGED_MATCH, &err);
if (dbus_error_is_set(&err)) {
fputs(err.message, stderr);
return 1;
}
// Register handler for PropertiesChanged signal
if (!dbus_connection_add_filter(connection, properties_changed_handler,
NULL, free_user_data)) {
fputs("Failed to add properties changed handler", stderr);
return 1;
}
// Register handler for NameOwnerChanged signal
if (!dbus_connection_add_filter(connection, name_owner_changed_handler,
NULL, free_user_data)) {
fputs("Failed to add NameOwnerChanged handler", stderr);
return 1;
}
// Read messages and call handlers when neccessary
while (dbus_connection_read_write_dispatch(connection, -1)) {
if (VERBOSE) puts("In dispatch loop");
}
dbus_connection_unref(connection);
return 0;
}