Code

included patch from Jonathan Fors
[ncmpc.git] / src / conf.c
index 0d6d96834d517743783798a1ce87e653b8eca560..f2c21ea05fb3aaf49dbefffbd5d112046c177e57 100644 (file)
 #include "colors.h"
 #include "conf.h"
 
-#define ENABLE_OLD_COLOR_SYNTAX
-
 #define MAX_LINE_LENGTH 1024
 #define COMMENT_TOKEN   '#'
 
 /* configuration field names */
-#define CONF_ENABLE_COLORS           "enable_colors"
-#define CONF_AUTO_CENTER             "auto_center"
-#define CONF_WIDE_CURSOR             "wide_cursor"
+#define CONF_ENABLE_COLORS           "enable-colors"
+#define CONF_AUTO_CENTER             "auto-center"
+#define CONF_WIDE_CURSOR             "wide-cursor"
+#define CONF_ENABLE_BELL             "enable-bell"
 #define CONF_KEY_DEFINITION          "key"
 #define CONF_COLOR                   "color"
 #define CONF_COLOR_DEFINITION        "colordef"
-
-/* Deprecated - configuration field names */
-#define CONF_COLOR_BACKGROUND        "background_color"
-#define CONF_COLOR_TITLE             "title_color"
-#define CONF_COLOR_LINE              "line_color"
-#define CONF_COLOR_LIST              "list_color"
-#define CONF_COLOR_PROGRESS          "progress_color"
-#define CONF_COLOR_STATUS            "status_color"
-#define CONF_COLOR_ALERT             "alert_color"
-
+#define CONF_LIST_FORMAT             "list-format"
+#define CONF_STATUS_FORMAT           "status-format"
+#define CONF_XTERM_TITLE_FORMAT      "xterm-title-format"
+#define CONF_LIST_WRAP               "wrap-around"
+#define CONF_FIND_WRAP               "find-wrap"
+#define CONF_FIND_SHOW_LAST          "find-show-last"
+#define CONF_AUDIBLE_BELL            "audible-bell"
+#define CONF_VISIBLE_BELL            "visible-bell"
+#define CONF_XTERM_TITLE             "set-xterm-title"
+#define CONF_ENABLE_MOUSE            "enable-mouse"
+#define CONF_CROSSFADE_TIME          "crossfade-time"
+#define CONF_SEARCH_MODE             "search-mode"
+#define CONF_HIDE_CURSOR             "hide-cursor"
+#define CONF_SEEK_TIME               "seek-time"
+#define CONF_SCREEN_LIST             "screen-list"
+#define CONF_TIMEDISPLAY_TYPE        "timedisplay-type"
 
 typedef enum {
   KEY_PARSER_UNKNOWN,
@@ -69,13 +74,17 @@ typedef enum {
   KEY_PARSER_DONE
 } key_parser_state_t;
 
-static int
+
+extern gint screen_get_id(char *name);
+
+
+static gboolean
 str2bool(char *str)
 {
-  if( !strcasecmp(str,"no")  || !strcasecmp(str,"false") || 
-      !strcasecmp(str,"off") || !strcasecmp(str,"0") )
-    return 0;
-  return 1;
+  if( !strcasecmp(str,"yes")  || !strcasecmp(str,"true") || 
+      !strcasecmp(str,"on")   || !strcasecmp(str,"1") )
+    return TRUE;
+  return FALSE;
 }
 
 static int
@@ -178,7 +187,7 @@ parse_key_definition(char *str)
 
   /* get the value part */
   memset(buf, 0, MAX_LINE_LENGTH);
-  strncpy(buf, str+i, len-i);
+  g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
   len = strlen(buf);
   if( len==0 )
     {
@@ -209,6 +218,17 @@ parse_key_definition(char *str)
   return assign_keys(cmd, keys);
 }
 
+static char *
+parse_timedisplay_type(char *str)
+{
+ if((!strcmp(str,"elapsed")) || (!strcmp(str,"remaining"))){
+   return str;
+ } else {
+   fprintf(stderr,_("Error: Bad time display type - %s\n"), str);
+   return DEFAULT_TIMEDISPLAY_TYPE;
+ }
+}
+
 static int
 parse_color(char *str)
 {
@@ -235,7 +255,6 @@ parse_color(char *str)
   return colors_assign(name, value);
 }
 
-
 static int
 parse_color_definition(char *str)
 {
@@ -265,7 +284,7 @@ parse_color_definition(char *str)
 
   /* get the value part */
   memset(buf, 0, MAX_LINE_LENGTH);
-  strncpy(buf, str+i, len-i);
+  g_strlcpy(buf, str+i, MAX_LINE_LENGTH);
   len = strlen(buf);
   if( len==0 )
     {
@@ -299,6 +318,49 @@ parse_color_definition(char *str)
   return value;
 }
 
+static char *
+get_format(char *str)
+{
+  gsize len = strlen(str);
+
+  if( str && str[0]=='\"' && str[len-1] == '\"' )
+    {
+      str[len-1] = '\0';
+      str++;
+    }
+  return g_strdup(str);
+}
+
+static char **
+check_screen_list(char *value)
+{
+  char **tmp = g_strsplit_set(value, " \t,", 100);
+  char **screen = NULL;
+  int i,j;
+
+  i=0;
+  j=0;
+  while( tmp && tmp[i] )
+    {
+      tmp[i] = lowerstr(tmp[i]);
+      if( screen_get_id(tmp[i]) == -1 )
+       fprintf(stderr,
+               _("Error: Unsupported screen \"%s\"\n"), 
+               tmp[i]);
+      else
+       {
+         screen = g_realloc(screen, (j+2)*sizeof(char *));
+         screen[j++] = g_strdup(tmp[i]);
+         screen[j] = NULL;
+       }
+      i++;
+    }
+  g_strfreev(tmp);
+  if( screen == NULL )
+    return g_strsplit_set(DEFAULT_SCREEN_LIST, " ", 0);
+
+  return screen;
+}
 
 static int
 read_rc_file(char *filename, options_t *options)
@@ -404,80 +466,110 @@ read_rc_file(char *filename, options_t *options)
                {
                  parse_color(value);
                }
-#ifdef ENABLE_OLD_COLOR_SYNTAX
-             /* background color */
-             else if( !strcasecmp(CONF_COLOR_BACKGROUND, name) )
+             /* wide cursor */
+             else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("background", value);
+                 options->wide_cursor = str2bool(value);
                }
-             /* color - top (title) window */
-             else if( !strcasecmp(CONF_COLOR_TITLE, name) )
+             /* timer display type */
+             else if( !strcasecmp(CONF_TIMEDISPLAY_TYPE, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("title", value);
-                 colors_assign("title2", value);
+                   g_free(options->timedisplay_type);
+                   options->timedisplay_type=g_strdup(parse_timedisplay_type(value));
+                   D("deb");
+                   D(options->timedisplay_type);
                }
-             /* color - line (title) window */
-             else if( !strcasecmp(CONF_COLOR_LINE, name) )
+             /* color definition */
+             else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("line", value);
-                 colors_assign("line2", value);
+                 parse_color_definition(value);
                }
-             /* color - list window */
-             else if( !strcasecmp(CONF_COLOR_LIST, name) )
+             /* list format string */
+             else if( !strcasecmp(CONF_LIST_FORMAT, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("list", value);
+                 g_free(options->list_format);
+                 options->list_format = get_format(value);
                }
-             /* color - progress bar */
-             else if( !strcasecmp(CONF_COLOR_PROGRESS, name) )
+             /* status format string */
+             else if( !strcasecmp(CONF_STATUS_FORMAT, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("progressbar", value);
+                 g_free(options->status_format);
+                 options->status_format = get_format(value);
                }
-             /* color - status window */
-             else if( !strcasecmp(CONF_COLOR_STATUS, name) )
+             /* xterm title format string */
+             else if( !strcasecmp(CONF_XTERM_TITLE_FORMAT, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("status", value);
-                 colors_assign("status2", value);
+                 g_free(options->xterm_title_format);
+                 options->xterm_title_format = get_format(value);
                }
-             /* color - alerts */
-             else if( !strcasecmp(CONF_COLOR_ALERT, name) )
+             else if( !strcasecmp(CONF_LIST_WRAP, name) )
                {
-                 fprintf(stderr,"%s: %s - deprecated!\n", filename,name);
-                 colors_assign("alert", value);
+                 options->list_wrap = str2bool(value);
                }
-#endif
-             /* wide cursor */
-             else if( !strcasecmp(CONF_WIDE_CURSOR, name) )
+             else if( !strcasecmp(CONF_FIND_WRAP, name) )
                {
-                 options->wide_cursor = str2bool(value);
+                 options->find_wrap = str2bool(value);
                }
-             /* color definition */
-             else if( !strcasecmp(CONF_COLOR_DEFINITION, name) )
+             else if( !strcasecmp(CONF_FIND_SHOW_LAST,name) )
                {
-                 parse_color_definition(value);
+                 options->find_show_last_pattern = str2bool(value);
+               }
+             else if( !strcasecmp(CONF_AUDIBLE_BELL, name) )
+               {
+                 options->audible_bell = str2bool(value);
+               }
+             else if( !strcasecmp(CONF_VISIBLE_BELL, name) )
+               {
+                 options->visible_bell = str2bool(value);
+               }
+             else if( !strcasecmp(CONF_XTERM_TITLE, name) )
+               {
+                 options->enable_xterm_title = str2bool(value);
+               }
+             else if( !strcasecmp(CONF_ENABLE_MOUSE, name) )
+               {
+                 options->enable_mouse = str2bool(value);
+               }
+             else if( !strcasecmp(CONF_CROSSFADE_TIME, name) )
+               {
+                 options->crossfade_time = atoi(value);
+               }
+             else if( !strcasecmp(CONF_SEARCH_MODE, name) )
+               {
+                 options->search_mode = atoi(value);
+               }
+             else if( !strcasecmp(CONF_HIDE_CURSOR, name) )
+               {
+                 options->hide_cursor = atoi(value);
+               }
+             else if( !strcasecmp(CONF_SEEK_TIME, name) )
+               {
+                 options->seek_time = atoi(value);
+               }
+             else if( !strcasecmp(CONF_SCREEN_LIST, name) )
+               {
+                 g_strfreev(options->screen_list);
+                 options->screen_list = check_screen_list(value);
+                 
+#ifdef DEBUG
+                 D("screen-list:"); 
+                 j=0;
+                 while(options->screen_list[j])
+                   D(" %s", options->screen_list[j++]);
+                 D("\n"); 
+#endif
                }
              else
                {
                  match_found = 0;
                }
-             
 
              if( !match_found )
                fprintf(stderr, 
                        _("Unknown configuration parameter: %s\n"), 
                        name);
-#ifdef DEBUG
-             printf( "  %s = %s %s\n", 
-                     name, 
-                     value,
-                     match_found ? "" : "- UNKNOWN SETTING!" );
-#endif
-
+             D("conf>  %s = %s %s\n", name, value,
+               match_found ? "" : "- UNKNOWN SETTING!" );
            }
        }         
     }