src/joystick/windows/SDL_windowsjoystick.c
changeset 8972 dfc759d7486f
child 8988 1deb2998dbae
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/joystick/windows/SDL_windowsjoystick.c	Thu Jul 03 15:39:55 2014 -0700
     1.3 @@ -0,0 +1,569 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT
    1.27 +
    1.28 +/* DirectInput joystick driver; written by Glenn Maynard, based on Andrei de
    1.29 + * A. Formiga's WINMM driver.
    1.30 + *
    1.31 + * Hats and sliders are completely untested; the app I'm writing this for mostly
    1.32 + * doesn't use them and I don't own any joysticks with them.
    1.33 + *
    1.34 + * We don't bother to use event notification here.  It doesn't seem to work
    1.35 + * with polled devices, and it's fine to call IDirectInputDevice8_GetDeviceData and
    1.36 + * let it return 0 events. */
    1.37 +
    1.38 +#include "SDL_error.h"
    1.39 +#include "SDL_assert.h"
    1.40 +#include "SDL_events.h"
    1.41 +#include "SDL_thread.h"
    1.42 +#include "SDL_timer.h"
    1.43 +#include "SDL_mutex.h"
    1.44 +#include "SDL_events.h"
    1.45 +#include "SDL_hints.h"
    1.46 +#include "SDL_joystick.h"
    1.47 +#include "../SDL_sysjoystick.h"
    1.48 +#if !SDL_EVENTS_DISABLED
    1.49 +#include "../../events/SDL_events_c.h"
    1.50 +#endif
    1.51 +#include "../../core/windows/SDL_windows.h"
    1.52 +#include <dbt.h>
    1.53 +
    1.54 +#define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
    1.55 +#include "SDL_windowsjoystick_c.h"
    1.56 +#include "SDL_dinputjoystick_c.h"
    1.57 +#include "SDL_xinputjoystick_c.h"
    1.58 +
    1.59 +#include "../../haptic/windows/SDL_dinputhaptic_c.h"    /* For haptic hot plugging */
    1.60 +#include "../../haptic/windows/SDL_xinputhaptic_c.h"    /* For haptic hot plugging */
    1.61 +
    1.62 +
    1.63 +/* local variables */
    1.64 +static SDL_bool s_bDeviceAdded = SDL_FALSE;
    1.65 +static SDL_bool s_bDeviceRemoved = SDL_FALSE;
    1.66 +static SDL_JoystickID s_nInstanceID = -1;
    1.67 +static SDL_cond *s_condJoystickThread = NULL;
    1.68 +static SDL_mutex *s_mutexJoyStickEnum = NULL;
    1.69 +static SDL_Thread *s_threadJoystick = NULL;
    1.70 +static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
    1.71 +
    1.72 +JoyStick_DeviceData *SYS_Joystick;    /* array to hold joystick ID values */
    1.73 +
    1.74 +static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
    1.75 +
    1.76 +#ifdef __WINRT__
    1.77 +
    1.78 +typedef struct
    1.79 +{
    1.80 +    int unused;
    1.81 +} SDL_DeviceNotificationData;
    1.82 +
    1.83 +static void
    1.84 +SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)
    1.85 +{
    1.86 +}
    1.87 +
    1.88 +static int
    1.89 +SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)
    1.90 +{
    1.91 +    return 0;
    1.92 +}
    1.93 +
    1.94 +static void
    1.95 +SDL_CheckDeviceNotification(SDL_DeviceNotificationData *data)
    1.96 +{
    1.97 +}
    1.98 +
    1.99 +#else /* !__WINRT__ */
   1.100 +
   1.101 +typedef struct
   1.102 +{
   1.103 +    HRESULT coinitialized;
   1.104 +    WNDCLASSEX wincl;
   1.105 +    HWND messageWindow;
   1.106 +    HDEVNOTIFY hNotify;
   1.107 +} SDL_DeviceNotificationData;
   1.108 +
   1.109 +
   1.110 +/* windowproc for our joystick detect thread message only window, to detect any USB device addition/removal */
   1.111 +static LRESULT CALLBACK
   1.112 +SDL_PrivateJoystickDetectProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   1.113 +{
   1.114 +    switch (message) {
   1.115 +    case WM_DEVICECHANGE:
   1.116 +        switch (wParam) {
   1.117 +        case DBT_DEVICEARRIVAL:
   1.118 +            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
   1.119 +                s_bWindowsDeviceChanged = SDL_TRUE;
   1.120 +            }
   1.121 +            break;
   1.122 +        case DBT_DEVICEREMOVECOMPLETE:
   1.123 +            if (((DEV_BROADCAST_HDR*)lParam)->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) {
   1.124 +                s_bWindowsDeviceChanged = SDL_TRUE;
   1.125 +            }
   1.126 +            break;
   1.127 +        }
   1.128 +        return 0;
   1.129 +    }
   1.130 +
   1.131 +    return DefWindowProc (hwnd, message, wParam, lParam);
   1.132 +}
   1.133 +
   1.134 +static void
   1.135 +SDL_CleanupDeviceNotification(SDL_DeviceNotificationData *data)
   1.136 +{
   1.137 +    if (data->hNotify)
   1.138 +        UnregisterDeviceNotification(data->hNotify);
   1.139 +
   1.140 +    if (data->messageWindow)
   1.141 +        DestroyWindow(data->messageWindow);
   1.142 +
   1.143 +    UnregisterClass(data->wincl.lpszClassName, data->wincl.hInstance);
   1.144 +
   1.145 +    if (data->coinitialized == S_OK) {
   1.146 +        WIN_CoUninitialize();
   1.147 +    }
   1.148 +}
   1.149 +
   1.150 +static int
   1.151 +SDL_CreateDeviceNotification(SDL_DeviceNotificationData *data)
   1.152 +{
   1.153 +    DEV_BROADCAST_DEVICEINTERFACE dbh;
   1.154 +    GUID GUID_DEVINTERFACE_HID = { 0x4D1E55B2L, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
   1.155 +
   1.156 +    SDL_zerop(data);
   1.157 +
   1.158 +    data->coinitialized = WIN_CoInitialize();
   1.159 +
   1.160 +    data->wincl.hInstance = GetModuleHandle(NULL);
   1.161 +    data->wincl.lpszClassName = L"Message";
   1.162 +    data->wincl.lpfnWndProc = SDL_PrivateJoystickDetectProc;      /* This function is called by windows */
   1.163 +    data->wincl.cbSize = sizeof (WNDCLASSEX);
   1.164 +
   1.165 +    if (!RegisterClassEx(&data->wincl)) {
   1.166 +        WIN_SetError("Failed to create register class for joystick autodetect");
   1.167 +        SDL_CleanupDeviceNotification(data);
   1.168 +        return -1;
   1.169 +    }
   1.170 +
   1.171 +    data->messageWindow = (HWND)CreateWindowEx(0,  L"Message", NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
   1.172 +    if (!data->messageWindow) {
   1.173 +        WIN_SetError("Failed to create message window for joystick autodetect");
   1.174 +        SDL_CleanupDeviceNotification(data);
   1.175 +        return -1;
   1.176 +    }
   1.177 +
   1.178 +    SDL_zero(dbh);
   1.179 +    dbh.dbcc_size = sizeof(dbh);
   1.180 +    dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
   1.181 +    dbh.dbcc_classguid = GUID_DEVINTERFACE_HID;
   1.182 +
   1.183 +    data->hNotify = RegisterDeviceNotification(data->messageWindow, &dbh, DEVICE_NOTIFY_WINDOW_HANDLE);
   1.184 +    if (!data->hNotify) {
   1.185 +        WIN_SetError("Failed to create notify device for joystick autodetect");
   1.186 +        SDL_CleanupDeviceNotification(data);
   1.187 +        return -1;
   1.188 +    }
   1.189 +    return 0;
   1.190 +}
   1.191 +
   1.192 +static void
   1.193 +SDL_CheckDeviceNotification(SDL_DeviceNotificationData *data)
   1.194 +{
   1.195 +    MSG msg;
   1.196 +
   1.197 +    if (!data->messageWindow) {
   1.198 +        return;
   1.199 +    }
   1.200 +
   1.201 +    while (PeekMessage(&msg, data->messageWindow, 0, 0, PM_NOREMOVE)) {
   1.202 +        if (GetMessage(&msg, data->messageWindow, 0, 0) != 0)  {
   1.203 +            TranslateMessage(&msg);
   1.204 +            DispatchMessage(&msg);
   1.205 +        }
   1.206 +    }
   1.207 +}
   1.208 +
   1.209 +#endif /* __WINRT__ */
   1.210 +
   1.211 +/* Function/thread to scan the system for joysticks. */
   1.212 +static int
   1.213 +SDL_JoystickThread(void *_data)
   1.214 +{
   1.215 +    SDL_DeviceNotificationData notification_data;
   1.216 +
   1.217 +#if SDL_JOYSTICK_XINPUT
   1.218 +    SDL_bool bOpenedXInputDevices[XUSER_MAX_COUNT];
   1.219 +    SDL_zero(bOpenedXInputDevices);
   1.220 +#endif
   1.221 +
   1.222 +    if (SDL_CreateDeviceNotification(&notification_data) < 0) {
   1.223 +        return -1;
   1.224 +    }
   1.225 +
   1.226 +    SDL_LockMutex(s_mutexJoyStickEnum);
   1.227 +    while (s_bJoystickThreadQuit == SDL_FALSE) {
   1.228 +        SDL_bool bXInputChanged = SDL_FALSE;
   1.229 +
   1.230 +        SDL_CondWaitTimeout(s_condJoystickThread, s_mutexJoyStickEnum, 300);
   1.231 +
   1.232 +        SDL_CheckDeviceNotification(&notification_data);
   1.233 +
   1.234 +#if SDL_JOYSTICK_XINPUT
   1.235 +        if (SDL_XINPUT_Enabled() && XINPUTGETCAPABILITIES) {
   1.236 +            /* scan for any change in XInput devices */
   1.237 +            Uint8 userId;
   1.238 +            for (userId = 0; userId < XUSER_MAX_COUNT; userId++) {
   1.239 +                XINPUT_CAPABILITIES capabilities;
   1.240 +                const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);
   1.241 +                const SDL_bool available = (result == ERROR_SUCCESS);
   1.242 +                if (bOpenedXInputDevices[userId] != available) {
   1.243 +                    bXInputChanged = SDL_TRUE;
   1.244 +                    bOpenedXInputDevices[userId] = available;
   1.245 +                }
   1.246 +            }
   1.247 +        }
   1.248 +#endif /* SDL_JOYSTICK_XINPUT */
   1.249 +
   1.250 +        if (s_bWindowsDeviceChanged || bXInputChanged) {
   1.251 +            SDL_UnlockMutex(s_mutexJoyStickEnum);  /* let main thread go while we SDL_Delay(). */
   1.252 +            SDL_Delay(300); /* wait for direct input to find out about this device */
   1.253 +            SDL_LockMutex(s_mutexJoyStickEnum);
   1.254 +
   1.255 +            s_bDeviceRemoved = SDL_TRUE;
   1.256 +            s_bDeviceAdded = SDL_TRUE;
   1.257 +            s_bWindowsDeviceChanged = SDL_FALSE;
   1.258 +        }
   1.259 +    }
   1.260 +    SDL_UnlockMutex(s_mutexJoyStickEnum);
   1.261 +
   1.262 +    SDL_CleanupDeviceNotification(&notification_data);
   1.263 +
   1.264 +    return 1;
   1.265 +}
   1.266 +
   1.267 +void SDL_SYS_AddJoystickDevice(JoyStick_DeviceData *device)
   1.268 +{
   1.269 +    device->send_add_event = SDL_TRUE;
   1.270 +    device->nInstanceID = ++s_nInstanceID;
   1.271 +    device->pNext = SYS_Joystick;
   1.272 +    SYS_Joystick = device;
   1.273 +
   1.274 +    s_bDeviceAdded = SDL_TRUE;
   1.275 +}
   1.276 +
   1.277 +/* Function to scan the system for joysticks.
   1.278 + * This function should set SDL_numjoysticks to the number of available
   1.279 + * joysticks.  Joystick 0 should be the system default joystick.
   1.280 + * It should return 0, or -1 on an unrecoverable fatal error.
   1.281 + */
   1.282 +int
   1.283 +SDL_SYS_JoystickInit(void)
   1.284 +{
   1.285 +    if (SDL_DINPUT_JoystickInit() < 0) {
   1.286 +        SDL_SYS_JoystickQuit();
   1.287 +        return -1;
   1.288 +    }
   1.289 +
   1.290 +    if (SDL_XINPUT_JoystickInit() < 0) {
   1.291 +        SDL_SYS_JoystickQuit();
   1.292 +        return -1;
   1.293 +    }
   1.294 +
   1.295 +    s_mutexJoyStickEnum = SDL_CreateMutex();
   1.296 +    s_condJoystickThread = SDL_CreateCond();
   1.297 +    s_bDeviceAdded = SDL_TRUE; /* force a scan of the system for joysticks this first time */
   1.298 +
   1.299 +    SDL_SYS_JoystickDetect();
   1.300 +
   1.301 +    if (!s_threadJoystick) {
   1.302 +        s_bJoystickThreadQuit = SDL_FALSE;
   1.303 +        /* spin up the thread to detect hotplug of devices */
   1.304 +#if defined(__WIN32__) && !defined(HAVE_LIBC)
   1.305 +#undef SDL_CreateThread
   1.306 +#if SDL_DYNAMIC_API
   1.307 +        s_threadJoystick= SDL_CreateThread_REAL(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
   1.308 +#else
   1.309 +        s_threadJoystick= SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL, NULL, NULL);
   1.310 +#endif
   1.311 +#else
   1.312 +        s_threadJoystick = SDL_CreateThread(SDL_JoystickThread, "SDL_joystick", NULL);
   1.313 +#endif
   1.314 +    }
   1.315 +    return SDL_SYS_NumJoysticks();
   1.316 +}
   1.317 +
   1.318 +/* return the number of joysticks that are connected right now */
   1.319 +int
   1.320 +SDL_SYS_NumJoysticks()
   1.321 +{
   1.322 +    int nJoysticks = 0;
   1.323 +    JoyStick_DeviceData *device = SYS_Joystick;
   1.324 +    while (device) {
   1.325 +        nJoysticks++;
   1.326 +        device = device->pNext;
   1.327 +    }
   1.328 +
   1.329 +    return nJoysticks;
   1.330 +}
   1.331 +
   1.332 +/* detect any new joysticks being inserted into the system */
   1.333 +void
   1.334 +SDL_SYS_JoystickDetect()
   1.335 +{
   1.336 +    JoyStick_DeviceData *pCurList = NULL;
   1.337 +#if !SDL_EVENTS_DISABLED
   1.338 +    SDL_Event event;
   1.339 +#endif
   1.340 +
   1.341 +    /* only enum the devices if the joystick thread told us something changed */
   1.342 +    if (!s_bDeviceAdded && !s_bDeviceRemoved) {
   1.343 +        return;  /* thread hasn't signaled, nothing to do right now. */
   1.344 +    }
   1.345 +
   1.346 +    SDL_LockMutex(s_mutexJoyStickEnum);
   1.347 +
   1.348 +    s_bDeviceAdded = SDL_FALSE;
   1.349 +    s_bDeviceRemoved = SDL_FALSE;
   1.350 +
   1.351 +    pCurList = SYS_Joystick;
   1.352 +    SYS_Joystick = NULL;
   1.353 +
   1.354 +    /* Look for DirectInput joysticks, wheels, head trackers, gamepads, etc.. */
   1.355 +    SDL_DINPUT_JoystickDetect(&pCurList);
   1.356 +
   1.357 +    /* Look for XInput devices. Do this last, so they're first in the final list. */
   1.358 +    SDL_XINPUT_JoystickDetect(&pCurList);
   1.359 +
   1.360 +    SDL_UnlockMutex(s_mutexJoyStickEnum);
   1.361 +
   1.362 +    while (pCurList) {
   1.363 +        JoyStick_DeviceData *pListNext = NULL;
   1.364 +
   1.365 +        if (pCurList->bXInputDevice) {
   1.366 +            SDL_XINPUT_MaybeRemoveDevice(pCurList->XInputUserId);
   1.367 +        } else {
   1.368 +            SDL_DINPUT_MaybeRemoveDevice(&pCurList->dxdevice);
   1.369 +        }
   1.370 +
   1.371 +#if !SDL_EVENTS_DISABLED
   1.372 +        SDL_zero(event);
   1.373 +        event.type = SDL_JOYDEVICEREMOVED;
   1.374 +
   1.375 +        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.376 +            event.jdevice.which = pCurList->nInstanceID;
   1.377 +            if ((!SDL_EventOK) || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   1.378 +                SDL_PushEvent(&event);
   1.379 +            }
   1.380 +        }
   1.381 +#endif /* !SDL_EVENTS_DISABLED */
   1.382 +
   1.383 +        pListNext = pCurList->pNext;
   1.384 +        SDL_free(pCurList->joystickname);
   1.385 +        SDL_free(pCurList);
   1.386 +        pCurList = pListNext;
   1.387 +    }
   1.388 +
   1.389 +    if (s_bDeviceAdded) {
   1.390 +        JoyStick_DeviceData *pNewJoystick;
   1.391 +        int device_index = 0;
   1.392 +        s_bDeviceAdded = SDL_FALSE;
   1.393 +        pNewJoystick = SYS_Joystick;
   1.394 +        while (pNewJoystick) {
   1.395 +            if (pNewJoystick->send_add_event) {
   1.396 +                if (pNewJoystick->bXInputDevice) {
   1.397 +                    SDL_XINPUT_MaybeAddDevice(pNewJoystick->XInputUserId);
   1.398 +                } else {
   1.399 +                    SDL_DINPUT_MaybeAddDevice(&pNewJoystick->dxdevice);
   1.400 +                }
   1.401 +
   1.402 +#if !SDL_EVENTS_DISABLED
   1.403 +                SDL_zero(event);
   1.404 +                event.type = SDL_JOYDEVICEADDED;
   1.405 +
   1.406 +                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
   1.407 +                    event.jdevice.which = device_index;
   1.408 +                    if ((!SDL_EventOK) || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
   1.409 +                        SDL_PushEvent(&event);
   1.410 +                    }
   1.411 +                }
   1.412 +#endif /* !SDL_EVENTS_DISABLED */
   1.413 +                pNewJoystick->send_add_event = SDL_FALSE;
   1.414 +            }
   1.415 +            device_index++;
   1.416 +            pNewJoystick = pNewJoystick->pNext;
   1.417 +        }
   1.418 +    }
   1.419 +}
   1.420 +
   1.421 +/* Function to get the device-dependent name of a joystick */
   1.422 +const char *
   1.423 +SDL_SYS_JoystickNameForDeviceIndex(int device_index)
   1.424 +{
   1.425 +    JoyStick_DeviceData *device = SYS_Joystick;
   1.426 +
   1.427 +    for (; device_index > 0; device_index--)
   1.428 +        device = device->pNext;
   1.429 +
   1.430 +    return device->joystickname;
   1.431 +}
   1.432 +
   1.433 +/* Function to perform the mapping between current device instance and this joysticks instance id */
   1.434 +SDL_JoystickID
   1.435 +SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
   1.436 +{
   1.437 +    JoyStick_DeviceData *device = SYS_Joystick;
   1.438 +    int index;
   1.439 +
   1.440 +    for (index = device_index; index > 0; index--)
   1.441 +        device = device->pNext;
   1.442 +
   1.443 +    return device->nInstanceID;
   1.444 +}
   1.445 +
   1.446 +/* Function to open a joystick for use.
   1.447 +   The joystick to open is specified by the index field of the joystick.
   1.448 +   This should fill the nbuttons and naxes fields of the joystick structure.
   1.449 +   It returns 0, or -1 if there is an error.
   1.450 + */
   1.451 +int
   1.452 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
   1.453 +{
   1.454 +    JoyStick_DeviceData *joystickdevice = SYS_Joystick;
   1.455 +
   1.456 +    for (; device_index > 0; device_index--)
   1.457 +        joystickdevice = joystickdevice->pNext;
   1.458 +
   1.459 +    /* allocate memory for system specific hardware data */
   1.460 +    joystick->instance_id = joystickdevice->nInstanceID;
   1.461 +    joystick->closed = SDL_FALSE;
   1.462 +    joystick->hwdata =
   1.463 +        (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   1.464 +    if (joystick->hwdata == NULL) {
   1.465 +        return SDL_OutOfMemory();
   1.466 +    }
   1.467 +    SDL_zerop(joystick->hwdata);
   1.468 +    joystick->hwdata->guid = joystickdevice->guid;
   1.469 +
   1.470 +    if (joystickdevice->bXInputDevice) {
   1.471 +        return SDL_XINPUT_JoystickOpen(joystick, joystickdevice);
   1.472 +    } else {
   1.473 +        return SDL_DINPUT_JoystickOpen(joystick, joystickdevice);
   1.474 +    }
   1.475 +}
   1.476 +
   1.477 +/* return true if this joystick is plugged in right now */
   1.478 +SDL_bool 
   1.479 +SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
   1.480 +{
   1.481 +    return !joystick->closed && !joystick->hwdata->removed;
   1.482 +}
   1.483 +
   1.484 +void
   1.485 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   1.486 +{
   1.487 +    if (joystick->closed || !joystick->hwdata) {
   1.488 +        return;
   1.489 +    }
   1.490 +
   1.491 +    if (joystick->hwdata->bXInputDevice) {
   1.492 +        SDL_XINPUT_JoystickUpdate(joystick);
   1.493 +    } else {
   1.494 +        SDL_DINPUT_JoystickUpdate(joystick);
   1.495 +    }
   1.496 +
   1.497 +    if (joystick->hwdata->removed) {
   1.498 +        joystick->closed = SDL_TRUE;
   1.499 +        joystick->uncentered = SDL_TRUE;
   1.500 +    }
   1.501 +}
   1.502 +
   1.503 +/* Function to close a joystick after use */
   1.504 +void
   1.505 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
   1.506 +{
   1.507 +    if (joystick->hwdata->bXInputDevice) {
   1.508 +        SDL_XINPUT_JoystickClose(joystick);
   1.509 +    } else {
   1.510 +        SDL_DINPUT_JoystickClose(joystick);
   1.511 +    }
   1.512 +
   1.513 +    /* free system specific hardware data */
   1.514 +    SDL_free(joystick->hwdata);
   1.515 +
   1.516 +    joystick->closed = SDL_TRUE;
   1.517 +}
   1.518 +
   1.519 +/* Function to perform any system-specific joystick related cleanup */
   1.520 +void
   1.521 +SDL_SYS_JoystickQuit(void)
   1.522 +{
   1.523 +    JoyStick_DeviceData *device = SYS_Joystick;
   1.524 +
   1.525 +    while (device) {
   1.526 +        JoyStick_DeviceData *device_next = device->pNext;
   1.527 +        SDL_free(device->joystickname);
   1.528 +        SDL_free(device);
   1.529 +        device = device_next;
   1.530 +    }
   1.531 +    SYS_Joystick = NULL;
   1.532 +
   1.533 +    if (s_threadJoystick) {
   1.534 +        SDL_LockMutex(s_mutexJoyStickEnum);
   1.535 +        s_bJoystickThreadQuit = SDL_TRUE;
   1.536 +        SDL_CondBroadcast(s_condJoystickThread); /* signal the joystick thread to quit */
   1.537 +        SDL_UnlockMutex(s_mutexJoyStickEnum);
   1.538 +        SDL_WaitThread(s_threadJoystick, NULL); /* wait for it to bugger off */
   1.539 +
   1.540 +        SDL_DestroyMutex(s_mutexJoyStickEnum);
   1.541 +        SDL_DestroyCond(s_condJoystickThread);
   1.542 +        s_condJoystickThread= NULL;
   1.543 +        s_mutexJoyStickEnum = NULL;
   1.544 +        s_threadJoystick = NULL;
   1.545 +    }
   1.546 +
   1.547 +    SDL_DINPUT_JoystickQuit();
   1.548 +    SDL_XINPUT_JoystickQuit();
   1.549 +}
   1.550 +
   1.551 +/* return the stable device guid for this device index */
   1.552 +SDL_JoystickGUID
   1.553 +SDL_SYS_JoystickGetDeviceGUID(int device_index)
   1.554 +{
   1.555 +    JoyStick_DeviceData *device = SYS_Joystick;
   1.556 +    int index;
   1.557 +
   1.558 +    for (index = device_index; index > 0; index--)
   1.559 +        device = device->pNext;
   1.560 +
   1.561 +    return device->guid;
   1.562 +}
   1.563 +
   1.564 +SDL_JoystickGUID
   1.565 +SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
   1.566 +{
   1.567 +    return joystick->hwdata->guid;
   1.568 +}
   1.569 +
   1.570 +#endif /* SDL_JOYSTICK_DINPUT || SDL_JOYSTICK_XINPUT */
   1.571 +
   1.572 +/* vi: set ts=4 sw=4 expandtab: */