use crate::*;
use winapi::um::winuser::*;
pub fn get_message_a(msg: &mut impl AsMut<Msg>, hwnd: impl Into<HWnd>, min: impl Into<WM32>, max: impl Into<WM32>) -> Result<bool, Error> {
fn_context!(get_message_a => GetMessageA);
let msg = msg.as_mut().as_mut();
let hwnd = hwnd.into();
let min : u32 = min.into().into();
let max : u32 = max.into().into();
if cfg!(debug_assertions) {
assert!(min <= max, "min ({min}) > max ({max}) may cause WM_QUIT to not be processed");
if let Ok((t, p)) = get_window_thread_process_id(hwnd) {
assert_eq!(p, get_current_process_id(), "GetMessageA may hang if fed a hwnd belonging to another process");
assert_eq!(t, get_current_thread_id(), "GetMessageA may hang if fed a hwnd belonging to another thread");
}
}
match unsafe { GetMessageA(msg, hwnd.into(), min, max) } {
1 => Ok(true),
0 => Ok(false), -1 | _ => Err(fn_error_gle!()), }
}
pub fn get_message_w(msg: &mut impl AsMut<Msg>, hwnd: impl Into<HWnd>, min: impl Into<WM32>, max: impl Into<WM32>) -> Result<bool, Error> {
fn_context!(get_message_w => GetMessageW);
let msg = msg.as_mut().as_mut();
let hwnd = hwnd.into();
let min : u32 = min.into().into();
let max : u32 = max.into().into();
if cfg!(debug_assertions) {
assert!(min <= max, "min ({min}) > max ({max}) may cause WM_QUIT to not be processed");
if let Ok((t, p)) = get_window_thread_process_id(hwnd) {
assert_eq!(p, get_current_process_id(), "GetMessageW may hang if fed a hwnd belonging to another process");
assert_eq!(t, get_current_thread_id(), "GetMessageW may hang if fed a hwnd belonging to another thread");
}
}
match unsafe { GetMessageW(msg, hwnd.into(), min, max) } {
1 => Ok(true),
0 => Ok(false), -1 | _ => Err(fn_error_gle!()), }
}
#[cfg(debug_assertions)] #[test] #[should_panic] fn get_message_a_wrong_process() {
let mut msg = Msg::zeroed();
let _ = get_message_a(&mut msg, get_desktop_window(), 0, 0);
}
#[cfg(debug_assertions)] #[test] #[should_panic] fn get_message_a_wrong_thread() {
use std::ptr::*;
let (s, r) = std::sync::mpsc::channel();
let t = std::thread::spawn(move || {
s.send(unsafe { create_window_a(abistr::cstr!("Message"), (), 0, 0, 0, 0, 0, HWnd::MESSAGE, null_mut(), None, null_mut()).unwrap() }).unwrap();
std::thread::sleep(std::time::Duration::from_secs(3));
});
let hwnd = r.recv().unwrap();
let mut msg = Msg::zeroed();
let _ = get_message_a(&mut msg, hwnd, 0, 0);
t.join().unwrap();
}
#[cfg(debug_assertions)] #[test] #[should_panic] fn get_message_w_wrong_process() {
let mut msg = Msg::zeroed();
let _ = get_message_w(&mut msg, get_desktop_window(), 0, 0);
}
#[cfg(debug_assertions)] #[test] #[should_panic] fn get_message_w_wrong_thread() {
use std::ptr::*;
let (s, r) = std::sync::mpsc::channel();
let t = std::thread::spawn(move || {
s.send(unsafe { create_window_w(abistr::cstr16!("Message"), (), 0, 0, 0, 0, 0, HWnd::MESSAGE, null_mut(), None, null_mut()).unwrap() }).unwrap();
std::thread::sleep(std::time::Duration::from_secs(3));
});
let hwnd = r.recv().unwrap();
let mut msg = Msg::zeroed();
let _ = get_message_w(&mut msg, hwnd, 0, 0);
t.join().unwrap();
}