From d3bc78097c0c94b88f742ae5c6c8f5c5f72e2f2c Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 22 Dec 2023 17:44:35 -0800 Subject: [PATCH] fix(wm): pass *const u8 to enum_display_devices Thanks to @ids1024 for pointing out that the failing system calls were likely due to optimizations being made with the release profile's opt-level=3 and to @saethlin for pointing out that in the previous commit I was returning a pointer to a temporary that was about to be deallocated. https://fosstodon.org/@ids1024/111627094548141620 https://hachyderm.io/@saethlin/111627135615930244 With this commit, the display ids are now successfully returned from calls to EnumDisplayDevicesA on release builds. --- komorebi/src/windows_api.rs | 9 ++++----- komorebi/src/windows_callbacks.rs | 3 ++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index a3898c88..d5640ad1 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -242,13 +242,12 @@ impl WindowsApi { pub fn enum_display_devices( index: u32, - lp_device: Option<[u8; 32]>, + lp_device: Option<*const u8>, ) -> Result { #[allow(clippy::option_if_let_else)] - let lp_device = if let Some(lp_device) = lp_device { - PCSTR::from_raw(lp_device.as_ptr()) - } else { - PCSTR::null() + let lp_device = match lp_device { + None => PCSTR::null(), + Some(lp_device) => PCSTR(lp_device), }; let mut display_device = DISPLAY_DEVICEA { diff --git a/komorebi/src/windows_callbacks.rs b/komorebi/src/windows_callbacks.rs index f192beaa..d1826b23 100644 --- a/komorebi/src/windows_callbacks.rs +++ b/komorebi/src/windows_callbacks.rs @@ -85,7 +85,8 @@ pub extern "system" fn enum_display_monitor( .to_string(); if clean_name.eq(m.name()) { - if let Ok(device) = WindowsApi::enum_display_devices(0, Some(d.DeviceName)) { + if let Ok(device) = WindowsApi::enum_display_devices(0, Some(d.DeviceName.as_ptr())) + { let id = String::from_utf8_lossy(&device.DeviceID); let clean_id = id.replace('\u{0000}', "");