[PATCH 2/6] dinput: Introduce direct_input_device_alloc helper.

Rémi Bernon rbernon at codeweavers.com
Fri May 28 04:41:27 CDT 2021


To factor out device allocation and initialization.

Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---
 dlls/dinput/device.c              | 20 ++++++++++++++++++++
 dlls/dinput/device_private.h      |  4 ++++
 dlls/dinput/joystick_linux.c      | 15 +++------------
 dlls/dinput/joystick_linuxinput.c | 15 ++++-----------
 dlls/dinput/joystick_osx.c        | 15 +++------------
 dlls/dinput/keyboard.c            | 14 ++++----------
 dlls/dinput/mouse.c               | 15 ++++-----------
 7 files changed, 42 insertions(+), 56 deletions(-)

diff --git a/dlls/dinput/device.c b/dlls/dinput/device.c
index 5da58b85126..a5dfc78370c 100644
--- a/dlls/dinput/device.c
+++ b/dlls/dinput/device.c
@@ -1703,3 +1703,23 @@ HRESULT WINAPI IDirectInputDevice8WImpl_GetImageInfo(LPDIRECTINPUTDEVICE8W iface
     
     return DI_OK;
 }
+
+HRESULT direct_input_device_alloc( SIZE_T size, const IDirectInputDevice8WVtbl *vtblw,
+                                   const IDirectInputDevice8AVtbl *vtbla, const GUID *guid,
+                                   IDirectInputImpl *dinput, void **out )
+{
+    IDirectInputDeviceImpl *This;
+
+    if (!(This = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ))) return DIERR_OUTOFMEMORY;
+
+    This->IDirectInputDevice8A_iface.lpVtbl = vtbla;
+    This->IDirectInputDevice8W_iface.lpVtbl = vtblw;
+    This->ref = 1;
+    This->guid = *guid;
+    InitializeCriticalSection( &This->crit );
+    This->dinput = dinput;
+    IDirectInput_AddRef( &dinput->IDirectInput7A_iface );
+
+    *out = This;
+    return DI_OK;
+}
diff --git a/dlls/dinput/device_private.h b/dlls/dinput/device_private.h
index e9168712fd9..b6c024def0d 100644
--- a/dlls/dinput/device_private.h
+++ b/dlls/dinput/device_private.h
@@ -86,6 +86,10 @@ struct IDirectInputDeviceImpl
     ActionMap                  *action_map;  /* array of mappings */
 };
 
+extern HRESULT direct_input_device_alloc( SIZE_T size, const IDirectInputDevice8WVtbl *vtblw,
+                                          const IDirectInputDevice8AVtbl *vtbla, const GUID *guid,
+                                          IDirectInputImpl *dinput, void **out ) DECLSPEC_HIDDEN;
+
 extern BOOL get_app_key(HKEY*, HKEY*) DECLSPEC_HIDDEN;
 extern DWORD get_config_key(HKEY, HKEY, const char*, char*, DWORD) DECLSPEC_HIDDEN;
 
diff --git a/dlls/dinput/joystick_linux.c b/dlls/dinput/joystick_linux.c
index 14074bb176c..7151e6e676c 100644
--- a/dlls/dinput/joystick_linux.c
+++ b/dlls/dinput/joystick_linux.c
@@ -462,8 +462,9 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
 
     TRACE( "%s %p %p %hu\n", debugstr_guid( rguid ), dinput, out, index );
 
-    newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
-    if (!newDevice) return DIERR_OUTOFMEMORY;
+    if (FAILED(hr = direct_input_device_alloc( sizeof(JoystickImpl), &JoystickWvt, &JoystickAvt, rguid, dinput, (void **)&newDevice )))
+        return hr;
+    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
 
     newDevice->joydev = &joystick_devices[index];
     newDevice->joyfd = -1;
@@ -481,14 +482,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
         newDevice->generic.devcaps.dwButtons = 128;
     }
 
-    newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
-    newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
-    newDevice->generic.base.ref = 1;
-    newDevice->generic.base.dinput = dinput;
-    newDevice->generic.base.guid = *rguid;
-    InitializeCriticalSection(&newDevice->generic.base.crit);
-    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
-
     /* setup_dinput_options may change these */
     newDevice->generic.deadzone = 0;
 
@@ -537,8 +530,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
         newDevice->generic.props[i].lSaturation = 0;
     }
 
-    IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
-
     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
     newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
 
diff --git a/dlls/dinput/joystick_linuxinput.c b/dlls/dinput/joystick_linuxinput.c
index 95b2df81ece..1b581f511ed 100644
--- a/dlls/dinput/joystick_linuxinput.c
+++ b/dlls/dinput/joystick_linuxinput.c
@@ -451,15 +451,12 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
     int i, idx = 0;
     int default_axis_map[WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS*2];
     DIDEVICEINSTANCEW ddi;
+    HRESULT hr;
 
-    newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
-    if (!newDevice) return DIERR_OUTOFMEMORY;
+    if (FAILED(hr = direct_input_device_alloc( sizeof(JoystickImpl), &JoystickWvt, &JoystickAvt, rguid, dinput, (void **)&newDevice )))
+        return hr;
+    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
 
-    newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
-    newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
-    newDevice->generic.base.ref    = 1;
-    newDevice->generic.base.guid   = *rguid;
-    newDevice->generic.base.dinput = dinput;
     newDevice->generic.joy_polldev = joy_polldev;
     newDevice->joyfd       = -1;
     newDevice->joydev      = &joydevs[index];
@@ -473,8 +470,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
        enabled. */
     newDevice->ff_autocenter = 1;
     newDevice->ff_gain = 0xFFFF;
-    InitializeCriticalSection(&newDevice->generic.base.crit);
-    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
 
     /* Count number of available axes - supported Axis & POVs */
     for (i = 0; i < ABS_MAX; i++)
@@ -582,8 +577,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
     if (newDevice->joydev->has_ff)
         newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
 
-    IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
-
     *out = newDevice;
     return DI_OK;
 
diff --git a/dlls/dinput/joystick_osx.c b/dlls/dinput/joystick_osx.c
index 65d0a4a36eb..bd758fb10a4 100644
--- a/dlls/dinput/joystick_osx.c
+++ b/dlls/dinput/joystick_osx.c
@@ -1120,8 +1120,9 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
 
     TRACE( "%s %p %p %hu\n", debugstr_guid( rguid ), dinput, out, index );
 
-    newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
-    if (!newDevice) return DIERR_OUTOFMEMORY;
+    if (FAILED(hr = direct_input_device_alloc( sizeof(JoystickImpl), &JoystickWvt, &JoystickAvt, rguid, dinput, (void **)&newDevice )))
+        return hr;
+    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
 
     newDevice->id = index;
 
@@ -1183,14 +1184,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
         newDevice->generic.devcaps.dwButtons = 128;
     }
 
-    newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
-    newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
-    newDevice->generic.base.ref = 1;
-    newDevice->generic.base.dinput = dinput;
-    newDevice->generic.base.guid = *rguid;
-    InitializeCriticalSection(&newDevice->generic.base.crit);
-    newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
-
     /* Create copy of default data format */
     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
     memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
@@ -1262,8 +1255,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, JoystickIm
     /* initialize default properties */
     get_osx_device_elements_props(newDevice);
 
-    IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
-
     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
     newDevice->generic.devcaps.dwFlags |= DIDC_ATTACHED;
     if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
diff --git a/dlls/dinput/keyboard.c b/dlls/dinput/keyboard.c
index e9fa9eb8cbc..b8d48513faf 100644
--- a/dlls/dinput/keyboard.c
+++ b/dlls/dinput/keyboard.c
@@ -245,17 +245,12 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, SysKeyboar
     SysKeyboardImpl* newDevice;
     LPDIDATAFORMAT df = NULL;
     int i, idx = 0;
+    HRESULT hr;
 
-    newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysKeyboardImpl));
-    if (!newDevice) return DIERR_OUTOFMEMORY;
-
-    newDevice->base.IDirectInputDevice8A_iface.lpVtbl = &SysKeyboardAvt;
-    newDevice->base.IDirectInputDevice8W_iface.lpVtbl = &SysKeyboardWvt;
-    newDevice->base.ref = 1;
-    memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
-    newDevice->base.dinput = dinput;
-    InitializeCriticalSection(&newDevice->base.crit);
+    if (FAILED(hr = direct_input_device_alloc( sizeof(SysKeyboardImpl), &SysKeyboardWvt, &SysKeyboardAvt, rguid, dinput, (void **)&newDevice )))
+        return hr;
     newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysKeyboardImpl*->base.crit");
+
     newDevice->subtype = get_keyboard_subtype();
 
     /* Create copy of default data format */
@@ -278,7 +273,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, SysKeyboar
     df->dwNumObjs = idx;
 
     newDevice->base.data_format.wine_df = df;
-    IDirectInput_AddRef(&newDevice->base.dinput->IDirectInput7A_iface);
 
     *out = newDevice;
     return DI_OK;
diff --git a/dlls/dinput/mouse.c b/dlls/dinput/mouse.c
index e789d260e5a..7cf196d0173 100644
--- a/dlls/dinput/mouse.c
+++ b/dlls/dinput/mouse.c
@@ -191,18 +191,13 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, SysMouseIm
     unsigned i;
     char buffer[20];
     HKEY hkey, appkey;
+    HRESULT hr;
 
-    newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(SysMouseImpl));
-    if (!newDevice) return DIERR_OUTOFMEMORY;
+    if (FAILED(hr = direct_input_device_alloc( sizeof(SysMouseImpl), &SysMouseWvt, &SysMouseAvt, rguid, dinput, (void **)&newDevice )))
+        return hr;
+    newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
 
-    newDevice->base.IDirectInputDevice8A_iface.lpVtbl = &SysMouseAvt;
-    newDevice->base.IDirectInputDevice8W_iface.lpVtbl = &SysMouseWvt;
-    newDevice->base.ref = 1;
     newDevice->base.dwCoopLevel = DISCL_NONEXCLUSIVE | DISCL_BACKGROUND;
-    newDevice->base.guid = *rguid;
-    InitializeCriticalSection(&newDevice->base.crit);
-    newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SysMouseImpl*->base.crit");
-    newDevice->base.dinput = dinput;
 
     get_app_key(&hkey, &appkey);
     if (!get_config_key(hkey, appkey, "MouseWarpOverride", buffer, sizeof(buffer)))
@@ -229,8 +224,6 @@ static HRESULT alloc_device( REFGUID rguid, IDirectInputImpl *dinput, SysMouseIm
             df->rgodf[i].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
 
     newDevice->base.data_format.wine_df = df;
-    IDirectInput_AddRef(&newDevice->base.dinput->IDirectInput7A_iface);
-
     if (dinput->dwVersion >= 0x0800)
     {
         newDevice->base.use_raw_input = TRUE;
-- 
2.31.0




More information about the wine-devel mailing list