From d5fbf95178591fd9200d37289ed968cdb54fbab4 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Fri, 6 Jan 2023 20:05:41 +0800 Subject: [PATCH] Add macOS bare implementation macOS implementation is untested. --- .gitignore | 1 + CMakeLists.txt | 5 +- src/main.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 172 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 990936c..b6aaf4c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ cmake-build-debug/ +.DS_Store diff --git a/CMakeLists.txt b/CMakeLists.txt index 08aa462..f2af117 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,8 +13,11 @@ if (WIN32) add_definitions(-DWIN32) endif () add_executable(cerberus WIN32 src/main.c) -elseif(APPLE) +elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") add_definitions(-DMACOS) + add_executable(cerberus src/main.c) + include_directories(/usr/include) + link_directories() elseif(UNIX) add_definitions(-DUNIX) endif () diff --git a/src/main.c b/src/main.c index 500f491..c96bca4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,8 @@ +#define WINDOW_X 0 +#define WINDOW_Y 0 +#define WINDOW_WIDTH 350 +#define WINDOW_HEIGHT 250 + #ifdef WIN64 #include @@ -25,7 +30,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, RegisterClassW(&wc); hwnd = CreateWindowW(wc.lpszClassName, windowName, WS_OVERLAPPEDWINDOW | WS_VISIBLE, - 100, 100, 350, 250, NULL, NULL, hInstance, NULL); + WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); @@ -53,6 +58,167 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, #elif defined MACOS +//original objective-c +// +//#import +//int main () +//{ +// @autoreleasepool{ +// [NSApplication sharedApplication]; +// [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; +// id applicationName = [[NSProcessInfo processInfo] processName]; +// id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 120, 120) +// styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO]; +// [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; +// [window setTitle: applicationName]; +// [window makeKeyAndOrderFront:nil]; +// [NSApp activateIgnoringOtherApps:YES]; +// [NSApp run]; +// } +// return 0; +//} + +// $ clang minimal.c -framework Cocoa -o minimal.app + +#include +#include + +#include + +#define cls objc_getClass +#define sel sel_getUid +#define msg ((id (*)(id, SEL, ...))objc_msgSend) +#define cls_msg ((id (*)(Class, SEL, ...))objc_msgSend) + +// poor man's bindings! +typedef enum NSApplicationActivationPolicy { + NSApplicationActivationPolicyRegular = 0, + NSApplicationActivationPolicyAccessory = 1, + NSApplicationActivationPolicyERROR = 2, +} NSApplicationActivationPolicy; + +typedef enum NSWindowStyleMask { + NSWindowStyleMaskBorderless = 0, + NSWindowStyleMaskTitled = 1 << 0, + NSWindowStyleMaskClosable = 1 << 1, + NSWindowStyleMaskMiniaturizable = 1 << 2, + NSWindowStyleMaskResizable = 1 << 3, +} NSWindowStyleMask; + +typedef enum NSBackingStoreType { + NSBackingStoreBuffered = 2, +} NSBackingStoreType; + +int main(int argc, char *argv[]) +{ + // id app = [NSApplication sharedApplication]; + id app = cls_msg(cls("NSApplication"), sel("sharedApplication")); + + // [app setActivationPolicy:NSApplicationActivationPolicyRegular]; + msg(app, sel("setActivationPolicy:"), NSApplicationActivationPolicyRegular); + + struct CGRect frameRect = {0, 0, 600, 500}; + + // id window = [[NSWindow alloc] initWithContentRect:frameRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO]; + id window = msg(cls_msg(cls("NSWindow"), sel("alloc")), + sel("initWithContentRect:styleMask:backing:defer:"), + frameRect, + NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable, + NSBackingStoreBuffered, + false); + msg(window, sel("setTitle:"), cls_msg(cls("NSString"), sel("stringWithUTF8String:"), "Pure C App")); + + // [window makeKeyAndOrderFront:nil]; + msg(window, sel("makeKeyAndOrderFront:"), nil); + + // [app activateIgnoringOtherApps:YES]; + msg(app, sel("activateIgnoringOtherApps:"), true); + + msg(app, sel("run")); +} + +// +//#include +//#include +// +//#include +// +//#define cls objc_getClass +//#define sel sel_getUid +//#define msg ((id (*)(id, SEL, ...))objc_msgSend) +//#define cls_msg ((id (*)(Class, SEL, ...))objc_msgSend) +// +//// poor man's bindings! +//typedef enum NSApplicationActivationPolicy { +// NSApplicationActivationPolicyRegular = 0, +// NSApplicationActivationPolicyAccessory = 1, +// NSApplicationActivationPolicyERROR = 2, +//} NSApplicationActivationPolicy; +// +//typedef enum NSWindowStyleMask { +// NSWindowStyleMaskBorderless = 0, +// NSWindowStyleMaskTitled = 1 << 0, +// NSWindowStyleMaskClosable = 1 << 1, +// NSWindowStyleMaskMiniaturizable = 1 << 2, +// NSWindowStyleMaskResizable = 1 << 3, +//} NSWindowStyleMask; +// +//typedef enum NSBackingStoreType { +// NSBackingStoreBuffered = 2, +//} NSBackingStoreType; +// +//int main(int argc, char *argv[]) +//{ +// // id app = [NSApplication sharedApplication]; +// id app = cls_msg( +// cls("NSApplication"), +// sel("sharedApplication") +// ); +// +// // [app setActivationPolicy:NSApplicationActivationPolicyRegular]; +// msg( +// app, +// sel("setActivationPolicy:"), +// NSApplicationActivationPolicyRegular +// ); +// +// struct CGRect frameRect = { +// WINDOW_X, +// WINDOW_Y, +// WINDOW_WIDTH, +// WINDOW_HEIGHT +// }; +// +// id applicationName = msg( +// cls_msg( +// cls("NSProcessInfo"), +// sel("processInfo") +// ), +// sel("processName") +// ); +// +// // id window = [[NSWindow alloc] initWithContentRect:frameRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO]; +// id window = msg( +// cls_msg(cls("NSWindow"), sel("alloc")), +// sel("initWithContentRect:styleMask:backing:defer:"), +// frameRect, +// NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable, +// NSBackingStoreBuffered, +// false +// ); +// +// printf("%p\n", window); +// msg(window, sel("setTitle:"), applicationName); + + // [window makeKeyAndOrderFront:nil]; + //msg(window, sel("makeKeyAndOrderFront:"), nil); + + // [app activateIgnoringOtherApps:YES]; + //msg(app, sel("activateIgnoringOtherApps:"), true); + + //msg(app, sel("run")); +//} + // https://stackoverflow.com/questions/30269329/creating-window-application-in-pure-c-on-mac-osx #endif