Debian Codegraph @

src/linux/lhaptic.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Linux haptic (force-feedback) device driver.
12 *
13 * By Beoran.
14 *
15 * See LICENSE.txt for copyright information.
16 */
17
18
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22
23 #include <math.h>
24 #include <stdio.h>
25 #include <sys/time.h>
26
27 #include "allegro5/allegro.h"
28 #include "allegro5/internal/aintern_haptic.h"
29 #include "allegro5/internal/aintern_ljoynu.h"
30 #include "allegro5/platform/aintunix.h"
31
32 #ifdef ALLEGRO_HAVE_LINUX_INPUT_H
33
34 #include <linux/input.h>
35
36 ALLEGRO_DEBUG_CHANNEL("lhaptic")
37
38
39 /* For compatibility with older kernels. */
40 #ifndef input_event_sec
41 #define input_event_sec time.tv_sec
42 #define input_event_usec time.tv_usec
43 #endif
44
45
46 /* Support at most 32 haptic devices. */
47 #define HAPTICS_MAX 32
48
49 /* Support at most 16 effects per device. */
50 #define HAPTICS_EFFECTS_MAX 16
51
52
53 typedef struct
54 {
55 struct ALLEGRO_HAPTIC parent; /* must be first */
56 bool active;
57 int fd;
58 int flags;
59 int effects[HAPTICS_EFFECTS_MAX];
60 } ALLEGRO_HAPTIC_LINUX;
61
62
63 #define LONG_BITS (sizeof(long) * 8)
64 #define NLONGS(x) (((x) + LONG_BITS - 1) / LONG_BITS)
65 /* Tests if a bit in an array of longs is set. */
66 #define TEST_BIT(nr, addr) \
67 ((1UL << ((nr) % LONG_BITS)) & (addr)[(nr) / LONG_BITS])
68
69
70 /* forward declarations */
71 static bool lhap_init_haptic(void);
72 static void lhap_exit_haptic(void);
73
74 static bool lhap_is_mouse_haptic(ALLEGRO_MOUSE *dev);
75 static bool lhap_is_joystick_haptic(ALLEGRO_JOYSTICK *);
76 static bool lhap_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev);
77 static bool lhap_is_display_haptic(ALLEGRO_DISPLAY *dev);
78 static bool lhap_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev);
79
80 static ALLEGRO_HAPTIC *lhap_get_from_mouse(ALLEGRO_MOUSE *dev);
81 static ALLEGRO_HAPTIC *lhap_get_from_joystick(ALLEGRO_JOYSTICK *dev);
82 static ALLEGRO_HAPTIC *lhap_get_from_keyboard(ALLEGRO_KEYBOARD *dev);
83 static ALLEGRO_HAPTIC *lhap_get_from_display(ALLEGRO_DISPLAY *dev);
84 static ALLEGRO_HAPTIC *lhap_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev);
85
86 static bool lhap_release(ALLEGRO_HAPTIC *haptic);
87
88 static bool lhap_get_active(ALLEGRO_HAPTIC *hap);
89 static int lhap_get_capabilities(ALLEGRO_HAPTIC *dev);
90 static double lhap_get_gain(ALLEGRO_HAPTIC *dev);
91 static bool lhap_set_gain(ALLEGRO_HAPTIC *dev, double);
92 static int lhap_get_max_effects(ALLEGRO_HAPTIC *dev);
93
94 static bool lhap_is_effect_ok(ALLEGRO_HAPTIC *dev, ALLEGRO_HAPTIC_EFFECT *eff);
95 static bool lhap_upload_effect(ALLEGRO_HAPTIC *dev,
96 ALLEGRO_HAPTIC_EFFECT *eff,
97 ALLEGRO_HAPTIC_EFFECT_ID *id);
98 static bool lhap_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loop);
99 static bool lhap_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id);
100 static bool lhap_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id);
101 static bool lhap_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id);
102
103 static double lhap_get_autocenter(ALLEGRO_HAPTIC *dev);
104 static bool lhap_set_autocenter(ALLEGRO_HAPTIC *dev, double);
105
106 static void lhap_timerclear(struct input_event *evt);
107
108 ALLEGRO_HAPTIC_DRIVER _al_hapdrv_linux =
109 {
110 _ALLEGRO_HAPDRV_LINUX,
111 "",
112 "",
113 "Linux haptic(s)",
114 lhap_init_haptic,
115 lhap_exit_haptic,
116
117 lhap_is_mouse_haptic,
118 lhap_is_joystick_haptic,
119 lhap_is_keyboard_haptic,
120 lhap_is_display_haptic,
121 lhap_is_touch_input_haptic,
122
123 lhap_get_from_mouse,
124 lhap_get_from_joystick,
125 lhap_get_from_keyboard,
126 lhap_get_from_display,
127 lhap_get_from_touch_input,
128
129 lhap_get_active,
130 lhap_get_capabilities,
131 lhap_get_gain,
132 lhap_set_gain,
133 lhap_get_max_effects,
134
135 lhap_is_effect_ok,
136 lhap_upload_effect,
137 lhap_play_effect,
138 lhap_stop_effect,
139 lhap_is_effect_playing,
140 lhap_release_effect,
141
142 lhap_release,
143
144 lhap_get_autocenter,
145 lhap_set_autocenter
146 };
147
148
149 static ALLEGRO_HAPTIC_LINUX haptics[HAPTICS_MAX];
150 static ALLEGRO_MUTEX *haptic_mutex = NULL;
151
152
153 struct CAP_MAP {
154 int linux_bit;
155 int allegro_bit;
156 };
157
158 static const struct CAP_MAP cap_map[] = {
159 { FF_PERIODIC, ALLEGRO_HAPTIC_PERIODIC },
160 { FF_RUMBLE, ALLEGRO_HAPTIC_RUMBLE },
161 { FF_CONSTANT, ALLEGRO_HAPTIC_CONSTANT },
162 { FF_SPRING, ALLEGRO_HAPTIC_SPRING },
163 { FF_FRICTION, ALLEGRO_HAPTIC_FRICTION },
164 { FF_DAMPER, ALLEGRO_HAPTIC_DAMPER },
165 { FF_INERTIA, ALLEGRO_HAPTIC_INERTIA },
166 { FF_RAMP, ALLEGRO_HAPTIC_RAMP },
167 { FF_SQUARE, ALLEGRO_HAPTIC_SQUARE },
168 { FF_TRIANGLE, ALLEGRO_HAPTIC_TRIANGLE },
169 { FF_SINE, ALLEGRO_HAPTIC_SINE },
170 { FF_SAW_UP, ALLEGRO_HAPTIC_SAW_UP },
171 { FF_SAW_DOWN, ALLEGRO_HAPTIC_SAW_DOWN },
172 { FF_CUSTOM, ALLEGRO_HAPTIC_CUSTOM },
173 { FF_GAIN, ALLEGRO_HAPTIC_GAIN },
174 { FF_AUTOCENTER, ALLEGRO_HAPTIC_AUTOCENTER },
175 { -1, -1 }
176 };
177
178
179 static bool lhap_init_haptic(void)
180 {
181 int i;
182
183 ASSERT(haptic_mutex == NULL);
184 haptic_mutex = al_create_mutex();
185 if (!haptic_mutex)
186 return false;
187
188 for (i = 0; i < HAPTICS_MAX; i++) {
189 haptics[i].active = false;
190 }
191
192 return true;
193 }
194
195
196 static ALLEGRO_HAPTIC_LINUX *lhap_get_available_haptic(void)
197 {
198 int i;
199
200 for (i = 0; i < HAPTICS_MAX; i++) {
201 if (!haptics[i].active) {
202 haptics[i].active = true;
203 return &haptics[i];
204 }
205 }
206
207 return NULL;
208 }
209
210
211 /* Converts a generic haptic device to a Linux-specific one. */
212 static ALLEGRO_HAPTIC_LINUX *lhap_from_al(ALLEGRO_HAPTIC *hap)
213 {
214 return (ALLEGRO_HAPTIC_LINUX *)hap;
215 }
216
217
218 static void lhap_exit_haptic(void)
219 {
220 ASSERT(haptic_mutex);
221 al_destroy_mutex(haptic_mutex);
222 haptic_mutex = NULL;
223 }
224
225
226 static bool lhap_type2lin(__u16 *res, int type)
227 {
228 ASSERT(res);
229
230 switch (type) {
231 case ALLEGRO_HAPTIC_RUMBLE:
232 (*res) = FF_RUMBLE;
233 break;
234 case ALLEGRO_HAPTIC_PERIODIC:
235 (*res) = FF_PERIODIC;
236 break;
237 case ALLEGRO_HAPTIC_CONSTANT:
238 (*res) = FF_CONSTANT;
239 break;
240 case ALLEGRO_HAPTIC_SPRING:
241 (*res) = FF_SPRING;
242 break;
243 case ALLEGRO_HAPTIC_FRICTION:
244 (*res) = FF_FRICTION;
245 break;
246 case ALLEGRO_HAPTIC_DAMPER:
247 (*res) = FF_DAMPER;
248 break;
249 case ALLEGRO_HAPTIC_INERTIA:
250 (*res) = FF_INERTIA;
251 break;
252 case ALLEGRO_HAPTIC_RAMP:
253 (*res) = FF_RAMP;
254 break;
255 default:
256 return false;
257 }
258 return true;
259 }
260
261
262 static bool lhap_wave2lin(__u16 *res, int type)
263 {
264 ASSERT(res);
265
266 switch (type) {
267 case ALLEGRO_HAPTIC_SQUARE:
268 (*res) = FF_SQUARE;
269 break;
270 case ALLEGRO_HAPTIC_TRIANGLE:
271 (*res) = FF_TRIANGLE;
272 break;
273 case ALLEGRO_HAPTIC_SINE:
274 (*res) = FF_SINE;
275 break;
276 case ALLEGRO_HAPTIC_SAW_UP:
277 (*res) = FF_SAW_UP;
278 break;
279 case ALLEGRO_HAPTIC_SAW_DOWN:
280 (*res) = FF_SAW_DOWN;
281 break;
282 case ALLEGRO_HAPTIC_CUSTOM:
283 (*res) = FF_CUSTOM;
284 break;
285 default:
286 return false;
287 }
288 return true;
289 }
290
291
292 /* Converts the time in seconds to a Linux-compatible time.
293 * Return false if out of bounds.
294 */
295 static bool lhap_time2lin(__u16 *res, double sec)
296 {
297 ASSERT(res);
298
299 if (sec < 0.0 || sec > 32.767)
300 return false;
301 (*res) = (__u16) round(sec * 1000.0);
302 return true;
303 }
304
305
306 /* Converts the time in seconds to a Linux-compatible time.
307 * Return false if out of bounds. This one allows negative times.
308 */
309 static bool lhap_stime2lin(__s16 *res, double sec)
310 {
311 ASSERT(res);
312
313 if (sec < -32.767 || sec > 32.767)
314 return false;
315 (*res) = (__s16) round(sec * 1000.0);
316 return true;
317 }
318
319
320 /* Converts replay data to Linux-compatible data. */
321 static bool lhap_replay2lin(struct ff_replay *lin,
322 struct ALLEGRO_HAPTIC_REPLAY *al)
323 {
324 return lhap_time2lin(&lin->delay, al->delay)
325 && lhap_time2lin(&lin->length, al->length);
326 }
327
328
329 /* Converts the level in range 0.0 to 1.0 to a Linux-compatible level.
330 * Returns false if out of bounds.
331 */
332 static bool lhap_level2lin(__u16 *res, double level)
333 {
334 ASSERT(res);
335
336 if (level < 0.0 || level > 1.0)
337 return false;
338 *res = (__u16) round(level * (double)0x7fff);
339 return true;
340 }
341
342
343 /* Converts the level in range -1.0 to 1.0 to a Linux-compatible level.
344 * Returns false if out of bounds.
345 */
346 static bool lhap_slevel2lin(__s16 *res, double level)
347 {
348 ASSERT(res);
349
350 if (level < -1.0 || level > 1.0)
351 return false;
352 *res = (__s16) round(level * (double)0x7ffe);
353 return true;
354 }
355
356
357 /* Converts an Allegro haptic effect envelope to Linux input API. */
358 static bool lhap_envelope2lin(struct ff_envelope *lin,
359 struct ALLEGRO_HAPTIC_ENVELOPE *al)
360 {
361 return lhap_time2lin(&lin->attack_length, al->attack_length)
362 && lhap_time2lin(&lin->fade_length, al->fade_length)
363 && lhap_level2lin(&lin->attack_level, al->attack_level)
364 && lhap_level2lin(&lin->fade_level, al->fade_level);
365 }
366
367
368 /* Converts a rumble effect to Linux input API. */
369 static bool lhap_rumble2lin(struct ff_rumble_effect *lin,
370 struct ALLEGRO_HAPTIC_RUMBLE_EFFECT *al)
371 {
372 return lhap_level2lin(&lin->strong_magnitude, al->strong_magnitude)
373 && lhap_level2lin(&lin->weak_magnitude, al->weak_magnitude);
374 }
375
376
377 /* Converts a constant effect to Linux input API. */
378 static bool lhap_constant2lin(struct ff_constant_effect *lin,
379 struct ALLEGRO_HAPTIC_CONSTANT_EFFECT *al)
380 {
381 return lhap_envelope2lin(&lin->envelope, &al->envelope)
382 && lhap_slevel2lin(&lin->level, al->level);
383 }
384
385
386 /* Converts a ramp effect to Linux input API. */
387 static bool lhap_ramp2lin(struct ff_ramp_effect *lin,
388 struct ALLEGRO_HAPTIC_RAMP_EFFECT *al)
389 {
390 return lhap_envelope2lin(&lin->envelope, &al->envelope)
391 && lhap_slevel2lin(&lin->start_level, al->start_level)
392 && lhap_slevel2lin(&lin->end_level, al->end_level);
393 }
394
395
396 /* Converts a ramp effect to Linux input API. */
397 static bool lhap_condition2lin(struct ff_condition_effect *lin,
398 struct ALLEGRO_HAPTIC_CONDITION_EFFECT *al)
399 {
400 return lhap_slevel2lin(&lin->center, al->center)
401 && lhap_level2lin(&lin->deadband, al->deadband)
402 && lhap_slevel2lin(&lin->right_coeff, al->right_coeff)
403 && lhap_level2lin(&lin->right_saturation, al->right_saturation)
404 && lhap_slevel2lin(&lin->left_coeff, al->left_coeff)
405 && lhap_level2lin(&lin->left_saturation, al->left_saturation);
406 }
407
408
409 /* Converts a periodic effect to linux input API. */
410 static bool lhap_periodic2lin(struct ff_periodic_effect *lin,
411 struct ALLEGRO_HAPTIC_PERIODIC_EFFECT *al)
412 {
413 /* Custom data is not supported yet, because currently no Linux
414 * haptic driver supports it.
415 */
416 if (al->custom_data)
417 return false;
418
419 return lhap_slevel2lin(&lin->magnitude, al->magnitude)
420 && lhap_stime2lin(&lin->offset, al->offset)
421 && lhap_time2lin(&lin->period, al->period)
422 && lhap_time2lin(&lin->phase, al->phase)
423 && lhap_wave2lin(&lin->waveform, al->waveform)
424 && lhap_envelope2lin(&lin->envelope, &al->envelope);
425 }
426
427
428 /* Converts Allegro haptic effect to Linux input API. */
429 static bool lhap_effect2lin(struct ff_effect *lin, ALLEGRO_HAPTIC_EFFECT *al)
430 {
431 memset(lin, 0, sizeof(*lin));
432
433 if (!lhap_type2lin(&lin->type, al->type))
434 return false;
435 /* lin_effect->replay = effect->re; */
436 lin->direction = (__u16)
437 round(((double)0xC000 * al->direction.angle) / (2 * M_PI));
438 lin->id = -1;
439 if (!lhap_replay2lin(&lin->replay, &al->replay))
440 return false;
441 switch (lin->type) {
442 case FF_RUMBLE:
443 return lhap_rumble2lin(&lin->u.rumble, &al->data.rumble);
444 case FF_PERIODIC:
445 return lhap_periodic2lin(&lin->u.periodic, &al->data.periodic);
446 case FF_CONSTANT:
447 return lhap_constant2lin(&lin->u.constant, &al->data.constant);
448 case FF_RAMP:
449 return lhap_ramp2lin(&lin->u.ramp, &al->data.ramp);
450 case FF_SPRING: /* fall through */
451 case FF_FRICTION: /* fall through */
452 case FF_DAMPER: /* fall through */
453 case FF_INERTIA:
454 return lhap_condition2lin(&lin->u.condition[0], &al->data.condition);
455 default:
456 return false;
457 }
458 }
459
460
461 static bool lhap_get_active(ALLEGRO_HAPTIC *haptic)
462 {
463 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(haptic);
464 return lhap->active;
465 }
466
467
468 static bool lhap_is_mouse_haptic(ALLEGRO_MOUSE *mouse)
469 {
470 (void)mouse;
471 return false;
472 }
473
474
475 static bool lhap_fd_can_ff(int fd)
476 {
477 long bitmask[NLONGS(EV_CNT)] = { 0 };
478
479 if (ioctl(fd, EVIOCGBIT(0, sizeof(bitmask)), bitmask) < 0) {
480 return false;
481 }
482 if (TEST_BIT(EV_FF, bitmask)) {
483 return true;
484 }
485 return false;
486 }
487
488
489 static bool lhap_is_joystick_haptic(ALLEGRO_JOYSTICK *joy)
490 {
491 ALLEGRO_JOYSTICK_LINUX *ljoy = (ALLEGRO_JOYSTICK_LINUX *) joy;
492 if (!al_is_joystick_installed())
493 return false;
494 if (!al_get_joystick_active(joy))
495 return false;
496 if (ljoy->fd <= 0)
497 return false;
498 return lhap_fd_can_ff(ljoy->fd);
499 }
500
501
502 static bool lhap_is_display_haptic(ALLEGRO_DISPLAY *dev)
503 {
504 (void)dev;
505 return false;
506 }
507
508
509 static bool lhap_is_keyboard_haptic(ALLEGRO_KEYBOARD *dev)
510 {
511 (void)dev;
512 return false;
513 }
514
515
516 static bool lhap_is_touch_input_haptic(ALLEGRO_TOUCH_INPUT *dev)
517 {
518 (void)dev;
519 return false;
520 }
521
522
523 static ALLEGRO_HAPTIC *lhap_get_from_mouse(ALLEGRO_MOUSE *mouse)
524 {
525 (void)mouse;
526 return NULL;
527 }
528
529
530 static bool get_haptic_capabilities(int fd, int *capabilities)
531 {
532 unsigned long bitmask[NLONGS(FF_CNT)] = { 0 };
533 int caps;
534 int i;
535
536 if (ioctl(fd, EVIOCGBIT(EV_FF, sizeof(bitmask)), bitmask) < 0) {
537 ALLEGRO_ERROR("EVIOCGBIT failed for fd %d", fd);
538 return false;
539 }
540
541 caps = 0;
542 for (i = 0; cap_map[i].allegro_bit >= 0; i++) {
543 if (TEST_BIT(cap_map[i].linux_bit, bitmask)) {
544 caps |= cap_map[i].allegro_bit;
545 }
546 }
547 (*capabilities) = caps;
548 ALLEGRO_INFO("Capabilities: 0x%x\n", caps);
549 return true;
550 }
551
552
553 static ALLEGRO_HAPTIC *lhap_get_from_joystick(ALLEGRO_JOYSTICK *joy)
554 {
555 ALLEGRO_JOYSTICK_LINUX *ljoy = (ALLEGRO_JOYSTICK_LINUX *) joy;
556 ALLEGRO_HAPTIC_LINUX *lhap;
557 int i;
558
559 if (!al_is_joystick_haptic(joy))
560 return NULL;
561
562 al_lock_mutex(haptic_mutex);
563
564 lhap = lhap_get_available_haptic();
565
566 if (!lhap) {
567 al_unlock_mutex(haptic_mutex);
568 return NULL;
569 }
570
571 lhap->parent.device = joy;
572 lhap->parent.from = _AL_HAPTIC_FROM_JOYSTICK;
573
574 lhap->fd = ljoy->fd;
575 lhap->active = true;
576 for (i = 0; i < HAPTICS_EFFECTS_MAX; i++) {
577 lhap->effects[i] = -1; /* not in use */
578 }
579 lhap->parent.gain = 1.0;
580 get_haptic_capabilities(lhap->fd, &lhap->flags);
581
582 al_unlock_mutex(haptic_mutex);
583
584 return &lhap->parent;
585 }
586
587
588 static ALLEGRO_HAPTIC *lhap_get_from_display(ALLEGRO_DISPLAY *dev)
589 {
590 (void)dev;
591 return NULL;
592 }
593
594
595 static ALLEGRO_HAPTIC *lhap_get_from_keyboard(ALLEGRO_KEYBOARD *dev)
596 {
597 (void)dev;
598 return NULL;
599 }
600
601
602 static ALLEGRO_HAPTIC *lhap_get_from_touch_input(ALLEGRO_TOUCH_INPUT *dev)
603 {
604 (void)dev;
605 return NULL;
606 }
607
608
609 static int lhap_get_capabilities(ALLEGRO_HAPTIC *dev)
610 {
611 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
612 return lhap->flags;
613 }
614
615
616 static double lhap_get_gain(ALLEGRO_HAPTIC *dev)
617 {
618 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
619 (void)dev;
620
621 if(!al_is_haptic_capable(dev, ALLEGRO_HAPTIC_GAIN)) {
622 return 0.0;
623 }
624
625 /* Unfortunately there seems to be no API to GET gain, only to set?!
626 * So, return the stored gain.
627 */
628 return lhap->parent.gain;
629 }
630
631
632 static bool lhap_set_gain(ALLEGRO_HAPTIC *dev, double gain)
633 {
634 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
635 struct input_event ie;
636
637 lhap->parent.gain = gain;
638 lhap_timerclear(&ie);
639 ie.type = EV_FF;
640 ie.code = FF_GAIN;
641 ie.value = (__s32) ((double)0xFFFF * gain);
642 if (write(lhap->fd, &ie, sizeof(ie)) < 0) {
643 return false;
644 }
645 return true;
646 }
647
648
649 static bool lhap_set_autocenter(ALLEGRO_HAPTIC *dev, double autocenter)
650 {
651 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
652 struct input_event ie;
653
654 lhap->parent.autocenter = autocenter;
655 lhap_timerclear(&ie);
656 ie.type = EV_FF;
657 ie.code = FF_AUTOCENTER;
658 ie.value = (__s32) ((double)0xFFFF * autocenter);
659 if (write(lhap->fd, &ie, sizeof(ie)) < 0) {
660 return false;
661 }
662 return true;
663 }
664
665 static double lhap_get_autocenter(ALLEGRO_HAPTIC *dev)
666 {
667 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
668 (void)dev;
669
670 if(!al_is_haptic_capable(dev, ALLEGRO_HAPTIC_AUTOCENTER)) {
671 return 0.0;
672 }
673
674 /* Unfortunately there seems to be no API to GET gain, only to set?!
675 * So, return the stored autocenter.
676 */
677 return lhap->parent.autocenter;
678 }
679
680 int lhap_get_max_effects(ALLEGRO_HAPTIC *dev)
681 {
682 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
683 int n_effects;
684
685 if (ioctl(lhap->fd, EVIOCGEFFECTS, &n_effects) < 0) {
686 ALLEGRO_WARN("EVIOCGEFFECTS failed on fd %d\n", lhap->fd);
687 n_effects = HAPTICS_EFFECTS_MAX;
688 }
689
690 if (n_effects < HAPTICS_EFFECTS_MAX)
691 return n_effects;
692 else
693 return HAPTICS_EFFECTS_MAX;
694 }
695
696
697 static bool lhap_is_effect_ok(ALLEGRO_HAPTIC *haptic,
698 ALLEGRO_HAPTIC_EFFECT *effect)
699 {
700 int caps;
701 struct ff_effect leff;
702
703 caps = al_get_haptic_capabilities(haptic);
704 if (caps & effect->type) {
705 return lhap_effect2lin(&leff, effect);
706 }
707 return false;
708 }
709
710
711 static bool lhap_upload_effect(ALLEGRO_HAPTIC *dev,
712 ALLEGRO_HAPTIC_EFFECT *effect, ALLEGRO_HAPTIC_EFFECT_ID *id)
713 {
714 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(dev);
715 struct ff_effect leff;
716 int found;
717 int i;
718
719 ASSERT(dev);
720 ASSERT(id);
721 ASSERT(effect);
722
723 /* Set id's values to indicate failure. */
724 id->_haptic = NULL;
725 id->_id = -1;
726 id->_handle = -1;
727
728 if (!lhap_effect2lin(&leff, effect)) {
729 ALLEGRO_WARN("lhap_effect2lin failed");
730 return false;
731 }
732
733 /* Find empty spot for effect . */
734 found = -1;
735 for (i = 0; i < al_get_max_haptic_effects(dev); i++) {
736 if (lhap->effects[i] < 0) {
737 found = i;
738 break;
739 }
740 }
741
742 /* No more space for an effect. */
743 if (found < 0) {
744 ALLEGRO_WARN("No free effect slot.");
745 return false;
746 }
747
748 /* Upload effect. */
749 leff.id = -1;
750 if (ioctl(lhap->fd, EVIOCSFF, &leff) < 0) {
751 ALLEGRO_ERROR("EVIOCSFF failed for fd %d\n", lhap->fd);
752 return false;
753 }
754
755 id->_haptic = dev;
756 id->_id = found;
757 id->_handle = leff.id;
758 id->_effect_duration = al_get_haptic_effect_duration(effect);
759 id->_playing = false;
760
761 /* XXX should be bool or something? */
762 lhap->effects[i] = found;
763
764 return true;
765 }
766
767
768 static bool lhap_play_effect(ALLEGRO_HAPTIC_EFFECT_ID *id, int loops)
769 {
770 ALLEGRO_HAPTIC_LINUX *lhap = (ALLEGRO_HAPTIC_LINUX *) id->_haptic;
771 struct input_event play;
772 int fd;
773 double now;
774 double duration;
775
776 if (!lhap)
777 return false;
778
779 fd = lhap->fd;
780
781 lhap_timerclear(&play);
782 play.type = EV_FF;
783 play.code = id->_handle;
784 loops = (loops < 0) ? 1 : loops;
785 play.value = loops; /* play: 1, stop: 0 */
786
787 if (write(fd, (const void *)&play, sizeof(play)) < 0) {
788 ALLEGRO_ERROR("Effect play failed.\n");
789 return false;
790 }
791
792 now = al_get_time();
793 duration = loops * id->_effect_duration;
794
795 id->_playing = true;
796 id->_start_time = now;
797 id->_end_time = now + duration;
798
799 return true;
800 }
801
802
803 static bool lhap_stop_effect(ALLEGRO_HAPTIC_EFFECT_ID *id)
804 {
805 ALLEGRO_HAPTIC_LINUX *lhap = (ALLEGRO_HAPTIC_LINUX *) id->_haptic;
806 struct input_event play;
807
808 if (!lhap)
809 return false;
810
811 memset(&play, 0, sizeof(play));
812
813 play.type = EV_FF;
814 play.code = id->_handle;
815 play.value = 0;
816 if (write(lhap->fd, (const void *)&play, sizeof(play)) < 0) {
817 ALLEGRO_ERROR("Stop effect failed.\n");
818 return false;
819 }
820 id->_playing = false;
821 return true;
822 }
823
824
825 static bool lhap_is_effect_playing(ALLEGRO_HAPTIC_EFFECT_ID *id)
826 {
827 ASSERT(id);
828
829 /* Since AFAICS there is no Linux API to test this, use a timer to check
830 * if the effect has been playing long enough to be finished or not.
831 */
832 return (id->_playing && al_get_time() < id->_end_time);
833 }
834
835
836 static bool lhap_release_effect(ALLEGRO_HAPTIC_EFFECT_ID *id)
837 {
838 ALLEGRO_HAPTIC_LINUX *lhap = (ALLEGRO_HAPTIC_LINUX *)id->_haptic;
839
840 lhap_stop_effect(id);
841
842 if (ioctl(lhap->fd, EVIOCRMFF, id->_handle) < 0) {
843 ALLEGRO_ERROR("EVIOCRMFF failed.\n");
844 return false;
845 }
846 lhap->effects[id->_id] = -1; /* not in use */
847 return true;
848 }
849
850
851 static bool lhap_release(ALLEGRO_HAPTIC *haptic)
852 {
853 ALLEGRO_HAPTIC_LINUX *lhap = lhap_from_al(haptic);
854 ASSERT(haptic);
855
856 if (!lhap->active)
857 return false;
858
859 lhap->active = false;
860 lhap->fd = -1;
861 return true;
862 }
863
864 void lhap_timerclear(struct input_event* evt)
865 {
866 evt->input_event_sec = 0;
867 evt->input_event_usec = 0;
868 }
869
870 #endif /* ALLEGRO_HAVE_LINUX_INPUT_H */
871
872
873 /* vim: set sts=3 sw=3 et: */