-
Notifications
You must be signed in to change notification settings - Fork 0
/
Virtualtray.cs
486 lines (419 loc) · 15 KB
/
Virtualtray.cs
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright (c) 2010 Justin Huntington
// This file is licensed under the MIT license. See the LICENSE file.
//$/reference:VirtualBox.dll
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VirtualBox;
[assembly: AssemblyTitle("Virtualtray")]
[assembly: AssemblyProduct("Virtualtray")]
[assembly: AssemblyCopyright("© 2010 Justin Huntington, MIT license, https://github.com/justinwh")]
[assembly: AssemblyVersion("1.0.*")]
/* A running Virtualtray has one instance of this class to manage the icon &
* global menu items. It creates one MachineMenu per VM, to manage each
* submenu.
*/
public class Virtualtray : ApplicationContext, IVirtualBoxCallback {
private NotifyIcon _notifyIcon;
private IVirtualBox _virtualbox;
private Hashtable _vms;
private Icon _icon;
private Icon _idleIcon;
private Virtualtray() {
_virtualbox = (IVirtualBox) new VirtualBox.VirtualBox();
/* the generated VirtualBox.dll COM library doesn't work with the C# event
* system, so we handle events by registering callbacks
*/
_virtualbox.RegisterCallback(this);
_icon = GetIcon("icon/icon-16.ico");
_idleIcon = GetIcon("icon/icon-gray-16.ico");
_notifyIcon = new NotifyIcon();
_notifyIcon.Icon = _idleIcon;
_notifyIcon.Text = "Virtualtray";
_notifyIcon.Visible = true;
_vms = new Hashtable();
ContextMenu menu = new ContextMenu();
foreach (IMachine machine in _virtualbox.Machines) {
MachineMenu mm = new MachineMenu(machine, menu);
mm.StateChange += new EventHandler(MachineStateChangeEventHandler);
_vms.Add(machine.Id, mm);
}
menu.MenuItems.Add(new MenuItem("-") {Name = "-"});
menu.MenuItems.Add(new MenuItem("Open VirtualBox...", new EventHandler(
MenuLaunchVirtualBoxEventHandler)));
menu.MenuItems.Add(new MenuItem("Exit", new EventHandler(MenuExitEventHandler)));
_notifyIcon.ContextMenu = menu;
ToggleIcon();
_notifyIcon.MouseClick += new MouseEventHandler(IconClickEventHandler);
Application.ApplicationExit += new EventHandler(ApplicationExitEventHandler);
}
// get the icon embedded in the EXE, fall back to looking for the file
private Icon GetIcon(string path) {
System.IO.Stream s = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(path);
if (s != null) {
return new Icon(s);
}
return new Icon(path);
}
private void IconClickEventHandler(object sender, MouseEventArgs e) {
if (e.Button != MouseButtons.Left) {
return;
}
// there's no public interface to get NotifyIcon to show its ContextMenu
MethodInfo showContextMenu = typeof(NotifyIcon).GetMethod("ShowContextMenu",
BindingFlags.Instance | BindingFlags.NonPublic);
if (showContextMenu != null) {
showContextMenu.Invoke(_notifyIcon, null);
}
}
private void MenuLaunchVirtualBoxEventHandler(object sender, EventArgs e) {
LaunchVirtualBox();
}
private void MenuExitEventHandler(object sender, EventArgs e) {
Application.Exit();
}
private void MachineStateChangeEventHandler(object sender, EventArgs e) {
ToggleIcon();
}
/* toggle between the grayscale (idle) icon and the normal icon when any machine
* becomes active
*/
private void ToggleIcon() {
foreach (MachineMenu mm in _vms.Values) {
if (!mm.Stopped) {
_notifyIcon.Icon = _icon;
return;
}
}
_notifyIcon.Icon = _idleIcon;
}
// add/remove MachineMenus when VMs are created/destroyed in the main Virtualbox UI
public void OnMachineRegistered(string machineId, int registered) {
if (registered > 0) {
IMachine machine = Array.Find<IMachine>(_virtualbox.Machines, delegate(IMachine m) {
return m.Id == machineId;
});
MachineMenu mm = new MachineMenu(machine, _notifyIcon.ContextMenu);
_vms.Add(machineId, mm);
}
else {
MachineMenu m = (MachineMenu) _vms[machineId];
m.Remove();
_vms.Remove(machineId);
}
}
private void ApplicationExitEventHandler(object sender, EventArgs e) {
_notifyIcon.Icon = null;
}
// unused callbacks
public void OnMachineStateChange(string machineId, MachineState state) {}
public void OnMachineDataChange(string machineId) {}
public void OnExtraDataChange(string machineId, string key, string value) {}
public void OnMediumRegistered(string mediumId, VirtualBox.DeviceType type, int registered) {}
public void OnSessionStateChange(string sessionId, VirtualBox.SessionState state) {}
public void OnSnapshotTaken(string machineId, string snapshotId) {}
public void OnSnapshotDeleted(string machineId, string snapshotId) {}
public void OnSnapshotChange(string machineId, string snapshotId) {}
public void OnGuestPropertyChange(string machineId, string name, string value, string flags) {}
public int OnExtraDataCanChange(string machineId, string key, string value, out string error) {
error = null;
return 1;
}
// launch the main Virtualbox UI (or bring it to the front if it's running)
private static void LaunchVirtualBox() {
Process virtualbox = null;
foreach (Process p in Process.GetProcesses()) {
/* since VMs have the same ProcessName as the config UI, guess which is
* which based on Window title
*/
if (p.ProcessName == "VirtualBox" && p.MainWindowTitle != null &&
!p.MainWindowTitle.Contains("-"))
{
virtualbox = p;
break;
}
}
if (virtualbox != null) {
ShowWindow(virtualbox.MainWindowHandle.ToInt32(), 5);
SetForegroundWindow(virtualbox.MainWindowHandle.ToInt32());
}
else {
string path = Environment.GetEnvironmentVariable("VBOX_INSTALL_PATH");
Process.Start(path + "VirtualBox.exe");
}
}
[DllImport("User32")]
private static extern int ShowWindow(int hWnd, int nCmdShow);
[DllImport("User32")]
private static extern int SetForegroundWindow(int hWnd);
// Each instance of MachineMenu manage one VM and its submenu
private class MachineMenu : IVirtualBoxCallback {
private enum Submenu {Start = 0, StartHeadless, Save, Stop}
private IVirtualBox _virtualbox;
private ISession _session;
private string _id;
private string _name;
private Menu _parentMenu;
private MenuItem _menu;
private MenuItem[] _submenu = new MenuItem[Enum.GetValues(typeof(Submenu)).Length];
public MachineMenu(IMachine machine, Menu parentMenu) {
_virtualbox = machine.Parent;
_virtualbox.RegisterCallback(this);
_session = null;
_id = machine.Id;
_name = machine.Name;
_parentMenu = parentMenu;
_menu = new MenuItem(machine.Name);
/* initially create MenuItems for all possible VM actions, then show/hide
* them as needed
*/
object[][] submenus = new object [][] {
new object[] {Submenu.Start, "Start Machine", new EventHandler(MenuStartEventHandler)},
new object[] {Submenu.StartHeadless, "Start Machine Headless", new EventHandler(MenuStartHeadlessEventHandler)},
new object[] {Submenu.Save, "Save Machine", new EventHandler(MenuSaveEventHandler)},
new object[] {Submenu.Stop, "Stop Machine", new EventHandler(MenuStopEventHandler)}
};
Array.ForEach<object[]>(submenus, delegate(object[] s) {
MenuItem m = new MenuItem((string) s[1], (EventHandler) s[2]);
m.Visible = false;
_submenu[(int) s[0]] = m;
_menu.MenuItems.Add(m);
});
State = machine.State;
int last = _parentMenu.MenuItems.IndexOfKey("-");
last = last < 0 ? _parentMenu.MenuItems.Count : last;
_parentMenu.MenuItems.Add(last, _menu);
}
public event EventHandler StateChange;
public void Remove() {
_parentMenu.MenuItems.Remove(_menu);
}
private MachineState _state;
// toggle label and MenuItem visibility based on VM state
public MachineState State {
get {
return _state;
}
set {
_state = value;
string stateLabel = null;
switch (_state) {
case MachineState.MachineState_Running:
case MachineState.MachineState_Paused:
case MachineState.MachineState_Teleporting:
case MachineState.MachineState_LiveSnapshotting:
case MachineState.MachineState_TeleportingPausedVM:
case MachineState.MachineState_TeleportingIn:
case MachineState.MachineState_DeletingSnapshotOnline:
case MachineState.MachineState_DeletingSnapshotPaused:
stateLabel = "Running";
onlyShowSubmenus(Submenu.Save, Submenu.Stop);
break;
case MachineState.MachineState_Starting:
case MachineState.MachineState_Restoring:
stateLabel = "Starting";
onlyShowSubmenus();
break;
case MachineState.MachineState_Stopping:
stateLabel = "Stopping";
onlyShowSubmenus();
break;
case MachineState.MachineState_Saving:
stateLabel = "Saving";
onlyShowSubmenus();
break;
case MachineState.MachineState_Stuck:
stateLabel = "Crashed";
onlyShowSubmenus(Submenu.Stop);
break;
case MachineState.MachineState_PoweredOff:
case MachineState.MachineState_Saved:
case MachineState.MachineState_Teleported:
case MachineState.MachineState_Aborted:
case MachineState.MachineState_RestoringSnapshot:
case MachineState.MachineState_DeletingSnapshot:
case MachineState.MachineState_SettingUp:
default:
stateLabel = "Stopped";
onlyShowSubmenus(Submenu.Start, Submenu.StartHeadless);
break;
}
_menu.Text = _name + " (" + stateLabel + ")";
if (StateChange != null) {
StateChange(this, EventArgs.Empty);
}
}
}
public bool Stopped {
get {
switch (_state) {
case MachineState.MachineState_Running:
case MachineState.MachineState_Paused:
case MachineState.MachineState_Teleporting:
case MachineState.MachineState_LiveSnapshotting:
case MachineState.MachineState_TeleportingPausedVM:
case MachineState.MachineState_TeleportingIn:
case MachineState.MachineState_DeletingSnapshotOnline:
case MachineState.MachineState_DeletingSnapshotPaused:
case MachineState.MachineState_Starting:
case MachineState.MachineState_Restoring:
case MachineState.MachineState_Stopping:
case MachineState.MachineState_Saving:
case MachineState.MachineState_Stuck:
return false;
case MachineState.MachineState_PoweredOff:
case MachineState.MachineState_Saved:
case MachineState.MachineState_Teleported:
case MachineState.MachineState_Aborted:
case MachineState.MachineState_RestoringSnapshot:
case MachineState.MachineState_DeletingSnapshot:
case MachineState.MachineState_SettingUp:
default:
return true;
}
}
}
/* convenience method to toggle submenus, call like:
* onlyShowSubmenus(Submenu.Start, Submenu.StartHeadless)
*/
private void onlyShowSubmenus(params Submenu[] toshow) {
foreach (MenuItem m in _submenu) {
m.Visible = false;
}
foreach (Submenu i in toshow) {
_submenu[(int) i].Visible = true;
}
}
/* disable submenus while an action is in progress & re-enable
* once complete
*/
private void enableSubmenus(bool enable) {
foreach (MenuItem m in _submenu) {
m.Enabled = enable;
}
}
// start the VM using the VirtualBox.dll COM interface
private void MenuStartEventHandler(object sender, EventArgs e) {
if (_session != null) {
return;
}
enableSubmenus(false);
_session = new Session();
_virtualbox.OpenRemoteSession((Session) _session, _id, "gui", "");
}
// start the VM in headless mode
private void MenuStartHeadlessEventHandler(object sender, EventArgs e) {
if (_session != null) {
return;
}
enableSubmenus(false);
/* Using OpenRemoteSession() to initiate 'headless' VM execution causes the
* VM to execute in a cmd window. Virtualbox has a 'DETACHED' flag internally
* that allows execution without a cmd window, but doesn't expose it via the
* COM API. To execute a VM headless without a cmd window, we instead execute
* the VBoxHeadless.exe executable directly.
*/
/*
_session = new Session();
_virtualbox.OpenRemoteSession((Session) _session, _id, "headless", "");
return;
*/
string path = Environment.GetEnvironmentVariable("VBOX_INSTALL_PATH");
Process p = new Process();
p.StartInfo.FileName = path + "VBoxHeadless.exe";
p.StartInfo.Arguments = "--startvm " + _id + " --vrdp=config";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
enableSubmenus(true);
}
// stop the VM
private void MenuStopEventHandler(object sender, EventArgs e) {
if (_session != null) {
return;
}
enableSubmenus(false);
_session = new Session();
_virtualbox.OpenExistingSession((Session) _session, _id);
_session.Console.PowerDown();
_session.Close();
_session = null;
enableSubmenus(true);
}
// stop the VM & save its state
private void MenuSaveEventHandler(object sender, EventArgs e) {
if (_session != null) {
return;
}
enableSubmenus(false);
_session = new Session();
_virtualbox.OpenExistingSession((Session) _session, _id);
_session.Console.SaveState();
_session.Close();
_session = null;
enableSubmenus(true);
}
private IMachine GetMachine() {
foreach (IMachine m in _virtualbox.Machines) {
if (m.Id == _id) {
return m;
}
}
throw new Exception("Machine '" + _id + "' doesn't exist");
}
public void OnMachineStateChange(string machineId, MachineState state) {
if (machineId != _id) {
return;
}
this.State = state;
}
// listen for VM startup and immediately close our remote session
public void OnSessionStateChange(string machineId, SessionState state) {
if (machineId != _id) {
return;
}
if (state == SessionState.SessionState_Open) {
_session.Close();
_session = null;
enableSubmenus(true);
}
}
// unused callbacks
public void OnMachineDataChange(string machineId) {}
public void OnExtraDataChange(string machineId, string key, string value) {}
public void OnMediumRegistered(string mediumId, DeviceType type, int registered) {}
public void OnMachineRegistered(string machineId, int registered) {}
public void OnSnapshotTaken(string machineId, string snapshotId) {}
public void OnSnapshotDeleted(string machineId, string snapshotId) {}
public void OnSnapshotChange(string machineId, string snapshotId) {}
public void OnGuestPropertyChange(string machineId, string name, string value, string flags) {}
public int OnExtraDataCanChange(string machineId, string key, string value, out string error) {
error = null;
return 1;
}
}
[STAThread]
static void Main(string[] args) {
// find Virtualbox's install path so we can run executables
string path = Environment.GetEnvironmentVariable("VBOX_INSTALL_PATH");
if (path == null) {
// fall back to looking in the registry
path = (string) Microsoft.Win32.Registry.GetValue(
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Oracle\\VirtualBox", "InstallDir", null);
if (path != null) {
Environment.SetEnvironmentVariable("VBOX_INSTALL_PATH", path);
}
else {
Console.WriteLine("VBOX_INSTALL_PATH environment variable is not set");
return;
}
}
Virtualtray v = new Virtualtray();
Application.Run(v);
}
}