github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/mobile/app/darwin_armx.m (about) 1 // Copyright 2015 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 arm arm64 7 8 #include "_cgo_export.h" 9 #include <pthread.h> 10 #include <stdio.h> 11 #include <sys/utsname.h> 12 13 #import <UIKit/UIKit.h> 14 #import <GLKit/GLKit.h> 15 16 struct utsname sysInfo; 17 18 @interface GoAppAppController : GLKViewController<UIContentContainer> 19 @end 20 21 @interface GoAppAppDelegate : UIResponder<UIApplicationDelegate> 22 @property (strong, nonatomic) UIWindow *window; 23 @property (strong, nonatomic) GoAppAppController *controller; 24 @end 25 26 @implementation GoAppAppDelegate 27 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 28 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 29 self.controller = [[GoAppAppController alloc] initWithNibName:nil bundle:nil]; 30 self.window.rootViewController = self.controller; 31 [self.window makeKeyAndVisible]; 32 return YES; 33 } 34 @end 35 36 @interface GoAppAppController () 37 @property (strong, nonatomic) EAGLContext *context; 38 @end 39 40 @implementation GoAppAppController 41 - (void)viewDidLoad { 42 [super viewDidLoad]; 43 self.preferredFramesPerSecond = 60; 44 self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; 45 GLKView *view = (GLKView *)self.view; 46 view.context = self.context; 47 view.drawableDepthFormat = GLKViewDrawableDepthFormat24; 48 view.multipleTouchEnabled = true; // TODO expose setting to user. 49 50 int scale = 1; 51 if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) { 52 scale = (int)[UIScreen mainScreen].scale; // either 1.0, 2.0, or 3.0. 53 } 54 setScreen(scale); 55 56 CGSize size = [UIScreen mainScreen].bounds.size; 57 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 58 updateConfig((int)size.width, (int)size.height, orientation); 59 } 60 61 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 62 [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { 63 // TODO(crawshaw): come up with a plan to handle animations. 64 } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { 65 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 66 updateConfig((int)size.width, (int)size.height, orientation); 67 }]; 68 } 69 70 - (void)update { 71 drawgl((GoUintptr)self.context); 72 } 73 74 #define TOUCH_TYPE_BEGIN 0 // touch.TypeBegin 75 #define TOUCH_TYPE_MOVE 1 // touch.TypeMove 76 #define TOUCH_TYPE_END 2 // touch.TypeEnd 77 78 static void sendTouches(int change, NSSet* touches) { 79 CGFloat scale = [UIScreen mainScreen].scale; 80 for (UITouch* touch in touches) { 81 CGPoint p = [touch locationInView:touch.view]; 82 sendTouch((GoUintptr)touch, (GoUintptr)change, p.x*scale, p.y*scale); 83 } 84 } 85 86 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 87 sendTouches(TOUCH_TYPE_BEGIN, touches); 88 } 89 90 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 91 sendTouches(TOUCH_TYPE_MOVE, touches); 92 } 93 94 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { 95 sendTouches(TOUCH_TYPE_END, touches); 96 } 97 @end 98 99 void runApp(void) { 100 @autoreleasepool { 101 UIApplicationMain(0, nil, nil, NSStringFromClass([GoAppAppDelegate class])); 102 } 103 } 104 105 void setContext(void* context) { 106 EAGLContext* ctx = (EAGLContext*)context; 107 if (![EAGLContext setCurrentContext:ctx]) { 108 // TODO(crawshaw): determine how terrible this is. Exit? 109 NSLog(@"failed to set current context"); 110 } 111 } 112 113 uint64_t threadID() { 114 uint64_t id; 115 if (pthread_threadid_np(pthread_self(), &id)) { 116 abort(); 117 } 118 return id; 119 }