From a86aee3d6a3ffc4307f38067cb56ec7e3974f218 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Fri, 6 Jan 2023 20:37:42 +0800 Subject: [PATCH] Organize implementation Put Windows implementation to its own C file. --- CMakeLists.txt | 6 +- src/main.c | 170 +------------------------------------------------ src/main.m | 30 ++++----- 3 files changed, 20 insertions(+), 186 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f2af117..9316a1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,10 +12,12 @@ if (WIN32) set(PROJECT_ARCH x86) add_definitions(-DWIN32) endif () - add_executable(cerberus WIN32 src/main.c) + add_executable(cerberus WIN32 src/main.h src/main.c) elseif (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + # does not work, use Xcode to build + # TODO use makefile instead of cmake? add_definitions(-DMACOS) - add_executable(cerberus src/main.c) + add_executable(cerberus src/main.h src/main.m) include_directories(/usr/include) link_directories() elseif(UNIX) diff --git a/src/main.c b/src/main.c index c96bca4..8e55abd 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,4 @@ -#define WINDOW_X 0 -#define WINDOW_Y 0 -#define WINDOW_WIDTH 350 -#define WINDOW_HEIGHT 250 +#include "main.h" #ifdef WIN64 @@ -56,169 +53,4 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, return DefWindowProcW(hwnd, msg, wParam, lParam); } -#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 diff --git a/src/main.m b/src/main.m index c4fc250..dcffc62 100644 --- a/src/main.m +++ b/src/main.m @@ -1,19 +1,19 @@ #import #import "main.h" -int main () -{ - @autoreleasepool{ - [NSApplication sharedApplication]; - [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; - id applicationName = [[NSProcessInfo processInfo] processName]; - id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT) - styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO]; - [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; - [window setTitle: applicationName]; - [window makeKeyAndOrderFront:nil]; - [NSApp activateIgnoringOtherApps:YES]; - [NSApp run]; - } - return 0; +int main() { + // https://stackoverflow.com/questions/30269329/creating-window-application-in-pure-c-on-mac-osx + @autoreleasepool { + [NSApplication sharedApplication]; + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; + id applicationName = [[NSProcessInfo processInfo] processName]; + id window = [[NSWindow alloc] initWithContentRect:NSMakeRect(WINDOW_X, WINDOW_Y, WINDOW_WIDTH, WINDOW_HEIGHT) + styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:NO]; + [window cascadeTopLeftFromPoint:NSMakePoint(20, 20)]; + [window setTitle:applicationName]; + [window makeKeyAndOrderFront:nil]; + [NSApp activateIgnoringOtherApps:YES]; + [NSApp run]; + } + return 0; }