2007-11-24 10:55:21 -07:00
/ * X11Application . m - - subclass of NSApplication to multiplex events
2012-06-10 07:21:05 -06:00
*
* Copyright ( c ) 2002 -2012 Apple Inc . All rights reserved .
*
* Permission is hereby granted , free of charge , to any person
* obtaining a copy of this software and associated documentation files
* ( the "Software" ) , to deal in the Software without restriction ,
* including without limitation the rights to use , copy , modify , merge ,
* publish , distribute , sublicense , and / or sell copies of the Software ,
* and to permit persons to whom the Software is furnished to do so ,
* subject to the following conditions :
*
* 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 ABOVE LISTED COPYRIGHT
* HOLDER ( S ) 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 ( s ) of the above
* copyright holders shall not be used in advertising or otherwise to
* promote the sale , use or other dealings in this Software without
* prior written authorization .
* /
2007-11-24 10:55:21 -07:00
2009-09-06 13:44:18 -06:00
# include "sanitizedCarbon.h"
2008-11-02 08:26:08 -07:00
# ifdef HAVE_DIX _CONFIG _H
# include < dix - config . h >
# endif
# include "quartzCommon.h"
2007-11-24 10:55:21 -07:00
# import "X11Application.h"
2009-09-06 13:44:18 -06:00
# include "darwin.h"
2010-12-05 08:36:02 -07:00
# include "quartz.h"
2009-09-06 13:44:18 -06:00
# include "darwinEvents.h"
# include "quartzKeyboard.h"
2010-07-27 13:02:24 -06:00
# include < X11 / extensions / applewmconst . h >
2009-09-06 13:44:18 -06:00
# include "micmap.h"
2010-07-27 13:02:24 -06:00
# include "exglobals.h"
2007-11-24 10:55:21 -07:00
# include < mach / mach . h >
# include < unistd . h >
2009-09-06 13:44:18 -06:00
# include < AvailabilityMacros . h >
2011-11-05 07:32:40 -06:00
# include < pthread . h >
2009-09-06 13:44:18 -06:00
# include < Xplugin . h >
2007-11-24 10:55:21 -07:00
2009-09-06 13:44:18 -06:00
// pbproxy / pbproxy . h
2012-06-10 07:21:05 -06:00
extern int
xpbproxy_run ( void ) ;
2008-11-02 08:26:08 -07:00
2012-06-10 07:21:05 -06:00
# define DEFAULTS_FILE X11LIBDIR "/X11/xserver/Xquartz.plist"
2009-09-06 13:44:18 -06:00
# ifndef XSERVER_VERSION
# define XSERVER_VERSION "?"
# endif
2008-11-02 08:26:08 -07:00
2011-11-05 07:32:40 -06:00
# ifdef HAVE_LIBDISPATCH
# include < dispatch / dispatch . h >
static dispatch_queue _t eventTranslationQueue ;
# endif
2014-05-02 13:27:46 -06:00
# ifndef __has _feature
# define __has _feature ( x ) 0
# endif
# ifndef CF_RETURNS _RETAINED
# if __has _feature ( attribute_cf _returns _retained )
# define CF_RETURNS _RETAINED __attribute __ ( ( cf_returns _retained ) )
# else
# define CF_RETURNS _RETAINED
# endif
# endif
2009-09-06 13:44:18 -06:00
extern Bool noTestExtensions ;
2012-06-10 07:21:05 -06:00
extern Bool noRenderExtension ;
2009-09-06 13:44:18 -06:00
# if MAC_OS _X _VERSION _MIN _REQUIRED >= 1050
static TISInputSourceRef last_key _layout ;
# else
static KeyboardLayoutRef last_key _layout ;
# endif
2007-11-24 10:55:21 -07:00
2011-11-05 07:32:40 -06:00
/ * This preference is only tested on Lion or later as it ' s not relevant to
* earlier OS versions .
* /
Bool XQuartzScrollInDeviceDirection = FALSE ;
2009-09-06 13:44:18 -06:00
extern int darwinFakeButtons ;
2007-11-24 10:55:21 -07:00
2010-07-27 13:02:24 -06:00
/ * Store the mouse location while in the background , and update X11 ' s pointer
* location when we become the foreground application
* /
static NSPoint bgMouseLocation ;
static BOOL bgMouseLocationUpdated = FALSE ;
2007-11-24 10:55:21 -07:00
X11Application * X11App ;
2009-09-06 13:44:18 -06:00
CFStringRef app_prefs _domain _cfstr = NULL ;
2012-06-10 07:21:05 -06:00
# define ALL_KEY _MASKS ( NSShiftKeyMask | NSControlKeyMask | \
NSAlternateKeyMask | NSCommandKeyMask )
2007-11-24 10:55:21 -07:00
2009-09-06 13:44:18 -06:00
@ interface X11Application ( Private )
- ( void ) sendX11NSEvent : ( NSEvent * ) e ;
@ end
2007-11-24 10:55:21 -07:00
@ implementation X11Application
typedef struct message_struct message ;
struct message_struct {
mach_msg _header _t hdr ;
SEL selector ;
NSObject * arg ;
} ;
static mach_port _t _port ;
/ * Quartz mode initialization routine . This is often dynamically loaded
2008-11-02 08:26:08 -07:00
but is statically linked into this X server . * /
2012-06-10 07:21:05 -06:00
Bool
QuartzModeBundleInit ( void ) ;
2007-11-24 10:55:21 -07:00
2012-06-10 07:21:05 -06:00
static void
init_ports ( void )
{
2007-11-24 10:55:21 -07:00
kern_return _t r ;
NSPort * p ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
if ( _port ! = MACH_PORT _NULL ) return ;
2012-06-10 07:21:05 -06:00
r = mach_port _allocate ( mach_task _self ( ) , MACH_PORT _RIGHT _RECEIVE , & _port ) ;
2007-11-24 10:55:21 -07:00
if ( r ! = KERN_SUCCESS ) return ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
p = [ NSMachPort portWithMachPort : _port ] ;
[ p setDelegate : NSApp ] ;
2012-06-10 07:21:05 -06:00
[ p scheduleInRunLoop : [ NSRunLoop currentRunLoop ] forMode :
NSDefaultRunLoopMode ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
static void
message_kit _thread ( SEL selector , NSObject * arg )
{
2007-11-24 10:55:21 -07:00
message msg ;
kern_return _t r ;
2012-06-10 07:21:05 -06:00
msg . hdr . msgh_bits = MACH_MSGH _BITS ( MACH_MSG _TYPE _MAKE _SEND , 0 ) ;
msg . hdr . msgh_size = sizeof ( msg ) ;
2007-11-24 10:55:21 -07:00
msg . hdr . msgh_remote _port = _port ;
msg . hdr . msgh_local _port = MACH_PORT _NULL ;
msg . hdr . msgh_reserved = 0 ;
msg . hdr . msgh_id = 0 ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
msg . selector = selector ;
msg . arg = [ arg retain ] ;
2012-06-10 07:21:05 -06:00
r = mach_msg ( & msg . hdr , MACH_SEND _MSG , msg . hdr . msgh_size ,
0 , MACH_PORT _NULL , 0 , MACH_PORT _NULL ) ;
2007-11-24 10:55:21 -07:00
if ( r ! = KERN_SUCCESS )
2012-06-10 07:21:05 -06:00
ErrorF ( "%s: mach_msg failed: %x\n" , __FUNCTION __ , r ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) handleMachMessage : ( void * ) _msg
{
2007-11-24 10:55:21 -07:00
message * msg = _msg ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
[ self performSelector : msg -> selector withObject : msg -> arg ] ;
[ msg -> arg release ] ;
}
2012-06-10 07:21:05 -06:00
- ( void ) set_controller : obj
{
2007-11-24 10:55:21 -07:00
if ( _controller = = nil ) _controller = [ obj retain ] ;
}
2012-06-10 07:21:05 -06:00
- ( void ) dealloc
{
2007-11-24 10:55:21 -07:00
if ( _controller ! = nil ) [ _controller release ] ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
if ( _port ! = MACH_PORT _NULL )
2012-06-10 07:21:05 -06:00
mach_port _deallocate ( mach_task _self ( ) , _port ) ;
2007-11-24 10:55:21 -07:00
[ super dealloc ] ;
}
2012-06-10 07:21:05 -06:00
- ( void ) orderFrontStandardAboutPanel : ( id ) sender
{
2007-11-24 10:55:21 -07:00
NSMutableDictionary * dict ;
NSDictionary * infoDict ;
NSString * tem ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
dict = [ NSMutableDictionary dictionaryWithCapacity : 3 ] ;
2007-11-24 10:55:21 -07:00
infoDict = [ [ NSBundle mainBundle ] infoDictionary ] ;
2012-06-10 07:21:05 -06:00
[ dict setObject : NSLocalizedString ( @ "The X Window System" , @ "About panel" )
forKey : @ "ApplicationName" ] ;
2007-11-24 10:55:21 -07:00
tem = [ infoDict objectForKey : @ "CFBundleShortVersionString" ] ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
[ dict setObject : [ NSString stringWithFormat : @ "XQuartz %@" , tem ]
2012-06-10 07:21:05 -06:00
forKey : @ "ApplicationVersion" ] ;
[ dict setObject : [ NSString stringWithFormat : @ "xorg-server %s" ,
XSERVER_VERSION ]
forKey : @ "Version" ] ;
2009-09-06 13:44:18 -06:00
2007-11-24 10:55:21 -07:00
[ self orderFrontStandardAboutPanelWithOptions : dict ] ;
}
2012-06-10 07:21:05 -06:00
- ( void ) activateX : ( OSX_BOOL ) state
{
if ( _x _active = = state )
return ;
2011-11-05 07:32:40 -06:00
DEBUG_LOG ( "state=%d, _x_active=%d, \n" , state , _x _active ) ;
2007-11-24 10:55:21 -07:00
if ( state ) {
2012-06-10 07:21:05 -06:00
if ( bgMouseLocationUpdated ) {
DarwinSendPointerEvents ( darwinPointer , MotionNotify , 0 ,
bgMouseLocation . x , bgMouseLocation . y ,
0.0 , 0.0 ) ;
2010-07-27 13:02:24 -06:00
bgMouseLocationUpdated = FALSE ;
2009-09-06 13:44:18 -06:00
}
2010-07-27 13:02:24 -06:00
DarwinSendDDXEvent ( kXquartzActivate , 0 ) ;
2012-06-10 07:21:05 -06:00
}
else {
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( darwin_all _modifier _flags )
2009-09-06 13:44:18 -06:00
DarwinUpdateModKeys ( 0 ) ;
2012-06-10 07:21:05 -06:00
DarwinInputReleaseButtonsAndKeys ( darwinKeyboard ) ;
DarwinInputReleaseButtonsAndKeys ( darwinPointer ) ;
DarwinInputReleaseButtonsAndKeys ( darwinTabletCursor ) ;
DarwinInputReleaseButtonsAndKeys ( darwinTabletStylus ) ;
DarwinInputReleaseButtonsAndKeys ( darwinTabletEraser ) ;
2009-09-06 13:44:18 -06:00
DarwinSendDDXEvent ( kXquartzDeactivate , 0 ) ;
2007-11-24 10:55:21 -07:00
}
2009-09-06 13:44:18 -06:00
2007-11-24 10:55:21 -07:00
_x _active = state ;
}
2012-06-10 07:21:05 -06:00
- ( void ) became_key : ( NSWindow * ) win
{
[ self activateX : NO ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) sendEvent : ( NSEvent * ) e
{
2009-09-06 13:44:18 -06:00
OSX_BOOL for_appkit , for_x ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
/ * By default pass down the responder chain and to X . * /
for_appkit = YES ;
for_x = YES ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
switch ( [ e type ] ) {
2012-06-10 07:21:05 -06:00
case NSLeftMouseDown :
case NSRightMouseDown :
case NSOtherMouseDown :
case NSLeftMouseUp :
case NSRightMouseUp :
case NSOtherMouseUp :
if ( [ e window ] ! = nil ) {
/ * Pointer event has an ( AppKit ) window . Probably something for the kit . * /
for_x = NO ;
if ( _x _active ) [ self activateX : NO ] ;
}
else if ( [ self modalWindow ] = = nil ) {
/ * Must be an X window . Tell appkit it doesn ' t have focus . * /
for_appkit = NO ;
if ( [ self isActive ] ) {
[ self deactivate ] ;
if ( ! _x _active && quartzProcs -> IsX11Window ( [ e windowNumber ] ) )
[ self activateX : YES ] ;
2009-09-06 13:44:18 -06:00
}
2012-06-10 07:21:05 -06:00
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
/ * We want to force sending to appkit if we ' re over the menu bar * /
if ( ! for_appkit ) {
NSPoint NSlocation = [ e locationInWindow ] ;
NSWindow * window = [ e window ] ;
NSRect NSframe , NSvisibleFrame ;
CGRect CGframe , CGvisibleFrame ;
CGPoint CGlocation ;
if ( window ! = nil ) {
NSRect frame = [ window frame ] ;
NSlocation . x + = frame . origin . x ;
NSlocation . y + = frame . origin . y ;
2009-09-06 13:44:18 -06:00
}
2012-06-10 07:21:05 -06:00
NSframe = [ [ NSScreen mainScreen ] frame ] ;
NSvisibleFrame = [ [ NSScreen mainScreen ] visibleFrame ] ;
CGframe = CGRectMake ( NSframe . origin . x , NSframe . origin . y ,
NSframe . size . width , NSframe . size . height ) ;
CGvisibleFrame = CGRectMake ( NSvisibleFrame . origin . x ,
NSvisibleFrame . origin . y ,
NSvisibleFrame . size . width ,
NSvisibleFrame . size . height ) ;
CGlocation = CGPointMake ( NSlocation . x , NSlocation . y ) ;
if ( CGRectContainsPoint ( CGframe , CGlocation ) &&
! CGRectContainsPoint ( CGvisibleFrame , CGlocation ) )
for_appkit = YES ;
}
break ;
case NSKeyDown :
case NSKeyUp :
if ( _x _active ) {
static BOOL do_swallow = NO ;
static int swallow_keycode ;
if ( [ e type ] = = NSKeyDown ) {
/ * Before that though , see if there are any global
* shortcuts bound to it . * /
if ( darwinAppKitModMask & [ e modifierFlags ] ) {
/ * Override to force sending to Appkit * /
swallow_keycode = [ e keyCode ] ;
do_swallow = YES ;
for_x = NO ;
2009-09-06 13:44:18 -06:00
# if XPLUGIN_VERSION >= 1
2012-06-10 07:21:05 -06:00
}
else if ( XQuartzEnableKeyEquivalents &&
xp_is _symbolic _hotkey _event ( [ e eventRef ] ) ) {
swallow_keycode = [ e keyCode ] ;
do_swallow = YES ;
for_x = NO ;
2009-09-06 13:44:18 -06:00
# endif
}
2012-06-10 07:21:05 -06:00
else if ( XQuartzEnableKeyEquivalents &&
[ [ self mainMenu ] performKeyEquivalent : e ] ) {
swallow_keycode = [ e keyCode ] ;
do_swallow = YES ;
for_appkit = NO ;
for_x = NO ;
}
else if ( ! XQuartzIsRootless
&& ( [ e modifierFlags ] & ALL_KEY _MASKS ) = =
( NSCommandKeyMask | NSAlternateKeyMask )
&& ( [ e keyCode ] = = 0 / * a * / || [ e keyCode ] = =
53 / * Esc * / ) ) {
/ * We have this here to force processing fullscreen
* toggle even if XQuartzEnableKeyEquivalents is disabled * /
swallow_keycode = [ e keyCode ] ;
do_swallow = YES ;
for_x = NO ;
for_appkit = NO ;
DarwinSendDDXEvent ( kXquartzToggleFullscreen , 0 ) ;
}
else {
/ * No kit window is focused , so send it to X . * /
for_appkit = NO ;
2017-12-08 08:01:59 -07:00
/ * Reset our swallow state if we ' re seeing the same keyCode again .
* This can happen if we become ! _x _active when the keyCode we
* intended to swallow is delivered . See :
* https : // bugs . freedesktop . org / show_bug . cgi ? id = 92648
* /
if ( [ e keyCode ] = = swallow_keycode ) {
do_swallow = NO ;
}
2012-06-10 07:21:05 -06:00
}
}
else { / * KeyUp * /
/ * If we saw a key equivalent on the down , don ' t pass
* the up through to X . * /
if ( do_swallow && [ e keyCode ] = = swallow_keycode ) {
do_swallow = NO ;
for_x = NO ;
}
}
}
else { / * ! _x _active * /
for_x = NO ;
}
break ;
case NSFlagsChanged :
/ * Don ' t tell X11 about modifiers changing while it ' s not active * /
if ( ! _x _active )
for_x = NO ;
break ;
case NSAppKitDefined :
switch ( [ e subtype ] ) {
static BOOL x_was _active = NO ;
case NSApplicationActivatedEventType :
for_x = NO ;
if ( [ e window ] = = nil && x_was _active ) {
BOOL order_all _windows = YES , workspaces , ok ;
for_appkit = NO ;
/ * FIXME : This is a hack to avoid passing the event to AppKit which
* would result in it raising one of its windows .
* /
_appFlags . _active = YES ;
[ self set_front _process : nil ] ;
/ * Get the Spaces preference for SwitchOnActivate * /
( void ) CFPreferencesAppSynchronize ( CFSTR ( "com.apple.dock" ) ) ;
workspaces =
CFPreferencesGetAppBooleanValue ( CFSTR ( "workspaces" ) ,
CFSTR (
"com.apple.dock" ) ,
& ok ) ;
if ( ! ok )
workspaces = NO ;
if ( workspaces ) {
( void ) CFPreferencesAppSynchronize ( CFSTR (
".GlobalPreferences" ) ) ;
order_all _windows =
CFPreferencesGetAppBooleanValue ( CFSTR (
"AppleSpacesSwitchOnActivate" ) ,
CFSTR (
".GlobalPreferences" ) ,
& ok ) ;
if ( ! ok )
order_all _windows = YES ;
}
/ * TODO : In the workspaces && ! AppleSpacesSwitchOnActivate case , the windows are ordered
* correctly , but we need to activate the top window on this space if there is
* none active .
*
* If there are no active windows , and there are minimized windows , we should
* be restoring one of them .
* /
if ( [ e data2 ] & 0 x10 ) { // 0 x10 ( bfCPSOrderAllWindowsForward ) is set when we use cmd - tab or the dock icon
DarwinSendDDXEvent ( kXquartzBringAllToFront , 1 ,
order_all _windows ) ;
}
2009-09-06 13:44:18 -06:00
}
break ;
2012-06-10 07:21:05 -06:00
case 18 : / * ApplicationDidReactivate * /
if ( XQuartzFullscreenVisible ) for_appkit = NO ;
2009-09-06 13:44:18 -06:00
break ;
2011-11-05 07:32:40 -06:00
2012-06-10 07:21:05 -06:00
case NSApplicationDeactivatedEventType :
for_x = NO ;
2011-11-05 07:32:40 -06:00
2012-06-10 07:21:05 -06:00
x_was _active = _x _active ;
if ( _x _active )
[ self activateX : NO ] ;
2009-09-06 13:44:18 -06:00
break ;
2012-06-10 07:21:05 -06:00
}
break ;
default :
break ; / * for gcc * /
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
if ( for_appkit ) [ super sendEvent : e ] ;
2012-06-10 07:21:05 -06:00
2011-11-05 07:32:40 -06:00
if ( for_x ) {
# ifdef HAVE_LIBDISPATCH
dispatch_async ( eventTranslationQueue , ^ {
2012-06-10 07:21:05 -06:00
[ self sendX11NSEvent : e ] ;
} ) ;
# else
[ self sendX11NSEvent : e ] ;
2011-11-05 07:32:40 -06:00
# endif
}
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) set_window _menu : ( NSArray * ) list
{
[ _controller set_window _menu : list ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) set_window _menu _check : ( NSNumber * ) n
{
[ _controller set_window _menu _check : n ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) set_apps _menu : ( NSArray * ) list
{
[ _controller set_apps _menu : list ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) set_front _process : unused
{
[ NSApp activateIgnoringOtherApps : YES ] ;
2008-11-02 08:26:08 -07:00
2012-06-10 07:21:05 -06:00
if ( [ self modalWindow ] = = nil )
[ self activateX : YES ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) set_can _quit : ( NSNumber * ) state
{
[ _controller set_can _quit : [ state boolValue ] ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) server_ready : unused
{
[ _controller server_ready ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) show_hide _menubar : ( NSNumber * ) state
{
2009-09-06 13:44:18 -06:00
/ * Also shows / hides the dock * /
if ( [ state boolValue ] )
2012-06-10 07:21:05 -06:00
SetSystemUIMode ( kUIModeNormal , 0 ) ;
2009-09-06 13:44:18 -06:00
else
2012-06-10 07:21:05 -06:00
SetSystemUIMode ( kUIModeAllHidden ,
XQuartzFullscreenMenu ? kUIOptionAutoShowMenuBar : 0 ) ; // kUIModeAllSuppressed or kUIOptionAutoShowMenuBar can be used to allow "mouse-activation"
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) launch_client : ( NSString * ) cmd
{
2010-07-27 13:02:24 -06:00
( void ) [ _controller application : self openFile : cmd ] ;
}
2007-11-24 10:55:21 -07:00
/ * user preferences * /
/ * Note that these functions only work for arrays whose elements
2012-06-10 07:21:05 -06:00
can be toll - free - bridged between NS and CF worlds . * /
2007-11-24 10:55:21 -07:00
2012-06-10 07:21:05 -06:00
static const void *
cfretain ( CFAllocatorRef a , const void * b )
{
return CFRetain ( b ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
static void
cfrelease ( CFAllocatorRef a , const void * b )
{
CFRelease ( b ) ;
2007-11-24 10:55:21 -07:00
}
2014-05-02 13:27:46 -06:00
CF_RETURNS _RETAINED
2012-06-10 07:21:05 -06:00
static CFMutableArrayRef
nsarray_to _cfarray ( NSArray * in )
{
CFMutableArrayRef out ;
CFArrayCallBacks cb ;
NSObject * ns ;
const CFTypeRef * cf ;
int i , count ;
memset ( & cb , 0 , sizeof ( cb ) ) ;
cb . version = 0 ;
cb . retain = cfretain ;
cb . release = cfrelease ;
count = [ in count ] ;
out = CFArrayCreateMutable ( NULL , count , & cb ) ;
for ( i = 0 ; i < count ; i + + ) {
ns = [ in objectAtIndex : i ] ;
if ( [ ns isKindOfClass : [ NSArray class ] ] )
cf = ( CFTypeRef ) nsarray_to _cfarray ( ( NSArray * ) ns ) ;
else
cf = CFRetain ( ( CFTypeRef ) ns ) ;
CFArrayAppendValue ( out , cf ) ;
CFRelease ( cf ) ;
}
return out ;
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
static NSMutableArray *
cfarray_to _nsarray ( CFArrayRef in )
{
NSMutableArray * out ;
const CFTypeRef * cf ;
NSObject * ns ;
int i , count ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
count = CFArrayGetCount ( in ) ;
out = [ [ NSMutableArray alloc ] initWithCapacity : count ] ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
for ( i = 0 ; i < count ; i + + ) {
cf = CFArrayGetValueAtIndex ( in , i ) ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( CFGetTypeID ( cf ) = = CFArrayGetTypeID ( ) )
ns = cfarray_to _nsarray ( ( CFArrayRef ) cf ) ;
else
ns = [ ( id ) cf retain ] ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
[ out addObject : ns ] ;
[ ns release ] ;
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
return out ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( CFPropertyListRef ) prefs_get _copy : ( NSString * ) key
{
CFPropertyListRef value ;
value = CFPreferencesCopyAppValue ( ( CFStringRef ) key ,
app_prefs _domain _cfstr ) ;
if ( value = = NULL ) {
static CFDictionaryRef defaults ;
if ( defaults = = NULL ) {
CFStringRef error = NULL ;
CFDataRef data ;
CFURLRef url ;
SInt32 error_code ;
url = ( CFURLCreateFromFileSystemRepresentation
( NULL , ( unsigned char * ) DEFAULTS_FILE ,
strlen ( DEFAULTS_FILE ) , false ) ) ;
if ( CFURLCreateDataAndPropertiesFromResource ( NULL , url , & data ,
NULL , NULL ,
& error_code ) ) {
defaults = ( CFPropertyListCreateFromXMLData
( NULL , data ,
kCFPropertyListMutableContainersAndLeaves ,
& error ) ) ;
if ( error ! = NULL ) CFRelease ( error ) ;
CFRelease ( data ) ;
}
CFRelease ( url ) ;
if ( defaults ! = NULL ) {
NSMutableArray * apps , * elt ;
int count , i ;
NSString * name , * nname ;
/ * Localize the names in the default apps menu . * /
apps =
[ ( NSDictionary * ) defaults objectForKey : @ PREFS_APPSMENU ] ;
if ( apps ! = nil ) {
count = [ apps count ] ;
for ( i = 0 ; i < count ; i + + ) {
elt = [ apps objectAtIndex : i ] ;
if ( elt ! = nil &&
[ elt isKindOfClass : [ NSArray class ] ] ) {
name = [ elt objectAtIndex : 0 ] ;
if ( name ! = nil ) {
nname = NSLocalizedString ( name , nil ) ;
if ( nname ! = nil && nname ! = name )
[ elt replaceObjectAtIndex : 0 withObject :
nname ] ;
}
}
}
}
}
}
if ( defaults ! = NULL ) value = CFDictionaryGetValue ( defaults , key ) ;
if ( value ! = NULL ) CFRetain ( value ) ;
}
return value ;
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
- ( int ) prefs_get _integer : ( NSString * ) key default : ( int ) def
{
CFPropertyListRef value ;
int ret ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
value = [ self prefs_get _copy : key ] ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( value ! = NULL && CFGetTypeID ( value ) = = CFNumberGetTypeID ( ) )
CFNumberGetValue ( value , kCFNumberIntType , & ret ) ;
else if ( value ! = NULL && CFGetTypeID ( value ) = = CFStringGetTypeID ( ) )
ret = CFStringGetIntValue ( value ) ;
else
ret = def ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( value ! = NULL ) CFRelease ( value ) ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
return ret ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( const char * ) prefs_get _string : ( NSString * ) key default : ( const char * ) def
{
2007-11-24 10:55:21 -07:00
CFPropertyListRef value ;
2012-06-10 07:21:05 -06:00
const char * ret = NULL ;
value = [ self prefs_get _copy : key ] ;
if ( value ! = NULL && CFGetTypeID ( value ) = = CFStringGetTypeID ( ) ) {
NSString * s = ( NSString * ) value ;
ret = [ s UTF8String ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
if ( value ! = NULL ) CFRelease ( value ) ;
2007-11-24 10:55:21 -07:00
2012-06-10 07:21:05 -06:00
return ret ! = NULL ? ret : def ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( NSURL * ) prefs_copy _url : ( NSString * ) key default : ( NSURL * ) def
{
2010-07-27 13:02:24 -06:00
CFPropertyListRef value ;
NSURL * ret = NULL ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
value = [ self prefs_get _copy : key ] ;
2012-06-10 07:21:05 -06:00
if ( value ! = NULL && CFGetTypeID ( value ) = = CFStringGetTypeID ( ) ) {
NSString * s = ( NSString * ) value ;
2010-07-27 13:02:24 -06:00
ret = [ NSURL URLWithString : s ] ;
[ ret retain ] ;
}
2012-06-10 07:21:05 -06:00
if ( value ! = NULL ) CFRelease ( value ) ;
2010-07-27 13:02:24 -06:00
return ret ! = NULL ? ret : def ;
}
2012-06-10 07:21:05 -06:00
- ( float ) prefs_get _float : ( NSString * ) key default : ( float ) def
{
CFPropertyListRef value ;
float ret = def ;
value = [ self prefs_get _copy : key ] ;
if ( value ! = NULL
&& CFGetTypeID ( value ) = = CFNumberGetTypeID ( )
&& CFNumberIsFloatType ( value ) )
CFNumberGetValue ( value , kCFNumberFloatType , & ret ) ;
else if ( value ! = NULL && CFGetTypeID ( value ) = = CFStringGetTypeID ( ) )
ret = CFStringGetDoubleValue ( value ) ;
if ( value ! = NULL ) CFRelease ( value ) ;
return ret ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( int ) prefs_get _boolean : ( NSString * ) key default : ( int ) def
{
CFPropertyListRef value ;
int ret = def ;
value = [ self prefs_get _copy : key ] ;
if ( value ! = NULL ) {
if ( CFGetTypeID ( value ) = = CFNumberGetTypeID ( ) )
CFNumberGetValue ( value , kCFNumberIntType , & ret ) ;
else if ( CFGetTypeID ( value ) = = CFBooleanGetTypeID ( ) )
ret = CFBooleanGetValue ( value ) ;
else if ( CFGetTypeID ( value ) = = CFStringGetTypeID ( ) ) {
const char * tem = [ ( NSString * ) value UTF8String ] ;
if ( strcasecmp ( tem , "true" ) = = 0 || strcasecmp ( tem , "yes" ) = = 0 )
ret = YES ;
else
ret = NO ;
}
CFRelease ( value ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
return ret ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( NSArray * ) prefs_get _array : ( NSString * ) key
{
NSArray * ret = nil ;
CFPropertyListRef value ;
value = [ self prefs_get _copy : key ] ;
if ( value ! = NULL ) {
if ( CFGetTypeID ( value ) = = CFArrayGetTypeID ( ) )
ret = [ cfarray_to _nsarray ( value ) autorelease ] ;
CFRelease ( value ) ;
}
return ret ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_set _integer : ( NSString * ) key value : ( int ) value
{
2007-11-24 10:55:21 -07:00
CFNumberRef x ;
2012-06-10 07:21:05 -06:00
x = CFNumberCreate ( NULL , kCFNumberIntType , & value ) ;
CFPreferencesSetValue ( ( CFStringRef ) key , ( CFTypeRef ) x ,
app_prefs _domain _cfstr ,
kCFPreferencesCurrentUser ,
kCFPreferencesAnyHost ) ;
CFRelease ( x ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_set _float : ( NSString * ) key value : ( float ) value
{
2007-11-24 10:55:21 -07:00
CFNumberRef x ;
2012-06-10 07:21:05 -06:00
x = CFNumberCreate ( NULL , kCFNumberFloatType , & value ) ;
CFPreferencesSetValue ( ( CFStringRef ) key , ( CFTypeRef ) x ,
app_prefs _domain _cfstr ,
kCFPreferencesCurrentUser ,
kCFPreferencesAnyHost ) ;
CFRelease ( x ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_set _boolean : ( NSString * ) key value : ( int ) value
{
CFPreferencesSetValue (
( CFStringRef ) key ,
( CFTypeRef ) ( value ? kCFBooleanTrue
: kCFBooleanFalse ) ,
app_prefs _domain _cfstr ,
kCFPreferencesCurrentUser , kCFPreferencesAnyHost ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_set _array : ( NSString * ) key value : ( NSArray * ) value
{
CFArrayRef cfarray ;
cfarray = nsarray_to _cfarray ( value ) ;
CFPreferencesSetValue ( ( CFStringRef ) key ,
( CFTypeRef ) cfarray ,
app_prefs _domain _cfstr ,
kCFPreferencesCurrentUser , kCFPreferencesAnyHost ) ;
CFRelease ( cfarray ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_set _string : ( NSString * ) key value : ( NSString * ) value
{
CFPreferencesSetValue ( ( CFStringRef ) key , ( CFTypeRef ) value ,
app_prefs _domain _cfstr , kCFPreferencesCurrentUser ,
kCFPreferencesAnyHost ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( void ) prefs_synchronize
{
CFPreferencesAppSynchronize ( kCFPreferencesCurrentApplication ) ;
2007-11-24 10:55:21 -07:00
}
2008-11-02 08:26:08 -07:00
- ( void ) read_defaults
{
2009-09-06 13:44:18 -06:00
NSString * nsstr ;
2008-11-02 08:26:08 -07:00
const char * tem ;
2012-06-10 07:21:05 -06:00
2010-12-05 08:36:02 -07:00
XQuartzRootlessDefault = [ self prefs_get _boolean : @ PREFS_ROOTLESS
2012-06-10 07:21:05 -06:00
default : XQuartzRootlessDefault ] ;
2010-12-05 08:36:02 -07:00
XQuartzFullscreenMenu = [ self prefs_get _boolean : @ PREFS_FULLSCREEN _MENU
2012-06-10 07:21:05 -06:00
default : XQuartzFullscreenMenu ] ;
XQuartzFullscreenDisableHotkeys =
! [ self prefs_get _boolean : @ PREFS_FULLSCREEN _HOTKEYS
default : !
XQuartzFullscreenDisableHotkeys ] ;
2008-11-02 08:26:08 -07:00
darwinFakeButtons = [ self prefs_get _boolean : @ PREFS_FAKEBUTTONS
2012-06-10 07:21:05 -06:00
default : darwinFakeButtons ] ;
2010-12-05 08:36:02 -07:00
XQuartzOptionSendsAlt = [ self prefs_get _boolean : @ PREFS_OPTION _SENDS _ALT
2012-06-10 07:21:05 -06:00
default : XQuartzOptionSendsAlt ] ;
2010-07-27 13:02:24 -06:00
2008-11-02 08:26:08 -07:00
if ( darwinFakeButtons ) {
2009-09-06 13:44:18 -06:00
const char * fake2 , * fake3 ;
fake2 = [ self prefs_get _string : @ PREFS_FAKE _BUTTON2 default : NULL ] ;
fake3 = [ self prefs_get _string : @ PREFS_FAKE _BUTTON3 default : NULL ] ;
2012-06-10 07:21:05 -06:00
if ( fake2 ! = NULL ) darwinFakeMouse2Mask = DarwinParseModifierList (
fake2 , TRUE ) ;
if ( fake3 ! = NULL ) darwinFakeMouse3Mask = DarwinParseModifierList (
fake3 , TRUE ) ;
2008-11-02 08:26:08 -07:00
}
2009-09-06 13:44:18 -06:00
tem = [ self prefs_get _string : @ PREFS_APPKIT _MODIFIERS default : NULL ] ;
if ( tem ! = NULL ) darwinAppKitModMask = DarwinParseModifierList ( tem , TRUE ) ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
tem = [ self prefs_get _string : @ PREFS_WINDOW _ITEM _MODIFIERS default : NULL ] ;
if ( tem ! = NULL ) {
windowItemModMask = DarwinParseModifierList ( tem , FALSE ) ;
2012-06-10 07:21:05 -06:00
}
else {
nsstr = NSLocalizedString ( @ "window item modifiers" ,
@ "window item modifiers" ) ;
if ( nsstr ! = NULL ) {
2009-09-06 13:44:18 -06:00
tem = [ nsstr UTF8String ] ;
2012-06-10 07:21:05 -06:00
if ( ( tem ! = NULL ) && strcmp ( tem , "window item modifiers" ) ) {
2009-09-06 13:44:18 -06:00
windowItemModMask = DarwinParseModifierList ( tem , FALSE ) ;
}
}
}
2010-12-05 08:36:02 -07:00
XQuartzEnableKeyEquivalents = [ self prefs_get _boolean : @ PREFS_KEYEQUIVS
2012-06-10 07:21:05 -06:00
default :
XQuartzEnableKeyEquivalents ] ;
2008-11-02 08:26:08 -07:00
darwinSyncKeymap = [ self prefs_get _boolean : @ PREFS_SYNC _KEYMAP
2012-06-10 07:21:05 -06:00
default : darwinSyncKeymap ] ;
2008-11-02 08:26:08 -07:00
darwinDesiredDepth = [ self prefs_get _integer : @ PREFS_DEPTH
2012-06-10 07:21:05 -06:00
default : darwinDesiredDepth ] ;
2009-09-06 13:44:18 -06:00
noTestExtensions = ! [ self prefs_get _boolean : @ PREFS_TEST _EXTENSIONS
2012-06-10 07:21:05 -06:00
default : FALSE ] ;
noRenderExtension = ! [ self prefs_get _boolean : @ PREFS_RENDER _EXTENSION
default : TRUE ] ;
XQuartzScrollInDeviceDirection =
[ self prefs_get _boolean : @ PREFS_SCROLL _IN _DEV _DIRECTION
default :
XQuartzScrollInDeviceDirection ] ;
2010-07-27 13:02:24 -06:00
# if XQUARTZ_SPARKLE
2012-06-10 07:21:05 -06:00
NSURL * url = [ self prefs_copy _url : @ PREFS_UPDATE _FEED default : nil ] ;
if ( url ) {
2010-07-27 13:02:24 -06:00
[ [ SUUpdater sharedUpdater ] setFeedURL : url ] ;
[ url release ] ;
}
# endif
2007-11-24 10:55:21 -07:00
}
/ * This will end up at the end of the responder chain . * /
2012-06-10 07:21:05 -06:00
- ( void ) copy : sender
{
DarwinSendDDXEvent ( kXquartzPasteboardNotify , 1 ,
AppleWMCopyToPasteboard ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
- ( X11Controller * ) controller
{
2010-07-27 13:02:24 -06:00
return _controller ;
}
2012-06-10 07:21:05 -06:00
- ( OSX_BOOL ) x_active
{
2007-11-24 10:55:21 -07:00
return _x _active ;
}
@ end
static NSArray *
2012-06-10 07:21:05 -06:00
array_with _strings _and _numbers ( int nitems , const char * * items ,
const char * numbers )
{
NSMutableArray * array , * subarray ;
NSString * string , * number ;
int i ;
/ * ( Can ' t autorelease on the X server thread ) * /
array = [ [ NSMutableArray alloc ] initWithCapacity : nitems ] ;
for ( i = 0 ; i < nitems ; i + + ) {
subarray = [ [ NSMutableArray alloc ] initWithCapacity : 2 ] ;
string = [ [ NSString alloc ] initWithUTF8String : items [ i ] ] ;
[ subarray addObject : string ] ;
[ string release ] ;
if ( numbers [ i ] ! = 0 ) {
number = [ [ NSString alloc ] initWithFormat : @ "%d" , numbers [ i ] ] ;
[ subarray addObject : number ] ;
[ number release ] ;
}
else
[ subarray addObject : @ "" ] ;
[ array addObject : subarray ] ;
[ subarray release ] ;
}
return array ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationSetWindowMenu ( int nitems , const char * * items ,
const char * shortcuts )
{
NSArray * array ;
array = array_with _strings _and _numbers ( nitems , items , shortcuts ) ;
/ * Send the array of strings over to the appkit thread * /
message_kit _thread ( @ selector ( set_window _menu : ) , array ) ;
[ array release ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationSetWindowMenuCheck ( int idx )
{
NSNumber * n ;
n = [ [ NSNumber alloc ] initWithInt : idx ] ;
message_kit _thread ( @ selector ( set_window _menu _check : ) , n ) ;
[ n release ] ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationSetFrontProcess ( void )
{
message_kit _thread ( @ selector ( set_front _process : ) , nil ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationSetCanQuit ( int state )
{
2007-11-24 10:55:21 -07:00
NSNumber * n ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
n = [ [ NSNumber alloc ] initWithBool : state ] ;
2012-06-10 07:21:05 -06:00
message_kit _thread ( @ selector ( set_can _quit : ) , n ) ;
2007-11-24 10:55:21 -07:00
[ n release ] ;
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationServerReady ( void )
{
message_kit _thread ( @ selector ( server_ready : ) , nil ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationShowHideMenubar ( int state )
{
2007-11-24 10:55:21 -07:00
NSNumber * n ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
n = [ [ NSNumber alloc ] initWithBool : state ] ;
2012-06-10 07:21:05 -06:00
message_kit _thread ( @ selector ( show_hide _menubar : ) , n ) ;
2007-11-24 10:55:21 -07:00
[ n release ] ;
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationLaunchClient ( const char * cmd )
{
2010-07-27 13:02:24 -06:00
NSString * string ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
string = [ [ NSString alloc ] initWithUTF8String : cmd ] ;
2012-06-10 07:21:05 -06:00
message_kit _thread ( @ selector ( launch_client : ) , string ) ;
2010-07-27 13:02:24 -06:00
[ string release ] ;
}
2011-04-02 10:08:38 -06:00
/ * This is a special function in that it is run from the * SERVER * thread and
* not the AppKit thread . We want to block entering a screen - capturing RandR
* mode until we notify the user about how to get out if the X11 client crashes .
* /
2012-06-10 07:21:05 -06:00
Bool
X11ApplicationCanEnterRandR ( void )
{
2011-04-02 10:08:38 -06:00
NSString * title , * msg ;
2012-06-10 07:21:05 -06:00
if ( [ X11App prefs_get _boolean : @ PREFS_NO _RANDR _ALERT default : NO ] ||
XQuartzShieldingWindowLevel ! = 0 )
2011-04-02 10:08:38 -06:00
return TRUE ;
2012-06-10 07:21:05 -06:00
title = NSLocalizedString ( @ "Enter RandR mode?" ,
@ "Dialog title when switching to RandR" ) ;
msg = NSLocalizedString (
@ "An application has requested X11 to change the resolution of your display. X11 will restore the display to its previous state when the requesting application requests to return to the previous state. Alternatively, you can use the ⌥⌘A key sequence to force X11 to return to the previous state." ,
@ "Dialog when switching to RandR" ) ;
if ( ! XQuartzIsRootless )
2011-04-02 10:08:38 -06:00
QuartzShowFullscreen ( FALSE ) ;
2015-11-07 09:48:51 -07:00
switch ( NSRunAlertPanel ( title , @ "%@" ,
2012-06-10 07:21:05 -06:00
NSLocalizedString ( @ "Allow" ,
@ "" ) ,
NSLocalizedString ( @ "Cancel" ,
@ "" ) ,
2015-11-07 09:48:51 -07:00
NSLocalizedString ( @ "Always Allow" , @ "" ) , msg ) ) {
2012-06-10 07:21:05 -06:00
case NSAlertOtherReturn :
[ X11App prefs_set _boolean : @ PREFS_NO _RANDR _ALERT value : YES ] ;
[ X11App prefs_synchronize ] ;
case NSAlertDefaultReturn :
return YES ;
default :
return NO ;
2011-04-02 10:08:38 -06:00
}
}
2012-06-10 07:21:05 -06:00
static void
check_xinitrc ( void )
{
2007-11-24 10:55:21 -07:00
char * tem , buf [ 1024 ] ;
NSString * msg ;
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
if ( [ X11App prefs_get _boolean : @ PREFS_DONE _XINIT _CHECK default : NO ] )
2012-06-10 07:21:05 -06:00
return ;
tem = getenv ( "HOME" ) ;
2007-11-24 10:55:21 -07:00
if ( tem = = NULL ) goto done ;
2012-06-10 07:21:05 -06:00
snprintf ( buf , sizeof ( buf ) , "%s/.xinitrc" , tem ) ;
if ( access ( buf , F_OK ) ! = 0 )
goto done ;
msg =
NSLocalizedString (
@ " You have an existing ~ /. xinitrc file . \ n \ n \
Windows displayed by X11 applications may not have titlebars , or may look \
different to windows displayed by native applications . \ n \ n \
Would you like to move aside the existing file and use the standard X11 \
environment the next time you start X11 ? " ,
@ "Startup xinitrc dialog" ) ;
if ( NSAlertDefaultReturn = =
2015-11-07 09:48:51 -07:00
NSRunAlertPanel ( nil , @ "%@" , NSLocalizedString ( @ "Yes" , @ "" ) ,
NSLocalizedString ( @ "No" , @ "" ) , nil , msg ) ) {
2009-09-06 13:44:18 -06:00
char buf2 [ 1024 ] ;
int i = -1 ;
2012-06-10 07:21:05 -06:00
snprintf ( buf2 , sizeof ( buf2 ) , "%s.old" , buf ) ;
for ( i = 1 ; access ( buf2 , F_OK ) = = 0 ; i + + )
snprintf ( buf2 , sizeof ( buf2 ) , "%s.old.%d" , buf , i ) ;
rename ( buf , buf2 ) ;
2007-11-24 10:55:21 -07:00
}
2012-06-10 07:21:05 -06:00
done :
2007-11-24 10:55:21 -07:00
[ X11App prefs_set _boolean : @ PREFS_DONE _XINIT _CHECK value : YES ] ;
[ X11App prefs_synchronize ] ;
}
2012-06-10 07:21:05 -06:00
static inline pthread_t
create_thread ( void * (*func)(void *) , void * arg )
{
2010-07-27 13:02:24 -06:00
pthread_attr _t attr ;
pthread_t tid ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
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 ) ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
return tid ;
}
2012-06-10 07:21:05 -06:00
static void *
xpbproxy_x _thread ( void * args )
{
2010-07-27 13:02:24 -06:00
xpbproxy_run ( ) ;
2011-11-05 07:32:40 -06:00
ErrorF ( "xpbproxy thread is terminating unexpectedly.\n" ) ;
2010-07-27 13:02:24 -06:00
return NULL ;
}
2012-06-10 07:21:05 -06:00
void
X11ApplicationMain ( int argc , char * * argv , char * * envp )
{
2007-11-24 10:55:21 -07:00
NSAutoreleasePool * pool ;
2008-11-02 08:26:08 -07:00
2007-11-24 10:55:21 -07:00
# ifdef DEBUG
2012-06-10 07:21:05 -06:00
while ( access ( "/tmp/x11-block" , F_OK ) = = 0 ) sleep ( 1 ) ;
2007-11-24 10:55:21 -07:00
# endif
2012-06-10 07:21:05 -06:00
2007-11-24 10:55:21 -07:00
pool = [ [ NSAutoreleasePool alloc ] init ] ;
2012-06-10 07:21:05 -06:00
X11App = ( X11Application * ) [ X11Application sharedApplication ] ;
init_ports ( ) ;
app_prefs _domain _cfstr =
( CFStringRef ) [ [ NSBundle mainBundle ] bundleIdentifier ] ;
2009-09-06 13:44:18 -06:00
2011-11-05 07:32:40 -06:00
if ( app_prefs _domain _cfstr = = NULL ) {
2012-06-10 07:21:05 -06:00
ErrorF (
"X11ApplicationMain: Unable to determine bundle identifier. Your installation of XQuartz may be broken.\n" ) ;
app_prefs _domain _cfstr = CFSTR ( BUNDLE_ID _PREFIX ".X11" ) ;
2011-11-05 07:32:40 -06:00
}
2007-11-24 10:55:21 -07:00
[ NSApp read_defaults ] ;
[ NSBundle loadNibNamed : @ "main" owner : NSApp ] ;
[ [ NSNotificationCenter defaultCenter ] addObserver : NSApp
2012-06-10 07:21:05 -06:00
selector : @ selector ( became_key : )
name :
NSWindowDidBecomeKeyNotification object : nil ] ;
2008-11-02 08:26:08 -07:00
2007-11-24 10:55:21 -07:00
/ *
* The xpr Quartz mode is statically linked into this server .
* Initialize all the Quartz functions .
* /
QuartzModeBundleInit ( ) ;
2008-11-02 08:26:08 -07:00
2007-11-24 10:55:21 -07:00
/ * Calculate the height of the menubar so we can avoid it . * /
2015-11-07 09:48:51 -07:00
aquaMenuBarHeight = [ [ NSApp mainMenu ] menuBarHeight ] ;
# if ! __LP64 __
if ( ! aquaMenuBarHeight ) {
aquaMenuBarHeight = [ NSMenuView menuBarHeight ] ;
}
# endif
if ( ! aquaMenuBarHeight ) {
NSScreen * primaryScreen = [ [ NSScreen screens ] objectAtIndex : 0 ] ;
aquaMenuBarHeight = NSHeight ( [ primaryScreen frame ] ) - NSMaxY ( [ primaryScreen visibleFrame ] ) ;
}
2008-11-02 08:26:08 -07:00
2011-11-05 07:32:40 -06:00
# ifdef HAVE_LIBDISPATCH
2012-06-10 07:21:05 -06:00
eventTranslationQueue = dispatch_queue _create (
BUNDLE_ID _PREFIX ".X11.NSEventsToX11EventsQueue" , NULL ) ;
2011-11-05 07:32:40 -06:00
assert ( eventTranslationQueue ! = NULL ) ;
# endif
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
/ * Set the key layout seed before we start the server * /
# if MAC_OS _X _VERSION _MIN _REQUIRED >= 1050
2012-06-10 07:21:05 -06:00
last_key _layout = TISCopyCurrentKeyboardLayoutInputSource ( ) ;
2008-11-02 08:26:08 -07:00
2012-06-10 07:21:05 -06:00
if ( ! last_key _layout )
ErrorF (
"X11ApplicationMain: Unable to determine TISCopyCurrentKeyboardLayoutInputSource() at startup.\n" ) ;
2009-09-06 13:44:18 -06:00
# else
KLGetCurrentKeyboardLayout ( & last_key _layout ) ;
2012-06-10 07:21:05 -06:00
if ( ! last_key _layout )
ErrorF (
"X11ApplicationMain: Unable to determine KLGetCurrentKeyboardLayout() at startup.\n" ) ;
2009-09-06 13:44:18 -06:00
# endif
2010-07-27 13:02:24 -06:00
if ( ! QuartsResyncKeymap ( FALSE ) ) {
2011-11-05 07:32:40 -06:00
ErrorF ( "X11ApplicationMain: Could not build a valid keymap.\n" ) ;
2009-09-06 13:44:18 -06:00
}
/ * Tell the server thread that it can proceed * /
QuartzInitServer ( argc , argv , envp ) ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
/ * This must be done after QuartzInitServer because it can result in
* an mieqEnqueue ( ) - < rdar : // problem / 6300249 >
* /
check_xinitrc ( ) ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
create_thread ( xpbproxy_x _thread , NULL ) ;
# if XQUARTZ_SPARKLE
[ [ X11App controller ] setup_sparkle ] ;
[ [ SUUpdater sharedUpdater ] resetUpdateCycle ] ;
2012-06-10 07:21:05 -06:00
// [ [ SUUpdater sharedUpdater ] checkForUpdates : X11App ] ;
2010-07-27 13:02:24 -06:00
# endif
[ pool release ] ;
2007-11-24 10:55:21 -07:00
[ NSApp run ] ;
2008-11-02 08:26:08 -07:00
/ * not reached * /
2007-11-24 10:55:21 -07:00
}
2009-09-06 13:44:18 -06:00
@ implementation X11Application ( Private )
# ifdef NX_DEVICELCMDKEYMASK
/ * This is to workaround a bug in the VNC server where we sometimes see the L
* modifier and sometimes see no "side"
* /
2012-06-10 07:21:05 -06:00
static inline int
ensure_flag ( int flags , int device_independent , int device_dependents ,
int device_dependent _default )
{
if ( ( flags & device_independent ) &&
! ( flags & device_dependents ) )
2009-09-06 13:44:18 -06:00
flags | = device_dependent _default ;
return flags ;
2007-11-24 10:55:21 -07:00
}
2009-09-06 13:44:18 -06:00
# endif
2007-11-24 10:55:21 -07:00
2011-11-05 07:32:40 -06:00
# ifdef DEBUG_UNTRUSTED _POINTER _DELTA
2012-06-10 07:21:05 -06:00
static const char *
untrusted_str ( NSEvent * e )
{
switch ( [ e type ] ) {
case NSScrollWheel :
return "NSScrollWheel" ;
case NSTabletPoint :
return "NSTabletPoint" ;
case NSOtherMouseDown :
return "NSOtherMouseDown" ;
case NSOtherMouseUp :
return "NSOtherMouseUp" ;
case NSLeftMouseDown :
return "NSLeftMouseDown" ;
case NSLeftMouseUp :
return "NSLeftMouseUp" ;
default :
switch ( [ e subtype ] ) {
case NSTabletPointEventSubtype :
return "NSTabletPointEventSubtype" ;
case NSTabletProximityEventSubtype :
return "NSTabletProximityEventSubtype" ;
2011-11-05 07:32:40 -06:00
default :
2012-06-10 07:21:05 -06:00
return "Other" ;
}
2011-11-05 07:32:40 -06:00
}
}
# endif
2012-06-10 07:21:05 -06:00
extern void
2017-12-08 08:01:59 -07:00
wait_for _mieq _init ( void ) ;
2012-06-10 07:21:05 -06:00
- ( void ) sendX11NSEvent : ( NSEvent * ) e
{
2011-11-05 07:32:40 -06:00
NSPoint location = NSZeroPoint ;
2009-09-06 13:44:18 -06:00
int ev_button , ev_type ;
2011-11-05 07:32:40 -06:00
static float pressure = 0.0 ; // static so ProximityOut will have the value from the previous tablet event
static NSPoint tilt ; // static so ProximityOut will have the value from the previous tablet event
static DeviceIntPtr darwinTabletCurrent = NULL ;
static BOOL needsProximityIn = NO ; // Do we do need to handle a pending ProximityIn once we have pressure / tilt ?
2009-09-06 13:44:18 -06:00
DeviceIntPtr pDev ;
int modifierFlags ;
2010-07-27 13:02:24 -06:00
BOOL isMouseOrTabletEvent , isTabletEvent ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( ! darwinTabletCurrent ) {
/ * Ensure that the event system is initialized * /
2017-12-08 08:01:59 -07:00
wait_for _mieq _init ( ) ;
2012-06-10 07:21:05 -06:00
assert ( darwinTabletStylus ) ;
2011-11-05 07:32:40 -06:00
tilt = NSZeroPoint ;
darwinTabletCurrent = darwinTabletStylus ;
}
2012-06-10 07:21:05 -06:00
isMouseOrTabletEvent = [ e type ] = = NSLeftMouseDown ||
[ e type ] = = NSOtherMouseDown ||
[ e type ] = = NSRightMouseDown ||
[ e type ] = = NSLeftMouseUp ||
[ e type ] = = NSOtherMouseUp ||
[ e type ] = = NSRightMouseUp ||
[ e type ] = = NSLeftMouseDragged ||
[ e type ] = = NSOtherMouseDragged ||
[ e type ] = = NSRightMouseDragged ||
[ e type ] = = NSMouseMoved ||
[ e type ] = = NSTabletPoint ||
[ e type ] = = NSScrollWheel ;
2009-09-06 13:44:18 -06:00
2010-07-27 13:02:24 -06:00
isTabletEvent = ( [ e type ] = = NSTabletPoint ) ||
2012-06-10 07:21:05 -06:00
( isMouseOrTabletEvent &&
( [ e subtype ] = = NSTabletPointEventSubtype ||
[ e subtype ] = = NSTabletProximityEventSubtype ) ) ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( isMouseOrTabletEvent ) {
2010-07-27 13:02:24 -06:00
static NSPoint lastpt ;
NSWindow * window = [ e window ] ;
NSRect screen = [ [ [ NSScreen screens ] objectAtIndex : 0 ] frame ] ;
2012-06-10 07:21:05 -06:00
BOOL hasUntrustedPointerDelta ;
2010-07-27 13:02:24 -06:00
// NSEvents for tablets are not consistent wrt deltaXY between events , so we cannot rely on that
// Thus tablets will be subject to the warp - pointer bug worked around by the delta , but tablets
// are not normally used in cases where that bug would present itself , so this is a fair tradeoff
// < rdar : // problem / 7111003 > deltaX and deltaY are incorrect for NSMouseMoved , NSTabletPointEventSubtype
// http : // xquartz . macosforge . org / trac / ticket / 288
hasUntrustedPointerDelta = isTabletEvent ;
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
// The deltaXY for middle click events also appear erroneous after fast user switching
// < rdar : // problem / 7979468 > deltaX and deltaY are incorrect for NSOtherMouseDown and NSOtherMouseUp after FUS
// http : // xquartz . macosforge . org / trac / ticket / 389
2012-06-10 07:21:05 -06:00
hasUntrustedPointerDelta | = [ e type ] = = NSOtherMouseDown ||
[ e type ] = = NSOtherMouseUp ;
2010-07-27 13:02:24 -06:00
// The deltaXY for scroll events correspond to the scroll delta , not the pointer delta
// < rdar : // problem / 7989690 > deltaXY for wheel events are being sent as mouse movement
2012-06-10 07:21:05 -06:00
hasUntrustedPointerDelta | = [ e type ] = = NSScrollWheel ;
2011-11-05 07:32:40 -06:00
# ifdef DEBUG_UNTRUSTED _POINTER _DELTA
2012-06-10 07:21:05 -06:00
hasUntrustedPointerDelta | = [ e type ] = = NSLeftMouseDown ||
[ e type ] = = NSLeftMouseUp ;
2011-11-05 07:32:40 -06:00
# endif
2012-06-10 07:21:05 -06:00
if ( window ! = nil ) {
2010-07-27 13:02:24 -06:00
NSRect frame = [ window frame ] ;
location = [ e locationInWindow ] ;
location . x + = frame . origin . x ;
location . y + = frame . origin . y ;
lastpt = location ;
2012-06-10 07:21:05 -06:00
}
else if ( hasUntrustedPointerDelta ) {
2011-11-05 07:32:40 -06:00
# ifdef DEBUG_UNTRUSTED _POINTER _DELTA
ErrorF ( "--- Begin Event Debug ---\n" ) ;
ErrorF ( "Event type: %s\n" , untrusted_str ( e ) ) ;
ErrorF ( "old lastpt: (%0.2f, %0.2f)\n" , lastpt . x , lastpt . y ) ;
ErrorF ( " delta: (%0.2f, %0.2f)\n" , [ e deltaX ] , - [ e deltaY ] ) ;
2012-06-10 07:21:05 -06:00
ErrorF ( " location: (%0.2f, %0.2f)\n" , lastpt . x + [ e deltaX ] ,
lastpt . y - [ e deltaY ] ) ;
ErrorF ( "workaround: (%0.2f, %0.2f)\n" , [ e locationInWindow ] . x ,
[ e locationInWindow ] . y ) ;
2011-11-05 07:32:40 -06:00
ErrorF ( "--- End Event Debug ---\n" ) ;
location . x = lastpt . x + [ e deltaX ] ;
location . y = lastpt . y - [ e deltaY ] ;
lastpt = [ e locationInWindow ] ;
# else
2010-07-27 13:02:24 -06:00
location = [ e locationInWindow ] ;
lastpt = location ;
2011-11-05 07:32:40 -06:00
# endif
2012-06-10 07:21:05 -06:00
}
else {
2010-07-27 13:02:24 -06:00
location . x = lastpt . x + [ e deltaX ] ;
location . y = lastpt . y - [ e deltaY ] ;
lastpt = [ e locationInWindow ] ;
}
2012-06-10 07:21:05 -06:00
2010-07-27 13:02:24 -06:00
/ * Convert coordinate system * /
location . y = ( screen . origin . y + screen . size . height ) - location . y ;
}
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
modifierFlags = [ e modifierFlags ] ;
2012-06-10 07:21:05 -06:00
2009-09-06 13:44:18 -06:00
# ifdef NX_DEVICELCMDKEYMASK
/ * This is to workaround a bug in the VNC server where we sometimes see the L
* modifier and sometimes see no "side"
* /
2012-06-10 07:21:05 -06:00
modifierFlags = ensure_flag ( modifierFlags , NX_CONTROLMASK ,
NX_DEVICELCTLKEYMASK | NX_DEVICERCTLKEYMASK ,
NX_DEVICELCTLKEYMASK ) ;
modifierFlags = ensure_flag ( modifierFlags , NX_SHIFTMASK ,
NX_DEVICELSHIFTKEYMASK | NX_DEVICERSHIFTKEYMASK ,
NX_DEVICELSHIFTKEYMASK ) ;
modifierFlags = ensure_flag ( modifierFlags , NX_COMMANDMASK ,
NX_DEVICELCMDKEYMASK | NX_DEVICERCMDKEYMASK ,
NX_DEVICELCMDKEYMASK ) ;
modifierFlags = ensure_flag ( modifierFlags , NX_ALTERNATEMASK ,
NX_DEVICELALTKEYMASK | NX_DEVICERALTKEYMASK ,
NX_DEVICELALTKEYMASK ) ;
2009-09-06 13:44:18 -06:00
# endif
2010-07-27 13:02:24 -06:00
modifierFlags & = darwin_all _modifier _mask ;
2007-11-24 10:55:21 -07:00
2009-09-06 13:44:18 -06:00
/ * We don ' t receive modifier key events while out of focus , and 3 button
* emulation mucks this up , so we need to check our modifier flag state
* on every event . . . ugg
* /
2012-06-10 07:21:05 -06:00
if ( darwin_all _modifier _flags ! = modifierFlags )
2009-09-06 13:44:18 -06:00
DarwinUpdateModKeys ( modifierFlags ) ;
2012-06-10 07:21:05 -06:00
switch ( [ e type ] ) {
case NSLeftMouseDown :
ev_button = 1 ;
ev_type = ButtonPress ;
goto handle_mouse ;
case NSOtherMouseDown :
ev_button = 2 ;
ev_type = ButtonPress ;
goto handle_mouse ;
case NSRightMouseDown :
ev_button = 3 ;
ev_type = ButtonPress ;
goto handle_mouse ;
case NSLeftMouseUp :
ev_button = 1 ;
ev_type = ButtonRelease ;
goto handle_mouse ;
case NSOtherMouseUp :
ev_button = 2 ;
ev_type = ButtonRelease ;
goto handle_mouse ;
case NSRightMouseUp :
ev_button = 3 ;
ev_type = ButtonRelease ;
goto handle_mouse ;
case NSLeftMouseDragged :
ev_button = 1 ;
ev_type = MotionNotify ;
goto handle_mouse ;
case NSOtherMouseDragged :
ev_button = 2 ;
ev_type = MotionNotify ;
goto handle_mouse ;
case NSRightMouseDragged :
ev_button = 3 ;
ev_type = MotionNotify ;
goto handle_mouse ;
case NSMouseMoved :
ev_button = 0 ;
ev_type = MotionNotify ;
goto handle_mouse ;
case NSTabletPoint :
ev_button = 0 ;
ev_type = MotionNotify ;
goto handle_mouse ;
handle_mouse :
pDev = darwinPointer ;
/ * NSTabletPoint can have no subtype * /
if ( [ e type ] ! = NSTabletPoint &&
[ e subtype ] = = NSTabletProximityEventSubtype ) {
switch ( [ e pointingDeviceType ] ) {
case NSEraserPointingDevice :
darwinTabletCurrent = darwinTabletEraser ;
break ;
case NSPenPointingDevice :
darwinTabletCurrent = darwinTabletStylus ;
break ;
case NSCursorPointingDevice :
case NSUnknownPointingDevice :
default :
darwinTabletCurrent = darwinTabletCursor ;
break ;
2009-09-06 13:44:18 -06:00
}
2012-06-10 07:21:05 -06:00
if ( [ e isEnteringProximity ] )
needsProximityIn = YES ;
else
DarwinSendTabletEvents ( darwinTabletCurrent , ProximityOut , 0 ,
location . x , location . y , pressure ,
tilt . x , tilt . y ) ;
return ;
}
if ( [ e type ] = = NSTabletPoint ||
[ e subtype ] = = NSTabletPointEventSubtype ) {
pressure = [ e pressure ] ;
tilt = [ e tilt ] ;
pDev = darwinTabletCurrent ;
if ( needsProximityIn ) {
DarwinSendTabletEvents ( darwinTabletCurrent , ProximityIn , 0 ,
location . x , location . y , pressure ,
tilt . x , tilt . y ) ;
needsProximityIn = NO ;
2009-09-06 13:44:18 -06:00
}
2012-06-10 07:21:05 -06:00
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
if ( ! XQuartzServerVisible && noTestExtensions ) {
2010-07-27 13:02:24 -06:00
# if defined ( XPLUGIN_VERSION ) && XPLUGIN_VERSION > 0
2012-06-10 07:21:05 -06:00
/ * Older libXplugin ( Tiger / "Stock" Leopard ) aren ' t thread safe , so we can ' t call xp_find _window from the Appkit thread * /
xp_window _id wid = 0 ;
xp_error err ;
/ * Sigh . Need to check that we ' re really over one of
* our windows . ( We need to receive pointer events while
* not in the foreground , but we don ' t want to receive them
* when another window is over us or we might show a tooltip )
* /
2010-07-27 13:02:24 -06:00
2012-06-10 07:21:05 -06:00
err = xp_find _window ( location . x , location . y , 0 , & wid ) ;
2010-07-27 13:02:24 -06:00
2012-06-10 07:21:05 -06:00
if ( err ! = XP_Success || ( err = = XP_Success && wid = = 0 ) )
2009-09-06 13:44:18 -06:00
# endif
2012-06-10 07:21:05 -06:00
{
bgMouseLocation = location ;
bgMouseLocationUpdated = TRUE ;
return ;
2010-07-27 13:02:24 -06:00
}
2012-06-10 07:21:05 -06:00
}
if ( bgMouseLocationUpdated ) {
if ( ! ( ev_type = = MotionNotify && ev_button = = 0 ) ) {
DarwinSendPointerEvents ( darwinPointer , MotionNotify , 0 ,
location . x , location . y ,
0.0 , 0.0 ) ;
2010-07-27 13:02:24 -06:00
}
2012-06-10 07:21:05 -06:00
bgMouseLocationUpdated = FALSE ;
}
2010-07-27 13:02:24 -06:00
2012-06-10 07:21:05 -06:00
if ( pDev = = darwinPointer ) {
DarwinSendPointerEvents ( pDev , ev_type , ev_button ,
location . x , location . y ,
[ e deltaX ] , [ e deltaY ] ) ;
} else {
DarwinSendTabletEvents ( pDev , ev_type , ev_button ,
location . x , location . y , pressure ,
tilt . x , tilt . y ) ;
}
break ;
case NSTabletProximity :
switch ( [ e pointingDeviceType ] ) {
case NSEraserPointingDevice :
darwinTabletCurrent = darwinTabletEraser ;
2009-09-06 13:44:18 -06:00
break ;
2012-06-10 07:21:05 -06:00
case NSPenPointingDevice :
darwinTabletCurrent = darwinTabletStylus ;
break ;
case NSCursorPointingDevice :
case NSUnknownPointingDevice :
default :
darwinTabletCurrent = darwinTabletCursor ;
2009-09-06 13:44:18 -06:00
break ;
2012-06-10 07:21:05 -06:00
}
if ( [ e isEnteringProximity ] )
needsProximityIn = YES ;
else
DarwinSendTabletEvents ( darwinTabletCurrent , ProximityOut , 0 ,
location . x , location . y , pressure ,
tilt . x , tilt . y ) ;
break ;
case NSScrollWheel :
{
# if MAC_OS _X _VERSION _MAX _ALLOWED < 1050
float deltaX = [ e deltaX ] ;
float deltaY = [ e deltaY ] ;
BOOL isContinuous = NO ;
# else
CGFloat deltaX = [ e deltaX ] ;
CGFloat deltaY = [ e deltaY ] ;
CGEventRef cge = [ e CGEvent ] ;
BOOL isContinuous =
CGEventGetIntegerValueField ( cge , kCGScrollWheelEventIsContinuous ) ;
# if 0
/ * Scale the scroll value by line height * /
CGEventSourceRef source = CGEventCreateSourceFromEvent ( cge ) ;
if ( source ) {
double lineHeight = CGEventSourceGetPixelsPerLine ( source ) ;
CFRelease ( source ) ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
/ * There ' s no real reason for the 1 / 5 ratio here other than that
* it feels like a good ratio after some testing .
* /
deltaX * = lineHeight / 5.0 ;
deltaY * = lineHeight / 5.0 ;
}
# endif
# endif
2010-07-27 13:02:24 -06:00
# if ! defined ( XPLUGIN_VERSION ) || XPLUGIN_VERSION = = 0
2012-06-10 07:21:05 -06:00
/ * If we ' re in the background , we need to send a MotionNotify event
* first , since we aren ' t getting them on background mouse motion
* /
if ( ! XQuartzServerVisible && noTestExtensions ) {
bgMouseLocationUpdated = FALSE ;
DarwinSendPointerEvents ( darwinPointer , MotionNotify , 0 ,
location . x , location . y ,
0.0 , 0.0 ) ;
}
2010-07-27 13:02:24 -06:00
# endif
2011-11-05 07:32:40 -06:00
# if MAC_OS _X _VERSION _MAX _ALLOWED >= 1070
2012-06-10 07:21:05 -06:00
// TODO : Change 1117 to NSAppKitVersionNumber10_7 when it is defined
if ( NSAppKitVersionNumber >= 1117 &&
XQuartzScrollInDeviceDirection &&
[ e isDirectionInvertedFromDevice ] ) {
deltaX * = -1 ;
deltaY * = -1 ;
}
2011-11-05 07:32:40 -06:00
# endif
2012-06-10 07:21:05 -06:00
/ * This hack is in place to better deal with "clicky" scroll wheels :
* http : // xquartz . macosforge . org / trac / ticket / 562
* /
if ( ! isContinuous ) {
static NSTimeInterval lastScrollTime = 0.0 ;
/ * These store how much extra we have already scrolled .
* ie , this is how much we ignore on the next event .
* /
static double deficit_x = 0.0 ;
static double deficit_y = 0.0 ;
/ * If we have past a second since the last scroll , wipe the slate
* clean
* /
if ( [ e timestamp ] - lastScrollTime > 1.0 ) {
deficit_x = deficit_y = 0.0 ;
2011-11-05 07:32:40 -06:00
}
2012-06-10 07:21:05 -06:00
lastScrollTime = [ e timestamp ] ;
if ( deltaX ! = 0.0 ) {
/ * If we changed directions , wipe the slate clean * /
if ( ( deficit_x < 0.0 && deltaX > 0.0 ) ||
( deficit_x > 0.0 && deltaX < 0.0 ) ) {
deficit_x = 0.0 ;
}
/ * Eat up the deficit , but ensure that something is
* always sent
2010-07-27 13:02:24 -06:00
* /
2012-06-10 07:21:05 -06:00
if ( fabs ( deltaX ) > fabs ( deficit_x ) ) {
deltaX - = deficit_x ;
if ( deltaX > 0.0 ) {
deficit_x = ceil ( deltaX ) - deltaX ;
deltaX = ceil ( deltaX ) ;
} else {
deficit_x = floor ( deltaX ) - deltaX ;
deltaX = floor ( deltaX ) ;
}
} else {
deficit_x - = deltaX ;
if ( deltaX > 0.0 ) {
deltaX = 1.0 ;
} else {
deltaX = -1.0 ;
}
deficit_x + = deltaX ;
2010-07-27 13:02:24 -06:00
}
}
2012-06-10 07:21:05 -06:00
if ( deltaY ! = 0.0 ) {
/ * If we changed directions , wipe the slate clean * /
if ( ( deficit_y < 0.0 && deltaY > 0.0 ) ||
( deficit_y > 0.0 && deltaY < 0.0 ) ) {
deficit_y = 0.0 ;
}
/ * Eat up the deficit , but ensure that something is
* always sent
* /
if ( fabs ( deltaY ) > fabs ( deficit_y ) ) {
deltaY - = deficit_y ;
if ( deltaY > 0.0 ) {
deficit_y = ceil ( deltaY ) - deltaY ;
deltaY = ceil ( deltaY ) ;
} else {
deficit_y = floor ( deltaY ) - deltaY ;
deltaY = floor ( deltaY ) ;
}
2009-09-06 13:44:18 -06:00
} else {
2012-06-10 07:21:05 -06:00
deficit_y - = deltaY ;
if ( deltaY > 0.0 ) {
deltaY = 1.0 ;
} else {
deltaY = -1.0 ;
}
deficit_y + = deltaY ;
}
}
}
DarwinSendScrollEvents ( deltaX , deltaY ) ;
break ;
}
case NSKeyDown :
case NSKeyUp :
{
/ * XKB clobbers our keymap at startup , so we need to force it on the first keypress .
* TODO : Make this less of a kludge .
* /
static int force_resync _keymap = YES ;
if ( force_resync _keymap ) {
DarwinSendDDXEvent ( kXquartzReloadKeymap , 0 ) ;
force_resync _keymap = NO ;
}
}
if ( darwinSyncKeymap ) {
# if MAC_OS _X _VERSION _MIN _REQUIRED >= 1050
TISInputSourceRef key_layout =
TISCopyCurrentKeyboardLayoutInputSource ( ) ;
TISInputSourceRef clear ;
if ( CFEqual ( key_layout , last_key _layout ) ) {
CFRelease ( key_layout ) ;
}
else {
/ * Swap / free thread - safely * /
clear = last_key _layout ;
last_key _layout = key_layout ;
CFRelease ( clear ) ;
2009-09-06 13:44:18 -06:00
# else
2012-06-10 07:21:05 -06:00
KeyboardLayoutRef key_layout ;
KLGetCurrentKeyboardLayout ( & key_layout ) ;
if ( key_layout ! = last_key _layout ) {
last_key _layout = key_layout ;
2009-09-06 13:44:18 -06:00
# endif
2012-06-10 07:21:05 -06:00
/ * Update keyInfo * /
if ( ! QuartsResyncKeymap ( TRUE ) ) {
ErrorF (
"sendX11NSEvent: Could not build a valid keymap.\n" ) ;
2009-09-06 13:44:18 -06:00
}
}
2012-06-10 07:21:05 -06:00
}
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
ev_type = ( [ e type ] = = NSKeyDown ) ? KeyPress : KeyRelease ;
DarwinSendKeyboardEvents ( ev_type , [ e keyCode ] ) ;
break ;
2009-09-06 13:44:18 -06:00
2012-06-10 07:21:05 -06:00
default :
break ; / * for gcc * /
}
2007-11-24 10:55:21 -07:00
}
2009-09-06 13:44:18 -06:00
@ end