2003-07-15 09:44:02 +00:00
|
|
|
/*
|
|
|
|
|
* ArchHooks_darwin.cpp
|
|
|
|
|
* stepmania
|
|
|
|
|
*
|
|
|
|
|
* Created by Steve Checkoway on Tue Jul 15 2003.
|
|
|
|
|
* Copyright (c) 2003 Steve Checkoway. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
|
#include "ArchHooks_darwin.h"
|
2003-07-15 13:35:07 +00:00
|
|
|
#include "RageLog.h"
|
2004-03-28 07:17:13 +00:00
|
|
|
#include "RageThreads.h"
|
|
|
|
|
#include "RageUtil.h"
|
|
|
|
|
#include "RageTimer.h"
|
2003-07-31 22:41:26 +00:00
|
|
|
#include "archutils/Darwin/Crash.h"
|
|
|
|
|
#include "archutils/Unix/CrashHandler.h"
|
2004-04-16 07:16:37 +00:00
|
|
|
#include "archutils/Unix/SignalHandler.h"
|
2004-03-11 23:58:49 +00:00
|
|
|
#include "StepMania.h"
|
2004-03-28 07:17:13 +00:00
|
|
|
#define Random Random_ // work around namespace pollution
|
2003-07-15 12:50:50 +00:00
|
|
|
#include <Carbon/Carbon.h>
|
2004-03-28 07:17:13 +00:00
|
|
|
#undef Random_
|
|
|
|
|
#include <mach/thread_act.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/sysctl.h>
|
2003-07-15 12:50:50 +00:00
|
|
|
|
2003-07-16 11:33:34 +00:00
|
|
|
/* You would think that these would be defined somewhere. */
|
2003-10-07 07:04:34 +00:00
|
|
|
enum
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
kMacOSX_10_2 = 0x1020,
|
|
|
|
|
kMacOSX_10_3 = 0x1030
|
|
|
|
|
};
|
|
|
|
|
|
2004-03-28 07:17:13 +00:00
|
|
|
static thread_time_constraint_policy g_oldttcpolicy;
|
|
|
|
|
static float g_fStartedTimeCritAt;
|
|
|
|
|
|
2003-09-24 09:46:52 +00:00
|
|
|
SInt16 ShowAlert(int type, CFStringRef message, CFStringRef OK, CFStringRef cancel = NULL)
|
|
|
|
|
{
|
|
|
|
|
struct AlertStdCFStringAlertParamRec params = {kStdCFStringAlertVersionOne, true, false, OK, cancel, NULL,
|
|
|
|
|
kAlertStdAlertOKButton, kAlertStdAlertCancelButton, kWindowAlertPositionParentWindowScreen, NULL};
|
|
|
|
|
DialogRef dialog;
|
|
|
|
|
SInt16 result;
|
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
|
|
CreateStandardAlert(type, message, NULL, ¶ms, &dialog);
|
|
|
|
|
err = AutoSizeDialog(dialog);
|
|
|
|
|
ASSERT(err == noErr);
|
|
|
|
|
RunStandardAlert(dialog, NULL, &result);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2003-07-15 18:11:17 +00:00
|
|
|
|
2004-04-16 07:16:37 +00:00
|
|
|
/* The signal handler is only used to trap SIGABRT; for other signals, just return
|
|
|
|
|
* and the exceptoin handler will take care of it. */
|
|
|
|
|
static void DoCrashSignalHandler( int signal, siginfo_t *si, const ucontext_t *uc )
|
|
|
|
|
{
|
|
|
|
|
if( signal != SIGABRT )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CrashSignalHandler( signal, si, uc );
|
|
|
|
|
/* not reached */
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-23 21:53:11 +00:00
|
|
|
ArchHooks_darwin::ArchHooks_darwin()
|
|
|
|
|
{
|
2004-03-25 12:48:24 +00:00
|
|
|
CrashHandlerHandleArgs(g_argc, g_argv);
|
|
|
|
|
InstallExceptionHandler(CrashExceptionHandler);
|
2004-04-16 07:16:37 +00:00
|
|
|
SignalHandler::OnClose( DoCrashSignalHandler );
|
2004-03-28 07:17:13 +00:00
|
|
|
TimeCritMutex = new RageMutex("TimeCritMutex");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ArchHooks_darwin::~ArchHooks_darwin()
|
|
|
|
|
{
|
|
|
|
|
delete TimeCritMutex;
|
2003-07-15 18:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
2003-07-16 11:33:34 +00:00
|
|
|
#define CASE_GESTALT_M(str,code,result) case gestalt##code: str = result; break
|
|
|
|
|
#define CASE_GESTALT(str,code) CASE_GESTALT_M(str, code, #code)
|
|
|
|
|
|
2003-07-23 21:53:11 +00:00
|
|
|
void ArchHooks_darwin::DumpDebugInfo()
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
CString systemVersion;
|
|
|
|
|
long ram;
|
|
|
|
|
long vRam;
|
|
|
|
|
long processorSpeed;
|
|
|
|
|
CString processor;
|
|
|
|
|
long numProcessors;
|
|
|
|
|
CString machine;
|
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
|
|
OSErr err = noErr;
|
|
|
|
|
long code;
|
|
|
|
|
|
|
|
|
|
/* Get system version */
|
|
|
|
|
err = Gestalt(gestaltSystemVersion, &code);
|
2003-08-11 07:13:46 +00:00
|
|
|
if (err == noErr)
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
systemVersion = "Mac OS X ";
|
2003-08-11 07:13:46 +00:00
|
|
|
if (code >= kMacOSX_10_2 && code < kMacOSX_10_3)
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
systemVersion += "10.2.";
|
|
|
|
|
asprintf(&temp, "%d", code - kMacOSX_10_2);
|
|
|
|
|
systemVersion += temp;
|
|
|
|
|
free(temp);
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
systemVersion += "10.3";
|
|
|
|
|
int ssv = code - kMacOSX_10_3;
|
|
|
|
|
if (ssv > 9)
|
|
|
|
|
systemVersion += "+";
|
|
|
|
|
else {
|
|
|
|
|
asprintf(&temp, ".%d", ssv);
|
|
|
|
|
systemVersion += temp;
|
|
|
|
|
free(temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
2003-07-16 11:33:34 +00:00
|
|
|
systemVersion = "Unknown system version";
|
|
|
|
|
|
|
|
|
|
/* Get memory */
|
|
|
|
|
err = Gestalt(gestaltLogicalRAMSize, &vRam);
|
|
|
|
|
if (err != noErr)
|
|
|
|
|
vRam = 0;
|
|
|
|
|
err = Gestalt(gestaltPhysicalRAMSize, &ram);
|
2003-08-11 07:13:46 +00:00
|
|
|
if (err == noErr)
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
vRam -= ram;
|
|
|
|
|
if (vRam < 0)
|
|
|
|
|
vRam = 0;
|
|
|
|
|
ram /= 1048576; /* 1048576 = 1024*1024 */
|
|
|
|
|
vRam /= 1048576;
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
ram = 0;
|
|
|
|
|
vRam = 0;
|
|
|
|
|
}
|
2004-03-17 04:55:55 +00:00
|
|
|
|
|
|
|
|
/* XXX update this information for G5s */
|
2003-07-16 11:33:34 +00:00
|
|
|
/* Get processor */
|
|
|
|
|
numProcessors = MPProcessorsScheduled();
|
|
|
|
|
err = Gestalt(gestaltNativeCPUtype, &code);
|
2003-08-11 07:13:46 +00:00
|
|
|
if (err == noErr)
|
|
|
|
|
{
|
|
|
|
|
switch (code)
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
CASE_GESTALT_M(processor, CPU601, "601");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU603, "603");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU603e, "603e");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU603ev, "603ev");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU604, "604");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU604e, "604e");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU604ev, "604ev");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU750, "G3");
|
|
|
|
|
CASE_GESTALT_M(processor, CPUG4, "G4");
|
|
|
|
|
CASE_GESTALT_M(processor, CPUG47450, "G4");
|
|
|
|
|
CASE_GESTALT_M(processor, CPUApollo, "G4 (Apollo)");
|
|
|
|
|
CASE_GESTALT_M(processor, CPU750FX, "G3 (Sahara)");
|
|
|
|
|
default:
|
|
|
|
|
asprintf(&temp, "%d", code);
|
|
|
|
|
processor = temp;
|
|
|
|
|
free(temp);
|
|
|
|
|
}
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
2003-07-16 11:33:34 +00:00
|
|
|
processor = "unknown";
|
|
|
|
|
err = Gestalt(gestaltProcClkSpeed, &processorSpeed);
|
|
|
|
|
if (err != noErr)
|
|
|
|
|
processorSpeed = 0;
|
|
|
|
|
/* Get machine */
|
|
|
|
|
err = Gestalt(gestaltMachineType, &code);
|
2003-08-11 07:13:46 +00:00
|
|
|
if (err == noErr)
|
|
|
|
|
{
|
|
|
|
|
switch (code)
|
|
|
|
|
{
|
2003-07-16 11:33:34 +00:00
|
|
|
/* PowerMacs */
|
|
|
|
|
CASE_GESTALT(machine, PowerMac4400);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac4400_160);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac5200);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac5400);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac5500);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac6100_60);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac6100_66);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac6200);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac6400);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac6500);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac7100_66);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac7100_80);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac7200);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac7300);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac7500);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac8100_80);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac8100_100);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac8100_110);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac8500);
|
|
|
|
|
CASE_GESTALT(machine, PowerMac9500);
|
|
|
|
|
/* upgrade cards */
|
|
|
|
|
CASE_GESTALT(machine, PowerMacLC475);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacLC575);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra610);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra630);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra650);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra700);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra800);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra900);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacQuadra950);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacCentris610);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacCentris650);
|
|
|
|
|
/* PowerBooks */
|
|
|
|
|
CASE_GESTALT(machine, PowerBook1400);
|
|
|
|
|
CASE_GESTALT(machine, PowerBook2400);
|
|
|
|
|
CASE_GESTALT(machine, PowerBook3400);
|
|
|
|
|
CASE_GESTALT(machine, PowerBook500PPCUpgrade);
|
|
|
|
|
CASE_GESTALT(machine, PowerBookG3);
|
|
|
|
|
CASE_GESTALT(machine, PowerBookG3Series);
|
|
|
|
|
CASE_GESTALT(machine, PowerBookG3Series2);
|
|
|
|
|
/* NewWorld */
|
|
|
|
|
CASE_GESTALT(machine, PowerMacNewWorld);
|
|
|
|
|
CASE_GESTALT(machine, PowerMacG3);
|
|
|
|
|
default:
|
|
|
|
|
asprintf(&temp, "%d", code);
|
|
|
|
|
machine = temp;
|
|
|
|
|
free(temp);
|
|
|
|
|
}
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else if (err == gestaltUndefSelectorErr ) {
|
2003-07-16 11:33:34 +00:00
|
|
|
machine = "PowerMac";
|
|
|
|
|
machine += processor;
|
2003-08-11 07:13:46 +00:00
|
|
|
}
|
|
|
|
|
else
|
2003-07-16 11:33:34 +00:00
|
|
|
machine = "unknown machine";
|
|
|
|
|
|
|
|
|
|
/* Send all of the information to the log */
|
|
|
|
|
LOG->Info(machine.c_str());
|
2003-09-22 10:48:05 +00:00
|
|
|
LOG->Info("Processor: %s (%ld)", processor.c_str(), numProcessors);
|
2003-07-16 11:33:34 +00:00
|
|
|
LOG->Info("%s", systemVersion.c_str());
|
2003-09-22 10:48:05 +00:00
|
|
|
LOG->Info("Memory: %ld MB total, %ld MB swap", ram, vRam);
|
2003-07-16 11:33:34 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-15 08:54:57 +00:00
|
|
|
void ArchHooks_darwin::MessageBoxOKPrivate(CString sMessage, CString ID)
|
2003-07-23 21:53:11 +00:00
|
|
|
{
|
2003-07-15 12:50:50 +00:00
|
|
|
bool allowHush = ID != "";
|
|
|
|
|
|
|
|
|
|
if (allowHush && MessageIsIgnored(ID))
|
|
|
|
|
return;
|
2003-07-15 09:44:02 +00:00
|
|
|
|
2003-09-24 09:46:52 +00:00
|
|
|
CFStringRef message = CFStringCreateWithCString(NULL, sMessage, kCFStringEncodingASCII);
|
|
|
|
|
SInt16 result = ShowAlert(kAlertNoteAlert, message, CFSTR("OK"), CFSTR("Don't show again"));
|
2003-09-06 22:22:07 +00:00
|
|
|
|
2003-09-24 09:46:52 +00:00
|
|
|
CFRelease(message);
|
|
|
|
|
if (result == kAlertStdAlertCancelButton && allowHush)
|
2003-07-15 13:35:07 +00:00
|
|
|
IgnoreMessage(ID);
|
2003-09-24 09:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
2004-01-16 22:43:59 +00:00
|
|
|
void ArchHooks_darwin::MessageBoxErrorPrivate(CString sError, CString ID)
|
2003-09-24 09:46:52 +00:00
|
|
|
{
|
|
|
|
|
CFStringRef error = CFStringCreateWithCString(NULL, sError, kCFStringEncodingASCII);
|
|
|
|
|
ShowAlert(kAlertStopAlert, error, CFSTR("OK"));
|
|
|
|
|
|
2003-07-15 12:50:50 +00:00
|
|
|
CFRelease(error);
|
2003-07-15 09:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
2003-11-15 08:54:57 +00:00
|
|
|
ArchHooks::MessageBoxResult ArchHooks_darwin::MessageBoxAbortRetryIgnorePrivate(CString sMessage, CString ID)
|
2003-07-23 21:53:11 +00:00
|
|
|
{
|
2003-09-24 09:46:52 +00:00
|
|
|
CFStringRef error = CFStringCreateWithCString(NULL, sMessage, kCFStringEncodingASCII);
|
|
|
|
|
SInt16 result = ShowAlert(kAlertNoteAlert, error, CFSTR("Retry"), CFSTR("Ignore"));
|
|
|
|
|
ArchHooks::MessageBoxResult ret;
|
|
|
|
|
|
2003-07-15 12:50:50 +00:00
|
|
|
CFRelease(error);
|
2003-09-24 09:46:52 +00:00
|
|
|
switch (result)
|
|
|
|
|
{
|
|
|
|
|
case kAlertStdAlertOKButton:
|
|
|
|
|
ret = retry;
|
|
|
|
|
break;
|
|
|
|
|
case kAlertStdAlertCancelButton:
|
|
|
|
|
ret = ignore;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ASSERT(0);
|
2003-11-06 00:55:42 +00:00
|
|
|
ret = ignore;
|
2003-09-24 09:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
2003-07-15 18:11:17 +00:00
|
|
|
}
|
2004-03-28 07:17:13 +00:00
|
|
|
|
|
|
|
|
void ArchHooks_darwin::EnterTimeCriticalSection()
|
|
|
|
|
{
|
|
|
|
|
TimeCritMutex->Lock();
|
|
|
|
|
|
|
|
|
|
int mib[] = { CTL_HW, HW_BUS_FREQ };
|
|
|
|
|
int miblen = ARRAYSIZE( mib );
|
|
|
|
|
int bus_speed;
|
|
|
|
|
size_t len = sizeof (bus_speed);
|
|
|
|
|
if( sysctl( mib, miblen, &bus_speed, &len, NULL, 0 ) == -1 )
|
|
|
|
|
{
|
|
|
|
|
LOG->Warn( "sysctl(HW_BUS_FREQ): %s", strerror(errno) );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mach_msg_type_number_t cnt = THREAD_TIME_CONSTRAINT_POLICY_COUNT;
|
|
|
|
|
boolean_t bDefaults = false;
|
|
|
|
|
thread_policy_get( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY, (int*)&g_oldttcpolicy, &cnt, &bDefaults );
|
|
|
|
|
|
|
|
|
|
/* We want to monopolize the CPU for a very short period of time. This means that the
|
|
|
|
|
* period doesn't really matter, and we don't want to be preempted. Set the period
|
|
|
|
|
* very high (~1 second), so that if we ever lose the CPU when we shouldn't, we can
|
|
|
|
|
* detect it and log it in ExitTimeCriticalSection(). */
|
|
|
|
|
thread_time_constraint_policy ttcpolicy;
|
|
|
|
|
ttcpolicy.period = bus_speed;
|
|
|
|
|
ttcpolicy.computation = ttcpolicy.constraint = bus_speed/60;
|
|
|
|
|
ttcpolicy.preemptible = 0;
|
|
|
|
|
thread_policy_set( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY,
|
|
|
|
|
(int*)&ttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT );
|
|
|
|
|
|
|
|
|
|
g_fStartedTimeCritAt = RageTimer::GetTimeSinceStart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ArchHooks_darwin::ExitTimeCriticalSection()
|
|
|
|
|
{
|
|
|
|
|
thread_policy_set( mach_thread_self(), THREAD_TIME_CONSTRAINT_POLICY,
|
|
|
|
|
(int*) &g_oldttcpolicy, THREAD_TIME_CONSTRAINT_POLICY_COUNT );
|
|
|
|
|
TimeCritMutex->Unlock();
|
|
|
|
|
|
|
|
|
|
float fTimeCritLen = RageTimer::GetTimeSinceStart() - g_fStartedTimeCritAt;
|
|
|
|
|
if( fTimeCritLen > 0.1f )
|
|
|
|
|
LOG->Warn( "Time-critical section lasted for %f", fTimeCritLen );
|
|
|
|
|
}
|
|
|
|
|
|