/* Darwin event queue and event handling Copyright 2007-2008 Apple Inc. Copyright 2004 Kaleb S. KEITHLEY. All Rights Reserved. Copyright (c) 2002-2004 Torrey T. Lyons. All Rights Reserved. This file is based on mieq.c by Keith Packard, which contains the following copyright: Copyright 1990, 1998 The Open Group Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Except as contained in this notice, the name of The Open Group shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from The Open Group. */ #ifdef HAVE_DIX_CONFIG_H #include #endif #define NEED_EVENTS #include #include #include #include "misc.h" #include "windowstr.h" #include "pixmapstr.h" #include "inputstr.h" #include "mi.h" #include "scrnintstr.h" #include "mipointer.h" #include "os.h" #include "darwin.h" #include "quartz.h" #include "quartzKeyboard.h" #include "darwinEvents.h" #include #include #include #include #include #include /* Fake button press/release for scroll wheel move. */ #define SCROLLWHEELUPFAKE 4 #define SCROLLWHEELDOWNFAKE 5 #define SCROLLWHEELLEFTFAKE 6 #define SCROLLWHEELRIGHTFAKE 7 /* These values were chosen to match the output of xinput under Linux */ #define SCALEFACTOR_TILT 64.0 #define SCALEFACTOR_PRESSURE 1000.0 #define _APPLEWM_SERVER_ #include "applewmExt.h" #include /* FIXME: Abstract this better */ void QuartzModeEQInit(void); static int modifier_flags = 0; // last known modifier state #define FD_ADD_MAX 128 static int fd_add[FD_ADD_MAX]; int fd_add_count = 0; static pthread_mutex_t fd_add_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t fd_add_ready_cond = PTHREAD_COND_INITIALIZER; static pthread_t fd_add_tid = NULL; static xEvent *darwinEvents = NULL; static pthread_mutex_t mieq_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t mieq_ready_cond = PTHREAD_COND_INITIALIZER; /*** Pthread Magics ***/ static pthread_t create_thread(void *func, void *arg) { pthread_attr_t attr; pthread_t tid; pthread_attr_init (&attr); pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); pthread_create (&tid, &attr, func, arg); pthread_attr_destroy (&attr); return tid; } static inline void darwinEvents_lock(void) { int err; if((err = pthread_mutex_lock(&mieq_lock))) { ErrorF("%s:%s:%d: Failed to lock mieq_lock: %d\n", __FILE__, __FUNCTION__, __LINE__, err); spewCallStack(); } if(darwinEvents == NULL) { pthread_cond_wait(&mieq_ready_cond, &mieq_lock); /* We want to give xinit time to finish running xinitrc before we accept * the launchd socket connection. * * Yes, we lock then immediately unlock because the lock does a cond_wait * when darwinEvents == NULL * * TODO: Cleanup this race more elegantly. * * For some reason, xinitrc doesn't run until after this anyways... =/ * sleep(2); */ } } static inline void darwinEvents_unlock(void) { int err; if((err = pthread_mutex_unlock(&mieq_lock))) { ErrorF("%s:%s:%d: Failed to unlock mieq_lock: %d\n", __FILE__, __FUNCTION__, __LINE__, err); spewCallStack(); } } /* * DarwinPressModifierMask * Press or release the given modifier key, specified by its mask (one of NX_*MASK constants) */ static void DarwinPressModifierMask(int pressed, int mask) { int keycode; int key = DarwinModifierNXMaskToNXKey(mask); if (key != -1) { keycode = DarwinModifierNXKeyToNXKeycode(key, 0); if (keycode != 0) DarwinSendKeyboardEvents(pressed, keycode); } } #ifdef NX_DEVICELCTLKEYMASK #define CONTROL_MASK(flags) (flags & (NX_DEVICELCTLKEYMASK|NX_DEVICERCTLKEYMASK)) #define NX_CONTROLMASK_FULL (NX_CONTROLMASK | NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK) #else #define CONTROL_MASK(flags) (NX_CONTROLMASK) #define NX_CONTROLMASK_FULL NX_CONTROLMASK #endif /* NX_DEVICELCTLKEYMASK */ #ifdef NX_DEVICELSHIFTKEYMASK #define SHIFT_MASK(flags) (flags & (NX_DEVICELSHIFTKEYMASK|NX_DEVICERSHIFTKEYMASK)) #define NX_SHIFTMASK_FULL (NX_SHIFTMASK | NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK) #else #define SHIFT_MASK(flags) (NX_SHIFTMASK) #define NX_SHIFTMASK_FULL NX_SHIFTMASK #endif /* NX_DEVICELSHIFTKEYMASK */ #ifdef NX_DEVICELCMDKEYMASK #define COMMAND_MASK(flags) (flags & (NX_DEVICELCMDKEYMASK|NX_DEVICERCMDKEYMASK)) #define NX_COMMANDMASK_FULL (NX_COMMANDMASK | NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK) #else #define COMMAND_MASK(flags) (NX_COMMANDMASK) #define NX_COMMANDMASK_FULL NX_COMMANDMASK #endif /* NX_DEVICELCMDKEYMASK */ #ifdef NX_DEVICELALTKEYMASK #define ALTERNATE_MASK(flags) (flags & (NX_DEVICELALTKEYMASK|NX_DEVICERALTKEYMASK)) #define NX_ALTERNATEMASK_FULL (NX_ALTERNATEMASK | NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK) #else #define ALTERNATE_MASK(flags) (NX_ALTERNATEMASK) #define NX_ALTERNATEMASK_FULL NX_ALTERNATEMASK #endif /* NX_DEVICELALTKEYMASK */ /* * DarwinUpdateModifiers * Send events to update the modifier state. */ static void DarwinUpdateModifiers( int pressed, // KeyPress or KeyRelease int flags ) // modifier flags that have changed { if (flags & NX_ALPHASHIFTMASK) { DarwinPressModifierMask(pressed, NX_ALPHASHIFTMASK); } if (flags & NX_COMMANDMASK_FULL) { DarwinPressModifierMask(pressed, COMMAND_MASK(flags)); } if (flags & NX_CONTROLMASK_FULL) { DarwinPressModifierMask(pressed, CONTROL_MASK(flags)); } if (flags & NX_ALTERNATEMASK_FULL) { DarwinPressModifierMask(pressed, ALTERNATE_MASK(flags)); } if (flags & NX_SHIFTMASK_FULL) { DarwinPressModifierMask(pressed, SHIFT_MASK(flags)); } if (flags & NX_SECONDARYFNMASK) { DarwinPressModifierMask(pressed, NX_SECONDARYFNMASK); } } /* * DarwinReleaseModifiers * This hacky function releases all modifier keys. It should be called when X11.app * is deactivated (kXquartzDeactivate) to prevent modifiers from getting stuck if they * are held down during a "context" switch -- otherwise, we would miss the KeyUp. */ static void DarwinReleaseModifiers(void) { ErrorF("DarwinReleaseModifiers\n"); DarwinUpdateModifiers(KeyRelease, COMMAND_MASK(-1) | CONTROL_MASK(-1) | ALTERNATE_MASK(-1) | SHIFT_MASK(-1)); } /* Generic handler for Xquartz-specifc events. When possible, these should be moved into their own individual functions and set as handlers using mieqSetHandler. */ static void DarwinEventHandler(int screenNum, xEventPtr xe, DeviceIntPtr dev, int nevents) { int i; TA_SERVER(); DEBUG_LOG("DarwinEventHandler(%d, %p, %p, %d)\n", screenNum, xe, dev, nevents); for (i=0; i oh, i ... er ... christ. write(darwinEventWriteFD, &nullbyte, 1); } /* Convert from Appkit pointer input values to X input values: * Note: pointer_x and pointer_y are relative to the upper-left of primary * display. */ static void DarwinPrepareValuators(int *valuators, ScreenPtr screen, int pointer_x, int pointer_y, float pressure, float tilt_x, float tilt_y) { /* Fix offset between darwin and X screens */ pointer_x -= darwinMainScreenX + dixScreenOrigins[screen->myNum].x; pointer_y -= darwinMainScreenY + dixScreenOrigins[screen->myNum].y; /* Setup our array of values */ valuators[0] = pointer_x; valuators[1] = pointer_y; valuators[2] = pressure * SCALEFACTOR_PRESSURE; valuators[3] = tilt_x * SCALEFACTOR_TILT; valuators[4] = tilt_y * SCALEFACTOR_TILT; DEBUG_LOG("Valuators: {%d,%d,%d,%d,%d}\n", valuators[0], valuators[1], valuators[2], valuators[3], valuators[4]); } void DarwinSendPointerEvents(int ev_type, int ev_button, int pointer_x, int pointer_y, float pressure, float tilt_x, float tilt_y) { static int darwinFakeMouseButtonDown = 0; int i, num_events; DeviceIntPtr dev; ScreenPtr screen; int valuators[5]; DEBUG_LOG("x=%d, y=%d, p=%f, tx=%f, ty=%f\n", pointer_x, pointer_y, pressure, tilt_x, tilt_y); if(!darwinEvents) { DEBUG_LOG("DarwinSendPointerEvents called before darwinEvents was initialized\n"); return; } if (pressure == 0 && tilt_x == 0 && tilt_y == 0) dev = darwinPointer; else dev = darwinTablet; screen = miPointerGetScreen(dev); if(!screen) { DEBUG_LOG("DarwinSendPointerEvents called before screen was initialized\n"); return; } /* Handle fake click */ if (ev_type == ButtonPress && darwinFakeButtons && ev_button == 1) { if(darwinFakeMouseButtonDown != 0) { /* We're currently "down" with another button, so release it first */ DarwinSendPointerEvents(ButtonRelease, darwinFakeMouseButtonDown, pointer_x, pointer_y, pressure, tilt_x, tilt_y); darwinFakeMouseButtonDown=0; } if ((modifier_flags & darwinFakeMouse2Mask) == darwinFakeMouse2Mask) { ev_button = 2; darwinFakeMouseButtonDown = 2; } else if ((modifier_flags & darwinFakeMouse3Mask) == darwinFakeMouse3Mask) { ev_button = 3; darwinFakeMouseButtonDown = 3; } } if (ev_type == ButtonRelease && ev_button == 1) { if(darwinFakeMouseButtonDown) { ev_button = darwinFakeMouseButtonDown; darwinFakeMouseButtonDown = 0; } } DarwinPrepareValuators(valuators, screen, pointer_x, pointer_y, pressure, tilt_x, tilt_y); darwinEvents_lock(); { num_events = GetPointerEvents(darwinEvents, dev, ev_type, ev_button, POINTER_ABSOLUTE, 0, dev==darwinTablet?5:2, valuators); for(i=0; i 0.0f ? SCROLLWHEELLEFTFAKE : SCROLLWHEELRIGHTFAKE; int sign_y = count_y > 0.0f ? SCROLLWHEELUPFAKE : SCROLLWHEELDOWNFAKE; count_x = fabs(count_x); count_y = fabs(count_y); while ((count_x > 0.0f) || (count_y > 0.0f)) { if (count_x > 0.0f) { DarwinSendPointerEvents(ButtonPress, sign_x, pointer_x, pointer_y, pressure, tilt_x, tilt_y); DarwinSendPointerEvents(ButtonRelease, sign_x, pointer_x, pointer_y, pressure, tilt_x, tilt_y); count_x = count_x - 1.0f; } if (count_y > 0.0f) { DarwinSendPointerEvents(ButtonPress, sign_y, pointer_x, pointer_y, pressure, tilt_x, tilt_y); DarwinSendPointerEvents(ButtonRelease, sign_y, pointer_x, pointer_y, pressure, tilt_x, tilt_y); count_y = count_y - 1.0f; } } } /* Send the appropriate KeyPress/KeyRelease events to GetKeyboardEvents to reflect changing modifier flags (alt, control, meta, etc) */ void DarwinUpdateModKeys(int flags) { DarwinUpdateModifiers(KeyRelease, modifier_flags & ~flags); DarwinUpdateModifiers(KeyPress, ~modifier_flags & flags); modifier_flags = flags; } /* * DarwinSendDDXEvent * Send the X server thread a message by placing it on the event queue. */ void DarwinSendDDXEvent(int type, int argc, ...) { xEvent xe; INT32 *argv; int i, max_args; va_list args; memset(&xe, 0, sizeof(xe)); xe.u.u.type = type; xe.u.clientMessage.u.l.type = type; argv = &xe.u.clientMessage.u.l.longs0; max_args = 4; if (argc > 0 && argc <= max_args) { va_start (args, argc); for (i = 0; i < argc; i++) argv[i] = (int) va_arg (args, int); va_end (args); } darwinEvents_lock(); { mieqEnqueue(darwinPointer, &xe); DarwinPokeEQ(); } darwinEvents_unlock(); }