forked from external/yambar
module/i3: export module info through the new module_info struct type
This commit is contained in:
parent
d2af5b2c84
commit
f3721d9d80
4 changed files with 87 additions and 83 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include "modules/backlight/backlight.h"
|
#include "modules/backlight/backlight.h"
|
||||||
#include "modules/battery/battery.h"
|
#include "modules/battery/battery.h"
|
||||||
#include "modules/clock/clock.h"
|
#include "modules/clock/clock.h"
|
||||||
|
#include "modules/i3/i3.h"
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
conf_err_prefix(const keychain_t *chain, const struct yml_node *node)
|
conf_err_prefix(const keychain_t *chain, const struct yml_node *node)
|
||||||
|
@ -413,35 +414,6 @@ conf_verify_particle(keychain_t *chain, const struct yml_node *node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
|
||||||
verify_i3_content(keychain_t *chain, const struct yml_node *node)
|
|
||||||
{
|
|
||||||
if (!yml_is_dict(node)) {
|
|
||||||
LOG_ERR(
|
|
||||||
"%s: must be a dictionary of workspace-name: particle mappings",
|
|
||||||
err_prefix(chain, node));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (struct yml_dict_iter it = yml_dict_iter(node);
|
|
||||||
it.key != NULL;
|
|
||||||
yml_dict_next(&it))
|
|
||||||
{
|
|
||||||
const char *key = yml_value_as_string(it.key);
|
|
||||||
if (key == NULL) {
|
|
||||||
LOG_ERR("%s: key must be a string (a i3 workspace name)",
|
|
||||||
err_prefix(chain, it.key));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!conf_verify_particle(chain_push(chain, key), it.value))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
chain_pop(chain);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
verify_module(keychain_t *chain, const struct yml_node *node)
|
verify_module(keychain_t *chain, const struct yml_node *node)
|
||||||
|
@ -474,14 +446,6 @@ verify_module(keychain_t *chain, const struct yml_node *node)
|
||||||
{"anchors", false, NULL},
|
{"anchors", false, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct attr_info i3[] = {
|
|
||||||
{"spacing", false, &conf_verify_int},
|
|
||||||
{"left-spacing", false, &conf_verify_int},
|
|
||||||
{"right-spacing", false, &conf_verify_int},
|
|
||||||
{"content", true, &verify_i3_content},
|
|
||||||
{"anchors", false, NULL},
|
|
||||||
};
|
|
||||||
|
|
||||||
static const struct attr_info network[] = {
|
static const struct attr_info network[] = {
|
||||||
{"name", true, &conf_verify_string},
|
{"name", true, &conf_verify_string},
|
||||||
{"content", true, &conf_verify_particle},
|
{"content", true, &conf_verify_particle},
|
||||||
|
@ -515,6 +479,7 @@ verify_module(keychain_t *chain, const struct yml_node *node)
|
||||||
{"backlight", &module_backlight},
|
{"backlight", &module_backlight},
|
||||||
{"battery", &module_battery},
|
{"battery", &module_battery},
|
||||||
{"clock", &module_clock},
|
{"clock", &module_clock},
|
||||||
|
{"i3", &module_i3},
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
|
@ -522,7 +487,6 @@ verify_module(keychain_t *chain, const struct yml_node *node)
|
||||||
const struct attr_info *attrs;
|
const struct attr_info *attrs;
|
||||||
size_t count;
|
size_t count;
|
||||||
} modules[] = {
|
} modules[] = {
|
||||||
{"i3", i3, sizeof(i3) / sizeof(i3[0])},
|
|
||||||
{"label", label, sizeof(label) / sizeof(label[0])},
|
{"label", label, sizeof(label) / sizeof(label[0])},
|
||||||
{"mpd", mpd, sizeof(mpd) / sizeof(mpd[0])},
|
{"mpd", mpd, sizeof(mpd) / sizeof(mpd[0])},
|
||||||
{"network", network, sizeof(network) / sizeof(network[0])},
|
{"network", network, sizeof(network) / sizeof(network[0])},
|
||||||
|
|
33
config.c
33
config.c
|
@ -376,33 +376,6 @@ module_xwindow_from_config(const struct yml_node *node, const struct font *paren
|
||||||
return module_xwindow(conf_to_particle(c, parent_font));
|
return module_xwindow(conf_to_particle(c, parent_font));
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct module *
|
|
||||||
module_i3_from_config(const struct yml_node *node, const struct font *parent_font)
|
|
||||||
{
|
|
||||||
const struct yml_node *c = yml_get_value(node, "content");
|
|
||||||
const struct yml_node *spacing = yml_get_value(node, "spacing");
|
|
||||||
const struct yml_node *left_spacing = yml_get_value(node, "left_spacing");
|
|
||||||
const struct yml_node *right_spacing = yml_get_value(node, "right_spacing");
|
|
||||||
|
|
||||||
int left = spacing != NULL ? yml_value_as_int(spacing) :
|
|
||||||
left_spacing != NULL ? yml_value_as_int(left_spacing) : 0;
|
|
||||||
int right = spacing != NULL ? yml_value_as_int(spacing) :
|
|
||||||
right_spacing != NULL ? yml_value_as_int(right_spacing) : 0;
|
|
||||||
|
|
||||||
struct i3_workspaces workspaces[yml_dict_length(c)];
|
|
||||||
|
|
||||||
size_t idx = 0;
|
|
||||||
for (struct yml_dict_iter it = yml_dict_iter(c);
|
|
||||||
it.key != NULL;
|
|
||||||
yml_dict_next(&it), idx++)
|
|
||||||
{
|
|
||||||
workspaces[idx].name = yml_value_as_string(it.key);
|
|
||||||
workspaces[idx].content = conf_to_particle(it.value, parent_font);
|
|
||||||
}
|
|
||||||
|
|
||||||
return module_i3(workspaces, yml_dict_length(c), left, right);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct module *
|
static struct module *
|
||||||
module_xkb_from_config(const struct yml_node *node,
|
module_xkb_from_config(const struct yml_node *node,
|
||||||
const struct font *parent_font)
|
const struct font *parent_font)
|
||||||
|
@ -552,14 +525,14 @@ conf_to_bar(const struct yml_node *bar)
|
||||||
mods[idx] = module_battery.from_conf(m.value, font);
|
mods[idx] = module_battery.from_conf(m.value, font);
|
||||||
else if (strcmp(mod_name, "clock") == 0)
|
else if (strcmp(mod_name, "clock") == 0)
|
||||||
mods[idx] = module_clock.from_conf(m.value, font);
|
mods[idx] = module_clock.from_conf(m.value, font);
|
||||||
|
else if (strcmp(mod_name, "i3") == 0)
|
||||||
|
mods[idx] = module_i3.from_conf(m.value, font);
|
||||||
|
|
||||||
else if (strcmp(mod_name, "label") == 0)
|
else if (strcmp(mod_name, "label") == 0)
|
||||||
mods[idx] = module_label_from_config(m.value, font);
|
mods[idx] = module_label_from_config(m.value, font);
|
||||||
else if (strcmp(mod_name, "xwindow") == 0)
|
else if (strcmp(mod_name, "xwindow") == 0)
|
||||||
mods[idx] = module_xwindow_from_config(m.value, font);
|
mods[idx] = module_xwindow_from_config(m.value, font);
|
||||||
else if (strcmp(mod_name, "i3") == 0)
|
else if (strcmp(mod_name, "xkb") == 0)
|
||||||
mods[idx] = module_i3_from_config(m.value, font);
|
|
||||||
else if (strcmp(mod_name, "xkb") == 0)
|
|
||||||
mods[idx] = module_xkb_from_config(m.value, font);
|
mods[idx] = module_xkb_from_config(m.value, font);
|
||||||
else if (strcmp(mod_name, "mpd") == 0)
|
else if (strcmp(mod_name, "mpd") == 0)
|
||||||
mods[idx] = module_mpd_from_config(m.value, font);
|
mods[idx] = module_mpd_from_config(m.value, font);
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
#define LOG_ENABLE_DBG 0
|
#define LOG_ENABLE_DBG 0
|
||||||
#include "../../log.h"
|
#include "../../log.h"
|
||||||
#include "../../bar.h"
|
#include "../../bar.h"
|
||||||
|
#include "../../config.h"
|
||||||
|
|
||||||
#include "../../particles/dynlist.h"
|
#include "../../particles/dynlist.h"
|
||||||
|
|
||||||
|
@ -611,9 +612,15 @@ content(struct module *mod)
|
||||||
particles, particle_count, m->left_spacing, m->right_spacing);
|
particles, particle_count, m->left_spacing, m->right_spacing);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct module *
|
/* Maps workspace name to a content particle. */
|
||||||
module_i3(struct i3_workspaces workspaces[], size_t workspace_count,
|
struct i3_workspaces {
|
||||||
int left_spacing, int right_spacing)
|
const char *name;
|
||||||
|
struct particle *content;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct module *
|
||||||
|
i3_new(struct i3_workspaces workspaces[], size_t workspace_count,
|
||||||
|
int left_spacing, int right_spacing)
|
||||||
{
|
{
|
||||||
struct private *m = malloc(sizeof(*m));
|
struct private *m = malloc(sizeof(*m));
|
||||||
|
|
||||||
|
@ -638,3 +645,73 @@ module_i3(struct i3_workspaces workspaces[], size_t workspace_count,
|
||||||
mod->content = &content;
|
mod->content = &content;
|
||||||
return mod;
|
return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct module *
|
||||||
|
from_conf(const struct yml_node *node, const struct font *parent_font)
|
||||||
|
{
|
||||||
|
const struct yml_node *c = yml_get_value(node, "content");
|
||||||
|
const struct yml_node *spacing = yml_get_value(node, "spacing");
|
||||||
|
const struct yml_node *left_spacing = yml_get_value(node, "left_spacing");
|
||||||
|
const struct yml_node *right_spacing = yml_get_value(node, "right_spacing");
|
||||||
|
|
||||||
|
int left = spacing != NULL ? yml_value_as_int(spacing) :
|
||||||
|
left_spacing != NULL ? yml_value_as_int(left_spacing) : 0;
|
||||||
|
int right = spacing != NULL ? yml_value_as_int(spacing) :
|
||||||
|
right_spacing != NULL ? yml_value_as_int(right_spacing) : 0;
|
||||||
|
|
||||||
|
struct i3_workspaces workspaces[yml_dict_length(c)];
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
for (struct yml_dict_iter it = yml_dict_iter(c);
|
||||||
|
it.key != NULL;
|
||||||
|
yml_dict_next(&it), idx++)
|
||||||
|
{
|
||||||
|
workspaces[idx].name = yml_value_as_string(it.key);
|
||||||
|
workspaces[idx].content = conf_to_particle(it.value, parent_font);
|
||||||
|
}
|
||||||
|
|
||||||
|
return i3_new(workspaces, yml_dict_length(c), left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
verify_content(keychain_t *chain, const struct yml_node *node)
|
||||||
|
{
|
||||||
|
if (!yml_is_dict(node)) {
|
||||||
|
LOG_ERR(
|
||||||
|
"%s: must be a dictionary of workspace-name: particle mappings",
|
||||||
|
conf_err_prefix(chain, node));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (struct yml_dict_iter it = yml_dict_iter(node);
|
||||||
|
it.key != NULL;
|
||||||
|
yml_dict_next(&it))
|
||||||
|
{
|
||||||
|
const char *key = yml_value_as_string(it.key);
|
||||||
|
if (key == NULL) {
|
||||||
|
LOG_ERR("%s: key must be a string (a i3 workspace name)",
|
||||||
|
conf_err_prefix(chain, it.key));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!conf_verify_particle(chain_push(chain, key), it.value))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
chain_pop(chain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct module_info module_i3 = {
|
||||||
|
.from_conf = &from_conf,
|
||||||
|
.attr_count = 5,
|
||||||
|
.attrs = {
|
||||||
|
{"spacing", false, &conf_verify_int},
|
||||||
|
{"left-spacing", false, &conf_verify_int},
|
||||||
|
{"right-spacing", false, &conf_verify_int},
|
||||||
|
{"content", true, &verify_content},
|
||||||
|
{"anchors", false, NULL},
|
||||||
|
{NULL, false, NULL},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
|
@ -1,14 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "../../module.h"
|
#include "../../module.h"
|
||||||
#include "../../particle.h"
|
|
||||||
|
|
||||||
/* Maps workspace name to a content particle. */
|
extern const struct module_info module_i3;
|
||||||
struct i3_workspaces {
|
|
||||||
const char *name;
|
|
||||||
struct particle *content;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct module *module_i3(
|
|
||||||
struct i3_workspaces workspaces[], size_t workspace_count,
|
|
||||||
int left_spacing, int right_spacing);
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue