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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "common/logging/log.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/kernel/k_shared_memory.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/set/system_settings_server.h"
#include "core/hle/service/sm/sm.h"
#include "hid_core/hid_core.h"
#include "hid_core/hid_util.h"
#include "hid_core/resource_manager.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/debug_pad/debug_pad.h"
#include "hid_core/resources/digitizer/digitizer.h"
#include "hid_core/resources/keyboard/keyboard.h"
#include "hid_core/resources/mouse/debug_mouse.h"
#include "hid_core/resources/mouse/mouse.h"
#include "hid_core/resources/npad/npad.h"
#include "hid_core/resources/palma/palma.h"
#include "hid_core/resources/shared_memory_format.h"
#include "hid_core/resources/six_axis/console_six_axis.h"
#include "hid_core/resources/six_axis/seven_six_axis.h"
#include "hid_core/resources/six_axis/six_axis.h"
#include "hid_core/resources/system_buttons/capture_button.h"
#include "hid_core/resources/system_buttons/home_button.h"
#include "hid_core/resources/system_buttons/sleep_button.h"
#include "hid_core/resources/touch_screen/gesture.h"
#include "hid_core/resources/touch_screen/touch_screen.h"
#include "hid_core/resources/unique_pad/unique_pad.h"
#include "hid_core/resources/vibration/gc_vibration_device.h"
#include "hid_core/resources/vibration/n64_vibration_device.h"
#include "hid_core/resources/vibration/vibration_base.h"
#include "hid_core/resources/vibration/vibration_device.h"
namespace Service::HID {
// Updating period for each HID device.
// Period time is obtained by measuring the number of samples in a second on HW using a homebrew
// Correct npad_update_ns is 4ms this is overclocked to lower input lag
constexpr auto npad_update_ns = std::chrono::nanoseconds{1 * 1000 * 1000}; // (1ms, 1000Hz)
constexpr auto default_update_ns = std::chrono::nanoseconds{4 * 1000 * 1000}; // (4ms, 1000Hz)
constexpr auto mouse_keyboard_update_ns = std::chrono::nanoseconds{8 * 1000 * 1000}; // (8ms, 125Hz)
constexpr auto motion_update_ns = std::chrono::nanoseconds{5 * 1000 * 1000}; // (5ms, 200Hz)
ResourceManager::ResourceManager(Core::System& system_)
: system{system_}, service_context{system_, "hid"} {
applet_resource = std::make_shared<AppletResource>(system);
}
ResourceManager::~ResourceManager() = default;
void ResourceManager::Initialize() {
if (is_initialized) {
return;
}
system.HIDCore().ReloadInputDevices();
handheld_config = std::make_shared<HandheldConfig>();
InitializeHidCommonSampler();
InitializeTouchScreenSampler();
InitializeConsoleSixAxisSampler();
InitializeAHidSampler();
is_initialized = true;
}
std::shared_ptr<AppletResource> ResourceManager::GetAppletResource() const {
return applet_resource;
}
std::shared_ptr<CaptureButton> ResourceManager::GetCaptureButton() const {
return capture_button;
}
std::shared_ptr<ConsoleSixAxis> ResourceManager::GetConsoleSixAxis() const {
return console_six_axis;
}
std::shared_ptr<DebugMouse> ResourceManager::GetDebugMouse() const {
return debug_mouse;
}
std::shared_ptr<DebugPad> ResourceManager::GetDebugPad() const {
return debug_pad;
}
std::shared_ptr<Digitizer> ResourceManager::GetDigitizer() const {
return digitizer;
}
std::shared_ptr<Gesture> ResourceManager::GetGesture() const {
return gesture;
}
std::shared_ptr<HomeButton> ResourceManager::GetHomeButton() const {
return home_button;
}
std::shared_ptr<Keyboard> ResourceManager::GetKeyboard() const {
return keyboard;
}
std::shared_ptr<Mouse> ResourceManager::GetMouse() const {
return mouse;
}
std::shared_ptr<NPad> ResourceManager::GetNpad() const {
return npad;
}
std::shared_ptr<Palma> ResourceManager::GetPalma() const {
return palma;
}
std::shared_ptr<SevenSixAxis> ResourceManager::GetSevenSixAxis() const {
return seven_six_axis;
}
std::shared_ptr<SixAxis> ResourceManager::GetSixAxis() const {
return six_axis;
}
std::shared_ptr<SleepButton> ResourceManager::GetSleepButton() const {
return sleep_button;
}
std::shared_ptr<TouchScreen> ResourceManager::GetTouchScreen() const {
return touch_screen;
}
std::shared_ptr<UniquePad> ResourceManager::GetUniquePad() const {
return unique_pad;
}
Result ResourceManager::CreateAppletResource(u64 aruid) {
if (aruid == SystemAruid) {
const auto result = RegisterCoreAppletResource();
if (result.IsError()) {
return result;
}
return GetNpad()->ActivateNpadResource();
}
const auto result = CreateAppletResourceImpl(aruid);
if (result.IsError()) {
return result;
}
// Homebrew doesn't try to activate some controllers, so we activate them by default
npad->Activate();
six_axis->Activate();
touch_screen->Activate();
return GetNpad()->ActivateNpadResource(aruid);
}
Result ResourceManager::CreateAppletResourceImpl(u64 aruid) {
std::scoped_lock lock{shared_mutex};
return applet_resource->CreateAppletResource(aruid);
}
void ResourceManager::InitializeHidCommonSampler() {
debug_pad = std::make_shared<DebugPad>(system.HIDCore());
mouse = std::make_shared<Mouse>(system.HIDCore());
debug_mouse = std::make_shared<DebugMouse>(system.HIDCore());
keyboard = std::make_shared<Keyboard>(system.HIDCore());
unique_pad = std::make_shared<UniquePad>(system.HIDCore());
npad = std::make_shared<NPad>(system.HIDCore(), service_context);
gesture = std::make_shared<Gesture>(system.HIDCore());
home_button = std::make_shared<HomeButton>(system.HIDCore());
sleep_button = std::make_shared<SleepButton>(system.HIDCore());
capture_button = std::make_shared<CaptureButton>(system.HIDCore());
digitizer = std::make_shared<Digitizer>(system.HIDCore());
palma = std::make_shared<Palma>(system.HIDCore(), service_context);
six_axis = std::make_shared<SixAxis>(system.HIDCore(), npad);
debug_pad->SetAppletResource(applet_resource, &shared_mutex);
digitizer->SetAppletResource(applet_resource, &shared_mutex);
keyboard->SetAppletResource(applet_resource, &shared_mutex);
const auto settings =
system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>("set:sys");
npad->SetNpadExternals(applet_resource, &shared_mutex, handheld_config, settings);
six_axis->SetAppletResource(applet_resource, &shared_mutex);
mouse->SetAppletResource(applet_resource, &shared_mutex);
debug_mouse->SetAppletResource(applet_resource, &shared_mutex);
home_button->SetAppletResource(applet_resource, &shared_mutex);
sleep_button->SetAppletResource(applet_resource, &shared_mutex);
capture_button->SetAppletResource(applet_resource, &shared_mutex);
}
void ResourceManager::InitializeTouchScreenSampler() {
gesture = std::make_shared<Gesture>(system.HIDCore());
touch_screen = std::make_shared<TouchScreen>(system.HIDCore());
touch_screen->SetAppletResource(applet_resource, &shared_mutex);
gesture->SetAppletResource(applet_resource, &shared_mutex);
}
void ResourceManager::InitializeConsoleSixAxisSampler() {
console_six_axis = std::make_shared<ConsoleSixAxis>(system.HIDCore());
seven_six_axis = std::make_shared<SevenSixAxis>(system);
console_six_axis->SetAppletResource(applet_resource, &shared_mutex);
}
void ResourceManager::InitializeAHidSampler() {
// TODO
}
Result ResourceManager::RegisterCoreAppletResource() {
std::scoped_lock lock{shared_mutex};
return applet_resource->RegisterCoreAppletResource();
}
Result ResourceManager::UnregisterCoreAppletResource() {
std::scoped_lock lock{shared_mutex};
return applet_resource->UnregisterCoreAppletResource();
}
Result ResourceManager::RegisterAppletResourceUserId(u64 aruid, bool bool_value) {
std::scoped_lock lock{shared_mutex};
auto result = applet_resource->RegisterAppletResourceUserId(aruid, bool_value);
if (result.IsSuccess()) {
result = npad->RegisterAppletResourceUserId(aruid);
}
return result;
}
void ResourceManager::UnregisterAppletResourceUserId(u64 aruid) {
std::scoped_lock lock{shared_mutex};
applet_resource->UnregisterAppletResourceUserId(aruid);
npad->UnregisterAppletResourceUserId(aruid);
}
Result ResourceManager::GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid) {
std::scoped_lock lock{shared_mutex};
return applet_resource->GetSharedMemoryHandle(out_handle, aruid);
}
void ResourceManager::FreeAppletResourceId(u64 aruid) {
std::scoped_lock lock{shared_mutex};
applet_resource->FreeAppletResourceId(aruid);
}
void ResourceManager::EnableInput(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableInput(aruid, is_enabled);
}
void ResourceManager::EnableSixAxisSensor(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableSixAxisSensor(aruid, is_enabled);
}
void ResourceManager::EnablePadInput(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnablePadInput(aruid, is_enabled);
}
void ResourceManager::EnableTouchScreen(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableTouchScreen(aruid, is_enabled);
}
NpadVibrationBase* ResourceManager::GetVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetVibrationDevice(handle);
}
NpadN64VibrationDevice* ResourceManager::GetN64VibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetN64VibrationDevice(handle);
}
NpadVibrationDevice* ResourceManager::GetNSVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetNSVibrationDevice(handle);
}
NpadGcVibrationDevice* ResourceManager::GetGcVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetGcVibrationDevice(handle);
}
Result ResourceManager::SetAruidValidForVibration(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
const bool has_changed = applet_resource->SetAruidValidForVibration(aruid, is_enabled);
if (has_changed) {
auto devices = npad->GetAllVibrationDevices();
for ([[maybe_unused]] auto* device : devices) {
// TODO
}
}
auto* vibration_handler = npad->GetVibrationHandler();
if (aruid != vibration_handler->GetSessionAruid()) {
vibration_handler->EndPermitVibrationSession();
}
return ResultSuccess;
}
void ResourceManager::SetForceHandheldStyleVibration(bool is_forced) {
handheld_config->is_force_handheld_style_vibration = is_forced;
}
Result ResourceManager::IsVibrationAruidActive(u64 aruid, bool& is_active) const {
std::scoped_lock lock{shared_mutex};
is_active = applet_resource->IsVibrationAruidActive(aruid);
return ResultSuccess;
}
Result ResourceManager::GetVibrationDeviceInfo(Core::HID::VibrationDeviceInfo& device_info,
const Core::HID::VibrationDeviceHandle& handle) {
bool check_device_index = false;
const Result is_valid = IsVibrationHandleValid(handle);
if (is_valid.IsError()) {
return is_valid;
}
switch (handle.npad_type) {
case Core::HID::NpadStyleIndex::Fullkey:
case Core::HID::NpadStyleIndex::Handheld:
case Core::HID::NpadStyleIndex::JoyconDual:
case Core::HID::NpadStyleIndex::JoyconLeft:
case Core::HID::NpadStyleIndex::JoyconRight:
device_info.type = Core::HID::VibrationDeviceType::LinearResonantActuator;
check_device_index = true;
break;
case Core::HID::NpadStyleIndex::GameCube:
device_info.type = Core::HID::VibrationDeviceType::GcErm;
break;
case Core::HID::NpadStyleIndex::N64:
device_info.type = Core::HID::VibrationDeviceType::N64;
break;
default:
device_info.type = Core::HID::VibrationDeviceType::Unknown;
break;
}
device_info.position = Core::HID::VibrationDevicePosition::None;
if (check_device_index) {
switch (handle.device_index) {
case Core::HID::DeviceIndex::Left:
device_info.position = Core::HID::VibrationDevicePosition::Left;
break;
case Core::HID::DeviceIndex::Right:
device_info.position = Core::HID::VibrationDevicePosition::Right;
break;
case Core::HID::DeviceIndex::None:
default:
ASSERT_MSG(false, "DeviceIndex should never be None!");
break;
}
}
return ResultSuccess;
}
Result ResourceManager::SendVibrationValue(u64 aruid,
const Core::HID::VibrationDeviceHandle& handle,
const Core::HID::VibrationValue& value) {
bool has_active_aruid{};
NpadVibrationDevice* device{nullptr};
Result result = IsVibrationAruidActive(aruid, has_active_aruid);
if (result.IsSuccess() && has_active_aruid) {
result = IsVibrationHandleValid(handle);
}
if (result.IsSuccess() && has_active_aruid) {
device = GetNSVibrationDevice(handle);
}
if (device != nullptr) {
// Prevent sending vibrations to an inactive vibration handle
if (!device->IsActive()) {
return ResultSuccess;
}
result = device->SendVibrationValue(value);
}
return result;
}
void ResourceManager::UpdateControllers(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
debug_pad->OnUpdate(core_timing);
digitizer->OnUpdate(core_timing);
unique_pad->OnUpdate(core_timing);
gesture->OnUpdate(core_timing);
touch_screen->OnUpdate(core_timing);
palma->OnUpdate(core_timing);
home_button->OnUpdate(core_timing);
sleep_button->OnUpdate(core_timing);
capture_button->OnUpdate(core_timing);
}
void ResourceManager::UpdateNpad(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
npad->OnUpdate(core_timing);
}
void ResourceManager::UpdateMouseKeyboard(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
mouse->OnUpdate(core_timing);
debug_mouse->OnUpdate(core_timing);
keyboard->OnUpdate(core_timing);
}
void ResourceManager::UpdateMotion(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
six_axis->OnUpdate(core_timing);
seven_six_axis->OnUpdate(core_timing);
console_six_axis->OnUpdate(core_timing);
}
IAppletResource::IAppletResource(Core::System& system_, std::shared_ptr<ResourceManager> resource,
u64 applet_resource_user_id)
: ServiceFramework{system_, "IAppletResource"}, aruid{applet_resource_user_id},
resource_manager{resource} {
static const FunctionInfo functions[] = {
{0, &IAppletResource::GetSharedMemoryHandle, "GetSharedMemoryHandle"},
};
RegisterHandlers(functions);
// Register update callbacks
npad_update_event = Core::Timing::CreateEvent(
"HID::UpdatePadCallback",
[this, resource](
s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
const auto guard = LockService();
resource->UpdateNpad(ns_late);
return std::nullopt;
});
default_update_event = Core::Timing::CreateEvent(
"HID::UpdateDefaultCallback",
[this, resource](
s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
const auto guard = LockService();
resource->UpdateControllers(ns_late);
return std::nullopt;
});
mouse_keyboard_update_event = Core::Timing::CreateEvent(
"HID::UpdateMouseKeyboardCallback",
[this, resource](
s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
const auto guard = LockService();
resource->UpdateMouseKeyboard(ns_late);
return std::nullopt;
});
motion_update_event = Core::Timing::CreateEvent(
"HID::UpdateMotionCallback",
[this, resource](
s64 time, std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
const auto guard = LockService();
resource->UpdateMotion(ns_late);
return std::nullopt;
});
system.CoreTiming().ScheduleLoopingEvent(npad_update_ns, npad_update_ns, npad_update_event);
system.CoreTiming().ScheduleLoopingEvent(default_update_ns, default_update_ns,
default_update_event);
system.CoreTiming().ScheduleLoopingEvent(mouse_keyboard_update_ns, mouse_keyboard_update_ns,
mouse_keyboard_update_event);
system.CoreTiming().ScheduleLoopingEvent(motion_update_ns, motion_update_ns,
motion_update_event);
}
IAppletResource::~IAppletResource() {
system.CoreTiming().UnscheduleEvent(npad_update_event);
system.CoreTiming().UnscheduleEvent(default_update_event);
system.CoreTiming().UnscheduleEvent(mouse_keyboard_update_event);
system.CoreTiming().UnscheduleEvent(motion_update_event);
resource_manager->FreeAppletResourceId(aruid);
}
void IAppletResource::GetSharedMemoryHandle(HLERequestContext& ctx) {
Kernel::KSharedMemory* handle;
const auto result = resource_manager->GetSharedMemoryHandle(&handle, aruid);
LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, result=0x{:X}", aruid, result.raw);
IPC::ResponseBuilder rb{ctx, 2, 1};
rb.Push(result);
rb.PushCopyObjects(handle);
}
} // namespace Service::HID
|