Skip to content

Commit

Permalink
Experiment in updating mouse cursor position for you
Browse files Browse the repository at this point in the history
  • Loading branch information
icefoxen committed Nov 25, 2019
1 parent a95ebc5 commit 0ff0b8e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub enum WindowEvent {
state: ElementState,
button: MouseButton,
modifiers: ModifiersState,
position: LogicalPosition,
},

/// Touchpad pressure event.
Expand Down
9 changes: 7 additions & 2 deletions src/platform_impl/linux/wayland/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::{Arc, Mutex};

use crate::dpi::LogicalPosition;
use crate::event::{
DeviceEvent, ElementState, ModifiersState, MouseButton, MouseScrollDelta, TouchPhase,
WindowEvent,
Expand Down Expand Up @@ -40,6 +41,7 @@ pub fn implement_pointer<T: 'static>(
let mut axis_buffer = None;
let mut axis_discrete_buffer = None;
let mut axis_state = TouchPhase::Ended;
let mut cursor_position: LogicalPosition = LogicalPosition::from((0.0, 0.0));

pointer.implement_closure(
move |evt, pointer| {
Expand All @@ -54,6 +56,7 @@ pub fn implement_pointer<T: 'static>(
..
} => {
let wid = store.find_wid(&surface);
cursor_position = (surface_x, surface_y).into();
if let Some(wid) = wid {
mouse_focus = Some(wid);
sink.send_window_event(
Expand All @@ -69,7 +72,7 @@ pub fn implement_pointer<T: 'static>(
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
),
position: (surface_x, surface_y).into(),
position: cursor_position,
modifiers: modifiers_tracker.lock().unwrap().clone(),
},
wid,
Expand Down Expand Up @@ -98,12 +101,13 @@ pub fn implement_pointer<T: 'static>(
..
} => {
if let Some(wid) = mouse_focus {
cursor_position = (surface_x, surface_y).into();
sink.send_window_event(
WindowEvent::CursorMoved {
device_id: crate::event::DeviceId(
crate::platform_impl::DeviceId::Wayland(DeviceId),
),
position: (surface_x, surface_y).into(),
position: cursor_position,
modifiers: modifiers_tracker.lock().unwrap().clone(),
},
wid,
Expand Down Expand Up @@ -132,6 +136,7 @@ pub fn implement_pointer<T: 'static>(
state,
button,
modifiers: modifiers_tracker.lock().unwrap().clone(),
position: cursor_position,
},
wid,
);
Expand Down
8 changes: 8 additions & 0 deletions src/platform_impl/linux/x11/event_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(super) struct EventProcessor<T: 'static> {
pub(super) target: Rc<RootELW<T>>,
pub(super) mod_keymap: ModifierKeymap,
pub(super) device_mod_state: ModifierKeyState,
pub(super) cursor_position: LogicalPosition,
}

impl<T: 'static> EventProcessor<T> {
Expand Down Expand Up @@ -659,6 +660,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Left,
modifiers,
position: self.cursor_position,
},
}),
ffi::Button2 => callback(Event::WindowEvent {
Expand All @@ -668,6 +670,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Middle,
modifiers,
position: self.cursor_position,
},
}),
ffi::Button3 => callback(Event::WindowEvent {
Expand All @@ -677,6 +680,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Right,
modifiers,
position: self.cursor_position,
},
}),

Expand Down Expand Up @@ -710,6 +714,7 @@ impl<T: 'static> EventProcessor<T> {
state,
button: Other(x as u8),
modifiers,
position: self.cursor_position,
},
}),
}
Expand All @@ -735,6 +740,7 @@ impl<T: 'static> EventProcessor<T> {
(xev.event_x as f64, xev.event_y as f64),
dpi_factor,
);
self.cursor_position = position;
callback(Event::WindowEvent {
window_id,
event: CursorMoved {
Expand Down Expand Up @@ -861,6 +867,7 @@ impl<T: 'static> EventProcessor<T> {
.expect("Failed to query pointer device")
.get_modifier_state();

self.cursor_position = position;
callback(Event::WindowEvent {
window_id,
event: CursorMoved {
Expand Down Expand Up @@ -919,6 +926,7 @@ impl<T: 'static> EventProcessor<T> {
(xev.event_x as f64, xev.event_y as f64),
dpi_factor,
);
self.cursor_position = position;
callback(Event::WindowEvent {
window_id,
event: CursorMoved {
Expand Down
9 changes: 8 additions & 1 deletion src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#![cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))]
#![cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]

mod dnd;
mod event_processor;
Expand Down Expand Up @@ -193,6 +199,7 @@ impl<T: 'static> EventLoop<T> {
xi2ext,
mod_keymap,
device_mod_state: Default::default(),
cursor_position: (0.0, 0.0).into(),
};

// Register for device hotplug events
Expand Down

0 comments on commit 0ff0b8e

Please sign in to comment.