Alexandre Lision | 8af73cb | 2013-12-10 14:11:20 -0500 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2011-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/os.h> |
| 20 | #include "TargetConditionals.h" |
| 21 | |
| 22 | #if TARGET_OS_IPHONE |
| 23 | |
| 24 | PJ_DEF(int) pj_run_app(pj_main_func_ptr main_func, int argc, char *argv[], |
| 25 | unsigned flags) |
| 26 | { |
| 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 | |
| 84 | pool = [[NSAutoreleasePool alloc] init]; |
| 85 | [NSApplication sharedApplication]; |
| 86 | [DeadThread enterMultiThreadedMode]; |
| 87 | |
| 88 | param.argc = argc; |
| 89 | param.argv = (char **)argv; |
| 90 | param.main_func = main_func; |
| 91 | if (pthread_create(&thread, NULL, &main_thread, ¶m) == 0) { |
| 92 | CFRunLoopRun(); |
| 93 | } |
| 94 | |
| 95 | PJ_UNUSED_ARG(pool); |
| 96 | |
| 97 | return param.retval; |
| 98 | } |
| 99 | |
| 100 | #endif |