aboutsummaryrefslogtreecommitdiff
path: root/adapters/fparkan-platform-winit/src/raw_mouse.rs
blob: 091823c21494599c8af316e029f0bf4df50cd0a7 (plain) (blame)
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
use std::mem::size_of;
use windows_sys::Win32::UI::{
    Input::{
        GetRawInputData, MOUSE_MOVE_ABSOLUTE, RAWINPUT, RAWINPUTHEADER, RID_INPUT, RIM_TYPEMOUSE,
    },
    WindowsAndMessaging::{DefWindowProcW, MSG, WM_INPUT},
};
use winit::{event_loop::EventLoopBuilder, platform::windows::EventLoopBuilderExtWindows};

// winit 0.30 tests for MOUSE_MOVE_RELATIVE as a set flag, although its value
// is zero. Keep absolute positions out of the relative-look stream. Windows
// still sends the pointer movement, button and wheel messages to the window.
pub(super) fn ignore_absolute_motion(builder: &mut EventLoopBuilder<()>) {
    builder.with_msg_hook(|message| {
        // SAFETY: The winit hook supplies a pointer to a live Win32 MSG until
        // the callback returns.
        let message = unsafe { &*message.cast::<MSG>() };
        if message.message != WM_INPUT {
            return false;
        }
        let mut input = RAWINPUT::default();
        let mut capacity = size_of::<RAWINPUT>() as u32;
        // SAFETY: lParam of WM_INPUT is a live HRAWINPUT. The output is
        // aligned and capacity describes the complete writable buffer.
        let read = unsafe {
            GetRawInputData(
                message.lParam as _,
                RID_INPUT,
                (&mut input as *mut RAWINPUT).cast(),
                &mut capacity,
                size_of::<RAWINPUTHEADER>() as u32,
            )
        };
        if read != size_of::<RAWINPUT>() as u32 || !is_absolute_mouse(&input) {
            return false;
        }
        // SAFETY: This is the original, live WM_INPUT message. Since the hook
        // consumes it, pass it to DefWindowProc to release the input handle.
        unsafe {
            DefWindowProcW(
                message.hwnd,
                message.message,
                message.wParam,
                message.lParam,
            )
        };
        true
    });
}

fn is_absolute_mouse(input: &RAWINPUT) -> bool {
    if input.header.dwType != RIM_TYPEMOUSE {
        return false;
    }
    // SAFETY: The packet type selects the mouse member of the Win32 union.
    let flags = unsafe { input.data.mouse.usFlags };
    flags & MOUSE_MOVE_ABSOLUTE != 0
}

#[cfg(test)]
mod tests {
    use super::*;
    use windows_sys::Win32::UI::Input::{
        MOUSE_MOVE_NOCOALESCE, MOUSE_VIRTUAL_DESKTOP, RIM_TYPEKEYBOARD,
    };

    #[test]
    fn only_absolute_mouse_packets_are_removed_from_raw_look() {
        let mut input = RAWINPUT::default();
        input.header.dwType = RIM_TYPEMOUSE;
        assert!(!is_absolute_mouse(&input));
        input.data.mouse.usFlags = MOUSE_MOVE_NOCOALESCE;
        assert!(!is_absolute_mouse(&input));
        input.data.mouse.usFlags = MOUSE_MOVE_ABSOLUTE | MOUSE_VIRTUAL_DESKTOP;
        assert!(is_absolute_mouse(&input));
        input.header.dwType = RIM_TYPEKEYBOARD;
        assert!(!is_absolute_mouse(&input));
    }
}