SDL  2.0
SDL_xinput.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "../../SDL_internal.h"
22 
23 #include "SDL_assert.h"
24 #include "SDL_xinput.h"
25 
26 
27 #ifdef HAVE_XINPUT_H
28 
29 XInputGetState_t SDL_XInputGetState = NULL;
30 XInputSetState_t SDL_XInputSetState = NULL;
31 XInputGetCapabilities_t SDL_XInputGetCapabilities = NULL;
32 XInputGetBatteryInformation_t SDL_XInputGetBatteryInformation = NULL;
33 DWORD SDL_XInputVersion = 0;
34 
35 static HANDLE s_pXInputDLL = 0;
36 static int s_XInputDLLRefCount = 0;
37 
38 
39 #ifdef __WINRT__
40 
41 int
42 WIN_LoadXInputDLL(void)
43 {
44  /* Getting handles to system dlls (via LoadLibrary and its variants) is not
45  * supported on WinRT, thus, pointers to XInput's functions can't be
46  * retrieved via GetProcAddress.
47  *
48  * When on WinRT, assume that XInput is already loaded, and directly map
49  * its XInput.h-declared functions to the SDL_XInput* set of function
50  * pointers.
51  *
52  * Side-note: XInputGetStateEx is not available for use in WinRT.
53  * This seems to mean that support for the guide button is not available
54  * in WinRT, unfortunately.
55  */
56  SDL_XInputGetState = (XInputGetState_t)XInputGetState;
57  SDL_XInputSetState = (XInputSetState_t)XInputSetState;
58  SDL_XInputGetCapabilities = (XInputGetCapabilities_t)XInputGetCapabilities;
59  SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)XInputGetBatteryInformation;
60 
61  /* XInput 1.4 ships with Windows 8 and 8.1: */
62  SDL_XInputVersion = (1 << 16) | 4;
63 
64  return 0;
65 }
66 
67 void
68 WIN_UnloadXInputDLL(void)
69 {
70 }
71 
72 #else /* !__WINRT__ */
73 
74 int
75 WIN_LoadXInputDLL(void)
76 {
77  DWORD version = 0;
78 
79  if (s_pXInputDLL) {
80  SDL_assert(s_XInputDLLRefCount > 0);
81  s_XInputDLLRefCount++;
82  return 0; /* already loaded */
83  }
84 
85  version = (1 << 16) | 4;
86  s_pXInputDLL = LoadLibrary(L"XInput1_4.dll"); /* 1.4 Ships with Windows 8. */
87  if (!s_pXInputDLL) {
88  version = (1 << 16) | 3;
89  s_pXInputDLL = LoadLibrary(L"XInput1_3.dll"); /* 1.3 can be installed as a redistributable component. */
90  }
91  if (!s_pXInputDLL) {
92  s_pXInputDLL = LoadLibrary(L"bin\\XInput1_3.dll");
93  }
94  if (!s_pXInputDLL) {
95  /* "9.1.0" Ships with Vista and Win7, and is more limited than 1.3+ (e.g. XInputGetStateEx is not available.) */
96  s_pXInputDLL = LoadLibrary(L"XInput9_1_0.dll");
97  }
98  if (!s_pXInputDLL) {
99  return -1;
100  }
101 
102  SDL_assert(s_XInputDLLRefCount == 0);
103  SDL_XInputVersion = version;
104  s_XInputDLLRefCount = 1;
105 
106  /* 100 is the ordinal for _XInputGetStateEx, which returns the same struct as XinputGetState, but with extra data in wButtons for the guide button, we think... */
107  SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, (LPCSTR)100);
108  if (!SDL_XInputGetState) {
109  SDL_XInputGetState = (XInputGetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetState");
110  }
111  SDL_XInputSetState = (XInputSetState_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputSetState");
112  SDL_XInputGetCapabilities = (XInputGetCapabilities_t)GetProcAddress((HMODULE)s_pXInputDLL, "XInputGetCapabilities");
113  SDL_XInputGetBatteryInformation = (XInputGetBatteryInformation_t)GetProcAddress( (HMODULE)s_pXInputDLL, "XInputGetBatteryInformation" );
114  if (!SDL_XInputGetState || !SDL_XInputSetState || !SDL_XInputGetCapabilities) {
115  WIN_UnloadXInputDLL();
116  return -1;
117  }
118 
119  return 0;
120 }
121 
122 void
123 WIN_UnloadXInputDLL(void)
124 {
125  if (s_pXInputDLL) {
126  SDL_assert(s_XInputDLLRefCount > 0);
127  if (--s_XInputDLLRefCount == 0) {
128  FreeLibrary(s_pXInputDLL);
129  s_pXInputDLL = NULL;
130  }
131  } else {
132  SDL_assert(s_XInputDLLRefCount == 0);
133  }
134 }
135 
136 #endif /* __WINRT__ */
137 #endif /* HAVE_XINPUT_H */
138 
139 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164