[13607] | 1 | /* $Id: KeyboardImpl.cpp 25310 2009-12-10 17:06:44Z vboxsync $ */
|
---|
[13606] | 2 |
|
---|
[1] | 3 | /** @file
|
---|
| 4 | *
|
---|
| 5 | * VirtualBox COM class implementation
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
[13606] | 9 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
[1] | 10 | *
|
---|
| 11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 14 | * General Public License (GPL) as published by the Free Software
|
---|
| 15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[8155] | 18 | *
|
---|
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
| 20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
| 21 | * additional information or have any questions.
|
---|
[1] | 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "KeyboardImpl.h"
|
---|
| 25 | #include "ConsoleImpl.h"
|
---|
| 26 |
|
---|
| 27 | #include "Logging.h"
|
---|
| 28 |
|
---|
[11661] | 29 | #include <VBox/com/array.h>
|
---|
[4015] | 30 | #include <VBox/pdmdrv.h>
|
---|
[1] | 31 | #include <iprt/asm.h>
|
---|
| 32 |
|
---|
| 33 | // defines
|
---|
| 34 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 35 |
|
---|
| 36 | // globals
|
---|
| 37 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Keyboard driver instance data.
|
---|
| 41 | */
|
---|
| 42 | typedef struct DRVMAINKEYBOARD
|
---|
| 43 | {
|
---|
| 44 | /** Pointer to the keyboard object. */
|
---|
| 45 | Keyboard *pKeyboard;
|
---|
| 46 | /** Pointer to the driver instance structure. */
|
---|
| 47 | PPDMDRVINS pDrvIns;
|
---|
| 48 | /** Pointer to the keyboard port interface of the driver/device above us. */
|
---|
| 49 | PPDMIKEYBOARDPORT pUpPort;
|
---|
| 50 | /** Our mouse connector interface. */
|
---|
| 51 | PDMIKEYBOARDCONNECTOR Connector;
|
---|
| 52 | } DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
|
---|
| 53 |
|
---|
| 54 | /** Converts PDMIVMMDEVCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
|
---|
| 55 | #define PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface) ( (PDRVMAINKEYBOARD) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINKEYBOARD, Connector)) )
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | // constructor / destructor
|
---|
| 59 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 60 |
|
---|
[13606] | 61 | DEFINE_EMPTY_CTOR_DTOR (Keyboard)
|
---|
| 62 |
|
---|
[1] | 63 | HRESULT Keyboard::FinalConstruct()
|
---|
| 64 | {
|
---|
| 65 | mpDrv = NULL;
|
---|
| 66 | mpVMMDev = NULL;
|
---|
| 67 | mfVMMDevInited = false;
|
---|
| 68 | return S_OK;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void Keyboard::FinalRelease()
|
---|
| 72 | {
|
---|
[13606] | 73 | uninit();
|
---|
[1] | 74 | }
|
---|
| 75 |
|
---|
| 76 | // public methods
|
---|
| 77 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * Initializes the keyboard object.
|
---|
| 81 | *
|
---|
| 82 | * @returns COM result indicator
|
---|
| 83 | * @param parent handle of our parent object
|
---|
| 84 | */
|
---|
[13606] | 85 | HRESULT Keyboard::init (Console *aParent)
|
---|
[1] | 86 | {
|
---|
[21878] | 87 | LogFlowThisFunc(("aParent=%p\n", aParent));
|
---|
[1] | 88 |
|
---|
[13606] | 89 | ComAssertRet (aParent, E_INVALIDARG);
|
---|
[1] | 90 |
|
---|
[13606] | 91 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
[21878] | 92 | AutoInitSpan autoInitSpan(this);
|
---|
| 93 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
[1] | 94 |
|
---|
[21878] | 95 | unconst(mParent) = aParent;
|
---|
[1] | 96 |
|
---|
[13606] | 97 | /* Confirm a successful initialization */
|
---|
| 98 | autoInitSpan.setSucceeded();
|
---|
| 99 |
|
---|
[1] | 100 | return S_OK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
| 105 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
| 106 | */
|
---|
| 107 | void Keyboard::uninit()
|
---|
| 108 | {
|
---|
[21878] | 109 | LogFlowThisFunc(("\n"));
|
---|
[1] | 110 |
|
---|
[13606] | 111 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
[21878] | 112 | AutoUninitSpan autoUninitSpan(this);
|
---|
[13606] | 113 | if (autoUninitSpan.uninitDone())
|
---|
| 114 | return;
|
---|
[1] | 115 |
|
---|
| 116 | if (mpDrv)
|
---|
| 117 | mpDrv->pKeyboard = NULL;
|
---|
[13606] | 118 |
|
---|
[1] | 119 | mpDrv = NULL;
|
---|
| 120 | mpVMMDev = NULL;
|
---|
| 121 | mfVMMDevInited = true;
|
---|
| 122 |
|
---|
[21878] | 123 | unconst(mParent).setNull();
|
---|
[1] | 124 | }
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * Sends a scancode to the keyboard.
|
---|
| 128 | *
|
---|
| 129 | * @returns COM status code
|
---|
| 130 | * @param scancode The scancode to send
|
---|
| 131 | */
|
---|
[13606] | 132 | STDMETHODIMP Keyboard::PutScancode (LONG scancode)
|
---|
[1] | 133 | {
|
---|
[13606] | 134 | HRESULT rc = S_OK;
|
---|
| 135 |
|
---|
[21878] | 136 | AutoCaller autoCaller(this);
|
---|
[25149] | 137 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[13606] | 138 |
|
---|
[25310] | 139 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 140 |
|
---|
| 141 | CHECK_CONSOLE_DRV (mpDrv);
|
---|
| 142 |
|
---|
[13606] | 143 | int vrc = mpDrv->pUpPort->pfnPutEvent (mpDrv->pUpPort, (uint8_t)scancode);
|
---|
[1] | 144 |
|
---|
[21878] | 145 | if (RT_FAILURE(vrc))
|
---|
[15715] | 146 | rc = setError (VBOX_E_IPRT_ERROR,
|
---|
| 147 | tr ("Could not send scan code 0x%08X to the virtual keyboard (%Rrc)"),
|
---|
| 148 | scancode, vrc);
|
---|
[1] | 149 |
|
---|
[13606] | 150 | return rc;
|
---|
[1] | 151 | }
|
---|
| 152 |
|
---|
| 153 | /**
|
---|
| 154 | * Sends a list of scancodes to the keyboard.
|
---|
| 155 | *
|
---|
| 156 | * @returns COM status code
|
---|
| 157 | * @param scancodes Pointer to the first scancode
|
---|
| 158 | * @param count Number of scancodes
|
---|
| 159 | * @param codesStored Address of variable to store the number
|
---|
| 160 | * of scancodes that were sent to the keyboard.
|
---|
| 161 | This value can be NULL.
|
---|
| 162 | */
|
---|
[13606] | 163 | STDMETHODIMP Keyboard::PutScancodes (ComSafeArrayIn (LONG, scancodes),
|
---|
| 164 | ULONG *codesStored)
|
---|
[1] | 165 | {
|
---|
[13606] | 166 | HRESULT rc = S_OK;
|
---|
| 167 |
|
---|
| 168 | if (ComSafeArrayInIsNull (scancodes))
|
---|
[1] | 169 | return E_INVALIDARG;
|
---|
| 170 |
|
---|
[21878] | 171 | AutoCaller autoCaller(this);
|
---|
[25149] | 172 | if (FAILED(autoCaller.rc())) return autoCaller.rc();
|
---|
[13606] | 173 |
|
---|
[25310] | 174 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 175 |
|
---|
| 176 | CHECK_CONSOLE_DRV (mpDrv);
|
---|
| 177 |
|
---|
[21878] | 178 | com::SafeArray<LONG> keys (ComSafeArrayInArg (scancodes));
|
---|
[13606] | 179 | int vrc = VINF_SUCCESS;
|
---|
[1] | 180 |
|
---|
[21878] | 181 | for (uint32_t i = 0; (i < keys.size()) && RT_SUCCESS(vrc); i++)
|
---|
[13606] | 182 | vrc = mpDrv->pUpPort->pfnPutEvent (mpDrv->pUpPort, (uint8_t)keys [i]);
|
---|
[1] | 183 |
|
---|
[21878] | 184 | if (RT_FAILURE(vrc))
|
---|
[15715] | 185 | return setError (VBOX_E_IPRT_ERROR,
|
---|
| 186 | tr ("Could not send all scan codes to the virtual keyboard (%Rrc)"),
|
---|
| 187 | vrc);
|
---|
[1] | 188 |
|
---|
| 189 | /// @todo is it actually possible that not all scancodes can be transmitted?
|
---|
| 190 | if (codesStored)
|
---|
[14215] | 191 | *codesStored = (uint32_t)keys.size();
|
---|
[1] | 192 |
|
---|
[13606] | 193 | return rc;
|
---|
[1] | 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
|
---|
| 198 | * but it's so common that we'll be nice and supply a convenience API.
|
---|
| 199 | *
|
---|
| 200 | * @returns COM status code
|
---|
| 201 | *
|
---|
| 202 | */
|
---|
| 203 | STDMETHODIMP Keyboard::PutCAD()
|
---|
| 204 | {
|
---|
[13606] | 205 | static com::SafeArray<LONG> cadSequence (6);
|
---|
[1] | 206 |
|
---|
[13606] | 207 | cadSequence [0] = 0x1d; // Ctrl down
|
---|
| 208 | cadSequence [1] = 0x38; // Alt down
|
---|
| 209 | cadSequence [2] = 0x53; // Del down
|
---|
| 210 | cadSequence [3] = 0xd3; // Del up
|
---|
| 211 | cadSequence [4] = 0xb8; // Alt up
|
---|
| 212 | cadSequence [5] = 0x9d; // Ctrl up
|
---|
| 213 |
|
---|
| 214 | return PutScancodes (ComSafeArrayAsInParam (cadSequence), NULL);
|
---|
[1] | 215 | }
|
---|
| 216 |
|
---|
| 217 | //
|
---|
| 218 | // private methods
|
---|
| 219 | //
|
---|
| 220 |
|
---|
| 221 | /**
|
---|
| 222 | * Queries an interface to the driver.
|
---|
| 223 | *
|
---|
| 224 | * @returns Pointer to interface.
|
---|
| 225 | * @returns NULL if the interface was not supported by the driver.
|
---|
| 226 | * @param pInterface Pointer to this interface structure.
|
---|
| 227 | * @param enmInterface The requested interface identification.
|
---|
| 228 | */
|
---|
[13606] | 229 | DECLCALLBACK(void *) Keyboard::drvQueryInterface (PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
[1] | 230 | {
|
---|
[13606] | 231 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV (pInterface);
|
---|
| 232 | PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA (pDrvIns, PDRVMAINKEYBOARD);
|
---|
[1] | 233 | switch (enmInterface)
|
---|
| 234 | {
|
---|
| 235 | case PDMINTERFACE_BASE:
|
---|
| 236 | return &pDrvIns->IBase;
|
---|
| 237 | case PDMINTERFACE_KEYBOARD_CONNECTOR:
|
---|
| 238 | return &pDrv->Connector;
|
---|
| 239 | default:
|
---|
| 240 | return NULL;
|
---|
| 241 | }
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Destruct a keyboard driver instance.
|
---|
| 247 | *
|
---|
| 248 | * @returns VBox status.
|
---|
| 249 | * @param pDrvIns The driver instance data.
|
---|
| 250 | */
|
---|
[13606] | 251 | DECLCALLBACK(void) Keyboard::drvDestruct (PPDMDRVINS pDrvIns)
|
---|
[1] | 252 | {
|
---|
[11296] | 253 | PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
|
---|
[1] | 254 | LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
| 255 | if (pData->pKeyboard)
|
---|
| 256 | {
|
---|
[25310] | 257 | AutoWriteLock kbdLock(pData->pKeyboard COMMA_LOCKVAL_SRC_POS);
|
---|
[1] | 258 | pData->pKeyboard->mpDrv = NULL;
|
---|
| 259 | pData->pKeyboard->mpVMMDev = NULL;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[13606] | 263 | DECLCALLBACK(void) keyboardLedStatusChange (PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
|
---|
[1] | 264 | {
|
---|
[13606] | 265 | PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD (pInterface);
|
---|
| 266 | pDrv->pKeyboard->getParent()->onKeyboardLedsChange (!!(enmLeds & PDMKEYBLEDS_NUMLOCK),
|
---|
| 267 | !!(enmLeds & PDMKEYBLEDS_CAPSLOCK),
|
---|
| 268 | !!(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
|
---|
[1] | 269 | }
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Construct a keyboard driver instance.
|
---|
| 273 | *
|
---|
[22277] | 274 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
[1] | 275 | */
|
---|
[22277] | 276 | DECLCALLBACK(int) Keyboard::drvConstruct (PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
|
---|
[1] | 277 | {
|
---|
[13606] | 278 | PDRVMAINKEYBOARD pData = PDMINS_2_DATA (pDrvIns, PDRVMAINKEYBOARD);
|
---|
[1] | 279 | LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
| 280 |
|
---|
| 281 | /*
|
---|
| 282 | * Validate configuration.
|
---|
| 283 | */
|
---|
[13606] | 284 | if (!CFGMR3AreValuesValid (pCfgHandle, "Object\0"))
|
---|
[1] | 285 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
[25149] | 286 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
[22277] | 287 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
| 288 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
[1] | 289 |
|
---|
| 290 | /*
|
---|
| 291 | * IBase.
|
---|
| 292 | */
|
---|
| 293 | pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
|
---|
| 294 |
|
---|
| 295 | pData->Connector.pfnLedStatusChange = keyboardLedStatusChange;
|
---|
| 296 |
|
---|
| 297 | /*
|
---|
| 298 | * Get the IKeyboardPort interface of the above driver/device.
|
---|
| 299 | */
|
---|
[13606] | 300 | pData->pUpPort = (PPDMIKEYBOARDPORT)pDrvIns->pUpBase->pfnQueryInterface (pDrvIns->pUpBase, PDMINTERFACE_KEYBOARD_PORT);
|
---|
[1] | 301 | if (!pData->pUpPort)
|
---|
| 302 | {
|
---|
[13606] | 303 | AssertMsgFailed (("Configuration error: No keyboard port interface above!\n"));
|
---|
[1] | 304 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | /*
|
---|
| 308 | * Get the Keyboard object pointer and update the mpDrv member.
|
---|
| 309 | */
|
---|
| 310 | void *pv;
|
---|
[22277] | 311 | int rc = CFGMR3QueryPtr (pCfgHandle, "Object", &pv);
|
---|
[21878] | 312 | if (RT_FAILURE(rc))
|
---|
[1] | 313 | {
|
---|
[13837] | 314 | AssertMsgFailed (("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
|
---|
[1] | 315 | return rc;
|
---|
| 316 | }
|
---|
| 317 | pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
|
---|
| 318 | pData->pKeyboard->mpDrv = pData;
|
---|
| 319 |
|
---|
| 320 | return VINF_SUCCESS;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 |
|
---|
| 324 | /**
|
---|
| 325 | * Keyboard driver registration record.
|
---|
| 326 | */
|
---|
| 327 | const PDMDRVREG Keyboard::DrvReg =
|
---|
| 328 | {
|
---|
| 329 | /* u32Version */
|
---|
| 330 | PDM_DRVREG_VERSION,
|
---|
| 331 | /* szDriverName */
|
---|
| 332 | "MainKeyboard",
|
---|
| 333 | /* pszDescription */
|
---|
| 334 | "Main keyboard driver (Main as in the API).",
|
---|
| 335 | /* fFlags */
|
---|
| 336 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
| 337 | /* fClass. */
|
---|
| 338 | PDM_DRVREG_CLASS_KEYBOARD,
|
---|
| 339 | /* cMaxInstances */
|
---|
| 340 | ~0,
|
---|
| 341 | /* cbInstance */
|
---|
| 342 | sizeof(DRVMAINKEYBOARD),
|
---|
| 343 | /* pfnConstruct */
|
---|
| 344 | Keyboard::drvConstruct,
|
---|
| 345 | /* pfnDestruct */
|
---|
| 346 | Keyboard::drvDestruct,
|
---|
| 347 | /* pfnIOCtl */
|
---|
| 348 | NULL,
|
---|
| 349 | /* pfnPowerOn */
|
---|
| 350 | NULL,
|
---|
| 351 | /* pfnReset */
|
---|
| 352 | NULL,
|
---|
| 353 | /* pfnSuspend */
|
---|
| 354 | NULL,
|
---|
| 355 | /* pfnResume */
|
---|
| 356 | NULL,
|
---|
[22277] | 357 | /* pfnAttach */
|
---|
| 358 | NULL,
|
---|
[1] | 359 | /* pfnDetach */
|
---|
[25149] | 360 | NULL,
|
---|
[22277] | 361 | /* pfnPowerOff */
|
---|
[25149] | 362 | NULL,
|
---|
[22277] | 363 | /* pfnSoftReset */
|
---|
| 364 | NULL,
|
---|
| 365 | /* u32EndVersion */
|
---|
| 366 | PDM_DRVREG_VERSION
|
---|
[1] | 367 | };
|
---|
[14772] | 368 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|