From ae723f5251402283ea8e32bb932e0d62659b85dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?L=C3=A9o=20Lam?= <leo@innovatetechnologi.es>
Date: Tue, 20 Sep 2016 19:33:08 +0200
Subject: [PATCH] IPC_HLE/stm: Implement STM_UnregisterStateEvent

---
 .../Core/IPC_HLE/WII_IPC_HLE_Device_stm.cpp   | 32 +++++++++++++------
 .../Core/IPC_HLE/WII_IPC_HLE_Device_stm.h     |  3 --
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.cpp b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.cpp
index 0c1dd43d13..38612a4929 100644
--- a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.cpp
+++ b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.cpp
@@ -4,6 +4,8 @@
 
 #include "Core/IPC_HLE/WII_IPC_HLE_Device_stm.h"
 
+static u32 s_event_hook_address = 0;
+
 IPCCommandResult CWII_IPC_HLE_Device_stm_immediate::Open(u32 command_address, u32 mode)
 {
   INFO_LOG(WII_IPC_STM, "STM immediate: Open");
@@ -37,8 +39,17 @@ IPCCommandResult CWII_IPC_HLE_Device_stm_immediate::IOCtl(u32 command_address)
   switch (parameter)
   {
   case IOCTL_STM_RELEASE_EH:
-    INFO_LOG(WII_IPC_STM, "%s - IOCtl:", GetDeviceName().c_str());
-    INFO_LOG(WII_IPC_STM, "    IOCTL_STM_RELEASE_EH");
+    if (s_event_hook_address == 0)
+    {
+      return_value = FS_ENOENT;
+      break;
+    }
+    Memory::Write_U32(0, Memory::Read_U32(s_event_hook_address + 0x18));
+    Memory::Write_U32(FS_SUCCESS, s_event_hook_address + 4);
+    Memory::Write_U32(IPC_REP_ASYNC, s_event_hook_address);
+    Memory::Write_U32(IPC_CMD_IOCTL, s_event_hook_address + 8);
+    WII_IPC_HLE_Interface::EnqueueReply(s_event_hook_address);
+    s_event_hook_address = 0;
     break;
 
   case IOCTL_STM_HOTRESET:
@@ -87,7 +98,7 @@ IPCCommandResult CWII_IPC_HLE_Device_stm_eventhook::Open(u32 command_address, u3
 
 IPCCommandResult CWII_IPC_HLE_Device_stm_eventhook::Close(u32 command_address, bool force)
 {
-  m_event_hook_address = 0;
+  s_event_hook_address = 0;
 
   INFO_LOG(WII_IPC_STM, "STM eventhook: Close");
   if (!force)
@@ -108,27 +119,28 @@ IPCCommandResult CWII_IPC_HLE_Device_stm_eventhook::IOCtl(u32 command_address)
 
   // IOCTL_STM_EVENTHOOK waits until the reset button or power button
   // is pressed.
-  m_event_hook_address = command_address;
+  s_event_hook_address = command_address;
   return GetNoReply();
 }
 
 void CWII_IPC_HLE_Device_stm_eventhook::ResetButton() const
 {
-  if (!m_Active || m_event_hook_address == 0)
+  if (!m_Active || s_event_hook_address == 0)
   {
     // If the device isn't open, ignore the button press.
     return;
   }
 
   // The reset button returns STM_EVENT_RESET.
-  u32 buffer_out = Memory::Read_U32(m_event_hook_address + 0x18);
+  u32 buffer_out = Memory::Read_U32(s_event_hook_address + 0x18);
   Memory::Write_U32(STM_EVENT_RESET, buffer_out);
 
   // Fill in command buffer.
-  Memory::Write_U32(FS_SUCCESS, m_event_hook_address + 4);
-  Memory::Write_U32(IPC_REP_ASYNC, m_event_hook_address);
-  Memory::Write_U32(IPC_CMD_IOCTL, m_event_hook_address + 8);
+  Memory::Write_U32(FS_SUCCESS, s_event_hook_address + 4);
+  Memory::Write_U32(IPC_REP_ASYNC, s_event_hook_address);
+  Memory::Write_U32(IPC_CMD_IOCTL, s_event_hook_address + 8);
 
   // Generate a reply to the IPC command.
-  WII_IPC_HLE_Interface::EnqueueReply(m_event_hook_address);
+  WII_IPC_HLE_Interface::EnqueueReply(s_event_hook_address);
+  s_event_hook_address = 0;
 }
diff --git a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.h b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.h
index fa9153c25e..a84837bb29 100644
--- a/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.h
+++ b/Source/Core/Core/IPC_HLE/WII_IPC_HLE_Device_stm.h
@@ -60,7 +60,4 @@ public:
   IPCCommandResult IOCtl(u32 command_address) override;
 
   void ResetButton() const;
-
-  // STATE_TO_SAVE
-  u32 m_event_hook_address = 0;
 };