1 #include <Cocoa/Cocoa.h>
4 #define CAML_NAME_SPACE
7 #include <sys/socket.h>
11 #include <caml/mlvalues.h>
12 #include <caml/memory.h>
13 #include <caml/callback.h>
14 #include <caml/alloc.h>
15 #include <caml/fail.h>
40 static int terminating = 0;
41 static pthread_mutex_t terminate_mutex = PTHREAD_MUTEX_INITIALIZER;
42 static int server_fd = -1;
43 static CGFloat backing_scale_factor = -1.0;
45 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 10120
47 #define NS_CRITICAL_ALERT_STYLE NSAlertStyleCritical
48 #define NS_FULL_SCREEN_WINDOW_MASK NSWindowStyleMaskFullScreen
49 #define NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK NSEventModifierFlagDeviceIndependentFlagsMask
50 #define NS_FUNCTION_KEY_MASK NSEventModifierFlagFunction
51 #define NS_ALTERNATE_KEY_MASK NSEventModifierFlagOption
52 #define NS_COMMAND_KEY_MASK NSEventModifierFlagCommand
53 #define NS_CLOSABLE_WINDOW_MASK NSWindowStyleMaskClosable
54 #define NS_MINIATURIZABLE_WINDOW_MASK NSWindowStyleMaskMiniaturizable
55 #define NS_TITLED_WINDOW_MASK NSWindowStyleMaskTitled
56 #define NS_RESIZABLE_WINDOW_MASK NSWindowStyleMaskResizable
60 #define NS_CRITICAL_ALERT_STYLE NSCriticalAlertStyle
61 #define NS_FULL_SCREEN_WINDOW_MASK NSFullScreenWindowMask
62 #define NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK NSDeviceIndependentModifierFlagsMask
63 #define NS_FUNCTION_KEY_MASK NSFunctionKeyMask
64 #define NS_ALTERNATE_KEY_MASK NSAlternateKeyMask
65 #define NS_COMMAND_KEY_MASK NSCommandKeyMask
66 #define NS_CLOSABLE_WINDOW_MASK NSClosableWindowMask
67 #define NS_MINIATURIZABLE_WINDOW_MASK NSMiniaturizableWindowMask
68 #define NS_TITLED_WINDOW_MASK NSTitledWindowMask
69 #define NS_RESIZABLE_WINDOW_MASK NSResizableWindowMask
73 void Abort (NSString *format, ...)
76 va_start (argList, format);
77 NSString *str = [[NSString alloc] initWithFormat:format arguments:argList];
80 NSAlert *alert = [[NSAlert alloc] init];
81 [alert addButtonWithTitle:@"Quit"];
82 [alert setMessageText:@"Internal Error"];
83 [alert setInformativeText:str];
84 [alert setAlertStyle:NS_CRITICAL_ALERT_STYLE];
86 [NSApp terminate:nil];
89 void *caml_main_thread (void *argv)
93 pthread_mutex_lock (&terminate_mutex);
94 if (terminating == 0) {
96 [NSApp performSelectorOnMainThread:@selector(terminate:)
100 pthread_mutex_unlock (&terminate_mutex);
105 NSCursor *GetCursor (int idx)
107 static NSCursor *cursors[5];
108 static BOOL initialised = NO;
110 if (initialised == NO) {
111 cursors[0] = [NSCursor arrowCursor];
112 cursors[1] = [NSCursor pointingHandCursor];
113 cursors[2] = [NSCursor arrowCursor];
114 cursors[3] = [NSCursor closedHandCursor];
115 cursors[4] = [NSCursor IBeamCursor];
122 @implementation NSWindow (CategoryNSWindow)
126 return ([self styleMask] & NS_FULL_SCREEN_WINDOW_MASK) == NS_FULL_SCREEN_WINDOW_MASK;
131 @implementation NSView (CategoryNSView)
133 - (NSPoint)locationFromEvent:(NSEvent *)event
136 [self convertPointToBacking:[self convertPoint:[event locationInWindow] fromView:nil]];
137 NSRect bounds = [self convertRectToBacking:[self bounds]];
138 point.y = bounds.size.height - point.y;
142 - (NSRect)convertFrameToBacking
144 return [self convertRectToBacking:[self frame]];
149 @implementation NSEvent (CategoryNSEvent)
151 - (int)deviceIndependentModifierFlags
153 return [self modifierFlags] & NS_DEVICE_INDEPENDENT_MODIFIER_FLAGS_MASK;
158 @interface Connector : NSObject
160 - (instancetype)initWithFileDescriptor:(int)fd;
162 - (void)notifyReshapeWidth:(int)w height:(int)h;
163 - (void)notifyExpose;
164 - (void)keyDown:(uint32_t)key modifierFlags:(NSEventModifierFlags)mask;
166 - (void)mouseEntered:(NSPoint)loc;
168 - (void)mouseMoved:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
169 - (void)mouseDown:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
170 - (void)mouseUp:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags;
173 @implementation Connector
176 NSFileHandle *fileHandle;
179 - (instancetype)initWithFileDescriptor:(int)fd
182 data = [NSMutableData dataWithLength:32];
183 fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd];
187 - (void)setByte:(int8_t)b offset:(int)off
189 [data replaceBytesInRange:NSMakeRange (off, 1) withBytes:&b];
192 - (void)setShort:(int16_t)s offset:(int)off
194 [data replaceBytesInRange:NSMakeRange (off, 2) withBytes:&s];
197 - (void)setInt:(int32_t)n offset:(int)off
199 [data replaceBytesInRange:NSMakeRange (off, 4) withBytes:&n];
204 [fileHandle writeData:data];
207 - (void)notifyReshapeWidth:(int)w height:(int)h
209 [self setByte:EVENT_RESHAPE offset:0];
210 [self setShort:w offset:16];
211 [self setShort:h offset:18];
217 [self setByte:EVENT_EXPOSE offset:0];
221 - (void)keyDown:(uint32_t)key modifierFlags:(NSEventModifierFlags)mask
223 [self setByte:EVENT_KEYDOWN offset:0];
224 [self setInt:key offset:16];
225 [self setInt:mask offset:20];
229 - (void)notifyWinstate:(BOOL)fullScreen
231 [self setByte:EVENT_WINSTATE offset:0];
232 [self setInt:fullScreen offset:16];
238 [self setByte:EVENT_QUIT offset:0];
242 - (void)mouseEntered:(NSPoint)loc
244 [self setByte:EVENT_ENTER offset:0];
245 [self setShort:loc.x offset:16];
246 [self setShort:loc.y offset:20];
252 [self setByte:EVENT_LEAVE offset:0];
256 - (void)mouseDragged:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
258 [self setByte:EVENT_MOTION offset:0];
259 [self setShort:aPoint.x offset:16];
260 [self setShort:aPoint.y offset:20];
261 [self setInt:flags offset:24];
265 - (void)mouseMoved:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
267 [self setByte:EVENT_PMOTION offset:0];
268 [self setShort:aPoint.x offset:16];
269 [self setShort:aPoint.y offset:20];
270 [self setInt:flags offset:24];
274 - (void)mouseDown:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
276 [self setByte:EVENT_MOUSE offset:0];
277 [self setShort:1 offset:10];
278 [self setInt:buttons offset:12];
279 [self setShort:aPoint.x offset:16];
280 [self setShort:aPoint.y offset:20];
281 [self setInt:flags offset:24];
285 - (void)mouseUp:(NSUInteger)buttons atPoint:(NSPoint)aPoint modifierFlags:(NSEventModifierFlags)flags
287 [self setByte:EVENT_MOUSE offset:0];
288 [self setShort:0 offset:10];
289 [self setInt:buttons offset:12];
290 [self setShort:aPoint.x offset:16];
291 [self setShort:aPoint.y offset:20];
292 [self setInt:flags offset:24];
296 - (void)scrollByDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
298 [self setByte:EVENT_SCROLL offset:0];
299 [self setInt:(int32_t) deltaX offset:16];
300 [self setInt:(int32_t) deltaY offset:20];
304 - (void)zoom:(CGFloat)z at:(NSPoint)p
306 [self setByte:EVENT_ZOOM offset:0];
307 [self setInt:(int32_t) (z * 1000) offset:16];
308 [self setShort:p.x offset:20];
309 [self setShort:p.y offset:22];
313 - (void)openFile:(NSString *)filename
315 const char *utf8 = [filename UTF8String];
316 unsigned len = [filename lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
317 [self setByte:EVENT_OPEN offset:0];
319 unsigned data_len = [data length] - 4;
321 unsigned chunk_len = MIN (data_len - 4, len - off);
322 [self setShort:chunk_len offset:2];
323 [data replaceBytesInRange:NSMakeRange (4, chunk_len) withBytes:(utf8 + off)];
327 [self setShort:0 offset:2];
333 @interface MyDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
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 #pragma GCC diagnostic push
363 #pragma GCC diagnostic ignored "-Wdeprecated"
364 - (instancetype)initWithFrame:(NSRect)frame connector:(Connector *)aConnector
366 self = [super initWithFrame:frame];
369 connector = aConnector;
370 cursor = [NSCursor arrowCursor];
371 self.acceptsTouchEvents = YES;
375 #pragma GCC diagnostic pop
378 - (void)setCursor:(NSCursor *)aCursor
383 -(void)resetCursorRects
385 [self addCursorRect:[self bounds] cursor:cursor];
388 - (void)drawRect:(NSRect)bounds
390 // NSLog(@"drawRect: %@", [NSValue valueWithRect:bounds]);
391 [connector notifyExpose];
394 - (void)viewWillMoveToWindow:(NSWindow *)newWindow {
395 NSTrackingArea* trackingArea = [[NSTrackingArea alloc]
396 initWithRect:[self bounds]
397 options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveInActiveApp | NSTrackingInVisibleRect)
400 [self addTrackingArea:trackingArea];
403 - (void)keyDown:(NSEvent *)event
405 // int key = [event keyCode];
406 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
407 NSString *chars = [event charactersIgnoringModifiers];
408 const uint32_t *c = (uint32_t *) [chars cStringUsingEncoding:NSUTF32LittleEndianStringEncoding];
410 if (*c == 0x7f && !(mask & NS_FUNCTION_KEY_MASK)) {
411 [connector keyDown:0x8 modifierFlags:mask];
413 [connector keyDown:*c modifierFlags:mask];
419 - (void)flagsChanged:(NSEvent *)event
421 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
422 NSLog (@"flagsChanged: 0x%lx", mask);
424 [connector keyDown:0 modifierFlags:mask];
428 - (void)mouseDown:(NSEvent *)event
430 [connector mouseDown:BUTTON_LEFT
431 atPoint:[self locationFromEvent:event]
432 modifierFlags:[event deviceIndependentModifierFlags]];
435 - (void)mouseUp:(NSEvent *)event
437 [connector mouseUp:BUTTON_LEFT
438 atPoint:[self locationFromEvent:event]
439 modifierFlags:[event deviceIndependentModifierFlags]];
442 - (void)rightMouseDown:(NSEvent *)event
444 [connector mouseDown:BUTTON_RIGHT
445 atPoint:[self locationFromEvent:event]
446 modifierFlags:[event deviceIndependentModifierFlags]];
449 - (void)rightMouseUp:(NSEvent *)event
451 [connector mouseUp:BUTTON_RIGHT
452 atPoint:[self locationFromEvent:event]
453 modifierFlags:[event deviceIndependentModifierFlags]];
456 - (void)rightMouseDragged:(NSEvent *)event
458 [connector mouseDragged:[self locationFromEvent:event]
459 modifierFlags:[event deviceIndependentModifierFlags]];
462 - (void)mouseDragged:(NSEvent *)event
464 [connector mouseDragged:[self locationFromEvent:event]
465 modifierFlags:[event deviceIndependentModifierFlags]];
468 - (void)mouseMoved:(NSEvent *)event
470 [connector mouseMoved:[self locationFromEvent:event]
471 modifierFlags:[event deviceIndependentModifierFlags]];
474 - (void)mouseEntered:(NSEvent *)event
476 [connector mouseEntered:[self locationFromEvent:event]];
479 - (void)mouseExited:(NSEvent *)event
481 [connector mouseExited];
484 - (void)scrollWheel:(NSEvent *)event
486 CGFloat deltaX = [event scrollingDeltaX];
487 CGFloat deltaY = -[event scrollingDeltaY];
489 if ([event hasPreciseScrollingDeltas]) {
490 [connector scrollByDeltaX:(backing_scale_factor * deltaX)
491 deltaY:(backing_scale_factor * deltaY)];
493 NSPoint loc = [self locationFromEvent:event];
494 NSEventModifierFlags mask = [event deviceIndependentModifierFlags];
496 [connector mouseDown:BUTTON_WHEEL_DOWN atPoint:loc modifierFlags:mask];
497 [connector mouseUp:BUTTON_WHEEL_DOWN atPoint:loc modifierFlags:mask];
498 } else if (deltaY < 0.0) {
499 [connector mouseDown:BUTTON_WHEEL_UP atPoint:loc modifierFlags:mask];
500 [connector mouseUp:BUTTON_WHEEL_UP atPoint:loc modifierFlags:mask];
505 - (void)magnifyWithEvent:(NSEvent *)event
507 [connector zoom:[event magnification] at:[self locationFromEvent:event]];
512 @implementation MyWindow
514 - (BOOL)canBecomeKeyWindow
521 @implementation MyDelegate
525 NSOpenGLContext *glContext;
527 Connector *connector;
530 - (instancetype)initWithArgv:(char **)theArgv fileDescriptor:(int)fd
535 connector = [[Connector alloc] initWithFileDescriptor:fd];
540 - (void)setTitle:(NSString *)title
542 [window setTitle:title];
547 [window makeKeyAndOrderFront:self];
552 return [[window contentView] convertFrameToBacking].size.width;
557 return [[window contentView] convertFrameToBacking].size.height;
560 - (void)applicationWillFinishLaunching:(NSNotification *)not
562 NSLog(@"applicationWillFinishLaunching");
563 id menubar = [NSMenu new];
564 id appMenuItem = [NSMenuItem new];
565 id fileMenuItem = [NSMenuItem new];
566 id windowMenuItem = [NSMenuItem new];
567 id helpMenuItem = [NSMenuItem new];
568 [menubar addItem:appMenuItem];
569 [menubar addItem:fileMenuItem];
570 [menubar addItem:windowMenuItem];
571 [menubar addItem:helpMenuItem];
572 [NSApp setMainMenu:menubar];
573 id appMenu = [NSMenu new];
574 id appName = [[NSProcessInfo processInfo] processName];
575 id aboutMenuItem = [[NSMenuItem alloc] initWithTitle:[@"About " stringByAppendingString:appName]
576 action:@selector(orderFrontStandardAboutPanel:)
578 id hideMenuItem = [[NSMenuItem alloc] initWithTitle:[@"Hide " stringByAppendingString:appName]
579 action:@selector(hide:)
581 id hideOthersMenuItem = [[NSMenuItem alloc] initWithTitle:@"Hide Others"
582 action:@selector(hideOtherApplications:)
584 [hideOthersMenuItem setKeyEquivalentModifierMask:(NS_ALTERNATE_KEY_MASK | NS_COMMAND_KEY_MASK)];
585 id showAllMenuItem = [[NSMenuItem alloc] initWithTitle:@"Show All"
586 action:@selector(unhideAllApplications:)
588 id quitMenuItem = [[NSMenuItem alloc] initWithTitle:[@"Quit " stringByAppendingString:appName]
589 action:@selector(terminate:)
591 [appMenu addItem:aboutMenuItem];
592 [appMenu addItem:[NSMenuItem separatorItem]];
593 [appMenu addItem:hideMenuItem];
594 [appMenu addItem:hideOthersMenuItem];
595 [appMenu addItem:showAllMenuItem];
596 [appMenu addItem:[NSMenuItem separatorItem]];
597 [appMenu addItem:quitMenuItem];
598 [appMenuItem setSubmenu:appMenu];
600 id fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
601 id openMenuItem = [[NSMenuItem alloc] initWithTitle:@"Open..."
602 action:@selector(openDocument:)
604 id closeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Close"
605 action:@selector(performClose:)
607 [fileMenu addItem:openMenuItem];
608 [fileMenu addItem:[NSMenuItem separatorItem]];
609 [fileMenu addItem:closeMenuItem];
610 [fileMenuItem setSubmenu:fileMenu];
612 id windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
613 id miniaturizeMenuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize"
614 action:@selector(performMiniaturize:)
616 id zoomMenuItem = [[NSMenuItem alloc] initWithTitle:@"Zoom"
617 action:@selector(performZoom:)
620 [windowMenu addItem:miniaturizeMenuItem];
621 [windowMenu addItem:zoomMenuItem];
622 [windowMenuItem setSubmenu:windowMenu];
624 id helpMenu = [[NSMenu alloc] initWithTitle:@"Help"];
625 id reportIssueMenuItem = [[NSMenuItem alloc] initWithTitle:@"Report an issue..."
626 action:@selector(reportIssue:)
628 [helpMenu addItem:reportIssueMenuItem];
629 [helpMenuItem setSubmenu:helpMenu];
631 window = [[MyWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 400)
632 styleMask:(NS_CLOSABLE_WINDOW_MASK | NS_MINIATURIZABLE_WINDOW_MASK | NS_TITLED_WINDOW_MASK | NS_RESIZABLE_WINDOW_MASK)
633 backing:NSBackingStoreBuffered
637 [window setAcceptsMouseMovedEvents:YES];
638 [window setDelegate:self];
641 [[NSNotificationCenter defaultCenter] addObserver:self
642 selector:@selector(didEnterFullScreen)
643 name:NSWindowDidEnterFullScreenNotification
645 [[NSNotificationCenter defaultCenter] addObserver:self
646 selector:@selector(didExitFullScreen)
647 name:NSWindowDidExitFullScreenNotification
650 MyView *myView = [[MyView alloc] initWithFrame:[[window contentView] bounds]
651 connector:connector];
653 [window setContentView:myView];
654 [window makeFirstResponder:myView];
656 [myView setWantsBestResolutionOpenGLSurface:YES];
658 NSOpenGLPixelFormatAttribute attrs[] =
660 NSOpenGLPFAAccelerated,
661 NSOpenGLPFADoubleBuffer,
662 NSOpenGLPFAColorSize, 24,
663 NSOpenGLPFAAlphaSize, 8,
664 NSOpenGLPFADepthSize, 24,
667 NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
668 glContext = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext:nil];
670 [glContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
671 [glContext setView:myView];
673 backing_scale_factor = [window backingScaleFactor];
676 - (void)reshape:(NSValue *)val
678 // NSLog (@"reshape: %@ isFullScreen: %d", val, [window isFullScreen]);
679 if ([window isFullScreen]) {
680 [window toggleFullScreen:self];
682 [window setFrame:[window frameRectForContentRect:[val rectValue]]
686 - (void)makeCurrentContext
688 [glContext makeCurrentContext];
689 NSLog (@"OpenGL Version: %s", glGetString(GL_VERSION));
694 [glContext flushBuffer];
697 - (void)didEnterFullScreen
699 // NSLog (@"didEnterFullScreen: %d", [window isFullScreen]);
700 [connector notifyWinstate:YES];
703 - (void)didExitFullScreen
705 // NSLog (@"didExitFullScreen: %d", [window isFullScreen]);
706 [connector notifyWinstate:NO];
711 // NSLog (@"fullscreen: %d", [window isFullScreen]);
712 if ([window isFullScreen] == NO) {
713 [window toggleFullScreen:self];
717 - (void)setCursor:(NSCursor *)aCursor
719 [[window contentView] setCursor: aCursor];
720 [window invalidateCursorRectsForView:[window contentView]];
723 - (void)windowDidResize:(NSNotification *)notification
726 NSRect frame = [[window contentView] convertFrameToBacking];
727 [connector notifyReshapeWidth:frame.size.width height:frame.size.height];
730 - (void)windowDidMove:(NSNotification *)notification
735 - (void)applicationWillTerminate:(NSDictionary *)userInfo
737 pthread_mutex_lock (&terminate_mutex);
738 if (terminating == 0) {
740 [connector notifyQuit];
742 pthread_mutex_unlock (&terminate_mutex);
743 pthread_join (thread, NULL);
746 - (void)windowDidChangeOcclusionState:(NSNotification *)notification
750 - (void)applicationDidFinishLaunching:(NSNotification *)not
752 NSLog(@"applicationDidFinishLaunching");
753 int ret = pthread_create (&thread, NULL, caml_main_thread, argv);
755 Abort (@"pthread_create: %s.", strerror (ret));
759 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
764 - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
766 NSLog (@"openFile: %@", filename);
767 [connector openFile:filename];
771 #pragma GCC diagnostic push
772 #pragma GCC diagnostic ignored "-Wdeprecated"
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];
786 #pragma GCC diagnostic pop
788 - (void)reportIssue:(id)sender
790 [[NSWorkspace sharedWorkspace]
791 openURL:[NSURL URLWithString:@"https://github.com/moosotc/llpp/issues"]];
796 CAMLprim value ml_mapwin (value unit)
799 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(mapwin)
802 CAMLreturn (Val_unit);
805 CAMLprim value ml_swapb (value unit)
808 [(MyDelegate *)[NSApp delegate] swapb];
809 CAMLreturn (Val_unit);
812 CAMLprim value ml_getw (value unit)
814 return Val_int([(MyDelegate *)[NSApp delegate] getw]);
817 CAMLprim value ml_geth (value unit)
819 return Val_int([(MyDelegate *)[NSApp delegate] geth]);
822 CAMLprim value ml_makecurrentcontext (value unit)
825 [(MyDelegate *)[NSApp delegate] makeCurrentContext];
826 CAMLreturn (Val_unit);
829 CAMLprim value ml_settitle (value title)
832 NSString *str = [NSString stringWithUTF8String:String_val(title)];
833 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(setTitle:)
836 CAMLreturn (Val_unit);
839 CAMLprim value ml_reshape (value w, value h)
842 NSRect r = NSMakeRect (0, 0, Int_val (w), Int_val (h));
843 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(reshape:)
844 withObject:[NSValue valueWithRect:r]
846 CAMLreturn (Val_unit);
849 CAMLprim value ml_fullscreen (value unit)
852 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(fullscreen)
855 CAMLreturn (Val_unit);
858 CAMLprim value ml_setcursor (value curs)
861 // NSLog (@"ml_setcursor: %d", Int_val (curs));
862 NSCursor *cursor = GetCursor (Int_val (curs));
863 [(MyDelegate *)[NSApp delegate] performSelectorOnMainThread:@selector(setCursor:)
866 CAMLreturn (Val_unit);
869 CAMLprim value ml_get_server_fd (value unit)
872 CAMLreturn (Val_int (server_fd));
875 CAMLprim value ml_get_backing_scale_factor (value unit)
878 CAMLreturn (Val_int ((int) backing_scale_factor));
881 CAMLprim value ml_nslog (value str)
884 NSLog (@"%s", String_val (str));
885 CAMLreturn (Val_unit);
888 // HACK to eliminate arg injected by OS X -psn_...
889 int adjust_argv (int argc, char **argv)
891 if (argc > 1 && strncmp (argv[1], "-psn", 4) == 0) {
892 for (unsigned i = 1; i < argc - 1; i ++) {
897 for (int i = 0; i < argc; i ++) {
898 NSLog (@"arg %d: %s", i, argv[i]);
903 int main(int argc, char **argv)
907 int ret = socketpair (AF_UNIX, SOCK_STREAM, 0, sv);
909 Abort (@"socketpair: %s", strerror (errno));
911 // NSLog (@"socketpair sv0 %d sv1 %d", sv[0], sv[1]);
913 argc = adjust_argv (argc, argv);
914 [NSApplication sharedApplication];
915 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
916 id delegate = [[MyDelegate alloc] initWithArgv:argv fileDescriptor:sv[1]];
917 [NSApp setDelegate:delegate];
918 [NSApp activateIgnoringOtherApps:YES];