Debian Codegraph @

src/win/whapxi.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows haptic (force-feedback) device driver.
12 *
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 isn't supported. */
37
38 #ifndef ALLEGRO_WINDOWS
39 #error something is wrong with the makefile
40 #endif
41
42 #ifndef ALLEGRO_XINPUT_POLL_DELAY
43 #define ALLEGRO_XINPUT_POLL_DELAY 0.1
44 #endif
45
46 #ifdef ALLEGRO_MINGW32
47 #undef MAKEFOURCC
48 #endif
49
50 #include <initguid.h>
51 #include <stdio.h>
52 #include <mmsystem.h>
53 #include <process.h>
54 #include <math.h>
55
56 /* The official DirectX xinput.h uses SAL annotations.
57 * Need the sal.h header to get rid of them. On some other platforms
58 * such as MinGW on Linux this header is lacking because it is not needed there.
59 * So, simply try to include sal.h IF we have it , and if not, hope
60 * for the best.
61 * This does no harm on msys2 either, they have a sal.h header.
62 */
63 #ifdef ALLEGRO_HAVE_SAL_H
64 #include <sal.h>
65 #endif
66
67 #include <xinput.h>
68
69 #include "allegro5/internal/aintern_wjoyxi.h"
70
71
72
73 ALLEGRO_DEBUG_CHANNEL("haptic")
74
75 /* Support at most 4 haptic devices. */
76 #define HAPTICS_MAX 4
77
78 /* Support at most 1 rumble effect per device, because
79 * XInput doesn't really support uploading the effects. */
80 #define HAPTIC_EFFECTS_MAX 1
81
82 /* Enum for the state of a haptic effect. The playback is managed by a small
83 * finite state machine.
84 */
85
86 typedef enum ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE
87 {
88 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE = 0,
89 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY = 1,
90 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STARTING = 2,
91 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_PLAYING = 3,
92 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_DELAYED = 4,
93 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STOPPING = 5,
94 } ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE;
95
96 /* Allowed state transitions:
97 * from inactive to ready,
98 * from ready to starting
99 * from starting to delayed or playing
100 * from delayed to playing
101 * from playing to delayed (for loops) or stopping
102 * from stopping to ready
103 * from ready to inactive
104 */
105
106 typedef struct ALLEGRO_HAPTIC_EFFECT_XINPUT
107 {
108 ALLEGRO_HAPTIC_EFFECT effect;
109 XINPUT_VIBRATION vibration;
110 int id;
111 double start_time;
112 double loop_start;
113 double stop_time;
114 int repeats;
115 int delay_repeated;
116 int play_repeated;
117 ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE state;
118 } ALLEGRO_HAPTIC_EFFECT_XINPUT;
119
120
121 typedef struct ALLEGRO_HAPTIC_XINPUT
122 {
123 /* Important, parent must be first. */
124 ALLEGRO_HAPTIC parent;
125 ALLEGRO_JOYSTICK_XINPUT *xjoy;
126 bool active;
127 ALLEGRO_HAPTIC_EFFECT_XINPUT effect;
128 int flags;
129 /* Only one effect is supported since the XInput API allows only one pair of
130 vibration speeds to be set to the device. */
131 } ALLEGRO_HAPTIC_XINPUT;
132
133
134 /* forward declarations */
135 static bool hapxi_init_haptic(void);
136 static void hapxi_exit_haptic(void);
137
138 static bool hapxi_is_mouse_haptic(ALLEGRO_MOUSE *dev);
139 static bool hapxi_is_joystick_haptic(ALLEGRO_JOYSTICK *);
140 static bool hapxi_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev);
141 static bool hapxi_is_display_haptic(ALLEGRO_DISPLAY *dev);
142 static bool hapxi_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev);
143
144 static ALLEGRO_HAPTIC *hapxi_get_from_mouse(ALLEGRO_MOUSE *dev);
145 static ALLEGRO_HAPTIC *hapxi_get_from_joystick(ALLEGRO_JOYSTICK *dev);
146 static ALLEGRO_HAPTIC *hapxi_get_from_keyboard(ALLEGRO_KEYBOARD *dev);
147 static ALLEGRO_HAPTIC *hapxi_get_from_display(ALLEGRO_DISPLAY *dev);
148 static ALLEGRO_HAPTIC *hapxi_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev);
149
150 static bool hapxi_release(ALLEGRO_HAPTIC *haptic);
151
152 static bool hapxi_get_active(ALLEGRO_HAPTIC *hap);
153 static int hapxi_get_capabilities(ALLEGRO_HAPTIC *dev);
154 static double hapxi_get_gain(ALLEGRO_HAPTIC *dev);
155 static bool hapxi_set_gain(ALLEGRO_HAPTIC *dev, double);
156 static int hapxi_get_max_effects(ALLEGRO_HAPTIC *dev);
157
158 static bool hapxi_is_effect_ok(ALLEGRO_HAPTIC *dev, ALLEGRO_HAPTIC_EFFECT *eff);
159 static bool hapxi_upload_effect(ALLEGRO_HAPTIC *dev,
160 ALLEGRO_HAPTIC_EFFECT *eff,
161 ALLEGRO_HAPTIC_EFFECT_ID *id);
162 static bool hapxi_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loop);
163 static bool hapxi_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id);
164 static bool hapxi_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id);
165 static bool hapxi_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id);
166
167 static double hapxi_get_autocenter(ALLEGRO_HAPTIC *dev);
168 static bool hapxi_set_autocenter(ALLEGRO_HAPTIC *dev, double);
169
170 static void *hapxi_poll_thread(ALLEGRO_THREAD *thread, void *arg);
171
172 ALLEGRO_HAPTIC_DRIVER _al_hapdrv_xinput =
173 {
174 AL_HAPTIC_TYPE_XINPUT,
175 "",
176 "",
177 "Windows XInput haptic(s)",
178 hapxi_init_haptic,
179 hapxi_exit_haptic,
180
181 hapxi_is_mouse_haptic,
182 hapxi_is_joystick_haptic,
183 hapxi_is_keyboard_haptic,
184 hapxi_is_display_haptic,
185 hapxi_is_touch_input_haptic,
186
187 hapxi_get_from_mouse,
188 hapxi_get_from_joystick,
189 hapxi_get_from_keyboard,
190 hapxi_get_from_display,
191 hapxi_get_from_touch_input,
192
193 hapxi_get_active,
194 hapxi_get_capabilities,
195 hapxi_get_gain,
196 hapxi_set_gain,
197 hapxi_get_max_effects,
198
199 hapxi_is_effect_ok,
200 hapxi_upload_effect,
201 hapxi_play_effect,
202 hapxi_stop_effect,
203 hapxi_is_effect_playing,
204 hapxi_release_effect,
205
206 hapxi_release,
207
208 hapxi_get_autocenter,
209 hapxi_set_autocenter
210 };
211
212
213 static ALLEGRO_HAPTIC_XINPUT haptics[HAPTICS_MAX];
214 /* For the background thread */
215 static ALLEGRO_THREAD *hapxi_thread = NULL;
216 static ALLEGRO_MUTEX *hapxi_mutex = NULL;
217 /* Use a condition variable to put the thread to sleep and prevent too
218 frequent polling. */
219 static ALLEGRO_COND *hapxi_cond = NULL;
220
221 typedef DWORD(WINAPI *XInputSetStatePROC)(DWORD, XINPUT_VIBRATION*);
222 extern XInputSetStatePROC _al_imp_XInputSetState;
223
224 /* Forces vibration to stop immediately. */
225 static bool hapxi_force_stop(ALLEGRO_HAPTIC_XINPUT *hapxi,
226 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
227 {
228 XINPUT_VIBRATION no_vibration = { 0, 0 };
229 ALLEGRO_DEBUG("XInput haptic effect stopped.\n");
230 _al_imp_XInputSetState(hapxi->xjoy->index, &no_vibration);
231 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY;
232 return true;
233 }
234
235 /* starts vibration immediately. If successfully also sets playing */
236 static bool hapxi_force_play(ALLEGRO_HAPTIC_XINPUT *hapxi,
237 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
238 {
239 DWORD res;
240 res = _al_imp_XInputSetState(hapxi->xjoy->index, &effxi->vibration);
241 ALLEGRO_DEBUG("Starting to play back haptic effect: %d.\n", (int)(res));
242 if (res == ERROR_SUCCESS) {
243 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_PLAYING;
244 return true;
245 }
246 else {
247 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY;
248 return false;
249 }
250 }
251
252
253
254 static bool hapxi_poll_haptic_effect_ready(ALLEGRO_HAPTIC_XINPUT *hapxi,
255 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
256 {
257 (void)hapxi; (void)effxi;
258 /* when ready do nothing */
259 return true;
260 }
261
262 static bool hapxi_poll_haptic_effect_starting(ALLEGRO_HAPTIC_XINPUT *hapxi,
263 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
264 {
265 /* when starting switch to delayed mode or play mode */
266 double now = al_get_time();
267 if ((now - effxi->start_time) < effxi->effect.replay.delay) {
268 effxi->loop_start = al_get_time();
269 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_DELAYED;
270 }
271 else {
272 hapxi_force_play(hapxi, effxi);
273 }
274 ALLEGRO_DEBUG("Polling XInput haptic effect. Really Starting: %d!\n", effxi->state);
275 return true;
276 }
277
278 static bool hapxi_poll_haptic_effect_playing(ALLEGRO_HAPTIC_XINPUT *hapxi,
279 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
280 {
281 double now = al_get_time();
282 double stop = effxi->loop_start + effxi->effect.replay.delay +
283 effxi->effect.replay.length;
284 (void)hapxi;
285 if (now > stop) {
286 /* may need to repeat play because of "loop" in playback. */
287 effxi->play_repeated++;
288 if (effxi->play_repeated < effxi->repeats) {
289 /* need to play another loop. Stop playing now. */
290 hapxi_force_stop(hapxi, effxi);
291 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_DELAYED;
292 effxi->loop_start = al_get_time();
293 }
294 else {
295 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STOPPING;
296 }
297 return true;
298 }
299 return false;
300 }
301
302 static bool hapxi_poll_haptic_effect_delayed(ALLEGRO_HAPTIC_XINPUT *hapxi,
303 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
304 {
305 double now = al_get_time();
306 if (now > (effxi->loop_start + effxi->effect.replay.delay)) {
307 return hapxi_force_play(hapxi, effxi);
308 }
309 return false;
310 }
311
312 static bool hapxi_poll_haptic_effect_stopping(ALLEGRO_HAPTIC_XINPUT *hapxi,
313 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
314 {
315 /* when stopping, force stop and go to ready state (hapxi_force_stop does this)*/
316 return hapxi_force_stop(hapxi, effxi);
317 }
318
319
320 /* Polls the xinput API for a single haptic device and effect. */
321 static bool hapxi_poll_haptic_effect(ALLEGRO_HAPTIC_XINPUT *hapxi,
322 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
323 {
324 /* Check the state of the effect. */
325 switch (effxi->state) {
326 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE:
327 return false;
328 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY:
329 return hapxi_poll_haptic_effect_ready(hapxi, effxi);
330 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STARTING:
331 return hapxi_poll_haptic_effect_starting(hapxi, effxi);
332 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_PLAYING:
333 return hapxi_poll_haptic_effect_playing(hapxi, effxi);
334 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_DELAYED:
335 return hapxi_poll_haptic_effect_delayed(hapxi, effxi);
336 case ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STOPPING:
337 return hapxi_poll_haptic_effect_stopping(hapxi, effxi);
338 default:
339 ALLEGRO_DEBUG("XInput haptic effect state not valid :%d.\n", effxi->state);
340 return false;
341 }
342 }
343
344 /* Polls the xinput API for a single haptic device. */
345 static void hapxi_poll_haptic(ALLEGRO_HAPTIC_XINPUT *hapxi)
346 {
347 hapxi_poll_haptic_effect(hapxi, &hapxi->effect);
348 }
349
350 /* Polls the xinput API for hapxi effect and starts
351 * or stops playback when needed.
352 */
353 static void hapxi_poll_haptics(void)
354 {
355 int i;
356
357 for (i = 0; i < HAPTICS_MAX; i++) {
358 if (haptics[i].active) {
359 hapxi_poll_haptic(haptics + i);
360 }
361 }
362 }
363
364
365 /* Function for the haptics polling thread. */
366 static void *hapxi_poll_thread(ALLEGRO_THREAD *thread, void *arg)
367 {
368 ALLEGRO_TIMEOUT timeout;
369 al_lock_mutex(hapxi_mutex);
370 while (!al_get_thread_should_stop(thread)) {
371 /* Poll once every 10 milliseconds. XXX: Should this be configurable? */
372 al_init_timeout(&timeout, ALLEGRO_XINPUT_POLL_DELAY);
373 /* Wait for the condition to allow the
374 polling thread to be awoken when needed. */
375 al_wait_cond_until(hapxi_cond, hapxi_mutex, &timeout);
376 /* If we get here poll joystick for new input or connection
377 * and dispatch events. */
378 hapxi_poll_haptics();
379 }
380 al_unlock_mutex(hapxi_mutex);
381 return arg;
382 }
383
384
385
386 /* Initializes the XInput haptic system. */
387 static bool hapxi_init_haptic(void)
388 {
389 int i;
390
391 ASSERT(hapxi_mutex == NULL);
392 ASSERT(hapxi_thread == NULL);
393 ASSERT(hapxi_cond == NULL);
394
395
396 /* Create the mutex and a condition vaiable. */
397 hapxi_mutex = al_create_mutex_recursive();
398 if (!hapxi_mutex)
399 return false;
400 hapxi_cond = al_create_cond();
401 if (!hapxi_cond)
402 return false;
403
404 al_lock_mutex(hapxi_mutex);
405
406 for (i = 0; i < HAPTICS_MAX; i++) {
407 haptics[i].active = false;
408 }
409
410 /* Now start a polling background thread, since XInput is a polled API,
411 and also to make it possible for effects to stop running when their
412 duration has passed. */
413 hapxi_thread = al_create_thread(hapxi_poll_thread, NULL);
414 al_unlock_mutex(hapxi_mutex);
415 if (hapxi_thread) al_start_thread(hapxi_thread);
416 return(hapxi_thread != NULL);
417 }
418
419
420 /* Converts a generic haptic device to a Windows-specific one. */
421 static ALLEGRO_HAPTIC_XINPUT *hapxi_from_al(ALLEGRO_HAPTIC *hap)
422 {
423 return (ALLEGRO_HAPTIC_XINPUT *)hap;
424 }
425
426 static void hapxi_exit_haptic(void)
427 {
428 void *ret_value;
429 ASSERT(hapxi_thread);
430 ASSERT(hapxi_mutex);
431 ASSERT(hapxi_cond);
432
433 /* Request the event thread to shut down, signal the condition, then join the thread. */
434 al_set_thread_should_stop(hapxi_thread);
435 al_signal_cond(hapxi_cond);
436 al_join_thread(hapxi_thread, &ret_value);
437
438 /* clean it all up. */
439 al_destroy_thread(hapxi_thread);
440 al_destroy_cond(hapxi_cond);
441
442 al_destroy_mutex(hapxi_mutex);
443 hapxi_mutex = NULL;
444 }
445
446 /* Converts a float to a unsigned WORD range */
447 static bool hapxi_magnitude2win(WORD *word, double value)
448 {
449 if (!word) return false;
450 (*word) = (WORD)(65535 * value);
451 return true;
452 }
453
454 /* Converts Allegro haptic effect to xinput API. */
455 static bool hapxi_effect2win(
456 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi,
457 ALLEGRO_HAPTIC_EFFECT *effect,
458 ALLEGRO_HAPTIC_XINPUT *hapxi)
459 {
460 (void)hapxi;
461 /* Generic setup */
462 if (effect->type != ALLEGRO_HAPTIC_RUMBLE)
463 return false;
464
465 /* Right motor is "weaker" than left one, That is, right produces a less
466 violent vibration than left. */
467 return
468 hapxi_magnitude2win(&effxi->vibration.wRightMotorSpeed,
469 effect->data.rumble.weak_magnitude) &&
470 hapxi_magnitude2win(&effxi->vibration.wLeftMotorSpeed,
471 effect->data.rumble.strong_magnitude);
472 }
473
474 static bool hapxi_get_active(ALLEGRO_HAPTIC *haptic)
475 {
476 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_from_al(haptic);
477 return hapxi->active;
478 }
479
480
481
482 static bool hapxi_is_mouse_haptic(ALLEGRO_MOUSE *mouse)
483 {
484 (void)mouse;
485 return false;
486 }
487
488
489 static bool hapxi_is_joystick_haptic(ALLEGRO_JOYSTICK *joy)
490 {
491 ALLEGRO_JOYSTICK_XINPUT *joyxi = (ALLEGRO_JOYSTICK_XINPUT *)joy;
492 if (!al_is_joystick_installed())
493 return false;
494 if (!al_get_joystick_active(joy))
495 return false;
496
497 /* IF this flag is not supported, then it means we're compiling against
498 an older XInput library. In this case, the Flags are inoperable,
499 and force feedback must be assumed to be always available. */
500 #ifndef XINPUT_CAPS_FFB_SUPPORTED
501 (void)joyxi;
502 ALLEGRO_DEBUG("Compiled against older XInput library, assuming force feedback support is available.\n");
503 return true;
504 #else
505 ALLEGRO_DEBUG("joyxi->capabilities.Flags: %d <-> %d\n", joyxi->capabilities.Flags, XINPUT_CAPS_FFB_SUPPORTED);
506 return(joyxi->capabilities.Flags & XINPUT_CAPS_FFB_SUPPORTED);
507 #endif
508 }
509
510
511 static bool hapxi_is_display_haptic(ALLEGRO_DISPLAY *dev)
512 {
513 (void)dev;
514 return false;
515 }
516
517
518 static bool hapxi_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev)
519 {
520 (void)dev;
521 return false;
522 }
523
524
525 static bool hapxi_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev)
526 {
527 (void)dev;
528 return false;
529 }
530
531
532 static ALLEGRO_HAPTIC *hapxi_get_from_mouse(ALLEGRO_MOUSE *mouse)
533 {
534 (void)mouse;
535 return NULL;
536 }
537
538
539 static ALLEGRO_HAPTIC *hapxi_get_from_joystick(ALLEGRO_JOYSTICK *joy)
540 {
541 ALLEGRO_JOYSTICK_XINPUT *joyxi = (ALLEGRO_JOYSTICK_XINPUT *)joy;
542 ALLEGRO_HAPTIC_XINPUT *hapxi;
543
544 if (!al_is_joystick_haptic(joy))
545 return NULL;
546
547 al_lock_mutex(hapxi_mutex);
548
549 hapxi = haptics + joyxi->index;
550 hapxi->parent.driver = &_al_hapdrv_xinput;
551 hapxi->parent.device = joyxi;
552 hapxi->parent.from = _AL_HAPTIC_FROM_JOYSTICK;
553 hapxi->active = true;
554 hapxi->effect.state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE;
555 /* not in use */
556 hapxi->parent.gain = 1.0;
557 hapxi->parent.autocenter = 0.0;
558 hapxi->flags = ALLEGRO_HAPTIC_RUMBLE;
559 hapxi->xjoy = joyxi;
560 al_unlock_mutex(hapxi_mutex);
561
562 return &hapxi->parent;
563 }
564
565
566 static ALLEGRO_HAPTIC *hapxi_get_from_display(ALLEGRO_DISPLAY *dev)
567 {
568 (void)dev;
569 return NULL;
570 }
571
572
573 static ALLEGRO_HAPTIC *hapxi_get_from_keyboard(ALLEGRO_KEYBOARD *dev)
574 {
575 (void)dev;
576 return NULL;
577 }
578
579
580 static ALLEGRO_HAPTIC *hapxi_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev)
581 {
582 (void)dev;
583 return NULL;
584 }
585
586
587 static int hapxi_get_capabilities(ALLEGRO_HAPTIC *dev)
588 {
589 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_from_al(dev);
590 return hapxi->flags;
591 }
592
593
594 static double hapxi_get_gain(ALLEGRO_HAPTIC *dev)
595 {
596 (void)dev;
597 /* Just return the 1.0, gain isn't supported */
598 return 1.0;
599 }
600
601
602 static bool hapxi_set_gain(ALLEGRO_HAPTIC *dev, double gain)
603 {
604 (void)dev; (void)gain;
605 /* Gain not supported*/
606 return false;
607 }
608
609
610 double hapxi_get_autocenter(ALLEGRO_HAPTIC *dev)
611 {
612 (void)dev;
613 /* Autocenter not supported so return 0.0. */
614 return 0.0;
615 }
616
617
618 static bool hapxi_set_autocenter(ALLEGRO_HAPTIC *dev, double intensity)
619 {
620 (void)dev; (void)intensity;
621 /* Autocenter not supported*/
622 return false;
623 }
624
625 static int hapxi_get_max_effects(ALLEGRO_HAPTIC *dev)
626 {
627 (void)dev;
628 /* Support only one effect */
629 return 1;
630 }
631
632
633 static bool hapxi_is_effect_ok(ALLEGRO_HAPTIC *haptic,
634 ALLEGRO_HAPTIC_EFFECT *effect)
635 {
636 int caps;
637
638 caps = al_get_haptic_capabilities(haptic);
639 if (caps & effect->type) {
640 return true;
641 }
642 return false;
643 }
644
645 /* Gets an available haptic effect slot from the device or NULL if not
646 * available.
647 */
648 static ALLEGRO_HAPTIC_EFFECT_XINPUT *
649 hapxi_get_available_effect(ALLEGRO_HAPTIC_XINPUT *hapxi)
650 {
651 if (hapxi->effect.state == ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE) {
652 /* Set up ID here. */
653 hapxi->effect.id = 0;
654 return &hapxi->effect;
655 }
656 return NULL;
657 }
658
659 static bool hapxi_release_effect_windows(ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi)
660 {
661 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE;
662 return true;
663 }
664
665 static bool hapxi_upload_effect(ALLEGRO_HAPTIC *dev,
666 ALLEGRO_HAPTIC_EFFECT *effect, ALLEGRO_HAPTIC_EFFECT_ID *id)
667 {
668 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_from_al(dev);
669 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi = NULL;
670
671 ASSERT(dev);
672 ASSERT(id);
673 ASSERT(effect);
674
675 /* Set id's values to indicate failure beforehand. */
676 id->_haptic = NULL;
677 id->_id = -1;
678 id->_pointer = NULL;
679 id->_playing = false;
680 id->_effect_duration = 0.0;
681 id->_start_time = 0.0;
682 id->_end_time = 0.0;
683
684 if (!al_is_haptic_effect_ok(dev, effect))
685 return false;
686
687 al_lock_mutex(hapxi_mutex);
688
689 /* Is a haptic effect slot available? */
690 effxi = hapxi_get_available_effect(hapxi);
691 /* No more space for an effect. */
692 if (!effxi) {
693 ALLEGRO_WARN("No free effect slot.");
694 al_unlock_mutex(hapxi_mutex);
695 return false;
696 }
697
698 if (!hapxi_effect2win(effxi, effect, hapxi)) {
699 ALLEGRO_WARN("Cannot convert haptic effect to XINPUT effect.\n");
700 al_unlock_mutex(hapxi_mutex);
701 return false;
702 }
703
704 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY;
705 effxi->effect = (*effect);
706 /* set ID handle to signify success */
707 id->_haptic = dev;
708 id->_pointer = effxi;
709 id->_id = effxi->id;
710 id->_effect_duration = al_get_haptic_effect_duration(effect);
711
712 al_unlock_mutex(hapxi_mutex);
713 return true;
714 }
715
716
717 static ALLEGRO_HAPTIC_XINPUT *
718 hapxi_device_for_id(ALLEGRO_HAPTIC_EFFECT_ID *id)
719 {
720 return (ALLEGRO_HAPTIC_XINPUT *)id->_haptic;
721 }
722
723
724 static ALLEGRO_HAPTIC_EFFECT_XINPUT *
725 hapxi_effect_for_id(ALLEGRO_HAPTIC_EFFECT_ID *id)
726 {
727 return (ALLEGRO_HAPTIC_EFFECT_XINPUT *)id->_pointer;
728 }
729
730 static bool hapxi_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loops)
731 {
732 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_device_for_id(id);
733 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi = hapxi_effect_for_id(id);
734
735 if ((!hapxi) || (id->_id < 0) || (!effxi) || (loops < 1))
736 return false;
737 al_lock_mutex(hapxi_mutex);
738 /* Simply set some flags. The polling thread will see this and start playing.
739 after the effect's delay has passed. */
740 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STARTING;
741 effxi->start_time = al_get_time();
742 effxi->stop_time = effxi->start_time + al_get_haptic_effect_duration(&effxi->effect) * loops;
743 effxi->repeats = loops;
744 effxi->play_repeated = 0;
745 effxi->loop_start = effxi->start_time;
746
747 id->_playing = true;
748 id->_start_time = al_get_time();
749 id->_start_time = effxi->start_time;
750 id->_end_time = effxi->stop_time;
751 al_unlock_mutex(hapxi_mutex);
752 al_signal_cond(hapxi_cond);
753 return true;
754 }
755
756
757 static bool hapxi_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id)
758 {
759 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_device_for_id(id);
760 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi = hapxi_effect_for_id(id);
761
762 if ((!hapxi) || (id->_id < 0))
763 return false;
764 /* Simply set some flags. The polling thread will see this and stop playing.*/
765 effxi = (ALLEGRO_HAPTIC_EFFECT_XINPUT *)id->_pointer;
766 if (effxi->state <= ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY) return false;
767 al_lock_mutex(hapxi_mutex);
768 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_STOPPING;
769 id->_playing = false;
770 al_unlock_mutex(hapxi_mutex);
771 al_signal_cond(hapxi_cond);
772 return true;
773 }
774
775
776 static bool hapxi_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id)
777 {
778 ALLEGRO_HAPTIC_XINPUT *hapxi;
779 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi;
780 bool result;
781 ASSERT(id);
782
783 hapxi = hapxi_device_for_id(id);
784 effxi = hapxi_effect_for_id(id);
785
786 if ((!hapxi) || (id->_id < 0) || (!id->_playing))
787 return false;
788 al_lock_mutex(hapxi_mutex);
789 ALLEGRO_DEBUG("Playing effect state: %d %p %lf %lf\n", effxi->state, effxi, al_get_time(), id->_end_time);
790
791 result = (effxi->state > ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_READY);
792 al_unlock_mutex(hapxi_mutex);
793 al_signal_cond(hapxi_cond);
794
795 return result;
796 }
797
798
799 static bool hapxi_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id)
800 {
801 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_device_for_id(id);
802 ALLEGRO_HAPTIC_EFFECT_XINPUT *effxi = hapxi_effect_for_id(id);
803 bool result;
804 if ((!hapxi) || (!effxi))
805 return false;
806
807 al_lock_mutex(hapxi_mutex);
808 /* Forcefully stop since a normal stop may not be instant. */
809 hapxi_force_stop(hapxi, effxi);
810 effxi->state = ALLEGRO_HAPTIC_EFFECT_XINPUT_STATE_INACTIVE;
811 result = hapxi_release_effect_windows(effxi);
812 al_unlock_mutex(hapxi_mutex);
813 return result;
814 }
815
816
817 static bool hapxi_release(ALLEGRO_HAPTIC *haptic)
818 {
819 ALLEGRO_HAPTIC_XINPUT *hapxi = hapxi_from_al(haptic);
820 ASSERT(haptic);
821
822 if (!hapxi->active)
823 return false;
824 al_lock_mutex(hapxi_mutex);
825
826 /* Release the effect for this device. */
827 /* Forcefully stop since a normal stop may not be instant. */
828 hapxi_force_stop(hapxi, &hapxi->effect);
829 hapxi_release_effect_windows(&hapxi->effect);
830
831 hapxi->active = false;
832 hapxi->parent.device = NULL;
833 al_unlock_mutex(hapxi_mutex);
834 return true;
835 }
836
837 #endif
838
839 /* vim: set sts=3 sw=3 et: */