2013-01-07 21:44:36 +01:00
|
|
|
/*
|
|
|
|
* MACDRV Cocoa initialization code
|
|
|
|
*
|
|
|
|
* Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import <AppKit/AppKit.h>
|
2013-01-27 23:19:33 +01:00
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/mach_time.h>
|
2013-01-07 21:44:36 +01:00
|
|
|
|
|
|
|
#include "macdrv_cocoa.h"
|
|
|
|
#import "cocoa_app.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* Condition values for an NSConditionLock. Used to signal between run_cocoa_app
|
|
|
|
and macdrv_start_cocoa_app so the latter knows when the former is running
|
|
|
|
the application event loop. */
|
|
|
|
enum {
|
|
|
|
COCOA_APP_NOT_RUNNING,
|
|
|
|
COCOA_APP_RUNNING,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-27 23:19:33 +01:00
|
|
|
struct cocoa_app_startup_info {
|
|
|
|
NSConditionLock* lock;
|
|
|
|
unsigned long long tickcount;
|
|
|
|
uint64_t uptime_ns;
|
2013-04-16 07:38:01 +02:00
|
|
|
BOOL success;
|
2013-01-27 23:19:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-07 21:44:36 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* run_cocoa_app
|
|
|
|
*
|
|
|
|
* Transforms the main thread from merely idling in its run loop to
|
|
|
|
* being a Cocoa application running its event loop.
|
|
|
|
*
|
|
|
|
* This will be the perform callback of a custom run loop source that
|
|
|
|
* will be scheduled in the main thread's run loop from a secondary
|
|
|
|
* thread by macdrv_start_cocoa_app. This function communicates that
|
|
|
|
* it has successfully started the application by changing the condition
|
|
|
|
* of a shared NSConditionLock, passed in via the info parameter.
|
|
|
|
*
|
|
|
|
* This function never returns. It's the new permanent home of the
|
|
|
|
* main thread.
|
|
|
|
*/
|
|
|
|
static void run_cocoa_app(void* info)
|
|
|
|
{
|
2013-01-27 23:19:33 +01:00
|
|
|
struct cocoa_app_startup_info* startup_info = info;
|
|
|
|
NSConditionLock* lock = startup_info->lock;
|
2013-04-16 07:38:01 +02:00
|
|
|
BOOL created_app = FALSE;
|
2013-01-07 21:44:36 +01:00
|
|
|
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2013-04-16 07:38:01 +02:00
|
|
|
if (!NSApp)
|
|
|
|
{
|
|
|
|
[WineApplication sharedApplication];
|
|
|
|
created_app = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([NSApp respondsToSelector:@selector(setWineController:)])
|
|
|
|
{
|
|
|
|
WineApplicationController* controller = [WineApplicationController sharedController];
|
|
|
|
[NSApp setWineController:controller];
|
|
|
|
[controller computeEventTimeAdjustmentFromTicks:startup_info->tickcount uptime:startup_info->uptime_ns];
|
|
|
|
startup_info->success = TRUE;
|
|
|
|
}
|
2013-01-07 21:44:36 +01:00
|
|
|
|
|
|
|
/* Retain the lock while we're using it, so macdrv_start_cocoa_app()
|
|
|
|
doesn't deallocate it in the middle of us unlocking it. */
|
|
|
|
[lock retain];
|
|
|
|
[lock lock];
|
|
|
|
[lock unlockWithCondition:COCOA_APP_RUNNING];
|
|
|
|
[lock release];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
2013-04-16 07:38:01 +02:00
|
|
|
if (created_app && startup_info->success)
|
|
|
|
{
|
|
|
|
/* Never returns */
|
|
|
|
[NSApp run];
|
|
|
|
}
|
2013-01-07 21:44:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_start_cocoa_app
|
|
|
|
*
|
|
|
|
* Tells the main thread to transform itself into a Cocoa application.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, non-zero on failure.
|
|
|
|
*/
|
2013-01-27 23:19:33 +01:00
|
|
|
int macdrv_start_cocoa_app(unsigned long long tickcount)
|
2013-01-07 21:44:36 +01:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
CFRunLoopSourceRef source;
|
2013-01-27 23:19:33 +01:00
|
|
|
struct cocoa_app_startup_info startup_info;
|
|
|
|
uint64_t uptime_mach = mach_absolute_time();
|
|
|
|
mach_timebase_info_data_t mach_timebase;
|
2013-01-07 21:44:36 +01:00
|
|
|
NSDate* timeLimit;
|
|
|
|
CFRunLoopSourceContext source_context = { 0 };
|
|
|
|
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
/* Make sure Cocoa is in multi-threading mode by detaching a
|
|
|
|
do-nothing thread. */
|
|
|
|
[NSThread detachNewThreadSelector:@selector(self)
|
|
|
|
toTarget:[NSThread class]
|
|
|
|
withObject:nil];
|
|
|
|
|
2013-01-27 23:19:33 +01:00
|
|
|
startup_info.lock = [[NSConditionLock alloc] initWithCondition:COCOA_APP_NOT_RUNNING];
|
|
|
|
startup_info.tickcount = tickcount;
|
2013-04-16 07:38:01 +02:00
|
|
|
startup_info.success = FALSE;
|
2013-01-27 23:19:33 +01:00
|
|
|
|
|
|
|
mach_timebase_info(&mach_timebase);
|
|
|
|
startup_info.uptime_ns = uptime_mach * mach_timebase.numer / mach_timebase.denom;
|
|
|
|
|
2013-01-07 21:44:36 +01:00
|
|
|
timeLimit = [NSDate dateWithTimeIntervalSinceNow:5];
|
|
|
|
|
2013-01-27 23:19:33 +01:00
|
|
|
source_context.info = &startup_info;
|
2013-01-07 21:44:36 +01:00
|
|
|
source_context.perform = run_cocoa_app;
|
|
|
|
source = CFRunLoopSourceCreate(NULL, 0, &source_context);
|
|
|
|
|
2013-01-27 23:19:33 +01:00
|
|
|
if (source && startup_info.lock && timeLimit)
|
2013-01-07 21:44:36 +01:00
|
|
|
{
|
|
|
|
CFRunLoopAddSource(CFRunLoopGetMain(), source, kCFRunLoopCommonModes);
|
|
|
|
CFRunLoopSourceSignal(source);
|
|
|
|
CFRunLoopWakeUp(CFRunLoopGetMain());
|
|
|
|
|
2013-01-27 23:19:33 +01:00
|
|
|
if ([startup_info.lock lockWhenCondition:COCOA_APP_RUNNING beforeDate:timeLimit])
|
2013-01-07 21:44:36 +01:00
|
|
|
{
|
2013-01-27 23:19:33 +01:00
|
|
|
[startup_info.lock unlock];
|
2013-04-16 07:38:01 +02:00
|
|
|
ret = !startup_info.success;
|
2013-01-07 21:44:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source)
|
|
|
|
CFRelease(source);
|
2013-01-27 23:19:33 +01:00
|
|
|
[startup_info.lock release];
|
2013-01-07 21:44:36 +01:00
|
|
|
[pool release];
|
|
|
|
return ret;
|
|
|
|
}
|