int main (int argc, char **argv) { GMainLoop *l = NULL; g_type_init_with_debug_flags(G_TYPE_DEBUG_MASK); if(!g_thread_supported()) g_thread_init(NULL); l = g_main_loop_new(NULL, TRUE); mpd_Song *song = mpd_newSong(); song->artist = g_strdup("Eric Clapton"); song->album = g_strdup("Unplugged"); discogs_plugin.metadata->get_metadata(song, META_ALBUM_ART, callback, l); discogs_plugin.metadata->get_metadata(song, META_ARTIST_ART, callback, l); mpd_freeSong(song); song = mpd_newSong(); song->artist = g_strdup("Appnottoaf"); song->album = g_strdup("grabba"); discogs_plugin.metadata->get_metadata(song, META_ALBUM_ART, callback, l); discogs_plugin.metadata->get_metadata(song, META_ARTIST_ART, callback, l); mpd_freeSong(song); g_main_loop_run(l); gmpc_easy_async_quit(); g_main_loop_unref(l); xmlCleanupParser(); return EXIT_SUCCESS; }
mpd_Song * mpd_playlist_get_current_song(MpdObj *mi) { if(!mpd_check_connected(mi)) { debug_printf(DEBUG_WARNING, "Not Connected\n"); return NULL; } if(mpd_status_check(mi) != MPD_OK) { debug_printf(DEBUG_ERROR, "Failed to check status\n"); return NULL; } if(mi->CurrentSong != NULL && mi->CurrentSong->id != mi->status->songid) { debug_printf(DEBUG_WARNING, "Current song not up2date, updating\n"); mpd_freeSong(mi->CurrentSong); mi->CurrentSong = NULL; } /* only update song when playing/pasing */ if(mi->CurrentSong == NULL && (mpd_player_get_state(mi) != MPD_PLAYER_STOP && mpd_player_get_state(mi) != MPD_PLAYER_UNKNOWN)) { /* TODO: this to use the geT_current_song_id function */ mi->CurrentSong = mpd_playlist_get_song(mi, mpd_player_get_current_song_id(mi)); if(mi->CurrentSong == NULL) { debug_printf(DEBUG_ERROR, "Failed to grab song\n"); return NULL; } } return mi->CurrentSong; }
void mpd_data_free(MpdData *data) { MpdData_real *data_real,*temp; if(data == NULL) { debug_printf(DEBUG_ERROR, "data != NULL Failed"); return; } data_real = (MpdData_real *)mpd_data_get_first(data); while(data_real){ temp = data_real; if (data_real->type == MPD_DATA_TYPE_SONG) { if(data_real->song) mpd_freeSong(data_real->song); } else if (data_real->type == MPD_DATA_TYPE_OUTPUT_DEV) { mpd_freeOutputElement(data_real->output_dev); } else if(data_real->type == MPD_DATA_TYPE_DIRECTORY) { if(data_real->directory)free(data_real->directory); } else if(data_real->type == MPD_DATA_TYPE_PLAYLIST) { if(data_real->playlist)free(data_real->playlist); } else { free((void*)(data_real->tag)); } data_real = data_real->next; free(temp); } }
void mpd_finishInfoEntity(mpd_InfoEntity * entity) { if(entity->info.directory) { if(entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) { mpd_freeDirectory(entity->info.directory); } else if(entity->type == MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeSong(entity->info.song); } else if(entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE) { mpd_freePlaylistFile(entity->info.playlistFile); } } }
void MpdClient::updatePlaylist(long long version) { if (version < 0) mpd_sendPlChangesCommand(conn, version); else mpd_sendPlaylistInfoCommand(conn, -1); mpd_InfoEntity *entity; while ((entity = mpd_getNextInfoEntity(conn))) { assert(entity->type == MPD_INFO_ENTITY_TYPE_SONG); mpd_Song *song = mpd_songDup(entity->info.song); mpd_freeInfoEntity(entity); assert(song->pos <= playlist.size()); if (song->pos == playlist.size()) playlist += song; else { mpd_freeSong(playlist[song->pos]); playlist[song->pos] = song; } emit(changedSong(song)); } mpd_finishCommand(conn); /* remove extra songs if the playlist was shortened */ for (int i = playlist.size() - 1; i >= status->playlistLength; i--) { mpd_freeSong(playlist[i]); playlist.removeAt(i); emit(changedSong(NULL)); } }
void mpd_free(MpdObj *mi) { debug_printf(DEBUG_INFO, "destroying MpdObj object\n"); if(mi->connected) { /* disconnect */ debug_printf(DEBUG_WARNING, "Connection still running, disconnecting\n"); mpd_disconnect(mi); } if(mi->hostname) { free(mi->hostname); } if(mi->password) { free(mi->password); } if(mi->error_msg) { free(mi->error_msg); } if(mi->connection) { /* obsolete */ mpd_closeConnection(mi->connection); } if(mi->status) { mpd_freeStatus(mi->status); } if(mi->stats) { mpd_freeStats(mi->stats); } if(mi->CurrentSong) { mpd_freeSong(mi->CurrentSong); } mpd_free_queue_ob(mi); mpd_server_free_commands(mi); free(mi); }
int mpd_status_update(MpdObj *mi) { ChangedStatusType what_changed=0; if(!mpd_check_connected(mi)) { debug_printf(DEBUG_INFO,"not connected\n"); return MPD_NOT_CONNECTED; } if(mpd_lock_conn(mi)) { debug_printf(DEBUG_ERROR,"lock failed\n"); return MPD_LOCK_FAILED; } if(mi->status != NULL) { mpd_freeStatus(mi->status); mi->status = NULL; } mpd_sendStatusCommand(mi->connection); mi->status = mpd_getStatus(mi->connection); if(mi->status == NULL) { debug_printf(DEBUG_ERROR,"Failed to grab status from mpd\n"); mpd_unlock_conn(mi); return MPD_STATUS_FAILED; } if(mpd_unlock_conn(mi)) { debug_printf(DEBUG_ERROR, "Failed to unlock"); return MPD_LOCK_FAILED; } /* * check for changes */ /* first save the old status */ memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState)); /* playlist change */ if(mi->CurrentState.playlistid != mi->status->playlist) { /* print debug message */ debug_printf(DEBUG_INFO, "Playlist has changed!"); /* We can't trust the current song anymore. so we remove it */ /* tags might have been updated */ if(mi->CurrentSong != NULL) { mpd_freeSong(mi->CurrentSong); mi->CurrentSong = NULL; } /* set MPD_CST_PLAYLIST to be changed */ what_changed |= MPD_CST_PLAYLIST; if(mi->CurrentState.playlistLength == mi->status->playlistLength) { // what_changed |= MPD_CST_SONGID; } /* save new id */ mi->CurrentState.playlistid = mi->status->playlist; } if(mi->CurrentState.storedplaylistid != mi->status->storedplaylist) { /* set MPD_CST_QUEUE to be changed */ what_changed |= MPD_CST_STORED_PLAYLIST; /* save new id */ mi->CurrentState.storedplaylistid = mi->status->storedplaylist; } /* state change */ if(mi->CurrentState.state != mi->status->state) { what_changed |= MPD_CST_STATE; mi->CurrentState.state = mi->status->state; } if(mi->CurrentState.songid != mi->status->songid) { /* print debug message */ debug_printf(DEBUG_INFO, "Songid has changed %i %i!", mi->OldState.songid, mi->status->songid); what_changed |= MPD_CST_SONGID; /* save new songid */ mi->CurrentState.songid = mi->status->songid; } if(mi->CurrentState.songpos != mi->status->song) { /* print debug message */ debug_printf(DEBUG_INFO, "Songpos has changed %i %i!", mi->OldState.songpos, mi->status->song); what_changed |= MPD_CST_SONGPOS; /* save new songid */ mi->CurrentState.songpos = mi->status->song; } if(mi->CurrentState.nextsongid != mi->status->nextsongid || mi->CurrentState.nextsongpos != mi->status->nextsong) { what_changed |= MPD_CST_NEXTSONG; /* save new songid */ mi->CurrentState.nextsongpos = mi->status->nextsong; mi->CurrentState.nextsongid = mi->status->nextsongid; } if(mi->CurrentState.single != mi->status->single) { what_changed |= MPD_CST_SINGLE_MODE; mi->CurrentState.single = mi->status->single; } if(mi->CurrentState.consume != mi->status->consume) { what_changed |= MPD_CST_CONSUME_MODE; mi->CurrentState.consume = mi->status->consume; } if(mi->CurrentState.repeat != mi->status->repeat) { what_changed |= MPD_CST_REPEAT; mi->CurrentState.repeat = mi->status->repeat; } if(mi->CurrentState.random != mi->status->random) { what_changed |= MPD_CST_RANDOM; mi->CurrentState.random = mi->status->random; } if(mi->CurrentState.volume != mi->status->volume) { what_changed |= MPD_CST_VOLUME; mi->CurrentState.volume = mi->status->volume; } if(mi->CurrentState.xfade != mi->status->crossfade) { what_changed |= MPD_CST_CROSSFADE; mi->CurrentState.xfade = mi->status->crossfade; } if(mi->CurrentState.totaltime != mi->status->totalTime) { what_changed |= MPD_CST_TOTAL_TIME; mi->CurrentState.totaltime = mi->status->totalTime; } if(mi->CurrentState.elapsedtime != mi->status->elapsedTime) { what_changed |= MPD_CST_ELAPSED_TIME; mi->CurrentState.elapsedtime = mi->status->elapsedTime; } /* Check if bitrate changed, happens with vbr encodings. */ if(mi->CurrentState.bitrate != mi->status->bitRate) { what_changed |= MPD_CST_BITRATE; mi->CurrentState.bitrate = mi->status->bitRate; } /* The following 3 probly only happen on a song change, or is it possible in one song/stream? */ /* Check if the sample rate changed */ if(mi->CurrentState.samplerate != mi->status->sampleRate) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.samplerate = mi->status->sampleRate; } /* check if the sampling depth changed */ if(mi->CurrentState.bits != mi->status->bits) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.bits = mi->status->bits; } /* Check if the amount of audio channels changed */ if(mi->CurrentState.channels != mi->status->channels) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.channels = mi->status->channels; } if(mi->status->error) { what_changed |= MPD_CST_SERVER_ERROR; strcpy(mi->CurrentState.error,mi->status->error); mpd_sendClearErrorCommand(mi->connection); mpd_finishCommand(mi->connection); } else { mi->CurrentState.error[0] ='\0'; } /* Check if the updating changed, * If it stopped, also update the stats for the new db-time. */ if(mi->CurrentState.updatingDb != mi->status->updatingDb ) { what_changed |= MPD_CST_UPDATING; if(!mi->status->updatingDb) { mpd_stats_update_real(mi, &what_changed); } mi->CurrentState.updatingDb = mi->status->updatingDb; } mi->CurrentState.playlistLength = mi->status->playlistLength; /* Detect changed outputs */ if(!mi->has_idle) { if(mi->num_outputs >0 ) { mpd_OutputEntity *output = NULL; mpd_sendOutputsCommand(mi->connection); while (( output = mpd_getNextOutput(mi->connection)) != NULL) { if(mi->num_outputs < output->id) { mi->num_outputs++; mi->output_states = realloc(mi->output_states,mi->num_outputs*sizeof(int)); mi->output_states[mi->num_outputs] = output->enabled; what_changed |= MPD_CST_OUTPUT; } if(mi->output_states[output->id] != output->enabled) { mi->output_states[output->id] = output->enabled; what_changed |= MPD_CST_OUTPUT; } mpd_freeOutputElement(output); } mpd_finishCommand(mi->connection); } else { /* if no outputs, lets fetch them */ mpd_server_update_outputs(mi); if(mi->num_outputs == 0) { assert("No outputs defined? that cannot be\n"); } what_changed |= MPD_CST_OUTPUT; } }else { char *name; int update_stats = 0; mpd_sendGetEventsCommand(mi->connection); while((name = mpd_getNextEvent(mi->connection))){ if(strcmp(name, "output") == 0){ what_changed |= MPD_CST_OUTPUT; }else if (strcmp(name, "database") == 0) { if((what_changed&MPD_CST_DATABASE) == 0) { update_stats = 1; } what_changed |= MPD_CST_DATABASE; }else if (strcmp(name, "stored_playlist")==0) { what_changed |= MPD_CST_STORED_PLAYLIST; }else if (strcmp(name, "tag") == 0) { what_changed |= MPD_CST_PLAYLIST; }else if (strcmp (name, "sticker") == 0) { what_changed |= MPD_CST_STICKER; } free(name); } mpd_finishCommand(mi->connection); if(update_stats) { mpd_stats_update_real(mi, &what_changed); } } /* Run the callback */ if((mi->the_status_changed_callback != NULL) && what_changed) { mi->the_status_changed_callback( mi, what_changed, mi->the_status_changed_signal_userdata ); } /* We could have lost connection again during signal handling... so before we return check again if we are connected */ if(!mpd_check_connected(mi)) { return MPD_NOT_CONNECTED; } return MPD_OK; }
int mpd_status_update(MpdObj *mi) { ChangedStatusType what_changed=0; if(!mpd_check_connected(mi)) { debug_printf(DEBUG_INFO,"Where not connected\n"); return MPD_NOT_CONNECTED; } if(mpd_lock_conn(mi)) { debug_printf(DEBUG_ERROR,"lock failed\n"); return MPD_LOCK_FAILED; } if(mi->status != NULL) { mpd_freeStatus(mi->status); mi->status = NULL; } mpd_sendStatusCommand(mi->connection); mi->status = mpd_getStatus(mi->connection); if(mi->status == NULL) { debug_printf(DEBUG_ERROR,"Failed to grab status from mpd\n"); mpd_unlock_conn(mi); return MPD_STATUS_FAILED; } if(mpd_unlock_conn(mi)) { debug_printf(DEBUG_ERROR, "Failed to unlock"); return MPD_LOCK_FAILED; } /* * check for changes */ /* first save the old status */ memcpy(&(mi->OldState), &(mi->CurrentState), sizeof(MpdServerState)); /* playlist change */ if(mi->CurrentState.playlistid != mi->status->playlist) { /* print debug message */ debug_printf(DEBUG_INFO, "Playlist has changed!"); /* We can't trust the current song anymore. so we remove it */ /* tags might have been updated */ if(mi->CurrentSong != NULL) { mpd_freeSong(mi->CurrentSong); mi->CurrentSong = NULL; } /* set MPD_CST_PLAYLIST to be changed */ what_changed |= MPD_CST_PLAYLIST; /* save new id */ mi->CurrentState.playlistid = mi->status->playlist; } /* state change */ if(mi->CurrentState.state != mi->status->state) { what_changed |= MPD_CST_STATE; mi->CurrentState.state = mi->status->state; } if(mi->CurrentState.songid != mi->status->songid) { /* print debug message */ debug_printf(DEBUG_INFO, "Song has changed %i %i!", mi->OldState.songid, mi->status->songid); what_changed |= MPD_CST_SONGID; /* save new songid */ mi->CurrentState.songid = mi->status->songid; } if(mi->CurrentState.songpos != mi->status->song) { /* print debug message */ debug_printf(DEBUG_INFO, "Song has changed %i %i!", mi->OldState.songpos, mi->status->song); what_changed |= MPD_CST_SONGPOS; /* save new songid */ mi->CurrentState.songpos = mi->status->song; } if(mi->CurrentState.repeat != mi->status->repeat) { what_changed |= MPD_CST_REPEAT; mi->CurrentState.repeat = mi->status->repeat; } if(mi->CurrentState.random != mi->status->random) { what_changed |= MPD_CST_RANDOM; mi->CurrentState.random = mi->status->random; } if(mi->CurrentState.volume != mi->status->volume) { what_changed |= MPD_CST_VOLUME; mi->CurrentState.volume = mi->status->volume; } if(mi->CurrentState.xfade != mi->status->crossfade) { what_changed |= MPD_CST_CROSSFADE; mi->CurrentState.xfade = mi->status->crossfade; } if(mi->CurrentState.totaltime != mi->status->totalTime) { what_changed |= MPD_CST_TOTAL_TIME; mi->CurrentState.totaltime = mi->status->totalTime; } if(mi->CurrentState.elapsedtime != mi->status->elapsedTime) { what_changed |= MPD_CST_ELAPSED_TIME; mi->CurrentState.elapsedtime = mi->status->elapsedTime; } /* Check if bitrate changed, happens with vbr encodings. */ if(mi->CurrentState.bitrate != mi->status->bitRate) { what_changed |= MPD_CST_BITRATE; mi->CurrentState.bitrate = mi->status->bitRate; } /* The following 3 probly only happen on a song change, or is it possible in one song/stream? */ /* Check if the sample rate changed */ if(mi->CurrentState.samplerate != mi->status->sampleRate) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.samplerate = mi->status->sampleRate; } /* check if the sampling depth changed */ if(mi->CurrentState.bits != mi->status->bits) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.bits = mi->status->bits; } /* Check if the amount of audio channels changed */ if(mi->CurrentState.channels != mi->status->channels) { what_changed |= MPD_CST_AUDIOFORMAT; mi->CurrentState.channels = mi->status->channels; } /* Check if the updating changed, * If it stopped, also update the stats for the new db-time. */ if(mi->CurrentState.updatingDb != mi->status->updatingDb ) { what_changed |= MPD_CST_UPDATING; if(!mi->status->updatingDb) { mpd_stats_update_real(mi, &what_changed); } mi->CurrentState.updatingDb = mi->status->updatingDb; } /* Run the callback */ if((mi->the_status_changed_callback != NULL) && what_changed) { mi->the_status_changed_callback( mi, what_changed, mi->the_status_changed_signal_userdata ); } /* We could have lost connection again during signal handling... so before we return check again if we are connected */ if(!mpd_check_connected(mi)) { return MPD_NOT_CONNECTED; } return MPD_OK; }
int mpd_disconnect(MpdObj *mi) { /* lock */ mpd_lock_conn(mi); debug_printf(DEBUG_INFO, "disconnecting\n"); if(mi->connection) { mpd_closeConnection(mi->connection); mi->connection = NULL; } if(mi->status) { mpd_freeStatus(mi->status); mi->status = NULL; } if(mi->stats) { mpd_freeStats(mi->stats); mi->stats = NULL; } if(mi->CurrentSong) { mpd_freeSong(mi->CurrentSong); mi->CurrentSong = NULL; } mi->CurrentState.playlistid = -1; mi->CurrentState.queueid = -2; mi->CurrentState.storedplaylistid = -1; mi->CurrentState.state = -1; mi->CurrentState.songid = -1; mi->CurrentState.songpos = -1; mi->CurrentState.dbUpdateTime = 0; mi->CurrentState.updatingDb = 0; mi->CurrentState.repeat = -1; mi->CurrentState.random = -1; mi->CurrentState.volume = -2; mi->CurrentState.xfade = -1; mi->CurrentState.totaltime = 0; mi->CurrentState.elapsedtime = 0; mi->CurrentState.bitrate = 0; mi->CurrentState.samplerate = 0; mi->CurrentState.channels = 0; mi->CurrentState.bits = 0; mi->CurrentState.playlistLength = 0; mi->CurrentState.error[0] = '\0'; /* search stuff */ mi->search_type = MPD_SEARCH_TYPE_NONE; /* no need to initialize, but set it to anything anyway*/ mi->search_field = MPD_TAG_ITEM_ARTIST; memcpy(&(mi->OldState), &(mi->CurrentState) , sizeof(MpdServerState)); mpd_free_queue_ob(mi); mpd_server_free_commands(mi); /*don't reset errors */ /* Remove this signal, we don't actually disconnect */ if(mi->connected) { /* set disconnect flag */ mi->connected = FALSE; if(mi->the_connection_changed_callback != NULL) { mi->the_connection_changed_callback( mi, FALSE, mi->the_connection_changed_signal_userdata ); } } debug_printf(DEBUG_INFO, "Disconnect completed\n"); return MPD_OK; }