From 290d6b2b773e9729c38495ec8c1291b40678bc31 Mon Sep 17 00:00:00 2001
From: Jetro Jormalainen <jje-wine@jv.jetro.fi>
Date: Tue, 30 Apr 2019 09:20:20 +1000
Subject: [PATCH 1/3] dinput: Load users Joystick mappings.

---
 dlls/dinput/device.c         |  2 +-
 dlls/dinput/device_private.h |  2 ++
 dlls/dinput/joystick.c       | 25 +++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/dlls/dinput/device.c b/dlls/dinput/device.c
index 28329d03b5..d1bf934a82 100644
--- a/dlls/dinput/device.c
+++ b/dlls/dinput/device.c
@@ -714,7 +714,7 @@ static HRESULT save_mapping_settings(IDirectInputDevice8W *iface, LPDIACTIONFORM
     return DI_OK;
 }
 
-static BOOL load_mapping_settings(IDirectInputDeviceImpl *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username)
+BOOL load_mapping_settings(IDirectInputDeviceImpl *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username)
 {
     HKEY hkey;
     WCHAR *guid_str;
diff --git a/dlls/dinput/device_private.h b/dlls/dinput/device_private.h
index 27e9c26286..ea794d7396 100644
--- a/dlls/dinput/device_private.h
+++ b/dlls/dinput/device_private.h
@@ -125,6 +125,8 @@ extern const char *_dump_dinput_GUID(const GUID *guid)  DECLSPEC_HIDDEN;
 
 extern LPDIOBJECTDATAFORMAT dataformat_to_odf_by_type(LPCDIDATAFORMAT df, int n, DWORD type)   DECLSPEC_HIDDEN;
 
+extern BOOL load_mapping_settings(IDirectInputDeviceImpl *This, LPDIACTIONFORMATW lpdiaf, const WCHAR *username) DECLSPEC_HIDDEN;
+
 extern HRESULT _build_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, DWORD devMask, LPCDIDATAFORMAT df)  DECLSPEC_HIDDEN;
 extern HRESULT _set_action_map(LPDIRECTINPUTDEVICE8W iface, LPDIACTIONFORMATW lpdiaf, LPCWSTR lpszUserName, DWORD dwFlags, LPCDIDATAFORMAT df) DECLSPEC_HIDDEN;
 
diff --git a/dlls/dinput/joystick.c b/dlls/dinput/joystick.c
index 2220b5d576..8341e0313a 100644
--- a/dlls/dinput/joystick.c
+++ b/dlls/dinput/joystick.c
@@ -28,8 +28,10 @@
 
 #include <stdio.h>
 
+#include "device_private.h"
 #include "joystick_private.h"
 #include "wine/debug.h"
+#include "wine/heap.h"
 #include "winreg.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
@@ -825,9 +827,32 @@ HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
     unsigned int i, j;
     BOOL has_actions = FALSE;
+    WCHAR *username;
+    DWORD size;
+    BOOL load_success = FALSE;
 
     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
 
+    /* Unless asked the contrary by these flags, try to load a previous mapping */
+    if (!(dwFlags & DIDBAM_HWDEFAULTS))
+    {
+        if (!lpszUserName)
+            GetUserNameW(NULL, &size);
+        else
+            size = lstrlenW(lpszUserName) + 1;
+
+        username = heap_alloc(size * sizeof(WCHAR));
+        if (!lpszUserName)
+            GetUserNameW(username, &size);
+        else
+            lstrcpynW(username, lpszUserName, size);
+
+        load_success = load_mapping_settings((IDirectInputDeviceImpl *) This, lpdiaf, username);
+        heap_free(username);
+    }
+
+    if (load_success) return DI_OK;
+
     for (i=0; i < lpdiaf->dwNumActions; i++)
     {
         DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
-- 
2.17.1

