1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2013-2015 - pinumbernumber
3  *  Copyright (C) 2011-2020 - Daniel De Matteis
4  *
5  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
6  *  of the GNU General Public License as published by the Free Software Found-
7  *  ation, either version 3 of the License, or (at your option) any later version.
8  *
9  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11  *  PURPOSE.  See the GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License along with RetroArch.
14  *  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 /* Support 360 controllers on Windows.
18  * Said controllers do show under DInput but they have limitations in this mode;
19  * The triggers are combined rather than seperate and it is not possible to use
20  * the guide button.
21  *
22  * Some wrappers for other controllers also simulate xinput (as it is easier to implement)
23  * so this may be useful for those also.
24  **/
25 #include <stdlib.h>
26 #include <stddef.h>
27 #include <string.h>
28 
29 #include <boolean.h>
30 #include <retro_inline.h>
31 #include <compat/strl.h>
32 #include <dynamic/dylib.h>
33 #include <string/stdstring.h>
34 
35 #ifdef HAVE_CONFIG_H
36 #include "../../config.h"
37 #endif
38 
39 #include "../../config.def.h"
40 
41 #include "../../tasks/tasks_internal.h"
42 #include "../input_driver.h"
43 
44 #include "../../verbosity.h"
45 
46 #include "xinput_joypad.h"
47 
48 typedef struct
49 {
50    XINPUT_STATE xstate;
51    bool         connected;
52 } xinput_joypad_state;
53 
54 /* Function pointer, to be assigned with dylib_proc */
55 typedef uint32_t (__stdcall *XInputGetStateEx_t)(uint32_t, XINPUT_STATE*);
56 typedef uint32_t (__stdcall *XInputSetState_t)(uint32_t, XINPUT_VIBRATION*);
57 
58 /* TODO/FIXME - static globals */
59 #ifdef HAVE_DYNAMIC
60 /* For xinput1_n.dll */
61 static dylib_t g_xinput_dll = NULL;
62 #endif
63 /* Guide button may or may not be available */
64 static bool g_xinput_guide_button_supported = false;
65 static unsigned g_xinput_num_buttons        = 0;
66 static XInputSetState_t g_XInputSetState;
67 static XInputGetStateEx_t g_XInputGetStateEx;
68 #ifdef _XBOX1
69 static XINPUT_FEEDBACK     g_xinput_rumble_states[4];
70 #else
71 static XINPUT_VIBRATION    g_xinput_rumble_states[4];
72 #endif
73 static xinput_joypad_state g_xinput_states[4];
74 
75 /* Buttons are provided by XInput as bits of a uint16.
76  * Map from rarch button index (0..10) to a mask to
77  * bitwise-& the buttons against.
78  * dpad is handled seperately. */
79 static const uint16_t button_index_to_bitmap_code[] =  {
80    XINPUT_GAMEPAD_A,
81    XINPUT_GAMEPAD_B,
82    XINPUT_GAMEPAD_X,
83    XINPUT_GAMEPAD_Y,
84    XINPUT_GAMEPAD_LEFT_SHOULDER,
85    XINPUT_GAMEPAD_RIGHT_SHOULDER,
86    XINPUT_GAMEPAD_START,
87    XINPUT_GAMEPAD_BACK,
88    XINPUT_GAMEPAD_LEFT_THUMB,
89    XINPUT_GAMEPAD_RIGHT_THUMB
90 #ifndef _XBOX
91    ,
92    XINPUT_GAMEPAD_GUIDE
93 #endif
94 };
95 
96 #include "xinput_joypad_inl.h"
97 
pad_index_to_xuser_index(unsigned pad)98 static INLINE int pad_index_to_xuser_index(unsigned pad)
99 {
100    return pad < DEFAULT_MAX_PADS
101       && g_xinput_states[pad].connected ? pad : -1;
102 }
103 
xinput_joypad_name(unsigned pad)104 static const char *xinput_joypad_name(unsigned pad)
105 {
106    /* Generic 'XInput' instead of 'Xbox 360', because
107     * there are some other non-Xbox third party PC
108     * controllers */
109    static const char XBOX_CONTROLLER_NAME[] = "XInput Controller";
110    if (pad_index_to_xuser_index(pad) < 0)
111       return NULL;
112 
113    /* On platforms without dinput support, no
114     * device-specific name is available
115     * > Have to use generic names instead */
116    return XBOX_CONTROLLER_NAME;
117 }
118 
xinput_joypad_init(void * data)119 static void *xinput_joypad_init(void *data)
120 {
121    unsigned i, j;
122    XINPUT_STATE dummy_state;
123 
124 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
125    if (!g_xinput_dll)
126       if (!load_xinput_dll())
127          goto error;
128 
129    /* If we get here then an xinput DLL is correctly loaded.
130     * First try to load ordinal 100 (XInputGetStateEx).
131     */
132    g_XInputGetStateEx = (XInputGetStateEx_t)dylib_proc(
133          g_xinput_dll, (const char*)100);
134 #elif defined(__WINRT__)
135    /* XInputGetStateEx is not available on WinRT */
136    g_XInputGetStateEx = NULL;
137 #else
138    g_XInputGetStateEx = (XInputGetStateEx_t)XInputGetStateEx;
139 #endif
140    g_xinput_guide_button_supported = true;
141 
142    if (!g_XInputGetStateEx)
143    {
144       /* no ordinal 100. (Presumably a wrapper.) Load the ordinary
145        * XInputGetState, at the cost of losing guide button support.
146        */
147       g_xinput_guide_button_supported = false;
148 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
149       g_XInputGetStateEx = (XInputGetStateEx_t)dylib_proc(
150             g_xinput_dll, "XInputGetState");
151 #else
152 	  g_XInputGetStateEx = (XInputGetStateEx_t)XInputGetState;
153 #endif
154 
155       if (!g_XInputGetStateEx)
156       {
157          RARCH_ERR("[XInput]: Failed to init: DLL is invalid or corrupt.\n");
158 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
159          dylib_close(g_xinput_dll);
160 #endif
161          /* DLL was loaded but did not contain the correct function. */
162          goto error;
163       }
164       RARCH_WARN("[XInput]: No guide button support.\n");
165    }
166 
167 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
168    g_XInputSetState = (XInputSetState_t)dylib_proc(
169          g_xinput_dll, "XInputSetState");
170 #else
171    g_XInputSetState = (XInputSetState_t)XInputSetState;
172 #endif
173    if (!g_XInputSetState)
174    {
175       RARCH_ERR("[XInput]: Failed to init: DLL is invalid or corrupt.\n");
176 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
177       dylib_close(g_xinput_dll);
178 #endif
179       goto error; /* DLL was loaded but did not contain the correct function. */
180    }
181 
182    /* Zero out the states. */
183    for (i = 0; i < 4; ++i)
184    {
185       g_xinput_states[i].xstate.dwPacketNumber        = 0;
186       g_xinput_states[i].xstate.Gamepad.wButtons      = 0;
187       g_xinput_states[i].xstate.Gamepad.bLeftTrigger  = 0;
188       g_xinput_states[i].xstate.Gamepad.bRightTrigger = 0;
189       g_xinput_states[i].xstate.Gamepad.sThumbLX      = 0;
190       g_xinput_states[i].xstate.Gamepad.sThumbLY      = 0;
191       g_xinput_states[i].xstate.Gamepad.sThumbRX      = 0;
192       g_xinput_states[i].xstate.Gamepad.sThumbRY      = 0;
193       g_xinput_states[i].connected                    =
194          !(g_XInputGetStateEx(i, &dummy_state) == ERROR_DEVICE_NOT_CONNECTED);
195    }
196 
197    if (  (!g_xinput_states[0].connected) &&
198          (!g_xinput_states[1].connected) &&
199          (!g_xinput_states[2].connected) &&
200          (!g_xinput_states[3].connected))
201 #ifdef __WINRT__
202       goto succeeded;
203 #else
204       goto error;
205 #endif
206 
207    for (j = 0; j < MAX_USERS; j++)
208    {
209       const char *name = xinput_joypad_name(j);
210 
211       if (pad_index_to_xuser_index(j) > -1)
212       {
213          /* TODO/FIXME - fill in VID/PID? */
214          int32_t vid          = 0;
215          int32_t pid          = 0;
216          input_autoconfigure_connect(
217                name,
218                NULL,
219                xinput_joypad.ident,
220                j,
221                vid,
222                pid);
223       }
224    }
225 
226 #ifdef __WINRT__
227 succeeded:
228 #endif
229    /* non-hat button. */
230    g_xinput_num_buttons = g_xinput_guide_button_supported ? 11 : 10;
231 
232    return (void*)-1;
233 
234 error:
235    /* non-hat button. */
236    g_xinput_num_buttons = g_xinput_guide_button_supported ? 11 : 10;
237 
238    return NULL;
239 }
240 
xinput_joypad_query_pad(unsigned pad)241 static bool xinput_joypad_query_pad(unsigned pad)
242 {
243    int xuser = pad_index_to_xuser_index(pad);
244    if (xuser > -1)
245       return g_xinput_states[xuser].connected;
246    return false;
247 }
248 
xinput_joypad_destroy(void)249 static void xinput_joypad_destroy(void)
250 {
251    unsigned i;
252 
253    for (i = 0; i < 4; ++i)
254    {
255       g_xinput_states[i].xstate.dwPacketNumber        = 0;
256       g_xinput_states[i].xstate.Gamepad.wButtons      = 0;
257       g_xinput_states[i].xstate.Gamepad.bLeftTrigger  = 0;
258       g_xinput_states[i].xstate.Gamepad.bRightTrigger = 0;
259       g_xinput_states[i].xstate.Gamepad.sThumbLX      = 0;
260       g_xinput_states[i].xstate.Gamepad.sThumbLY      = 0;
261       g_xinput_states[i].xstate.Gamepad.sThumbRX      = 0;
262       g_xinput_states[i].xstate.Gamepad.sThumbRY      = 0;
263       g_xinput_states[i].connected                    = false;
264    }
265 
266 #if defined(HAVE_DYNAMIC) && !defined(__WINRT__)
267    dylib_close(g_xinput_dll);
268 
269    g_xinput_dll        = NULL;
270 #endif
271    g_XInputGetStateEx  = NULL;
272    g_XInputSetState    = NULL;
273 }
274 
275 
xinput_joypad_button(unsigned port,uint16_t joykey)276 static int16_t xinput_joypad_button(unsigned port, uint16_t joykey)
277 {
278    int xuser                  = pad_index_to_xuser_index(port);
279    uint16_t btn_word          = 0;
280    xinput_joypad_state *state = &g_xinput_states[xuser];
281    if (!state->connected)
282       return 0;
283    btn_word                   = state->xstate.Gamepad.wButtons;
284    return xinput_joypad_button_state(xuser, btn_word, port, joykey);
285 }
286 
xinput_joypad_axis(unsigned port,uint32_t joyaxis)287 static int16_t xinput_joypad_axis(unsigned port, uint32_t joyaxis)
288 {
289    int xuser                  = pad_index_to_xuser_index(port);
290    xinput_joypad_state *state = &g_xinput_states[xuser];
291    XINPUT_GAMEPAD *pad        = &(state->xstate.Gamepad);
292    if (!state->connected)
293       return 0;
294    return xinput_joypad_axis_state(pad, port, joyaxis);
295 }
296 
xinput_joypad_state_func(rarch_joypad_info_t * joypad_info,const struct retro_keybind * binds,unsigned port)297 static int16_t xinput_joypad_state_func(
298       rarch_joypad_info_t *joypad_info,
299       const struct retro_keybind *binds,
300       unsigned port)
301 {
302    unsigned i;
303    uint16_t btn_word;
304    int16_t ret                = 0;
305    uint16_t port_idx          = joypad_info->joy_idx;
306    int xuser                  = pad_index_to_xuser_index(port_idx);
307    xinput_joypad_state *state = &g_xinput_states[xuser];
308    XINPUT_GAMEPAD *pad        = &state->xstate.Gamepad;
309    if (!state->connected)
310       return 0;
311    btn_word                   = state->xstate.Gamepad.wButtons;
312 
313    for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
314    {
315       /* Auto-binds are per joypad, not per user. */
316       const uint64_t joykey  = (binds[i].joykey != NO_BTN)
317          ? binds[i].joykey  : joypad_info->auto_binds[i].joykey;
318       const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
319          ? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
320       if (
321                (uint16_t)joykey != NO_BTN
322             && xinput_joypad_button_state(
323                xuser, btn_word, port_idx, (uint16_t)joykey))
324          ret |= ( 1 << i);
325       else if (joyaxis != AXIS_NONE &&
326             ((float)abs(xinput_joypad_axis_state(pad, port_idx, joyaxis))
327              / 0x8000) > joypad_info->axis_threshold)
328          ret |= (1 << i);
329    }
330 
331    return ret;
332 }
333 
xinput_joypad_poll(void)334 static void xinput_joypad_poll(void)
335 {
336    unsigned i;
337 
338    for (i = 0; i < 4; ++i)
339    {
340       xinput_joypad_state
341          *state          = &g_xinput_states[i];
342       DWORD status       = g_XInputGetStateEx(i, &state->xstate);
343       bool success       = status == ERROR_SUCCESS;
344       bool new_connected = status != ERROR_DEVICE_NOT_CONNECTED;
345       if (new_connected != state->connected)
346       {
347          /* Normally, dinput handles device insertion/removal for us, but
348           * since dinput is not available on UWP we have to do it ourselves */
349          /* Also note that on UWP, the controllers are not available on startup
350           * and are instead 'plugged in' a moment later because Microsoft reasons */
351          /* TODO: This may be bad for performance? */
352          if (new_connected)
353          {
354             /* This is kinda ugly, but it's the same thing that dinput does */
355             xinput_joypad_destroy();
356             xinput_joypad_init(NULL);
357             return;
358          }
359 
360          state->connected = new_connected;
361          if (!success)
362             input_autoconfigure_disconnect(i, xinput_joypad_name(i));
363       }
364    }
365 }
366 
xinput_joypad_rumble(unsigned pad,enum retro_rumble_effect effect,uint16_t strength)367 static bool xinput_joypad_rumble(unsigned pad,
368       enum retro_rumble_effect effect, uint16_t strength)
369 {
370    int xuser = pad_index_to_xuser_index(pad);
371 
372    if (xuser == -1)
373       return false;
374 
375    /* Consider the low frequency (left) motor the "strong" one. */
376    if (effect == RETRO_RUMBLE_STRONG)
377       g_xinput_rumble_states[xuser].wLeftMotorSpeed = strength;
378    else if (effect == RETRO_RUMBLE_WEAK)
379       g_xinput_rumble_states[xuser].wRightMotorSpeed = strength;
380 
381    if (!g_XInputSetState)
382       return false;
383 
384    return (g_XInputSetState(xuser, &g_xinput_rumble_states[xuser])
385       == 0);
386 }
387 
388 input_device_driver_t xinput_joypad = {
389    xinput_joypad_init,
390    xinput_joypad_query_pad,
391    xinput_joypad_destroy,
392    xinput_joypad_button,
393    xinput_joypad_state_func,
394    NULL,
395    xinput_joypad_axis,
396    xinput_joypad_poll,
397    xinput_joypad_rumble,
398    xinput_joypad_name,
399    "xinput",
400 };
401