~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

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
/** @file
 *
 * VBox frontends: Basic Frontend (BFE):
 * VBoxBFE VM control routines
 *
 */

/*
 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 USA or visit http://www.sun.com if you need
 * additional information or have any questions.
 */

#include <iprt/stream.h>
#include <VBox/err.h>
#include "DisplayImpl.h"
#include "ConsoleImpl.h"
#include "VBoxBFE.h"
#include "VMControl.h"

/**
 * Fullscreen / Windowed toggle.
 */
int
VMCtrlToggleFullscreen(void)
{
    /* not allowed */
    if (!gfAllowFullscreenToggle)
        return VERR_ACCESS_DENIED;

    gFramebuffer->setFullscreen(!gFramebuffer->getFullscreen());

    /*
     * We have switched from/to fullscreen, so request a full
     * screen repaint, just to be sure.
     */
    gDisplay->InvalidateAndUpdate();

    return VINF_SUCCESS;
}

/**
 * Pause the VM.
 */
int
VMCtrlPause(void)
{
    if (machineState != VMSTATE_RUNNING)
        return VERR_VM_INVALID_VM_STATE;

    if (gConsole->inputGrabbed())
        gConsole->inputGrabEnd();

    int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMR3Suspend, 1, pVM);
    AssertRC(rcVBox);
    return VINF_SUCCESS;
}

/**
 * Resume the VM.
 */
int
VMCtrlResume(void)
{
    if (machineState != VMSTATE_SUSPENDED)
        return VERR_VM_INVALID_VM_STATE;

    int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMR3Resume, 1, pVM);
    AssertRC(rcVBox);
    return VINF_SUCCESS;
}

/**
 * Reset the VM
 */
int
VMCtrlReset(void)
{
    int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMR3Reset, 1, pVM);
    AssertRC(rcVBox);
    return VINF_SUCCESS;
}

/**
 * Send ACPI power button press event
 */
int
VMCtrlACPIPowerButton(void)
{
    PPDMIBASE pBase;
    int vrc = PDMR3QueryDeviceLun (pVM, "acpi", 0, 0, &pBase);
    if (RT_SUCCESS (vrc))
    {
        Assert (pBase);
        PPDMIACPIPORT pPort =
            (PPDMIACPIPORT) pBase->pfnQueryInterface(pBase, PDMINTERFACE_ACPI_PORT);
        vrc = pPort ? pPort->pfnPowerButtonPress(pPort) : VERR_INVALID_POINTER;
    }
    return VINF_SUCCESS;
}

/**
 * Send ACPI sleep button press event
 */
int
VMCtrlACPISleepButton(void)
{
    PPDMIBASE pBase;
    int vrc = PDMR3QueryDeviceLun (pVM, "acpi", 0, 0, &pBase);
    if (RT_SUCCESS (vrc))
    {
        Assert (pBase);
        PPDMIACPIPORT pPort =
            (PPDMIACPIPORT) pBase->pfnQueryInterface(pBase, PDMINTERFACE_ACPI_PORT);
        vrc = pPort ? pPort->pfnSleepButtonPress(pPort) : VERR_INVALID_POINTER;
    }
    return VINF_SUCCESS;
}

/**
 * Worker thread while saving the VM
 */
DECLCALLBACK(int) VMSaveThread(RTTHREAD Thread, void *pvUser)
{
    void (*pfnQuit)(void) = (void(*)(void))pvUser;
    int rc;

    startProgressInfo("Saving");
    rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, 
                         (PFNRT)VMR3Save, 5, pVM, g_pszStateFile, false /*fContinueAftewards*/, &callProgressInfo, (uintptr_t)NULL);
    AssertRC(rc);
    endProgressInfo();
    pfnQuit();

    return VINF_SUCCESS;
}

/*
 * Save the machine's state
 */
int
VMCtrlSave(void (*pfnQuit)(void))
{
    int rc;

    if (!g_pszStateFile || !*g_pszStateFile)
        return VERR_INVALID_PARAMETER;

    gConsole->resetKeys();
    RTThreadYield();
    if (gConsole->inputGrabbed())
        gConsole->inputGrabEnd();
    RTThreadYield();

    if (machineState == VMSTATE_RUNNING)
    {
        rc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMR3Suspend, 1, pVM);
        AssertRC(rc);
    }

    RTTHREAD thread;
    rc = RTThreadCreate(&thread, VMSaveThread, (void*)pfnQuit, 0,
                        RTTHREADTYPE_MAIN_WORKER, 0, "Save");
    if (RT_FAILURE(rc))
    {
        RTPrintf("Error: Thread creation failed with %d\n", rc);
        return rc;
    }

    return VINF_SUCCESS;
}