-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
console.go
173 lines (142 loc) · 5.4 KB
/
console.go
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
package vz
/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization
# include "virtualization_13.h"
*/
import "C"
import (
"unsafe"
"github.com/Code-Hex/vz/v3/internal/objc"
)
// ConsoleDeviceConfiguration interface for an console device configuration.
type ConsoleDeviceConfiguration interface {
objc.NSObject
consoleDeviceConfiguration()
}
type baseConsoleDeviceConfiguration struct{}
func (*baseConsoleDeviceConfiguration) consoleDeviceConfiguration() {}
// VirtioConsoleDeviceConfiguration is Virtio Console Device.
type VirtioConsoleDeviceConfiguration struct {
*pointer
portsPtr unsafe.Pointer
*baseConsoleDeviceConfiguration
consolePorts map[int]*VirtioConsolePortConfiguration
}
var _ ConsoleDeviceConfiguration = (*VirtioConsoleDeviceConfiguration)(nil)
// NewVirtioConsoleDeviceConfiguration creates a new VirtioConsoleDeviceConfiguration.
func NewVirtioConsoleDeviceConfiguration() (*VirtioConsoleDeviceConfiguration, error) {
if err := macOSAvailable(13); err != nil {
return nil, err
}
config := &VirtioConsoleDeviceConfiguration{
pointer: objc.NewPointer(
C.newVZVirtioConsoleDeviceConfiguration(),
),
consolePorts: make(map[int]*VirtioConsolePortConfiguration),
}
config.portsPtr = C.portsVZVirtioConsoleDeviceConfiguration(objc.Ptr(config))
objc.SetFinalizer(config, func(self *VirtioConsoleDeviceConfiguration) {
objc.Release(self)
})
return config, nil
}
// MaximumPortCount returns the maximum number of ports allocated by this device.
// The default is the number of ports attached to this device.
func (v *VirtioConsoleDeviceConfiguration) MaximumPortCount() uint32 {
return uint32(C.maximumPortCountVZVirtioConsolePortConfigurationArray(v.portsPtr))
}
func (v *VirtioConsoleDeviceConfiguration) SetVirtioConsolePortConfiguration(idx int, portConfig *VirtioConsolePortConfiguration) {
C.setObjectAtIndexedSubscriptVZVirtioConsolePortConfigurationArray(
v.portsPtr,
objc.Ptr(portConfig),
C.int(idx),
)
// to mark as currently reachable.
// This ensures that the object is not freed, and its finalizer is not run
v.consolePorts[idx] = portConfig
}
type ConsolePortConfiguration interface {
objc.NSObject
consolePortConfiguration()
}
type baseConsolePortConfiguration struct{}
func (*baseConsolePortConfiguration) consolePortConfiguration() {}
// VirtioConsolePortConfiguration is Virtio Console Port
//
// A console port is a two-way communication channel between a host VZSerialPortAttachment and
// a virtual machine console port. One or more console ports are attached to a Virtio console device.
type VirtioConsolePortConfiguration struct {
*pointer
*baseConsolePortConfiguration
isConsole bool
name string
attachment SerialPortAttachment
}
var _ ConsolePortConfiguration = (*VirtioConsolePortConfiguration)(nil)
// NewVirtioConsolePortConfigurationOption is an option type to initialize a new VirtioConsolePortConfiguration
type NewVirtioConsolePortConfigurationOption func(*VirtioConsolePortConfiguration)
// WithVirtioConsolePortConfigurationName sets the console port's name.
// The default behavior is to not use a name unless set.
func WithVirtioConsolePortConfigurationName(name string) NewVirtioConsolePortConfigurationOption {
return func(vcpc *VirtioConsolePortConfiguration) {
consolePortName := charWithGoString(name)
defer consolePortName.Free()
C.setNameVZVirtioConsolePortConfiguration(
objc.Ptr(vcpc),
consolePortName.CString(),
)
vcpc.name = name
}
}
// WithVirtioConsolePortConfigurationIsConsole sets the console port may be marked
// for use as the system console. The default is false.
func WithVirtioConsolePortConfigurationIsConsole(isConsole bool) NewVirtioConsolePortConfigurationOption {
return func(vcpc *VirtioConsolePortConfiguration) {
C.setIsConsoleVZVirtioConsolePortConfiguration(
objc.Ptr(vcpc),
C.bool(isConsole),
)
vcpc.isConsole = isConsole
}
}
// WithVirtioConsolePortConfigurationAttachment sets the console port attachment.
// The default is nil.
func WithVirtioConsolePortConfigurationAttachment(attachment SerialPortAttachment) NewVirtioConsolePortConfigurationOption {
return func(vcpc *VirtioConsolePortConfiguration) {
C.setAttachmentVZVirtioConsolePortConfiguration(
objc.Ptr(vcpc),
objc.Ptr(attachment),
)
vcpc.attachment = attachment
}
}
// NewVirtioConsolePortConfiguration creates a new VirtioConsolePortConfiguration.
//
// This is only supported on macOS 13 and newer, error will
// be returned on older versions.
func NewVirtioConsolePortConfiguration(opts ...NewVirtioConsolePortConfigurationOption) (*VirtioConsolePortConfiguration, error) {
if err := macOSAvailable(13); err != nil {
return nil, err
}
vcpc := &VirtioConsolePortConfiguration{
pointer: objc.NewPointer(
C.newVZVirtioConsolePortConfiguration(),
),
}
for _, optFunc := range opts {
optFunc(vcpc)
}
objc.SetFinalizer(vcpc, func(self *VirtioConsolePortConfiguration) {
objc.Release(self)
})
return vcpc, nil
}
// Name returns the console port's name.
func (v *VirtioConsolePortConfiguration) Name() string { return v.name }
// IsConsole returns the console port may be marked for use as the system console.
func (v *VirtioConsolePortConfiguration) IsConsole() bool { return v.isConsole }
// Attachment returns the console port attachment.
func (v *VirtioConsolePortConfiguration) Attachment() SerialPortAttachment {
return v.attachment
}