2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-10-06 00:23:21 +10:00
|
|
|
|
|
|
|
#include "core/core_timing.h"
|
2018-11-01 22:01:59 -04:00
|
|
|
#include "core/frontend/emu_window.h"
|
2024-01-04 20:37:43 -06:00
|
|
|
#include "hid_core/frontend/emulated_devices.h"
|
|
|
|
#include "hid_core/hid_core.h"
|
|
|
|
#include "hid_core/resources/applet_resource.h"
|
|
|
|
#include "hid_core/resources/mouse/mouse.h"
|
|
|
|
#include "hid_core/resources/shared_memory_format.h"
|
2018-10-06 00:23:21 +10:00
|
|
|
|
|
|
|
namespace Service::HID {
|
2018-10-06 13:14:42 +10:00
|
|
|
|
2023-12-31 00:42:23 -06:00
|
|
|
Mouse::Mouse(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
|
2021-11-04 19:05:58 -06:00
|
|
|
emulated_devices = hid_core.GetEmulatedDevices();
|
2021-09-20 20:35:27 -05:00
|
|
|
}
|
|
|
|
|
2023-11-17 11:46:26 -06:00
|
|
|
Mouse::~Mouse() = default;
|
2018-10-06 13:14:42 +10:00
|
|
|
|
2023-11-17 11:46:26 -06:00
|
|
|
void Mouse::OnInit() {}
|
|
|
|
void Mouse::OnRelease() {}
|
2018-10-06 13:14:42 +10:00
|
|
|
|
2023-11-17 11:46:26 -06:00
|
|
|
void Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
|
2024-01-01 21:33:07 -06:00
|
|
|
std::scoped_lock shared_lock{*shared_mutex};
|
2023-12-31 00:42:23 -06:00
|
|
|
const u64 aruid = applet_resource->GetActiveAruid();
|
|
|
|
auto* data = applet_resource->GetAruidData(aruid);
|
|
|
|
|
2024-01-01 21:33:07 -06:00
|
|
|
if (data == nullptr || !data->flag.is_assigned) {
|
2023-12-31 00:42:23 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MouseSharedMemoryFormat& shared_memory = data->shared_memory_format->mouse;
|
|
|
|
|
2018-10-06 00:23:21 +10:00
|
|
|
if (!IsControllerActivated()) {
|
2023-12-13 21:39:38 -06:00
|
|
|
shared_memory.mouse_lifo.buffer_count = 0;
|
|
|
|
shared_memory.mouse_lifo.buffer_tail = 0;
|
2018-10-06 00:23:21 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-21 12:41:34 -06:00
|
|
|
next_state = {};
|
|
|
|
|
2023-12-13 21:39:38 -06:00
|
|
|
const auto& last_entry = shared_memory.mouse_lifo.ReadCurrentEntry().state;
|
2021-09-20 20:35:27 -05:00
|
|
|
next_state.sampling_number = last_entry.sampling_number + 1;
|
2021-01-11 00:03:17 -06:00
|
|
|
|
2018-11-01 22:01:59 -04:00
|
|
|
if (Settings::values.mouse_enabled) {
|
2021-09-20 20:35:27 -05:00
|
|
|
const auto& mouse_button_state = emulated_devices->GetMouseButtons();
|
|
|
|
const auto& mouse_position_state = emulated_devices->GetMousePosition();
|
2021-11-14 21:28:38 -06:00
|
|
|
const auto& mouse_wheel_state = emulated_devices->GetMouseWheel();
|
2021-09-20 20:35:27 -05:00
|
|
|
next_state.attribute.is_connected.Assign(1);
|
2021-11-14 14:09:29 -06:00
|
|
|
next_state.x = static_cast<s32>(mouse_position_state.x * Layout::ScreenUndocked::Width);
|
|
|
|
next_state.y = static_cast<s32>(mouse_position_state.y * Layout::ScreenUndocked::Height);
|
2021-09-20 20:35:27 -05:00
|
|
|
next_state.delta_x = next_state.x - last_entry.x;
|
|
|
|
next_state.delta_y = next_state.y - last_entry.y;
|
2021-11-14 21:28:38 -06:00
|
|
|
next_state.delta_wheel_x = mouse_wheel_state.x - last_mouse_wheel_state.x;
|
|
|
|
next_state.delta_wheel_y = mouse_wheel_state.y - last_mouse_wheel_state.y;
|
2021-09-20 20:35:27 -05:00
|
|
|
|
2021-11-14 21:28:38 -06:00
|
|
|
last_mouse_wheel_state = mouse_wheel_state;
|
2021-09-20 20:35:27 -05:00
|
|
|
next_state.button = mouse_button_state;
|
2018-11-01 22:01:59 -04:00
|
|
|
}
|
2018-10-06 00:23:21 +10:00
|
|
|
|
2023-12-13 21:39:38 -06:00
|
|
|
shared_memory.mouse_lifo.WriteNextEntry(next_state);
|
2018-10-06 00:23:21 +10:00
|
|
|
}
|
2018-10-06 13:14:42 +10:00
|
|
|
|
|
|
|
} // namespace Service::HID
|