VMDisplayQemuMetalWindowController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 CocoaSpiceRenderer
  17. import Carbon.HIToolbox
  18. import SwiftUI
  19. class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
  20. var metalView: VMMetalView!
  21. var renderer: CSMetalRenderer?
  22. private var vmDisplay: CSDisplay? {
  23. didSet {
  24. if let renderer = renderer {
  25. oldValue?.removeRenderer(renderer)
  26. vmDisplay?.addRenderer(renderer)
  27. }
  28. }
  29. }
  30. private var vmInput: CSInput?
  31. private var displaySize: CGSize = .zero
  32. private var isDisplaySizeDynamic: Bool = false
  33. private var isFullScreen: Bool = false
  34. private let minDynamicSize = CGSize(width: 800, height: 600)
  35. private let resizeDebounceSecs: Double = 1
  36. private let resizeTimeoutSecs: Double = 5
  37. private var debounceResize: DispatchWorkItem?
  38. private var cancelResize: DispatchWorkItem?
  39. private var localEventMonitor: Any? = nil
  40. private var globalEventMonitor: Any? = nil
  41. private var ctrlKeyDown: Bool = false
  42. private var screenChangedToken: Any?
  43. private var displayConfig: UTMQemuConfigurationDisplay? {
  44. vmQemuConfig?.displays[id]
  45. }
  46. override var defaultTitle: String {
  47. if isSecondary {
  48. return String.localizedStringWithFormat(NSLocalizedString("%@ (Display %lld)", comment: "VMDisplayMetalWindowController"), vmQemuConfig.information.name, id + 1)
  49. } else {
  50. return super.defaultTitle
  51. }
  52. }
  53. // MARK: - User preferences
  54. @Setting("NoCursorCaptureAlert") private var isCursorCaptureAlertShown: Bool = false
  55. @Setting("NoFullscreenCursorCaptureAlert") private var isFullscreenCursorCaptureAlertShown: Bool = false
  56. @Setting("FullScreenAutoCapture") private var isFullScreenAutoCapture: Bool = false
  57. @Setting("WindowFocusAutoCapture") private var isWindowFocusAutoCapture: Bool = false
  58. @Setting("CtrlRightClick") private var isCtrlRightClick: Bool = false
  59. @Setting("AlternativeCaptureKey") private var isAlternativeCaptureKey: Bool = false
  60. @Setting("IsCapsLockKey") private var isCapsLockKey: Bool = false
  61. @Setting("IsNumLockForced") private var isNumLockForced: Bool = false
  62. @Setting("InvertScroll") private var isInvertScroll: Bool = false
  63. @Setting("QEMURendererFPSLimit") private var rendererFpsLimit: Int = 0
  64. // MARK: - Init
  65. convenience init(secondaryFromDisplay display: CSDisplay, primary: VMDisplayQemuMetalWindowController, vm: UTMQemuVirtualMachine, id: Int) {
  66. self.init(vm: vm, id: id)
  67. self.vmDisplay = display
  68. self.vmInput = primary.vmInput
  69. self.isDisplaySizeDynamic = primary.isDisplaySizeDynamic
  70. }
  71. override func windowDidLoad() {
  72. metalView = VMMetalView(frame: displayView.bounds)
  73. metalView.autoresizingMask = [.width, .height]
  74. metalView.device = MTLCreateSystemDefaultDevice()
  75. guard let _ = metalView.device else {
  76. showErrorAlert(NSLocalizedString("Metal is not supported on this device. Cannot render display.", comment: "VMDisplayMetalWindowController"))
  77. logger.critical("Cannot find system default Metal device.")
  78. return
  79. }
  80. displayView.addSubview(metalView)
  81. renderer = CSMetalRenderer.init(metalKitView: metalView)
  82. guard let renderer = self.renderer else {
  83. showErrorAlert(NSLocalizedString("Internal error.", comment: "VMDisplayMetalWindowController"))
  84. logger.critical("Failed to create renderer.")
  85. return
  86. }
  87. if rendererFpsLimit > 0 {
  88. metalView.preferredFramesPerSecond = rendererFpsLimit
  89. }
  90. renderer.changeUpscaler(displayConfig?.upscalingFilter.metalSamplerMinMagFilter ?? .linear, downscaler: displayConfig?.downscalingFilter.metalSamplerMinMagFilter ?? .linear)
  91. vmDisplay?.addRenderer(renderer) // can be nil if primary
  92. metalView.delegate = renderer
  93. metalView.inputDelegate = self
  94. screenChangedToken = NotificationCenter.default.addObserver(forName: NSWindow.didChangeScreenNotification, object: nil, queue: .main) { [weak self] _ in
  95. // update minSize when we change screens
  96. if let self = self,
  97. let window = window,
  98. displaySize != .zero,
  99. !isDisplaySizeDynamic {
  100. window.contentMinSize = contentMinSize(in: window, for: displaySize)
  101. }
  102. }
  103. if isSecondary && isDisplaySizeDynamic, let window = window {
  104. restoreDynamicResolution(for: window)
  105. }
  106. super.windowDidLoad()
  107. }
  108. override func windowWillClose(_ notification: Notification) {
  109. vmDisplay?.removeRenderer(renderer!)
  110. stopAllCapture()
  111. if let screenChangedToken = screenChangedToken {
  112. NotificationCenter.default.removeObserver(screenChangedToken)
  113. }
  114. screenChangedToken = nil
  115. super.windowWillClose(notification)
  116. }
  117. override func enterLive() {
  118. metalView.isHidden = false
  119. screenshotView.isHidden = true
  120. if vmQemuConfig!.sharing.hasClipboardSharing {
  121. UTMPasteboard.general.requestPollingMode(forHashable: self) // start clipboard polling
  122. }
  123. // monitor Cmd+Q and Cmd+W and capture them if needed
  124. localEventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown, .keyUp]) { [weak self] event in
  125. if let self = self, !self.handleCaptureKeys(for: event) {
  126. return event
  127. } else {
  128. return nil
  129. }
  130. }
  131. // monitor caps lock
  132. globalEventMonitor = NSEvent.addGlobalMonitorForEvents(matching: [.flagsChanged]) { [weak self] event in
  133. if let self = self {
  134. // sync caps lock while window is outside focus
  135. self.syncCapsLock(with: event.modifierFlags)
  136. }
  137. }
  138. // resize if we already have a vmDisplay
  139. if let vmDisplay = vmDisplay {
  140. displaySizeDidChange(size: vmDisplay.displaySize)
  141. }
  142. super.enterLive()
  143. setControl(.resize, isEnabled: false) // disable item
  144. if isWindowFocusAutoCapture {
  145. captureMouse()
  146. }
  147. }
  148. override func enterSuspended(isBusy busy: Bool) {
  149. if !busy {
  150. metalView.isHidden = true
  151. screenshotView.image = vm.screenshot?.image
  152. screenshotView.isHidden = false
  153. }
  154. if vm.state == .stopped {
  155. vmDisplay = nil
  156. vmInput = nil
  157. displaySize = .zero
  158. }
  159. stopAllCapture()
  160. super.enterSuspended(isBusy: busy)
  161. }
  162. private func stopAllCapture() {
  163. if vmQemuConfig!.sharing.hasClipboardSharing {
  164. UTMPasteboard.general.releasePollingMode(forHashable: self) // stop clipboard polling
  165. }
  166. if let localEventMonitor = self.localEventMonitor {
  167. NSEvent.removeMonitor(localEventMonitor)
  168. self.localEventMonitor = nil
  169. }
  170. if let globalEventMonitor = globalEventMonitor {
  171. NSEvent.removeMonitor(globalEventMonitor)
  172. self.globalEventMonitor = nil
  173. }
  174. releaseMouse()
  175. }
  176. override func captureMouseButtonPressed(_ sender: Any) {
  177. captureMouse()
  178. }
  179. }
  180. // MARK: - SPICE IO
  181. extension VMDisplayQemuMetalWindowController {
  182. override func spiceDidCreateInput(_ input: CSInput) {
  183. if vmInput == nil {
  184. vmInput = input
  185. }
  186. super.spiceDidCreateInput(input)
  187. }
  188. override func spiceDidDestroyInput(_ input: CSInput) {
  189. if vmInput == input {
  190. vmInput = nil
  191. }
  192. super.spiceDidDestroyInput(input)
  193. }
  194. override func spiceDidCreateDisplay(_ display: CSDisplay) {
  195. if !isSecondary && vmDisplay == nil && display.isPrimaryDisplay {
  196. vmDisplay = display
  197. displaySizeDidChange(size: display.displaySize)
  198. } else {
  199. super.spiceDidCreateDisplay(display)
  200. }
  201. }
  202. override func spiceDidDestroyDisplay(_ display: CSDisplay) {
  203. if vmDisplay == display {
  204. if isSecondary {
  205. DispatchQueue.main.async {
  206. self.close()
  207. }
  208. } else {
  209. vmDisplay = nil
  210. }
  211. } else {
  212. super.spiceDidDestroyDisplay(display)
  213. }
  214. }
  215. override func spiceDidUpdateDisplay(_ display: CSDisplay) {
  216. if vmDisplay == display {
  217. if display.displaySize != self.displaySize {
  218. displaySizeDidChange(size: display.displaySize)
  219. }
  220. } else {
  221. super.spiceDidUpdateDisplay(display)
  222. }
  223. }
  224. override func spiceDynamicResolutionSupportDidChange(_ supported: Bool) {
  225. guard displayConfig!.isDynamicResolution else {
  226. super.spiceDynamicResolutionSupportDidChange(supported)
  227. return
  228. }
  229. if isDisplaySizeDynamic != supported {
  230. displaySizeDidChange(size: displaySize, shouldSaveResolution: false)
  231. DispatchQueue.main.async {
  232. if supported, let window = self.window {
  233. self.restoreDynamicResolution(for: window)
  234. }
  235. }
  236. }
  237. isDisplaySizeDynamic = supported
  238. super.spiceDynamicResolutionSupportDidChange(supported)
  239. }
  240. }
  241. // MARK: - Screen management
  242. extension VMDisplayQemuMetalWindowController {
  243. fileprivate func displaySizeDidChange(size: CGSize, shouldSaveResolution: Bool = true) {
  244. // cancel any pending resize
  245. cancelResize?.cancel()
  246. cancelResize = nil
  247. guard size != .zero else {
  248. logger.debug("Ignoring zero size display")
  249. return
  250. }
  251. DispatchQueue.main.async {
  252. logger.debug("resizing to: (\(size.width), \(size.height))")
  253. guard let window = self.window else {
  254. logger.debug("Invalid window, ignoring size change")
  255. return
  256. }
  257. self.displaySize = size
  258. if self.isFullScreen {
  259. _ = self.updateHostScaling(for: window, frameSize: window.frame.size)
  260. } else {
  261. self.updateHostFrame(forGuestResolution: size)
  262. }
  263. if shouldSaveResolution {
  264. self.saveDynamicResolution()
  265. }
  266. }
  267. }
  268. func windowDidChangeScreen(_ notification: Notification) {
  269. logger.debug("screen changed")
  270. if let vmDisplay = self.vmDisplay {
  271. displaySizeDidChange(size: vmDisplay.displaySize)
  272. }
  273. }
  274. private func contentMinSize(in window: NSWindow, for displaySize: CGSize) -> CGSize {
  275. let currentScreenScale = window.screen?.backingScaleFactor ?? 1.0
  276. let nativeScale = displayConfig!.isNativeResolution ? 1.0 : currentScreenScale
  277. let minScaledSize = CGSize(width: displaySize.width * nativeScale / currentScreenScale, height: displaySize.height * nativeScale / currentScreenScale)
  278. guard let screenSize = window.screen?.visibleFrame.size else {
  279. return minScaledSize
  280. }
  281. let excessSize = window.frameRect(forContentRect: .zero).size
  282. // if the window is larger than our host screen, shrink the min size allowed
  283. let widthScale = (screenSize.width - excessSize.width) / displaySize.width
  284. let heightScale = (screenSize.height - excessSize.height) / displaySize.height
  285. let scale = min(min(widthScale, heightScale), 1.0)
  286. return CGSize(width: displaySize.width * scale, height: displaySize.height * scale)
  287. }
  288. fileprivate func updateHostFrame(forGuestResolution size: CGSize) {
  289. guard let window = window else { return }
  290. guard let vmDisplay = vmDisplay else { return }
  291. let currentScreenScale = window.screen?.backingScaleFactor ?? 1.0
  292. let nativeScale = displayConfig!.isNativeResolution ? 1.0 : currentScreenScale
  293. // change optional scale if needed
  294. if isDisplaySizeDynamic || (!displayConfig!.isNativeResolution && vmDisplay.viewportScale < currentScreenScale) {
  295. vmDisplay.viewportScale = nativeScale
  296. }
  297. let fullContentWidth = size.width * vmDisplay.viewportScale / currentScreenScale
  298. let fullContentHeight = size.height * vmDisplay.viewportScale / currentScreenScale
  299. let contentRect = CGRect(x: window.frame.origin.x,
  300. y: 0,
  301. width: ceil(fullContentWidth),
  302. height: ceil(fullContentHeight))
  303. var windowRect = window.frameRect(forContentRect: contentRect)
  304. windowRect.origin.y = window.frame.origin.y + window.frame.height - windowRect.height
  305. if isDisplaySizeDynamic {
  306. window.contentMinSize = minDynamicSize
  307. window.contentResizeIncrements = NSSize(width: 1, height: 1)
  308. window.setFrame(windowRect, display: false, animate: false)
  309. } else {
  310. window.contentMinSize = contentMinSize(in: window, for: size)
  311. window.contentAspectRatio = size
  312. window.setFrame(windowRect, display: false, animate: true)
  313. }
  314. }
  315. fileprivate func updateHostScaling(for window: NSWindow, frameSize: NSSize) -> NSSize {
  316. guard displaySize != .zero else { return frameSize }
  317. guard let vmDisplay = self.vmDisplay else { return frameSize }
  318. let currentScreenScale = window.screen?.backingScaleFactor ?? 1.0
  319. let targetContentSize = window.contentRect(forFrameRect: CGRect(origin: .zero, size: frameSize)).size
  320. let targetScaleX = targetContentSize.width * currentScreenScale / displaySize.width
  321. let targetScaleY = targetContentSize.height * currentScreenScale / displaySize.height
  322. let targetScale = min(targetScaleX, targetScaleY)
  323. let scaledSize = CGSize(width: displaySize.width * targetScale / currentScreenScale, height: displaySize.height * targetScale / currentScreenScale)
  324. let targetFrameSize = window.frameRect(forContentRect: CGRect(origin: .zero, size: scaledSize)).size
  325. vmDisplay.viewportScale = targetScale
  326. logger.debug("changed scale \(targetScale)")
  327. return targetFrameSize
  328. }
  329. fileprivate func updateGuestResolution(for window: NSWindow, frameSize: NSSize) -> NSSize {
  330. guard let vmDisplay = self.vmDisplay else { return frameSize }
  331. let currentScreenScale = window.screen?.backingScaleFactor ?? 1.0
  332. let nativeScale = displayConfig!.isNativeResolution ? currentScreenScale : 1.0
  333. let targetSize = window.contentRect(forFrameRect: CGRect(origin: .zero, size: frameSize)).size
  334. let targetSizeScaled = displayConfig!.isNativeResolution ? targetSize.applying(CGAffineTransform(scaleX: nativeScale, y: nativeScale)) : targetSize
  335. logger.debug("Requesting resolution: (\(targetSizeScaled.width), \(targetSizeScaled.height))")
  336. let bounds = CGRect(origin: .zero, size: targetSizeScaled)
  337. vmDisplay.requestResolution(bounds)
  338. return frameSize
  339. }
  340. func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
  341. guard !self.isDisplaySizeDynamic else {
  342. return frameSize
  343. }
  344. let newSize = updateHostScaling(for: sender, frameSize: frameSize)
  345. if isFullScreen {
  346. return frameSize
  347. } else {
  348. return newSize
  349. }
  350. }
  351. func windowDidResize(_ notification: Notification) {
  352. guard self.isDisplaySizeDynamic, let window = self.window else {
  353. return
  354. }
  355. debounceResize?.cancel()
  356. debounceResize = DispatchWorkItem {
  357. self._handleResizeEnd(for: window)
  358. }
  359. // when resizing with a mouse drag, we get flooded with this notification
  360. // when using accessibility APIs, we do not get a `windowDidEndLiveResize` notification
  361. DispatchQueue.main.asyncAfter(deadline: .now() + resizeDebounceSecs, execute: debounceResize!)
  362. }
  363. func windowDidEndLiveResize(_ notification: Notification) {
  364. guard self.isDisplaySizeDynamic, let window = self.window else {
  365. return
  366. }
  367. _handleResizeEnd(for: window)
  368. }
  369. private func _handleResizeEnd(for window: NSWindow) {
  370. debounceResize?.cancel()
  371. debounceResize = nil
  372. _ = updateGuestResolution(for: window, frameSize: window.frame.size)
  373. cancelResize?.cancel()
  374. cancelResize = DispatchWorkItem {
  375. if let vmDisplay = self.vmDisplay {
  376. self.displaySizeDidChange(size: vmDisplay.displaySize)
  377. }
  378. }
  379. DispatchQueue.main.asyncAfter(deadline: .now() + resizeTimeoutSecs, execute: cancelResize!)
  380. }
  381. func windowDidEnterFullScreen(_ notification: Notification) {
  382. isFullScreen = true
  383. if isFullScreenAutoCapture {
  384. captureMouse()
  385. }
  386. }
  387. func windowDidExitFullScreen(_ notification: Notification) {
  388. isFullScreen = false
  389. if isFullScreenAutoCapture {
  390. releaseMouse()
  391. }
  392. }
  393. func windowDidBecomeMain(_ notification: Notification) {
  394. // Do not capture mouse if user did not clicked inside the metalView because the window will be draged if user hold the mouse button.
  395. guard let window = window,
  396. window.mouseLocationOutsideOfEventStream.y < metalView.frame.height,
  397. captureMouseToolbarButton.state == .off,
  398. isWindowFocusAutoCapture else {
  399. return
  400. }
  401. captureMouse()
  402. }
  403. func windowDidResignMain(_ notification: Notification) {
  404. releaseMouse()
  405. }
  406. override func windowDidBecomeKey(_ notification: Notification) {
  407. if isFullScreen && isFullScreenAutoCapture {
  408. captureMouse()
  409. }
  410. super.windowDidBecomeKey(notification)
  411. }
  412. override func windowDidResignKey(_ notification: Notification) {
  413. releaseMouse()
  414. super.windowDidResignKey(notification)
  415. }
  416. }
  417. // MARK: - Save and restore resolution
  418. @MainActor extension VMDisplayQemuMetalWindowController {
  419. func saveDynamicResolution() {
  420. guard isDisplaySizeDynamic else {
  421. return
  422. }
  423. var resolution = UTMRegistryEntry.Resolution()
  424. resolution.isFullscreen = isFullScreen
  425. resolution.size = displaySize
  426. vm.registryEntry.resolutionSettings[id] = resolution
  427. }
  428. func restoreDynamicResolution(for window: NSWindow) {
  429. guard let resolution = vm.registryEntry.resolutionSettings[id] else {
  430. return
  431. }
  432. if resolution.isFullscreen && !isFullScreen {
  433. window.toggleFullScreen(self)
  434. } else if let vmDisplay = vmDisplay, resolution.size != .zero {
  435. vmDisplay.requestResolution(CGRect(origin: .zero, size: resolution.size))
  436. } else {
  437. _ = self.updateGuestResolution(for: window, frameSize: window.frame.size)
  438. }
  439. }
  440. }
  441. // MARK: - Input events
  442. extension VMDisplayQemuMetalWindowController: VMMetalViewInputDelegate {
  443. var shouldUseCmdOptForCapture: Bool {
  444. isAlternativeCaptureKey || NSWorkspace.shared.isVoiceOverEnabled
  445. }
  446. func captureMouse() {
  447. guard NSApp.modalWindow == nil && window?.attachedSheet == nil else {
  448. return // don't capture if modal is shown
  449. }
  450. let action = { () -> Void in
  451. self.qemuVM.requestInputTablet(false)
  452. self.metalView?.captureMouse()
  453. self.captureMouseToolbarButton.state = .on
  454. let format = NSLocalizedString("Press %@ to release cursor", comment: "VMDisplayQemuMetalWindowController")
  455. let keys = NSLocalizedString(self.shouldUseCmdOptForCapture ? "⌘+⌥" : "⌃+⌥", comment: "VMDisplayQemuMetalWindowController")
  456. self.window?.subtitle = String.localizedStringWithFormat(format, keys)
  457. self.window?.makeFirstResponder(self.metalView)
  458. self.syncCapsLock()
  459. }
  460. if !isCursorCaptureAlertShown || (isFullScreen && !isFullscreenCursorCaptureAlertShown) {
  461. let alert = NSAlert()
  462. alert.messageText = NSLocalizedString("Captured mouse", comment: "VMDisplayQemuMetalWindowController")
  463. let format = NSLocalizedString("To release the mouse cursor, press %@ at the same time.", comment: "VMDisplayQemuMetalWindowController")
  464. let keys = NSLocalizedString(self.shouldUseCmdOptForCapture ? "⌘+⌥ (Cmd+Opt)" : "⌃+⌥ (Ctrl+Opt)", comment: "VMDisplayQemuMetalWindowController")
  465. alert.informativeText = String.localizedStringWithFormat(format, keys)
  466. alert.showsSuppressionButton = true
  467. alert.beginSheetModal(for: window!) { _ in
  468. if alert.suppressionButton?.state ?? .off == .on {
  469. self.isCursorCaptureAlertShown = true
  470. if self.isFullScreen {
  471. self.isFullscreenCursorCaptureAlertShown = true
  472. }
  473. }
  474. DispatchQueue.main.async(execute: action)
  475. }
  476. } else {
  477. action()
  478. }
  479. }
  480. func releaseMouse() {
  481. syncCapsLock()
  482. qemuVM.requestInputTablet(true)
  483. metalView?.releaseMouse()
  484. self.captureMouseToolbarButton.state = .off
  485. self.window?.subtitle = defaultSubtitle
  486. }
  487. func mouseMove(absolutePoint: CGPoint, buttonMask: CSInputButton) {
  488. guard let window = self.window else { return }
  489. guard let vmInput = vmInput, !vmInput.serverModeCursor else {
  490. logger.trace("requesting client mode cursor")
  491. qemuVM.requestInputTablet(true)
  492. return
  493. }
  494. let currentScreenScale = window.screen?.backingScaleFactor ?? 1.0
  495. let viewportScale = vmDisplay?.viewportScale ?? 1.0
  496. let frameSize = metalView.frame.size
  497. let newX = absolutePoint.x * currentScreenScale / viewportScale
  498. let newY = (frameSize.height - absolutePoint.y) * currentScreenScale / viewportScale
  499. let point = CGPoint(x: newX, y: newY)
  500. logger.trace("move cursor: cocoa (\(absolutePoint.x), \(absolutePoint.y)), native (\(newX), \(newY))")
  501. vmInput.sendMousePosition(buttonMask, absolutePoint: point, forMonitorID: vmDisplay?.monitorID ?? 0)
  502. vmDisplay?.cursor?.move(to: point) // required to show cursor on screen
  503. }
  504. func mouseMove(relativePoint: CGPoint, buttonMask: CSInputButton) {
  505. guard let vmInput = vmInput, vmInput.serverModeCursor else {
  506. logger.trace("requesting server mode cursor")
  507. qemuVM.requestInputTablet(false)
  508. return
  509. }
  510. let translated = CGPoint(x: relativePoint.x, y: -relativePoint.y)
  511. vmInput.sendMouseMotion(buttonMask, relativePoint: translated, forMonitorID: vmDisplay?.monitorID ?? 0)
  512. }
  513. private func modifyMouseButton(_ button: CSInputButton) -> CSInputButton {
  514. let buttonMod: CSInputButton
  515. if button.contains(.left) && ctrlKeyDown && isCtrlRightClick {
  516. buttonMod = button.subtracting(.left).union(.right)
  517. } else {
  518. buttonMod = button
  519. }
  520. return buttonMod
  521. }
  522. func mouseDown(button: CSInputButton, mask: CSInputButton) {
  523. vmInput?.sendMouseButton(modifyMouseButton(button), mask: modifyMouseButton(mask), pressed: true)
  524. }
  525. func mouseUp(button: CSInputButton, mask: CSInputButton) {
  526. vmInput?.sendMouseButton(modifyMouseButton(button), mask: modifyMouseButton(mask), pressed: false)
  527. }
  528. func mouseScroll(dy: CGFloat, buttonMask: CSInputButton) {
  529. let scrollDy = isInvertScroll ? -dy : dy
  530. vmInput?.sendMouseScroll(.smooth, buttonMask: buttonMask, dy: scrollDy)
  531. }
  532. private func sendExtendedKey(_ button: CSInputKey, keyCode: Int) {
  533. if (keyCode & 0xFF00) == 0xE000 {
  534. vmInput?.send(button, code: Int32(0x100 | (keyCode & 0xFF)))
  535. } else if keyCode >= 0x100 {
  536. logger.warning("ignored invalid keycode \(keyCode)");
  537. } else {
  538. vmInput?.send(button, code: Int32(keyCode))
  539. }
  540. }
  541. func keyDown(scanCode: Int) {
  542. if (scanCode & 0xFF) == 0x1D { // Ctrl
  543. ctrlKeyDown = true
  544. }
  545. if !isCapsLockKey && (scanCode & 0xFF) == 0x3A { // Caps Lock
  546. return
  547. }
  548. sendExtendedKey(.press, keyCode: scanCode)
  549. }
  550. func keyUp(scanCode: Int) {
  551. if (scanCode & 0xFF) == 0x1D { // Ctrl
  552. ctrlKeyDown = false
  553. }
  554. if !isCapsLockKey && (scanCode & 0xFF) == 0x3A { // Caps Lock
  555. return
  556. }
  557. sendExtendedKey(.release, keyCode: scanCode)
  558. }
  559. private func handleCaptureKeys(for event: NSEvent) -> Bool {
  560. // if captured we route all keyevents to view
  561. if let metalView = metalView, metalView.isMouseCaptured {
  562. if event.type == .keyDown {
  563. metalView.keyDown(with: event)
  564. } else if event.type == .keyUp {
  565. metalView.keyUp(with: event)
  566. }
  567. return true
  568. }
  569. if event.modifierFlags.contains(.command) && event.type == .keyUp {
  570. // for some reason, macOS doesn't like to send Cmd+KeyUp
  571. metalView.keyUp(with: event)
  572. return false
  573. }
  574. if event.type == .keyDown && (event.keyCode == kVK_JIS_Eisu || event.keyCode == kVK_JIS_Kana) {
  575. // Eisu and Kana keydown events are swallowed and sent directly to IME
  576. metalView.keyDown(with: event)
  577. return true
  578. }
  579. return false
  580. }
  581. /// Syncs the host caps lock state with the guest
  582. /// - Parameter modifier: An NSEvent modifier, or nil to get the current system state
  583. func syncCapsLock(with modifier: NSEvent.ModifierFlags? = nil) {
  584. guard !isCapsLockKey else {
  585. // ignore sync if user disabled it
  586. return
  587. }
  588. guard let vmInput = vmInput else {
  589. return
  590. }
  591. let capsLock: Bool
  592. if let modifier = modifier {
  593. capsLock = modifier.contains(.capsLock)
  594. } else {
  595. let status = CGEventSource.flagsState(.hidSystemState)
  596. capsLock = status.contains(.maskAlphaShift)
  597. }
  598. var locks = vmInput.keyLock
  599. if capsLock {
  600. locks.update(with: .caps)
  601. } else {
  602. locks.subtract(.caps)
  603. }
  604. vmInput.keyLock = locks
  605. }
  606. /// Update virtual num lock status if we force num pad to on
  607. func didUseNumericPad() {
  608. guard isNumLockForced else {
  609. return // nothing to do
  610. }
  611. guard let vmInput = vmInput else {
  612. return
  613. }
  614. if !vmInput.keyLock.contains(.num) {
  615. vmInput.keyLock.insert(.num)
  616. }
  617. }
  618. }
  619. // MARK: - Keyboard shortcuts menu
  620. extension VMDisplayQemuMetalWindowController {
  621. override func updateKeyboardShortcutMenu(_ menu: NSMenu) {
  622. let keyboardShortcuts = UTMKeyboardShortcuts.shared.loadKeyboardShortcuts()
  623. for (index, keyboardShortcut) in keyboardShortcuts.enumerated() {
  624. let item = NSMenuItem()
  625. item.title = keyboardShortcut.title
  626. item.target = self
  627. item.action = #selector(keyboardShortcutHandler)
  628. item.tag = index
  629. menu.addItem(item)
  630. }
  631. menu.addItem(.separator())
  632. let item = NSMenuItem()
  633. item.title = NSLocalizedString("Edit…", comment: "VMDisplayQemuMetalWindowController")
  634. item.target = self
  635. item.action = #selector(keyboardShortcutEdit)
  636. menu.addItem(item)
  637. }
  638. @MainActor @objc private func keyboardShortcutHandler(sender: AnyObject) {
  639. let keyboardShortcuts = UTMKeyboardShortcuts.shared.loadKeyboardShortcuts()
  640. let item = sender as! NSMenuItem
  641. let index = item.tag
  642. guard index < keyboardShortcuts.count else {
  643. return
  644. }
  645. let keys = keyboardShortcuts[index]
  646. withErrorAlert {
  647. try await self.qemuVM.monitor?.sendKeys(keys)
  648. }
  649. }
  650. @MainActor @objc private func keyboardShortcutEdit(sender: AnyObject) {
  651. guard let window = window else {
  652. return
  653. }
  654. let content = NSHostingController(rootView: VMKeyboardShortcutsView {
  655. if let sheet = window.attachedSheet {
  656. window.endSheet(sheet)
  657. }
  658. }.padding())
  659. var fittingSize = content.view.fittingSize
  660. fittingSize.width = 400
  661. let sheetWindow = NSWindow(contentViewController: content)
  662. sheetWindow.setContentSize(fittingSize)
  663. window.beginSheet(sheetWindow)
  664. }
  665. }