2
0

VMDisplayViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Copyright © 2021 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 SwiftUI
  17. private var memoryAlertOnce = false
  18. @objc public extension VMDisplayViewController {
  19. var runInBackground: Bool {
  20. boolForSetting("RunInBackground")
  21. }
  22. var disableIdleTimer: Bool {
  23. boolForSetting("DisableIdleTimer")
  24. }
  25. }
  26. // MARK: - View Loading
  27. public extension VMDisplayViewController {
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. }
  31. override func viewWillAppear(_ animated: Bool) {
  32. super.viewWillAppear(animated)
  33. navigationController?.setNavigationBarHidden(true, animated: animated)
  34. }
  35. override func viewWillDisappear(_ animated: Bool) {
  36. super.viewWillDisappear(animated)
  37. if let parent = parent {
  38. parent.setChildForHomeIndicatorAutoHidden(nil)
  39. parent.setChildViewControllerForPointerLock(nil)
  40. UIPress.pressResponderOverride = nil
  41. }
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. if let parent = parent {
  46. parent.setChildForHomeIndicatorAutoHidden(self)
  47. parent.setChildViewControllerForPointerLock(self)
  48. UIPress.pressResponderOverride = self
  49. }
  50. #if !os(visionOS) && !WITH_REMOTE
  51. if runInBackground {
  52. logger.info("Start location tracking to enable running in background")
  53. UTMLocationManager.sharedInstance().startUpdatingLocation()
  54. }
  55. #endif
  56. delegate.displayDidAppear()
  57. }
  58. }
  59. @objc extension VMDisplayViewController {
  60. func enterSuspended(isBusy busy: Bool) {
  61. if !busy {
  62. UIApplication.shared.isIdleTimerDisabled = false
  63. }
  64. }
  65. func enterLive() {
  66. UIApplication.shared.isIdleTimerDisabled = disableIdleTimer
  67. }
  68. private func suspend() {
  69. // dummy function for selector
  70. }
  71. func terminateApplication() {
  72. DispatchQueue.main.async { [self] in
  73. // animate to home screen
  74. let app = UIApplication.shared
  75. app.performSelector(onMainThread: #selector(suspend), with: nil, waitUntilDone: true)
  76. // wait 2 seconds while app is going background
  77. Thread.sleep(forTimeInterval: 2)
  78. // exit app when app is in background
  79. exit(0);
  80. }
  81. }
  82. }
  83. // MARK: Toolbar hiding
  84. public extension VMDisplayViewController {
  85. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  86. for touch in touches {
  87. if touch.type == .direct {
  88. delegate.displayDidAssertUserInteraction()
  89. break
  90. }
  91. }
  92. super.touchesBegan(touches, with: event)
  93. }
  94. }
  95. // MARK: Helper functions
  96. @objc public extension VMDisplayViewController {
  97. /*
  98. - (void)onDelay:(float)delay action:(void (^)(void))block {
  99. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*0.1), dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), block);
  100. }
  101. - (BOOL)boolForSetting:(NSString *)key {
  102. return [[NSUserDefaults standardUserDefaults] boolForKey:key];
  103. }
  104. - (NSInteger)integerForSetting:(NSString *)key {
  105. return [[NSUserDefaults standardUserDefaults] integerForKey:key];
  106. }
  107. */
  108. func onDelay(_ delay: Float, action: @escaping () -> Void) {
  109. DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + .milliseconds(100), execute: action)
  110. }
  111. func boolForSetting(_ key: String) -> Bool {
  112. return UserDefaults.standard.bool(forKey: key)
  113. }
  114. func integerForSetting(_ key: String) -> Int {
  115. return UserDefaults.standard.integer(forKey: key)
  116. }
  117. }