diff --git a/src/mpdclient.c b/src/mpdclient.c
index 57b470681a82832af39143a22c8c8060988202bd..8449ca42b464be4bdf675cb7b9174dd34a144405 100644 (file)
--- a/src/mpdclient.c
+++ b/src/mpdclient.c
mpdclient_t *
mpdclient_new(void)
{
- mpdclient_t *c;
+ mpdclient_t *c;
- c = g_malloc0(sizeof(mpdclient_t));
+ c = g_malloc0(sizeof(mpdclient_t));
+ playlist_init(&c->playlist);
- return c;
+ return c;
}
mpdclient_t *
mpdclient_free(mpdclient_t *c)
{
- mpdclient_disconnect(c);
- g_list_free(c->error_callbacks);
- g_list_free(c->playlist_callbacks);
- g_list_free(c->browse_callbacks);
- g_free(c);
+ mpdclient_disconnect(c);
- return NULL;
+ mpdclient_playlist_free(&c->playlist);
+
+ g_list_free(c->error_callbacks);
+ g_list_free(c->playlist_callbacks);
+ g_list_free(c->browse_callbacks);
+ g_free(c);
+
+ return NULL;
}
gint
mpdclient_disconnect(mpdclient_t *c)
{
- if( c->connection )
- mpd_closeConnection(c->connection);
- c->connection = NULL;
+ if (c->connection)
+ mpd_closeConnection(c->connection);
+ c->connection = NULL;
- if( c->status )
- mpd_freeStatus(c->status);
- c->status = NULL;
+ if (c->status)
+ mpd_freeStatus(c->status);
+ c->status = NULL;
- if( c->playlist.list )
- mpdclient_playlist_free(&c->playlist);
+ playlist_clear(&c->playlist);
- if( c->song )
- c->song = NULL;
-
- return 0;
+ if (c->song)
+ c->song = NULL;
+
+ return 0;
}
gint
gint
mpdclient_update(mpdclient_t *c)
{
- gint retval = 0;
+ gint retval = 0;
- if( MPD_ERROR(c) )
- return -1;
+ if (MPD_ERROR(c))
+ return -1;
- /* free the old status */
- if( c->status )
- mpd_freeStatus(c->status);
-
- /* retreive new status */
- mpd_sendStatusCommand(c->connection);
- c->status = mpd_getStatus(c->connection);
- if( (retval=mpdclient_finish_command(c)) )
- return retval;
+ /* free the old status */
+ if (c->status)
+ mpd_freeStatus(c->status);
+
+ /* retreive new status */
+ mpd_sendStatusCommand(c->connection);
+ c->status = mpd_getStatus(c->connection);
+ if ((retval=mpdclient_finish_command(c)))
+ return retval;
#ifndef NDEBUG
- if( c->status->error )
- D("status> %s\n", c->status->error);
+ if (c->status->error)
+ D("status> %s\n", c->status->error);
#endif
- /* check if the playlist needs an update */
- if( c->playlist.id != c->status->playlist )
- {
- if( c->playlist.list )
- retval = mpdclient_playlist_update_changes(c);
- else
- retval = mpdclient_playlist_update(c);
- }
+ /* check if the playlist needs an update */
+ if (c->playlist.id != c->status->playlist) {
+ if (c->playlist.list)
+ retval = mpdclient_playlist_update_changes(c);
+ else
+ retval = mpdclient_playlist_update(c);
+ }
- /* update the current song */
- if( !c->song || c->status->songid != c->song->id )
- {
- c->song = playlist_get_song(c, c->status->song);
- }
+ /* update the current song */
+ if (!c->song || c->status->songid != c->song->id) {
+ c->song = playlist_get_song(c, c->status->song);
+ }
- c->need_update = FALSE;
+ c->need_update = FALSE;
- return retval;
+ return retval;
}
mpdclient_cmd_play(mpdclient_t *c, gint idx)
{
#ifdef ENABLE_SONG_ID
- mpd_Song *song = playlist_get_song(c, idx);
+ struct mpd_song *song = playlist_get_song(c, idx);
D("Play id:%d\n", song ? song->id : -1);
if (song)
}
gint
-mpdclient_cmd_add(mpdclient_t *c, mpd_Song *song)
+mpdclient_cmd_add(mpdclient_t *c, struct mpd_song *song)
{
gint retval = 0;
mpdclient_cmd_delete(mpdclient_t *c, gint idx)
{
gint retval = 0;
- mpd_Song *song = playlist_get_song(c, idx);
+ struct mpd_song *song = playlist_get_song(c, idx);
if( !song )
return -1;
c->playlist.id++;
/* remove the song from the playlist */
- c->playlist.list = g_list_remove(c->playlist.list, (gpointer) song);
- c->playlist.length = g_list_length(c->playlist.list);
+ g_array_remove_index(c->playlist.list, idx);
/* call playlist updated callback */
mpdclient_playlist_callback(c, PLAYLIST_EVENT_DELETE, (gpointer) song);
gint
mpdclient_cmd_move(mpdclient_t *c, gint old_index, gint new_index)
{
- gint n, index1, index2;
- GList *item1, *item2;
- gpointer data1, data2;
- mpd_Song *song1, *song2;
+ gint n;
+ struct mpd_song *song1, *song2;
if (old_index == new_index || new_index < 0 ||
- (guint)new_index >= c->playlist.length)
+ (guint)new_index >= c->playlist.list->len)
return -1;
song1 = playlist_get_song(c, old_index);
n = song1->pos;
song1->pos = song2->pos;
song2->pos = n;
- index1 = MIN(old_index, new_index);
- index2 = MAX(old_index, new_index);
- /* retreive the list items */
- item1 = g_list_nth(c->playlist.list, index1);
- item2 = g_list_nth(c->playlist.list, index2);
- /* retrieve the songs */
- data1 = item1->data;
- data2 = item2->data;
-
- /* move the second item */
- c->playlist.list = g_list_remove(c->playlist.list, data2);
- c->playlist.list = g_list_insert_before(c->playlist.list, item1, data2);
-
- /* move the first item */
- if (index2-index1 > 1) {
- item2 = g_list_nth(c->playlist.list, index2);
- c->playlist.list = g_list_remove(c->playlist.list, data1);
- c->playlist.list = g_list_insert_before(c->playlist.list,
- item2, data1);
- }
+ /* update the array */
+ g_array_index(c->playlist.list, struct mpd_song *, old_index) = song2;
+ g_array_index(c->playlist.list, struct mpd_song *, new_index) = song1;
/* increment the playlist id, so we dont retrives a new playlist */
c->playlist.id++;
/*** Playlist managment functions *******************************************/
/****************************************************************************/
-gint
-mpdclient_playlist_free(mpdclient_playlist_t *playlist)
-{
- GList *list = g_list_first(playlist->list);
-
- while(list)
- {
- mpd_Song *song = (mpd_Song *) list->data;
- mpd_freeSong(song);
- list=list->next;
- }
- g_list_free(playlist->list);
- memset(playlist, 0, sizeof(mpdclient_playlist_t));
- return 0;
-}
/* update playlist */
gint
mpd_sendPlaylistInfoCommand(c->connection,-1);
while ((entity = mpd_getNextInfoEntity(c->connection))) {
if (entity->type == MPD_INFO_ENTITY_TYPE_SONG) {
- mpd_Song *song = mpd_songDup(entity->info.song);
-
- c->playlist.list = g_list_append(c->playlist.list,
- (gpointer)song);
- c->playlist.length++;
+ struct mpd_song *song = mpd_songDup(entity->info.song);
+ g_array_append_val(c->playlist.list, song);
}
mpd_freeInfoEntity(entity);
}
mpd_sendPlChangesCommand(c->connection, c->playlist.id);
while ((entity = mpd_getNextInfoEntity(c->connection)) != NULL) {
- mpd_Song *song = entity->info.song;
-
- if (song->pos >= 0 && (guint)song->pos < c->playlist.length) {
- GList *item = g_list_nth(c->playlist.list, song->pos);
+ struct mpd_song *song = mpd_songDup(entity->info.song);
+ if (song->pos >= 0 && (guint)song->pos < c->playlist.list->len) {
/* update song */
- D("updating pos:%d, id=%d [%p] - %s\n",
- song->pos, song->id, item, song->file);
- mpd_freeSong((mpd_Song *) item->data);
- item->data = mpd_songDup(song);
+ D("updating pos:%d, id=%d - %s\n",
+ song->pos, song->id, song->file);
+ mpd_freeSong(g_array_index(c->playlist.list,
+ struct mpd_song *, song->pos));
+ g_array_index(c->playlist.list,
+ struct mpd_song *, song->pos) = song;
} else {
/* add a new song */
D("adding song at pos %d\n", song->pos);
- c->playlist.list = g_list_append(c->playlist.list,
- (gpointer)mpd_songDup(song));
+ g_array_append_val(c->playlist.list, song);
}
+
+ mpd_freeInfoEntity(entity);
}
/* remove trailing songs */
- while ((guint)c->status->playlistLength < c->playlist.length) {
- GList *item = g_list_last(c->playlist.list);
+ while ((guint)c->status->playlistLength < c->playlist.list->len) {
+ guint pos = c->playlist.list->len - 1;
+ struct mpd_song *song = g_array_index(c->playlist.list, struct mpd_song *, pos);
/* Remove the last playlist entry */
- D("removing song at pos %d\n", ((mpd_Song *) item->data)->pos);
- mpd_freeSong((mpd_Song *) item->data);
- c->playlist.list = g_list_delete_link(c->playlist.list, item);
- c->playlist.length = g_list_length(c->playlist.list);
+ D("removing song at pos %d\n", pos);
+ mpd_freeSong(song);
+ g_array_remove_index(c->playlist.list, pos);
}
c->song = NULL;
c->playlist.id = c->status->playlist;
c->playlist.updated = TRUE;
- c->playlist.length = g_list_length(c->playlist.list);
mpdclient_playlist_callback(c, PLAYLIST_EVENT_UPDATED, NULL);
}
#endif
-mpd_Song *
-playlist_get_song(mpdclient_t *c, gint idx)
-{
- return (mpd_Song *) g_list_nth_data(c->playlist.list, idx);
-}
-
-GList *
-playlist_lookup(mpdclient_t *c, int id)
-{
- GList *list = g_list_first(c->playlist.list);
-
- while (list) {
- mpd_Song *song = (mpd_Song *) list->data;
- if( song->id == id )
- return list;
- list=list->next;
- }
-
- return NULL;
-}
-
-mpd_Song *
-playlist_lookup_song(mpdclient_t *c, gint id)
-{
- GList *list = c->playlist.list;
-
- while (list) {
- mpd_Song *song = (mpd_Song *) list->data;
- if (song->id == id)
- return song;
- list=list->next;
- }
-
- return NULL;
-}
-
-gint
-playlist_get_index(mpdclient_t *c, mpd_Song *song)
-{
- return g_list_index(c->playlist.list, song);
-}
-
-gint
-playlist_get_index_from_id(mpdclient_t *c, gint id)
-{
- return g_list_index(c->playlist.list, playlist_lookup_song(c, id));
-}
-
-gint
-playlist_get_index_from_file(mpdclient_t *c, gchar *filename)
-{
- GList *list = c->playlist.list;
- gint i=0;
-
- while( list )
- {
- mpd_Song *song = (mpd_Song *) list->data;
- if( strcmp(song->file, filename ) == 0 )
- return i;
- list=list->next;
- i++;
- }
- return -1;
-}
-
/****************************************************************************/
/*** Filelist functions *****************************************************/
}
filelist_entry_t *
-mpdclient_filelist_find_song(mpdclient_filelist_t *fl, mpd_Song *song)
+mpdclient_filelist_find_song(mpdclient_filelist_t *fl, struct mpd_song *song)
{
GList *list = g_list_first(fl->list);
if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
{
- mpd_Song *song2 = entity->info.song;
+ struct mpd_song *song2 = entity->info.song;
if( strcmp(song->file, song2->file) == 0 )
{
if( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG )
{
- mpd_Song *song = entity->info.song;
+ struct mpd_song *song = entity->info.song;
mpd_sendAddCommand(c->connection, song->file);
}