From fe8bd0bb5b5af30fcf0ef557b99064594e2d61d1 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Wed, 19 Mar 2008 11:15:58 +0100 Subject: [PATCH] Implement a CFG_POSITION type and use it --- common/configopts.c | 56 ++++++++++++++++++++++++++------------------- config.c | 6 ++--- widgets/graph.c | 14 +++++------- 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/common/configopts.c b/common/configopts.c index 9fadb2d63..7eaf71479 100644 --- a/common/configopts.c +++ b/common/configopts.c @@ -36,6 +36,10 @@ CFG_FUNC((char *) "include", cfg_awesome_include), \ CFG_END() +#define CFG_POSITION(name, value, flags) \ + CFG_PTR_CB(name, value, flags, \ + cfg_position_parse, cfg_value_free) + /** This is a better writing of cfg_include coming from libconfuse. * With this one, we do not treat errors as fatal. */ @@ -67,9 +71,34 @@ cfg_awesome_include(cfg_t *cfg, cfg_opt_t *opt, return cfg_include(cfg, opt, argc, argv); } +static int +cfg_position_parse(cfg_t *cfg, cfg_opt_t *opt, + const char *value, void *result) +{ + Position *p = p_new(Position, 1); + + if((*p = position_get_from_str(value)) == Off + && a_strcmp(value, "off")) + { + cfg_error(cfg, + "position option '%s' must be top, bottom, right, left, auto or off in section '%s'", + opt->name, cfg->name); + p_delete(&p); + return -1; + } + *(void **) result = p; + return 0; +} + +static void +cfg_value_free(void *value) +{ + p_delete(&value); +} + cfg_opt_t titlebar_opts[] = { - CFG_STR((char *) "position", (char *) "auto", CFGF_NONE), + CFG_POSITION((char *) "position", (char *) "auto", CFGF_NONE), CFG_STR((char *) "text_align", (char *) "center", CFGF_NONE), CFG_STR((char *) "icon", (char *) "left", CFGF_NONE), CFG_AWESOME_END() @@ -189,7 +218,7 @@ cfg_opt_t widget_graph_opts[] = CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_SEC((char *) "data", widget_graph_data_opts, CFGF_TITLE | CFGF_MULTI | CFGF_NO_TITLE_DUPES), CFG_INT((char *) "width", 100, CFGF_NONE), - CFG_STR((char *) "grow", (char *) "left", CFGF_NONE), + CFG_POSITION((char *) "grow", (char *) "left", CFGF_NONE), CFG_INT((char *) "padding_left", 0, CFGF_NONE), CFG_FLOAT((char *) "height", 0.67, CFGF_NONE), CFG_STR((char *) "bg", (char *) NULL, CFGF_NONE), @@ -222,7 +251,7 @@ cfg_opt_t widget_progressbar_opts[] = }; cfg_opt_t statusbar_opts[] = { - CFG_STR((char *) "position", (char *) "top", CFGF_NONE), + CFG_POSITION((char *) "position", (char *) "top", CFGF_NONE), CFG_INT((char *) "height", 0, CFGF_NONE), CFG_INT((char *) "width", 0, CFGF_NONE), CFG_SEC((char *) "textbox", widget_textbox_opts, CFGF_TITLE | CFGF_MULTI | CFGF_NO_TITLE_DUPES), @@ -405,22 +434,6 @@ config_validate_zero_one_float(cfg_t *cfg, cfg_opt_t *opt) return 0; } -static int -config_validate_position(cfg_t *cfg, cfg_opt_t *opt) -{ - char *value = cfg_opt_getnstr(opt, cfg_opt_size(opt) - 1); - - if(position_get_from_str(value) == Off - && a_strcmp(value, "off")) - { - cfg_error(cfg, - "position option '%s' must be top, bottom, right, left, auto or off in section '%s'", - opt->name, cfg->name); - return -1; - } - return 0; -} - static int config_validate_alignment(cfg_t *cfg, cfg_opt_t *opt) { @@ -459,11 +472,6 @@ cfg_new(void) cfg_set_validate_func(cfg, "screen|general|mwfact_upper_limit", config_validate_zero_one_float); cfg_set_validate_func(cfg, "screen|tags|tag|mwfact", config_validate_zero_one_float); - /* Check position values */ - cfg_set_validate_func(cfg, "screen|titlebar|position", config_validate_position); - cfg_set_validate_func(cfg, "rules|rule|titlebar|position", config_validate_position); - cfg_set_validate_func(cfg, "screen|statusbar|position", config_validate_position); - /* Check alignment values */ cfg_set_validate_func(cfg, "screen|titlebar|text_align", config_validate_alignment); cfg_set_validate_func(cfg, "rules|rule|titlebar|text_align", config_validate_alignment); diff --git a/config.c b/config.c index 36e59a462..fe1f05da9 100644 --- a/config.c +++ b/config.c @@ -275,9 +275,7 @@ create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar) static void config_section_titlebar_init(cfg_t *cfg_titlebar, Titlebar *tb) { - tb->position = tb->dposition = - position_get_from_str(cfg_getstr(cfg_titlebar, "position")); - tb->icon = position_get_from_str(cfg_getstr(cfg_titlebar, "icon")); + tb->position = tb->dposition = *(Position *) cfg_getptr(cfg_titlebar, "position"); tb->text_align = draw_get_align(cfg_getstr(cfg_titlebar, "text_align")); } @@ -375,7 +373,7 @@ config_parse_screen(cfg_t *cfg, int screen) statusbar = p_new(Statusbar, 1); cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i); statusbar->position = statusbar->dposition = - position_get_from_str(cfg_getstr(cfgsectmp, "position")); + * (Position *) cfg_getptr(cfgsectmp, "position"); statusbar->height = cfg_getint(cfgsectmp, "height"); statusbar->width = cfg_getint(cfgsectmp, "width"); statusbar->name = a_strdup(cfg_title(cfgsectmp)); diff --git a/widgets/graph.c b/widgets/graph.c index c8079f91f..ccbec9055 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -348,18 +348,16 @@ graph_tell(Widget *widget, char *property, char *command) return WIDGET_ERROR_FORMAT_COLOR; } else if(!a_strcmp(property, "grow")) - { - if(!a_strcmp(command, "left")) - d->grow = Left; - else if(!a_strcmp(command, "right")) - d->grow = Right; - else + switch((d->grow = position_get_from_str(command))) { + case Left: + case Right: + break; + default: warn("error changing property %s of widget %s, must be 'left' or 'right'\n", property, widget->name); return WIDGET_ERROR_CUSTOM; } - } else return WIDGET_ERROR; @@ -405,7 +403,7 @@ graph_new(Statusbar *statusbar, cfg_t *config) return w; } - d->grow = position_get_from_str(cfg_getstr(config, "grow")); + d->grow = *(Position *) cfg_getptr(config, "grow"); if(d->grow != Left && d->grow != Right) { warn("graph widget: 'grow' argument must be 'left' or 'right'\n");