]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Simplify and merge {config,theme}-dir.c
authorJohan Malm <jgm323@gmail.com>
Mon, 10 Aug 2020 16:24:17 +0000 (17:24 +0100)
committerJohan Malm <jgm323@gmail.com>
Mon, 10 Aug 2020 16:24:17 +0000 (17:24 +0100)
20 files changed:
include/common/dir.h [moved from include/theme/theme-dir.h with 61% similarity]
include/config/config-dir.h [deleted file]
src/common/dir.c [new file with mode: 0644]
src/common/meson.build
src/config/config-dir.c [deleted file]
src/config/meson.build
src/config/rcxml.c
src/theme/meson.build
src/theme/theme-dir.c [deleted file]
src/theme/theme.c
src/theme/xbm/xbm.c
tests/meson.build
tools/dirs/Makefile [new file with mode: 0644]
tools/dirs/dir-list [new file with mode: 0755]
tools/dirs/dir-list.c [new file with mode: 0644]
tools/rcxml/Makefile
tools/theme/Makefile
tools/xbm/Makefile
tools/xbm/xbm-parse.c
tools/xbm/xbm-tokenize.c

similarity index 61%
rename from include/theme/theme-dir.h
rename to include/common/dir.h
index 310e60779a844807d6a54b96ede1ceb8930ad9f3..e654664d9fd2750b51c47d72fc5e5c307ea02524 100644 (file)
@@ -1,5 +1,7 @@
-#ifndef __LABWC_THEME_DIR_H
-#define __LABWC_THEME_DIR_H
+#ifndef __LABWC_DIR_H
+#define __LABWC_DIR_H
+
+char *config_dir(void);
 
 /**
  * theme_dir - find theme directory containing theme @theme_name
@@ -7,4 +9,4 @@
  */
 char *theme_dir(const char *theme_name);
 
-#endif /* __LABWC_THEME_DIR_H */
+#endif /* __LABWC_DIR_H */
diff --git a/include/config/config-dir.h b/include/config/config-dir.h
deleted file mode 100644 (file)
index 19ae130..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef __LABWC_CONFIG_DIR_H
-#define __LABWC_CONFIG_DIR_H
-
-char *config_dir(void);
-
-#endif /* __LABWC_CONFIG_DIR_H */
diff --git a/src/common/dir.c b/src/common/dir.c
new file mode 100644 (file)
index 0000000..aa8cadc
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Find the configuration and theme directories
+ *
+ * Copyright Johan Malm 2020
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <stdbool.h>
+#include <glib.h>
+
+#include "common/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 }
+};
+
+static struct dir theme_dirs[] = {
+       { "XDG_DATA_HOME", "themes" },
+       { "HOME", ".local/share/themes" },
+       { "HOME", ".themes" },
+       { "XDG_DATA_DIRS", "themes" },
+       { NULL, "/usr/share/themes" },
+       { NULL, "/usr/local/share/themes" },
+       { NULL, "opt/share/themes" },
+       { NULL, NULL }
+};
+/* clang-format on */
+
+static bool isdir(const char *path)
+{
+       struct stat st;
+       return (!stat(path, &st) && S_ISDIR(st.st_mode));
+}
+
+struct ctx {
+       void (*build_path_fn)(struct ctx *ctx, char *prefix, const char *path);
+       char *buf;
+       size_t len;
+       struct dir *dirs;
+       const char *theme_name;
+};
+
+static void build_config_path(struct ctx *ctx, char *prefix, const char *path)
+{
+       if (!prefix)
+               snprintf(ctx->buf, ctx->len, "%s", path);
+       else
+               snprintf(ctx->buf, ctx->len, "%s/%s", prefix, path);
+}
+
+static void build_theme_path(struct ctx *ctx, char *prefix, const char *path)
+{
+       if (!prefix)
+               snprintf(ctx->buf, ctx->len, "%s/%s/openbox-3", path,
+                        ctx->theme_name);
+       else
+               snprintf(ctx->buf, ctx->len, "%s/%s/%s/openbox-3", prefix, path,
+                        ctx->theme_name);
+}
+
+char *find_dir(struct ctx *ctx)
+{
+       char *debug = getenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME");
+
+       for (int i = 0; ctx->dirs[i].path; i++) {
+               struct dir d = ctx->dirs[i];
+               if (!d.prefix) {
+                       /* handle /etc/xdg... */
+                       ctx->build_path_fn(ctx, NULL, d.path);
+                       if (debug)
+                               fprintf(stderr, "DEBUG: %s\n", ctx->buf);
+                       if (isdir(ctx->buf))
+                               return ctx->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++) {
+                               ctx->build_path_fn(ctx, *p, d.path);
+                               if (debug)
+                                       fprintf(stderr, "DEBUG: %s\n",
+                                               ctx->buf);
+                               if (isdir(ctx->buf))
+                                       return ctx->buf;
+                       }
+               }
+       }
+       /* no directory was found */
+       ctx->buf[0] = '.';
+       ctx->buf[1] = '\0';
+       return ctx->buf;
+}
+
+char *config_dir(void)
+{
+       static char buf[4096] = { 0 };
+       if (buf[0] != '\0')
+               return buf;
+       struct ctx ctx = { .build_path_fn = build_config_path,
+                          .buf = buf,
+                          .len = sizeof(buf),
+                          .dirs = config_dirs };
+       return find_dir(&ctx);
+}
+
+char *theme_dir(const char *theme_name)
+{
+       static char buf[4096] = { 0 };
+       if (buf[0] != '\0')
+               return buf;
+       struct ctx ctx = { .build_path_fn = build_theme_path,
+                          .buf = buf,
+                          .len = sizeof(buf),
+                          .dirs = theme_dirs,
+                          .theme_name = theme_name };
+       return find_dir(&ctx);
+}
index 4a471ab1a83ee79762184c9b52a2576c669e3b62..48e1dd56a7320c1f2feb871d6e4b9faf10fbb979 100644 (file)
@@ -1,5 +1,6 @@
 labwc_sources += files(
   'buf.c',
+  'dir.c',
   'font.c',
   'grab-file.c',
   'spawn.c',
diff --git a/src/config/config-dir.c b/src/config/config-dir.c
deleted file mode 100644 (file)
index f506b96..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * 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;
-}
index 97a67693f7cb8836b20c2022c07d8cd6394c325c..ac8e60128c169f345902095f04d55d1cf43f57d2 100644 (file)
@@ -1,5 +1,4 @@
 labwc_sources += files(
-  'config-dir.c',
   'rcxml.c',
   'keybind.c',
 )
index b5c86842890864182d6823ce772749eb7b793dc4..8b2c369849fcf776038c21d6600793fb5a7b59e3 100644 (file)
@@ -12,7 +12,7 @@
 
 #include "config/rcxml.h"
 #include "config/keybind.h"
-#include "config/config-dir.h"
+#include "common/dir.h"
 #include "common/bug-on.h"
 #include "common/font.h"
 
index bbf34363b7e7d3282173f4e436c767765a6f5c13..e7181a0668521beb7c310cef0caeba8468fd082f 100644 (file)
@@ -1,6 +1,5 @@
 labwc_sources += files(
   'theme.c',
-  'theme-dir.c',
 )
 
 subdir('xbm')
diff --git a/src/theme/theme-dir.c b/src/theme/theme-dir.c
deleted file mode 100644 (file)
index ee8b584..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Find the openbox theme directory
- *
- * Copyright Johan Malm 2020
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/stat.h>
-#include <stdbool.h>
-#include <glib.h>
-
-struct dir {
-       const char *prefix;
-       const char *path;
-};
-
-/* clang-format off */
-static struct dir theme_dirs[] = {
-       { "XDG_DATA_HOME", "themes" },
-       { "HOME", ".local/share/themes" },
-       { "HOME", ".themes" },
-       { "XDG_DATA_DIRS", "themes" },
-       { NULL, "/usr/share/themes" },
-       { NULL, "/usr/local/share/themes" },
-       { NULL, "opt/share/themes" },
-       { NULL, NULL }
-};
-/* clang-format on */
-
-static bool isdir(const char *path)
-{
-       struct stat st;
-       return (!stat(path, &st) && S_ISDIR(st.st_mode));
-}
-
-char *theme_dir(const char *theme_name)
-{
-       static char buf[4096] = { 0 };
-       if (buf[0] != '\0')
-               return buf;
-
-       for (int i = 0; theme_dirs[i].path; i++) {
-               struct dir d = theme_dirs[i];
-               if (!d.prefix) {
-                       snprintf(buf, sizeof(buf), "%s/%s/openbox-3", d.path,
-                                theme_name);
-                       if (isdir(buf))
-                               return buf;
-               } else {
-                       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/%s/openbox-3", *p, d.path,
-                                        theme_name);
-                               if (isdir(buf))
-                                       return buf;
-                       }
-               }
-       }
-       /* no config directory was found */
-       buf[0] = '.';
-       buf[1] = '\0';
-       return buf;
-}
index 8175482dddc1681404bd503c0f188acc08bb1d27..c2ce613539cbbd2ef411b917b6501eb6347a8fc6 100644 (file)
@@ -7,7 +7,7 @@
 #include <glib.h>
 
 #include "theme/theme.h"
-#include "theme/theme-dir.h"
+#include "common/dir.h"
 
 static int hex_to_dec(char c)
 {
index 925811468c90d32942c15014ab1a544a40519f83..d1256050fc1208b1e01d11c4f1d3412f325f1fcc 100644 (file)
@@ -10,8 +10,8 @@
 #include "theme/theme.h"
 #include "theme/xbm/xbm.h"
 #include "theme/xbm/parse.h"
-#include "theme/theme-dir.h"
 #include "config/rcxml.h"
+#include "common/dir.h"
 #include "common/grab-file.h"
 
 /* built-in 6x6 buttons */
index eedb43a9ff70d480356c262b1b82e1c5a4fe94b1..44756a17c7655c5f039ce5da640684588ff751f7 100644 (file)
@@ -3,7 +3,7 @@ rcxml_lib = static_library(
   sources: files(
     '../src/config/rcxml.c',
     '../src/config/keybind.c',
-    '../src/config/config-dir.c',
+    '../src/common/dir.c',
     '../src/common/buf.c',
     '../src/common/font.c',
   ),
diff --git a/tools/dirs/Makefile b/tools/dirs/Makefile
new file mode 100644 (file)
index 0000000..3633b5c
--- /dev/null
@@ -0,0 +1,6 @@
+CFLAGS  += -g -Wall -I../../include
+CFLAGS  += `pkg-config --cflags glib-2.0`
+LDFLAGS += `pkg-config --libs glib-2.0`
+
+all:
+       $(CC) $(CFLAGS) -o dir-list dir-list.c ../../src/common/dir.c $(LDFLAGS)
diff --git a/tools/dirs/dir-list b/tools/dirs/dir-list
new file mode 100755 (executable)
index 0000000..2dea97f
Binary files /dev/null and b/tools/dirs/dir-list differ
diff --git a/tools/dirs/dir-list.c b/tools/dirs/dir-list.c
new file mode 100644 (file)
index 0000000..eb7eb00
--- /dev/null
@@ -0,0 +1,12 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "common/dir.h"
+
+int main()
+{
+       setenv("LABWC_DEBUG_DIR_CONFIG_AND_THEME", "1", 1);
+       setenv("XDG_CONFIG_HOME", "/a:/bbb:/ccccc:/etc/foo", 1);
+       printf("%s\n", config_dir());   
+       printf("%s\n", theme_dir("Numix"));
+}
index 9973985ec4fb2884a60254ca71bf3abb91cff82f..c1ef9bebda40f31d8d194b541dd73d17ac4e8b27 100644 (file)
@@ -6,14 +6,15 @@ ASAN_FLAGS = -O0 -fsanitize=address -fno-common -fno-omit-frame-pointer -rdynami
 CFLAGS    += $(ASAN_FLAGS)
 LDFLAGS   += $(ASAN_FLAGS) -fuse-ld=gold
 LDFLAGS   += `xml2-config --libs`
-LDFLAGS   += `pkg-config --cflags --libs glib-2.0 wayland-server xkbcommon`
+LDFLAGS   += `pkg-config --cflags --libs glib-2.0 cairo pangocairo wayland-server xkbcommon`
 
 PROGS = rcxml-print-nodenames
 SRC = \
        rcxml-print-nodenames.c \
        ../../src/config/rcxml.c \
-       ../../src/config/config-dir.c \
+       ../../src/common/dir.c \
        ../../src/common/buf.c \
+       ../../src/common/font.c \
        ../../src/config/keybind.c
 
 
index 82492008526ef518b545a94b3998698a3bbb924e..cbfeb893c19ac70a6e56abda5e80a85c0be03010 100644 (file)
@@ -10,7 +10,7 @@ LDFLAGS   += -DWLR_USE_UNSTABLE
 SRCS = \
        theme-helper.c \
        ../../src/theme/theme.c \
-       ../../src/theme/theme-dir.c
+       ../../src/common/dir.c
 
 all:
        gcc $(CFLAGS) -o theme-helper $(SRCS) $(LDFLAGS)
index af07e3a2b9c034256d75b2ad5816fc3066495b2c..938753d1661960bb90ed275c45d7041e8b421f32 100644 (file)
@@ -4,8 +4,14 @@ LDFLAGS     += `pkg-config --cflags --libs cairo`
 ASAN        += -fsanitize=address
 
 PROGS        = xbm-tokenize xbm-parse
-DEP_TOKENIZE = ../../src/common/buf.c ../../src/theme/xbm/tokenize.c
-DEP_PARSE    = $(DEP_TOKENIZE) ../../src/theme/xbm/parse.c
+
+DEP_TOKENIZE = \
+              ../../src/common/buf.c \
+              ../../src/theme/xbm/tokenize.c
+
+DEP_PARSE    = $(DEP_TOKENIZE) \
+              ../../src/theme/xbm/parse.c \
+              ../../src/common/grab-file.c
 
 all: $(PROGS)
 
index 71cd6aeb290df8cf63a295cbba338d07d34dcf3f..5bdbf28f61412d0ff855218f61e163ea912df802 100644 (file)
@@ -4,6 +4,7 @@
 #include <cairo.h>
 
 #include "theme/xbm/parse.h"
+#include "common/grab-file.h"
 
 int main(int argc, char **argv)
 {
@@ -14,12 +15,12 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       char *buffer = xbm_read_file(argv[1]);
+       char *buffer = grab_file(argv[1]);
        if (!buffer)
                exit(EXIT_FAILURE);
-       tokens = xbm_tokenize(buffer);
+       tokens = tokenize_xbm(buffer);
        free(buffer);
-       struct pixmap pixmap = xbm_create_pixmap(tokens);
+       struct pixmap pixmap = parse_xbm_tokens(tokens);
        free(tokens);
 
        cairo_surface_t *g_surface;
index e5e6c06ba7befe47b3b7c6aa25cad162f00fc393..c299464b9e9926c36556c1b1888d790e73680df2 100644 (file)
@@ -3,7 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "buf.h"
+#include "common/buf.h"
 #include "theme/xbm/tokenize.h"
 
 /* Read file into buffer, because it's easier to tokenize that way */
@@ -41,7 +41,7 @@ int main(int argc, char **argv)
        char *buffer = read_file(argv[1]);
        if (!buffer)
                exit(EXIT_FAILURE);
-       tokens = xbm_tokenize(buffer);
+       tokens = tokenize_xbm(buffer);
        free(buffer);
        for (struct token *t = tokens; t->type; t++)
                printf("%s\n", t->name);