Update libtool version by 1 age (added functions)
[libmpd.git] / src / libmpd-status.c
blob65773dd27cfc8e8d6b11ad9272118183d7f46870
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;
154 if(mi->CurrentState.nextsongid != mi->status->nextsongid || mi->CurrentState.nextsongpos != mi->status->nextsong)
156 what_changed |= MPD_CST_NEXTSONG;
157 /* save new songid */
158 mi->CurrentState.nextsongpos = mi->status->nextsong;
159 mi->CurrentState.nextsongid = mi->status->nextsongid;
162 if(mi->CurrentState.repeat != mi->status->repeat)
164 what_changed |= MPD_CST_REPEAT;
165 mi->CurrentState.repeat = mi->status->repeat;
167 if(mi->CurrentState.random != mi->status->random)
169 what_changed |= MPD_CST_RANDOM;
170 mi->CurrentState.random = mi->status->random;
172 if(mi->CurrentState.volume != mi->status->volume)
174 what_changed |= MPD_CST_VOLUME;
175 mi->CurrentState.volume = mi->status->volume;
177 if(mi->CurrentState.xfade != mi->status->crossfade)
179 what_changed |= MPD_CST_CROSSFADE;
180 mi->CurrentState.xfade = mi->status->crossfade;
182 if(mi->CurrentState.totaltime != mi->status->totalTime)
184 what_changed |= MPD_CST_TOTAL_TIME;
185 mi->CurrentState.totaltime = mi->status->totalTime;
187 if(mi->CurrentState.elapsedtime != mi->status->elapsedTime)
189 what_changed |= MPD_CST_ELAPSED_TIME;
190 mi->CurrentState.elapsedtime = mi->status->elapsedTime;
193 /* Check if bitrate changed, happens with vbr encodings. */
194 if(mi->CurrentState.bitrate != mi->status->bitRate)
196 what_changed |= MPD_CST_BITRATE;
197 mi->CurrentState.bitrate = mi->status->bitRate;
200 /* The following 3 probly only happen on a song change, or is it possible in one song/stream? */
201 /* Check if the sample rate changed */
202 if(mi->CurrentState.samplerate != mi->status->sampleRate)
204 what_changed |= MPD_CST_AUDIOFORMAT;
205 mi->CurrentState.samplerate = mi->status->sampleRate;
208 /* check if the sampling depth changed */
209 if(mi->CurrentState.bits != mi->status->bits)
211 what_changed |= MPD_CST_AUDIOFORMAT;
212 mi->CurrentState.bits = mi->status->bits;
215 /* Check if the amount of audio channels changed */
216 if(mi->CurrentState.channels != mi->status->channels)
218 what_changed |= MPD_CST_AUDIOFORMAT;
219 mi->CurrentState.channels = mi->status->channels;
222 if(mi->status->error)
224 what_changed |= MPD_CST_SERVER_ERROR;
225 strcpy(mi->CurrentState.error,mi->status->error);
226 mpd_sendClearErrorCommand(mi->connection);
227 mpd_finishCommand(mi->connection);
229 else
231 mi->CurrentState.error[0] ='\0';
234 /* Check if the updating changed,
235 * If it stopped, also update the stats for the new db-time.
237 if(mi->CurrentState.updatingDb != mi->status->updatingDb )
239 what_changed |= MPD_CST_UPDATING;
240 if(!mi->status->updatingDb)
242 mpd_stats_update_real(mi, &what_changed);
244 mi->CurrentState.updatingDb = mi->status->updatingDb;
248 mi->CurrentState.playlistLength = mi->status->playlistLength;
251 /* Detect changed outputs */
252 if(!mi->has_idle)
254 if(mi->num_outputs >0 )
256 mpd_OutputEntity *output = NULL;
257 mpd_sendOutputsCommand(mi->connection);
258 while (( output = mpd_getNextOutput(mi->connection)) != NULL)
260 if(mi->output_states[output->id] != output->enabled)
262 mi->output_states[output->id] = output->enabled;
263 what_changed |= MPD_CST_OUTPUT;
265 mpd_freeOutputElement(output);
267 mpd_finishCommand(mi->connection);
269 else
271 /* if no outputs, lets fetch them */
272 mpd_server_update_outputs(mi);
273 if(mi->num_outputs == 0)
275 assert("No outputs defined? that cannot be\n");
277 what_changed |= MPD_CST_OUTPUT;
279 }else {
280 char *name;
281 int update_stats = 0;
282 mpd_sendGetEventsCommand(mi->connection);
283 while((name = mpd_getNextEvent(mi->connection))){
284 if(strcmp(name, "output") == 0){
285 what_changed |= MPD_CST_OUTPUT;
286 }else if (strcmp(name, "database") == 0) {
287 if((what_changed&MPD_CST_DATABASE) == 0)
289 update_stats = 1;
291 what_changed |= MPD_CST_DATABASE;
292 }else if (strcmp(name, "stored_playlist")==0) {
293 what_changed |= MPD_CST_STORED_PLAYLIST;
294 }else if (strcmp(name, "tag") == 0) {
295 what_changed |= MPD_CST_PLAYLIST;
296 }else if (strcmp (name, "sticker") == 0) {
297 what_changed |= MPD_CST_STICKER;
299 free(name);
301 mpd_finishCommand(mi->connection);
302 if(update_stats) {
303 mpd_stats_update_real(mi, &what_changed);
308 /* Run the callback */
309 if((mi->the_status_changed_callback != NULL) && what_changed)
311 mi->the_status_changed_callback( mi, what_changed, mi->the_status_changed_signal_userdata );
314 /* We could have lost connection again during signal handling... so before we return check again if we are connected */
315 if(!mpd_check_connected(mi))
317 return MPD_NOT_CONNECTED;
319 return MPD_OK;
322 /* returns TRUE when status is availible, when not availible and connected it tries to grab it */
323 int mpd_status_check(MpdObj *mi)
325 if(!mpd_check_connected(mi))
327 debug_printf(DEBUG_INFO,"not connected\n");
328 return MPD_NOT_CONNECTED;
330 if(mi->status == NULL)
332 /* try to update */
333 if(mpd_status_update(mi))
335 debug_printf(DEBUG_INFO, "failed to update status\n");
336 return MPD_STATUS_FAILED;
339 return MPD_OK;
343 int mpd_stats_get_total_songs(MpdObj *mi)
345 if(mi == NULL)
347 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
348 return MPD_ARGS_ERROR;
350 if(mpd_stats_check(mi) != MPD_OK)
352 debug_printf(DEBUG_ERROR,"Failed to get status\n");
353 return MPD_STATUS_FAILED;
355 return mi->stats->numberOfSongs;
358 int mpd_stats_get_total_artists(MpdObj *mi)
360 if(mi == NULL)
362 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
363 return MPD_ARGS_ERROR;
365 if(mpd_stats_check(mi) != MPD_OK)
367 debug_printf(DEBUG_ERROR,"Failed to get status\n");
368 return MPD_STATS_FAILED;
370 return mi->stats->numberOfArtists;
373 int mpd_stats_get_total_albums(MpdObj *mi)
375 if(mi == NULL)
377 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
378 return MPD_ARGS_ERROR;
380 if(mpd_stats_check(mi) != MPD_OK)
382 debug_printf(DEBUG_WARNING,"Failed to get status\n");
383 return MPD_STATS_FAILED;
385 return mi->stats->numberOfAlbums;
389 int mpd_stats_get_uptime(MpdObj *mi)
391 if(mi == NULL)
393 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
394 return MPD_ARGS_ERROR;
396 if(mpd_stats_check(mi) != MPD_OK)
398 debug_printf(DEBUG_WARNING,"Failed to get status\n");
399 return MPD_STATS_FAILED;
401 return mi->stats->uptime;
404 int mpd_stats_get_playtime(MpdObj *mi)
406 if(mi == NULL)
408 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
409 return MPD_ARGS_ERROR;
411 if(mpd_stats_check(mi) != MPD_OK)
413 debug_printf(DEBUG_WARNING,"Failed to get status\n");
414 return MPD_STATS_FAILED;
416 return mi->stats->playTime;
419 int mpd_stats_get_db_playtime(MpdObj *mi)
421 if(mi == NULL)
423 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
424 return MPD_ARGS_ERROR;
426 if(mpd_stats_check(mi) != MPD_OK)
428 debug_printf(DEBUG_WARNING,"Failed to get stats\n");
429 return MPD_STATS_FAILED;
431 return mi->stats->dbPlayTime;
450 int mpd_status_get_volume(MpdObj *mi)
452 if(mi == NULL)
454 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
455 return MPD_ARGS_ERROR;
457 if(mpd_status_check(mi) != MPD_OK)
459 debug_printf(DEBUG_WARNING, "Failed to get status\n");
460 return MPD_STATUS_FAILED;
462 return mi->status->volume;
466 int mpd_status_get_bitrate(MpdObj *mi)
468 if(mi == NULL)
470 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
471 return MPD_ARGS_ERROR;
473 if(mpd_status_check(mi) != MPD_OK)
475 debug_printf(DEBUG_WARNING, "Failed to get status\n");
476 return MPD_STATUS_FAILED;
478 return mi->CurrentState.bitrate;
481 int mpd_status_get_channels(MpdObj *mi)
483 if(mi == NULL)
485 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
486 return MPD_ARGS_ERROR;
488 if(mpd_status_check(mi) != MPD_OK)
490 debug_printf(DEBUG_WARNING, "Failed to get status\n");
491 return MPD_STATUS_FAILED;
493 return mi->CurrentState.channels;
496 unsigned int mpd_status_get_samplerate(MpdObj *mi)
498 if(mi == NULL)
500 debug_printf(DEBUG_ERROR,"failed to check mi == NULL\n");
501 return MPD_ARGS_ERROR;
503 if(mpd_status_check(mi) != MPD_OK)
505 debug_printf(DEBUG_WARNING, "Failed to get status\n");
506 return MPD_STATUS_FAILED;
508 return mi->CurrentState.samplerate;
511 int mpd_status_get_bits(MpdObj *mi)
513 if(mi == NULL)
515 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
516 return MPD_ARGS_ERROR;
518 if(mpd_status_check(mi) != MPD_OK)
520 debug_printf(DEBUG_WARNING, "Failed to get status\n");
521 return MPD_STATUS_FAILED;
523 return mi->CurrentState.bits;
526 char * mpd_status_get_mpd_error(MpdObj *mi)
528 if(mi->CurrentState.error[0] != '\0')
530 return strdup(mi->CurrentState.error);
532 return NULL;
535 int mpd_status_db_is_updating(MpdObj *mi)
537 if(!mpd_check_connected(mi))
539 debug_printf(DEBUG_WARNING, "mpd_check_connected failed.\n");
540 return FALSE;
542 return mi->CurrentState.updatingDb;
546 int mpd_status_get_total_song_time(MpdObj *mi)
548 if(!mpd_check_connected(mi))
550 debug_printf(DEBUG_ERROR, "failed to check mi == NULL\n");
551 return MPD_ARGS_ERROR;
553 if(mpd_status_check(mi) != MPD_OK)
555 debug_printf(DEBUG_WARNING, "Failed to get status\n");
556 return MPD_STATUS_FAILED;
558 return mi->status->totalTime;
562 int mpd_status_get_elapsed_song_time(MpdObj *mi)
564 if(!mpd_check_connected(mi))
566 debug_printf(DEBUG_WARNING,"failed to check mi == NULL\n");
567 return MPD_NOT_CONNECTED;
569 if(mpd_status_check(mi) != MPD_OK)
571 debug_printf(DEBUG_WARNING,"Failed to get status\n");
572 return MPD_STATUS_FAILED;
574 return mi->status->elapsedTime;
577 int mpd_status_set_volume(MpdObj *mi,int volume)
579 if(!mpd_check_connected(mi))
581 debug_printf(DEBUG_WARNING,"not connected\n");
582 return MPD_NOT_CONNECTED;
584 /* making sure volume is between 0 and 100 */
585 volume = (volume < 0)? 0:(volume>100)? 100:volume;
587 if(mpd_lock_conn(mi))
589 debug_printf(DEBUG_ERROR,"lock failed\n");
590 return MPD_LOCK_FAILED;
593 /* send the command */
594 mpd_sendSetvolCommand(mi->connection , volume);
595 mpd_finishCommand(mi->connection);
596 /* check for errors */
598 mpd_unlock_conn(mi);
599 /* update status, because we changed it */
600 mpd_status_queue_update(mi);
601 /* return current volume */
602 return mpd_status_get_volume(mi);
605 int mpd_status_get_crossfade(MpdObj *mi)
607 if(!mpd_check_connected(mi))
609 debug_printf(DEBUG_WARNING,"not connected\n");
610 return MPD_NOT_CONNECTED;
612 if(mpd_status_check(mi) != MPD_OK)
614 debug_printf(DEBUG_WARNING,"Failed grabbing status\n");
615 return MPD_NOT_CONNECTED;
617 return mi->status->crossfade;
620 int mpd_status_set_crossfade(MpdObj *mi,int crossfade_time)
622 if(!mpd_check_connected(mi))
624 debug_printf(DEBUG_WARNING,"not connected\n");
625 return MPD_NOT_CONNECTED;
627 if(mpd_lock_conn(mi))
629 debug_printf(DEBUG_ERROR,"lock failed\n");
630 return MPD_LOCK_FAILED;
632 mpd_sendCrossfadeCommand(mi->connection, crossfade_time);
633 mpd_finishCommand(mi->connection);
635 mpd_unlock_conn(mi);
636 mpd_status_queue_update(mi);
637 return MPD_OK;
641 float mpd_status_set_volume_as_float(MpdObj *mi, float fvol)
643 int volume = mpd_status_set_volume(mi, (int)(fvol*100.0));
644 if(volume > -1)
646 return (float)volume/100.0;
648 return (float)volume;
651 int mpd_stats_update(MpdObj *mi)
653 return mpd_stats_update_real(mi, NULL);
656 int mpd_stats_update_real(MpdObj *mi, ChangedStatusType* what_changed)
658 ChangedStatusType what_changed_here = 0;
659 if ( what_changed == NULL ) {
660 /* we need to save the current state, because we're called standalone */
661 memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState));
664 if(!mpd_check_connected(mi))
666 debug_printf(DEBUG_INFO,"not connected\n");
667 return MPD_NOT_CONNECTED;
669 if(mpd_lock_conn(mi))
671 debug_printf(DEBUG_ERROR,"lock failed\n");
672 return MPD_LOCK_FAILED;
675 if(mi->stats != NULL)
677 mpd_freeStats(mi->stats);
679 mpd_sendStatsCommand(mi->connection);
680 mi->stats = mpd_getStats(mi->connection);
681 if(mi->stats == NULL)
683 debug_printf(DEBUG_ERROR,"Failed to grab stats from mpd\n");
685 else if(mi->stats->dbUpdateTime != mi->OldState.dbUpdateTime)
687 debug_printf(DEBUG_INFO, "database updated\n");
688 what_changed_here |= MPD_CST_DATABASE;
690 mi->CurrentState.dbUpdateTime = mi->stats->dbUpdateTime;
693 if (what_changed) {
694 (*what_changed) |= what_changed_here;
695 } else {
696 if((mi->the_status_changed_callback != NULL) & what_changed_here)
698 mi->the_status_changed_callback(mi, what_changed_here, mi->the_status_changed_signal_userdata);
702 if(mpd_unlock_conn(mi))
704 debug_printf(DEBUG_ERROR, "unlock failed");
705 return MPD_LOCK_FAILED;
707 return MPD_OK;
711 int mpd_stats_check(MpdObj *mi)
713 if(!mpd_check_connected(mi))
715 debug_printf(DEBUG_WARNING,"not connected\n");
716 return MPD_NOT_CONNECTED;
718 if(mi->stats == NULL)
720 /* try to update */
721 if(mpd_stats_update(mi))
723 debug_printf(DEBUG_ERROR,"failed to update status\n");
724 return MPD_STATUS_FAILED;
727 return MPD_OK;