mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-02-24 00:37:43 +11:00
Fix embedded NULs in C wide strings returned from Windows API (#2264)
This commit is contained in:
parent
e7f88588bf
commit
ea09d1d10e
3 changed files with 14 additions and 10 deletions
src/platform_impl/windows
|
@ -1,10 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeSet, VecDeque},
|
collections::{BTreeSet, VecDeque},
|
||||||
ffi::OsString,
|
|
||||||
hash::Hash,
|
hash::Hash,
|
||||||
io, mem,
|
io, mem, ptr,
|
||||||
os::windows::prelude::OsStringExt,
|
|
||||||
ptr,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use windows_sys::Win32::{
|
use windows_sys::Win32::{
|
||||||
|
@ -17,6 +14,7 @@ use windows_sys::Win32::{
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::util::decode_wide;
|
||||||
use crate::{
|
use crate::{
|
||||||
dpi::{PhysicalPosition, PhysicalSize},
|
dpi::{PhysicalPosition, PhysicalSize},
|
||||||
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
|
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
|
||||||
|
@ -169,7 +167,7 @@ impl MonitorHandle {
|
||||||
pub fn name(&self) -> Option<String> {
|
pub fn name(&self) -> Option<String> {
|
||||||
let monitor_info = get_monitor_info(self.0).unwrap();
|
let monitor_info = get_monitor_info(self.0).unwrap();
|
||||||
Some(
|
Some(
|
||||||
OsString::from_wide(&monitor_info.szDevice)
|
decode_wide(&monitor_info.szDevice)
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.to_string(),
|
.to_string(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsString,
|
|
||||||
mem::{self, size_of},
|
mem::{self, size_of},
|
||||||
os::windows::prelude::OsStringExt,
|
|
||||||
ptr,
|
ptr,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -129,7 +127,7 @@ pub fn get_raw_input_device_name(handle: HANDLE) -> Option<String> {
|
||||||
|
|
||||||
unsafe { name.set_len(minimum_size as _) };
|
unsafe { name.set_len(minimum_size as _) };
|
||||||
|
|
||||||
OsString::from_wide(&name).into_string().ok()
|
util::decode_wide(&name).into_string().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_raw_input_devices(devices: &[RAWINPUTDEVICE]) -> bool {
|
pub fn register_raw_input_devices(devices: &[RAWINPUTDEVICE]) -> bool {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use std::{
|
use std::{
|
||||||
ffi::{c_void, OsStr},
|
ffi::{c_void, OsStr, OsString},
|
||||||
io,
|
io,
|
||||||
iter::once,
|
iter::once,
|
||||||
mem,
|
mem,
|
||||||
ops::BitAnd,
|
ops::BitAnd,
|
||||||
os::windows::prelude::OsStrExt,
|
os::windows::prelude::{OsStrExt, OsStringExt},
|
||||||
ptr,
|
ptr,
|
||||||
sync::atomic::{AtomicBool, Ordering},
|
sync::atomic::{AtomicBool, Ordering},
|
||||||
};
|
};
|
||||||
|
@ -37,6 +37,14 @@ pub fn encode_wide(string: impl AsRef<OsStr>) -> Vec<u16> {
|
||||||
string.as_ref().encode_wide().chain(once(0)).collect()
|
string.as_ref().encode_wide().chain(once(0)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn decode_wide(mut wide_c_string: &[u16]) -> OsString {
|
||||||
|
if let Some(null_pos) = wide_c_string.iter().position(|c| *c == 0) {
|
||||||
|
wide_c_string = &wide_c_string[..null_pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
OsString::from_wide(wide_c_string)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn has_flag<T>(bitset: T, flag: T) -> bool
|
pub fn has_flag<T>(bitset: T, flag: T) -> bool
|
||||||
where
|
where
|
||||||
T: Copy + PartialEq + BitAnd<T, Output = T>,
|
T: Copy + PartialEq + BitAnd<T, Output = T>,
|
||||||
|
|
Loading…
Add table
Reference in a new issue