Debian Codegraph @

app/darwin_ios.m

raw
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 //go:build darwin && ios
6 // +build darwin
7 // +build ios
8
9 #include "_cgo_export.h"
10 #include <pthread.h>
11 #include <stdio.h>
12 #include <sys/utsname.h>
13
14 #import <UIKit/UIKit.h>
15 #import <GLKit/GLKit.h>
16
17 struct utsname sysInfo;
18
19 @interface GoAppAppController : GLKViewController<UIContentContainer, GLKViewDelegate>
20 @end
21
22 @interface GoAppAppDelegate : UIResponder<UIApplicationDelegate>
23 @property (strong, nonatomic) UIWindow *window;
24 @property (strong, nonatomic) GoAppAppController *controller;
25 @end
26
27 @implementation GoAppAppDelegate
28 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
29 lifecycleAlive();
30 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
31 self.controller = [[GoAppAppController alloc] initWithNibName:nil bundle:nil];
32 self.window.rootViewController = self.controller;
33 [self.window makeKeyAndVisible];
34 return YES;
35 }
36
37 - (void)applicationDidBecomeActive:(UIApplication * )application {
38 lifecycleFocused();
39 }
40
41 - (void)applicationWillResignActive:(UIApplication *)application {
42 lifecycleVisible();
43 }
44
45 - (void)applicationDidEnterBackground:(UIApplication *)application {
46 lifecycleAlive();
47 }
48
49 - (void)applicationWillTerminate:(UIApplication *)application {
50 lifecycleDead();
51 }
52 @end
53
54 @interface GoAppAppController ()
55 @property (strong, nonatomic) EAGLContext *context;
56 @property (strong, nonatomic) GLKView *glview;
57 @end
58
59 @implementation GoAppAppController
60 - (void)viewWillAppear:(BOOL)animated
61 {
62 // TODO: replace by swapping out GLKViewController for a UIVIewController.
63 [super viewWillAppear:animated];
64 self.paused = YES;
65 }
66
67 - (void)viewDidLoad {
68 [super viewDidLoad];
69 self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
70 self.glview = (GLKView*)self.view;
71 self.glview.drawableDepthFormat = GLKViewDrawableDepthFormat24;
72 self.glview.multipleTouchEnabled = true; // TODO expose setting to user.
73 self.glview.context = self.context;
74 self.glview.userInteractionEnabled = YES;
75 self.glview.enableSetNeedsDisplay = YES; // only invoked once
76
77 // Do not use the GLKViewController draw loop.
78 self.paused = YES;
79 self.resumeOnDidBecomeActive = NO;
80 self.preferredFramesPerSecond = 0;
81
82 int scale = 1;
83 if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
84 scale = (int)[UIScreen mainScreen].scale; // either 1.0, 2.0, or 3.0.
85 }
86 setScreen(scale);
87
88 CGSize size = [UIScreen mainScreen].bounds.size;
89 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
90 updateConfig((int)size.width, (int)size.height, orientation);
91 }
92
93 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
94 [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
95 // TODO(crawshaw): come up with a plan to handle animations.
96 } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
97 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
98 updateConfig((int)size.width, (int)size.height, orientation);
99 }];
100 }
101
102 - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
103 // Now that we have been asked to do the first draw, disable any
104 // future draw and hand control over to the Go paint.Event cycle.
105 self.glview.enableSetNeedsDisplay = NO;
106 startloop((GLintptr)self.context);
107 }
108
109 #define TOUCH_TYPE_BEGIN 0 // touch.TypeBegin
110 #define TOUCH_TYPE_MOVE 1 // touch.TypeMove
111 #define TOUCH_TYPE_END 2 // touch.TypeEnd
112
113 static void sendTouches(int change, NSSet* touches) {
114 CGFloat scale = [UIScreen mainScreen].scale;
115 for (UITouch* touch in touches) {
116 CGPoint p = [touch locationInView:touch.view];
117 sendTouch((GoUintptr)touch, (GoUintptr)change, p.x*scale, p.y*scale);
118 }
119 }
120
121 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
122 sendTouches(TOUCH_TYPE_BEGIN, touches);
123 }
124
125 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
126 sendTouches(TOUCH_TYPE_MOVE, touches);
127 }
128
129 - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
130 sendTouches(TOUCH_TYPE_END, touches);
131 }
132
133 - (void)touchesCanceled:(NSSet*)touches withEvent:(UIEvent*)event {
134 sendTouches(TOUCH_TYPE_END, touches);
135 }
136 @end
137
138 void runApp(void) {
139 char* argv[] = {};
140 @autoreleasepool {
141 UIApplicationMain(0, argv, nil, NSStringFromClass([GoAppAppDelegate class]));
142 }
143 }
144
145 void makeCurrentContext(GLintptr context) {
146 EAGLContext* ctx = (EAGLContext*)context;
147 if (![EAGLContext setCurrentContext:ctx]) {
148 // TODO(crawshaw): determine how terrible this is. Exit?
149 NSLog(@"failed to set current context");
150 }
151 }
152
153 void swapBuffers(GLintptr context) {
154 __block EAGLContext* ctx = (EAGLContext*)context;
155 dispatch_sync(dispatch_get_main_queue(), ^{
156 [EAGLContext setCurrentContext:ctx];
157 [ctx presentRenderbuffer:GL_RENDERBUFFER];
158 });
159 }
160
161 uint64_t threadID() {
162 uint64_t id;
163 if (pthread_threadid_np(pthread_self(), &id)) {
164 abort();
165 }
166 return id;
167 }