winemac: Work around mouse "pausing" after CGWarpMouseCursorPosition().
[wine] / dlls / winemac.drv / cocoa_app.m
1 /*
2  * MACDRV Cocoa application class
3  *
4  * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #import <Carbon/Carbon.h>
22 #include <dlfcn.h>
23
24 #import "cocoa_app.h"
25 #import "cocoa_event.h"
26 #import "cocoa_window.h"
27
28
29 static NSString* const WineAppWaitQueryResponseMode = @"WineAppWaitQueryResponseMode";
30
31
32 int macdrv_err_on;
33
34
35 @implementation WineApplication
36
37 @synthesize wineController;
38
39     - (void) sendEvent:(NSEvent*)anEvent
40     {
41         if (![wineController handleEvent:anEvent])
42         {
43             [super sendEvent:anEvent];
44             [wineController didSendEvent:anEvent];
45         }
46     }
47
48     - (void) setWineController:(WineApplicationController*)newController
49     {
50         wineController = newController;
51         [self setDelegate:wineController];
52     }
53
54 @end
55
56
57 @interface WarpRecord : NSObject
58 {
59     CGEventTimestamp timeBefore, timeAfter;
60     CGPoint from, to;
61 }
62
63 @property (nonatomic) CGEventTimestamp timeBefore;
64 @property (nonatomic) CGEventTimestamp timeAfter;
65 @property (nonatomic) CGPoint from;
66 @property (nonatomic) CGPoint to;
67
68 @end
69
70
71 @implementation WarpRecord
72
73 @synthesize timeBefore, timeAfter, from, to;
74
75 @end;
76
77
78 @interface WineApplicationController ()
79
80 @property (readwrite, copy, nonatomic) NSEvent* lastFlagsChanged;
81 @property (copy, nonatomic) NSArray* cursorFrames;
82 @property (retain, nonatomic) NSTimer* cursorTimer;
83 @property (retain, nonatomic) NSImage* applicationIcon;
84 @property (readonly, nonatomic) BOOL inputSourceIsInputMethod;
85
86     - (void) setupObservations;
87     - (void) applicationDidBecomeActive:(NSNotification *)notification;
88
89     static void PerformRequest(void *info);
90
91 @end
92
93
94 @implementation WineApplicationController
95
96     @synthesize keyboardType, lastFlagsChanged;
97     @synthesize orderedWineWindows, applicationIcon;
98     @synthesize cursorFrames, cursorTimer;
99
100     + (void) initialize
101     {
102         if (self == [WineApplicationController class])
103         {
104             NSDictionary* defaults = [NSDictionary dictionaryWithObjectsAndKeys:
105                                       @"", @"NSQuotedKeystrokeBinding",
106                                       @"", @"NSRepeatCountBinding",
107                                       [NSNumber numberWithBool:NO], @"ApplePressAndHoldEnabled",
108                                       nil];
109             [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
110         }
111     }
112
113     + (WineApplicationController*) sharedController
114     {
115         static WineApplicationController* sharedController;
116         static dispatch_once_t once;
117
118         dispatch_once(&once, ^{
119             sharedController = [[self alloc] init];
120         });
121
122         return sharedController;
123     }
124
125     - (id) init
126     {
127         self = [super init];
128         if (self != nil)
129         {
130             CFRunLoopSourceContext context = { 0 };
131             context.perform = PerformRequest;
132             requestSource = CFRunLoopSourceCreate(NULL, 0, &context);
133             if (!requestSource)
134             {
135                 [self release];
136                 return nil;
137             }
138             CFRunLoopAddSource(CFRunLoopGetMain(), requestSource, kCFRunLoopCommonModes);
139             CFRunLoopAddSource(CFRunLoopGetMain(), requestSource, (CFStringRef)WineAppWaitQueryResponseMode);
140
141             requests =  [[NSMutableArray alloc] init];
142             requestsManipQueue = dispatch_queue_create("org.winehq.WineAppRequestManipQueue", NULL);
143
144             eventQueues = [[NSMutableArray alloc] init];
145             eventQueuesLock = [[NSLock alloc] init];
146
147             keyWindows = [[NSMutableArray alloc] init];
148             orderedWineWindows = [[NSMutableArray alloc] init];
149
150             originalDisplayModes = [[NSMutableDictionary alloc] init];
151
152             warpRecords = [[NSMutableArray alloc] init];
153
154             if (!requests || !requestsManipQueue || !eventQueues || !eventQueuesLock ||
155                 !keyWindows || !orderedWineWindows || !originalDisplayModes || !warpRecords)
156             {
157                 [self release];
158                 return nil;
159             }
160
161             [self setupObservations];
162
163             keyboardType = LMGetKbdType();
164
165             if ([NSApp isActive])
166                 [self applicationDidBecomeActive:nil];
167         }
168         return self;
169     }
170
171     - (void) dealloc
172     {
173         [applicationIcon release];
174         [warpRecords release];
175         [cursorTimer release];
176         [cursorFrames release];
177         [originalDisplayModes release];
178         [orderedWineWindows release];
179         [keyWindows release];
180         [eventQueues release];
181         [eventQueuesLock release];
182         if (requestsManipQueue) dispatch_release(requestsManipQueue);
183         [requests release];
184         if (requestSource)
185         {
186             CFRunLoopSourceInvalidate(requestSource);
187             CFRelease(requestSource);
188         }
189         [super dealloc];
190     }
191
192     - (void) transformProcessToForeground
193     {
194         if ([NSApp activationPolicy] != NSApplicationActivationPolicyRegular)
195         {
196             NSMenu* mainMenu;
197             NSMenu* submenu;
198             NSString* bundleName;
199             NSString* title;
200             NSMenuItem* item;
201
202             [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
203             [NSApp activateIgnoringOtherApps:YES];
204
205             mainMenu = [[[NSMenu alloc] init] autorelease];
206
207             submenu = [[[NSMenu alloc] initWithTitle:@"Wine"] autorelease];
208             bundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleNameKey];
209             if ([bundleName length])
210                 title = [NSString stringWithFormat:@"Quit %@", bundleName];
211             else
212                 title = @"Quit";
213             item = [submenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
214             [item setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
215             item = [[[NSMenuItem alloc] init] autorelease];
216             [item setTitle:@"Wine"];
217             [item setSubmenu:submenu];
218             [mainMenu addItem:item];
219
220             submenu = [[[NSMenu alloc] initWithTitle:@"Window"] autorelease];
221             [submenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
222             [submenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
223             [submenu addItem:[NSMenuItem separatorItem]];
224             [submenu addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
225             item = [[[NSMenuItem alloc] init] autorelease];
226             [item setTitle:@"Window"];
227             [item setSubmenu:submenu];
228             [mainMenu addItem:item];
229
230             [NSApp setMainMenu:mainMenu];
231             [NSApp setWindowsMenu:submenu];
232
233             [NSApp setApplicationIconImage:self.applicationIcon];
234         }
235     }
236
237     - (BOOL) waitUntilQueryDone:(int*)done timeout:(NSDate*)timeout processEvents:(BOOL)processEvents
238     {
239         PerformRequest(NULL);
240
241         do
242         {
243             if (processEvents)
244             {
245                 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
246                 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
247                                                     untilDate:timeout
248                                                        inMode:NSDefaultRunLoopMode
249                                                       dequeue:YES];
250                 if (event)
251                     [NSApp sendEvent:event];
252                 [pool release];
253             }
254             else
255                 [[NSRunLoop currentRunLoop] runMode:WineAppWaitQueryResponseMode beforeDate:timeout];
256         } while (!*done && [timeout timeIntervalSinceNow] >= 0);
257
258         return *done;
259     }
260
261     - (BOOL) registerEventQueue:(WineEventQueue*)queue
262     {
263         [eventQueuesLock lock];
264         [eventQueues addObject:queue];
265         [eventQueuesLock unlock];
266         return TRUE;
267     }
268
269     - (void) unregisterEventQueue:(WineEventQueue*)queue
270     {
271         [eventQueuesLock lock];
272         [eventQueues removeObjectIdenticalTo:queue];
273         [eventQueuesLock unlock];
274     }
275
276     - (void) computeEventTimeAdjustmentFromTicks:(unsigned long long)tickcount uptime:(uint64_t)uptime_ns
277     {
278         eventTimeAdjustment = (tickcount / 1000.0) - (uptime_ns / (double)NSEC_PER_SEC);
279     }
280
281     - (double) ticksForEventTime:(NSTimeInterval)eventTime
282     {
283         return (eventTime + eventTimeAdjustment) * 1000;
284     }
285
286     /* Invalidate old focus offers across all queues. */
287     - (void) invalidateGotFocusEvents
288     {
289         WineEventQueue* queue;
290
291         windowFocusSerial++;
292
293         [eventQueuesLock lock];
294         for (queue in eventQueues)
295         {
296             [queue discardEventsMatchingMask:event_mask_for_type(WINDOW_GOT_FOCUS)
297                                    forWindow:nil];
298         }
299         [eventQueuesLock unlock];
300     }
301
302     - (void) windowGotFocus:(WineWindow*)window
303     {
304         macdrv_event* event;
305
306         [self invalidateGotFocusEvents];
307
308         event = macdrv_create_event(WINDOW_GOT_FOCUS, window);
309         event->window_got_focus.serial = windowFocusSerial;
310         if (triedWindows)
311             event->window_got_focus.tried_windows = [triedWindows retain];
312         else
313             event->window_got_focus.tried_windows = [[NSMutableSet alloc] init];
314         [window.queue postEvent:event];
315         macdrv_release_event(event);
316     }
317
318     - (void) windowRejectedFocusEvent:(const macdrv_event*)event
319     {
320         if (event->window_got_focus.serial == windowFocusSerial)
321         {
322             triedWindows = (NSMutableSet*)event->window_got_focus.tried_windows;
323             [triedWindows addObject:(WineWindow*)event->window];
324             for (NSWindow* window in [keyWindows arrayByAddingObjectsFromArray:[self orderedWineWindows]])
325             {
326                 if (![triedWindows containsObject:window] && [window canBecomeKeyWindow])
327                 {
328                     [window makeKeyWindow];
329                     break;
330                 }
331             }
332             triedWindows = nil;
333         }
334     }
335
336     - (void) keyboardSelectionDidChange
337     {
338         TISInputSourceRef inputSource;
339
340         inputSourceIsInputMethodValid = FALSE;
341
342         inputSource = TISCopyCurrentKeyboardLayoutInputSource();
343         if (inputSource)
344         {
345             CFDataRef uchr;
346             uchr = TISGetInputSourceProperty(inputSource,
347                     kTISPropertyUnicodeKeyLayoutData);
348             if (uchr)
349             {
350                 macdrv_event* event;
351                 WineEventQueue* queue;
352
353                 event = macdrv_create_event(KEYBOARD_CHANGED, nil);
354                 event->keyboard_changed.keyboard_type = self.keyboardType;
355                 event->keyboard_changed.iso_keyboard = (KBGetLayoutType(self.keyboardType) == kKeyboardISO);
356                 event->keyboard_changed.uchr = CFDataCreateCopy(NULL, uchr);
357
358                 if (event->keyboard_changed.uchr)
359                 {
360                     [eventQueuesLock lock];
361
362                     for (queue in eventQueues)
363                         [queue postEvent:event];
364
365                     [eventQueuesLock unlock];
366                 }
367
368                 macdrv_release_event(event);
369             }
370
371             CFRelease(inputSource);
372         }
373     }
374
375     - (CGFloat) primaryScreenHeight
376     {
377         if (!primaryScreenHeightValid)
378         {
379             NSArray* screens = [NSScreen screens];
380             if ([screens count])
381             {
382                 primaryScreenHeight = NSHeight([[screens objectAtIndex:0] frame]);
383                 primaryScreenHeightValid = TRUE;
384             }
385             else
386                 return 1280; /* arbitrary value */
387         }
388
389         return primaryScreenHeight;
390     }
391
392     - (NSPoint) flippedMouseLocation:(NSPoint)point
393     {
394         /* This relies on the fact that Cocoa's mouse location points are
395            actually off by one (precisely because they were flipped from
396            Quartz screen coordinates using this same technique). */
397         point.y = [self primaryScreenHeight] - point.y;
398         return point;
399     }
400
401     - (void) flipRect:(NSRect*)rect
402     {
403         // We don't use -primaryScreenHeight here so there's no chance of having
404         // out-of-date cached info.  This method is called infrequently enough
405         // that getting the screen height each time is not prohibitively expensive.
406         rect->origin.y = NSMaxY([[[NSScreen screens] objectAtIndex:0] frame]) - NSMaxY(*rect);
407     }
408
409     - (void) wineWindow:(WineWindow*)window
410                 ordered:(NSWindowOrderingMode)order
411              relativeTo:(WineWindow*)otherWindow
412     {
413         NSUInteger index;
414
415         switch (order)
416         {
417             case NSWindowAbove:
418                 [window retain];
419                 [orderedWineWindows removeObjectIdenticalTo:window];
420                 if (otherWindow)
421                 {
422                     index = [orderedWineWindows indexOfObjectIdenticalTo:otherWindow];
423                     if (index == NSNotFound)
424                         index = 0;
425                 }
426                 else
427                 {
428                     index = 0;
429                     for (otherWindow in orderedWineWindows)
430                     {
431                         if ([otherWindow levelWhenActive] <= [window levelWhenActive])
432                             break;
433                         index++;
434                     }
435                 }
436                 [orderedWineWindows insertObject:window atIndex:index];
437                 [window release];
438                 break;
439             case NSWindowBelow:
440                 [window retain];
441                 [orderedWineWindows removeObjectIdenticalTo:window];
442                 if (otherWindow)
443                 {
444                     index = [orderedWineWindows indexOfObjectIdenticalTo:otherWindow];
445                     if (index == NSNotFound)
446                         index = [orderedWineWindows count];
447                 }
448                 else
449                 {
450                     index = 0;
451                     for (otherWindow in orderedWineWindows)
452                     {
453                         if ([otherWindow levelWhenActive] < [window levelWhenActive])
454                             break;
455                         index++;
456                     }
457                 }
458                 [orderedWineWindows insertObject:window atIndex:index];
459                 [window release];
460                 break;
461             case NSWindowOut:
462             default:
463                 break;
464         }
465     }
466
467     - (void) sendDisplaysChanged:(BOOL)activating
468     {
469         macdrv_event* event;
470         WineEventQueue* queue;
471
472         event = macdrv_create_event(DISPLAYS_CHANGED, nil);
473         event->displays_changed.activating = activating;
474
475         [eventQueuesLock lock];
476
477         // If we're activating, then we just need one of our threads to get the
478         // event, so it can send it directly to the desktop window.  Otherwise,
479         // we need all of the threads to get it because we don't know which owns
480         // the desktop window and only that one will do anything with it.
481         if (activating) event->deliver = 1;
482
483         for (queue in eventQueues)
484             [queue postEvent:event];
485         [eventQueuesLock unlock];
486
487         macdrv_release_event(event);
488     }
489
490     // We can compare two modes directly using CFEqual, but that may require that
491     // they are identical to a level that we don't need.  In particular, when the
492     // OS switches between the integrated and discrete GPUs, the set of display
493     // modes can change in subtle ways.  We're interested in whether two modes
494     // match in their most salient features, even if they aren't identical.
495     - (BOOL) mode:(CGDisplayModeRef)mode1 matchesMode:(CGDisplayModeRef)mode2
496     {
497         NSString *encoding1, *encoding2;
498         uint32_t ioflags1, ioflags2, different;
499         double refresh1, refresh2;
500
501         if (CGDisplayModeGetWidth(mode1) != CGDisplayModeGetWidth(mode2)) return FALSE;
502         if (CGDisplayModeGetHeight(mode1) != CGDisplayModeGetHeight(mode2)) return FALSE;
503
504         encoding1 = [(NSString*)CGDisplayModeCopyPixelEncoding(mode1) autorelease];
505         encoding2 = [(NSString*)CGDisplayModeCopyPixelEncoding(mode2) autorelease];
506         if (![encoding1 isEqualToString:encoding2]) return FALSE;
507
508         ioflags1 = CGDisplayModeGetIOFlags(mode1);
509         ioflags2 = CGDisplayModeGetIOFlags(mode2);
510         different = ioflags1 ^ ioflags2;
511         if (different & (kDisplayModeValidFlag | kDisplayModeSafeFlag | kDisplayModeStretchedFlag |
512                          kDisplayModeInterlacedFlag | kDisplayModeTelevisionFlag))
513             return FALSE;
514
515         refresh1 = CGDisplayModeGetRefreshRate(mode1);
516         if (refresh1 == 0) refresh1 = 60;
517         refresh2 = CGDisplayModeGetRefreshRate(mode2);
518         if (refresh2 == 0) refresh2 = 60;
519         if (fabs(refresh1 - refresh2) > 0.1) return FALSE;
520
521         return TRUE;
522     }
523
524     - (CGDisplayModeRef)modeMatchingMode:(CGDisplayModeRef)mode forDisplay:(CGDirectDisplayID)displayID
525     {
526         CGDisplayModeRef ret = NULL;
527         NSArray *modes = [(NSArray*)CGDisplayCopyAllDisplayModes(displayID, NULL) autorelease];
528         for (id candidateModeObject in modes)
529         {
530             CGDisplayModeRef candidateMode = (CGDisplayModeRef)candidateModeObject;
531             if ([self mode:candidateMode matchesMode:mode])
532             {
533                 ret = candidateMode;
534                 break;
535             }
536         }
537         return ret;
538     }
539
540     - (BOOL) setMode:(CGDisplayModeRef)mode forDisplay:(CGDirectDisplayID)displayID
541     {
542         BOOL ret = FALSE;
543         NSNumber* displayIDKey = [NSNumber numberWithUnsignedInt:displayID];
544         CGDisplayModeRef currentMode, originalMode;
545
546         currentMode = CGDisplayCopyDisplayMode(displayID);
547         if (!currentMode) // Invalid display ID
548             return FALSE;
549
550         if ([self mode:mode matchesMode:currentMode]) // Already there!
551         {
552             CGDisplayModeRelease(currentMode);
553             return TRUE;
554         }
555
556         mode = [self modeMatchingMode:mode forDisplay:displayID];
557         if (!mode)
558         {
559             CGDisplayModeRelease(currentMode);
560             return FALSE;
561         }
562
563         originalMode = (CGDisplayModeRef)[originalDisplayModes objectForKey:displayIDKey];
564         if (!originalMode)
565             originalMode = currentMode;
566
567         if ([self mode:mode matchesMode:originalMode])
568         {
569             if ([originalDisplayModes count] == 1) // If this is the last changed display, do a blanket reset
570             {
571                 CGRestorePermanentDisplayConfiguration();
572                 CGReleaseAllDisplays();
573                 [originalDisplayModes removeAllObjects];
574                 ret = TRUE;
575             }
576             else // ... otherwise, try to restore just the one display
577             {
578                 if (CGDisplaySetDisplayMode(displayID, mode, NULL) == CGDisplayNoErr)
579                 {
580                     [originalDisplayModes removeObjectForKey:displayIDKey];
581                     ret = TRUE;
582                 }
583             }
584         }
585         else
586         {
587             if ([originalDisplayModes count] || CGCaptureAllDisplays() == CGDisplayNoErr)
588             {
589                 if (CGDisplaySetDisplayMode(displayID, mode, NULL) == CGDisplayNoErr)
590                 {
591                     [originalDisplayModes setObject:(id)originalMode forKey:displayIDKey];
592                     ret = TRUE;
593                 }
594                 else if (![originalDisplayModes count])
595                 {
596                     CGRestorePermanentDisplayConfiguration();
597                     CGReleaseAllDisplays();
598                 }
599             }
600         }
601
602         CGDisplayModeRelease(currentMode);
603
604         if (ret)
605         {
606             [orderedWineWindows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
607                 [(WineWindow*)obj adjustWindowLevel];
608             }];
609         }
610
611         return ret;
612     }
613
614     - (BOOL) areDisplaysCaptured
615     {
616         return ([originalDisplayModes count] > 0);
617     }
618
619     - (void) hideCursor
620     {
621         if (!cursorHidden)
622         {
623             [NSCursor hide];
624             cursorHidden = TRUE;
625         }
626     }
627
628     - (void) unhideCursor
629     {
630         if (cursorHidden)
631         {
632             [NSCursor unhide];
633             cursorHidden = FALSE;
634         }
635     }
636
637     - (void) setCursor
638     {
639         NSDictionary* frame = [cursorFrames objectAtIndex:cursorFrame];
640         CGImageRef cgimage = (CGImageRef)[frame objectForKey:@"image"];
641         NSImage* image = [[NSImage alloc] initWithCGImage:cgimage size:NSZeroSize];
642         CFDictionaryRef hotSpotDict = (CFDictionaryRef)[frame objectForKey:@"hotSpot"];
643         CGPoint hotSpot;
644         NSCursor* cursor;
645
646         if (!CGPointMakeWithDictionaryRepresentation(hotSpotDict, &hotSpot))
647             hotSpot = CGPointZero;
648         cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSPointFromCGPoint(hotSpot)];
649         [image release];
650         [cursor set];
651         [self unhideCursor];
652         [cursor release];
653     }
654
655     - (void) nextCursorFrame:(NSTimer*)theTimer
656     {
657         NSDictionary* frame;
658         NSTimeInterval duration;
659         NSDate* date;
660
661         cursorFrame++;
662         if (cursorFrame >= [cursorFrames count])
663             cursorFrame = 0;
664         [self setCursor];
665
666         frame = [cursorFrames objectAtIndex:cursorFrame];
667         duration = [[frame objectForKey:@"duration"] doubleValue];
668         date = [[theTimer fireDate] dateByAddingTimeInterval:duration];
669         [cursorTimer setFireDate:date];
670     }
671
672     - (void) setCursorWithFrames:(NSArray*)frames
673     {
674         if (self.cursorFrames == frames)
675             return;
676
677         self.cursorFrames = frames;
678         cursorFrame = 0;
679         [cursorTimer invalidate];
680         self.cursorTimer = nil;
681
682         if ([frames count])
683         {
684             if ([frames count] > 1)
685             {
686                 NSDictionary* frame = [frames objectAtIndex:0];
687                 NSTimeInterval duration = [[frame objectForKey:@"duration"] doubleValue];
688                 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:duration];
689                 self.cursorTimer = [[[NSTimer alloc] initWithFireDate:date
690                                                              interval:1000000
691                                                                target:self
692                                                              selector:@selector(nextCursorFrame:)
693                                                              userInfo:nil
694                                                               repeats:YES] autorelease];
695                 [[NSRunLoop currentRunLoop] addTimer:cursorTimer forMode:NSRunLoopCommonModes];
696             }
697
698             [self setCursor];
699         }
700     }
701
702     - (void) setApplicationIconFromCGImageArray:(NSArray*)images
703     {
704         NSImage* nsimage = nil;
705
706         if ([images count])
707         {
708             NSSize bestSize = NSZeroSize;
709             id image;
710
711             nsimage = [[[NSImage alloc] initWithSize:NSZeroSize] autorelease];
712
713             for (image in images)
714             {
715                 CGImageRef cgimage = (CGImageRef)image;
716                 NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithCGImage:cgimage];
717                 if (imageRep)
718                 {
719                     NSSize size = [imageRep size];
720
721                     [nsimage addRepresentation:imageRep];
722                     [imageRep release];
723
724                     if (MIN(size.width, size.height) > MIN(bestSize.width, bestSize.height))
725                         bestSize = size;
726                 }
727             }
728
729             if ([[nsimage representations] count] && bestSize.width && bestSize.height)
730                 [nsimage setSize:bestSize];
731             else
732                 nsimage = nil;
733         }
734
735         self.applicationIcon = nsimage;
736         [NSApp setApplicationIconImage:nsimage];
737     }
738
739     - (void) handleCommandTab
740     {
741         if ([NSApp isActive])
742         {
743             NSRunningApplication* thisApp = [NSRunningApplication currentApplication];
744             NSRunningApplication* app;
745             NSRunningApplication* otherValidApp = nil;
746
747             if ([originalDisplayModes count])
748             {
749                 CGRestorePermanentDisplayConfiguration();
750                 CGReleaseAllDisplays();
751                 [originalDisplayModes removeAllObjects];
752             }
753
754             for (app in [[NSWorkspace sharedWorkspace] runningApplications])
755             {
756                 if (![app isEqual:thisApp] && !app.terminated &&
757                     app.activationPolicy == NSApplicationActivationPolicyRegular)
758                 {
759                     if (!app.hidden)
760                     {
761                         // There's another visible app.  Just hide ourselves and let
762                         // the system activate the other app.
763                         [NSApp hide:self];
764                         return;
765                     }
766
767                     if (!otherValidApp)
768                         otherValidApp = app;
769                 }
770             }
771
772             // Didn't find a visible GUI app.  Try the Finder or, if that's not
773             // running, the first hidden GUI app.  If even that doesn't work, we
774             // just fail to switch and remain the active app.
775             app = [[NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.finder"] lastObject];
776             if (!app) app = otherValidApp;
777             [app unhide];
778             [app activateWithOptions:0];
779         }
780     }
781
782     /*
783      * ---------- Cursor clipping methods ----------
784      *
785      * Neither Quartz nor Cocoa has an exact analog for Win32 cursor clipping.
786      * For one simple case, clipping to a 1x1 rectangle, Quartz does have an
787      * equivalent: CGAssociateMouseAndMouseCursorPosition(false).  For the
788      * general case, we leverage that.  We disassociate mouse movements from
789      * the cursor position and then move the cursor manually, keeping it within
790      * the clipping rectangle.
791      *
792      * Moving the cursor manually isn't enough.  We need to modify the event
793      * stream so that the events have the new location, too.  We need to do
794      * this at a point before the events enter Cocoa, so that Cocoa will assign
795      * the correct window to the event.  So, we install a Quartz event tap to
796      * do that.
797      *
798      * Also, there's a complication when we move the cursor.  We use
799      * CGWarpMouseCursorPosition().  That doesn't generate mouse movement
800      * events, but the change of cursor position is incorporated into the
801      * deltas of the next mouse move event.  When the mouse is disassociated
802      * from the cursor position, we need the deltas to only reflect actual
803      * device movement, not programmatic changes.  So, the event tap cancels
804      * out the change caused by our calls to CGWarpMouseCursorPosition().
805      */
806     - (void) clipCursorLocation:(CGPoint*)location
807     {
808         if (location->x < CGRectGetMinX(cursorClipRect))
809             location->x = CGRectGetMinX(cursorClipRect);
810         if (location->y < CGRectGetMinY(cursorClipRect))
811             location->y = CGRectGetMinY(cursorClipRect);
812         if (location->x > CGRectGetMaxX(cursorClipRect) - 1)
813             location->x = CGRectGetMaxX(cursorClipRect) - 1;
814         if (location->y > CGRectGetMaxY(cursorClipRect) - 1)
815             location->y = CGRectGetMaxY(cursorClipRect) - 1;
816     }
817
818     - (BOOL) warpCursorTo:(CGPoint*)newLocation from:(const CGPoint*)currentLocation
819     {
820         CGPoint oldLocation;
821
822         if (currentLocation)
823             oldLocation = *currentLocation;
824         else
825             oldLocation = NSPointToCGPoint([self flippedMouseLocation:[NSEvent mouseLocation]]);
826
827         if (!CGPointEqualToPoint(oldLocation, *newLocation))
828         {
829             WarpRecord* warpRecord = [[[WarpRecord alloc] init] autorelease];
830             CGError err;
831
832             warpRecord.from = oldLocation;
833             warpRecord.timeBefore = [[NSProcessInfo processInfo] systemUptime] * NSEC_PER_SEC;
834
835             /* Actually move the cursor. */
836             err = CGWarpMouseCursorPosition(*newLocation);
837             if (err != kCGErrorSuccess)
838                 return FALSE;
839
840             warpRecord.timeAfter = [[NSProcessInfo processInfo] systemUptime] * NSEC_PER_SEC;
841             *newLocation = NSPointToCGPoint([self flippedMouseLocation:[NSEvent mouseLocation]]);
842
843             if (!CGPointEqualToPoint(oldLocation, *newLocation))
844             {
845                 warpRecord.to = *newLocation;
846                 [warpRecords addObject:warpRecord];
847             }
848         }
849
850         return TRUE;
851     }
852
853     - (BOOL) isMouseMoveEventType:(CGEventType)type
854     {
855         switch(type)
856         {
857         case kCGEventMouseMoved:
858         case kCGEventLeftMouseDragged:
859         case kCGEventRightMouseDragged:
860         case kCGEventOtherMouseDragged:
861             return TRUE;
862         }
863
864         return FALSE;
865     }
866
867     - (int) warpsFinishedByEventTime:(CGEventTimestamp)eventTime location:(CGPoint)eventLocation
868     {
869         int warpsFinished = 0;
870         for (WarpRecord* warpRecord in warpRecords)
871         {
872             if (warpRecord.timeAfter < eventTime ||
873                 (warpRecord.timeBefore <= eventTime && CGPointEqualToPoint(eventLocation, warpRecord.to)))
874                 warpsFinished++;
875             else
876                 break;
877         }
878
879         return warpsFinished;
880     }
881
882     - (CGEventRef) eventTapWithProxy:(CGEventTapProxy)proxy
883                                 type:(CGEventType)type
884                                event:(CGEventRef)event
885     {
886         CGEventTimestamp eventTime;
887         CGPoint eventLocation, cursorLocation;
888
889         if (type == kCGEventTapDisabledByUserInput)
890             return event;
891         if (type == kCGEventTapDisabledByTimeout)
892         {
893             CGEventTapEnable(cursorClippingEventTap, TRUE);
894             return event;
895         }
896
897         if (!clippingCursor)
898             return event;
899
900         eventTime = CGEventGetTimestamp(event);
901         lastEventTapEventTime = eventTime / (double)NSEC_PER_SEC;
902
903         eventLocation = CGEventGetLocation(event);
904
905         cursorLocation = NSPointToCGPoint([self flippedMouseLocation:[NSEvent mouseLocation]]);
906
907         if ([self isMouseMoveEventType:type])
908         {
909             double deltaX, deltaY;
910             int warpsFinished = [self warpsFinishedByEventTime:eventTime location:eventLocation];
911             int i;
912
913             deltaX = CGEventGetDoubleValueField(event, kCGMouseEventDeltaX);
914             deltaY = CGEventGetDoubleValueField(event, kCGMouseEventDeltaY);
915
916             for (i = 0; i < warpsFinished; i++)
917             {
918                 WarpRecord* warpRecord = [warpRecords objectAtIndex:0];
919                 deltaX -= warpRecord.to.x - warpRecord.from.x;
920                 deltaY -= warpRecord.to.y - warpRecord.from.y;
921                 [warpRecords removeObjectAtIndex:0];
922             }
923
924             if (warpsFinished)
925             {
926                 CGEventSetDoubleValueField(event, kCGMouseEventDeltaX, deltaX);
927                 CGEventSetDoubleValueField(event, kCGMouseEventDeltaY, deltaY);
928             }
929
930             synthesizedLocation.x += deltaX;
931             synthesizedLocation.y += deltaY;
932         }
933
934         // If the event is destined for another process, don't clip it.  This may
935         // happen if the user activates Exposé or Mission Control.  In that case,
936         // our app does not resign active status, so clipping is still in effect,
937         // but the cursor should not actually be clipped.
938         //
939         // In addition, the fact that mouse moves may have been delivered to a
940         // different process means we have to treat the next one we receive as
941         // absolute rather than relative.
942         if (CGEventGetIntegerValueField(event, kCGEventTargetUnixProcessID) == getpid())
943             [self clipCursorLocation:&synthesizedLocation];
944         else
945             lastSetCursorPositionTime = lastEventTapEventTime;
946
947         [self warpCursorTo:&synthesizedLocation from:&cursorLocation];
948         if (!CGPointEqualToPoint(eventLocation, synthesizedLocation))
949             CGEventSetLocation(event, synthesizedLocation);
950
951         return event;
952     }
953
954     CGEventRef WineAppEventTapCallBack(CGEventTapProxy proxy, CGEventType type,
955                                        CGEventRef event, void *refcon)
956     {
957         WineApplicationController* controller = refcon;
958         return [controller eventTapWithProxy:proxy type:type event:event];
959     }
960
961     - (BOOL) installEventTap
962     {
963         ProcessSerialNumber psn;
964         OSErr err;
965         CGEventMask mask = CGEventMaskBit(kCGEventLeftMouseDown)        |
966                            CGEventMaskBit(kCGEventLeftMouseUp)          |
967                            CGEventMaskBit(kCGEventRightMouseDown)       |
968                            CGEventMaskBit(kCGEventRightMouseUp)         |
969                            CGEventMaskBit(kCGEventMouseMoved)           |
970                            CGEventMaskBit(kCGEventLeftMouseDragged)     |
971                            CGEventMaskBit(kCGEventRightMouseDragged)    |
972                            CGEventMaskBit(kCGEventOtherMouseDown)       |
973                            CGEventMaskBit(kCGEventOtherMouseUp)         |
974                            CGEventMaskBit(kCGEventOtherMouseDragged)    |
975                            CGEventMaskBit(kCGEventScrollWheel);
976         CFRunLoopSourceRef source;
977         void* appServices;
978         OSErr (*pGetCurrentProcess)(ProcessSerialNumber* PSN);
979
980         if (cursorClippingEventTap)
981             return TRUE;
982
983         // We need to get the Mac GetCurrentProcess() from the ApplicationServices
984         // framework with dlsym() because the Win32 function of the same name
985         // obscures it.
986         appServices = dlopen("/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices", RTLD_LAZY);
987         if (!appServices)
988             return FALSE;
989
990         pGetCurrentProcess = dlsym(appServices, "GetCurrentProcess");
991         if (!pGetCurrentProcess)
992         {
993             dlclose(appServices);
994             return FALSE;
995         }
996
997         err = pGetCurrentProcess(&psn);
998         dlclose(appServices);
999         if (err != noErr)
1000             return FALSE;
1001
1002         // We create an annotated session event tap rather than a process-specific
1003         // event tap because we need to programmatically move the cursor even when
1004         // mouse moves are directed to other processes.  We disable our tap when
1005         // other processes are active, but things like Exposé are handled by other
1006         // processes even when we remain active.
1007         cursorClippingEventTap = CGEventTapCreate(kCGAnnotatedSessionEventTap, kCGHeadInsertEventTap,
1008             kCGEventTapOptionDefault, mask, WineAppEventTapCallBack, self);
1009         if (!cursorClippingEventTap)
1010             return FALSE;
1011
1012         CGEventTapEnable(cursorClippingEventTap, FALSE);
1013
1014         source = CFMachPortCreateRunLoopSource(NULL, cursorClippingEventTap, 0);
1015         if (!source)
1016         {
1017             CFRelease(cursorClippingEventTap);
1018             cursorClippingEventTap = NULL;
1019             return FALSE;
1020         }
1021
1022         CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopCommonModes);
1023         CFRelease(source);
1024         return TRUE;
1025     }
1026
1027     - (BOOL) setCursorPosition:(CGPoint)pos
1028     {
1029         BOOL ret;
1030
1031         if (clippingCursor)
1032         {
1033             [self clipCursorLocation:&pos];
1034
1035             synthesizedLocation = pos;
1036             ret = [self warpCursorTo:&synthesizedLocation from:NULL];
1037             if (ret)
1038             {
1039                 // We want to discard mouse-move events that have already been
1040                 // through the event tap, because it's too late to account for
1041                 // the setting of the cursor position with them.  However, the
1042                 // events that may be queued with times after that but before
1043                 // the above warp can still be used.  So, use the last event
1044                 // tap event time so that -sendEvent: doesn't discard them.
1045                 lastSetCursorPositionTime = lastEventTapEventTime;
1046             }
1047         }
1048         else
1049         {
1050             ret = (CGWarpMouseCursorPosition(pos) == kCGErrorSuccess);
1051             if (ret)
1052             {
1053                 lastSetCursorPositionTime = [[NSProcessInfo processInfo] systemUptime];
1054
1055                 // Annoyingly, CGWarpMouseCursorPosition() effectively disassociates
1056                 // the mouse from the cursor position for 0.25 seconds.  This means
1057                 // that mouse movement during that interval doesn't move the cursor
1058                 // and events carry a constant location (the warped-to position)
1059                 // even though they have delta values.  This screws us up because
1060                 // the accumulated deltas we send to Wine don't match any eventual
1061                 // absolute position we send (like with a button press).  We can
1062                 // work around this by simply forcibly reassociating the mouse and
1063                 // cursor position.
1064                 CGAssociateMouseAndMouseCursorPosition(true);
1065             }
1066         }
1067
1068         if (ret)
1069         {
1070             WineEventQueue* queue;
1071
1072             // Discard all pending mouse move events.
1073             [eventQueuesLock lock];
1074             for (queue in eventQueues)
1075             {
1076                 [queue discardEventsMatchingMask:event_mask_for_type(MOUSE_MOVED) |
1077                                                  event_mask_for_type(MOUSE_MOVED_ABSOLUTE)
1078                                        forWindow:nil];
1079             }
1080             [eventQueuesLock unlock];
1081         }
1082
1083         return ret;
1084     }
1085
1086     - (void) activateCursorClipping
1087     {
1088         if (clippingCursor)
1089         {
1090             CGEventTapEnable(cursorClippingEventTap, TRUE);
1091             [self setCursorPosition:NSPointToCGPoint([self flippedMouseLocation:[NSEvent mouseLocation]])];
1092         }
1093     }
1094
1095     - (void) deactivateCursorClipping
1096     {
1097         if (clippingCursor)
1098         {
1099             CGEventTapEnable(cursorClippingEventTap, FALSE);
1100             [warpRecords removeAllObjects];
1101             lastSetCursorPositionTime = [[NSProcessInfo processInfo] systemUptime];
1102         }
1103     }
1104
1105     - (BOOL) startClippingCursor:(CGRect)rect
1106     {
1107         CGError err;
1108
1109         if (!cursorClippingEventTap && ![self installEventTap])
1110             return FALSE;
1111
1112         err = CGAssociateMouseAndMouseCursorPosition(false);
1113         if (err != kCGErrorSuccess)
1114             return FALSE;
1115
1116         clippingCursor = TRUE;
1117         cursorClipRect = rect;
1118         if ([NSApp isActive])
1119             [self activateCursorClipping];
1120
1121         return TRUE;
1122     }
1123
1124     - (BOOL) stopClippingCursor
1125     {
1126         CGError err = CGAssociateMouseAndMouseCursorPosition(true);
1127         if (err != kCGErrorSuccess)
1128             return FALSE;
1129
1130         [self deactivateCursorClipping];
1131         clippingCursor = FALSE;
1132
1133         return TRUE;
1134     }
1135
1136
1137     // Returns TRUE if the event was handled and caller should do nothing more
1138     // with it.  Returns FALSE if the caller should process it as normal and
1139     // then call -didSendEvent:.
1140     - (BOOL) handleEvent:(NSEvent*)anEvent
1141     {
1142         if ([anEvent type] == NSFlagsChanged)
1143             self.lastFlagsChanged = anEvent;
1144         return FALSE;
1145     }
1146
1147     - (void) didSendEvent:(NSEvent*)anEvent
1148     {
1149         NSEventType type = [anEvent type];
1150
1151         if (type == NSMouseMoved || type == NSLeftMouseDragged ||
1152             type == NSRightMouseDragged || type == NSOtherMouseDragged)
1153         {
1154             WineWindow* targetWindow;
1155
1156             /* Because of the way -[NSWindow setAcceptsMouseMovedEvents:] works, the
1157                event indicates its window is the main window, even if the cursor is
1158                over a different window.  Find the actual WineWindow that is under the
1159                cursor and post the event as being for that window. */
1160             if (type == NSMouseMoved)
1161             {
1162                 CGPoint cgpoint = CGEventGetLocation([anEvent CGEvent]);
1163                 NSPoint point = [self flippedMouseLocation:NSPointFromCGPoint(cgpoint)];
1164                 NSInteger windowUnderNumber;
1165
1166                 windowUnderNumber = [NSWindow windowNumberAtPoint:point
1167                                       belowWindowWithWindowNumber:0];
1168                 targetWindow = (WineWindow*)[NSApp windowWithWindowNumber:windowUnderNumber];
1169             }
1170             else
1171                 targetWindow = (WineWindow*)[anEvent window];
1172
1173             if ([targetWindow isKindOfClass:[WineWindow class]])
1174             {
1175                 BOOL absolute = forceNextMouseMoveAbsolute || (targetWindow != lastTargetWindow);
1176                 forceNextMouseMoveAbsolute = FALSE;
1177
1178                 // If we recently warped the cursor (other than in our cursor-clipping
1179                 // event tap), discard mouse move events until we see an event which is
1180                 // later than that time.
1181                 if (lastSetCursorPositionTime)
1182                 {
1183                     if ([anEvent timestamp] <= lastSetCursorPositionTime)
1184                         return;
1185
1186                     lastSetCursorPositionTime = 0;
1187                     absolute = TRUE;
1188                 }
1189
1190                 [targetWindow postMouseMovedEvent:anEvent absolute:absolute];
1191                 lastTargetWindow = targetWindow;
1192             }
1193             else if (lastTargetWindow)
1194             {
1195                 [[NSCursor arrowCursor] set];
1196                 [self unhideCursor];
1197                 lastTargetWindow = nil;
1198             }
1199         }
1200         else if (type == NSLeftMouseDown || type == NSLeftMouseUp ||
1201                  type == NSRightMouseDown || type == NSRightMouseUp ||
1202                  type == NSOtherMouseDown || type == NSOtherMouseUp ||
1203                  type == NSScrollWheel)
1204         {
1205             // Since mouse button and scroll wheel events deliver absolute cursor
1206             // position, the accumulating delta from move events is invalidated.
1207             // Make sure next mouse move event starts over from an absolute baseline.
1208             forceNextMouseMoveAbsolute = TRUE;
1209         }
1210         else if (type == NSKeyDown && ![anEvent isARepeat] && [anEvent keyCode] == kVK_Tab)
1211         {
1212             NSUInteger modifiers = [anEvent modifierFlags];
1213             if ((modifiers & NSCommandKeyMask) &&
1214                 !(modifiers & (NSControlKeyMask | NSAlternateKeyMask)))
1215             {
1216                 // Command-Tab and Command-Shift-Tab would normally be intercepted
1217                 // by the system to switch applications.  If we're seeing it, it's
1218                 // presumably because we've captured the displays, preventing
1219                 // normal application switching.  Do it manually.
1220                 [self handleCommandTab];
1221             }
1222         }
1223     }
1224
1225     - (void) setupObservations
1226     {
1227         NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
1228
1229         [nc addObserverForName:NSWindowDidBecomeKeyNotification
1230                         object:nil
1231                          queue:nil
1232                     usingBlock:^(NSNotification *note){
1233             NSWindow* window = [note object];
1234             [keyWindows removeObjectIdenticalTo:window];
1235             [keyWindows insertObject:window atIndex:0];
1236         }];
1237
1238         [nc addObserverForName:NSWindowWillCloseNotification
1239                         object:nil
1240                          queue:[NSOperationQueue mainQueue]
1241                     usingBlock:^(NSNotification *note){
1242             NSWindow* window = [note object];
1243             [keyWindows removeObjectIdenticalTo:window];
1244             [orderedWineWindows removeObjectIdenticalTo:window];
1245             if (window == lastTargetWindow)
1246                 lastTargetWindow = nil;
1247         }];
1248
1249         [nc addObserver:self
1250                selector:@selector(keyboardSelectionDidChange)
1251                    name:NSTextInputContextKeyboardSelectionDidChangeNotification
1252                  object:nil];
1253
1254         /* The above notification isn't sent unless the NSTextInputContext
1255            class has initialized itself.  Poke it. */
1256         [NSTextInputContext self];
1257     }
1258
1259     - (BOOL) inputSourceIsInputMethod
1260     {
1261         if (!inputSourceIsInputMethodValid)
1262         {
1263             TISInputSourceRef inputSource = TISCopyCurrentKeyboardInputSource();
1264             if (inputSource)
1265             {
1266                 CFStringRef type = TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceType);
1267                 inputSourceIsInputMethod = !CFEqual(type, kTISTypeKeyboardLayout);
1268                 CFRelease(inputSource);
1269             }
1270             else
1271                 inputSourceIsInputMethod = FALSE;
1272             inputSourceIsInputMethodValid = TRUE;
1273         }
1274
1275         return inputSourceIsInputMethod;
1276     }
1277
1278
1279     /*
1280      * ---------- NSApplicationDelegate methods ----------
1281      */
1282     - (void)applicationDidBecomeActive:(NSNotification *)notification
1283     {
1284         [self activateCursorClipping];
1285
1286         [orderedWineWindows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
1287             WineWindow* window = obj;
1288             if ([window levelWhenActive] != [window level])
1289                 [window setLevel:[window levelWhenActive]];
1290         }];
1291
1292         // If a Wine process terminates abruptly while it has the display captured
1293         // and switched to a different resolution, Mac OS X will uncapture the
1294         // displays and switch their resolutions back.  However, the other Wine
1295         // processes won't have their notion of the desktop rect changed back.
1296         // This can lead them to refuse to draw or acknowledge clicks in certain
1297         // portions of their windows.
1298         //
1299         // To solve this, we synthesize a displays-changed event whenever we're
1300         // activated.  This will provoke a re-synchronization of Wine's notion of
1301         // the desktop rect with the actual state.
1302         [self sendDisplaysChanged:TRUE];
1303
1304         // The cursor probably moved while we were inactive.  Accumulated mouse
1305         // movement deltas are invalidated.  Make sure the next mouse move event
1306         // starts over from an absolute baseline.
1307         forceNextMouseMoveAbsolute = TRUE;
1308     }
1309
1310     - (void)applicationDidChangeScreenParameters:(NSNotification *)notification
1311     {
1312         primaryScreenHeightValid = FALSE;
1313         [self sendDisplaysChanged:FALSE];
1314
1315         // When the display configuration changes, the cursor position may jump.
1316         // Accumulated mouse movement deltas are invalidated.  Make sure the next
1317         // mouse move event starts over from an absolute baseline.
1318         forceNextMouseMoveAbsolute = TRUE;
1319     }
1320
1321     - (void)applicationDidResignActive:(NSNotification *)notification
1322     {
1323         macdrv_event* event;
1324         WineEventQueue* queue;
1325
1326         [self invalidateGotFocusEvents];
1327
1328         event = macdrv_create_event(APP_DEACTIVATED, nil);
1329
1330         [eventQueuesLock lock];
1331         for (queue in eventQueues)
1332             [queue postEvent:event];
1333         [eventQueuesLock unlock];
1334
1335         macdrv_release_event(event);
1336     }
1337
1338     - (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication *)sender
1339     {
1340         NSApplicationTerminateReply ret = NSTerminateNow;
1341         NSAppleEventManager* m = [NSAppleEventManager sharedAppleEventManager];
1342         NSAppleEventDescriptor* desc = [m currentAppleEvent];
1343         macdrv_event* event;
1344         WineEventQueue* queue;
1345
1346         event = macdrv_create_event(APP_QUIT_REQUESTED, nil);
1347         event->deliver = 1;
1348         switch ([[desc attributeDescriptorForKeyword:kAEQuitReason] int32Value])
1349         {
1350             case kAELogOut:
1351             case kAEReallyLogOut:
1352                 event->app_quit_requested.reason = QUIT_REASON_LOGOUT;
1353                 break;
1354             case kAEShowRestartDialog:
1355                 event->app_quit_requested.reason = QUIT_REASON_RESTART;
1356                 break;
1357             case kAEShowShutdownDialog:
1358                 event->app_quit_requested.reason = QUIT_REASON_SHUTDOWN;
1359                 break;
1360             default:
1361                 event->app_quit_requested.reason = QUIT_REASON_NONE;
1362                 break;
1363         }
1364
1365         [eventQueuesLock lock];
1366
1367         if ([eventQueues count])
1368         {
1369             for (queue in eventQueues)
1370                 [queue postEvent:event];
1371             ret = NSTerminateLater;
1372         }
1373
1374         [eventQueuesLock unlock];
1375
1376         macdrv_release_event(event);
1377
1378         return ret;
1379     }
1380
1381     - (void)applicationWillResignActive:(NSNotification *)notification
1382     {
1383         [self deactivateCursorClipping];
1384
1385         [orderedWineWindows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
1386             WineWindow* window = obj;
1387             NSInteger level = window.floating ? NSFloatingWindowLevel : NSNormalWindowLevel;
1388             if ([window level] > level)
1389                 [window setLevel:level];
1390         }];
1391     }
1392
1393 /***********************************************************************
1394  *              PerformRequest
1395  *
1396  * Run-loop-source perform callback.  Pull request blocks from the
1397  * array of queued requests and invoke them.
1398  */
1399 static void PerformRequest(void *info)
1400 {
1401     WineApplicationController* controller = [WineApplicationController sharedController];
1402
1403     for (;;)
1404     {
1405         __block dispatch_block_t block;
1406
1407         dispatch_sync(controller->requestsManipQueue, ^{
1408             if ([controller->requests count])
1409             {
1410                 block = (dispatch_block_t)[[controller->requests objectAtIndex:0] retain];
1411                 [controller->requests removeObjectAtIndex:0];
1412             }
1413             else
1414                 block = nil;
1415         });
1416
1417         if (!block)
1418             break;
1419
1420         block();
1421         [block release];
1422     }
1423 }
1424
1425 /***********************************************************************
1426  *              OnMainThreadAsync
1427  *
1428  * Run a block on the main thread asynchronously.
1429  */
1430 void OnMainThreadAsync(dispatch_block_t block)
1431 {
1432     WineApplicationController* controller = [WineApplicationController sharedController];
1433
1434     block = [block copy];
1435     dispatch_sync(controller->requestsManipQueue, ^{
1436         [controller->requests addObject:block];
1437     });
1438     [block release];
1439     CFRunLoopSourceSignal(controller->requestSource);
1440     CFRunLoopWakeUp(CFRunLoopGetMain());
1441 }
1442
1443 @end
1444
1445 /***********************************************************************
1446  *              LogError
1447  */
1448 void LogError(const char* func, NSString* format, ...)
1449 {
1450     va_list args;
1451     va_start(args, format);
1452     LogErrorv(func, format, args);
1453     va_end(args);
1454 }
1455
1456 /***********************************************************************
1457  *              LogErrorv
1458  */
1459 void LogErrorv(const char* func, NSString* format, va_list args)
1460 {
1461     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
1462
1463     NSString* message = [[NSString alloc] initWithFormat:format arguments:args];
1464     fprintf(stderr, "err:%s:%s", func, [message UTF8String]);
1465     [message release];
1466
1467     [pool release];
1468 }
1469
1470 /***********************************************************************
1471  *              macdrv_window_rejected_focus
1472  *
1473  * Pass focus to the next window that hasn't already rejected this same
1474  * WINDOW_GOT_FOCUS event.
1475  */
1476 void macdrv_window_rejected_focus(const macdrv_event *event)
1477 {
1478     OnMainThread(^{
1479         [[WineApplicationController sharedController] windowRejectedFocusEvent:event];
1480     });
1481 }
1482
1483 /***********************************************************************
1484  *              macdrv_get_keyboard_layout
1485  *
1486  * Returns the keyboard layout uchr data.
1487  */
1488 CFDataRef macdrv_copy_keyboard_layout(CGEventSourceKeyboardType* keyboard_type, int* is_iso)
1489 {
1490     __block CFDataRef result = NULL;
1491
1492     OnMainThread(^{
1493         TISInputSourceRef inputSource;
1494
1495         inputSource = TISCopyCurrentKeyboardLayoutInputSource();
1496         if (inputSource)
1497         {
1498             CFDataRef uchr = TISGetInputSourceProperty(inputSource,
1499                                 kTISPropertyUnicodeKeyLayoutData);
1500             result = CFDataCreateCopy(NULL, uchr);
1501             CFRelease(inputSource);
1502
1503             *keyboard_type = [WineApplicationController sharedController].keyboardType;
1504             *is_iso = (KBGetLayoutType(*keyboard_type) == kKeyboardISO);
1505         }
1506     });
1507
1508     return result;
1509 }
1510
1511 /***********************************************************************
1512  *              macdrv_beep
1513  *
1514  * Play the beep sound configured by the user in System Preferences.
1515  */
1516 void macdrv_beep(void)
1517 {
1518     OnMainThreadAsync(^{
1519         NSBeep();
1520     });
1521 }
1522
1523 /***********************************************************************
1524  *              macdrv_set_display_mode
1525  */
1526 int macdrv_set_display_mode(const struct macdrv_display* display,
1527                             CGDisplayModeRef display_mode)
1528 {
1529     __block int ret;
1530
1531     OnMainThread(^{
1532         ret = [[WineApplicationController sharedController] setMode:display_mode forDisplay:display->displayID];
1533     });
1534
1535     return ret;
1536 }
1537
1538 /***********************************************************************
1539  *              macdrv_set_cursor
1540  *
1541  * Set the cursor.
1542  *
1543  * If name is non-NULL, it is a selector for a class method on NSCursor
1544  * identifying the cursor to set.  In that case, frames is ignored.  If
1545  * name is NULL, then frames is used.
1546  *
1547  * frames is an array of dictionaries.  Each dictionary is a frame of
1548  * an animated cursor.  Under the key "image" is a CGImage for the
1549  * frame.  Under the key "duration" is a CFNumber time interval, in
1550  * seconds, for how long that frame is presented before proceeding to
1551  * the next frame.  Under the key "hotSpot" is a CFDictionary encoding a
1552  * CGPoint, to be decoded using CGPointMakeWithDictionaryRepresentation().
1553  * This is the hot spot, measured in pixels down and to the right of the
1554  * top-left corner of the image.
1555  *
1556  * If the array has exactly 1 element, the cursor is static, not
1557  * animated.  If frames is NULL or has 0 elements, the cursor is hidden.
1558  */
1559 void macdrv_set_cursor(CFStringRef name, CFArrayRef frames)
1560 {
1561     SEL sel;
1562
1563     sel = NSSelectorFromString((NSString*)name);
1564     if (sel)
1565     {
1566         OnMainThreadAsync(^{
1567             WineApplicationController* controller = [WineApplicationController sharedController];
1568             NSCursor* cursor = [NSCursor performSelector:sel];
1569             [controller setCursorWithFrames:nil];
1570             [cursor set];
1571             [controller unhideCursor];
1572         });
1573     }
1574     else
1575     {
1576         NSArray* nsframes = (NSArray*)frames;
1577         if ([nsframes count])
1578         {
1579             OnMainThreadAsync(^{
1580                 [[WineApplicationController sharedController] setCursorWithFrames:nsframes];
1581             });
1582         }
1583         else
1584         {
1585             OnMainThreadAsync(^{
1586                 WineApplicationController* controller = [WineApplicationController sharedController];
1587                 [controller setCursorWithFrames:nil];
1588                 [controller hideCursor];
1589             });
1590         }
1591     }
1592 }
1593
1594 /***********************************************************************
1595  *              macdrv_get_cursor_position
1596  *
1597  * Obtains the current cursor position.  Returns zero on failure,
1598  * non-zero on success.
1599  */
1600 int macdrv_get_cursor_position(CGPoint *pos)
1601 {
1602     OnMainThread(^{
1603         NSPoint location = [NSEvent mouseLocation];
1604         location = [[WineApplicationController sharedController] flippedMouseLocation:location];
1605         *pos = NSPointToCGPoint(location);
1606     });
1607
1608     return TRUE;
1609 }
1610
1611 /***********************************************************************
1612  *              macdrv_set_cursor_position
1613  *
1614  * Sets the cursor position without generating events.  Returns zero on
1615  * failure, non-zero on success.
1616  */
1617 int macdrv_set_cursor_position(CGPoint pos)
1618 {
1619     __block int ret;
1620
1621     OnMainThread(^{
1622         ret = [[WineApplicationController sharedController] setCursorPosition:pos];
1623     });
1624
1625     return ret;
1626 }
1627
1628 /***********************************************************************
1629  *              macdrv_clip_cursor
1630  *
1631  * Sets the cursor cursor clipping rectangle.  If the rectangle is equal
1632  * to or larger than the whole desktop region, the cursor is unclipped.
1633  * Returns zero on failure, non-zero on success.
1634  */
1635 int macdrv_clip_cursor(CGRect rect)
1636 {
1637     __block int ret;
1638
1639     OnMainThread(^{
1640         WineApplicationController* controller = [WineApplicationController sharedController];
1641         BOOL clipping = FALSE;
1642
1643         if (!CGRectIsInfinite(rect))
1644         {
1645             NSRect nsrect = NSRectFromCGRect(rect);
1646             NSScreen* screen;
1647
1648             /* Convert the rectangle from top-down coords to bottom-up. */
1649             [controller flipRect:&nsrect];
1650
1651             clipping = FALSE;
1652             for (screen in [NSScreen screens])
1653             {
1654                 if (!NSContainsRect(nsrect, [screen frame]))
1655                 {
1656                     clipping = TRUE;
1657                     break;
1658                 }
1659             }
1660         }
1661
1662         if (clipping)
1663             ret = [controller startClippingCursor:rect];
1664         else
1665             ret = [controller stopClippingCursor];
1666     });
1667
1668     return ret;
1669 }
1670
1671 /***********************************************************************
1672  *              macdrv_set_application_icon
1673  *
1674  * Set the application icon.  The images array contains CGImages.  If
1675  * there are more than one, then they represent different sizes or
1676  * color depths from the icon resource.  If images is NULL or empty,
1677  * restores the default application image.
1678  */
1679 void macdrv_set_application_icon(CFArrayRef images)
1680 {
1681     NSArray* imageArray = (NSArray*)images;
1682
1683     OnMainThreadAsync(^{
1684         [[WineApplicationController sharedController] setApplicationIconFromCGImageArray:imageArray];
1685     });
1686 }
1687
1688 /***********************************************************************
1689  *              macdrv_quit_reply
1690  */
1691 void macdrv_quit_reply(int reply)
1692 {
1693     OnMainThread(^{
1694         [NSApp replyToApplicationShouldTerminate:reply];
1695     });
1696 }
1697
1698 /***********************************************************************
1699  *              macdrv_using_input_method
1700  */
1701 int macdrv_using_input_method(void)
1702 {
1703     __block BOOL ret;
1704
1705     OnMainThread(^{
1706         ret = [[WineApplicationController sharedController] inputSourceIsInputMethod];
1707     });
1708
1709     return ret;
1710 }