Modify version string to post-release version 0.17.97-git
[libmpd.git] / src / libmpd-status.c
blobbf21960026a55c8444d3ee87793b18efe93b962d
1 /* libmpd (high level libmpdclient library)
2 * Copyright (C) 2004-2009 Qball Cow <qball@sarine.nl>
3 * Project homepage: http://gmpcwiki.sarine.nl/
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <assert.h>
23 #define __USE_GNU
25 #include <string.h>
26 #include <stdarg.h>
27 #include <config.h>
28 #include "debug_printf.h"
29 #include "libmpd.h"
30 #include "libmpd-internal.h"
32 int mpd_stats_update_real(MpdObj *mi, ChangedStatusType* what_changed);
34 int mpd_status_queue_update(MpdObj *mi)
37 if(!mpd_check_connected(mi))
39 debug_printf(DEBUG_INFO,"not connected\n");
40 return MPD_NOT_CONNECTED;
42 if(mi->status != NULL)
44 mpd_freeStatus(mi->status);
45 mi->status = NULL;
47 return MPD_OK;
51 int mpd_status_update(MpdObj *mi)
53 ChangedStatusType what_changed=0;
54 if(!mpd_check_connected(mi))
56 debug_printf(DEBUG_INFO,"not connected\n");
57 return MPD_NOT_CONNECTED;
59 if(mpd_lock_conn(mi))
61 debug_printf(DEBUG_ERROR,"lock failed\n");
62 return MPD_LOCK_FAILED;
67 if(mi->status != NULL)
69 mpd_freeStatus(mi->status);
70 mi->status = NULL;
72 mpd_sendStatusCommand(mi->connection);
73 mi->status = mpd_getStatus(mi->connection);
74 if(mi->status == NULL)
76 debug_printf(DEBUG_ERROR,"Failed to grab status from mpd\n");
77 mpd_unlock_conn(mi);
78 return MPD_STATUS_FAILED;
80 if(mpd_unlock_conn(mi))
82 debug_printf(DEBUG_ERROR, "Failed to unlock");
83 return MPD_LOCK_FAILED;
86 * check for changes
88 /* first save the old status */
89 memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));
91 /* playlist change */
92 if(mi->CurrentState.playlistid != mi->status->playlist)
94 /* print debug message */
95 debug_printf(DEBUG_INFO, "Playlist has changed!");
97 /* We can't trust the current song anymore. so we remove it */
98 /* tags might have been updated */
99 if(mi->CurrentSong != NULL)
101 mpd_freeSong(mi->CurrentSong);
102 mi->CurrentSong = NULL;
105 /* set MPD_CST_PLAYLIST to be changed */
106 what_changed |= MPD_CST_PLAYLIST;
108 if(mi->CurrentState.playlistLength == mi->status->playlistLength)
110 // what_changed |= MPD_CST_SONGID;
112 /* save new id */
113 mi->CurrentState.playlistid = mi->status->playlist;
116 if(mi->CurrentState.storedplaylistid != mi->status->storedplaylist)
118 /* set MPD_CST_QUEUE to be changed */
119 what_changed |= MPD_CST_STORED_PLAYLIST;
122 /* save new id */
123 mi->CurrentState.storedplaylistid = mi->status->storedplaylist;
127 /* state change */
128 if(mi->CurrentState.state != mi->status->state)
130 what_changed |= MPD_CST_STATE;
131 mi->CurrentState.state = mi->status->state;
134 if(mi->CurrentState.songid != mi->status->songid)
136 /* print debug message */
137 debug_printf(DEBUG_INFO, "Songid has changed %i %i!", mi->OldState.songid, mi->status->songid);
139 what_changed |= MPD_CST_SONGID;
140 /* save new songid */
141 mi->CurrentState.songid = mi->status->songid;
144 if(mi->CurrentState.songpos != mi->status->song)
146 /* print debug message */
147 debug_printf(DEBUG_INFO, "Songpos has changed %i %i!", mi->OldState.songpos, mi->status->song);
149 what_changed |= MPD_CST_SONGPOS;
150 /* save new songid */
151 mi->CurrentState.songpos = mi->status->song;
155 if(mi->CurrentState.repeat != mi->status->repeat)
157 what_changed |= MPD_CST_REPEAT;
158 mi->CurrentState.repeat = mi->status->repeat;
160 if(mi->CurrentState.random != mi->status->random)
162 what_changed |= MPD_CST_RANDOM;
163 mi->CurrentState.random = mi->status->random;
165 if(mi->CurrentState.volume != mi->status->volume)
167 what_changed |= MPD_CST_VOLUME;
168 mi->CurrentState.volume = mi->status->volume;
170 if(mi->CurrentState.xfade != mi->status->crossfade)
172 what_changed |= MPD_CST_CROSSFADE;
173 mi->CurrentState.xfade = mi->status->crossfade;
175 if(mi->CurrentState.totaltime != mi->status->totalTime)
177 what_changed |= MPD_CST_TOTAL_TIME;
178 mi->CurrentState.totaltime = mi->status->totalTime;
180 if(mi->CurrentState.elapsedtime != mi->status->elapsedTime)
182 what_changed |= MPD_CST_ELAPSED_TIME;
183 mi->CurrentState.elapsedtime = mi->status->elapsedTime;
186 /* Check if bitrate changed, happens with vbr encodings. */
187 if(mi->CurrentState.bitrate != mi->status->bitRate)
189 what_changed |= MPD_CST_BITRATE;
190 mi->CurrentState.bitrate = mi->status->bitRate;
193 /* The following 3 probly only happen on a song change, or is it possible in one song/stream? */
194 /* Check if the sample rate changed */
195 if(mi->CurrentState.samplerate != mi->status->sampleRate)
197 what_changed |= MPD_CST_AUDIOFORMAT;
198 mi->CurrentState.samplerate = mi->status->sampleRate;
201 /* check if the sampling depth changed */
202 if(mi->CurrentState.bits != mi->status->bits)
204 what_changed |= MPD_CST_AUDIOFORMAT;
205 mi->CurrentState.bits = mi->status->bits;
208 /* Check if the amount of audio channels changed */
209 if(mi->CurrentState.channels != mi->status->channels)
211 what_changed |= MPD_CST_AUDIOFORMAT;
212 mi->CurrentState.channels = mi->status->channels;
215 if(mi->status->error)
217 what_changed |= MPD_CST_SERVER_ERROR;
218 strcpy(mi->CurrentState.error,mi->status->error);
219 mpd_sendClearErrorCommand(mi->connection);
220 mpd_finishCommand(mi->connection);
222 else
224 mi->CurrentState.error[0] ='\0';
227 /* Check if the updating changed,
228 * If it stopped, also update the stats for the new db-time.
230 if(mi->CurrentState.updatingDb != mi->status->updatingDb )
232 what_changed |= MPD_CST_UPDATING;
233 if(!mi->status->updatingDb)
235 mpd_stats_update_real(mi, &what_changed);
237 mi->CurrentState.updatingDb = mi->status->updatingDb;
241 mi->CurrentState.playlistLength = mi->status->playlistLength;
244 /* Detect changed outputs */
245 if(!mi->has_idle)
247 if(mi->num_outputs >0 )
249 mpd_OutputEntity *output = NULL;
250 mpd_sendOutputsCommand(mi->connection);
251 while (( output = mpd_getNextOutput(mi->connection)) != NULL)
253 if(mi->output_states[output->id] != output->enabled)
255 mi->output_states[output->id] = output->enabled;
256 what_changed |= MPD_CST_OUTPUT;
258 mpd_freeOutputElement(output);
260 mpd_finishCommand(mi->connection);
262 else
264 /* if no outputs, lets fetch them */
265 mpd_server_update_outputs(mi);
266 if(mi->num_outputs == 0)
268 assert("No outputs defined? that cannot be\n");
270 what_changed |= MPD_CST_OUTPUT;
272 }else {
273 char *name;
274 int update_stats = 0;
275 mpd_sendGetEventsCommand(mi->connection);
276 while((name = mpd_getNextEvent(mi->connection))){
277 if(strcmp(name, "output") == 0){
278 what_changed |= MPD_CST_OUTPUT;
279 }else if (strcmp(name, "database") == 0) {
280 if((what_changed&MPD_CST_DATABASE) == 0)
282 update_stats = 1;
284 what_changed |= MPD_CST_DATABASE;
285 }else if (strcmp(name, "stored_playlist")==0) {
286 what_changed |= MPD_CST_STORED_PLAYLIST;
287 }else if (strcmp(name, "tag") == 0) {
288 what_changed |= MPD_CST_PLAYLIST;
289 }else if (strcmp (name, "sticker") == 0) {
290 what_changed |= MPD_CST_STICKER;
292 free(name);
294 mpd_finishCommand(mi->connection);
295 if(update_stats) {
296 mpd_stats_update_real(mi, &what_changed);
301 /* Run the callback */
302 if((mi->the_status_changed_callback != NULL) && what_changed)
304 mi->the_status_changed_callback( mi, what_changed, mi->the_status_changed_signal_userdata );
307 /* We could have lost connection again during signal handling... so before we return check again if we are connected */
308 if(!mpd_check_connected(mi))
310 return MPD_NOT_CONNECTED;
312 return MPD_OK;
315 /* returns TRUE when status is availible, when not availible and connected it tries to grab it */
316 int mpd_status_check(MpdObj *mi)
318 if(!mpd_check_connected(mi))
320 debug_printf(DEBUG_INFO,"not connected\n");
321 return MPD_NOT_CONNECTED;
323 if(mi->status == NULL)
325 /* try to update */
326 if(mpd_status_update(mi))
328 debug_printf(DEBUG_INFO, "failed to update status\n");
329 return MPD_STATUS_FAILED;
332 return MPD_OK;
336 int mpd_stats_get_total_songs(MpdObj *mi)
338 if(mi == NULL)
340 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
341 return MPD_ARGS_ERROR;
343 if(mpd_stats_check(mi) != MPD_OK)
345 debug_printf(DEBUG_ERROR,"Failed to get status\n");
346 return MPD_STATUS_FAILED;
348 return mi->stats->numberOfSongs;
351 int mpd_stats_get_total_artists(MpdObj *mi)
353 if(mi == NULL)
355 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
356 return MPD_ARGS_ERROR;
358 if(mpd_stats_check(mi) != MPD_OK)
360 debug_printf(DEBUG_ERROR,"Failed to get status\n");
361 return MPD_STATS_FAILED;
363 return mi->stats->numberOfArtists;
366 int mpd_stats_get_total_albums(MpdObj *mi)
368 if(mi == NULL)
370 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
371 return MPD_ARGS_ERROR;
373 if(mpd_stats_check(mi) != MPD_OK)
375 debug_printf(DEBUG_WARNING,"Failed to get status\n");
376 return MPD_STATS_FAILED;
378 return mi->stats->numberOfAlbums;
382 int mpd_stats_get_uptime(MpdObj *mi)
384 if(mi == NULL)
386 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
387 return MPD_ARGS_ERROR;
389 if(mpd_stats_check(mi) != MPD_OK)
391 debug_printf(DEBUG_WARNING,"Failed to get status\n");
392 return MPD_STATS_FAILED;
394 return mi->stats->uptime;
397 int mpd_stats_get_playtime(MpdObj *mi)
399 if(mi == NULL)
401 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
402 return MPD_ARGS_ERROR;
404 if(mpd_stats_check(mi) != MPD_OK)
406 debug_printf(DEBUG_WARNING,"Failed to get status\n");
407 return MPD_STATS_FAILED;
409 return mi->stats->playTime;
412 int mpd_stats_get_db_playtime(MpdObj *mi)
414 if(mi == NULL)
416 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
417 return MPD_ARGS_ERROR;
419 if(mpd_stats_check(mi) != MPD_OK)
421 debug_printf(DEBUG_WARNING,"Failed to get stats\n");
422 return MPD_STATS_FAILED;
424 return mi->stats->dbPlayTime;
443 int mpd_status_get_volume(MpdObj *mi)
445 if(mi == NULL)
447 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
448 return MPD_ARGS_ERROR;
450 if(mpd_status_check(mi) != MPD_OK)
452 debug_printf(DEBUG_WARNING, "Failed to get status\n");
453 return MPD_STATUS_FAILED;
455 return mi->status->volume;
459 int mpd_status_get_bitrate(MpdObj *mi)
461 if(mi == NULL)
463 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
464 return MPD_ARGS_ERROR;
466 if(mpd_status_check(mi) != MPD_OK)
468 debug_printf(DEBUG_WARNING, "Failed to get status\n");
469 return MPD_STATUS_FAILED;
471 return mi->CurrentState.bitrate;
474 int mpd_status_get_channels(MpdObj *mi)
476 if(mi == NULL)
478 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
479 return MPD_ARGS_ERROR;
481 if(mpd_status_check(mi) != MPD_OK)
483 debug_printf(DEBUG_WARNING, "Failed to get status\n");
484 return MPD_STATUS_FAILED;
486 return mi->CurrentState.channels;
489 unsigned int mpd_status_get_samplerate(MpdObj *mi)
491 if(mi == NULL)
493 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
494 return MPD_ARGS_ERROR;
496 if(mpd_status_check(mi) != MPD_OK)
498 debug_printf(DEBUG_WARNING, "Failed to get status\n");
499 return MPD_STATUS_FAILED;
501 return mi->CurrentState.samplerate;
504 int mpd_status_get_bits(MpdObj *mi)
506 if(mi == NULL)
508 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
509 return MPD_ARGS_ERROR;
511 if(mpd_status_check(mi) != MPD_OK)
513 debug_printf(DEBUG_WARNING, "Failed to get status\n");
514 return MPD_STATUS_FAILED;
516 return mi->CurrentState.bits;
519 char * mpd_status_get_mpd_error(MpdObj *mi)
521 if(mi->CurrentState.error[0] != '\0')
523 return strdup(mi->CurrentState.error);
525 return NULL;
528 int mpd_status_db_is_updating(MpdObj *mi)
530 if(!mpd_check_connected(mi))
532 debug_printf(DEBUG_WARNING, "mpd_check_connected failed.\n");
533 return FALSE;
535 return mi->CurrentState.updatingDb;
539 int mpd_status_get_total_song_time(MpdObj *mi)
541 if(!mpd_check_connected(mi))
543 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
544 return MPD_ARGS_ERROR;
546 if(mpd_status_check(mi) != MPD_OK)
548 debug_printf(DEBUG_WARNING, "Failed to get status\n");
549 return MPD_STATUS_FAILED;
551 return mi->status->totalTime;
555 int mpd_status_get_elapsed_song_time(MpdObj *mi)
557 if(!mpd_check_connected(mi))
559 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
560 return MPD_NOT_CONNECTED;
562 if(mpd_status_check(mi) != MPD_OK)
564 debug_printf(DEBUG_WARNING,"Failed to get status\n");
565 return MPD_STATUS_FAILED;
567 return mi->status->elapsedTime;
570 int mpd_status_set_volume(MpdObj *mi,int volume)
572 if(!mpd_check_connected(mi))
574 debug_printf(DEBUG_WARNING,"not connected\n");
575 return MPD_NOT_CONNECTED;
577 /* making sure volume is between 0 and 100 */
578 volume = (volume < 0)? 0:(volume>100)? 100:volume;
580 if(mpd_lock_conn(mi))
582 debug_printf(DEBUG_ERROR,"lock failed\n");
583 return MPD_LOCK_FAILED;
586 /* send the command */
587 mpd_sendSetvolCommand(mi->connection , volume);
588 mpd_finishCommand(mi->connection);
589 /* check for errors */
591 mpd_unlock_conn(mi);
592 /* update status, because we changed it */
593 mpd_status_queue_update(mi);
594 /* return current volume */
595 return mpd_status_get_volume(mi);
598 int mpd_status_get_crossfade(MpdObj *mi)
600 if(!mpd_check_connected(mi))
602 debug_printf(DEBUG_WARNING,"not connected\n");
603 return MPD_NOT_CONNECTED;
605 if(mpd_status_check(mi) != MPD_OK)
607 debug_printf(DEBUG_WARNING,"Failed grabbing status\n");
608 return MPD_NOT_CONNECTED;
610 return mi->status->crossfade;
613 int mpd_status_set_crossfade(MpdObj *mi,int crossfade_time)
615 if(!mpd_check_connected(mi))
617 debug_printf(DEBUG_WARNING,"not connected\n");
618 return MPD_NOT_CONNECTED;
620 if(mpd_lock_conn(mi))
622 debug_printf(DEBUG_ERROR,"lock failed\n");
623 return MPD_LOCK_FAILED;
625 mpd_sendCrossfadeCommand(mi->connection, crossfade_time);
626 mpd_finishCommand(mi->connection);
628 mpd_unlock_conn(mi);
629 mpd_status_queue_update(mi);
630 return MPD_OK;
634 float mpd_status_set_volume_as_float(MpdObj *mi, float fvol)
636 int volume = mpd_status_set_volume(mi, (int)(fvol*100.0));
637 if(volume > -1)
639 return (float)volume/100.0;
641 return (float)volume;
644 int mpd_stats_update(MpdObj *mi)
646 return mpd_stats_update_real(mi, NULL);
649 int mpd_stats_update_real(MpdObj *mi, ChangedStatusType* what_changed)
651 ChangedStatusType what_changed_here = 0;
652 if ( what_changed == NULL ) {
653 /* we need to save the current state, because we're called standalone */
654 memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));
657 if(!mpd_check_connected(mi))
659 debug_printf(DEBUG_INFO,"not connected\n");
660 return MPD_NOT_CONNECTED;
662 if(mpd_lock_conn(mi))
664 debug_printf(DEBUG_ERROR,"lock failed\n");
665 return MPD_LOCK_FAILED;
668 if(mi->stats != NULL)
670 mpd_freeStats(mi->stats);
672 mpd_sendStatsCommand(mi->connection);
673 mi->stats = mpd_getStats(mi->connection);
674 if(mi->stats == NULL)
676 debug_printf(DEBUG_ERROR,"Failed to grab stats from mpd\n");
678 else if(mi->stats->dbUpdateTime != mi->OldState.dbUpdateTime)
680 debug_printf(DEBUG_INFO, "database updated\n");
681 what_changed_here |= MPD_CST_DATABASE;
683 mi->CurrentState.dbUpdateTime = mi->stats->dbUpdateTime;
686 if (what_changed) {
687 (*what_changed) |= what_changed_here;
688 } else {
689 if((mi->the_status_changed_callback != NULL) & what_changed_here)
691 mi->the_status_changed_callback(mi, what_changed_here, mi->the_status_changed_signal_userdata);
695 if(mpd_unlock_conn(mi))
697 debug_printf(DEBUG_ERROR, "unlock failed");
698 return MPD_LOCK_FAILED;
700 return MPD_OK;
704 int mpd_stats_check(MpdObj *mi)
706 if(!mpd_check_connected(mi))
708 debug_printf(DEBUG_WARNING,"not connected\n");
709 return MPD_NOT_CONNECTED;
711 if(mi->stats == NULL)
713 /* try to update */
714 if(mpd_stats_update(mi))
716 debug_printf(DEBUG_ERROR,"failed to update status\n");
717 return MPD_STATUS_FAILED;
720 return MPD_OK;