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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! This crate provides basic access to a ps2 mouse in x86 environments.


#![no_std]
#![warn(missing_docs)]
#![feature(const_fn)]
#![feature(const_fn_fn_ptr_basics)]

use bitflags::bitflags;
use x86_64::instructions::port::Port;

const ADDRESS_PORT_ADDRESS: u16 = 0x64;
const DATA_PORT_ADDRESS: u16 = 0x60;
const GET_STATUS_BYTE: u8 = 0x20;
const SET_STATUS_BYTE: u8 = 0x60;

bitflags! {
    /// Represents the flags currently set for the mouse.

    #[derive(Default)]
    pub struct MouseFlags: u8 {
        /// Whether or not the left mouse button is pressed.

        const LEFT_BUTTON = 0b0000_0001;

        /// Whether or not the right mouse button is pressed.

        const RIGHT_BUTTON = 0b0000_0010;

        /// Whether or not the middle mouse button is pressed.

        const MIDDLE_BUTTON = 0b0000_0100;

        /// Whether or not the packet is valid or not.

        const ALWAYS_ONE = 0b0000_1000;

        /// Whether or not the x delta is negative.

        const X_SIGN = 0b0001_0000;

        /// Whether or not the y delta is negative.

        const Y_SIGN = 0b0010_0000;

        /// Whether or not the x delta overflowed.

        const X_OVERFLOW = 0b0100_0000;

        /// Whether or not the y delta overflowed.

        const Y_OVERFLOW = 0b1000_0000;
    }
}

#[repr(u8)]
enum Command {
    EnablePacketStreaming = 0xF4,
    SetDefaults = 0xF6,
}

/// A basic interface to interact with a PS2 mouse.

#[derive(Debug)]
pub struct Mouse {
    command_port: Port<u8>,
    data_port: Port<u8>,
    current_packet: u8,
    current_state: MouseState,
    completed_state: MouseState,
    on_complete: Option<fn(MouseState)>,
}

impl Default for Mouse {
    fn default() -> Mouse {
        Mouse::new()
    }
}

/// A snapshot of the mouse flags, x delta and y delta.

#[derive(Debug, Copy, Clone, Default)]
pub struct MouseState {
    flags: MouseFlags,
    x: i16,
    y: i16,
}

impl MouseState {
    /// Returns a new `MouseState`.

    pub const fn new() -> MouseState {
        MouseState {
            flags: MouseFlags::empty(),
            x: 0,
            y: 0,
        }
    }

    /// Returns true if the left mouse button is currently down.

    pub fn left_button_down(&self) -> bool {
        self.flags.contains(MouseFlags::LEFT_BUTTON)
    }

    /// Returns true if the left mouse button is currently up.

    pub fn left_button_up(&self) -> bool {
        !self.flags.contains(MouseFlags::LEFT_BUTTON)
    }

    /// Returns true if the right mouse button is currently down.

    pub fn right_button_down(&self) -> bool {
        self.flags.contains(MouseFlags::RIGHT_BUTTON)
    }

    /// Returns true if the right mouse button is currently up.

    pub fn right_button_up(&self) -> bool {
        !self.flags.contains(MouseFlags::RIGHT_BUTTON)
    }

    /// Returns true if the x axis has moved.

    pub fn x_moved(&self) -> bool {
        self.x != 0
    }

    /// Returns true if the y axis has moved.

    pub fn y_moved(&self) -> bool {
        self.y != 0
    }

    /// Returns true if the x or y axis has moved.

    pub fn moved(&self) -> bool {
        self.x_moved() || self.y_moved()
    }

    /// Returns the x delta of the mouse state.

    pub fn get_x(&self) -> i16 {
        self.x
    }

    /// Returns the y delta of the mouse state.

    pub fn get_y(&self) -> i16 {
        self.y
    }
}

impl Mouse {
    /// Creates a new `Mouse`.

    pub const fn new() -> Mouse {
        Mouse {
            command_port: Port::new(ADDRESS_PORT_ADDRESS),
            data_port: Port::new(DATA_PORT_ADDRESS),
            current_packet: 0,
            current_state: MouseState::new(),
            completed_state: MouseState::new(),
            on_complete: None,
        }
    }

    /// Returns the last completed state of the mouse.

    pub fn get_state(&self) -> MouseState {
        self.completed_state
    }

    /// Attempts to initialize a `Mouse`. If successful, interrupts will be generated

    /// as `PIC offset + 12`.

    pub fn init(&mut self) -> Result<(), &'static str> {
        self.write_command_port(GET_STATUS_BYTE)?;
        let status = self.read_data_port()? | 0x02;
        self.write_command_port(SET_STATUS_BYTE)?;
        self.write_data_port(status & 0xDF)?;
        self.send_command(Command::SetDefaults)?;
        self.send_command(Command::EnablePacketStreaming)?;
        Ok(())
    }

    /// Attempts to process a packet.

    pub fn process_packet(&mut self, packet: u8) {
        match self.current_packet {
            0 => {
                let flags = MouseFlags::from_bits_truncate(packet);
                if !flags.contains(MouseFlags::ALWAYS_ONE) {
                    return;
                }
                self.current_state.flags = flags;
            }
            1 => self.process_x_movement(packet),
            2 => {
                self.process_y_movement(packet);
                self.completed_state = self.current_state;
                if let Some(on_complete) = self.on_complete {
                    on_complete(self.completed_state);
                }
            }
            _ => unreachable!(),
        }
        self.current_packet = (self.current_packet + 1) % 3;
    }

    /// Sets the `on_complete` function to be called when a packet is completed.

    pub fn set_on_complete(&mut self, handler: fn(MouseState)) {
        self.on_complete = Some(handler);
    }

    fn process_x_movement(&mut self, packet: u8) {
        if !self.current_state.flags.contains(MouseFlags::X_OVERFLOW) {
            self.current_state.x = if self.current_state.flags.contains(MouseFlags::X_SIGN) {
                self.sign_extend(packet)
            } else {
                packet as i16
            };
        }
    }

    fn process_y_movement(&mut self, packet: u8) {
        if !self.current_state.flags.contains(MouseFlags::Y_OVERFLOW) {
            self.current_state.y = if self.current_state.flags.contains(MouseFlags::Y_SIGN) {
                self.sign_extend(packet)
            } else {
                packet as i16
            };
        }
    }

    fn read_data_port(&mut self) -> Result<u8, &'static str> {
        self.wait_for_read()?;
        Ok(unsafe { self.data_port.read() })
    }

    fn send_command(&mut self, command: Command) -> Result<(), &'static str> {
        self.write_command_port(0xD4)?;
        self.write_data_port(command as u8)?;
        if self.read_data_port()? != 0xFA {
            return Err("mouse did not respond to the command");
        }
        Ok(())
    }

    fn sign_extend(&self, packet: u8) -> i16 {
        ((packet as u16) | 0xFF00) as i16
    }

    fn write_command_port(&mut self, value: u8) -> Result<(), &'static str> {
        self.wait_for_write()?;
        unsafe {
            self.command_port.write(value);
        }
        Ok(())
    }

    fn write_data_port(&mut self, value: u8) -> Result<(), &'static str> {
        self.wait_for_write()?;
        unsafe {
            self.data_port.write(value);
        }
        Ok(())
    }

    fn wait_for_read(&mut self) -> Result<(), &'static str> {
        let timeout = 100_000;
        for _ in 0..timeout {
            let value = unsafe { self.command_port.read() };
            if (value & 0x1) == 0x1 {
                return Ok(());
            }
        }
        Err("wait for mouse read timeout")
    }

    fn wait_for_write(&mut self) -> Result<(), &'static str> {
        let timeout = 100_000;
        for _ in 0..timeout {
            let value = unsafe { self.command_port.read() };
            if (value & 0x2) == 0x0 {
                return Ok(());
            }
        }
        Err("wait for mouse write timeout")
    }
}

#[cfg(test)]
mod test {
    use super::*;

    const EMPTY_PACKET: u8 = 0;
    const VALID_PACKET: u8 = MouseFlags::ALWAYS_ONE.bits();
    const NEGATIVE_PACKET: u8 =
        MouseFlags::ALWAYS_ONE.bits() | MouseFlags::X_SIGN.bits() | MouseFlags::Y_SIGN.bits();
    const NEGATIVE_PACKET_WITH_OVERFLOW: u8 = MouseFlags::ALWAYS_ONE.bits()
        | MouseFlags::X_SIGN.bits()
        | MouseFlags::Y_SIGN.bits()
        | MouseFlags::X_OVERFLOW.bits()
        | MouseFlags::Y_OVERFLOW.bits();
    const LEFT_MOUSE_BUTTON_DOWN_PACKET: u8 =
        MouseFlags::ALWAYS_ONE.bits() | MouseFlags::LEFT_BUTTON.bits();
    const RIGHT_MOUSE_BUTTON_DOWN_PACKET: u8 =
        MouseFlags::ALWAYS_ONE.bits() | MouseFlags::RIGHT_BUTTON.bits();
    const POSITIVE_X_PACKET: u8 = 0x5;
    const POSITIVE_Y_PACKET: u8 = 0x8;
    const NEGATIVE_X_PACKET: u8 = 0xD8;
    const NEGATIVE_Y_PACKET: u8 = 0xD9;

    #[test]
    fn process_packets() {
        let mut mouse = Mouse::new();

        mouse.process_packet(VALID_PACKET);
        assert_eq!(mouse.current_packet, 1);

        mouse.process_packet(EMPTY_PACKET);
        assert_eq!(mouse.current_packet, 2);

        mouse.process_packet(EMPTY_PACKET);
        assert_eq!(mouse.current_packet, 0);

        let mouse_state = mouse.completed_state;
        assert_eq!(mouse_state.flags, MouseFlags::ALWAYS_ONE);
        assert_eq!(mouse_state.x, 0);
        assert_eq!(mouse_state.y, 0);
    }

    #[test]
    fn always_one_bit_not_set() {
        let mut mouse = Mouse::new();
        mouse.process_packet(EMPTY_PACKET);
        assert_eq!(mouse.current_packet, 0);
    }

    #[test]
    fn positive_movement() {
        let mut mouse = Mouse::new();
        mouse.process_packet(VALID_PACKET);
        mouse.process_packet(POSITIVE_X_PACKET);
        mouse.process_packet(POSITIVE_Y_PACKET);

        let mouse_state = mouse.completed_state;
        assert_eq!(mouse_state.x, POSITIVE_X_PACKET as i16);
        assert_eq!(mouse_state.y, POSITIVE_Y_PACKET as i16);
    }

    #[test]
    fn negative_movement() {
        let mut mouse = Mouse::new();
        mouse.process_packet(NEGATIVE_PACKET);
        mouse.process_packet(NEGATIVE_X_PACKET);
        mouse.process_packet(NEGATIVE_Y_PACKET);

        let mouse_state = mouse.get_state();
        assert_eq!(mouse_state.x, -40);
        assert_eq!(mouse_state.y, -39);
    }

    #[test]
    fn discard_overflow() {
        let mut mouse = Mouse::new();
        mouse.process_packet(VALID_PACKET);
        mouse.process_packet(POSITIVE_X_PACKET);
        mouse.process_packet(POSITIVE_Y_PACKET);

        mouse.process_packet(NEGATIVE_PACKET_WITH_OVERFLOW);
        mouse.process_packet(NEGATIVE_X_PACKET);
        mouse.process_packet(NEGATIVE_Y_PACKET);

        let mouse_state = mouse.completed_state;
        assert_eq!(mouse_state.x, POSITIVE_X_PACKET as i16);
        assert_eq!(mouse_state.y, POSITIVE_Y_PACKET as i16);
    }

    #[test]
    fn left_mouse_button_down() {
        let mut mouse = Mouse::new();
        mouse.process_packet(LEFT_MOUSE_BUTTON_DOWN_PACKET);
        assert_eq!(mouse.current_state.left_button_down(), true);
    }

    #[test]
    fn left_mouse_button_up() {
        let mut mouse = Mouse::new();
        mouse.process_packet(VALID_PACKET);
        assert_eq!(mouse.current_state.left_button_up(), true);
    }

    #[test]
    fn right_mouse_button_down() {
        let mut mouse = Mouse::new();
        mouse.process_packet(RIGHT_MOUSE_BUTTON_DOWN_PACKET);
        assert_eq!(mouse.current_state.right_button_down(), true);
    }

    #[test]
    fn right_mouse_button_up() {
        let mut mouse = Mouse::new();
        mouse.process_packet(VALID_PACKET);
        assert_eq!(mouse.current_state.right_button_up(), true);
    }
}