From ca4adf5f394a52c51179a38356751881200230dd Mon Sep 17 00:00:00 2001 From: IoIxD Date: Mon, 12 Jan 2026 13:04:30 -0700 Subject: [PATCH] cocoa: fill a bunch of no-brainer functions, i haven't actually tested this yet and won't until I get home but I assume the worst case scenario is that I fucked up the syntax --- include/Mw/LowLevel/AppKit.h | 27 ------------ include/Mw/LowLevel/Cocoa.h | 1 + src/backend/cocoa.m | 83 ++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 31 deletions(-) delete mode 100644 include/Mw/LowLevel/AppKit.h diff --git a/include/Mw/LowLevel/AppKit.h b/include/Mw/LowLevel/AppKit.h deleted file mode 100644 index 4a6c8e5..0000000 --- a/include/Mw/LowLevel/AppKit.h +++ /dev/null @@ -1,27 +0,0 @@ -/*! - * @file Mw/LowLevel/AppKit.h - * @brief Work in progress AppKit Backend - * @warning This is used internally. - */ -#ifndef __MW_LOWLEVEL_APPKIT_H__ -#define __MW_LOWLEVEL_APPKIT_H__ - -#include -#include -#include - -MWDECL int MwLLAppKitCallInit(void); - -struct _MwLLAppKit { - struct _MwLLCommon common; -}; - -struct _MwLLAppKitColor { - struct _MwLLCommonColor common; -}; - -struct _MwLLAppKitPixmap { - struct _MwLLCommonPixmap common; -}; - -#endif diff --git a/include/Mw/LowLevel/Cocoa.h b/include/Mw/LowLevel/Cocoa.h index 1ff4c1e..4ff9f49 100644 --- a/include/Mw/LowLevel/Cocoa.h +++ b/include/Mw/LowLevel/Cocoa.h @@ -23,6 +23,7 @@ struct _MwLLCocoaColor { struct _MwLLCocoaPixmap { struct _MwLLCommonPixmap common; + void* real; }; #endif diff --git a/src/backend/cocoa.m b/src/backend/cocoa.m index 3960164..bd2320b 100644 --- a/src/backend/cocoa.m +++ b/src/backend/cocoa.m @@ -3,13 +3,44 @@ #endif #import -#include +#import #import #include #include "../../external/stb_ds.h" +@interface MilskoCocoaPixmap : NSObject { + NSImage *image; +} ++ (MilskoCocoaPixmap*)newWithWidth:(int)width + height:(int)height; +- (void)updateWithData(void *); +- (void)destroy; +@end + +@implementation MilskoCocoaPixmap + ++ (MilskoCocoaPixmap*)newWithWidth:(int)width + height:(int)height { + MilskoCocoaPixmap * p = [MilskoCocoaPixmap alloc]; + NSSize sz; + + sz.width = width; + sz.height = height; + + p->image = [NSImage initWithSize:sz]; + + return p; +} +- (void)updateWithData(void *) { +} +- (void)destroy { + [self->image dealloc]; +} +@end + + @interface MilskoCocoa : NSObject { NSApplication *application; NSWindow *window; @@ -101,10 +132,24 @@ - (void)lineWithPoints:(MwPoint *)points color:(MwLLColor)color { }; - (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h { + NSRect frame = self->window->frame; + + *x = frame.origin.x; + *y = frame.origin.y; + *w = frame.size.x; + *h = frame.size.y; }; - (void)setX:(int)x Y:(int)y { + NSPoint p; + p.x = x; + p.y = y; + [self->window setFrameTopLeftPoint p]; }; - (void)setW:(int)w H:(int)h { + NSSize s; + s.w = w; + s.h = h; + [self->window setFrameSize s]; }; - (int)pending { return 1; @@ -122,6 +167,9 @@ [pool release]; }; - (void)setTitle:(const char *)title { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [self->window setTitleWithRepresentedFilename:[NSString stringWithUTF8String:title]]; + [pool release]; }; - (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect *)rect { }; @@ -143,10 +191,21 @@ MaxY:(int)maxy { }; - (void)makeBorderless:(int)toggle { + NSWindowStyleMask mask = self->window.styleMask; + if(mask & NSBorderlessWindowMask) { + mask ^= NSBorderlessWindowMask; + mask |= NSTitledWindowMask; + } else { + mask |= NSBorderlessWindowMask; + mask ^= NSTitledWindowMask; + } + [self->window setStyleMask:mask]; }; - (void)focus { + [self->window makeMainWindow]; }; - (void)grabPointer:(int)toggle { + /* MacOS didn't have a "pointer grab" function until 10.13.2 so I need to do this manually */ }; - (void)setClipboard:(const char *)text { }; @@ -155,8 +214,16 @@ - (void)makeToolWindow { }; - (void)getCursorCoord:(MwPoint *)point { + NSPoint p = self->window->mouseLocation; + point->x = p.x; + point->y = p.y; }; - (void)getScreenSize:(MwRect *)rect { + NSScreen * screen = self->window->screen; + rect->x = screen->frame.origin.x; + rect->y = screen->frame.origin.y; + rect->width = screen->frame.size.x; + rect->height = screen->frame.size.y; }; - (void)destroy { [self->window dealloc]; @@ -185,7 +252,6 @@ static void MwLLDestroyImpl(MwLL handle) { MilskoCocoa *h = handle->cocoa.real; [h destroy]; - [h dealloc]; MwLLDestroyCommon(handle); @@ -267,13 +333,22 @@ static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char *data, r->common.width = width; r->common.height = height; + r->cocoa.real = [MilskoCocoaPixmap newWithWidth:width height:height]; + MwLLPixmapUpdate(r); return r; } -static void MwLLPixmapUpdateImpl(MwLLPixmap r) { (void)r; } +static void MwLLPixmapUpdateImpl(MwLLPixmap pixmap) { + MilskoCocoaPixmap *p = pixmap->cocoa.real; + [p updateWithData:pixmap->common.raw]; +} -static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) { free(pixmap); } +static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) { + MilskoCocoaPixmap *p = pixmap->cocoa.real; + [p destroy]; + free(pixmap); +} static void MwLLDrawPixmapImpl(MwLL handle, MwRect *rect, MwLLPixmap pixmap) { MilskoCocoa *h = handle->cocoa.real;