| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Windows wrap-all joystick driver. |
| 12 |
* By Beoran. |
| 13 |
* See readme.txt for copyright information. |
| 14 |
* |
| 15 |
* This driver exists because on Windows, DirectInput and XInput are two |
| 16 |
* different joystick input APIs which bith need to be supported simultaneously. |
| 17 |
* |
| 18 |
* Although DirectInput is deprecated, it is a far more capable API in terms |
| 19 |
* of joystick layout and force feedback effects. XInput is a much simpler |
| 20 |
* API but it only supports joysticks that have a layout similar to that of an |
| 21 |
* XBOX controller. XInput also has very limited force feedback effects, |
| 22 |
* it only supportd rubmble style vibrations. |
| 23 |
* |
| 24 |
* Older joystics or input devices such as steering wheels that do not map |
| 25 |
* cleanly to an XBOX controller tend to have DirectInput drivers available, |
| 26 |
* while more recent joypads tend to have a XInput driver available. In theory |
| 27 |
* XInput devices also support DirectInput, and some devices even have a switch |
| 28 |
* that lets the user select the API. But XInput devices only partially support |
| 29 |
* DirectInput. In particular, XInput devices do not support force feedback when |
| 30 |
* using DirectInput to access them. |
| 31 |
* |
| 32 |
* For all these reasons, both directinput and xinput drivers should be |
| 33 |
* supported on Windows to allow greates comfort to end users. |
| 34 |
* The wjoyall.c and whapall.c drivers are wrapper drivers that combine |
| 35 |
* the Allegro XInput and DirectInput drivers into a single driver. |
| 36 |
* For this reason, the same joystick ormay show up twice |
| 37 |
* in the joystick list. However, XInput devices will appear before DirectInput |
| 38 |
* devices in the list, so when in doubt, use the joystick with the lowest ID. |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
|
| 43 |
#define ALLEGRO_NO_COMPATIBILITY |
| 44 |
|
| 45 |
/* For waitable timers */ |
| 46 |
#define _WIN32_WINNT 0x0501 |
| 47 |
|
| 48 |
#include "allegro5/allegro.h" |
| 49 |
#include "allegro5/internal/aintern.h" |
| 50 |
#include "allegro5/platform/aintwin.h" |
| 51 |
#include "allegro5/internal/aintern_events.h" |
| 52 |
#include "allegro5/internal/aintern_joystick.h" |
| 53 |
#include "allegro5/internal/aintern_bitmap.h" |
| 54 |
|
| 55 |
|
| 56 |
#ifdef ALLEGRO_CFG_XINPUT |
| 57 |
|
| 58 |
/* Don't compile this lot if xinput isn't supported. */ |
| 59 |
|
| 60 |
#ifndef ALLEGRO_WINDOWS |
| 61 |
#error something is wrong with the makefile |
| 62 |
#endif |
| 63 |
|
| 64 |
#ifdef ALLEGRO_MINGW32 |
| 65 |
#undef MAKEFOURCC |
| 66 |
#endif |
| 67 |
|
| 68 |
#include <stdio.h> |
| 69 |
|
| 70 |
ALLEGRO_DEBUG_CHANNEL("wjoyall") |
| 71 |
|
| 72 |
#include "allegro5/joystick.h" |
| 73 |
#include "allegro5/internal/aintern_joystick.h" |
| 74 |
#include "allegro5/internal/aintern_wjoyall.h" |
| 75 |
|
| 76 |
/* forward declarations */ |
| 77 |
static bool joyall_init_joystick(void); |
| 78 |
static void joyall_exit_joystick(void); |
| 79 |
static bool joyall_reconfigure_joysticks(void); |
| 80 |
static int joyall_get_num_joysticks(void); |
| 81 |
static ALLEGRO_JOYSTICK *joyall_get_joystick(int num); |
| 82 |
static void joyall_release_joystick(ALLEGRO_JOYSTICK *joy); |
| 83 |
static void joyall_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state); |
| 84 |
static const char *joyall_get_name(ALLEGRO_JOYSTICK *joy); |
| 85 |
static bool joyall_get_active(ALLEGRO_JOYSTICK *joy); |
| 86 |
|
| 87 |
|
| 88 |
/* the driver vtable */ |
| 89 |
ALLEGRO_JOYSTICK_DRIVER _al_joydrv_windows_all = |
| 90 |
{ |
| 91 |
AL_JOY_TYPE_WINDOWS_ALL, |
| 92 |
"", |
| 93 |
"", |
| 94 |
"Windows Joystick", |
| 95 |
joyall_init_joystick, |
| 96 |
joyall_exit_joystick, |
| 97 |
joyall_reconfigure_joysticks, |
| 98 |
joyall_get_num_joysticks, |
| 99 |
joyall_get_joystick, |
| 100 |
joyall_release_joystick, |
| 101 |
joyall_get_joystick_state, |
| 102 |
joyall_get_name, |
| 103 |
joyall_get_active |
| 104 |
}; |
| 105 |
|
| 106 |
/* Mutex to protect state access. XXX is this needed? */ |
| 107 |
static ALLEGRO_MUTEX *joyall_mutex = NULL; |
| 108 |
|
| 109 |
static bool ok_xi = false; |
| 110 |
static bool ok_di = false; |
| 111 |
|
| 112 |
/* Sets up all joysticks from the two wrapped apis. */ |
| 113 |
static void joyall_setup_joysticks(void) |
| 114 |
{ |
| 115 |
int index; |
| 116 |
int num_xinput = 0; |
| 117 |
int num_dinput = 0; |
| 118 |
if (ok_di) |
| 119 |
num_dinput = _al_joydrv_directx.num_joysticks(); |
| 120 |
if (ok_xi) |
| 121 |
num_xinput = _al_joydrv_xinput.num_joysticks(); |
| 122 |
|
| 123 |
for (index = 0; index < num_xinput; index++) { |
| 124 |
ALLEGRO_JOYSTICK *joystick = _al_joydrv_xinput.get_joystick(index); |
| 125 |
joystick->driver = &_al_joydrv_xinput; |
| 126 |
} |
| 127 |
|
| 128 |
for (index = 0; index < num_dinput; index++) { |
| 129 |
ALLEGRO_JOYSTICK *joystick = _al_joydrv_directx.get_joystick(index); |
| 130 |
joystick->driver = &_al_joydrv_directx; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/* Initialization API function. */ |
| 136 |
static bool joyall_init_joystick(void) |
| 137 |
{ |
| 138 |
/* Create the mutex and a condition vaiable. */ |
| 139 |
joyall_mutex = al_create_mutex_recursive(); |
| 140 |
if (!joyall_mutex) |
| 141 |
return false; |
| 142 |
|
| 143 |
al_lock_mutex(joyall_mutex); |
| 144 |
|
| 145 |
ok_xi = _al_joydrv_xinput.init_joystick(); |
| 146 |
ok_di = _al_joydrv_directx.init_joystick(); |
| 147 |
joyall_setup_joysticks(); |
| 148 |
al_unlock_mutex(joyall_mutex); |
| 149 |
return ok_xi || ok_di; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
static void joyall_exit_joystick(void) |
| 154 |
{ |
| 155 |
al_lock_mutex(joyall_mutex); |
| 156 |
_al_joydrv_xinput.exit_joystick(); |
| 157 |
_al_joydrv_directx.exit_joystick(); |
| 158 |
al_unlock_mutex(joyall_mutex); |
| 159 |
al_destroy_mutex(joyall_mutex); |
| 160 |
} |
| 161 |
|
| 162 |
static bool joyall_reconfigure_joysticks(void) |
| 163 |
{ |
| 164 |
al_lock_mutex(joyall_mutex); |
| 165 |
if (ok_xi) |
| 166 |
_al_joydrv_xinput.reconfigure_joysticks(); |
| 167 |
if (ok_di) |
| 168 |
_al_joydrv_directx.reconfigure_joysticks(); |
| 169 |
joyall_setup_joysticks(); |
| 170 |
al_unlock_mutex(joyall_mutex); |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
static int joyall_get_num_joysticks(void) |
| 175 |
{ |
| 176 |
int ret = 0; |
| 177 |
if (ok_xi) |
| 178 |
ret += _al_joydrv_xinput.num_joysticks(); |
| 179 |
if (ok_di) |
| 180 |
ret += _al_joydrv_directx.num_joysticks(); |
| 181 |
return ret; |
| 182 |
} |
| 183 |
|
| 184 |
static ALLEGRO_JOYSTICK *joyall_get_joystick(int num) |
| 185 |
{ |
| 186 |
int num_xinput = 0; |
| 187 |
int num_dinput = 0; |
| 188 |
if (ok_di) |
| 189 |
num_dinput = _al_joydrv_directx.num_joysticks(); |
| 190 |
if (ok_xi) |
| 191 |
num_xinput = _al_joydrv_xinput.num_joysticks(); |
| 192 |
if (num < 0) return NULL; |
| 193 |
/* Shift the joystick number to fit the range of each of the subdrivers */ |
| 194 |
if (num < num_xinput) { |
| 195 |
return _al_joydrv_xinput.get_joystick(num); |
| 196 |
} |
| 197 |
else if (num < (num_xinput + num_dinput)) { |
| 198 |
return _al_joydrv_directx.get_joystick(num - num_xinput); |
| 199 |
} |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
static void joyall_release_joystick(ALLEGRO_JOYSTICK *joy) |
| 204 |
{ |
| 205 |
/* Forward to the driver's function. Here it's OK to use joy |
| 206 |
* since the get_joystick function returns a |
| 207 |
* pointer to the real underlying driver-specific joystick data. |
| 208 |
*/ |
| 209 |
if (joy) { |
| 210 |
joy->driver->release_joystick(joy); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
static void joyall_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state) |
| 215 |
{ |
| 216 |
joy->driver->get_joystick_state(joy, ret_state); |
| 217 |
} |
| 218 |
|
| 219 |
static const char *joyall_get_name(ALLEGRO_JOYSTICK *joy) |
| 220 |
{ |
| 221 |
return joy->driver->get_name(joy); |
| 222 |
} |
| 223 |
|
| 224 |
static bool joyall_get_active(ALLEGRO_JOYSTICK *joy) |
| 225 |
{ |
| 226 |
return joy->driver->get_active(joy); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
#endif /* #ifdef ALLEGRO_CFG_XINPUT */ |