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
use crate::result::*;
use crate::service::applet;
use crate::service::hid;
use crate::service::hid::IAppletResource;
use crate::service::hid::IHidServer;
use crate::ipc::sf;
use crate::svc;
use crate::mem;
use crate::vmem;
use crate::service;
use core::mem as cmem;

pub mod rc;

bit_enum! {
    Key (u64) {
        A = bit!(0),
        B = bit!(1),
        X = bit!(2),
        Y = bit!(3),
        LStick = bit!(4),
        RStick = bit!(5),
        L = bit!(6),
        R = bit!(7),
        ZL = bit!(8),
        ZR = bit!(9),
        Plus = bit!(10),
        Minus = bit!(11),
        Left = bit!(12),
        Right = bit!(13),
        Up = bit!(14),
        Down = bit!(15),
        LStickLeft = bit!(16),
        LStickUp = bit!(17),
        LStickRight = bit!(18),
        LStickDown = bit!(19),
        RStickLeft = bit!(20),
        RStickUp = bit!(21),
        RStickRight = bit!(22),
        RStickDown = bit!(23),
        SLLeft = bit!(24),
        SRLeft = bit!(25),
        SLRight = bit!(26),
        SRRight = bit!(27),
        Touch = bit!(28)
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct TouchData {
    pub timestamp: u64,
    pub pad: u32,
    pub index: u32,
    pub x: u32,
    pub y: u32,
    pub diameter_x: u32,
    pub diameter_y: u32,
    pub angle: u32,
    pub pad_2: u32
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct TouchEntry {
    pub timestamp: u64,
    pub count: u64,
    pub touches: [TouchData; 16],
    pub pad: u64
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct TouchState {
    pub timestamp_ticks: u64,
    pub entry_count: u64,
    pub latest_index: u64,
    pub max_index: u64,
    pub timestamp: u64,
    pub entries: [TouchEntry; 17]
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct JoystickPosition {
    pub x: u32,
    pub y: u32
}

bit_enum! {
    ConnectionState (u64) {
        None = 0,
        Connected = bit!(0),
        Wired = bit!(1)
    }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct ControllerStateEntry {
    pub timestamp: u64,
    pub timestamp_2: u64,
    pub button_state: u64,
    pub left_position: JoystickPosition,
    pub right_position: JoystickPosition,
    pub connection_state: ConnectionState
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct ControllerState {
    pub timestamp: u64,
    pub entry_count: u64,
    pub latest_index: u64,
    pub max_index: u64,
    pub entries: [ControllerStateEntry; 17]
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct ControllerMacAddress {
    pub address: [u8; 0x10]
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct ControllerColor {
    pub body: u32,
    pub buttons: u32
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct ControllerData {
    pub status: u32,
    pub is_joycon_half: bool,
    pub pad: [u8; 3],
    pub color_descriptor_single: u32,
    pub color_single: ControllerColor,
    pub color_descriptor_split: u32,
    pub color_right: ControllerColor,
    pub color_left: ControllerColor,
    pub pro_controller_state: ControllerState,
    pub handheld_state: ControllerState,
    pub joined_state: ControllerState,
    pub left_state: ControllerState,
    pub right_state: ControllerState,
    pub main_no_analog_state: ControllerState,
    pub main_state: ControllerState,
    pub unk: [u8; 0x2A78],
    pub mac_addresses: [ControllerMacAddress; 2],
    pub unk_2: [u8; 0xE10]
}

#[derive(Copy, Clone)]
#[repr(C)]
pub struct SharedMemoryData {
    pub header: [u8; 0x400],
    pub touch_state: TouchState,
    pub pad: [u8; 0x3C0],
    pub mouse: [u8; 0x400],
    pub keyboard: [u8; 0x400],
    pub unk: [u8; 0x400],
    pub unk_2: [u8; 0x400],
    pub unk_3: [u8; 0x400],
    pub unk_4: [u8; 0x400],
    pub unk_5: [u8; 0x200],
    pub unk_6: [u8; 0x200],
    pub unk_7: [u8; 0x200],
    pub unk_8: [u8; 0x800],
    pub controller_serials: [u8; 0x4000],
    pub controllers: [ControllerData; 10],
    pub unk_9: [u8; 0x4600]
}

pub struct Player {
    controller: hid::ControllerId,
    data: *const ControllerData,
    prev_button_state: u64
}

impl Player {
    pub fn new(controller: hid::ControllerId, data: *const ControllerData) -> Self {
        Self { controller, data, prev_button_state: 0 }
    }

    fn get_latest_state_entry(&self) -> *const ControllerStateEntry {
        unsafe {
            &(*self.data).main_state.entries[(*self.data).main_state.latest_index as usize]
        }
    }

    fn get_button_state(&self) -> u64 {
        let last_entry = self.get_latest_state_entry();
        unsafe {
            (*last_entry).button_state
        }
    }

    pub fn get_button_state_held(&mut self) -> Key {
        let button_state = self.get_button_state();
        self.prev_button_state = button_state;
        Key(button_state)
    }

    pub fn get_button_state_down(&mut self) -> Key {
        let button_state = self.get_button_state();
        let down_state = (!self.prev_button_state) & button_state;
        self.prev_button_state = button_state;
        Key(down_state)
    }

    pub fn get_button_state_up(&mut self) -> Key {
        let button_state = self.get_button_state();
        let up_state = self.prev_button_state & (!button_state);
        self.prev_button_state = button_state;
        Key(up_state)
    }

    pub fn get_controller(&self) -> hid::ControllerId {
        self.controller
    }
}

#[allow(dead_code)]
pub struct InputContext {
    hid_service: mem::Shared<dyn IHidServer>,
    applet_resource: mem::Shared<dyn IAppletResource>,
    shared_mem_handle: svc::Handle,
    aruid: applet::AppletResourceUserId,
    shared_mem_data: *const SharedMemoryData
}

macro_rules! set_all_controllers_mode_dual_impl {
    (? $srv:expr, $process_id:expr, $( $id:expr ),*) => {
        $( $srv.get().set_npad_joy_assignment_mode_dual($process_id, $id)?; )*
    };
    ($srv:expr, $process_id:expr, $( $id:expr ),*) => {
        $( let _ = $srv.get().set_npad_joy_assignment_mode_dual($process_id, $id); )*
    };
}

#[allow(unreachable_patterns)]
fn get_index_for_controller(controller: hid::ControllerId) -> Result<usize> {
    match controller {
        hid::ControllerId::Player1 | hid::ControllerId::Player2 | hid::ControllerId::Player3 | hid::ControllerId::Player4 | hid::ControllerId::Player5 | hid::ControllerId::Player6 | hid::ControllerId::Player7 | hid::ControllerId::Player8 => Ok(controller as usize),
        hid::ControllerId::Handheld => Ok(8),
        _ => rc::ResultInvalidControllerId::make_err()
    }
}

impl InputContext {
    pub fn new(aruid: applet::AppletResourceUserId, supported_tags: hid::NpadStyleTag, controllers: &[hid::ControllerId]) -> Result<Self> {
        let hid_srv = service::new_service_object::<hid::HidServer>()?;
        let applet_res = hid_srv.get().create_applet_resource(sf::ProcessId::from(aruid))?;
        let shmem_handle = applet_res.get().get_shared_memory_handle()?;
        let shmem_size = cmem::size_of::<SharedMemoryData>();
        let shmem_address = vmem::allocate(shmem_size)?;
        svc::map_shared_memory(shmem_handle.handle, shmem_address, shmem_size, svc::MemoryPermission::Read())?;
        hid_srv.get().activate_npad(sf::ProcessId::from(aruid))?;
        hid_srv.get().set_supported_npad_style_set(sf::ProcessId::from(aruid), supported_tags)?;
        hid_srv.get().set_supported_npad_id_type(sf::ProcessId::from(aruid), sf::Buffer::from_array(controllers))?;
        hid_srv.get().activate_npad(sf::ProcessId::from(aruid))?;
        set_all_controllers_mode_dual_impl!(? hid_srv, sf::ProcessId::from(aruid), hid::ControllerId::Player1, hid::ControllerId::Player2, hid::ControllerId::Player3, hid::ControllerId::Player4, hid::ControllerId::Player5, hid::ControllerId::Player6, hid::ControllerId::Player7, hid::ControllerId::Player8, hid::ControllerId::Handheld);
        Ok(Self { hid_service: hid_srv, applet_resource: applet_res, shared_mem_handle: shmem_handle.handle, aruid, shared_mem_data: shmem_address as *const SharedMemoryData })
    }

    pub fn is_controller_connected(&mut self, controller: hid::ControllerId) -> bool {
        if let Ok(index) = get_index_for_controller(controller) {
            let controller_data = unsafe { &(*self.shared_mem_data).controllers[index] };
            let last_entry = controller_data.main_state.entries[controller_data.main_state.latest_index as usize];
            last_entry.connection_state.contains(ConnectionState::Connected())
        }
        else {
            false
        }
    }

    pub fn get_player(&mut self, controller: hid::ControllerId) -> Result<Player> {
        let index = get_index_for_controller(controller)?;
        let controller_data: *const ControllerData = unsafe { &(*self.shared_mem_data).controllers[index] };
        Ok(Player::new(controller, controller_data))
    }

    pub fn get_touch_data(&mut self, touch_index: u32) -> Result<TouchData> {
        unsafe {
            let touch_entry: *const TouchEntry = &(*self.shared_mem_data).touch_state.entries[(*self.shared_mem_data).touch_state.latest_index as usize];
            result_return_unless!((touch_index as u64) < (*touch_entry).count, rc::ResultInvalidTouchIndex);
            Ok((*touch_entry).touches[touch_index as usize])
        }
    }
}

impl Drop for InputContext {
    fn drop(&mut self) {
        set_all_controllers_mode_dual_impl!(self.hid_service, sf::ProcessId::from(self.aruid), hid::ControllerId::Player1, hid::ControllerId::Player2, hid::ControllerId::Player3, hid::ControllerId::Player4, hid::ControllerId::Player5, hid::ControllerId::Player6, hid::ControllerId::Player7, hid::ControllerId::Player8, hid::ControllerId::Handheld);
        let _ = self.hid_service.get().deactivate_npad(sf::ProcessId::from(self.aruid));
        let _ = svc::unmap_shared_memory(self.shared_mem_handle, self.shared_mem_data as *mut u8, cmem::size_of::<SharedMemoryData>());
        let _ = svc::close_handle(self.shared_mem_handle);
    }
}