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