From 375d60e8b41f2e0d09bab3fc23546b0092ea7618 Mon Sep 17 00:00:00 2001 From: SMWHff Date: Thu, 9 Jul 2026 09:02:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20identify=5Fdisplay=20=E4=B8=AD=E8=B7=9F?= =?UTF-8?q?=E8=B8=AA=20=5FSetDeviceGammaRamp=20=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 闪烁循环中之前忽略了 SetDeviceGammaRamp 的返回值,如果伽马写入被 静默拒绝(如远程桌面、权限不足),屏幕不会闪烁但函数仍返回 True。 修复后,在循环中检查每次调用的返回值,失败时记录警告并 break, 最终恢复后返回 False。 --- tests/features/test_gamma.py | 22 ++++++++++++++++++++++ winrandr/features/gamma.py | 13 ++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/features/test_gamma.py b/tests/features/test_gamma.py index e46701d..875f28c 100644 --- a/tests/features/test_gamma.py +++ b/tests/features/test_gamma.py @@ -122,6 +122,28 @@ def test_identify_getramp_fails(): assert identify_display(r"\\.\DISPLAY1") is False +def test_identify_flash_fails(): + from winrandr.features.gamma import identify_display + + with patch("winrandr.features.gamma._CreateDCW", return_value=0x1234): + with patch("winrandr.features.gamma._GetDeviceGammaRamp", return_value=1): + with patch("winrandr.features.gamma._SetDeviceGammaRamp", side_effect=[0, 1]): + with patch("winrandr.features.gamma._DeleteDC"): + with patch("winrandr.features.gamma.time.sleep"): + assert identify_display(r"\\.\DISPLAY1") is False + + +def test_identify_blank_fails(): + from winrandr.features.gamma import identify_display + + with patch("winrandr.features.gamma._CreateDCW", return_value=0x1234): + with patch("winrandr.features.gamma._GetDeviceGammaRamp", return_value=1): + with patch("winrandr.features.gamma._SetDeviceGammaRamp", side_effect=[1, 0, 1]): + with patch("winrandr.features.gamma._DeleteDC"): + with patch("winrandr.features.gamma.time.sleep"): + assert identify_display(r"\\.\DISPLAY1") is False + + def test_identify_exception(): from winrandr.features.gamma import identify_display diff --git a/winrandr/features/gamma.py b/winrandr/features/gamma.py index 2c9eefa..d285020 100644 --- a/winrandr/features/gamma.py +++ b/winrandr/features/gamma.py @@ -87,6 +87,7 @@ def identify_display(device_name: str, duration: float = 2.0) -> bool: return False saved = (c_uint16 * (3 * 256))() + all_ok = True try: ramp = (c_uint16 * (3 * 256))() if not _GetDeviceGammaRamp(dc, ramp): @@ -102,11 +103,17 @@ def identify_display(device_name: str, duration: float = 2.0) -> bool: interval = duration / 6 for _ in range(3): - _SetDeviceGammaRamp(dc, flash) + if not _SetDeviceGammaRamp(dc, flash): + logger.warning("设置 %s 闪烁白屏失败(伽马写入被拒绝)", device_name) + all_ok = False + break time.sleep(interval) - _SetDeviceGammaRamp(dc, blank) + if not _SetDeviceGammaRamp(dc, blank): + logger.warning("设置 %s 闪烁黑屏失败(伽马写入被拒绝)", device_name) + all_ok = False + break time.sleep(interval) finally: _SetDeviceGammaRamp(dc, saved) _DeleteDC(dc) - return True + return all_ok