2020-06-09 21:40:46 +01:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2020-06-05 23:04:54 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <strings.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <libxml/parser.h>
|
|
|
|
|
#include <libxml/tree.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
2020-06-16 07:21:53 +01:00
|
|
|
#include <wayland-server-core.h>
|
2020-06-05 23:04:54 +01:00
|
|
|
|
2020-08-03 20:56:38 +01:00
|
|
|
#include "config/rcxml.h"
|
|
|
|
|
#include "config/keybind.h"
|
2020-08-10 17:24:17 +01:00
|
|
|
#include "common/dir.h"
|
2020-07-31 21:31:03 +01:00
|
|
|
#include "common/bug-on.h"
|
2020-08-05 20:14:17 +01:00
|
|
|
#include "common/font.h"
|
2020-08-12 19:37:44 +01:00
|
|
|
#include "common/log.h"
|
2020-06-05 23:04:54 +01:00
|
|
|
|
|
|
|
|
static bool in_keybind = false;
|
|
|
|
|
static bool is_attribute = false;
|
2020-06-09 22:01:19 +01:00
|
|
|
static bool write_to_nodename_buffer = false;
|
|
|
|
|
static struct buf *nodename_buffer;
|
2020-06-19 22:00:22 +01:00
|
|
|
static struct keybind *current_keybind;
|
2020-06-05 23:04:54 +01:00
|
|
|
|
2020-07-31 11:11:50 +01:00
|
|
|
enum font_place {
|
|
|
|
|
FONT_PLACE_UNKNOWN = 0,
|
|
|
|
|
FONT_PLACE_ACTIVEWINDOW,
|
|
|
|
|
FONT_PLACE_INACTIVEWINDOW,
|
|
|
|
|
/* TODO: Add all places based on Openbox's rc.xml */
|
|
|
|
|
};
|
|
|
|
|
|
2020-06-05 23:04:54 +01:00
|
|
|
static void rstrip(char *buf, const char *pattern)
|
|
|
|
|
{
|
|
|
|
|
char *p = strstr(buf, pattern);
|
|
|
|
|
if (!p)
|
|
|
|
|
return;
|
|
|
|
|
*p = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 11:11:50 +01:00
|
|
|
static void fill_keybind(char *nodename, char *content)
|
2020-06-05 23:04:54 +01:00
|
|
|
{
|
2020-06-19 22:00:22 +01:00
|
|
|
if (!content)
|
|
|
|
|
return;
|
2020-06-05 23:04:54 +01:00
|
|
|
rstrip(nodename, ".keybind.keyboard");
|
2020-08-11 21:12:02 +01:00
|
|
|
if (!strcmp(nodename, "key"))
|
2020-06-19 22:00:22 +01:00
|
|
|
current_keybind = keybind_add(content);
|
|
|
|
|
/* We expect <keybind key=""> to come first */
|
|
|
|
|
BUG_ON(!current_keybind);
|
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);
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool get_bool(const char *s)
|
|
|
|
|
{
|
|
|
|
|
if (!s)
|
|
|
|
|
return false;
|
|
|
|
|
if (!strcasecmp(s, "yes"))
|
|
|
|
|
return true;
|
|
|
|
|
if (!strcasecmp(s, "true"))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 11:11:50 +01:00
|
|
|
static void fill_font(char *nodename, char *content, enum font_place place)
|
|
|
|
|
{
|
|
|
|
|
if (!content)
|
|
|
|
|
return;
|
|
|
|
|
rstrip(nodename, ".font.theme");
|
|
|
|
|
|
|
|
|
|
/* TODO: implement for all font places */
|
|
|
|
|
if (place != FONT_PLACE_ACTIVEWINDOW)
|
|
|
|
|
return;
|
|
|
|
|
if (!strcmp(nodename, "name"))
|
|
|
|
|
rc.font_name_activewindow = strdup(content);
|
|
|
|
|
else if (!strcmp(nodename, "size"))
|
|
|
|
|
rc.font_size_activewindow = atoi(content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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, "InactiveWindow"))
|
|
|
|
|
return FONT_PLACE_INACTIVEWINDOW;
|
|
|
|
|
return FONT_PLACE_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 23:04:54 +01:00
|
|
|
static void entry(xmlNode *node, char *nodename, char *content)
|
|
|
|
|
{
|
2020-07-31 11:11:50 +01:00
|
|
|
/* current <theme><font place=""></theme> */
|
|
|
|
|
static enum font_place font_place = FONT_PLACE_UNKNOWN;
|
|
|
|
|
|
2020-06-05 23:04:54 +01:00
|
|
|
if (!nodename)
|
|
|
|
|
return;
|
|
|
|
|
rstrip(nodename, ".openbox_config");
|
2020-07-31 11:11:50 +01:00
|
|
|
|
|
|
|
|
/* for debugging */
|
2020-06-09 22:01:19 +01:00
|
|
|
if (write_to_nodename_buffer) {
|
2020-06-05 23:04:54 +01:00
|
|
|
if (is_attribute)
|
2020-06-09 22:01:19 +01:00
|
|
|
buf_add(nodename_buffer, "@");
|
|
|
|
|
buf_add(nodename_buffer, nodename);
|
2020-06-09 22:20:24 +01:00
|
|
|
if (content) {
|
|
|
|
|
buf_add(nodename_buffer, ": ");
|
|
|
|
|
buf_add(nodename_buffer, content);
|
|
|
|
|
}
|
2020-06-09 22:01:19 +01:00
|
|
|
buf_add(nodename_buffer, "\n");
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|
2020-07-31 11:11:50 +01:00
|
|
|
|
2020-06-05 23:04:54 +01:00
|
|
|
if (!content)
|
|
|
|
|
return;
|
|
|
|
|
if (in_keybind)
|
2020-07-31 11:11:50 +01:00
|
|
|
fill_keybind(nodename, content);
|
|
|
|
|
|
|
|
|
|
if (is_attribute && !strcmp(nodename, "place.font.theme"))
|
|
|
|
|
font_place = enum_font_place(content);
|
|
|
|
|
|
2020-06-08 19:49:19 +01:00
|
|
|
if (!strcmp(nodename, "csd.lab"))
|
2020-06-05 23:04:54 +01:00
|
|
|
rc.client_side_decorations = get_bool(content);
|
2020-07-09 22:41:54 +01:00
|
|
|
else if (!strcmp(nodename, "layout.keyboard.lab"))
|
2020-06-08 19:49:19 +01:00
|
|
|
setenv("XKB_DEFAULT_LAYOUT", content, 1);
|
2020-07-09 22:41:54 +01:00
|
|
|
else if (!strcmp(nodename, "name.theme"))
|
|
|
|
|
rc.theme_name = strdup(content);
|
2020-07-31 11:11:50 +01:00
|
|
|
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);
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *nodename(xmlNode *node, char *buf, int len)
|
|
|
|
|
{
|
|
|
|
|
if (!node || !node->name)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Ignore superflous 'text.' in node name */
|
|
|
|
|
if (node->parent && !strcmp((char *)node->name, "text"))
|
|
|
|
|
node = node->parent;
|
|
|
|
|
|
|
|
|
|
char *p = buf;
|
|
|
|
|
p[--len] = 0;
|
|
|
|
|
for (;;) {
|
|
|
|
|
const char *name = (char *)node->name;
|
|
|
|
|
char c;
|
|
|
|
|
while ((c = *name++) != 0) {
|
|
|
|
|
*p++ = tolower(c);
|
|
|
|
|
if (!--len)
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
*p = 0;
|
|
|
|
|
node = node->parent;
|
|
|
|
|
if (!node || !node->name)
|
|
|
|
|
return buf;
|
|
|
|
|
*p++ = '.';
|
|
|
|
|
if (!--len)
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void process_node(xmlNode *node)
|
|
|
|
|
{
|
|
|
|
|
char *content;
|
|
|
|
|
static char buffer[256];
|
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
|
|
content = (char *)node->content;
|
|
|
|
|
if (xmlIsBlankNode(node))
|
|
|
|
|
return;
|
|
|
|
|
name = nodename(node, buffer, sizeof(buffer));
|
|
|
|
|
entry(node, name, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void xml_tree_walk(xmlNode *node);
|
|
|
|
|
|
|
|
|
|
static void traverse(xmlNode *n)
|
|
|
|
|
{
|
|
|
|
|
process_node(n);
|
|
|
|
|
is_attribute = true;
|
|
|
|
|
for (xmlAttr *attr = n->properties; attr; attr = attr->next)
|
|
|
|
|
xml_tree_walk(attr->children);
|
|
|
|
|
is_attribute = false;
|
|
|
|
|
xml_tree_walk(n->children);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void xml_tree_walk(xmlNode *node)
|
|
|
|
|
{
|
|
|
|
|
for (xmlNode *n = node; n && n->name; n = n->next) {
|
|
|
|
|
if (!strcasecmp((char *)n->name, "comment"))
|
|
|
|
|
continue;
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 21:40:46 +01:00
|
|
|
/* 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
|
|
|
{
|
2020-06-09 21:40:46 +01:00
|
|
|
xmlDoc *d = xmlParseMemory(b->buf, b->len);
|
2020-06-05 23:04:54 +01:00
|
|
|
if (!d) {
|
2020-08-12 19:37:44 +01:00
|
|
|
warn("xmlParseMemory()");
|
2020-06-05 23:04:54 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
xml_tree_walk(xmlDocGetRootElement(d));
|
|
|
|
|
xmlFreeDoc(d);
|
|
|
|
|
xmlCleanupParser();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 22:00:22 +01:00
|
|
|
static void rcxml_init()
|
2020-06-05 23:04:54 +01:00
|
|
|
{
|
|
|
|
|
LIBXML_TEST_VERSION
|
2020-06-19 22:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bind(const char *binding, const char *action)
|
|
|
|
|
{
|
|
|
|
|
if (!binding || !action)
|
|
|
|
|
return;
|
|
|
|
|
struct keybind *k = keybind_add(binding);
|
|
|
|
|
if (k)
|
|
|
|
|
k->action = strdup(action);
|
2020-08-12 19:37:44 +01:00
|
|
|
info("binding %s: %s", binding, action);
|
2020-06-19 22:00:22 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-05 20:14:17 +01:00
|
|
|
static void set_title_height(void)
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s %d", rc.font_name_activewindow,
|
|
|
|
|
rc.font_size_activewindow);
|
|
|
|
|
rc.title_height = font_height(buf);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 22:00:22 +01:00
|
|
|
static void post_processing(void)
|
|
|
|
|
{
|
|
|
|
|
if (!wl_list_length(&rc.keybinds)) {
|
2020-08-12 19:37:44 +01:00
|
|
|
info("loading default key bindings");
|
2020-06-19 22:00:22 +01:00
|
|
|
bind("A-Escape", "Exit");
|
|
|
|
|
bind("A-Tab", "NextWindow");
|
|
|
|
|
bind("A-F3", "Execute");
|
|
|
|
|
}
|
2020-07-31 11:11:50 +01:00
|
|
|
/* TODO: Set all char* variables if NULL */
|
2020-08-05 20:14:17 +01:00
|
|
|
|
|
|
|
|
set_title_height();
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-18 11:28:39 +01:00
|
|
|
static void rcxml_path(char *buf, size_t len, const char *filename)
|
|
|
|
|
{
|
|
|
|
|
if (filename)
|
|
|
|
|
snprintf(buf, len, "%s", filename);
|
|
|
|
|
else
|
|
|
|
|
snprintf(buf, len, "%s/rc.xml", config_dir());
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 23:04:54 +01:00
|
|
|
void rcxml_read(const char *filename)
|
|
|
|
|
{
|
2020-06-09 21:40:46 +01:00
|
|
|
FILE *stream;
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
struct buf b;
|
2020-07-18 11:28:39 +01:00
|
|
|
char rcxml[4096];
|
2020-06-09 21:40:46 +01:00
|
|
|
|
2020-06-19 22:00:22 +01:00
|
|
|
rcxml_init();
|
|
|
|
|
wl_list_init(&rc.keybinds);
|
|
|
|
|
|
2020-07-18 11:28:39 +01:00
|
|
|
/*
|
|
|
|
|
* Reading file into buffer before parsing makes it easier to write
|
|
|
|
|
* unit tests.
|
|
|
|
|
*/
|
|
|
|
|
rcxml_path(rcxml, sizeof(rcxml), filename);
|
2020-08-12 19:37:44 +01:00
|
|
|
info("reading config file (%s)", rcxml);
|
2020-07-18 11:28:39 +01:00
|
|
|
stream = fopen(rcxml, "r");
|
2020-06-09 21:40:46 +01:00
|
|
|
if (!stream) {
|
2020-08-12 19:37:44 +01:00
|
|
|
warn("cannot read (%s)", rcxml);
|
2020-06-19 22:00:22 +01:00
|
|
|
goto out;
|
2020-06-09 21:40:46 +01:00
|
|
|
}
|
|
|
|
|
buf_init(&b);
|
2020-06-18 20:39:55 +01:00
|
|
|
while (getline(&line, &len, stream) != -1) {
|
2020-06-09 21:40:46 +01:00
|
|
|
char *p = strrchr(line, '\n');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
buf_add(&b, line);
|
|
|
|
|
}
|
|
|
|
|
free(line);
|
|
|
|
|
fclose(stream);
|
|
|
|
|
rcxml_parse_xml(&b);
|
|
|
|
|
free(b.buf);
|
2020-06-19 22:00:22 +01:00
|
|
|
out:
|
|
|
|
|
post_processing();
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-13 20:18:48 +01:00
|
|
|
static void free_safe(const void *p)
|
|
|
|
|
{
|
|
|
|
|
if (p)
|
|
|
|
|
free((void *)p);
|
|
|
|
|
p = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void rcxml_finish(void)
|
|
|
|
|
{
|
|
|
|
|
free_safe(rc.font_name_activewindow);
|
|
|
|
|
free_safe(rc.theme_name);
|
|
|
|
|
|
|
|
|
|
struct keybind *k, *k_tmp;
|
|
|
|
|
wl_list_for_each_safe (k, k_tmp, &rc.keybinds, link) {
|
|
|
|
|
wl_list_remove(&k->link);
|
|
|
|
|
free_safe(k->command);
|
|
|
|
|
free_safe(k->action);
|
|
|
|
|
free_safe(k->keysyms);
|
|
|
|
|
free_safe(k);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 22:01:19 +01:00
|
|
|
void rcxml_get_nodenames(struct buf *b)
|
2020-06-05 23:04:54 +01:00
|
|
|
{
|
2020-06-09 22:01:19 +01:00
|
|
|
write_to_nodename_buffer = true;
|
|
|
|
|
nodename_buffer = b;
|
2020-06-05 23:04:54 +01:00
|
|
|
}
|