Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

126 KB
Raw Blame
use serde::Serialize;
use tauri::ipc::{Channel, InvokeResponseBody};

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TabletStatus {
    pub active: bool,
    pub backend: String, // "wintab" | "windows-ink" | "none"
    pub wintab_available: bool,
    pub windows_ink_available: bool,
    pub device_name: Option<String>,
    pub max_pressure: u32,
}

#[derive(Debug, Clone, Copy)]
pub struct TabletSample {
    pub client_x: f32,
    pub client_y: f32,
    pub pressure: f32,
    pub tilt_x: f32,
    pub tilt_y: f32,
    pub twist: f32,
    pub timestamp_ms: f64,
    pub buttons: u16,
    pub phase: u8,   // 0=hover, 1=down, 2=move/drag, 3=up, 4=leave
    pub backend: u8, // 1=wintab, 2=windows-ink
}

pub const SAMPLE_WIRE_BYTES: usize = 36;

/// Per-pointer contact & frame deduplication state for Windows Ink WM_POINTER messages.
/// Prevents child/parent WebView2 HWND message duplication or child-HWND WM_POINTERLEAVE
/// transitions from prematurely ending an active stroke while the stylus is still in contact.
#[derive(Default, Clone, Copy, Debug)]
struct InkPointerState {
    in_contact: bool,
    consecutive_no_contact: u8,
    last_frame_id: u32,
    last_phase: u8,
    last_perf_count: u64,
    last_ts_ms: f64,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct InkPointerDecision {
    pub emit_phase: u8,
    pub prev_perf_cutoff: u64,
    pub prev_frame_id: u32,
    pub prev_ts_ms: f64,
}

#[derive(Default)]
pub struct InkStateMachine {
    pointers: std::collections::HashMap<u32, InkPointerState>,
}

impl InkStateMachine {
    pub fn reset(&mut self) {
        self.pointers.clear();
    }

    /// Force-ends all currently in-contact pointers (e.g. on window deactivation / focus loss).
    /// Returns `true` if at least one pointer was actively in contact.
    pub fn force_end_all_active(&mut self) -> bool {
        let mut had_active = false;
        for st in self.pointers.values_mut() {
            if st.in_contact {
                had_active = true;
                st.in_contact = false;
                st.consecutive_no_contact = 0;
                st.last_phase = 3;
                st.last_perf_count = 0;
            }
        }
        had_active
    }

    pub fn process_message(
        &mut self,
        hwnd: isize,
        main_hwnd: isize,
        pointer_id: u32,
        raw_phase_code: u8,
        newest_frame_id: u32,
        newest_in_contact: bool,
    ) -> Option<InkPointerDecision> {
        if self.pointers.len() > 16 && !self.pointers.contains_key(&pointer_id) {
            self.pointers.retain(|_, st| st.in_contact);
        }
        let st = self.pointers.entry(pointer_id).or_default();

        // Reject out-of-order or duplicate frames across subclassed parent + child HWNDs
        if newest_frame_id != 0 && st.last_frame_id != 0 {
            let frame_delta = newest_frame_id.wrapping_sub(st.last_frame_id) as i32;
            if frame_delta < 0 {
                return None;
            }
            if frame_delta == 0 {
                if raw_phase_code == st.last_phase {
                    return None;
                }
                if st.in_contact && (raw_phase_code == 4 || raw_phase_code == 0) {
                    return None;
                }
                if st.last_phase == 3 && raw_phase_code == 2 {
                    return None;
                }
            }
        }

        let emit_phase = match raw_phase_code {
            1 => {
                // WM_POINTERDOWN: avoid emitting duplicate down if another HWND already started contact
                let p = if st.in_contact { 2 } else { 1 };
                st.in_contact = true;
                st.consecutive_no_contact = 0;
                p
            }
            2 => {
                // WM_POINTERUPDATE
                if newest_in_contact {
                    st.consecutive_no_contact = 0;
                    st.in_contact = true;
                    2
                } else if st.in_contact {
                    // Require 2 consecutive !INCONTACT frames before ending stroke on update
                    st.consecutive_no_contact = st.consecutive_no_contact.saturating_add(1);
                    if st.consecutive_no_contact >= 2 {
                        st.in_contact = false;
                        st.consecutive_no_contact = 0;
                        3
                    } else {
                        if newest_frame_id != 0 {
                            st.last_frame_id = newest_frame_id;
                        }
                        return None;
                    }
                } else {
                    // Hover update while not in contact
                    0
                }
            }
            3 => {
                // WM_POINTERUP: ignore bogus UP while INCONTACT is still set or duplicate UP
                if newest_in_contact || !st.in_contact {
                    return None;
                }
                st.in_contact = false;
                st.consecutive_no_contact = 0;
                3
            }
            4 => {
                // WM_POINTERLEAVE: NEVER end a stroke while INCONTACT is set or on child-HWND bounce
                if newest_in_contact {
                    return None;
                }
                if st.in_contact {
                    if hwnd != main_hwnd {
                        return None;
                    }
                    // Main top-level window received WM_POINTERLEAVE while not in contact:
                    // Windows only sends one leave when the stylus exits the window, so end contact
                    // immediately instead of waiting for a second leave frame that never arrives.
                    st.in_contact = false;
                    st.consecutive_no_contact = 0;
                    3
                } else {
                    4
                }
            }
            0 => {
                // WM_POINTERENTER: do not disrupt an active stroke on child-HWND enter
                if st.in_contact {
                    return None;
                }
                0
            }
            _ => return None,
        };

        let prev_perf_cutoff = if emit_phase == 2 || emit_phase == 3 {
            st.last_perf_count
        } else {
            0
        };
        let prev_frame_id = if emit_phase == 2 || emit_phase == 3 {
            st.last_frame_id
        } else {
            0
        };
        let prev_ts_ms = st.last_ts_ms;

        if newest_frame_id != 0 {
            st.last_frame_id = newest_frame_id;
        }
        st.last_phase = emit_phase;
        if emit_phase == 1 || emit_phase == 3 || emit_phase == 4 {
            st.last_perf_count = 0;
        }

        Some(InkPointerDecision {
            emit_phase,
            prev_perf_cutoff,
            prev_frame_id,
            prev_ts_ms,
        })
    }

    pub fn record_emitted(
        &mut self,
        pointer_id: u32,
        emit_phase: u8,
        max_perf_count: u64,
        last_ts_ms: f64,
    ) {
        if let Some(st) = self.pointers.get_mut(&pointer_id) {
            if emit_phase == 1 || emit_phase == 2 {
                st.last_perf_count = max_perf_count;
            }
            st.last_ts_ms = last_ts_ms;
        }
    }
}

/// WinTab tip-contact hysteresis state machine.
/// Debounces single-packet zero-pressure micro-dips while the stylus remains in proximity
/// WITHOUT emitting post-lift hover coordinates into the active stroke (requiring >= 2
/// consecutive zero-contact packets or >= 12ms before emitting pen-up phase = 3).
#[derive(Default, Clone, Copy, Debug)]
pub struct WintabContactState {
    pub was_down: bool,
    zero_contact_streak: u8,
    zero_contact_start_ms: f64,
    last_nonzero_pressure: f32,
}

impl WintabContactState {
    pub fn reset(&mut self) {
        self.was_down = false;
        self.zero_contact_streak = 0;
        self.zero_contact_start_ms = 0.0;
        self.last_nonzero_pressure = 0.0;
    }

    /// Processes a WinTab packet and returns `Some((phase, effective_pressure, effective_tip_down))`
    /// or `None` when holding through a single-packet zero-pressure debounce window (suppressing
    /// post-lift hover coordinates so pen-up never draws a stray hook).
    pub fn step(
        &mut self,
        out_of_proximity: bool,
        raw_pressure: f32,
        tip_button_down: bool,
        timestamp_ms: f64,
    ) -> Option<(u8, f32, bool)> {
        if out_of_proximity {
            self.reset();
            return Some((4, 0.0, false));
        }

        let raw_tip_down = raw_pressure > 0.0 || tip_button_down;
        if raw_tip_down {
            self.zero_contact_streak = 0;
            self.zero_contact_start_ms = 0.0;
            if raw_pressure > 0.0 {
                self.last_nonzero_pressure = raw_pressure;
            }
            let eff_p = raw_pressure.max(0.006);
            if !self.was_down {
                self.was_down = true;
                Some((1, eff_p, true))
            } else {
                Some((2, eff_p, true))
            }
        } else if self.was_down {
            if self.zero_contact_streak == 0 {
                // First zero-pressure packet while still in proximity: hold stroke active
                // WITHOUT emitting this hover packet's coordinates into the stroke.
                self.zero_contact_streak = 1;
                self.zero_contact_start_ms = timestamp_ms;
                None
            } else {
                self.zero_contact_streak = self.zero_contact_streak.saturating_add(1);
                let elapsed_ms = (timestamp_ms - self.zero_contact_start_ms).max(0.0);
                if self.zero_contact_streak >= 2 || elapsed_ms >= 12.0 {
                    self.was_down = false;
                    self.zero_contact_streak = 0;
                    self.zero_contact_start_ms = 0.0;
                    Some((3, 0.0, false))
                } else {
                    None
                }
            }
        } else {
            Some((0, 0.0, false))
        }
    }
}

/// WinTab output-space -> virtual-screen coordinate mapper with automatic Y-orientation
/// calibration against the Win32 system cursor (CXO_SYSTEM).
///
/// In WinTab 1.4, requesting negative `lcOutExtY = -vh * scale` flips the Y axis so
/// `pk_y` spans `[0, +vh * scale]` (0 = top, +H = bottom). Dividing positive `pk_y` by
/// the signed negative `lcOutExtY` yields negative off-screen coordinates! Some drivers
/// also ignore negative `lcOutExtY` or output signed negative `pk_y ∈ [-H, 0]`.
/// `WintabCoordMapper` defaults to WinTab 1.4 (`StandardPos`: divide by `|out_ext_y|`)
/// and auto-locks the driver's exact orientation on the first off-center cursor sample.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WintabYMode {
    StandardPos,
    FlippedPos,
    SignedNeg,
}

#[derive(Debug, Default, Clone, Copy)]
pub struct WintabCoordMapper {
    locked_y_mode: Option<WintabYMode>,
}

impl WintabCoordMapper {
    pub fn locked_mode(&self) -> Option<WintabYMode> {
        self.locked_y_mode
    }

    #[allow(clippy::too_many_arguments)]
    pub fn map_to_screen(
        &mut self,
        pk_x: i32,
        pk_y: i32,
        out_org_x: i32,
        out_org_y: i32,
        out_ext_x: i32,
        out_ext_y: i32,
        sys_org_x: i32,
        sys_org_y: i32,
        sys_ext_x: i32,
        sys_ext_y: i32,
        cursor_pos: Option<(i32, i32)>,
    ) -> (f64, f64) {
        let abs_ext_x = (out_ext_x as i64).abs().max(1) as f64;
        let abs_ext_y = (out_ext_y as i64).abs().max(1) as f64;
        let nx = (pk_x as i64 - out_org_x as i64) as f64 / abs_ext_x;
        let sx = sys_org_x as f64 + nx * (sys_ext_x as f64);

        let eval_y = |mode: WintabYMode| -> f64 {
            let dy = (pk_y as i64 - out_org_y as i64) as f64;
            let ny = match mode {
                WintabYMode::StandardPos => dy / abs_ext_y,
                WintabYMode::FlippedPos => 1.0 - (dy / abs_ext_y),
                WintabYMode::SignedNeg => dy / (-abs_ext_y),
            };
            sys_org_y as f64 + ny * (sys_ext_y as f64)
        };

        let pick_best_mode = |cur_y: f64| -> (WintabYMode, f64, f64) {
            let candidates = [
                WintabYMode::StandardPos,
                WintabYMode::FlippedPos,
                WintabYMode::SignedNeg,
            ];
            let mut best_mode = WintabYMode::StandardPos;
            let mut best_sy = eval_y(best_mode);
            let mut best_err = (best_sy - cur_y).abs();
            for &m in &candidates[1..] {
                let sy = eval_y(m);
                let err = (sy - cur_y).abs();
                if err < best_err {
                    best_err = err;
                    best_mode = m;
                    best_sy = sy;
                }
            }
            (best_mode, best_sy, best_err)
        };

        let mut sy = if let Some(mode) = self.locked_y_mode {
            eval_y(mode)
        } else if let Some((_, cy)) = cursor_pos {
            let cur_y = cy as f64;
            let (best_mode, best_sy, best_err) = pick_best_mode(cur_y);
            let mid_y = sys_org_y as f64 + (sys_ext_y as f64) * 0.5;
            let off_center = (cur_y - mid_y).abs() > (sys_ext_y.unsigned_abs() as f64) * 0.06;
            if best_err <= 24.0 && (off_center || best_mode == WintabYMode::SignedNeg) {
                self.locked_y_mode = Some(best_mode);
            }
            best_sy
        } else if out_ext_y < 0 && pk_y < out_org_y.saturating_sub(16) {
            eval_y(WintabYMode::SignedNeg)
        } else {
            eval_y(WintabYMode::StandardPos)
        };

        // Guard against monitor/DPI layout changes or driver quirks:
        // Once orientation is locked, use a wide 600px ceiling so fast manga flicks (where Win32
        // GetCursorPos lags behind the high-frequency WinTab packet queue) never snap backward to
        // lagged integer cursor coordinates!
        if let Some((cx, cy)) = cursor_pos {
            let cur_x = cx as f64;
            let cur_y = cy as f64;
            let guard_px = if self.locked_y_mode.is_some() {
                600.0
            } else {
                48.0
            };
            if (sy - cur_y).abs() > guard_px {
                let (best_mode, best_sy, best_err) = pick_best_mode(cur_y);
                if best_err <= 24.0 {
                    self.locked_y_mode = Some(best_mode);
                    sy = best_sy;
                }
            }
            if (sx - cur_x).abs() > guard_px || (sy - cur_y).abs() > guard_px {
                return (cur_x, cur_y);
            }
        }

        (sx, sy)
    }
}

/// Assigns the correct per-sample phase when emitting coalesced Windows Ink history
/// oldest -> newest (`is_newest` is `true` only for `idx == 0`).
///
/// Crucially, when `phase_code == 1` (`WM_POINTERDOWN`) arrives with coalesced history `n > 1`,
/// the **first** (oldest) in-contact sample receives `phase = 1` (Down) and subsequent in-contact
/// samples receive `phase = 2` (Move) — never `2` before `1`, which would clip stroke starts!
#[inline]
pub fn assign_coalesced_ink_sample_phase(
    phase_code: u8,
    in_contact: bool,
    is_newest: bool,
    emitted_down: &mut bool,
) -> u8 {
    match phase_code {
        1 => {
            if in_contact {
                if !*emitted_down {
                    *emitted_down = true;
                    1
                } else {
                    2
                }
            } else if is_newest && !*emitted_down {
                *emitted_down = true;
                1
            } else {
                0
            }
        }
        2 => {
            if is_newest || in_contact {
                2
            } else {
                0
            }
        }
        3 => {
            if is_newest {
                3
            } else if in_contact {
                2
            } else {
                0
            }
        }
        4 => {
            if is_newest {
                4
            } else if in_contact {
                2
            } else {
                0
            }
        }
        _ => 0,
    }
}

/// Routes a stylus sample between the DX12 `rtrb` ring and JS fallback using non-blocking
/// `SharedHitTest::try_*` (`try_read()`) queries.
///
/// Returns `(ring_sample_opt, owned_by_engine)`:
/// - `ring_sample_opt`: `Some(sample)` to push into the DX12 `rtrb` ring, or `None` to avoid
///   spurious DX12 render-thread wakeups (e.g. hovering outside the unoccluded canvas viewport,
///   where only a single `phase = 4` leave transition is emitted upon exiting the canvas).
/// - `owned_by_engine`: `true` when JS Channel forwarding should be suppressed.
#[inline]
pub fn route_and_filter_native_sample(
    sample: ink_engine::NativeInputSample,
    hit_test: Option<&ink_engine::SharedHitTest>,
    native_stroke_owned: &mut bool,
    was_hovering_canvas: &mut bool,
) -> (Option<ink_engine::NativeInputSample>, bool) {
    let Some(ht) = hit_test else {
        *native_stroke_owned = false;
        *was_hovering_canvas = false;
        return (Some(sample), false);
    };
    match sample.phase {
        1 => {
            let owned = ht.try_should_capture_native_stroke(sample.phys_x, sample.phys_y);
            *native_stroke_owned = owned;
            if owned {
                *was_hovering_canvas = false;
                (Some(sample), true)
            } else if *was_hovering_canvas {
                *was_hovering_canvas = false;
                let leave = ink_engine::NativeInputSample {
                    pressure: 0.0,
                    phase: 4,
                    ..sample
                };
                (Some(leave), false)
            } else {
                (None, false)
            }
        }
        2 => {
            if *native_stroke_owned {
                *was_hovering_canvas = false;
                (Some(sample), true)
            } else {
                (None, false)
            }
        }
        3 => {
            let was_owned = *native_stroke_owned;
            *native_stroke_owned = false;
            *was_hovering_canvas = false;
            if was_owned {
                (Some(sample), true)
            } else {
                (None, false)
            }
        }
        4 => {
            let was_owned = *native_stroke_owned;
            *native_stroke_owned = false;
            if was_owned || *was_hovering_canvas {
                *was_hovering_canvas = false;
                (Some(sample), was_owned)
            } else {
                (None, false)
            }
        }
        0 => {
            let (over_canvas, capture_stroke) =
                ht.try_classify_canvas_point(sample.phys_x, sample.phys_y);
            if over_canvas {
                *was_hovering_canvas = true;
                (Some(sample), capture_stroke)
            } else if *was_hovering_canvas {
                *was_hovering_canvas = false;
                let leave = ink_engine::NativeInputSample {
                    pressure: 0.0,
                    phase: 4,
                    ..sample
                };
                (Some(leave), false)
            } else {
                (None, false)
            }
        }
        _ => (None, false),
    }
}

/// Packs a slice of TabletSamples into a raw little-endian binary frame:
/// `[count: u32 LE][sample_0: 36 bytes]...[sample_N-1: 36 bytes]`
pub fn pack_tablet_samples(samples: &[TabletSample]) -> Vec<u8> {
    let mut out = Vec::with_capacity(4 + samples.len() * SAMPLE_WIRE_BYTES);
    out.extend_from_slice(&(samples.len() as u32).to_le_bytes());
    for s in samples {
        out.extend_from_slice(&s.client_x.to_le_bytes());
        out.extend_from_slice(&s.client_y.to_le_bytes());
        out.extend_from_slice(&s.pressure.to_le_bytes());
        out.extend_from_slice(&s.tilt_x.to_le_bytes());
        out.extend_from_slice(&s.tilt_y.to_le_bytes());
        out.extend_from_slice(&s.twist.to_le_bytes());
        out.extend_from_slice(&s.timestamp_ms.to_le_bytes());
        out.extend_from_slice(&s.buttons.to_le_bytes());
        out.push(s.phase);
        out.push(s.backend);
    }
    out
}

#[cfg(target_os = "windows")]
mod win_impl {
    use super::*;
    use std::ffi::c_void;
    use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicU64, Ordering};
    use std::sync::{LazyLock, Mutex};
    use std::thread::JoinHandle;
    use windows_sys::Win32::Foundation::{FreeLibrary, BOOL, HANDLE, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM};
    use windows_sys::Win32::Graphics::Gdi::ScreenToClient;
    use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress, LoadLibraryW};
    use windows_sys::Win32::System::Performance::{
        QueryPerformanceCounter, QueryPerformanceFrequency,
    };
    use windows_sys::Win32::System::Threading::{
        GetCurrentThread, GetCurrentThreadId, SetThreadPriority, THREAD_PRIORITY_ABOVE_NORMAL,
    };
    use windows_sys::Win32::UI::HiDpi::GetDpiForWindow;
    use windows_sys::Win32::UI::Input::KeyboardAndMouse::{
        GetAsyncKeyState, VK_CONTROL, VK_MENU, VK_SHIFT, VK_SPACE,
    };
    use windows_sys::Win32::UI::Input::Pointer::{
        GetPointerPenInfoHistory, GetPointerType, POINTER_FLAG_FIRSTBUTTON,
        POINTER_FLAG_INCONTACT, POINTER_FLAG_SECONDBUTTON, POINTER_PEN_INFO,
    };

    const PEN_FLAG_BARREL: u32 = 0x0000_0001;
    const PEN_FLAG_INVERTED: u32 = 0x0000_0002;
    const PEN_FLAG_ERASER: u32 = 0x0000_0004;
    const PEN_MASK_PRESSURE: u32 = 0x0000_0001;
    const PEN_MASK_ROTATION: u32 = 0x0000_0002;
    const PEN_MASK_TILT_X: u32 = 0x0000_0004;
    const PEN_MASK_TILT_Y: u32 = 0x0000_0008;
    use windows_sys::Win32::UI::Shell::{
        DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass,
    };
    use windows_sys::Win32::UI::WindowsAndMessaging::{
        CreateWindowExW, DefWindowProcW, DestroyWindow, DispatchMessageW, EnumChildWindows,
        GetCursorPos, GetForegroundWindow, GetMessageW, GetSystemMetrics,
        GetWindowThreadProcessId, IsWindow, PostMessageW, PostThreadMessageW, RegisterClassW,
        TranslateMessage, HWND_MESSAGE, MSG, PT_PEN, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN,
        SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN, WM_ACTIVATE, WM_ACTIVATEAPP, WM_APP, WM_CREATE,
        WM_DISPLAYCHANGE, WM_DPICHANGED, WM_NCACTIVATE, WM_NCDESTROY, WM_PARENTNOTIFY,
        WM_POINTERDOWN, WM_POINTERENTER, WM_POINTERLEAVE, WM_POINTERUP, WM_POINTERUPDATE, WM_QUIT,
        WM_SETFOCUS, WNDCLASSW,
    };

    const WM_APP_WINTAB_FOCUS: u32 = WM_APP + 1;
    const WM_APP_WINTAB_BLUR: u32 = WM_APP + 2;
    const WINTAB_FOCUS_SUBCLASS_ID: usize = 0x494E_4B57; // "INKW"

    // --- WinTab ABI constants & types ---
    const WTI_INTERFACE: u32 = 1;
    const IFC_WINTABID: u32 = 1;
    const WTI_DEFSYSCTX: u32 = 4;
    const WTI_DEVICES: u32 = 100;
    const DVC_NAME: u32 = 1;
    const DVC_NPRESSURE: u32 = 15;
    const DVC_ORIENTATION: u32 = 17;

    const CXO_SYSTEM: u32 = 0x0001;
    const CXO_MESSAGES: u32 = 0x0004;

    const PK_STATUS: u32 = 0x0002;
    const PK_TIME: u32 = 0x0004;
    const PK_CURSOR: u32 = 0x0020;
    const PK_BUTTONS: u32 = 0x0040;
    const PK_X: u32 = 0x0080;
    const PK_Y: u32 = 0x0100;
    const PK_NORMAL_PRESSURE: u32 = 0x0400;
    const PK_ORIENTATION: u32 = 0x1000;

    const PACKET_DATA_MASK: u32 = PK_STATUS
        | PK_TIME
        | PK_CURSOR
        | PK_BUTTONS
        | PK_X
        | PK_Y
        | PK_NORMAL_PRESSURE
        | PK_ORIENTATION;

    const TPS_PROXIMITY: u32 = 0x0001;
    const TPS_INVERT: u32 = 0x0010;
    const WT_PACKET: u32 = 0x7FF0;
    const WT_PROXIMITY: u32 = 0x7FF5;

    type HCTX = *mut c_void;

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct Axis {
        ax_min: i32,
        ax_max: i32,
        ax_units: u32,
        ax_resolution: u32,
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct Orientation {
        or_azimuth: i32,
        or_altitude: i32,
        or_twist: i32,
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct LogContextW {
        lc_name: [u16; 40],
        lc_options: u32,
        lc_status: u32,
        lc_locks: u32,
        lc_msg_base: u32,
        lc_device: u32,
        lc_pkt_rate: u32,
        lc_pkt_data: u32,
        lc_pkt_mode: u32,
        lc_move_mask: u32,
        lc_btn_dn_mask: u32,
        lc_btn_up_mask: u32,
        lc_in_org_x: i32,
        lc_in_org_y: i32,
        lc_in_org_z: i32,
        lc_in_ext_x: i32,
        lc_in_ext_y: i32,
        lc_in_ext_z: i32,
        lc_out_org_x: i32,
        lc_out_org_y: i32,
        lc_out_org_z: i32,
        lc_out_ext_x: i32,
        lc_out_ext_y: i32,
        lc_out_ext_z: i32,
        lc_sens_x: u32,
        lc_sens_y: u32,
        lc_sens_z: u32,
        lc_sys_mode: i32,
        lc_sys_org_x: i32,
        lc_sys_org_y: i32,
        lc_sys_ext_x: i32,
        lc_sys_ext_y: i32,
        lc_sys_sens_x: u32,
        lc_sys_sens_y: u32,
    }

    #[repr(C)]
    #[derive(Clone, Copy)]
    struct WtPacket {
        pk_status: u32,
        pk_time: u32,
        pk_cursor: u32,
        pk_buttons: u32,
        pk_x: i32,
        pk_y: i32,
        pk_normal_pressure: u32,
        pk_orientation: Orientation,
    }

    type FnWTInfoW = unsafe extern "system" fn(u32, u32, *mut c_void) -> u32;
    type FnWTOpenW = unsafe extern "system" fn(HWND, *mut LogContextW, BOOL) -> HCTX;
    type FnWTGetW = unsafe extern "system" fn(HCTX, *mut LogContextW) -> BOOL;
    type FnWTClose = unsafe extern "system" fn(HCTX) -> BOOL;
    type FnWTPacketsGet = unsafe extern "system" fn(HCTX, i32, *mut c_void) -> i32;
    type FnWTOverlap = unsafe extern "system" fn(HCTX, BOOL) -> BOOL;
    type FnWTEnable = unsafe extern "system" fn(HCTX, BOOL) -> BOOL;
    type FnWTQueueSizeSet = unsafe extern "system" fn(HCTX, i32) -> BOOL;

    struct WintabLib {
        module: *mut c_void,
        wt_info: FnWTInfoW,
        wt_open: FnWTOpenW,
        wt_get: Option<FnWTGetW>,
        wt_close: FnWTClose,
        wt_packets_get: FnWTPacketsGet,
        wt_overlap: Option<FnWTOverlap>,
        wt_enable: Option<FnWTEnable>,
        wt_queue_size_set: Option<FnWTQueueSizeSet>,
    }

    unsafe impl Send for WintabLib {}
    unsafe impl Sync for WintabLib {}

    impl WintabLib {
        unsafe fn load() -> Option<Self> {
            let wide: Vec<u16> = "wintab32.dll\0".encode_utf16().collect();
            let module = LoadLibraryW(wide.as_ptr());
            if module.is_null() {
                return None;
            }
            let wt_info = GetProcAddress(module, b"WTInfoW\0".as_ptr())?;
            let wt_open = GetProcAddress(module, b"WTOpenW\0".as_ptr())?;
            let wt_get = GetProcAddress(module, b"WTGetW\0".as_ptr());
            let wt_close = GetProcAddress(module, b"WTClose\0".as_ptr())?;
            let wt_packets_get = GetProcAddress(module, b"WTPacketsGet\0".as_ptr())?;
            let wt_overlap = GetProcAddress(module, b"WTOverlap\0".as_ptr());
            let wt_enable = GetProcAddress(module, b"WTEnable\0".as_ptr());
            let wt_queue_size_set = GetProcAddress(module, b"WTQueueSizeSet\0".as_ptr());

            Some(Self {
                module,
                wt_info: std::mem::transmute(wt_info),
                wt_open: std::mem::transmute(wt_open),
                wt_get: wt_get.map(|f| std::mem::transmute(f)),
                wt_close: std::mem::transmute(wt_close),
                wt_packets_get: std::mem::transmute(wt_packets_get),
                wt_overlap: wt_overlap.map(|f| std::mem::transmute(f)),
                wt_enable: wt_enable.map(|f| std::mem::transmute(f)),
                wt_queue_size_set: wt_queue_size_set.map(|f| std::mem::transmute(f)),
            })
        }
    }

    impl Drop for WintabLib {
        fn drop(&mut self) {
            if !self.module.is_null() {
                unsafe {
                    FreeLibrary(self.module);
                }
            }
        }
    }

    const MAX_WINTAB_NAME_BYTES: usize = 4096;

    /// Safely queries a fixed-size WinTab struct via `WTInfoW` after verifying the driver's
    /// reported byte length fits within `target` (preventing stack buffer overflows).
    unsafe fn query_wintab_struct<T>(lib: &WintabLib, cat: u32, idx: u32, target: &mut T) -> bool {
        let req_bytes = (lib.wt_info)(cat, idx, std::ptr::null_mut()) as usize;
        if req_bytes == 0 || req_bytes > std::mem::size_of_val(target) {
            return false;
        }
        (lib.wt_info)(cat, idx, target as *mut T as *mut c_void) > 0
    }

    /// Safely queries the WinTab device name into a heap buffer sized to `WTInfoW`'s reported
    /// byte length (clamped to `MAX_WINTAB_NAME_BYTES`).
    unsafe fn query_wintab_device_name(lib: &WintabLib) -> Option<String> {
        let req_bytes = (lib.wt_info)(WTI_DEVICES, DVC_NAME, std::ptr::null_mut()) as usize;
        if req_bytes == 0 || req_bytes > MAX_WINTAB_NAME_BYTES {
            return Some("WinTab Digitizer".to_string());
        }
        let u16_len = req_bytes.div_ceil(2);
        let mut name_buf = vec![0u16; u16_len];
        let written = (lib.wt_info)(
            WTI_DEVICES,
            DVC_NAME,
            name_buf.as_mut_ptr() as *mut c_void,
        ) as usize;
        if written > 0 {
            let chars = (written / 2).min(name_buf.len());
            let s = String::from_utf16_lossy(&name_buf[..chars])
                .trim_end_matches('\0')
                .to_string();
            if s.is_empty() {
                None
            } else {
                Some(s)
            }
        } else {
            Some("WinTab Digitizer".to_string())
        }
    }

    fn qpc_freq_per_ms() -> f64 {
        static FREQ: LazyLock<f64> = LazyLock::new(|| unsafe {
            let mut f = 0i64;
            QueryPerformanceFrequency(&mut f);
            if f > 0 {
                f as f64 / 1000.0
            } else {
                1.0
            }
        });
        *FREQ
    }

    fn qpc_now_ms() -> f64 {
        unsafe {
            let mut now = 0i64;
            QueryPerformanceCounter(&mut now);
            (now as f64) / qpc_freq_per_ms()
        }
    }

    /// Pre-computed per-burst window coordinate transform (1 `ScreenToClient` + 1 `GetDpiForWindow`
    /// per burst instead of per sample).
    #[derive(Clone, Copy)]
    struct WindowCoordTransform {
        offset_x: f64,
        offset_y: f64,
        inv_scale: f64,
    }

    impl WindowCoordTransform {
        #[inline]
        unsafe fn for_hwnd(main_hwnd: HWND) -> Self {
            let mut origin = POINT { x: 0, y: 0 };
            ScreenToClient(main_hwnd, &mut origin);
            let dpi = GetDpiForWindow(main_hwnd);
            let inv_scale = if dpi > 0 { 96.0 / (dpi as f64) } else { 1.0 };
            Self {
                offset_x: origin.x as f64,
                offset_y: origin.y as f64,
                inv_scale,
            }
        }

        #[inline]
        fn map(&self, sx: f64, sy: f64) -> ((f32, f32), (f32, f32)) {
            let phys_x = (self.offset_x + sx) as f32;
            let phys_y = (self.offset_y + sy) as f32;
            (
                (phys_x, phys_y),
                (
                    (phys_x as f64 * self.inv_scale) as f32,
                    (phys_y as f64 * self.inv_scale) as f32,
                ),
            )
        }
    }

    /// Queries modifier key bits ONCE per `WM_POINTER*` / `WT_PACKET` burst (4 syscalls total
    /// instead of `4 * N` syscalls per burst).
    #[inline]
    unsafe fn query_modifier_bits() -> u16 {
        let mut mod_bits = 0u16;
        if (GetAsyncKeyState(VK_SHIFT as i32) as u16 & 0x8000) != 0 {
            mod_bits |= 0x0100;
        }
        if (GetAsyncKeyState(VK_MENU as i32) as u16 & 0x8000) != 0 {
            mod_bits |= 0x0200;
        }
        if (GetAsyncKeyState(VK_SPACE as i32) as u16 & 0x8000) != 0 {
            mod_bits |= 0x0400;
        }
        if (GetAsyncKeyState(VK_CONTROL as i32) as u16 & 0x8000) != 0 {
            mod_bits |= 0x0800;
        }
        mod_bits
    }

    type FnGetPointerDeviceRects =
        unsafe extern "system" fn(HANDLE, *mut RECT, *mut RECT) -> BOOL;

    #[derive(Clone, Copy)]
    struct PointerDeviceRects {
        dev_left: f64,
        dev_top: f64,
        inv_dev_w: f64,
        inv_dev_h: f64,
        disp_left: f64,
        disp_top: f64,
        disp_w: f64,
        disp_h: f64,
    }

    impl PointerDeviceRects {
        #[inline]
        fn map_himetric(
            &self,
            himetric_x: i32,
            himetric_y: i32,
            pixel_x: i32,
            pixel_y: i32,
        ) -> (f64, f64) {
            let sx = self.disp_left + ((himetric_x as f64 - self.dev_left) * self.inv_dev_w) * self.disp_w;
            let sy = self.disp_top + ((himetric_y as f64 - self.dev_top) * self.inv_dev_h) * self.disp_h;
            // Guard against exotic multi-monitor DPI discrepancies:
            // accept sub-pixel HIMETRIC only when tightly aligned with ptPixelLocationRaw.
            if (sx - pixel_x as f64).abs() <= 2.0 && (sy - pixel_y as f64).abs() <= 2.0 {
                (sx, sy)
            } else {
                (pixel_x as f64, pixel_y as f64)
            }
        }
    }

    fn query_pointer_device_rects(source_device: HANDLE) -> Option<PointerDeviceRects> {
        static FN_PTR: LazyLock<Option<FnGetPointerDeviceRects>> = LazyLock::new(|| unsafe {
            let user32: Vec<u16> = "user32.dll\0".encode_utf16().collect();
            let hmod = GetModuleHandleW(user32.as_ptr());
            if hmod.is_null() {
                return None;
            }
            let proc = GetProcAddress(hmod, b"GetPointerDeviceRects\0".as_ptr())?;
            Some(std::mem::transmute(proc))
        });

        if source_device.is_null() {
            return None;
        }
        let get_rects = (*FN_PTR)?;
        unsafe {
            let mut dev_rect: RECT = std::mem::zeroed();
            let mut disp_rect: RECT = std::mem::zeroed();
            if get_rects(source_device, &mut dev_rect, &mut disp_rect) != 0 {
                let dev_w = dev_rect.right as f64 - dev_rect.left as f64;
                let dev_h = dev_rect.bottom as f64 - dev_rect.top as f64;
                let disp_w = disp_rect.right as f64 - disp_rect.left as f64;
                let disp_h = disp_rect.bottom as f64 - disp_rect.top as f64;
                if dev_w > 0.0 && dev_h > 0.0 && disp_w > 0.0 && disp_h > 0.0 {
                    return Some(PointerDeviceRects {
                        dev_left: dev_rect.left as f64,
                        dev_top: dev_rect.top as f64,
                        inv_dev_w: 1.0 / dev_w,
                        inv_dev_h: 1.0 / dev_h,
                        disp_left: disp_rect.left as f64,
                        disp_top: disp_rect.top as f64,
                        disp_w,
                        disp_h,
                    });
                }
            }
        }
        None
    }

    /// Thread-local scratch buffers & per-`sourceDevice` `GetPointerDeviceRects` cache
    /// reused across every `WM_POINTER*` message (zero per-message heap allocations!).
    struct WinInkTlsScratch {
        device_rects_cache: Vec<(isize, Option<PointerDeviceRects>)>,
        ring_samples: Vec<ink_engine::NativeInputSample>,
        forward_js: Vec<TabletSample>,
    }

    impl WinInkTlsScratch {
        fn resolve_device_rects(&mut self, source_device: HANDLE) -> Option<PointerDeviceRects> {
            let key = source_device as isize;
            if key == 0 {
                return None;
            }
            for &(cached_key, rects) in &self.device_rects_cache {
                if cached_key == key {
                    return rects;
                }
            }
            let rects = query_pointer_device_rects(source_device);
            if self.device_rects_cache.len() >= 16 {
                self.device_rects_cache.clear();
            }
            self.device_rects_cache.push((key, rects));
            rects
        }
    }

    thread_local! {
        static WININK_SCRATCH: std::cell::RefCell<WinInkTlsScratch> = const {
            std::cell::RefCell::new(WinInkTlsScratch {
                device_rects_cache: Vec::new(),
                ring_samples: Vec::new(),
                forward_js: Vec::new(),
            })
        };
    }

    fn wintab_orientation_to_tilt(
        ori: Orientation,
        az_max: i32,
        alt_max: i32,
        twist_max: i32,
    ) -> (f32, f32, f32) {
        let mut tilt_x = 0.0f32;
        let mut tilt_y = 0.0f32;
        if az_max > 0 && alt_max > 0 {
            let az = (ori.or_azimuth as f64 / az_max as f64) * std::f64::consts::TAU;
            let alt = (ori.or_altitude.unsigned_abs() as f64 / alt_max as f64)
                * std::f64::consts::FRAC_PI_2;
            let sin_alt = alt.sin().max(1e-5);
            let cos_alt = alt.cos();
            tilt_x = (az.sin() * cos_alt).atan2(sin_alt).to_degrees() as f32;
            tilt_y = (-az.cos() * cos_alt).atan2(sin_alt).to_degrees() as f32;
        }
        let twist = if twist_max > 0 {
            ((ori.or_twist as f64 / twist_max as f64) * 360.0).rem_euclid(360.0) as f32
        } else {
            0.0
        };
        (tilt_x.clamp(-90.0, 90.0), tilt_y.clamp(-90.0, 90.0), twist)
    }

    // --- Shared active channel & state ---
    struct ActiveSink {
        channel: Channel<InvokeResponseBody>,
    }

    static ACTIVE_SINK: LazyLock<Mutex<Option<ActiveSink>>> = LazyLock::new(|| Mutex::new(None));
    static HAS_JS_SINK: AtomicBool = AtomicBool::new(false);
    static SUBCLASSED_HWNDS: LazyLock<Mutex<Vec<isize>>> = LazyLock::new(|| Mutex::new(Vec::new()));
    static WINTAB_RUNNING: AtomicBool = AtomicBool::new(false);
    static WINTAB_THREAD_ID: AtomicIsize = AtomicIsize::new(0);
    static WINTAB_MSG_HWND: AtomicIsize = AtomicIsize::new(0);
    static WINTAB_FOCUS_SUBCLASS_HWND: AtomicIsize = AtomicIsize::new(0);
    static LAST_WINTAB_PACKET_MS_BITS: AtomicU64 = AtomicU64::new(0);
    static LAST_STYLUS_ACTIVITY_MS_BITS: AtomicU64 = AtomicU64::new(0);
    static STYLUS_IN_CONTACT: AtomicBool = AtomicBool::new(false);
    static WINTAB_IN_PROXIMITY: AtomicBool = AtomicBool::new(false);
    static WINTAB_THREAD: LazyLock<Mutex<Option<JoinHandle<()>>>> =
        LazyLock::new(|| Mutex::new(None));

    // Lock-free SPSC ring handoff slots into `ink-engine`.
    static ENGINE_ATTACHED: AtomicBool = AtomicBool::new(false);
    static WINTAB_RING_PENDING: AtomicBool = AtomicBool::new(false);
    static WINTAB_RING_SLOT: LazyLock<Mutex<Option<ink_engine::TabletRingProducer>>> =
        LazyLock::new(|| Mutex::new(None));
    static ENGINE_HIT_TEST: LazyLock<Mutex<Option<ink_engine::SharedHitTest>>> =
        LazyLock::new(|| Mutex::new(None));

    /// Shared Windows Ink ring producer + ownership state (prevents TLS single-take loss
    /// across multiple windows/threads or detach/re-attach cycles).
    struct WinInkEngineShared {
        tx: Option<ink_engine::TabletRingProducer>,
        hit_test: Option<ink_engine::SharedHitTest>,
        native_stroke_owned: bool,
        was_hovering_canvas: bool,
    }

    static WININK_ENGINE_SHARED: LazyLock<parking_lot::Mutex<WinInkEngineShared>> =
        LazyLock::new(|| {
            parking_lot::Mutex::new(WinInkEngineShared {
                tx: None,
                hit_test: None,
                native_stroke_owned: false,
                was_hovering_canvas: false,
            })
        });

    pub fn attach_engine_rings(
        wintab_tx: ink_engine::TabletRingProducer,
        winink_tx: ink_engine::TabletRingProducer,
        hit_test: ink_engine::SharedHitTest,
    ) {
        if let Ok(mut guard) = ENGINE_HIT_TEST.lock() {
            *guard = Some(hit_test.clone());
        }
        if let Ok(mut guard) = WINTAB_RING_SLOT.lock() {
            *guard = Some(wintab_tx);
        }
        {
            let mut ink_eng = WININK_ENGINE_SHARED.lock();
            ink_eng.tx = Some(winink_tx);
            ink_eng.hit_test = Some(hit_test);
            ink_eng.native_stroke_owned = false;
            ink_eng.was_hovering_canvas = false;
        }
        WINTAB_RING_PENDING.store(true, Ordering::Release);
        ENGINE_ATTACHED.store(true, Ordering::Release);
    }

    pub fn detach_engine_rings() {
        ENGINE_ATTACHED.store(false, Ordering::SeqCst);
        WINTAB_RING_PENDING.store(false, Ordering::SeqCst);
        if let Ok(mut guard) = WINTAB_RING_SLOT.lock() {
            *guard = None;
        }
        if let Ok(mut guard) = ENGINE_HIT_TEST.lock() {
            *guard = None;
        }
        {
            let mut ink_eng = WININK_ENGINE_SHARED.lock();
            ink_eng.tx = None;
            ink_eng.hit_test = None;
            ink_eng.native_stroke_owned = false;
            ink_eng.was_hovering_canvas = false;
        }
    }

    #[inline]
    pub fn last_stylus_activity_ms() -> f64 {
        let bits = LAST_STYLUS_ACTIVITY_MS_BITS.load(Ordering::Relaxed);
        if bits == 0 {
            0.0
        } else {
            f64::from_bits(bits)
        }
    }

    #[inline]
    pub fn last_wintab_activity_ms() -> f64 {
        if !WINTAB_IN_PROXIMITY.load(Ordering::Acquire) {
            return 0.0;
        }
        let bits = LAST_WINTAB_PACKET_MS_BITS.load(Ordering::Relaxed);
        if bits == 0 {
            0.0
        } else {
            f64::from_bits(bits)
        }
    }

    #[inline]
    pub fn is_stylus_in_contact() -> bool {
        STYLUS_IN_CONTACT.load(Ordering::Acquire)
    }

    fn take_wintab_engine_binding() -> (
        Option<ink_engine::TabletRingProducer>,
        Option<ink_engine::SharedHitTest>,
    ) {
        if !ENGINE_ATTACHED.load(Ordering::Acquire) {
            return (None, None);
        }
        let tx = WINTAB_RING_SLOT.lock().ok().and_then(|mut g| g.take());
        let ht = ENGINE_HIT_TEST.lock().ok().and_then(|g| g.clone());
        if tx.is_some() {
            WINTAB_RING_PENDING.store(false, Ordering::Release);
        }
        (tx, ht)
    }

    static CURRENT_STATUS: LazyLock<Mutex<TabletStatus>> = LazyLock::new(|| {
        Mutex::new(TabletStatus {
            active: false,
            backend: "none".to_string(),
            wintab_available: false,
            windows_ink_available: true,
            device_name: None,
            max_pressure: 1024,
        })
    });

    const SUBCLASS_ID: usize = 0x494E_4B54; // "INKT"

    fn send_samples(samples: &[TabletSample]) {
        if samples.is_empty() || !HAS_JS_SINK.load(Ordering::Acquire) {
            return;
        }
        if let Ok(guard) = ACTIVE_SINK.lock() {
            if let Some(sink) = guard.as_ref() {
                let packed = pack_tablet_samples(samples);
                let _ = sink.channel.send(InvokeResponseBody::Raw(packed));
            }
        }
    }

    static INK_DEDUP: LazyLock<Mutex<InkStateMachine>> =
        LazyLock::new(|| Mutex::new(InkStateMachine::default()));

    // --- Windows Ink WM_POINTER subclass callback ---
    unsafe extern "system" fn ink_subclass_proc(
        hwnd: HWND,
        msg: u32,
        wparam: WPARAM,
        lparam: LPARAM,
        uid_subclass: usize,
        dw_ref_data: usize,
    ) -> LRESULT {
        let _ = std::panic::catch_unwind(|| {
            if msg == WM_NCDESTROY {
                RemoveWindowSubclass(hwnd, Some(ink_subclass_proc), uid_subclass);
                let raw = hwnd as isize;
                if let Ok(mut list) = SUBCLASSED_HWNDS.try_lock() {
                    list.retain(|&h| h != raw);
                } else if let Ok(mut list) = SUBCLASSED_HWNDS.lock() {
                    list.retain(|&h| h != raw);
                }
                return;
            }
            let main_hwnd = dw_ref_data as HWND;
            if msg == WM_DISPLAYCHANGE || msg == WM_DPICHANGED {
                WININK_SCRATCH.with(|cell| {
                    cell.borrow_mut().device_rects_cache.clear();
                });
                return;
            }
            // Catch late-created WebView2 child HWNDs (`Chrome_WidgetWin_1` / `Chrome_RenderWidgetHostHWND`)
            if msg == WM_PARENTNOTIFY && (wparam & 0xFFFF) == WM_CREATE as usize {
                ensure_child_windows_subclassed(main_hwnd);
                return;
            }
            if hwnd == main_hwnd && msg == WM_ACTIVATE && (wparam & 0xFFFF) != 0 {
                ensure_child_windows_subclassed(main_hwnd);
                return;
            }
            if hwnd == main_hwnd
                && ((msg == WM_ACTIVATE && (wparam & 0xFFFF) == 0)
                    || (msg == WM_ACTIVATEAPP && wparam == 0))
            {
                STYLUS_IN_CONTACT.store(false, Ordering::Release);
                let had_active = INK_DEDUP
                    .lock()
                    .map(|mut dedup| dedup.force_end_all_active())
                    .unwrap_or(false);
                if had_active {
                    let ts = qpc_now_ms();
                    let was_native = {
                        let mut eng_guard = WININK_ENGINE_SHARED.lock();
                        let eng = &mut *eng_guard;
                        let need_end = eng.native_stroke_owned || eng.was_hovering_canvas;
                        if need_end {
                            if let Some(tx) = eng.tx.as_mut() {
                                tx.push(ink_engine::NativeInputSample {
                                    phys_x: 0.0,
                                    phys_y: 0.0,
                                    pressure: 0.0,
                                    tilt_x: 0.0,
                                    tilt_y: 0.0,
                                    twist: 0.0,
                                    timestamp_ms: ts,
                                    buttons: 0,
                                    phase: 3,
                                    backend: 2,
                                });
                            }
                        }
                        let owned = eng.native_stroke_owned;
                        eng.native_stroke_owned = false;
                        eng.was_hovering_canvas = false;
                        owned
                    };
                    if !was_native {
                        send_samples(&[TabletSample {
                            client_x: 0.0,
                            client_y: 0.0,
                            pressure: 0.0,
                            tilt_x: 0.0,
                            tilt_y: 0.0,
                            twist: 0.0,
                            timestamp_ms: ts,
                            buttons: 0,
                            phase: 3,
                            backend: 2,
                        }]);
                    }
                }
                return;
            }

            let phase = match msg {
                WM_POINTERDOWN => Some(1u8),
                WM_POINTERUPDATE => Some(2u8),
                WM_POINTERUP => Some(3u8),
                WM_POINTERENTER => Some(0u8),
                WM_POINTERLEAVE => Some(4u8),
                _ => None,
            };

            if let Some(raw_phase_code) = phase {
                // In "auto" dual-hook mode, defer to WinTab whenever WinTab is actively delivering packets
                // or the stylus is in WinTab proximity (3s window avoids stroke-start collisions on brief pauses).
                if WINTAB_RUNNING.load(Ordering::Relaxed) {
                    let wt_prox = WINTAB_IN_PROXIMITY.load(Ordering::Acquire);
                    let last_wt_bits = LAST_WINTAB_PACKET_MS_BITS.load(Ordering::Relaxed);
                    let wt_recent = last_wt_bits != 0
                        && (qpc_now_ms() - f64::from_bits(last_wt_bits)) < 3000.0;
                    if wt_prox || wt_recent {
                        if let Ok(mut dedup) = INK_DEDUP.try_lock() {
                            let _ = dedup.force_end_all_active();
                        }
                        return;
                    }
                }
                let pointer_id = (wparam & 0xFFFF) as u32;
                let mut ptype = 0i32;
                if GetPointerType(pointer_id, &mut ptype) != 0 && ptype == PT_PEN {
                    let mut count: u32 = 64;
                    let mut history: [POINTER_PEN_INFO; 64] = std::mem::zeroed();
                    if GetPointerPenInfoHistory(pointer_id, &mut count, history.as_mut_ptr()) != 0
                        && count > 0
                    {
                        let n = (count as usize).min(64);
                        let newest_frame_id = history[0].pointerInfo.frameId;
                        let newest_in_contact =
                            (history[0].pointerInfo.pointerFlags & POINTER_FLAG_INCONTACT) != 0;
                        let ts_now = qpc_now_ms();
                        let qpc_ms = qpc_freq_per_ms();

                        // Hoist window client origin, DPI, and modifier key queries ONCE per burst!
                        let coord_xform = WindowCoordTransform::for_hwnd(main_hwnd);
                        let mod_bits = query_modifier_bits();
                        let has_js_sink = HAS_JS_SINK.load(Ordering::Relaxed);

                        WININK_SCRATCH.with(|scratch_cell| {
                            let mut scratch = scratch_cell.borrow_mut();
                            scratch.ring_samples.clear();
                            scratch.forward_js.clear();

                            // Lock INK_DEDUP ONCE per WM_POINTER* message (process + record_emitted).
                            let (phase_code, emitted_any) = {
                                let Ok(mut dedup) = INK_DEDUP.lock() else {
                                    return;
                                };
                                let Some(decision) = dedup.process_message(
                                    hwnd as isize,
                                    main_hwnd as isize,
                                    pointer_id,
                                    raw_phase_code,
                                    newest_frame_id,
                                    newest_in_contact,
                                ) else {
                                    return;
                                };
                                let phase_code = decision.emit_phase;
                                let prev_perf_cutoff = decision.prev_perf_cutoff;
                                let prev_frame_id = decision.prev_frame_id;
                                let prev_ts_ms = decision.prev_ts_ms;
                                let mut max_perf_emitted = prev_perf_cutoff;
                                let mut running_ts = prev_ts_ms;
                                let mut emitted_down = false;

                                LAST_STYLUS_ACTIVITY_MS_BITS
                                    .store(ts_now.to_bits(), Ordering::Relaxed);
                                STYLUS_IN_CONTACT.store(
                                    phase_code == 1 || phase_code == 2,
                                    Ordering::Release,
                                );

                                // Fallback span when hardware PerformanceCount is unavailable
                                let fallback_span = if prev_ts_ms > 0.0 && ts_now > prev_ts_ms + 0.5
                                {
                                    (ts_now - prev_ts_ms).min(40.0)
                                } else {
                                    (n as f64) * 4.0
                                };
                                let fallback_start = ts_now - fallback_span;

                                // Resolve cached GetPointerDeviceRects ONCE for this burst's device
                                let mut last_dev = history[0].pointerInfo.sourceDevice;
                                let mut cached_rects = scratch.resolve_device_rects(last_dev);

                                // History is reverse chronological (0 = newest), emit oldest -> newest
                                for (emit_pos, idx) in (0..n).rev().enumerate() {
                                    let info = &history[idx];
                                    let pinfo = &info.pointerInfo;

                                    // Skip already-emitted overlapping history samples during drag or pen-up
                                    // (always keep idx == 0 on phase_code == 3 so pen-up itself is never dropped).
                                    if phase_code == 2 || (phase_code == 3 && idx > 0) {
                                        if prev_perf_cutoff > 0
                                            && pinfo.PerformanceCount > 0
                                            && pinfo.PerformanceCount <= prev_perf_cutoff
                                        {
                                            continue;
                                        }
                                        if pinfo.PerformanceCount == 0
                                            && prev_frame_id != 0
                                            && pinfo.frameId != 0
                                            && (pinfo.frameId.wrapping_sub(prev_frame_id) as i32) <= 0
                                        {
                                            continue;
                                        }
                                    }
                                    if pinfo.PerformanceCount > max_perf_emitted {
                                        max_perf_emitted = pinfo.PerformanceCount;
                                    }

                                    if pinfo.sourceDevice != last_dev {
                                        last_dev = pinfo.sourceDevice;
                                        cached_rects = scratch.resolve_device_rects(last_dev);
                                    }
                                    let (sx, sy) = match cached_rects {
                                        Some(r) => r.map_himetric(
                                            pinfo.ptHimetricLocationRaw.x,
                                            pinfo.ptHimetricLocationRaw.y,
                                            pinfo.ptPixelLocationRaw.x,
                                            pinfo.ptPixelLocationRaw.y,
                                        ),
                                        None => (
                                            pinfo.ptPixelLocationRaw.x as f64,
                                            pinfo.ptPixelLocationRaw.y as f64,
                                        ),
                                    };
                                    let ((phys_x, phys_y), _) = coord_xform.map(sx, sy);

                                    let in_contact =
                                        (pinfo.pointerFlags & POINTER_FLAG_INCONTACT) != 0;
                                    let sample_phase = assign_coalesced_ink_sample_phase(
                                        phase_code,
                                        in_contact,
                                        idx == 0,
                                        &mut emitted_down,
                                    );

                                    let pressure = if (info.penMask & PEN_MASK_PRESSURE) != 0 {
                                        let p = (info.pressure as f32 / 1024.0).clamp(0.0, 1.0);
                                        if in_contact { p.max(0.006) } else { p }
                                    } else if in_contact {
                                        // Never fabricate a fake 0.5 pressure spike on initial WM_POINTERDOWN
                                        // before the pen digitizer populates PEN_MASK_PRESSURE.
                                        if sample_phase == 1 { 0.006 } else { 0.5 }
                                    } else {
                                        0.0
                                    };

                                    let tilt_x = if (info.penMask & PEN_MASK_TILT_X) != 0 {
                                        (info.tiltX as f32).clamp(-90.0, 90.0)
                                    } else {
                                        0.0
                                    };
                                    let tilt_y = if (info.penMask & PEN_MASK_TILT_Y) != 0 {
                                        (info.tiltY as f32).clamp(-90.0, 90.0)
                                    } else {
                                        0.0
                                    };
                                    let twist = if (info.penMask & PEN_MASK_ROTATION) != 0 {
                                        (info.rotation as f32).rem_euclid(360.0)
                                    } else {
                                        0.0
                                    };

                                    let mut buttons: u16 = 0;
                                    if in_contact
                                        || (pinfo.pointerFlags & POINTER_FLAG_FIRSTBUTTON) != 0
                                    {
                                        buttons |= 1;
                                    }
                                    if (info.penFlags & PEN_FLAG_BARREL) != 0
                                        || (pinfo.pointerFlags & POINTER_FLAG_SECONDBUTTON) != 0
                                    {
                                        buttons |= 2;
                                    }
                                    if (info.penFlags & (PEN_FLAG_ERASER | PEN_FLAG_INVERTED)) != 0
                                    {
                                        buttons |= 8;
                                    }

                                    let raw_sample_ts = if pinfo.PerformanceCount > 0 {
                                        (pinfo.PerformanceCount as f64) / qpc_ms
                                    } else {
                                        fallback_start
                                            + fallback_span * ((emit_pos + 1) as f64 / (n as f64))
                                    };
                                    let sample_ts = if running_ts > 0.0
                                        && raw_sample_ts < running_ts + 0.25
                                    {
                                        running_ts + 0.5
                                    } else {
                                        raw_sample_ts
                                    };
                                    running_ts = sample_ts;

                                    scratch.ring_samples.push(ink_engine::NativeInputSample {
                                        phys_x,
                                        phys_y,
                                        pressure,
                                        tilt_x,
                                        tilt_y,
                                        twist,
                                        timestamp_ms: sample_ts,
                                        buttons: buttons | mod_bits,
                                        phase: sample_phase,
                                        backend: 2,
                                    });
                                }

                                let emitted_any = !scratch.ring_samples.is_empty();
                                if emitted_any {
                                    dedup.record_emitted(
                                        pointer_id,
                                        phase_code,
                                        max_perf_emitted,
                                        running_ts,
                                    );
                                }
                                (phase_code, emitted_any)
                            };
                            let _ = phase_code;

                            if emitted_any {
                                // Route & filter into the shared DX12 SPSC ring (zero wakeups for
                                // out-of-canvas hover!) and optionally collect JS fallback samples.
                                {
                                    let mut eng_guard = WININK_ENGINE_SHARED.lock();
                                    let eng = &mut *eng_guard;
                                    let has_engine = eng.tx.is_some();
                                    let mut write_idx = 0usize;
                                    for i in 0..scratch.ring_samples.len() {
                                        let ns = scratch.ring_samples[i];
                                        let (ring_opt, owned_by_engine) = if has_engine {
                                            route_and_filter_native_sample(
                                                ns,
                                                eng.hit_test.as_ref(),
                                                &mut eng.native_stroke_owned,
                                                &mut eng.was_hovering_canvas,
                                            )
                                        } else {
                                            (None, false)
                                        };
                                        if let Some(filtered) = ring_opt {
                                            scratch.ring_samples[write_idx] = filtered;
                                            write_idx += 1;
                                        }
                                        if has_js_sink && !owned_by_engine {
                                            scratch.forward_js.push(TabletSample {
                                                client_x: (ns.phys_x as f64 * coord_xform.inv_scale)
                                                    as f32,
                                                client_y: (ns.phys_y as f64 * coord_xform.inv_scale)
                                                    as f32,
                                                pressure: ns.pressure,
                                                tilt_x: ns.tilt_x,
                                                tilt_y: ns.tilt_y,
                                                twist: ns.twist,
                                                timestamp_ms: ns.timestamp_ms,
                                                buttons: ns.buttons & 0x00FF,
                                                phase: ns.phase,
                                                backend: 2,
                                            });
                                        }
                                    }
                                    scratch.ring_samples.truncate(write_idx);
                                    if !scratch.ring_samples.is_empty() {
                                        if let Some(tx) = eng.tx.as_mut() {
                                            tx.push_batch(&scratch.ring_samples);
                                        }
                                    }
                                }

                                if has_js_sink && !scratch.forward_js.is_empty() {
                                    send_samples(&scratch.forward_js);
                                }
                            }
                        });
                    }
                }
            }
        });

        DefSubclassProc(hwnd, msg, wparam, lparam)
    }

    unsafe extern "system" fn enum_child_install_subclass(hwnd: HWND, lparam: LPARAM) -> BOOL {
        let _ = std::panic::catch_unwind(|| {
            // comctl32 window subclassing across threads is UB; only hook child HWNDs
            // owned by the current UI thread.
            if GetWindowThreadProcessId(hwnd, std::ptr::null_mut()) != GetCurrentThreadId() {
                return;
            }
            let raw = hwnd as isize;
            let already = SUBCLASSED_HWNDS
                .try_lock()
                .map(|list| list.contains(&raw))
                .unwrap_or(false);
            if already {
                return;
            }
            let main_hwnd = lparam as usize;
            if SetWindowSubclass(hwnd, Some(ink_subclass_proc), SUBCLASS_ID, main_hwnd) != 0 {
                if let Ok(mut list) = SUBCLASSED_HWNDS.lock() {
                    if !list.contains(&raw) {
                        list.push(raw);
                    }
                }
            }
        });
        1 // continue enumeration
    }

    unsafe fn ensure_child_windows_subclassed(main_hwnd: HWND) {
        if !main_hwnd.is_null() && IsWindow(main_hwnd) != 0 {
            EnumChildWindows(
                main_hwnd,
                Some(enum_child_install_subclass),
                main_hwnd as LPARAM,
            );
        }
    }

    fn install_windows_ink_hooks(main_hwnd: HWND) -> Result<(), String> {
        uninstall_windows_ink_hooks();
        unsafe {
            let main_ref = main_hwnd as usize;
            if SetWindowSubclass(main_hwnd, Some(ink_subclass_proc), SUBCLASS_ID, main_ref) != 0 {
                if let Ok(mut list) = SUBCLASSED_HWNDS.lock() {
                    list.push(main_hwnd as isize);
                }
            }
            // Also hook WebView2 child HWNDs (Chrome_WidgetWin_1 / Chrome_RenderWidgetHostHWND)
            ensure_child_windows_subclassed(main_hwnd);
        }
        Ok(())
    }

    fn uninstall_windows_ink_hooks() {
        let hwnds = {
            let Ok(mut list) = SUBCLASSED_HWNDS.lock() else {
                return;
            };
            std::mem::take(&mut *list)
        };
        for raw in hwnds {
            unsafe {
                let hwnd = raw as HWND;
                if !hwnd.is_null() && IsWindow(hwnd) != 0 {
                    RemoveWindowSubclass(hwnd, Some(ink_subclass_proc), SUBCLASS_ID);
                }
            }
        }
        WININK_SCRATCH.with(|cell| {
            cell.borrow_mut().device_rects_cache.clear();
        });
        if let Ok(mut dedup) = INK_DEDUP.lock() {
            dedup.reset();
        }
    }

    // --- WinTab dedicated high-priority message-window thread ---
    const WINTAB_SUBPIXEL_SCALE: i32 = 16;

    struct WintabRuntime {
        lib: WintabLib,
        hctx: HCTX,
        main_hwnd: HWND,
        max_pressure: u32,
        az_max: i32,
        alt_max: i32,
        twist_max: i32,
        out_org_x: i32,
        out_org_y: i32,
        out_ext_x: i32,
        out_ext_y: i32,
        sys_org_x: i32,
        sys_org_y: i32,
        sys_ext_x: i32,
        sys_ext_y: i32,
        coord_mapper: WintabCoordMapper,
        contact: WintabContactState,
        last_contact_phys: (f32, f32),
        last_contact_css: (f32, f32),
        has_contact_pos: bool,
        was_foreground: bool,
        last_ts_ms: f64,
        engine_tx: Option<ink_engine::TabletRingProducer>,
        hit_test: Option<ink_engine::SharedHitTest>,
        native_stroke_owned: bool,
        was_hovering_canvas: bool,
        scratch_pkts: Vec<WtPacket>,
        scratch_ring_samples: Vec<ink_engine::NativeInputSample>,
        scratch_forward_js: Vec<TabletSample>,
    }

    thread_local! {
        static WT_RT: std::cell::RefCell<Option<WintabRuntime>> = const { std::cell::RefCell::new(None) };
    }

    unsafe extern "system" fn wintab_focus_subclass_proc(
        hwnd: HWND,
        msg: u32,
        wparam: WPARAM,
        lparam: LPARAM,
        uid_subclass: usize,
        _dw_ref_data: usize,
    ) -> LRESULT {
        let _ = std::panic::catch_unwind(|| {
            if msg == WM_NCDESTROY {
                RemoveWindowSubclass(hwnd, Some(wintab_focus_subclass_proc), uid_subclass);
                let _ = WINTAB_FOCUS_SUBCLASS_HWND.compare_exchange(
                    hwnd as isize,
                    0,
                    Ordering::SeqCst,
                    Ordering::SeqCst,
                );
                return;
            }
            let should_raise = match msg {
                WM_ACTIVATE => (wparam & 0xFFFF) != 0,
                WM_ACTIVATEAPP => wparam != 0,
                WM_NCACTIVATE => wparam != 0,
                WM_SETFOCUS => true,
                _ => false,
            };
            let should_blur = match msg {
                WM_ACTIVATE => (wparam & 0xFFFF) == 0,
                WM_ACTIVATEAPP => wparam == 0,
                _ => false,
            };
            if should_raise || should_blur {
                let wt_hwnd = WINTAB_MSG_HWND.load(Ordering::Acquire);
                if wt_hwnd != 0 {
                    let app_msg = if should_raise {
                        WM_APP_WINTAB_FOCUS
                    } else {
                        WM_APP_WINTAB_BLUR
                    };
                    PostMessageW(wt_hwnd as HWND, app_msg, 0, 0);
                }
            }
        });
        DefSubclassProc(hwnd, msg, wparam, lparam)
    }

    fn install_wintab_focus_hook(main_hwnd: HWND) {
        uninstall_wintab_focus_hook();
        unsafe {
            if SetWindowSubclass(
                main_hwnd,
                Some(wintab_focus_subclass_proc),
                WINTAB_FOCUS_SUBCLASS_ID,
                0,
            ) != 0
            {
                WINTAB_FOCUS_SUBCLASS_HWND.store(main_hwnd as isize, Ordering::SeqCst);
            }
        }
    }

    fn uninstall_wintab_focus_hook() {
        let prev = WINTAB_FOCUS_SUBCLASS_HWND.swap(0, Ordering::SeqCst);
        if prev != 0 {
            unsafe {
                let hwnd = prev as HWND;
                if !hwnd.is_null() && IsWindow(hwnd) != 0 {
                    RemoveWindowSubclass(
                        hwnd,
                        Some(wintab_focus_subclass_proc),
                        WINTAB_FOCUS_SUBCLASS_ID,
                    );
                }
            }
        }
    }

    unsafe extern "system" fn wintab_wnd_proc(
        hwnd: HWND,
        msg: u32,
        wparam: WPARAM,
        lparam: LPARAM,
    ) -> LRESULT {
        let _ = std::panic::catch_unwind(|| {
            if msg == WM_APP_WINTAB_FOCUS {
                WT_RT.with(|cell| {
                    let mut borrow = cell.borrow_mut();
                    let Some(rt) = borrow.as_mut() else { return };
                    if let Some(enable) = rt.lib.wt_enable {
                        enable(rt.hctx, 1);
                    }
                    if let Some(overlap) = rt.lib.wt_overlap {
                        overlap(rt.hctx, 1);
                    }
                    rt.was_foreground = true;
                });
            } else if msg == WM_APP_WINTAB_BLUR {
                STYLUS_IN_CONTACT.store(false, Ordering::Release);
                WINTAB_IN_PROXIMITY.store(false, Ordering::Release);
                WT_RT.with(|cell| {
                    let mut borrow = cell.borrow_mut();
                    let Some(rt) = borrow.as_mut() else { return };
                    rt.was_foreground = false;
                    if rt.contact.was_down {
                        rt.contact.reset();
                        let ts = qpc_now_ms();
                        let was_native = rt.native_stroke_owned;
                        rt.native_stroke_owned = false;
                        rt.was_hovering_canvas = false;
                        if was_native {
                            if let Some(tx) = rt.engine_tx.as_mut() {
                                tx.push(ink_engine::NativeInputSample {
                                    phys_x: 0.0,
                                    phys_y: 0.0,
                                    pressure: 0.0,
                                    tilt_x: 0.0,
                                    tilt_y: 0.0,
                                    twist: 0.0,
                                    timestamp_ms: ts,
                                    buttons: 0,
                                    phase: 3,
                                    backend: 1,
                                });
                            }
                        } else {
                            send_samples(&[TabletSample {
                                client_x: 0.0,
                                client_y: 0.0,
                                pressure: 0.0,
                                tilt_x: 0.0,
                                tilt_y: 0.0,
                                twist: 0.0,
                                timestamp_ms: ts,
                                buttons: 0,
                                phase: 3,
                                backend: 1,
                            }]);
                        }
                    } else if rt.was_hovering_canvas {
                        rt.was_hovering_canvas = false;
                        if let Some(tx) = rt.engine_tx.as_mut() {
                            tx.push(ink_engine::NativeInputSample {
                                phys_x: 0.0,
                                phys_y: 0.0,
                                pressure: 0.0,
                                tilt_x: 0.0,
                                tilt_y: 0.0,
                                twist: 0.0,
                                timestamp_ms: qpc_now_ms(),
                                buttons: 0,
                                phase: 4,
                                backend: 1,
                            });
                        }
                    }
                });
            } else if msg == WT_PACKET {
                WT_RT.with(|cell| {
                    let mut borrow = cell.borrow_mut();
                    let Some(rt) = borrow.as_mut() else { return };

                    // Lazily claim/drop engine SPSC ring producer if engine attached/detached
                    if !ENGINE_ATTACHED.load(Ordering::Acquire) {
                        rt.engine_tx = None;
                        rt.hit_test = None;
                        rt.native_stroke_owned = false;
                        rt.was_hovering_canvas = false;
                    } else if rt.engine_tx.is_none()
                        && WINTAB_RING_PENDING.load(Ordering::Acquire)
                    {
                        let (tx, ht) = take_wintab_engine_binding();
                        rt.engine_tx = tx;
                        rt.hit_test = ht;
                    }

                    // Only assert WinTab Z-order overlap when foreground transitions to our window
                    // (avoid calling WTOverlap at 250Hz inside every WT_PACKET, which stalls some drivers)
                    let is_fg = GetForegroundWindow() == rt.main_hwnd;
                    if is_fg && !rt.was_foreground {
                        if let Some(overlap) = rt.lib.wt_overlap {
                            overlap(rt.hctx, 1);
                        }
                    }
                    rt.was_foreground = is_fg;

                    // Drain full WinTab queue (up to 4 x 64 = 256 packets) so a 128-packet queue
                    // is never half-drained under load.
                    rt.scratch_pkts.clear();
                    let mut buf: [WtPacket; 64] = std::mem::zeroed();
                    for _ in 0..4 {
                        let n = (rt.lib.wt_packets_get)(rt.hctx, 64, buf.as_mut_ptr() as *mut c_void);
                        if n <= 0 {
                            break;
                        }
                        let got = (n as usize).min(buf.len());
                        rt.scratch_pkts.extend_from_slice(&buf[..got]);
                        if got < buf.len() {
                            break;
                        }
                    }
                    let count = rt.scratch_pkts.len();
                    if count == 0 {
                        return;
                    }

                    // Drain driver queue above so it never stalls, but NEVER paint onto InkStudio
                    // when another application window has focus.
                    if !is_fg {
                        STYLUS_IN_CONTACT.store(false, Ordering::Release);
                        WINTAB_IN_PROXIMITY.store(false, Ordering::Release);
                        if rt.contact.was_down {
                            rt.contact.reset();
                            let ts = qpc_now_ms();
                            if rt.native_stroke_owned {
                                if let Some(tx) = rt.engine_tx.as_mut() {
                                    tx.push(ink_engine::NativeInputSample {
                                        phys_x: 0.0,
                                        phys_y: 0.0,
                                        pressure: 0.0,
                                        tilt_x: 0.0,
                                        tilt_y: 0.0,
                                        twist: 0.0,
                                        timestamp_ms: ts,
                                        buttons: 0,
                                        phase: 3,
                                        backend: 1,
                                    });
                                }
                            }
                            rt.native_stroke_owned = false;
                            rt.was_hovering_canvas = false;
                        } else if rt.was_hovering_canvas {
                            rt.was_hovering_canvas = false;
                            if let Some(tx) = rt.engine_tx.as_mut() {
                                tx.push(ink_engine::NativeInputSample {
                                    phys_x: 0.0,
                                    phys_y: 0.0,
                                    pressure: 0.0,
                                    tilt_x: 0.0,
                                    tilt_y: 0.0,
                                    twist: 0.0,
                                    timestamp_ms: qpc_now_ms(),
                                    buttons: 0,
                                    phase: 4,
                                    backend: 1,
                                });
                            }
                        }
                        return;
                    }

                    let ts_now = qpc_now_ms();
                    LAST_WINTAB_PACKET_MS_BITS.store(ts_now.to_bits(), Ordering::Relaxed);
                    LAST_STYLUS_ACTIVITY_MS_BITS.store(ts_now.to_bits(), Ordering::Relaxed);

                    // Hoist window client origin, DPI, and modifier key queries ONCE per burst!
                    let coord_xform = WindowCoordTransform::for_hwnd(rt.main_hwnd);
                    let mod_bits = query_modifier_bits();
                    let has_js_sink = HAS_JS_SINK.load(Ordering::Relaxed);

                    rt.scratch_ring_samples.clear();
                    rt.scratch_forward_js.clear();
                    let denom_p = (rt.max_pressure.max(1)) as f32;

                    let mut cur_pt = POINT { x: 0, y: 0 };
                    let cursor_pos = if GetCursorPos(&mut cur_pt) != 0 {
                        Some((cur_pt.x, cur_pt.y))
                    } else {
                        None
                    };

                    // Pre-calibrate Y-axis orientation on newest packet (which aligns with GetCursorPos)
                    // before mapping older packets in this burst.
                    if rt.coord_mapper.locked_mode().is_none() {
                        let newest = rt.scratch_pkts[count - 1];
                        let _ = rt.coord_mapper.map_to_screen(
                            newest.pk_x,
                            newest.pk_y,
                            rt.out_org_x,
                            rt.out_org_y,
                            rt.out_ext_x,
                            rt.out_ext_y,
                            rt.sys_org_x,
                            rt.sys_org_y,
                            rt.sys_ext_x,
                            rt.sys_ext_y,
                            cursor_pos,
                        );
                    }

                    // Resolve true hardware packet timestamps from pk_time (with clean even interpolation fallback)
                    let newest_pk_time = rt.scratch_pkts[count - 1].pk_time;
                    let oldest_pk_time = rt.scratch_pkts[0].pk_time;
                    let pk_span = newest_pk_time.wrapping_sub(oldest_pk_time);
                    let has_valid_pk_span = count > 1 && pk_span > 0 && pk_span <= 200;
                    let fallback_span = if rt.last_ts_ms > 0.0 && ts_now > rt.last_ts_ms + 0.5 {
                        (ts_now - rt.last_ts_ms).min(40.0)
                    } else {
                        (count as f64) * 4.0
                    };
                    let fallback_start = ts_now - fallback_span;
                    let has_engine = rt.engine_tx.is_some();

                    for idx in 0..count {
                        let pkt = rt.scratch_pkts[idx];
                        // Map WinTab high-res output space -> sub-pixel virtual screen -> physical + CSS client pixels.
                        // Pass cursor_pos only for the newest packet in the burst so older history points
                        // preserve exact sub-pixel positions while still guarding against driver quirks.
                        let pkt_cursor = if idx + 1 == count { cursor_pos } else { None };
                        let (sx, sy) = rt.coord_mapper.map_to_screen(
                            pkt.pk_x,
                            pkt.pk_y,
                            rt.out_org_x,
                            rt.out_org_y,
                            rt.out_ext_x,
                            rt.out_ext_y,
                            rt.sys_org_x,
                            rt.sys_org_y,
                            rt.sys_ext_x,
                            rt.sys_ext_y,
                            pkt_cursor,
                        );
                        let ((phys_x, phys_y), (cx, cy)) = coord_xform.map(sx, sy);

                        let raw_pressure = (pkt.pk_normal_pressure as f32 / denom_p).clamp(0.0, 1.0);
                        let (tilt_x, tilt_y, twist) = wintab_orientation_to_tilt(
                            pkt.pk_orientation,
                            rt.az_max,
                            rt.alt_max,
                            rt.twist_max,
                        );

                        let raw_sample_ts = if has_valid_pk_span {
                            let age_ms = newest_pk_time.wrapping_sub(pkt.pk_time).min(200) as f64;
                            ts_now - age_ms
                        } else if count > 1 {
                            fallback_start + fallback_span * ((idx + 1) as f64 / (count as f64))
                        } else {
                            ts_now
                        };
                        let sample_ts = if rt.last_ts_ms > 0.0 && raw_sample_ts < rt.last_ts_ms + 0.25
                        {
                            rt.last_ts_ms + 0.5
                        } else {
                            raw_sample_ts
                        };
                        rt.last_ts_ms = sample_ts;

                        let out_of_proximity = (pkt.pk_status & TPS_PROXIMITY) != 0;
                        let tip_button_down = (pkt.pk_buttons & 1) != 0;
                        let Some((phase, pressure, eff_tip_down)) = rt.contact.step(
                            out_of_proximity,
                            raw_pressure,
                            tip_button_down,
                            sample_ts,
                        ) else {
                            // Single-packet zero-pressure debounce hold: keep stroke alive without
                            // feeding post-lift hover coordinates into the stroke.
                            WINTAB_IN_PROXIMITY.store(!out_of_proximity, Ordering::Release);
                            STYLUS_IN_CONTACT.store(true, Ordering::Release);
                            continue;
                        };
                        WINTAB_IN_PROXIMITY.store(!out_of_proximity, Ordering::Release);
                        STYLUS_IN_CONTACT.store(eff_tip_down, Ordering::Release);

                        // Record exact last in-contact coordinates and anchor pen-up (phase 3)
                        // there so post-lift hover recoil never injects a hook at stroke end.
                        let (emit_phys_x, emit_phys_y, emit_cx, emit_cy) = if phase == 1 || phase == 2 {
                            rt.last_contact_phys = (phys_x, phys_y);
                            rt.last_contact_css = (cx, cy);
                            rt.has_contact_pos = true;
                            (phys_x, phys_y, cx, cy)
                        } else if phase == 3 && rt.has_contact_pos {
                            rt.has_contact_pos = false;
                            (
                                rt.last_contact_phys.0,
                                rt.last_contact_phys.1,
                                rt.last_contact_css.0,
                                rt.last_contact_css.1,
                            )
                        } else {
                            if phase == 4 {
                                rt.has_contact_pos = false;
                            }
                            (phys_x, phys_y, cx, cy)
                        };

                        let mut buttons: u16 = 0;
                        if eff_tip_down {
                            buttons |= 1;
                        }
                        if (pkt.pk_buttons & 2) != 0 {
                            buttons |= 2;
                        }
                        if (pkt.pk_buttons & 4) != 0 {
                            buttons |= 4;
                        }
                        if (pkt.pk_status & TPS_INVERT) != 0 {
                            buttons |= 8;
                        }

                        let ns = ink_engine::NativeInputSample {
                            phys_x: emit_phys_x,
                            phys_y: emit_phys_y,
                            pressure,
                            tilt_x,
                            tilt_y,
                            twist,
                            timestamp_ms: sample_ts,
                            buttons: buttons | mod_bits,
                            phase,
                            backend: 1,
                        };

                        let (ring_opt, owned_by_engine) = if has_engine {
                            route_and_filter_native_sample(
                                ns,
                                rt.hit_test.as_ref(),
                                &mut rt.native_stroke_owned,
                                &mut rt.was_hovering_canvas,
                            )
                        } else {
                            (None, false)
                        };

                        if let Some(filtered) = ring_opt {
                            rt.scratch_ring_samples.push(filtered);
                        }

                        if has_js_sink && !owned_by_engine {
                            rt.scratch_forward_js.push(TabletSample {
                                client_x: emit_cx,
                                client_y: emit_cy,
                                pressure,
                                tilt_x,
                                tilt_y,
                                twist,
                                timestamp_ms: sample_ts,
                                buttons,
                                phase,
                                backend: 1,
                            });
                        }
                    }

                    // Lock-free SPSC push directly into `ink-engine` ONLY when there are samples
                    // relevant to the canvas (zero wakeups when hovering sidebars/toolbars!)
                    if !rt.scratch_ring_samples.is_empty() {
                        if let Some(tx) = rt.engine_tx.as_mut() {
                            tx.push_batch(&rt.scratch_ring_samples);
                        }
                    }
                    if has_js_sink && !rt.scratch_forward_js.is_empty() {
                        send_samples(&rt.scratch_forward_js);
                    }
                });
            } else if msg == WT_PROXIMITY {
                let entering = (lparam & 0xFFFF) != 0;
                WINTAB_IN_PROXIMITY.store(entering, Ordering::Release);
                if !entering {
                    STYLUS_IN_CONTACT.store(false, Ordering::Release);
                    let ts = qpc_now_ms();
                    let was_native = WT_RT.with(|cell| {
                        if let Some(rt) = cell.borrow_mut().as_mut() {
                            rt.contact.reset();
                            let owned = rt.native_stroke_owned;
                            let was_hovering = rt.was_hovering_canvas;
                            rt.native_stroke_owned = false;
                            rt.was_hovering_canvas = false;
                            if owned || was_hovering {
                                if let Some(tx) = rt.engine_tx.as_mut() {
                                    tx.push(ink_engine::NativeInputSample {
                                        phys_x: 0.0,
                                        phys_y: 0.0,
                                        pressure: 0.0,
                                        tilt_x: 0.0,
                                        tilt_y: 0.0,
                                        twist: 0.0,
                                        timestamp_ms: ts,
                                        buttons: 0,
                                        phase: 4,
                                        backend: 1,
                                    });
                                }
                            }
                            owned
                        } else {
                            false
                        }
                    });
                    if !was_native {
                        send_samples(&[TabletSample {
                            client_x: 0.0,
                            client_y: 0.0,
                            pressure: 0.0,
                            tilt_x: 0.0,
                            tilt_y: 0.0,
                            twist: 0.0,
                            timestamp_ms: ts,
                            buttons: 0,
                            phase: 4,
                            backend: 1,
                        }]);
                    }
                }
            }
        });
        DefWindowProcW(hwnd, msg, wparam, lparam)
    }

    pub fn probe_wintab() -> (bool, Option<String>, u32) {
        unsafe {
            let Some(lib) = WintabLib::load() else {
                return (false, None, 1024);
            };
            let id_len = (lib.wt_info)(WTI_INTERFACE, IFC_WINTABID, std::ptr::null_mut());
            if id_len == 0 {
                return (false, None, 1024);
            }
            let device_name = query_wintab_device_name(&lib);

            let mut p_axis: Axis = std::mem::zeroed();
            let max_p = if query_wintab_struct(&lib, WTI_DEVICES, DVC_NPRESSURE, &mut p_axis)
                && p_axis.ax_max > 0
            {
                p_axis.ax_max as u32
            } else {
                1024
            };
            (true, device_name, max_p)
        }
    }

    fn start_wintab_thread(main_hwnd: isize) -> Result<(Option<String>, u32), String> {
        stop_wintab_thread();
        let (tx, rx) = std::sync::mpsc::channel::<Result<(Option<String>, u32), String>>();
        WINTAB_RUNNING.store(true, Ordering::SeqCst);

        let handle = std::thread::Builder::new()
            .name("inkstudio-wintab".to_string())
            .spawn(move || unsafe {
                SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
                let tid = GetCurrentThreadId();
                WINTAB_THREAD_ID.store(tid as isize, Ordering::SeqCst);

                let Some(lib) = WintabLib::load() else {
                    let _ = tx.send(Err("wintab32.dll not available".to_string()));
                    WINTAB_RUNNING.store(false, Ordering::SeqCst);
                    return;
                };

                let mut lc: LogContextW = std::mem::zeroed();
                if !query_wintab_struct(&lib, WTI_DEFSYSCTX, 0, &mut lc) {
                    let _ = tx.send(Err("WinTab driver reported no compatible default system context".to_string()));
                    WINTAB_RUNNING.store(false, Ordering::SeqCst);
                    return;
                }

                let device_name = query_wintab_device_name(&lib);

                let mut p_axis: Axis = std::mem::zeroed();
                let max_pressure = if query_wintab_struct(&lib, WTI_DEVICES, DVC_NPRESSURE, &mut p_axis)
                    && p_axis.ax_max > 0
                {
                    p_axis.ax_max as u32
                } else {
                    1024
                };

                let mut ori_axes: [Axis; 3] = std::mem::zeroed();
                let has_ori =
                    query_wintab_struct(&lib, WTI_DEVICES, DVC_ORIENTATION, &mut ori_axes);
                let (az_max, alt_max, twist_max) = if has_ori {
                    (ori_axes[0].ax_max, ori_axes[1].ax_max, ori_axes[2].ax_max)
                } else {
                    (0, 0, 0)
                };

                // Configure virtual desktop mapping
                let vx = GetSystemMetrics(SM_XVIRTUALSCREEN);
                let vy = GetSystemMetrics(SM_YVIRTUALSCREEN);
                let vw = GetSystemMetrics(SM_CXVIRTUALSCREEN).max(1);
                let vh = GetSystemMetrics(SM_CYVIRTUALSCREEN).max(1);

                lc.lc_options |= CXO_SYSTEM | CXO_MESSAGES;
                lc.lc_pkt_data = PACKET_DATA_MASK;
                lc.lc_pkt_mode = 0;
                lc.lc_move_mask = PACKET_DATA_MASK;
                lc.lc_btn_dn_mask = u32::MAX;
                lc.lc_btn_up_mask = u32::MAX;
                lc.lc_out_org_x = 0;
                lc.lc_out_org_y = 0;
                lc.lc_out_ext_x = vw.saturating_mul(WINTAB_SUBPIXEL_SCALE);
                lc.lc_out_ext_y = (-vh).saturating_mul(WINTAB_SUBPIXEL_SCALE); // top-down Y with 1/16px subpixel precision
                lc.lc_sys_org_x = vx;
                lc.lc_sys_org_y = vy;
                lc.lc_sys_ext_x = vw;
                lc.lc_sys_ext_y = vh;

                // Register lightweight message-only window class
                let class_name: Vec<u16> = "InkStudioWinTabSink\0".encode_utf16().collect();
                let wc = WNDCLASSW {
                    style: 0,
                    lpfnWndProc: Some(wintab_wnd_proc),
                    cbClsExtra: 0,
                    cbWndExtra: 0,
                    hInstance: std::ptr::null_mut(),
                    hIcon: std::ptr::null_mut(),
                    hCursor: std::ptr::null_mut(),
                    hbrBackground: std::ptr::null_mut(),
                    lpszMenuName: std::ptr::null(),
                    lpszClassName: class_name.as_ptr(),
                };
                RegisterClassW(&wc);

                let msg_hwnd = CreateWindowExW(
                    0,
                    class_name.as_ptr(),
                    class_name.as_ptr(),
                    0,
                    0,
                    0,
                    0,
                    0,
                    HWND_MESSAGE,
                    std::ptr::null_mut(),
                    std::ptr::null_mut(),
                    std::ptr::null(),
                );
                if msg_hwnd.is_null() {
                    let _ = tx.send(Err("Failed to create WinTab message window".to_string()));
                    WINTAB_RUNNING.store(false, Ordering::SeqCst);
                    return;
                }

                let hctx = (lib.wt_open)(msg_hwnd, &mut lc, 1);
                if hctx.is_null() {
                    DestroyWindow(msg_hwnd);
                    let _ = tx.send(Err("WTOpenW failed to open WinTab context".to_string()));
                    WINTAB_RUNNING.store(false, Ordering::SeqCst);
                    return;
                }

                // Re-verify active packet format matches our #[repr(C)] WtPacket layout
                // so WTPacketsGet can never write mismatched struct fields onto the stack.
                if let Some(wt_get) = lib.wt_get {
                    let mut active_lc: LogContextW = std::mem::zeroed();
                    if wt_get(hctx, &mut active_lc) != 0 {
                        lc = active_lc;
                    }
                }
                if lc.lc_pkt_data != PACKET_DATA_MASK {
                    (lib.wt_close)(hctx);
                    DestroyWindow(msg_hwnd);
                    let _ = tx.send(Err(format!(
                        "WinTab driver returned incompatible packet mask: 0x{:08X} (expected 0x{:08X})",
                        lc.lc_pkt_data, PACKET_DATA_MASK
                    )));
                    WINTAB_RUNNING.store(false, Ordering::SeqCst);
                    return;
                }

                if let Some(qset) = lib.wt_queue_size_set {
                    qset(hctx, 128);
                }
                if let Some(enable) = lib.wt_enable {
                    enable(hctx, 1);
                }
                if let Some(overlap) = lib.wt_overlap {
                    overlap(hctx, 1);
                }
                WINTAB_MSG_HWND.store(msg_hwnd as isize, Ordering::Release);

                let (engine_tx, hit_test) = take_wintab_engine_binding();

                WT_RT.with(|cell| {
                    *cell.borrow_mut() = Some(WintabRuntime {
                        lib,
                        hctx,
                        main_hwnd: main_hwnd as HWND,
                        max_pressure,
                        az_max,
                        alt_max,
                        twist_max,
                        out_org_x: lc.lc_out_org_x,
                        out_org_y: lc.lc_out_org_y,
                        out_ext_x: lc.lc_out_ext_x,
                        out_ext_y: lc.lc_out_ext_y,
                        sys_org_x: lc.lc_sys_org_x,
                        sys_org_y: lc.lc_sys_org_y,
                        sys_ext_x: lc.lc_sys_ext_x,
                        sys_ext_y: lc.lc_sys_ext_y,
                        coord_mapper: WintabCoordMapper::default(),
                        contact: WintabContactState::default(),
                        last_contact_phys: (0.0, 0.0),
                        last_contact_css: (0.0, 0.0),
                        has_contact_pos: false,
                        was_foreground: GetForegroundWindow() == main_hwnd as HWND,
                        last_ts_ms: 0.0,
                        engine_tx,
                        hit_test,
                        native_stroke_owned: false,
                        was_hovering_canvas: false,
                        scratch_pkts: Vec::with_capacity(128),
                        scratch_ring_samples: Vec::with_capacity(128),
                        scratch_forward_js: Vec::with_capacity(64),
                    });
                });

                let _ = tx.send(Ok((device_name, max_pressure)));

                let mut msg: MSG = std::mem::zeroed();
                while WINTAB_RUNNING.load(Ordering::Relaxed)
                    && GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) > 0
                {
                    TranslateMessage(&msg);
                    DispatchMessageW(&msg);
                }

                WINTAB_MSG_HWND.store(0, Ordering::Release);
                WT_RT.with(|cell| {
                    if let Some(mut rt) = cell.borrow_mut().take() {
                        // Preserve SPSC producer across WinTab restarts while engine is attached
                        if ENGINE_ATTACHED.load(Ordering::Acquire) {
                            if let Some(producer) = rt.engine_tx.take() {
                                if let Ok(mut slot) = WINTAB_RING_SLOT.lock() {
                                    *slot = Some(producer);
                                    WINTAB_RING_PENDING.store(true, Ordering::Release);
                                }
                            }
                        }
                        (rt.lib.wt_close)(rt.hctx);
                    }
                });
                DestroyWindow(msg_hwnd);
                WINTAB_THREAD_ID.store(0, Ordering::SeqCst);
                WINTAB_RUNNING.store(false, Ordering::SeqCst);
            })
            .map_err(|e| format!("Failed to spawn WinTab thread: {}", e))?;

        let init_res = rx
            .recv_timeout(std::time::Duration::from_secs(2))
            .map_err(|_| "WinTab initialization timed out".to_string())?;

        match init_res {
            Ok(info) => {
                if let Ok(mut guard) = WINTAB_THREAD.lock() {
                    *guard = Some(handle);
                }
                install_wintab_focus_hook(main_hwnd as HWND);
                Ok(info)
            }
            Err(e) => {
                let _ = handle.join();
                Err(e)
            }
        }
    }

    fn stop_wintab_thread() {
        uninstall_wintab_focus_hook();
        WINTAB_RUNNING.store(false, Ordering::SeqCst);
        WINTAB_MSG_HWND.store(0, Ordering::SeqCst);
        LAST_WINTAB_PACKET_MS_BITS.store(0, Ordering::Relaxed);
        let tid = WINTAB_THREAD_ID.load(Ordering::SeqCst);
        if tid != 0 {
            unsafe {
                PostThreadMessageW(tid as u32, WM_QUIT, 0, 0);
            }
        }
        if let Ok(mut guard) = WINTAB_THREAD.lock() {
            if let Some(handle) = guard.take() {
                let _ = handle.join();
            }
        }
    }

    pub fn start(
        window: &tauri::WebviewWindow,
        mode: &str,
        channel: Channel<InvokeResponseBody>,
    ) -> Result<TabletStatus, String> {
        stop();

        let hwnd = window
            .hwnd()
            .map_err(|e| format!("Failed to acquire window HWND: {}", e))?;
        let main_hwnd_raw = hwnd.0 as isize;

        {
            let mut sink = ACTIVE_SINK
                .lock()
                .map_err(|_| "Failed to lock tablet sink".to_string())?;
            *sink = Some(ActiveSink { channel });
            HAS_JS_SINK.store(true, Ordering::Release);
        }

        let (wt_avail, _, _) = probe_wintab();

        let (chosen_backend, device_name, max_pressure) = match mode {
            "wintab" => {
                let (dev, mp) = start_wintab_thread(main_hwnd_raw)?;
                ("wintab".to_string(), dev, mp)
            }
            "windows-ink" => {
                install_windows_ink_hooks(hwnd.0 as HWND)?;
                (
                    "windows-ink".to_string(),
                    Some("Windows Ink Digitizer".to_string()),
                    1024,
                )
            }
            "off" | "pointer-events" => {
                stop();
                return Ok(get_status());
            }
            _ => {
                // "auto": prefer WinTab if driver + tablet context opens, and ALSO install Windows Ink
                // WM_POINTER hooks as a live fallback (arbitrated by LAST_WINTAB_PACKET_MS_BITS) so
                // toggling "Windows Ink" ON/OFF in Wacom/XP-Pen/Huion drivers always delivers full pressure.
                if wt_avail {
                    match start_wintab_thread(main_hwnd_raw) {
                        Ok((dev, mp)) => {
                            let _ = install_windows_ink_hooks(hwnd.0 as HWND);
                            ("wintab".to_string(), dev, mp)
                        }
                        Err(_) => {
                            install_windows_ink_hooks(hwnd.0 as HWND)?;
                            (
                                "windows-ink".to_string(),
                                Some("Windows Ink Digitizer".to_string()),
                                1024,
                            )
                        }
                    }
                } else {
                    install_windows_ink_hooks(hwnd.0 as HWND)?;
                    (
                        "windows-ink".to_string(),
                        Some("Windows Ink Digitizer".to_string()),
                        1024,
                    )
                }
            }
        };

        let status = TabletStatus {
            active: true,
            backend: chosen_backend,
            wintab_available: wt_avail,
            windows_ink_available: true,
            device_name,
            max_pressure,
        };
        if let Ok(mut st) = CURRENT_STATUS.lock() {
            *st = status.clone();
        }
        Ok(status)
    }

    pub fn stop() {
        stop_wintab_thread();
        uninstall_windows_ink_hooks();
        STYLUS_IN_CONTACT.store(false, Ordering::SeqCst);
        WINTAB_IN_PROXIMITY.store(false, Ordering::SeqCst);
        HAS_JS_SINK.store(false, Ordering::SeqCst);
        if let Ok(mut sink) = ACTIVE_SINK.lock() {
            *sink = None;
        }
        if let Ok(mut st) = CURRENT_STATUS.lock() {
            st.active = false;
            st.backend = "none".to_string();
        }
    }

    pub fn start_native_only_if_idle(main_hwnd_raw: isize) {
        if main_hwnd_raw == 0 {
            return;
        }
        let already_active = CURRENT_STATUS
            .lock()
            .map(|st| st.active)
            .unwrap_or(false);
        if already_active {
            return;
        }
        let (wt_avail, _, _) = probe_wintab();
        let (chosen_backend, device_name, max_pressure) = if wt_avail {
            match start_wintab_thread(main_hwnd_raw) {
                Ok((dev, mp)) => {
                    let _ = install_windows_ink_hooks(main_hwnd_raw as HWND);
                    ("wintab".to_string(), dev, mp)
                }
                Err(_) => {
                    let _ = install_windows_ink_hooks(main_hwnd_raw as HWND);
                    (
                        "windows-ink".to_string(),
                        Some("Windows Ink Digitizer".to_string()),
                        1024,
                    )
                }
            }
        } else {
            let _ = install_windows_ink_hooks(main_hwnd_raw as HWND);
            (
                "windows-ink".to_string(),
                Some("Windows Ink Digitizer".to_string()),
                1024,
            )
        };
        if let Ok(mut st) = CURRENT_STATUS.lock() {
            *st = TabletStatus {
                active: true,
                backend: chosen_backend,
                wintab_available: wt_avail,
                windows_ink_available: true,
                device_name,
                max_pressure,
            };
        }
    }

    pub fn get_status() -> TabletStatus {
        if let Ok(st) = CURRENT_STATUS.lock() {
            if st.active {
                return st.clone();
            }
        }
        let (wt_avail, dev, mp) = probe_wintab();
        TabletStatus {
            active: false,
            backend: "none".to_string(),
            wintab_available: wt_avail,
            windows_ink_available: true,
            device_name: dev,
            max_pressure: mp,
        }
    }
}

#[cfg(not(target_os = "windows"))]
mod fallback_impl {
    use super::*;

    pub fn start(
        _window: &tauri::WebviewWindow,
        _mode: &str,
        _channel: Channel<InvokeResponseBody>,
    ) -> Result<TabletStatus, String> {
        Ok(get_status())
    }

    pub fn stop() {}

    pub fn get_status() -> TabletStatus {
        TabletStatus {
            active: false,
            backend: "none".to_string(),
            wintab_available: false,
            windows_ink_available: false,
            device_name: None,
            max_pressure: 1024,
        }
    }
}

pub fn start_tablet_input(
    window: &tauri::WebviewWindow,
    mode: &str,
    channel: Channel<InvokeResponseBody>,
) -> Result<TabletStatus, String> {
    #[cfg(target_os = "windows")]
    {
        win_impl::start(window, mode, channel)
    }
    #[cfg(not(target_os = "windows"))]
    {
        fallback_impl::start(window, mode, channel)
    }
}

pub fn stop_tablet_input() {
    #[cfg(target_os = "windows")]
    win_impl::stop();
    #[cfg(not(target_os = "windows"))]
    fallback_impl::stop();
}

pub fn get_tablet_status() -> TabletStatus {
    #[cfg(target_os = "windows")]
    {
        win_impl::get_status()
    }
    #[cfg(not(target_os = "windows"))]
    {
        fallback_impl::get_status()
    }
}

#[cfg(target_os = "windows")]
pub fn attach_engine_rings(
    wintab_tx: ink_engine::TabletRingProducer,
    winink_tx: ink_engine::TabletRingProducer,
    hit_test: ink_engine::SharedHitTest,
) {
    win_impl::attach_engine_rings(wintab_tx, winink_tx, hit_test);
}

#[cfg(target_os = "windows")]
pub fn detach_engine_rings() {
    win_impl::detach_engine_rings();
}

#[cfg(target_os = "windows")]
pub fn start_native_only_if_idle(main_hwnd_raw: isize) {
    win_impl::start_native_only_if_idle(main_hwnd_raw);
}

#[cfg(target_os = "windows")]
#[allow(dead_code)]
pub fn last_stylus_activity_ms() -> f64 {
    win_impl::last_stylus_activity_ms()
}

#[cfg(target_os = "windows")]
pub fn last_wintab_activity_ms() -> f64 {
    win_impl::last_wintab_activity_ms()
}

#[cfg(target_os = "windows")]
pub fn is_stylus_in_contact() -> bool {
    win_impl::is_stylus_in_contact()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pack_tablet_samples_binary_layout() {
        let s = TabletSample {
            client_x: 123.5,
            client_y: 456.25,
            pressure: 0.75,
            tilt_x: -15.0,
            tilt_y: 30.0,
            twist: 90.0,
            timestamp_ms: 123456.789,
            buttons: 9,
            phase: 2,
            backend: 1,
        };
        let buf = pack_tablet_samples(&[s]);
        assert_eq!(buf.len(), 4 + SAMPLE_WIRE_BYTES);
        assert_eq!(u32::from_le_bytes(buf[0..4].try_into().unwrap()), 1);
        assert_eq!(f32::from_le_bytes(buf[4..8].try_into().unwrap()), 123.5);
        assert_eq!(f32::from_le_bytes(buf[8..12].try_into().unwrap()), 456.25);
        assert_eq!(f32::from_le_bytes(buf[12..16].try_into().unwrap()), 0.75);
        assert_eq!(f32::from_le_bytes(buf[16..20].try_into().unwrap()), -15.0);
        assert_eq!(f32::from_le_bytes(buf[20..24].try_into().unwrap()), 30.0);
        assert_eq!(f32::from_le_bytes(buf[24..28].try_into().unwrap()), 90.0);
        assert_eq!(
            f64::from_le_bytes(buf[28..36].try_into().unwrap()),
            123456.789
        );
        assert_eq!(u16::from_le_bytes(buf[36..38].try_into().unwrap()), 9);
        assert_eq!(buf[38], 2);
        assert_eq!(buf[39], 1);
    }

    #[test]
    fn test_ink_state_machine_ignores_child_hwnd_leave_during_active_contact() {
        let mut sm = InkStateMachine::default();
        let main_hwnd: isize = 1000;
        let child_hwnd: isize = 2000;
        let pid: u32 = 42;

        // 1. Pen down on child HWND
        let d1 = sm
            .process_message(child_hwnd, main_hwnd, pid, 1, 100, true)
            .expect("pen down should emit");
        assert_eq!(d1.emit_phase, 1);

        // 2. Duplicate pen down on main HWND with same frameId is dropped
        assert!(sm
            .process_message(main_hwnd, main_hwnd, pid, 1, 100, true)
            .is_none());

        // 3. Normal drag update
        let d2 = sm
            .process_message(child_hwnd, main_hwnd, pid, 2, 101, true)
            .expect("drag should emit");
        assert_eq!(d2.emit_phase, 2);

        // 4. Spurious WM_POINTERLEAVE from child HWND while INCONTACT is still set -> dropped
        assert!(sm
            .process_message(child_hwnd, main_hwnd, pid, 4, 101, true)
            .is_none());

        // 5. Spurious WM_POINTERLEAVE from child HWND transition even with INCONTACT=false -> dropped
        assert!(sm
            .process_message(child_hwnd, main_hwnd, pid, 4, 102, false)
            .is_none());

        // 6. Out-of-order older frame from parent HWND -> dropped
        assert!(sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 100, true)
            .is_none());

        // 7. Subsequent drag continues uninterrupted
        let d3 = sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 103, true)
            .expect("subsequent drag should continue");
        assert_eq!(d3.emit_phase, 2);

        // 8. Single !in_contact update glitch does not end stroke
        assert!(sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 104, false)
            .is_none());

        // 9. Recovery frame with in_contact=true resets streak and keeps stroke alive
        let d4 = sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 105, true)
            .expect("recovery drag should emit");
        assert_eq!(d4.emit_phase, 2);

        // 10. Genuine WM_POINTERUP with in_contact=false cleanly ends stroke
        let d5 = sm
            .process_message(child_hwnd, main_hwnd, pid, 3, 106, false)
            .expect("genuine pen up should emit");
        assert_eq!(d5.emit_phase, 3);

        // 11. Duplicate WM_POINTERUP from main HWND is dropped
        assert!(sm
            .process_message(main_hwnd, main_hwnd, pid, 3, 106, false)
            .is_none());

        // 12. Consecutive !in_contact updates without WM_POINTERUP cleanly end stroke on 2nd frame
        let d6 = sm
            .process_message(main_hwnd, main_hwnd, pid, 1, 200, true)
            .expect("second stroke down");
        assert_eq!(d6.emit_phase, 1);
        assert!(sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 201, false)
            .is_none());
        let d7 = sm
            .process_message(main_hwnd, main_hwnd, pid, 2, 202, false)
            .expect("second consecutive !in_contact frame ends stroke");
        assert_eq!(d7.emit_phase, 3);
    }

    #[test]
    fn test_ink_state_machine_ends_stroke_on_main_hwnd_leave_and_window_deactivation() {
        let mut sm = InkStateMachine::default();
        let main_hwnd: isize = 1000;
        let child_hwnd: isize = 2000;
        let pid: u32 = 77;

        // 1. Start stroke on child HWND
        let d1 = sm
            .process_message(child_hwnd, main_hwnd, pid, 1, 10, true)
            .expect("pen down should emit");
        assert_eq!(d1.emit_phase, 1);

        // 2. Single WM_POINTERLEAVE on main_hwnd while !in_contact MUST end stroke immediately
        //    (Windows only delivers one leave when stylus exits the app window).
        let leave_end = sm
            .process_message(main_hwnd, main_hwnd, pid, 4, 11, false)
            .expect("main HWND leave with !in_contact must emit phase=3 immediately");
        assert_eq!(leave_end.emit_phase, 3);

        // 3. Subsequent pen down starts a brand-new stroke cleanly (phase=1, not stuck as phase=2)
        let d2 = sm
            .process_message(main_hwnd, main_hwnd, pid, 1, 20, true)
            .expect("next pen down should emit phase=1");
        assert_eq!(d2.emit_phase, 1);

        // 4. Window deactivation force_end_all_active clears stuck contact so next down emits phase=1
        assert!(sm.force_end_all_active());
        assert!(!sm.force_end_all_active());

        let d3 = sm
            .process_message(main_hwnd, main_hwnd, pid, 1, 25, true)
            .expect("pen down after deactivation reset must emit phase=1");
        assert_eq!(d3.emit_phase, 1);
    }

    #[test]
    fn test_wintab_contact_debounces_single_packet_pressure_dip() {
        let mut wt = WintabContactState::default();

        // 1. Light pen down (e.g. 4 / 8192 = 0.000488, previously dropped by > 0.001 check!)
        let (p1, pr1, down1) = wt.step(false, 0.0005, false, 100.0).unwrap();
        assert_eq!(p1, 1);
        assert!(down1);
        assert!(pr1 >= 0.006);

        // 2. Normal drag
        let (p2, pr2, down2) = wt.step(false, 0.35, true, 104.0).unwrap();
        assert_eq!(p2, 2);
        assert!(down2);
        assert!((pr2 - 0.35).abs() < 1e-5);

        // 3. Single-packet zero-pressure micro-dip while still in proximity -> held silently
        //    (does NOT end stroke AND does NOT emit post-lift hover coordinates into the stroke!)
        assert!(wt.step(false, 0.0, false, 108.0).is_none());
        assert!(wt.was_down);

        // 4. Next packet recovers light pressure -> continues seamlessly as phase=2
        let (p4, pr4, down4) = wt.step(false, 0.02, false, 112.0).unwrap();
        assert_eq!(p4, 2);
        assert!(down4);
        assert!((pr4 - 0.02).abs() < 1e-5);

        // 5. Genuine lift: 1st zero-pressure packet is suppressed (no hover hook),
        //    2nd consecutive zero-pressure packet cleanly emits phase=3
        assert!(wt.step(false, 0.0, false, 116.0).is_none());
        let (p6, pr6, down6) = wt.step(false, 0.0, false, 120.0).unwrap();
        assert_eq!(p6, 3);
        assert!(!down6);
        assert_eq!(pr6, 0.0);

        // 6. Hover packets stay phase=0
        let (p7, pr7, down7) = wt.step(false, 0.0, false, 124.0).unwrap();
        assert_eq!(p7, 0);
        assert!(!down7);
        assert_eq!(pr7, 0.0);
    }

    #[test]
    fn test_wintab_coord_mapper_handles_negative_out_ext_y_without_offscreen_y() {
        // WinTab 1.4 spec: lcOutExtY = -vh * 16 flips Y so pk_y spans [0, +vh * 16] (positive).
        // Previously dividing positive pk_y by negative out_ext_y yielded negative off-screen sy!
        let mut mapper = WintabCoordMapper::default();
        let (sx, sy) = mapper.map_to_screen(
            1280 * 16 + 8, // 1280.5px
            400 * 16 + 4,  // 400.25px (positive!)
            0,
            0,
            2560 * 16,
            -1600 * 16, // negative lcOutExtY
            0,
            0,
            2560,
            1600,
            Some((1280, 400)),
        );
        assert!((sx - 1280.5).abs() < 1e-4, "expected 1280.5, got {}", sx);
        assert!((sy - 400.25).abs() < 1e-4, "expected +400.25 (not -400.25!), got {}", sy);
        assert_eq!(mapper.locked_mode(), Some(WintabYMode::StandardPos));

        // Subsequent history samples without cursor_pos preserve exact sub-pixel coordinates
        let (sx2, sy2) = mapper.map_to_screen(
            640 * 16 + 12,
            1200 * 16 + 8,
            0,
            0,
            2560 * 16,
            -1600 * 16,
            0,
            0,
            2560,
            1600,
            None,
        );
        assert!((sx2 - 640.75).abs() < 1e-4);
        assert!((sy2 - 1200.5).abs() < 1e-4);

        // Driver that outputs bottom-up pk_y despite negative lcOutExtY auto-calibrates to FlippedPos
        let mut flipped = WintabCoordMapper::default();
        let (_, sy_f) = flipped.map_to_screen(
            500 * 16,
            1300 * 16, // bottom-up value for screen y=300 on 1600px tall screen
            0,
            0,
            2560 * 16,
            -1600 * 16,
            0,
            0,
            2560,
            1600,
            Some((500, 300)),
        );
        assert!((sy_f - 300.0).abs() < 1e-4);
        assert_eq!(flipped.locked_mode(), Some(WintabYMode::FlippedPos));

        // Driver that outputs signed-negative pk_y in [-H, 0] auto-calibrates to SignedNeg
        let mut neg = WintabCoordMapper::default();
        let (_, sy_n) = neg.map_to_screen(
            500 * 16,
            -300 * 16,
            0,
            0,
            2560 * 16,
            -1600 * 16,
            0,
            0,
            2560,
            1600,
            Some((500, 300)),
        );
        assert!((sy_n - 300.0).abs() < 1e-4);
        assert_eq!(neg.locked_mode(), Some(WintabYMode::SignedNeg));

        // Once orientation is locked, fast manga flicks (e.g. 90px ahead of lagged Win32 GetCursorPos)
        // must preserve exact sub-pixel WinTab coordinates instead of snapping back to integer cursor_pos!
        let (sx_fast, sy_fast) = mapper.map_to_screen(
            1370 * 16 + 8, // 1370.5px (90.5px ahead of lagged cursor_x=1280)
            460 * 16 + 4,  // 460.25px (60.25px ahead of lagged cursor_y=400)
            0,
            0,
            2560 * 16,
            -1600 * 16,
            0,
            0,
            2560,
            1600,
            Some((1280, 400)),
        );
        assert!((sx_fast - 1370.5).abs() < 1e-4, "fast flick snapped back to cursor: {}", sx_fast);
        assert!((sy_fast - 460.25).abs() < 1e-4, "fast flick snapped back to cursor: {}", sy_fast);
    }

    #[test]
    fn test_coalesced_pointerdown_assigns_phase_1_to_first_in_contact_sample() {
        // 1. All 3 coalesced WM_POINTERDOWN samples are in contact -> oldest gets phase=1, later get phase=2
        let mut emitted_down = false;
        let in_contact_flags = [true, true, true];
        let phases: Vec<u8> = in_contact_flags
            .iter()
            .enumerate()
            .map(|(i, &ic)| {
                assign_coalesced_ink_sample_phase(
                    1,
                    ic,
                    i + 1 == in_contact_flags.len(),
                    &mut emitted_down,
                )
            })
            .collect();
        assert_eq!(
            phases,
            vec![1, 2, 2],
            "first in-contact sample must receive phase=1, never phase=2 before phase=1"
        );

        // 2. Leading hover sample before pen touches down -> [0, 1, 2]
        let mut emitted_down = false;
        let flags_with_hover = [false, true, true];
        let phases_hover: Vec<u8> = flags_with_hover
            .iter()
            .enumerate()
            .map(|(i, &ic)| {
                assign_coalesced_ink_sample_phase(
                    1,
                    ic,
                    i + 1 == flags_with_hover.len(),
                    &mut emitted_down,
                )
            })
            .collect();
        assert_eq!(phases_hover, vec![0, 1, 2]);

        // 3. Edge case: WM_POINTERDOWN without INCONTACT bit set -> newest still gets phase=1
        let mut emitted_down = false;
        let no_contact = [false, false];
        let phases_fallback: Vec<u8> = no_contact
            .iter()
            .enumerate()
            .map(|(i, &ic)| {
                assign_coalesced_ink_sample_phase(
                    1,
                    ic,
                    i + 1 == no_contact.len(),
                    &mut emitted_down,
                )
            })
            .collect();
        assert_eq!(phases_fallback, vec![0, 1]);

        // 4. Coalesced WM_POINTERUP with trailing drag points before lift -> [2, 2, 3]
        let mut emitted_down = false;
        let up_flags = [true, true, false];
        let phases_up: Vec<u8> = up_flags
            .iter()
            .enumerate()
            .map(|(i, &ic)| {
                assign_coalesced_ink_sample_phase(
                    3,
                    ic,
                    i + 1 == up_flags.len(),
                    &mut emitted_down,
                )
            })
            .collect();
        assert_eq!(phases_up, vec![2, 2, 3]);
    }

    #[test]
    fn test_non_canvas_hover_does_not_wake_render_ring() {
        let ht = ink_engine::SharedHitTest::new();
        ht.set_visible(true);
        ht.set_viewport(ink_engine::ViewportRect {
            x_px: 200,
            y_px: 80,
            width_px: 1000,
            height_px: 700,
            dpr: 1.0,
        });
        // Occluded floating brush pill inside viewport
        ht.set_occluded_rects(vec![ink_engine::UiRect {
            x: 212.0,
            y: 280.0,
            width: 48.0,
            height: 260.0,
        }]);
        ht.set_policy(ink_engine::InteractionPolicy::default());

        let mk_hover = |x: f32, y: f32, ts: f64| ink_engine::NativeInputSample {
            phys_x: x,
            phys_y: y,
            pressure: 0.0,
            tilt_x: 0.0,
            tilt_y: 0.0,
            twist: 0.0,
            timestamp_ms: ts,
            buttons: 0,
            phase: 0,
            backend: 1,
        };

        let mut native_owned = false;
        let mut was_hovering = false;

        // 1. Hovering outside canvas (left sidebar) NEVER pushes to render ring
        for i in 0..20 {
            let (ring_opt, owned) = route_and_filter_native_sample(
                mk_hover(50.0, 200.0 + i as f32, i as f64 * 4.0),
                Some(&ht),
                &mut native_owned,
                &mut was_hovering,
            );
            assert!(ring_opt.is_none(), "sidebar hover must not wake render ring");
            assert!(!owned);
            assert!(!was_hovering);
        }

        // 2. Entering unoccluded canvas pushes hover sample & suppresses JS flood
        let (ring_in, owned_in) = route_and_filter_native_sample(
            mk_hover(600.0, 400.0, 100.0),
            Some(&ht),
            &mut native_owned,
            &mut was_hovering,
        );
        assert_eq!(ring_in.map(|s| s.phase), Some(0));
        assert!(owned_in);
        assert!(was_hovering);

        // 3. Moving over occluded FloatingBrushPill emits ONE phase=4 leave transition,
        //    then ZERO subsequent wakeups while hovering the pill
        let (ring_leave_pill, owned_pill) = route_and_filter_native_sample(
            mk_hover(230.0, 350.0, 104.0),
            Some(&ht),
            &mut native_owned,
            &mut was_hovering,
        );
        assert_eq!(ring_leave_pill.map(|s| s.phase), Some(4));
        assert!(!owned_pill);
        assert!(!was_hovering);

        for i in 0..10 {
            let (ring_opt, _) = route_and_filter_native_sample(
                mk_hover(232.0, 355.0 + i as f32, 108.0 + i as f64 * 4.0),
                Some(&ht),
                &mut native_owned,
                &mut was_hovering,
            );
            assert!(ring_opt.is_none(), "occluded UI hover must not wake render ring");
        }

        // 4. Re-enter canvas, then exit to sidebar -> exactly ONE phase=4 leave transition
        let (ring_reenter, _) = route_and_filter_native_sample(
            mk_hover(500.0, 300.0, 200.0),
            Some(&ht),
            &mut native_owned,
            &mut was_hovering,
        );
        assert_eq!(ring_reenter.map(|s| s.phase), Some(0));
        assert!(was_hovering);

        let (ring_exit, _) = route_and_filter_native_sample(
            mk_hover(40.0, 300.0, 204.0),
            Some(&ht),
            &mut native_owned,
            &mut was_hovering,
        );
        assert_eq!(ring_exit.map(|s| s.phase), Some(4));
        assert!(!was_hovering);

        let (ring_outside_again, _) = route_and_filter_native_sample(
            mk_hover(30.0, 300.0, 208.0),
            Some(&ht),
            &mut native_owned,
            &mut was_hovering,
        );
        assert!(ring_outside_again.is_none());
    }

    #[test]
    fn test_ink_state_machine_carries_perf_and_frame_cutoff_into_pointer_up() {
        let mut sm = InkStateMachine::default();
        let main_hwnd: isize = 1000;
        let pid: u32 = 99;

        let d1 = sm.process_message(main_hwnd, main_hwnd, pid, 1, 50, true).unwrap();
        assert_eq!(d1.emit_phase, 1);
        sm.record_emitted(pid, 1, 1000, 10.0);

        let d2 = sm.process_message(main_hwnd, main_hwnd, pid, 2, 51, true).unwrap();
        assert_eq!(d2.emit_phase, 2);
        assert_eq!(d2.prev_perf_cutoff, 1000);
        assert_eq!(d2.prev_frame_id, 50);
        sm.record_emitted(pid, 2, 2000, 14.0);

        // WM_POINTERUP MUST receive prior perf & frame cutoffs so overlapping coalesced history
        // samples from frame 51 are never replayed backward at the end of the stroke!
        let d3 = sm.process_message(main_hwnd, main_hwnd, pid, 3, 52, false).unwrap();
        assert_eq!(d3.emit_phase, 3);
        assert_eq!(d3.prev_perf_cutoff, 2000);
        assert_eq!(d3.prev_frame_id, 51);
    }
}