diff --git a/common/configopts.c b/common/configopts.c index 7eaf71479..25e93b4a9 100644 --- a/common/configopts.c +++ b/common/configopts.c @@ -40,6 +40,10 @@ CFG_PTR_CB(name, value, flags, \ cfg_position_parse, cfg_value_free) +#define CFG_ALIGNMENT(name, value, flags) \ + CFG_PTR_CB(name, value, flags, \ + cfg_alignment_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. */ @@ -90,6 +94,25 @@ cfg_position_parse(cfg_t *cfg, cfg_opt_t *opt, return 0; } +static int +cfg_alignment_parse(cfg_t *cfg, cfg_opt_t *opt, + const char *value, void *result) +{ + Alignment *p = p_new(Alignment, 1); + + if((*p = draw_align_get_from_str(value)) == Auto + && a_strcmp(value, "auto")) + { + cfg_error(cfg, + "alignment option '%s' must be left, center, right, flex or auto in section '%s'", + opt->name, cfg->name); + p_delete(&p); + return -1; + } + *(void **) result = p; + return 0; +} + static void cfg_value_free(void *value) { @@ -99,7 +122,7 @@ cfg_value_free(void *value) cfg_opt_t titlebar_opts[] = { CFG_POSITION((char *) "position", (char *) "auto", CFGF_NONE), - CFG_STR((char *) "text_align", (char *) "center", CFGF_NONE), + CFG_ALIGNMENT((char *) "text_align", (char *) "center", CFGF_NONE), CFG_STR((char *) "icon", (char *) "left", CFGF_NONE), CFG_AWESOME_END() }; @@ -154,7 +177,7 @@ cfg_opt_t widget_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_AWESOME_END() }; @@ -162,7 +185,7 @@ cfg_opt_t widget_taglist_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), CFG_SEC((char *) "mouse", mouse_taglist_opts, CFGF_MULTI), CFG_AWESOME_END() }; @@ -170,7 +193,7 @@ cfg_opt_t widget_iconbox_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_STR((char *) "image", (char *) NULL, CFGF_NONE), CFG_BOOL((char *) "resize", cfg_true, CFGF_NONE), @@ -180,11 +203,11 @@ cfg_opt_t widget_textbox_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_INT((char *) "width", 0, CFGF_NONE), CFG_STR((char *) "text", (char *) NULL, CFGF_NONE), - CFG_STR((char *) "text_align", (char *) "center", CFGF_NONE), + CFG_ALIGNMENT((char *) "text_align", (char *) "center", CFGF_NONE), CFG_SEC((char *) "style", style_opts, CFGF_NONE), CFG_AWESOME_END() }; @@ -194,7 +217,7 @@ cfg_opt_t widget_tasklist_opts[] = CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_SEC((char *) "styles", styles_opts, CFGF_NONE), - CFG_STR((char *) "text_align", (char *) "left", CFGF_NONE), + CFG_ALIGNMENT((char *) "text_align", (char *) "left", CFGF_NONE), CFG_STR((char *) "show", (char *) "tags", CFGF_NONE), CFG_BOOL((char *) "show_icons", cfg_true, CFGF_NONE), CFG_AWESOME_END() @@ -214,7 +237,7 @@ cfg_opt_t widget_graph_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), 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), @@ -239,7 +262,7 @@ cfg_opt_t widget_progressbar_opts[] = { CFG_INT((char *) "x", 0xffffffff, CFGF_NONE), CFG_INT((char *) "y", 0xffffffff, CFGF_NONE), - CFG_STR((char *) "align", (char *) "auto", CFGF_NONE), + CFG_ALIGNMENT((char *) "align", (char *) "auto", CFGF_NONE), CFG_SEC((char *) "mouse", mouse_generic_opts, CFGF_MULTI), CFG_SEC((char *) "data", widget_progressbar_data_opts, CFGF_TITLE | CFGF_MULTI | CFGF_NO_TITLE_DUPES), CFG_INT((char *) "width", 100, CFGF_NONE), @@ -434,22 +457,6 @@ config_validate_zero_one_float(cfg_t *cfg, cfg_opt_t *opt) return 0; } -static int -config_validate_alignment(cfg_t *cfg, cfg_opt_t *opt) -{ - char *value = cfg_opt_getnstr(opt, cfg_opt_size(opt) - 1); - - if(draw_get_align(value) == Auto - && a_strcmp(value, "auto")) - { - cfg_error(cfg, - "alignment option '%s' must be left, center, right, flex or auto in section '%s'", - opt->name, cfg->name); - return -1; - } - return 0; -} - cfg_t * cfg_new(void) { @@ -472,10 +479,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 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); - return cfg; } diff --git a/common/draw.c b/common/draw.c index b99fb9461..5c58134ce 100644 --- a/common/draw.c +++ b/common/draw.c @@ -723,7 +723,7 @@ draw_textwidth(Display *disp, font_t *font, char *text) * \return Alignment type */ Alignment -draw_get_align(const char *align) +draw_align_get_from_str(const char *align) { if(!a_strncmp(align, "left", 4)) return AlignLeft; diff --git a/common/draw.h b/common/draw.h index 612127b0d..4d2e2afbb 100644 --- a/common/draw.h +++ b/common/draw.h @@ -135,7 +135,7 @@ void draw_image_from_argb_data(DrawCtx *, int, int, int, int, int, unsigned char area_t draw_get_image_size(const char *filename); Drawable draw_rotate(DrawCtx *, int, double, int, int); unsigned short draw_textwidth(Display *, font_t *, char *); -Alignment draw_get_align(const char *); +Alignment draw_align_get_from_str(const char *); Bool draw_color_new(Display *, int, const char *, XColor *); void draw_style_init(Display *, int, cfg_t *, style_t *, style_t *); diff --git a/config.c b/config.c index fe1f05da9..e606abd6f 100644 --- a/config.c +++ b/config.c @@ -276,7 +276,7 @@ static void config_section_titlebar_init(cfg_t *cfg_titlebar, Titlebar *tb) { tb->position = tb->dposition = *(Position *) cfg_getptr(cfg_titlebar, "position"); - tb->text_align = draw_get_align(cfg_getstr(cfg_titlebar, "text_align")); + tb->text_align = * (Alignment *) cfg_getptr(cfg_titlebar, "text_align"); } static void diff --git a/widgets/focusicon.c b/widgets/focusicon.c index de1395eba..e3c967ae6 100644 --- a/widgets/focusicon.c +++ b/widgets/focusicon.c @@ -100,7 +100,7 @@ focusicon_new(Statusbar *statusbar, cfg_t *config) w = p_new(Widget, 1); widget_common_new(w, statusbar, config); w->draw = focusicon_draw; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); /* Set cache property */ w->cache.flags = WIDGET_CACHE_CLIENTS; diff --git a/widgets/graph.c b/widgets/graph.c index ccbec9055..a0ec3b9f8 100644 --- a/widgets/graph.c +++ b/widgets/graph.c @@ -383,7 +383,7 @@ graph_new(Statusbar *statusbar, cfg_t *config) w->draw = graph_draw; w->tell = graph_tell; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); d = w->data = p_new(Data, 1); d->width = cfg_getint(config, "width"); diff --git a/widgets/iconbox.c b/widgets/iconbox.c index 201b5c4f3..317cab72a 100644 --- a/widgets/iconbox.c +++ b/widgets/iconbox.c @@ -99,7 +99,7 @@ iconbox_new(Statusbar *statusbar, cfg_t *config) w = p_new(Widget, 1); widget_common_new(w, statusbar, config); - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); w->draw = iconbox_draw; w->tell = iconbox_tell; w->data = d = p_new(Data, 1); diff --git a/widgets/layoutinfo.c b/widgets/layoutinfo.c index fbc618445..15c72dd98 100644 --- a/widgets/layoutinfo.c +++ b/widgets/layoutinfo.c @@ -64,7 +64,7 @@ layoutinfo_new(Statusbar *statusbar, cfg_t* config) w = p_new(Widget, 1); widget_common_new(w, statusbar, config); w->draw = layoutinfo_draw; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); /* Set cache property */ w->cache.flags = WIDGET_CACHE_LAYOUTS; diff --git a/widgets/progressbar.c b/widgets/progressbar.c index 7e5365f1c..a65e49386 100644 --- a/widgets/progressbar.c +++ b/widgets/progressbar.c @@ -430,7 +430,7 @@ progressbar_new(Statusbar *statusbar, cfg_t *config) if(!(d->vertical = cfg_getbool(config, "vertical"))) d->vertical = False; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); if(!(d->data_items = cfg_size(config, "data"))) { diff --git a/widgets/taglist.c b/widgets/taglist.c index 8d1917e86..22d1c9bc3 100644 --- a/widgets/taglist.c +++ b/widgets/taglist.c @@ -205,7 +205,7 @@ taglist_new(Statusbar *statusbar, cfg_t *config) widget_common_new(w, statusbar, config); w->draw = taglist_draw; w->button_press = taglist_button_press; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); /* Set cache property */ w->cache.flags = WIDGET_CACHE_TAGS | WIDGET_CACHE_CLIENTS; diff --git a/widgets/tasklist.c b/widgets/tasklist.c index 9c961749f..e3640a581 100644 --- a/widgets/tasklist.c +++ b/widgets/tasklist.c @@ -297,7 +297,7 @@ tasklist_new(Statusbar *statusbar, cfg_t *config) &d->styles.urgent, &globalconf.screens[statusbar->screen].styles.urgent); - d->align = draw_get_align(cfg_getstr(config, "text_align")); + d->align = * (Alignment *) cfg_getptr(config, "text_align"); d->show_icons = cfg_getbool(config, "show_icons"); buf = cfg_getstr(config, "show"); diff --git a/widgets/textbox.c b/widgets/textbox.c index 87652cfbd..a5d74006d 100644 --- a/widgets/textbox.c +++ b/widgets/textbox.c @@ -100,7 +100,7 @@ textbox_tell(Widget *widget, char *property, char *command) else if(!a_strcmp(property, "width")) d->width = atoi(command); else if(!a_strcmp(property, "text_align")) - d->align = draw_get_align(command); + d->align = draw_align_get_from_str(command); else return WIDGET_ERROR; @@ -118,7 +118,7 @@ textbox_new(Statusbar *statusbar, cfg_t *config) widget_common_new(w, statusbar, config); w->draw = textbox_draw; w->tell = textbox_tell; - w->alignment = draw_get_align(cfg_getstr(config, "align")); + w->alignment = * (Alignment *) cfg_getptr(config, "align"); w->data = d = p_new(Data, 1); @@ -128,7 +128,7 @@ textbox_new(Statusbar *statusbar, cfg_t *config) &globalconf.screens[statusbar->screen].styles.normal); d->width = cfg_getint(config, "width"); - d->align = draw_get_align(cfg_getstr(config, "text_align")); + d->align = * (Alignment *) cfg_getptr(config, "text_align"); d->text = a_strdup(cfg_getstr(config, "text"));