mac: took a detour and got it working in 10.4, before accidentally fixing the remaining memory leak I think

This commit is contained in:
IoIxD 2026-03-08 18:04:37 -07:00
commit 3b78872ce8
2 changed files with 871 additions and 834 deletions

View file

@ -21,18 +21,19 @@
#import <CoreGraphics/CoreGraphics.h>
#endif
@interface MilskoCocoaWindowDelegate<NSWindowDelegate> : NSObject {
NSWindow *w;
// Note: implements NSWindowDelegate
@interface MilskoCocoaWindowDelegate : NSObject {
NSWindow* w;
}
- (instancetype)initWithWin:(NSWindow *)win;
- (MilskoCocoaWindowDelegate*)initWithWin:(NSWindow*)win;
@end
@interface MilskoFakePointer : NSView {
void *ptr;
void* ptr;
}
- (void)setPointer:(void *)ptr;
- (void *)pointer;
- (void)setPointer:(void*)ptr;
- (void*)pointer;
@end
@ -40,24 +41,24 @@
MwBool valid;
int width;
int height;
unsigned char *buf;
NSImage *image;
NSBitmapImageRep *rep;
unsigned char* buf;
NSImage* image;
NSBitmapImageRep* rep;
}
+ (MilskoCocoaPixmap *)newWithWidth:(int)width height:(int)height;
- (void)updateWithData:(unsigned char *)data;
+ (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height;
- (void)updateWithData:(unsigned char*)data;
- (void)destroy;
/* using @property to create instance variables fucks up 10.4 gcc for some
* reason? */
- (NSImage *)image;
- (NSImage*)image;
@end
@interface MilskoCocoaView : NSView {
NSBitmapImageRep *rep;
NSGraphicsContext *context;
NSBitmapImageRep* rep;
NSGraphicsContext* context;
MwBool valid;
CGColorSpaceRef space;
CGDataProviderRef provider;
@ -65,48 +66,48 @@
float height;
}
- (NSGraphicsContext *)context;
- (NSGraphicsContext*)context;
- (void)destroy;
- (NSBitmapImageRep *)getRep;
- (NSBitmapImageRep*)getRep;
@end
@interface MilskoCocoa : NSObject {
NSApplication *application;
NSApplication* application;
MwBool _forceRender;
NSWindow *window;
NSWindow* window;
NSRect rect;
MilskoCocoaView *view;
MilskoCocoaView* view;
MwLL parent;
MilskoFakePointer *handle;
NSUInteger strHash;
NSEvent *lastEvent;
MilskoFakePointer* handle;
unsigned int strHash;
NSEvent* lastEvent;
}
+ (MilskoCocoa *)newWithParent:(MwLL)parent
+ (MilskoCocoa*)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height
handle:(MwLL)handle;
- (void)polygonWithPoints:(MwPoint *)points
- (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)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)eventProcess:(NSEvent *)ev;
- (void)handleKeyEvent:(NSEvent *)ev;
- (void)handleMouseEvent:(NSEvent *)ev ll:(MwLL)ll;
- (void)eventProcess:(NSEvent*)ev;
- (void)handleKeyEvent:(NSEvent*)ev;
- (void)handleMouseEvent:(NSEvent*)ev ll:(MwLL)ll;
- (void)getNextEvent;
- (void)setTitle:(const char *)title;
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect *)rect;
- (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)setCursor:(MwCursor*)image mask:(MwCursor*)mask;
- (void)detachWithPoint:(MwPoint*)point;
- (void)show:(int)show;
- (void)makePopupWithParent:(MwLL)parent;
- (void)setSizeHintsWithMinX:(int)minx
@ -116,29 +117,29 @@
- (void)makeBorderless:(int)toggle;
- (void)focus;
- (void)grabPointer:(int)toggle;
- (void)setClipboard:(const char *)text;
- (void)setClipboard:(const char*)text;
- (void)makeToolWindow;
- (void)getCursorCoord:(MwPoint *)point;
- (void)getScreenSize:(MwRect *)rect;
- (void)getCursorCoord:(MwPoint*)point;
- (void)getScreenSize:(MwRect*)rect;
- (void)destroy;
- (void)sendClipboardEvent;
- (NSWindow *)parentWindow;
- (NSView *)getView;
- (NSWindow *)getWindow;
- (MilskoFakePointer *)getHandle;
- (NSWindow*)parentWindow;
- (NSView*)getView;
- (NSWindow*)getWindow;
- (MilskoFakePointer*)getHandle;
@end
#define OBJC(x) x
#else
#define OBJC(x) void *
#define OBJC(x) void*
#endif
MWDECL int MwLLCocoaCallInit(void);
struct _MwLLCocoa {
struct _MwLLCommon common;
OBJC(MilskoCocoa *)
OBJC(MilskoCocoa*)
real;
};
@ -148,7 +149,7 @@ struct _MwLLCocoaColor {
struct _MwLLCocoaPixmap {
struct _MwLLCommonPixmap common;
OBJC(MilskoCocoaPixmap *)
OBJC(MilskoCocoaPixmap*)
real;
};

View file

@ -1,38 +1,38 @@
#include "Mw/BaseTypes.h"
#include "Mw/LowLevel.h"
#include <Mw/Milsko.h>
#include <stdlib.h>
#ifdef __clang__
#pragma clang push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
static CGRect rectFlip(CGRect originFrame) {
NSScreen *zeroScreen = NSScreen.screens[0];
double screenHeight = zeroScreen.frame.size.height;
static NSRect rectFlip(NSRect originFrame) {
NSScreen* zeroScreen = [[NSScreen screens] objectAtIndex:0];
double screenHeight = [zeroScreen frame].size.height;
double originY = originFrame.origin.y;
double frameHeight = originFrame.size.height;
double destinationY = screenHeight - (originY + frameHeight);
CGRect destinationFrame = originFrame;
NSRect destinationFrame = originFrame;
destinationFrame.origin.y = destinationY;
if (destinationFrame.origin.x < 0)
if(destinationFrame.origin.x < 0)
destinationFrame.origin.x = 0;
if (destinationFrame.origin.y < 0)
if(destinationFrame.origin.y < 0)
destinationFrame.origin.y = 0;
if (destinationFrame.size.width < 0)
if(destinationFrame.size.width < 0)
destinationFrame.size.width = 0;
if (destinationFrame.size.height < 0)
if(destinationFrame.size.height < 0)
destinationFrame.size.height = 0;
return destinationFrame;
}
static CGPoint pointFlip(CGPoint point) {
static NSPoint pointFlip(NSPoint point) {
return rectFlip(NSMakeRect(point.x, point.y, 0, 0)).origin;
}
@implementation MilskoCocoaPixmap
+ (MilskoCocoaPixmap *)newWithWidth:(int)width height:(int)height {
MilskoCocoaPixmap *p = [MilskoCocoaPixmap alloc];
+ (MilskoCocoaPixmap*)newWithWidth:(int)width height:(int)height {
MilskoCocoaPixmap* p = [MilskoCocoaPixmap alloc];
p->width = width;
p->height = height;
@ -59,7 +59,7 @@ static CGPoint pointFlip(CGPoint point) {
return p;
}
- (void)updateWithData:(unsigned char *)_data {
- (void)updateWithData:(unsigned char*)_data {
memcpy(self->buf, _data, width * height * 4);
}
- (void)destroy {
@ -68,32 +68,32 @@ static CGPoint pointFlip(CGPoint point) {
[self->image dealloc];
[self->rep dealloc];
}
- (NSImage *)image {
- (NSImage*)image {
return self->image;
}
@end
@implementation MilskoCocoa
+ (MilskoCocoa *)newWithParent:(MwLL)parent
+ (MilskoCocoa*)newWithParent:(MwLL)parent
x:(int)x
y:(int)y
width:(int)width
height:(int)height
handle:(MwLL)r {
MilskoCocoa *c = [MilskoCocoa alloc];
MilskoCocoa* c = [MilskoCocoa alloc];
[c retain];
if (x == MwDEFAULT) {
x = ([NSScreen mainScreen].frame.size.width / 2.) - (width / 2.);
if(x == MwDEFAULT) {
x = ([[NSScreen mainScreen] frame].size.width / 2.) - (width / 2.);
}
if (y == MwDEFAULT) {
y = ([NSScreen mainScreen].frame.size.height / 2.) - (height / 2.);
if(y == MwDEFAULT) {
y = ([[NSScreen mainScreen] frame].size.height / 2.) - (height / 2.);
}
c->application = [NSApplication sharedApplication];
c->rect = rectFlip(NSMakeRect(x, y, width, height));
if (parent == NULL) {
if(parent == NULL) {
c->window = [[NSWindow alloc]
initWithContentRect:c->rect
styleMask:(NSTitledWindowMask | NSClosableWindowMask |
@ -102,13 +102,13 @@ static CGPoint pointFlip(CGPoint point) {
defer:NO];
} else {
double offset = 0;
NSWindow *parentWindow = parent->cocoa.real->window;
NSWindow* parentWindow = parent->cocoa.real->window;
offset =
[parentWindow frameRectForContentRect:parentWindow.frame].size.height -
[parentWindow contentRectForFrameRect:parentWindow.frame].size.height;
[parentWindow frameRectForContentRect:[parentWindow frame]].size.height -
[parentWindow contentRectForFrameRect:[parentWindow frame]].size.height;
c->rect.origin.x += parentWindow.frame.origin.x;
c->rect.origin.y -= parentWindow.frame.origin.y - offset;
c->rect.origin.x += [parentWindow frame].origin.x;
c->rect.origin.y -= [parentWindow frame].origin.y - offset;
c->rect.origin.y -= offset;
c->window = [[NSWindow alloc] initWithContentRect:c->rect
@ -116,24 +116,25 @@ static CGPoint pointFlip(CGPoint point) {
backing:NSBackingStoreBuffered
defer:NO];
}
c->window.delegate = (id<NSWindowDelegate>)[[MilskoCocoaWindowDelegate alloc]
initWithWin:c->window];
[c->window setDelegate:[[MilskoCocoaWindowDelegate alloc]
initWithWin:c->window]];
[c->window makeKeyAndOrderFront:c->application];
[c->window retain];
if (parent != NULL) {
MilskoCocoa *p = parent->cocoa.real;
if(parent != NULL) {
MilskoCocoa* p = parent->cocoa.real;
[p->window addChildWindow:c->window ordered:NSWindowAbove];
[c->window setHasShadow:MwFALSE];
} else {
[c->application setActivationPolicy:NSApplicationActivationPolicyRegular];
[c->application activateIgnoringOtherApps:true];
[c->window makeFirstResponder:c->view];
}
c->view = [[MilskoCocoaView alloc] initWithFrame:c->rect];
[c->view retain];
c->handle = [[MilskoFakePointer alloc] initWithFrame:NSMakeRect(0, 0, 1, 1)];
[c->handle retain];
[c->handle setPointer:r];
[c->view addSubview:c->handle];
@ -146,15 +147,15 @@ static CGPoint pointFlip(CGPoint point) {
return c;
}
- (void)polygonWithPoints:(MwPoint *)points
- (void)polygonWithPoints:(MwPoint*)points
points_count:(int)points_count
color:(MwLLColor)color {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSGraphicsContext *ctx = [self->view context];
if (ctx) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSGraphicsContext* ctx = [self->view context];
if(ctx) {
int i;
NSBezierPath *path = [NSBezierPath bezierPath];
NSColor *nscolor = [NSColor colorWithRed:color->common.red / 255.
NSBezierPath* path = [NSBezierPath bezierPath];
NSColor* nscolor = [NSColor colorWithCalibratedRed:color->common.red / 255.
green:color->common.green / 255.
blue:color->common.blue / 255.
alpha:1.0];
@ -163,8 +164,8 @@ static CGPoint pointFlip(CGPoint point) {
[NSGraphicsContext setCurrentContext:ctx];
[nscolor setFill];
for (i = 0; i < points_count; i++) {
if (i == 0) {
for(i = 0; i < points_count; i++) {
if(i == 0) {
[path moveToPoint:NSMakePoint(points[i].x,
[self->window frame].size.height -
points[i].y)];
@ -184,9 +185,11 @@ static CGPoint pointFlip(CGPoint point) {
}
[pool release];
};
- (void)lineWithPoints:(MwPoint *)points color:(MwLLColor)color {
- (void)lineWithPoints:(MwPoint*)points color:(MwLLColor)color {
(void)points;
(void)color;
};
- (void)getX:(int *)x Y:(int *)y W:(unsigned int *)w H:(unsigned int *)h {
- (void)getX:(int*)x Y:(int*)y W:(unsigned int*)w H:(unsigned int*)h {
NSRect frame = rectFlip([self->window frame]);
frame = rectFlip(frame);
@ -204,22 +207,20 @@ static CGPoint pointFlip(CGPoint point) {
frame = rectFlip(frame);
if (parent) {
if(parent) {
double offset = 0;
NSWindow *parentWindow = parent->cocoa.real->window;
NSWindow* parentWindow = parent->cocoa.real->window;
offset =
[parentWindow frameRectForContentRect:parentWindow.frame].size.height -
[parentWindow contentRectForFrameRect:parentWindow.frame].size.height;
[parentWindow frameRectForContentRect:[parentWindow frame]].size.height -
[parentWindow contentRectForFrameRect:[parentWindow frame]].size.height;
if (x < parentWindow.frame.origin.x) {
frame.origin.x += parentWindow.frame.origin.x;
if(x < [parentWindow frame].origin.x) {
frame.origin.x += [parentWindow frame].origin.x;
}
if (y < parentWindow.frame.origin.y) {
frame.origin.y -= parentWindow.frame.origin.y - offset;
frame.origin.y -= [parentWindow frame].origin.y - offset;
frame.origin.y -= offset;
}
}
[self->view setFrameSize:frame.size];
[self->window setFrame:frame display:YES animate:false];
@ -235,9 +236,9 @@ static CGPoint pointFlip(CGPoint point) {
[self->window setFrame:frame display:YES animate:false];
};
- (int)pending {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
MwBool isPending = MwFALSE;
if (_forceRender) {
if(_forceRender) {
_forceRender = MwFALSE;
[pool release];
return 1;
@ -253,102 +254,106 @@ static CGPoint pointFlip(CGPoint point) {
};
- (void)getNextEvent {
NSEvent *ev;
while ((ev = [self->window nextEventMatchingMask:NSAnyEventMask
while(true) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSEvent* ev = [self->window nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES])) {
dequeue:YES];
if(!ev) {
[pool release];
break;
}
[self eventProcess:ev];
[ev release];
[pool release];
}
[self sendClipboardEvent];
};
- (void)eventProcess:(NSEvent *)ev {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSWindow *win = [ev window];
NSWindow *parentWindow = [self parentWindow];
- (void)eventProcess:(NSEvent*)ev {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSWindow* win = [ev window];
MwLL h;
MwBool doSendEvent = MwTRUE;
if (!win) {
if(!win) {
[pool release];
return;
}
if ([win contentView].subviews.count == 0) {
if([[[win contentView] subviews] count] == 0) {
printf("no subviews on %p\n", win);
[pool release];
return;
} else {
h = [((MilskoFakePointer *)[win contentView].subviews[0]) pointer];
h = [((MilskoFakePointer*)[[[win contentView] subviews] objectAtIndex:0]) pointer];
}
switch (ev.type) {
case NSEventTypeLeftMouseDown:
case NSEventTypeRightMouseDown:
case NSEventTypeOtherMouseDown:
case NSEventTypeLeftMouseUp:
case NSEventTypeRightMouseUp:
case NSEventTypeOtherMouseUp: {
switch([ev type]) {
case NSLeftMouseDown:
case NSRightMouseDown:
case NSOtherMouseDown:
case NSLeftMouseUp:
case NSRightMouseUp:
case NSOtherMouseUp: {
[self handleMouseEvent:ev ll:h];
}
case NSEventTypeLeftMouseDragged:
case NSEventTypeRightMouseDragged:
case NSEventTypeOtherMouseDragged:
case NSEventTypeMouseMoved: {
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged:
case NSMouseMoved: {
MwPoint pos;
pos.x = [ev locationInWindow].x;
pos.y = [win contentRectForFrameRect:win.frame].size.height -
pos.y = [win contentRectForFrameRect:[win frame]].size.height -
[ev locationInWindow].y;
MwLLDispatch(h, move, &pos);
break;
}
case NSEventTypeMouseEntered:
case NSMouseEntered:
MwLLDispatch(h, focus_in, NULL);
break;
case NSEventTypeMouseExited:
case NSMouseExited:
MwLLDispatch(h, focus_out, NULL);
break;
case NSEventTypeKeyDown:
case NSEventTypeKeyUp: {
case NSKeyDown:
case NSKeyUp: {
[self handleKeyEvent:ev];
doSendEvent = MwFALSE;
}
case NSEventTypeCursorUpdate:
case NSCursorUpdate:
break;
case NSEventTypeScrollWheel:
case NSScrollWheel:
break;
default:
break;
};
if (doSendEvent) {
if(doSendEvent) {
[win sendEvent:ev];
}
[pool release];
}
- (void)handleMouseEvent:(NSEvent *)ev ll:(MwLL)ll {
MwLLMouse mouse = {};
- (void)handleMouseEvent:(NSEvent*)ev ll:(MwLL)ll {
MwLLMouse mouse;
MwBool isDown = MwTRUE;
CGPoint mousePoint = pointFlip([ev locationInWindow]);
switch (ev.type) {
case NSEventTypeLeftMouseUp:
NSPoint mousePoint = pointFlip([ev locationInWindow]);
switch([ev type]) {
case NSLeftMouseUp:
isDown = MwFALSE;
case NSEventTypeLeftMouseDown:
case NSLeftMouseDown:
mouse.button = MwLLMouseLeft;
break;
case NSEventTypeRightMouseUp:
case NSRightMouseUp:
isDown = MwFALSE;
case NSEventTypeRightMouseDown:
case NSRightMouseDown:
mouse.button = MwLLMouseRight;
break;
case NSEventTypeOtherMouseUp:
case NSOtherMouseUp:
isDown = MwFALSE;
case NSEventTypeOtherMouseDown:
case NSOtherMouseDown:
mouse.button = MwLLMouseMiddle;
break;
default:
@ -357,16 +362,16 @@ static CGPoint pointFlip(CGPoint point) {
mouse.point.x = mousePoint.x;
mouse.point.y = mousePoint.y;
if (isDown) {
if(isDown) {
MwLLDispatch(ll, down, &mouse);
} else {
MwLLDispatch(ll, up, &mouse);
}
}
- (void)handleKeyEvent:(NSEvent *)ev {
- (void)handleKeyEvent:(NSEvent*)ev {
int ch;
MwLL this = self->handle.pointer;
MwLL this = [self->handle pointer];
enum {
kVK_ANSI_A = 0x00,
kVK_ANSI_S = 0x01,
@ -431,7 +436,7 @@ static CGPoint pointFlip(CGPoint point) {
case x: \
ch = y; \
break;
switch (ev.keyCode) {
switch([ev keyCode]) {
KEY_CASE(kVK_ANSI_A, 'a')
KEY_CASE(kVK_ANSI_B, 'b')
KEY_CASE(kVK_ANSI_C, 'c')
@ -491,11 +496,11 @@ static CGPoint pointFlip(CGPoint point) {
KEY_CASE(kVK_RightArrow, MwLLKeyRight)
KEY_CASE(kVK_UpArrow, MwLLKeyUp)
}
switch (ev.type) {
case NSEventTypeKeyDown:
switch([ev type]) {
case NSKeyDown:
MwLLDispatch(this, key, &ch);
break;
case NSEventTypeKeyUp:
case NSKeyUp:
MwLLDispatch(this, key_released, &ch);
break;
default:
@ -504,24 +509,24 @@ static CGPoint pointFlip(CGPoint point) {
}
- (void)sendClipboardEvent {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *items = @[
/*NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
NSArray* items = @[
@"public.utf8-plain-text",
@"public.utf16-external-plain-text",
@"com.apple.traditional-mac-plain-text",
];
MwLL this = self->handle.pointer;
if ([pasteboard canReadItemWithDataConformingToTypes:items]) {
char *data = NULL;
if([pasteboard canReadItemWithDataConformingToTypes:items]) {
char* data = NULL;
size_t size = 0;
for (NSPasteboardItem *item in [pasteboard pasteboardItems]) {
for (NSString *it in items) {
NSString *itemData = [item stringForType:(NSString *)it];
if (itemData != NULL) {
if (strHash != 0 && strHash != [itemData hash]) {
char *text = malloc([itemData length]);
for(NSPasteboardItem* item in [pasteboard pasteboardItems]) {
for(NSString* it in items) {
NSString* itemData = [item stringForType:(NSString*)it];
if(itemData != NULL) {
if(strHash != 0 && strHash != [itemData hash]) {
char* text = malloc([itemData length]);
strncpy(text, [itemData UTF8String], [itemData length]);
MwLLDispatch(this, clipboard, text);
printf("%s -> %p\n", text, this);
@ -533,20 +538,20 @@ static CGPoint pointFlip(CGPoint point) {
}
}
[pool release];
[pool release];*/
}
- (void)setTitle:(const char *)title {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- (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 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MilskoCocoaPixmap *p = pixmap->cocoa.real;
NSGraphicsContext *ctx = [self->view context];
if (ctx) {
- (void)drawPixmap:(MwLLPixmap)pixmap rect:(MwRect*)_rect {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
MilskoCocoaPixmap* p = pixmap->cocoa.real;
NSGraphicsContext* ctx = [self->view context];
if(ctx) {
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:ctx];
@ -554,9 +559,7 @@ static CGPoint pointFlip(CGPoint point) {
_rect->height)
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:NO
hints:nil];
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
@ -565,26 +568,36 @@ static CGPoint pointFlip(CGPoint point) {
[pool release];
};
- (void)setIcon:(MwLLPixmap)pixmap {
(void)pixmap;
};
- (void)forceRender {
_forceRender = MwTRUE;
};
- (void)setCursor:(MwCursor *)image mask:(MwCursor *)mask {
- (void)setCursor:(MwCursor*)image mask:(MwCursor*)mask {
(void)image;
(void)mask;
};
- (void)detachWithPoint:(MwPoint *)point {
- (void)detachWithPoint:(MwPoint*)point {
(void)point;
};
- (void)show:(int)show {
(void)show;
};
- (void)makePopupWithParent:(MwLL)parent {
- (void)makePopupWithParent:(MwLL)_parent {
(void)_parent;
};
- (void)setSizeHintsWithMinX:(int)minx
MinY:(int)miny
MaxX:(int)maxx
MaxY:(int)maxy {
(void)minx;
(void)miny;
(void)maxx;
(void)maxy;
};
- (void)makeBorderless:(int)toggle {
MwU32 mask = [self->window styleMask];
if (mask & NSBorderlessWindowMask) {
if(toggle) {
mask ^= NSBorderlessWindowMask;
mask |= NSTitledWindowMask;
} else {
@ -600,34 +613,37 @@ static CGPoint pointFlip(CGPoint point) {
[self->window makeMainWindow];
};
- (void)grabPointer:(int)toggle {
(void)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 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:@[ NSPasteboardTypeString ] owner:nil];
[pasteboard setString:@(text) forType:NSPasteboardTypeString];
[pool release];
- (void)setClipboard:(const char*)text {
(void)text;
// TODO: find out how to do this while supporting 10.4
// NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
// [pasteboard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString] owner:nil];
// [pasteboard setString:[NSString stringWithUTF8String:text] forType:NSPasteboardTypeString];
// [pool release];
};
- (void)getClipboard {
};
- (void)makeToolWindow {
};
- (void)getCursorCoord:(MwPoint *)point {
- (void)getCursorCoord:(MwPoint*)point {
NSPoint p = [NSEvent mouseLocation];
point->x = p.x;
point->y = p.y;
};
- (void)getScreenSize:(MwRect *)_rect {
NSScreen *screen = [self->window screen];
- (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.width;
_rect->height = [screen frame].size.height;
};
- (void)destroy {
if (self->lastEvent) {
if(self->lastEvent) {
[self->lastEvent release];
[self->lastEvent dealloc];
}
@ -637,21 +653,21 @@ static CGPoint pointFlip(CGPoint point) {
[self->window dealloc];
}
- (NSWindow *)parentWindow {
NSWindow *topmostWindow = self->window;
while (topmostWindow.parentWindow)
topmostWindow = topmostWindow.parentWindow;
- (NSWindow*)parentWindow {
NSWindow* topmostWindow = self->window;
while([topmostWindow parentWindow])
topmostWindow = [topmostWindow parentWindow];
return topmostWindow;
}
- (NSView *)getView {
- (NSView*)getView {
return view;
}
- (NSWindow *)getWindow {
- (NSWindow*)getWindow {
return window;
}
- (MilskoFakePointer *)getHandle {
- (MilskoFakePointer*)getHandle {
return handle;
}
@end
@ -663,7 +679,7 @@ static CGPoint pointFlip(CGPoint point) {
self = [super initWithFrame:frame];
self->space = CGColorSpaceCreateDeviceRGB();
if (width == 0 || height == 0) {
if(width == 0 || height == 0) {
self->rep = NULL;
self->context = NULL;
} else {
@ -679,28 +695,30 @@ static CGPoint pointFlip(CGPoint point) {
bytesPerRow:width * 4
bitsPerPixel:32];
assert(self->rep);
[self->rep retain];
self->context =
[NSGraphicsContext graphicsContextWithBitmapImageRep:self->rep];
assert(self->context);
[self->context retain];
}
return self;
}
- (NSGraphicsContext *)context {
- (NSGraphicsContext*)context {
return self->context;
}
- (NSBitmapImageRep *)getRep {
- (NSBitmapImageRep*)getRep {
return self->rep;
}
- (void)drawRect:(NSRect)dirtyRect {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSize sz = self->rep.size;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSSize sz = [self->rep size];
[super drawRect:dirtyRect];
if (!self->rep) {
if(!self->rep) {
[pool release];
return;
}
@ -726,36 +744,39 @@ static CGPoint pointFlip(CGPoint point) {
}
- (void)displayRect:(NSRect)rect {
(void)rect;
};
@end
@implementation MilskoCocoaWindowDelegate
- (NSSize)windowWillResize:(NSWindow *)win toSize:(NSSize)frameSize;
- (NSSize)windowWillResize:(NSWindow*)win toSize:(NSSize)frameSize;
{
if (win.contentView.subviews.count >= 1) {
MilskoFakePointer *ptr = win.contentView.subviews[0];
if([[[win contentView] subviews] count] >= 1) {
MilskoFakePointer* ptr = [[[win contentView] subviews] objectAtIndex:0];
MwLL h = [ptr pointer];
MwLLDispatch(h, resize, NULL);
// MwLLDispatch(h, resize, NULL);
MwLLDispatch(h, draw, NULL);
}
return frameSize;
}
- (void)windowDidResize:(NSNotification *)notification {
- (void)windowDidResize:(NSNotification*)notification {
(void)notification;
}
// This will close/terminate the application when the main window is closed.
- (void)windowWillClose:(NSNotification *)notification {
- (void)windowWillClose:(NSNotification*)notification {
(void)notification;
// MilskoCocoa *window = notification.object;
// MwLL handle = [window getHandle].pointer;
// MwLLDispatch(handle, close, NULL);
[NSApp terminate:nil];
}
- (instancetype)initWithWin:(NSWindow *)win {
- (MilskoCocoaWindowDelegate*)initWithWin:(NSWindow*)win {
self->w = win;
return self;
}
@ -763,16 +784,17 @@ static CGPoint pointFlip(CGPoint point) {
@end
@implementation MilskoFakePointer
- (void)setPointer:(void *)pointer {
self.frame = *(NSRect *)&pointer;
- (void)setPointer:(void*)pointer {
[self setFrame:*(NSRect*)&pointer];
self->ptr = pointer;
};
- (void *)pointer {
- (void*)pointer {
return self->ptr;
};
- (void)drawRect:(NSRect)dirtyRect {
/* explicitly do nothing */
(void)dirtyRect;
}
- (void)destroy {
@ -790,7 +812,7 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
MwLLCreateCommon(r);
MilskoCocoa *o = [MilskoCocoa newWithParent:parent
MilskoCocoa* o = [MilskoCocoa newWithParent:parent
x:x
y:y
width:width
@ -802,7 +824,7 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) {
}
static void MwLLDestroyImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h destroy];
@ -811,18 +833,22 @@ static void MwLLDestroyImpl(MwLL handle) {
free(handle);
}
static void MwLLBeginDrawImpl(MwLL handle) { (void)handle; }
static void MwLLBeginDrawImpl(MwLL handle) {
(void)handle;
}
static void MwLLEndDrawImpl(MwLL handle) { (void)handle; }
static void MwLLEndDrawImpl(MwLL handle) {
(void)handle;
}
static void MwLLPolygonImpl(MwLL handle, MwPoint *points, int points_count,
static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count,
MwLLColor color) {
MilskoCocoa *h = handle->cocoa.real;
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;
static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) {
MilskoCocoa* h = handle->cocoa.real;
[h lineWithPoints:points color:color];
}
@ -840,27 +866,29 @@ static void MwLLColorUpdateImpl(MwLL handle, MwLLColor c, int r, int g, int b) {
c->common.blue = b;
}
static void MwLLGetXYWHImpl(MwLL handle, int *x, int *y, unsigned int *w,
unsigned int *height) {
MilskoCocoa *h = handle->cocoa.real;
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;
MilskoCocoa* h = handle->cocoa.real;
[h setX:x Y:y];
}
static void MwLLSetWHImpl(MwLL handle, int w, int height) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h setW:w H:height];
}
static void MwLLFreeColorImpl(MwLLColor color) { free(color); }
static void MwLLFreeColorImpl(MwLLColor color) {
free(color);
}
static int MwLLPendingImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
if ([h pending]) {
MilskoCocoa* h = handle->cocoa.real;
if([h pending]) {
MwLLDispatch(handle, draw, NULL);
return 1;
};
@ -868,16 +896,16 @@ static int MwLLPendingImpl(MwLL handle) {
}
static void MwLLNextEventImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h getNextEvent];
}
static void MwLLSetTitleImpl(MwLL handle, const char *title) {
MilskoCocoa *h = handle->cocoa.real;
static void MwLLSetTitleImpl(MwLL handle, const char* title) {
MilskoCocoa* h = handle->cocoa.real;
[h setTitle:title];
}
static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char *data,
static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char* data,
int width, int height) {
(void)handle;
@ -897,102 +925,110 @@ static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char *data,
}
static void MwLLPixmapUpdateImpl(MwLLPixmap pixmap) {
MilskoCocoaPixmap *p = pixmap->cocoa.real;
MilskoCocoaPixmap* p = pixmap->cocoa.real;
[p updateWithData:pixmap->common.raw];
}
static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) {
MilskoCocoaPixmap *p = pixmap->cocoa.real;
MilskoCocoaPixmap* p = pixmap->cocoa.real;
[p destroy];
[p dealloc];
free(pixmap);
}
static void MwLLDrawPixmapImpl(MwLL handle, MwRect *rect, MwLLPixmap pixmap) {
MilskoCocoa *h = handle->cocoa.real;
static void MwLLDrawPixmapImpl(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
MilskoCocoa* h = handle->cocoa.real;
[h drawPixmap:pixmap rect:rect];
MwLLForceRender(handle);
}
static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h setIcon:pixmap];
}
static void MwLLForceRenderImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h forceRender];
}
static void MwLLSetCursorImpl(MwLL handle, MwCursor *image, MwCursor *mask) {
MilskoCocoa *h = handle->cocoa.real;
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;
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;
MilskoCocoa* h = handle->cocoa.real;
[h show:show];
}
static void MwLLMakePopupImpl(MwLL handle, MwLL parent) {
MilskoCocoa *h = handle->cocoa.real;
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;
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;
MilskoCocoa* h = handle->cocoa.real;
[h makeBorderless:toggle];
}
static void MwLLFocusImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h focus];
}
static void MwLLGrabPointerImpl(MwLL handle, int toggle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h grabPointer:toggle];
}
static void MwLLSetClipboardImpl(MwLL handle, const char *text) {
MilskoCocoa *h = handle->cocoa.real;
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 MwLLGetClipboardImpl(MwLL handle) {
(void)handle;
}
static void MwLLMakeToolWindowImpl(MwLL handle) {
MilskoCocoa *h = handle->cocoa.real;
MilskoCocoa* h = handle->cocoa.real;
[h makeToolWindow];
}
static void MwLLGetCursorCoordImpl(MwLL handle, MwPoint *point) {
MilskoCocoa *h = handle->cocoa.real;
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;
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 MwLLBeginStateChangeImpl(MwLL handle) {
MwLLShow(handle, 0);
}
static void MwLLEndStateChangeImpl(MwLL handle) { MwLLShow(handle, 1); }
static void MwLLEndStateChangeImpl(MwLL handle) {
MwLLShow(handle, 1);
}
static int MwLLCocoaCallInitImpl(void) { return 0; }
static int MwLLCocoaCallInitImpl(void) {
return 0;
}
#include "call.c"
CALL(Cocoa);