From 72151c14582302f2c1290493532627f15457e7f0 Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Wed, 13 Aug 2025 01:25:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E9=85=8D=E7=BD=AE=E4=B8=BA?= =?UTF-8?q?=E6=AF=8F=E4=B8=AA=E5=B1=8F=E5=B9=95=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=20tags=20=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.c | 18 ++++++++++++++-- src/include/config.h | 22 +++++++++++++++++++ src/include/types.h | 5 +---- src/main.c | 51 ++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/config.c b/src/config.c index be3def9..f81866e 100644 --- a/src/config.c +++ b/src/config.c @@ -3,7 +3,21 @@ #include #include -/* 壁纸列表,支持通配符和 shell 环境变量拼接,最后以 NULL 结尾 */ const char *wallpager_list[] = {NULL}; -/* 壁纸切换间隔(单位:秒),0 表示不切换 */ const uint32_t wallpaper_interval = 0; + +const char *const *const *const monitor_tags[] = { + (const char *const *const[]){ + (const char *const[]){"1", "2", "3", "4", "5", "6", NULL}, + NULL, + }, + (const char *const *const[]){ + (const char *const[]){"1", "2", "3", NULL}, + (const char *const[]){"1", "2", "3", "4", "5", NULL}, + NULL, + }, + NULL, +}; +const char *const default_monitor_tags[] = { + "1", "2", "3", "4", "5", "6", "7", "8", "9", NULL, +}; diff --git a/src/include/config.h b/src/include/config.h index 02cc801..0505e08 100644 --- a/src/include/config.h +++ b/src/include/config.h @@ -2,5 +2,27 @@ #include +/* 壁纸列表,支持通配符和 shell 环境变量拼接,最后以 NULL 结尾 */ extern const char *wallpager_list[]; +/* 壁纸切换间隔(单位:秒),0 表示不切换 */ extern const uint32_t wallpaper_interval; + +/** + * @brief 屏幕 tags 设置 + * @description 每个屏幕的 tags 配置为字符串数组,以 NULL 结束,如 + * {"1", "2", "3", "4", "5", "6", NULL} + * + * 每一项配置里面的数组个数表示对应的屏幕个数,并最后以 NULL 结束, + * + * 如一个屏幕应该配置为 {{"1", "2", "3", "4", NULL}, NULL}, + * 两个屏幕应该配置为 {{"1", "2", "3", NULL}, {"1", "2", NULL}, NULL},以此类推 + */ +extern const char *const *const *const monitor_tags[]; +/** + * @brief 默认屏幕 tags 设置 + * @description 若屏幕个数与 monitor_tags 中设置的所有配置项都不符, + * 则所有屏幕的 tags 都使用这个默认配置 + * + * 格式为字符串数组,以 NULL 标志结束,如 {"1", "2", "3", NULL} + */ +extern const char *const default_monitor_tags[]; diff --git a/src/include/types.h b/src/include/types.h index 83cfacb..7242598 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -11,8 +11,6 @@ typedef struct layout_t { void (*arrange)(monitor_t *monitor); } layout_t; -#define TAG_SYMBOL_LENGTH 32 - /* 避免引入 xcb 头文件 */ typedef uint32_t xcb_window_t; @@ -22,8 +20,7 @@ struct monitor_t { uint32_t monitor_width, monitor_height, window_width, window_height; uint32_t selected_tag; - uint32_t tag_count; - char (*tags)[TAG_SYMBOL_LENGTH]; + char **tags; /* 为了避免引入 cairo 相关头文件,这里用 void * * 指针代替实际的指针类型,使用时强转为相应指针类型使用即可 */ diff --git a/src/main.c b/src/main.c index cb26cac..e001615 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include "xcb.h" static void init_monitors(void); +static char **get_monitor_tags(uint32_t monitor_count, uint32_t monitor_index); static void init_wallpaper(void); static gboolean update_wallpaper(gpointer user_data); static void set_wallpaper_by_index(uint32_t index); @@ -31,10 +32,18 @@ static GMainLoop *loop = NULL; /* 调试函数,开发完成后删除 */ static void print_monitor_list_info(monitor_t *monitor_list) { + printf("========================= monitor info =========================\n"); for (monitor_t *m = monitor_list; m; m = m->next) { - printf("mon[%u]: x: %d, y: %d, width: %u, height: %u\n", m->index, - m->monitor_x, m->monitor_y, m->monitor_width, m->monitor_height); + printf("========== mon[%u]:\n", m->index); + printf("x: %d, y: %d, width: %u, height: %u\n", m->monitor_x, m->monitor_y, + m->monitor_width, m->monitor_height); + printf("mon[%u] tags: ", m->index); + for (int i = 0; m->tags[i]; i++) { + printf("%s%s", i > 0 ? ", " : "", m->tags[i]); + } + printf("\n"); } + printf("======================= monitor info end =======================\n"); } static void print_wallpaper_config(wallpaper_config_t *config) { if (!config) { @@ -83,6 +92,39 @@ void init_monitors(void) { free(mr); mr = next_mr; } + for (monitor_t *m = monitors; m; m = m->next) { + m->tags = get_monitor_tags(i, m->index); + } +} + +char **get_monitor_tags(uint32_t monitor_count, uint32_t monitor_index) { + int config_count = 0; + while (monitor_tags[config_count]) config_count++; + + GStrvBuilder *builder = g_strv_builder_new(); + const char *const *tags = NULL; + + /* 倒序遍历,达到后面的配置覆盖前面相同配置的效果 */ + for (int i = config_count - 1; i >= 0; i--) { + const char *const *const *config = monitor_tags[i]; + + uint32_t monitor_count_in_tags_config = 0; + while (config[monitor_count_in_tags_config]) monitor_count_in_tags_config++; + + if (monitor_count_in_tags_config != monitor_count) continue; + + tags = config[monitor_index]; + break; + const char *const *const tags = config[monitor_index]; + for (int j = 0; tags[j]; j++) g_strv_builder_add(builder, tags[j]); + + return g_strv_builder_unref_to_strv(builder); + } + + if (tags == NULL) tags = default_monitor_tags; + + for (int i = 0; tags[i]; i++) g_strv_builder_add(builder, tags[i]); + return g_strv_builder_unref_to_strv(builder); } void init_wallpaper(void) { @@ -199,9 +241,10 @@ void cleanup(void) { free(c); c = tc; } - monitor_t *tm = m->next; + monitor_t *next_monitor = m->next; + g_strfreev(m->tags); free(m); - m = tm; + m = next_monitor; } monitors = NULL; }