UTMVirtualMachine.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Copyright © 2019 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/Foundation.h>
  17. #import "UTMVirtualMachineDelegate.h"
  18. @import QEMUKitInternal;
  19. @class UTMConfigurationWrapper;
  20. @class UTMRegistryEntry;
  21. @class CSScreenshot;
  22. NS_ASSUME_NONNULL_BEGIN
  23. /// Abstract interface to a UTM virtual machine
  24. @interface UTMVirtualMachine : NSObject
  25. /// Path where the .utm is stored
  26. @property (nonatomic, readonly) NSURL *path;
  27. /// True if the .utm is loaded outside of the default storage
  28. ///
  29. /// This property is observable and must only be accessed on the main thread.
  30. @property (nonatomic) BOOL isShortcut;
  31. /// Set by caller to handle VM events
  32. @property (nonatomic, weak, nullable) id<UTMVirtualMachineDelegate> delegate;
  33. /// Configuration for this VM
  34. ///
  35. /// This property is observable and must only be accessed on the main thread.
  36. @property (nonatomic, readonly) UTMConfigurationWrapper *config;
  37. /// Additional configuration on a short lived, per-host basis
  38. ///
  39. /// This includes display size, bookmarks to removable drives, etc.
  40. /// This property is observable and must only be accessed on the main thread.
  41. @property (nonatomic, readonly) UTMRegistryEntry *registryEntry;
  42. /// Current VM state, can observe this property for state changes or use the delegate
  43. ///
  44. /// This property is observable and must only be accessed on the main thread.
  45. @property (nonatomic, assign, readonly) UTMVMState state;
  46. /// If non-null, is the most recent screenshot image of the running VM
  47. ///
  48. /// This property is observable and must only be accessed on the main thread.
  49. @property (nonatomic, readonly, nullable) CSScreenshot *screenshot;
  50. /// Display VM as "deleted" for UI elements
  51. ///
  52. /// This is a workaround for SwiftUI bugs not hiding deleted elements.
  53. /// This property is observable and must only be accessed on the main thread.
  54. @property (nonatomic) BOOL isDeleted;
  55. /// Display VM as "busy" for UI elements
  56. ///
  57. /// This is shorthand for checking if the `state` is one of the busy ones.
  58. /// This property is observable and must only be accessed on the main thread.
  59. @property (nonatomic, readonly) BOOL isBusy;
  60. /// Whether the next start of this VM should have the -snapshot flag set
  61. ///
  62. /// This will be passed to UTMQemuSystem,
  63. /// and will be cleared when the VM stops or has an error.
  64. ///
  65. /// This property is observable and must only be accessed on the main thread.
  66. @property (nonatomic) BOOL isRunningAsSnapshot;
  67. /// Checks if a save state exists
  68. ///
  69. /// This property is observable and must only be accessed on the main thread.
  70. @property (nonatomic, readonly) BOOL hasSaveState;
  71. /// Checks if a file URL is a .utm bundle
  72. /// @param url File URL
  73. + (BOOL)URLisVirtualMachine:(NSURL *)url NS_SWIFT_NAME(isVirtualMachine(url:));
  74. /// Get name of UTM virtual machine from a file
  75. /// @param url File URL
  76. + (NSString *)virtualMachineName:(NSURL *)url;
  77. /// Get the path of a UTM virtual machine from a name and parent directory
  78. /// @param name VM name
  79. /// @param parent Base directory file URL
  80. + (NSURL *)virtualMachinePath:(NSString *)name inParentURL:(NSURL *)parent;
  81. /// Create an existing UTM virtual machine from a path
  82. /// @param url File URL
  83. + (nullable UTMVirtualMachine *)virtualMachineWithURL:(NSURL *)url;
  84. /// Create a new UTM virtual machine from a configuration
  85. ///
  86. /// `-saveUTMWithCompletion:` should be called to save to disk.
  87. /// @param configuration VM configuration
  88. /// @param packageURL Location of the VM
  89. + (UTMVirtualMachine *)virtualMachineWithConfigurationWrapper:(UTMConfigurationWrapper *)configuration packageURL:(NSURL *)packageURL;
  90. /// Discard any changes to configuration by reloading from disk
  91. /// @param err Error thrown
  92. /// @returns True if successful
  93. - (BOOL)reloadConfigurationWithError:(NSError * _Nullable *)err;
  94. /// Save .utm bundle to disk
  95. ///
  96. /// This will create a configuration file and any auxiliary data files if needed.
  97. /// @param completion Handler always will be called on completion
  98. /// @returns Any error thrown will be non-null passed to the `completion` handler
  99. - (void)saveUTMWithCompletion:(void (^)(NSError * _Nullable))completion;
  100. /// Starts accessing security scoped bookmark for this .utm bundle
  101. ///
  102. /// Must be called if this UTM VM is a shortcut. Otherwise, this will do nothing.
  103. /// If called, on `-dealloc`, the security scoped resource will be released.
  104. /// @param completion Handler always will be called on completion
  105. /// @returns Any error thrown will be non-null passed to the `completion` handler
  106. - (void)accessShortcutWithCompletion:(void (^ _Nullable)(NSError * _Nullable))completion;
  107. /// Starts the VM
  108. ///
  109. /// Any error will be passed to the `delegate`
  110. - (void)requestVmStart;
  111. /// Starts the VM
  112. ///
  113. /// @param completion Handler always will be called on completion
  114. /// @returns Any error thrown will be non-null passed to the `completion` handler
  115. - (void)vmStartWithCompletion:(void (^)(NSError * _Nullable))completion;
  116. /// Stops the VM
  117. ///
  118. /// Waits for the VM to acknowledge the stop request before attempting to clean up.
  119. /// Any error will be passed to the `delegate`
  120. - (void)requestVmStop;
  121. /// Stops the VM
  122. ///
  123. /// @param completion Handler always will be called on completion
  124. /// @returns Any error thrown will be non-null passed to the `completion` handler
  125. - (void)vmStopWithCompletion:(void (^)(NSError * _Nullable))completion;
  126. /// Stops the VM without waiting
  127. ///
  128. /// Any error will be passed to the `delegate`
  129. /// @param force If true, will not wait for VM to stop before cleanup
  130. - (void)requestVmStopForce:(BOOL)force NS_SWIFT_NAME(requestVmStop(force:));
  131. /// Stops the VM
  132. ///
  133. /// @param force If true, will not wait for VM to stop before cleanup
  134. /// @param completion Handler always will be called on completion
  135. /// @returns Any error thrown will be non-null passed to the `completion` handler
  136. - (void)vmStopForce:(BOOL)force completion:(void (^)(NSError * _Nullable))completion NS_SWIFT_NAME(vmStop(force:completion:));
  137. /// Restarts the VM
  138. ///
  139. /// Any error will be passed to the `delegate`
  140. - (void)requestVmReset;
  141. /// Restarts the VM
  142. ///
  143. /// @param completion Handler always will be called on completion
  144. /// @returns Any error thrown will be non-null passed to the `completion` handler
  145. - (void)vmResetWithCompletion:(void (^)(NSError * _Nullable))completion;
  146. /// Pauses the VM
  147. ///
  148. /// Any error will be passed to the `delegate`
  149. /// @param save Save VM state.
  150. - (void)requestVmPauseSave:(BOOL)save NS_SWIFT_NAME(requestVmPause(save:));
  151. /// Pauses the VM
  152. ///
  153. /// @param save Save VM state.
  154. /// @param completion Handler always will be called on completion
  155. /// @returns Any error thrown will be non-null passed to the `completion` handler
  156. - (void)vmPauseSave:(BOOL)save completion:(void (^)(NSError * _Nullable))completion NS_SWIFT_NAME(vmPause(save:completion:));
  157. /// Saves the current VM state
  158. ///
  159. /// Any error will be passed to the `delegate`
  160. - (void)requestVmSaveState;
  161. /// Saves the current VM state
  162. ///
  163. /// @param completion Handler always will be called on completion
  164. /// @returns Any error thrown will be non-null passed to the `completion` handler
  165. - (void)vmSaveStateWithCompletion:(void (^)(NSError * _Nullable))completion;
  166. /// Deletes the saved VM state
  167. ///
  168. /// Any error will be passed to the `delegate`
  169. - (void)requestVmDeleteState;
  170. /// Deletes the saved VM state
  171. ///
  172. /// @param completion Handler always will be called on completion
  173. /// @returns Any error thrown will be non-null passed to the `completion` handler
  174. - (void)vmDeleteStateWithCompletion:(void (^)(NSError * _Nullable))completion;
  175. /// Resumes a paused VM
  176. ///
  177. /// Any error will be passed to the `delegate`
  178. - (void)requestVmResume;
  179. /// Resumes a paused VM
  180. ///
  181. /// @param completion Handler always will be called on completion
  182. /// @returns Any error thrown will be non-null passed to the `completion` handler
  183. - (void)vmResumeWithCompletion:(void (^)(NSError * _Nullable))completion;
  184. /// Sends power off request to the guest
  185. - (void)requestGuestPowerDown;
  186. /// Request power down from the guest
  187. ///
  188. /// @param completion Handler always will be called on completion
  189. /// @returns Any error thrown will be non-null passed to the `completion` handler
  190. - (void)vmGuestPowerDownWithCompletion:(void (^)(NSError * _Nullable))completion;
  191. @end
  192. NS_ASSUME_NONNULL_END