From f42b3080b47a7b731d7638ec7e657bf0db2104aa Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Fri, 25 Jul 2025 16:06:26 +0800 Subject: [PATCH] feat: support set monitor resolution and refresh rate in monitor rule --- src/config/parse_config.h | 39 +++++++++++++++++++++++---------------- src/maomao.c | 11 ++++++++++- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index a7a1417..8e0880b 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -70,15 +70,14 @@ typedef struct { } ConfigWinRule; typedef struct { - const char *name; // 显示器名称 - float mfact; // 主区域比例 - int nmaster; // 主区域窗口数量 - const char *layout; // 布局名称(字符串) - int rr; // 旋转和翻转(假设为整数) - float scale; // 显示器缩放比例 - int x, y; // 显示器位置 - int isterm; - int noswallow; + const char *name; // 显示器名称 + float mfact; // 主区域比例 + int nmaster; // 主区域窗口数量 + const char *layout; // 布局名称(字符串) + int rr; // 旋转和翻转(假设为整数) + float scale; // 显示器缩放比例 + int x, y; // 显示器位置 + int width, height, refresh; // 显示器分辨率和刷新率 } ConfigMonitorRule; // 修改后的宏定义 @@ -1470,16 +1469,18 @@ void parse_config_line(Config *config, const char *line) { // 临时存储每个字段的原始字符串 char raw_name[256], raw_layout[256]; char raw_mfact[256], raw_nmaster[256], raw_rr[256]; - char raw_scale[256], raw_x[256], raw_y[256]; + char raw_scale[256], raw_x[256], raw_y[256], raw_width[256], + raw_height[256], raw_refresh[256]; // 先读取所有字段为字符串 - int parsed = sscanf(value, - "%255[^,],%255[^,],%255[^,],%255[^,],%255[^,],%255[" - "^,],%255[^,],%255s", - raw_name, raw_mfact, raw_nmaster, raw_layout, - raw_rr, raw_scale, raw_x, raw_y); + int parsed = + sscanf(value, + "%255[^,],%255[^,],%255[^,],%255[^,],%255[^,],%255[" + "^,],%255[^,],%255[^,],%255[^,],%255[^,],%255s", + raw_name, raw_mfact, raw_nmaster, raw_layout, raw_rr, + raw_scale, raw_x, raw_y, raw_width, raw_height, raw_refresh); - if (parsed == 8) { + if (parsed == 11) { // 修剪每个字段的空格 trim_whitespace(raw_name); trim_whitespace(raw_mfact); @@ -1489,6 +1490,9 @@ void parse_config_line(Config *config, const char *line) { trim_whitespace(raw_scale); trim_whitespace(raw_x); trim_whitespace(raw_y); + trim_whitespace(raw_width); + trim_whitespace(raw_height); + trim_whitespace(raw_refresh); // 转换修剪后的字符串为特定类型 rule->name = strdup(raw_name); @@ -1499,6 +1503,9 @@ void parse_config_line(Config *config, const char *line) { rule->scale = atof(raw_scale); rule->x = atoi(raw_x); rule->y = atoi(raw_y); + rule->width = atoi(raw_width); + rule->height = atoi(raw_height); + rule->refresh = atoi(raw_refresh); if (!rule->name || !rule->layout) { if (rule->name) diff --git a/src/maomao.c b/src/maomao.c index 40fb548..d779cc4 100644 --- a/src/maomao.c +++ b/src/maomao.c @@ -2343,6 +2343,7 @@ void createmon(struct wl_listener *listener, void *data) { int ji, jk; struct wlr_output_state state; Monitor *m; + bool custom_monitor_mode = false; if (!wlr_output_init_render(wlr_output, alloc, drw)) return; @@ -2392,6 +2393,12 @@ void createmon(struct wl_listener *listener, void *data) { } scale = r->scale; rr = r->rr; + + if (r->width > 0 && r->height > 0 && r->refresh > 0) { + custom_monitor_mode = true; + wlr_output_state_set_custom_mode(&state, r->width, r->height, + r->refresh * 1000); + } wlr_output_state_set_scale(&state, r->scale); wlr_output_state_set_transform(&state, r->rr); break; @@ -2402,7 +2409,9 @@ void createmon(struct wl_listener *listener, void *data) { * monitor supports only a specific set of modes. We just pick the * monitor's preferred mode; a more sophisticated compositor would let * the user configure it. */ - wlr_output_state_set_mode(&state, wlr_output_preferred_mode(wlr_output)); + if (!custom_monitor_mode) + wlr_output_state_set_mode(&state, + wlr_output_preferred_mode(wlr_output)); /* Set up event listeners */ LISTEN(&wlr_output->events.frame, &m->frame, rendermon);