Debian Codegraph @

src/sdl/sdl_mouse.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * SDL mouse driver.
12 *
13 * See LICENSE.txt for copyright information.
14 */
15 #include "allegro5/allegro.h"
16 #include "allegro5/internal/aintern_system.h"
17 #include "allegro5/platform/allegro_internal_sdl.h"
18
19 ALLEGRO_DEBUG_CHANNEL("SDL")
20
21 typedef struct ALLEGRO_MOUSE_SDL
22 {
23 ALLEGRO_MOUSE mouse;
24 ALLEGRO_MOUSE_STATE state;
25 ALLEGRO_DISPLAY *display;
26 float ratio;
27 } ALLEGRO_MOUSE_SDL;
28
29 static ALLEGRO_MOUSE_DRIVER *vt;
30 static ALLEGRO_MOUSE_SDL *mouse;
31
32 static ALLEGRO_DISPLAY *find_display(uint32_t window_id)
33 {
34 ALLEGRO_DISPLAY *d = _al_sdl_find_display(window_id);
35 if (d) {
36 return d;
37 }
38 else {
39 // if there's only one display, we can assume that all
40 // events refer to its coordinate system
41 ALLEGRO_SYSTEM *s = al_get_system_driver();
42 if (_al_vector_size(&s->displays) == 1) {
43 void **v = (void **)_al_vector_ref(&s->displays, 0);
44 ALLEGRO_DISPLAY_SDL *d = *v;
45 return &d->display;
46 }
47 }
48 return NULL;
49 }
50
51 void _al_sdl_mouse_event(SDL_Event *e)
52 {
53 if (!mouse)
54 return;
55
56 ALLEGRO_EVENT_SOURCE *es = &mouse->mouse.es;
57 _al_event_source_lock(es);
58 ALLEGRO_EVENT event;
59 memset(&event, 0, sizeof event);
60
61 event.mouse.timestamp = al_get_time();
62
63 ALLEGRO_DISPLAY *d = NULL;
64
65 if (e->type == SDL_WINDOWEVENT) {
66 d = find_display(e->window.windowID);
67 mouse->ratio = _al_sdl_get_display_pixel_ratio(d);
68 if (e->window.event == SDL_WINDOWEVENT_ENTER) {
69 event.mouse.type = ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY;
70 SDL_GetMouseState(&event.mouse.x, &event.mouse.y);
71 event.mouse.x *= mouse->ratio;
72 event.mouse.y *= mouse->ratio;
73 }
74 else {
75 event.mouse.type = ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY;
76 event.mouse.x = mouse->state.x;
77 event.mouse.y = mouse->state.y;
78 event.mouse.z = mouse->state.z;
79 event.mouse.w = mouse->state.w;
80 }
81 mouse->display = e->window.event == SDL_WINDOWEVENT_ENTER ? d : NULL;
82 }
83 else if (e->type == SDL_MOUSEMOTION) {
84 if (e->motion.which == SDL_TOUCH_MOUSEID) {
85 _al_event_source_unlock(es);
86 return;
87 }
88 d = find_display(e->motion.windowID);
89 float ratio = d ? _al_sdl_get_display_pixel_ratio(d) : 1.0;
90 event.mouse.type = ALLEGRO_EVENT_MOUSE_AXES;
91 event.mouse.x = e->motion.x * ratio;
92 event.mouse.y = e->motion.y * ratio;
93 event.mouse.z = mouse->state.z;
94 event.mouse.w = mouse->state.w;
95 event.mouse.dx = e->motion.xrel * ratio;
96 event.mouse.dy = e->motion.yrel * ratio;
97 event.mouse.dz = 0;
98 event.mouse.dw = 0;
99 mouse->state.x = e->motion.x * ratio;
100 mouse->state.y = e->motion.y * ratio;
101 }
102 else if (e->type == SDL_MOUSEWHEEL) {
103 if (e->wheel.which == SDL_TOUCH_MOUSEID) {
104 _al_event_source_unlock(es);
105 return;
106 }
107 d = find_display(e->wheel.windowID);
108 event.mouse.type = ALLEGRO_EVENT_MOUSE_AXES;
109 mouse->state.z += al_get_mouse_wheel_precision() * e->wheel.y;
110 mouse->state.w += al_get_mouse_wheel_precision() * e->wheel.x;
111 event.mouse.x = mouse->state.x;
112 event.mouse.y = mouse->state.y;
113 event.mouse.z = mouse->state.z;
114 event.mouse.w = mouse->state.w;
115 event.mouse.dx = 0;
116 event.mouse.dy = 0;
117 event.mouse.dz = al_get_mouse_wheel_precision() * e->wheel.y;
118 event.mouse.dw = al_get_mouse_wheel_precision() * e->wheel.x;
119 }
120 else {
121 if (e->button.which == SDL_TOUCH_MOUSEID) {
122 _al_event_source_unlock(es);
123 return;
124 }
125 d = find_display(e->button.windowID);
126 float ratio = d ? _al_sdl_get_display_pixel_ratio(d) : 1.0;
127 switch (e->button.button) {
128 case SDL_BUTTON_LEFT: event.mouse.button = 1; break;
129 case SDL_BUTTON_RIGHT: event.mouse.button = 2; break;
130 case SDL_BUTTON_MIDDLE: event.mouse.button = 3; break;
131 case SDL_BUTTON_X1: event.mouse.button = 4; break;
132 case SDL_BUTTON_X2: event.mouse.button = 5; break;
133 }
134 event.mouse.x = e->button.x * ratio;
135 event.mouse.y = e->button.y * ratio;
136 event.mouse.z = mouse->state.z;
137 event.mouse.w = mouse->state.w;
138 if (e->type == SDL_MOUSEBUTTONDOWN) {
139 event.mouse.type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
140 mouse->state.buttons |= 1 << (event.mouse.button - 1);
141 }
142 if (e->type == SDL_MOUSEBUTTONUP) {
143 event.mouse.type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
144 mouse->state.buttons &= ~(1 << (event.mouse.button - 1));
145 }
146 }
147
148 event.mouse.pressure = mouse->state.buttons ? 1.0 : 0.0; /* TODO */
149 event.mouse.display = d;
150
151 _al_event_source_emit_event(es, &event);
152 _al_event_source_unlock(es);
153 }
154
155 static bool sdl_init_mouse(void)
156 {
157 mouse = al_calloc(1, sizeof *mouse);
158 _al_event_source_init(&mouse->mouse.es);
159 mouse->ratio = 1.0f;
160 return true;
161 }
162
163 static void sdl_exit_mouse(void)
164 {
165 }
166
167 static ALLEGRO_MOUSE *sdl_get_mouse(void)
168 {
169 return &mouse->mouse;
170 }
171
172 static unsigned int sdl_get_mouse_num_buttons(void)
173 {
174 return 5;
175 }
176
177 static unsigned int sdl_get_mouse_num_axes(void)
178 {
179 return 4;
180 }
181
182 static bool sdl_set_mouse_xy(ALLEGRO_DISPLAY *display, int x, int y)
183 {
184 ALLEGRO_DISPLAY_SDL *sdl = (void *)display;
185 float ratio = _al_sdl_get_display_pixel_ratio(display);
186 SDL_WarpMouseInWindow(sdl->window, x / ratio, y / ratio);
187 return true;
188 }
189
190 static bool sdl_set_mouse_axis(int which, int value)
191 {
192 if (which == 2) mouse->state.z = value;
193 else if (which == 3) mouse->state.w = value;
194 return false;
195 }
196
197 static void sdl_get_mouse_state(ALLEGRO_MOUSE_STATE *ret_state)
198 {
199 int x, y, i;
200 ALLEGRO_SYSTEM_INTERFACE *sdl = _al_sdl_system_driver();
201 sdl->heartbeat();
202 SDL_GetMouseState(&x, &y);
203 ret_state->x = x * mouse->ratio;
204 ret_state->y = y * mouse->ratio;
205 ret_state->z = 0;
206 ret_state->w = 0;
207 for (i = 0; i < ALLEGRO_MOUSE_MAX_EXTRA_AXES; i++)
208 ret_state->more_axes[i] = 0;
209 ret_state->buttons = mouse->state.buttons;
210 ret_state->pressure = mouse->state.buttons ? 1.0 : 0.0; /* TODO */
211 ret_state->display = mouse->display;
212 }
213
214 ALLEGRO_MOUSE_DRIVER *_al_sdl_mouse_driver(void)
215 {
216 if (vt)
217 return vt;
218
219 vt = al_calloc(1, sizeof *vt);
220 vt->msedrv_id = AL_ID('S','D','L','2');
221 vt->msedrv_name = "SDL2 Mouse";
222 vt->msedrv_desc = "SDL2 Mouse";
223 vt->msedrv_ascii_name = "SDL2 Mouse";
224 vt->init_mouse = sdl_init_mouse;
225 vt->exit_mouse = sdl_exit_mouse;
226 vt->get_mouse = sdl_get_mouse;
227 vt->get_mouse_num_buttons = sdl_get_mouse_num_buttons;
228 vt->get_mouse_num_axes = sdl_get_mouse_num_axes;
229 vt->set_mouse_xy = sdl_set_mouse_xy;
230 vt->set_mouse_axis = sdl_set_mouse_axis;
231 vt->get_mouse_state = sdl_get_mouse_state;
232
233 return vt;
234 }