darwin_ios.m 5.0 KB

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