Repository navigation
Expand file tree
/
Copy pathgraphics_device.go
More file actions
136 lines (123 loc) · 4.37 KB
/
Copy pathgraphics_device.go
File metadata and controls
136 lines (123 loc) · 4.37 KB
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
package vz
import (
"errors"
"unsafe"
"github.com/Code-Hex/vz/v4/internal/objc"
"github.com/Code-Hex/vz/v4/internal/objcutil"
"github.com/Code-Hex/vz/v4/internal/vzbridge"
)
// GraphicsDisplayConfiguration configures a graphics display.
// Its concrete type must match the display's original configuration.
type GraphicsDisplayConfiguration interface {
objc.NSObject
graphicsDisplayConfiguration()
}
// GraphicsDevice is a graphics device attached to a virtual machine.
// Obtain it from VirtualMachine.GraphicsDevices.
type GraphicsDevice struct {
*pointer
vm *VirtualMachine
}
// GraphicsDisplayEvent reports the beginning or end of display reconfiguration.
type GraphicsDisplayEvent struct {
// DeviceIndex identifies the graphics device in VirtualMachine.GraphicsDevices.
DeviceIndex int
// DisplayIndex identifies the display in GraphicsDevice.Displays.
DisplayIndex int
// Reconfiguring is true at the beginning and false when frames arrive
// at the most recently requested size and configuration.
Reconfiguring bool
}
// GraphicsDisplay is a display attached to a virtual machine.
// Obtain it from GraphicsDevice.Displays.
type GraphicsDisplay struct {
*pointer
vm *VirtualMachine
}
// GraphicsDisplayChangedNotify registers c for future display reconfiguration events.
// Delivery follows VirtualMachine.StateChangedNotify. The returned function
// unregisters c. This requires macOS 14 or newer.
func (v *VirtualMachine) GraphicsDisplayChangedNotify(c chan<- GraphicsDisplayEvent) (func(), error) {
if err := macOSAvailable(14); err != nil {
return nil, err
}
return v.machineState.graphicsDisplays.subscribe(c)
}
// GraphicsDevices returns the graphics devices configured on the VM.
// It returns nil on macOS versions older than 14.
func (v *VirtualMachine) GraphicsDevices() []*GraphicsDevice {
if macOSAvailable(14) != nil {
return nil
}
pointers := nativeArray(vzbridge.VZVirtualMachine_GraphicsDevices(v))
devices := make([]*GraphicsDevice, len(pointers))
for i, ptr := range pointers {
devices[i] = &GraphicsDevice{
pointer: ptr,
vm: v,
}
}
return devices
}
// Displays returns the displays configured on the graphics device.
func (d *GraphicsDevice) Displays() []*GraphicsDisplay {
pointers := nativeArray(vzbridge.VZGraphicsDevice_Displays(d))
displays := make([]*GraphicsDisplay, len(pointers))
for i, ptr := range pointers {
displays[i] = &GraphicsDisplay{
pointer: ptr,
vm: d.vm,
}
}
return displays
}
// SizeInPixels returns the display's current width and height in pixels.
func (d *GraphicsDisplay) SizeInPixels() (width, height float64) {
var size [2]float64
vzbridge.GraphicsDisplaySize(d, unsafe.Pointer(&size))
return size[0], size[1]
}
// ReconfigureWithSizeInPixels requests a new display size in pixels.
// A nil error means the request was accepted. The guest may ignore the new size.
// This requires macOS 14 or newer.
func (d *GraphicsDisplay) ReconfigureWithSizeInPixels(width, height float64) error {
if err := macOSAvailable(14); err != nil {
return err
}
var errorPointer unsafe.Pointer
vzbridge.ReconfigureGraphicsDisplaySize(d, width, height, &errorPointer)
if err := objcutil.NewNSError(errorPointer); err != nil {
return err
}
return nil
}
// ReconfigureWithConfiguration requests a new display configuration.
// The configuration type must match the display's original configuration.
// A nil error means the request was accepted. The guest may ignore the changes.
// This requires macOS 14 or newer.
func (d *GraphicsDisplay) ReconfigureWithConfiguration(configuration GraphicsDisplayConfiguration) error {
if err := macOSAvailable(14); err != nil {
return err
}
if configuration == nil {
return errors.New("graphics display configuration must not be nil")
}
var errorPointer unsafe.Pointer
vzbridge.VZGraphicsDisplay_ReconfigureWithConfiguration_Error(d, configuration, &errorPointer)
if err := objcutil.NewNSError(errorPointer); err != nil {
return err
}
return nil
}
// PixelsPerInch returns the pixel density of a Mac graphics display.
// It returns an error for other display types or on macOS versions older than 14.
func (d *GraphicsDisplay) PixelsPerInch() (int64, error) {
if err := macOSAvailable(14); err != nil {
return 0, err
}
value, ok := vzbridge.MacGraphicsDisplayPixelsPerInch(d)
if !ok {
return 0, errors.New("pixel density is only available for Mac graphics displays")
}
return value, nil
}