VMDisplayTerminalViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright © 2020 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 "VMDisplayTerminalViewController.h"
  17. #import "VMDisplayTerminalViewController+Keyboard.h"
  18. #import "UTMConfiguration.h"
  19. #import "UTMConfiguration+Display.h"
  20. #import "UTMLogging.h"
  21. #import "UTMVirtualMachine.h"
  22. #import "UTMVirtualMachine+Terminal.h"
  23. #import "UIViewController+Extensions.h"
  24. #import "WKWebView+Workarounds.h"
  25. NSString *const kVMDefaultResizeCmd = @"stty cols $COLS rows $ROWS\\n";
  26. NSString *const kVMSendInputHandler = @"UTMSendInput";
  27. NSString* const kVMDebugHandler = @"UTMDebug";
  28. NSString* const kVMSendGestureHandler = @"UTMSendGesture";
  29. NSString* const kVMSendTerminalSizeHandler = @"UTMSendTerminalSize";
  30. @interface VMDisplayTerminalViewController ()
  31. @end
  32. @implementation VMDisplayTerminalViewController {
  33. // gestures
  34. UISwipeGestureRecognizer *_swipeUp;
  35. UISwipeGestureRecognizer *_swipeDown;
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // webview setup
  40. UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Disable this bar in Settings -> General -> Keyboards -> Shortcuts", @"VMDisplayTerminalViewController")
  41. style:UIBarButtonItemStylePlain
  42. target:nil
  43. action:nil];
  44. UIBarButtonItemGroup *group = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:@[ item ]
  45. representativeItem:nil];
  46. _webView.inputAssistantItem.leadingBarButtonGroups = @[ group ];
  47. _webView.inputAssistantItem.trailingBarButtonGroups = @[];
  48. [_webView setCustomInputAccessoryView: self.inputAccessoryView];
  49. [[[_webView configuration] userContentController] addScriptMessageHandler: self name: kVMSendInputHandler];
  50. [[[_webView configuration] userContentController] addScriptMessageHandler: self name: kVMDebugHandler];
  51. [[[_webView configuration] userContentController] addScriptMessageHandler: self name: kVMSendGestureHandler];
  52. [[[_webView configuration] userContentController] addScriptMessageHandler: self name: kVMSendTerminalSizeHandler];
  53. // load terminal.html
  54. NSURL* resourceURL = [[NSBundle mainBundle] resourceURL];
  55. NSURL* indexFile = [resourceURL URLByAppendingPathComponent: @"terminal.html"];
  56. [_webView loadFileURL: indexFile allowingReadAccessToURL: resourceURL];
  57. _webView.navigationDelegate = self;
  58. }
  59. - (void)viewWillAppear:(BOOL)animated {
  60. [super viewWillAppear: animated];
  61. if (self.vm.state == kVMStopped || self.vm.state == kVMSuspended) {
  62. if ([self.vm startVM]) {
  63. self.vm.ioDelegate = self;
  64. }
  65. }
  66. }
  67. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
  68. [self updateSettings];
  69. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  70. // hack to make sure keyboard is shown
  71. self.keyboardVisible = self.keyboardVisible;
  72. });
  73. }
  74. - (void)updateSettings {
  75. [_webView evaluateJavaScript:[NSString stringWithFormat:@"changeFont('%@', %ld);", self.vmConfiguration.consoleFont, self.vmConfiguration.consoleFontSize.integerValue] completionHandler:^(id _Nullable _, NSError * _Nullable error) {
  76. UTMLog(@"changeFont error: %@", error);
  77. }];
  78. [_webView evaluateJavaScript:[NSString stringWithFormat:@"setCursorBlink(%@);", self.vmConfiguration.consoleCursorBlink ? @"true" : @"false"] completionHandler:^(id _Nullable _, NSError * _Nullable error) {
  79. UTMLog(@"setCursorBlink error: %@", error);
  80. }];
  81. }
  82. #pragma mark - Keyboard
  83. - (void)setKeyboardVisible:(BOOL)keyboardVisible {
  84. if (keyboardVisible) {
  85. [self showKeyboard];
  86. } else {
  87. [self hideKeyboard];
  88. }
  89. [super setKeyboardVisible:keyboardVisible];
  90. }
  91. - (void)hideKeyboard {
  92. [_webView endEditing:YES];
  93. }
  94. - (void)showKeyboard {
  95. [_webView toggleKeyboardDisplayRequiresUserAction:NO];
  96. NSString* jsString = @"focusTerminal()";
  97. [_webView evaluateJavaScript: jsString completionHandler:^(id _Nullable _, NSError * _Nullable error) {
  98. if (error != nil) {
  99. UTMLog(@"Error while focusing terminal element: %@", error);
  100. }
  101. [self->_webView toggleKeyboardDisplayRequiresUserAction:YES];
  102. }];
  103. }
  104. #pragma mark - Resize console
  105. - (void)changeDisplayZoom:(UIButton *)sender {
  106. NSString *cmd = self.vmConfiguration.consoleResizeCommand;
  107. if (cmd.length == 0) {
  108. cmd = kVMDefaultResizeCmd;
  109. }
  110. cmd = [cmd stringByReplacingOccurrencesOfString:@"$COLS" withString:[self.columns stringValue]];
  111. cmd = [cmd stringByReplacingOccurrencesOfString:@"$ROWS" withString:[self.rows stringValue]];
  112. cmd = [cmd stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
  113. [self.vm sendInput:cmd];
  114. }
  115. #pragma mark - Gestures
  116. - (void)handleGestureFromJs:(NSString *)gesture {
  117. if ([gesture isEqualToString:@"threeSwipeUp"]) {
  118. if (self.toolbarVisible) {
  119. self.toolbarVisible = NO;
  120. } else if (!self.keyboardVisible) {
  121. self.keyboardVisible = YES;
  122. }
  123. } else if ([gesture isEqualToString:@"threeSwipeDown"]) {
  124. if (self.keyboardVisible) {
  125. self.keyboardVisible = NO;
  126. } else if (!self.toolbarVisible) {
  127. self.toolbarVisible = YES;
  128. }
  129. }
  130. }
  131. #pragma mark - WKScriptMessageHandler
  132. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  133. if ([[message name] isEqualToString: kVMSendInputHandler]) {
  134. UTMLog(@"Received input from HTerm: %@", (NSString*) message.body);
  135. [self.vm sendInput: (NSString*) message.body];
  136. [self resetModifierToggles];
  137. } else if ([[message name] isEqualToString: kVMDebugHandler]) {
  138. UTMLog(@"Debug message from HTerm: %@", (NSString*) message.body);
  139. } else if ([[message name] isEqualToString: kVMSendGestureHandler]) {
  140. UTMLog(@"Gesture message from HTerm: %@", (NSString*) message.body);
  141. [self handleGestureFromJs:message.body];
  142. } else if ([[message name] isEqualToString: kVMSendTerminalSizeHandler]) {
  143. UTMLog(@"Terminal resize: %@", message.body);
  144. self.columns = message.body[0];
  145. self.rows = message.body[1];
  146. }
  147. }
  148. #pragma mark - UTMTerminalDelegate
  149. - (void)terminal:(UTMTerminal *)terminal didReceiveData:(NSData *)data {
  150. NSMutableString* dataString = [NSMutableString stringWithString: @"["];
  151. const uint8_t* buf = (uint8_t*) [data bytes];
  152. for (size_t i = 0; i < [data length]; i++) {
  153. [dataString appendFormat: @"%u,", buf[i]];
  154. }
  155. [dataString appendString:@"]"];
  156. //UTMLog(@"Array: %@", dataString);
  157. NSString* jsString = [NSString stringWithFormat: @"writeData(new Uint8Array(%@));", dataString];
  158. dispatch_async(dispatch_get_main_queue(), ^{
  159. [_webView evaluateJavaScript: jsString completionHandler:^(id _Nullable _, NSError * _Nullable error) {
  160. if (error != nil) {
  161. UTMLog(@"JS evaluation failed: %@", [error localizedDescription]);
  162. }
  163. }];
  164. });
  165. }
  166. #pragma mark - UTMVirtualMachineDelegate
  167. - (void)virtualMachine:(UTMVirtualMachine *)vm transitionToState:(UTMVMState)state {
  168. [super virtualMachine:vm transitionToState:state];
  169. switch (state) {
  170. case kVMPausing:
  171. case kVMStopping:
  172. case kVMStarting:
  173. case kVMResuming: {
  174. self.webView.userInteractionEnabled = NO;
  175. break;
  176. }
  177. case kVMStarted: {
  178. self.webView.userInteractionEnabled = YES;
  179. break;
  180. }
  181. default: {
  182. break;
  183. }
  184. }
  185. }
  186. @end