添加解析 .Xresources 文件配置功能
This commit is contained in:
@@ -32,3 +32,7 @@ window_list_t *scan_window_list(void);
|
||||
void free_window_list(window_list_t *window_list);
|
||||
void start_xcb_event_loop(void);
|
||||
void clean_xcb(void);
|
||||
|
||||
bool get_xresource_uint32(const char *name, uint32_t *value);
|
||||
void get_xresource_string(const char *name, char **value, const char *fallback);
|
||||
void clean_xresource(void);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
set(LIB_XCB "zswm-xcb")
|
||||
set(LIB_XCB_NAME ${LIB_XCB} PARENT_SCOPE)
|
||||
|
||||
add_library(${LIB_XCB} STATIC xcb.c window.c)
|
||||
add_library(${LIB_XCB} STATIC xcb.c window.c res.c)
|
||||
|
||||
target_include_directories(${LIB_XCB} SYSTEM
|
||||
PRIVATE ${XCB_INCLUDE_DIRS}
|
||||
|
||||
54
src/xcb/res.c
Normal file
54
src/xcb/res.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <xcb/xcb_xrm.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "xcb-private.h"
|
||||
#include "xcb.h"
|
||||
|
||||
static xcb_xrm_database_t *db = NULL;
|
||||
|
||||
static xcb_xrm_database_t *get_xrm_db(void) {
|
||||
if (!db) {
|
||||
db = xcb_xrm_database_from_default(conn);
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
bool get_xresource_uint32(const char *name, uint32_t *value) {
|
||||
xcb_xrm_database_t *db = get_xrm_db();
|
||||
if (!db) return false;
|
||||
|
||||
long temp_value = *value;
|
||||
bool success = xcb_xrm_resource_get_long(db, name, NULL, &temp_value) == 0;
|
||||
if (success) *value = (uint32_t)temp_value;
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 读取 Xresources 文件中的字符串配置
|
||||
* @param name Xresources 文件中的配置名
|
||||
* @param value 存放读取到的字符串存储的指针的指针,使用完后记得使用
|
||||
* free(*value) 释放字符串对应的内存
|
||||
* @param fallback 若 Xresources 解析失败,则使用这个默认值
|
||||
*/
|
||||
void get_xresource_string(const char *name, char **value,
|
||||
const char *fallback) {
|
||||
xcb_xrm_database_t *db = get_xrm_db();
|
||||
if (!db) return;
|
||||
|
||||
if (!xcb_xrm_resource_get_string(db, name, NULL, value)) return;
|
||||
|
||||
size_t len = strlen(fallback) + 1;
|
||||
*value = ecalloc(len, sizeof(char));
|
||||
strncpy(*value, fallback, len);
|
||||
}
|
||||
|
||||
void clean_xresource(void) {
|
||||
if (!db) return;
|
||||
|
||||
xcb_xrm_database_free(db);
|
||||
db = NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user