diff --git a/src/include/xcb.h b/src/include/xcb.h index 6ba2700..c2aea36 100644 --- a/src/include/xcb.h +++ b/src/include/xcb.h @@ -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); diff --git a/src/xcb/CMakeLists.txt b/src/xcb/CMakeLists.txt index 415cb0f..ab01bf4 100644 --- a/src/xcb/CMakeLists.txt +++ b/src/xcb/CMakeLists.txt @@ -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} diff --git a/src/xcb/res.c b/src/xcb/res.c new file mode 100644 index 0000000..04382f6 --- /dev/null +++ b/src/xcb/res.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +#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; +}