tmpd.c - spoon - [fork] customized build of spoon, the dwm status utility
(HTM) git clone git://src.adamsgaard.dk/spoon
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) LICENSE
---
tmpd.c (1567B)
---
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 int
10 mpdread(void *arg, char *buf, size_t len)
11 {
12 static struct mpd_connection *conn;
13 struct mpd_status *status;
14 enum mpd_state state;
15 struct mpd_song *song;
16 const char *artist, *title;
17 struct mpdarg *mpdarg = arg;
18 static int frame = 0;
19
20 #define CHECK_CONNECTION(conn) \
21 if (mpd_connection_get_error(conn) != MPD_ERROR_SUCCESS) { \
22 warnx("mpd_connection_get_error: %s", \
23 mpd_connection_get_error_message(conn)); \
24 goto out; \
25 }
26
27 if (conn == NULL) {
28 conn = mpd_connection_new(mpdarg->host, mpdarg->port, 0);
29 if (conn == NULL)
30 return -1;
31 CHECK_CONNECTION(conn);
32 }
33 mpd_send_status(conn);
34 status = mpd_recv_status(conn);
35 if (status == NULL) {
36 CHECK_CONNECTION(conn);
37 mpd_response_finish(conn);
38 return -1;
39 }
40 state = mpd_status_get_state(status);
41 mpd_status_free(status);
42 mpd_response_finish(conn);
43 if (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE)
44 return -1;
45 mpd_send_current_song(conn);
46 song = mpd_recv_song(conn);
47 if (song == NULL) {
48 CHECK_CONNECTION(conn);
49 mpd_response_finish(conn);
50 return -1;
51 }
52 artist = mpd_song_get_tag(song, MPD_TAG_ARTIST, 0);
53 title = mpd_song_get_tag(song, MPD_TAG_TITLE, 0);
54 if (artist != NULL && title != NULL) {
55 snprintf(buf, len, "%s - %s", artist, title);
56 } else if (title != NULL) {
57 strlcpy(buf, title, len);
58 } else {
59 strlcpy(buf, ">", 2);
60 }
61 mpd_song_free(song);
62 mpd_response_finish(conn);
63 return 0;
64 out:
65 mpd_connection_free(conn);
66 conn = NULL;
67 return -1;
68 }