button/common.c: share button_filename() to reduce duplication

This commit is contained in:
Johan Malm 2023-08-21 21:03:46 +01:00 committed by Johan Malm
parent 143714f1c9
commit a8951c4b75
7 changed files with 38 additions and 24 deletions

View 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 */

View 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 */

15
include/button/common.h Normal file
View file

@ -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 */

View 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;
}

View 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);

11
src/button/common.c Normal file
View file

@ -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);
}

View file

@ -1,4 +1,5 @@
labwc_sources += files(
'button-png.c',
'button-xbm.c',
'common.c',
)