KeyboardImpl.cpp revision 390b6e3a16f6886c75ddcd7d94481b4065bc1e85
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/** @file
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * VBox frontends: Basic Frontend (BFE):
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Implementation of Keyboard class and related things
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/*
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * available from http://www.virtualbox.org. This file is free software;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * you can redistribute it and/or modify it under the terms of the GNU
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * General Public License (GPL) as published by the Free Software
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * additional information or have any questions.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#ifdef VBOXBFE_WITHOUT_COM
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# include "COMDefs.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#else
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync# include <VBox/com/defs.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync//# include <VBox/com/array.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#endif
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/pdm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/cfgm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/err.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/assert.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <VBox/log.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include <iprt/asm.h>
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync#include "KeyboardImpl.h"
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync// defines
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync////////////////////////////////////////////////////////////////////////////////
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync// globals
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync////////////////////////////////////////////////////////////////////////////////
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Keyboard driver instance data.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsynctypedef struct DRVMAINKEYBOARD
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Pointer to the keyboard object. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync Keyboard *pKeyboard;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Pointer to the driver instance structure. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPDMDRVINS pDrvIns;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Pointer to the keyboard port interface of the driver/device above us. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PPDMIKEYBOARDPORT pUpPort;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /** Our mouse connector interface. */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync PDMIKEYBOARDCONNECTOR Connector;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync} DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync// constructor / destructor
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync////////////////////////////////////////////////////////////////////////////////
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncKeyboard::Keyboard()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mpDrv = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mpVMMDev = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mfVMMDevInited = false;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncKeyboard::~Keyboard()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (mpDrv)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mpDrv->pKeyboard = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mpDrv = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mpVMMDev = NULL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync mfVMMDevInited = true;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync// public methods
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync////////////////////////////////////////////////////////////////////////////////
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Sends a scancode to the keyboard.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns COM status code
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param scancode The scancode to send
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncSTDMETHODIMP Keyboard::PutScancode(LONG scancode)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!mpDrv)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return S_OK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rcVBox = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, (uint8_t)scancode);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (VBOX_FAILURE (rcVBox))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return E_FAIL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return S_OK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Sends a list of scancodes to the keyboard.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns COM status code
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param scancodes Safe array of scancodes
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @param codesStored Address of variable to store the number
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * of scancodes that were sent to the keyboard.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync This value can be NULL.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncSTDMETHODIMP Keyboard::PutScancodes(ComSafeArrayIn (LONG, scancodes),
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync ULONG *codesStored)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (ComSafeArrayInIsNull(scancodes))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return E_INVALIDARG;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (!mpDrv)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return S_OK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync com::SafeArray <LONG> keys(ComSafeArrayInArg(scancodes));
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync int rcVBox = VINF_SUCCESS;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync for (uint32_t i = 0; (i < keys.size()) && VBOX_SUCCESS(rcVBox); i++)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync {
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync rcVBox = mpDrv->pUpPort->pfnPutEvent(mpDrv->pUpPort, (uint8_t)keys[i]);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync }
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (VBOX_FAILURE (rcVBox))
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return E_FAIL;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync /// @todo is it actually possible that not all scancodes can be transmitted?
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync if (codesStored)
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *codesStored = keys.size();
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return S_OK;
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync/**
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * but it's so common that we'll be nice and supply a convenience API.
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync * @returns COM status code
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync *
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync */
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsyncSTDMETHODIMP Keyboard::PutCAD()
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync{
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync static com::SafeArray<LONG> cadSequence(6);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[0] = 0x1d; // Ctrl down
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[1] = 0x38; // Alt down
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[2] = 0x53; // Del down
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[3] = 0xd3; // Del up
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[4] = 0xb8; // Alt up
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync cadSequence[5] = 0x9d; // Ctrl up
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync return PutScancodes (ComSafeArrayAsInParam(cadSequence), NULL);
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync}
0b74a2f80aba476dc8be8bc1c63891fc53945986vboxsync
//
// private methods
//
/**
* Queries an interface to the driver.
*
* @returns Pointer to interface.
* @returns NULL if the interface was not supported by the driver.
* @param pInterface Pointer to this interface structure.
* @param enmInterface The requested interface identification.
*/
DECLCALLBACK(void *) Keyboard::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
{
PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
switch (enmInterface)
{
case PDMINTERFACE_BASE:
return &pDrvIns->IBase;
case PDMINTERFACE_KEYBOARD_CONNECTOR:
return &pDrv->Connector;
default:
return NULL;
}
}
/**
* Destruct a keyboard driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
*/
DECLCALLBACK(void) Keyboard::drvDestruct(PPDMDRVINS pDrvIns)
{
PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
if (pData->pKeyboard)
{
pData->pKeyboard->mpDrv = NULL;
pData->pKeyboard->mpVMMDev = NULL;
}
}
/** @copydoc PDMIKEYBOARDCONNECTOR::pfnLedStatusChange */
DECLCALLBACK(void) keyboardLedStatusChange(PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
{
/** @todo Implement me. */
}
/**
* Construct a keyboard driver instance.
*
* @returns VBox status.
* @param pDrvIns The driver instance data.
* If the registration structure is needed, pDrvIns->pDrvReg points to it.
* @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
* of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
* iInstance it's expected to be used a bit in this function.
*/
DECLCALLBACK(int) Keyboard::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
{
PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
/*
* Validate configuration.
*/
if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
PPDMIBASE pBaseIgnore;
int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
{
AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
return VERR_PDM_DRVINS_NO_ATTACH;
}
/*
* IBase.
*/
pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
pData->Connector.pfnLedStatusChange = keyboardLedStatusChange;
/*
* Get the IKeyboardPort interface of the above driver/device.
*/
pData->pUpPort = (PPDMIKEYBOARDPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_KEYBOARD_PORT);
if (!pData->pUpPort)
{
AssertMsgFailed(("Configuration error: No keyboard port interface above!\n"));
return VERR_PDM_MISSING_INTERFACE_ABOVE;
}
/*
* Get the Keyboard object pointer and update the mpDrv member.
*/
void *pv;
rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
if (VBOX_FAILURE(rc))
{
AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
return rc;
}
pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
pData->pKeyboard->mpDrv = pData;
return VINF_SUCCESS;
}
/**
* Keyboard driver registration record.
*/
const PDMDRVREG Keyboard::DrvReg =
{
/* u32Version */
PDM_DRVREG_VERSION,
/* szDriverName */
"MainKeyboard",
/* pszDescription */
"Main keyboard driver (Main as in the API).",
/* fFlags */
PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
/* fClass. */
PDM_DRVREG_CLASS_KEYBOARD,
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVMAINKEYBOARD),
/* pfnConstruct */
Keyboard::drvConstruct,
/* pfnDestruct */
Keyboard::drvDestruct,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
NULL,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnDetach */
NULL
};