mirror of
https://github.com/labwc/labwc.git
synced 2026-02-15 22:05:25 -05:00
Add src/config/config-dir.c
This commit is contained in:
parent
7fc643fdb9
commit
ccc818fda5
5 changed files with 102 additions and 6 deletions
6
include/config/config-dir.h
Normal file
6
include/config/config-dir.h
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
#ifndef CONFIG_DIR_H
|
||||||
|
#define CONFIG_DIR_H
|
||||||
|
|
||||||
|
char *config_dir(void);
|
||||||
|
|
||||||
|
#endif /* CONFIG_DIR_H */
|
||||||
70
src/config/config-dir.c
Normal file
70
src/config/config-dir.c
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* Find the labwc configuration directory
|
||||||
|
*
|
||||||
|
* Copyright Johan Malm 2020
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include "config/config-dir.h"
|
||||||
|
|
||||||
|
struct dir {
|
||||||
|
const char *prefix;
|
||||||
|
const char *path;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* clang-format off */
|
||||||
|
static struct dir config_dirs[] = {
|
||||||
|
{ "XDG_CONFIG_HOME", "labwc" },
|
||||||
|
{ "HOME", ".config/labwc" },
|
||||||
|
{ "XDG_CONFIG_DIRS", "labwc" },
|
||||||
|
{ NULL, "/etc/xdg/labwc" },
|
||||||
|
{ "XDG_CONFIG_HOME", "openbox" },
|
||||||
|
{ "HOME", ".config/openbox" },
|
||||||
|
{ "XDG_CONFIG_DIRS", "openbox" },
|
||||||
|
{ NULL, "/etc/xdg/openbox" },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
/* clang-format on */
|
||||||
|
|
||||||
|
static bool isdir(const char *path)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
return (!stat(path, &st) && S_ISDIR(st.st_mode));
|
||||||
|
}
|
||||||
|
|
||||||
|
char *config_dir(void)
|
||||||
|
{
|
||||||
|
static char buf[4096] = { 0 };
|
||||||
|
if (buf[0] != '\0')
|
||||||
|
return buf;
|
||||||
|
|
||||||
|
for (int i = 0; config_dirs[i].path; i++) {
|
||||||
|
struct dir d = config_dirs[i];
|
||||||
|
if (!d.prefix) {
|
||||||
|
/* handle /etc/xdg... */
|
||||||
|
snprintf(buf, sizeof(buf), "%s", d.path);
|
||||||
|
if (isdir(buf))
|
||||||
|
return buf;
|
||||||
|
} else {
|
||||||
|
/* handle $HOME/.config/... and $XDG_* */
|
||||||
|
char *prefix = getenv(d.prefix);
|
||||||
|
if (!prefix)
|
||||||
|
continue;
|
||||||
|
gchar **prefixes = g_strsplit(prefix, ":", -1);
|
||||||
|
for (gchar **p = prefixes; *p; p++) {
|
||||||
|
snprintf(buf, sizeof(buf), "%s/%s", *p, d.path);
|
||||||
|
if (isdir(buf))
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* no config directory was found */
|
||||||
|
buf[0] = '.';
|
||||||
|
buf[1] = '\0';
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
labwc_sources += files(
|
labwc_sources += files(
|
||||||
|
'config-dir.c',
|
||||||
'rcxml.c',
|
'rcxml.c',
|
||||||
'keybind.c',
|
'keybind.c',
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
#include <wayland-server-core.h>
|
#include <wayland-server-core.h>
|
||||||
|
|
||||||
#include "rcxml.h"
|
#include "rcxml.h"
|
||||||
|
#include "config/config-dir.h"
|
||||||
|
|
||||||
static bool in_keybind = false;
|
static bool in_keybind = false;
|
||||||
static bool is_attribute = false;
|
static bool is_attribute = false;
|
||||||
|
|
@ -192,20 +193,34 @@ static void post_processing(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
void rcxml_read(const char *filename)
|
void rcxml_read(const char *filename)
|
||||||
{
|
{
|
||||||
FILE *stream;
|
FILE *stream;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
struct buf b;
|
struct buf b;
|
||||||
|
char rcxml[4096];
|
||||||
|
|
||||||
rcxml_init();
|
rcxml_init();
|
||||||
wl_list_init(&rc.keybinds);
|
wl_list_init(&rc.keybinds);
|
||||||
|
|
||||||
/* Read <filename> into buffer and then call rcxml_parse_xml() */
|
/*
|
||||||
stream = fopen(filename, "r");
|
* Reading file into buffer before parsing makes it easier to write
|
||||||
|
* unit tests.
|
||||||
|
*/
|
||||||
|
rcxml_path(rcxml, sizeof(rcxml), filename);
|
||||||
|
fprintf(stderr, "info: read config file (%s)\n", rcxml);
|
||||||
|
stream = fopen(rcxml, "r");
|
||||||
if (!stream) {
|
if (!stream) {
|
||||||
fprintf(stderr, "warn: cannot read '%s'\n", filename);
|
fprintf(stderr, "warn: cannot read '%s'\n", rcxml);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
buf_init(&b);
|
buf_init(&b);
|
||||||
|
|
|
||||||
10
src/main.c
10
src/main.c
|
|
@ -8,7 +8,7 @@ struct rcxml rc = { 0 };
|
||||||
struct theme theme = { 0 };
|
struct theme theme = { 0 };
|
||||||
|
|
||||||
static const char labwc_usage[] =
|
static const char labwc_usage[] =
|
||||||
"Usage: labwc [-h] [-s <command>]\n";
|
"Usage: labwc [-h] [-s <startup-command>] [-c <config-file>]\n";
|
||||||
|
|
||||||
static void usage(void)
|
static void usage(void)
|
||||||
{
|
{
|
||||||
|
|
@ -19,14 +19,18 @@ static void usage(void)
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *startup_cmd = NULL;
|
char *startup_cmd = NULL;
|
||||||
|
char *config_file = NULL;
|
||||||
wlr_log_init(WLR_ERROR, NULL);
|
wlr_log_init(WLR_ERROR, NULL);
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
while ((c = getopt(argc, argv, "s:h")) != -1) {
|
while ((c = getopt(argc, argv, "s:c:h")) != -1) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 's':
|
case 's':
|
||||||
startup_cmd = optarg;
|
startup_cmd = optarg;
|
||||||
break;
|
break;
|
||||||
|
case 'c':
|
||||||
|
config_file = optarg;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
usage();
|
usage();
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +38,7 @@ int main(int argc, char *argv[])
|
||||||
if (optind < argc)
|
if (optind < argc)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
rcxml_read("data/rc.xml");
|
rcxml_read(config_file);
|
||||||
|
|
||||||
/* Wayland requires XDG_RUNTIME_DIR to be set */
|
/* Wayland requires XDG_RUNTIME_DIR to be set */
|
||||||
if (!getenv("XDG_RUNTIME_DIR")) {
|
if (!getenv("XDG_RUNTIME_DIR")) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue