Przeglądaj źródła

Add macOS bare implementation

macOS implementation is untested.
master
TheoryOfNekomata 1 rok temu
rodzic
commit
d5fbf95178
3 zmienionych plików z 172 dodań i 2 usunięć
  1. +1
    -0
      .gitignore
  2. +4
    -1
      CMakeLists.txt
  3. +167
    -1
      src/main.c

+ 1
- 0
.gitignore Wyświetl plik

@@ -1,2 +1,3 @@
.idea/
cmake-build-debug/
.DS_Store

+ 4
- 1
CMakeLists.txt Wyświetl plik

@@ -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 ()

+ 167
- 1
src/main.c Wyświetl plik

@@ -1,3 +1,8 @@
#define WINDOW_X 0
#define WINDOW_Y 0
#define WINDOW_WIDTH 350
#define WINDOW_HEIGHT 250

#ifdef WIN64

#include <windows.h>
@@ -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 <Cocoa/Cocoa.h>
//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 <objc/runtime.h>
#include <objc/message.h>

#include <Carbon/Carbon.h>

#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 <objc/runtime.h>
//#include <objc/message.h>
//
//#include <Carbon/Carbon.h>
//
//#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

Ładowanie…
Anuluj
Zapisz