1 /*
2 * Darwin event queue and event handling
3 *
4 * Copyright 2007-2008 Apple Inc.
5 * Copyright 2004 Kaleb S. KEITHLEY. All Rights Reserved.
6 * Copyright (c) 2002-2004 Torrey T. Lyons. All Rights Reserved.
7 *
8 * This file is based on mieq.c by Keith Packard,
9 * which contains the following copyright:
10 * Copyright 1990, 1998 The Open Group
11 *
12 *
13 * Copyright (c) 2002-2012 Apple Inc. All rights reserved.
14 *
15 * Permission is hereby granted, free of charge, to any person
16 * obtaining a copy of this software and associated documentation files
17 * (the "Software"), to deal in the Software without restriction,
18 * including without limitation the rights to use, copy, modify, merge,
19 * publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so,
21 * subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
30 * HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33 * DEALINGS IN THE SOFTWARE.
34 *
35 * Except as contained in this notice, the name(s) of the above
36 * copyright holders shall not be used in advertising or otherwise to
37 * promote the sale, use or other dealings in this Software without
38 * prior written authorization.
39 */
40
41 #include "sanitizedCarbon.h"
42
43 #ifdef HAVE_DIX_CONFIG_H
44 #include <dix-config.h>
45 #endif
46
47 #include <X11/X.h>
48 #include <X11/Xmd.h>
49 #include <X11/Xproto.h>
50 #include "misc.h"
51 #include "windowstr.h"
52 #include "pixmapstr.h"
53 #include "inputstr.h"
54 #include "inpututils.h"
55 #include "eventstr.h"
56 #include "mi.h"
57 #include "scrnintstr.h"
58 #include "mipointer.h"
59 #include "os.h"
60 #include "exglobals.h"
61
62 #include "darwin.h"
63 #include "quartz.h"
64 #include "quartzKeyboard.h"
65 #include "quartzRandR.h"
66 #include "darwinEvents.h"
67
68 #include <sys/types.h>
69 #include <sys/uio.h>
70 #include <unistd.h>
71 #include <pthread.h>
72 #include <errno.h>
73 #include <time.h>
74
75 #include <IOKit/hidsystem/IOLLEvent.h>
76
77 #include <X11/extensions/applewmconst.h>
78 #include "applewmExt.h"
79
80 /* FIXME: Abstract this better */
81 extern Bool
82 QuartzModeEventHandler(int screenNum, XQuartzEvent *e, DeviceIntPtr dev);
83
84 int darwin_all_modifier_flags = 0; // last known modifier state
85 int darwin_all_modifier_mask = 0;
86 int darwin_x11_modifier_mask = 0;
87
88 #define FD_ADD_MAX 128
89 static int fd_add[FD_ADD_MAX];
90 int fd_add_count = 0;
91 static pthread_mutex_t fd_add_lock = PTHREAD_MUTEX_INITIALIZER;
92 static pthread_cond_t fd_add_ready_cond = PTHREAD_COND_INITIALIZER;
93 static pthread_t fd_add_tid = NULL;
94
95 static BOOL mieqInitialized;
96 static pthread_mutex_t mieqInitializedMutex = PTHREAD_MUTEX_INITIALIZER;
97 static pthread_cond_t mieqInitializedCond = PTHREAD_COND_INITIALIZER;
98
99 _X_NOTSAN
100 extern inline void
wait_for_mieq_init(void)101 wait_for_mieq_init(void)
102 {
103 if (!mieqInitialized) {
104 pthread_mutex_lock(&mieqInitializedMutex);
105 while (!mieqInitialized) {
106 pthread_cond_wait(&mieqInitializedCond, &mieqInitializedMutex);
107 }
108 pthread_mutex_unlock(&mieqInitializedMutex);
109 }
110 }
111
112 _X_NOTSAN
113 static inline void
signal_mieq_init(void)114 signal_mieq_init(void)
115 {
116 pthread_mutex_lock(&mieqInitializedMutex);
117 mieqInitialized = TRUE;
118 pthread_cond_broadcast(&mieqInitializedCond);
119 pthread_mutex_unlock(&mieqInitializedMutex);
120 }
121
122 /*** Pthread Magics ***/
123 static pthread_t
create_thread(void * (* func)(void *),void * arg)124 create_thread(void *(*func)(void *), void *arg)
125 {
126 pthread_attr_t attr;
127 pthread_t tid;
128
129 pthread_attr_init(&attr);
130 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
131 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
132 pthread_create(&tid, &attr, func, arg);
133 pthread_attr_destroy(&attr);
134
135 return tid;
136 }
137
138 /*
139 * DarwinPressModifierKey
140 * Press or release the given modifier key (one of NX_MODIFIERKEY_* constants)
141 */
142 static void
DarwinPressModifierKey(int pressed,int key)143 DarwinPressModifierKey(int pressed, int key)
144 {
145 int keycode = DarwinModifierNXKeyToNXKeycode(key, 0);
146
147 if (keycode == 0) {
148 ErrorF("DarwinPressModifierKey bad keycode: key=%d\n", key);
149 return;
150 }
151
152 DarwinSendKeyboardEvents(pressed, keycode);
153 }
154
155 /*
156 * DarwinUpdateModifiers
157 * Send events to update the modifier state.
158 */
159
160 static int darwin_x11_modifier_mask_list[] = {
161 #ifdef NX_DEVICELCMDKEYMASK
162 NX_DEVICELCTLKEYMASK, NX_DEVICERCTLKEYMASK,
163 NX_DEVICELSHIFTKEYMASK, NX_DEVICERSHIFTKEYMASK,
164 NX_DEVICELCMDKEYMASK, NX_DEVICERCMDKEYMASK,
165 NX_DEVICELALTKEYMASK, NX_DEVICERALTKEYMASK,
166 #else
167 NX_CONTROLMASK, NX_SHIFTMASK, NX_COMMANDMASK,
168 NX_ALTERNATEMASK,
169 #endif
170 NX_ALPHASHIFTMASK,
171 0
172 };
173
174 static int darwin_all_modifier_mask_additions[] = { NX_SECONDARYFNMASK, 0 };
175
176 static void
DarwinUpdateModifiers(int pressed,int flags)177 DarwinUpdateModifiers(int pressed, // KeyPress or KeyRelease
178 int flags) // modifier flags that have changed
179 {
180 int *f;
181 int key;
182
183 /* Capslock is special. This mask is the state of capslock (on/off),
184 * not the state of the button. Hopefully we can find a better solution.
185 */
186 if (NX_ALPHASHIFTMASK & flags) {
187 DarwinPressModifierKey(KeyPress, NX_MODIFIERKEY_ALPHALOCK);
188 DarwinPressModifierKey(KeyRelease, NX_MODIFIERKEY_ALPHALOCK);
189 }
190
191 for (f = darwin_x11_modifier_mask_list; *f; f++)
192 if (*f & flags && *f != NX_ALPHASHIFTMASK) {
193 key = DarwinModifierNXMaskToNXKey(*f);
194 if (key == -1)
195 ErrorF("DarwinUpdateModifiers: Unsupported NXMask: 0x%x\n",
196 *f);
197 else
198 DarwinPressModifierKey(pressed, key);
199 }
200 }
201
202 /* Generic handler for Xquartz-specifc events. When possible, these should
203 be moved into their own individual functions and set as handlers using
204 mieqSetHandler. */
205
206 static void
DarwinEventHandler(int screenNum,InternalEvent * ie,DeviceIntPtr dev)207 DarwinEventHandler(int screenNum, InternalEvent *ie, DeviceIntPtr dev)
208 {
209 XQuartzEvent *e = &(ie->xquartz_event);
210
211 switch (e->subtype) {
212 case kXquartzControllerNotify:
213 DEBUG_LOG("kXquartzControllerNotify\n");
214 AppleWMSendEvent(AppleWMControllerNotify,
215 AppleWMControllerNotifyMask,
216 e->data[0],
217 e->data[1]);
218 break;
219
220 case kXquartzPasteboardNotify:
221 DEBUG_LOG("kXquartzPasteboardNotify\n");
222 AppleWMSendEvent(AppleWMPasteboardNotify,
223 AppleWMPasteboardNotifyMask,
224 e->data[0],
225 e->data[1]);
226 break;
227
228 case kXquartzActivate:
229 DEBUG_LOG("kXquartzActivate\n");
230 QuartzShow();
231 AppleWMSendEvent(AppleWMActivationNotify,
232 AppleWMActivationNotifyMask,
233 AppleWMIsActive, 0);
234 break;
235
236 case kXquartzDeactivate:
237 DEBUG_LOG("kXquartzDeactivate\n");
238 AppleWMSendEvent(AppleWMActivationNotify,
239 AppleWMActivationNotifyMask,
240 AppleWMIsInactive, 0);
241 QuartzHide();
242 break;
243
244 case kXquartzReloadPreferences:
245 DEBUG_LOG("kXquartzReloadPreferences\n");
246 AppleWMSendEvent(AppleWMActivationNotify,
247 AppleWMActivationNotifyMask,
248 AppleWMReloadPreferences, 0);
249 break;
250
251 case kXquartzToggleFullscreen:
252 DEBUG_LOG("kXquartzToggleFullscreen\n");
253 if (XQuartzIsRootless)
254 ErrorF(
255 "Ignoring kXquartzToggleFullscreen because of rootless mode.");
256 else
257 QuartzRandRToggleFullscreen();
258 break;
259
260 case kXquartzSetRootless:
261 DEBUG_LOG("kXquartzSetRootless\n");
262 if (e->data[0]) {
263 QuartzRandRSetFakeRootless();
264 }
265 else {
266 QuartzRandRSetFakeFullscreen(FALSE);
267 }
268 break;
269
270 case kXquartzSetRootClip:
271 QuartzSetRootClip(e->data[0]);
272 break;
273
274 case kXquartzQuit:
275 GiveUp(0);
276 break;
277
278 case kXquartzSpaceChanged:
279 DEBUG_LOG("kXquartzSpaceChanged\n");
280 QuartzSpaceChanged(e->data[0]);
281 break;
282
283 case kXquartzListenOnOpenFD:
284 ErrorF("Calling ListenOnOpenFD() for new fd: %d\n", (int)e->data[0]);
285 ListenOnOpenFD((int)e->data[0], 1);
286 break;
287
288 case kXquartzReloadKeymap:
289 DarwinKeyboardReloadHandler();
290 break;
291
292 case kXquartzDisplayChanged:
293 DEBUG_LOG("kXquartzDisplayChanged\n");
294 QuartzUpdateScreens();
295
296 /* Update our RandR info */
297 QuartzRandRUpdateFakeModes(TRUE);
298 break;
299
300 default:
301 if (!QuartzModeEventHandler(screenNum, e, dev))
302 ErrorF("Unknown application defined event type %d.\n", e->subtype);
303 }
304 }
305
306 void
DarwinListenOnOpenFD(int fd)307 DarwinListenOnOpenFD(int fd)
308 {
309 ErrorF("DarwinListenOnOpenFD: %d\n", fd);
310
311 pthread_mutex_lock(&fd_add_lock);
312 if (fd_add_count < FD_ADD_MAX)
313 fd_add[fd_add_count++] = fd;
314 else
315 ErrorF("FD Addition buffer at max. Dropping fd addition request.\n");
316
317 pthread_cond_broadcast(&fd_add_ready_cond);
318 pthread_mutex_unlock(&fd_add_lock);
319 }
320
321 static void *
DarwinProcessFDAdditionQueue_thread(void * args)322 DarwinProcessFDAdditionQueue_thread(void *args)
323 {
324 /* TODO: Possibly adjust this to no longer be a race... maybe trigger this
325 * once a client connects and claims to be the WM.
326 *
327 * From ajax:
328 * There's already an internal callback chain for setting selection [in 1.5]
329 * ownership. See the CallSelectionCallback at the bottom of
330 * ProcSetSelectionOwner, and xfixes/select.c for an example of how to hook
331 * into it.
332 */
333
334 struct timespec sleep_for;
335 struct timespec sleep_remaining;
336
337 sleep_for.tv_sec = 3;
338 sleep_for.tv_nsec = 0;
339
340 ErrorF(
341 "X11.app: DarwinProcessFDAdditionQueue_thread: Sleeping to allow xinitrc to catchup.\n");
342 while (nanosleep(&sleep_for, &sleep_remaining) != 0) {
343 sleep_for = sleep_remaining;
344 }
345
346 pthread_mutex_lock(&fd_add_lock);
347 while (true) {
348 while (fd_add_count) {
349 DarwinSendDDXEvent(kXquartzListenOnOpenFD, 1,
350 fd_add[--fd_add_count]);
351 }
352 pthread_cond_wait(&fd_add_ready_cond, &fd_add_lock);
353 }
354
355 return NULL;
356 }
357
358 Bool
DarwinEQInit(void)359 DarwinEQInit(void)
360 {
361 int *p;
362
363 for (p = darwin_x11_modifier_mask_list; *p; p++) {
364 darwin_x11_modifier_mask |= *p;
365 }
366
367 darwin_all_modifier_mask = darwin_x11_modifier_mask;
368 for (p = darwin_all_modifier_mask_additions; *p; p++) {
369 darwin_all_modifier_mask |= *p;
370 }
371
372 mieqInit();
373 mieqSetHandler(ET_XQuartz, DarwinEventHandler);
374
375 if (!fd_add_tid)
376 fd_add_tid = create_thread(DarwinProcessFDAdditionQueue_thread, NULL);
377
378 signal_mieq_init();
379
380 return TRUE;
381 }
382
383 void
DarwinEQFini(void)384 DarwinEQFini(void)
385 {
386 mieqFini();
387 }
388
389 /*
390 * ProcessInputEvents
391 * Read and process events from the event queue until it is empty.
392 */
393 void
ProcessInputEvents(void)394 ProcessInputEvents(void)
395 {
396 char nullbyte;
397 int x = sizeof(nullbyte);
398
399 mieqProcessInputEvents();
400
401 // Empty the signaling pipe
402 while (x == sizeof(nullbyte)) {
403 x = read(darwinEventReadFD, &nullbyte, sizeof(nullbyte));
404 }
405 }
406
407 /* Sends a null byte down darwinEventWriteFD, which will cause the
408 Dispatch() event loop to check out event queue */
409 static void
DarwinPokeEQ(void)410 DarwinPokeEQ(void)
411 {
412 char nullbyte = 0;
413 // <daniels> oh, i ... er ... christ.
414 write(darwinEventWriteFD, &nullbyte, sizeof(nullbyte));
415 }
416
417 void
DarwinInputReleaseButtonsAndKeys(DeviceIntPtr pDev)418 DarwinInputReleaseButtonsAndKeys(DeviceIntPtr pDev)
419 {
420 input_lock();
421 {
422 int i;
423 if (pDev->button) {
424 for (i = 0; i < pDev->button->numButtons; i++) {
425 if (BitIsOn(pDev->button->down, i)) {
426 QueuePointerEvents(pDev, ButtonRelease, i,
427 POINTER_ABSOLUTE,
428 NULL);
429 }
430 }
431 }
432
433 if (pDev->key) {
434 for (i = 0; i < NUM_KEYCODES; i++) {
435 if (BitIsOn(pDev->key->down, i + MIN_KEYCODE)) {
436 QueueKeyboardEvents(pDev, KeyRelease, i + MIN_KEYCODE);
437 }
438 }
439 }
440 DarwinPokeEQ();
441 } input_unlock();
442 }
443
444 void
DarwinSendTabletEvents(DeviceIntPtr pDev,int ev_type,int ev_button,double pointer_x,double pointer_y,double pressure,double tilt_x,double tilt_y)445 DarwinSendTabletEvents(DeviceIntPtr pDev, int ev_type, int ev_button,
446 double pointer_x, double pointer_y,
447 double pressure, double tilt_x,
448 double tilt_y)
449 {
450 ScreenPtr screen;
451 ValuatorMask valuators;
452
453 screen = miPointerGetScreen(pDev);
454 if (!screen) {
455 DEBUG_LOG("%s called before screen was initialized\n",
456 __FUNCTION__);
457 return;
458 }
459
460 /* Fix offset between darwin and X screens */
461 pointer_x -= darwinMainScreenX + screen->x;
462 pointer_y -= darwinMainScreenY + screen->y;
463
464 /* Adjust our pointer location to the [0,1] range */
465 pointer_x = pointer_x / (double)screenInfo.width;
466 pointer_y = pointer_y / (double)screenInfo.height;
467
468 valuator_mask_zero(&valuators);
469 valuator_mask_set_double(&valuators, 0, XQUARTZ_VALUATOR_LIMIT * pointer_x);
470 valuator_mask_set_double(&valuators, 1, XQUARTZ_VALUATOR_LIMIT * pointer_y);
471 valuator_mask_set_double(&valuators, 2, XQUARTZ_VALUATOR_LIMIT * pressure);
472 valuator_mask_set_double(&valuators, 3, XQUARTZ_VALUATOR_LIMIT * tilt_x);
473 valuator_mask_set_double(&valuators, 4, XQUARTZ_VALUATOR_LIMIT * tilt_y);
474
475 input_lock();
476 {
477 if (ev_type == ProximityIn || ev_type == ProximityOut) {
478 QueueProximityEvents(pDev, ev_type, &valuators);
479 } else {
480 QueuePointerEvents(pDev, ev_type, ev_button, POINTER_ABSOLUTE,
481 &valuators);
482 }
483 DarwinPokeEQ();
484 } input_unlock();
485 }
486
487 void
DarwinSendPointerEvents(DeviceIntPtr pDev,int ev_type,int ev_button,double pointer_x,double pointer_y,double pointer_dx,double pointer_dy)488 DarwinSendPointerEvents(DeviceIntPtr pDev, int ev_type, int ev_button,
489 double pointer_x, double pointer_y,
490 double pointer_dx, double pointer_dy)
491 {
492 static int darwinFakeMouseButtonDown = 0;
493 ScreenPtr screen;
494 ValuatorMask valuators;
495
496 screen = miPointerGetScreen(pDev);
497 if (!screen) {
498 DEBUG_LOG("%s called before screen was initialized\n",
499 __FUNCTION__);
500 return;
501 }
502
503 /* Handle fake click */
504 if (ev_type == ButtonPress && darwinFakeButtons && ev_button == 1) {
505 if (darwinFakeMouseButtonDown != 0) {
506 /* We're currently "down" with another button, so release it first */
507 DarwinSendPointerEvents(pDev, ButtonRelease,
508 darwinFakeMouseButtonDown,
509 pointer_x, pointer_y, 0.0, 0.0);
510 darwinFakeMouseButtonDown = 0;
511 }
512 if (darwin_all_modifier_flags & darwinFakeMouse2Mask) {
513 ev_button = 2;
514 darwinFakeMouseButtonDown = 2;
515 DarwinUpdateModKeys(
516 darwin_all_modifier_flags & ~darwinFakeMouse2Mask);
517 }
518 else if (darwin_all_modifier_flags & darwinFakeMouse3Mask) {
519 ev_button = 3;
520 darwinFakeMouseButtonDown = 3;
521 DarwinUpdateModKeys(
522 darwin_all_modifier_flags & ~darwinFakeMouse3Mask);
523 }
524 }
525
526 if (ev_type == ButtonRelease && ev_button == 1) {
527 if (darwinFakeMouseButtonDown) {
528 ev_button = darwinFakeMouseButtonDown;
529 }
530
531 if (darwinFakeMouseButtonDown == 2) {
532 DarwinUpdateModKeys(
533 darwin_all_modifier_flags & ~darwinFakeMouse2Mask);
534 }
535 else if (darwinFakeMouseButtonDown == 3) {
536 DarwinUpdateModKeys(
537 darwin_all_modifier_flags & ~darwinFakeMouse3Mask);
538 }
539
540 darwinFakeMouseButtonDown = 0;
541 }
542
543 /* Fix offset between darwin and X screens */
544 pointer_x -= darwinMainScreenX + screen->x;
545 pointer_y -= darwinMainScreenY + screen->y;
546
547 valuator_mask_zero(&valuators);
548 valuator_mask_set_double(&valuators, 0, pointer_x);
549 valuator_mask_set_double(&valuators, 1, pointer_y);
550
551 if (ev_type == MotionNotify) {
552 if (pointer_dx != 0.0)
553 valuator_mask_set_double(&valuators, 2, pointer_dx);
554 if (pointer_dy != 0.0)
555 valuator_mask_set_double(&valuators, 3, pointer_dy);
556 }
557
558 input_lock();
559 {
560 QueuePointerEvents(pDev, ev_type, ev_button, POINTER_ABSOLUTE,
561 &valuators);
562 DarwinPokeEQ();
563 } input_unlock();
564 }
565
566 void
DarwinSendKeyboardEvents(int ev_type,int keycode)567 DarwinSendKeyboardEvents(int ev_type, int keycode)
568 {
569 input_lock();
570 {
571 QueueKeyboardEvents(darwinKeyboard, ev_type, keycode + MIN_KEYCODE);
572 DarwinPokeEQ();
573 } input_unlock();
574 }
575
576 /* Send the appropriate number of button clicks to emulate scroll wheel */
577 void
DarwinSendScrollEvents(double scroll_x,double scroll_y)578 DarwinSendScrollEvents(double scroll_x, double scroll_y) {
579 ScreenPtr screen;
580 ValuatorMask valuators;
581
582 screen = miPointerGetScreen(darwinPointer);
583 if (!screen) {
584 DEBUG_LOG(
585 "DarwinSendScrollEvents called before screen was initialized\n");
586 return;
587 }
588
589 valuator_mask_zero(&valuators);
590 valuator_mask_set_double(&valuators, 4, scroll_y);
591 valuator_mask_set_double(&valuators, 5, scroll_x);
592
593 input_lock();
594 {
595 QueuePointerEvents(darwinPointer, MotionNotify, 0,
596 POINTER_RELATIVE, &valuators);
597 DarwinPokeEQ();
598 } input_unlock();
599 }
600
601 /* Send the appropriate KeyPress/KeyRelease events to GetKeyboardEvents to
602 reflect changing modifier flags (alt, control, meta, etc) */
603 void
DarwinUpdateModKeys(int flags)604 DarwinUpdateModKeys(int flags)
605 {
606 DarwinUpdateModifiers(
607 KeyRelease, darwin_all_modifier_flags & ~flags &
608 darwin_x11_modifier_mask);
609 DarwinUpdateModifiers(
610 KeyPress, ~darwin_all_modifier_flags & flags &
611 darwin_x11_modifier_mask);
612 darwin_all_modifier_flags = flags;
613 }
614
615 /*
616 * DarwinSendDDXEvent
617 * Send the X server thread a message by placing it on the event queue.
618 */
619 void
DarwinSendDDXEvent(int type,int argc,...)620 DarwinSendDDXEvent(int type, int argc, ...)
621 {
622 XQuartzEvent e;
623 int i;
624 va_list args;
625
626 memset(&e, 0, sizeof(e));
627 e.header = ET_Internal;
628 e.type = ET_XQuartz;
629 e.length = sizeof(e);
630 e.time = GetTimeInMillis();
631 e.subtype = type;
632
633 if (argc > 0 && argc < XQUARTZ_EVENT_MAXARGS) {
634 va_start(args, argc);
635 for (i = 0; i < argc; i++)
636 e.data[i] = (uint32_t)va_arg(args, uint32_t);
637 va_end(args);
638 }
639
640 wait_for_mieq_init();
641
642 input_lock();
643 {
644 mieqEnqueue(NULL, (InternalEvent *)&e);
645 DarwinPokeEQ();
646 } input_unlock();
647 }
648