From: Zebediah Figura zfigura@codeweavers.com
--- dlls/winmm/joystick.c | 74 ++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 29 deletions(-)
diff --git a/dlls/winmm/joystick.c b/dlls/winmm/joystick.c index 0285074bd40..56ff61582ba 100644 --- a/dlls/winmm/joystick.c +++ b/dlls/winmm/joystick.c @@ -23,6 +23,7 @@ */
#include <stdarg.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -175,14 +176,48 @@ void joystick_unload(void) IDirectInput8_Release( dinput ); }
+static bool acquire_joystick( DWORD index ) +{ + const DIDEVICEINSTANCEW *instance = &instances[index]; + struct joystick *joystick = &joysticks[index]; + IDirectInputDevice8W *device; + HANDLE event; + HRESULT hr; + + if (FAILED(hr = IDirectInput8_CreateDevice( dinput, &instance->guidInstance, &device, NULL ))) + { + WARN( "failed to create dinput device %s, hr %#lx\n", debugstr_guid( &instance->guidInstance ), hr ); + return false; + } + + if (!(event = CreateEventW( NULL, FALSE, FALSE, NULL ))) + ERR( "failed to create event, error %lu\n", GetLastError() ); + if (FAILED(hr = IDirectInputDevice8_SetEventNotification( device, event ))) + ERR( "failed to set event notification, hr %#lx\n", hr ); + if (FAILED(hr = IDirectInputDevice8_SetCooperativeLevel( device, NULL, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ))) + ERR( "failed to set cooperative level, hr %#lx\n", hr ); + if (FAILED(hr = IDirectInputDevice8_SetDataFormat( device, &data_format ))) + ERR( "failed to set data format, hr %#lx\n", hr ); + if (FAILED(hr = IDirectInputDevice8_Acquire( device ))) + { + WARN( "failed to acquire, hr %#lx\n", hr ); + IDirectInputDevice8_Release( device ); + CloseHandle( event ); + return false; + } + + memset( joystick, 0, sizeof(*joystick) ); + joystick->instance = *instance; + joystick->device = device; + joystick->event = event; + return true; +} + static void find_joysticks(void) { static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
- IDirectInputDevice8W *device; - HANDLE event; DWORD index; - HRESULT hr;
InitOnceExecuteOnce( &init_once, joystick_load_once, NULL, NULL );
@@ -203,35 +238,16 @@ static void find_joysticks(void) CloseHandle( joysticks[index].event ); }
- if (!(event = CreateEventW( NULL, FALSE, FALSE, NULL ))) - WARN( "could not event for device, error %lu\n", GetLastError() ); - else if (FAILED(hr = IDirectInput8_CreateDevice( dinput, &instances[index].guidInstance, &device, NULL ))) - WARN( "could not create device %s instance, hr %#lx\n", - debugstr_guid( &instances[index].guidInstance ), hr ); - else if (FAILED(hr = IDirectInputDevice8_SetEventNotification( device, event ))) - WARN( "SetEventNotification device %p hr %#lx\n", device, hr ); - else if (FAILED(hr = IDirectInputDevice8_SetCooperativeLevel( device, NULL, DISCL_NONEXCLUSIVE|DISCL_BACKGROUND ))) - WARN( "SetCooperativeLevel device %p hr %#lx\n", device, hr ); - else if (FAILED(hr = IDirectInputDevice8_SetDataFormat( device, &data_format ))) - WARN( "SetDataFormat device %p hr %#lx\n", device, hr ); - else if (FAILED(hr = IDirectInputDevice8_Acquire( device ))) - WARN( "Acquire device %p hr %#lx\n", device, hr ); + if (acquire_joystick( index )) + { + TRACE( "opened device %s\n", debugstr_guid( &instances[index].guidInstance )); + } else { - TRACE( "opened device %p event %p\n", device, event ); - - memset( &joysticks[index], 0, sizeof(struct joystick) ); - joysticks[index].instance = instances[index]; - joysticks[index].device = device; - joysticks[index].event = event; - continue; + memmove( joysticks + index, joysticks + index + 1, + (ARRAY_SIZE(joysticks) - index - 1) * sizeof(struct joystick) ); + memset( &joysticks[ARRAY_SIZE(joysticks) - 1], 0, sizeof(struct joystick) ); } - - CloseHandle( event ); - if (device) IDirectInputDevice8_Release( device ); - memmove( joysticks + index, joysticks + index + 1, - (ARRAY_SIZE(joysticks) - index - 1) * sizeof(struct joystick) ); - memset( &joysticks[ARRAY_SIZE(joysticks) - 1], 0, sizeof(struct joystick) ); } }