labwc/src/config/rcxml.c

404 lines
8.7 KiB
C
Raw Normal View History

#define _POSIX_C_SOURCE 200809L
2020-09-28 20:53:59 +01:00
#include <assert.h>
2020-06-05 23:04:54 +01:00
#include <ctype.h>
#include <fcntl.h>
2020-06-05 23:04:54 +01:00
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
2020-06-05 23:04:54 +01:00
#include <unistd.h>
2020-06-16 07:21:53 +01:00
#include <wayland-server-core.h>
2021-07-22 21:30:17 +01:00
#include <wlr/util/log.h>
#include "common/dir.h"
2021-02-16 21:04:49 +00:00
#include "common/nodename.h"
#include "common/string-helpers.h"
2021-02-21 21:59:53 +00:00
#include "common/zfree.h"
#include "config/keybind.h"
#include "config/rcxml.h"
2020-06-05 23:04:54 +01:00
static bool in_keybind = false;
static bool is_attribute = false;
2020-06-19 22:00:22 +01:00
static struct keybind *current_keybind;
2020-06-05 23:04:54 +01:00
enum font_place {
FONT_PLACE_UNKNOWN = 0,
FONT_PLACE_ACTIVEWINDOW,
FONT_PLACE_MENUITEM,
/* TODO: Add all places based on Openbox's rc.xml */
};
static void load_default_key_bindings(void);
static void
fill_keybind(char *nodename, char *content)
2020-06-05 23:04:54 +01:00
{
if (!content) {
2020-06-19 22:00:22 +01:00
return;
}
string_truncate_at_pattern(nodename, ".keybind.keyboard");
if (!strcmp(nodename, "key")) {
2020-09-02 21:05:28 +01:00
current_keybind = keybind_create(content);
}
2021-07-19 07:07:33 +01:00
/*
* We expect <keybind key=""> to come first
* If a invalid keybind has been provided, keybind_create() complains
* so we just silently ignore it here.
*/
if (!current_keybind) {
return;
}
2020-06-05 23:04:54 +01:00
if (!strcmp(nodename, "name.action")) {
2020-06-19 22:00:22 +01:00
current_keybind->action = strdup(content);
} else if (!strcmp(nodename, "command.action")) {
current_keybind->command = strdup(content);
} else if (!strcmp(nodename, "direction.action")) {
current_keybind->command = strdup(content);
} else if (!strcmp(nodename, "menu.action")) {
current_keybind->command = strdup(content);
2020-06-05 23:04:54 +01:00
}
}
static bool
get_bool(const char *s)
2020-06-05 23:04:54 +01:00
{
if (!s) {
2020-06-05 23:04:54 +01:00
return false;
}
if (!strcasecmp(s, "yes")) {
2020-06-05 23:04:54 +01:00
return true;
}
if (!strcasecmp(s, "true")) {
2020-06-05 23:04:54 +01:00
return true;
}
2020-06-05 23:04:54 +01:00
return false;
}
static void
fill_font(char *nodename, char *content, enum font_place place)
{
if (!content) {
return;
}
string_truncate_at_pattern(nodename, ".font.theme");
switch (place) {
case FONT_PLACE_UNKNOWN:
/*
* If <theme><font></font></theme> is used without a place=""
* attribute, we set all font variables
*/
if (!strcmp(nodename, "name")) {
rc.font_name_activewindow = strdup(content);
rc.font_name_menuitem = strdup(content);
} else if (!strcmp(nodename, "size")) {
rc.font_size_activewindow = atoi(content);
rc.font_size_menuitem = atoi(content);
}
break;
case FONT_PLACE_ACTIVEWINDOW:
if (!strcmp(nodename, "name")) {
rc.font_name_activewindow = strdup(content);
} else if (!strcmp(nodename, "size")) {
rc.font_size_activewindow = atoi(content);
}
break;
case FONT_PLACE_MENUITEM:
if (!strcmp(nodename, "name")) {
rc.font_name_menuitem = strdup(content);
} else if (!strcmp(nodename, "size")) {
rc.font_size_menuitem = atoi(content);
}
break;
/* TODO: implement for all font places */
default:
break;
}
}
static enum font_place
enum_font_place(const char *place)
{
if (!place) {
return FONT_PLACE_UNKNOWN;
}
if (!strcasecmp(place, "ActiveWindow")) {
return FONT_PLACE_ACTIVEWINDOW;
} else if (!strcasecmp(place, "MenuItem")) {
return FONT_PLACE_MENUITEM;
}
return FONT_PLACE_UNKNOWN;
}
static void
entry(xmlNode *node, char *nodename, char *content)
2020-06-05 23:04:54 +01:00
{
/* current <theme><font place=""></font></theme> */
static enum font_place font_place = FONT_PLACE_UNKNOWN;
if (!nodename) {
2020-06-05 23:04:54 +01:00
return;
}
string_truncate_at_pattern(nodename, ".openbox_config");
2021-07-16 17:30:07 +01:00
string_truncate_at_pattern(nodename, ".labwc_config");
2021-03-30 22:40:41 +01:00
if (getenv("LABWC_DEBUG_CONFIG_NODENAMES")) {
if (is_attribute) {
2021-03-30 22:40:41 +01:00
printf("@");
}
2021-03-30 22:40:41 +01:00
printf("%s: %s\n", nodename, content);
2020-06-05 23:04:54 +01:00
}
/* handle nodes without content, e.g. <keyboard><default /> */
if (!strcmp(nodename, "default.keyboard")) {
load_default_key_bindings();
return;
}
/* handle the rest */
if (!content) {
2020-06-05 23:04:54 +01:00
return;
}
if (in_keybind) {
fill_keybind(nodename, content);
}
if (is_attribute && !strcmp(nodename, "place.font.theme")) {
font_place = enum_font_place(content);
}
if (!strcmp(nodename, "decoration.core")) {
if (!strcmp(content, "client")) {
rc.xdg_shell_server_side_deco = false;
} else {
rc.xdg_shell_server_side_deco = true;
}
} else if (!strcmp(nodename, "gap.core")) {
rc.gap = atoi(content);
} else if (!strcmp(nodename, "name.theme")) {
rc.theme_name = strdup(content);
2021-03-30 22:39:52 +01:00
} else if (!strcmp(nodename, "cornerradius.theme")) {
rc.corner_radius = atoi(content);
} else if (!strcmp(nodename, "name.font.theme")) {
fill_font(nodename, content, font_place);
} else if (!strcmp(nodename, "size.font.theme")) {
fill_font(nodename, content, font_place);
2021-05-27 02:11:11 +03:00
} else if (!strcasecmp(nodename, "FollowMouse.focus")) {
rc.focus_follow_mouse = get_bool(content);
2021-05-28 21:31:02 +01:00
} else if (!strcasecmp(nodename, "RaiseOnFocus.focus")) {
2021-05-27 02:11:11 +03:00
rc.focus_follow_mouse = true;
rc.raise_on_focus = get_bool(content);
}
2020-06-05 23:04:54 +01:00
}
static void
process_node(xmlNode *node)
2020-06-05 23:04:54 +01:00
{
char *content;
static char buffer[256];
char *name;
content = (char *)node->content;
if (xmlIsBlankNode(node)) {
2020-06-05 23:04:54 +01:00
return;
}
2020-06-05 23:04:54 +01:00
name = nodename(node, buffer, sizeof(buffer));
entry(node, name, content);
}
static void xml_tree_walk(xmlNode *node);
static void
traverse(xmlNode *n)
2020-06-05 23:04:54 +01:00
{
process_node(n);
is_attribute = true;
for (xmlAttr *attr = n->properties; attr; attr = attr->next) {
2020-06-05 23:04:54 +01:00
xml_tree_walk(attr->children);
}
2020-06-05 23:04:54 +01:00
is_attribute = false;
xml_tree_walk(n->children);
}
static void
xml_tree_walk(xmlNode *node)
2020-06-05 23:04:54 +01:00
{
for (xmlNode *n = node; n && n->name; n = n->next) {
if (!strcasecmp((char *)n->name, "comment")) {
2020-06-05 23:04:54 +01:00
continue;
}
2020-06-05 23:04:54 +01:00
if (!strcasecmp((char *)n->name, "keybind")) {
2020-06-19 22:00:22 +01:00
in_keybind = true;
2020-06-05 23:04:54 +01:00
traverse(n);
2020-06-19 22:00:22 +01:00
in_keybind = false;
2020-06-05 23:04:54 +01:00
continue;
}
traverse(n);
}
}
/* Exposed in header file to allow unit tests to parse buffers */
void
rcxml_parse_xml(struct buf *b)
2020-06-05 23:04:54 +01:00
{
xmlDoc *d = xmlParseMemory(b->buf, b->len);
2020-06-05 23:04:54 +01:00
if (!d) {
wlr_log(WLR_ERROR, "xmlParseMemory()");
2020-06-05 23:04:54 +01:00
exit(EXIT_FAILURE);
}
xml_tree_walk(xmlDocGetRootElement(d));
xmlFreeDoc(d);
xmlCleanupParser();
}
static void
rcxml_init()
2020-06-05 23:04:54 +01:00
{
2020-09-25 19:42:40 +01:00
static bool has_run;
if (has_run) {
2020-09-25 19:42:40 +01:00
return;
}
2020-09-25 19:42:40 +01:00
has_run = true;
2020-06-05 23:04:54 +01:00
LIBXML_TEST_VERSION
2020-09-25 19:42:40 +01:00
wl_list_init(&rc.keybinds);
2021-03-30 22:39:52 +01:00
rc.xdg_shell_server_side_deco = true;
rc.corner_radius = 8;
rc.font_size_activewindow = 10;
rc.font_size_menuitem = 10;
2020-06-19 22:00:22 +01:00
}
static void
bind(const char *binding, const char *action, const char *command)
2020-06-19 22:00:22 +01:00
{
if (!binding || !action) {
2020-06-19 22:00:22 +01:00
return;
}
2020-09-02 21:05:28 +01:00
struct keybind *k = keybind_create(binding);
if (!k) {
return;
}
if (action) {
2020-06-19 22:00:22 +01:00
k->action = strdup(action);
}
if (command) {
k->command = strdup(command);
}
2020-06-19 22:00:22 +01:00
}
static void
load_default_key_bindings(void)
{
bind("A-Tab", "NextWindow", NULL);
bind("A-Escape", "Exit", NULL);
bind("W-Return", "Execute", "alacritty");
bind("A-F3", "Execute", "bemenu-run");
bind("A-F4", "Close", NULL);
bind("W-a", "ToggleMaximize", NULL);
bind("A-Left", "MoveToEdge", "left");
bind("A-Right", "MoveToEdge", "right");
bind("A-Up", "MoveToEdge", "up");
bind("A-Down", "MoveToEdge", "down");
}
static void
post_processing(void)
2020-06-19 22:00:22 +01:00
{
if (!wl_list_length(&rc.keybinds)) {
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "load default key bindings");
load_default_key_bindings();
2020-06-19 22:00:22 +01:00
}
if (!rc.font_name_activewindow) {
rc.font_name_activewindow = strdup("sans");
}
if (!rc.font_name_menuitem) {
rc.font_name_menuitem = strdup("sans");
}
2020-06-05 23:04:54 +01:00
}
static void
rcxml_path(char *buf, size_t len)
2020-07-18 11:28:39 +01:00
{
if (!strlen(config_dir())) {
return;
}
snprintf(buf, len, "%s/rc.xml", config_dir());
2020-07-18 11:28:39 +01:00
}
static void
find_config_file(char *buffer, size_t len, const char *filename)
2020-09-25 19:42:40 +01:00
{
if (filename) {
snprintf(buffer, len, "%s", filename);
return;
}
rcxml_path(buffer, len);
}
void
rcxml_read(const char *filename)
2020-06-05 23:04:54 +01:00
{
FILE *stream;
char *line = NULL;
size_t len = 0;
struct buf b;
2020-09-25 19:42:40 +01:00
static char rcxml[4096] = { 0 };
2020-06-19 22:00:22 +01:00
rcxml_init();
2020-07-18 11:28:39 +01:00
/*
2020-09-25 19:42:40 +01:00
* rcxml_read() can be called multiple times, but we only set rcxml[]
* the first time. The specified 'filename' is only respected the first
* time.
2020-07-18 11:28:39 +01:00
*/
if (rcxml[0] == '\0') {
2020-09-25 19:42:40 +01:00
find_config_file(rcxml, sizeof(rcxml), filename);
}
if (rcxml[0] == '\0') {
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "cannot find rc.xml config file");
goto no_config;
}
2020-09-25 19:42:40 +01:00
/* Reading file into buffer before parsing - better for unit tests */
2020-07-18 11:28:39 +01:00
stream = fopen(rcxml, "r");
if (!stream) {
wlr_log(WLR_ERROR, "cannot read (%s)", rcxml);
goto no_config;
}
2021-07-22 21:30:17 +01:00
wlr_log(WLR_INFO, "read config file %s", rcxml);
buf_init(&b);
while (getline(&line, &len, stream) != -1) {
char *p = strrchr(line, '\n');
if (p)
*p = '\0';
buf_add(&b, line);
}
free(line);
fclose(stream);
rcxml_parse_xml(&b);
free(b.buf);
no_config:
2020-06-19 22:00:22 +01:00
post_processing();
2020-06-05 23:04:54 +01:00
}
void
rcxml_finish(void)
2020-08-13 20:18:48 +01:00
{
2021-02-21 21:59:53 +00:00
zfree(rc.font_name_activewindow);
zfree(rc.font_name_menuitem);
2021-02-21 21:59:53 +00:00
zfree(rc.theme_name);
2020-08-13 20:18:48 +01:00
struct keybind *k, *k_tmp;
wl_list_for_each_safe (k, k_tmp, &rc.keybinds, link) {
wl_list_remove(&k->link);
2021-02-21 21:59:53 +00:00
zfree(k->command);
zfree(k->action);
zfree(k->keysyms);
zfree(k);
2020-08-13 20:18:48 +01:00
}
}