osx_main.m

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1997-2001 Id Software, Inc.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include <stdlib.h>
00027 #include <fcntl.h>
00028 #include <stdarg.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <errno.h>
00032 
00033 #include <dlfcn.h>
00034 
00035 #include "../../common/common.h"
00036 #include "../system.h"
00037 #include "osx_main.h"
00038 
00039 @interface NSApplication(SDL_Missing_Methods)
00040 - (void)setAppleMenu:(NSMenu *)menu;
00041 @end
00042 
00043 
00044 typedef struct CPSProcessSerNum
00045 {
00046     UInt32      lo;
00047     UInt32      hi;
00048 } CPSProcessSerNum;
00049 
00050 extern OSErr CPSGetCurrentProcess(CPSProcessSerNum *psn);
00051 extern OSErr CPSEnableForegroundOperation(CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
00052 extern OSErr CPSSetFrontProcess(CPSProcessSerNum *psn);
00053 
00054 /* ======================================================================= */
00055 /* General routines */
00056 /* ======================================================================= */
00057 
00058 void Sys_Init (void)
00059 {
00060     sys_os = Cvar_Get("sys_os", "macosx", CVAR_SERVERINFO, NULL);
00061     sys_affinity = Cvar_Get("sys_affinity", "0", CVAR_ARCHIVE, NULL);
00062     sys_priority = Cvar_Get("sys_priority", "0", CVAR_ARCHIVE, "Process nice level");
00063 }
00064 
00065 static void InitCocoa (void)
00066 {
00067     void (*nsappload)(void);
00068     void* cocoa_lib;
00069     /* @todo: Don't hardcode the path - let configure decide */
00070     cocoa_lib = dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY);
00071     if (!cocoa_lib)
00072         Sys_Error("InitCocoa: Could not load cocoa framework\n");
00073     nsappload = (void(*)()) dlsym(cocoa_lib, "NSApplicationLoad");
00074     nsappload();
00075 }
00076 
00081 static void SetWorkingDirectory (const char **argv)
00082 {
00083     char newPath[MAX_OSPATH];
00084     NSBundle *mainBundle;
00085     int strLength = strlen(*argv);
00086 
00087     Com_DPrintf(DEBUG_SYSTEM, "Path Length : %d \n", strLength);
00088     mainBundle = [NSBundle mainBundle];
00089 
00090     if (strLength >= sizeof(newPath)) {
00091         Com_Printf("Path is too long. Please copy Bundle to a shorter path location \n");
00092     } else {
00093         if (mainBundle != NULL) {
00094             strncpy(newPath, [[mainBundle bundlePath] UTF8String], sizeof(newPath));
00095             Com_DPrintf(DEBUG_SYSTEM, "%s = new path\n", newPath);
00096             chdir(newPath);
00097         } else {
00098             Com_Printf("Main bundle appears to be NULL!\n");
00099         }
00100     }
00101 }
00102 @implementation osx_main
00103 @end
00104 @implementation SDLApplication : NSApplication
00105 @end
00106 
00113 #ifdef main
00114 #undef main
00115 #endif
00116 int main (int argc, const char **argv)
00117 {
00118     /* create Autorelease Pool, to avoid Error Messages under MacOSX */
00119     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
00120 
00121     InitCocoa();
00122 
00123     [SDLApplication sharedApplication];
00124     {
00125         CPSProcessSerNum PSN;
00126         if (!CPSGetCurrentProcess(&PSN))
00127             if (!CPSEnableForegroundOperation(&PSN, 0x03, 0x3C, 0x2C, 0x1103))
00128                 if (!CPSSetFrontProcess(&PSN))
00129                     [SDLApplication sharedApplication];
00130     }
00131 
00132     SetWorkingDirectory(argv);
00133 
00134     Sys_ConsoleInit();
00135     Qcommon_Init(argc, argv);
00136     fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
00137 
00138     while (1)
00139         Qcommon_Frame();
00140 
00141     /* Free the Release Pool resources */
00142     [pool release];
00143     return 0;
00144 }

Generated by  doxygen 1.6.2