Implement a CFG_POSITION type and use it

This commit is contained in:
Julien Danjou 2008-03-19 11:15:58 +01:00
parent f01a2ad47d
commit fe8bd0bb5b
3 changed files with 40 additions and 36 deletions

View File

@ -36,6 +36,10 @@
CFG_FUNC((char *) "include", cfg_awesome_include), \ CFG_FUNC((char *) "include", cfg_awesome_include), \
CFG_END() 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. /** This is a better writing of cfg_include coming from libconfuse.
* With this one, we do not treat errors as fatal. * 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); 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_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 *) "text_align", (char *) "center", CFGF_NONE),
CFG_STR((char *) "icon", (char *) "left", CFGF_NONE), CFG_STR((char *) "icon", (char *) "left", CFGF_NONE),
CFG_AWESOME_END() 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 *) "mouse", mouse_generic_opts, CFGF_MULTI),
CFG_SEC((char *) "data", widget_graph_data_opts, CFGF_TITLE | CFGF_MULTI | CFGF_NO_TITLE_DUPES), CFG_SEC((char *) "data", widget_graph_data_opts, CFGF_TITLE | CFGF_MULTI | CFGF_NO_TITLE_DUPES),
CFG_INT((char *) "width", 100, CFGF_NONE), 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_INT((char *) "padding_left", 0, CFGF_NONE),
CFG_FLOAT((char *) "height", 0.67, CFGF_NONE), CFG_FLOAT((char *) "height", 0.67, CFGF_NONE),
CFG_STR((char *) "bg", (char *) NULL, 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_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 *) "height", 0, CFGF_NONE),
CFG_INT((char *) "width", 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), 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; 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 static int
config_validate_alignment(cfg_t *cfg, cfg_opt_t *opt) 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|general|mwfact_upper_limit", config_validate_zero_one_float);
cfg_set_validate_func(cfg, "screen|tags|tag|mwfact", 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 */ /* Check alignment values */
cfg_set_validate_func(cfg, "screen|titlebar|text_align", config_validate_alignment); 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); cfg_set_validate_func(cfg, "rules|rule|titlebar|text_align", config_validate_alignment);

View File

@ -275,9 +275,7 @@ create_widgets(cfg_t* cfg_statusbar, Statusbar *statusbar)
static void static void
config_section_titlebar_init(cfg_t *cfg_titlebar, Titlebar *tb) config_section_titlebar_init(cfg_t *cfg_titlebar, Titlebar *tb)
{ {
tb->position = tb->dposition = tb->position = tb->dposition = *(Position *) cfg_getptr(cfg_titlebar, "position");
position_get_from_str(cfg_getstr(cfg_titlebar, "position"));
tb->icon = position_get_from_str(cfg_getstr(cfg_titlebar, "icon"));
tb->text_align = draw_get_align(cfg_getstr(cfg_titlebar, "text_align")); 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); statusbar = p_new(Statusbar, 1);
cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i); cfgsectmp = cfg_getnsec(cfg_screen, "statusbar", i);
statusbar->position = statusbar->dposition = statusbar->position = statusbar->dposition =
position_get_from_str(cfg_getstr(cfgsectmp, "position")); * (Position *) cfg_getptr(cfgsectmp, "position");
statusbar->height = cfg_getint(cfgsectmp, "height"); statusbar->height = cfg_getint(cfgsectmp, "height");
statusbar->width = cfg_getint(cfgsectmp, "width"); statusbar->width = cfg_getint(cfgsectmp, "width");
statusbar->name = a_strdup(cfg_title(cfgsectmp)); statusbar->name = a_strdup(cfg_title(cfgsectmp));

View File

@ -348,18 +348,16 @@ graph_tell(Widget *widget, char *property, char *command)
return WIDGET_ERROR_FORMAT_COLOR; return WIDGET_ERROR_FORMAT_COLOR;
} }
else if(!a_strcmp(property, "grow")) else if(!a_strcmp(property, "grow"))
switch((d->grow = position_get_from_str(command)))
{ {
if(!a_strcmp(command, "left")) case Left:
d->grow = Left; case Right:
else if(!a_strcmp(command, "right")) break;
d->grow = Right; default:
else
{
warn("error changing property %s of widget %s, must be 'left' or 'right'\n", warn("error changing property %s of widget %s, must be 'left' or 'right'\n",
property, widget->name); property, widget->name);
return WIDGET_ERROR_CUSTOM; return WIDGET_ERROR_CUSTOM;
} }
}
else else
return WIDGET_ERROR; return WIDGET_ERROR;
@ -405,7 +403,7 @@ graph_new(Statusbar *statusbar, cfg_t *config)
return w; 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) if(d->grow != Left && d->grow != Right)
{ {
warn("graph widget: 'grow' argument must be 'left' or 'right'\n"); warn("graph widget: 'grow' argument must be 'left' or 'right'\n");