forked from Code-Hex/vz
-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtualization_12.m
365 lines (316 loc) · 11.5 KB
/
virtualization_12.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// virtualization_12.m
//
// Created by codehex.
//
#import "virtualization_12.h"
bool vmCanStop(void *machine, void *queue)
{
if (@available(macOS 12, *)) {
__block BOOL result;
dispatch_sync((dispatch_queue_t)queue, ^{
result = ((VZVirtualMachine *)machine).canStop;
});
return (bool)result;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
void stopWithCompletionHandler(void *machine, void *queue, uintptr_t cgoHandle)
{
if (@available(macOS 12, *)) {
vm_completion_handler_t handler = makeVMCompletionHandler(cgoHandle);
dispatch_sync((dispatch_queue_t)queue, ^{
[(VZVirtualMachine *)machine stopWithCompletionHandler:handler];
});
Block_release(handler);
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract The platform configuration for a generic Intel or ARM virtual machine.
*/
void *newVZGenericPlatformConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZGenericPlatformConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of directory sharing devices. Empty by default.
@see VZDirectorySharingDeviceConfiguration
*/
void setDirectorySharingDevicesVZVirtualMachineConfiguration(void *config, void *directorySharingDevices)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setDirectorySharingDevices:[(NSMutableArray *)directorySharingDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract The hardware platform to use.
@discussion
Can be an instance of a VZGenericPlatformConfiguration or VZMacPlatformConfiguration. Defaults to VZGenericPlatformConfiguration.
*/
void setPlatformVZVirtualMachineConfiguration(void *config, void *platform)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setPlatform:(VZPlatformConfiguration *)platform];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of graphics devices. Empty by default.
@see VZMacGraphicsDeviceConfiguration
*/
void setGraphicsDevicesVZVirtualMachineConfiguration(void *config, void *graphicsDevices)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setGraphicsDevices:[(NSMutableArray *)graphicsDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of pointing devices. Empty by default.
@see VZUSBScreenCoordinatePointingDeviceConfiguration
*/
void setPointingDevicesVZVirtualMachineConfiguration(void *config, void *pointingDevices)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setPointingDevices:[(NSMutableArray *)pointingDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of keyboards. Empty by default.
@see VZUSBKeyboardConfiguration
*/
void setKeyboardsVZVirtualMachineConfiguration(void *config, void *keyboards)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setKeyboards:[(NSMutableArray *)keyboards copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract List of audio devices. Empty by default.
@see VZVirtioSoundDeviceConfiguration
*/
void setAudioDevicesVZVirtualMachineConfiguration(void *config, void *audioDevices)
{
if (@available(macOS 12, *)) {
[(VZVirtualMachineConfiguration *)config setAudioDevices:[(NSMutableArray *)audioDevices copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new Virtio Sound Device Configuration.
@discussion The device exposes a source or destination of sound.
*/
void *newVZVirtioSoundDeviceConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZVirtioSoundDeviceConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Set the list of audio streams exposed by this device. Empty by default.
*/
void setStreamsVZVirtioSoundDeviceConfiguration(void *audioDeviceConfiguration, void *streams)
{
if (@available(macOS 12, *)) {
[(VZVirtioSoundDeviceConfiguration *)audioDeviceConfiguration setStreams:[(NSMutableArray *)streams copy]];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new Virtio Sound Device Input Stream Configuration.
@discussion A PCM stream of input audio data, such as from a microphone.
*/
void *newVZVirtioSoundDeviceInputStreamConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZVirtioSoundDeviceInputStreamConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new Virtio Sound Device Host Audio Input Stream Configuration.
*/
void *newVZVirtioSoundDeviceHostInputStreamConfiguration()
{
if (@available(macOS 12, *)) {
VZVirtioSoundDeviceInputStreamConfiguration *inputStream = (VZVirtioSoundDeviceInputStreamConfiguration *)newVZVirtioSoundDeviceInputStreamConfiguration();
[inputStream setSource:[[VZHostAudioInputStreamSource alloc] init]];
return inputStream;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new Virtio Sound Device Output Stream Configuration.
@discussion A PCM stream of output audio data, such as to a speaker.
*/
void *newVZVirtioSoundDeviceOutputStreamConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZVirtioSoundDeviceOutputStreamConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new Virtio Sound Device Host Audio Output Stream Configuration.
*/
void *newVZVirtioSoundDeviceHostOutputStreamConfiguration()
{
if (@available(macOS 12, *)) {
VZVirtioSoundDeviceOutputStreamConfiguration *outputStream = (VZVirtioSoundDeviceOutputStreamConfiguration *)newVZVirtioSoundDeviceOutputStreamConfiguration();
[outputStream setSink:[[VZHostAudioOutputStreamSink alloc] init]];
return outputStream;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the attachment from a local file url.
@param diskPath Local file path to the disk image in RAW format.
@param readOnly If YES, the device attachment is read-only, otherwise the device can write data to the disk image.
@param cacheMode The caching mode from one of the available VZDiskImageCachingMode options.
@param syncMode How the disk image synchronizes with the underlying storage when the guest operating system flushes data, described by one of the available VZDiskImageSynchronizationMode modes.
@param error If not nil, assigned with the error if the initialization failed.
@return A VZDiskImageStorageDeviceAttachment on success. Nil otherwise and the error parameter is populated if set.
*/
void *newVZDiskImageStorageDeviceAttachmentWithCacheAndSyncMode(const char *diskPath, bool readOnly, int cacheMode, int syncMode, void **error)
{
if (@available(macOS 12, *)) {
NSString *diskPathNSString = [NSString stringWithUTF8String:diskPath];
NSURL *diskURL = [NSURL fileURLWithPath:diskPathNSString];
return [[VZDiskImageStorageDeviceAttachment alloc]
initWithURL:diskURL
readOnly:(BOOL)readOnly
cachingMode:(VZDiskImageCachingMode)cacheMode
synchronizationMode:(VZDiskImageSynchronizationMode)syncMode
error:(NSError *_Nullable *_Nullable)error];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZSharedDirectory from the directory path and read only option.
@param dirPath
The directory path that will be share.
@param readOnly
If the directory should be mounted read only.
@return A VZSharedDirectory
*/
void *newVZSharedDirectory(const char *dirPath, bool readOnly)
{
if (@available(macOS 12, *)) {
NSString *dirPathNSString = [NSString stringWithUTF8String:dirPath];
NSURL *dirURL = [NSURL fileURLWithPath:dirPathNSString];
return [[VZSharedDirectory alloc] initWithURL:dirURL readOnly:(BOOL)readOnly];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZSingleDirectoryShare from the shared directory.
@param sharedDirectory
The shared directory to use.
@return A VZSingleDirectoryShare
*/
void *newVZSingleDirectoryShare(void *sharedDirectory)
{
if (@available(macOS 12, *)) {
return [[VZSingleDirectoryShare alloc] initWithDirectory:(VZSharedDirectory *)sharedDirectory];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZMultipleDirectoryShare from the shared directories.
@param sharedDirectories
NSDictionary mapping names to shared directories.
@return A VZMultipleDirectoryShare
*/
void *newVZMultipleDirectoryShare(void *sharedDirectories)
{
if (@available(macOS 12, *)) {
return [[VZMultipleDirectoryShare alloc] initWithDirectories:(NSDictionary<NSString *, VZSharedDirectory *> *)sharedDirectories];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize the VZVirtioFileSystemDeviceConfiguration from the fs tag.
@param tag
The tag to use for this device configuration.
@return A VZVirtioFileSystemDeviceConfiguration
*/
void *newVZVirtioFileSystemDeviceConfiguration(const char *tag, void **error)
{
if (@available(macOS 12, *)) {
NSString *tagNSString = [NSString stringWithUTF8String:tag];
BOOL valid = [VZVirtioFileSystemDeviceConfiguration validateTag:tagNSString error:(NSError *_Nullable *_Nullable)error];
if (!valid) {
return nil;
}
return [[VZVirtioFileSystemDeviceConfiguration alloc] initWithTag:tagNSString];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Sets share associated with this configuration.
*/
void setVZVirtioFileSystemDeviceConfigurationShare(void *config, void *share)
{
if (@available(macOS 12, *)) {
[(VZVirtioFileSystemDeviceConfiguration *)config setShare:(VZDirectoryShare *)share];
return;
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new configuration for a USB pointing device that reports absolute coordinates.
@discussion This device can be used by VZVirtualMachineView to send pointer events to the virtual machine.
*/
void *newVZUSBScreenCoordinatePointingDeviceConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZUSBScreenCoordinatePointingDeviceConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
/*!
@abstract Initialize a new configuration for a USB keyboard.
@discussion This device can be used by VZVirtualMachineView to send key events to the virtual machine.
*/
void *newVZUSBKeyboardConfiguration()
{
if (@available(macOS 12, *)) {
return [[VZUSBKeyboardConfiguration alloc] init];
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}
void startVirtualMachineWindow(void *machine, double width, double height)
{
// Create a shared app instance.
// This will initialize the global variable
// 'NSApp' with the application instance.
[VZApplication sharedApplication];
if (@available(macOS 12, *)) {
@autoreleasepool {
AppDelegate *appDelegate = [[[AppDelegate alloc]
initWithVirtualMachine:(VZVirtualMachine *)machine
windowWidth:(CGFloat)width
windowHeight:(CGFloat)height] autorelease];
NSApp.delegate = appDelegate;
[NSApp run];
return;
}
}
RAISE_UNSUPPORTED_MACOS_EXCEPTION();
}