1 xtk_config_registry
liquibyte edited this page 2026-11-19 16:23:20 -06:00

xtk_config_registry.h

The list of config file sections that widgets read and write themselves.

xtk_config.h reads a config file line by line. It handles a few sections itself ([main], [behavior], [clipboard], [sizes], [zoom], [zoom-widget] and [zoom-hex]); every other section, such as [button] or [button-widget], belongs to a widget. Each widget registers a handler for its sections here: a section name and three functions, which give the section's defaults, read one of its keys and write the whole section. xtk_config.c then calls whichever handler is registered for a section, without knowing the widget.

A widget registers its handlers from a constructor function in its own .c file, which runs when the program starts. A widget that is not linked into the program registers nothing. With the static library this means: the sections of a widget the program does not use are not registered, are not written to a config file, and their lines in a file are ignored.

A program that uses only the widgets does not call this header. It is for a new widget, or for code that wants to list the sections.

Example. A widget with one section, [mywidget], that has one key:

static struct { int gap; } my_settings;

static void mywidget_defaults(struct XtkConfig *config) {
    my_settings.gap = 4;
}
static int mywidget_parse(struct XtkConfig *config, const char *key, const char *value) {
    if (strcmp(key, "gap") == 0) { my_settings.gap = atoi(value); return 1; }
    return 0;   // not a key of this section
}
static void mywidget_write(FILE *file, const struct XtkConfig *config) {
    fprintf(file, "[mywidget]\ngap = %d\n\n", my_settings.gap);
}
static const ConfigSectionHandler mywidget_handler = {
    .section = "mywidget",
    .init_defaults = mywidget_defaults,
    .parse = mywidget_parse,
    .write = mywidget_write,
};
__attribute__((constructor))
static void mywidget_register(void) {
    xtk_config_registry_register(&mywidget_handler);
}
 * 
#include "xtk_config_registry.h"

Types: ConfigSectionHandler

Functions: xtk_config_registry_register, xtk_config_registry_find, xtk_config_registry_for_each, xtk_config_registry_init_all_defaults, xtk_config_registry_write_all, xtk_config_registry_dispatch_parse

Types

ConfigSectionHandler

How one config section is given its defaults, read and written.

Field Type Description
section const char * The section name without brackets, for example "button"; must not be NULL.
init_defaults void (*)(struct XtkConfig *config) Sets the section's defaults in config; called by xtk_config_init_defaults(); may be NULL.
parse int (*)(struct XtkConfig *config, const char *key, const char *value) Reads one "key = value" line of the section into config; returns 1 if it knows the key, 0 if not; may be NULL.
write void (*)(FILE *file, const struct XtkConfig *config) Writes the section, its "[section]" line first and a blank line last; may be NULL (the section is then not written)

Functions

xtk_config_registry_register

void xtk_config_registry_register(const ConfigSectionHandler *handler);

Register a handler for a config section.

Usually called from a constructor function in the widget's own file (see the example above). Ignored if handler is NULL or has no section name, if a handler for the same section is already registered, or if memory runs out.

Parameter Description
handler The handler. The pointer is kept, not copied, so it must stay valid while the program runs (make it a static).

xtk_config_registry_find

const ConfigSectionHandler *xtk_config_registry_find(const char *section);

The handler registered for a section.

Parameter Description
section The section name, for example "button"

Returns: The handler, or NULL if none is registered for that section (or section is NULL)

xtk_config_registry_for_each

void xtk_config_registry_for_each(
    void(*)(const ConfigSectionHandler *handler, void *user_data)callback,
    void *user_data);

Call a function for every registered handler, in the order they were registered.

Parameter Description
callback Called once for each handler, with the handler and user_data; nothing happens when it is NULL
user_data Passed to callback

xtk_config_registry_init_all_defaults

void xtk_config_registry_init_all_defaults(struct XtkConfig *config);

Call every registered handler's init_defaults, in registration order.

xtk_config_init_defaults() calls this; a program does not need to.

Parameter Description
config The configuration to fill

xtk_config_registry_write_all

void xtk_config_registry_write_all(FILE *file, const struct XtkConfig *config);

Write every registered section, in registration order.

Each section is written by its handler. Then each color key of that section in the toolkit's color table (see xtk_config_color_key_at()) that the handler did not write is added, so a written file holds every color the toolkit has. xtk_config_write_defaults_with_values() calls this.

Parameter Description
file The file to write to
config The configuration to write

xtk_config_registry_dispatch_parse

int xtk_config_registry_dispatch_parse(
    struct XtkConfig *config,
    const char *section,
    const char *key,
    const char *value);

Give one "key = value" line to its section's handler.

xtk_config_set() calls this for every section it does not handle itself.

Parameter Description
config The configuration to update
section The section the line is in
key The key
value The value

Returns: What the handler's parse function returned (1 if it knew the key, 0 if not), or 0 if no handler with a parse function is registered for the section