Debian Codegraph @

src/iphone/iphone_joystick.m

raw
1 #include "allegro5/allegro.h"
2 #include "allegro5/internal/aintern.h"
3 #include "allegro5/internal/aintern_events.h"
4 #include "allegro5/internal/aintern_joystick.h"
5 #include "allegro5/internal/aintern_iphone.h"
6
7 ALLEGRO_DEBUG_CHANNEL("iphone")
8
9 typedef struct ALLEGRO_JOYSTICK_IPHONE {
10 ALLEGRO_JOYSTICK parent;
11 ALLEGRO_JOYSTICK_STATE joystate;
12 } ALLEGRO_JOYSTICK_IPHONE;
13
14 static ALLEGRO_JOYSTICK_IPHONE the_joystick;
15 static bool initialized;
16
17 static bool ijoy_init_joystick(void)
18 {
19 ALLEGRO_JOYSTICK_IPHONE *ijoy;
20 ALLEGRO_JOYSTICK *joy;
21
22 ijoy = &the_joystick;
23
24 memset(ijoy, 0, sizeof *ijoy);
25 joy = (void *)ijoy;
26
27 /* Fill in the joystick information fields. */
28 joy->info.num_sticks = 1;
29 joy->info.num_buttons = 0;
30 joy->info.stick[0].name = "Accelerometer";
31 joy->info.stick[0].num_axes = 3;
32 joy->info.stick[0].axis[0].name = "X";
33 joy->info.stick[0].axis[1].name = "Y";
34 joy->info.stick[0].axis[2].name = "Z";
35 joy->info.stick[0].flags = ALLEGRO_JOYFLAG_ANALOGUE;
36
37 // TODO: What's a good frequency to use here?
38 _al_iphone_accelerometer_control(60);
39 initialized = true;
40
41 return true;
42 }
43
44 static void ijoy_exit_joystick(void)
45 {
46 _al_iphone_accelerometer_control(0);
47 initialized = false;
48 }
49
50 static bool ijoy_reconfigure_joysticks(void)
51 {
52 return false;
53 }
54
55 static int ijoy_num_joysticks(void)
56 {
57 return 1;
58 }
59
60 static ALLEGRO_JOYSTICK *ijoy_get_joystick(int num)
61 {
62 if (num != 0)
63 return NULL;
64
65 ALLEGRO_DEBUG("Joystick %d acquired.\n", num);
66
67 return &the_joystick.parent;
68 }
69
70 static void ijoy_release_joystick(ALLEGRO_JOYSTICK *joy)
71 {
72 (void)joy;
73 ALLEGRO_DEBUG("Joystick released.\n");
74 initialized = false;
75 }
76
77 static void ijoy_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_state)
78 {
79 ALLEGRO_JOYSTICK_IPHONE *ijoy = (void *)joy;
80 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
81
82 _al_event_source_lock(es);
83 *ret_state = ijoy->joystate;
84 _al_event_source_unlock(es);
85 }
86
87 void _al_iphone_generate_joystick_event(float x, float y, float z)
88 {
89 if (!initialized)
90 return;
91 ALLEGRO_EVENT_SOURCE *es = al_get_joystick_event_source();
92
93 ALLEGRO_EVENT event;
94
95 _al_event_source_lock(es);
96
97 if (_al_event_source_needs_to_generate_event(es)) {
98 float pos[] = {x, y, z};
99 for (int i = 0; i < 3; i++) {
100 event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS;
101 event.joystick.timestamp = al_get_time();
102 event.joystick.stick = 0;
103 event.joystick.axis = i;
104 event.joystick.pos = pos[i];
105 event.joystick.button = 0;
106 _al_event_source_emit_event(es, &event);
107 }
108 }
109 _al_event_source_unlock(es);
110 }
111
112 static char const *ijoy_get_name(ALLEGRO_JOYSTICK *joy)
113 {
114 (void)joy;
115 return "Accelerometer";
116 }
117
118 static bool ijoy_get_active(ALLEGRO_JOYSTICK *joy)
119 {
120 (void)joy;
121 return true;
122 }
123
124 static ALLEGRO_JOYSTICK_DRIVER iphone_joystick_driver = {
125 AL_ID('I', 'P', 'H', 'O'),
126 "",
127 "",
128 "iphone joystick",
129 ijoy_init_joystick,
130 ijoy_exit_joystick,
131 ijoy_reconfigure_joysticks,
132 ijoy_num_joysticks,
133 ijoy_get_joystick,
134 ijoy_release_joystick,
135 ijoy_get_joystick_state,
136 ijoy_get_name,
137 ijoy_get_active
138 };
139
140 ALLEGRO_JOYSTICK_DRIVER *_al_get_iphone_joystick_driver(void)
141 {
142 return &iphone_joystick_driver;
143 }