Code

Removed nested functions
[ncmpc.git] / src / screen_play.c
index a86ab01287601d923a3c4f6169e08ddddaa2c04d..718bb13ff96f0b7ce7b12e237f623b9b451c3767 100644 (file)
@@ -21,9 +21,9 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <glib.h>
 #include <ncurses.h>
-#include <panel.h>
 
 #include "config.h"
 #include "ncmpc.h"
 
 #define MAX_SONG_LENGTH 512
 
+typedef struct
+{
+  GList **list;
+  GList **dir_list;
+  screen_t *screen;
+  mpdclient_t *c;
+} completion_callback_data_t;
+
 static list_window_t *lw = NULL;
 
 static void 
@@ -114,55 +122,75 @@ center_playing_item(screen_t *screen, mpdclient_t *c)
   return 0;
 }
 
-static int
-handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
+
+
+
+             
+void save_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
+  {
+   completion_callback_data_t *tmp = (completion_callback_data_t *)data;
+   GList **list = tmp->list;
+   mpdclient_t *c = tmp->c;
+  
+   if( *list == NULL )
+      {
+       /* create completion list */
+       *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
+       g_completion_add_items(gcmp, *list);
+      }
+ }
+  
+ void save_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items,
+                              void *data)
+ {
+   completion_callback_data_t *tmp = (completion_callback_data_t *)data;
+   screen_t *screen = tmp->screen;
+   if( g_list_length(items)>=1 )
+      {
+       screen_display_completion_list(screen, items);
+       lw->clear = 1;
+       lw->repaint = 1;
+      }
+ }
+int
+playlist_save(screen_t *screen, mpdclient_t *c, char *name, char *defaultname)
 {
   gchar *filename;
   gint error;
   GCompletion *gcmp;
   GList *list = NULL;
-
-  void pre_completion_cb(GCompletion *gcmp, gchar *line)
-    {
-      if( list == NULL )
-       {
-         /* create completion list */
-         list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
-         g_completion_add_items(gcmp, list);
-       }
-    }
-
-  void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
-    {
-      if( g_list_length(items)>=1 )
-       {
-         screen_display_completion_list(screen, items);
-         lw->clear = 1;
-         lw->repaint = 1;
-       }
-    }
+  completion_callback_data_t data;
 
   if( name==NULL )
     {
       /* initialize completion support */
       gcmp = g_completion_new(NULL);
       g_completion_set_compare(gcmp, strncmp);
-      wrln_pre_completion_callback = pre_completion_cb;
-      wrln_post_completion_callback = post_completion_cb;
+      data.list = &list;
+      data.dir_list = NULL;
+      data.screen = screen;
+      data.c = c;
+      wrln_completion_callback_data = &data;
+      wrln_pre_completion_callback = save_pre_completion_cb;
+      wrln_post_completion_callback = save_post_completion_cb;
+
 
       /* query the user for a filename */
       filename = screen_readln(screen->status_window.w,
                               _("Save playlist as: "),
-                              NULL,
+                              defaultname,
                               NULL,
                               gcmp);                                  
-      filename=g_strstrip(filename);
 
       /* destroy completion support */
+      wrln_completion_callback_data = NULL;
       wrln_pre_completion_callback = NULL;
       wrln_post_completion_callback = NULL;
       g_completion_free(gcmp);
       list = string_list_free(list);
+      if( filename )
+       filename=g_strstrip(filename);
     }
   else
     {
@@ -191,7 +219,7 @@ handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
                  g_free(filename);
                  return -1;
                }
-             error = handle_save_playlist(screen, c, filename);
+             error = playlist_save(screen, c, filename, NULL);
              g_free(filename);
              return error;
            }     
@@ -206,63 +234,82 @@ handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
   return 0;
 }
 
-static int
-handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
+void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list, GList **list,
+             mpdclient_t *c)
 {
-  gchar *path;
-  GCompletion *gcmp;
-  GList *list = NULL;
-  GList *dir_list = NULL;
+  g_completion_remove_items(gcmp, *list);
+  *list = string_list_remove(*list, dir);
+  *list = gcmp_list_from_path(c, dir, *list, GCMP_TYPE_RFILE);
+  g_completion_add_items(gcmp, *list);
+  *dir_list = g_list_append(*dir_list, g_strdup(dir));
+}
 
-  void add_dir(gchar *dir)
-    {
-      g_completion_remove_items(gcmp, list);
-      list = string_list_remove(list, dir);
-      list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
-      g_completion_add_items(gcmp, list);
-      dir_list = g_list_append(dir_list, g_strdup(dir));
-    }
+void add_pre_completion_cb(GCompletion *gcmp, gchar *line, void *data)
+{
+  completion_callback_data_t *tmp = (completion_callback_data_t *)data;
+  GList **dir_list = tmp->dir_list;
+  GList **list = tmp->list;
+  mpdclient_t *c = tmp->c;
 
-  void pre_completion_cb(GCompletion *gcmp, gchar *line)       
+  D("pre_completion()...\n");
+  if( *list == NULL )
     {
-      D("pre_completion()...\n");
-      if( list == NULL )
-       {
-         /* create initial list */
-         list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
-         g_completion_add_items(gcmp, list);
-       }
-      else if( line && line[0] && line[strlen(line)-1]=='/' &&
-              string_list_find(dir_list, line) == NULL )
-       {         
-         /* add directory content to list */
-         add_dir(line);
-       }
+      /* create initial list */
+      *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
+      g_completion_add_items(gcmp, *list);
     }
+  else if( line && line[0] && line[strlen(line)-1]=='/' &&
+          string_list_find(*dir_list, line) == NULL )
+    {    
+      /* add directory content to list */
+      add_dir(gcmp, line, dir_list, list, c);
+    }
+}
 
-  void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
+void add_post_completion_cb(GCompletion *gcmp, gchar *line, GList *items,
+                            void *data)
+{
+  completion_callback_data_t *tmp = (completion_callback_data_t *)data;
+  GList **dir_list = tmp->dir_list;
+  GList **list = tmp->list;
+  mpdclient_t *c = tmp->c;
+  screen_t *screen = tmp->screen;
+
+  D("post_completion()...\n");
+  if( g_list_length(items)>=1 )
     {
-      D("post_completion()...\n");
-      if( g_list_length(items)>=1 )
-       {
-         screen_display_completion_list(screen, items);
-         lw->clear = 1;
-         lw->repaint = 1;
-       }
+      screen_display_completion_list(screen, items);
+      lw->clear = 1;
+      lw->repaint = 1;
+    }
 
-      if( line && line[0] && line[strlen(line)-1]=='/' &&
-         string_list_find(dir_list, line) == NULL )
-       {         
-         /* add directory content to list */
-         add_dir(line);
-       }
+  if( line && line[0] && line[strlen(line)-1]=='/' &&
+      string_list_find(*dir_list, line) == NULL )
+    {    
+      /* add directory content to list */
+      add_dir(gcmp, line, dir_list, list, c);
     }
+}
+
+static int
+handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
+{
+  gchar *path;
+  GCompletion *gcmp;
+  GList *list = NULL;
+  GList *dir_list = NULL;
+  completion_callback_data_t data;
     
   /* initialize completion support */
   gcmp = g_completion_new(NULL);
   g_completion_set_compare(gcmp, strncmp);
-  wrln_pre_completion_callback = pre_completion_cb;
-  wrln_post_completion_callback = post_completion_cb;
+  data.list = &list;
+  data.dir_list = &dir_list;
+  data.screen = screen;
+  data.c = c;
+  wrln_completion_callback_data = &data;
+  wrln_pre_completion_callback = add_pre_completion_cb;
+  wrln_post_completion_callback = add_post_completion_cb;
   /* get path */
   path = screen_readln(screen->status_window.w, 
                       _("Add: "),
@@ -271,6 +318,7 @@ handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
                       gcmp);
 
   /* destroy completion data */
+  wrln_completion_callback_data = NULL;
   wrln_pre_completion_callback = NULL;
   wrln_post_completion_callback = NULL;
   g_completion_free(gcmp);
@@ -339,6 +387,18 @@ play_paint(screen_t *screen, mpdclient_t *c)
 static void
 play_update(screen_t *screen, mpdclient_t *c)
 {
+  /* hide the cursor when mpd are playing and the user are inactive */
+  if( options.hide_cursor>0 && c->status->state == MPD_STATUS_STATE_PLAY &&
+      time(NULL)-screen->input_timestamp >= options.hide_cursor )
+    {
+      lw->flags |= LW_HIDE_CURSOR;
+    }
+  else
+    {
+      lw->flags &= ~LW_HIDE_CURSOR;
+    }
+
+  /* center the cursor */
   if( options.auto_center )
     {
       static int prev_song_id = 0;
@@ -421,7 +481,7 @@ play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
       mpdclient_cmd_delete(c, lw->selected);
       return 1;
     case CMD_SAVE_PLAYLIST:
-      handle_save_playlist(screen, c, NULL);
+      playlist_save(screen, c, NULL, NULL);
       return 1;
     case CMD_ADD:
       handle_add_to_playlist(screen, c);