Skip to content

Commit 753907e

Browse files
committed
Implement mouse event scaling for X11
1 parent 9e4dc0e commit 753907e

1 file changed

Lines changed: 27 additions & 22 deletions

File tree

plugin-canvas/src/platform/x11/window.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{cell::RefCell, ffi::OsStr, num::NonZeroU32, ptr::NonNull, sync::atomic
33

44
use cursor_icon::CursorIcon;
55
use keyboard_types::Code;
6+
use portable_atomic::AtomicF64;
67
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
78
use x11rb::{COPY_DEPTH_FROM_PARENT, COPY_FROM_PARENT};
89
use x11rb::connection::Connection;
@@ -30,6 +31,7 @@ pub struct OsWindow {
3031
keyboard_modifiers: RefCell<KeyboardModifiers>,
3132

3233
showing_cursor: AtomicBool,
34+
scale: AtomicF64,
3335
}
3436

3537
impl OsWindow {
@@ -42,16 +44,13 @@ impl OsWindow {
4244
x11rb::protocol::Event::ButtonPress(event) => {
4345
self.update_modifiers_from_mask(event.state);
4446

45-
let position = LogicalPosition {
46-
x: event.event_x as _,
47-
y: event.event_y as _,
48-
};
47+
let position = self.convert_mouse_position(event.event_x, event.event_y);
4948

5049
if let Some(button) = Self::mouse_button_from_detail(event.detail) {
5150
self.send_event(Event::MouseButtonDown {
5251
button,
5352
position,
54-
});
53+
});
5554
} else if [4, 5].contains(&event.detail) {
5655
let delta_y = if event.detail == 4 {
5756
1.0
@@ -70,16 +69,13 @@ impl OsWindow {
7069
x11rb::protocol::Event::ButtonRelease(event) => {
7170
self.update_modifiers_from_mask(event.state);
7271

73-
let position = LogicalPosition {
74-
x: event.event_x as _,
75-
y: event.event_y as _,
76-
};
72+
let position = self.convert_mouse_position(event.event_x, event.event_y);
7773

7874
if let Some(button) = Self::mouse_button_from_detail(event.detail) {
7975
self.send_event(Event::MouseButtonUp {
8076
button,
8177
position,
82-
});
78+
});
8379
}
8480
}
8581

@@ -132,7 +128,7 @@ impl OsWindow {
132128

133129
let text = xkb_state.key_get_utf8(x11_keycode);
134130
xkb_state.update_key(x11_keycode, xkb::KeyDirection::Up);
135-
131+
136132
self.send_event(Event::KeyUp {
137133
key_code: keycode,
138134
text: Some(text),
@@ -146,14 +142,11 @@ impl OsWindow {
146142
x11rb::protocol::Event::MotionNotify(event) => {
147143
self.update_modifiers_from_mask(event.state);
148144

149-
let position = LogicalPosition {
150-
x: event.event_x as _,
151-
y: event.event_y as _,
152-
};
145+
let position = self.convert_mouse_position(event.event_x, event.event_y);
153146

154147
self.send_event(Event::MouseMoved { position });
155148
}
156-
149+
157150
_ => {},
158151
}
159152

@@ -169,6 +162,15 @@ impl OsWindow {
169162
}
170163
}
171164

165+
fn convert_mouse_position(&self, x: i16, y: i16) -> LogicalPosition {
166+
let scale = self.scale.load(Ordering::Acquire);
167+
168+
LogicalPosition {
169+
x: x as f64 / scale,
170+
y: y as f64 / scale,
171+
}
172+
}
173+
172174
fn update_modifiers_from_keycode(&self, keycode: Code, down: bool) {
173175
let mut modifiers = self.keyboard_modifiers.borrow_mut();
174176
let mut new_modifiers = *modifiers;
@@ -220,7 +222,7 @@ impl OsWindow {
220222
locales.push("C".into());
221223

222224
let mut xkb_compose_state = None;
223-
225+
224226
for locale in locales.iter() {
225227
if let Ok(compose_table) = xkb::compose::Table::new_from_locale(&xkb_context, OsStr::new(&locale), 0) {
226228
xkb_compose_state = Some(xkb::compose::State::new(&compose_table, 0));
@@ -265,11 +267,11 @@ impl OsWindowInterface for OsWindow {
265267
COPY_FROM_PARENT,
266268
&CreateWindowAux::new()
267269
.event_mask(
268-
EventMask::BUTTON_PRESS |
270+
EventMask::BUTTON_PRESS |
269271
EventMask::BUTTON_RELEASE |
270-
EventMask::KEY_PRESS |
271-
EventMask::KEY_RELEASE |
272-
EventMask::LEAVE_WINDOW |
272+
EventMask::KEY_PRESS |
273+
EventMask::KEY_RELEASE |
274+
EventMask::LEAVE_WINDOW |
273275
EventMask::POINTER_MOTION
274276
),
275277
)?;
@@ -298,6 +300,7 @@ impl OsWindowInterface for OsWindow {
298300
keyboard_modifiers: KeyboardModifiers::empty().into(),
299301

300302
showing_cursor: true.into(),
303+
scale: window_attributes.scale().into(),
301304
};
302305

303306
Ok(OsWindowHandle::new(Arc::new(window.into())))
@@ -307,13 +310,15 @@ impl OsWindowInterface for OsWindow {
307310
1.0
308311
}
309312

310-
fn resized(&self, size: crate::LogicalSize) {
313+
fn resized(&self, size: crate::LogicalSize, scale: f64) {
311314
self.connection.configure_window(
312315
self.window_handle.window.into(),
313316
&ConfigureWindowAux::new()
314317
.width(size.width as u32)
315318
.height(size.height as u32),
316319
).unwrap();
320+
321+
self.scale.store(scale, Ordering::Release);
317322
}
318323

319324
fn set_cursor(&self, cursor: Option<cursor_icon::CursorIcon>) {

0 commit comments

Comments
 (0)