From 845d4e1268c0c2c806eb6a39c3c2ef1aee54aeb1 Mon Sep 17 00:00:00 2001 From: DreamMaoMao <2523610504@qq.com> Date: Wed, 17 Sep 2025 12:02:14 +0800 Subject: [PATCH] opt: use nearest refresh for monitor rule --- src/mango.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/mango.c b/src/mango.c index 7c0c9ca..8782cb0 100644 --- a/src/mango.c +++ b/src/mango.c @@ -2462,15 +2462,22 @@ void createlocksurface(struct wl_listener *listener, void *data) { struct wlr_output_mode *get_output_mode(struct wlr_output *output, int width, int height, float refresh) { - struct wlr_output_mode *mode; + struct wlr_output_mode *mode, *nearest_mode = NULL; + float min_diff = 99999.0f; + wl_list_for_each(mode, &output->modes, link) { - if (mode->width == width && mode->height == height && - (int)round(mode->refresh / 1000) == (int)round(refresh)) { - return mode; + if (mode->width == width && mode->height == height) { + float mode_refresh = mode->refresh / 1000.0f; + float diff = fabsf(mode_refresh - refresh); + + if (diff < min_diff) { + min_diff = diff; + nearest_mode = mode; + } } } - return NULL; + return nearest_mode; } void createmon(struct wl_listener *listener, void *data) {