-
Notifications
You must be signed in to change notification settings - Fork 4
/
vboxgo.go
371 lines (300 loc) · 10.5 KB
/
vboxgo.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
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
366
367
368
369
370
371
package vboxgo
// the following comment block is interpreted by cgo
// http://golang.org/cmd/cgo/
// ------------------------------------------------------ cgo
/*
#cgo CFLAGS: -I vbox_sdk/bindings/c/include -I vbox_sdk/bindings/c/glue
#cgo LDFLAGS: -ldl -lpthread
#include "VBoxCAPIGlue.h"
#include <stdlib.h>
HRESULT MY_FAILED(HRESULT rc) {
return FAILED(rc);
}
// formerly : wrappers around pointer-functions, as go can not handle those
// currently: wrappers around define macros
// oh the humanity
void ClientInitialize(IVirtualBoxClient** vboxclient) {
g_pVBoxFuncs->pfnClientInitialize(NULL, vboxclient);
}
void ClientUninitialize() {
g_pVBoxFuncs->pfnClientUninitialize();
}
void ReleaseEverything(ISession* session, IVirtualBox* vbox, IVirtualBoxClient* vboxclient) {
if (session) {
ISession_Release(session);
}
if (vbox) {
IVirtualBox_Release(vbox);
}
if (vboxclient) {
IVirtualBoxClient_Release(vboxclient);
}
}
HRESULT GetVirtualBox(IVirtualBoxClient* vboxclient, IVirtualBox** vbox) {
return IVirtualBoxClient_get_VirtualBox(vboxclient, vbox);
}
HRESULT GetSession(IVirtualBoxClient* vboxclient, ISession** session) {
return IVirtualBoxClient_get_Session(vboxclient, session);
}
HRESULT FindMachine(IVirtualBox* vbox, char* nameOrId, IMachine** machine) {
BSTR BSTR_nameOrId = NULL;
HRESULT res;
g_pVBoxFuncs->pfnUtf8ToUtf16(nameOrId, &BSTR_nameOrId);
res = IVirtualBox_FindMachine(vbox, BSTR_nameOrId, machine);
g_pVBoxFuncs->pfnUtf16Free(BSTR_nameOrId);
return res;
}
HRESULT LockMachine(IMachine* machine, ISession* session) {
return IMachine_LockMachine(machine, session, LockType_Shared);
}
HRESULT UnlockMachine(ISession* session) {
return ISession_UnlockMachine(session);
}
HRESULT GetConsole(ISession* session, IConsole** console) {
return ISession_GetConsole(session, console);
}
HRESULT GetKeyboard(IConsole* console, IKeyboard** keyboard) {
return IConsole_GetKeyboard(console, keyboard);
}
HRESULT GetMouse(IConsole* console, IMouse** mouse) {
return IConsole_GetMouse(console, mouse);
}
HRESULT GetDisplay(IConsole* console, IDisplay** display) {
return IConsole_GetDisplay(console, display);
}
const int MOUSE_1_DOWN = 0x1; //actually, see https://www.virtualbox.org/sdkref/interface_i_mouse.html
const int MOUSE_2_DOWN = 0x2;
const int MOUSE_ALL_UP = 0x0;
HRESULT MouseClickEvent(IMouse* mouse, int x, int y, int upOrDown) {
return IMouse_PutMouseEventAbsolute(mouse, x, y, 0, 0, upOrDown);
}
HRESULT GetScreenResolution(IDisplay* display, uint* out_width, uint* out_height, uint* out_bitsPerPixel) {
PRUint32 screenId = 0;
PRUint32 width, height, bitsPerPixel;
PRInt32 xOrigin, yOrigin;
HRESULT res;
res = IDisplay_GetScreenResolution(display, screenId, &width, &height, &bitsPerPixel, &xOrigin, &yOrigin);
*out_width = (int)width;
*out_height = (int)height;
*out_bitsPerPixel = (int)bitsPerPixel;
// assume, xOrigin and yOrigin are somewhere at 0? Do we even care?
return res;
}
//TODO: supposedly, this api call is slow?
HRESULT TakeScreenShotPNGToArray(IDisplay* display, uint width_in, uint height_in, PRUint32* screenDataSize, PRUint8** screenData) {
PRUint32 screenId = 0;
PRUint32 width = width_in;
PRUint32 height = height_in;
HRESULT res;
//res = IDisplay_TakeScreenShotPNGToArray(display, screenId, width, height, imageData);
res = display->lpVtbl->TakeScreenShotPNGToArray(display, screenId, width, height, screenDataSize, screenData);
return res;
}
*/
import "C" //this triggers the comment to be interpreted
// ------------------------------------------------------ /cgo
import (
"bytes"
"fmt"
"log"
"errors"
"unsafe"
"image"
"image/png"
"image/draw"
)
type VirtualBox struct {
vboxclient *C.IVirtualBoxClient
vbox *C.IVirtualBox
session *C.ISession
// those variables could be gathered, but are not needed
//versionApi uint
//versionVbox uint
//revision C.ULONG
//versionUtf16 C.BSTR
//homefolderUtf16 C.BSTR
}
func (vb *VirtualBox) Init() error {
if C.VBoxCGlueInit() != 0 {
message := fmt.Sprintf("VBoxCGlueInit failed: %v", C.g_szVBoxErrMsg)
log.Println(message)
return errors.New(message)
}
//unsigned ver = g_pVBoxFuncs->pfnGetVersion();
//printf("VirtualBox version: %u.%u.%u\n", ver / 1000000, ver / 1000 % 1000, ver % 1000);
//ver = g_pVBoxFuncs->pfnGetAPIVersion();
//printf("VirtualBox API version: %u.%u\n", ver / 1000, ver % 1000);
C.ClientInitialize(&vb.vboxclient)
if vb.vboxclient == nil {
message := fmt.Sprintf("Could not get vboxclient reference")
log.Println(message)
return errors.New(message)
}
var rc C.HRESULT = C.GetVirtualBox(vb.vboxclient, &vb.vbox)
if C.MY_FAILED(rc) != 0 || vb.vbox == nil {
message := fmt.Sprintf("Could not get vbox reference")
log.Println(message)
return errors.New(message)
}
rc = C.GetSession(vb.vboxclient, &vb.session)
if C.MY_FAILED(rc) != 0 || vb.session == nil {
message := fmt.Sprintf("Could not get session reference")
log.Println(message)
return errors.New(message)
}
return nil
}
func (vb *VirtualBox) Uninit() {
C.ReleaseEverything(vb.session, vb.vbox, vb.vboxclient)
C.ClientUninitialize()
C.VBoxCGlueTerm()
}
type VirtualMachine struct {
Machine *C.IMachine
Name string
Session *C.ISession
locked bool
Console *C.IConsole
Display *C.IDisplay
Mouse *C.IMouse
Keyboard *C.IKeyboard
}
// TODO: name is actually nameOrId
func (vb *VirtualBox) ConnectToMachine(name string) (*VirtualMachine, error) {
var machine *C.IMachine
cs := C.CString(name)
defer C.free(unsafe.Pointer(cs))
var rc C.HRESULT = C.FindMachine(vb.vbox, cs, &machine)
if C.MY_FAILED(rc) != 0 || machine == nil {
if machine != nil {
defer C.free(unsafe.Pointer(machine))
}
message := fmt.Sprintf("Could not get machine reference")
log.Println(message)
return nil, errors.New(message)
}
vm := new(VirtualMachine)
vm.Machine = machine
vm.Name = name
vm.Session = vb.session
vm.locked = false
return vm, nil
}
/*
locking machine or starting it up:
see startVM in tstCAPIGlue.c
https://www.virtualbox.org/sdkref/interface_i_console.html#_details
https://www.virtualbox.org/sdkref/interface_i_mouse.html#_details
*/
// I guess this one expects the vm to be active already, the other possibility would be "LaunchVMProcess"
func (vm *VirtualMachine) LockMachine() error {
var rc C.HRESULT = C.LockMachine(vm.Machine, vm.Session)
if C.MY_FAILED(rc) != 0 {
vm.locked = false
message := fmt.Sprintf("Could not lock machine")
log.Println(message)
return errors.New(message)
}
vm.locked = true
rc = C.GetConsole(vm.Session, &vm.Console)
if C.MY_FAILED(rc) != 0 || vm.Console == nil {
vm.UnlockMachine()
message := fmt.Sprintf("Could not get console reference")
log.Println(message)
return errors.New(message)
}
rc = C.GetMouse(vm.Console, &vm.Mouse)
if C.MY_FAILED(rc) != 0 || vm.Mouse == nil {
vm.UnlockMachine()
message := fmt.Sprintf("Could not get mouse reference")
log.Println(message)
return errors.New(message)
}
rc = C.GetKeyboard(vm.Console, &vm.Keyboard)
if C.MY_FAILED(rc) != 0 || vm.Keyboard == nil {
vm.UnlockMachine()
message := fmt.Sprintf("Could not get keyboard reference")
log.Println(message)
return errors.New(message)
}
rc = C.GetDisplay(vm.Console, &vm.Display)
if C.MY_FAILED(rc) != 0 || vm.Display == nil {
vm.UnlockMachine()
message := fmt.Sprintf("Could not get display reference")
log.Println(message)
return errors.New(message)
}
return nil
}
func (vm *VirtualMachine) UnlockMachine() (error) {
var rc C.HRESULT = C.UnlockMachine(vm.Session)
if C.MY_FAILED(rc) != 0 {
message := fmt.Sprintf("Could not unlock machine")
log.Println(message)
return errors.New(message)
}
vm.locked = false
return nil
}
const (
MOUSE_EVENT_1_DOWN = 0x1
MOUSE_EVENT_2_DOWN = 0x2
MOUSE_EVENT_ALL_UP = 0x0
)
//state should be one of MOUSE_EVENT_...
func (vm *VirtualMachine) MouseEvent(x,y, state int) (error) {
if !vm.locked {
message := fmt.Sprintf("Machine not locked, can't perform mouse down")
log.Println(message)
return errors.New(message)
}
var rc C.HRESULT = C.MouseClickEvent(vm.Mouse, C.int(x),C.int(y), C.int(state));
if C.MY_FAILED(rc) != 0 {
message := fmt.Sprintf("Failed to perform mouse down")
log.Println(message)
return errors.New(message)
}
return nil
}
func (vm *VirtualMachine) GetScreenshot() (*image.NRGBA, error) {
if !vm.locked {
message := fmt.Sprintf("Machine not locked, can't take screenshot")
log.Println(message)
return nil, errors.New(message)
}
width := C.uint(0)
height := C.uint(0)
bitsPerPixel := C.uint(0)
//TODO: is vm.Display threadsafe?
var rc C.HRESULT = C.GetScreenResolution(vm.Display, &width, &height, &bitsPerPixel);
if C.MY_FAILED(rc) != 0 {
message := fmt.Sprintf("Failed to get screen resolution")
log.Println(message)
return nil, errors.New(message)
}
//var imageData [int(width)*(height)*4]char
var imageDataSize C.PRUint32
var imageData *C.PRUint8
defer C.free(unsafe.Pointer(imageData))
rc = C.TakeScreenShotPNGToArray(vm.Display, width, height, &imageDataSize, &imageData)
if C.MY_FAILED(rc) != 0 {
message := fmt.Sprintf("Failed to get screen shot data")
log.Println(message)
return nil, errors.New(message)
}
// convert data to a go byte array
dataSize := int(imageDataSize)
dataBytes := C.GoBytes(unsafe.Pointer(imageData), C.int(dataSize))
reader := bytes.NewReader(dataBytes)
imgUnknown, err := png.Decode(reader) //this guy is probably RGBA
if err != nil {
message := fmt.Sprintf("Failed to decode image from png raw data", err)
log.Println(message)
return nil, err
}
// copy the image into NRGBA format. This is wasteful (TODO?)
b := imgUnknown.Bounds()
imgNRGBA := image.NewNRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
draw.Draw(imgNRGBA, imgNRGBA.Bounds(), imgUnknown, b.Min, draw.Src)
return imgNRGBA, nil
}