gui: Allow selecting a startup theme.
authorAndre Noll <maan@systemlinux.org>
Mon, 17 Jan 2011 22:00:40 +0000 (23:00 +0100)
committerAndre Noll <maan@systemlinux.org>
Fri, 11 Feb 2011 10:25:07 +0000 (11:25 +0100)
This implements the --theme option which allows to start para_gui
with a theme different than the default theme.

ggo/gui.m4
gui.c
gui.h
gui_theme.c

index 3f31dc1..3a59807 100644 (file)
@@ -19,6 +19,17 @@ option "timeout" t
        default="30"
        optional
 
+option "theme" T
+#~~~~~~~~~~~~~~~
+"select startup theme"
+       string typestr = "name"
+       optional
+       details = "
+       If this option is not given the default theme is used.
+       If the given name is not a valid theme name, the list of
+       available themes is printed and the program terminates.
+"
+
 option "stat_cmd" s
 #~~~~~~~~~~~~~~~~~~
 "command to read server and audiod status
diff --git a/gui.c b/gui.c
index 8e70dd9..5b25c51 100644 (file)
--- a/gui.c
+++ b/gui.c
@@ -1399,8 +1399,6 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
        HANDLE_VERSION_FLAG("gui", conf);
-       init_theme(0, &theme);
-       top.lines = theme.top_lines_default;
        if (check_key_map_args() < 0) {
                fprintf(stderr, "invalid key map\n");
                exit(EXIT_FAILURE);
@@ -1425,6 +1423,8 @@ int main(int argc, char *argv[])
                fprintf(stderr, "invalid key map in config file\n");
                exit(EXIT_FAILURE);
        }
+       init_theme_or_die(conf.theme_arg, &theme);
+       top.lines = theme.top_lines_default;
        setup_signal_handling();
        bot_win_rb = ringbuffer_new(RINGBUFFER_SIZE);
        initscr(); /* needed only once, always successful */
diff --git a/gui.h b/gui.h
index f8c6712..aad2048 100644 (file)
--- a/gui.h
+++ b/gui.h
@@ -30,7 +30,7 @@ struct gui_theme {
        struct stat_item_data data[NUM_STAT_ITEMS];
 };
 
-void init_theme(int i, struct gui_theme *);
+void init_theme_or_die(const char *name, struct gui_theme *t);
 void next_theme(struct gui_theme *);
 void prev_theme(struct gui_theme *);
 #define LEFT 1
index ffb47d4..5976a0e 100644 (file)
@@ -4,19 +4,14 @@
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
+#include <stdbool.h>
 #include "para.h"
 #include "gui.h"
 #include <curses.h>
 
-#define NUM_THEMES 2
-
-
-static int current_theme_num;
-
 static void init_theme_simple(struct gui_theme *t)
 {
        struct stat_item_data *d = t->data;
-       t->name = "simple";
        t->author = "Andre Noll";
        t->lines_min = 5;
        t->top_lines_min = 2;
@@ -72,7 +67,6 @@ static void init_theme_simple(struct gui_theme *t)
 static void init_theme_colorful_blackness(struct gui_theme *t)
 {
        struct stat_item_data *d = t->data;
-       t->name = "colorful blackness";
        t->author = "Andre Noll";
        /* minimal number of lines that is needed to display all
         * information provided by this theme
@@ -363,24 +357,59 @@ static void init_theme_colorful_blackness(struct gui_theme *t)
        d[SI_DIRECTORY].len = 100;
 }
 
-void init_theme(int num, struct gui_theme *t)
+struct theme_description {
+       const char *name;
+       void (*init)(struct gui_theme *t);
+};
+
+struct theme_description themes[] = {
+       {
+               .name = "colorful blackness",
+               .init = init_theme_colorful_blackness,
+       },
+       {
+               .name = "simple",
+               .init = init_theme_simple,
+       },
+};
+
+#define NUM_THEMES (ARRAY_SIZE(themes))
+
+static int current_theme_num;
+
+void set_theme(int num, struct gui_theme *t)
 {
        int i;
        FOR_EACH_STATUS_ITEM(i)
                t->data[i].len = 0;
+       num %= NUM_THEMES;
+       t->name = themes[num].name;
+       themes[num].init(t);
        current_theme_num = num;
+}
+
+void init_theme_or_die(const char *name, struct gui_theme *t)
+{
+       int i;
 
-       return (num % NUM_THEMES)?
-               init_theme_simple(t) : init_theme_colorful_blackness(t);
+       if (!name)
+               return set_theme(0, t);
+       for (i = 0; i < NUM_THEMES; i++)
+               if (strcmp(name, themes[i].name) == 0)
+                       return set_theme(i, t);
+       fprintf(stderr, "Available themes:\n");
+       for (i = 0; i < NUM_THEMES; i++)
+               fprintf(stderr, "\t%s\n", themes[i].name);
+       exit(EXIT_FAILURE);
 }
 
 void prev_theme(struct gui_theme *t)
 {
-       return init_theme(++current_theme_num, t);
+       return set_theme(++current_theme_num, t);
 }
 
 void next_theme(struct gui_theme *t)
 {
-       return init_theme(--current_theme_num, t);
+       return set_theme(--current_theme_num, t);
 }