summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/hle/kernel/hle_ipc.cpp3
-rw-r--r--src/core/hle/kernel/k_auto_object.cpp9
-rw-r--r--src/core/hle/kernel/k_auto_object.h12
-rw-r--r--src/core/hle/kernel/k_process.cpp8
-rw-r--r--src/core/hle/kernel/k_server_session.cpp5
-rw-r--r--src/core/hle/kernel/kernel.cpp97
-rw-r--r--src/core/hle/kernel/kernel.h17
-rw-r--r--src/core/hle/kernel/svc.cpp6
-rw-r--r--src/core/hle/service/am/applets/applet_controller.cpp21
-rw-r--r--src/core/hle/service/am/applets/applet_controller.h15
-rw-r--r--src/core/hle/service/hid/controllers/npad.cpp13
-rw-r--r--src/core/hle/service/hid/controllers/npad.h8
-rw-r--r--src/core/hle/service/hid/hid.cpp14
-rw-r--r--src/core/hle/service/hid/hid.h13
-rw-r--r--src/core/hle/service/kernel_helpers.cpp62
-rw-r--r--src/core/hle/service/kernel_helpers.h35
-rw-r--r--src/core/hle/service/nvdrv/nvdrv.cpp11
-rw-r--r--src/core/hle/service/nvdrv/nvdrv.h3
-rw-r--r--src/core/hle/service/service.cpp11
-rw-r--r--src/core/hle/service/service.h7
-rw-r--r--src/core/hle/service/sm/sm.cpp65
-rw-r--r--src/core/hle/service/sm/sm.h14
-rw-r--r--src/input_common/sdl/sdl_impl.cpp61
-rw-r--r--src/video_core/renderer_vulkan/vk_texture_cache.cpp56
-rw-r--r--src/yuzu/configuration/configure_input_player.cpp47
-rw-r--r--src/yuzu/configuration/configure_input_player.ui44
-rw-r--r--src/yuzu_cmd/config.cpp1
-rw-r--r--src/yuzu_cmd/default_ini.h4
-rw-r--r--src/yuzu_cmd/emu_window/emu_window_sdl2.cpp44
-rw-r--r--src/yuzu_cmd/emu_window/emu_window_sdl2_vk.cpp6
-rw-r--r--src/yuzu_cmd/emu_window/emu_window_sdl2_vk.h2
-rw-r--r--src/yuzu_cmd/yuzu.cpp2
33 files changed, 580 insertions, 138 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c7b899131..5c99c00f5 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -517,6 +517,8 @@ add_library(core STATIC
517 hle/service/psc/psc.h 517 hle/service/psc/psc.h
518 hle/service/ptm/psm.cpp 518 hle/service/ptm/psm.cpp
519 hle/service/ptm/psm.h 519 hle/service/ptm/psm.h
520 hle/service/kernel_helpers.cpp
521 hle/service/kernel_helpers.h
520 hle/service/service.cpp 522 hle/service/service.cpp
521 hle/service/service.h 523 hle/service/service.h
522 hle/service/set/set.cpp 524 hle/service/set/set.cpp
diff --git a/src/core/hle/kernel/hle_ipc.cpp b/src/core/hle/kernel/hle_ipc.cpp
index 28ed6265a..ca68fc325 100644
--- a/src/core/hle/kernel/hle_ipc.cpp
+++ b/src/core/hle/kernel/hle_ipc.cpp
@@ -58,6 +58,9 @@ bool SessionRequestManager::HasSessionRequestHandler(const HLERequestContext& co
58 58
59void SessionRequestHandler::ClientConnected(KServerSession* session) { 59void SessionRequestHandler::ClientConnected(KServerSession* session) {
60 session->ClientConnected(shared_from_this()); 60 session->ClientConnected(shared_from_this());
61
62 // Ensure our server session is tracked globally.
63 kernel.RegisterServerSession(session);
61} 64}
62 65
63void SessionRequestHandler::ClientDisconnected(KServerSession* session) { 66void SessionRequestHandler::ClientDisconnected(KServerSession* session) {
diff --git a/src/core/hle/kernel/k_auto_object.cpp b/src/core/hle/kernel/k_auto_object.cpp
index dbe237f09..c99a9ebb7 100644
--- a/src/core/hle/kernel/k_auto_object.cpp
+++ b/src/core/hle/kernel/k_auto_object.cpp
@@ -3,6 +3,7 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "core/hle/kernel/k_auto_object.h" 5#include "core/hle/kernel/k_auto_object.h"
6#include "core/hle/kernel/kernel.h"
6 7
7namespace Kernel { 8namespace Kernel {
8 9
@@ -11,4 +12,12 @@ KAutoObject* KAutoObject::Create(KAutoObject* obj) {
11 return obj; 12 return obj;
12} 13}
13 14
15void KAutoObject::RegisterWithKernel() {
16 kernel.RegisterKernelObject(this);
17}
18
19void KAutoObject::UnregisterWithKernel() {
20 kernel.UnregisterKernelObject(this);
21}
22
14} // namespace Kernel 23} // namespace Kernel
diff --git a/src/core/hle/kernel/k_auto_object.h b/src/core/hle/kernel/k_auto_object.h
index 88a052f65..e4fcdbc67 100644
--- a/src/core/hle/kernel/k_auto_object.h
+++ b/src/core/hle/kernel/k_auto_object.h
@@ -85,8 +85,12 @@ private:
85 KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject); 85 KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
86 86
87public: 87public:
88 explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {} 88 explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {
89 virtual ~KAutoObject() = default; 89 RegisterWithKernel();
90 }
91 virtual ~KAutoObject() {
92 UnregisterWithKernel();
93 }
90 94
91 static KAutoObject* Create(KAutoObject* ptr); 95 static KAutoObject* Create(KAutoObject* ptr);
92 96
@@ -166,6 +170,10 @@ public:
166 } 170 }
167 } 171 }
168 172
173private:
174 void RegisterWithKernel();
175 void UnregisterWithKernel();
176
169protected: 177protected:
170 KernelCore& kernel; 178 KernelCore& kernel;
171 std::string name; 179 std::string name;
diff --git a/src/core/hle/kernel/k_process.cpp b/src/core/hle/kernel/k_process.cpp
index d1bd98051..8ead1a769 100644
--- a/src/core/hle/kernel/k_process.cpp
+++ b/src/core/hle/kernel/k_process.cpp
@@ -10,6 +10,7 @@
10#include "common/alignment.h" 10#include "common/alignment.h"
11#include "common/assert.h" 11#include "common/assert.h"
12#include "common/logging/log.h" 12#include "common/logging/log.h"
13#include "common/scope_exit.h"
13#include "common/settings.h" 14#include "common/settings.h"
14#include "core/core.h" 15#include "core/core.h"
15#include "core/device_memory.h" 16#include "core/device_memory.h"
@@ -43,6 +44,8 @@ void SetupMainThread(Core::System& system, KProcess& owner_process, u32 priority
43 ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1)); 44 ASSERT(owner_process.GetResourceLimit()->Reserve(LimitableResource::Threads, 1));
44 45
45 KThread* thread = KThread::Create(system.Kernel()); 46 KThread* thread = KThread::Create(system.Kernel());
47 SCOPE_EXIT({ thread->Close(); });
48
46 ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority, 49 ASSERT(KThread::InitializeUserThread(system, thread, entry_point, 0, stack_top, priority,
47 owner_process.GetIdealCoreId(), &owner_process) 50 owner_process.GetIdealCoreId(), &owner_process)
48 .IsSuccess()); 51 .IsSuccess());
@@ -162,7 +165,7 @@ void KProcess::DecrementThreadCount() {
162 ASSERT(num_threads > 0); 165 ASSERT(num_threads > 0);
163 166
164 if (const auto count = --num_threads; count == 0) { 167 if (const auto count = --num_threads; count == 0) {
165 UNIMPLEMENTED_MSG("Process termination is not implemented!"); 168 LOG_WARNING(Kernel, "Process termination is not fully implemented.");
166 } 169 }
167} 170}
168 171
@@ -406,6 +409,9 @@ void KProcess::Finalize() {
406 resource_limit->Close(); 409 resource_limit->Close();
407 } 410 }
408 411
412 // Finalize the handle table and close any open handles.
413 handle_table.Finalize();
414
409 // Perform inherited finalization. 415 // Perform inherited finalization.
410 KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject>::Finalize(); 416 KAutoObjectWithSlabHeapAndContainer<KProcess, KSynchronizationObject>::Finalize();
411} 417}
diff --git a/src/core/hle/kernel/k_server_session.cpp b/src/core/hle/kernel/k_server_session.cpp
index 5c3c13ce6..b9f24475c 100644
--- a/src/core/hle/kernel/k_server_session.cpp
+++ b/src/core/hle/kernel/k_server_session.cpp
@@ -28,7 +28,10 @@ namespace Kernel {
28 28
29KServerSession::KServerSession(KernelCore& kernel_) : KSynchronizationObject{kernel_} {} 29KServerSession::KServerSession(KernelCore& kernel_) : KSynchronizationObject{kernel_} {}
30 30
31KServerSession::~KServerSession() {} 31KServerSession::~KServerSession() {
32 // Ensure that the global list tracking server sessions does not hold on to a reference.
33 kernel.UnregisterServerSession(this);
34}
32 35
33void KServerSession::Initialize(KSession* parent_session_, std::string&& name_, 36void KServerSession::Initialize(KSession* parent_session_, std::string&& name_,
34 std::shared_ptr<SessionRequestManager> manager_) { 37 std::shared_ptr<SessionRequestManager> manager_) {
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index 64bd0c494..92fbc5532 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -61,6 +61,7 @@ struct KernelCore::Impl {
61 void Initialize(KernelCore& kernel) { 61 void Initialize(KernelCore& kernel) {
62 global_scheduler_context = std::make_unique<Kernel::GlobalSchedulerContext>(kernel); 62 global_scheduler_context = std::make_unique<Kernel::GlobalSchedulerContext>(kernel);
63 global_handle_table = std::make_unique<Kernel::KHandleTable>(kernel); 63 global_handle_table = std::make_unique<Kernel::KHandleTable>(kernel);
64 global_handle_table->Initialize(KHandleTable::MaxTableSize);
64 65
65 is_phantom_mode_for_singlecore = false; 66 is_phantom_mode_for_singlecore = false;
66 67
@@ -90,9 +91,39 @@ struct KernelCore::Impl {
90 } 91 }
91 92
92 void Shutdown() { 93 void Shutdown() {
94 // Shutdown all processes.
95 if (current_process) {
96 current_process->Finalize();
97 current_process->Close();
98 current_process = nullptr;
99 }
93 process_list.clear(); 100 process_list.clear();
94 101
95 // Ensures all service threads gracefully shutdown 102 // Close all open server ports.
103 std::unordered_set<KServerPort*> server_ports_;
104 {
105 std::lock_guard lk(server_ports_lock);
106 server_ports_ = server_ports;
107 server_ports.clear();
108 }
109 for (auto* server_port : server_ports_) {
110 server_port->Close();
111 }
112 // Close all open server sessions.
113 std::unordered_set<KServerSession*> server_sessions_;
114 {
115 std::lock_guard lk(server_sessions_lock);
116 server_sessions_ = server_sessions;
117 server_sessions.clear();
118 }
119 for (auto* server_session : server_sessions_) {
120 server_session->Close();
121 }
122
123 // Ensure that the object list container is finalized and properly shutdown.
124 object_list_container.Finalize();
125
126 // Ensures all service threads gracefully shutdown.
96 service_threads.clear(); 127 service_threads.clear();
97 128
98 next_object_id = 0; 129 next_object_id = 0;
@@ -111,11 +142,7 @@ struct KernelCore::Impl {
111 142
112 cores.clear(); 143 cores.clear();
113 144
114 if (current_process) { 145 global_handle_table->Finalize();
115 current_process->Close();
116 current_process = nullptr;
117 }
118
119 global_handle_table.reset(); 146 global_handle_table.reset();
120 147
121 preemption_event = nullptr; 148 preemption_event = nullptr;
@@ -142,6 +169,16 @@ struct KernelCore::Impl {
142 169
143 // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others 170 // Next host thead ID to use, 0-3 IDs represent core threads, >3 represent others
144 next_host_thread_id = Core::Hardware::NUM_CPU_CORES; 171 next_host_thread_id = Core::Hardware::NUM_CPU_CORES;
172
173 // Track kernel objects that were not freed on shutdown
174 {
175 std::lock_guard lk(registered_objects_lock);
176 if (registered_objects.size()) {
177 LOG_WARNING(Kernel, "{} kernel objects were dangling on shutdown!",
178 registered_objects.size());
179 registered_objects.clear();
180 }
181 }
145 } 182 }
146 183
147 void InitializePhysicalCores() { 184 void InitializePhysicalCores() {
@@ -630,6 +667,21 @@ struct KernelCore::Impl {
630 user_slab_heap_size); 667 user_slab_heap_size);
631 } 668 }
632 669
670 KClientPort* CreateNamedServicePort(std::string name) {
671 auto search = service_interface_factory.find(name);
672 if (search == service_interface_factory.end()) {
673 UNIMPLEMENTED();
674 return {};
675 }
676
677 KClientPort* port = &search->second(system.ServiceManager(), system);
678 {
679 std::lock_guard lk(server_ports_lock);
680 server_ports.insert(&port->GetParent()->GetServerPort());
681 }
682 return port;
683 }
684
633 std::atomic<u32> next_object_id{0}; 685 std::atomic<u32> next_object_id{0};
634 std::atomic<u64> next_kernel_process_id{KProcess::InitialKIPIDMin}; 686 std::atomic<u64> next_kernel_process_id{KProcess::InitialKIPIDMin};
635 std::atomic<u64> next_user_process_id{KProcess::ProcessIDMin}; 687 std::atomic<u64> next_user_process_id{KProcess::ProcessIDMin};
@@ -656,6 +708,12 @@ struct KernelCore::Impl {
656 /// the ConnectToPort SVC. 708 /// the ConnectToPort SVC.
657 std::unordered_map<std::string, ServiceInterfaceFactory> service_interface_factory; 709 std::unordered_map<std::string, ServiceInterfaceFactory> service_interface_factory;
658 NamedPortTable named_ports; 710 NamedPortTable named_ports;
711 std::unordered_set<KServerPort*> server_ports;
712 std::unordered_set<KServerSession*> server_sessions;
713 std::unordered_set<KAutoObject*> registered_objects;
714 std::mutex server_ports_lock;
715 std::mutex server_sessions_lock;
716 std::mutex registered_objects_lock;
659 717
660 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor; 718 std::unique_ptr<Core::ExclusiveMonitor> exclusive_monitor;
661 std::vector<Kernel::PhysicalCore> cores; 719 std::vector<Kernel::PhysicalCore> cores;
@@ -844,12 +902,27 @@ void KernelCore::RegisterNamedService(std::string name, ServiceInterfaceFactory&
844} 902}
845 903
846KClientPort* KernelCore::CreateNamedServicePort(std::string name) { 904KClientPort* KernelCore::CreateNamedServicePort(std::string name) {
847 auto search = impl->service_interface_factory.find(name); 905 return impl->CreateNamedServicePort(std::move(name));
848 if (search == impl->service_interface_factory.end()) { 906}
849 UNIMPLEMENTED(); 907
850 return {}; 908void KernelCore::RegisterServerSession(KServerSession* server_session) {
851 } 909 std::lock_guard lk(impl->server_sessions_lock);
852 return &search->second(impl->system.ServiceManager(), impl->system); 910 impl->server_sessions.insert(server_session);
911}
912
913void KernelCore::UnregisterServerSession(KServerSession* server_session) {
914 std::lock_guard lk(impl->server_sessions_lock);
915 impl->server_sessions.erase(server_session);
916}
917
918void KernelCore::RegisterKernelObject(KAutoObject* object) {
919 std::lock_guard lk(impl->registered_objects_lock);
920 impl->registered_objects.insert(object);
921}
922
923void KernelCore::UnregisterKernelObject(KAutoObject* object) {
924 std::lock_guard lk(impl->registered_objects_lock);
925 impl->registered_objects.erase(object);
853} 926}
854 927
855bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const { 928bool KernelCore::IsValidNamedPort(NamedPortTable::const_iterator port) const {
diff --git a/src/core/hle/kernel/kernel.h b/src/core/hle/kernel/kernel.h
index 2d01e1ae0..3a6db0b1c 100644
--- a/src/core/hle/kernel/kernel.h
+++ b/src/core/hle/kernel/kernel.h
@@ -45,6 +45,7 @@ class KPort;
45class KProcess; 45class KProcess;
46class KResourceLimit; 46class KResourceLimit;
47class KScheduler; 47class KScheduler;
48class KServerSession;
48class KSession; 49class KSession;
49class KSharedMemory; 50class KSharedMemory;
50class KThread; 51class KThread;
@@ -185,6 +186,22 @@ public:
185 /// Opens a port to a service previously registered with RegisterNamedService. 186 /// Opens a port to a service previously registered with RegisterNamedService.
186 KClientPort* CreateNamedServicePort(std::string name); 187 KClientPort* CreateNamedServicePort(std::string name);
187 188
189 /// Registers a server session with the gobal emulation state, to be freed on shutdown. This is
190 /// necessary because we do not emulate processes for HLE sessions.
191 void RegisterServerSession(KServerSession* server_session);
192
193 /// Unregisters a server session previously registered with RegisterServerSession when it was
194 /// destroyed during the current emulation session.
195 void UnregisterServerSession(KServerSession* server_session);
196
197 /// Registers all kernel objects with the global emulation state, this is purely for tracking
198 /// leaks after emulation has been shutdown.
199 void RegisterKernelObject(KAutoObject* object);
200
201 /// Unregisters a kernel object previously registered with RegisterKernelObject when it was
202 /// destroyed during the current emulation session.
203 void UnregisterKernelObject(KAutoObject* object);
204
188 /// Determines whether or not the given port is a valid named port. 205 /// Determines whether or not the given port is a valid named port.
189 bool IsValidNamedPort(NamedPortTable::const_iterator port) const; 206 bool IsValidNamedPort(NamedPortTable::const_iterator port) const;
190 207
diff --git a/src/core/hle/kernel/svc.cpp b/src/core/hle/kernel/svc.cpp
index 8339e11a0..2eb532472 100644
--- a/src/core/hle/kernel/svc.cpp
+++ b/src/core/hle/kernel/svc.cpp
@@ -298,6 +298,7 @@ static ResultCode ConnectToNamedPort(Core::System& system, Handle* out, VAddr po
298 // Create a session. 298 // Create a session.
299 KClientSession* session{}; 299 KClientSession* session{};
300 R_TRY(port->CreateSession(std::addressof(session))); 300 R_TRY(port->CreateSession(std::addressof(session)));
301 port->Close();
301 302
302 // Register the session in the table, close the extra reference. 303 // Register the session in the table, close the extra reference.
303 handle_table.Register(*out, session); 304 handle_table.Register(*out, session);
@@ -1439,11 +1440,6 @@ static void ExitProcess(Core::System& system) {
1439 LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID()); 1440 LOG_INFO(Kernel_SVC, "Process {} exiting", current_process->GetProcessID());
1440 ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running, 1441 ASSERT_MSG(current_process->GetStatus() == ProcessStatus::Running,
1441 "Process has already exited"); 1442 "Process has already exited");
1442
1443 current_process->PrepareForTermination();
1444
1445 // Kill the current thread
1446 system.Kernel().CurrentScheduler()->GetCurrentThread()->Exit();
1447} 1443}
1448 1444
1449static void ExitProcess32(Core::System& system) { 1445static void ExitProcess32(Core::System& system) {
diff --git a/src/core/hle/service/am/applets/applet_controller.cpp b/src/core/hle/service/am/applets/applet_controller.cpp
index 12682effe..2721679c1 100644
--- a/src/core/hle/service/am/applets/applet_controller.cpp
+++ b/src/core/hle/service/am/applets/applet_controller.cpp
@@ -87,6 +87,10 @@ void Controller::Initialize() {
87 case sizeof(ControllerUpdateFirmwareArg): 87 case sizeof(ControllerUpdateFirmwareArg):
88 controller_private_arg.mode = ControllerSupportMode::ShowControllerFirmwareUpdate; 88 controller_private_arg.mode = ControllerSupportMode::ShowControllerFirmwareUpdate;
89 break; 89 break;
90 case sizeof(ControllerKeyRemappingArg):
91 controller_private_arg.mode =
92 ControllerSupportMode::ShowControllerKeyRemappingForSystem;
93 break;
90 default: 94 default:
91 UNIMPLEMENTED_MSG("Unknown ControllerPrivateArg mode={} with arg_size={}", 95 UNIMPLEMENTED_MSG("Unknown ControllerPrivateArg mode={} with arg_size={}",
92 controller_private_arg.mode, controller_private_arg.arg_size); 96 controller_private_arg.mode, controller_private_arg.arg_size);
@@ -99,7 +103,9 @@ void Controller::Initialize() {
99 // This is always 0 (Application) except with ShowControllerFirmwareUpdateForSystem. 103 // This is always 0 (Application) except with ShowControllerFirmwareUpdateForSystem.
100 if (controller_private_arg.caller >= ControllerSupportCaller::MaxControllerSupportCaller) { 104 if (controller_private_arg.caller >= ControllerSupportCaller::MaxControllerSupportCaller) {
101 if (controller_private_arg.flag_1 && 105 if (controller_private_arg.flag_1 &&
102 controller_private_arg.mode == ControllerSupportMode::ShowControllerFirmwareUpdate) { 106 (controller_private_arg.mode == ControllerSupportMode::ShowControllerFirmwareUpdate ||
107 controller_private_arg.mode ==
108 ControllerSupportMode::ShowControllerKeyRemappingForSystem)) {
103 controller_private_arg.caller = ControllerSupportCaller::System; 109 controller_private_arg.caller = ControllerSupportCaller::System;
104 } else { 110 } else {
105 controller_private_arg.caller = ControllerSupportCaller::Application; 111 controller_private_arg.caller = ControllerSupportCaller::Application;
@@ -121,6 +127,7 @@ void Controller::Initialize() {
121 std::memcpy(&controller_user_arg_old, user_arg.data(), user_arg.size()); 127 std::memcpy(&controller_user_arg_old, user_arg.data(), user_arg.size());
122 break; 128 break;
123 case ControllerAppletVersion::Version7: 129 case ControllerAppletVersion::Version7:
130 case ControllerAppletVersion::Version8:
124 ASSERT(user_arg.size() == sizeof(ControllerSupportArgNew)); 131 ASSERT(user_arg.size() == sizeof(ControllerSupportArgNew));
125 std::memcpy(&controller_user_arg_new, user_arg.data(), user_arg.size()); 132 std::memcpy(&controller_user_arg_new, user_arg.data(), user_arg.size());
126 break; 133 break;
@@ -143,6 +150,16 @@ void Controller::Initialize() {
143 std::memcpy(&controller_update_arg, update_arg.data(), update_arg.size()); 150 std::memcpy(&controller_update_arg, update_arg.data(), update_arg.size());
144 break; 151 break;
145 } 152 }
153 case ControllerSupportMode::ShowControllerKeyRemappingForSystem: {
154 const auto remapping_arg_storage = broker.PopNormalDataToApplet();
155 ASSERT(remapping_arg_storage != nullptr);
156
157 const auto& remapping_arg = remapping_arg_storage->GetData();
158 ASSERT(remapping_arg.size() == sizeof(ControllerKeyRemappingArg));
159
160 std::memcpy(&controller_key_remapping_arg, remapping_arg.data(), remapping_arg.size());
161 break;
162 }
146 default: { 163 default: {
147 UNIMPLEMENTED_MSG("Unimplemented ControllerSupportMode={}", controller_private_arg.mode); 164 UNIMPLEMENTED_MSG("Unimplemented ControllerSupportMode={}", controller_private_arg.mode);
148 break; 165 break;
@@ -179,6 +196,7 @@ void Controller::Execute() {
179 std::vector<ExplainText>(controller_user_arg_old.explain_text.begin(), 196 std::vector<ExplainText>(controller_user_arg_old.explain_text.begin(),
180 controller_user_arg_old.explain_text.end())); 197 controller_user_arg_old.explain_text.end()));
181 case ControllerAppletVersion::Version7: 198 case ControllerAppletVersion::Version7:
199 case ControllerAppletVersion::Version8:
182 default: 200 default:
183 return ConvertToFrontendParameters( 201 return ConvertToFrontendParameters(
184 controller_private_arg, controller_user_arg_new.header, 202 controller_private_arg, controller_user_arg_new.header,
@@ -210,6 +228,7 @@ void Controller::Execute() {
210 } 228 }
211 case ControllerSupportMode::ShowControllerStrapGuide: 229 case ControllerSupportMode::ShowControllerStrapGuide:
212 case ControllerSupportMode::ShowControllerFirmwareUpdate: 230 case ControllerSupportMode::ShowControllerFirmwareUpdate:
231 case ControllerSupportMode::ShowControllerKeyRemappingForSystem:
213 UNIMPLEMENTED_MSG("ControllerSupportMode={} is not implemented", 232 UNIMPLEMENTED_MSG("ControllerSupportMode={} is not implemented",
214 controller_private_arg.mode); 233 controller_private_arg.mode);
215 ConfigurationComplete(); 234 ConfigurationComplete();
diff --git a/src/core/hle/service/am/applets/applet_controller.h b/src/core/hle/service/am/applets/applet_controller.h
index 20617e91f..0a34c4fc0 100644
--- a/src/core/hle/service/am/applets/applet_controller.h
+++ b/src/core/hle/service/am/applets/applet_controller.h
@@ -25,13 +25,15 @@ enum class ControllerAppletVersion : u32_le {
25 Version3 = 0x3, // 1.0.0 - 2.3.0 25 Version3 = 0x3, // 1.0.0 - 2.3.0
26 Version4 = 0x4, // 3.0.0 - 5.1.0 26 Version4 = 0x4, // 3.0.0 - 5.1.0
27 Version5 = 0x5, // 6.0.0 - 7.0.1 27 Version5 = 0x5, // 6.0.0 - 7.0.1
28 Version7 = 0x7, // 8.0.0+ 28 Version7 = 0x7, // 8.0.0 - 10.2.0
29 Version8 = 0x8, // 11.0.0+
29}; 30};
30 31
31enum class ControllerSupportMode : u8 { 32enum class ControllerSupportMode : u8 {
32 ShowControllerSupport, 33 ShowControllerSupport,
33 ShowControllerStrapGuide, 34 ShowControllerStrapGuide,
34 ShowControllerFirmwareUpdate, 35 ShowControllerFirmwareUpdate,
36 ShowControllerKeyRemappingForSystem,
35 37
36 MaxControllerSupportMode, 38 MaxControllerSupportMode,
37}; 39};
@@ -78,7 +80,7 @@ struct ControllerSupportArgOld {
78static_assert(sizeof(ControllerSupportArgOld) == 0x21C, 80static_assert(sizeof(ControllerSupportArgOld) == 0x21C,
79 "ControllerSupportArgOld has incorrect size."); 81 "ControllerSupportArgOld has incorrect size.");
80 82
81// LibraryAppletVersion 0x7 83// LibraryAppletVersion 0x7, 0x8
82struct ControllerSupportArgNew { 84struct ControllerSupportArgNew {
83 ControllerSupportArgHeader header{}; 85 ControllerSupportArgHeader header{};
84 std::array<IdentificationColor, 8> identification_colors{}; 86 std::array<IdentificationColor, 8> identification_colors{};
@@ -95,6 +97,14 @@ struct ControllerUpdateFirmwareArg {
95static_assert(sizeof(ControllerUpdateFirmwareArg) == 0x4, 97static_assert(sizeof(ControllerUpdateFirmwareArg) == 0x4,
96 "ControllerUpdateFirmwareArg has incorrect size."); 98 "ControllerUpdateFirmwareArg has incorrect size.");
97 99
100struct ControllerKeyRemappingArg {
101 u64 unknown{};
102 u32 unknown_2{};
103 INSERT_PADDING_WORDS(1);
104};
105static_assert(sizeof(ControllerKeyRemappingArg) == 0x10,
106 "ControllerKeyRemappingArg has incorrect size.");
107
98struct ControllerSupportResultInfo { 108struct ControllerSupportResultInfo {
99 s8 player_count{}; 109 s8 player_count{};
100 INSERT_PADDING_BYTES(3); 110 INSERT_PADDING_BYTES(3);
@@ -128,6 +138,7 @@ private:
128 ControllerSupportArgOld controller_user_arg_old; 138 ControllerSupportArgOld controller_user_arg_old;
129 ControllerSupportArgNew controller_user_arg_new; 139 ControllerSupportArgNew controller_user_arg_new;
130 ControllerUpdateFirmwareArg controller_update_arg; 140 ControllerUpdateFirmwareArg controller_update_arg;
141 ControllerKeyRemappingArg controller_key_remapping_arg;
131 bool complete{false}; 142 bool complete{false};
132 ResultCode status{ResultSuccess}; 143 ResultCode status{ResultSuccess};
133 bool is_single_mode{false}; 144 bool is_single_mode{false};
diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp
index 6ce1360e3..b7f551e40 100644
--- a/src/core/hle/service/hid/controllers/npad.cpp
+++ b/src/core/hle/service/hid/controllers/npad.cpp
@@ -18,6 +18,7 @@
18#include "core/hle/kernel/k_writable_event.h" 18#include "core/hle/kernel/k_writable_event.h"
19#include "core/hle/kernel/kernel.h" 19#include "core/hle/kernel/kernel.h"
20#include "core/hle/service/hid/controllers/npad.h" 20#include "core/hle/service/hid/controllers/npad.h"
21#include "core/hle/service/kernel_helpers.h"
21 22
22namespace Service::HID { 23namespace Service::HID {
23constexpr s32 HID_JOYSTICK_MAX = 0x7fff; 24constexpr s32 HID_JOYSTICK_MAX = 0x7fff;
@@ -147,7 +148,9 @@ bool Controller_NPad::IsDeviceHandleValid(const DeviceHandle& device_handle) {
147 device_handle.device_index < DeviceIndex::MaxDeviceIndex; 148 device_handle.device_index < DeviceIndex::MaxDeviceIndex;
148} 149}
149 150
150Controller_NPad::Controller_NPad(Core::System& system_) : ControllerBase{system_} { 151Controller_NPad::Controller_NPad(Core::System& system_,
152 KernelHelpers::ServiceContext& service_context_)
153 : ControllerBase{system_}, service_context{service_context_} {
151 latest_vibration_values.fill({DEFAULT_VIBRATION_VALUE, DEFAULT_VIBRATION_VALUE}); 154 latest_vibration_values.fill({DEFAULT_VIBRATION_VALUE, DEFAULT_VIBRATION_VALUE});
152} 155}
153 156
@@ -251,10 +254,9 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
251} 254}
252 255
253void Controller_NPad::OnInit() { 256void Controller_NPad::OnInit() {
254 auto& kernel = system.Kernel();
255 for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { 257 for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) {
256 styleset_changed_events[i] = Kernel::KEvent::Create(kernel); 258 styleset_changed_events[i] =
257 styleset_changed_events[i]->Initialize(fmt::format("npad:NpadStyleSetChanged_{}", i)); 259 service_context.CreateEvent(fmt::format("npad:NpadStyleSetChanged_{}", i));
258 } 260 }
259 261
260 if (!IsControllerActivated()) { 262 if (!IsControllerActivated()) {
@@ -344,8 +346,7 @@ void Controller_NPad::OnRelease() {
344 } 346 }
345 347
346 for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) { 348 for (std::size_t i = 0; i < styleset_changed_events.size(); ++i) {
347 styleset_changed_events[i]->Close(); 349 service_context.CloseEvent(styleset_changed_events[i]);
348 styleset_changed_events[i] = nullptr;
349 } 350 }
350} 351}
351 352
diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h
index 1409d82a2..4fcc6f93a 100644
--- a/src/core/hle/service/hid/controllers/npad.h
+++ b/src/core/hle/service/hid/controllers/npad.h
@@ -20,6 +20,10 @@ class KEvent;
20class KReadableEvent; 20class KReadableEvent;
21} // namespace Kernel 21} // namespace Kernel
22 22
23namespace Service::KernelHelpers {
24class ServiceContext;
25}
26
23namespace Service::HID { 27namespace Service::HID {
24 28
25constexpr u32 NPAD_HANDHELD = 32; 29constexpr u32 NPAD_HANDHELD = 32;
@@ -27,7 +31,8 @@ constexpr u32 NPAD_UNKNOWN = 16; // TODO(ogniK): What is this?
27 31
28class Controller_NPad final : public ControllerBase { 32class Controller_NPad final : public ControllerBase {
29public: 33public:
30 explicit Controller_NPad(Core::System& system_); 34 explicit Controller_NPad(Core::System& system_,
35 KernelHelpers::ServiceContext& service_context_);
31 ~Controller_NPad() override; 36 ~Controller_NPad() override;
32 37
33 // Called when the controller is initialized 38 // Called when the controller is initialized
@@ -566,6 +571,7 @@ private:
566 std::array<std::unique_ptr<Input::MotionDevice>, Settings::NativeMotion::NUM_MOTIONS_HID>, 571 std::array<std::unique_ptr<Input::MotionDevice>, Settings::NativeMotion::NUM_MOTIONS_HID>,
567 10>; 572 10>;
568 573
574 KernelHelpers::ServiceContext& service_context;
569 std::mutex mutex; 575 std::mutex mutex;
570 ButtonArray buttons; 576 ButtonArray buttons;
571 StickArray sticks; 577 StickArray sticks;
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index d68b023d0..b8b80570d 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -46,8 +46,9 @@ constexpr auto pad_update_ns = std::chrono::nanoseconds{1000 * 1000}; //
46constexpr auto motion_update_ns = std::chrono::nanoseconds{15 * 1000 * 1000}; // (15ms, 66.666Hz) 46constexpr auto motion_update_ns = std::chrono::nanoseconds{15 * 1000 * 1000}; // (15ms, 66.666Hz)
47constexpr std::size_t SHARED_MEMORY_SIZE = 0x40000; 47constexpr std::size_t SHARED_MEMORY_SIZE = 0x40000;
48 48
49IAppletResource::IAppletResource(Core::System& system_) 49IAppletResource::IAppletResource(Core::System& system_,
50 : ServiceFramework{system_, "IAppletResource"} { 50 KernelHelpers::ServiceContext& service_context_)
51 : ServiceFramework{system_, "IAppletResource"}, service_context{service_context_} {
51 static const FunctionInfo functions[] = { 52 static const FunctionInfo functions[] = {
52 {0, &IAppletResource::GetSharedMemoryHandle, "GetSharedMemoryHandle"}, 53 {0, &IAppletResource::GetSharedMemoryHandle, "GetSharedMemoryHandle"},
53 }; 54 };
@@ -63,7 +64,7 @@ IAppletResource::IAppletResource(Core::System& system_)
63 MakeController<Controller_Stubbed>(HidController::CaptureButton); 64 MakeController<Controller_Stubbed>(HidController::CaptureButton);
64 MakeController<Controller_Stubbed>(HidController::InputDetector); 65 MakeController<Controller_Stubbed>(HidController::InputDetector);
65 MakeController<Controller_Stubbed>(HidController::UniquePad); 66 MakeController<Controller_Stubbed>(HidController::UniquePad);
66 MakeController<Controller_NPad>(HidController::NPad); 67 MakeControllerWithServiceContext<Controller_NPad>(HidController::NPad);
67 MakeController<Controller_Gesture>(HidController::Gesture); 68 MakeController<Controller_Gesture>(HidController::Gesture);
68 MakeController<Controller_ConsoleSixAxis>(HidController::ConsoleSixAxisSensor); 69 MakeController<Controller_ConsoleSixAxis>(HidController::ConsoleSixAxisSensor);
69 70
@@ -191,13 +192,14 @@ private:
191 192
192std::shared_ptr<IAppletResource> Hid::GetAppletResource() { 193std::shared_ptr<IAppletResource> Hid::GetAppletResource() {
193 if (applet_resource == nullptr) { 194 if (applet_resource == nullptr) {
194 applet_resource = std::make_shared<IAppletResource>(system); 195 applet_resource = std::make_shared<IAppletResource>(system, service_context);
195 } 196 }
196 197
197 return applet_resource; 198 return applet_resource;
198} 199}
199 200
200Hid::Hid(Core::System& system_) : ServiceFramework{system_, "hid"} { 201Hid::Hid(Core::System& system_)
202 : ServiceFramework{system_, "hid"}, service_context{system_, service_name} {
201 // clang-format off 203 // clang-format off
202 static const FunctionInfo functions[] = { 204 static const FunctionInfo functions[] = {
203 {0, &Hid::CreateAppletResource, "CreateAppletResource"}, 205 {0, &Hid::CreateAppletResource, "CreateAppletResource"},
@@ -347,7 +349,7 @@ void Hid::CreateAppletResource(Kernel::HLERequestContext& ctx) {
347 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 349 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
348 350
349 if (applet_resource == nullptr) { 351 if (applet_resource == nullptr) {
350 applet_resource = std::make_shared<IAppletResource>(system); 352 applet_resource = std::make_shared<IAppletResource>(system, service_context);
351 } 353 }
352 354
353 IPC::ResponseBuilder rb{ctx, 2, 0, 1}; 355 IPC::ResponseBuilder rb{ctx, 2, 0, 1};
diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h
index 83fc2ea1d..9c5c7f252 100644
--- a/src/core/hle/service/hid/hid.h
+++ b/src/core/hle/service/hid/hid.h
@@ -7,6 +7,7 @@
7#include <chrono> 7#include <chrono>
8 8
9#include "core/hle/service/hid/controllers/controller_base.h" 9#include "core/hle/service/hid/controllers/controller_base.h"
10#include "core/hle/service/kernel_helpers.h"
10#include "core/hle/service/service.h" 11#include "core/hle/service/service.h"
11 12
12namespace Core::Timing { 13namespace Core::Timing {
@@ -39,7 +40,8 @@ enum class HidController : std::size_t {
39 40
40class IAppletResource final : public ServiceFramework<IAppletResource> { 41class IAppletResource final : public ServiceFramework<IAppletResource> {
41public: 42public:
42 explicit IAppletResource(Core::System& system_); 43 explicit IAppletResource(Core::System& system_,
44 KernelHelpers::ServiceContext& service_context_);
43 ~IAppletResource() override; 45 ~IAppletResource() override;
44 46
45 void ActivateController(HidController controller); 47 void ActivateController(HidController controller);
@@ -60,11 +62,18 @@ private:
60 void MakeController(HidController controller) { 62 void MakeController(HidController controller) {
61 controllers[static_cast<std::size_t>(controller)] = std::make_unique<T>(system); 63 controllers[static_cast<std::size_t>(controller)] = std::make_unique<T>(system);
62 } 64 }
65 template <typename T>
66 void MakeControllerWithServiceContext(HidController controller) {
67 controllers[static_cast<std::size_t>(controller)] =
68 std::make_unique<T>(system, service_context);
69 }
63 70
64 void GetSharedMemoryHandle(Kernel::HLERequestContext& ctx); 71 void GetSharedMemoryHandle(Kernel::HLERequestContext& ctx);
65 void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); 72 void UpdateControllers(std::uintptr_t user_data, std::chrono::nanoseconds ns_late);
66 void UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late); 73 void UpdateMotion(std::uintptr_t user_data, std::chrono::nanoseconds ns_late);
67 74
75 KernelHelpers::ServiceContext& service_context;
76
68 std::shared_ptr<Core::Timing::EventType> pad_update_event; 77 std::shared_ptr<Core::Timing::EventType> pad_update_event;
69 std::shared_ptr<Core::Timing::EventType> motion_update_event; 78 std::shared_ptr<Core::Timing::EventType> motion_update_event;
70 79
@@ -176,6 +185,8 @@ private:
176 static_assert(sizeof(VibrationDeviceInfo) == 0x8, "VibrationDeviceInfo has incorrect size."); 185 static_assert(sizeof(VibrationDeviceInfo) == 0x8, "VibrationDeviceInfo has incorrect size.");
177 186
178 std::shared_ptr<IAppletResource> applet_resource; 187 std::shared_ptr<IAppletResource> applet_resource;
188
189 KernelHelpers::ServiceContext service_context;
179}; 190};
180 191
181/// Reload input devices. Used when input configuration changed 192/// Reload input devices. Used when input configuration changed
diff --git a/src/core/hle/service/kernel_helpers.cpp b/src/core/hle/service/kernel_helpers.cpp
new file mode 100644
index 000000000..62f4cdfb2
--- /dev/null
+++ b/src/core/hle/service/kernel_helpers.cpp
@@ -0,0 +1,62 @@
1// Copyright 2021 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include "core/core.h"
6#include "core/hle/kernel/k_event.h"
7#include "core/hle/kernel/k_process.h"
8#include "core/hle/kernel/k_readable_event.h"
9#include "core/hle/kernel/k_resource_limit.h"
10#include "core/hle/kernel/k_scoped_resource_reservation.h"
11#include "core/hle/kernel/k_writable_event.h"
12#include "core/hle/service/kernel_helpers.h"
13
14namespace Service::KernelHelpers {
15
16ServiceContext::ServiceContext(Core::System& system_, std::string name_)
17 : kernel(system_.Kernel()) {
18 process = Kernel::KProcess::Create(kernel);
19 ASSERT(Kernel::KProcess::Initialize(process, system_, std::move(name_),
20 Kernel::KProcess::ProcessType::Userland)
21 .IsSuccess());
22}
23
24ServiceContext::~ServiceContext() {
25 process->Close();
26 process = nullptr;
27}
28
29Kernel::KEvent* ServiceContext::CreateEvent(std::string&& name) {
30 // Reserve a new event from the process resource limit
31 Kernel::KScopedResourceReservation event_reservation(process,
32 Kernel::LimitableResource::Events);
33 if (!event_reservation.Succeeded()) {
34 LOG_CRITICAL(Service, "Resource limit reached!");
35 return {};
36 }
37
38 // Create a new event.
39 auto* event = Kernel::KEvent::Create(kernel);
40 if (!event) {
41 LOG_CRITICAL(Service, "Unable to create event!");
42 return {};
43 }
44
45 // Initialize the event.
46 event->Initialize(std::move(name));
47
48 // Commit the thread reservation.
49 event_reservation.Commit();
50
51 // Register the event.
52 Kernel::KEvent::Register(kernel, event);
53
54 return event;
55}
56
57void ServiceContext::CloseEvent(Kernel::KEvent* event) {
58 event->GetReadableEvent().Close();
59 event->GetWritableEvent().Close();
60}
61
62} // namespace Service::KernelHelpers
diff --git a/src/core/hle/service/kernel_helpers.h b/src/core/hle/service/kernel_helpers.h
new file mode 100644
index 000000000..4f3e95f67
--- /dev/null
+++ b/src/core/hle/service/kernel_helpers.h
@@ -0,0 +1,35 @@
1// Copyright 2021 yuzu emulator team
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <string>
8
9namespace Core {
10class System;
11}
12
13namespace Kernel {
14class KernelCore;
15class KEvent;
16class KProcess;
17} // namespace Kernel
18
19namespace Service::KernelHelpers {
20
21class ServiceContext {
22public:
23 ServiceContext(Core::System& system_, std::string name_);
24 ~ServiceContext();
25
26 Kernel::KEvent* CreateEvent(std::string&& name);
27
28 void CloseEvent(Kernel::KEvent* event);
29
30private:
31 Kernel::KernelCore& kernel;
32 Kernel::KProcess* process{};
33};
34
35} // namespace Service::KernelHelpers
diff --git a/src/core/hle/service/nvdrv/nvdrv.cpp b/src/core/hle/service/nvdrv/nvdrv.cpp
index 03992af5e..ff405099a 100644
--- a/src/core/hle/service/nvdrv/nvdrv.cpp
+++ b/src/core/hle/service/nvdrv/nvdrv.cpp
@@ -39,11 +39,11 @@ void InstallInterfaces(SM::ServiceManager& service_manager, NVFlinger::NVFlinger
39 nvflinger.SetNVDrvInstance(module_); 39 nvflinger.SetNVDrvInstance(module_);
40} 40}
41 41
42Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} { 42Module::Module(Core::System& system)
43 auto& kernel = system.Kernel(); 43 : syncpoint_manager{system.GPU()}, service_context{system, "nvdrv"} {
44 for (u32 i = 0; i < MaxNvEvents; i++) { 44 for (u32 i = 0; i < MaxNvEvents; i++) {
45 events_interface.events[i].event = Kernel::KEvent::Create(kernel); 45 events_interface.events[i].event =
46 events_interface.events[i].event->Initialize(fmt::format("NVDRV::NvEvent_{}", i)); 46 service_context.CreateEvent(fmt::format("NVDRV::NvEvent_{}", i));
47 events_interface.status[i] = EventState::Free; 47 events_interface.status[i] = EventState::Free;
48 events_interface.registered[i] = false; 48 events_interface.registered[i] = false;
49 } 49 }
@@ -65,8 +65,7 @@ Module::Module(Core::System& system) : syncpoint_manager{system.GPU()} {
65 65
66Module::~Module() { 66Module::~Module() {
67 for (u32 i = 0; i < MaxNvEvents; i++) { 67 for (u32 i = 0; i < MaxNvEvents; i++) {
68 events_interface.events[i].event->Close(); 68 service_context.CloseEvent(events_interface.events[i].event);
69 events_interface.events[i].event = nullptr;
70 } 69 }
71} 70}
72 71
diff --git a/src/core/hle/service/nvdrv/nvdrv.h b/src/core/hle/service/nvdrv/nvdrv.h
index a43ceb7ae..e2a1dde5b 100644
--- a/src/core/hle/service/nvdrv/nvdrv.h
+++ b/src/core/hle/service/nvdrv/nvdrv.h
@@ -9,6 +9,7 @@
9#include <vector> 9#include <vector>
10 10
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "core/hle/service/kernel_helpers.h"
12#include "core/hle/service/nvdrv/nvdata.h" 13#include "core/hle/service/nvdrv/nvdata.h"
13#include "core/hle/service/nvdrv/syncpoint_manager.h" 14#include "core/hle/service/nvdrv/syncpoint_manager.h"
14#include "core/hle/service/service.h" 15#include "core/hle/service/service.h"
@@ -154,6 +155,8 @@ private:
154 std::unordered_map<std::string, std::shared_ptr<Devices::nvdevice>> devices; 155 std::unordered_map<std::string, std::shared_ptr<Devices::nvdevice>> devices;
155 156
156 EventInterface events_interface; 157 EventInterface events_interface;
158
159 KernelHelpers::ServiceContext service_context;
157}; 160};
158 161
159/// Registers all NVDRV services with the specified service manager. 162/// Registers all NVDRV services with the specified service manager.
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index e6fba88b2..b3e50433b 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -104,23 +104,22 @@ ServiceFrameworkBase::~ServiceFrameworkBase() {
104void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) { 104void ServiceFrameworkBase::InstallAsService(SM::ServiceManager& service_manager) {
105 const auto guard = LockService(); 105 const auto guard = LockService();
106 106
107 ASSERT(!port_installed); 107 ASSERT(!service_registered);
108 108
109 auto port = service_manager.RegisterService(service_name, max_sessions).Unwrap(); 109 service_manager.RegisterService(service_name, max_sessions, shared_from_this());
110 port->SetSessionHandler(shared_from_this()); 110 service_registered = true;
111 port_installed = true;
112} 111}
113 112
114Kernel::KClientPort& ServiceFrameworkBase::CreatePort() { 113Kernel::KClientPort& ServiceFrameworkBase::CreatePort() {
115 const auto guard = LockService(); 114 const auto guard = LockService();
116 115
117 ASSERT(!port_installed); 116 ASSERT(!service_registered);
118 117
119 auto* port = Kernel::KPort::Create(kernel); 118 auto* port = Kernel::KPort::Create(kernel);
120 port->Initialize(max_sessions, false, service_name); 119 port->Initialize(max_sessions, false, service_name);
121 port->GetServerPort().SetSessionHandler(shared_from_this()); 120 port->GetServerPort().SetSessionHandler(shared_from_this());
122 121
123 port_installed = true; 122 service_registered = true;
124 123
125 return port->GetClientPort(); 124 return port->GetClientPort();
126} 125}
diff --git a/src/core/hle/service/service.h b/src/core/hle/service/service.h
index e078ac176..c9d6b879d 100644
--- a/src/core/hle/service/service.h
+++ b/src/core/hle/service/service.h
@@ -96,6 +96,9 @@ protected:
96 /// System context that the service operates under. 96 /// System context that the service operates under.
97 Core::System& system; 97 Core::System& system;
98 98
99 /// Identifier string used to connect to the service.
100 std::string service_name;
101
99private: 102private:
100 template <typename T> 103 template <typename T>
101 friend class ServiceFramework; 104 friend class ServiceFramework;
@@ -117,14 +120,12 @@ private:
117 void RegisterHandlersBaseTipc(const FunctionInfoBase* functions, std::size_t n); 120 void RegisterHandlersBaseTipc(const FunctionInfoBase* functions, std::size_t n);
118 void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info); 121 void ReportUnimplementedFunction(Kernel::HLERequestContext& ctx, const FunctionInfoBase* info);
119 122
120 /// Identifier string used to connect to the service.
121 std::string service_name;
122 /// Maximum number of concurrent sessions that this service can handle. 123 /// Maximum number of concurrent sessions that this service can handle.
123 u32 max_sessions; 124 u32 max_sessions;
124 125
125 /// Flag to store if a port was already create/installed to detect multiple install attempts, 126 /// Flag to store if a port was already create/installed to detect multiple install attempts,
126 /// which is not supported. 127 /// which is not supported.
127 bool port_installed = false; 128 bool service_registered = false;
128 129
129 /// Function used to safely up-cast pointers to the derived class before invoking a handler. 130 /// Function used to safely up-cast pointers to the derived class before invoking a handler.
130 InvokerFn* handler_invoker; 131 InvokerFn* handler_invoker;
diff --git a/src/core/hle/service/sm/sm.cpp b/src/core/hle/service/sm/sm.cpp
index 15034abed..ae4dc4a75 100644
--- a/src/core/hle/service/sm/sm.cpp
+++ b/src/core/hle/service/sm/sm.cpp
@@ -4,6 +4,7 @@
4 4
5#include <tuple> 5#include <tuple>
6#include "common/assert.h" 6#include "common/assert.h"
7#include "common/scope_exit.h"
7#include "core/core.h" 8#include "core/core.h"
8#include "core/hle/ipc_helpers.h" 9#include "core/hle/ipc_helpers.h"
9#include "core/hle/kernel/k_client_port.h" 10#include "core/hle/kernel/k_client_port.h"
@@ -40,17 +41,13 @@ static ResultCode ValidateServiceName(const std::string& name) {
40} 41}
41 42
42Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) { 43Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) {
43 ASSERT(self.sm_interface.expired()); 44 self.sm_interface = std::make_shared<SM>(self, system);
44
45 auto sm = std::make_shared<SM>(self, system);
46 self.sm_interface = sm;
47 self.controller_interface = std::make_unique<Controller>(system); 45 self.controller_interface = std::make_unique<Controller>(system);
48 46 return self.sm_interface->CreatePort();
49 return sm->CreatePort();
50} 47}
51 48
52ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name, 49ResultCode ServiceManager::RegisterService(std::string name, u32 max_sessions,
53 u32 max_sessions) { 50 Kernel::SessionRequestHandlerPtr handler) {
54 51
55 CASCADE_CODE(ValidateServiceName(name)); 52 CASCADE_CODE(ValidateServiceName(name));
56 53
@@ -59,12 +56,9 @@ ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name
59 return ERR_ALREADY_REGISTERED; 56 return ERR_ALREADY_REGISTERED;
60 } 57 }
61 58
62 auto* port = Kernel::KPort::Create(kernel); 59 registered_services.emplace(std::move(name), handler);
63 port->Initialize(max_sessions, false, name);
64 60
65 registered_services.emplace(std::move(name), port); 61 return ResultSuccess;
66
67 return MakeResult(&port->GetServerPort());
68} 62}
69 63
70ResultCode ServiceManager::UnregisterService(const std::string& name) { 64ResultCode ServiceManager::UnregisterService(const std::string& name) {
@@ -76,14 +70,11 @@ ResultCode ServiceManager::UnregisterService(const std::string& name) {
76 return ERR_SERVICE_NOT_REGISTERED; 70 return ERR_SERVICE_NOT_REGISTERED;
77 } 71 }
78 72
79 iter->second->Close();
80
81 registered_services.erase(iter); 73 registered_services.erase(iter);
82 return ResultSuccess; 74 return ResultSuccess;
83} 75}
84 76
85ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) { 77ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name) {
86
87 CASCADE_CODE(ValidateServiceName(name)); 78 CASCADE_CODE(ValidateServiceName(name));
88 auto it = registered_services.find(name); 79 auto it = registered_services.find(name);
89 if (it == registered_services.end()) { 80 if (it == registered_services.end()) {
@@ -91,10 +82,13 @@ ResultVal<Kernel::KPort*> ServiceManager::GetServicePort(const std::string& name
91 return ERR_SERVICE_NOT_REGISTERED; 82 return ERR_SERVICE_NOT_REGISTERED;
92 } 83 }
93 84
94 return MakeResult(it->second); 85 auto* port = Kernel::KPort::Create(kernel);
95} 86 port->Initialize(ServerSessionCountMax, false, name);
87 auto handler = it->second;
88 port->GetServerPort().SetSessionHandler(std::move(handler));
96 89
97SM::~SM() = default; 90 return MakeResult(port);
91}
98 92
99/** 93/**
100 * SM::Initialize service function 94 * SM::Initialize service function
@@ -156,11 +150,15 @@ ResultVal<Kernel::KClientSession*> SM::GetServiceImpl(Kernel::HLERequestContext&
156 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, port_result.Code().raw); 150 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, port_result.Code().raw);
157 return port_result.Code(); 151 return port_result.Code();
158 } 152 }
159 auto& port = port_result.Unwrap()->GetClientPort(); 153 auto& port = port_result.Unwrap();
154 SCOPE_EXIT({ port->GetClientPort().Close(); });
155
156 server_ports.emplace_back(&port->GetServerPort());
160 157
161 // Create a new session. 158 // Create a new session.
162 Kernel::KClientSession* session{}; 159 Kernel::KClientSession* session{};
163 if (const auto result = port.CreateSession(std::addressof(session)); result.IsError()) { 160 if (const auto result = port->GetClientPort().CreateSession(std::addressof(session));
161 result.IsError()) {
164 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.raw); 162 LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.raw);
165 return result; 163 return result;
166 } 164 }
@@ -180,20 +178,21 @@ void SM::RegisterService(Kernel::HLERequestContext& ctx) {
180 LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name, 178 LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
181 max_session_count, is_light); 179 max_session_count, is_light);
182 180
183 auto handle = service_manager.RegisterService(name, max_session_count); 181 if (const auto result = service_manager.RegisterService(name, max_session_count, nullptr);
184 if (handle.Failed()) { 182 result.IsError()) {
185 LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}", 183 LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}", result.raw);
186 handle.Code().raw);
187 IPC::ResponseBuilder rb{ctx, 2}; 184 IPC::ResponseBuilder rb{ctx, 2};
188 rb.Push(handle.Code()); 185 rb.Push(result);
189 return; 186 return;
190 } 187 }
191 188
192 IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; 189 auto* port = Kernel::KPort::Create(kernel);
193 rb.Push(handle.Code()); 190 port->Initialize(ServerSessionCountMax, is_light, name);
191 SCOPE_EXIT({ port->GetClientPort().Close(); });
194 192
195 auto server_port = handle.Unwrap(); 193 IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
196 rb.PushMoveObjects(server_port); 194 rb.Push(ResultSuccess);
195 rb.PushMoveObjects(port->GetServerPort());
197} 196}
198 197
199void SM::UnregisterService(Kernel::HLERequestContext& ctx) { 198void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
@@ -225,4 +224,10 @@ SM::SM(ServiceManager& service_manager_, Core::System& system_)
225 }); 224 });
226} 225}
227 226
227SM::~SM() {
228 for (auto& server_port : server_ports) {
229 server_port->Close();
230 }
231}
232
228} // namespace Service::SM 233} // namespace Service::SM
diff --git a/src/core/hle/service/sm/sm.h b/src/core/hle/service/sm/sm.h
index ea37f11d4..068c78588 100644
--- a/src/core/hle/service/sm/sm.h
+++ b/src/core/hle/service/sm/sm.h
@@ -49,6 +49,7 @@ private:
49 ServiceManager& service_manager; 49 ServiceManager& service_manager;
50 bool is_initialized{}; 50 bool is_initialized{};
51 Kernel::KernelCore& kernel; 51 Kernel::KernelCore& kernel;
52 std::vector<Kernel::KServerPort*> server_ports;
52}; 53};
53 54
54class ServiceManager { 55class ServiceManager {
@@ -58,7 +59,8 @@ public:
58 explicit ServiceManager(Kernel::KernelCore& kernel_); 59 explicit ServiceManager(Kernel::KernelCore& kernel_);
59 ~ServiceManager(); 60 ~ServiceManager();
60 61
61 ResultVal<Kernel::KServerPort*> RegisterService(std::string name, u32 max_sessions); 62 ResultCode RegisterService(std::string name, u32 max_sessions,
63 Kernel::SessionRequestHandlerPtr handler);
62 ResultCode UnregisterService(const std::string& name); 64 ResultCode UnregisterService(const std::string& name);
63 ResultVal<Kernel::KPort*> GetServicePort(const std::string& name); 65 ResultVal<Kernel::KPort*> GetServicePort(const std::string& name);
64 66
@@ -69,21 +71,17 @@ public:
69 LOG_DEBUG(Service, "Can't find service: {}", service_name); 71 LOG_DEBUG(Service, "Can't find service: {}", service_name);
70 return nullptr; 72 return nullptr;
71 } 73 }
72 auto* port = service->second; 74 return std::static_pointer_cast<T>(service->second);
73 if (port == nullptr) {
74 return nullptr;
75 }
76 return std::static_pointer_cast<T>(port->GetServerPort().GetSessionRequestHandler());
77 } 75 }
78 76
79 void InvokeControlRequest(Kernel::HLERequestContext& context); 77 void InvokeControlRequest(Kernel::HLERequestContext& context);
80 78
81private: 79private:
82 std::weak_ptr<SM> sm_interface; 80 std::shared_ptr<SM> sm_interface;
83 std::unique_ptr<Controller> controller_interface; 81 std::unique_ptr<Controller> controller_interface;
84 82
85 /// Map of registered services, retrieved using GetServicePort. 83 /// Map of registered services, retrieved using GetServicePort.
86 std::unordered_map<std::string, Kernel::KPort*> registered_services; 84 std::unordered_map<std::string, Kernel::SessionRequestHandlerPtr> registered_services;
87 85
88 /// Kernel context 86 /// Kernel context
89 Kernel::KernelCore& kernel; 87 Kernel::KernelCore& kernel;
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 1656b85fb..70a0ba09c 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -115,6 +115,41 @@ public:
115 return state.buttons.at(button); 115 return state.buttons.at(button);
116 } 116 }
117 117
118 bool ToggleButton(int button) {
119 std::lock_guard lock{mutex};
120
121 if (!state.toggle_buttons.contains(button) || !state.lock_buttons.contains(button)) {
122 state.toggle_buttons.insert_or_assign(button, false);
123 state.lock_buttons.insert_or_assign(button, false);
124 }
125
126 const bool button_state = state.toggle_buttons.at(button);
127 const bool button_lock = state.lock_buttons.at(button);
128
129 if (button_lock) {
130 return button_state;
131 }
132
133 state.lock_buttons.insert_or_assign(button, true);
134
135 if (button_state) {
136 state.toggle_buttons.insert_or_assign(button, false);
137 } else {
138 state.toggle_buttons.insert_or_assign(button, true);
139 }
140
141 return !button_state;
142 }
143
144 bool UnlockButton(int button) {
145 std::lock_guard lock{mutex};
146 if (!state.toggle_buttons.contains(button)) {
147 return false;
148 }
149 state.lock_buttons.insert_or_assign(button, false);
150 return state.toggle_buttons.at(button);
151 }
152
118 void SetAxis(int axis, Sint16 value) { 153 void SetAxis(int axis, Sint16 value) {
119 std::lock_guard lock{mutex}; 154 std::lock_guard lock{mutex};
120 state.axes.insert_or_assign(axis, value); 155 state.axes.insert_or_assign(axis, value);
@@ -241,6 +276,8 @@ public:
241private: 276private:
242 struct State { 277 struct State {
243 std::unordered_map<int, bool> buttons; 278 std::unordered_map<int, bool> buttons;
279 std::unordered_map<int, bool> toggle_buttons{};
280 std::unordered_map<int, bool> lock_buttons{};
244 std::unordered_map<int, Sint16> axes; 281 std::unordered_map<int, Sint16> axes;
245 std::unordered_map<int, Uint8> hats; 282 std::unordered_map<int, Uint8> hats;
246 } state; 283 } state;
@@ -402,16 +439,25 @@ void SDLState::CloseJoysticks() {
402 439
403class SDLButton final : public Input::ButtonDevice { 440class SDLButton final : public Input::ButtonDevice {
404public: 441public:
405 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_) 442 explicit SDLButton(std::shared_ptr<SDLJoystick> joystick_, int button_, bool toggle_)
406 : joystick(std::move(joystick_)), button(button_) {} 443 : joystick(std::move(joystick_)), button(button_), toggle(toggle_) {}
407 444
408 bool GetStatus() const override { 445 bool GetStatus() const override {
409 return joystick->GetButton(button); 446 const bool button_state = joystick->GetButton(button);
447 if (!toggle) {
448 return button_state;
449 }
450
451 if (button_state) {
452 return joystick->ToggleButton(button);
453 }
454 return joystick->UnlockButton(button);
410 } 455 }
411 456
412private: 457private:
413 std::shared_ptr<SDLJoystick> joystick; 458 std::shared_ptr<SDLJoystick> joystick;
414 int button; 459 int button;
460 bool toggle;
415}; 461};
416 462
417class SDLDirectionButton final : public Input::ButtonDevice { 463class SDLDirectionButton final : public Input::ButtonDevice {
@@ -635,6 +681,7 @@ public:
635 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override { 681 std::unique_ptr<Input::ButtonDevice> Create(const Common::ParamPackage& params) override {
636 const std::string guid = params.Get("guid", "0"); 682 const std::string guid = params.Get("guid", "0");
637 const int port = params.Get("port", 0); 683 const int port = params.Get("port", 0);
684 const auto toggle = params.Get("toggle", false);
638 685
639 auto joystick = state.GetSDLJoystickByGUID(guid, port); 686 auto joystick = state.GetSDLJoystickByGUID(guid, port);
640 687
@@ -660,7 +707,8 @@ public:
660 707
661 if (params.Has("axis")) { 708 if (params.Has("axis")) {
662 const int axis = params.Get("axis", 0); 709 const int axis = params.Get("axis", 0);
663 const float threshold = params.Get("threshold", 0.5f); 710 // Convert range from (0.0, 1.0) to (-1.0, 1.0)
711 const float threshold = (params.Get("threshold", 0.5f) - 0.5f) * 2.0f;
664 const std::string direction_name = params.Get("direction", ""); 712 const std::string direction_name = params.Get("direction", "");
665 bool trigger_if_greater; 713 bool trigger_if_greater;
666 if (direction_name == "+") { 714 if (direction_name == "+") {
@@ -679,7 +727,7 @@ public:
679 const int button = params.Get("button", 0); 727 const int button = params.Get("button", 0);
680 // This is necessary so accessing GetButton with button won't crash 728 // This is necessary so accessing GetButton with button won't crash
681 joystick->SetButton(button, false); 729 joystick->SetButton(button, false);
682 return std::make_unique<SDLButton>(joystick, button); 730 return std::make_unique<SDLButton>(joystick, button, toggle);
683 } 731 }
684 732
685private: 733private:
@@ -933,12 +981,11 @@ Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid
933 params.Set("port", port); 981 params.Set("port", port);
934 params.Set("guid", std::move(guid)); 982 params.Set("guid", std::move(guid));
935 params.Set("axis", axis); 983 params.Set("axis", axis);
984 params.Set("threshold", "0.5");
936 if (value > 0) { 985 if (value > 0) {
937 params.Set("direction", "+"); 986 params.Set("direction", "+");
938 params.Set("threshold", "0.5");
939 } else { 987 } else {
940 params.Set("direction", "-"); 988 params.Set("direction", "-");
941 params.Set("threshold", "-0.5");
942 } 989 }
943 return params; 990 return params;
944} 991}
diff --git a/src/video_core/renderer_vulkan/vk_texture_cache.cpp b/src/video_core/renderer_vulkan/vk_texture_cache.cpp
index fd01c902c..88ccf96f5 100644
--- a/src/video_core/renderer_vulkan/vk_texture_cache.cpp
+++ b/src/video_core/renderer_vulkan/vk_texture_cache.cpp
@@ -765,7 +765,7 @@ void TextureCacheRuntime::CopyImage(Image& dst, Image& src,
765 dst_range.AddLayers(copy.dstSubresource); 765 dst_range.AddLayers(copy.dstSubresource);
766 src_range.AddLayers(copy.srcSubresource); 766 src_range.AddLayers(copy.srcSubresource);
767 } 767 }
768 const std::array read_barriers{ 768 const std::array pre_barriers{
769 VkImageMemoryBarrier{ 769 VkImageMemoryBarrier{
770 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, 770 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
771 .pNext = nullptr, 771 .pNext = nullptr,
@@ -774,7 +774,7 @@ void TextureCacheRuntime::CopyImage(Image& dst, Image& src,
774 VK_ACCESS_TRANSFER_WRITE_BIT, 774 VK_ACCESS_TRANSFER_WRITE_BIT,
775 .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT, 775 .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
776 .oldLayout = VK_IMAGE_LAYOUT_GENERAL, 776 .oldLayout = VK_IMAGE_LAYOUT_GENERAL,
777 .newLayout = VK_IMAGE_LAYOUT_GENERAL, 777 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
778 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, 778 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
779 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, 779 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
780 .image = src_image, 780 .image = src_image,
@@ -795,29 +795,43 @@ void TextureCacheRuntime::CopyImage(Image& dst, Image& src,
795 .subresourceRange = dst_range.SubresourceRange(aspect_mask), 795 .subresourceRange = dst_range.SubresourceRange(aspect_mask),
796 }, 796 },
797 }; 797 };
798 const VkImageMemoryBarrier write_barrier{ 798 const std::array post_barriers{
799 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, 799 VkImageMemoryBarrier{
800 .pNext = nullptr, 800 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
801 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, 801 .pNext = nullptr,
802 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT | 802 .srcAccessMask = 0,
803 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | 803 .dstAccessMask = 0,
804 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | 804 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
805 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | 805 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
806 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | 806 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
807 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, 807 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
808 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 808 .image = src_image,
809 .newLayout = VK_IMAGE_LAYOUT_GENERAL, 809 .subresourceRange = src_range.SubresourceRange(aspect_mask),
810 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, 810 },
811 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, 811 VkImageMemoryBarrier{
812 .image = dst_image, 812 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER,
813 .subresourceRange = dst_range.SubresourceRange(aspect_mask), 813 .pNext = nullptr,
814 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
815 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT |
816 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
817 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
818 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
819 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
820 VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
821 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
822 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
823 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
824 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
825 .image = dst_image,
826 .subresourceRange = dst_range.SubresourceRange(aspect_mask),
827 },
814 }; 828 };
815 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 829 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
816 0, {}, {}, read_barriers); 830 0, {}, {}, pre_barriers);
817 cmdbuf.CopyImage(src_image, VK_IMAGE_LAYOUT_GENERAL, dst_image, 831 cmdbuf.CopyImage(src_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_image,
818 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_copies); 832 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_copies);
819 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 833 cmdbuf.PipelineBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
820 0, write_barrier); 834 0, {}, {}, post_barriers);
821 }); 835 });
822} 836}
823 837
diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp
index d5d624b96..6b9bd05f1 100644
--- a/src/yuzu/configuration/configure_input_player.cpp
+++ b/src/yuzu/configuration/configure_input_player.cpp
@@ -149,8 +149,9 @@ QString ButtonToText(const Common::ParamPackage& param) {
149 149
150 if (param.Has("button")) { 150 if (param.Has("button")) {
151 const QString button_str = QString::fromStdString(param.Get("button", "")); 151 const QString button_str = QString::fromStdString(param.Get("button", ""));
152 const QString toggle = QString::fromStdString(param.Get("toggle", false) ? "~" : "");
152 153
153 return QObject::tr("Button %1").arg(button_str); 154 return QObject::tr("%1Button %2").arg(toggle, button_str);
154 } 155 }
155 156
156 if (param.Has("motion")) { 157 if (param.Has("motion")) {
@@ -313,6 +314,24 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
313 buttons_param[button_id].Set("toggle", toggle_value); 314 buttons_param[button_id].Set("toggle", toggle_value);
314 button_map[button_id]->setText(ButtonToText(buttons_param[button_id])); 315 button_map[button_id]->setText(ButtonToText(buttons_param[button_id]));
315 }); 316 });
317 if (buttons_param[button_id].Has("threshold")) {
318 context_menu.addAction(tr("Set threshold"), [&] {
319 const int button_threshold = static_cast<int>(
320 buttons_param[button_id].Get("threshold", 0.5f) * 100.0f);
321 const int new_threshold = QInputDialog::getInt(
322 this, tr("Set threshold"), tr("Choose a value between 0% and 100%"),
323 button_threshold, 0, 100);
324 buttons_param[button_id].Set("threshold", new_threshold / 100.0f);
325
326 if (button_id == Settings::NativeButton::ZL) {
327 ui->sliderZLThreshold->setValue(new_threshold);
328 }
329 if (button_id == Settings::NativeButton::ZR) {
330 ui->sliderZRThreshold->setValue(new_threshold);
331 }
332 });
333 }
334
316 context_menu.exec(button_map[button_id]->mapToGlobal(menu_location)); 335 context_menu.exec(button_map[button_id]->mapToGlobal(menu_location));
317 ui->controllerFrame->SetPlayerInput(player_index, buttons_param, analogs_param); 336 ui->controllerFrame->SetPlayerInput(player_index, buttons_param, analogs_param);
318 }); 337 });
@@ -341,6 +360,20 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i
341 }); 360 });
342 } 361 }
343 362
363 connect(ui->sliderZLThreshold, &QSlider::valueChanged, [=, this] {
364 if (buttons_param[Settings::NativeButton::ZL].Has("threshold")) {
365 const auto slider_value = ui->sliderZLThreshold->value();
366 buttons_param[Settings::NativeButton::ZL].Set("threshold", slider_value / 100.0f);
367 }
368 });
369
370 connect(ui->sliderZRThreshold, &QSlider::valueChanged, [=, this] {
371 if (buttons_param[Settings::NativeButton::ZR].Has("threshold")) {
372 const auto slider_value = ui->sliderZRThreshold->value();
373 buttons_param[Settings::NativeButton::ZR].Set("threshold", slider_value / 100.0f);
374 }
375 });
376
344 for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) { 377 for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
345 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) { 378 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
346 auto* const analog_button = analog_map_buttons[analog_id][sub_button_id]; 379 auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
@@ -849,6 +882,18 @@ void ConfigureInputPlayer::UpdateUI() {
849 button_map[button]->setText(ButtonToText(buttons_param[button])); 882 button_map[button]->setText(ButtonToText(buttons_param[button]));
850 } 883 }
851 884
885 if (buttons_param[Settings::NativeButton::ZL].Has("threshold")) {
886 const int button_threshold = static_cast<int>(
887 buttons_param[Settings::NativeButton::ZL].Get("threshold", 0.5f) * 100.0f);
888 ui->sliderZLThreshold->setValue(button_threshold);
889 }
890
891 if (buttons_param[Settings::NativeButton::ZR].Has("threshold")) {
892 const int button_threshold = static_cast<int>(
893 buttons_param[Settings::NativeButton::ZR].Get("threshold", 0.5f) * 100.0f);
894 ui->sliderZRThreshold->setValue(button_threshold);
895 }
896
852 for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) { 897 for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
853 motion_map[motion_id]->setText(ButtonToText(motions_param[motion_id])); 898 motion_map[motion_id]->setText(ButtonToText(motions_param[motion_id]));
854 } 899 }
diff --git a/src/yuzu/configuration/configure_input_player.ui b/src/yuzu/configuration/configure_input_player.ui
index e76aa484f..e7433912b 100644
--- a/src/yuzu/configuration/configure_input_player.ui
+++ b/src/yuzu/configuration/configure_input_player.ui
@@ -1334,6 +1334,12 @@
1334 </item> 1334 </item>
1335 <item> 1335 <item>
1336 <widget class="QGroupBox" name="buttonShoulderButtonsButtonZLGroup"> 1336 <widget class="QGroupBox" name="buttonShoulderButtonsButtonZLGroup">
1337 <property name="sizePolicy">
1338 <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
1339 <horstretch>0</horstretch>
1340 <verstretch>0</verstretch>
1341 </sizepolicy>
1342 </property>
1337 <property name="title"> 1343 <property name="title">
1338 <string>ZL</string> 1344 <string>ZL</string>
1339 </property> 1345 </property>
@@ -1378,6 +1384,22 @@
1378 </property> 1384 </property>
1379 </widget> 1385 </widget>
1380 </item> 1386 </item>
1387 <item>
1388 <widget class="QSlider" name="sliderZLThreshold">
1389 <property name="maximumSize">
1390 <size>
1391 <width>70</width>
1392 <height>15</height>
1393 </size>
1394 </property>
1395 <property name="maximum">
1396 <number>100</number>
1397 </property>
1398 <property name="orientation">
1399 <enum>Qt::Horizontal</enum>
1400 </property>
1401 </widget>
1402 </item>
1381 </layout> 1403 </layout>
1382 </widget> 1404 </widget>
1383 </item> 1405 </item>
@@ -1759,6 +1781,12 @@
1759 </item> 1781 </item>
1760 <item> 1782 <item>
1761 <widget class="QGroupBox" name="buttonShoulderButtonsZRGroup"> 1783 <widget class="QGroupBox" name="buttonShoulderButtonsZRGroup">
1784 <property name="sizePolicy">
1785 <sizepolicy hsizetype="Maximum" vsizetype="Preferred">
1786 <horstretch>0</horstretch>
1787 <verstretch>0</verstretch>
1788 </sizepolicy>
1789 </property>
1762 <property name="title"> 1790 <property name="title">
1763 <string>ZR</string> 1791 <string>ZR</string>
1764 </property> 1792 </property>
@@ -1803,6 +1831,22 @@
1803 </property> 1831 </property>
1804 </widget> 1832 </widget>
1805 </item> 1833 </item>
1834 <item>
1835 <widget class="QSlider" name="sliderZRThreshold">
1836 <property name="maximumSize">
1837 <size>
1838 <width>70</width>
1839 <height>15</height>
1840 </size>
1841 </property>
1842 <property name="maximum">
1843 <number>100</number>
1844 </property>
1845 <property name="orientation">
1846 <enum>Qt::Horizontal</enum>
1847 </property>
1848 </widget>
1849 </item>
1806 </layout> 1850 </layout>
1807 </widget> 1851 </widget>
1808 </item> 1852 </item>
diff --git a/src/yuzu_cmd/config.cpp b/src/yuzu_cmd/config.cpp
index b18056baf..3e22fee37 100644
--- a/src/yuzu_cmd/config.cpp
+++ b/src/yuzu_cmd/config.cpp
@@ -446,6 +446,7 @@ void Config::ReadValues() {
446 ReadSetting("Renderer", Settings::values.renderer_debug); 446 ReadSetting("Renderer", Settings::values.renderer_debug);
447 ReadSetting("Renderer", Settings::values.vulkan_device); 447 ReadSetting("Renderer", Settings::values.vulkan_device);
448 448
449 ReadSetting("Renderer", Settings::values.fullscreen_mode);
449 ReadSetting("Renderer", Settings::values.aspect_ratio); 450 ReadSetting("Renderer", Settings::values.aspect_ratio);
450 ReadSetting("Renderer", Settings::values.max_anisotropy); 451 ReadSetting("Renderer", Settings::values.max_anisotropy);
451 ReadSetting("Renderer", Settings::values.use_frame_limit); 452 ReadSetting("Renderer", Settings::values.use_frame_limit);
diff --git a/src/yuzu_cmd/default_ini.h b/src/yuzu_cmd/default_ini.h
index b362f10b4..88d33ecab 100644
--- a/src/yuzu_cmd/default_ini.h
+++ b/src/yuzu_cmd/default_ini.h
@@ -224,6 +224,10 @@ debug =
224# Which Vulkan physical device to use (defaults to 0) 224# Which Vulkan physical device to use (defaults to 0)
225vulkan_device = 225vulkan_device =
226 226
227# Whether to use fullscreen or borderless window mode
228# 0 (Windows default): Borderless window, 1 (All other default): Exclusive fullscreen
229fullscreen_mode =
230
227# Aspect ratio 231# Aspect ratio
228# 0: Default (16:9), 1: Force 4:3, 2: Force 21:9, 3: Stretch to Window 232# 0: Default (16:9), 1: Force 4:3, 2: Force 21:9, 3: Stretch to Window
229aspect_ratio = 233aspect_ratio =
diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp
index 896181f0b..353e51ea7 100644
--- a/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp
+++ b/src/yuzu_cmd/emu_window/emu_window_sdl2.cpp
@@ -6,6 +6,7 @@
6 6
7#include "common/logging/log.h" 7#include "common/logging/log.h"
8#include "common/scm_rev.h" 8#include "common/scm_rev.h"
9#include "common/settings.h"
9#include "core/core.h" 10#include "core/core.h"
10#include "core/perf_stats.h" 11#include "core/perf_stats.h"
11#include "input_common/keyboard.h" 12#include "input_common/keyboard.h"
@@ -122,24 +123,37 @@ void EmuWindow_SDL2::OnResize() {
122} 123}
123 124
124void EmuWindow_SDL2::Fullscreen() { 125void EmuWindow_SDL2::Fullscreen() {
125 if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) { 126 switch (Settings::values.fullscreen_mode.GetValue()) {
126 return; 127 case 1: // Exclusive fullscreen
127 } 128 // Set window size to render size before entering fullscreen -- SDL does not resize to
128 129 // display dimensions in this mode.
129 LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError()); 130 // TODO: Multiply the window size by resolution_factor (for both docked modes)
131 if (Settings::values.use_docked_mode) {
132 SDL_SetWindowSize(render_window, Layout::ScreenDocked::Width,
133 Layout::ScreenDocked::Height);
134 }
130 135
131 // Try a different fullscreening method 136 if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN) == 0) {
132 LOG_INFO(Frontend, "Attempting to use borderless fullscreen..."); 137 return;
133 if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) { 138 }
134 return;
135 }
136 139
137 LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError()); 140 LOG_ERROR(Frontend, "Fullscreening failed: {}", SDL_GetError());
141 LOG_INFO(Frontend, "Attempting to use borderless fullscreen...");
142 [[fallthrough]];
143 case 0: // Borderless window
144 if (SDL_SetWindowFullscreen(render_window, SDL_WINDOW_FULLSCREEN_DESKTOP) == 0) {
145 return;
146 }
138 147
139 // Fallback algorithm: Maximise window. 148 LOG_ERROR(Frontend, "Borderless fullscreening failed: {}", SDL_GetError());
140 // Works on all systems (unless something is seriously wrong), so no fallback for this one. 149 [[fallthrough]];
141 LOG_INFO(Frontend, "Falling back on a maximised window..."); 150 default:
142 SDL_MaximizeWindow(render_window); 151 // Fallback algorithm: Maximise window.
152 // Works on all systems (unless something is seriously wrong), so no fallback for this one.
153 LOG_INFO(Frontend, "Falling back on a maximised window...");
154 SDL_MaximizeWindow(render_window);
155 break;
156 }
143} 157}
144 158
145void EmuWindow_SDL2::WaitEvent() { 159void EmuWindow_SDL2::WaitEvent() {
diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.cpp b/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.cpp
index 152e56db8..d1473dbab 100644
--- a/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.cpp
+++ b/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.cpp
@@ -24,7 +24,7 @@
24#include <SDL.h> 24#include <SDL.h>
25#include <SDL_syswm.h> 25#include <SDL_syswm.h>
26 26
27EmuWindow_SDL2_VK::EmuWindow_SDL2_VK(InputCommon::InputSubsystem* input_subsystem) 27EmuWindow_SDL2_VK::EmuWindow_SDL2_VK(InputCommon::InputSubsystem* input_subsystem, bool fullscreen)
28 : EmuWindow_SDL2{input_subsystem} { 28 : EmuWindow_SDL2{input_subsystem} {
29 const std::string window_title = fmt::format("yuzu {} | {}-{} (Vulkan)", Common::g_build_name, 29 const std::string window_title = fmt::format("yuzu {} | {}-{} (Vulkan)", Common::g_build_name,
30 Common::g_scm_branch, Common::g_scm_desc); 30 Common::g_scm_branch, Common::g_scm_desc);
@@ -42,6 +42,10 @@ EmuWindow_SDL2_VK::EmuWindow_SDL2_VK(InputCommon::InputSubsystem* input_subsyste
42 42
43 SetWindowIcon(); 43 SetWindowIcon();
44 44
45 if (fullscreen) {
46 Fullscreen();
47 }
48
45 switch (wm.subsystem) { 49 switch (wm.subsystem) {
46#ifdef SDL_VIDEO_DRIVER_WINDOWS 50#ifdef SDL_VIDEO_DRIVER_WINDOWS
47 case SDL_SYSWM_TYPE::SDL_SYSWM_WINDOWS: 51 case SDL_SYSWM_TYPE::SDL_SYSWM_WINDOWS:
diff --git a/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.h b/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.h
index bdfdc3c6f..de53844f0 100644
--- a/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.h
+++ b/src/yuzu_cmd/emu_window/emu_window_sdl2_vk.h
@@ -19,7 +19,7 @@ class InputSubsystem;
19 19
20class EmuWindow_SDL2_VK final : public EmuWindow_SDL2 { 20class EmuWindow_SDL2_VK final : public EmuWindow_SDL2 {
21public: 21public:
22 explicit EmuWindow_SDL2_VK(InputCommon::InputSubsystem* input_subsystem); 22 explicit EmuWindow_SDL2_VK(InputCommon::InputSubsystem* input_subsystem, bool fullscreen);
23 ~EmuWindow_SDL2_VK() override; 23 ~EmuWindow_SDL2_VK() override;
24 24
25 std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override; 25 std::unique_ptr<Core::Frontend::GraphicsContext> CreateSharedContext() const override;
diff --git a/src/yuzu_cmd/yuzu.cpp b/src/yuzu_cmd/yuzu.cpp
index 9607cdcb1..ac4ea88d3 100644
--- a/src/yuzu_cmd/yuzu.cpp
+++ b/src/yuzu_cmd/yuzu.cpp
@@ -175,7 +175,7 @@ int main(int argc, char** argv) {
175 emu_window = std::make_unique<EmuWindow_SDL2_GL>(&input_subsystem, fullscreen); 175 emu_window = std::make_unique<EmuWindow_SDL2_GL>(&input_subsystem, fullscreen);
176 break; 176 break;
177 case Settings::RendererBackend::Vulkan: 177 case Settings::RendererBackend::Vulkan:
178 emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem); 178 emu_window = std::make_unique<EmuWindow_SDL2_VK>(&input_subsystem, fullscreen);
179 break; 179 break;
180 } 180 }
181 181