mpd.c - spoon - set dwm status | |
git clone git://git.codemadness.org/spoon | |
Log | |
Files | |
Refs | |
LICENSE | |
--- | |
mpd.c (1662B) | |
--- | |
1 #include <err.h> | |
2 #include <stdio.h> | |
3 | |
4 #include <mpd/client.h> | |
5 | |
6 #include "types.h" | |
7 #include "util.h" | |
8 | |
9 char *anim[] = { | |
10 "!!.|.", | |
11 "|!.!.", | |
12 "!.!!.", | |
13 "!!|.!", | |
14 ".!!|!", | |
15 }; | |
16 | |
17 int | |
18 mpdread(void *arg, char *buf, size_t len) | |
19 { | |
20 static struct mpd_connection *conn; | |
21 struct mpd_status *status; | |
22 enum mpd_state state; | |
23 struct mpd_song *song; | |
24 const char *artist, *title; | |
25 struct mpdarg *mpdarg = arg; | |
26 static int frame = 0; | |
27 | |
28 #define CHECK_CONNECTION(conn) \ | |
29 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { \ | |
30 warnx("mpd_connection_get_error: %s", \ | |
31 mpd_connection_get_error_message(conn)); \ | |
32 goto out; \ | |
33 } | |
34 | |
35 if (conn == NULL) { | |
36 conn = mpd_connection_new(mpdarg->host, mpdarg->port, 0); | |
37 if (conn == NULL) | |
38 return -1; | |
39 CHECK_CONNECTION(conn); | |
40 } | |
41 mpd_send_status(conn); | |
42 status = mpd_recv_status(conn); | |
43 if (status == NULL) { | |
44 CHECK_CONNECTION(conn); | |
45 mpd_response_finish(conn); | |
46 return -1; | |
47 } | |
48 state = mpd_status_get_state(status); | |
49 mpd_status_free(status); | |
50 mpd_response_finish(conn); | |
51 if (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE) | |
52 return -1; | |
53 mpd_send_current_song(conn); | |
54 song = mpd_recv_song(conn); | |
55 if (song == NULL) { | |
56 CHECK_CONNECTION(conn); | |
57 mpd_response_finish(conn); | |
58 return -1; | |
59 } | |
60 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0); | |
61 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0); | |
62 if (artist != NULL && title != NULL) { | |
63 snprintf(buf, len, "%s - %s", artist, title); | |
64 } else if (title != NULL) { | |
65 strlcpy(buf, title, len); | |
66 } else { | |
67 strlcpy(buf, anim[frame++ % LEN(anim)], len); | |
68 } | |
69 mpd_song_free(song); | |
70 mpd_response_finish(conn); | |
71 return 0; | |
72 out: | |
73 mpd_connection_free(conn); | |
74 conn = NULL; | |
75 return -1; | |
76 } |