2 * xmms2.c: xmms2 stuff for Conky
11 #include <xmmsclient/xmmsclient.h>
19 void connection_lost( void *ptr ) {
20 ((struct information *)ptr)->xmms2_conn_state = CONN_NO;
22 if ( ((struct information *)ptr)->xmms2.status == NULL )
23 ((struct information *)ptr)->xmms2.status = malloc( TEXT_BUFFER_SIZE );
24 strncpy( ((struct information *)ptr)->xmms2.status, "xmms2 not responding", TEXT_BUFFER_SIZE - 1 );
27 if ( ((struct information *)ptr)->xmms2.artist == NULL )
28 ((struct information *)ptr)->xmms2.artist = malloc( TEXT_BUFFER_SIZE );
29 strncpy( ((struct information *)ptr)->xmms2.artist, "", TEXT_BUFFER_SIZE - 1 );
31 if ( ((struct information *)ptr)->xmms2.album == NULL )
32 ((struct information *)ptr)->xmms2.album = malloc( TEXT_BUFFER_SIZE );
33 strncpy( ((struct information *)ptr)->xmms2.album, "", TEXT_BUFFER_SIZE - 1 );
35 if ( ((struct information *)ptr)->xmms2.title == NULL )
36 ((struct information *)ptr)->xmms2.title = malloc( TEXT_BUFFER_SIZE );
37 strncpy( ((struct information *)ptr)->xmms2.title, "", TEXT_BUFFER_SIZE - 1 );
39 if ( ((struct information *)ptr)->xmms2.genre == NULL )
40 ((struct information *)ptr)->xmms2.genre = malloc( TEXT_BUFFER_SIZE );
41 strncpy( ((struct information *)ptr)->xmms2.genre, "", TEXT_BUFFER_SIZE - 1 );
43 if ( ((struct information *)ptr)->xmms2.comment == NULL )
44 ((struct information *)ptr)->xmms2.comment = malloc( TEXT_BUFFER_SIZE );
45 strncpy( ((struct information *)ptr)->xmms2.comment, "", TEXT_BUFFER_SIZE - 1 );
47 if ( ((struct information *)ptr)->xmms2.decoder == NULL )
48 ((struct information *)ptr)->xmms2.decoder = malloc( TEXT_BUFFER_SIZE );
49 strncpy( ((struct information *)ptr)->xmms2.decoder, "", TEXT_BUFFER_SIZE - 1 );
51 if ( ((struct information *)ptr)->xmms2.transport == NULL )
52 ((struct information *)ptr)->xmms2.transport = malloc( TEXT_BUFFER_SIZE );
53 strncpy( ((struct information *)ptr)->xmms2.transport, "", TEXT_BUFFER_SIZE - 1 );
55 if ( ((struct information *)ptr)->xmms2.url == NULL )
56 ((struct information *)ptr)->xmms2.url = malloc( TEXT_BUFFER_SIZE );
57 strncpy( ((struct information *)ptr)->xmms2.url, "", TEXT_BUFFER_SIZE - 1 );
59 if ( ((struct information *)ptr)->xmms2.date == NULL )
60 ((struct information *)ptr)->xmms2.date = malloc( TEXT_BUFFER_SIZE );
61 strncpy( ((struct information *)ptr)->xmms2.date, "", TEXT_BUFFER_SIZE - 1 );
63 ((struct information *)ptr)->xmms2.tracknr = 0;
64 ((struct information *)ptr)->xmms2.id = 0;
65 ((struct information *)ptr)->xmms2.bitrate = 0;
66 ((struct information *)ptr)->xmms2.duration = 0;
67 ((struct information *)ptr)->xmms2.elapsed = 0;
68 ((struct information *)ptr)->xmms2.size = 0;
69 ((struct information *)ptr)->xmms2.progress = 0;
72 void handle_curent_id( xmmsc_result_t *res, void *ptr ) {
76 if ( xmmsc_result_get_uint( res, ¤t_id ) ) {
79 res2 = xmmsc_medialib_get_info( ((struct information *)ptr)->xmms2_conn, current_id );
80 xmmsc_result_wait( res2 );
83 if ( ((struct information *)ptr)->xmms2.artist == NULL )
84 ((struct information *)ptr)->xmms2.artist = malloc( TEXT_BUFFER_SIZE );
86 if ( ((struct information *)ptr)->xmms2.album == NULL )
87 ((struct information *)ptr)->xmms2.album = malloc( TEXT_BUFFER_SIZE );
89 if ( ((struct information *)ptr)->xmms2.title == NULL )
90 ((struct information *)ptr)->xmms2.title = malloc( TEXT_BUFFER_SIZE );
92 if ( ((struct information *)ptr)->xmms2.genre == NULL )
93 ((struct information *)ptr)->xmms2.genre = malloc( TEXT_BUFFER_SIZE );
95 if ( ((struct information *)ptr)->xmms2.comment == NULL )
96 ((struct information *)ptr)->xmms2.comment = malloc( TEXT_BUFFER_SIZE );
98 if ( ((struct information *)ptr)->xmms2.decoder == NULL )
99 ((struct information *)ptr)->xmms2.decoder = malloc( TEXT_BUFFER_SIZE );
101 if ( ((struct information *)ptr)->xmms2.transport == NULL )
102 ((struct information *)ptr)->xmms2.transport = malloc( TEXT_BUFFER_SIZE );
104 if ( ((struct information *)ptr)->xmms2.url == NULL )
105 ((struct information *)ptr)->xmms2.url = malloc( TEXT_BUFFER_SIZE );
107 if ( ((struct information *)ptr)->xmms2.date == NULL )
108 ((struct information *)ptr)->xmms2.date = malloc( TEXT_BUFFER_SIZE );
111 ((struct information *)ptr)->xmms2.id = current_id;
114 xmmsc_result_get_dict_entry_string( res2, "artist", &temp );
115 if ( temp != NULL ) {
116 strncpy( ((struct information *)ptr)->xmms2.artist, temp, TEXT_BUFFER_SIZE - 1 );
118 strncpy( ((struct information *)ptr)->xmms2.artist, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
122 xmmsc_result_get_dict_entry_string( res2, "title", &temp );
123 if ( temp != NULL ) {
124 strncpy( ((struct information *)ptr)->xmms2.title, temp, TEXT_BUFFER_SIZE - 1 );
126 strncpy( ((struct information *)ptr)->xmms2.title, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
129 xmmsc_result_get_dict_entry_string( res2, "album", &temp );
130 if ( temp != NULL ) {
131 strncpy( ((struct information *)ptr)->xmms2.album, temp, TEXT_BUFFER_SIZE - 1 );
133 strncpy( ((struct information *)ptr)->xmms2.album, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
137 xmmsc_result_get_dict_entry_string( res2, "genre", &temp );
138 if ( temp != NULL ) {
140 strncpy( ((struct information *)ptr)->xmms2.genre, temp, TEXT_BUFFER_SIZE - 1 );
142 strncpy( ((struct information *)ptr)->xmms2.genre, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
146 xmmsc_result_get_dict_entry_string( res2, "comment", &temp );
147 if ( temp != NULL ) {
148 strncpy( ((struct information *)ptr)->xmms2.comment, temp, TEXT_BUFFER_SIZE - 1 );
150 strncpy( ((struct information *)ptr)->xmms2.comment, "", TEXT_BUFFER_SIZE - 1 );
154 xmmsc_result_get_dict_entry_string( res2, "decoder", &temp );
155 if ( temp != NULL ) {
156 strncpy( ((struct information *)ptr)->xmms2.decoder, temp, TEXT_BUFFER_SIZE - 1 );
158 strncpy( ((struct information *)ptr)->xmms2.decoder, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
162 xmmsc_result_get_dict_entry_string( res2, "transport", &temp );
163 if ( temp != NULL ) {
164 strncpy( ((struct information *)ptr)->xmms2.transport, temp, TEXT_BUFFER_SIZE - 1 );
166 strncpy( ((struct information *)ptr)->xmms2.transport, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
170 xmmsc_result_get_dict_entry_string( res2, "url", &temp );
171 if ( temp != NULL ) {
172 strncpy( ((struct information *)ptr)->xmms2.url, temp, TEXT_BUFFER_SIZE - 1 );
174 strncpy( ((struct information *)ptr)->xmms2.url, "[Unknown]", TEXT_BUFFER_SIZE - 1 );
178 xmmsc_result_get_dict_entry_string( res2, "date", &temp );
179 if ( temp != NULL ) {
180 strncpy( ((struct information *)ptr)->xmms2.date, temp, TEXT_BUFFER_SIZE - 1 );
182 strncpy( ((struct information *)ptr)->xmms2.date, "????", TEXT_BUFFER_SIZE - 1 );
187 xmmsc_result_get_dict_entry_int( res2, "tracknr", &itemp );
188 ((struct information *)ptr)->xmms2.tracknr = itemp;
190 xmmsc_result_get_dict_entry_int( res2, "duration", &itemp );
191 ((struct information *)ptr)->xmms2.duration = itemp;
193 xmmsc_result_get_dict_entry_int( res2, "bitrate", &itemp );
194 ((struct information *)ptr)->xmms2.bitrate = itemp / 1000;
196 xmmsc_result_get_dict_entry_int( res2, "size", &itemp );
197 ((struct information *)ptr)->xmms2.size = (float)itemp / 1048576;
199 xmmsc_result_unref( res2 );
203 void handle_playtime( xmmsc_result_t *res, void *ptr ) {
204 xmmsc_result_t * res2;
207 if ( xmmsc_result_iserror( res ) )
210 if ( !xmmsc_result_get_uint( res, &play_time ) )
213 res2 = xmmsc_result_restart( res );
214 xmmsc_result_unref( res2 );
216 ((struct information *)ptr)->xmms2.elapsed = play_time;
217 ((struct information *)ptr)->xmms2.progress = (float) play_time / ((struct information *)ptr)->xmms2.duration;
220 void handle_playback_state_change( xmmsc_result_t *res, void *ptr ) {
222 if ( xmmsc_result_iserror( res ) )
225 if ( !xmmsc_result_get_uint( res, &pb_state ) )
228 if ( ((struct information *)ptr)->xmms2.status == NULL )
229 ((struct information *)ptr)->xmms2.status = malloc( TEXT_BUFFER_SIZE );
232 case XMMS_PLAYBACK_STATUS_PLAY:
233 strncpy( ((struct information *)ptr)->xmms2.status,
234 "Playing", TEXT_BUFFER_SIZE - 1 );
236 case XMMS_PLAYBACK_STATUS_PAUSE:
237 strncpy( ((struct information *)ptr)->xmms2.status,
238 "Paused", TEXT_BUFFER_SIZE - 1 );
240 case XMMS_PLAYBACK_STATUS_STOP:
241 strncpy( ((struct information *)ptr)->xmms2.status,
242 "Stopped", TEXT_BUFFER_SIZE - 1 );
245 strncpy( ((struct information *)ptr)->xmms2.status,
246 "Unknown", TEXT_BUFFER_SIZE - 1 );
251 void update_xmms2() {
252 struct information * current_info = &info;
254 /* initialize connection */
255 if ( current_info->xmms2_conn_state == CONN_INIT ) {
257 if ( current_info->xmms2_conn == NULL ) {
258 current_info->xmms2_conn = xmmsc_init( "conky" );
262 if ( current_info->xmms2_conn == NULL ) {
263 fprintf(stderr,"Conky: xmms2 init failed. %s\n", xmmsc_get_last_error ( current_info->xmms2_conn ));
268 /* init ok but not connected yet.. */
269 current_info->xmms2_conn_state = CONN_NO;
271 /* clear all values */
272 if ( current_info->xmms2.artist == NULL )
273 current_info->xmms2.artist = malloc( TEXT_BUFFER_SIZE );
274 strncpy( current_info->xmms2.artist, "", TEXT_BUFFER_SIZE - 1 );
276 if ( current_info->xmms2.album == NULL )
277 current_info->xmms2.album = malloc( TEXT_BUFFER_SIZE );
278 strncpy( current_info->xmms2.album, "", TEXT_BUFFER_SIZE - 1 );
280 if ( current_info->xmms2.title == NULL )
281 current_info->xmms2.title = malloc( TEXT_BUFFER_SIZE );
282 strncpy( current_info->xmms2.title, "", TEXT_BUFFER_SIZE - 1 );
284 if ( current_info->xmms2.genre == NULL )
285 current_info->xmms2.genre = malloc( TEXT_BUFFER_SIZE );
286 strncpy( current_info->xmms2.genre, "", TEXT_BUFFER_SIZE - 1 );
288 if ( current_info->xmms2.comment == NULL )
289 current_info->xmms2.comment = malloc( TEXT_BUFFER_SIZE );
290 strncpy( current_info->xmms2.comment, "", TEXT_BUFFER_SIZE - 1 );
292 if ( current_info->xmms2.decoder == NULL )
293 current_info->xmms2.decoder = malloc( TEXT_BUFFER_SIZE );
294 strncpy( current_info->xmms2.decoder, "", TEXT_BUFFER_SIZE - 1 );
296 if ( current_info->xmms2.transport == NULL )
297 current_info->xmms2.transport = malloc( TEXT_BUFFER_SIZE );
298 strncpy( current_info->xmms2.transport, "", TEXT_BUFFER_SIZE - 1 );
300 if ( current_info->xmms2.url == NULL )
301 current_info->xmms2.url = malloc( TEXT_BUFFER_SIZE );
302 strncpy( current_info->xmms2.url, "", TEXT_BUFFER_SIZE - 1 );
304 if ( current_info->xmms2.date == NULL )
305 current_info->xmms2.date = malloc( TEXT_BUFFER_SIZE );
306 strncpy( current_info->xmms2.date, "", TEXT_BUFFER_SIZE - 1 );
309 current_info->xmms2.tracknr = 0;
310 current_info->xmms2.id = 0;
311 current_info->xmms2.bitrate = 0;
312 current_info->xmms2.duration = 0;
313 current_info->xmms2.elapsed = 0;
314 current_info->xmms2.size = 0;
315 current_info->xmms2.progress = 0;
317 /* fprintf(stderr,"Conky: xmms2 init ok.\n");
322 if ( current_info->xmms2_conn_state == CONN_NO ) {
324 char *path = getenv ( "XMMS_PATH" );
325 if ( !xmmsc_connect( current_info->xmms2_conn, path ) ) {
326 fprintf(stderr,"Conky: xmms2 connection failed. %s\n",
327 xmmsc_get_last_error ( current_info->xmms2_conn ));
329 current_info->xmms2_conn_state = CONN_NO;
334 xmmsc_disconnect_callback_set( current_info->xmms2_conn, connection_lost, current_info );
335 XMMS_CALLBACK_SET( current_info->xmms2_conn, xmmsc_playback_current_id, handle_curent_id, current_info );
336 XMMS_CALLBACK_SET( current_info->xmms2_conn, xmmsc_broadcast_playback_current_id, handle_curent_id, current_info );
337 XMMS_CALLBACK_SET( current_info->xmms2_conn, xmmsc_signal_playback_playtime, handle_playtime, current_info );
338 XMMS_CALLBACK_SET( current_info->xmms2_conn, xmmsc_broadcast_playback_status, handle_playback_state_change, current_info );
340 /* get playback status, it wont be broadcasted untill it chages */
341 xmmsc_result_t * res = xmmsc_playback_status( current_info->xmms2_conn );
342 xmmsc_result_wait ( res );
343 unsigned int pb_state;
345 if ( current_info->xmms2.status == NULL )
346 current_info->xmms2.status = malloc( TEXT_BUFFER_SIZE );
348 xmmsc_result_get_uint( res, &pb_state );
350 case XMMS_PLAYBACK_STATUS_PLAY:
351 strncpy( current_info->xmms2.status,
352 "Playing", TEXT_BUFFER_SIZE - 1 );
354 case XMMS_PLAYBACK_STATUS_PAUSE:
355 strncpy( current_info->xmms2.status,
356 "Paused", TEXT_BUFFER_SIZE - 1 );
358 case XMMS_PLAYBACK_STATUS_STOP:
359 strncpy( current_info->xmms2.status,
360 "Stopped", TEXT_BUFFER_SIZE - 1 );
363 strncpy( current_info->xmms2.status,
364 "Unknown", TEXT_BUFFER_SIZE - 1 );
366 xmmsc_result_unref ( res );
368 /* everything seems to be ok */
369 current_info->xmms2_conn_state = CONN_OK;
371 /* fprintf(stderr,"Conky: xmms2 connected.\n");
376 /* handle callbacks */
377 if ( current_info->xmms2_conn_state == CONN_OK ) {
378 struct timeval tmout;
382 select( current_info->xmms2_fd + 1, ¤t_info->xmms2_fdset, NULL, NULL, &tmout );
384 xmmsc_io_in_handle(current_info->xmms2_conn);
385 if (xmmsc_io_want_out(current_info->xmms2_conn)) {
386 xmmsc_io_out_handle(current_info->xmms2_conn);