Skip to content

Commit

Permalink
use winit implementation instead
Browse files Browse the repository at this point in the history
Co-authored-by: ajtribick <[email protected]>
  • Loading branch information
2 people authored and Yu-Wei Wu committed Aug 17, 2022
1 parent a6fe800 commit 7066efd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
6 changes: 4 additions & 2 deletions .changes/filter-windows.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
"tao": patch
"tao": minor
---

Add DeviceEventFilter on Windows.
* Add DeviceEventFilter on Windows.
* **Breaking**: On Windows, device events are now ignored for unfocused windows by default, use `EventLoopWindowTarget::set_device_event_filter` to set the filter level.

15 changes: 5 additions & 10 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ impl<T: 'static> EventLoop<T> {
let runner_shared = Rc::new(EventLoopRunner::new(thread_msg_target, wait_thread_id));

let thread_msg_sender = subclass_event_target_window(thread_msg_target, runner_shared.clone());
raw_input::register_all_mice_and_keyboards_for_raw_input(thread_msg_target);
raw_input::register_all_mice_and_keyboards_for_raw_input(thread_msg_target, Default::default());

EventLoop {
thread_msg_sender,
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<T> EventLoopWindowTarget<T> {
RawDisplayHandle::Windows(WindowsDisplayHandle::empty())

pub fn set_device_event_filter(&self, filter: DeviceEventFilter) {
self.runner_shared.set_device_event_filter(filter);
raw_input::register_all_mice_and_keyboards_for_raw_input(self.thread_msg_target, filter);
}
}

Expand Down Expand Up @@ -2160,14 +2160,9 @@ unsafe extern "system" fn thread_event_target_callback<T: 'static>(
}

win32wm::WM_INPUT => {
let filter = subclass_input.event_loop_runner.device_event_filter();
if filter == DeviceEventFilter::Never
|| (filter == DeviceEventFilter::Unfocused && GetActiveWindow().0 != 0)
{
if let Some(data) = raw_input::get_raw_input_data(HRAWINPUT(lparam.0)) {
handle_raw_input(&subclass_input, data);
RedrawWindow(window, ptr::null(), HRGN::default(), RDW_INTERNALPAINT);
}
if let Some(data) = raw_input::get_raw_input_data(HRAWINPUT(lparam.0)) {
handle_raw_input(&subclass_input, data);
RedrawWindow(window, ptr::null(), HRGN::default(), RDW_INTERNALPAINT);
}

DefSubclassProc(window, msg, wparam, lparam)
Expand Down
13 changes: 1 addition & 12 deletions src/platform_impl/windows/event_loop/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use windows::Win32::{
use crate::{
dpi::PhysicalSize,
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, DeviceEventFilter},
event_loop::ControlFlow,
platform_impl::platform::util,
window::WindowId,
};
Expand All @@ -39,7 +39,6 @@ pub(crate) struct EventLoopRunner<T: 'static> {
owned_windows: Cell<HashSet<isize>>,

panic_error: Cell<Option<PanicError>>,
device_event_filter: Cell<DeviceEventFilter>,
}

pub type PanicError = Box<dyn Any + Send + 'static>;
Expand Down Expand Up @@ -78,7 +77,6 @@ impl<T> EventLoopRunner<T> {
event_handler: Cell::new(None),
event_buffer: RefCell::new(VecDeque::new()),
owned_windows: Cell::new(HashSet::new()),
device_event_filter: Cell::new(DeviceEventFilter::default()),
}
}

Expand All @@ -104,7 +102,6 @@ impl<T> EventLoopRunner<T> {
event_handler,
event_buffer: _,
owned_windows: _,
device_event_filter: _,
} = self;
runner_state.set(RunnerState::Uninitialized);
panic_error.set(None);
Expand Down Expand Up @@ -199,14 +196,6 @@ impl<T> EventLoopRunner<T> {
owned_windows.extend(&new_owned_windows);
self.owned_windows.set(owned_windows);
}

pub fn set_device_event_filter(&self, filter: DeviceEventFilter) {
self.device_event_filter.replace(filter);
}

pub fn device_event_filter(&self) -> DeviceEventFilter {
self.device_event_filter.get()
}
}

/// Event dispatch functions.
Expand Down
17 changes: 14 additions & 3 deletions src/platform_impl/windows/raw_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use windows::Win32::{
},
};

use crate::{event::ElementState, platform_impl::platform::util};
use crate::{event::ElementState, event_loop::DeviceEventFilter, platform_impl::platform::util};

#[allow(dead_code)]
pub fn get_raw_input_device_list() -> Option<Vec<RAWINPUTDEVICELIST>> {
Expand Down Expand Up @@ -130,10 +130,21 @@ pub fn register_raw_input_devices(devices: &[RAWINPUTDEVICE]) -> bool {
success.as_bool()
}

pub fn register_all_mice_and_keyboards_for_raw_input(window_handle: HWND) -> bool {
pub fn register_all_mice_and_keyboards_for_raw_input(
mut window_handle: HWND,
filter: DeviceEventFilter,
) -> bool {
// RIDEV_DEVNOTIFY: receive hotplug events
// RIDEV_INPUTSINK: receive events even if we're not in the foreground
let flags = RAWINPUTDEVICE_FLAGS(RIDEV_DEVNOTIFY.0 | RIDEV_INPUTSINK.0);
// RIDEV_REMOVE: don't receive device events (requires NULL hwndTarget)
let flags = match filter {
DeviceEventFilter::Always => {
window_handle = HWND(0);
RIDEV_REMOVE
}
DeviceEventFilter::Unfocused => RIDEV_DEVNOTIFY,
DeviceEventFilter::Never => RIDEV_DEVNOTIFY | RIDEV_INPUTSINK,
};

let devices: [RAWINPUTDEVICE; 2] = [
RAWINPUTDEVICE {
Expand Down

0 comments on commit 7066efd

Please sign in to comment.