Code

Moved list window state code to list_window.c
authorKalle Wallin <kaw@linux.se>
Tue, 14 Jun 2005 17:30:17 +0000 (17:30 +0000)
committerKalle Wallin <kaw@linux.se>
Tue, 14 Jun 2005 17:30:17 +0000 (17:30 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@3353 09075e82-0dd4-0310-85a5-a0d7c8717e4f

ChangeLog
src/list_window.c
src/list_window.h
src/screen_artist.c
src/screen_file.c

index 964bfcbafdbb43d93c7b8181ae8e6420b20db16c..393bb68094325e890a9eb1395ef5cb7097a6c25d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
+2005-06-14: Kalle Wallin <kaw@linux.se>
+       * Moved list window state code to list_window.c
+
 2005-06-11: Kalle Wallin <kaw@linux.se>
        * configure.ac: support wide-char ncurses library (--with-ncursesw)
+       * support.c: added my_strlen for wide-char strings
        
 2005-06-06: Kalle Wallin <kaw@linux.se>
        * Added Norwegian translations from Ole R. Thorsen
index 8966622925ee12bb45a2815f0e27880a38a16786..5ee81d0697e90ce05ecdd68832348589fee4bf79 100644 (file)
@@ -327,3 +327,62 @@ list_window_cmd(list_window_t *lw, int rows, command_t cmd)
 }
 
 
+
+
+
+list_window_state_t *
+list_window_init_state(void)
+{
+  return g_malloc0(sizeof(list_window_state_t));
+}
+
+list_window_state_t *
+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);
+    }
+  return NULL;
+}
+
+void 
+list_window_push_state(list_window_state_t *state, list_window_t *lw)
+{
+  if( state )
+    {
+      list_window_t *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);
+    }
+}
+
+void 
+list_window_pop_state(list_window_state_t *state, list_window_t *lw)
+{
+  if( state && state->list )
+    {
+      list_window_t *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);
+    }
+}
+
+
+
index 8374e53d7235e781f25f8fef5ea51adacab60062..2061e81aa9d6ddf4f8fc5b0dbf6388c2ffc64bc4 100644 (file)
@@ -23,6 +23,11 @@ typedef struct
 
 } list_window_t;
 
+typedef struct 
+{
+  GList *list;
+} list_window_state_t;
+
 
 /* create a new list window */
 list_window_t *list_window_init(WINDOW *w, int width, int height);
@@ -71,4 +76,12 @@ list_window_rfind(list_window_t *lw,
                  int wrap,
                  int rows);
 
+/* list window states */
+list_window_state_t *list_window_init_state(void);
+list_window_state_t *list_window_free_state(list_window_state_t *state);
+void list_window_push_state(list_window_state_t *state, list_window_t *lw);
+void list_window_pop_state(list_window_state_t *state, list_window_t *lw);
+
+
+
 #endif
index e09d9f04927779ba0ff4481b9cc42a30f3ec3713..a363304f52d5a4f5caa9c651b615597b8019219a 100644 (file)
@@ -1,7 +1,7 @@
 /* 
  * $Id$
  *
- * (c) 2004 by Kalle Wallin <kaw@linux.se>
+ * (c) 2005 by Kalle Wallin <kaw@linux.se>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -45,32 +45,7 @@ static list_window_t *lw = NULL;
 static mpdclient_filelist_t *filelist = NULL;
 static int metalist_length = 0;
 static GList *metalist = NULL;
-static GList *lw_state_list = NULL;
-
-/* store current state when entering a subdirectory */
-static void
-push_lw_state(void)
-{
-  list_window_t *tmp = g_malloc(sizeof(list_window_t));
-
-  memcpy(tmp, lw, sizeof(list_window_t));
-  lw_state_list = g_list_prepend(lw_state_list, (gpointer) tmp);
-}
-
-/* get previous state when leaving a directory */
-static void
-pop_lw_state(void)
-{
-  if( lw_state_list )
-    {
-      list_window_t *tmp = lw_state_list->data;
-
-      memcpy(lw, tmp, sizeof(list_window_t));
-      g_free(tmp);
-      lw_state_list->data = NULL;
-      lw_state_list = g_list_delete_link(lw_state_list, lw_state_list);
-    }
-}
+static list_window_state_t *lw_state = NULL;
 
 /* list_window callback */
 static char *
@@ -108,6 +83,7 @@ playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
     }
 }
 
+/* fetch artists/albums/songs from mpd */
 static void
 update_metalist(mpdclient_t *c, char *m_artist, char *m_album)
 {
@@ -160,12 +136,31 @@ update_metalist(mpdclient_t *c, char *m_artist, char *m_album)
       metalist = mpdclient_get_artists_utf8(c);
     }
   metalist_length = g_list_length(metalist);
+  lw->clear = TRUE;
+}
+
+/* db updated */
+static void 
+browse_callback(mpdclient_t *c, int event, gpointer data)
+{
+  switch(event)
+    {
+    case BROWSE_DB_UPDATED:
+      D("screen_artist.c> browse_callback() [BROWSE_DB_UPDATED]\n");
+      lw->clear = 1;
+      lw->repaint = 1;
+      update_metalist(c, g_strdup(artist), g_strdup(album));
+      break;
+    default:
+      break;
+    }
 }
 
 static void
 init(WINDOW *w, int cols, int rows)
 {
   lw = list_window_init(w, cols, rows);
+  lw_state = list_window_init_state();
   artist = NULL;
   album = NULL;
 }
@@ -173,20 +168,6 @@ init(WINDOW *w, int cols, int rows)
 static void
 quit(void)
 {
-  /* clear list window state */
-  if( lw_state_list )
-    {
-      GList *list = lw_state_list;
-      while( list )
-       {
-         g_free(list->data);
-         list->data = NULL;
-         list = list->next;
-       }
-      g_list_free(lw_state_list);
-      lw_state_list = NULL;
-    }
-  
   if( filelist )
     filelist = mpdclient_filelist_free(filelist);
   if( metalist )
@@ -195,14 +176,22 @@ quit(void)
   g_free(album);
   artist = NULL;
   album = NULL;
-  list_window_free(lw);  
+  lw = list_window_free(lw);  
+  lw_state = list_window_free_state(lw_state);
 }
 
 static void
 open(screen_t *screen, mpdclient_t *c)
 {
+  static gboolean callback_installed = FALSE;
+
   if( metalist==NULL && filelist ==NULL)
     update_metalist(c, NULL, NULL);
+  if( !callback_installed )
+    {
+      mpdclient_install_browse_callback(c, browse_callback);
+      callback_installed = TRUE;
+    }
 }
 
 static void
@@ -242,12 +231,18 @@ paint(screen_t *screen, mpdclient_t *c)
 static void 
 update(screen_t *screen, mpdclient_t *c)
 {
-  if( filelist==NULL || filelist->updated )
+  if( filelist && !filelist->updated )
+    {
+      list_window_paint(lw, browse_lw_callback, (void *) filelist);
+    }
+  else if( metalist )
+    {
+      list_window_paint(lw, artist_lw_callback, (void *) metalist);
+    }
+  else
     {
       paint(screen, c);
-      return;
     }
-  list_window_paint(lw, browse_lw_callback, (void *) filelist);
   wnoutrefresh(lw->w);
 }
 
@@ -287,7 +282,7 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
              update_metalist(c, g_strdup(artist), NULL);
              list_window_reset(lw);
              /* restore previous list window state */
-             pop_lw_state(); 
+             list_window_pop_state(lw_state,lw); 
            }
          else
            browse_handle_enter(screen, c, lw, filelist);
@@ -300,28 +295,25 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
              update_metalist(c, NULL, NULL);
              list_window_reset(lw);
              /* restore previous list window state */
-             pop_lw_state(); 
+             list_window_pop_state(lw_state,lw); 
            }
          else if( lw->selected == metalist_length-1) /* handle "show all" */
            {
              update_metalist(c, g_strdup(artist), g_strdup("\0"));
-             push_lw_state(); 
-             list_window_reset(lw);
+             list_window_push_state(lw_state,lw); 
            }
          else /* select album */
            {
              char *selected = (char *) g_list_nth_data(metalist, lw->selected);
              update_metalist(c, g_strdup(artist), g_strdup(selected));
-             push_lw_state(); 
-             list_window_reset(lw);
+             list_window_push_state(lw_state,lw); 
            }
        }
       else
        {
          char *selected = (char *) g_list_nth_data(metalist, lw->selected);
          update_metalist(c, g_strdup(selected), NULL);
-         push_lw_state(); 
-         list_window_reset(lw);
+         list_window_push_state(lw_state,lw); 
        }
       return 1;
 
@@ -335,6 +327,11 @@ artist_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
 
       /* continue and update... */
     case CMD_SCREEN_UPDATE:
+      screen->painted = 0;
+      lw->clear = 1;
+      lw->repaint = 1;
+      update_metalist(c, g_strdup(artist), g_strdup(album));
+      screen_status_printf(_("Screen updated!"));
       return 0;
 
     case CMD_LIST_FIND:
index 002e9cd73261132d5bf3704e2f90194f9d3d411c..b2ca1b6cd9c6c5acd0bf72f9935f116ce9e55a82 100644 (file)
@@ -45,7 +45,7 @@
 
 
 static list_window_t *lw = NULL;
-static GList *lw_state_list = NULL;
+static list_window_state_t *lw_state = NULL;
 static mpdclient_filelist_t *filelist = NULL;
 
 
@@ -153,31 +153,6 @@ playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
     }
 }
 
-/* store current state when entering a subdirectory */
-static void
-push_lw_state(void)
-{
-  list_window_t *tmp = g_malloc(sizeof(list_window_t));
-
-  memcpy(tmp, lw, sizeof(list_window_t));
-  lw_state_list = g_list_prepend(lw_state_list, (gpointer) tmp);
-}
-
-/* get previous state when leaving a directory */
-static void
-pop_lw_state(void)
-{
-  if( lw_state_list )
-    {
-      list_window_t *tmp = lw_state_list->data;
-
-      memcpy(lw, tmp, sizeof(list_window_t));
-      g_free(tmp);
-      lw_state_list->data = NULL;
-      lw_state_list = g_list_delete_link(lw_state_list, lw_state_list);
-    }
-}
-
 /* list_window callback */
 char *
 browse_lw_callback(int index, int *highlight, void *data)
@@ -248,7 +223,7 @@ change_directory(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry)
       path = g_strdup(parent);
       list_window_reset(lw);
       /* restore previous list window state */
-      pop_lw_state(); 
+      list_window_pop_state(lw_state,lw); 
     }
   else
     if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY)
@@ -257,8 +232,7 @@ change_directory(screen_t *screen, mpdclient_t *c, filelist_entry_t *entry)
        mpd_Directory *dir = entity->info.directory;
        path = utf8_to_locale(dir->path);      
        /* save current list window state */
-       push_lw_state(); 
-       list_window_reset(lw);
+       list_window_push_state(lw_state,lw); 
       }
     else
       return -1;
@@ -537,6 +511,7 @@ static void
 browse_init(WINDOW *w, int cols, int rows)
 {
   lw = list_window_init(w, cols, rows);
+  lw_state = list_window_init_state();
 }
 
 static void
@@ -549,22 +524,10 @@ browse_resize(int cols, int rows)
 static void
 browse_exit(void)
 {
-  if( lw_state_list )
-    {
-      GList *list = lw_state_list;
-      while( list )
-       {
-         g_free(list->data);
-         list->data = NULL;
-         list = list->next;
-       }
-      g_list_free(lw_state_list);
-      lw_state_list = NULL;
-
-    }
   if( filelist )
     filelist = mpdclient_filelist_free(filelist);
-  list_window_free(lw);
+  lw = list_window_free(lw);
+  lw_state = list_window_free_state(lw_state);
 }
 
 static void