Mac support progress

This commit is contained in:
Wojtek Figat
2021-12-29 19:43:53 +01:00
parent 0dbbdc9149
commit e361ab811a
15 changed files with 465 additions and 63 deletions

View File

@@ -32,15 +32,37 @@
#include <unistd.h>
#include <cstdint>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <mach/mach_time.h>
#include <mach-o/dyld.h>
#include <uuid/uuid.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreGraphics/CoreGraphics.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <IOKit/IOKitLib.h>
#if CRASH_LOG_ENABLE
#include <execinfo.h>
#endif
CPUInfo MacCpu;
Guid DeviceId;
String UserLocale, ComputerName;
byte MacAddress[6];
double SecondsPerCycle;
String ToString(CFStringRef str)
{
String result;
const int32 length = CFStringGetLength(str);
if (length > 0)
{
CFRange range = CFRangeMake(0, length);
result.ReserveSpace(length);
CFStringGetBytes(str, range, kCFStringEncodingUTF16LE, '?', false, (uint8*)result.Get(), length * sizeof(Char), nullptr);
}
return result;
}
DialogResult MessageBox::Show(Window* parent, const StringView& text, const StringView& caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
if (CommandLine::Options.Headless)
@@ -97,25 +119,39 @@ int32 MacPlatform::GetCacheLineSize()
MemoryStats MacPlatform::GetMemoryStats()
{
MISSING_CODE("MacPlatform::GetMemoryStats");
return MemoryStats(); // TODO: platform stats on Mac
MemoryStats result;
int64 value64;
size_t value64Size = sizeof(value64);
if (sysctlbyname("hw.memsize", &value64, &value64Size, nullptr, 0) != 0)
value64 = 1024 * 1024;
result.TotalPhysicalMemory = value64;
int id[] = { CTL_HW, HW_MEMSIZE };
if (sysctl(id, 2, &value64, &value64Size, nullptr, 0) != 0)
value64Size = 1024;
result.UsedPhysicalMemory = value64Size;
xsw_usage swapusage;
size_t swapusageSize = sizeof(swapusage);
result.TotalVirtualMemory = result.TotalPhysicalMemory;
result.UsedVirtualMemory = result.UsedPhysicalMemory;
if (sysctlbyname("vm.swapusage", &swapusage, &swapusageSize, nullptr, 0) == 0)
{
result.TotalVirtualMemory += swapusage.xsu_total;
result.UsedVirtualMemory += swapusage.xsu_used;
}
return result;
}
ProcessMemoryStats MacPlatform::GetProcessMemoryStats()
{
MISSING_CODE("MacPlatform::GetProcessMemoryStats");
return ProcessMemoryStats(); // TODO: platform stats on Mac
}
uint64 MacPlatform::GetCurrentProcessId()
{
return getpid();
ProcessMemoryStats result;
result.UsedPhysicalMemory = 1024;
result.UsedVirtualMemory = 1024;
return result;
}
uint64 MacPlatform::GetCurrentThreadID()
{
MISSING_CODE("MacPlatform::GetCurrentThreadID");
return 0; // TODO: threading on Mac
return (uint64)pthread_mach_thread_np(pthread_self());
}
void MacPlatform::SetThreadPriority(ThreadPriority priority)
@@ -151,7 +187,7 @@ uint64 MacPlatform::GetClockFrequency()
void MacPlatform::GetSystemTime(int32& year, int32& month, int32& dayOfWeek, int32& day, int32& hour, int32& minute, int32& second, int32& millisecond)
{
// Query for calendar time
// Query for calendar time
struct timeval time;
gettimeofday(&time, nullptr);
@@ -203,16 +239,75 @@ bool MacPlatform::Init()
SecondsPerCycle = 1e-9 * (double)info.numer / (double)info.denom;
}
// TODO: get MacCpu
// Get CPU info
int32 value32;
int64 value64;
size_t value32Size = sizeof(value32), value64Size = sizeof(value64);
if (sysctlbyname("hw.packages", &value32, &value32Size, nullptr, 0) != 0)
value32 = 1;
MacCpu.ProcessorPackageCount = value32;
if (sysctlbyname("hw.physicalcpu", &value32, &value32Size, nullptr, 0) != 0)
value32 = 1;
MacCpu.ProcessorCoreCount = value32;
if (sysctlbyname("hw.logicalcpu", &value32, &value32Size, nullptr, 0) != 0)
value32 = 1;
MacCpu.LogicalProcessorCount = value32;
if (sysctlbyname("hw.l1icachesize", &value32, &value32Size, nullptr, 0) != 0)
value32 = 0;
MacCpu.L1CacheSize = value32;
if (sysctlbyname("hw.l2cachesize", &value32, &value32Size, nullptr, 0) != 0)
value32 = 0;
MacCpu.L2CacheSize = value32;
if (sysctlbyname("hw.l3cachesize", &value32, &value32Size, nullptr, 0) != 0)
value32 = 0;
MacCpu.L3CacheSize = value32;
if (sysctlbyname("hw.pagesize", &value32, &value32Size, nullptr, 0) != 0)
value32 = vm_page_size;
MacCpu.PageSize = value32;
if (sysctlbyname("hw.cpufrequency_max", &value64, &value64Size, nullptr, 0) != 0)
value64 = GetClockFrequency();
MacCpu.ClockSpeed = value64;
if (sysctlbyname("hw.cachelinesize", &value32, &value32Size, nullptr, 0) != 0)
value32 = PLATFORM_CACHE_LINE_SIZE;
MacCpu.CacheLineSize = value32;
// TODO: get MacAddress
// TODO: get DeviceId
// Get device id
{
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
CFStringRef deviceUuid = (CFStringRef)IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
IOObjectRelease(ioRegistryRoot);
String uuidStr = ToString(deviceUuid);
Guid::Parse(uuidStr, DeviceId);
CFRelease(deviceUuid);
}
// TODO: get username
OnPlatformUserAdd(New<User>(TEXT("User")));
// Get locale
{
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef localeLang = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleLanguageCode);
CFStringRef localeCountry = (CFStringRef)CFLocaleGetValue(locale, kCFLocaleCountryCode);
UserLocale = ToString(localeLang);
String localeCountryStr = ToString(localeCountry);
if (localeCountryStr.HasChars())
UserLocale += TEXT("-") + localeCountryStr;
CFRelease(locale);
CFRelease(localeLang);
CFRelease(localeCountry);
}
// TODO: get UserLocale
// TODO: get ComputerName
// Get computer name
{
CFStringRef computerName = SCDynamicStoreCopyComputerName(nullptr, nullptr);
ComputerName = ToString(computerName);
CFRelease(computerName);
}
// Init user
{
String username;
GetEnvironmentVariable(TEXT("USER"), username);
OnPlatformUserAdd(New<User>(username));
}
Input::Mouse = New<MacMouse>();
Input::Keyboard = New<MacKeyboard>();
@@ -220,13 +315,27 @@ bool MacPlatform::Init()
return false;
}
void MacPlatform::LogInfo()
{
UnixPlatform::LogInfo();
char str[250];
size_t strSize = sizeof(str);
if (sysctlbyname("kern.osrelease", str, &strSize, nullptr, 0) != 0)
str[0] = 0;
String osRelease(str);
if (sysctlbyname("kern.osproductversion", str, &strSize, nullptr, 0) != 0)
str[0] = 0;
String osProductVer(str);
LOG(Info, "macOS {1} (kernel {0})", osRelease, osProductVer);
}
void MacPlatform::BeforeRun()
{
}
void MacPlatform::Tick()
{
// TODO: app events
}
void MacPlatform::BeforeExit()
@@ -239,8 +348,11 @@ void MacPlatform::Exit()
int32 MacPlatform::GetDpi()
{
// TODO: Screen DPI on Mac
return 96;
CGDirectDisplayID mainDisplay = CGMainDisplayID();
CGSize size = CGDisplayScreenSize(mainDisplay);
float wide = (float)CGDisplayPixelsWide(mainDisplay);
float dpi = (wide * 25.4f) / size.width;
return Math::Max(dpi, 72.0f);
}
String MacPlatform::GetUserLocaleName()
@@ -267,6 +379,17 @@ bool MacPlatform::GetHasFocus()
return WindowsManager::Windows.IsEmpty();
}
void MacPlatform::CreateGuid(Guid& result)
{
uuid_t uuid;
uuid_generate(uuid);
auto ptr = (uint32*)&uuid;
result.A = ptr[0];
result.B = ptr[1];
result.C = ptr[2];
result.D = ptr[3];
}
bool MacPlatform::CanOpenUrl(const StringView& url)
{
return false;
@@ -290,32 +413,55 @@ void MacPlatform::SetMousePosition(const Vector2& pos)
Vector2 MacPlatform::GetDesktopSize()
{
MISSING_CODE("MacPlatform::GetDesktopSize");
return Vector2(0, 0); // TODO: desktop size on Mac
CGDirectDisplayID mainDisplay = CGMainDisplayID();
return Vector2((float)CGDisplayPixelsWide(mainDisplay), (float)CGDisplayPixelsHigh(mainDisplay));
}
Rectangle GetDisplayBounds(CGDirectDisplayID display)
{
CGRect rect = CGDisplayBounds(display);
return Rectangle(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}
Rectangle MacPlatform::GetMonitorBounds(const Vector2& screenPos)
{
// TODO: do it in a proper way
CGPoint point;
point.x = screenPos.X;
point.y = screenPos.Y;
CGDirectDisplayID display;
uint32_t count = 0;
CGGetDisplaysWithPoint(point, 1, &display, &count);
if (count == 1)
return GetDisplayBounds(display);
return Rectangle(Vector2::Zero, GetDesktopSize());
}
Rectangle MacPlatform::GetVirtualDesktopBounds()
{
// TODO: do it in a proper way
return Rectangle(Vector2::Zero, GetDesktopSize());
CGDirectDisplayID displays[16];
uint32_t count = 0;
CGGetOnlineDisplayList(ARRAY_COUNT(displays), displays, &count);
if (count == 0)
return Rectangle(Vector2::Zero, GetDesktopSize());
Rectangle result = GetDisplayBounds(displays[0]);
for (uint32_t i = 1; i < count; i++)
result = Rectangle::Union(result, GetDisplayBounds(displays[i]));
return result;
}
String MacPlatform::GetMainDirectory()
{
MISSING_CODE("MacPlatform::GetMainDirectory");
return TEXT("/"); // TODO: GetMainDirectory
return StringUtils::GetDirectoryName(GetExecutableFilePath());
}
String MacPlatform::GetExecutableFilePath()
{
MISSING_CODE("MacPlatform::GetExecutableFilePath");
return TEXT("/"); // TODO: GetMainDirectory
char buf[PATH_MAX];
uint32 size = PATH_MAX;
String result;
if (_NSGetExecutablePath(buf, &size) == 0)
result.SetUTF8(buf, size);
return result;
}
Guid MacPlatform::GetUniqueDeviceId()
@@ -374,4 +520,36 @@ void* MacPlatform::GetProcAddress(void* handle, const char* symbol)
return nullptr; // TODO: dynamic libs on Mac
}
Array<MacPlatform::StackFrame> MacPlatform::GetStackFrames(int32 skipCount, int32 maxDepth, void* context)
{
Array<StackFrame> result;
#if CRASH_LOG_ENABLE
void* callstack[120];
skipCount = Math::Min<int32>(skipCount, ARRAY_COUNT(callstack));
int32 maxCount = Math::Min<int32>(ARRAY_COUNT(callstack), skipCount + maxDepth);
int32 count = backtrace(callstack, maxCount);
int32 useCount = count - skipCount;
if (useCount > 0)
{
char** names = backtrace_symbols(callstack + skipCount, useCount);
result.Resize(useCount);
for (int32 i = 0; i < useCount; i++)
{
char* name = names[i];
StackFrame& frame = result[i];
frame.ProgramCounter = callstack[skipCount + i];
frame.ModuleName[0] = 0;
frame.FileName[0] = 0;
frame.LineNumber = 0;
int32 nameLen = Math::Min<int32>(StringUtils::Length(name), ARRAY_COUNT(frame.FunctionName) - 1);
Platform::MemoryCopy(frame.FunctionName, name, nameLen);
frame.FunctionName[nameLen] = 0;
}
free(names);
}
#endif
return result;
}
#endif