From 97c10f4b19df8b1a93ca723e28a3bc90b981cf36 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Sun, 8 Mar 2026 15:10:27 +0800 Subject: [PATCH] opt: more unified output state set --- src/config/parse_config.h | 9 ++------- src/dispatch/bind_define.h | 15 ++++++--------- src/mango.c | 36 ++++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/config/parse_config.h b/src/config/parse_config.h index 3220b713..a511d85d 100644 --- a/src/config/parse_config.h +++ b/src/config/parse_config.h @@ -3580,14 +3580,11 @@ void reapply_monitor_rules(void) { Monitor *m = NULL; int32_t ji, vrr, custom; int32_t mx, my; - struct wlr_output_state state; wl_list_for_each(m, &mons, link) { if (!m->wlr_output->enabled) continue; - wlr_output_state_init(&state); - for (ji = 0; ji < config.monitor_rules_count; ji++) { if (config.monitor_rules_count < 1) break; @@ -3600,14 +3597,12 @@ void reapply_monitor_rules(void) { vrr = mr->vrr >= 0 ? mr->vrr : 0; custom = mr->custom >= 0 ? mr->custom : 0; - (void)apply_rule_to_state(m, mr, &state, vrr, custom); + (void)apply_rule_to_state(m, mr, &m->pending, vrr, custom); wlr_output_layout_add(output_layout, m->wlr_output, mx, my); - wlr_output_commit_state(m->wlr_output, &state); + monitor_state_commit(m); break; } } - - wlr_output_state_finish(&state); } updatemons(NULL, NULL); } diff --git a/src/dispatch/bind_define.h b/src/dispatch/bind_define.h index 676be515..fd371efa 100644 --- a/src/dispatch/bind_define.h +++ b/src/dispatch/bind_define.h @@ -1716,11 +1716,10 @@ int32_t toggleoverview(const Arg *arg) { int32_t disable_monitor(const Arg *arg) { Monitor *m = NULL; - struct wlr_output_state state = {0}; wl_list_for_each(m, &mons, link) { if (match_monitor_spec(arg->v, m)) { - wlr_output_state_set_enabled(&state, false); - wlr_output_commit_state(m->wlr_output, &state); + wlr_output_state_set_enabled(&m->pending, false); + monitor_state_commit(m); m->asleep = 1; updatemons(NULL, NULL); break; @@ -1731,11 +1730,10 @@ int32_t disable_monitor(const Arg *arg) { int32_t enable_monitor(const Arg *arg) { Monitor *m = NULL; - struct wlr_output_state state = {0}; wl_list_for_each(m, &mons, link) { if (match_monitor_spec(arg->v, m)) { - wlr_output_state_set_enabled(&state, true); - wlr_output_commit_state(m->wlr_output, &state); + wlr_output_state_set_enabled(&m->pending, true); + monitor_state_commit(m); m->asleep = 0; updatemons(NULL, NULL); break; @@ -1746,11 +1744,10 @@ int32_t enable_monitor(const Arg *arg) { int32_t toggle_monitor(const Arg *arg) { Monitor *m = NULL; - struct wlr_output_state state = {0}; wl_list_for_each(m, &mons, link) { if (match_monitor_spec(arg->v, m)) { - wlr_output_state_set_enabled(&state, !m->wlr_output->enabled); - wlr_output_commit_state(m->wlr_output, &state); + wlr_output_state_set_enabled(&m->pending, !m->wlr_output->enabled); + monitor_state_commit(m); m->asleep = !m->wlr_output->enabled; updatemons(NULL, NULL); break; diff --git a/src/mango.c b/src/mango.c index 2af2720f..059b7dcb 100644 --- a/src/mango.c +++ b/src/mango.c @@ -806,6 +806,8 @@ static int32_t keep_idle_inhibit(void *data); static void check_keep_idle_inhibit(Client *c); static void pre_caculate_before_arrange(Monitor *m, bool want_animation, bool from_view, bool only_caculate); +static bool monitor_state_commit(Monitor *m); + #include "data/static_keymap.h" #include "dispatch/bind_declare.h" #include "layout/layout.h" @@ -2290,6 +2292,8 @@ void cleanupmon(struct wl_listener *listener, void *data) { wl_event_source_remove(m->skip_frame_timeout); m->skip_frame_timeout = NULL; } + + wlr_output_state_finish(&m->pending); m->wlr_output->data = NULL; free(m->pertag); free(m); @@ -2920,6 +2924,17 @@ bool apply_rule_to_state(Monitor *m, const ConfigMonitorRule *rule, return mode_set; } +bool monitor_state_commit(Monitor *m) { + bool committed = wlr_output_commit_state(m->wlr_output, &m->pending); + if (committed) { + wlr_output_state_finish(&m->pending); + wlr_output_state_init(&m->pending); + } else { + wlr_log(WLR_ERROR, "Failed to commit frame"); + } + return committed; +} + void createmon(struct wl_listener *listener, void *data) { /* This event is raised by the backend when a new output (aka a display or * monitor) becomes available. */ @@ -2927,7 +2942,6 @@ void createmon(struct wl_listener *listener, void *data) { const ConfigMonitorRule *r; uint32_t i; int32_t ji, vrr, custom; - struct wlr_output_state state; Monitor *m = NULL; bool custom_monitor_mode = false; @@ -2972,9 +2986,9 @@ void createmon(struct wl_listener *listener, void *data) { float scale = 1; enum wl_output_transform rr = WL_OUTPUT_TRANSFORM_NORMAL; - wlr_output_state_init(&state); - wlr_output_state_set_scale(&state, scale); - wlr_output_state_set_transform(&state, rr); + wlr_output_state_init(&m->pending); // 初始化为0 + wlr_output_state_set_scale(&m->pending, scale); + wlr_output_state_set_transform(&m->pending, rr); for (ji = 0; ji < config.monitor_rules_count; ji++) { if (config.monitor_rules_count < 1) @@ -2990,7 +3004,7 @@ void createmon(struct wl_listener *listener, void *data) { scale = r->scale; rr = r->rr; - if (apply_rule_to_state(m, r, &state, vrr, custom)) { + if (apply_rule_to_state(m, r, &m->pending, vrr, custom)) { custom_monitor_mode = true; } break; // 只应用第一个匹配规则 @@ -2998,7 +3012,7 @@ void createmon(struct wl_listener *listener, void *data) { } if (!custom_monitor_mode) - wlr_output_state_set_mode(&state, + wlr_output_state_set_mode(&m->pending, wlr_output_preferred_mode(wlr_output)); /* Set up event listeners */ @@ -3007,9 +3021,8 @@ void createmon(struct wl_listener *listener, void *data) { LISTEN(&wlr_output->events.request_state, &m->request_state, requestmonstate); - wlr_output_state_set_enabled(&state, 1); - wlr_output_commit_state(wlr_output, &state); - wlr_output_state_finish(&state); + wlr_output_state_set_enabled(&m->pending, 1); + monitor_state_commit(m); wl_list_insert(&mons, &m->link); m->pertag = calloc(1, sizeof(Pertag)); @@ -4520,14 +4533,13 @@ void printstatus(void) { wl_signal_emit(&mango_print_status, NULL); } void powermgrsetmode(struct wl_listener *listener, void *data) { struct wlr_output_power_v1_set_mode_event *event = data; - struct wlr_output_state state = {0}; Monitor *m = event->output->data; if (!m) return; - wlr_output_state_set_enabled(&state, event->mode); - wlr_output_commit_state(m->wlr_output, &state); + wlr_output_state_set_enabled(&m->pending, event->mode); + monitor_state_commit(m); m->asleep = !event->mode; updatemons(NULL, NULL);