From 4ca0f663a918fc04389ff4351b596209902bd11c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 3 Oct 2008 11:56:00 +0200 Subject: [PATCH] list_window: remove list_window_state_t We do not need to save a stack of list window states. When we return to a parent directory, we just have to find the directory which we come from in the parent list. Note that this patch resets the cursor when going to the root directory, but I think it's not that important, and I will deal with that later. --- src/list_window.c | 54 -------------------------------- src/list_window.h | 13 -------- src/screen_artist.c | 74 +++++++++++++++++++++++++++++++++++++------- src/screen_browser.c | 24 ++++++++------ src/screen_browser.h | 1 - src/screen_file.c | 2 -- 6 files changed, 78 insertions(+), 90 deletions(-) diff --git a/src/list_window.c b/src/list_window.c index 93bed86..55b621b 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -382,57 +382,3 @@ list_window_mouse(struct list_window *lw, unsigned rows, return 0; } #endif - -list_window_state_t * -list_window_init_state(void) -{ - return g_malloc0(sizeof(list_window_state_t)); -} - -void -list_window_free_state(list_window_state_t *state) -{ - if (state) { - if (state->list) { - GList *list = state->list; - - while (list) { - g_free(list->data); - list->data = NULL; - list = list->next; - } - - g_list_free(state->list); - state->list = NULL; - } - - g_free(state); - } -} - -void -list_window_push_state(list_window_state_t *state, struct list_window *lw) -{ - if (state) { - struct list_window *tmp = g_malloc(sizeof(list_window_t)); - memcpy(tmp, lw, sizeof(list_window_t)); - state->list = g_list_prepend(state->list, (gpointer) tmp); - list_window_reset(lw); - } -} - -bool -list_window_pop_state(list_window_state_t *state, struct list_window *lw) -{ - if (state && state->list) { - struct list_window *tmp = state->list->data; - - memcpy(lw, tmp, sizeof(list_window_t)); - g_free(tmp); - state->list->data = NULL; - state->list = g_list_delete_link(state->list, state->list); - } - - // return TRUE if there are still states in the list - return (state && state->list) ? TRUE : FALSE; -} diff --git a/src/list_window.h b/src/list_window.h index 6bfe5db..1d93a36 100644 --- a/src/list_window.h +++ b/src/list_window.h @@ -23,10 +23,6 @@ typedef struct list_window { int flags; } list_window_t; -typedef struct list_window_state { - GList *list; -} list_window_state_t; - /* create a new list window */ struct list_window *list_window_init(WINDOW *w, @@ -86,13 +82,4 @@ list_window_rfind(struct list_window *lw, int wrap, unsigned rows); -/* list window states */ -list_window_state_t *list_window_init_state(void); -void list_window_free_state(list_window_state_t *state); -void list_window_push_state(list_window_state_t *state, - struct list_window *lw); -bool list_window_pop_state(list_window_state_t *state, struct list_window *lw); - - - #endif diff --git a/src/screen_artist.c b/src/screen_artist.c index 8c5636f..e488a3a 100644 --- a/src/screen_artist.c +++ b/src/screen_artist.c @@ -319,7 +319,6 @@ static void init(WINDOW *w, int cols, int rows) { browser.lw = list_window_init(w, cols, rows); - browser.lw_state = list_window_init_state(); artist = NULL; album = NULL; } @@ -329,7 +328,6 @@ quit(void) { free_state(NULL); list_window_free(browser.lw); - list_window_free_state(browser.lw_state); } static void @@ -445,10 +443,25 @@ artist_lw_cmd(struct screen *screen, struct mpdclient *c, command_t cmd) return 0; } +static int +string_array_find(GPtrArray *array, const char *value) +{ + guint i; + + for (i = 0; i < array->len; ++i) + if (strcmp((const char*)g_ptr_array_index(array, i), + value) == 0) + return i; + + return -1; +} + static int artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) { char *selected; + char *old; + int idx; switch(cmd) { case CMD_PLAY: @@ -457,7 +470,7 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) selected = g_ptr_array_index(artist_list, browser.lw->selected); open_album_list(c, g_strdup(selected)); - list_window_push_state(browser.lw_state, browser.lw); + list_window_reset(browser.lw); artist_repaint(); return true; @@ -465,21 +478,29 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) case LIST_ALBUMS: if (browser.lw->selected == 0) { /* handle ".." */ + old = g_strdup(artist); open_artist_list(c); list_window_reset(browser.lw); /* restore previous list window state */ - list_window_pop_state(browser.lw_state, browser.lw); + idx = string_array_find(artist_list, old); + g_free(old); + + if (idx >= 0) { + list_window_set_selected(browser.lw, idx); + list_window_center(browser.lw, + artist_list->len, idx); + } } else if (browser.lw->selected == album_list->len + 1) { /* handle "show all" */ open_song_list(c, g_strdup(artist), g_strdup("\0")); - list_window_push_state(browser.lw_state, browser.lw); + list_window_reset(browser.lw); } else { /* select album */ selected = g_ptr_array_index(album_list, browser.lw->selected - 1); open_song_list(c, g_strdup(artist), g_strdup(selected)); - list_window_push_state(browser.lw_state, browser.lw); + list_window_reset(browser.lw); } artist_repaint(); @@ -488,12 +509,22 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) case LIST_SONGS: if (browser.lw->selected == 0) { /* handle ".." */ + old = g_strdup(album); open_album_list(c, g_strdup(artist)); list_window_reset(browser.lw); /* restore previous list window state */ - list_window_pop_state(browser.lw_state, - browser.lw); + idx = *old == 0 + ? (int)album_list->len + : string_array_find(album_list, old); + g_free(old); + + if (idx >= 0) { + ++idx; + list_window_set_selected(browser.lw, idx); + list_window_center(browser.lw, + album_list->len, idx); + } artist_repaint(); return true; @@ -510,17 +541,38 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) break; case LIST_ALBUMS: + old = g_strdup(artist); + open_artist_list(c); list_window_reset(browser.lw); /* restore previous list window state */ - list_window_pop_state(browser.lw_state, browser.lw); + idx = string_array_find(artist_list, old); + g_free(old); + + if (idx >= 0) { + list_window_set_selected(browser.lw, idx); + list_window_center(browser.lw, + artist_list->len, idx); + } break; case LIST_SONGS: + old = g_strdup(album); + open_album_list(c, g_strdup(artist)); list_window_reset(browser.lw); /* restore previous list window state */ - list_window_pop_state(browser.lw_state, browser.lw); + idx = *old == 0 + ? (int)album_list->len + : string_array_find(album_list, old); + g_free(old); + + if (idx >= 0) { + ++idx; + list_window_set_selected(browser.lw, idx); + list_window_center(browser.lw, + album_list->len, idx); + } break; } @@ -537,7 +589,7 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd) open_artist_list(c); list_window_reset(browser.lw); /* restore first list window state (pop while returning true) */ - while(list_window_pop_state(browser.lw_state, browser.lw)); + /* XXX */ break; } diff --git a/src/screen_browser.c b/src/screen_browser.c index da0dc89..4457e7e 100644 --- a/src/screen_browser.c +++ b/src/screen_browser.c @@ -164,6 +164,8 @@ browser_change_directory(struct screen_browser *browser, mpdclient_t *c, { mpd_InfoEntity *entity = NULL; gchar *path = NULL; + char *old_path; + int idx; if( entry!=NULL ) entity = entry->entity; @@ -177,29 +179,33 @@ browser_change_directory(struct screen_browser *browser, mpdclient_t *c, if( strcmp(parent, ".") == 0 ) parent[0] = '\0'; path = g_strdup(parent); - list_window_reset(browser->lw); - /* restore previous list window state */ - list_window_pop_state(browser->lw_state, browser->lw); } else { /* entry==NULL, then new_path ("" is root) */ path = g_strdup(new_path); - list_window_reset(browser->lw); - /* restore first list window state (pop while returning true) */ - while(list_window_pop_state(browser->lw_state, browser->lw)); } } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) { /* enter sub */ mpd_Directory *dir = entity->info.directory; path = utf8_to_locale(dir->path); - /* save current list window state */ - list_window_push_state(browser->lw_state, browser->lw); } else return -1; + old_path = g_strdup(browser->filelist->path); + filelist_free(browser->filelist); browser->filelist = mpdclient_filelist_get(c, path); sync_highlights(c, browser->filelist); - list_window_check_selected(browser->lw, filelist_length(browser->filelist)); + + idx = filelist_find_directory(browser->filelist, old_path); + g_free(old_path); + + list_window_reset(browser->lw); + if (idx >= 0) { + list_window_set_selected(browser->lw, idx); + list_window_center(browser->lw, + filelist_length(browser->filelist), idx); + } + g_free(path); return 0; } diff --git a/src/screen_browser.h b/src/screen_browser.h index 2b825c0..cd218bc 100644 --- a/src/screen_browser.h +++ b/src/screen_browser.h @@ -33,7 +33,6 @@ struct list_window_state; struct screen_browser { struct list_window *lw; - struct list_window_state *lw_state; mpdclient_filelist_t *filelist; }; diff --git a/src/screen_file.c b/src/screen_file.c index 95510a5..1ba9640 100644 --- a/src/screen_file.c +++ b/src/screen_file.c @@ -143,7 +143,6 @@ static void browse_init(WINDOW *w, int cols, int rows) { browser.lw = list_window_init(w, cols, rows); - browser.lw_state = list_window_init_state(); } static void @@ -159,7 +158,6 @@ browse_exit(void) if (browser.filelist) filelist_free(browser.filelist); list_window_free(browser.lw); - list_window_free_state(browser.lw_state); } static void -- 2.30.2