| 1 |
/* |
| 2 |
* Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
#include <pj/os.h> |
| 19 |
#include "TargetConditionals.h" |
| 20 |
|
| 21 |
#if TARGET_OS_IPHONE |
| 22 |
|
| 23 |
PJ_DEF(int) pj_run_app(pj_main_func_ptr main_func, int argc, char *argv[], |
| 24 |
unsigned flags) |
| 25 |
{ |
| 26 |
PJ_UNUSED_ARG(flags); |
| 27 |
return (*main_func)(argc, argv); |
| 28 |
} |
| 29 |
|
| 30 |
#else |
| 31 |
|
| 32 |
#include <pthread.h> |
| 33 |
#include <AppKit/AppKit.h> |
| 34 |
#include <CoreFoundation/CFRunLoop.h> |
| 35 |
#include <Foundation/Foundation.h> |
| 36 |
|
| 37 |
#define THIS_FILE "os_core_darwin.m" |
| 38 |
|
| 39 |
typedef struct run_app_t { |
| 40 |
pj_main_func_ptr main_func; |
| 41 |
int argc; |
| 42 |
char **argv; |
| 43 |
int retval; |
| 44 |
} run_app_t; |
| 45 |
|
| 46 |
@interface DeadThread: NSObject { ;; } |
| 47 |
+ (void)enterMultiThreadedMode; |
| 48 |
+ (void)emptyThreadMethod:(id)obj; |
| 49 |
@end |
| 50 |
|
| 51 |
@implementation DeadThread |
| 52 |
+ (void)enterMultiThreadedMode |
| 53 |
{ |
| 54 |
[NSThread detachNewThreadSelector:@selector(emptyThreadMethod:) |
| 55 |
toTarget:[DeadThread class] withObject:nil]; |
| 56 |
} |
| 57 |
|
| 58 |
+ (void)emptyThreadMethod:(id)obj { ; } |
| 59 |
@end |
| 60 |
|
| 61 |
static void* main_thread(void *data) |
| 62 |
{ |
| 63 |
run_app_t *param = (run_app_t *)data; |
| 64 |
|
| 65 |
param->retval = (*param->main_func)(param->argc, param->argv); |
| 66 |
CFRunLoopStop(CFRunLoopGetMain()); |
| 67 |
|
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* pj_run_app() |
| 73 |
* This function has to be called from the main thread. The purpose of |
| 74 |
* this function is to initialize the application's memory pool, event |
| 75 |
* loop management, and multi-threading environment. |
| 76 |
*/ |
| 77 |
PJ_DEF(int) pj_run_app(pj_main_func_ptr main_func, int argc, char *argv[], |
| 78 |
unsigned flags) |
| 79 |
{ |
| 80 |
pthread_t thread; |
| 81 |
run_app_t param; |
| 82 |
NSAutoreleasePool *pool; |
| 83 |
PJ_UNUSED_ARG(flags); |
| 84 |
|
| 85 |
pool = [[NSAutoreleasePool alloc] init]; |
| 86 |
[NSApplication sharedApplication]; |
| 87 |
[DeadThread enterMultiThreadedMode]; |
| 88 |
|
| 89 |
param.argc = argc; |
| 90 |
param.argv = (char **)argv; |
| 91 |
param.main_func = main_func; |
| 92 |
if (pthread_create(&thread, NULL, &main_thread, ¶m) == 0) { |
| 93 |
CFRunLoopRun(); |
| 94 |
pthread_join(thread, NULL); |
| 95 |
} |
| 96 |
|
| 97 |
PJ_UNUSED_ARG(pool); |
| 98 |
|
| 99 |
return param.retval; |
| 100 |
} |
| 101 |
|
| 102 |
#endif |