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
/*
    Copyright (C) 2020-2022  Rafal Michalski

    This file is part of SPECTRUSTY, a Rust library for building emulators.

    For the full copyright notice, see the lib.rs file.
*/
//! A joystick communication interface and emulators of various joysticks.
use core::fmt::Debug;

pub mod cursor;
pub mod fuller;
pub mod kempston;
pub mod sinclair;

bitflags! {
    /// Flags for reading and writing the current stick direction.
    /// * Bit = 1 a direction is active.
    /// * Bit = 0 a direction is inactive.
    #[derive(Default)]
    pub struct Directions: u8 {
        const UP    = 0b0000_0001;
        const RIGHT = 0b0000_0010;
        const DOWN  = 0b0000_0100;
        const LEFT  = 0b0000_1000;
    }
}

/// An enum for specifying one of 8 possible joystick's directional positions and a center (neutral) state.
pub enum JoyDirection {
    Center,
    Up,
    UpRight,
    Right,
    DownRight,
    Down,
    DownLeft,
    Left,
    UpLeft
}

/// An interface for providing user input data for a [JoystickDevice] implementation.
pub trait JoystickInterface {
    /// Press or release a "fire" button. `btn` is the button number for cases when the joystick have more
    /// than one button.
    ///
    /// Currently, `btn` is not being used by any of the implemented devices.
    fn fire(&mut self, btn: u8, pressed: bool);
    /// Returns `true` if an indicated "fire" button is being pressed, otherwise returns `false`.
    fn get_fire(&self, btn: u8) -> bool;
    /// Changes the stick direction using provided flags.
    fn set_directions(&mut self, dir: Directions);
    /// Returns the current stick direction.
    fn get_directions(&self) -> Directions;
    /// Changes the stick direction using an enum.
    #[inline]
    fn direction(&mut self, dir: JoyDirection) {
        self.set_directions(match dir {
            JoyDirection::Center => Directions::empty(),
            JoyDirection::Up => Directions::UP,
            JoyDirection::UpRight => Directions::UP|Directions::RIGHT,
            JoyDirection::Right => Directions::RIGHT,
            JoyDirection::DownRight => Directions::DOWN|Directions::RIGHT,
            JoyDirection::Down => Directions::DOWN,
            JoyDirection::DownLeft => Directions::DOWN|Directions::LEFT,
            JoyDirection::Left => Directions::LEFT,
            JoyDirection::UpLeft => Directions::UP|Directions::LEFT,
        })
    }
    /// Resets a joystick to a central (neutral) position.
    #[inline]
    fn center(&mut self) {
        self.set_directions(Directions::empty());
    }
    /// Returns `true` if a joystick is in the up (forward) position.
    #[inline]
    fn is_up(&self) -> bool {
        self.get_directions().intersects(Directions::UP)
    }
    /// Returns `true` if a joystick is in the right position.
    #[inline]
    fn is_right(&self) -> bool {
        self.get_directions().intersects(Directions::RIGHT)
    }
    /// Returns `true` if a joystick is in the left position.
    #[inline]
    fn is_left(&self) -> bool {
        self.get_directions().intersects(Directions::LEFT)
    }
    /// Returns `true` if a joystick is in the down (backward) position.
    #[inline]
    fn is_down(&self) -> bool {
        self.get_directions().intersects(Directions::DOWN)
    }
    /// Returns `true` if a joystick is in the center (neutral) position.
    #[inline]
    fn is_center(&self) -> bool {
        self.get_directions().intersects(Directions::empty())
    }
}

/// A joystick device interface for the joystick [bus][crate::bus::joystick] device implementations.
pub trait JoystickDevice: Debug {
    /// Should return the joystick state.
    fn port_read(&self, port: u16) -> u8;
    /// Writes data to a joystick device.
    ///
    /// If a device does not support writes, this method should return `false`.
    /// The default implementation does exactly just that.
    fn port_write(&mut self, _port: u16, _data: u8) -> bool { false }
}

/// The joystick device that can be used as a placeholder type.
#[derive(Clone, Copy, Default, Debug)]
pub struct NullJoystickDevice;

impl JoystickDevice for NullJoystickDevice {
    fn port_read(&self, _port: u16) -> u8 {
        u8::max_value()
    }
}

impl JoystickInterface for NullJoystickDevice {
    fn fire(&mut self, _btn: u8, _pressed: bool) {}
    fn get_fire(&self, _btn: u8) -> bool { false }
    fn set_directions(&mut self, _dir: Directions) {}
    fn get_directions(&self) -> Directions {
        Directions::empty()
    }
}