VMDisplayAppleTerminalWindowController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // Copyright © 2022 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 Foundation
  17. import SwiftTerm
  18. class VMDisplayAppleTerminalWindowController: VMDisplayAppleWindowController, VMDisplayTerminal {
  19. var terminalView: TerminalView! {
  20. mainView as? TerminalView
  21. }
  22. var serialConfig: UTMAppleConfigurationSerial! {
  23. appleConfig.serials[index]
  24. }
  25. var serialPort: UTMSerialPort! {
  26. serialConfig.interface
  27. }
  28. override var defaultTitle: String {
  29. if isSecondary {
  30. return String.localizedStringWithFormat(NSLocalizedString("%@ (Terminal %lld)", comment: "VMDisplayAppleTerminalWindowController"), super.defaultTitle, index + 1)
  31. } else {
  32. return super.defaultTitle
  33. }
  34. }
  35. private(set) var isPrimary: Bool = true
  36. private(set) var index: Int = 0
  37. convenience init(primaryForIndex index: Int, vm: UTMAppleVirtualMachine, onClose: ((Notification) -> Void)?) {
  38. self.init(vm: vm, onClose: onClose)
  39. self.index = index
  40. }
  41. convenience init(secondaryForIndex index: Int, vm: UTMAppleVirtualMachine) {
  42. self.init(vm: vm, onClose: nil)
  43. self.index = index
  44. }
  45. override func windowDidLoad() {
  46. mainView = TerminalView()
  47. terminalView!.terminalDelegate = self
  48. super.windowDidLoad()
  49. }
  50. override func updateWindowFrame() {
  51. setupTerminal(terminalView, using: serialConfig.terminal!, for: window!)
  52. super.updateWindowFrame()
  53. }
  54. override func resizeConsoleButtonPressed(_ sender: Any) {
  55. let cmd = resizeCommand(for: terminalView, using: serialConfig!.terminal!)
  56. serialPort.write(data: cmd.data(using: .ascii)!)
  57. }
  58. override func enterLive() {
  59. serialPort.delegate = self
  60. super.enterLive()
  61. }
  62. }
  63. // MARK: - Terminal view delegate
  64. extension VMDisplayAppleTerminalWindowController: TerminalViewDelegate, UTMSerialPortDelegate {
  65. func sizeChanged(source: TerminalView, newCols: Int, newRows: Int) {
  66. }
  67. func setTerminalTitle(source: TerminalView, title: String) {
  68. window!.subtitle = title
  69. }
  70. func hostCurrentDirectoryUpdate(source: TerminalView, directory: String?) {
  71. }
  72. func send(source: TerminalView, data: ArraySlice<UInt8>) {
  73. serialPort.write(data: Data(data))
  74. }
  75. func scrolled(source: TerminalView, position: Double) {
  76. }
  77. func serialPort(_ serialPort: UTMSerialPort, didRecieveData data: Data) {
  78. if let terminalView = terminalView {
  79. let arr = [UInt8](data)[...]
  80. DispatchQueue.main.async {
  81. terminalView.feed(byteArray: arr)
  82. }
  83. }
  84. }
  85. }