Force stats update when getting database event
[libmpd.git] / src / libmpd-status.c
blob3877da45021f562f1f0bb9335aa3d3e5d2c35864
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 mpd_sendGetEventsCommand(mi->connection);
275 while((name = mpd_getNextEvent(mi->connection))){
276 if(strcmp(name, "output") == 0){
277 what_changed |= MPD_CST_OUTPUT;
278 }else if (strcmp(name, "database") == 0) {
279 if((what_changed&MPD_CST_DATABASE) == 0)
281 mpd_stats_update_real(mi, &what_changed);
283 what_changed |= MPD_CST_DATABASE;
284 }else if (strcmp(name, "stored_playlist")==0) {
285 what_changed |= MPD_CST_STORED_PLAYLIST;
286 }else if (strcmp(name, "tag") == 0) {
287 what_changed |= MPD_CST_PLAYLIST;
288 }else if (strcmp (name, "sticker") == 0) {
289 what_changed |= MPD_CST_STICKER;
291 free(name);
293 mpd_finishCommand(mi->connection);
297 /* Run the callback */
298 if((mi->the_status_changed_callback != NULL) && what_changed)
300 mi->the_status_changed_callback( mi, what_changed, mi->the_status_changed_signal_userdata );
303 /* We could have lost connection again during signal handling... so before we return check again if we are connected */
304 if(!mpd_check_connected(mi))
306 return MPD_NOT_CONNECTED;
308 return MPD_OK;
311 /* returns TRUE when status is availible, when not availible and connected it tries to grab it */
312 int mpd_status_check(MpdObj *mi)
314 if(!mpd_check_connected(mi))
316 debug_printf(DEBUG_INFO,"not connected\n");
317 return MPD_NOT_CONNECTED;
319 if(mi->status == NULL)
321 /* try to update */
322 if(mpd_status_update(mi))
324 debug_printf(DEBUG_INFO, "failed to update status\n");
325 return MPD_STATUS_FAILED;
328 return MPD_OK;
332 int mpd_stats_get_total_songs(MpdObj *mi)
334 if(mi == NULL)
336 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
337 return MPD_ARGS_ERROR;
339 if(mpd_stats_check(mi) != MPD_OK)
341 debug_printf(DEBUG_ERROR,"Failed to get status\n");
342 return MPD_STATUS_FAILED;
344 return mi->stats->numberOfSongs;
347 int mpd_stats_get_total_artists(MpdObj *mi)
349 if(mi == NULL)
351 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
352 return MPD_ARGS_ERROR;
354 if(mpd_stats_check(mi) != MPD_OK)
356 debug_printf(DEBUG_ERROR,"Failed to get status\n");
357 return MPD_STATS_FAILED;
359 return mi->stats->numberOfArtists;
362 int mpd_stats_get_total_albums(MpdObj *mi)
364 if(mi == NULL)
366 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
367 return MPD_ARGS_ERROR;
369 if(mpd_stats_check(mi) != MPD_OK)
371 debug_printf(DEBUG_WARNING,"Failed to get status\n");
372 return MPD_STATS_FAILED;
374 return mi->stats->numberOfAlbums;
378 int mpd_stats_get_uptime(MpdObj *mi)
380 if(mi == NULL)
382 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
383 return MPD_ARGS_ERROR;
385 if(mpd_stats_check(mi) != MPD_OK)
387 debug_printf(DEBUG_WARNING,"Failed to get status\n");
388 return MPD_STATS_FAILED;
390 return mi->stats->uptime;
393 int mpd_stats_get_playtime(MpdObj *mi)
395 if(mi == NULL)
397 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
398 return MPD_ARGS_ERROR;
400 if(mpd_stats_check(mi) != MPD_OK)
402 debug_printf(DEBUG_WARNING,"Failed to get status\n");
403 return MPD_STATS_FAILED;
405 return mi->stats->playTime;
408 int mpd_stats_get_db_playtime(MpdObj *mi)
410 if(mi == NULL)
412 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
413 return MPD_ARGS_ERROR;
415 if(mpd_stats_check(mi) != MPD_OK)
417 debug_printf(DEBUG_WARNING,"Failed to get stats\n");
418 return MPD_STATS_FAILED;
420 return mi->stats->dbPlayTime;
439 int mpd_status_get_volume(MpdObj *mi)
441 if(mi == NULL)
443 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
444 return MPD_ARGS_ERROR;
446 if(mpd_status_check(mi) != MPD_OK)
448 debug_printf(DEBUG_WARNING, "Failed to get status\n");
449 return MPD_STATUS_FAILED;
451 return mi->status->volume;
455 int mpd_status_get_bitrate(MpdObj *mi)
457 if(mi == NULL)
459 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
460 return MPD_ARGS_ERROR;
462 if(mpd_status_check(mi) != MPD_OK)
464 debug_printf(DEBUG_WARNING, "Failed to get status\n");
465 return MPD_STATUS_FAILED;
467 return mi->CurrentState.bitrate;
470 int mpd_status_get_channels(MpdObj *mi)
472 if(mi == NULL)
474 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
475 return MPD_ARGS_ERROR;
477 if(mpd_status_check(mi) != MPD_OK)
479 debug_printf(DEBUG_WARNING, "Failed to get status\n");
480 return MPD_STATUS_FAILED;
482 return mi->CurrentState.channels;
485 unsigned int mpd_status_get_samplerate(MpdObj *mi)
487 if(mi == NULL)
489 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
490 return MPD_ARGS_ERROR;
492 if(mpd_status_check(mi) != MPD_OK)
494 debug_printf(DEBUG_WARNING, "Failed to get status\n");
495 return MPD_STATUS_FAILED;
497 return mi->CurrentState.samplerate;
500 int mpd_status_get_bits(MpdObj *mi)
502 if(mi == NULL)
504 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
505 return MPD_ARGS_ERROR;
507 if(mpd_status_check(mi) != MPD_OK)
509 debug_printf(DEBUG_WARNING, "Failed to get status\n");
510 return MPD_STATUS_FAILED;
512 return mi->CurrentState.bits;
515 char * mpd_status_get_mpd_error(MpdObj *mi)
517 if(mi->CurrentState.error[0] != '\0')
519 return strdup(mi->CurrentState.error);
521 return NULL;
524 int mpd_status_db_is_updating(MpdObj *mi)
526 if(!mpd_check_connected(mi))
528 debug_printf(DEBUG_WARNING, "mpd_check_connected failed.\n");
529 return FALSE;
531 return mi->CurrentState.updatingDb;
535 int mpd_status_get_total_song_time(MpdObj *mi)
537 if(!mpd_check_connected(mi))
539 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
540 return MPD_ARGS_ERROR;
542 if(mpd_status_check(mi) != MPD_OK)
544 debug_printf(DEBUG_WARNING, "Failed to get status\n");
545 return MPD_STATUS_FAILED;
547 return mi->status->totalTime;
551 int mpd_status_get_elapsed_song_time(MpdObj *mi)
553 if(!mpd_check_connected(mi))
555 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
556 return MPD_NOT_CONNECTED;
558 if(mpd_status_check(mi) != MPD_OK)
560 debug_printf(DEBUG_WARNING,"Failed to get status\n");
561 return MPD_STATUS_FAILED;
563 return mi->status->elapsedTime;
566 int mpd_status_set_volume(MpdObj *mi,int volume)
568 if(!mpd_check_connected(mi))
570 debug_printf(DEBUG_WARNING,"not connected\n");
571 return MPD_NOT_CONNECTED;
573 /* making sure volume is between 0 and 100 */
574 volume = (volume < 0)? 0:(volume>100)? 100:volume;
576 if(mpd_lock_conn(mi))
578 debug_printf(DEBUG_ERROR,"lock failed\n");
579 return MPD_LOCK_FAILED;
582 /* send the command */
583 mpd_sendSetvolCommand(mi->connection , volume);
584 mpd_finishCommand(mi->connection);
585 /* check for errors */
587 mpd_unlock_conn(mi);
588 /* update status, because we changed it */
589 mpd_status_queue_update(mi);
590 /* return current volume */
591 return mpd_status_get_volume(mi);
594 int mpd_status_get_crossfade(MpdObj *mi)
596 if(!mpd_check_connected(mi))
598 debug_printf(DEBUG_WARNING,"not connected\n");
599 return MPD_NOT_CONNECTED;
601 if(mpd_status_check(mi) != MPD_OK)
603 debug_printf(DEBUG_WARNING,"Failed grabbing status\n");
604 return MPD_NOT_CONNECTED;
606 return mi->status->crossfade;
609 int mpd_status_set_crossfade(MpdObj *mi,int crossfade_time)
611 if(!mpd_check_connected(mi))
613 debug_printf(DEBUG_WARNING,"not connected\n");
614 return MPD_NOT_CONNECTED;
616 if(mpd_lock_conn(mi))
618 debug_printf(DEBUG_ERROR,"lock failed\n");
619 return MPD_LOCK_FAILED;
621 mpd_sendCrossfadeCommand(mi->connection, crossfade_time);
622 mpd_finishCommand(mi->connection);
624 mpd_unlock_conn(mi);
625 mpd_status_queue_update(mi);
626 return MPD_OK;
630 float mpd_status_set_volume_as_float(MpdObj *mi, float fvol)
632 int volume = mpd_status_set_volume(mi, (int)(fvol*100.0));
633 if(volume > -1)
635 return (float)volume/100.0;
637 return (float)volume;
640 int mpd_stats_update(MpdObj *mi)
642 return mpd_stats_update_real(mi, NULL);
645 int mpd_stats_update_real(MpdObj *mi, ChangedStatusType* what_changed)
647 ChangedStatusType what_changed_here = 0;
648 if ( what_changed == NULL ) {
649 /* we need to save the current state, because we're called standalone */
650 memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));
653 if(!mpd_check_connected(mi))
655 debug_printf(DEBUG_INFO,"not connected\n");
656 return MPD_NOT_CONNECTED;
658 if(mpd_lock_conn(mi))
660 debug_printf(DEBUG_ERROR,"lock failed\n");
661 return MPD_LOCK_FAILED;
664 if(mi->stats != NULL)
666 mpd_freeStats(mi->stats);
668 mpd_sendStatsCommand(mi->connection);
669 mi->stats = mpd_getStats(mi->connection);
670 if(mi->stats == NULL)
672 debug_printf(DEBUG_ERROR,"Failed to grab stats from mpd\n");
674 else if(mi->stats->dbUpdateTime != mi->OldState.dbUpdateTime)
676 debug_printf(DEBUG_INFO, "database updated\n");
677 what_changed_here |= MPD_CST_DATABASE;
679 mi->CurrentState.dbUpdateTime = mi->stats->dbUpdateTime;
682 if (what_changed) {
683 (*what_changed) |= what_changed_here;
684 } else {
685 if((mi->the_status_changed_callback != NULL) & what_changed_here)
687 mi->the_status_changed_callback(mi, what_changed_here, mi->the_status_changed_signal_userdata);
691 if(mpd_unlock_conn(mi))
693 debug_printf(DEBUG_ERROR, "unlock failed");
694 return MPD_LOCK_FAILED;
696 return MPD_OK;
700 int mpd_stats_check(MpdObj *mi)
702 if(!mpd_check_connected(mi))
704 debug_printf(DEBUG_WARNING,"not connected\n");
705 return MPD_NOT_CONNECTED;
707 if(mi->stats == NULL)
709 /* try to update */
710 if(mpd_stats_update(mi))
712 debug_printf(DEBUG_ERROR,"failed to update status\n");
713 return MPD_STATUS_FAILED;
716 return MPD_OK;