30 #import "../../idlib/precompiled.h"
36 #import <Foundation/Foundation.h>
37 #import <Carbon/Carbon.h>
38 #import <AppKit/AppKit.h>
46 #import <sys/ucontext.h>
47 #import <mach/thread_status.h>
51 static idStr savepath;
55 #define TEST_FPU_EXCEPTIONS \
56 FPU_EXCEPTION_INVALID_OPERATION | \
57 FPU_EXCEPTION_DENORMALIZED_OPERAND | \
58 FPU_EXCEPTION_DIVIDE_BY_ZERO | \
64 #define kRegKey @"RegCode"
66 static const ControlID kRegCode1EditText = {
'RegC', 1 };
76 static OSErr DoRegCodeDialog(
char* ioRegCode1 );
100 #define MAX_ARGC 1024
102 - (
void)applicationDidFinishLaunching:(NSNotification *)notification;
105 NSAssert(
sizeof(
bool) == 1,
@"sizeof(bool) should equal 1 byte");
108 Sys_Error( (
const char *)[ [ localException reason ] cString ] );
113 - (
void)applicationWillHide:(NSNotification *)notification;
118 - (
void)applicationWillUnhide:(NSNotification *)notification;
123 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
126 return NSTerminateLater;
132 - (IBAction)paste:(
id)sender;
134 int shiftWasDown, insertWasDown;
135 unsigned int currentTime;
150 extern void CL_Quit_f(
void);
154 - (IBAction)requestTerminate:(
id)sender;
162 static bool hasShownBanner = NO;
164 if (!hasShownBanner) {
167 hasShownBanner = YES;
171 NSPanel *splashPanel;
172 NSImage *bannerImage;
174 NSImageView *bannerImageView;
176 bannerImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:@"banner.jpg"]];
179 splashPanel = [[NSPanel alloc] initWithContentRect:bannerRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
181 bannerImageView = [[NSImageView alloc] initWithFrame:bannerRect];
182 [bannerImageView setImage:bannerImage];
183 [splashPanel setContentView:bannerImageView];
184 [bannerImageView release];
186 [splashPanel center];
187 [splashPanel setHasShadow:YES];
188 [splashPanel orderFront: nil];
189 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.5]];
192 [bannerImage release];
199 - (
void)connectToServer:(NSPasteboard *)pasteboard userData:(NSString *)data error:(NSString **)error;
201 NSArray *pasteboardTypes;
203 pasteboardTypes = [pasteboard types];
204 if ([pasteboardTypes containsObject:NSStringPboardType]) {
205 NSString *requestedServer;
207 requestedServer = [pasteboard stringForType:NSStringPboardType];
208 if (requestedServer) {
209 Cbuf_AddText(
va(
"connect %s\n", [requestedServer cString]));
213 *error =
@"Unable to connect to server: could not find string on pasteboard";
216 - (
void)performCommand:(NSPasteboard *)pasteboard userData:(NSString *)data error:(NSString **)error;
218 NSArray *pasteboardTypes;
220 pasteboardTypes = [pasteboard types];
221 if ([pasteboardTypes containsObject:NSStringPboardType]) {
222 NSString *requestedCommand;
224 requestedCommand = [pasteboard stringForType:NSStringPboardType];
225 if (requestedCommand) {
226 Cbuf_AddText(
va(
"%s\n", [requestedCommand cString]));
230 *error =
@"Unable to perform command: could not find string on pasteboard";
233 #endif // commented out all the banners and actions
241 NSAutoreleasePool *pool;
243 const char *argv[MAX_ARGC];
244 NSProcessInfo *processInfo;
246 unsigned int argumentIndex, argumentCount;
251 pool = [[NSAutoreleasePool alloc] init];
253 [NSApp setServicesProvider:self];
255 processInfo = [NSProcessInfo processInfo];
256 arguments = [processInfo arguments];
257 argumentCount = [arguments count];
258 for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
259 argv[argc++] =
strdup([[arguments objectAtIndex:argumentIndex] cString]);
261 if (![[NSFileManager defaultManager] changeCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]]) {
262 Sys_Error(
"Could not access application resources");
328 if ( [
self checkDVD] ==
FALSE) {
337 if ( argc > 1 &&
strncmp( argv[ 1 ],
"-psn", 4 ) ) {
345 [NSApp activateIgnoringOtherApps:YES];
349 OTPeriodicTimerReset();
350 OTNodeStart(RootNode);
360 pool = [[NSAutoreleasePool alloc] init];
362 OTNodeStop(RootNode);
373 NSUserDefaults *userDefaults;
375 userDefaults = [NSUserDefaults standardUserDefaults];
376 cdKey = [userDefaults stringForKey:kRegKey];
379 if ( cdKey == nil || [cdKey
length] == 0 ) {
381 if ( DoRegCodeDialog( regCode ) != noErr ) {
385 [userDefaults setObject:[NSString stringWithCString: regCode] forKey:kRegKey];
386 [userDefaults synchronize];
395 long gestaltOSVersion;
396 err = Gestalt(gestaltSystemVersion, &gestaltOSVersion);
397 if ( err || gestaltOSVersion < 0x1038 ) {
398 NSBundle *thisBundle = [ NSBundle mainBundle ];
399 NSString *messsage = [ thisBundle localizedStringForKey:@"InsufficientOS" value:@"No translation" table:nil ];
400 NSRunAlertPanel(@
GAME_NAME, messsage, nil, nil, nil);
419 static char exepath[ 1024 ];
420 strncpy( exepath, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], 1024 );
430 #if defined( ID_DEMO_BUILD )
431 sprintf( savepath,
"%s/Library/Application Support/Doom 3 Demo", [NSHomeDirectory() cString] );
433 sprintf( savepath,
"%s/Library/Application Support/Doom 3", [NSHomeDirectory() cString] );
435 return savepath.
c_str();
444 static char basepath[ 1024 ];
445 strncpy( basepath, [ [ [ NSBundle mainBundle ] bundlePath ] cString ], 1024 );
446 char *snap = strrchr( basepath,
'/' );
473 #elif defined(__i386__)
476 return static_cast<cpuid_t>(cpuid);
486 return "ppc CPU with AltiVec extensions";
487 #elif defined(__i386__)
488 return "x86 CPU with MMX/SSE/SSE2/SSE3 extensions";
503 #define fegetenvd(x) asm volatile( "mffs %0" : "=f" (x) );
504 #define fesetenvd(x) asm volatile( "mtfsf 255,%0" : : "f" (x) );
522 static int exception_mask = 0;
569 #elif defined(__ppc__)
575 if ( exceptions & FPU_EXCEPTION_DIVIDE_BY_ZERO ) {
578 if ( exceptions & FPU_EXCEPTION_NUMERIC_OVERFLOW ) {
581 if ( exceptions & FPU_EXCEPTION_NUMERIC_UNDERFLOW ) {
584 if ( exceptions & FPU_EXCEPTION_INEXACT_RESULT ) {
587 Sys_Printf(
"Sys_FPUEnableExceptions: 0x%x\n", exception_mask );
589 feclearexcept( FE_ALL_EXCEPT );
593 t.
i.
lo |= exception_mask;
607 ppc_float_state_t *fs;
608 ppc_thread_state_t *ss;
610 fs = &( (
struct ucontext *)context )->uc_mcontext->fs;
611 ss = &( (
struct ucontext *)context )->uc_mcontext->ss;
613 Sys_Printf(
"FPE at 0x%x:\n", info->si_addr );
615 ret = fetestexcept( FE_ALL_EXCEPT );
616 if ( ret & FE_INEXACT ) {
619 if ( ret & FE_DIVBYZERO ) {
622 if ( ret & FE_UNDERFLOW ) {
625 if ( ret & FE_OVERFLOW ) {
628 if ( ret & FE_INVALID ) {
633 feclearexcept( FE_ALL_EXCEPT );
635 fs->fpscr &= exception_mask;
647 #warning FIXME: implement Sys_GetClockTicks
659 long gestaltSpeed, ioKitSpeed = -1;
667 Gestalt(gestaltSystemVersion, &osVers);
668 if (osVers >= 0x1030)
669 err = Gestalt(gestaltProcClkSpeedMHz, &gestaltSpeed);
672 err = Gestalt(gestaltProcClkSpeed, &gestaltSpeed);
674 gestaltSpeed = gestaltSpeed / 1000000;
680 CFMutableDictionaryRef matchDict = nil;
681 io_iterator_t itThis;
682 io_service_t service = nil;
684 if (IOMasterPort(MACH_PORT_NULL, &masterPort))
687 matchDict = IOServiceNameMatching(
"cpus");
688 if (IOServiceGetMatchingServices(masterPort, matchDict, &itThis))
691 service = IOIteratorNext(itThis);
694 io_service_t ioCpu =
NULL;
695 if (IORegistryEntryGetChildEntry(service, kIODeviceTreePlane, &ioCpu))
700 CFDataRef data = (CFDataRef)IORegistryEntryCreateCFProperty(ioCpu, CFSTR(
"clock-frequency"),kCFAllocatorDefault,0);
702 ioKitSpeed = *((
unsigned long*)CFDataGetBytePtr(data)) / 1000000;
704 service = IOIteratorNext(itThis);
710 return ( ioKitSpeed > gestaltSpeed ? ioKitSpeed : gestaltSpeed ) * 1000000.f;
722 if ( Gestalt( gestaltPhysicalRAMSize, &ramSize ) == noErr ) {
723 return ramSize / (1024*1024);
738 long vramStorage = 64;
739 const short MAXDISPLAYS = 8;
740 CGDisplayCount displayCount;
741 io_service_t dspPorts[MAXDISPLAYS];
742 CGDirectDisplayID displays[MAXDISPLAYS];
744 CGGetOnlineDisplayList( MAXDISPLAYS, displays, &displayCount );
746 for ( i = 0; i < displayCount; i++ ) {
748 dspPorts[i] = CGDisplayIOServicePort(displays[i]);
749 typeCode = IORegistryEntryCreateCFProperty( dspPorts[i], CFSTR(
"IOFBMemorySize"), kCFAllocatorDefault, kNilOptions );
750 if( typeCode && CFGetTypeID( typeCode ) == CFNumberGetTypeID() ) {
751 CFNumberGetValue( ( CFNumberRef )typeCode, kCFNumberSInt32Type, &vramStorage );
752 vramStorage /= (1024*1024);
763 Gestalt(gestaltNativeCPUtype, &cpu);
766 oldArchitecture = cpuId < gestaltCPU970;
774 io_iterator_t itThis;
775 io_service_t service;
781 err = IOMasterPort(MACH_PORT_NULL, &masterPort);
785 err = IOServiceGetMatchingServices(masterPort, IOServiceMatching(
"IOPCIDevice"), &itThis);
794 service = IOIteratorNext(itThis);
801 CFDataRef classCode = (CFDataRef)IORegistryEntryCreateCFProperty(service,CFSTR(
"class-code"),kCFAllocatorDefault,0);
804 if((*(UInt32*)CFDataGetBytePtr(classCode) & 0x00ff0000) == 0x00030000)
807 IORegistryEntryGetName(service, dName);
809 CFDataRef vendorID, deviceID;
812 vendorID = (CFDataRef)IORegistryEntryCreateCFProperty(service, CFSTR(
"vendor-id"),kCFAllocatorDefault,0);
813 deviceID = (CFDataRef)IORegistryEntryCreateCFProperty(service, CFSTR(
"device-id"),kCFAllocatorDefault,0);
815 outVendorId = *((
long*)CFDataGetBytePtr(vendorID));
816 outDeviceId = *((
long*)CFDataGetBytePtr(deviceID));
821 CFRelease(classCode);
824 if (outVendorId != -1)
837 int main(
int argc,
const char *argv[] ) {
838 return NSApplicationMain( argc, argv );
848 char*
dst = outRegCode;
849 const char*
src = inRegCode;
854 else if (*src !=
' ')
861 return strlen(outRegCode) == 18;
869 static pascal OSStatus RegCodeHandler( EventHandlerCallRef inHandler, EventRef inEvent,
void* inUserData )
871 #pragma unused( inHandler )
877 OSStatus
result = eventNotHandledErr;
880 GetEventParameter( inEvent, kEventParamDirectObject, typeHICommand,
NULL,
sizeof( cmd ),
NULL, &cmd );
882 switch ( cmd.commandID ) {
888 char strippedKey[256];
891 strippedKey[0] = doomKey[0] =
NULL;
892 GetControlData ( regCodeInfo->
regCode1EditText, kControlEntireControl, kControlEditTextTextTag, 256, cntrl, &actualSize );
893 cntrl[actualSize] =
NULL;
895 strncat( doomKey, strippedKey, 16 );
896 strcat( doomKey,
" " );
897 strncat( doomKey, strippedKey + 16, 2 );
905 unsigned char theError[512];
906 unsigned char theExplanation[512];
907 CFStringRef theErrorStr = CFCopyLocalizedString( CFSTR(
"DVD_KEY_ERROR"),
"" );
908 CFStringRef theExplanationStr = CFCopyLocalizedString( CFSTR(
"DVD_KEY_EXPLANATION"),
"" );
909 c2pstrcpy( theError, CFStringGetCStringPtr( theErrorStr, kCFStringEncodingMacRoman ) );
910 c2pstrcpy( theExplanation, CFStringGetCStringPtr( theExplanationStr, kCFStringEncodingMacRoman ) );
912 StandardAlert(kAlertStopAlert, theError, theExplanation,
NULL,
NULL);
915 ClearKeyboardFocus(regCodeInfo->
window);
917 ControlEditTextSelectionRec sel = {0, 32000};
918 SetControlData (regCodeInfo->
regCode1EditText, kControlEntireControl, kControlEditTextSelectionTag,
sizeof(sel), &sel);
923 QuitAppModalLoopForWindow( regCodeInfo->
window );
928 case kHICommandCancel:
930 QuitAppModalLoopForWindow( regCodeInfo->
window );
944 static OSErr DoRegCodeDialog(
char* ioRegCode1 )
948 memset(®CodeInfo, 0,
sizeof(regCodeInfo));
951 CFBundleRef
theBundle = CFBundleGetMainBundle();
952 err = CreateNibReferenceWithCFBundle( theBundle, CFSTR(
"ASLCore"), &aslNib );
953 err = ::CreateWindowFromNib( aslNib, CFSTR(
"Reg Code Sheet"), ®CodeInfo.
window );
960 ControlEditTextSelectionRec sel = {0, 32000};
961 SetControlData (regCodeInfo.
regCode1EditText, kControlEntireControl, kControlEditTextSelectionTag,
sizeof(sel), &sel);
963 EventTypeSpec
cmdEvent = { kEventClassCommand, kEventCommandProcess };
964 EventHandlerUPP
handler = NewEventHandlerUPP( RegCodeHandler );
968 ShowWindow( regCodeInfo.
window );
970 RunAppModalLoopForWindow( regCodeInfo.
window );
972 DisposeWindow( regCodeInfo.
window );
978 return regCodeInfo.
okPressed ? (OSErr)noErr : (OSErr)userCanceledErr;
991 pthread_testcancel();
1015 #elif defined(__i386__)
1017 #include <xmmintrin.h>
1026 uint32_t enable_l = (uint32_t) enable;
1028 enable_l = enable_l & 1;
1029 enable_l = enable_l << 6;
1030 dwData = _mm_getcsr();
1031 dwData = dwData & 0xffbf;
1032 dwData = dwData | enable_l;
1044 uint32_t enable_l = (uint32_t) enable;
1046 enable_l = enable_l & 1;
1047 enable_l = enable_l << 15;
1048 dwData = _mm_getcsr();
1049 dwData = dwData & 0x7fff;
1050 dwData = dwData | enable_l;
GetControlByID(prefInfo.window,&kFullscreenBtn,&prefInfo.fullscreenBtn)
const char * Sys_EXEPath(void)
int Sys_GetSystemRam(void)
assert(prefInfo.fullscreenBtn)
void OSX_GetVideoCard(int &outVendorId, int &outDeviceId)
const char * Sys_DefaultBasePath(void)
void Sys_ShutdownInput(void)
void Sys_Printf(const char *msg,...)
double Sys_ClockTicksPerSecond(void)
int Sys_Milliseconds(void)
CGDirectDisplayID Sys_DisplayToUse(void)
bool OSX_GetCPUIdentification(int &cpuId, bool &oldArchitecture)
const char * Sys_GetProcessorString(void)
void Sys_FPU_SetDAZ(bool enable)
cpuid_t Sys_GetProcessorId(void)
virtual void Async(void)=0
virtual void Frame(void)=0
InstallWindowEventHandler(prefInfo.window, handler, 1,&cmdEvent,&prefInfo, NULL)
int main(int argc, const char *argv[])
void Sys_AsyncThread(void)
ControlRef regCode1EditText
void Posix_EarlyInit(void)
void Posix_Shutdown(void)
int Sys_GetVideoRam(void)
bool FormatRegCode(const char *inRegCode, char *outRegCode)
GLenum GLsizei GLsizei height
virtual bool CheckKey(const char *key, bool netConnect, bool offline_valid[2])=0
static WindowRef ValidModeCallbackProc inCallback OSStatus err
void Posix_QueEvent(sysEventType_t type, int value, int value2, int ptrLength, void *ptr)
void Sys_TriggerEvent(int index)
double Sys_GetClockTicks(void)
GLsizei const GLcharARB const GLint * length
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
const char * c_str(void) const
void Sys_Error(const char *error,...)
void Sys_FPE_handler(int signum, siginfo_t *info, void *context)
void Sys_FPU_SetFTZ(bool enable)
virtual void Quit(void)=0
void Sys_FPU_EnableExceptions(int exceptions)
char * va(const char *fmt,...)
RepositionWindow(prefInfo.window, NULL, kWindowAlertPositionOnMainScreen)
int sprintf(idStr &string, const char *fmt,...)
virtual void Init(int argc, const char **argv, const char *cmdline)=0
const char * Sys_DefaultSavePath(void)
void Posix_LateInit(void)