2
0

VMDisplayMetalViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Copyright © 2019 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #import "VMDisplayMetalViewController.h"
  17. #import "VMDisplayMetalViewController+Private.h"
  18. #import "VMDisplayMetalViewController+Keyboard.h"
  19. #import "VMDisplayMetalViewController+Touch.h"
  20. #import "VMDisplayMetalViewController+Pointer.h"
  21. #if !defined(TARGET_OS_VISION) || !TARGET_OS_VISION
  22. #import "VMDisplayMetalViewController+Pencil.h"
  23. #endif
  24. #import "VMDisplayMetalViewController+Gamepad.h"
  25. #import "VMKeyboardView.h"
  26. #import "UTMLogging.h"
  27. #import "CSDisplay.h"
  28. #import "UTM-Swift.h"
  29. @import CocoaSpiceRenderer;
  30. static const NSInteger kResizeDebounceSecs = 1;
  31. static const NSInteger kResizeTimeoutSecs = 5;
  32. @interface VMDisplayMetalViewController ()
  33. @property (nonatomic, nullable) CSMetalRenderer *renderer;
  34. @property (nonatomic, nullable) id debounceResize;
  35. @property (nonatomic, nullable) id cancelResize;
  36. @property (nonatomic) BOOL ignoreNextResize;
  37. @end
  38. @implementation VMDisplayMetalViewController
  39. - (instancetype)initWithDisplay:(CSDisplay *)display input:(CSInput *)input {
  40. if (self = [super initWithNibName:nil bundle:nil]) {
  41. self.vmDisplay = display;
  42. self.vmInput = input;
  43. }
  44. return self;
  45. }
  46. - (void)loadView {
  47. [super loadView];
  48. self.keyboardView = [[VMKeyboardView alloc] initWithFrame:CGRectZero];
  49. self.mtkView = [[MTKView alloc] initWithFrame:CGRectZero];
  50. self.keyboardView.delegate = self;
  51. [self.view insertSubview:self.keyboardView atIndex:0];
  52. [self.view insertSubview:self.mtkView atIndex:1];
  53. [self.mtkView bindFrameToSuperviewBounds];
  54. [self loadInputAccessory];
  55. }
  56. - (void)loadInputAccessory {
  57. UINib *nib = [UINib nibWithNibName:@"VMDisplayMetalViewInputAccessory" bundle:nil];
  58. [nib instantiateWithOwner:self options:nil];
  59. }
  60. - (BOOL)serverModeCursor {
  61. return self.vmInput.serverModeCursor;
  62. }
  63. - (void)viewDidLoad {
  64. [super viewDidLoad];
  65. // set up software keyboard
  66. self.keyboardView.inputAccessoryView = self.inputAccessoryView;
  67. // Set the view to use the default device
  68. self.mtkView.frame = self.view.bounds;
  69. self.mtkView.device = MTLCreateSystemDefaultDevice();
  70. if (!self.mtkView.device) {
  71. UTMLog(@"Metal is not supported on this device");
  72. return;
  73. }
  74. self.renderer = [[CSMetalRenderer alloc] initWithMetalKitView:self.mtkView];
  75. if (!self.renderer) {
  76. UTMLog(@"Renderer failed initialization");
  77. return;
  78. }
  79. // Initialize our renderer with the view size
  80. if ([self integerForSetting:@"QEMURendererFPSLimit"] > 0) {
  81. self.mtkView.preferredFramesPerSecond = [self integerForSetting:@"QEMURendererFPSLimit"];
  82. }
  83. [self.renderer changeUpscaler:self.delegate.qemuDisplayUpscaler
  84. downscaler:self.delegate.qemuDisplayDownscaler];
  85. self.mtkView.delegate = self.renderer;
  86. [self initTouch];
  87. [self initGamepad];
  88. // Pointing device support on iPadOS 13.4 GM or later
  89. if (@available(iOS 13.4, *)) {
  90. // Betas of iPadOS 13.4 did not include this API, that's why I check if the class exists
  91. if (NSClassFromString(@"UIPointerInteraction") != nil) {
  92. [self initPointerInteraction];
  93. }
  94. }
  95. #if !defined(TARGET_OS_VISION) || !TARGET_OS_VISION
  96. // Apple Pencil 2 double tap support on iOS 12.1+
  97. if (@available(iOS 12.1, *)) {
  98. [self initPencilInteraction];
  99. }
  100. #endif
  101. }
  102. - (void)viewWillAppear:(BOOL)animated {
  103. [super viewWillAppear:animated];
  104. self.prefersHomeIndicatorAutoHidden = YES;
  105. #if !TARGET_OS_VISION
  106. [self startGCMouse];
  107. #endif
  108. [self.vmDisplay addRenderer:self.renderer];
  109. }
  110. - (void)viewWillDisappear:(BOOL)animated {
  111. [super viewWillDisappear:animated];
  112. #if !TARGET_OS_VISION
  113. [self stopGCMouse];
  114. #endif
  115. [self.vmDisplay removeRenderer:self.renderer];
  116. [self removeObserver:self forKeyPath:@"vmDisplay.displaySize"];
  117. }
  118. - (void)viewDidAppear:(BOOL)animated {
  119. [super viewDidAppear:animated];
  120. self.delegate.displayViewSize = [self convertSizeToNative:self.view.bounds.size];
  121. [self addObserver:self forKeyPath:@"vmDisplay.displaySize" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial) context:nil];
  122. }
  123. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  124. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  125. [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  126. self.delegate.displayViewSize = [self convertSizeToNative:size];
  127. [self.delegate display:self.vmDisplay didResizeTo:self.vmDisplay.displaySize];
  128. if (self.delegate.qemuDisplayIsDynamicResolution && self.isDynamicResolutionSupported) {
  129. if (!CGSizeEqualToSize(size, self.vmDisplay.displaySize)) {
  130. [self requestResolutionChangeToSize:size];
  131. }
  132. }
  133. }];
  134. }
  135. - (void)enterSuspendedWithIsBusy:(BOOL)busy {
  136. [super enterSuspendedWithIsBusy:busy];
  137. self.prefersPointerLocked = NO;
  138. self.view.window.isIndirectPointerTouchIgnored = NO;
  139. if (!busy) {
  140. if (self.delegate.qemuHasClipboardSharing) {
  141. [[UTMPasteboard generalPasteboard] releasePollingModeForObject:self];
  142. }
  143. }
  144. }
  145. - (void)enterLive {
  146. [super enterLive];
  147. self.prefersPointerLocked = YES;
  148. self.view.window.isIndirectPointerTouchIgnored = YES;
  149. if (self.delegate.qemuDisplayIsDynamicResolution && self.isDynamicResolutionSupported) {
  150. [self requestResolutionChangeToSize:self.view.bounds.size];
  151. }
  152. if (self.delegate.qemuHasClipboardSharing) {
  153. [[UTMPasteboard generalPasteboard] requestPollingModeForObject:self];
  154. }
  155. }
  156. #pragma mark - Key handling
  157. - (void)showKeyboard {
  158. [super showKeyboard];
  159. [self.keyboardView becomeFirstResponder];
  160. }
  161. - (void)hideKeyboard {
  162. [super hideKeyboard];
  163. [self.keyboardView resignFirstResponder];
  164. }
  165. - (void)sendExtendedKey:(CSInputKey)type code:(int)code {
  166. if ((code & 0xFF00) == 0xE000) {
  167. code = 0x100 | (code & 0xFF);
  168. } else if (code >= 0x100) {
  169. UTMLog(@"warning: ignored invalid keycode 0x%x", code);
  170. }
  171. [self.vmInput sendKey:type code:code];
  172. }
  173. #pragma mark - Resizing
  174. - (CGSize)convertSizeToNative:(CGSize)size {
  175. if (self.delegate.qemuDisplayIsNativeResolution) {
  176. size.width = CGPointToPixel(size.width);
  177. size.height = CGPointToPixel(size.height);
  178. }
  179. return size;
  180. }
  181. - (void)requestResolutionChangeToSize:(CGSize)size {
  182. self.debounceResize = [self debounce:kResizeDebounceSecs context:self.debounceResize action:^{
  183. UTMLog(@"DISPLAY: requesting resolution (%f, %f)", size.width, size.height);
  184. CGSize newSize = [self convertSizeToNative:size];
  185. CGRect bounds = CGRectMake(0, 0, newSize.width, newSize.height);
  186. self.debounceResize = nil;
  187. #if defined(TARGET_OS_VISION) && TARGET_OS_VISION
  188. self.cancelResize = [self debounce:kResizeTimeoutSecs context:self.cancelResize action:^{
  189. self.cancelResize = nil;
  190. UTMLog(@"DISPLAY: requesting resolution cancelled");
  191. [self resizeWindowToDisplaySize];
  192. }];
  193. #endif
  194. [self.vmDisplay requestResolution:bounds];
  195. }];
  196. }
  197. - (void)setVmDisplay:(CSDisplay *)display {
  198. if (self.renderer) {
  199. [_vmDisplay removeRenderer:self.renderer];
  200. _vmDisplay = display;
  201. [display addRenderer:self.renderer];
  202. }
  203. }
  204. - (void)setDisplayScaling:(CGFloat)scaling origin:(CGPoint)origin {
  205. self.vmDisplay.viewportOrigin = origin;
  206. if (!self.delegate.qemuDisplayIsNativeResolution) {
  207. scaling = CGPointToPixel(scaling);
  208. }
  209. if (scaling) { // cannot be zero
  210. self.vmDisplay.viewportScale = scaling;
  211. }
  212. }
  213. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  214. if ([keyPath isEqualToString:@"vmDisplay.displaySize"]) {
  215. UTMLog(@"DISPLAY: vmDisplay.displaySize changed");
  216. if (self.cancelResize) {
  217. [self debounce:0 context:self.cancelResize action:^{}];
  218. self.cancelResize = nil;
  219. }
  220. self.debounceResize = [self debounce:kResizeDebounceSecs context:self.debounceResize action:^{
  221. [self resizeWindowToDisplaySize];
  222. }];
  223. }
  224. }
  225. - (void)setIsDynamicResolutionSupported:(BOOL)isDynamicResolutionSupported {
  226. if (_isDynamicResolutionSupported != isDynamicResolutionSupported) {
  227. _isDynamicResolutionSupported = isDynamicResolutionSupported;
  228. UTMLog(@"DISPLAY: isDynamicResolutionSupported = %d", isDynamicResolutionSupported);
  229. if (self.delegate.qemuDisplayIsDynamicResolution) {
  230. if (isDynamicResolutionSupported) {
  231. [self requestResolutionChangeToSize:self.view.bounds.size];
  232. } else {
  233. [self resizeWindowToDisplaySize];
  234. }
  235. }
  236. }
  237. }
  238. - (void)resizeWindowToDisplaySize {
  239. CGSize displaySize = self.vmDisplay.displaySize;
  240. UTMLog(@"DISPLAY: request window resize to (%f, %f)", displaySize.width, displaySize.height);
  241. #if defined(TARGET_OS_VISION) && TARGET_OS_VISION
  242. CGSize minSize = displaySize;
  243. if (self.delegate.qemuDisplayIsNativeResolution) {
  244. minSize.width = CGPixelToPoint(minSize.width);
  245. minSize.height = CGPixelToPoint(minSize.height);
  246. }
  247. CGSize maxSize = CGSizeMake(UIProposedSceneSizeNoPreference, UIProposedSceneSizeNoPreference);
  248. UIWindowSceneGeometryPreferencesVision *geoPref = [[UIWindowSceneGeometryPreferencesVision alloc] initWithSize:minSize];
  249. if (self.delegate.qemuDisplayIsDynamicResolution && self.isDynamicResolutionSupported) {
  250. geoPref.minimumSize = CGSizeMake(800, 600);
  251. geoPref.maximumSize = maxSize;
  252. geoPref.resizingRestrictions = UIWindowSceneResizingRestrictionsFreeform;
  253. } else {
  254. geoPref.minimumSize = minSize;
  255. geoPref.maximumSize = maxSize;
  256. geoPref.resizingRestrictions = UIWindowSceneResizingRestrictionsUniform;
  257. }
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. CGSize currentViewSize = self.view.bounds.size;
  260. UTMLog(@"DISPLAY: old view size = (%f, %f)", currentViewSize.width, currentViewSize.height);
  261. if (CGSizeEqualToSize(minSize, currentViewSize)) {
  262. // since `-viewWillTransitionToSize:withTransitionCoordinator:` is not called
  263. self.delegate.displayViewSize = [self convertSizeToNative:currentViewSize];
  264. [self.delegate display:self.vmDisplay didResizeTo:displaySize];
  265. }
  266. [self.view.window.windowScene requestGeometryUpdateWithPreferences:geoPref errorHandler:nil];
  267. });
  268. #else
  269. if (CGSizeEqualToSize(displaySize, CGSizeZero)) {
  270. return;
  271. }
  272. [self.delegate display:self.vmDisplay didResizeTo:displaySize];
  273. #endif
  274. }
  275. @end