mirror of
				https://github.com/labwc/labwc.git
				synced 2025-11-03 09:01:51 -05:00 
			
		
		
		
	button/common.c: share button_filename() to reduce duplication
This commit is contained in:
		
							parent
							
								
									143714f1c9
								
							
						
					
					
						commit
						a8951c4b75
					
				
					 7 changed files with 38 additions and 24 deletions
				
			
		| 
						 | 
					@ -4,6 +4,6 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct lab_data_buffer;
 | 
					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 */
 | 
					#endif /* LABWC_BUTTON_PNG_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5,7 +5,7 @@
 | 
				
			||||||
struct lab_data_buffer;
 | 
					struct lab_data_buffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* button_xbm_load - Convert xbm file to buffer with cairo surface */
 | 
					/* 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);
 | 
						char *fallback_button, float *rgba);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* LABWC_BUTTON_XBM_H */
 | 
					#endif /* LABWC_BUTTON_XBM_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										15
									
								
								include/button/common.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								include/button/common.h
									
										
									
									
									
										Normal 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 */
 | 
				
			||||||
| 
						 | 
					@ -12,7 +12,7 @@
 | 
				
			||||||
#include <wlr/util/log.h>
 | 
					#include <wlr/util/log.h>
 | 
				
			||||||
#include "buffer.h"
 | 
					#include "buffer.h"
 | 
				
			||||||
#include "button/button-png.h"
 | 
					#include "button/button-png.h"
 | 
				
			||||||
#include "common/dir.h"
 | 
					#include "button/common.h"
 | 
				
			||||||
#include "labwc.h"
 | 
					#include "labwc.h"
 | 
				
			||||||
#include "theme.h"
 | 
					#include "theme.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -24,15 +24,6 @@ file_exists(const char *path)
 | 
				
			||||||
	return (!stat(path, &st));
 | 
						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
 | 
					 * 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.
 | 
					 * 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
 | 
					#undef PNG_BYTES_TO_CHECK
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void
 | 
					void
 | 
				
			||||||
png_load(const char *filename, struct lab_data_buffer **buffer)
 | 
					png_load(const char *button_name, struct lab_data_buffer **buffer)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if (*buffer) {
 | 
						if (*buffer) {
 | 
				
			||||||
		wlr_buffer_drop(&(*buffer)->base);
 | 
							wlr_buffer_drop(&(*buffer)->base);
 | 
				
			||||||
		*buffer = NULL;
 | 
							*buffer = NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char *path = button_path(filename);
 | 
						char path[4096] = { 0 };
 | 
				
			||||||
 | 
						button_filename(button_name, path, sizeof(path));
 | 
				
			||||||
	if (!file_exists(path) || !ispng(path)) {
 | 
						if (!file_exists(path) || !ispng(path)) {
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,7 @@
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
#include <drm_fourcc.h>
 | 
					#include <drm_fourcc.h>
 | 
				
			||||||
#include "button/button-xbm.h"
 | 
					#include "button/button-xbm.h"
 | 
				
			||||||
 | 
					#include "button/common.h"
 | 
				
			||||||
#include "common/dir.h"
 | 
					#include "common/dir.h"
 | 
				
			||||||
#include "common/grab-file.h"
 | 
					#include "common/grab-file.h"
 | 
				
			||||||
#include "common/mem.h"
 | 
					#include "common/mem.h"
 | 
				
			||||||
| 
						 | 
					@ -257,16 +258,8 @@ parse_xbm_builtin(const char *button, int size)
 | 
				
			||||||
	return pixmap;
 | 
						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
 | 
					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)
 | 
							char *fallback_button, float *rgba)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct pixmap pixmap = {0};
 | 
						struct pixmap pixmap = {0};
 | 
				
			||||||
| 
						 | 
					@ -278,7 +271,9 @@ button_xbm_load(const char *filename, struct lab_data_buffer **buffer,
 | 
				
			||||||
	color = u32(rgba);
 | 
						color = u32(rgba);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* Read file into memory as it's easier to tokenzie that way */
 | 
						/* 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) {
 | 
						if (token_buffer) {
 | 
				
			||||||
		struct token *tokens = tokenize_xbm(token_buffer);
 | 
							struct token *tokens = tokenize_xbm(token_buffer);
 | 
				
			||||||
		free(token_buffer);
 | 
							free(token_buffer);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										11
									
								
								src/button/common.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/button/common.c
									
										
									
									
									
										Normal 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);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,5 @@
 | 
				
			||||||
labwc_sources += files(
 | 
					labwc_sources += files(
 | 
				
			||||||
  'button-png.c',
 | 
					  'button-png.c',
 | 
				
			||||||
  'button-xbm.c',
 | 
					  'button-xbm.c',
 | 
				
			||||||
 | 
					  'common.c',
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue