2 * "THE BEER-WARE LICENSE" (Revision 42):
3 * <tobias.rehbein@web.de> wrote this file. As long as you retain this notice
4 * you can do whatever you want with this stuff. If we meet some day, and you
5 * think this stuff is worth it, you can buy me a beer in return.
18 #include <mpd/connection.h>
19 #include <mpd/error.h>
20 #include <mpd/player.h>
22 #include <mpd/status.h>
32 const char *NOTAVAILABLE
= "n/a";
33 const char *NOARTIST
= "unknown artist";
34 const char *NOTITLE
= "unknown title";
37 struct mpd_connection
*conn
;
38 char mpd_utf
[MPD_BUFFLEN
];
39 char mpd_str
[MPD_BUFFLEN
];
47 struct mpd_context
*ctx
;
49 if ((ctx
= malloc(sizeof(*ctx
))) == NULL
)
50 err(EX_SOFTWARE
, "malloc(%d) mpd_context", sizeof(*ctx
));
52 if ((ctx
->cd
= iconv_open("", "UTF-8")) == (iconv_t
) (-1))
53 err(EX_SOFTWARE
, "iconv_open");
56 ctx
->mpd_str
[0] = '\0';
57 ctx
->mpd_utf
[0] = '\0';
58 ctx
->last
= time(NULL
) - SLEEP
;
64 mpd_context_close(struct mpd_context
*ctx
)
68 if (iconv_close(ctx
->cd
) == -1)
69 err(EX_SOFTWARE
, "iconv_close");
71 if (ctx
->conn
!= NULL
)
72 mpd_connection_free(ctx
->conn
);
78 mpd_str(struct mpd_context
*ctx
)
80 struct mpd_song
*song
= NULL
;
81 struct mpd_status
*status
= NULL
;
82 const char *artist
, *title
;
84 size_t inleft
, outleft
;
90 if ((now
- ctx
->last
) < SLEEP
)
94 if (ctx
->conn
== NULL
) {
95 if ((ctx
->conn
= mpd_connection_new(NULL
, 0, 0)) == NULL
) {
96 warnx("mpd_connection_new");
97 strncpy(ctx
->mpd_str
, NOTAVAILABLE
, sizeof(ctx
->mpd_str
) - 1);
98 ctx
->mpd_str
[sizeof(ctx
->mpd_str
) - 1] = '\0';
102 assert(ctx
->conn
!= NULL
);
104 if ((status
= mpd_run_status(ctx
->conn
)) == NULL
) {
105 warnx("mpd_run_status: %s", mpd_connection_get_error_message(ctx
->conn
));
106 strncpy(ctx
->mpd_str
, NOTAVAILABLE
, sizeof(ctx
->mpd_str
) - 1);
107 ctx
->mpd_str
[sizeof(ctx
->mpd_str
) - 1] = '\0';
109 mpd_connection_free(ctx
->conn
);
113 if (mpd_status_get_state(status
) != MPD_STATE_PLAY
) {
114 ctx
->mpd_str
[0] = '\0';
117 if ((song
= mpd_run_current_song(ctx
->conn
)) == NULL
) {
118 strncpy(ctx
->mpd_str
, NOTAVAILABLE
, sizeof(ctx
->mpd_str
) - 1);
119 ctx
->mpd_str
[sizeof(ctx
->mpd_str
) - 1] = '\0';
122 if ((artist
= mpd_song_get_tag(song
, MPD_TAG_ARTIST
, 0)) == NULL
)
125 if ((title
= mpd_song_get_tag(song
, MPD_TAG_TITLE
, 0)) == NULL
)
128 inleft
= snprintf(ctx
->mpd_utf
, sizeof(ctx
->mpd_utf
), "%s - %s", artist
, title
);
130 inleft
= inleft
< sizeof(ctx
->mpd_utf
) ? inleft
: (sizeof(ctx
->mpd_utf
) - 1);
131 outleft
= sizeof(ctx
->mpd_str
);
132 in
= (char *)ctx
->mpd_utf
;
133 out
= (char *)ctx
->mpd_str
;
135 if (iconv(ctx
->cd
, NULL
, NULL
, &out
, &outleft
) == (size_t) (-1))
136 err(EX_SOFTWARE
, "iconv");
138 if (iconv(ctx
->cd
, (const char **)&in
, &inleft
, &out
, &outleft
) == (size_t) (-1)) {
149 mpd_status_free(status
);
151 return (ctx
->mpd_str
);