use core::any::Any;
use core::cell::{Cell, RefCell};
use core::ptr;
use std::io;
use std::panic::{self, AssertUnwindSafe};
use std::sync::atomic::{AtomicU32, Ordering};
use nadir_core::UVec2;
use nadir_core::log::{debug, error, warn};
use nadir_sys::windows::gdi32::{BLACK_BRUSH, GetStockObject};
use nadir_sys::windows::kernel32::GetModuleHandleW;
use nadir_sys::windows::types::{
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, ERROR_ACCESS_DENIED, FALSE, HINSTANCE, HWND,
LONG_PTR, LPARAM, LRESULT, POINT, RECT, TRUE, UINT_PTR, WPARAM,
};
use nadir_sys::windows::user32::{
AdjustWindowRectExForDpi, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, CreateWindowExW,
DefWindowProcW, DestroyWindow, DispatchMessageW, GWL_STYLE, GWLP_USERDATA, GetDpiForWindow,
GetKeyState, GetMonitorInfoW, GetWindowLongPtrW, GetWindowPlacement, HWND_TOP, IDC_ARROW,
KillTimer, LoadCursorW, MINMAXINFO, MONITOR_DEFAULTTONEAREST, MONITORINFO, MSG,
MonitorFromWindow, PM_REMOVE, PeekMessageW, RegisterClassExW, ReleaseCapture, SC_KEYMENU,
SW_HIDE, SW_SHOW, SW_SHOWNORMAL, SWP_FRAMECHANGED, SWP_NOACTIVATE, SWP_NOMOVE,
SWP_NOOWNERZORDER, SWP_NOSIZE, SWP_NOZORDER, SetCapture, SetProcessDpiAwarenessContext,
SetTimer, SetWindowLongPtrW, SetWindowPlacement, SetWindowPos, SetWindowTextW, ShowCursor,
ShowWindow, TranslateMessage, USER_DEFAULT_SCREEN_DPI, UnregisterClassW, VK_CONTROL, VK_LWIN,
VK_MENU, VK_RWIN, WINDOWPLACEMENT, WM_DPICHANGED, WM_ENTERSIZEMOVE, WM_EXITSIZEMOVE,
WM_GETMINMAXINFO, WM_SYSCOMMAND, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_TIMER, WM_XBUTTONDOWN,
WM_XBUTTONUP, WNDCLASSEXW, WS_CAPTION, WS_MINIMIZEBOX, WS_OVERLAPPED, WS_OVERLAPPEDWINDOW,
WS_POPUP, WS_SYSMENU,
};
use super::input::{Incoming, Modifiers, Translator};
use super::wide;
use crate::no_gl::NO_GL;
use crate::{Event, GpuSurface, ModalFrame, PlatformError, WindowConfig};
const MAX_DIMENSION: u32 = 32767;
const MODAL_TIMER: UINT_PTR = 1;
const MODAL_TIMER_INTERVAL: u32 = 1;
static NEXT_CLASS: AtomicU32 = AtomicU32::new(1);
type ModalFrameCallback = Box<dyn FnMut(ModalFrame)>;
struct WindowState {
translator: RefCell<Translator>,
events: RefCell<Vec<Event>>,
dpi: Cell<u32>,
min_size: UVec2,
fullscreen: Cell<bool>,
modal_frame: Cell<Option<ModalFrameCallback>>,
panic: Cell<Option<Box<dyn Any + Send>>>,
}
pub(crate) struct Backend {
hwnd: HWND,
instance: HINSTANCE,
class_name: Vec<u16>,
resizable: bool,
windowed_placement: Option<WINDOWPLACEMENT>,
cursor_visible: bool,
state: Box<WindowState>,
}
impl Backend {
pub(crate) fn new(config: &WindowConfig) -> Result<Self, PlatformError> {
become_dpi_aware();
let instance = unsafe { GetModuleHandleW(ptr::null()) };
if instance.is_null() {
return Err(PlatformError::CallFailed {
call: "GetModuleHandleW",
});
}
let class_name = wide(&format!(
"Nadir window {}",
NEXT_CLASS.fetch_add(1, Ordering::Relaxed)
));
let arrow = unsafe { LoadCursorW(ptr::null_mut(), IDC_ARROW) };
let class = WNDCLASSEXW {
cbSize: size_of::<WNDCLASSEXW>() as u32,
style: CS_HREDRAW | CS_VREDRAW,
lpfnWndProc: Some(window_proc),
cbClsExtra: 0,
cbWndExtra: 0,
hInstance: instance,
hIcon: ptr::null_mut(),
hCursor: arrow,
hbrBackground: GetStockObject(BLACK_BRUSH),
lpszMenuName: ptr::null(),
lpszClassName: class_name.as_ptr(),
hIconSm: ptr::null_mut(),
};
if unsafe { RegisterClassExW(&raw const class) } == 0 {
return Err(PlatformError::CallFailed {
call: "RegisterClassExW",
});
}
let style = windowed_style(config.resizable);
let title = wide(&config.title);
let hwnd = unsafe {
CreateWindowExW(
0,
class_name.as_ptr(),
title.as_ptr(),
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
ptr::null_mut(),
ptr::null_mut(),
instance,
ptr::null_mut(),
)
};
if hwnd.is_null() {
unsafe { UnregisterClassW(class_name.as_ptr(), instance) };
return Err(PlatformError::CallFailed {
call: "CreateWindowExW",
});
}
let mut backend = Self {
hwnd,
instance,
class_name,
resizable: config.resizable,
windowed_placement: None,
cursor_visible: true,
state: Box::new(WindowState {
translator: RefCell::new(Translator::new(config.size, 1.0)),
events: RefCell::new(Vec::new()),
dpi: Cell::new(USER_DEFAULT_SCREEN_DPI),
min_size: config.min_size,
fullscreen: Cell::new(false),
modal_frame: Cell::new(None),
panic: Cell::new(None),
}),
};
let state: *const WindowState = &raw const *backend.state;
let address = state.expose_provenance().cast_signed();
unsafe { SetWindowLongPtrW(hwnd, GWLP_USERDATA, address) };
let dpi = match unsafe { GetDpiForWindow(hwnd) } {
0 => USER_DEFAULT_SCREEN_DPI,
dpi => dpi,
};
let scale = scale_of(dpi);
let size = physical_size(config.size, scale);
backend.state.dpi.set(dpi);
*backend.state.translator.borrow_mut() = Translator::new(size, scale);
let Some(outer) = outer_size(size, style, dpi) else {
return Err(PlatformError::CallFailed {
call: "AdjustWindowRectExForDpi",
});
};
let sized = unsafe {
SetWindowPos(
hwnd,
ptr::null_mut(),
0,
0,
outer.x,
outer.y,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE,
)
};
if sized == FALSE {
return Err(PlatformError::CallFailed {
call: "SetWindowPos",
});
}
if config.fullscreen {
backend.set_fullscreen(true);
}
unsafe { ShowWindow(hwnd, SW_SHOW) };
debug!(
"opened a Win32 window of {}x{} pixels at scale {scale}",
size.x, size.y
);
Ok(backend)
}
pub(crate) fn pump_events(&mut self, out: &mut Vec<Event>) {
let mut message = MSG {
hwnd: ptr::null_mut(),
message: 0,
wParam: 0,
lParam: 0,
time: 0,
pt: POINT::default(),
};
while unsafe { PeekMessageW(&raw mut message, ptr::null_mut(), 0, 0, PM_REMOVE) } != FALSE {
unsafe { TranslateMessage(&raw const message) };
unsafe { DispatchMessageW(&raw const message) };
self.resume_panic();
}
self.resume_panic();
self.state.translator.borrow_mut().begin_pump(out);
out.append(&mut self.state.events.borrow_mut());
}
pub(crate) fn window_size(&self) -> UVec2 {
self.state.translator.borrow().size()
}
pub(crate) fn scale_factor(&self) -> f32 {
self.state.translator.borrow().scale()
}
pub(crate) fn set_fullscreen(&mut self, on: bool) {
if on == self.state.fullscreen.get() {
return;
}
if on {
self.enter_fullscreen();
} else {
self.leave_fullscreen();
}
}
pub(crate) fn set_title(&mut self, title: &str) {
let title = wide(title);
if unsafe { SetWindowTextW(self.hwnd, title.as_ptr()) } == FALSE {
error!(
"could not set the window title: {}",
io::Error::last_os_error()
);
}
}
pub(crate) fn set_cursor_visible(&mut self, visible: bool) {
if visible == self.cursor_visible {
return;
}
ShowCursor(if visible { TRUE } else { FALSE });
self.cursor_visible = visible;
}
pub(crate) fn set_modal_frame_callback(&mut self, callback: ModalFrameCallback) {
self.state.modal_frame.set(Some(callback));
}
pub(crate) fn gpu_surface(&self) -> GpuSurface<'_> {
GpuSurface::Gl(&NO_GL)
}
fn resume_panic(&self) {
if let Some(payload) = self.state.panic.take() {
panic::resume_unwind(payload);
}
}
fn enter_fullscreen(&mut self) {
let mut placement = WINDOWPLACEMENT {
length: size_of::<WINDOWPLACEMENT>() as u32,
..WINDOWPLACEMENT::default()
};
if unsafe { GetWindowPlacement(self.hwnd, &raw mut placement) } == FALSE {
error!(
"could not go fullscreen: GetWindowPlacement failed: {}",
io::Error::last_os_error()
);
return;
}
let monitor = unsafe { MonitorFromWindow(self.hwnd, MONITOR_DEFAULTTONEAREST) };
let mut info = MONITORINFO {
cbSize: size_of::<MONITORINFO>() as u32,
..MONITORINFO::default()
};
if unsafe { GetMonitorInfoW(monitor, &raw mut info) } == FALSE {
error!("could not go fullscreen: GetMonitorInfoW failed");
return;
}
self.windowed_placement = Some(placement);
self.state.fullscreen.set(true);
self.set_style(WS_POPUP);
let monitor = info.rcMonitor;
let moved = unsafe {
SetWindowPos(
self.hwnd,
HWND_TOP,
monitor.left,
monitor.top,
monitor.right - monitor.left,
monitor.bottom - monitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED,
)
};
if moved == FALSE {
error!(
"could not cover the monitor: SetWindowPos failed: {}",
io::Error::last_os_error()
);
}
}
fn leave_fullscreen(&mut self) {
self.state.fullscreen.set(false);
self.set_style(windowed_style(self.resizable));
if let Some(mut placement) = self.windowed_placement.take() {
if placement.showCmd == SW_HIDE {
placement.showCmd = SW_SHOWNORMAL;
}
if unsafe { SetWindowPlacement(self.hwnd, &raw const placement) } == FALSE {
error!(
"could not put the window back: SetWindowPlacement failed: {}",
io::Error::last_os_error()
);
}
}
let applied = unsafe {
SetWindowPos(
self.hwnd,
ptr::null_mut(),
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED,
)
};
if applied == FALSE {
error!(
"could not give the window its frame back: SetWindowPos failed: {}",
io::Error::last_os_error()
);
}
}
fn set_style(&self, style: u32) {
let old = window_style(self.hwnd);
let new = (old & !(WS_OVERLAPPEDWINDOW | WS_POPUP)) | style;
let value = new.cast_signed() as LONG_PTR;
let previous = unsafe { SetWindowLongPtrW(self.hwnd, GWL_STYLE, value) };
if previous == 0 {
error!(
"could not change the window style: {}",
io::Error::last_os_error()
);
}
}
}
impl Drop for Backend {
fn drop(&mut self) {
if !self.cursor_visible {
ShowCursor(TRUE);
}
if unsafe { DestroyWindow(self.hwnd) } == FALSE {
error!(
"could not destroy the window: {}",
io::Error::last_os_error()
);
unsafe { SetWindowLongPtrW(self.hwnd, GWLP_USERDATA, 0) };
return;
}
if unsafe { UnregisterClassW(self.class_name.as_ptr(), self.instance) } == FALSE {
warn!(
"could not unregister the window class: {}",
io::Error::last_os_error()
);
}
}
}
impl WindowState {
unsafe fn handle(
&self,
hwnd: HWND,
message: u32,
wparam: WPARAM,
lparam: LPARAM,
) -> Option<LRESULT> {
match message {
WM_DPICHANGED => {
let dpi = u32::from((wparam & 0xFFFF) as u16);
self.dpi.set(dpi);
self.translator
.borrow_mut()
.set_scale(scale_of(dpi), &mut self.events.borrow_mut());
if self.fullscreen.get() {
return Some(0);
}
let suggested = unsafe { *(lparam as *const RECT) };
let moved = unsafe {
SetWindowPos(
hwnd,
ptr::null_mut(),
suggested.left,
suggested.top,
suggested.right - suggested.left,
suggested.bottom - suggested.top,
SWP_NOZORDER | SWP_NOACTIVATE,
)
};
if moved == FALSE {
warn!("could not resize the window for its new DPI");
}
Some(0)
}
WM_GETMINMAXINFO => {
if let Some(min) = self.min_track_size(hwnd) {
let info = unsafe { &mut *(lparam as *mut MINMAXINFO) };
if min.x > 0 {
info.ptMinTrackSize.x = min.x;
}
if min.y > 0 {
info.ptMinTrackSize.y = min.y;
}
}
Some(0)
}
WM_SYSCOMMAND if (wparam & 0xFFF0) == SC_KEYMENU => Some(0),
WM_ENTERSIZEMOVE => {
if unsafe { SetTimer(hwnd, MODAL_TIMER, MODAL_TIMER_INTERVAL, None) } == 0 {
warn!("the game stops while the window is moved: SetTimer failed");
}
None
}
WM_EXITSIZEMOVE => {
unsafe { KillTimer(hwnd, MODAL_TIMER) };
None
}
WM_TIMER if wparam == MODAL_TIMER => {
self.run_modal_frame();
Some(0)
}
_ => {
let mut incoming = Incoming::decode(message, wparam, lparam);
if let Incoming::Char { modifiers, .. } = &mut incoming {
*modifiers = held_modifiers();
}
let any_button_down = {
let mut translator = self.translator.borrow_mut();
translator.handle(incoming, &mut self.events.borrow_mut());
translator.any_button_down()
};
if let Incoming::Button { .. } = incoming {
if any_button_down {
unsafe { SetCapture(hwnd) };
} else {
ReleaseCapture();
}
}
result(message, incoming)
}
}
}
fn min_track_size(&self, hwnd: HWND) -> Option<POINT> {
if self.fullscreen.get() || self.min_size == UVec2::ZERO {
return None;
}
let dpi = self.dpi.get();
let min = physical_min_size(self.min_size, scale_of(dpi));
let outer = outer_size(min.max(UVec2::ONE), window_style(hwnd), dpi)?;
Some(POINT {
x: if min.x == 0 { 0 } else { outer.x },
y: if min.y == 0 { 0 } else { outer.y },
})
}
fn run_modal_frame(&self) {
let frame = {
let translator = self.translator.borrow();
ModalFrame {
size: translator.size(),
scale_factor: translator.scale(),
}
};
let Some(mut callback) = self.modal_frame.take() else {
return;
};
callback(frame);
let replaced = self.modal_frame.take();
self.modal_frame.set(replaced.or(Some(callback)));
}
}
fn held_modifiers() -> Modifiers {
let down = |key| GetKeyState(key).cast_unsigned() & 0x8000 != 0;
Modifiers {
control: down(VK_CONTROL),
alt: down(VK_MENU),
command: down(VK_LWIN) || down(VK_RWIN),
}
}
fn result(message: u32, incoming: Incoming) -> Option<LRESULT> {
match incoming {
Incoming::Other => None,
Incoming::Key { .. } if message == WM_SYSKEYDOWN || message == WM_SYSKEYUP => None,
Incoming::Button { .. } if message == WM_XBUTTONDOWN || message == WM_XBUTTONUP => {
Some(TRUE as LRESULT)
}
_ => Some(0),
}
}
unsafe extern "system" fn window_proc(
hwnd: HWND,
message: u32,
wparam: WPARAM,
lparam: LPARAM,
) -> LRESULT {
let address = unsafe { GetWindowLongPtrW(hwnd, GWLP_USERDATA) };
let state = ptr::with_exposed_provenance::<WindowState>(address.cast_unsigned());
let Some(state) = (unsafe { state.as_ref() }) else {
return unsafe { DefWindowProcW(hwnd, message, wparam, lparam) };
};
let handled = panic::catch_unwind(AssertUnwindSafe(|| {
unsafe { state.handle(hwnd, message, wparam, lparam) }
}));
match handled {
Ok(Some(result)) => result,
Ok(None) => unsafe { DefWindowProcW(hwnd, message, wparam, lparam) },
Err(payload) => {
let first = state.panic.take().unwrap_or(payload);
state.panic.set(Some(first));
0
}
}
}
fn become_dpi_aware() {
let set = unsafe { SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) };
if set == FALSE {
let error = io::Error::last_os_error();
if error.raw_os_error() != Some(ERROR_ACCESS_DENIED) {
warn!("could not make the process DPI aware: {error}");
}
}
}
fn window_style(hwnd: HWND) -> u32 {
let style = unsafe { GetWindowLongPtrW(hwnd, GWL_STYLE) };
style as u32
}
fn windowed_style(resizable: bool) -> u32 {
if resizable {
WS_OVERLAPPEDWINDOW
} else {
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX
}
}
fn scale_of(dpi: u32) -> f32 {
dpi as f32 / USER_DEFAULT_SCREEN_DPI as f32
}
fn outer_size(inner: UVec2, style: u32, dpi: u32) -> Option<POINT> {
let int = |value: u32| i32::try_from(value).unwrap_or(i32::MAX);
let mut rect = RECT {
left: 0,
top: 0,
right: int(inner.x),
bottom: int(inner.y),
};
let adjusted = unsafe { AdjustWindowRectExForDpi(&raw mut rect, style, FALSE, 0, dpi) };
(adjusted != FALSE).then(|| POINT {
x: rect.right - rect.left,
y: rect.bottom - rect.top,
})
}
fn physical_size(logical: UVec2, scale: f32) -> UVec2 {
let dimension = |logical: u32| {
let physical = (logical as f32 * scale).round() as u32;
physical.clamp(1, MAX_DIMENSION)
};
UVec2::new(dimension(logical.x), dimension(logical.y))
}
fn physical_min_size(logical: UVec2, scale: f32) -> UVec2 {
let dimension = |logical: u32| match logical {
0 => 0,
_ => physical_size(UVec2::splat(logical), scale).x,
};
UVec2::new(dimension(logical.x), dimension(logical.y))
}
#[cfg(test)]
mod tests {
use nadir_core::MouseButton;
use nadir_sys::windows::user32::{
WM_KEYDOWN, WM_KEYUP, WM_LBUTTONDOWN, WS_MAXIMIZEBOX, WS_THICKFRAME,
};
use super::*;
#[test]
fn the_scale_factor_is_the_dpi_over_96() {
assert_eq!(scale_of(96), 1.0);
assert_eq!(scale_of(120), 1.25);
assert_eq!(scale_of(144), 1.5);
assert_eq!(scale_of(192), 2.0);
}
#[test]
fn a_logical_size_is_scaled_rounded_and_kept_in_range() {
let size = UVec2::new(640, 360);
assert_eq!(physical_size(size, 1.0), UVec2::new(640, 360));
assert_eq!(physical_size(size, 1.25), UVec2::new(800, 450));
assert_eq!(physical_size(UVec2::new(101, 3), 1.25), UVec2::new(126, 4));
assert_eq!(physical_size(UVec2::ZERO, 1.0), UVec2::new(1, 1));
assert_eq!(
physical_size(UVec2::new(u32::MAX, 40_000), 1.0),
UVec2::new(32767, 32767)
);
assert_eq!(physical_size(size, f32::NAN), UVec2::new(1, 1));
}
#[test]
fn a_minimum_size_is_scaled_and_zero_stays_no_limit() {
assert_eq!(
physical_min_size(UVec2::new(320, 180), 1.5),
UVec2::new(480, 270)
);
assert_eq!(
physical_min_size(UVec2::new(0, 180), 2.0),
UVec2::new(0, 360)
);
}
#[test]
fn only_a_resizable_window_has_a_sizing_border_and_a_maximize_button() {
let resizable = windowed_style(true);
assert_eq!(resizable, WS_OVERLAPPEDWINDOW);
let fixed = windowed_style(false);
assert_eq!(fixed & (WS_THICKFRAME | WS_MAXIMIZEBOX), 0);
assert_ne!(fixed & WS_CAPTION, 0);
assert_ne!(fixed & WS_SYSMENU, 0);
assert_eq!(WS_OVERLAPPEDWINDOW, 0x00CF_0000);
}
#[test]
fn system_keys_are_left_to_windows_and_others_are_handled() {
let key = Incoming::Key {
pressed: true,
scan_code: 0x003E,
};
assert_eq!(result(WM_SYSKEYDOWN, key), None);
assert_eq!(result(WM_SYSKEYUP, key), None);
assert_eq!(result(WM_KEYDOWN, key), Some(0));
assert_eq!(result(WM_KEYUP, key), Some(0));
}
#[test]
fn side_buttons_return_true_and_other_messages_zero_or_nothing() {
let button = |button| Incoming::Button {
pressed: true,
button,
x: 0,
y: 0,
};
assert_eq!(
result(WM_XBUTTONDOWN, button(MouseButton::Back)),
Some(TRUE as LRESULT)
);
assert_eq!(
result(WM_XBUTTONUP, button(MouseButton::Forward)),
Some(TRUE as LRESULT)
);
assert_eq!(result(WM_LBUTTONDOWN, button(MouseButton::Left)), Some(0));
assert_eq!(result(0x000F, Incoming::Other), None);
}
}