From 50e4a49c2ea63d9e946f1264f4fe735b76270d1c 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 4a6e546..60f72cc 100644 --- a/src/mango.c +++ b/src/mango.c @@ -2506,15 +2506,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) {