softer_gui 2.0.0

CPU-rendered windowing with vblank-exact frame pacing and one ordered input+render event stream. Speaks X11, Wayland, AppKit and Win32 directly; no dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Windows system interface: the `extern "system"` declarations the backend uses,
//! plus dynamic resolution for everything newer than the Vista baseline.
//!
//! This is a SIBLING of sys.rs, not a #[cfg] branch of it. sys.rs issues Linux
//! syscalls directly because the kernel ABI is a stability contract; Windows has
//! no such contract (ntdll's syscall numbers are an implementation detail of each
//! build), so the supported boundary is the DLL export. No binding crate is
//! involved: a binding crate is nothing but transcribed signatures, and these are
//! the fifty this crate actually uses.
//!
//! COMPATIBILITY RULE FOR THIS FILE, and it is load bearing: every function in a
//! `#[link]` block must exist on Windows Vista (most predate it by a decade).
//! Anything newer goes through `Dyn`, which GetProcAddress's it once at startup
//! and leaves a None behind when it is missing, so an old Windows degrades in
//! behaviour instead of failing to load the process. Adding a Windows 10 only
//! import to a link block below would silently raise the floor for the whole
//! crate, and it would do it at load time, before any of our code can react.

#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, dead_code)]

use core::ffi::c_void;

pub type HANDLE = *mut c_void;
pub type HWND = HANDLE;
pub type HDC = HANDLE;
pub type HBITMAP = HANDLE;
pub type HICON = HANDLE;
pub type HMODULE = HANDLE;
pub type HMONITOR = HANDLE;
pub type WPARAM = usize;
pub type LPARAM = isize;
pub type LRESULT = isize;
pub type BOOL = i32;
/// Windows calls this one, so it carries the same ABI caveat as the imports.
#[cfg(all(cosmo, target_arch = "x86_64"))]
pub type WNDPROC = unsafe extern "win64" fn(HWND, u32, WPARAM, LPARAM) -> LRESULT;
#[cfg(not(all(cosmo, target_arch = "x86_64")))]
pub type WNDPROC = unsafe extern "system" fn(HWND, u32, WPARAM, LPARAM) -> LRESULT;

pub const NULL: HANDLE = core::ptr::null_mut();

// ---- structures, transcribed from the SDK headers -----------------------------
#[repr(C)] #[derive(Clone, Copy, Default, Debug)] pub struct POINT { pub x: i32, pub y: i32 }
#[repr(C)] #[derive(Clone, Copy, Default, Debug)] pub struct RECT { pub left: i32, pub top: i32, pub right: i32, pub bottom: i32 }

#[repr(C)]
pub struct WNDCLASSEXW {
    pub cbSize: u32, pub style: u32, pub lpfnWndProc: Option<WNDPROC>,
    pub cbClsExtra: i32, pub cbWndExtra: i32, pub hInstance: HMODULE,
    pub hIcon: HICON, pub hCursor: HANDLE, pub hbrBackground: HANDLE,
    pub lpszMenuName: *const u16, pub lpszClassName: *const u16, pub hIconSm: HICON,
}

#[repr(C)] #[derive(Clone, Copy)]
pub struct MSG { pub hwnd: HWND, pub message: u32, pub wParam: WPARAM, pub lParam: LPARAM, pub time: u32, pub pt: POINT }
impl Default for MSG {
    fn default() -> MSG { MSG { hwnd: NULL, message: 0, wParam: 0, lParam: 0, time: 0, pt: POINT::default() } }
}

#[repr(C)]
pub struct PAINTSTRUCT { pub hdc: HDC, pub fErase: BOOL, pub rcPaint: RECT, pub fRestore: BOOL, pub fIncUpdate: BOOL, pub rgbReserved: [u8; 32] }
impl Default for PAINTSTRUCT {
    fn default() -> PAINTSTRUCT { unsafe { core::mem::zeroed() } }
}

#[repr(C)] #[derive(Clone, Copy, Default)]
pub struct BITMAPINFOHEADER {
    pub biSize: u32, pub biWidth: i32, pub biHeight: i32, pub biPlanes: u16, pub biBitCount: u16,
    pub biCompression: u32, pub biSizeImage: u32, pub biXPelsPerMeter: i32, pub biYPelsPerMeter: i32,
    pub biClrUsed: u32, pub biClrImportant: u32,
}
/// Header plus room for the colour table GDI never reads at 32bpp BI_RGB.
#[repr(C)] #[derive(Clone, Copy, Default)]
pub struct BITMAPINFO { pub bmiHeader: BITMAPINFOHEADER, pub bmiColors: [u32; 3] }

#[repr(C)]
pub struct ICONINFO { pub fIcon: BOOL, pub xHotspot: u32, pub yHotspot: u32, pub hbmMask: HBITMAP, pub hbmColor: HBITMAP }

#[repr(C)] #[derive(Clone, Copy, Default)]
pub struct MONITORINFO { pub cbSize: u32, pub rcMonitor: RECT, pub rcWork: RECT, pub dwFlags: u32 }

/// 220 bytes, asserted below: EnumDisplaySettingsW validates dmSize against it.
#[repr(C)] #[derive(Clone, Copy)]
pub struct DEVMODEW {
    pub dmDeviceName: [u16; 32],
    pub dmSpecVersion: u16, pub dmDriverVersion: u16, pub dmSize: u16, pub dmDriverExtra: u16,
    pub dmFields: u32,
    /// The printer/display union; this crate reads none of it.
    pub dmUnion1: [u32; 4],
    pub dmColor: i16, pub dmDuplex: i16, pub dmYResolution: i16, pub dmTTOption: i16, pub dmCollate: i16,
    pub dmFormName: [u16; 32],
    pub dmLogPixels: u16,
    pub dmBitsPerPel: u32, pub dmPelsWidth: u32, pub dmPelsHeight: u32,
    pub dmDisplayFlags: u32, pub dmDisplayFrequency: u32,
    pub dmICMMethod: u32, pub dmICMIntent: u32, pub dmMediaType: u32, pub dmDitherType: u32,
    pub dmReserved1: u32, pub dmReserved2: u32, pub dmPanningWidth: u32, pub dmPanningHeight: u32,
}
impl Default for DEVMODEW {
    fn default() -> DEVMODEW { unsafe { core::mem::zeroed() } }
}
const _: () = assert!(core::mem::size_of::<DEVMODEW>() == 220);

/// dwmapi.h's DWM_TIMING_INFO. Every field is present because cbSize must equal
/// the real size or the call returns E_INVALIDARG; only the first handful are read.
#[repr(C)] #[derive(Clone, Copy)]
pub struct DWM_TIMING_INFO {
    pub cbSize: u32,
    pub rateRefresh_num: u32, pub rateRefresh_den: u32,
    pub qpcRefreshPeriod: u64,
    pub rateCompose_num: u32, pub rateCompose_den: u32,
    pub qpcVBlank: u64,
    pub cRefresh: u64,
    pub cDXRefresh: u32,
    pub qpcCompose: u64,
    pub cFrame: u64,
    pub cDXPresent: u32,
    pub cRefreshFrame: u64,
    pub cFrameSubmitted: u64,
    pub cDXPresentSubmitted: u32,
    pub cFrameConfirmed: u64,
    pub cDXPresentConfirmed: u32,
    pub cRefreshConfirmed: u64,
    pub cDXRefreshConfirmed: u32,
    pub cFramesLate: u64,
    pub cFramesOutstanding: u32,
    pub cFrameDisplayed: u64,
    pub qpcFrameDisplayed: u64,
    pub cRefreshFrameDisplayed: u64,
    pub cFrameComplete: u64,
    pub qpcFrameComplete: u64,
    pub cFramePending: u64,
    pub qpcFramePending: u64,
    pub cFramesDisplayed: u64,
    pub cFramesComplete: u64,
    pub cFramesPending: u64,
    pub cFramesAvailable: u64,
    pub cFramesDropped: u64,
    pub cFramesMissed: u64,
    pub cRefreshNextDisplayed: u64,
    pub cRefreshNextPresented: u64,
    pub cRefreshesDisplayed: u64,
    pub cRefreshesPresented: u64,
    pub cRefreshStarted: u64,
    pub cPixelsReceived: u64,
    pub cPixelsDrawn: u64,
    pub cBuffersEmpty: u64,
}
impl Default for DWM_TIMING_INFO {
    fn default() -> DWM_TIMING_INFO {
        let mut t: DWM_TIMING_INFO = unsafe { core::mem::zeroed() };
        t.cbSize = core::mem::size_of::<DWM_TIMING_INFO>() as u32;
        t
    }
}

/// D3DKMT structures for the DWM-less vblank wait. Both are small and stable.
#[repr(C)] #[derive(Clone, Copy, Default)]
pub struct D3DKMT_OPENADAPTERFROMHDC { pub hDc: usize, pub hAdapter: u32, pub luid_low: u32, pub luid_high: i32, pub VidPnSourceId: u32 }
#[repr(C)] #[derive(Clone, Copy, Default)]
pub struct D3DKMT_WAITFORVERTICALBLANKEVENT { pub hAdapter: u32, pub hDevice: u32, pub VidPnSourceId: u32 }

// ---- constants ----------------------------------------------------------------
pub const CS_OWNDC: u32 = 0x0020;
pub const CS_HREDRAW: u32 = 0x0002;
pub const CS_VREDRAW: u32 = 0x0001;
pub const CS_DBLCLKS: u32 = 0x0008;

pub const WS_OVERLAPPEDWINDOW: u32 = 0x00CF0000;
pub const WS_POPUP: u32 = 0x8000_0000;
pub const WS_VISIBLE: u32 = 0x1000_0000;
pub const WS_CLIPCHILDREN: u32 = 0x0200_0000;
pub const WS_CLIPSIBLINGS: u32 = 0x0400_0000;
pub const WS_EX_APPWINDOW: u32 = 0x0004_0000;

pub const SW_SHOW: i32 = 5;
pub const SW_HIDE: i32 = 0;

pub const SWP_NOSIZE: u32 = 0x0001;
pub const SWP_NOMOVE: u32 = 0x0002;
pub const SWP_NOZORDER: u32 = 0x0004;
pub const SWP_NOACTIVATE: u32 = 0x0010;
pub const SWP_FRAMECHANGED: u32 = 0x0020;
pub const SWP_NOOWNERZORDER: u32 = 0x0200;

pub const GWL_STYLE: i32 = -16;
pub const GWL_EXSTYLE: i32 = -20;

pub const PM_REMOVE: u32 = 0x0001;
pub const QS_ALLINPUT: u32 = 0x04FF;
pub const MWMO_INPUTAVAILABLE: u32 = 0x0004;
pub const INFINITE: u32 = 0xFFFF_FFFF;
pub const WAIT_OBJECT_0: u32 = 0;
pub const WAIT_TIMEOUT: u32 = 258;

pub const WM_DESTROY: u32 = 0x0002;
pub const WM_SIZE: u32 = 0x0005;
pub const WM_SETFOCUS: u32 = 0x0007;
pub const WM_KILLFOCUS: u32 = 0x0008;
pub const WM_PAINT: u32 = 0x000F;
pub const WM_CLOSE: u32 = 0x0010;
pub const WM_ERASEBKGND: u32 = 0x0014;
pub const WM_SETTINGCHANGE: u32 = 0x001A;
pub const WM_ACTIVATEAPP: u32 = 0x001C;
pub const WM_SETCURSOR: u32 = 0x0020;
pub const WM_SETICON: u32 = 0x0080;
pub const WM_NCCREATE: u32 = 0x0081;
pub const WM_DISPLAYCHANGE: u32 = 0x007E;
pub const WM_KEYDOWN: u32 = 0x0100;
pub const WM_KEYUP: u32 = 0x0101;
pub const WM_SYSKEYDOWN: u32 = 0x0104;
pub const WM_SYSKEYUP: u32 = 0x0105;
pub const WM_SYSCOMMAND: u32 = 0x0112;
pub const WM_MOUSEMOVE: u32 = 0x0200;
pub const WM_LBUTTONDOWN: u32 = 0x0201;
pub const WM_LBUTTONUP: u32 = 0x0202;
pub const WM_RBUTTONDOWN: u32 = 0x0204;
pub const WM_RBUTTONUP: u32 = 0x0205;
pub const WM_MBUTTONDOWN: u32 = 0x0207;
pub const WM_MBUTTONUP: u32 = 0x0208;
pub const WM_MOUSEWHEEL: u32 = 0x020A;
pub const WM_XBUTTONDOWN: u32 = 0x020B;
pub const WM_XBUTTONUP: u32 = 0x020C;
pub const WM_MOUSEHWHEEL: u32 = 0x020E;
pub const WM_TIMER: u32 = 0x0113;
pub const WM_ENTERSIZEMOVE: u32 = 0x0231;
pub const WM_EXITSIZEMOVE: u32 = 0x0232;
pub const WM_DPICHANGED: u32 = 0x02E0;
pub const WM_USER: u32 = 0x0400;
/// Posted by the app thread to wake the pump out of its wait (poke()).
pub const WM_SOFTER_POKE: u32 = WM_USER + 1;
/// Posted by the app thread when it has a finished frame for the pump to put up.
pub const WM_SOFTER_BLIT: u32 = WM_USER + 2;

pub const SC_KEYMENU: usize = 0xF100;
pub const SC_SCREENSAVE: usize = 0xF140;
pub const SC_MONITORPOWER: usize = 0xF170;

pub const SIZE_MINIMIZED: usize = 1;

pub const ICON_SMALL: usize = 0;
pub const ICON_BIG: usize = 1;

pub const IDC_ARROW: usize = 32512;

pub const MONITOR_DEFAULTTONEAREST: u32 = 0x0002;

pub const BI_RGB: u32 = 0;
pub const DIB_RGB_COLORS: u32 = 0;
pub const SRCCOPY: u32 = 0x00CC_0020;
pub const LOGPIXELSX: i32 = 88;
pub const VREFRESH: i32 = 116;

pub const ENUM_CURRENT_SETTINGS: u32 = 0xFFFF_FFFF;

pub const SPI_GETKEYBOARDDELAY: u32 = 0x0016;
pub const SPI_GETKEYBOARDSPEED: u32 = 0x000A;

pub const MAPVK_VK_TO_CHAR: u32 = 2;
pub const MAPVK_VSC_TO_VK_EX: u32 = 3;

pub const VK_SHIFT: u32 = 0x10;
pub const VK_CONTROL: u32 = 0x11;
pub const VK_MENU: u32 = 0x12;
pub const VK_PAUSE: u32 = 0x13;
pub const VK_NUMLOCK: u32 = 0x90;
pub const VK_F4: u32 = 0x73;

pub const WHEEL_DELTA: i32 = 120;

pub const DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2: isize = -4;
pub const DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE: isize = -3;

// ---- Vista-and-older imports ---------------------------------------------------

// ---- how a Win32 import is bound ------------------------------------------------
/// Declare each import once and get whichever binding form the build needs.
///
/// Native Windows: an ordinary `#[link]` import that the PE loader fills in. Free,
/// and exactly what this file did before cosmo existed.
///
/// Cosmopolitan: there is no import table for the loader to fill, because the APE
/// is one file that becomes a PE only at run time, and it cannot name a DLL at
/// link time without giving up being an ELF everywhere else. So each symbol is
/// fetched with `cosmo_dlsym` on first call and cached in an atomic. The call
/// sites are byte-identical either way, which is the whole point: win.rs never
/// learns which kind of build it is in.
macro_rules! win32 {
    ($( $lib:literal => { $( $(#[$m:meta])* fn $name:ident($($arg:ident: $ty:ty),* $(,)?) $(-> $ret:ty)?; )* } )*) => {
        $(
            #[cfg(not(cosmo))]
            #[link(name = $lib)]
            unsafe extern "system" {
                $( $(#[$m])* pub fn $name($($arg: $ty),*) $(-> $ret)?; )*
            }
            $(
                #[cfg(cosmo)]
                $(#[$m])*
                #[inline]
                pub unsafe fn $name($($arg: $ty),*) $(-> $ret)? {
                    // NOT `extern "system"`. In a cosmo build the target is linux,
                    // where `system` means SysV: arguments in RDI/RSI/RDX/RCX and no
                    // shadow space. Win32 wants the Microsoft x64 convention, so the
                    // call lands inside user32 reading whatever happened to be in
                    // RCX. That is a segfault deep in a system DLL with a perfectly
                    // valid function pointer, which is a memorable afternoon.
                    // aarch64 needs no such note: Windows and Linux share AAPCS64.
                    #[cfg(target_arch = "x86_64")]
                    type Sig = unsafe extern "win64" fn($($ty),*) $(-> $ret)?;
                    #[cfg(not(target_arch = "x86_64"))]
                    type Sig = unsafe extern "C" fn($($ty),*) $(-> $ret)?;
                    static CACHE: core::sync::atomic::AtomicPtr<c_void> =
                        core::sync::atomic::AtomicPtr::new(core::ptr::null_mut());
                    let mut f = CACHE.load(core::sync::atomic::Ordering::Relaxed);
                    if f.is_null() {
                        f = cosmo::sym(concat!($lib, ".dll\0"), concat!(stringify!($name), "\0"));
                        CACHE.store(f, core::sync::atomic::Ordering::Relaxed);
                    }
                    unsafe { core::mem::transmute::<*mut c_void, Sig>(f)($($arg),*) }
                }
            )*
        )*
    };
}

/// The two cosmopolitan primitives this backend stands on, and nothing more.
///
/// `__hostos` is a word in BSS that cosmo's runtime fills in before main with the
/// OS it actually landed on, which is the only honest way to choose a backend in a
/// binary whose `target_os` is a compile-time fiction. `cosmo_dlopen`/`cosmo_dlsym`
/// reach the host's own shared libraries, which on Windows means user32 and gdi32.
#[cfg(cosmo)]
pub mod cosmo {
    use core::ffi::{c_char, c_void};

    unsafe extern "C" {
        static __hostos: i32;
        fn cosmo_dlopen(path: *const c_char, mode: i32) -> *mut c_void;
        fn cosmo_dlsym(handle: *mut c_void, name: *const c_char) -> *mut c_void;
    }

    // cosmo's own numbering, confirmed by cargo_cosmo's cross-os probe.
    pub const HOST_LINUX: i32 = 1;
    pub const HOST_WINDOWS: i32 = 4;

    pub fn host_os() -> i32 { unsafe { __hostos } }
    pub fn is_windows() -> bool { host_os() == HOST_WINDOWS }

    /// Resolve one export. Both arguments must already be nul-terminated.
    ///
    /// Panics rather than returning null: a missing user32 export on a Windows host
    /// is not a condition any caller can do anything about, and transmuting null
    /// into a function would crash somewhere far less informative.
    pub fn sym(lib: &str, name: &str) -> *mut c_void {
        unsafe {
            let h = cosmo_dlopen(lib.as_ptr() as *const c_char, 1);   // RTLD_LAZY
            if h.is_null() { panic!("softer_gui: cosmo_dlopen({}) failed", lib.trim_end_matches('\0')); }
            let f = cosmo_dlsym(h, name.as_ptr() as *const c_char);
            if f.is_null() {
                panic!("softer_gui: cosmo_dlsym({}, {}) failed",
                       lib.trim_end_matches('\0'), name.trim_end_matches('\0'));
            }
            f
        }
    }
}

win32! {
    "kernel32" => {
    fn GetModuleHandleW(name: *const u16) -> HMODULE;
    fn LoadLibraryW(name: *const u16) -> HMODULE;
    fn GetProcAddress(m: HMODULE, name: *const u8) -> *const c_void;
    fn QueryPerformanceCounter(v: *mut i64) -> BOOL;
    fn QueryPerformanceFrequency(v: *mut i64) -> BOOL;
    fn CreateEventW(sa: *mut c_void, manual: BOOL, initial: BOOL, name: *const u16) -> HANDLE;
    fn SetEvent(h: HANDLE) -> BOOL;
    fn CloseHandle(h: HANDLE) -> BOOL;
    fn Sleep(ms: u32);
    fn GetLastError() -> u32;
    }
    "user32" => {
    fn RegisterClassExW(c: *const WNDCLASSEXW) -> u16;
    fn CreateWindowExW(ex: u32, class: *const u16, title: *const u16, style: u32,
                           x: i32, y: i32, w: i32, h: i32,
                           parent: HWND, menu: HANDLE, inst: HMODULE, param: *mut c_void) -> HWND;
    fn DestroyWindow(h: HWND) -> BOOL;
    fn DefWindowProcW(h: HWND, msg: u32, w: WPARAM, l: LPARAM) -> LRESULT;
    fn ShowWindow(h: HWND, cmd: i32) -> BOOL;
    fn UpdateWindow(h: HWND) -> BOOL;
    fn PeekMessageW(m: *mut MSG, h: HWND, min: u32, max: u32, remove: u32) -> BOOL;
    fn TranslateMessage(m: *const MSG) -> BOOL;
    fn DispatchMessageW(m: *const MSG) -> LRESULT;
    fn PostMessageW(h: HWND, msg: u32, w: WPARAM, l: LPARAM) -> BOOL;
    fn SendMessageW(h: HWND, msg: u32, w: WPARAM, l: LPARAM) -> LRESULT;
    fn MsgWaitForMultipleObjectsEx(n: u32, handles: *const HANDLE, ms: u32, mask: u32, flags: u32) -> u32;
    fn GetClientRect(h: HWND, r: *mut RECT) -> BOOL;
    fn GetWindowRect(h: HWND, r: *mut RECT) -> BOOL;
    fn AdjustWindowRectEx(r: *mut RECT, style: u32, menu: BOOL, ex: u32) -> BOOL;
    fn SetWindowPos(h: HWND, after: HWND, x: i32, y: i32, w: i32, ht: i32, flags: u32) -> BOOL;
    fn GetDC(h: HWND) -> HDC;
    fn ReleaseDC(h: HWND, dc: HDC) -> i32;
    fn BeginPaint(h: HWND, ps: *mut PAINTSTRUCT) -> HDC;
    fn EndPaint(h: HWND, ps: *const PAINTSTRUCT) -> BOOL;
    fn InvalidateRect(h: HWND, r: *const RECT, erase: BOOL) -> BOOL;
    fn ValidateRect(h: HWND, r: *const RECT) -> BOOL;
    fn SetWindowTextW(h: HWND, s: *const u16) -> BOOL;
    fn SetCapture(h: HWND) -> HWND;
    fn ReleaseCapture() -> BOOL;
    fn ShowCursor(show: BOOL) -> i32;
    fn SetCursor(c: HANDLE) -> HANDLE;
    fn LoadCursorW(inst: HMODULE, name: usize) -> HANDLE;
    fn GetKeyboardState(state: *mut u8) -> BOOL;
    fn GetKeyboardLayout(thread: u32) -> HANDLE;
    fn ToUnicodeEx(vk: u32, sc: u32, state: *const u8, buf: *mut u16, n: i32, flags: u32, hkl: HANDLE) -> i32;
    fn MapVirtualKeyW(code: u32, map: u32) -> u32;
    fn SystemParametersInfoW(action: u32, param: u32, ptr: *mut c_void, ini: u32) -> BOOL;
    fn MonitorFromWindow(h: HWND, flags: u32) -> HMONITOR;
    fn GetMonitorInfoW(m: HMONITOR, info: *mut MONITORINFO) -> BOOL;
    fn EnumDisplaySettingsW(name: *const u16, mode: u32, dm: *mut DEVMODEW) -> BOOL;
    fn CreateIconIndirect(info: *const ICONINFO) -> HICON;
    fn DestroyIcon(i: HICON) -> BOOL;
    fn ClientToScreen(h: HWND, p: *mut POINT) -> BOOL;
    fn ScreenToClient(h: HWND, p: *mut POINT) -> BOOL;
    fn GetCursorPos(p: *mut POINT) -> BOOL;
    fn PostQuitMessage(code: i32);
    fn SetTimer(h: HWND, id: usize, ms: u32, proc_: *const c_void) -> usize;
    fn KillTimer(h: HWND, id: usize) -> BOOL;
    #[cfg(target_pointer_width = "64")]
    fn GetWindowLongPtrW(h: HWND, index: i32) -> isize;
    #[cfg(target_pointer_width = "64")]
    fn SetWindowLongPtrW(h: HWND, index: i32, v: isize) -> isize;
    #[cfg(target_pointer_width = "32")]
    fn GetWindowLongW(h: HWND, index: i32) -> i32;
    #[cfg(target_pointer_width = "32")]
    fn SetWindowLongW(h: HWND, index: i32, v: i32) -> i32;
    }
    "gdi32" => {
    fn CreateDIBSection(dc: HDC, bmi: *const BITMAPINFO, usage: u32, bits: *mut *mut c_void, section: HANDLE, offset: u32) -> HBITMAP;
    fn CreateCompatibleDC(dc: HDC) -> HDC;
    fn CreateBitmap(w: i32, h: i32, planes: u32, bits_per_pixel: u32, bits: *const c_void) -> HBITMAP;
    fn DeleteDC(dc: HDC) -> BOOL;
    fn DeleteObject(o: HANDLE) -> BOOL;
    fn SelectObject(dc: HDC, o: HANDLE) -> HANDLE;
    fn BitBlt(dst: HDC, x: i32, y: i32, w: i32, h: i32, src: HDC, sx: i32, sy: i32, rop: u32) -> BOOL;
    fn GdiFlush() -> BOOL;
    fn GetDeviceCaps(dc: HDC, index: i32) -> i32;
    }
}


/// One spelling for the window-style accessors on both widths: Win32 makes the
/// 64-bit names macros over the 32-bit ones and only exports Ptr on 64-bit.
#[cfg(target_pointer_width = "64")]
pub unsafe fn get_window_long(h: HWND, i: i32) -> isize { unsafe { GetWindowLongPtrW(h, i) } }
#[cfg(target_pointer_width = "64")]
pub unsafe fn set_window_long(h: HWND, i: i32, v: isize) -> isize { unsafe { SetWindowLongPtrW(h, i, v) } }
#[cfg(target_pointer_width = "32")]
pub unsafe fn get_window_long(h: HWND, i: i32) -> isize { unsafe { GetWindowLongW(h, i) as isize } }
#[cfg(target_pointer_width = "32")]
pub unsafe fn set_window_long(h: HWND, i: i32, v: isize) -> isize { unsafe { SetWindowLongW(h, i, v as i32) as isize } }


// ---- dynamically resolved: everything newer than the baseline ------------------
/// Function-pointer types for the exports resolved with GetProcAddress rather than
/// imported. These carry the same ABI caveat as everything else Win32: an
/// `extern "system"` pointer in a cosmo build is SysV and calling Windows through
/// it segfaults inside a system DLL with a perfectly valid address in hand.
macro_rules! win_fn_types {
    ($( $name:ident = fn($($ty:ty),* $(,)?) $(-> $ret:ty)?; )*) => {
        $(
            #[cfg(all(cosmo, target_arch = "x86_64"))]
            pub type $name = unsafe extern "win64" fn($($ty),*) $(-> $ret)?;
            #[cfg(not(all(cosmo, target_arch = "x86_64")))]
            pub type $name = unsafe extern "system" fn($($ty),*) $(-> $ret)?;
        )*
    };
}

pub(crate) use win_fn_types;

win_fn_types! {
    FnDwmGetCompositionTimingInfo = fn(HWND, *mut DWM_TIMING_INFO) -> i32;
    FnDwmFlush = fn() -> i32;
    FnDwmIsCompositionEnabled = fn(*mut BOOL) -> i32;
    FnD3DKMTOpenAdapterFromHdc = fn(*mut D3DKMT_OPENADAPTERFROMHDC) -> i32;
    FnD3DKMTWaitForVerticalBlankEvent = fn(*const D3DKMT_WAITFORVERTICALBLANKEVENT) -> i32;
    FnSetProcessDpiAwarenessContext = fn(isize) -> BOOL;
    FnSetProcessDpiAwareness = fn(u32) -> i32;
    FnSetProcessDPIAware = fn() -> BOOL;
    FnGetDpiForWindow = fn(HWND) -> u32;
}

/// Resolved once at open(); a None member means "this Windows does not have it"
/// and the caller takes its documented fallback. Never assume a member is present.
pub struct Dyn {
    pub dwm_timing: Option<FnDwmGetCompositionTimingInfo>,
    pub dwm_flush: Option<FnDwmFlush>,
    pub dwm_enabled: Option<FnDwmIsCompositionEnabled>,
    pub kmt_open: Option<FnD3DKMTOpenAdapterFromHdc>,
    pub kmt_wait: Option<FnD3DKMTWaitForVerticalBlankEvent>,
    pub dpi_for_window: Option<FnGetDpiForWindow>,
}

pub fn wide(s: &str) -> Vec<u16> { s.encode_utf16().chain(core::iter::once(0)).collect() }

unsafe fn sym(m: HMODULE, name: &[u8]) -> *const c_void {
    if m.is_null() { return core::ptr::null(); }
    debug_assert!(name.last() == Some(&0));
    unsafe { GetProcAddress(m, name.as_ptr()) }
}

impl Dyn {
    pub fn load() -> Dyn {
        unsafe {
            let dwm = LoadLibraryW(wide("dwmapi.dll").as_ptr());
            let gdi = GetModuleHandleW(wide("gdi32.dll").as_ptr());
            let user = GetModuleHandleW(wide("user32.dll").as_ptr());
            Dyn {
                dwm_timing: core::mem::transmute::<*const c_void, Option<FnDwmGetCompositionTimingInfo>>(sym(dwm, b"DwmGetCompositionTimingInfo\0")),
                dwm_flush: core::mem::transmute::<*const c_void, Option<FnDwmFlush>>(sym(dwm, b"DwmFlush\0")),
                dwm_enabled: core::mem::transmute::<*const c_void, Option<FnDwmIsCompositionEnabled>>(sym(dwm, b"DwmIsCompositionEnabled\0")),
                kmt_open: core::mem::transmute::<*const c_void, Option<FnD3DKMTOpenAdapterFromHdc>>(sym(gdi, b"D3DKMTOpenAdapterFromHdc\0")),
                kmt_wait: core::mem::transmute::<*const c_void, Option<FnD3DKMTWaitForVerticalBlankEvent>>(sym(gdi, b"D3DKMTWaitForVerticalBlankEvent\0")),
                dpi_for_window: core::mem::transmute::<*const c_void, Option<FnGetDpiForWindow>>(sym(user, b"GetDpiForWindow\0")),
            }
        }
    }
}

/// The best DPI awareness this Windows offers, newest first. Must run before the
/// first window exists. Without it Windows lies about window sizes and stretches
/// our output, which for a crate whose premise is exact pixels is not survivable.
pub fn set_dpi_aware() {
    unsafe {
        let user = GetModuleHandleW(wide("user32.dll").as_ptr());
        let f: Option<FnSetProcessDpiAwarenessContext> = core::mem::transmute(sym(user, b"SetProcessDpiAwarenessContext\0"));
        if let Some(f) = f {
            // v2 (Windows 10 1703) scales the non-client area too; v1 is the 8.1 behaviour.
            if f(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) != 0 { return; }
            if f(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE) != 0 { return; }
        }
        let shcore = LoadLibraryW(wide("shcore.dll").as_ptr());
        let f: Option<FnSetProcessDpiAwareness> = core::mem::transmute(sym(shcore, b"SetProcessDpiAwareness\0"));
        if let Some(f) = f { if f(2) >= 0 { return; } }        // 2 = PROCESS_PER_MONITOR_DPI_AWARE
        let f: Option<FnSetProcessDPIAware> = core::mem::transmute(sym(user, b"SetProcessDPIAware\0"));
        if let Some(f) = f { f(); }                             // Vista: system-DPI aware, the best it has
    }
}

pub fn qpc() -> i64 { let mut v = 0i64; unsafe { QueryPerformanceCounter(&mut v) }; v }
pub fn qpf() -> i64 { let mut v = 0i64; unsafe { QueryPerformanceFrequency(&mut v) }; if v == 0 { 1 } else { v } }