Debian Codegraph @

src/macosx/qzmouse.m

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * MacOS X mouse driver.
12 *
13 * By Angelo Mottola.
14 *
15 * Modified for 4.9 mouse API by Peter Hull.
16 *
17 * See readme.txt for copyright information.
18 */
19
20
21 #include "allegro5/allegro.h"
22 #include "allegro5/allegro_opengl.h"
23 #include "allegro5/internal/aintern.h"
24 #include "allegro5/internal/aintern_driver.h"
25 #include "allegro5/internal/aintern_mouse.h"
26 #include "allegro5/internal/aintern_keyboard.h"
27 #include "allegro5/platform/aintosx.h"
28 #include "./osxgl.h"
29 #ifndef ALLEGRO_MACOSX
30 #error Something is wrong with the makefile
31 #endif
32
33 ALLEGRO_DEBUG_CHANNEL("MacOSX");
34
35 typedef struct ALLEGRO_MOUSE AL_MOUSE;
36 typedef struct ALLEGRO_MOUSE_STATE AL_MOUSE_STATE;
37
38 static bool osx_init_mouse(void);
39 static unsigned int osx_get_mouse_num_buttons(void);
40 static unsigned int osx_get_mouse_num_axes(void);
41 static bool osx_set_mouse_axis(int axis, int value);
42 static ALLEGRO_MOUSE* osx_get_mouse(void);
43
44 /* Mouse info - includes extra info for OS X */
45 static struct {
46 ALLEGRO_MOUSE parent;
47 unsigned int button_count;
48 unsigned int axis_count;
49 int minx, miny, maxx, maxy;
50 ALLEGRO_MOUSE_STATE state;
51 float z_axis, w_axis;
52 BOOL warped;
53 int warped_x, warped_y;
54 NSCursor* cursor;
55 } osx_mouse;
56
57 void _al_osx_clear_mouse_state(void)
58 {
59 memset(&osx_mouse.state, 0, sizeof(ALLEGRO_MOUSE_STATE));
60 }
61
62 /* _al_osx_switch_keyboard_focus:
63 * Handle a focus switch event.
64 */
65 void _al_osx_switch_mouse_focus(ALLEGRO_DISPLAY *dpy, bool switch_in)
66 {
67 _al_event_source_lock(&osx_mouse.parent.es);
68
69 if (switch_in)
70 osx_mouse.state.display = dpy;
71 else
72 osx_mouse.state.display = NULL;
73
74 _al_event_source_unlock(&osx_mouse.parent.es);
75 }
76
77 /* osx_get_mouse:
78 * Return the Allegro mouse structure
79 */
80 static ALLEGRO_MOUSE* osx_get_mouse(void)
81 {
82 return (ALLEGRO_MOUSE*) &osx_mouse.parent;
83 }
84
85 /* _al_osx_mouse_generate_event:
86 * Convert an OS X mouse event to an Allegro event
87 * and push it into a queue.
88 * First check that the event is wanted.
89 */
90 void _al_osx_mouse_generate_event(NSEvent* evt, ALLEGRO_DISPLAY* dpy)
91 {
92 NSPoint pos;
93 int type, b_change = 0, b = 0;
94 float pressure = 0.0;
95 float dx = 0, dy = 0, dz = 0, dw = 0;
96 switch ([evt type])
97 {
98 case NSMouseMoved:
99 type = ALLEGRO_EVENT_MOUSE_AXES;
100 dx = [evt deltaX];
101 dy = [evt deltaY];
102 pressure = [evt pressure];
103 break;
104 case NSLeftMouseDragged:
105 case NSRightMouseDragged:
106 case NSOtherMouseDragged:
107 type = ALLEGRO_EVENT_MOUSE_AXES;
108 b = [evt buttonNumber]+1;
109 dx = [evt deltaX];
110 dy = [evt deltaY];
111 pressure = [evt pressure];
112 break;
113 case NSLeftMouseDown:
114 case NSRightMouseDown:
115 case NSOtherMouseDown:
116 type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN;
117 b = [evt buttonNumber]+1;
118 b_change = 1;
119 osx_mouse.state.buttons |= (1 << (b-1));
120 pressure = [evt pressure];
121 break;
122 case NSLeftMouseUp:
123 case NSRightMouseUp:
124 case NSOtherMouseUp:
125 type = ALLEGRO_EVENT_MOUSE_BUTTON_UP;
126 b = [evt buttonNumber]+1;
127 b_change = 1;
128 osx_mouse.state.buttons &= ~(1 << (b-1));
129 pressure = [evt pressure];
130 break;
131 case NSScrollWheel:
132 type = ALLEGRO_EVENT_MOUSE_AXES;
133 dx = 0;
134 dy = 0;
135 osx_mouse.w_axis += al_get_mouse_wheel_precision() * [evt deltaX];
136 osx_mouse.z_axis += al_get_mouse_wheel_precision() * [evt deltaY];
137 dw = osx_mouse.w_axis - osx_mouse.state.w;
138 dz = osx_mouse.z_axis - osx_mouse.state.z;
139 break;
140 case NSMouseEntered:
141 type = ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY;
142 b = [evt buttonNumber]+1;
143 dx = [evt deltaX];
144 dy = [evt deltaY];
145 break;
146 case NSMouseExited:
147 type = ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY;
148 b = [evt buttonNumber]+1;
149 dx = [evt deltaX];
150 dy = [evt deltaY];
151 break;
152 default:
153 return;
154 }
155 pos = [evt locationInWindow];
156 BOOL within = true;
157 float scaling_factor = 1.0;
158 if ([evt window])
159 {
160 NSRect frm = [[[evt window] contentView] frame];
161 within = NSMouseInRect(pos, frm, NO);
162 // Y-coordinates in OS X start from the bottom.
163 pos.y = NSHeight(frm) - pos.y;
164 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
165 scaling_factor = [[evt window] backingScaleFactor];
166 #endif
167 }
168 else
169 {
170 pos.y = [[NSScreen mainScreen] frame].size.height - pos.y;
171 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
172 scaling_factor = [[NSScreen mainScreen] backingScaleFactor];
173 #endif
174 }
175 dx *= scaling_factor;
176 dy *= scaling_factor;
177 if (osx_mouse.warped && type == ALLEGRO_EVENT_MOUSE_AXES) {
178 dx -= osx_mouse.warped_x;
179 dy -= osx_mouse.warped_y;
180 osx_mouse.warped = FALSE;
181 }
182 _al_event_source_lock(&osx_mouse.parent.es);
183 if ((within || b_change || type == ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY)
184 && _al_event_source_needs_to_generate_event(&osx_mouse.parent.es))
185 {
186 ALLEGRO_EVENT new_event;
187 ALLEGRO_MOUSE_EVENT* mouse_event = &new_event.mouse;
188 mouse_event->type = type;
189 // Note: we use 'allegro time' rather than the time stamp
190 // from the event
191 mouse_event->timestamp = al_get_time();
192 mouse_event->display = dpy;
193 mouse_event->button = b;
194 mouse_event->x = pos.x * scaling_factor;
195 mouse_event->y = pos.y * scaling_factor;
196 mouse_event->z = osx_mouse.z_axis;
197 mouse_event->w = osx_mouse.w_axis;
198 mouse_event->dx = dx;
199 mouse_event->dy = dy;
200 mouse_event->dz = dz;
201 mouse_event->dw = dw;
202 mouse_event->pressure = pressure;
203 _al_event_source_emit_event(&osx_mouse.parent.es, &new_event);
204 }
205 // Record current state
206 osx_mouse.state.x = pos.x * scaling_factor;
207 osx_mouse.state.y = pos.y * scaling_factor;
208 osx_mouse.state.w = osx_mouse.w_axis;
209 osx_mouse.state.z = osx_mouse.z_axis;
210 osx_mouse.state.pressure = pressure;
211 _al_event_source_unlock(&osx_mouse.parent.es);
212 }
213
214 /* osx_init_mouse:
215 * Initializes the driver.
216 */
217 static bool osx_init_mouse(void)
218 {
219 /* NOTE: This function is deprecated, however until we have a better fix
220 * or it is removed, we can used it. The problem without calling this
221 * function is that after synthesizing events (as with al_set_mouse_xy)
222 * there is an unacceptably long delay in receiving new events (mouse
223 * locks up for about 0.5 seconds.)
224 */
225 CGSetLocalEventsSuppressionInterval(0.0);
226
227 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
228 HID_DEVICE_COLLECTION devices={0,0,NULL};
229 int i = 0, j = 0;
230 int axes = 0, buttons = 0;
231 int max_axes = 0, max_buttons = 0;
232 HID_DEVICE* device = nil;
233 NSString* desc = nil;
234
235 _al_osx_hid_scan(HID_MOUSE, &devices);
236 ALLEGRO_INFO("Detected %d pointing devices\n", devices.count);
237 for (i=0; i<devices.count; i++) {
238 device=&devices.devices[i];
239 buttons = 0;
240 axes = 0;
241
242 desc = [NSString stringWithFormat: @"%s %s",
243 device->manufacturer ? device->manufacturer : "",
244 device->product ? device->product : ""];
245 ALLEGRO_INFO("Device %d is a \"%s\"\n", i, [desc UTF8String]);
246
247 for (j = 0; j < device->num_elements; j++) {
248 switch (device->element[j].type) {
249 case HID_ELEMENT_BUTTON:
250 buttons++;
251 break;
252 case HID_ELEMENT_AXIS:
253 case HID_ELEMENT_AXIS_PRIMARY_X:
254 case HID_ELEMENT_AXIS_PRIMARY_Y:
255 case HID_ELEMENT_STANDALONE_AXIS:
256 axes ++;
257 break;
258 }
259 }
260 ALLEGRO_INFO("Detected %d axes and %d buttons\n", axes, buttons);
261
262 /* When mouse events reach the application, it is not clear which
263 * device generated them, so effectively the largest number of
264 * buttons and axes reported corresponds to the device that the
265 * application "sees"
266 */
267 if (axes > max_axes) max_axes = axes;
268 if (buttons > max_buttons) max_buttons = buttons;
269 }
270 _al_osx_hid_free(&devices);
271 ALLEGRO_INFO("Device effectively has %d axes and %d buttons\n", axes, buttons);
272
273 if (max_buttons <= 0) return false;
274 _al_event_source_init(&osx_mouse.parent.es);
275 osx_mouse.button_count = max_buttons;
276 osx_mouse.axis_count = max_axes;
277 osx_mouse.warped = FALSE;
278 memset(&osx_mouse.state, 0, sizeof(ALLEGRO_MOUSE_STATE));
279 _al_osx_mouse_was_installed(YES);
280 [pool drain];
281 return true;
282 }
283
284 /* osx_exit_mouse:
285 * Shut down the mouse driver
286 */
287 static void osx_exit_mouse(void)
288 {
289 _al_osx_mouse_was_installed(NO);
290 }
291
292 /* osx_get_mouse_num_buttons:
293 * Return the number of buttons on the mouse
294 */
295 static unsigned int osx_get_mouse_num_buttons(void)
296 {
297 return osx_mouse.button_count;
298 }
299
300 /* osx_get_mouse_num_buttons:
301 * Return the number of buttons on the mouse
302 */
303 static unsigned int osx_get_mouse_num_axes(void)
304 {
305 return osx_mouse.axis_count;
306 }
307
308
309 static void osx_get_mouse_state(ALLEGRO_MOUSE_STATE *ret_state)
310 {
311 _al_event_source_lock(&osx_mouse.parent.es);
312 memcpy(ret_state, &osx_mouse.state, sizeof(ALLEGRO_MOUSE_STATE));
313 _al_event_source_unlock(&osx_mouse.parent.es);
314 }
315
316 static NSScreen *screen_from_display_id(CGDirectDisplayID display) {
317 for (NSScreen *screen in [NSScreen screens]) {
318 NSDictionary *info = [screen deviceDescription];
319 NSNumber *display_id = [info objectForKey:@"NSScreenNumber"];
320 if ([display_id unsignedIntValue] == display)
321 return screen;
322 }
323 return nil;
324 }
325
326 /* osx_set_mouse_xy:
327 * Set the current mouse position
328 */
329 static bool osx_set_mouse_xy(ALLEGRO_DISPLAY *dpy_, int x, int y)
330 {
331 CGPoint pos;
332 CGDirectDisplayID display = 0;
333 ALLEGRO_DISPLAY_OSX_WIN *dpy = (ALLEGRO_DISPLAY_OSX_WIN *)dpy_;
334
335 if ((dpy) && !(dpy->parent.flags & ALLEGRO_FULLSCREEN) &&
336 !(dpy->parent.flags & ALLEGRO_FULLSCREEN_WINDOW) && (dpy->win)) {
337 NSWindow *window = dpy->win;
338 NSRect content = [window contentRectForFrameRect: [window frame]];
339 NSRect frame = [[window screen] frame];
340 CGRect rect = { { NSMinX(frame), NSMinY(frame) }, { NSWidth(frame), NSHeight(frame) } };
341 CGDirectDisplayID displays[16];
342 CGDisplayCount displayCount;
343
344 if ((CGGetDisplaysWithRect(rect, 16, displays, &displayCount) == 0) && (displayCount >= 1))
345 display = displays[0];
346
347 CGPoint point_pos = CGPointMake(x, y);
348 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
349 point_pos = NSPointToCGPoint([[window contentView] convertPointFromBacking: NSPointFromCGPoint(point_pos)]);
350 #endif
351 pos.x = content.origin.x + point_pos.x;
352 pos.y = rect.size.height - content.origin.y - content.size.height + point_pos.y;
353 }
354 else {
355 float scaling_factor = 1.0;
356 NSScreen *screen = nil;
357 if (dpy) {
358 display = dpy->display_id;
359 screen = screen_from_display_id(display);
360 }
361 if (!screen)
362 screen = [NSScreen mainScreen];
363 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
364 scaling_factor = [screen backingScaleFactor];
365 #endif
366 pos.x = x / scaling_factor;
367 pos.y = y / scaling_factor;
368 }
369
370 _al_event_source_lock(&osx_mouse.parent.es);
371 if (_al_event_source_needs_to_generate_event(&osx_mouse.parent.es)) {
372 ALLEGRO_EVENT new_event;
373 ALLEGRO_MOUSE_EVENT* mouse_event = &new_event.mouse;
374 mouse_event->type = ALLEGRO_EVENT_MOUSE_WARPED;
375 // Note: we use 'allegro time' rather than the time stamp
376 // from the event
377 mouse_event->timestamp = al_get_time();
378 mouse_event->display = (ALLEGRO_DISPLAY *)dpy;
379 mouse_event->button = 0;
380 mouse_event->x = x;
381 mouse_event->y = y;
382 mouse_event->z = osx_mouse.z_axis;
383 mouse_event->dx = x - osx_mouse.state.x;
384 mouse_event->dy = y - osx_mouse.state.y;
385 mouse_event->dz = 0;
386 mouse_event->pressure = osx_mouse.state.pressure;
387 if (mouse_event->dx || mouse_event->dy) {
388 osx_mouse.warped = TRUE;
389 osx_mouse.warped_x = mouse_event->dx;
390 osx_mouse.warped_y = mouse_event->dy;
391 _al_event_source_emit_event(&osx_mouse.parent.es, &new_event);
392 }
393 }
394 CGDisplayMoveCursorToPoint(display, pos);
395 osx_mouse.state.x = x;
396 osx_mouse.state.y = y;
397 _al_event_source_unlock(&osx_mouse.parent.es);
398 return true;
399 }
400
401 /* osx_set_mouse_axis:
402 * Set the axis value of the mouse
403 */
404 static bool osx_set_mouse_axis(int axis, int value)
405 {
406 bool result = false;
407 _al_event_source_lock(&osx_mouse.parent.es);
408 switch (axis)
409 {
410 case 0:
411 case 1:
412 // By design, this doesn't apply to (x, y)
413 break;
414 case 2:
415 osx_mouse.z_axis = value;
416 result = true;
417 break;
418 case 3:
419 osx_mouse.w_axis = value;
420 result = true;
421 break;
422 }
423 /* XXX generate an event if the axis value changed */
424 _al_event_source_unlock(&osx_mouse.parent.es);
425 return result;
426 }
427 /* Mouse driver */
428 static ALLEGRO_MOUSE_DRIVER osx_mouse_driver =
429 {
430 0, //int msedrv_id;
431 "OSXMouse", //const char *msedrv_name;
432 "Driver for Mac OS X",// const char *msedrv_desc;
433 "OSX Mouse", //const char *msedrv_ascii_name;
434 osx_init_mouse, //AL_METHOD(bool, init_mouse, (void));
435 osx_exit_mouse, //AL_METHOD(void, exit_mouse, (void));
436 osx_get_mouse, //AL_METHOD(ALLEGRO_MOUSE*, get_mouse, (void));
437 osx_get_mouse_num_buttons, //AL_METHOD(unsigned int, get_mouse_num_buttons, (void));
438 osx_get_mouse_num_axes, //AL_METHOD(unsigned int, get_mouse_num_axes, (void));
439 osx_set_mouse_xy, //AL_METHOD(bool, set_mouse_xy, (int x, int y));
440 osx_set_mouse_axis, //AL_METHOD(bool, set_mouse_axis, (int which, int value));
441 osx_get_mouse_state, //AL_METHOD(void, get_mouse_state, (ALLEGRO_MOUSE_STATE *ret_state));
442 };
443 ALLEGRO_MOUSE_DRIVER* _al_osx_get_mouse_driver(void)
444 {
445 return &osx_mouse_driver;
446 }
447
448 /* list the available drivers */
449 _AL_DRIVER_INFO _al_mouse_driver_list[] =
450 {
451 { 1, &osx_mouse_driver, 1 },
452 { 0, NULL, 0 }
453 };
454
455 /* Local variables: */
456 /* c-basic-offset: 3 */
457 /* indent-tabs-mode: nil */
458 /* End: */