| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* Windows wrapper haptic (force-feedback) device driver. |
| 12 |
* This wraps around the XInput and DirectInput drivers to support both. |
| 13 |
* By Beoran. |
| 14 |
* |
| 15 |
* See LICENSE.txt for copyright information. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#define ALLEGRO_NO_COMPATIBILITY |
| 20 |
|
| 21 |
#define DIRECTINPUT_VERSION 0x0800 |
| 22 |
|
| 23 |
/* For waitable timers */ |
| 24 |
#define _WIN32_WINNT 0x0501 |
| 25 |
|
| 26 |
#include "allegro5/allegro.h" |
| 27 |
#include "allegro5/haptic.h" |
| 28 |
#include "allegro5/internal/aintern.h" |
| 29 |
#include "allegro5/platform/aintwin.h" |
| 30 |
#include "allegro5/internal/aintern_haptic.h" |
| 31 |
#include "allegro5/internal/aintern_events.h" |
| 32 |
#include "allegro5/internal/aintern_joystick.h" |
| 33 |
#include "allegro5/internal/aintern_bitmap.h" |
| 34 |
|
| 35 |
#ifdef ALLEGRO_CFG_XINPUT |
| 36 |
/* Don't compile this lot if xinput and directinput isn't supported. */ |
| 37 |
|
| 38 |
#include "allegro5/internal/aintern_wjoyall.h" |
| 39 |
|
| 40 |
ALLEGRO_DEBUG_CHANNEL("haptic") |
| 41 |
|
| 42 |
/* forward declarations */ |
| 43 |
static bool hapall_init_haptic(void); |
| 44 |
static void hapall_exit_haptic(void); |
| 45 |
|
| 46 |
static bool hapall_is_mouse_haptic(ALLEGRO_MOUSE *dev); |
| 47 |
static bool hapall_is_joystick_haptic(ALLEGRO_JOYSTICK *); |
| 48 |
static bool hapall_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev); |
| 49 |
static bool hapall_is_display_haptic(ALLEGRO_DISPLAY *dev); |
| 50 |
static bool hapall_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev); |
| 51 |
|
| 52 |
static ALLEGRO_HAPTIC *hapall_get_from_mouse(ALLEGRO_MOUSE *dev); |
| 53 |
static ALLEGRO_HAPTIC *hapall_get_from_joystick(ALLEGRO_JOYSTICK *dev); |
| 54 |
static ALLEGRO_HAPTIC *hapall_get_from_keyboard(ALLEGRO_KEYBOARD *dev); |
| 55 |
static ALLEGRO_HAPTIC *hapall_get_from_display(ALLEGRO_DISPLAY *dev); |
| 56 |
static ALLEGRO_HAPTIC *hapall_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev); |
| 57 |
|
| 58 |
static bool hapall_release(ALLEGRO_HAPTIC *haptic); |
| 59 |
|
| 60 |
static bool hapall_get_active(ALLEGRO_HAPTIC *hap); |
| 61 |
static int hapall_get_capabilities(ALLEGRO_HAPTIC *dev); |
| 62 |
static double hapall_get_gain(ALLEGRO_HAPTIC *dev); |
| 63 |
static bool hapall_set_gain(ALLEGRO_HAPTIC *dev, double); |
| 64 |
static int hapall_get_max_effects(ALLEGRO_HAPTIC *dev); |
| 65 |
|
| 66 |
static bool hapall_is_effect_ok(ALLEGRO_HAPTIC *dev, ALLEGRO_HAPTIC_EFFECT *eff); |
| 67 |
static bool hapall_upload_effect(ALLEGRO_HAPTIC *dev, |
| 68 |
ALLEGRO_HAPTIC_EFFECT *eff, |
| 69 |
ALLEGRO_HAPTIC_EFFECT_ID *id); |
| 70 |
static bool hapall_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loop); |
| 71 |
static bool hapall_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id); |
| 72 |
static bool hapall_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id); |
| 73 |
static bool hapall_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id); |
| 74 |
|
| 75 |
static double hapall_get_autocenter(ALLEGRO_HAPTIC *dev); |
| 76 |
static bool hapall_set_autocenter(ALLEGRO_HAPTIC *dev, double); |
| 77 |
|
| 78 |
|
| 79 |
ALLEGRO_HAPTIC_DRIVER _al_hapdrv_windows_all = |
| 80 |
{ |
| 81 |
AL_HAPTIC_TYPE_WINDOWS_ALL, |
| 82 |
"", |
| 83 |
"", |
| 84 |
"Windows haptic(s)", |
| 85 |
hapall_init_haptic, |
| 86 |
hapall_exit_haptic, |
| 87 |
|
| 88 |
hapall_is_mouse_haptic, |
| 89 |
hapall_is_joystick_haptic, |
| 90 |
hapall_is_keyboard_haptic, |
| 91 |
hapall_is_display_haptic, |
| 92 |
hapall_is_touch_input_haptic, |
| 93 |
|
| 94 |
hapall_get_from_mouse, |
| 95 |
hapall_get_from_joystick, |
| 96 |
hapall_get_from_keyboard, |
| 97 |
hapall_get_from_display, |
| 98 |
hapall_get_from_touch_input, |
| 99 |
|
| 100 |
hapall_get_active, |
| 101 |
hapall_get_capabilities, |
| 102 |
hapall_get_gain, |
| 103 |
hapall_set_gain, |
| 104 |
hapall_get_max_effects, |
| 105 |
|
| 106 |
hapall_is_effect_ok, |
| 107 |
hapall_upload_effect, |
| 108 |
hapall_play_effect, |
| 109 |
hapall_stop_effect, |
| 110 |
hapall_is_effect_playing, |
| 111 |
hapall_release_effect, |
| 112 |
|
| 113 |
hapall_release, |
| 114 |
|
| 115 |
hapall_get_autocenter, |
| 116 |
hapall_set_autocenter |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
/* Mutex for thread protection. */ |
| 121 |
static ALLEGRO_MUTEX *hapall_mutex = NULL; |
| 122 |
|
| 123 |
|
| 124 |
/* Initializes the combined haptic system. */ |
| 125 |
static bool hapall_init_haptic(void) |
| 126 |
{ |
| 127 |
bool xi_ok, di_ok; |
| 128 |
ASSERT(hapall_mutex == NULL); |
| 129 |
|
| 130 |
/* Create the mutex. */ |
| 131 |
hapall_mutex = al_create_mutex_recursive(); |
| 132 |
if (!hapall_mutex) |
| 133 |
return false; |
| 134 |
|
| 135 |
al_lock_mutex(hapall_mutex); |
| 136 |
|
| 137 |
xi_ok = _al_hapdrv_xinput.init_haptic(); |
| 138 |
di_ok = _al_hapdrv_directx.init_haptic(); |
| 139 |
al_unlock_mutex(hapall_mutex); |
| 140 |
return xi_ok || di_ok; |
| 141 |
} |
| 142 |
|
| 143 |
static void hapall_exit_haptic(void) |
| 144 |
{ |
| 145 |
ASSERT(hapall_mutex); |
| 146 |
al_destroy_mutex(hapall_mutex); |
| 147 |
hapall_mutex = NULL; |
| 148 |
} |
| 149 |
|
| 150 |
static bool hapall_get_active(ALLEGRO_HAPTIC *haptic) |
| 151 |
{ |
| 152 |
return haptic->driver->get_active(haptic); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static bool hapall_is_mouse_haptic(ALLEGRO_MOUSE *mouse) |
| 157 |
{ |
| 158 |
(void)mouse; |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
static bool hapall_is_joystick_haptic(ALLEGRO_JOYSTICK *joy) |
| 164 |
{ |
| 165 |
bool dx_ok = false, xi_ok = false; |
| 166 |
ASSERT(joy->driver); |
| 167 |
|
| 168 |
if (joy->driver == &_al_joydrv_xinput) { |
| 169 |
dx_ok = _al_hapdrv_xinput.is_joystick_haptic(joy); |
| 170 |
} |
| 171 |
else if (joy->driver == &_al_joydrv_directx) { |
| 172 |
xi_ok = _al_hapdrv_directx.is_joystick_haptic(joy); |
| 173 |
} |
| 174 |
return dx_ok || xi_ok; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static bool hapall_is_display_haptic(ALLEGRO_DISPLAY *dev) |
| 179 |
{ |
| 180 |
(void)dev; |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
static bool hapall_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev) |
| 186 |
{ |
| 187 |
(void)dev; |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
static bool hapall_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev) |
| 193 |
{ |
| 194 |
(void)dev; |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
static ALLEGRO_HAPTIC *hapall_get_from_mouse(ALLEGRO_MOUSE *mouse) |
| 200 |
{ |
| 201 |
(void)mouse; |
| 202 |
return NULL; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
static ALLEGRO_HAPTIC *hapall_get_from_joystick(ALLEGRO_JOYSTICK *joy) |
| 207 |
{ |
| 208 |
ALLEGRO_HAPTIC *haptic = NULL; |
| 209 |
if (!al_is_joystick_haptic(joy)) |
| 210 |
return NULL; |
| 211 |
|
| 212 |
al_lock_mutex(hapall_mutex); |
| 213 |
|
| 214 |
if (joy->driver == &_al_joydrv_xinput) { |
| 215 |
haptic = _al_hapdrv_xinput.get_from_joystick(joy); |
| 216 |
if (haptic) { |
| 217 |
haptic->driver = &_al_hapdrv_xinput; |
| 218 |
} |
| 219 |
} |
| 220 |
else if (joy->driver == &_al_joydrv_directx) { |
| 221 |
haptic = _al_hapdrv_directx.get_from_joystick(joy); |
| 222 |
if (haptic) { |
| 223 |
haptic->driver = &_al_hapdrv_directx; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
al_unlock_mutex(hapall_mutex); |
| 228 |
return haptic; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
static ALLEGRO_HAPTIC *hapall_get_from_display(ALLEGRO_DISPLAY *dev) |
| 233 |
{ |
| 234 |
(void)dev; |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
static ALLEGRO_HAPTIC *hapall_get_from_keyboard(ALLEGRO_KEYBOARD *dev) |
| 240 |
{ |
| 241 |
(void)dev; |
| 242 |
return NULL; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
static ALLEGRO_HAPTIC *hapall_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev) |
| 247 |
{ |
| 248 |
(void)dev; |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
static int hapall_get_capabilities(ALLEGRO_HAPTIC *dev) |
| 254 |
{ |
| 255 |
return dev->driver->get_capabilities(dev); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
static double hapall_get_gain(ALLEGRO_HAPTIC *dev) |
| 260 |
{ |
| 261 |
return dev->driver->get_gain(dev); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
static bool hapall_set_gain(ALLEGRO_HAPTIC *dev, double gain) |
| 266 |
{ |
| 267 |
return dev->driver->set_gain(dev, gain); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
double hapall_get_autocenter(ALLEGRO_HAPTIC *dev) |
| 272 |
{ |
| 273 |
return dev->driver->get_autocenter(dev); |
| 274 |
} |
| 275 |
|
| 276 |
static bool hapall_set_autocenter(ALLEGRO_HAPTIC *dev, double intensity) |
| 277 |
{ |
| 278 |
return dev->driver->set_autocenter(dev, intensity); |
| 279 |
} |
| 280 |
|
| 281 |
static int hapall_get_max_effects(ALLEGRO_HAPTIC *dev) |
| 282 |
{ |
| 283 |
return dev->driver->get_max_effects(dev); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
static bool hapall_is_effect_ok(ALLEGRO_HAPTIC *dev, |
| 288 |
ALLEGRO_HAPTIC_EFFECT *effect) |
| 289 |
{ |
| 290 |
return dev->driver->is_effect_ok(dev, effect); |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
static bool hapall_upload_effect(ALLEGRO_HAPTIC *dev, |
| 295 |
ALLEGRO_HAPTIC_EFFECT *effect, ALLEGRO_HAPTIC_EFFECT_ID *id) |
| 296 |
{ |
| 297 |
/* store the driver we'll need it later. */ |
| 298 |
id->driver = dev->driver; |
| 299 |
return dev->driver->upload_effect(dev, effect, id); |
| 300 |
} |
| 301 |
|
| 302 |
static bool hapall_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loops) |
| 303 |
{ |
| 304 |
ALLEGRO_HAPTIC_DRIVER *driver = id->driver; |
| 305 |
/* Use the stored driver to perform the operation. */ |
| 306 |
return driver->play_effect(id, loops); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
static bool hapall_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id) |
| 311 |
{ |
| 312 |
ALLEGRO_HAPTIC_DRIVER *driver = id->driver; |
| 313 |
/* Use the stored driver to perform the operation. */ |
| 314 |
return driver->stop_effect(id); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
static bool hapall_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id) |
| 319 |
{ |
| 320 |
ALLEGRO_HAPTIC_DRIVER *driver = id->driver; |
| 321 |
/* Use the stored driver to perform the operation. */ |
| 322 |
return driver->is_effect_playing(id); |
| 323 |
} |
| 324 |
|
| 325 |
static bool hapall_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id) |
| 326 |
{ |
| 327 |
ALLEGRO_HAPTIC_DRIVER *driver = id->driver; |
| 328 |
/* Use the stored driver to perform the operation. */ |
| 329 |
return driver->release_effect(id); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
static bool hapall_release(ALLEGRO_HAPTIC *haptic) |
| 334 |
{ |
| 335 |
if (!haptic) |
| 336 |
return false; |
| 337 |
|
| 338 |
return haptic->driver->release(haptic); |
| 339 |
} |
| 340 |
|
| 341 |
#endif |
| 342 |
|
| 343 |
/* vim: set sts=3 sw=3 et: */ |