2 * MACDRV Cocoa application class
4 * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
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.
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.
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
21 #import <Carbon/Carbon.h>
24 #import "cocoa_event.h"
25 #import "cocoa_window.h"
31 @interface WineApplication ()
33 @property (readwrite, copy, nonatomic) NSEvent* lastFlagsChanged;
38 @implementation WineApplication
40 @synthesize keyboardType, lastFlagsChanged;
47 eventQueues = [[NSMutableArray alloc] init];
48 eventQueuesLock = [[NSLock alloc] init];
50 keyWindows = [[NSMutableArray alloc] init];
52 if (!eventQueues || !eventQueuesLock || !keyWindows)
64 [eventQueues release];
65 [eventQueuesLock release];
69 - (void) transformProcessToForeground
71 if ([self activationPolicy] != NSApplicationActivationPolicyRegular)
79 [self setActivationPolicy:NSApplicationActivationPolicyRegular];
80 [self activateIgnoringOtherApps:YES];
82 mainMenu = [[[NSMenu alloc] init] autorelease];
84 submenu = [[[NSMenu alloc] initWithTitle:@"Wine"] autorelease];
85 bundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleNameKey];
86 if ([bundleName length])
87 title = [NSString stringWithFormat:@"Quit %@", bundleName];
90 item = [submenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
91 [item setKeyEquivalentModifierMask:NSCommandKeyMask | NSAlternateKeyMask];
92 item = [[[NSMenuItem alloc] init] autorelease];
93 [item setTitle:@"Wine"];
94 [item setSubmenu:submenu];
95 [mainMenu addItem:item];
97 submenu = [[[NSMenu alloc] initWithTitle:@"Window"] autorelease];
98 [submenu addItemWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
99 [submenu addItemWithTitle:@"Zoom" action:@selector(performZoom:) keyEquivalent:@""];
100 [submenu addItem:[NSMenuItem separatorItem]];
101 [submenu addItemWithTitle:@"Bring All to Front" action:@selector(arrangeInFront:) keyEquivalent:@""];
102 item = [[[NSMenuItem alloc] init] autorelease];
103 [item setTitle:@"Window"];
104 [item setSubmenu:submenu];
105 [mainMenu addItem:item];
107 [self setMainMenu:mainMenu];
108 [self setWindowsMenu:submenu];
112 - (BOOL) registerEventQueue:(WineEventQueue*)queue
114 [eventQueuesLock lock];
115 [eventQueues addObject:queue];
116 [eventQueuesLock unlock];
120 - (void) unregisterEventQueue:(WineEventQueue*)queue
122 [eventQueuesLock lock];
123 [eventQueues removeObjectIdenticalTo:queue];
124 [eventQueuesLock unlock];
127 - (void) computeEventTimeAdjustmentFromTicks:(unsigned long long)tickcount uptime:(uint64_t)uptime_ns
129 eventTimeAdjustment = (tickcount / 1000.0) - (uptime_ns / (double)NSEC_PER_SEC);
132 - (double) ticksForEventTime:(NSTimeInterval)eventTime
134 return (eventTime + eventTimeAdjustment) * 1000;
137 /* Invalidate old focus offers across all queues. */
138 - (void) invalidateGotFocusEvents
140 WineEventQueue* queue;
144 [eventQueuesLock lock];
145 for (queue in eventQueues)
147 [queue discardEventsMatchingMask:event_mask_for_type(WINDOW_GOT_FOCUS)
150 [eventQueuesLock unlock];
153 - (void) windowGotFocus:(WineWindow*)window
157 [NSApp invalidateGotFocusEvents];
159 event.type = WINDOW_GOT_FOCUS;
160 event.window = (macdrv_window)[window retain];
161 event.window_got_focus.serial = windowFocusSerial;
163 event.window_got_focus.tried_windows = [triedWindows retain];
165 event.window_got_focus.tried_windows = [[NSMutableSet alloc] init];
166 [window.queue postEvent:&event];
169 - (void) windowRejectedFocusEvent:(const macdrv_event*)event
171 if (event->window_got_focus.serial == windowFocusSerial)
173 triedWindows = (NSMutableSet*)event->window_got_focus.tried_windows;
174 [triedWindows addObject:(WineWindow*)event->window];
175 for (NSWindow* window in [keyWindows arrayByAddingObjectsFromArray:[self orderedWindows]])
177 if (![triedWindows containsObject:window] && [window canBecomeKeyWindow])
179 [window makeKeyWindow];
187 - (void) keyboardSelectionDidChange
189 TISInputSourceRef inputSource;
191 inputSource = TISCopyCurrentKeyboardLayoutInputSource();
195 uchr = TISGetInputSourceProperty(inputSource,
196 kTISPropertyUnicodeKeyLayoutData);
200 WineEventQueue* queue;
202 event.type = KEYBOARD_CHANGED;
204 event.keyboard_changed.keyboard_type = self.keyboardType;
205 event.keyboard_changed.iso_keyboard = (KBGetLayoutType(self.keyboardType) == kKeyboardISO);
206 event.keyboard_changed.uchr = CFDataCreateCopy(NULL, uchr);
208 if (event.keyboard_changed.uchr)
210 [eventQueuesLock lock];
212 for (queue in eventQueues)
214 CFRetain(event.keyboard_changed.uchr);
215 [queue postEvent:&event];
218 [eventQueuesLock unlock];
220 CFRelease(event.keyboard_changed.uchr);
224 CFRelease(inputSource);
230 * ---------- NSApplication method overrides ----------
232 - (void) sendEvent:(NSEvent*)anEvent
234 if ([anEvent type] == NSFlagsChanged)
235 self.lastFlagsChanged = anEvent;
237 [super sendEvent:anEvent];
242 * ---------- NSApplicationDelegate methods ----------
244 - (void)applicationDidResignActive:(NSNotification *)notification
247 WineEventQueue* queue;
249 [self invalidateGotFocusEvents];
251 event.type = APP_DEACTIVATED;
254 [eventQueuesLock lock];
255 for (queue in eventQueues)
256 [queue postEvent:&event];
257 [eventQueuesLock unlock];
260 - (void)applicationWillFinishLaunching:(NSNotification *)notification
262 NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
264 [nc addObserverForName:NSWindowDidBecomeKeyNotification
267 usingBlock:^(NSNotification *note){
268 NSWindow* window = [note object];
269 [keyWindows removeObjectIdenticalTo:window];
270 [keyWindows insertObject:window atIndex:0];
273 [nc addObserverForName:NSWindowWillCloseNotification
275 queue:[NSOperationQueue mainQueue]
276 usingBlock:^(NSNotification *note){
277 NSWindow* window = [note object];
278 [keyWindows removeObjectIdenticalTo:window];
282 selector:@selector(keyboardSelectionDidChange)
283 name:NSTextInputContextKeyboardSelectionDidChangeNotification
286 /* The above notification isn't sent unless the NSTextInputContext
287 class has initialized itself. Poke it. */
288 [NSTextInputContext self];
290 self.keyboardType = LMGetKbdType();
295 /***********************************************************************
298 * Run a block on the main thread synchronously.
300 void OnMainThread(dispatch_block_t block)
302 dispatch_sync(dispatch_get_main_queue(), block);
305 /***********************************************************************
308 * Run a block on the main thread asynchronously.
310 void OnMainThreadAsync(dispatch_block_t block)
312 dispatch_async(dispatch_get_main_queue(), block);
315 /***********************************************************************
318 void LogError(const char* func, NSString* format, ...)
321 va_start(args, format);
322 LogErrorv(func, format, args);
326 /***********************************************************************
329 void LogErrorv(const char* func, NSString* format, va_list args)
331 NSString* message = [[NSString alloc] initWithFormat:format arguments:args];
332 fprintf(stderr, "err:%s:%s", func, [message UTF8String]);
336 /***********************************************************************
337 * macdrv_window_rejected_focus
339 * Pass focus to the next window that hasn't already rejected this same
340 * WINDOW_GOT_FOCUS event.
342 void macdrv_window_rejected_focus(const macdrv_event *event)
345 [NSApp windowRejectedFocusEvent:event];
349 /***********************************************************************
350 * macdrv_get_keyboard_layout
352 * Returns the keyboard layout uchr data.
354 CFDataRef macdrv_copy_keyboard_layout(CGEventSourceKeyboardType* keyboard_type, int* is_iso)
356 __block CFDataRef result = NULL;
359 TISInputSourceRef inputSource;
361 inputSource = TISCopyCurrentKeyboardLayoutInputSource();
364 CFDataRef uchr = TISGetInputSourceProperty(inputSource,
365 kTISPropertyUnicodeKeyLayoutData);
366 result = CFDataCreateCopy(NULL, uchr);
367 CFRelease(inputSource);
369 *keyboard_type = ((WineApplication*)NSApp).keyboardType;
370 *is_iso = (KBGetLayoutType(*keyboard_type) == kKeyboardISO);