1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#[cfg(feature = "snapshot")]
use serde::{Serialize, Deserialize};
use super::{MouseInterface, MouseDevice, MouseMovement, MouseButtons};
const RIGHT_BTN_MASK: u8 = 0b0000_0001;
const LEFT_BTN_MASK: u8 = 0b0000_0010;
const MIDDLE_BTN_MASK: u8 = 0b0000_0100; const UNUSED_BTN_MASK: u8 = !(MIDDLE_BTN_MASK|LEFT_BTN_MASK|RIGHT_BTN_MASK);
const PORT_BTN_MASK: u16 = 0b0000_0001_0000_0000;
const PORT_BTN_BITS: u16 = 0b0000_0000_0000_0000;
const PORT_POS_MASK: u16 = 0b0000_0101_0000_0000;
const PORT_X_BITS: u16 = 0b0000_0001_0000_0000;
const PORT_Y_BITS: u16 = 0b0000_0101_0000_0000;
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "snapshot", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "snapshot", serde(rename_all = "camelCase"))]
pub struct KempstonMouseDevice {
#[cfg_attr(feature = "snapshot", serde(skip, default = "u8::max_value"))]
data_btn: u8,
data_x: u8,
data_y: u8,
#[cfg_attr(feature = "snapshot", serde(skip))]
buttons: MouseButtons,
}
impl Default for KempstonMouseDevice {
fn default() -> Self {
KempstonMouseDevice {
data_btn: !0,
data_x: !0,
data_y: !0,
buttons: Default::default(),
}
}
}
impl MouseDevice for KempstonMouseDevice {
#[inline]
fn port_read(&self, port: u16) -> u8 {
if port & PORT_BTN_MASK == PORT_BTN_BITS {
self.data_btn
}
else {
match port & PORT_POS_MASK {
PORT_X_BITS => self.data_x,
PORT_Y_BITS => self.data_y,
_ => unsafe { core::hint::unreachable_unchecked() }
}
}
}
}
impl MouseInterface for KempstonMouseDevice {
#[inline]
fn set_buttons(&mut self, buttons: MouseButtons) {
self.buttons = buttons;
self.data_btn = (self.data_btn & UNUSED_BTN_MASK) |
if buttons.intersects(MouseButtons::RIGHT) { 0 } else { RIGHT_BTN_MASK } |
if buttons.intersects(MouseButtons::LEFT) { 0 } else { LEFT_BTN_MASK } |
if buttons.intersects(MouseButtons::MIDDLE) { 0 } else { MIDDLE_BTN_MASK };
}
#[inline]
fn get_buttons(&self) -> MouseButtons {
self.buttons
}
#[inline]
fn move_mouse(&mut self, movement: MouseMovement) {
self.data_x = clamped_move(self.data_x, movement.horizontal);
self.data_y = clamped_move(self.data_y, -movement.vertical);
}
}
#[inline(always)]
fn clamped_move(prev: u8, mut delta: i16) -> u8 {
delta >>= 1;
if delta < i8::min_value().into() {
delta = i8::min_value().into();
}
else if delta > i8::max_value().into() {
delta = i8::max_value().into();
}
prev.wrapping_add(delta as u8)
}