VMDisplayMetalViewController+Keyboard.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "VMDisplayMetalViewController+Keyboard.h"
  17. #import "VMDisplayMetalViewController+Private.h"
  18. #import "UTMLogging.h"
  19. #import "VMKeyboardView.h"
  20. #import "VMKeyboardButton.h"
  21. #import "UTM-Swift.h"
  22. @implementation VMDisplayMetalViewController (Keyboard)
  23. #pragma mark - Software Keyboard
  24. - (BOOL)inputViewIsFirstResponder {
  25. return self.keyboardView.isFirstResponder;
  26. }
  27. - (void)keyboardView:(nonnull VMKeyboardView *)keyboardView didPressKeyDown:(int)scancode {
  28. [self sendExtendedKey:kCSInputKeyPress code:scancode];
  29. }
  30. - (void)keyboardView:(nonnull VMKeyboardView *)keyboardView didPressKeyUp:(int)scancode {
  31. [self sendExtendedKey:kCSInputKeyRelease code:scancode];
  32. [self resetModifierToggles];
  33. }
  34. - (IBAction)keyboardDonePressed:(UIButton *)sender {
  35. [self.keyboardView resignFirstResponder];
  36. }
  37. - (IBAction)keyboardPastePressed:(UIButton *)sender {
  38. UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  39. NSString *string = pasteboard.string;
  40. if (string) {
  41. UTMLog(@"Pasting: %@", string);
  42. [self.keyboardView insertText:string];
  43. } else {
  44. UTMLog(@"No string to paste.");
  45. }
  46. }
  47. - (void)resetModifierToggles {
  48. for (VMKeyboardButton *button in self.customKeyModifierButtons) {
  49. if (button.toggled) {
  50. [self sendExtendedKey:kCSInputKeyRelease code:button.scanCode];
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. button.toggled = NO;
  53. });
  54. }
  55. }
  56. }
  57. - (IBAction)customKeyTouchDown:(VMKeyboardButton *)sender {
  58. if (!sender.toggleable) {
  59. [self sendExtendedKey:kCSInputKeyPress code:sender.scanCode];
  60. }
  61. }
  62. - (IBAction)customKeyTouchUp:(VMKeyboardButton *)sender {
  63. if (sender.toggleable) {
  64. sender.toggled = !sender.toggled;
  65. } else {
  66. [self resetModifierToggles];
  67. }
  68. if (sender.toggleable && sender.toggled) {
  69. [self sendExtendedKey:kCSInputKeyPress code:sender.scanCode];
  70. } else {
  71. [self onDelay:0.05f action:^{
  72. [self sendExtendedKey:kCSInputKeyRelease code:sender.scanCode];
  73. }];
  74. }
  75. }
  76. #pragma mark - iOS 13.4+ key event handling
  77. // from: https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
  78. static const uint8_t hid_to_ps2_table[] = {
  79. 0x00, 0xff, 0xfc, 0x00, 0x1e, 0x30, 0x2e, 0x20,
  80. 0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26,
  81. 0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14,
  82. 0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03,
  83. 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  84. 0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a,
  85. 0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34,
  86. 0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40,
  87. 0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0x00, 0x46,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x45, 0x00, 0x37, 0x4a, 0x4e,
  90. 0x00, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47,
  91. 0x48, 0x49, 0x52, 0x53, 0x56, 0x00, 0x00, 0x59,
  92. 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
  93. 0x6c, 0x6d, 0x6e, 0x76, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x73,
  96. 0x70, 0x7d, 0x79, 0x7b, 0x5c, 0x00, 0x00, 0x00,
  97. 0xf2, 0xf1, 0x78, 0x77, 0x76, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  107. 0x1d, 0x2a, 0x38, 0x00, 0x00, 0x36, 0x00, 0x00,
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  109. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  110. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  111. };
  112. static const uint8_t hid_to_ps2_extended_table[] = {
  113. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  114. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  115. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  116. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  117. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  118. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x37, 0x00,
  122. 0x00, 0x52, 0x47, 0x49, 0x53, 0x4f, 0x51, 0x4d,
  123. 0x4b, 0x50, 0x48, 0x00, 0x35, 0x00, 0x00, 0x00,
  124. 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  125. 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x5e, 0x00,
  126. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  127. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  128. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  129. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  130. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  132. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  133. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  134. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  135. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  141. 0x00, 0x00, 0x00, 0x5b, 0x1d, 0x00, 0x38, 0x5c,
  142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  143. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  145. };
  146. static int API_AVAILABLE(ios(13.4)) hidToPs2(UIKeyboardHIDUsage hidCode) {
  147. int ps2Code = 0;
  148. if (hidCode < 0x100) {
  149. ps2Code = hid_to_ps2_table[hidCode & 0xFF];
  150. if (!ps2Code) {
  151. ps2Code = hid_to_ps2_extended_table[hidCode & 0xFF];
  152. if (ps2Code) {
  153. ps2Code |= 0xE000;
  154. }
  155. }
  156. }
  157. return ps2Code;
  158. }
  159. - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
  160. BOOL didHandleEvent = NO;
  161. for (UIPress *press in presses) {
  162. int code = hidToPs2(press.key.keyCode);
  163. if (code) {
  164. [self sendExtendedKey:kCSInputKeyPress code:code];
  165. didHandleEvent = YES;
  166. }
  167. }
  168. if (!didHandleEvent) {
  169. [super pressesBegan:presses withEvent:event];
  170. }
  171. }
  172. - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event {
  173. BOOL didHandleEvent = NO;
  174. for (UIPress *press in presses) {
  175. int code = hidToPs2(press.key.keyCode);
  176. if (code) {
  177. [self sendExtendedKey:kCSInputKeyRelease code:code];
  178. didHandleEvent = YES;
  179. }
  180. [self resetModifierToggles];
  181. }
  182. if (!didHandleEvent) {
  183. [super pressesEnded:presses withEvent:event];
  184. }
  185. }
  186. #pragma mark - VoiceOver Esc Key workaround
  187. - (BOOL)accessibilityPerformEscape {
  188. [self sendExtendedKey:kCSInputKeyPress code:0x01];
  189. [self onDelay:0.05f action:^{
  190. [self sendExtendedKey:kCSInputKeyRelease code:0x01];
  191. }];
  192. return YES;
  193. }
  194. @end