renamed appkit backend to cocoa and got something that compiles

This commit is contained in:
IoIxD
2026-01-09 21:35:23 -07:00
parent da33a1a3c6
commit e73964882e
8 changed files with 451 additions and 70 deletions

View File

@@ -220,10 +220,9 @@ foreach my $l (@library_targets) {
my $s = $l;
my $o = $object_suffix;
$o =~ s/\./\\\./g;
if ($l =~ /appkit/) {
if ($l =~ /cocoa/) {
$s =~ s/$o$/.m/;
} else {
$s =~ s/$o$/.c/;
}
@@ -254,7 +253,7 @@ if(param_get("examples")) {
print(OUT
"${l}: ${s}${object_suffix} src/${library_prefix}Mw${library_suffix}\n"
);
if (grep(/^appkit$/, @backends)) {
if (grep(/^cocoa$/, @backends)) {
print(OUT
" \$(CC) -L./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n"
);
@@ -262,7 +261,7 @@ if(param_get("examples")) {
print(OUT
" \$(CC) -L src -Wl,-R./src \$\(LIBDIR) -o ${l} ${s}${object_suffix} -lMw ${math} ${libs}\n"
);
}
}
print(OUT "${s}${object_suffix}: ${s}.c\n");
print(OUT
" \$(CC) -c \$\(INCDIR) -o ${s}${object_suffix} ${s}.c -lMw ${math}\n"

View File

@@ -27,7 +27,7 @@ enum MwLLBackends {
MwLLBackendX11 = 0,
MwLLBackendGDI,
MwLLBackendWayland,
MwLLBackendAppKit,
MwLLBackendCocoa,
};
struct _MwLLCommon {
@@ -62,8 +62,8 @@ struct _MwLLCommonPixmap {
#ifdef USE_WAYLAND
#include <Mw/LowLevel/Wayland.h>
#endif
#ifdef USE_APPKIT
#include <Mw/LowLevel/AppKit.h>
#ifdef USE_COCOA
#include <Mw/LowLevel/Cocoa.h>
#endif
union _MwLL {
@@ -77,8 +77,8 @@ union _MwLL {
#ifdef USE_WAYLAND
struct _MwLLWayland wayland;
#endif
#ifdef USE_APPKIT
struct _MwLLAppKit appkit;
#ifdef USE_COCOA
struct _MwLLCocoa cocoa;
#endif
};
@@ -93,8 +93,8 @@ union _MwLLColor {
#ifdef USE_WAYLAND
struct _MwLLWaylandColor wayland;
#endif
#ifdef USE_APPKIT
struct _MwLLAppKitColor appkit;
#ifdef USE_COCOA
struct _MwLLCocoaColor cocoa;
#endif
};
@@ -109,8 +109,8 @@ union _MwLLPixmap {
#ifdef USE_WAYLAND
struct _MwLLWaylandPixmap wayland;
#endif
#ifdef USE_APPKIT
struct _MwLLAppKitPixmap appkit;
#ifdef USE_COCOA
struct _MwLLCocoaPixmap cocoa;
#endif
};
#endif

View File

@@ -0,0 +1,28 @@
/*!
* @file Mw/LowLevel/Cocoa.h
* @brief Work in progress Cocoa Backend
* @warning This is used internally.
*/
#ifndef __MW_LOWLEVEL_COCOA_H__
#define __MW_LOWLEVEL_COCOA_H__
#include <Mw/LowLevel.h>
#include <Mw/MachDep.h>
#include <Mw/TypeDefs.h>
MWDECL int MwLLCocoaCallInit(void);
struct _MwLLCocoa {
struct _MwLLCommon common;
void* real;
};
struct _MwLLCocoaColor {
struct _MwLLCommonColor common;
};
struct _MwLLCocoaPixmap {
struct _MwLLCommonPixmap common;
};
#endif

View File

@@ -1,8 +1,9 @@
$library_suffix = ".dylib";
set_shared_flag("-dynamiclib");
use_backend("appkit");
use_backend("cocoa");
add_cflags("-DSTBI_NO_THREAD_LOCALS");
add_ldflags("-framework Cocoa -lobjc");
1;
1;

View File

@@ -46,9 +46,9 @@ if (grep(/^wayland$/, @backends)) {
$gl_libs = "-lGL -lGLU";
}
if (grep(/^appkit$/, @backends)) {
add_cflags("-DUSE_APPKIT");
new_object("src/backend/appkit.m");
if (grep(/^cocoa$/, @backends)) {
add_cflags("-DUSE_COCOA");
new_object("src/backend/cocoa.m");
$gl_libs = "-lGL -lGLU";
}

364
src/backend/cocoa.m Normal file
View File

@@ -0,0 +1,364 @@
#ifndef USE_COCOA
#define USE_COCOA
#endif
#import <AppKit/AppKit.h>
#include <AppKit/NSEvent.h>
#import <Foundation/Foundation.h>
#include <Mw/Milsko.h>
#include "../../external/stb_ds.h"
@interface MilskoCocoa : NSObject {
NSApplication *application;
NSWindow *window;
}
+ (MilskoCocoa *)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height;
- (void)polygonWithPoints:(MwPoint *)points
points_count:(int)points_count
color:(MwLLColor)color;
- (void)lineWithPoints:(MwPoint *)points color:(MwLLColor)color;
- (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h;
- (void)setX:(int)x Y:(int)y;
- (void)setW:(int)w H:(int)h;
- (int)pending;
- (void)getNextEvent;
- (void)setTitle:(const char *)title;
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect *)rect;
- (void)setIcon:(MwLLPixmap)pixmap;
- (void)forceRender;
- (void)setCursor:(MwCursor *)image mask:(MwCursor *)mask;
- (void)detachWithPoint:(MwPoint *)point;
- (void)show:(int)show;
- (void)makePopupWithParent:(MwLL)parent;
- (void)setSizeHintsWithMinX:(int)minx
MinY:(int)miny
MaxX:(int)maxx
MaxY:(int)maxy;
- (void)makeBorderless:(int)toggle;
- (void)focus;
- (void)grabPointer:(int)toggle;
- (void)setClipboard:(const char *)text;
- (void)makeToolWindow;
- (void)getCursorCoord:(MwPoint *)point;
- (void)getScreenSize:(MwRect *)rect;
- (void)destroy;
@end
@implementation MilskoCocoa
+ (MilskoCocoa *)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height {
MilskoCocoa *c = [MilskoCocoa alloc];
bool centerX = false, centerY = false;
if (x == MwDEFAULT) {
x = 0;
centerX = true;
}
if (y == MwDEFAULT) {
y = 0;
centerY = true;
}
c->application = [NSApplication sharedApplication];
NSRect rect = NSMakeRect(x, y, width, height);
c->window = [[[NSWindow alloc]
initWithContentRect:rect
styleMask:parent == NULL
? (NSTitledWindowMask | NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask)
: NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[c->window makeKeyAndOrderFront:c->application];
if (parent != NULL) {
[c->window setBackgroundColor:[NSColor blueColor]];
MilskoCocoa *p = parent->cocoa.real;
[p->window addChildWindow:c->window ordered:NSWindowAbove];
}
return c;
}
- (void)polygonWithPoints:(MwPoint *)points
points_count:(int)points_count
color:(MwLLColor)color {
};
- (void)lineWithPoints:(MwPoint *)points color:(MwLLColor)color {
};
- (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h {
};
- (void)setX:(int)x Y:(int)y {
};
- (void)setW:(int)w H:(int)h {
};
- (int)pending {
return 1;
};
- (void)getNextEvent {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSEvent *event = [self->application nextEventMatchingMask:NSAnyEventMask
untilDate:nil
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event != nil) {
printf("got event: %p\n", event);
}
[pool release];
};
- (void)setTitle:(const char *)title {
};
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect *)rect {
};
- (void)setIcon:(MwLLPixmap)pixmap {
};
- (void)forceRender {
};
- (void)setCursor:(MwCursor *)image mask:(MwCursor *)mask {
};
- (void)detachWithPoint:(MwPoint *)point {
};
- (void)show:(int)show {
};
- (void)makePopupWithParent:(MwLL)parent {
};
- (void)setSizeHintsWithMinX:(int)minx
MinY:(int)miny
MaxX:(int)maxx
MaxY:(int)maxy {
};
- (void)makeBorderless:(int)toggle {
};
- (void)focus {
};
- (void)grabPointer:(int)toggle {
};
- (void)setClipboard:(const char *)text {
};
- (void)getClipboard {
};
- (void)makeToolWindow {
};
- (void)getCursorCoord:(MwPoint *)point {
};
- (void)getScreenSize:(MwRect *)rect {
};
- (void)destroy {
[self->window dealloc];
}
@end
static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLL r;
(void)x;
(void)y;
(void)width;
(void)height;
r = malloc(sizeof(*r));
MwLLCreateCommon(r);
MilskoCocoa *o =
[MilskoCocoa newWithParent:parent x:x y:y width:width height:height];
r->cocoa.real = o;
return r;
}
static void MwLLDestroyImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
[h destroy];
[h dealloc];
MwLLDestroyCommon(handle);
free(handle);
}
static void MwLLBeginDrawImpl(MwLL handle) { (void)handle; }
static void MwLLEndDrawImpl(MwLL handle) { (void)handle; }
static void MwLLPolygonImpl(MwLL handle, MwPoint *points, int points_count,
MwLLColor color) {
MilskoCocoa *h = handle->cocoa.real;
[h polygonWithPoints:points points_count:points_count color:color];
}
static void MwLLLineImpl(MwLL handle, MwPoint *points, MwLLColor color) {
MilskoCocoa *h = handle->cocoa.real;
[h lineWithPoints:points color:color];
}
static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) {
MwLLColor c = malloc(sizeof(*c));
MwLLColorUpdate(handle, c, r, g, b);
return c;
}
static void MwLLColorUpdateImpl(MwLL handle, MwLLColor c, int r, int g, int b) {
(void)handle;
c->common.red = r;
c->common.green = g;
c->common.blue = b;
}
static void MwLLGetXYWHImpl(MwLL handle, int *x, int *y, unsigned int *w,
unsigned int *height) {
MilskoCocoa *h = handle->cocoa.real;
[h getX:x Y:y W:w H:height];
}
static void MwLLSetXYImpl(MwLL handle, int x, int y) {
MilskoCocoa *h = handle->cocoa.real;
[h setX:x Y:y];
}
static void MwLLSetWHImpl(MwLL handle, int w, int height) {
MilskoCocoa *h = handle->cocoa.real;
[h setW:w H:height];
}
static void MwLLFreeColorImpl(MwLLColor color) { free(color); }
static int MwLLPendingImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
return [h pending];
}
static void MwLLNextEventImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
[h getNextEvent];
}
static void MwLLSetTitleImpl(MwLL handle, const char *title) {
MilskoCocoa *h = handle->cocoa.real;
[h setTitle:title];
}
static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char *data,
int width, int height) {
(void)handle;
MwLLPixmap r = malloc(sizeof(*r));
r->common.raw = malloc(4 * width * height);
memcpy(r->common.raw, data, 4 * width * height);
r->common.width = width;
r->common.height = height;
MwLLPixmapUpdate(r);
return r;
}
static void MwLLPixmapUpdateImpl(MwLLPixmap r) { (void)r; }
static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) { free(pixmap); }
static void MwLLDrawPixmapImpl(MwLL handle, MwRect *rect, MwLLPixmap pixmap) {
MilskoCocoa *h = handle->cocoa.real;
[h drawPixmap:pixmap rect:rect];
}
static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) {
MilskoCocoa *h = handle->cocoa.real;
[h setIcon:pixmap];
}
static void MwLLForceRenderImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
[h forceRender];
}
static void MwLLSetCursorImpl(MwLL handle, MwCursor *image, MwCursor *mask) {
MilskoCocoa *h = handle->cocoa.real;
[h setCursor:image mask:mask];
}
static void MwLLDetachImpl(MwLL handle, MwPoint *point) {
MilskoCocoa *h = handle->cocoa.real;
[h detachWithPoint:point];
}
static void MwLLShowImpl(MwLL handle, int show) {
MilskoCocoa *h = handle->cocoa.real;
[h show:show];
}
static void MwLLMakePopupImpl(MwLL handle, MwLL parent) {
MilskoCocoa *h = handle->cocoa.real;
[h makePopupWithParent:parent];
}
static void MwLLSetSizeHintsImpl(MwLL handle, int minx, int miny, int maxx,
int maxy) {
MilskoCocoa *h = handle->cocoa.real;
[h setSizeHintsWithMinX:minx MinY:miny MaxX:maxx MaxY:maxy];
}
static void MwLLMakeBorderlessImpl(MwLL handle, int toggle) {
MilskoCocoa *h = handle->cocoa.real;
[h makeBorderless:toggle];
}
static void MwLLFocusImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
[h focus];
}
static void MwLLGrabPointerImpl(MwLL handle, int toggle) {
MilskoCocoa *h = handle->cocoa.real;
[h grabPointer:toggle];
}
static void MwLLSetClipboardImpl(MwLL handle, const char *text) {
MilskoCocoa *h = handle->cocoa.real;
[h setClipboard:text];
}
static void MwLLGetClipboardImpl(MwLL handle) { (void)handle; }
static void MwLLMakeToolWindowImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
[h makeToolWindow];
}
static void MwLLGetCursorCoordImpl(MwLL handle, MwPoint *point) {
MilskoCocoa *h = handle->cocoa.real;
[h getCursorCoord:point];
}
static void MwLLGetScreenSizeImpl(MwLL handle, MwRect *rect) {
MilskoCocoa *h = handle->cocoa.real;
[h getScreenSize:rect];
}
static void MwLLBeginStateChangeImpl(MwLL handle) { MwLLShow(handle, 0); }
static void MwLLEndStateChangeImpl(MwLL handle) { MwLLShow(handle, 1); }
static int MwLLCocoaCallInitImpl(void) { return 0; }
#include "call.c"
CALL(Cocoa);

View File

@@ -123,6 +123,7 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent,
h->children = NULL;
if(widget_class != NULL) {
printf("%p\n", MwLLCreate);
if((h->lowlevel = MwLLCreate(parent == NULL ? NULL : parent->lowlevel, x, y, width, height)) == NULL) {
free(h->name);
free(h);
@@ -729,6 +730,9 @@ MwWidget MwGetParent(MwWidget handle) {
typedef int (*call_t)(void);
int MwLibraryInit(void) {
call_t calls[] = {
#ifdef USE_COCOA
MwLLCocoaCallInit,
#endif
#ifdef USE_WAYLAND
MwLLWaylandCallInit,
#endif

View File

@@ -1,70 +1,55 @@
#include <Mw/Milsko.h>
/* Older GCC doesn't like this part of the code because of common sections.
What are common sections? This question is so obscure and unknown
that newer GCC actually compiles with -fno-common by default because
not enough people ever figured this out. But when compiling under
older Mac OS X. specifically the gcc that comes with tigerbrew (this
has not yet been tried under XCode 10.5), this comes up. Furthermore,
adding -fno-common to either the cflags or ldflags doesn't seem to fix this.
But what does fix it is using GNU attributes. So...yeah, sure, we'll just do
this.*/
#ifdef __APPLE__
#define NOCOMMON __attribute__((nocommon))
#else
#define NOCOMMON
#endif
MwLL (*MwLLCreate)(MwLL parent, int x, int y, int width, int height) = NULL;
void (*MwLLDestroy)(MwLL handle) = NULL;
NOCOMMON MwLL (*MwLLCreate)(MwLL parent, int x, int y, int width, int height);
NOCOMMON void (*MwLLDestroy)(MwLL handle);
void (*MwLLPolygon)(MwLL handle, MwPoint* points, int points_count, MwLLColor color) = NULL;
void (*MwLLLine)(MwLL handle, MwPoint* points, MwLLColor color) = NULL;
NOCOMMON void (*MwLLPolygon)(MwLL handle, MwPoint* points, int points_count, MwLLColor color);
NOCOMMON void (*MwLLLine)(MwLL handle, MwPoint* points, MwLLColor color);
void (*MwLLBeginDraw)(MwLL handle) = NULL;
void (*MwLLEndDraw)(MwLL handle) = NULL;
NOCOMMON void (*MwLLBeginDraw)(MwLL handle);
NOCOMMON void (*MwLLEndDraw)(MwLL handle);
MwLLColor (*MwLLAllocColor)(MwLL handle, int r, int g, int b) = NULL;
void (*MwLLColorUpdate)(MwLL handle, MwLLColor c, int r, int g, int b) = NULL;
void (*MwLLFreeColor)(MwLLColor color) = NULL;
NOCOMMON MwLLColor (*MwLLAllocColor)(MwLL handle, int r, int g, int b);
NOCOMMON void (*MwLLColorUpdate)(MwLL handle, MwLLColor c, int r, int g, int b);
NOCOMMON void (*MwLLFreeColor)(MwLLColor color);
void (*MwLLGetXYWH)(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) = NULL;
void (*MwLLSetXY)(MwLL handle, int x, int y) = NULL;
void (*MwLLSetWH)(MwLL handle, int w, int h) = NULL;
NOCOMMON void (*MwLLGetXYWH)(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h);
NOCOMMON void (*MwLLSetXY)(MwLL handle, int x, int y);
NOCOMMON void (*MwLLSetWH)(MwLL handle, int w, int h);
void (*MwLLSetTitle)(MwLL handle, const char* title) = NULL;
NOCOMMON void (*MwLLSetTitle)(MwLL handle, const char* title);
int (*MwLLPending)(MwLL handle) = NULL;
void (*MwLLNextEvent)(MwLL handle) = NULL;
NOCOMMON int (*MwLLPending)(MwLL handle);
NOCOMMON void (*MwLLNextEvent)(MwLL handle);
MwLLPixmap (*MwLLCreatePixmap)(MwLL handle, unsigned char* data, int width, int height) = NULL;
void (*MwLLPixmapUpdate)(MwLLPixmap pixmap) = NULL;
void (*MwLLDestroyPixmap)(MwLLPixmap pixmap) = NULL;
void (*MwLLDrawPixmap)(MwLL handle, MwRect* rect, MwLLPixmap pixmap) = NULL;
void (*MwLLSetIcon)(MwLL handle, MwLLPixmap pixmap) = NULL;
NOCOMMON MwLLPixmap (*MwLLCreatePixmap)(MwLL handle, unsigned char* data, int width, int height);
NOCOMMON void (*MwLLPixmapUpdate)(MwLLPixmap pixmap);
NOCOMMON void (*MwLLDestroyPixmap)(MwLLPixmap pixmap);
NOCOMMON void (*MwLLDrawPixmap)(MwLL handle, MwRect* rect, MwLLPixmap pixmap);
NOCOMMON void (*MwLLSetIcon)(MwLL handle, MwLLPixmap pixmap);
void (*MwLLForceRender)(MwLL handle) = NULL;
NOCOMMON void (*MwLLForceRender)(MwLL handle);
void (*MwLLSetCursor)(MwLL handle, MwCursor* image, MwCursor* mask) = NULL;
void (*MwLLDetach)(MwLL handle, MwPoint* point) = NULL;
void (*MwLLShow)(MwLL handle, int show) = NULL;
NOCOMMON void (*MwLLSetCursor)(MwLL handle, MwCursor* image, MwCursor* mask);
NOCOMMON void (*MwLLDetach)(MwLL handle, MwPoint* point);
NOCOMMON void (*MwLLShow)(MwLL handle, int show);
void (*MwLLSetSizeHints)(MwLL handle, int minx, int miny, int maxx, int maxy) = NULL;
void (*MwLLMakeBorderless)(MwLL handle, int toggle) = NULL;
void (*MwLLMakeToolWindow)(MwLL handle) = NULL;
void (*MwLLMakePopup)(MwLL handle, MwLL parent) = NULL;
NOCOMMON void (*MwLLSetSizeHints)(MwLL handle, int minx, int miny, int maxx, int maxy);
NOCOMMON void (*MwLLMakeBorderless)(MwLL handle, int toggle);
NOCOMMON void (*MwLLMakeToolWindow)(MwLL handle);
NOCOMMON void (*MwLLMakePopup)(MwLL handle, MwLL parent);
void (*MwLLBeginStateChange)(MwLL handle) = NULL;
void (*MwLLEndStateChange)(MwLL handle) = NULL;
NOCOMMON void (*MwLLBeginStateChange)(MwLL handle);
NOCOMMON void (*MwLLEndStateChange)(MwLL handle);
void (*MwLLFocus)(MwLL handle) = NULL;
void (*MwLLGrabPointer)(MwLL handle, int toggle) = NULL;
NOCOMMON void (*MwLLFocus)(MwLL handle);
NOCOMMON void (*MwLLGrabPointer)(MwLL handle, int toggle);
void (*MwLLSetClipboard)(MwLL handle, const char* text) = NULL;
void (*MwLLGetClipboard)(MwLL handle) = NULL;
NOCOMMON void (*MwLLSetClipboard)(MwLL handle, const char* text);
NOCOMMON void (*MwLLGetClipboard)(MwLL handle);
NOCOMMON void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point);
NOCOMMON void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect);
void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point) = NULL;
void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect) = NULL;
void MwLLCreateCommon(MwLL handle) {
handle->common.handler = malloc(sizeof(*handle->common.handler));