]> git.mdlowis.com Git - proto/labwc.git/commitdiff
button/common.c: share button_filename() to reduce duplication
authorJohan Malm <jgm323@gmail.com>
Mon, 21 Aug 2023 20:03:46 +0000 (21:03 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sun, 17 Sep 2023 18:26:41 +0000 (19:26 +0100)
include/button/button-png.h
include/button/button-xbm.h
include/button/common.h [new file with mode: 0644]
src/button/button-png.c
src/button/button-xbm.c
src/button/common.c [new file with mode: 0644]
src/button/meson.build

index 408a21fc8f5442cd4265ee640843bac1037328e9..e238b9d8b6449bb71bdf33005b711ff678cc7d1d 100644 (file)
@@ -4,6 +4,6 @@
 
 struct lab_data_buffer;
 
-void png_load(const char *filename, struct lab_data_buffer **buffer);
+void png_load(const char *button_name, struct lab_data_buffer **buffer);
 
 #endif /* LABWC_BUTTON_PNG_H */
index ae78222edbd87e0e9bf7fd00aad6c958bcb20c9d..862a926d83a896026d1c05b26571838337d7c2d6 100644 (file)
@@ -5,7 +5,7 @@
 struct lab_data_buffer;
 
 /* button_xbm_load - Convert xbm file to buffer with cairo surface */
-void button_xbm_load(const char *filename, struct lab_data_buffer **buffer,
+void button_xbm_load(const char *button_name, struct lab_data_buffer **buffer,
        char *fallback_button, float *rgba);
 
 #endif /* LABWC_BUTTON_XBM_H */
diff --git a/include/button/common.h b/include/button/common.h
new file mode 100644 (file)
index 0000000..b23d351
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef LABWC_BUTTON_COMMON_H
+#define LABWC_BUTTON_COMMON_H
+
+/**
+ * button_filename() - Get full filename for button.
+ * @name: The name of the button (for example 'iconify.xbm').
+ * @buf: Buffer to fill with the full filename
+ * @len: Length of buffer
+ *
+ * Example return value: /usr/share/themes/Numix/openbox-3/iconfify.xbm
+ */
+void button_filename(const char *name, char *buf, size_t len);
+
+#endif /* LABWC_BUTTON_COMMON_H */
index 399f988ba626de0dbd81262708cee052ec7e1dce..cc45594f70027cdeb9111151b35745b8d6c93dc5 100644 (file)
@@ -12,7 +12,7 @@
 #include <wlr/util/log.h>
 #include "buffer.h"
 #include "button/button-png.h"
-#include "common/dir.h"
+#include "button/common.h"
 #include "labwc.h"
 #include "theme.h"
 
@@ -24,15 +24,6 @@ file_exists(const char *path)
        return (!stat(path, &st));
 }
 
-/* Share with xbm.c:xbm_path() */
-static char *
-button_path(const char *filename)
-{
-       static char buffer[4096] = { 0 };
-       snprintf(buffer, sizeof(buffer), "%s/%s", theme_dir(rc.theme_name), filename);
-       return buffer;
-}
-
 /*
  * cairo_image_surface_create_from_png() does not gracefully handle non-png
  * files, so we verify the header before trying to read the rest of the file.
@@ -62,14 +53,15 @@ ispng(const char *filename)
 #undef PNG_BYTES_TO_CHECK
 
 void
-png_load(const char *filename, struct lab_data_buffer **buffer)
+png_load(const char *button_name, struct lab_data_buffer **buffer)
 {
        if (*buffer) {
                wlr_buffer_drop(&(*buffer)->base);
                *buffer = NULL;
        }
 
-       char *path = button_path(filename);
+       char path[4096] = { 0 };
+       button_filename(button_name, path, sizeof(path));
        if (!file_exists(path) || !ispng(path)) {
                return;
        }
index eb2a39ec3569e78c50febb39a86eca748fc57b96..9d5583323ee3b98728f040d7aa4951c60a87fe72 100644 (file)
@@ -14,6 +14,7 @@
 #include <string.h>
 #include <drm_fourcc.h>
 #include "button/button-xbm.h"
+#include "button/common.h"
 #include "common/dir.h"
 #include "common/grab-file.h"
 #include "common/mem.h"
@@ -257,16 +258,8 @@ parse_xbm_builtin(const char *button, int size)
        return pixmap;
 }
 
-static char *
-xbm_path(const char *button)
-{
-       static char buffer[4096] = { 0 };
-       snprintf(buffer, sizeof(buffer), "%s/%s", theme_dir(rc.theme_name), button);
-       return buffer;
-}
-
 void
-button_xbm_load(const char *filename, struct lab_data_buffer **buffer,
+button_xbm_load(const char *button_name, struct lab_data_buffer **buffer,
                char *fallback_button, float *rgba)
 {
        struct pixmap pixmap = {0};
@@ -278,7 +271,9 @@ button_xbm_load(const char *filename, struct lab_data_buffer **buffer,
        color = u32(rgba);
 
        /* Read file into memory as it's easier to tokenzie that way */
-       char *token_buffer = grab_file(xbm_path(filename));
+       char filename[4096] = { 0 };
+       button_filename(button_name, filename, sizeof(filename));
+       char *token_buffer = grab_file(filename);
        if (token_buffer) {
                struct token *tokens = tokenize_xbm(token_buffer);
                free(token_buffer);
diff --git a/src/button/common.c b/src/button/common.c
new file mode 100644 (file)
index 0000000..b5a3aaa
--- /dev/null
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <stdio.h>
+#include "button/common.h"
+#include "common/dir.h"
+#include "config/rcxml.h"
+
+void
+button_filename(const char *name, char *buf, size_t len)
+{
+       snprintf(buf, len, "%s/%s", theme_dir(rc.theme_name), name);
+}
index 4d738895494c6d943c195475f506563bd03cea3f..b5b10d2b6703a199d2d73aed1144e9023a08e17a 100644 (file)
@@ -1,4 +1,5 @@
 labwc_sources += files(
   'button-png.c',
   'button-xbm.c',
+  'common.c',
 )