| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// +build darwin |
| 6 |
// +build !ios |
| 7 |
|
| 8 |
#include "_cgo_export.h" |
| 9 |
#include <pthread.h> |
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#import <Cocoa/Cocoa.h> |
| 13 |
#import <Foundation/Foundation.h> |
| 14 |
#import <OpenGL/gl3.h> |
| 15 |
|
| 16 |
// The variables did not exist on older OS X releases, |
| 17 |
// we use the old variables deprecated on macOS to define them. |
| 18 |
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101200 |
| 19 |
enum |
| 20 |
{ |
| 21 |
NSEventTypeScrollWheel = NSScrollWheel, |
| 22 |
NSEventTypeKeyDown = NSKeyDown |
| 23 |
}; |
| 24 |
enum |
| 25 |
{ |
| 26 |
NSWindowStyleMaskTitled = NSTitledWindowMask, |
| 27 |
NSWindowStyleMaskResizable = NSResizableWindowMask, |
| 28 |
NSWindowStyleMaskMiniaturizable = NSMiniaturizableWindowMask, |
| 29 |
NSWindowStyleMaskClosable = NSClosableWindowMask |
| 30 |
}; |
| 31 |
#endif |
| 32 |
|
| 33 |
void makeCurrentContext(uintptr_t context) { |
| 34 |
NSOpenGLContext* ctx = (NSOpenGLContext*)context; |
| 35 |
[ctx makeCurrentContext]; |
| 36 |
} |
| 37 |
|
| 38 |
void flushContext(uintptr_t context) { |
| 39 |
NSOpenGLContext* ctx = (NSOpenGLContext*)context; |
| 40 |
[ctx flushBuffer]; |
| 41 |
} |
| 42 |
|
| 43 |
uint64 threadID() { |
| 44 |
uint64 id; |
| 45 |
if (pthread_threadid_np(pthread_self(), &id)) { |
| 46 |
abort(); |
| 47 |
} |
| 48 |
return id; |
| 49 |
} |
| 50 |
|
| 51 |
@interface ScreenGLView : NSOpenGLView<NSWindowDelegate> |
| 52 |
{ |
| 53 |
} |
| 54 |
@end |
| 55 |
|
| 56 |
@implementation ScreenGLView |
| 57 |
- (void)prepareOpenGL { |
| 58 |
[super prepareOpenGL]; |
| 59 |
|
| 60 |
[self setWantsBestResolutionOpenGLSurface:YES]; |
| 61 |
GLint swapInt = 1; |
| 62 |
NSOpenGLContext *ctx = [self openGLContext]; |
| 63 |
|
| 64 |
#pragma clang diagnostic push |
| 65 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 66 |
[ctx setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; |
| 67 |
#pragma clang diagnostic pop |
| 68 |
|
| 69 |
// Using attribute arrays in OpenGL 3.3 requires the use of a VBA. |
| 70 |
// But VBAs don't exist in ES 2. So we bind a default one. |
| 71 |
GLuint vba; |
| 72 |
glGenVertexArrays(1, &vba); |
| 73 |
glBindVertexArray(vba); |
| 74 |
|
| 75 |
preparedOpenGL((GoUintptr)self, (GoUintptr)ctx, (GoUintptr)vba); |
| 76 |
} |
| 77 |
|
| 78 |
- (void)callSetGeom { |
| 79 |
// Calculate screen PPI. |
| 80 |
// |
| 81 |
// Note that the backingScaleFactor converts from logical |
| 82 |
// pixels to actual pixels, but both of these units vary |
| 83 |
// independently from real world size. E.g. |
| 84 |
// |
| 85 |
// 13" Retina Macbook Pro, 2560x1600, 227ppi, backingScaleFactor=2, scale=3.15 |
| 86 |
// 15" Retina Macbook Pro, 2880x1800, 220ppi, backingScaleFactor=2, scale=3.06 |
| 87 |
// 27" iMac, 2560x1440, 109ppi, backingScaleFactor=1, scale=1.51 |
| 88 |
// 27" Retina iMac, 5120x2880, 218ppi, backingScaleFactor=2, scale=3.03 |
| 89 |
NSScreen *screen = self.window.screen; |
| 90 |
double screenPixW = [screen frame].size.width * [screen backingScaleFactor]; |
| 91 |
|
| 92 |
CGDirectDisplayID display = (CGDirectDisplayID)[[[screen deviceDescription] valueForKey:@"NSScreenNumber"] intValue]; |
| 93 |
CGSize screenSizeMM = CGDisplayScreenSize(display); // in millimeters |
| 94 |
float ppi = 25.4 * screenPixW / screenSizeMM.width; |
| 95 |
float pixelsPerPt = ppi/72.0; |
| 96 |
|
| 97 |
// The width and height reported to the geom package are the |
| 98 |
// bounds of the OpenGL view. Several steps are necessary. |
| 99 |
// First, [self bounds] gives us the number of logical pixels |
| 100 |
// in the view. Multiplying this by the backingScaleFactor |
| 101 |
// gives us the number of actual pixels. |
| 102 |
NSRect r = [self bounds]; |
| 103 |
int w = r.size.width * [screen backingScaleFactor]; |
| 104 |
int h = r.size.height * [screen backingScaleFactor]; |
| 105 |
|
| 106 |
setGeom((GoUintptr)self, pixelsPerPt, w, h); |
| 107 |
} |
| 108 |
|
| 109 |
- (void)reshape { |
| 110 |
[super reshape]; |
| 111 |
[self callSetGeom]; |
| 112 |
} |
| 113 |
|
| 114 |
- (void)drawRect:(NSRect)theRect { |
| 115 |
// Called during resize. Do an extra draw if we are visible. |
| 116 |
// This gets rid of flicker when resizing. |
| 117 |
drawgl((GoUintptr)self); |
| 118 |
} |
| 119 |
|
| 120 |
- (void)mouseEventNS:(NSEvent *)theEvent { |
| 121 |
NSPoint p = [theEvent locationInWindow]; |
| 122 |
double h = self.frame.size.height; |
| 123 |
|
| 124 |
// Both h and p are measured in Cocoa pixels, which are a fraction of |
| 125 |
// physical pixels, so we multiply by backingScaleFactor. |
| 126 |
double scale = [self.window.screen backingScaleFactor]; |
| 127 |
|
| 128 |
double x = p.x * scale; |
| 129 |
double y = (h - p.y) * scale - 1; // flip origin from bottom-left to top-left. |
| 130 |
|
| 131 |
double dx, dy; |
| 132 |
if (theEvent.type == NSEventTypeScrollWheel) { |
| 133 |
dx = theEvent.scrollingDeltaX; |
| 134 |
dy = theEvent.scrollingDeltaY; |
| 135 |
} |
| 136 |
|
| 137 |
mouseEvent((GoUintptr)self, x, y, dx, dy, theEvent.type, theEvent.buttonNumber, theEvent.modifierFlags); |
| 138 |
} |
| 139 |
|
| 140 |
- (void)mouseMoved:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 141 |
- (void)mouseDown:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 142 |
- (void)mouseUp:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 143 |
- (void)mouseDragged:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 144 |
- (void)rightMouseDown:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 145 |
- (void)rightMouseUp:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 146 |
- (void)rightMouseDragged:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 147 |
- (void)otherMouseDown:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 148 |
- (void)otherMouseUp:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 149 |
- (void)otherMouseDragged:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 150 |
- (void)scrollWheel:(NSEvent *)theEvent { [self mouseEventNS:theEvent]; } |
| 151 |
|
| 152 |
// raw modifier key presses |
| 153 |
- (void)flagsChanged:(NSEvent *)theEvent { |
| 154 |
flagEvent((GoUintptr)self, theEvent.modifierFlags); |
| 155 |
} |
| 156 |
|
| 157 |
// overrides special handling of escape and tab |
| 158 |
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent { |
| 159 |
[self key:theEvent]; |
| 160 |
return YES; |
| 161 |
} |
| 162 |
|
| 163 |
- (void)keyDown:(NSEvent *)theEvent { [self key:theEvent]; } |
| 164 |
- (void)keyUp:(NSEvent *)theEvent { [self key:theEvent]; } |
| 165 |
|
| 166 |
- (void)key:(NSEvent *)theEvent { |
| 167 |
NSRange range = [theEvent.characters rangeOfComposedCharacterSequenceAtIndex:0]; |
| 168 |
|
| 169 |
uint8_t buf[4] = {0, 0, 0, 0}; |
| 170 |
if (![theEvent.characters getBytes:buf |
| 171 |
maxLength:4 |
| 172 |
usedLength:nil |
| 173 |
encoding:NSUTF32LittleEndianStringEncoding |
| 174 |
options:NSStringEncodingConversionAllowLossy |
| 175 |
range:range |
| 176 |
remainingRange:nil]) { |
| 177 |
NSLog(@"failed to read key event %@", theEvent); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
uint32_t rune = (uint32_t)buf[0]<<0 | (uint32_t)buf[1]<<8 | (uint32_t)buf[2]<<16 | (uint32_t)buf[3]<<24; |
| 182 |
|
| 183 |
uint8_t direction; |
| 184 |
if ([theEvent isARepeat]) { |
| 185 |
direction = 0; |
| 186 |
} else if (theEvent.type == NSEventTypeKeyDown) { |
| 187 |
direction = 1; |
| 188 |
} else { |
| 189 |
direction = 2; |
| 190 |
} |
| 191 |
keyEvent((GoUintptr)self, (int32_t)rune, direction, theEvent.keyCode, theEvent.modifierFlags); |
| 192 |
} |
| 193 |
|
| 194 |
- (void)windowDidChangeScreenProfile:(NSNotification *)notification { |
| 195 |
[self callSetGeom]; |
| 196 |
} |
| 197 |
|
| 198 |
// TODO: catch windowDidMiniaturize? |
| 199 |
|
| 200 |
- (void)windowDidExpose:(NSNotification *)notification { |
| 201 |
lifecycleVisible((GoUintptr)self, true); |
| 202 |
} |
| 203 |
|
| 204 |
- (void)windowDidBecomeKey:(NSNotification *)notification { |
| 205 |
lifecycleFocused((GoUintptr)self, true); |
| 206 |
} |
| 207 |
|
| 208 |
- (void)windowDidResignKey:(NSNotification *)notification { |
| 209 |
lifecycleFocused((GoUintptr)self, false); |
| 210 |
if ([NSApp isHidden]) { |
| 211 |
lifecycleVisible((GoUintptr)self, false); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
- (void)windowWillClose:(NSNotification *)notification { |
| 216 |
// TODO: is this right? Closing a window via the top-left red button |
| 217 |
// seems to return early without ever calling windowClosing. |
| 218 |
if (self.window.nextResponder == NULL) { |
| 219 |
return; // already called close |
| 220 |
} |
| 221 |
|
| 222 |
windowClosing((GoUintptr)self); |
| 223 |
[self.window.nextResponder release]; |
| 224 |
self.window.nextResponder = NULL; |
| 225 |
} |
| 226 |
@end |
| 227 |
|
| 228 |
@interface AppDelegate : NSObject<NSApplicationDelegate> |
| 229 |
{ |
| 230 |
} |
| 231 |
@end |
| 232 |
|
| 233 |
@implementation AppDelegate |
| 234 |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
| 235 |
driverStarted(); |
| 236 |
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateAllWindows]; |
| 237 |
} |
| 238 |
|
| 239 |
- (void)applicationWillTerminate:(NSNotification *)aNotification { |
| 240 |
lifecycleDeadAll(); |
| 241 |
} |
| 242 |
|
| 243 |
- (void)applicationWillHide:(NSNotification *)aNotification { |
| 244 |
lifecycleHideAll(); |
| 245 |
} |
| 246 |
@end |
| 247 |
|
| 248 |
uintptr_t doNewWindow(int width, int height, char* title) { |
| 249 |
NSScreen *screen = [NSScreen mainScreen]; |
| 250 |
double w = (double)width / [screen backingScaleFactor]; |
| 251 |
double h = (double)height / [screen backingScaleFactor]; |
| 252 |
__block ScreenGLView* view = NULL; |
| 253 |
|
| 254 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 255 |
id menuBar = [NSMenu new]; |
| 256 |
id menuItem = [NSMenuItem new]; |
| 257 |
[menuBar addItem:menuItem]; |
| 258 |
[NSApp setMainMenu:menuBar]; |
| 259 |
|
| 260 |
id menu = [NSMenu new]; |
| 261 |
NSString* name = [[NSString alloc] initWithUTF8String:title]; |
| 262 |
|
| 263 |
id hideMenuItem = [[NSMenuItem alloc] initWithTitle:@"Hide" |
| 264 |
action:@selector(hide:) keyEquivalent:@"h"]; |
| 265 |
[menu addItem:hideMenuItem]; |
| 266 |
|
| 267 |
id quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" |
| 268 |
action:@selector(terminate:) keyEquivalent:@"q"]; |
| 269 |
[menu addItem:quitMenuItem]; |
| 270 |
[menuItem setSubmenu:menu]; |
| 271 |
|
| 272 |
NSRect rect = NSMakeRect(0, 0, w, h); |
| 273 |
|
| 274 |
NSWindow* window = [[NSWindow alloc] initWithContentRect:rect |
| 275 |
styleMask:NSWindowStyleMaskTitled |
| 276 |
backing:NSBackingStoreBuffered |
| 277 |
defer:NO]; |
| 278 |
window.styleMask |= NSWindowStyleMaskResizable; |
| 279 |
window.styleMask |= NSWindowStyleMaskMiniaturizable; |
| 280 |
window.styleMask |= NSWindowStyleMaskClosable; |
| 281 |
window.title = name; |
| 282 |
window.displaysWhenScreenProfileChanges = YES; |
| 283 |
[window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; |
| 284 |
[window setAcceptsMouseMovedEvents:YES]; |
| 285 |
|
| 286 |
NSOpenGLPixelFormatAttribute attr[] = { |
| 287 |
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core, |
| 288 |
NSOpenGLPFAColorSize, 24, |
| 289 |
NSOpenGLPFAAlphaSize, 8, |
| 290 |
NSOpenGLPFADepthSize, 16, |
| 291 |
NSOpenGLPFADoubleBuffer, |
| 292 |
NSOpenGLPFAAllowOfflineRenderers, |
| 293 |
0 |
| 294 |
}; |
| 295 |
id pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr]; |
| 296 |
view = [[ScreenGLView alloc] initWithFrame:rect pixelFormat:pixFormat]; |
| 297 |
[window setContentView:view]; |
| 298 |
[window setDelegate:view]; |
| 299 |
[window makeFirstResponder:view]; |
| 300 |
}); |
| 301 |
|
| 302 |
return (uintptr_t)view; |
| 303 |
} |
| 304 |
|
| 305 |
void doShowWindow(uintptr_t viewID) { |
| 306 |
ScreenGLView* view = (ScreenGLView*)viewID; |
| 307 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 308 |
[view.window makeKeyAndOrderFront:view.window]; |
| 309 |
}); |
| 310 |
} |
| 311 |
|
| 312 |
void doCloseWindow(uintptr_t viewID) { |
| 313 |
ScreenGLView* view = (ScreenGLView*)viewID; |
| 314 |
dispatch_sync(dispatch_get_main_queue(), ^{ |
| 315 |
[view.window performClose:view]; |
| 316 |
}); |
| 317 |
} |
| 318 |
|
| 319 |
void startDriver() { |
| 320 |
[NSAutoreleasePool new]; |
| 321 |
[NSApplication sharedApplication]; |
| 322 |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; |
| 323 |
AppDelegate* delegate = [[AppDelegate alloc] init]; |
| 324 |
[NSApp setDelegate:delegate]; |
| 325 |
[NSApp run]; |
| 326 |
} |
| 327 |
|
| 328 |
void stopDriver() { |
| 329 |
dispatch_async(dispatch_get_main_queue(), ^{ |
| 330 |
[NSApp terminate:nil]; |
| 331 |
}); |
| 332 |
} |