1 #include <Cocoa/Cocoa.h>
4 #define CAML_NAME_SPACE
7 #include <sys/socket.h>
10 #include <caml/mlvalues.h>
11 #include <caml/memory.h>
12 #include <caml/callback.h>
13 #include <caml/alloc.h>
14 #include <caml/fail.h>
39 static int terminating = 0;
40 static pthread_mutex_t terminate_mutex = PTHREAD_MUTEX_INITIALIZER;
41 static int server_fd = -1;
42 static CGFloat backing_scale_factor = -1.0;
44 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 10120
46 #define NS_CRITICAL_ALERT_STYLE NSAlertStyleCritical
47 #define NS_FULL_SCREEN_WINDOW_MASK NSWindowStyleMaskFullScreen
48 #define NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK NSEventModifierFlagDeviceIndependentFlagsMask
49 #define NS_FUNCTION_KEY_MASK NSEventModifierFlagFunction
50 #define NS_ALTERNATE_KEY_MASK NSEventModifierFlagOption
51 #define NS_COMMAND_KEY_MASK NSEventModifierFlagCommand
52 #define NS_CLOSABLE_WINDOW_MASK NSWindowStyleMaskClosable
53 #define NS_MINIATURIZABLE_WINDOW_MASK NSWindowStyleMaskMiniaturizable
54 #define NS_TITLED_WINDOW_MASK NSWindowStyleMaskTitled
55 #define NS_RESIZABLE_WINDOW_MASK NSWindowStyleMaskResizable
59 #define NS_CRITICAL_ALERT_STYLE NSCriticalAlertStyle
60 #define NS_FULL_SCREEN_WINDOW_MASK NSFullScreenWindowMask
61 #define NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK NSDeviceIndependentModifierFlagsMask
62 #define NS_FUNCTION_KEY_MASK NSFunctionKeyMask
63 #define NS_ALTERNATE_KEY_MASK NSAlternateKeyMask
64 #define NS_COMMAND_KEY_MASK NSCommandKeyMask
65 #define NS_CLOSABLE_WINDOW_MASK NSClosableWindowMask
66 #define NS_MINIATURIZABLE_WINDOW_MASK NSMiniaturizableWindowMask
67 #define NS_TITLED_WINDOW_MASK NSTitledWindowMask
68 #define NS_RESIZABLE_WINDOW_MASK NSResizableWindowMask
72 void Abort (NSString *format, ...)
75 va_start (argList, format);
76 NSString *str = [[NSString alloc] initWithFormat:format arguments:argList];
79 NSAlert *alert = [[NSAlert alloc] init];
80 [alert addButtonWithTitle:@"Quit"];
81 [alert setMessageText:@"Internal Error"];
82 [alert setInformativeText:str];
83 [alert setAlertStyle:NS_CRITICAL_ALERT_STYLE];
85 [NSApp terminate:nil];
88 void *caml_main_thread (void *argv)
92 pthread_mutex_lock (&terminate_mutex);
93 if (terminating == 0) {
95 [NSApp performSelectorOnMainThread:@selector(terminate:)
99 pthread_mutex_unlock (&terminate_mutex);
104 NSCursor *GetCursor (int idx)
106 static NSCursor *cursors[5];
107 static BOOL initialised = NO;
109 if (initialised == NO) {
110 cursors[0] = [NSCursor arrowCursor];
111 cursors[1] = [NSCursor pointingHandCursor];
112 cursors[2] = [NSCursor arrowCursor];
113 cursors[3] = [NSCursor closedHandCursor];
114 cursors[4] = [NSCursor IBeamCursor];
121 @implementation NSWindow (CategoryNSWindow)
125 return ([self styleMask] & NS_FULL_SCREEN_WINDOW_MASK) == NS_FULL_SCREEN_WINDOW_MASK;
130 @implementation NSView (CategoryNSView)
132 - (NSPoint)locationFromEvent:(NSEvent *)event
135 [self convertPointToBacking:[self convertPoint:[event locationInWindow] fromView:nil]];
136 NSRect bounds = [self convertRectToBacking:[self bounds]];
137 point.y = bounds.size.height - point.y;
141 - (NSRect)convertFrameToBacking
143 return [self convertRectToBacking:[self frame]];
148 @implementation NSEvent (CategoryNSEvent)
150 - (int)deviceIndependentModifierFlags
152 return [self modifierFlags] & NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK;
157 @interface Connector : NSObject
159 - (instancetype)initWithFileDescriptor:(int)fd;
161 - (void)notifyReshapeWidth:(int)w height:(int)h;
162 - (void)notifyExpose;
163 - (void)keyDown:(uint32_t)key modifierFlags:(NSEventModifierFlags)mask;
165 - (void)mouseEntered:(NSPoint)loc;
167 - (void)mouseMoved:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
168 - (void)mouseDown:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
169 - (void)mouseUp:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
172 @implementation Connector
175 NSFileHandle *fileHandle;
178 - (instancetype)initWithFileDescriptor:(int)fd
181 data = [NSMutableData dataWithLength:32];
182 fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd];
186 - (void)setByte:(int8_t)b offset:(int)off
188 [data replaceBytesInRange:NSMakeRange (off, 1) withBytes:&b];
191 - (void)setShort:(int16_t)s offset:(int)off
193 [data replaceBytesInRange:NSMakeRange (off, 2) withBytes:&s];
196 - (void)setInt:(int32_t)n offset:(int)off
198 [data replaceBytesInRange:NSMakeRange (off, 4) withBytes:&n];
203 [fileHandle writeData:data];
206 - (void)notifyReshapeWidth:(int)w height:(int)h
208 [self setByte:EVENT_RESHAPE offset:0];
209 [self setShort:w offset:16];
210 [self setShort:h offset:18];
216 [self setByte:EVENT_EXPOSE offset:0];
220 - (void)keyDown:(uint32_t)key modifierFlags:(NSEventModifierFlags)mask
222 [self setByte:EVENT_KEYDOWN offset:0];
223 [self setInt:key offset:16];
224 [self setInt:mask offset:20];
228 - (void)notifyWinstate:(BOOL)fullScreen
230 [self setByte:EVENT_WINSTATE offset:0];
231 [self setInt:fullScreen offset:16];
237 [self setByte:EVENT_QUIT offset:0];
241 - (void)mouseEntered:(NSPoint)loc
243 [self setByte:EVENT_ENTER offset:0];
244 [self setShort:loc.x offset:16];
245 [self setShort:loc.y offset:20];
251 [self setByte:EVENT_LEAVE offset:0];
255 - (void)mouseDragged:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
257 [self setByte:EVENT_MOTION offset:0];
258 [self setShort:aPoint.x offset:16];
259 [self setShort:aPoint.y offset:20];
260 [self setInt:flags offset:24];
264 - (void)mouseMoved:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
266 [self setByte:EVENT_PMOTION offset:0];
267 [self setShort:aPoint.x offset:16];
268 [self setShort:aPoint.y offset:20];
269 [self setInt:flags offset:24];
273 - (void)mouseDown:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
275 [self setByte:EVENT_MOUSE offset:0];
276 [self setShort:1 offset:10];
277 [self setInt:buttons offset:12];
278 [self setShort:aPoint.x offset:16];
279 [self setShort:aPoint.y offset:20];
280 [self setInt:flags offset:24];
284 - (void)mouseUp:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
286 [self setByte:EVENT_MOUSE offset:0];
287 [self setShort:0 offset:10];
288 [self setInt:buttons offset:12];
289 [self setShort:aPoint.x offset:16];
290 [self setShort:aPoint.y offset:20];
291 [self setInt:flags offset:24];
295 - (void)scrollByDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
297 [self setByte:EVENT_SCROLL offset:0];
298 [self setInt:(int32_t) deltaX offset:16];
299 [self setInt:(int32_t) deltaY offset:20];
303 - (void)zoom:(CGFloat)z at:(NSPoint)p
305 [self setByte:EVENT_ZOOM offset:0];
306 [self setInt:(int32_t) (z * 1000) offset:16];
307 [self setShort:p.x offset:20];
308 [self setShort:p.y offset:22];
312 - (void)openFile:(NSString *)filename
314 const char *utf8 = [filename UTF8String];
315 unsigned len = [filename lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
316 [self setByte:EVENT_OPEN offset:0];
318 unsigned data_len = [data length] - 4;
320 unsigned chunk_len = MIN (data_len - 4, len - off);
321 [self setShort:chunk_len offset:2];
322 [data replaceBytesInRange:NSMakeRange (4, chunk_len) withBytes:(utf8 + off)];
326 [self setShort:0 offset:2];
332 @interface MyDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
337 - (void)setwinbgcol:(NSColor *)col;
338 - (void)applicationWillFinishLaunching:(NSNotification *)not;
339 - (void)applicationDidFinishLaunching:(NSNotification *)not;
340 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
341 - (void)makeCurrentContext;
345 @interface MyWindow : NSWindow
349 @interface MyView : NSView
351 Connector *connector;
355 - (instancetype)initWithFrame:(NSRect)frame connector:(Connector *)aConnector;
356 - (void)setCursor:(NSCursor *)aCursor;
360 @implementation MyView
362 - (instancetype)initWithFrame:(NSRect)frame connector:(Connector *)aConnector
364 self = [super initWithFrame:frame];
367 connector = aConnector;
368 cursor = [NSCursor arrowCursor];
369 self.acceptsTouchEvents = YES;
375 - (void)setCursor:(NSCursor *)aCursor
380 -(void)resetCursorRects
382 [self addCursorRect:[self bounds] cursor:cursor];
385 - (void)drawRect:(NSRect)bounds
387 // NSLog(@"drawRect: %@", [NSValue valueWithRect:bounds]);
388 [connector notifyExpose];
391 - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
392 NSTrackingArea* trackingArea = [[NSTrackingArea alloc]
393 initWithRect:[self bounds]
394 options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp | NSTrackingInVisibleRect)
397 [self addTrackingArea:trackingArea];
400 - (void)keyDown:(NSEvent *)event
402 // int key = [event keyCode];
403 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
404 NSString *chars = [event charactersIgnoringModifiers];
405 const uint32_t *c = (uint32_t *) [chars cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
407 if (*c == 0x7f && !(mask & NS_FUNCTION_KEY_MASK)) {
408 [connector keyDown:0x8 modifierFlags:mask];
410 [connector keyDown:*c modifierFlags:mask];
416 - (void)flagsChanged:(NSEvent *)event
418 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
419 NSLog (@"flagsChanged: 0x%lx", mask);
421 [connector keyDown:0 modifierFlags:mask];
425 - (void)mouseDown:(NSEvent *)event
427 [connector mouseDown:BUTTON_LEFT
428 atPoint:[self locationFromEvent:event]
429 modifierFlags:[event deviceIndependentModifierFlags]];
432 - (void)mouseUp:(NSEvent *)event
434 [connector mouseUp:BUTTON_LEFT
435 atPoint:[self locationFromEvent:event]
436 modifierFlags:[event deviceIndependentModifierFlags]];
439 - (void)rightMouseDown:(NSEvent *)event
441 [connector mouseDown:BUTTON_RIGHT
442 atPoint:[self locationFromEvent:event]
443 modifierFlags:[event deviceIndependentModifierFlags]];
446 - (void)rightMouseUp:(NSEvent *)event
448 [connector mouseUp:BUTTON_RIGHT
449 atPoint:[self locationFromEvent:event]
450 modifierFlags:[event deviceIndependentModifierFlags]];
453 - (void)rightMouseDragged:(NSEvent *)event
455 [connector mouseDragged:[self locationFromEvent:event]
456 modifierFlags:[event deviceIndependentModifierFlags]];
459 - (void)mouseDragged:(NSEvent *)event
461 [connector mouseDragged:[self locationFromEvent:event]
462 modifierFlags:[event deviceIndependentModifierFlags]];
465 - (void)mouseMoved:(NSEvent *)event
467 [connector mouseMoved:[self locationFromEvent:event]
468 modifierFlags:[event deviceIndependentModifierFlags]];
471 - (void)mouseEntered:(NSEvent *)event
473 [connector mouseEntered:[self locationFromEvent:event]];
476 - (void)mouseExited:(NSEvent *)event
478 [connector mouseExited];
481 - (void)scrollWheel:(NSEvent *)event
483 CGFloat deltaX = [event scrollingDeltaX];
484 CGFloat deltaY = -[event scrollingDeltaY];
486 if ([event hasPreciseScrollingDeltas]) {
487 [connector scrollByDeltaX:(backing_scale_factor * deltaX)
488 deltaY:(backing_scale_factor * deltaY)];
490 NSPoint loc = [self locationFromEvent:event];
491 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
493 [connector mouseDown:BUTTON_WHEEL_DOWN atPoint:loc modifierFlags:mask];
494 [connector mouseUp:BUTTON_WHEEL_DOWN atPoint:loc modifierFlags:mask];
495 } else if (deltaY < 0.0) {
496 [connector mouseDown:BUTTON_WHEEL_UP atPoint:loc modifierFlags:mask];
497 [connector mouseUp:BUTTON_WHEEL_UP atPoint:loc modifierFlags:mask];
502 - (void)magnifyWithEvent:(NSEvent *)event
504 [connector zoom:[event magnification] at:[self locationFromEvent:event]];
509 @implementation MyWindow
511 - (BOOL)canBecomeKeyWindow
518 @implementation MyDelegate
522 NSOpenGLContext *glContext;
524 Connector *connector;
527 - (instancetype)initWithArgv:(char **)theArgv fileDescriptor:(int)fd
532 connector = [[Connector alloc] initWithFileDescriptor:fd];
537 - (void)setTitle:(NSString *)title
539 [window setTitle:title];
544 [window makeKeyAndOrderFront:self];
549 return [[window contentView] convertFrameToBacking].size.width;
554 return [[window contentView] convertFrameToBacking].size.height;
557 - (void)setwinbgcol:(NSColor *)col
559 [window setBackgroundColor:col];
562 - (void)applicationWillFinishLaunching:(NSNotification *)not
564 NSLog(@"applicationWillFinishLaunching");
565 id menubar = [NSMenu new];
566 id appMenuItem = [NSMenuItem new];
567 id fileMenuItem = [NSMenuItem new];
568 id windowMenuItem = [NSMenuItem new];
569 id helpMenuItem = [NSMenuItem new];
570 [menubar addItem:appMenuItem];
571 [menubar addItem:fileMenuItem];
572 [menubar addItem:windowMenuItem];
573 [menubar addItem:helpMenuItem];
574 [NSApp setMainMenu:menubar];
575 id appMenu = [NSMenu new];
576 id appName = [[NSProcessInfo processInfo] processName];
577 id aboutMenuItem = [[NSMenuItem alloc] initWithTitle:[@"About " stringByAppendingString:appName]
578 action:@selector(orderFrontStandardAboutPanel:)
580 id hideMenuItem = [[NSMenuItem alloc] initWithTitle:[@"Hide " stringByAppendingString:appName]
581 action:@selector(hide:)
583 id hideOthersMenuItem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
584 action:@selector(hideOtherApplications:)
586 [hideOthersMenuItem setKeyEquivalentModifierMask:(NS_ALTERNATE_KEY_MASK | NS_COMMAND_KEY_MASK)];
587 id showAllMenuItem = [[NSMenuItem alloc] initWithTitle:@"Show All"
588 action:@selector(unhideAllApplications:)
590 id quitMenuItem = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:appName]
591 action:@selector(terminate:)
593 [appMenu addItem:aboutMenuItem];
594 [appMenu addItem:[NSMenuItem separatorItem]];
595 [appMenu addItem:hideMenuItem];
596 [appMenu addItem:hideOthersMenuItem];
597 [appMenu addItem:showAllMenuItem];
598 [appMenu addItem:[NSMenuItem separatorItem]];
599 [appMenu addItem:quitMenuItem];
600 [appMenuItem setSubmenu:appMenu];
602 id fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
603 id openMenuItem = [[NSMenuItem alloc] initWithTitle:@"Open..."
604 action:@selector(openDocument:)
606 id closeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Close"
607 action:@selector(performClose:)
609 [fileMenu addItem:openMenuItem];
610 [fileMenu addItem:[NSMenuItem separatorItem]];
611 [fileMenu addItem:closeMenuItem];
612 [fileMenuItem setSubmenu:fileMenu];
614 id windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
615 id miniaturizeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize"
616 action:@selector(performMiniaturize:)
618 id zoomMenuItem = [[NSMenuItem alloc] initWithTitle:@"Zoom"
619 action:@selector(performZoom:)
622 [windowMenu addItem:miniaturizeMenuItem];
623 [windowMenu addItem:zoomMenuItem];
624 [windowMenuItem setSubmenu:windowMenu];
626 id helpMenu = [[NSMenu alloc] initWithTitle:@"Help"];
627 id reportIssueMenuItem = [[NSMenuItem alloc] initWithTitle:@"Report an issue..."
628 action:@selector(reportIssue:)
630 [helpMenu addItem:reportIssueMenuItem];
631 [helpMenuItem setSubmenu:helpMenu];
633 window = [[MyWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 400)
634 styleMask:(NS_CLOSABLE_WINDOW_MASK | NS_MINIATURIZABLE_WINDOW_MASK | NS_TITLED_WINDOW_MASK | NS_RESIZABLE_WINDOW_MASK)
635 backing:NSBackingStoreBuffered
639 [window setAcceptsMouseMovedEvents:YES];
640 [window setDelegate:self];
643 [[NSNotificationCenter defaultCenter] addObserver:self
644 selector:@selector(didEnterFullScreen)
645 name:NSWindowDidEnterFullScreenNotification
647 [[NSNotificationCenter defaultCenter] addObserver:self
648 selector:@selector(didExitFullScreen)
649 name:NSWindowDidExitFullScreenNotification
652 MyView *myView = [[MyView alloc] initWithFrame:[[window contentView] bounds]
653 connector:connector];
655 [window setContentView:myView];
656 [window makeFirstResponder:myView];
658 [myView setWantsBestResolutionOpenGLSurface:YES];
660 NSOpenGLPixelFormatAttribute attrs[] =
662 NSOpenGLPFAAccelerated,
663 NSOpenGLPFADoubleBuffer,
664 NSOpenGLPFAColorSize, 24,
665 NSOpenGLPFAAlphaSize, 8,
666 NSOpenGLPFADepthSize, 24,
669 NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
670 glContext = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext:nil];
672 [glContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
673 [glContext setView:myView];
675 backing_scale_factor = [window backingScaleFactor];
678 - (void)reshape:(NSValue *)val
680 // NSLog (@"reshape: %@ isFullScreen: %d", val, [window isFullScreen]);
681 if ([window isFullScreen]) {
682 [window toggleFullScreen:self];
684 [window setFrame:[window frameRectForContentRect:[val rectValue]]
688 - (void)makeCurrentContext
690 [glContext makeCurrentContext];
691 NSLog (@"OpenGL Version: %s", glGetString(GL_VERSION));
696 [glContext flushBuffer];
699 - (void)didEnterFullScreen
701 // NSLog (@"didEnterFullScreen: %d", [window isFullScreen]);
702 [connector notifyWinstate:YES];
705 - (void)didExitFullScreen
707 // NSLog (@"didExitFullScreen: %d", [window isFullScreen]);
708 [connector notifyWinstate:NO];
713 // NSLog (@"fullscreen: %d", [window isFullScreen]);
714 if ([window isFullScreen] == NO) {
715 [window toggleFullScreen:self];
719 - (void)setCursor:(NSCursor *)aCursor
721 [[window contentView] setCursor: aCursor];
722 [window invalidateCursorRectsForView:[window contentView]];
725 - (void)windowDidResize:(NSNotification *)notification
728 NSRect frame = [[window contentView] convertFrameToBacking];
729 [connector notifyReshapeWidth:frame.size.width height:frame.size.height];
732 - (void)windowDidMove:(NSNotification *)notification
737 - (void)applicationWillTerminate:(NSDictionary *)userInfo
739 pthread_mutex_lock (&terminate_mutex);
740 if (terminating == 0) {
742 [connector notifyQuit];
744 pthread_mutex_unlock (&terminate_mutex);
745 pthread_join (thread, NULL);
748 - (void)windowDidChangeOcclusionState:(NSNotification *)notification
752 - (void)applicationDidFinishLaunching:(NSNotification *)not
754 NSLog(@"applicationDidFinishLaunching");
755 int ret = pthread_create (&thread, NULL, caml_main_thread, argv);
757 Abort (@"pthread_create: %s.", strerror (ret));
761 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
766 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
768 NSLog (@"openFile: %@", filename);
769 [connector openFile:filename];
773 - (void)openDocument:(id)sender
775 NSOpenPanel *openPanel = [NSOpenPanel openPanel];
776 [openPanel beginSheetModalForWindow:window
777 completionHandler:^(NSInteger result){
778 if (result == NSFileHandlingPanelOKButton) {
779 NSString *filename = [[[openPanel URLs] objectAtIndex:0] path];
780 if (filename != nil) {
781 [self application:NSApp openFile:filename];
787 - (void)reportIssue:(id)sender
789 [[NSWorkspace sharedWorkspace]
790 openURL:[NSURL URLWithString:@"https://github.com/moosotc/llpp/issues"]];
795 CAMLprim value ml_mapwin (value unit)
798 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(mapwin)
801 CAMLreturn (Val_unit);
804 CAMLprim value ml_swapb (value unit)
807 [(MyDelegate *)[NSApp delegate] swapb];
808 CAMLreturn (Val_unit);
811 CAMLprim value ml_getw (value unit)
813 return Val_int([(MyDelegate *)[NSApp delegate] getw]);
816 CAMLprim value ml_geth (value unit)
818 return Val_int([(MyDelegate *)[NSApp delegate] geth]);
821 CAMLprim value ml_makecurrentcontext (value unit)
824 [(MyDelegate *)[NSApp delegate] makeCurrentContext];
825 CAMLreturn (Val_unit);
828 CAMLprim value ml_setwinbgcol (value col)
831 int r = ((col >> 16) & 0xff) / 255;
832 int g = ((col >> 8) & 0xff) / 255;
833 int b = ((col >> 0) & 0xff) / 255;
834 NSColor *color = [NSColor colorWithRed:r green:g blue:b alpha:1.0];
835 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(setwinbgcol:)
838 CAMLreturn (Val_unit);
841 CAMLprim value ml_settitle (value title)
844 NSString *str = [NSString stringWithUTF8String:String_val(title)];
845 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(setTitle:)
848 CAMLreturn (Val_unit);
851 CAMLprim value ml_reshape (value w, value h)
854 NSRect r = NSMakeRect (0, 0, Int_val (w), Int_val (h));
855 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(reshape:)
856 withObject:[NSValue valueWithRect:r]
858 CAMLreturn (Val_unit);
861 CAMLprim value ml_fullscreen (value unit)
864 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(fullscreen)
867 CAMLreturn (Val_unit);
870 CAMLprim value ml_setcursor (value curs)
873 // NSLog (@"ml_setcursor: %d", Int_val (curs));
874 NSCursor *cursor = GetCursor (Int_val (curs));
875 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(setCursor:)
878 CAMLreturn (Val_unit);
881 CAMLprim value ml_get_server_fd (value unit)
884 CAMLreturn (Val_int (server_fd));
887 CAMLprim value ml_get_backing_scale_factor (value unit)
890 CAMLreturn (Val_int ((int) backing_scale_factor));
893 CAMLprim value ml_nslog (value str)
896 NSLog (@"%s", String_val (str));
897 CAMLreturn (Val_unit);
900 // HACK to eliminate arg injected by OS X -psn_...
901 int adjust_argv (int argc, char **argv)
903 if (argc > 1 && strncmp (argv[1], "-psn", 4) == 0) {
904 for (unsigned i = 1; i < argc - 1; i ++) {
909 for (int i = 0; i < argc; i ++) {
910 NSLog (@"arg %d: %s", i, argv[i]);
915 int main(int argc, char **argv)
919 int ret = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
921 Abort (@"socketpair: %s", strerror (ret));
923 // NSLog (@"socketpair sv0 %d sv1 %d", sv[0], sv[1]);
925 argc = adjust_argv (argc, argv);
926 [NSApplication sharedApplication];
927 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
928 id delegate = [[MyDelegate alloc] initWithArgv:argv fileDescriptor:sv[1]];
929 [NSApp setDelegate:delegate];
930 [NSApp activateIgnoringOtherApps:YES];