| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Windows DirectInput joystick driver. |
| 12 |
* |
| 13 |
* By Eric Botcazou. |
| 14 |
* |
| 15 |
* Omar Cornut fixed it to handle a weird peculiarity of |
| 16 |
* the DirectInput joystick API. |
| 17 |
* |
| 18 |
* Modified extensively for the new joystick API by Peter Wang. |
| 19 |
* |
| 20 |
* See readme.txt for copyright information. |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
/* |
| 25 |
* Driver operation: |
| 26 |
* |
| 27 |
* 1. When the driver is initialised all the joysticks on the system |
| 28 |
* are enumerated. For each joystick, an ALLEGRO_JOYSTICK_DIRECTX structure |
| 29 |
* is created. A win32 Event is also created for each joystick and DirectInput |
| 30 |
* is told to set that event whenever the joystick state changes. For some |
| 31 |
* devices this is not possible -- they must be polled. In that case, a |
| 32 |
* Waitable Timer object is used instead of a win32 Event. Once all the |
| 33 |
* joysticks are set up, a dedicated background thread is started. |
| 34 |
* |
| 35 |
* 2. When al_get_joystick() is called the address of one of the |
| 36 |
* ALLEGRO_JOYSTICK_DIRECTX structures is returned to the user. |
| 37 |
* |
| 38 |
* 3. The background thread waits upon the win32 Events/Waitable Timer |
| 39 |
* objects. When one of them is triggered, the thread wakes up and |
| 40 |
* reads in buffered joystick events. An internal ALLEGRO_JOYSTICK_STATE |
| 41 |
* structure (part of ALLEGRO_JOYSTICK_DIRECTX) is updated accordingly. |
| 42 |
* Also, any Allegro events are generated if necessary. |
| 43 |
* |
| 44 |
* 4. When the user calls al_get_joystick_state() the contents of the |
| 45 |
* internal ALLEGRO_JOYSTICK_STATE structure are copied to a user |
| 46 |
* ALLEGRO_JOYSTICK_STATE structure. |
| 47 |
* |
| 48 |
* 5. Every second or so, all the joysticks on the system are enumerated again. |
| 49 |
* We compare the GUIDs of the enumerated joysticks with the existing |
| 50 |
* ALLEGRO_JOYSTICK structures to tell if any new joysticks have been connected, |
| 51 |
* or old ones disconnected. |
| 52 |
*/ |
| 53 |
|
| 54 |
|
| 55 |
#define ALLEGRO_NO_COMPATIBILITY |
| 56 |
|
| 57 |
#define DIRECTINPUT_VERSION 0x0800 |
| 58 |
|
| 59 |
/* For waitable timers */ |
| 60 |
#define _WIN32_WINNT 0x0501 |
| 61 |
|
| 62 |
#include "allegro5/allegro.h" |
| 63 |
#include "allegro5/internal/aintern.h" |
| 64 |
#include "allegro5/platform/aintwin.h" |
| 65 |
#include "allegro5/internal/aintern_events.h" |
| 66 |
#include "allegro5/internal/aintern_joystick.h" |
| 67 |
#include "allegro5/internal/aintern_bitmap.h" |
| 68 |
|
| 69 |
|
| 70 |
#ifndef ALLEGRO_WINDOWS |
| 71 |
#error something is wrong with the makefile |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef ALLEGRO_MINGW32 |
| 75 |
#undef MAKEFOURCC |
| 76 |
#endif |
| 77 |
|
| 78 |
#include <stdio.h> |
| 79 |
#include <mmsystem.h> |
| 80 |
#include <process.h> |
| 81 |
#include <dinput.h> |
| 82 |
|
| 83 |
/* We need XInput detection if we actually compile the XInput driver in. |
| 84 |
*/ |
| 85 |
#ifdef ALLEGRO_CFG_XINPUT |
| 86 |
/* Windows XP is required. If you still use older Windows, |
| 87 |
XInput won't work anyway. */ |
| 88 |
#undef WIN32_LEAN_AND_MEAN |
| 89 |
#include <windows.h> |
| 90 |
#include <winuser.h> |
| 91 |
#define ALLEGRO_DINPUT_FILTER_XINPUT |
| 92 |
#endif |
| 93 |
|
| 94 |
ALLEGRO_DEBUG_CHANNEL("dinput") |
| 95 |
|
| 96 |
#include "allegro5/joystick.h" |
| 97 |
#include "allegro5/internal/aintern_joystick.h" |
| 98 |
#include "allegro5/internal/aintern_wjoydxnu.h" |
| 99 |
#include "allegro5/internal/aintern_wunicode.h" |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
/* forward declarations */ |
| 104 |
static bool joydx_init_joystick(void); |
| 105 |
static void joydx_exit_joystick(void); |
| 106 |
static bool joydx_reconfigure_joysticks(void); |
| 107 |
static int joydx_get_num_joysticks(void); |
| 108 |
static ALLEGRO_JOYSTICK *joydx_get_joystick(int num); |
| 109 |
static void joydx_release_joystick(ALLEGRO_JOYSTICK *joy); |
| 110 |
static void joydx_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state); |
| 111 |
static const char *joydx_get_name(ALLEGRO_JOYSTICK *joy); |
| 112 |
static bool joydx_get_active(ALLEGRO_JOYSTICK *joy); |
| 113 |
|
| 114 |
static void joydx_inactivate_joy(ALLEGRO_JOYSTICK_DIRECTX *joy); |
| 115 |
|
| 116 |
static unsigned __stdcall joydx_thread_proc(LPVOID unused); |
| 117 |
static void update_joystick(ALLEGRO_JOYSTICK_DIRECTX *joy); |
| 118 |
static void handle_axis_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT output, DWORD value); |
| 119 |
static void handle_pov_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT *outputs, DWORD _value); |
| 120 |
static void handle_button_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT output, DWORD value); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
/* the driver vtable */ |
| 125 |
ALLEGRO_JOYSTICK_DRIVER _al_joydrv_directx = |
| 126 |
{ |
| 127 |
AL_JOY_TYPE_DIRECTX, |
| 128 |
"", |
| 129 |
"", |
| 130 |
"DirectInput joystick", |
| 131 |
joydx_init_joystick, |
| 132 |
joydx_exit_joystick, |
| 133 |
joydx_reconfigure_joysticks, |
| 134 |
joydx_get_num_joysticks, |
| 135 |
joydx_get_joystick, |
| 136 |
joydx_release_joystick, |
| 137 |
joydx_get_joystick_state, |
| 138 |
joydx_get_name, |
| 139 |
joydx_get_active |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
/* GUID values are borrowed from Wine */ |
| 144 |
#define DEFINE_PRIVATE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \ |
| 145 |
static const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } } |
| 146 |
|
| 147 |
DEFINE_PRIVATE_GUID(__al_GUID_XAxis, 0xA36D02E0,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 148 |
DEFINE_PRIVATE_GUID(__al_GUID_YAxis, 0xA36D02E1,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 149 |
DEFINE_PRIVATE_GUID(__al_GUID_ZAxis, 0xA36D02E2,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 150 |
DEFINE_PRIVATE_GUID(__al_GUID_RxAxis,0xA36D02F4,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 151 |
DEFINE_PRIVATE_GUID(__al_GUID_RyAxis,0xA36D02F5,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 152 |
DEFINE_PRIVATE_GUID(__al_GUID_RzAxis,0xA36D02E3,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 153 |
DEFINE_PRIVATE_GUID(__al_GUID_Slider,0xA36D02E4,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 154 |
DEFINE_PRIVATE_GUID(__al_GUID_Button,0xA36D02F0,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 155 |
DEFINE_PRIVATE_GUID(__al_GUID_POV, 0xA36D02F2,0xC9F3,0x11CF,0xBF,0xC7,0x44,0x45,0x53,0x54,0x00,0x00); |
| 156 |
|
| 157 |
DEFINE_PRIVATE_GUID(__al_IID_IDirectInput8W, 0xBF798031,0x483A,0x4DA2,0xAA,0x99,0x5D,0x64,0xED,0x36,0x97,0x00); |
| 158 |
DEFINE_PRIVATE_GUID(__al_IID_IDirectInput8A, 0xBF798030,0x483A,0x4DA2,0xAA,0x99,0x5D,0x64,0xED,0x36,0x97,0x00); |
| 159 |
DEFINE_PRIVATE_GUID(__al_IID_IDirectInputDevice8A,0x54D41080,0xDC15,0x4833,0xA4,0x1B,0x74,0x8F,0x73,0xA3,0x81,0x79); |
| 160 |
DEFINE_PRIVATE_GUID(__al_IID_IDirectInputDevice8W,0x54D41081,0xDC15,0x4833,0xA4,0x1B,0x74,0x8F,0x73,0xA3,0x81,0x79); |
| 161 |
|
| 162 |
#ifdef UNICODE |
| 163 |
#define __al_IID_IDirectInput8 __al_IID_IDirectInput8W |
| 164 |
#define __al_IID_IDirectInputDevice8 __al_IID_IDirectInputDevice8W |
| 165 |
#else |
| 166 |
#define __al_IID_IDirectInput __al_IID_IDirectInput8A |
| 167 |
#define __al_IID_IDirectInputDevice __al_IID_IDirectInputDevice8A |
| 168 |
#endif |
| 169 |
|
| 170 |
/* definition of DirectInput Joystick was borrowed from Wine implementation */ |
| 171 |
#define DIDFT_AXIS 0x00000003 |
| 172 |
#define DIDFT_ANYINSTANCE 0x00FFFF00 |
| 173 |
#define DIDFT_OPTIONAL 0x80000000 |
| 174 |
|
| 175 |
static const DIOBJECTDATAFORMAT __al_dfDIJoystick[] = { |
| 176 |
{ &__al_GUID_XAxis, DIJOFS_X, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 177 |
{ &__al_GUID_YAxis, DIJOFS_Y, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 178 |
{ &__al_GUID_ZAxis, DIJOFS_Z, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 179 |
{ &__al_GUID_RxAxis, DIJOFS_RX, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 180 |
{ &__al_GUID_RyAxis, DIJOFS_RY, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 181 |
{ &__al_GUID_RzAxis, DIJOFS_RZ, DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 182 |
{ &__al_GUID_Slider, DIJOFS_SLIDER(0), DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 183 |
{ &__al_GUID_Slider, DIJOFS_SLIDER(1), DIDFT_OPTIONAL | DIDFT_AXIS | DIDFT_ANYINSTANCE, 0}, |
| 184 |
{ &__al_GUID_POV, DIJOFS_POV(0), DIDFT_OPTIONAL | DIDFT_POV | DIDFT_ANYINSTANCE, 0}, |
| 185 |
{ &__al_GUID_POV, DIJOFS_POV(1), DIDFT_OPTIONAL | DIDFT_POV | DIDFT_ANYINSTANCE, 0}, |
| 186 |
{ &__al_GUID_POV, DIJOFS_POV(2), DIDFT_OPTIONAL | DIDFT_POV | DIDFT_ANYINSTANCE, 0}, |
| 187 |
{ &__al_GUID_POV, DIJOFS_POV(3), DIDFT_OPTIONAL | DIDFT_POV | DIDFT_ANYINSTANCE, 0}, |
| 188 |
{ NULL, DIJOFS_BUTTON(0), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 189 |
{ NULL, DIJOFS_BUTTON(1), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 190 |
{ NULL, DIJOFS_BUTTON(2), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 191 |
{ NULL, DIJOFS_BUTTON(3), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 192 |
{ NULL, DIJOFS_BUTTON(4), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 193 |
{ NULL, DIJOFS_BUTTON(5), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 194 |
{ NULL, DIJOFS_BUTTON(6), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 195 |
{ NULL, DIJOFS_BUTTON(7), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 196 |
{ NULL, DIJOFS_BUTTON(8), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 197 |
{ NULL, DIJOFS_BUTTON(9), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 198 |
{ NULL, DIJOFS_BUTTON(10), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 199 |
{ NULL, DIJOFS_BUTTON(11), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 200 |
{ NULL, DIJOFS_BUTTON(12), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 201 |
{ NULL, DIJOFS_BUTTON(13), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 202 |
{ NULL, DIJOFS_BUTTON(14), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 203 |
{ NULL, DIJOFS_BUTTON(15), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 204 |
{ NULL, DIJOFS_BUTTON(16), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 205 |
{ NULL, DIJOFS_BUTTON(17), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 206 |
{ NULL, DIJOFS_BUTTON(18), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 207 |
{ NULL, DIJOFS_BUTTON(19), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 208 |
{ NULL, DIJOFS_BUTTON(20), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 209 |
{ NULL, DIJOFS_BUTTON(21), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 210 |
{ NULL, DIJOFS_BUTTON(22), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 211 |
{ NULL, DIJOFS_BUTTON(23), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 212 |
{ NULL, DIJOFS_BUTTON(24), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 213 |
{ NULL, DIJOFS_BUTTON(25), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 214 |
{ NULL, DIJOFS_BUTTON(26), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 215 |
{ NULL, DIJOFS_BUTTON(27), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 216 |
{ NULL, DIJOFS_BUTTON(28), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 217 |
{ NULL, DIJOFS_BUTTON(29), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 218 |
{ NULL, DIJOFS_BUTTON(30), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 219 |
{ NULL, DIJOFS_BUTTON(31), DIDFT_OPTIONAL | DIDFT_BUTTON | DIDFT_ANYINSTANCE, 0}, |
| 220 |
}; |
| 221 |
|
| 222 |
static const DIDATAFORMAT __al_c_dfDIJoystick = { |
| 223 |
sizeof(DIDATAFORMAT), |
| 224 |
sizeof(DIOBJECTDATAFORMAT), |
| 225 |
DIDF_ABSAXIS, |
| 226 |
sizeof(DIJOYSTATE), |
| 227 |
sizeof(__al_dfDIJoystick) / sizeof(*__al_dfDIJoystick), |
| 228 |
(LPDIOBJECTDATAFORMAT)__al_dfDIJoystick |
| 229 |
}; |
| 230 |
/* end of Wine code */ |
| 231 |
|
| 232 |
|
| 233 |
/* DirectInput creation prototype */ |
| 234 |
typedef HRESULT (WINAPI *DIRECTINPUT8CREATEPROC)(HINSTANCE hinst, DWORD dwVersion, REFIID riidltf, LPVOID * ppvOut, LPUNKNOWN punkOuter); |
| 235 |
|
| 236 |
/* DirectInput module name */ |
| 237 |
static const char* _al_dinput_module_name = "dinput8.dll"; |
| 238 |
|
| 239 |
/* DirecInput module handle */ |
| 240 |
static HMODULE _al_dinput_module = NULL; |
| 241 |
|
| 242 |
/* DirectInput creation procedure */ |
| 243 |
static DIRECTINPUT8CREATEPROC _al_dinput_create = (DIRECTINPUT8CREATEPROC)NULL; |
| 244 |
|
| 245 |
/* a handle to the DirectInput interface */ |
| 246 |
static LPDIRECTINPUT joystick_dinput = NULL; |
| 247 |
|
| 248 |
/* last display which acquired the devices */ |
| 249 |
static ALLEGRO_DISPLAY_WIN *win_disp; |
| 250 |
|
| 251 |
/* number of joysticks visible to user */ |
| 252 |
static int joydx_num_joysticks; |
| 253 |
|
| 254 |
/* the joystick structures */ |
| 255 |
static ALLEGRO_JOYSTICK_DIRECTX joydx_joystick[MAX_JOYSTICKS]; |
| 256 |
|
| 257 |
/* for the background thread */ |
| 258 |
static HANDLE joydx_thread = NULL; |
| 259 |
static CRITICAL_SECTION joydx_thread_cs; |
| 260 |
|
| 261 |
/* whether the DirectInput devices need to be enumerated again */ |
| 262 |
static bool need_device_enumeration = false; |
| 263 |
|
| 264 |
/* whether the user should call al_reconfigure_joysticks */ |
| 265 |
static bool config_needs_merging = false; |
| 266 |
|
| 267 |
/* An array of objects that are to wake the background thread when |
| 268 |
* something interesting happens. The first handle is for thread |
| 269 |
* termination. The rest point to joydx_joystick[i].waker_event values, |
| 270 |
* for each active joystick. joydx_joystick[i].waker_event is NOT |
| 271 |
* necessarily at JOYSTICK_WAKER(i). |
| 272 |
*/ |
| 273 |
static HANDLE joydx_thread_wakers[1+MAX_JOYSTICKS]; |
| 274 |
#define STOP_EVENT (joydx_thread_wakers[0]) |
| 275 |
#define JOYSTICK_WAKER(n) (joydx_thread_wakers[1+(n)]) |
| 276 |
|
| 277 |
|
| 278 |
/* names for things in case DirectInput doesn't provide them */ |
| 279 |
static char default_name_x[] = "X"; |
| 280 |
static char default_name_y[] = "Y"; |
| 281 |
static char default_name_z[] = "Z"; |
| 282 |
static char default_name_rx[] = "RX"; |
| 283 |
static char default_name_ry[] = "RY"; |
| 284 |
static char default_name_rz[] = "RZ"; |
| 285 |
static char default_name_stick[] = "stick"; |
| 286 |
static char default_name_slider[] = "slider"; |
| 287 |
static char default_name_hat[] = "hat"; |
| 288 |
static const char *default_name_button[MAX_BUTTONS] = { |
| 289 |
"B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", |
| 290 |
"B9", "B10", "B11", "B12", "B13", "B14", "B15", "B16", |
| 291 |
"B17", "B18", "B19", "B20", "B21", "B22", "B23", "B24", |
| 292 |
"B25", "B26", "B27", "B28", "B29", "B30", "B31", "B32" |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
#define JOY_POVFORWARD_WRAP 36000 |
| 297 |
|
| 298 |
|
| 299 |
/* Returns a pointer to a static buffer, for debugging. */ |
| 300 |
static char *guid_to_string(const GUID * guid) |
| 301 |
{ |
| 302 |
static char buf[200]; |
| 303 |
|
| 304 |
sprintf(buf, "%lx-%x-%x-%x%x%x%x%x%x%x%x", |
| 305 |
guid->Data1, |
| 306 |
guid->Data2, |
| 307 |
guid->Data3, |
| 308 |
guid->Data4[0], |
| 309 |
guid->Data4[1], |
| 310 |
guid->Data4[2], |
| 311 |
guid->Data4[3], |
| 312 |
guid->Data4[4], |
| 313 |
guid->Data4[5], |
| 314 |
guid->Data4[6], |
| 315 |
guid->Data4[7] |
| 316 |
); |
| 317 |
|
| 318 |
return buf; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
/* Returns a pointer to a static buffer, for debugging. */ |
| 323 |
static char *joydx_guid_string(ALLEGRO_JOYSTICK_DIRECTX *joy) |
| 324 |
{ |
| 325 |
return guid_to_string((const GUID *)&joy->guid); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/* dinput_err_str: |
| 330 |
* Returns a DirectInput error string. |
| 331 |
*/ |
| 332 |
#ifdef DEBUGMODE |
| 333 |
static char* dinput_err_str(long err) |
| 334 |
{ |
| 335 |
static char err_str[64]; |
| 336 |
|
| 337 |
switch (err) { |
| 338 |
|
| 339 |
case DIERR_ACQUIRED: |
| 340 |
_al_sane_strncpy(err_str, "the device is acquired", sizeof(err_str)); |
| 341 |
break; |
| 342 |
|
| 343 |
case DIERR_NOTACQUIRED: |
| 344 |
_al_sane_strncpy(err_str, "the device is not acquired", sizeof(err_str)); |
| 345 |
break; |
| 346 |
|
| 347 |
case DIERR_INPUTLOST: |
| 348 |
_al_sane_strncpy(err_str, "access to the device was not granted", sizeof(err_str)); |
| 349 |
break; |
| 350 |
|
| 351 |
case DIERR_INVALIDPARAM: |
| 352 |
_al_sane_strncpy(err_str, "the device does not have a selected data format", sizeof(err_str)); |
| 353 |
break; |
| 354 |
|
| 355 |
case DIERR_OTHERAPPHASPRIO: |
| 356 |
_al_sane_strncpy(err_str, "can't acquire the device in background", sizeof(err_str)); |
| 357 |
break; |
| 358 |
|
| 359 |
default: |
| 360 |
_al_sane_strncpy(err_str, "unknown error", sizeof(err_str)); |
| 361 |
} |
| 362 |
|
| 363 |
return err_str; |
| 364 |
} |
| 365 |
#else |
| 366 |
#define dinput_err_str(hr) "\0" |
| 367 |
#endif |
| 368 |
|
| 369 |
|
| 370 |
/* _al_win_joystick_dinput_acquire: [window thread] |
| 371 |
* Acquires the joystick devices. |
| 372 |
*/ |
| 373 |
static void joystick_dinput_acquire(void) |
| 374 |
{ |
| 375 |
HRESULT hr; |
| 376 |
int i; |
| 377 |
|
| 378 |
if (!joystick_dinput) |
| 379 |
return; |
| 380 |
|
| 381 |
for (i=0; i < MAX_JOYSTICKS; i++) { |
| 382 |
if (joydx_joystick[i].device) { |
| 383 |
hr = IDirectInputDevice8_Acquire(joydx_joystick[i].device); |
| 384 |
|
| 385 |
if (FAILED(hr)) |
| 386 |
ALLEGRO_ERROR("acquire joystick %d failed: %s\n", i, dinput_err_str(hr)); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
/* _al_win_joystick_dinput_trigger_enumeration: [window thread] |
| 393 |
* Let joydx_thread_proc() know to reenumerate joysticks. |
| 394 |
*/ |
| 395 |
void _al_win_joystick_dinput_trigger_enumeration(void) |
| 396 |
{ |
| 397 |
if (!joystick_dinput) |
| 398 |
return; |
| 399 |
EnterCriticalSection(&joydx_thread_cs); |
| 400 |
need_device_enumeration = true; |
| 401 |
LeaveCriticalSection(&joydx_thread_cs); |
| 402 |
} |
| 403 |
|
| 404 |
/* _al_win_joystick_dinput_unacquire: [window thread] |
| 405 |
* Unacquires the joystick devices. |
| 406 |
*/ |
| 407 |
void _al_win_joystick_dinput_unacquire(void *unused) |
| 408 |
{ |
| 409 |
int i; |
| 410 |
|
| 411 |
(void)unused; |
| 412 |
|
| 413 |
if (joystick_dinput && win_disp) { |
| 414 |
for (i=0; i < MAX_JOYSTICKS; i++) { |
| 415 |
if (joydx_joystick[i].device) { |
| 416 |
ALLEGRO_DEBUG("Unacquiring joystick device at slot %d\n", i); |
| 417 |
IDirectInputDevice8_Unacquire(joydx_joystick[i].device); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
/* _al_win_joystick_dinput_grab: [window thread] |
| 425 |
* Grabs the joystick devices. |
| 426 |
*/ |
| 427 |
void _al_win_joystick_dinput_grab(void *param) |
| 428 |
{ |
| 429 |
int i; |
| 430 |
|
| 431 |
if (!joystick_dinput) |
| 432 |
return; |
| 433 |
|
| 434 |
/* Release the input from the previous window just in case, |
| 435 |
otherwise set cooperative level will fail. */ |
| 436 |
if (win_disp) { |
| 437 |
_al_win_wnd_call_proc(win_disp->window, _al_win_joystick_dinput_unacquire, NULL); |
| 438 |
} |
| 439 |
|
| 440 |
win_disp = (ALLEGRO_DISPLAY_WIN *)param; |
| 441 |
|
| 442 |
/* set cooperative level */ |
| 443 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 444 |
HRESULT hr; |
| 445 |
|
| 446 |
if (!joydx_joystick[i].device) |
| 447 |
continue; |
| 448 |
|
| 449 |
hr = IDirectInputDevice8_SetCooperativeLevel(joydx_joystick[i].device, win_disp->window, |
| 450 |
DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); |
| 451 |
if (FAILED(hr)) { |
| 452 |
ALLEGRO_ERROR("IDirectInputDevice8_SetCooperativeLevel failed.\n"); |
| 453 |
return; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
joystick_dinput_acquire(); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
static ALLEGRO_JOYSTICK_DIRECTX *joydx_by_guid(const GUID guid, |
| 462 |
const GUID product_guid) |
| 463 |
{ |
| 464 |
unsigned i; |
| 465 |
|
| 466 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 467 |
if ( |
| 468 |
GUID_EQUAL(joydx_joystick[i].guid, guid) && |
| 469 |
GUID_EQUAL(joydx_joystick[i].product_guid, product_guid) |
| 470 |
/* && |
| 471 |
joydx_joystick[i].config_state == STATE_ALIVE */ |
| 472 |
) |
| 473 |
return &joydx_joystick[i]; |
| 474 |
} |
| 475 |
|
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
static ALLEGRO_JOYSTICK_DIRECTX *joydx_allocate_structure(int *num) |
| 481 |
{ |
| 482 |
int i; |
| 483 |
|
| 484 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 485 |
if (joydx_joystick[i].config_state == STATE_UNUSED) { |
| 486 |
*num = i; |
| 487 |
return &joydx_joystick[i]; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
*num = -1; |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
/* object_enum_callback: [primary thread] |
| 497 |
* Helper function to find out what objects we have on the device. |
| 498 |
*/ |
| 499 |
static BOOL CALLBACK object_enum_callback(LPCDIDEVICEOBJECTINSTANCE lpddoi, LPVOID pvRef) |
| 500 |
{ |
| 501 |
#define GUIDTYPE_EQ(x) GUID_EQUAL(lpddoi->guidType, x) |
| 502 |
|
| 503 |
CAPS_AND_NAMES *can = (CAPS_AND_NAMES *)pvRef; |
| 504 |
|
| 505 |
if (GUIDTYPE_EQ(__al_GUID_XAxis)) { |
| 506 |
can->have_x = true; |
| 507 |
_tcsncpy(can->name_x, lpddoi->tszName, NAME_LEN); |
| 508 |
} |
| 509 |
else if (GUIDTYPE_EQ(__al_GUID_YAxis)) { |
| 510 |
can->have_y = true; |
| 511 |
_tcsncpy(can->name_y, lpddoi->tszName, NAME_LEN); |
| 512 |
} |
| 513 |
else if (GUIDTYPE_EQ(__al_GUID_ZAxis)) { |
| 514 |
can->have_z = true; |
| 515 |
_tcsncpy(can->name_z, lpddoi->tszName, NAME_LEN); |
| 516 |
} |
| 517 |
else if (GUIDTYPE_EQ(__al_GUID_RxAxis)) { |
| 518 |
can->have_rx = true; |
| 519 |
_tcsncpy(can->name_rx, lpddoi->tszName, NAME_LEN); |
| 520 |
} |
| 521 |
else if (GUIDTYPE_EQ(__al_GUID_RyAxis)) { |
| 522 |
can->have_ry = true; |
| 523 |
_tcsncpy(can->name_ry, lpddoi->tszName, NAME_LEN); |
| 524 |
} |
| 525 |
else if (GUIDTYPE_EQ(__al_GUID_RzAxis)) { |
| 526 |
can->have_rz = true; |
| 527 |
_tcsncpy(can->name_rz, lpddoi->tszName, NAME_LEN); |
| 528 |
} |
| 529 |
else if (GUIDTYPE_EQ(__al_GUID_Slider)) { |
| 530 |
if (can->num_sliders < MAX_SLIDERS) { |
| 531 |
_tcsncpy(can->name_slider[can->num_sliders], lpddoi->tszName, |
| 532 |
NAME_LEN); |
| 533 |
can->num_sliders++; |
| 534 |
} |
| 535 |
} |
| 536 |
else if (GUIDTYPE_EQ(__al_GUID_POV)) { |
| 537 |
if (can->num_povs < MAX_POVS) { |
| 538 |
_tcsncpy(can->name_pov[can->num_povs], lpddoi->tszName, |
| 539 |
NAME_LEN); |
| 540 |
can->num_povs++; |
| 541 |
} |
| 542 |
} |
| 543 |
else if (GUIDTYPE_EQ(__al_GUID_Button)) { |
| 544 |
if (can->num_buttons < MAX_BUTTONS) { |
| 545 |
_tcsncpy(can->name_button[can->num_buttons], lpddoi->tszName, |
| 546 |
NAME_LEN); |
| 547 |
can->num_buttons++; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
return DIENUM_CONTINUE; |
| 552 |
|
| 553 |
#undef GUIDTYPE_EQ |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
/* fill_standard_gamepad: [primary thread] |
| 558 |
*/ |
| 559 |
static bool fill_standard_gamepad(ALLEGRO_JOYSTICK_DIRECTX *joy, |
| 560 |
const CAPS_AND_NAMES *can) |
| 561 |
{ |
| 562 |
DIPROPDWORD property_vidpid = |
| 563 |
{ |
| 564 |
/* the header */ |
| 565 |
{ |
| 566 |
sizeof(DIPROPDWORD), // diph.dwSize |
| 567 |
sizeof(DIPROPHEADER), // diph.dwHeaderSize |
| 568 |
0, // diph.dwObj |
| 569 |
DIPH_DEVICE, // diph.dwHow |
| 570 |
}, |
| 571 |
|
| 572 |
/* the data */ |
| 573 |
0, // dwData |
| 574 |
}; |
| 575 |
|
| 576 |
DIPROPSTRING property_productname = |
| 577 |
{ |
| 578 |
/* the header */ |
| 579 |
{ |
| 580 |
sizeof(DIPROPSTRING), // diph.dwSize |
| 581 |
sizeof(DIPROPHEADER), // diph.dwHeaderSize |
| 582 |
0, // diph.dwObj |
| 583 |
DIPH_DEVICE, // diph.dwHow |
| 584 |
}, |
| 585 |
|
| 586 |
/* the data */ |
| 587 |
0, // wsz |
| 588 |
}; |
| 589 |
|
| 590 |
if (FAILED(IDirectInputDevice8_GetProperty(joy->device, DIPROP_VIDPID, &property_vidpid.diph))) |
| 591 |
return false; |
| 592 |
|
| 593 |
int vendor = LOWORD(property_vidpid.dwData); |
| 594 |
int product = HIWORD(property_vidpid.dwData); |
| 595 |
|
| 596 |
if (FAILED(IDirectInputDevice8_GetProperty(joy->device, DIPROP_PRODUCTNAME, &property_productname.diph))) |
| 597 |
return false; |
| 598 |
|
| 599 |
const char *product_string = _twin_tchar_to_utf8(property_productname.wsz); |
| 600 |
|
| 601 |
|
| 602 |
ALLEGRO_JOYSTICK_GUID guid; |
| 603 |
if (vendor && product) |
| 604 |
guid = _al_new_joystick_guid(0x03, vendor, product, 0, NULL, product_string, 0, 0); |
| 605 |
else |
| 606 |
guid = _al_new_joystick_guid(0x05, vendor, product, 0, NULL, product_string, 0, 0); |
| 607 |
joy->parent.info.guid = guid; |
| 608 |
|
| 609 |
const _AL_JOYSTICK_MAPPING *mapping = _al_get_gamepad_mapping("Windows", guid); |
| 610 |
if (!mapping) |
| 611 |
return false; |
| 612 |
|
| 613 |
int axis = 0; |
| 614 |
int num_axes_mapped = 0; |
| 615 |
int num_hats_mapped = 0; |
| 616 |
int num_buttons_mapped = 0; |
| 617 |
_AL_JOYSTICK_OUTPUT *output; |
| 618 |
int i = 0; |
| 619 |
|
| 620 |
if (can->have_x) { |
| 621 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 622 |
if (output) { |
| 623 |
joy->x_mapping = *output; |
| 624 |
num_axes_mapped++; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
if (can->have_y) { |
| 629 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 630 |
if (output) { |
| 631 |
joy->y_mapping = *output; |
| 632 |
num_axes_mapped++; |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
if (can->have_z) { |
| 637 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 638 |
if (output) { |
| 639 |
joy->z_mapping = *output; |
| 640 |
num_axes_mapped++; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
if (can->have_rx) { |
| 645 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 646 |
if (output) { |
| 647 |
joy->rx_mapping = *output; |
| 648 |
num_axes_mapped++; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
if (can->have_ry) { |
| 653 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 654 |
if (output) { |
| 655 |
joy->ry_mapping = *output; |
| 656 |
num_axes_mapped++; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if (can->have_rz) { |
| 661 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 662 |
if (output) { |
| 663 |
joy->rz_mapping = *output; |
| 664 |
num_axes_mapped++; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
for (i = 0; i < can->num_sliders; i++) { |
| 669 |
output = _al_get_joystick_output(&mapping->axis_map, axis++); |
| 670 |
if (output) { |
| 671 |
joy->slider_mapping[i] = *output; |
| 672 |
num_axes_mapped++; |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
for (i = 0; i < 2 * can->num_povs; i++) { |
| 677 |
output = _al_get_joystick_output(&mapping->hat_map, i); |
| 678 |
if (output) { |
| 679 |
joy->pov_mapping[i] = *output; |
| 680 |
num_hats_mapped++; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
for (i = 0; i < can->num_buttons; i++) { |
| 685 |
output = _al_get_joystick_output(&mapping->button_map, i); |
| 686 |
if (output) { |
| 687 |
joy->button_mapping[i] = *output; |
| 688 |
num_buttons_mapped++; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
if (num_axes_mapped != (int)_al_vector_size(&mapping->axis_map)) { |
| 693 |
return false; |
| 694 |
} |
| 695 |
if (num_hats_mapped != (int)_al_vector_size(&mapping->hat_map)) { |
| 696 |
return false; |
| 697 |
} |
| 698 |
if (num_buttons_mapped != (int)_al_vector_size(&mapping->button_map)) { |
| 699 |
return false; |
| 700 |
} |
| 701 |
|
| 702 |
_al_fill_gamepad_info(&joy->parent.info); |
| 703 |
|
| 704 |
memcpy(joy->name, mapping->name, sizeof(mapping->name)); |
| 705 |
|
| 706 |
return true; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
/* fill_joystick_info_using_caps_and_names: [primary thread] |
| 711 |
* Helper to fill in the contents of the joystick structure using the |
| 712 |
* information painstakingly stored into the caps_and_names substructure. |
| 713 |
*/ |
| 714 |
static void fill_joystick_info_using_caps_and_names(ALLEGRO_JOYSTICK_DIRECTX *joy, |
| 715 |
const CAPS_AND_NAMES *can) |
| 716 |
{ |
| 717 |
_AL_JOYSTICK_INFO *info = &joy->parent.info; |
| 718 |
int i; |
| 719 |
|
| 720 |
#define N_STICK (info->num_sticks) |
| 721 |
#define N_AXIS (info->stick[N_STICK].num_axes) |
| 722 |
#define ADD_STRING(A, dfl) ((A) ? _twin_tchar_to_utf8(A) : _al_strdup(dfl)) |
| 723 |
|
| 724 |
/* the X, Y, Z axes make up the first stick */ |
| 725 |
if (can->have_x || can->have_y || can->have_z) { |
| 726 |
if (can->have_x) { |
| 727 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 728 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_x, default_name_x); |
| 729 |
joy->x_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 730 |
N_AXIS++; |
| 731 |
} |
| 732 |
|
| 733 |
if (can->have_y) { |
| 734 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 735 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_y, default_name_y); |
| 736 |
joy->y_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 737 |
N_AXIS++; |
| 738 |
} |
| 739 |
|
| 740 |
if (can->have_z) { |
| 741 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 742 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_z, default_name_z); |
| 743 |
joy->z_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 744 |
N_AXIS++; |
| 745 |
} |
| 746 |
|
| 747 |
info->stick[N_STICK].name = ADD_STRING(NULL, default_name_stick); |
| 748 |
N_STICK++; |
| 749 |
} |
| 750 |
|
| 751 |
/* the Rx, Ry, Rz axes make up the next stick */ |
| 752 |
if (can->have_rx || can->have_ry || can->have_rz) { |
| 753 |
if (can->have_rx) { |
| 754 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 755 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_rx, default_name_rx); |
| 756 |
joy->rx_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 757 |
N_AXIS++; |
| 758 |
} |
| 759 |
|
| 760 |
if (can->have_ry) { |
| 761 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 762 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_ry, default_name_ry); |
| 763 |
joy->ry_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 764 |
N_AXIS++; |
| 765 |
} |
| 766 |
|
| 767 |
if (can->have_rz) { |
| 768 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 769 |
info->stick[N_STICK].axis[N_AXIS].name = ADD_STRING(can->name_rz, default_name_rz); |
| 770 |
joy->rz_mapping = _al_new_joystick_stick_output(N_STICK, N_AXIS); |
| 771 |
N_AXIS++; |
| 772 |
} |
| 773 |
|
| 774 |
info->stick[N_STICK].name = _al_strdup(default_name_stick); |
| 775 |
N_STICK++; |
| 776 |
} |
| 777 |
|
| 778 |
/* sliders are assigned to one stick each */ |
| 779 |
for (i = 0; i < can->num_sliders; i++) { |
| 780 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL | ALLEGRO_JOYFLAG_ANALOGUE; |
| 781 |
info->stick[N_STICK].num_axes = 1; |
| 782 |
info->stick[N_STICK].axis[0].name = _al_strdup("axis"); |
| 783 |
info->stick[N_STICK].name = ADD_STRING(can->name_slider[i], default_name_slider); |
| 784 |
joy->slider_mapping[i] = _al_new_joystick_stick_output(N_STICK, 0); |
| 785 |
N_STICK++; |
| 786 |
} |
| 787 |
|
| 788 |
/* POV devices are assigned to one stick each */ |
| 789 |
for (i = 0; i < 2 * can->num_povs; i += 2) { |
| 790 |
info->stick[N_STICK].flags = ALLEGRO_JOYFLAG_DIGITAL; |
| 791 |
info->stick[N_STICK].num_axes = 2; |
| 792 |
info->stick[N_STICK].axis[0].name = _al_strdup("left/right"); |
| 793 |
info->stick[N_STICK].axis[1].name = _al_strdup("up/down"); |
| 794 |
info->stick[N_STICK].name = ADD_STRING(can->name_pov[i], default_name_hat); |
| 795 |
joy->pov_mapping[i] = _al_new_joystick_stick_output(N_STICK, 0); |
| 796 |
joy->pov_mapping[i + 1] = _al_new_joystick_stick_output(N_STICK, 1); |
| 797 |
N_STICK++; |
| 798 |
} |
| 799 |
|
| 800 |
/* buttons */ |
| 801 |
for (i = 0; i < can->num_buttons; i++) { |
| 802 |
info->button[i].name = ADD_STRING(can->name_button[i], default_name_button[i]); |
| 803 |
joy->button_mapping[i] = _al_new_joystick_button_output(i); |
| 804 |
} |
| 805 |
|
| 806 |
info->num_buttons = can->num_buttons; |
| 807 |
|
| 808 |
/* correct buggy MP-8866 Dual USB Joypad info received from DirectInput */ |
| 809 |
if (strstr(joy->name, "MP-8866")) { |
| 810 |
/* axes were mapped weird; remap as expected */ |
| 811 |
/* really R-stick X axis */ |
| 812 |
joy->z_mapping = _al_new_joystick_stick_output(1, 0); |
| 813 |
|
| 814 |
/* really R-stick Y axis */ |
| 815 |
joy->rz_mapping = _al_new_joystick_stick_output(1, 1); |
| 816 |
|
| 817 |
info->stick[0].num_axes = 2; |
| 818 |
info->stick[1].num_axes = 2; |
| 819 |
|
| 820 |
/* reuse the axis names from the first stick */ |
| 821 |
al_free(info->stick[1].axis[0].name); |
| 822 |
al_free(info->stick[1].axis[1].name); |
| 823 |
al_free(info->stick[2].axis[0].name); |
| 824 |
al_free(info->stick[2].axis[1].name); |
| 825 |
info->stick[1].axis[0].name = _al_strdup(info->stick[0].axis[0].name); |
| 826 |
info->stick[1].axis[1].name = _al_strdup(info->stick[0].axis[1].name); |
| 827 |
info->stick[2].axis[0].name = _al_strdup(info->stick[0].axis[0].name); |
| 828 |
info->stick[2].axis[1].name = _al_strdup(info->stick[0].axis[1].name); |
| 829 |
|
| 830 |
/* first four button names contained junk; replace with valid strings */ |
| 831 |
info->button[0].name = _al_strdup("Triangle"); |
| 832 |
info->button[1].name = _al_strdup("Circle"); |
| 833 |
info->button[2].name = _al_strdup("X"); |
| 834 |
info->button[3].name = _al_strdup("Square"); |
| 835 |
|
| 836 |
/* while we're at it, give these controls more sensible names, too */ |
| 837 |
info->stick[0].name = _al_strdup("[L-stick] or D-pad"); |
| 838 |
info->stick[1].name = _al_strdup("[R-stick]"); |
| 839 |
info->stick[2].name = _al_strdup("[D-pad]"); |
| 840 |
} |
| 841 |
|
| 842 |
#undef N_AXIS |
| 843 |
#undef N_STICK |
| 844 |
#undef ADD_STRING |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
#ifdef ALLEGRO_DINPUT_FILTER_XINPUT |
| 849 |
|
| 850 |
/* A better Xinput detection method inspired by from SDL. |
| 851 |
* The method proposed by Microsoft on their web site is problematic because it |
| 852 |
* requires non standard compiler extensions and hard to get headers. |
| 853 |
* This method only needs Windows XP and user32.dll. |
| 854 |
*/ |
| 855 |
static bool dinput_is_device_xinput(const GUID *guid) |
| 856 |
{ |
| 857 |
PRAWINPUTDEVICELIST device_list = NULL; |
| 858 |
UINT amount = 0; |
| 859 |
UINT i; |
| 860 |
bool result = false; |
| 861 |
bool found = false; |
| 862 |
|
| 863 |
/* Go through RAWINPUT (WinXP and later) to find HID devices. */ |
| 864 |
if ((GetRawInputDeviceList(NULL, &amount, sizeof (RAWINPUTDEVICELIST)) |
| 865 |
!= 0)) { |
| 866 |
ALLEGRO_ERROR("Could not get amount of raw input devices.\n"); |
| 867 |
return false; /* drat... */ |
| 868 |
} |
| 869 |
|
| 870 |
if (amount < 1) { |
| 871 |
ALLEGRO_ERROR("Could not get any of raw input devices.\n"); |
| 872 |
return false; /* drat again... */ |
| 873 |
} |
| 874 |
|
| 875 |
device_list = (PRAWINPUTDEVICELIST) |
| 876 |
al_malloc(sizeof(RAWINPUTDEVICELIST) * amount); |
| 877 |
|
| 878 |
if (!device_list) { |
| 879 |
ALLEGRO_ERROR("Could allocate memory for raw input devices.\n"); |
| 880 |
return false; /* No luck. */ |
| 881 |
} |
| 882 |
|
| 883 |
if (GetRawInputDeviceList(device_list, &amount, sizeof(RAWINPUTDEVICELIST)) |
| 884 |
== ((UINT)-1)) { |
| 885 |
ALLEGRO_ERROR("Could not retrieve %d raw input devices.\n", amount); |
| 886 |
al_free((void *)device_list); |
| 887 |
return false; |
| 888 |
} |
| 889 |
|
| 890 |
for (i = 0; i < amount; i++) { |
| 891 |
PRAWINPUTDEVICELIST device = device_list + i; |
| 892 |
RID_DEVICE_INFO rdi; |
| 893 |
char device_name[128]; |
| 894 |
UINT rdi_size = sizeof (rdi); |
| 895 |
UINT name_size = 127; |
| 896 |
rdi.cbSize = sizeof (rdi); |
| 897 |
|
| 898 |
/* Get device info. */ |
| 899 |
if (GetRawInputDeviceInfoA(device->hDevice, RIDI_DEVICEINFO, |
| 900 |
&rdi, &rdi_size) == ((UINT)-1)) { |
| 901 |
ALLEGRO_ERROR("Could not get raw device info for list index %d.\n", i); |
| 902 |
continue; |
| 903 |
} |
| 904 |
/* See if vendor and product id match. */ |
| 905 |
if (MAKELONG(rdi.hid.dwVendorId, rdi.hid.dwProductId) |
| 906 |
!= ((LONG)guid->Data1)) |
| 907 |
continue; |
| 908 |
found = true; |
| 909 |
/* Get device name */ |
| 910 |
memset(device_name, 0, 128); |
| 911 |
if(GetRawInputDeviceInfoA(device->hDevice, RIDI_DEVICENAME, |
| 912 |
device_name, &name_size) == ((UINT)-1)) { |
| 913 |
ALLEGRO_ERROR("Could not get raw device name for list index %d.\n", i); |
| 914 |
continue; |
| 915 |
} |
| 916 |
/* See if there is IG_ in the name, if it is , it's an XInput device. */ |
| 917 |
ALLEGRO_DEBUG("Checking for XInput : %s\n", device_name); |
| 918 |
if (strstr(device_name, "IG_") != NULL) { |
| 919 |
ALLEGRO_DEBUG("Device %s is an XInput device.\n", device_name); |
| 920 |
result = true; |
| 921 |
break; |
| 922 |
} |
| 923 |
} |
| 924 |
if (!found) { |
| 925 |
ALLEGRO_ERROR("Could not find device %s in the raw device list.\n", |
| 926 |
guid_to_string(guid)); |
| 927 |
result = true; |
| 928 |
/* Ignore "mystery" devices. Testing shows that on MinGW these are never |
| 929 |
valid DirectInput devices. Real ones should show up in |
| 930 |
the raw device list. */ |
| 931 |
} |
| 932 |
al_free((void *)device_list); |
| 933 |
return result; |
| 934 |
} |
| 935 |
|
| 936 |
#endif |
| 937 |
|
| 938 |
/* joystick_enum_callback: [primary thread] |
| 939 |
* Helper function to find out how many joysticks we have and set them up. |
| 940 |
* At the end joydx_num_joysticks and joydx_joystick[] will be initialised. |
| 941 |
*/ |
| 942 |
static BOOL CALLBACK joystick_enum_callback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) |
| 943 |
{ |
| 944 |
DIPROPRANGE property_range = |
| 945 |
{ |
| 946 |
/* the header */ |
| 947 |
{ |
| 948 |
sizeof(DIPROPRANGE), // diph.dwSize |
| 949 |
sizeof(DIPROPHEADER), // diph.dwHeaderSize |
| 950 |
0, // diph.dwObj |
| 951 |
DIPH_DEVICE, // diph.dwHow |
| 952 |
}, |
| 953 |
|
| 954 |
/* the data */ |
| 955 |
-32767, // lMin |
| 956 |
+32767 // lMax |
| 957 |
}; |
| 958 |
|
| 959 |
DIPROPDWORD property_deadzone = |
| 960 |
{ |
| 961 |
/* the header */ |
| 962 |
{ |
| 963 |
sizeof(DIPROPDWORD), // diph.dwSize |
| 964 |
sizeof(DIPROPHEADER), // diph.dwHeaderSize |
| 965 |
0, // diph.dwObj |
| 966 |
DIPH_DEVICE, // diph.dwHow |
| 967 |
}, |
| 968 |
|
| 969 |
/* the data */ |
| 970 |
0, // dwData |
| 971 |
}; |
| 972 |
|
| 973 |
DIPROPDWORD property_buffersize = |
| 974 |
{ |
| 975 |
/* the header */ |
| 976 |
{ |
| 977 |
sizeof(DIPROPDWORD), // diph.dwSize |
| 978 |
sizeof(DIPROPHEADER), // diph.dwHeaderSize |
| 979 |
0, // diph.dwObj |
| 980 |
DIPH_DEVICE, // diph.dwHow |
| 981 |
}, |
| 982 |
|
| 983 |
/* the data */ |
| 984 |
DEVICE_BUFFER_SIZE // number of data items |
| 985 |
}; |
| 986 |
|
| 987 |
LPDIRECTINPUTDEVICE _dinput_device1; |
| 988 |
LPDIRECTINPUTDEVICE2 dinput_device = NULL; |
| 989 |
HRESULT hr; |
| 990 |
LPVOID temp; |
| 991 |
CAPS_AND_NAMES caps_and_names; |
| 992 |
ALLEGRO_JOYSTICK_DIRECTX *joy = NULL; |
| 993 |
int num; |
| 994 |
|
| 995 |
(void)pvRef; |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
/* check if the joystick already existed before |
| 1000 |
* Aslo have to check the product GUID because devices like the Logitech |
| 1001 |
* F710 have a backside switch that will change the product GUID, |
| 1002 |
* but not the instance guid. |
| 1003 |
*/ |
| 1004 |
joy = joydx_by_guid(lpddi->guidInstance, lpddi->guidProduct); |
| 1005 |
if (joy) { |
| 1006 |
ALLEGRO_DEBUG("Device %s still exists\n", joydx_guid_string(joy)); |
| 1007 |
joy->marked = true; |
| 1008 |
return DIENUM_CONTINUE; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* If we are compiling the XInput driver, ignore XInput devices, those can |
| 1012 |
go though the XInput driver, unless if the DirectInput driver |
| 1013 |
was set explicitly in the configuration. */ |
| 1014 |
#ifdef ALLEGRO_DINPUT_FILTER_XINPUT |
| 1015 |
if (dinput_is_device_xinput(&lpddi->guidProduct)) { |
| 1016 |
ALLEGRO_DEBUG("Filtered out XInput device %s\n", guid_to_string(&lpddi->guidInstance)); |
| 1017 |
goto Error; |
| 1018 |
} |
| 1019 |
#endif |
| 1020 |
|
| 1021 |
/* create the DirectInput joystick device */ |
| 1022 |
hr = IDirectInput8_CreateDevice(joystick_dinput, lpddi->guidInstance, &_dinput_device1, NULL); |
| 1023 |
if (FAILED(hr)) |
| 1024 |
goto Error; |
| 1025 |
|
| 1026 |
/* query the DirectInputDevice2 interface needed for the poll() method */ |
| 1027 |
hr = IDirectInputDevice8_QueryInterface(_dinput_device1, __al_IID_IDirectInputDevice8, &temp); |
| 1028 |
IDirectInputDevice8_Release(_dinput_device1); |
| 1029 |
if (FAILED(hr)) |
| 1030 |
goto Error; |
| 1031 |
|
| 1032 |
dinput_device = (LPDIRECTINPUTDEVICE2)temp; |
| 1033 |
|
| 1034 |
/* enumerate objects available on the device */ |
| 1035 |
memset(&caps_and_names, 0, sizeof(caps_and_names)); |
| 1036 |
hr = IDirectInputDevice8_EnumObjects(dinput_device, object_enum_callback, |
| 1037 |
&caps_and_names, DIDFT_PSHBUTTON | DIDFT_AXIS | DIDFT_POV); |
| 1038 |
if (FAILED(hr)) |
| 1039 |
goto Error; |
| 1040 |
|
| 1041 |
/* set data format */ |
| 1042 |
hr = IDirectInputDevice8_SetDataFormat(dinput_device, &__al_c_dfDIJoystick); |
| 1043 |
if (FAILED(hr)) |
| 1044 |
goto Error; |
| 1045 |
|
| 1046 |
/* set the range of axes */ |
| 1047 |
hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_RANGE, &property_range.diph); |
| 1048 |
if (FAILED(hr)) |
| 1049 |
goto Error; |
| 1050 |
|
| 1051 |
/* set the dead zone of axes */ |
| 1052 |
hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_DEADZONE, &property_deadzone.diph); |
| 1053 |
if (FAILED(hr)) |
| 1054 |
goto Error; |
| 1055 |
|
| 1056 |
/* set the buffer size */ |
| 1057 |
hr = IDirectInputDevice8_SetProperty(dinput_device, DIPROP_BUFFERSIZE, &property_buffersize.diph); |
| 1058 |
if (FAILED(hr)) |
| 1059 |
goto Error; |
| 1060 |
|
| 1061 |
/* set up the joystick structure */ |
| 1062 |
joy = joydx_allocate_structure(&num); |
| 1063 |
if (!joy) { |
| 1064 |
ALLEGRO_ERROR("Joystick array full\n"); |
| 1065 |
goto Error; |
| 1066 |
} |
| 1067 |
|
| 1068 |
joy->config_state = STATE_BORN; |
| 1069 |
joy->marked = true; |
| 1070 |
joy->device = dinput_device; |
| 1071 |
memcpy(&joy->guid, &lpddi->guidInstance, sizeof(GUID)); |
| 1072 |
memcpy(&joy->product_guid, &lpddi->guidProduct, sizeof(GUID)); |
| 1073 |
|
| 1074 |
_twin_copy_tchar_to_utf8(joy->name, lpddi->tszInstanceName, sizeof(joy->name)); |
| 1075 |
|
| 1076 |
/* fill in the joystick structure */ |
| 1077 |
if (!fill_standard_gamepad(joy, &caps_and_names)) |
| 1078 |
fill_joystick_info_using_caps_and_names(joy, &caps_and_names); |
| 1079 |
|
| 1080 |
/* create a thread event for this joystick, unless it was already created */ |
| 1081 |
joy->waker_event = CreateEvent(NULL, false, false, NULL); |
| 1082 |
|
| 1083 |
/* tell the joystick background thread to wake up when this joystick |
| 1084 |
* device's state changes |
| 1085 |
*/ |
| 1086 |
hr = IDirectInputDevice8_SetEventNotification(joy->device, joy->waker_event); |
| 1087 |
|
| 1088 |
if (FAILED(hr)) { |
| 1089 |
ALLEGRO_ERROR("SetEventNotification failed for joystick %d: %s\n", |
| 1090 |
num, dinput_err_str(hr)); |
| 1091 |
goto Error; |
| 1092 |
} |
| 1093 |
|
| 1094 |
if (hr == DI_POLLEDDEVICE) { |
| 1095 |
/* This joystick device must be polled -- replace the Event with |
| 1096 |
* a Waitable Timer object. |
| 1097 |
* |
| 1098 |
* Theoretically all polled devices could share a single |
| 1099 |
* waitable timer object. But, really, how many such devices |
| 1100 |
* are there going to be on a system? |
| 1101 |
*/ |
| 1102 |
|
| 1103 |
CloseHandle(joy->waker_event); |
| 1104 |
|
| 1105 |
joy->waker_event = CreateWaitableTimer(NULL, false, NULL); |
| 1106 |
if (joy->waker_event == NULL) { |
| 1107 |
ALLEGRO_ERROR("CreateWaitableTimer failed for polled device.\n"); |
| 1108 |
goto Error; |
| 1109 |
} |
| 1110 |
|
| 1111 |
{ |
| 1112 |
LARGE_INTEGER due_time = { 0 }; |
| 1113 |
due_time.QuadPart = -1; /* Start ASAP... */ |
| 1114 |
SetWaitableTimer(joy->waker_event, |
| 1115 |
&due_time, 15, /* ... then periodic, every 15ms*/ |
| 1116 |
NULL, NULL, false); |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
ALLEGRO_INFO("Joystick %d initialized, GUID: %s\n", |
| 1121 |
num, joydx_guid_string(joy)); |
| 1122 |
|
| 1123 |
config_needs_merging = true; |
| 1124 |
|
| 1125 |
return DIENUM_CONTINUE; |
| 1126 |
|
| 1127 |
Error: |
| 1128 |
|
| 1129 |
if (dinput_device) |
| 1130 |
IDirectInputDevice8_Release(dinput_device); |
| 1131 |
|
| 1132 |
if (joy) { |
| 1133 |
joy->device = NULL; |
| 1134 |
joydx_inactivate_joy(joy); |
| 1135 |
} |
| 1136 |
|
| 1137 |
return DIENUM_CONTINUE; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
static void joydx_inactivate_joy(ALLEGRO_JOYSTICK_DIRECTX *joy) |
| 1142 |
{ |
| 1143 |
if (joy->config_state == STATE_UNUSED) |
| 1144 |
return; |
| 1145 |
joy->config_state = STATE_UNUSED; |
| 1146 |
joy->marked = false; |
| 1147 |
|
| 1148 |
if (joy->device) { |
| 1149 |
IDirectInputDevice8_SetEventNotification(joy->device, NULL); |
| 1150 |
IDirectInputDevice8_Release(joy->device); |
| 1151 |
joy->device = NULL; |
| 1152 |
} |
| 1153 |
|
| 1154 |
memset(&joy->guid, 0, sizeof(GUID)); |
| 1155 |
|
| 1156 |
if (joy->waker_event) { |
| 1157 |
CloseHandle(joy->waker_event); |
| 1158 |
joy->waker_event = NULL; |
| 1159 |
} |
| 1160 |
|
| 1161 |
_al_destroy_joystick_info(&joy->parent.info); |
| 1162 |
memset(&joy->parent.info, 0, sizeof(joy->parent.info)); |
| 1163 |
/* XXX the joystick name really belongs in joy->parent.info too */ |
| 1164 |
joy->name[0] = '\0'; |
| 1165 |
memset(&joy->joystate, 0, sizeof(joy->joystate)); |
| 1166 |
/* Don't forget to wipe the guids as well! */ |
| 1167 |
memset(&joy->guid, 0, sizeof(joy->guid)); |
| 1168 |
memset(&joy->product_guid, 0, sizeof(joy->product_guid)); |
| 1169 |
|
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
static void joydx_generate_configure_event(void) |
| 1174 |
{ |
| 1175 |
ALLEGRO_EVENT event; |
| 1176 |
event.joystick.type = ALLEGRO_EVENT_JOYSTICK_CONFIGURATION; |
| 1177 |
event.joystick.timestamp = al_get_time(); |
| 1178 |
|
| 1179 |
_al_generate_joystick_event(&event); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
static bool joydx_scan(bool configure) |
| 1184 |
{ |
| 1185 |
HRESULT hr; |
| 1186 |
unsigned i; |
| 1187 |
|
| 1188 |
/* Clear mark bits. */ |
| 1189 |
for (i = 0; i < MAX_JOYSTICKS; i++) |
| 1190 |
joydx_joystick[i].marked = false; |
| 1191 |
|
| 1192 |
/* enumerate the joysticks attached to the system */ |
| 1193 |
hr = IDirectInput8_EnumDevices(joystick_dinput, DI8DEVCLASS_GAMECTRL, |
| 1194 |
joystick_enum_callback, NULL, DIEDFL_ATTACHEDONLY); |
| 1195 |
if (FAILED(hr)) { |
| 1196 |
/* XXX will this ruin everything? */ |
| 1197 |
IDirectInput8_Release(joystick_dinput); |
| 1198 |
joystick_dinput = NULL; |
| 1199 |
return false; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* Schedule unmarked structures to be inactivated. */ |
| 1203 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1204 |
ALLEGRO_JOYSTICK_DIRECTX *joy = &joydx_joystick[i]; |
| 1205 |
|
| 1206 |
if (joy->config_state == STATE_ALIVE && !joy->marked) { |
| 1207 |
ALLEGRO_DEBUG("Joystick %s to be inactivated\n", joydx_guid_string(joy)); |
| 1208 |
joy->config_state = STATE_DYING; |
| 1209 |
config_needs_merging = true; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (config_needs_merging && configure) |
| 1214 |
joydx_generate_configure_event(); |
| 1215 |
|
| 1216 |
return config_needs_merging; |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
static void joydx_merge(void) |
| 1221 |
{ |
| 1222 |
unsigned i; |
| 1223 |
HRESULT hr; |
| 1224 |
|
| 1225 |
config_needs_merging = false; |
| 1226 |
joydx_num_joysticks = 0; |
| 1227 |
|
| 1228 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1229 |
ALLEGRO_JOYSTICK_DIRECTX *joy = &joydx_joystick[i]; |
| 1230 |
|
| 1231 |
switch (joy->config_state) { |
| 1232 |
case STATE_UNUSED: |
| 1233 |
break; |
| 1234 |
|
| 1235 |
case STATE_BORN: |
| 1236 |
hr = IDirectInputDevice8_Acquire(joy->device); |
| 1237 |
if (FAILED(hr)) { |
| 1238 |
ALLEGRO_ERROR("acquire joystick %d failed: %s\n", |
| 1239 |
i, dinput_err_str(hr)); |
| 1240 |
} |
| 1241 |
joy->config_state = STATE_ALIVE; |
| 1242 |
/* fall through */ |
| 1243 |
case STATE_ALIVE: |
| 1244 |
JOYSTICK_WAKER(joydx_num_joysticks) = joy->waker_event; |
| 1245 |
joydx_num_joysticks++; |
| 1246 |
break; |
| 1247 |
|
| 1248 |
case STATE_DYING: |
| 1249 |
joydx_inactivate_joy(joy); |
| 1250 |
break; |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
ALLEGRO_INFO("Merged, num joysticks=%d\n", joydx_num_joysticks); |
| 1255 |
|
| 1256 |
joystick_dinput_acquire(); |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
/* joydx_init_joystick: [primary thread] |
| 1261 |
* |
| 1262 |
* Initialises the DirectInput joystick devices. |
| 1263 |
* |
| 1264 |
* To avoid enumerating the the joysticks over and over, this does |
| 1265 |
* the enumeration once and does almost all the setting up required |
| 1266 |
* of the devices. joydx_get_joystick() is left with very little work |
| 1267 |
* to do. |
| 1268 |
*/ |
| 1269 |
static bool joydx_init_joystick(void) |
| 1270 |
{ |
| 1271 |
HRESULT hr; |
| 1272 |
ALLEGRO_SYSTEM *system; |
| 1273 |
size_t i; |
| 1274 |
|
| 1275 |
MAKE_UNION(&joystick_dinput, LPDIRECTINPUT *); |
| 1276 |
|
| 1277 |
ALLEGRO_ASSERT(!joystick_dinput); |
| 1278 |
ALLEGRO_ASSERT(!joydx_num_joysticks); |
| 1279 |
ALLEGRO_ASSERT(!joydx_thread); |
| 1280 |
ALLEGRO_ASSERT(!STOP_EVENT); |
| 1281 |
|
| 1282 |
/* load DirectInput module */ |
| 1283 |
_al_dinput_module = _al_win_safe_load_library(_al_dinput_module_name); |
| 1284 |
if (_al_dinput_module == NULL) { |
| 1285 |
ALLEGRO_ERROR("Failed to open '%s' library\n", _al_dinput_module_name); |
| 1286 |
joystick_dinput = NULL; |
| 1287 |
return false; |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* import DirectInput create proc */ |
| 1291 |
_al_dinput_create = (DIRECTINPUT8CREATEPROC)GetProcAddress(_al_dinput_module, "DirectInput8Create"); |
| 1292 |
if (_al_dinput_create == NULL) { |
| 1293 |
ALLEGRO_ERROR("DirectInput8Create not in %s\n", _al_dinput_module_name); |
| 1294 |
FreeLibrary(_al_dinput_module); |
| 1295 |
joystick_dinput = NULL; |
| 1296 |
return false; |
| 1297 |
} |
| 1298 |
|
| 1299 |
/* get the DirectInput interface */ |
| 1300 |
hr = _al_dinput_create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, __al_IID_IDirectInput8, u.v, NULL); |
| 1301 |
if (FAILED(hr)) { |
| 1302 |
ALLEGRO_ERROR("Failed to create DirectInput interface\n"); |
| 1303 |
FreeLibrary(_al_dinput_module); |
| 1304 |
joystick_dinput = NULL; |
| 1305 |
return false; |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* initialise the lock for the background thread */ |
| 1309 |
InitializeCriticalSection(&joydx_thread_cs); |
| 1310 |
|
| 1311 |
// This initializes present joystick state |
| 1312 |
joydx_scan(false); |
| 1313 |
|
| 1314 |
// This copies present state to user state |
| 1315 |
joydx_merge(); |
| 1316 |
|
| 1317 |
/* create the dedicated thread stopping event */ |
| 1318 |
STOP_EVENT = CreateEvent(NULL, false, false, NULL); |
| 1319 |
|
| 1320 |
/* If one of our windows is the foreground window make it grab the input. */ |
| 1321 |
system = al_get_system_driver(); |
| 1322 |
for (i = 0; i < _al_vector_size(&system->displays); i++) { |
| 1323 |
ALLEGRO_DISPLAY_WIN **pwin_disp = (ALLEGRO_DISPLAY_WIN **)_al_vector_ref(&system->displays, i); |
| 1324 |
ALLEGRO_DISPLAY_WIN *win_disp = *pwin_disp; |
| 1325 |
if (win_disp->window == GetForegroundWindow()) { |
| 1326 |
_al_win_wnd_call_proc(win_disp->window, |
| 1327 |
_al_win_joystick_dinput_grab, win_disp); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
/* start the background thread */ |
| 1332 |
joydx_thread = (HANDLE) _beginthreadex(NULL, 0, joydx_thread_proc, NULL, 0, NULL); |
| 1333 |
|
| 1334 |
return true; |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
/* joydx_exit_joystick: [primary thread] |
| 1340 |
* Shuts down the DirectInput joystick devices. |
| 1341 |
*/ |
| 1342 |
static void joydx_exit_joystick(void) |
| 1343 |
{ |
| 1344 |
int i; |
| 1345 |
ALLEGRO_SYSTEM *system; |
| 1346 |
size_t j; |
| 1347 |
|
| 1348 |
ALLEGRO_DEBUG("Entering joydx_exit_joystick\n"); |
| 1349 |
|
| 1350 |
ALLEGRO_ASSERT(joydx_thread); |
| 1351 |
|
| 1352 |
/* stop the thread */ |
| 1353 |
SetEvent(STOP_EVENT); |
| 1354 |
WaitForSingleObject(joydx_thread, INFINITE); |
| 1355 |
CloseHandle(joydx_thread); |
| 1356 |
joydx_thread = NULL; |
| 1357 |
|
| 1358 |
/* free thread resources */ |
| 1359 |
CloseHandle(STOP_EVENT); |
| 1360 |
STOP_EVENT = NULL; |
| 1361 |
DeleteCriticalSection(&joydx_thread_cs); |
| 1362 |
|
| 1363 |
/* The toplevel display is assumed to have the input acquired. Release it. */ |
| 1364 |
system = al_get_system_driver(); |
| 1365 |
for (j = 0; j < _al_vector_size(&system->displays); j++) { |
| 1366 |
ALLEGRO_DISPLAY_WIN **pwin_disp = (ALLEGRO_DISPLAY_WIN **)_al_vector_ref(&system->displays, j); |
| 1367 |
ALLEGRO_DISPLAY_WIN *win_disp = *pwin_disp; |
| 1368 |
if (win_disp->window == GetForegroundWindow()) { |
| 1369 |
ALLEGRO_DEBUG("Requesting window unacquire joystick devices\n"); |
| 1370 |
_al_win_wnd_call_proc(win_disp->window, |
| 1371 |
_al_win_joystick_dinput_unacquire, |
| 1372 |
win_disp); |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
/* destroy the devices */ |
| 1377 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1378 |
joydx_inactivate_joy(&joydx_joystick[i]); |
| 1379 |
} |
| 1380 |
joydx_num_joysticks = 0; |
| 1381 |
|
| 1382 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1383 |
JOYSTICK_WAKER(i) = NULL; |
| 1384 |
} |
| 1385 |
|
| 1386 |
/* destroy the DirectInput interface */ |
| 1387 |
IDirectInput8_Release(joystick_dinput); |
| 1388 |
joystick_dinput = NULL; |
| 1389 |
|
| 1390 |
/* release module handle */ |
| 1391 |
FreeLibrary(_al_dinput_module); |
| 1392 |
_al_dinput_module = NULL; |
| 1393 |
|
| 1394 |
ALLEGRO_DEBUG("Leaving joydx_exit_joystick\n"); |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
/* joydx_reconfigure_joysticks: [primary thread] |
| 1400 |
*/ |
| 1401 |
static bool joydx_reconfigure_joysticks(void) |
| 1402 |
{ |
| 1403 |
bool ret = false; |
| 1404 |
|
| 1405 |
EnterCriticalSection(&joydx_thread_cs); |
| 1406 |
|
| 1407 |
if (config_needs_merging) { |
| 1408 |
joydx_merge(); |
| 1409 |
ret = true; |
| 1410 |
} |
| 1411 |
|
| 1412 |
LeaveCriticalSection(&joydx_thread_cs); |
| 1413 |
|
| 1414 |
return ret; |
| 1415 |
} |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
/* joydx_get_num_joysticks: [primary thread] |
| 1420 |
* Return the number of joysticks available on the system. |
| 1421 |
*/ |
| 1422 |
static int joydx_get_num_joysticks(void) |
| 1423 |
{ |
| 1424 |
return joydx_num_joysticks; |
| 1425 |
} |
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
/* joydx_get_joystick: [primary thread] |
| 1430 |
* |
| 1431 |
* Returns the address of a ALLEGRO_JOYSTICK structure for the device |
| 1432 |
* number NUM. |
| 1433 |
*/ |
| 1434 |
static ALLEGRO_JOYSTICK *joydx_get_joystick(int num) |
| 1435 |
{ |
| 1436 |
ALLEGRO_JOYSTICK *ret = NULL; |
| 1437 |
unsigned i; |
| 1438 |
ALLEGRO_ASSERT(num >= 0); |
| 1439 |
|
| 1440 |
EnterCriticalSection(&joydx_thread_cs); |
| 1441 |
|
| 1442 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1443 |
ALLEGRO_JOYSTICK_DIRECTX *joy = &joydx_joystick[i]; |
| 1444 |
|
| 1445 |
if (ACTIVE_STATE(joy->config_state)) { |
| 1446 |
if (num == 0) { |
| 1447 |
ret = (ALLEGRO_JOYSTICK *)joy; |
| 1448 |
break; |
| 1449 |
} |
| 1450 |
num--; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
/* Must set the driver of the joystick for the wrapper driver */ |
| 1454 |
ret->driver = &_al_joydrv_directx; |
| 1455 |
|
| 1456 |
LeaveCriticalSection(&joydx_thread_cs); |
| 1457 |
|
| 1458 |
#if 0 |
| 1459 |
/* is this needed? */ |
| 1460 |
if (ret) { |
| 1461 |
ALLEGRO_DISPLAY *display = al_get_current_display(); |
| 1462 |
if (display) |
| 1463 |
_al_win_joystick_dinput_grab(display); |
| 1464 |
} |
| 1465 |
#endif |
| 1466 |
|
| 1467 |
return ret; |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
/* joydx_release_joystick: [primary thread] |
| 1473 |
* Releases a previously gotten joystick. |
| 1474 |
*/ |
| 1475 |
static void joydx_release_joystick(ALLEGRO_JOYSTICK *joy) |
| 1476 |
{ |
| 1477 |
(void)joy; |
| 1478 |
} |
| 1479 |
|
| 1480 |
|
| 1481 |
/* joydx_get_joystick_state: [primary thread] |
| 1482 |
* Copy the internal joystick state to a user-provided structure. |
| 1483 |
*/ |
| 1484 |
static void joydx_get_joystick_state(ALLEGRO_JOYSTICK *joy_, ALLEGRO_JOYSTICK_STATE *ret_state) |
| 1485 |
{ |
| 1486 |
ALLEGRO_JOYSTICK_DIRECTX *joy = (ALLEGRO_JOYSTICK_DIRECTX *)joy_; |
| 1487 |
ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); |
| 1488 |
|
| 1489 |
_al_event_source_lock(es); |
| 1490 |
{ |
| 1491 |
*ret_state = joy->joystate; |
| 1492 |
} |
| 1493 |
_al_event_source_unlock(es); |
| 1494 |
} |
| 1495 |
|
| 1496 |
|
| 1497 |
static const char *joydx_get_name(ALLEGRO_JOYSTICK *joy_) |
| 1498 |
{ |
| 1499 |
ALLEGRO_JOYSTICK_DIRECTX *joy = (ALLEGRO_JOYSTICK_DIRECTX *)joy_; |
| 1500 |
return joy->name; |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
static bool joydx_get_active(ALLEGRO_JOYSTICK *joy) |
| 1505 |
{ |
| 1506 |
ALLEGRO_JOYSTICK_DIRECTX *joydx = (ALLEGRO_JOYSTICK_DIRECTX *)joy; |
| 1507 |
|
| 1508 |
return ACTIVE_STATE(joydx->config_state); |
| 1509 |
} |
| 1510 |
|
| 1511 |
|
| 1512 |
/* joydx_thread_proc: [joystick thread] |
| 1513 |
* Thread loop function for the joystick thread. |
| 1514 |
*/ |
| 1515 |
static unsigned __stdcall joydx_thread_proc(LPVOID unused) |
| 1516 |
{ |
| 1517 |
double last_update = al_get_time(); |
| 1518 |
|
| 1519 |
/* XXX is this needed? */ |
| 1520 |
_al_win_thread_init(); |
| 1521 |
|
| 1522 |
while (true) { |
| 1523 |
DWORD result; |
| 1524 |
result = WaitForMultipleObjects(joydx_num_joysticks + 1, /* +1 for STOP_EVENT */ |
| 1525 |
joydx_thread_wakers, |
| 1526 |
false, /* wait for any */ |
| 1527 |
1000); /* 1 second wait */ |
| 1528 |
|
| 1529 |
if (result == WAIT_OBJECT_0) |
| 1530 |
break; /* STOP_EVENT */ |
| 1531 |
|
| 1532 |
EnterCriticalSection(&joydx_thread_cs); |
| 1533 |
{ |
| 1534 |
/* handle hot plugging */ |
| 1535 |
if (al_get_time() > last_update+1 || result == WAIT_TIMEOUT) { |
| 1536 |
if (need_device_enumeration) { |
| 1537 |
joydx_scan(true); |
| 1538 |
need_device_enumeration = false; |
| 1539 |
} |
| 1540 |
last_update = al_get_time(); |
| 1541 |
} |
| 1542 |
|
| 1543 |
if (result != WAIT_TIMEOUT) { |
| 1544 |
int waker_num = result - WAIT_OBJECT_0 - 1; /* -1 for STOP_EVENT */ |
| 1545 |
HANDLE waker = JOYSTICK_WAKER(waker_num); |
| 1546 |
unsigned i; |
| 1547 |
|
| 1548 |
for (i = 0; i < MAX_JOYSTICKS; i++) { |
| 1549 |
if (waker == joydx_joystick[i].waker_event) { |
| 1550 |
update_joystick(&joydx_joystick[i]); |
| 1551 |
break; |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
if (i == MAX_JOYSTICKS) { |
| 1556 |
ALLEGRO_WARN("unable to match event to joystick\n"); |
| 1557 |
} |
| 1558 |
} |
| 1559 |
} |
| 1560 |
LeaveCriticalSection(&joydx_thread_cs); |
| 1561 |
} |
| 1562 |
|
| 1563 |
_al_win_thread_exit(); |
| 1564 |
|
| 1565 |
(void)unused; |
| 1566 |
return 0; |
| 1567 |
} |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
/* update_joystick: [joystick thread] |
| 1572 |
* Reads in data for a single DirectInput joystick device, updates |
| 1573 |
* the internal ALLEGRO_JOYSTICK_STATE structure, and generates any Allegro |
| 1574 |
* events required. |
| 1575 |
*/ |
| 1576 |
static void update_joystick(ALLEGRO_JOYSTICK_DIRECTX *joy) |
| 1577 |
{ |
| 1578 |
DIDEVICEOBJECTDATA buffer[DEVICE_BUFFER_SIZE]; |
| 1579 |
DWORD num_items = DEVICE_BUFFER_SIZE; |
| 1580 |
HRESULT hr; |
| 1581 |
ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source(); |
| 1582 |
|
| 1583 |
/* some devices require polling */ |
| 1584 |
IDirectInputDevice8_Poll(joy->device); |
| 1585 |
|
| 1586 |
/* get device data into buffer */ |
| 1587 |
hr = IDirectInputDevice8_GetDeviceData(joy->device, sizeof(DIDEVICEOBJECTDATA), buffer, &num_items, 0); |
| 1588 |
|
| 1589 |
if (hr != DI_OK && hr != DI_BUFFEROVERFLOW) { |
| 1590 |
if ((hr == DIERR_NOTACQUIRED) || (hr == DIERR_INPUTLOST)) { |
| 1591 |
ALLEGRO_WARN("joystick device not acquired or lost\n"); |
| 1592 |
} |
| 1593 |
else { |
| 1594 |
ALLEGRO_ERROR("unexpected error while polling the joystick\n"); |
| 1595 |
} |
| 1596 |
return; |
| 1597 |
} |
| 1598 |
|
| 1599 |
/* don't bother locking the event source if there's no work to do */ |
| 1600 |
/* this happens a lot for polled devices */ |
| 1601 |
if (num_items == 0) |
| 1602 |
return; |
| 1603 |
|
| 1604 |
_al_event_source_lock(es); |
| 1605 |
{ |
| 1606 |
unsigned int i; |
| 1607 |
|
| 1608 |
for (i = 0; i < num_items; i++) { |
| 1609 |
const DIDEVICEOBJECTDATA *item = &buffer[i]; |
| 1610 |
const int dwOfs = item->dwOfs; |
| 1611 |
const DWORD dwData = item->dwData; |
| 1612 |
|
| 1613 |
if (dwOfs == DIJOFS_X) |
| 1614 |
handle_axis_event(joy, joy->x_mapping, dwData); |
| 1615 |
else if (dwOfs == DIJOFS_Y) |
| 1616 |
handle_axis_event(joy, joy->y_mapping, dwData); |
| 1617 |
else if (dwOfs == DIJOFS_Z) |
| 1618 |
handle_axis_event(joy, joy->z_mapping, dwData); |
| 1619 |
else if (dwOfs == DIJOFS_RX) |
| 1620 |
handle_axis_event(joy, joy->rx_mapping, dwData); |
| 1621 |
else if (dwOfs == DIJOFS_RY) |
| 1622 |
handle_axis_event(joy, joy->ry_mapping, dwData); |
| 1623 |
else if (dwOfs == DIJOFS_RZ) |
| 1624 |
handle_axis_event(joy, joy->rz_mapping, dwData); |
| 1625 |
else if ((unsigned int)dwOfs == DIJOFS_SLIDER(0)) |
| 1626 |
handle_axis_event(joy, joy->slider_mapping[0], dwData); |
| 1627 |
else if ((unsigned int)dwOfs == DIJOFS_SLIDER(1)) |
| 1628 |
handle_axis_event(joy, joy->slider_mapping[1], dwData); |
| 1629 |
else if ((unsigned int)dwOfs == DIJOFS_POV(0)) |
| 1630 |
handle_pov_event(joy, &joy->pov_mapping[0], dwData); |
| 1631 |
else if ((unsigned int)dwOfs == DIJOFS_POV(1)) |
| 1632 |
handle_pov_event(joy, &joy->pov_mapping[2], dwData); |
| 1633 |
else if ((unsigned int)dwOfs == DIJOFS_POV(2)) |
| 1634 |
handle_pov_event(joy, &joy->pov_mapping[4], dwData); |
| 1635 |
else if ((unsigned int)dwOfs == DIJOFS_POV(3)) |
| 1636 |
handle_pov_event(joy, &joy->pov_mapping[6], dwData); |
| 1637 |
else { |
| 1638 |
/* buttons */ |
| 1639 |
int num = (dwOfs - DIJOFS_BUTTON0) / (DIJOFS_BUTTON1 - DIJOFS_BUTTON0); |
| 1640 |
if (num < MAX_BUTTONS) |
| 1641 |
handle_button_event(joy, joy->button_mapping[num], (dwData & 0x80)); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
} |
| 1645 |
_al_event_source_unlock(es); |
| 1646 |
} |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
/* handle_axis_event: [joystick thread] |
| 1651 |
* Helper function to handle a state change in a non-POV axis. |
| 1652 |
* The joystick must be locked BEFORE entering this function. |
| 1653 |
*/ |
| 1654 |
static void handle_axis_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT output, DWORD value) |
| 1655 |
{ |
| 1656 |
float pos = (int)value / 32767.0; |
| 1657 |
if (output.button_enabled || output.pos_enabled || output.neg_enabled) |
| 1658 |
_al_joystick_generate_axis_event((ALLEGRO_JOYSTICK*)joy, &joy->joystate, output, pos); |
| 1659 |
} |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
/* handle_pov_event: [joystick thread] |
| 1664 |
* Helper function to handle a state change in a POV device. |
| 1665 |
* The joystick must be locked BEFORE entering this function. |
| 1666 |
*/ |
| 1667 |
static void handle_pov_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT *outputs, DWORD _value) |
| 1668 |
{ |
| 1669 |
int value = _value; |
| 1670 |
float px, py; |
| 1671 |
/* left */ |
| 1672 |
if ((value > JOY_POVBACKWARD) && (value < JOY_POVFORWARD_WRAP)) |
| 1673 |
px = -1.0; |
| 1674 |
/* right */ |
| 1675 |
else if ((value > JOY_POVFORWARD) && (value < JOY_POVBACKWARD)) |
| 1676 |
px = +1.0; |
| 1677 |
else |
| 1678 |
px = 0.0; |
| 1679 |
|
| 1680 |
/* forward */ |
| 1681 |
if (((value > JOY_POVLEFT) && (value <= JOY_POVFORWARD_WRAP)) || |
| 1682 |
((value >= JOY_POVFORWARD) && (value < JOY_POVRIGHT))) |
| 1683 |
py = -1.0; |
| 1684 |
/* backward */ |
| 1685 |
else if ((value > JOY_POVRIGHT) && (value < JOY_POVLEFT)) |
| 1686 |
py = +1.0; |
| 1687 |
else |
| 1688 |
py = 0.0; |
| 1689 |
|
| 1690 |
_AL_JOYSTICK_OUTPUT output; |
| 1691 |
output = outputs[0]; |
| 1692 |
if (output.button_enabled || output.pos_enabled || output.neg_enabled) |
| 1693 |
_al_joystick_generate_axis_event((ALLEGRO_JOYSTICK*)joy, &joy->joystate, output, px); |
| 1694 |
output = outputs[1]; |
| 1695 |
if (output.button_enabled || output.pos_enabled || output.neg_enabled) |
| 1696 |
_al_joystick_generate_axis_event((ALLEGRO_JOYSTICK*)joy, &joy->joystate, output, py); |
| 1697 |
} |
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
/* handle_button_event: [joystick thread] |
| 1702 |
* Helper function to handle a state change in a button. |
| 1703 |
* The joystick must be locked BEFORE entering this function. |
| 1704 |
*/ |
| 1705 |
static void handle_button_event(ALLEGRO_JOYSTICK_DIRECTX *joy, _AL_JOYSTICK_OUTPUT output, DWORD value) |
| 1706 |
{ |
| 1707 |
if (output.button_enabled || output.pos_enabled || output.neg_enabled) |
| 1708 |
_al_joystick_generate_button_event((ALLEGRO_JOYSTICK *)joy, &joy->joystate, output, value); |
| 1709 |
} |
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
/* |
| 1714 |
* Local Variables: |
| 1715 |
* c-basic-offset: 3 |
| 1716 |
* indent-tabs-mode: nil |
| 1717 |
* End: |
| 1718 |
*/ |
| 1719 |
/* vim: set sts=3 sw=3 et: */ |