core: move scaling of input positions into core

The core may need to control the scale of the input positions to layout
positions, such as when the layout is changed. Handle the scaling of
input positions in the button of the keyboard.
This commit is contained in:
Richard Acayan 2024-09-30 22:08:53 -04:00
parent 0fa318ca7e
commit 0b985cefdb
2 changed files with 22 additions and 16 deletions

View file

@ -132,6 +132,9 @@ pub struct Button<D: Display, K: Keyboard> {
longpress: Duration, longpress: Duration,
repeat: Duration, repeat: Duration,
r_square: f64, r_square: f64,
x_scale: f64,
y_scale: f64,
} }
const PART_TL: usize = 1; const PART_TL: usize = 1;
@ -185,6 +188,9 @@ impl<D: Display, K: Keyboard> Button<D, K> {
longpress: Duration::from_millis(cfg.longpress_ms()), longpress: Duration::from_millis(cfg.longpress_ms()),
repeat: Duration::from_millis(cfg.repeat_ms()), repeat: Duration::from_millis(cfg.repeat_ms()),
r_square: 0.25, r_square: 0.25,
x_scale: 0.0,
y_scale: 0.0,
} }
} }
@ -224,6 +230,12 @@ impl<D: Display, K: Keyboard> Button<D, K> {
self.layout.update_keys_supported(&self.kbd) self.layout.update_keys_supported(&self.kbd)
} }
pub fn set_scale(&mut self, x_scale: f64, y_scale: f64)
{
self.x_scale = x_scale;
self.y_scale = y_scale;
}
pub fn next_time(&self) -> Option<Instant> pub fn next_time(&self) -> Option<Instant>
{ {
let id = *self.timers.front()?; let id = *self.timers.front()?;
@ -326,6 +338,8 @@ impl<D: Display, K: Keyboard> Button<D, K> {
return; return;
} }
let (x, y) = (x * self.x_scale, y * self.y_scale);
let key = match self.layout.locate_key_mut(x, y) { let key = match self.layout.locate_key_mut(x, y) {
Some(k) => k, Some(k) => k,
// Ignore the press if it is not for a key. // Ignore the press if it is not for a key.
@ -428,6 +442,8 @@ impl<D: Display, K: Keyboard> Button<D, K> {
None => return, None => return,
}; };
let (x, y) = (x * self.x_scale, y * self.y_scale);
let (dx, dy) = (x - press.x1, y - press.y1); let (dx, dy) = (x - press.x1, y - press.y1);
if dx * dx + dy * dy < self.r_square { if dx * dx + dy * dy < self.r_square {
// We only need to make changes when the key is being dragged. // We only need to make changes when the key is being dragged.

View file

@ -38,9 +38,6 @@ pub struct Seat<D: Display, K: Keyboard, T> {
button: Button<D, K>, button: Button<D, K>,
x_scale: f64,
y_scale: f64,
mouse_x: f64, mouse_x: f64,
mouse_y: f64, mouse_y: f64,
actions: Vec<TouchAction>, actions: Vec<TouchAction>,
@ -66,9 +63,6 @@ impl<D: Display, K: Keyboard,
button, button,
x_scale: 0.0,
y_scale: 0.0,
mouse_x: 0.0, mouse_x: 0.0,
mouse_y: 0.0, mouse_y: 0.0,
actions, actions,
@ -117,6 +111,12 @@ impl<D: Display, K: Keyboard,
self.button.set_text_supported(text_supp) self.button.set_text_supported(text_supp)
} }
#[inline(always)]
pub fn set_scale(&mut self, x_scale: f64, y_scale: f64)
{
self.button.set_scale(x_scale, y_scale);
}
pub fn set_capabilities(&mut self, caps: wl_seat::Capability) pub fn set_capabilities(&mut self, caps: wl_seat::Capability)
{ {
if caps.contains(wl_seat::Capability::Pointer) { if caps.contains(wl_seat::Capability::Pointer) {
@ -128,16 +128,8 @@ impl<D: Display, K: Keyboard,
} }
} }
pub fn set_scale(&mut self, x_scale: f64, y_scale: f64)
{
self.x_scale = x_scale;
self.y_scale = y_scale;
}
pub fn ptr_motion(&mut self, x: f64, y: f64) pub fn ptr_motion(&mut self, x: f64, y: f64)
{ {
let (x, y) = (x * self.x_scale, y * self.y_scale);
self.mouse_x = x; self.mouse_x = x;
self.mouse_y = y; self.mouse_y = y;
@ -164,7 +156,6 @@ impl<D: Display, K: Keyboard,
pub fn touch_press(&mut self, id: u32, x: f64, y: f64) pub fn touch_press(&mut self, id: u32, x: f64, y: f64)
{ {
let id = id as usize + 1; let id = id as usize + 1;
let (x, y) = (x * self.x_scale, y * self.y_scale);
let act = TouchAction { id, act: PressAction::Press(x, y), }; let act = TouchAction { id, act: PressAction::Press(x, y), };
self.actions.push(act); self.actions.push(act);
} }
@ -172,7 +163,6 @@ impl<D: Display, K: Keyboard,
pub fn touch_pos(&mut self, id: u32, x: f64, y: f64) pub fn touch_pos(&mut self, id: u32, x: f64, y: f64)
{ {
let id = id as usize + 1; let id = id as usize + 1;
let (x, y) = (x * self.x_scale, y * self.y_scale);
let act = TouchAction { id, act: PressAction::Pos(x, y), }; let act = TouchAction { id, act: PressAction::Pos(x, y), };
self.actions.push(act); self.actions.push(act);
} }