2 * MACDRV Cocoa initialization code
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 <AppKit/AppKit.h>
23 #include "macdrv_cocoa.h"
27 /* Condition values for an NSConditionLock. Used to signal between run_cocoa_app
28 and macdrv_start_cocoa_app so the latter knows when the former is running
29 the application event loop. */
31 COCOA_APP_NOT_RUNNING,
36 /***********************************************************************
39 * Transforms the main thread from merely idling in its run loop to
40 * being a Cocoa application running its event loop.
42 * This will be the perform callback of a custom run loop source that
43 * will be scheduled in the main thread's run loop from a secondary
44 * thread by macdrv_start_cocoa_app. This function communicates that
45 * it has successfully started the application by changing the condition
46 * of a shared NSConditionLock, passed in via the info parameter.
48 * This function never returns. It's the new permanent home of the
51 static void run_cocoa_app(void* info)
53 NSConditionLock* lock = info;
55 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
57 [WineApplication sharedApplication];
58 [NSApp setDelegate:(WineApplication*)NSApp];
60 /* Retain the lock while we're using it, so macdrv_start_cocoa_app()
61 doesn't deallocate it in the middle of us unlocking it. */
64 [lock unlockWithCondition:COCOA_APP_RUNNING];
74 /***********************************************************************
75 * macdrv_start_cocoa_app
77 * Tells the main thread to transform itself into a Cocoa application.
79 * Returns 0 on success, non-zero on failure.
81 int macdrv_start_cocoa_app(void)
84 CFRunLoopSourceRef source;
85 NSConditionLock* lock;
87 CFRunLoopSourceContext source_context = { 0 };
89 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
91 /* Make sure Cocoa is in multi-threading mode by detaching a
93 [NSThread detachNewThreadSelector:@selector(self)
94 toTarget:[NSThread class]
97 lock = [[NSConditionLock alloc] initWithCondition:COCOA_APP_NOT_RUNNING];
98 timeLimit = [NSDate dateWithTimeIntervalSinceNow:5];
100 source_context.info = lock;
101 source_context.perform = run_cocoa_app;
102 source = CFRunLoopSourceCreate(NULL, 0, &source_context);
104 if (source && lock && timeLimit)
106 CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopCommonModes);
107 CFRunLoopSourceSignal(source);
108 CFRunLoopWakeUp(CFRunLoopGetMain());
110 if ([lock lockWhenCondition:COCOA_APP_RUNNING beforeDate:timeLimit])