From 524cd26649ecbaef3b542fb7ad537de24ac6883a Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Sun, 16 Feb 2020 19:42:32 +0000 Subject: [PATCH] Refactor HID services Use the new service naming convention. Tidy up some unused includes. Move constants to service namespace. --- app/CMakeLists.txt | 3 +- .../main/cpp/skyline/services/base_service.h | 4 +- .../skyline/services/hid/IAppletResource.cpp | 14 ++++ .../skyline/services/hid/IAppletResource.h | 26 +++++++ .../cpp/skyline/services/hid/IHidServer.cpp | 57 +++++++++++++++ .../services/hid/{hid.h => IHidServer.h} | 35 +++------- app/src/main/cpp/skyline/services/hid/hid.cpp | 69 ------------------- .../main/cpp/skyline/services/serviceman.cpp | 6 +- 8 files changed, 112 insertions(+), 102 deletions(-) create mode 100644 app/src/main/cpp/skyline/services/hid/IAppletResource.cpp create mode 100644 app/src/main/cpp/skyline/services/hid/IAppletResource.h create mode 100644 app/src/main/cpp/skyline/services/hid/IHidServer.cpp rename app/src/main/cpp/skyline/services/hid/{hid.h => IHidServer.h} (78%) delete mode 100644 app/src/main/cpp/skyline/services/hid/hid.cpp diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 9cedf61b..cedd9eea 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -61,7 +61,8 @@ add_library(skyline SHARED ${source_DIR}/skyline/services/apm/apm.cpp ${source_DIR}/skyline/services/am/applet.cpp ${source_DIR}/skyline/services/am/appletController.cpp - ${source_DIR}/skyline/services/hid/hid.cpp + ${source_DIR}/skyline/services/hid/IHidServer.cpp + ${source_DIR}/skyline/services/hid/IAppletResource.cpp ${source_DIR}/skyline/services/timesrv/IStaticService.cpp ${source_DIR}/skyline/services/timesrv/ISystemClock.cpp ${source_DIR}/skyline/services/timesrv/ITimeZoneService.cpp diff --git a/app/src/main/cpp/skyline/services/base_service.h b/app/src/main/cpp/skyline/services/base_service.h index 2ad173e8..49326dc9 100644 --- a/app/src/main/cpp/skyline/services/base_service.h +++ b/app/src/main/cpp/skyline/services/base_service.h @@ -42,7 +42,7 @@ namespace skyline::service { audout_IAudioOut, IAudioRendererManager, IAudioRenderer, - hid, + hid_IHidServer, hid_IAppletResource, timesrv_IStaticService, timesrv_ISystemClock, @@ -68,7 +68,7 @@ namespace skyline::service { {"appletAE", Service::am_appletAE}, {"audout:u", Service::audout_u}, {"audren:u", Service::IAudioRendererManager}, - {"hid", Service::hid}, + {"hid", Service::hid_IHidServer}, {"time:s", Service::timesrv_IStaticService}, {"time:a", Service::timesrv_IStaticService}, {"time:u", Service::timesrv_IStaticService}, diff --git a/app/src/main/cpp/skyline/services/hid/IAppletResource.cpp b/app/src/main/cpp/skyline/services/hid/IAppletResource.cpp new file mode 100644 index 00000000..6a167d3f --- /dev/null +++ b/app/src/main/cpp/skyline/services/hid/IAppletResource.cpp @@ -0,0 +1,14 @@ +#include "IAppletResource.h" + +namespace skyline::service::hid { + IAppletResource::IAppletResource(const DeviceState &state, ServiceManager &manager) : BaseService(state, manager, Service::hid_IAppletResource, "hid:IAppletResource", { + {0x0, SFUNC(IAppletResource::GetSharedMemoryHandle)} + }) {} + + void IAppletResource::GetSharedMemoryHandle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + hidSharedMemory = std::make_shared(state, NULL, constant::hidSharedMemSize, memory::Permission{true, false, false}); + auto handle = state.process->InsertItem(hidSharedMemory); + state.logger->Debug("HID Shared Memory Handle: 0x{:X}", handle); + response.copyHandles.push_back(handle); + } +} diff --git a/app/src/main/cpp/skyline/services/hid/IAppletResource.h b/app/src/main/cpp/skyline/services/hid/IAppletResource.h new file mode 100644 index 00000000..6e017bac --- /dev/null +++ b/app/src/main/cpp/skyline/services/hid/IAppletResource.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +namespace skyline::service::hid { + namespace constant { + constexpr size_t hidSharedMemSize = 0x40000; //!< The size of HID Shared Memory (https://switchbrew.org/wiki/HID_Shared_Memory) + } + + /** + * @brief IAppletResource is used to get a handle to the HID shared memory (https://switchbrew.org/wiki/HID_services#IAppletResource) + */ + class IAppletResource : public BaseService { + public: + std::shared_ptr hidSharedMemory; //!< A pointer to HID shared memory + + IAppletResource(const DeviceState &state, ServiceManager &manager); + + /** + * @brief This opens a handle to HID shared memory (https://switchbrew.org/wiki/HID_services#GetSharedMemoryHandle) + */ + void GetSharedMemoryHandle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response); + }; +} diff --git a/app/src/main/cpp/skyline/services/hid/IHidServer.cpp b/app/src/main/cpp/skyline/services/hid/IHidServer.cpp new file mode 100644 index 00000000..9240cc55 --- /dev/null +++ b/app/src/main/cpp/skyline/services/hid/IHidServer.cpp @@ -0,0 +1,57 @@ +#include "IHidServer.h" + +namespace skyline::service::hid { + IHidServer::IHidServer(const DeviceState &state, ServiceManager &manager) : BaseService(state, manager, Service::hid_IHidServer, "hid:IHidServer", { + {0x0, SFUNC(IHidServer::CreateAppletResource)}, + {0x64, SFUNC(IHidServer::SetSupportedNpadStyleSet)}, + {0x66, SFUNC(IHidServer::SetSupportedNpadIdType)}, + {0x67, SFUNC(IHidServer::ActivateNpad)}, + {0x78, SFUNC(IHidServer::SetNpadJoyHoldType)}, + {0x7A, SFUNC(IHidServer::SetNpadJoyAssignmentModeSingleByDefault)}, + {0x7B, SFUNC(IHidServer::SetNpadJoyAssignmentModeSingle)}, + {0x7C, SFUNC(IHidServer::SetNpadJoyAssignmentModeDual)} + }) {} + + void IHidServer::CreateAppletResource(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + resource = std::make_shared(state, manager); + manager.RegisterService(resource, session, response); + } + + void IHidServer::SetSupportedNpadStyleSet(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + auto styleSet = request.Pop(); + state.logger->Debug("Controller Support:\nPro-Controller: {}\nJoy-Con: Handheld: {}, Dual: {}, L: {}, R: {}\nGameCube: {}\nPokeBall: {}\nNES: {}, NES Handheld: {}, SNES: {}", static_cast(styleSet.proController), static_cast(styleSet.joyconHandheld), static_cast(styleSet.joyconDual), static_cast(styleSet.joyconLeft), static_cast + (styleSet.joyconRight), static_cast(styleSet.gamecube), static_cast(styleSet.pokeball), static_cast(styleSet.nes), static_cast(styleSet.nesHandheld), static_cast(styleSet.snes)); + } + + void IHidServer::SetSupportedNpadIdType(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + const auto &buffer = request.inputBuf.at(0); + size_t numId = buffer.size / sizeof(NpadId); + u64 address = buffer.address; + for (size_t i = 0; i < numId; i++) { + auto id = state.process->GetObject(address); + deviceMap[id] = JoyConDevice(id); + address += sizeof(NpadId); + } + } + + void IHidServer::ActivateNpad(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {} + + void IHidServer::SetNpadJoyHoldType(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + deviceMap[request.Pop()].assignment = JoyConAssignment::Single; + } + + void IHidServer::SetNpadJoyAssignmentModeSingleByDefault(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + orientation = request.Pop(); + } + + void IHidServer::SetNpadJoyAssignmentModeSingle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + auto controllerId = request.Pop(); + auto appletUserId = request.Pop(); + deviceMap[controllerId].assignment = JoyConAssignment::Single; + deviceMap[controllerId].side = request.Pop(); + } + + void IHidServer::SetNpadJoyAssignmentModeDual(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { + deviceMap[request.Pop()].assignment = JoyConAssignment::Dual; + } +} diff --git a/app/src/main/cpp/skyline/services/hid/hid.h b/app/src/main/cpp/skyline/services/hid/IHidServer.h similarity index 78% rename from app/src/main/cpp/skyline/services/hid/hid.h rename to app/src/main/cpp/skyline/services/hid/IHidServer.h index 75ac6553..c29d220c 100644 --- a/app/src/main/cpp/skyline/services/hid/hid.h +++ b/app/src/main/cpp/skyline/services/hid/IHidServer.h @@ -2,32 +2,13 @@ #include #include -#include - -namespace skyline::constant { - constexpr size_t hidSharedMemSize = 0x40000; //!< The size of HID Shared Memory (https://switchbrew.org/wiki/HID_Shared_Memory) -} +#include "IAppletResource.h" namespace skyline::service::hid { /** - * @brief IAppletResource is used to get the handle to the HID shared memory (https://switchbrew.org/wiki/HID_services#IAppletResource) + * @brief IHidServer or hid service is used to access input devices (https://switchbrew.org/wiki/HID_services#hid) */ - class IAppletResource : public BaseService { - public: - IAppletResource(const DeviceState &state, ServiceManager &manager); - - std::shared_ptr hidSharedMemory; - - /** - * @brief This opens a handle to HID shared memory (https://switchbrew.org/wiki/HID_services#GetSharedMemoryHandle) - */ - void GetSharedMemoryHandle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response); - }; - - /** - * @brief hid or Human Interface Device service is used to access input devices (https://switchbrew.org/wiki/HID_services#hid) - */ - class hid : public BaseService { + class IHidServer : public BaseService { private: /** * @brief This holds the controller styles supported by an application @@ -43,7 +24,7 @@ namespace skyline::service::hid { bool nes : 1; //!< NES controller bool nesHandheld : 1; //!< NES controller in handheld mode bool snes : 1; //!< SNES controller - u32 : 22; + u32 _pad0_ : 22; }; static_assert(sizeof(StyleSet) == 4); @@ -105,13 +86,13 @@ namespace skyline::service::hid { JoyConDevice(const NpadId &id) : id(id) {} }; - std::shared_ptr resource{}; //!< A shared pointer to the applet resource - std::optional styleSet; //!< The controller styles supported by the application - std::unordered_map deviceMap; //!< Mapping from a controller's ID to it's corresponding JoyConDevice + std::shared_ptr resource{}; //!< A shared pointer to the applet resource + std::optional styleSet; //!< The controller styles supported by the application + std::unordered_map deviceMap; //!< Mapping from a controller's ID to it's corresponding JoyConDevice JoyConOrientation orientation{JoyConOrientation::Unset}; //!< The Orientation of the Joy-Con(s) public: - hid(const DeviceState &state, ServiceManager &manager); + IHidServer(const DeviceState &state, ServiceManager &manager); /** * @brief This returns an IAppletResource (https://switchbrew.org/wiki/HID_services#CreateAppletResource) diff --git a/app/src/main/cpp/skyline/services/hid/hid.cpp b/app/src/main/cpp/skyline/services/hid/hid.cpp deleted file mode 100644 index 90e991eb..00000000 --- a/app/src/main/cpp/skyline/services/hid/hid.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include "hid.h" -#include - -namespace skyline::service::hid { - IAppletResource::IAppletResource(const DeviceState &state, ServiceManager &manager) : BaseService(state, manager, Service::hid_IAppletResource, "hid:IAppletResource", { - {0x0, SFUNC(IAppletResource::GetSharedMemoryHandle)} - }) {} - - void IAppletResource::GetSharedMemoryHandle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - hidSharedMemory = std::make_shared(state, NULL, constant::hidSharedMemSize, memory::Permission{true, false, false}); - auto handle = state.process->InsertItem(hidSharedMemory); - state.logger->Debug("HID Shared Memory Handle: 0x{:X}", handle); - response.copyHandles.push_back(handle); - } - - hid::hid(const DeviceState &state, ServiceManager &manager) : BaseService(state, manager, Service::hid, "hid", { - {0x0, SFUNC(hid::CreateAppletResource)}, - {0x64, SFUNC(hid::SetSupportedNpadStyleSet)}, - {0x66, SFUNC(hid::SetSupportedNpadIdType)}, - {0x67, SFUNC(hid::ActivateNpad)}, - {0x78, SFUNC(hid::SetNpadJoyHoldType)}, - {0x7A, SFUNC(hid::SetNpadJoyAssignmentModeSingleByDefault)}, - {0x7B, SFUNC(hid::SetNpadJoyAssignmentModeSingle)}, - {0x7C, SFUNC(hid::SetNpadJoyAssignmentModeDual)} - }) {} - - void hid::CreateAppletResource(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - resource = std::make_shared(state, manager); - manager.RegisterService(resource, session, response); - } - - void hid::SetSupportedNpadStyleSet(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - auto styleSet = request.Pop(); - state.logger->Debug("Controller Support:\nPro-Controller: {}\nJoy-Con: Handheld: {}, Dual: {}, L: {}, R: {}\nGameCube: {}\nPokeBall: {}\nNES: {}, NES Handheld: {}, SNES: {}", static_cast(styleSet.proController), static_cast(styleSet.joyconHandheld), static_cast(styleSet.joyconDual), static_cast(styleSet.joyconLeft), static_cast - (styleSet.joyconRight), static_cast(styleSet.gamecube), static_cast(styleSet.pokeball), static_cast(styleSet.nes), static_cast(styleSet.nesHandheld), static_cast(styleSet.snes)); - } - - void hid::SetSupportedNpadIdType(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - const auto &buffer = request.inputBuf.at(0); - size_t numId = buffer.size / sizeof(NpadId); - u64 address = buffer.address; - for (size_t i = 0; i < numId; i++) { - auto id = state.process->GetObject(address); - deviceMap[id] = JoyConDevice(id); - address += sizeof(NpadId); - } - } - - void hid::ActivateNpad(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) {} - - void hid::SetNpadJoyHoldType(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - deviceMap[request.Pop()].assignment = JoyConAssignment::Single; - } - - void hid::SetNpadJoyAssignmentModeSingleByDefault(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - orientation = request.Pop(); - } - - void hid::SetNpadJoyAssignmentModeSingle(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - auto controllerId = request.Pop(); - auto appletUserId = request.Pop(); - deviceMap[controllerId].assignment = JoyConAssignment::Single; - deviceMap[controllerId].side = request.Pop(); - } - - void hid::SetNpadJoyAssignmentModeDual(type::KSession &session, ipc::IpcRequest &request, ipc::IpcResponse &response) { - deviceMap[request.Pop()].assignment = JoyConAssignment::Dual; - } -} diff --git a/app/src/main/cpp/skyline/services/serviceman.cpp b/app/src/main/cpp/skyline/services/serviceman.cpp index 46bc6d0a..61c885b6 100644 --- a/app/src/main/cpp/skyline/services/serviceman.cpp +++ b/app/src/main/cpp/skyline/services/serviceman.cpp @@ -7,7 +7,7 @@ #include "am/appletController.h" #include "audout/audout.h" #include "fatal/fatal.h" -#include "hid/hid.h" +#include "hid/IHidServer.h" #include "timesrv/IStaticService.h" #include "fs/fs.h" #include "nvdrv/nvdrv.h" @@ -47,8 +47,8 @@ namespace skyline::service { case Service::IAudioRendererManager: serviceObj = std::make_shared(state, *this); break; - case Service::hid: - serviceObj = std::make_shared(state, *this); + case Service::hid_IHidServer: + serviceObj = std::make_shared(state, *this); break; case Service::timesrv_IStaticService: serviceObj = std::make_shared(state, *this);