3
* VirtualBox COM class implementation
7
* Copyright (C) 2006-2007 innotek GmbH
9
* This file is part of VirtualBox Open Source Edition (OSE), as
10
* available from http://www.virtualbox.org. This file is free software;
11
* you can redistribute it and/or modify it under the terms of the GNU
12
* General Public License as published by the Free Software Foundation,
13
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14
* distribution. VirtualBox OSE is distributed in the hope that it will
15
* be useful, but WITHOUT ANY WARRANTY of any kind.
18
#include "KeyboardImpl.h"
19
#include "ConsoleImpl.h"
23
#include <VBox/pdmdrv.h>
27
////////////////////////////////////////////////////////////////////////////////
30
////////////////////////////////////////////////////////////////////////////////
33
* Keyboard driver instance data.
35
typedef struct DRVMAINKEYBOARD
37
/** Pointer to the keyboard object. */
39
/** Pointer to the driver instance structure. */
41
/** Pointer to the keyboard port interface of the driver/device above us. */
42
PPDMIKEYBOARDPORT pUpPort;
43
/** Our mouse connector interface. */
44
PDMIKEYBOARDCONNECTOR Connector;
45
} DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
47
/** Converts PDMIVMMDEVCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
48
#define PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface) ( (PDRVMAINKEYBOARD) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINKEYBOARD, Connector)) )
51
// constructor / destructor
52
////////////////////////////////////////////////////////////////////////////////
54
HRESULT Keyboard::FinalConstruct()
59
mfVMMDevInited = false;
63
void Keyboard::FinalRelease()
70
////////////////////////////////////////////////////////////////////////////////
73
* Initializes the keyboard object.
75
* @returns COM result indicator
76
* @param parent handle of our parent object
78
HRESULT Keyboard::init (Console *parent)
80
LogFlow(("Keyboard::init(): isReady=%d\n", isReady()));
82
ComAssertRet (parent, E_INVALIDARG);
84
AutoLock alock (this);
85
ComAssertRet (!isReady(), E_UNEXPECTED);
94
* Uninitializes the instance and sets the ready flag to FALSE.
95
* Called either from FinalRelease() or by the parent when it gets destroyed.
97
void Keyboard::uninit()
99
LogFlow(("Keyboard::uninit(): isReady=%d\n", isReady()));
101
AutoLock alock (this);
102
AssertReturn (isReady(), (void) 0);
105
mpDrv->pKeyboard = NULL;
108
mfVMMDevInited = true;
114
* Sends a scancode to the keyboard.
116
* @returns COM status code
117
* @param scancode The scancode to send
119
STDMETHODIMP Keyboard::PutScancode(LONG scancode)
121
AutoLock alock (this);
124
CHECK_CONSOLE_DRV (mpDrv);
126
int rcVBox = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, (uint8_t)scancode);
128
if (VBOX_FAILURE (rcVBox))
129
return setError (E_FAIL,
130
tr ("Could not send scan code 0x%08X to the virtual keyboard (%Vrc)"),
137
* Sends a list of scancodes to the keyboard.
139
* @returns COM status code
140
* @param scancodes Pointer to the first scancode
141
* @param count Number of scancodes
142
* @param codesStored Address of variable to store the number
143
* of scancodes that were sent to the keyboard.
144
This value can be NULL.
146
STDMETHODIMP Keyboard::PutScancodes(LONG *scancodes,
153
AutoLock alock (this);
156
CHECK_CONSOLE_DRV (mpDrv);
158
LONG *currentScancode = scancodes;
159
int rcVBox = VINF_SUCCESS;
161
for (uint32_t i = 0; (i < count) && VBOX_SUCCESS(rcVBox); i++, currentScancode++)
163
rcVBox = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, *(uint8_t*)currentScancode);
166
if (VBOX_FAILURE (rcVBox))
167
return setError (E_FAIL,
168
tr ("Could not send all scan codes to the virtual keyboard (%Vrc)"), rcVBox);
170
/// @todo is it actually possible that not all scancodes can be transmitted?
172
*codesStored = count;
178
* Sends Control-Alt-Delete to the keyboard. This could be done otherwise
179
* but it's so common that we'll be nice and supply a convenience API.
181
* @returns COM status code
184
STDMETHODIMP Keyboard::PutCAD()
186
static LONG cadSequence[] = {
195
return PutScancodes (cadSequence, ELEMENTS (cadSequence), NULL);
203
* Queries an interface to the driver.
205
* @returns Pointer to interface.
206
* @returns NULL if the interface was not supported by the driver.
207
* @param pInterface Pointer to this interface structure.
208
* @param enmInterface The requested interface identification.
210
DECLCALLBACK(void *) Keyboard::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
212
PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
213
PDRVMAINKEYBOARD pDrv = PDMINS2DATA(pDrvIns, PDRVMAINKEYBOARD);
214
switch (enmInterface)
216
case PDMINTERFACE_BASE:
217
return &pDrvIns->IBase;
218
case PDMINTERFACE_KEYBOARD_CONNECTOR:
219
return &pDrv->Connector;
227
* Destruct a keyboard driver instance.
229
* @returns VBox status.
230
* @param pDrvIns The driver instance data.
232
DECLCALLBACK(void) Keyboard::drvDestruct(PPDMDRVINS pDrvIns)
234
PDRVMAINKEYBOARD pData = PDMINS2DATA(pDrvIns, PDRVMAINKEYBOARD);
235
LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
236
if (pData->pKeyboard)
238
AutoLock kbdLock (pData->pKeyboard);
239
pData->pKeyboard->mpDrv = NULL;
240
pData->pKeyboard->mpVMMDev = NULL;
244
DECLCALLBACK(void) keyboardLedStatusChange(PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
246
PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface);
247
pDrv->pKeyboard->getParent()->onKeyboardLedsChange(!!(enmLeds & PDMKEYBLEDS_NUMLOCK),
248
!!(enmLeds & PDMKEYBLEDS_CAPSLOCK),
249
!!(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
253
* Construct a keyboard driver instance.
255
* @returns VBox status.
256
* @param pDrvIns The driver instance data.
257
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
258
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
259
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
260
* iInstance it's expected to be used a bit in this function.
262
DECLCALLBACK(int) Keyboard::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
264
PDRVMAINKEYBOARD pData = PDMINS2DATA(pDrvIns, PDRVMAINKEYBOARD);
265
LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
268
* Validate configuration.
270
if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
271
return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
272
PPDMIBASE pBaseIgnore;
273
int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
274
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
276
AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
277
return VERR_PDM_DRVINS_NO_ATTACH;
283
pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
285
pData->Connector.pfnLedStatusChange = keyboardLedStatusChange;
288
* Get the IKeyboardPort interface of the above driver/device.
290
pData->pUpPort = (PPDMIKEYBOARDPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_KEYBOARD_PORT);
293
AssertMsgFailed(("Configuration error: No keyboard port interface above!\n"));
294
return VERR_PDM_MISSING_INTERFACE_ABOVE;
298
* Get the Keyboard object pointer and update the mpDrv member.
301
rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
302
if (VBOX_FAILURE(rc))
304
AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
307
pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
308
pData->pKeyboard->mpDrv = pData;
315
* Keyboard driver registration record.
317
const PDMDRVREG Keyboard::DrvReg =
324
"Main keyboard driver (Main as in the API).",
326
PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
328
PDM_DRVREG_CLASS_KEYBOARD,
332
sizeof(DRVMAINKEYBOARD),
334
Keyboard::drvConstruct,
336
Keyboard::drvDestruct,