1 /*
2  *    Example program for the Allegro library, by Beoran.
3  *
4  *    This program tests haptic effects.
5  */
6 
7 #define ALLEGRO_UNSTABLE
8 #include <allegro5/allegro.h>
9 #include <allegro5/allegro_primitives.h>
10 
11 #include "common.c"
12 
13 
test_haptic_joystick(ALLEGRO_JOYSTICK * joy)14 static void test_haptic_joystick(ALLEGRO_JOYSTICK *joy)
15 {
16    ALLEGRO_HAPTIC_EFFECT_ID id;
17    ALLEGRO_HAPTIC *haptic;
18    ALLEGRO_HAPTIC_EFFECT effect;
19    const double intensity = 1.0;
20    const double duration = 1.0;
21 
22    haptic = al_get_haptic_from_joystick(joy);
23 
24    if (!haptic) {
25       log_printf("Could not get haptic device from joystick!");
26       return;
27    }
28 
29    log_printf("Can play back %d haptic effects.\n",
30       al_get_max_haptic_effects(haptic));
31 
32    log_printf("Set gain to 0.8: %d.\n",
33       al_set_haptic_gain(haptic, 0.8));
34 
35    log_printf("Get gain: %lf.\n",
36       al_get_haptic_gain(haptic));
37 
38    log_printf("Capabilities: %d.\n",
39       al_get_haptic_capabilities(haptic));
40 
41    memset(&effect, 0, sizeof(effect));
42    effect.type = ALLEGRO_HAPTIC_RUMBLE;
43    effect.data.rumble.strong_magnitude = intensity;
44    effect.data.rumble.weak_magnitude = intensity;
45    effect.replay.delay = 0.1;
46    effect.replay.length = duration;
47 
48    log_printf("Upload effect: %d.\n",
49       al_upload_haptic_effect(haptic, &effect, &id));
50 
51    log_printf("Playing effect: %d.\n",
52       al_play_haptic_effect(&id, 3));
53 
54    do {
55       al_rest(0.1);
56    } while (al_is_haptic_effect_playing(&id));
57 
58    log_printf("Set gain to 0.4: %d.\n",
59       al_set_haptic_gain(haptic, 0.4));
60 
61    log_printf("Get gain: %lf.\n",
62       al_get_haptic_gain(haptic));
63 
64    log_printf("Playing effect again: %d.\n",
65       al_play_haptic_effect(&id, 5));
66 
67    al_rest(2.0);
68 
69    log_printf("Stopping effect: %d.\n",
70       al_stop_haptic_effect(&id));
71 
72    do {
73       al_rest(0.1);
74    } while (al_is_haptic_effect_playing(&id));
75 
76    log_printf("Release effect: %d.\n",
77       al_release_haptic_effect(&id));
78 
79    log_printf("Release haptic: %d.\n",
80       al_release_haptic(haptic));
81 }
82 
83 
main(int argc,char ** argv)84 int main(int argc, char **argv)
85 {
86    ALLEGRO_DISPLAY *display;
87    int num_joysticks;
88    int i;
89 
90    (void)argc;
91    (void)argv;
92 
93    if (!al_init()) {
94       abort_example("Could not init Allegro.\n");
95    }
96 
97    display = al_create_display(640, 480);
98    if (!display) {
99       abort_example("al_create_display failed\n");
100    }
101    if (!al_install_joystick()) {
102       abort_example("al_install_joystick failed\n");
103    }
104    if (!al_install_haptic()) {
105       abort_example("al_install_haptic failed\n");
106    }
107 
108    open_log();
109 
110    num_joysticks = al_get_num_joysticks();
111    log_printf("Found %d joysticks.\n", num_joysticks);
112 
113    for (i = 0; i < num_joysticks; i++) {
114       ALLEGRO_JOYSTICK *joy = al_get_joystick(i);
115       if (!joy) {
116          continue;
117       }
118 
119       if (al_is_joystick_haptic(joy)) {
120          log_printf("Joystick %s supports force feedback.\n",
121             al_get_joystick_name(joy));
122          test_haptic_joystick(joy);
123       }
124       else {
125          log_printf("Joystick %s does not support force feedback.\n",
126             al_get_joystick_name(joy));
127       }
128 
129       al_release_joystick(joy);
130    }
131 
132    log_printf("\nAll done!\n");
133    close_log(true);
134 
135    return 0;
136 }
137 
138 /* vim: set ts=8 sts=3 sw=3 et: */
139