1 | /* $Id: KeyboardImpl.cpp 52400 2014-08-18 18:04:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 Oracle Corporation
|
---|
8 | *
|
---|
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 (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "KeyboardImpl.h"
|
---|
19 | #include "ConsoleImpl.h"
|
---|
20 |
|
---|
21 | #include "AutoCaller.h"
|
---|
22 | #include "Logging.h"
|
---|
23 |
|
---|
24 | #include <VBox/com/array.h>
|
---|
25 | #include <VBox/vmm/pdmdrv.h>
|
---|
26 |
|
---|
27 | #include <iprt/asm.h>
|
---|
28 | #include <iprt/cpp/utils.h>
|
---|
29 |
|
---|
30 | // defines
|
---|
31 | ////////////////////////////////////////////////////////////////////////////////
|
---|
32 |
|
---|
33 | // globals
|
---|
34 | ////////////////////////////////////////////////////////////////////////////////
|
---|
35 |
|
---|
36 | /** @name Keyboard device capabilities bitfield
|
---|
37 | * @{ */
|
---|
38 | enum
|
---|
39 | {
|
---|
40 | /** The keyboard device does not wish to receive keystrokes. */
|
---|
41 | KEYBOARD_DEVCAP_DISABLED = 0,
|
---|
42 | /** The keyboard device does wishes to receive keystrokes. */
|
---|
43 | KEYBOARD_DEVCAP_ENABLED = 1
|
---|
44 | };
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Keyboard driver instance data.
|
---|
48 | */
|
---|
49 | typedef struct DRVMAINKEYBOARD
|
---|
50 | {
|
---|
51 | /** Pointer to the keyboard object. */
|
---|
52 | Keyboard *pKeyboard;
|
---|
53 | /** Pointer to the driver instance structure. */
|
---|
54 | PPDMDRVINS pDrvIns;
|
---|
55 | /** Pointer to the keyboard port interface of the driver/device above us. */
|
---|
56 | PPDMIKEYBOARDPORT pUpPort;
|
---|
57 | /** Our keyboard connector interface. */
|
---|
58 | PDMIKEYBOARDCONNECTOR IConnector;
|
---|
59 | /** The capabilities of this device. */
|
---|
60 | uint32_t u32DevCaps;
|
---|
61 | } DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
|
---|
62 |
|
---|
63 |
|
---|
64 | // constructor / destructor
|
---|
65 | ////////////////////////////////////////////////////////////////////////////////
|
---|
66 |
|
---|
67 | Keyboard::Keyboard()
|
---|
68 | : mParent(NULL)
|
---|
69 | {
|
---|
70 | }
|
---|
71 |
|
---|
72 | Keyboard::~Keyboard()
|
---|
73 | {
|
---|
74 | }
|
---|
75 |
|
---|
76 | HRESULT Keyboard::FinalConstruct()
|
---|
77 | {
|
---|
78 | RT_ZERO(mpDrv);
|
---|
79 | mpVMMDev = NULL;
|
---|
80 | mfVMMDevInited = false;
|
---|
81 | return BaseFinalConstruct();
|
---|
82 | }
|
---|
83 |
|
---|
84 | void Keyboard::FinalRelease()
|
---|
85 | {
|
---|
86 | uninit();
|
---|
87 | BaseFinalRelease();
|
---|
88 | }
|
---|
89 |
|
---|
90 | // public methods
|
---|
91 | ////////////////////////////////////////////////////////////////////////////////
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Initializes the keyboard object.
|
---|
95 | *
|
---|
96 | * @returns COM result indicator
|
---|
97 | * @param parent handle of our parent object
|
---|
98 | */
|
---|
99 | HRESULT Keyboard::init(Console *aParent)
|
---|
100 | {
|
---|
101 | LogFlowThisFunc(("aParent=%p\n", aParent));
|
---|
102 |
|
---|
103 | ComAssertRet(aParent, E_INVALIDARG);
|
---|
104 |
|
---|
105 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
106 | AutoInitSpan autoInitSpan(this);
|
---|
107 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
108 |
|
---|
109 | unconst(mParent) = aParent;
|
---|
110 |
|
---|
111 | unconst(mEventSource).createObject();
|
---|
112 | HRESULT rc = mEventSource->init();
|
---|
113 | AssertComRCReturnRC(rc);
|
---|
114 |
|
---|
115 | /* Confirm a successful initialization */
|
---|
116 | autoInitSpan.setSucceeded();
|
---|
117 |
|
---|
118 | return S_OK;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
123 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
124 | */
|
---|
125 | void Keyboard::uninit()
|
---|
126 | {
|
---|
127 | LogFlowThisFunc(("\n"));
|
---|
128 |
|
---|
129 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
130 | AutoUninitSpan autoUninitSpan(this);
|
---|
131 | if (autoUninitSpan.uninitDone())
|
---|
132 | return;
|
---|
133 |
|
---|
134 | for (unsigned i = 0; i < KEYBOARD_MAX_DEVICES; ++i)
|
---|
135 | {
|
---|
136 | if (mpDrv[i])
|
---|
137 | mpDrv[i]->pKeyboard = NULL;
|
---|
138 | mpDrv[i] = NULL;
|
---|
139 | }
|
---|
140 |
|
---|
141 | mpVMMDev = NULL;
|
---|
142 | mfVMMDevInited = true;
|
---|
143 |
|
---|
144 | unconst(mParent) = NULL;
|
---|
145 | unconst(mEventSource).setNull();
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Sends a scancode to the keyboard.
|
---|
150 | *
|
---|
151 | * @returns COM status code
|
---|
152 | * @param aScancode The scancode to send
|
---|
153 | */
|
---|
154 | HRESULT Keyboard::putScancode(LONG aScancode)
|
---|
155 | {
|
---|
156 | std::vector<LONG> scancodes;
|
---|
157 | scancodes.resize(1);
|
---|
158 | scancodes[0] = aScancode;
|
---|
159 | return putScancodes(scancodes, NULL);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Sends a list of scancodes to the keyboard.
|
---|
164 | *
|
---|
165 | * @returns COM status code
|
---|
166 | * @param aScancodes Pointer to the first scancode
|
---|
167 | * @param aCodesStored Address of variable to store the number
|
---|
168 | * of scancodes that were sent to the keyboard.
|
---|
169 | This value can be NULL.
|
---|
170 | */
|
---|
171 | HRESULT Keyboard::putScancodes(const std::vector<LONG> &aScancodes,
|
---|
172 | ULONG *aCodesStored)
|
---|
173 | {
|
---|
174 | com::SafeArray<LONG> keys;
|
---|
175 | keys.resize(aScancodes.size());
|
---|
176 | for (size_t i = 0; i < aScancodes.size(); ++i)
|
---|
177 | keys[i] = aScancodes[i];
|
---|
178 |
|
---|
179 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
180 |
|
---|
181 | CHECK_CONSOLE_DRV(mpDrv[0]);
|
---|
182 |
|
---|
183 | /* Send input to the last enabled device. Relies on the fact that
|
---|
184 | * the USB keyboard is always initialized after the PS/2 keyboard.
|
---|
185 | */
|
---|
186 | PPDMIKEYBOARDPORT pUpPort = NULL;
|
---|
187 | for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
|
---|
188 | {
|
---|
189 | if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
|
---|
190 | {
|
---|
191 | pUpPort = mpDrv[i]->pUpPort;
|
---|
192 | break;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /* No enabled keyboard - throw the input away. */
|
---|
197 | if (!pUpPort)
|
---|
198 | {
|
---|
199 | if (aCodesStored)
|
---|
200 | *aCodesStored = (uint32_t)aScancodes.size();
|
---|
201 | return S_OK;
|
---|
202 | }
|
---|
203 |
|
---|
204 | int vrc = VINF_SUCCESS;
|
---|
205 |
|
---|
206 | uint32_t sent;
|
---|
207 | for (sent = 0; (sent < aScancodes.size()) && RT_SUCCESS(vrc); ++sent)
|
---|
208 | vrc = pUpPort->pfnPutEventScan(pUpPort, (uint8_t)aScancodes[sent]);
|
---|
209 |
|
---|
210 | if (aCodesStored)
|
---|
211 | *aCodesStored = sent;
|
---|
212 |
|
---|
213 | VBoxEventDesc evDesc;
|
---|
214 | evDesc.init(mEventSource, VBoxEventType_OnGuestKeyboard, ComSafeArrayAsInParam(keys));
|
---|
215 | evDesc.fire(0);
|
---|
216 |
|
---|
217 | if (RT_FAILURE(vrc))
|
---|
218 | return setError(VBOX_E_IPRT_ERROR,
|
---|
219 | tr("Could not send all scan codes to the virtual keyboard (%Rrc)"),
|
---|
220 | vrc);
|
---|
221 |
|
---|
222 | return S_OK;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
|
---|
227 | * but it's so common that we'll be nice and supply a convenience API.
|
---|
228 | *
|
---|
229 | * @returns COM status code
|
---|
230 | *
|
---|
231 | */
|
---|
232 | HRESULT Keyboard::putCAD()
|
---|
233 | {
|
---|
234 | static std::vector<LONG> cadSequence;
|
---|
235 | cadSequence.resize(8);
|
---|
236 |
|
---|
237 | cadSequence[0] = 0x1d; // Ctrl down
|
---|
238 | cadSequence[1] = 0x38; // Alt down
|
---|
239 | cadSequence[2] = 0xe0; // Del down 1
|
---|
240 | cadSequence[3] = 0x53; // Del down 2
|
---|
241 | cadSequence[4] = 0xe0; // Del up 1
|
---|
242 | cadSequence[5] = 0xd3; // Del up 2
|
---|
243 | cadSequence[6] = 0xb8; // Alt up
|
---|
244 | cadSequence[7] = 0x9d; // Ctrl up
|
---|
245 |
|
---|
246 | return putScancodes(cadSequence, NULL);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Releases all currently held keys in the virtual keyboard.
|
---|
251 | *
|
---|
252 | * @returns COM status code
|
---|
253 | *
|
---|
254 | */
|
---|
255 | HRESULT Keyboard::releaseKeys()
|
---|
256 | {
|
---|
257 | std::vector<LONG> scancodes;
|
---|
258 | scancodes.resize(1);
|
---|
259 | scancodes[0] = 0xFC; /* Magic scancode, see PS/2 and USB keyboard devices. */
|
---|
260 | return putScancodes(scancodes, NULL);
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | HRESULT Keyboard::getEventSource(ComPtr<IEventSource> &aEventSource)
|
---|
265 | {
|
---|
266 | // No need to lock - lifetime constant
|
---|
267 | mEventSource.queryInterfaceTo(aEventSource.asOutParam());
|
---|
268 |
|
---|
269 | return S_OK;
|
---|
270 | }
|
---|
271 |
|
---|
272 | //
|
---|
273 | // private methods
|
---|
274 | //
|
---|
275 | DECLCALLBACK(void) Keyboard::i_keyboardLedStatusChange(PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
|
---|
276 | {
|
---|
277 | PDRVMAINKEYBOARD pDrv = RT_FROM_MEMBER(pInterface, DRVMAINKEYBOARD, IConnector);
|
---|
278 | pDrv->pKeyboard->i_getParent()->i_onKeyboardLedsChange(RT_BOOL(enmLeds & PDMKEYBLEDS_NUMLOCK),
|
---|
279 | RT_BOOL(enmLeds & PDMKEYBLEDS_CAPSLOCK),
|
---|
280 | RT_BOOL(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * @interface_method_impl{PDMIKEYBOARDCONNECTOR,pfnSetActive}
|
---|
285 | */
|
---|
286 | DECLCALLBACK(void) Keyboard::i_keyboardSetActive(PPDMIKEYBOARDCONNECTOR pInterface, bool fActive)
|
---|
287 | {
|
---|
288 | PDRVMAINKEYBOARD pDrv = RT_FROM_MEMBER(pInterface, DRVMAINKEYBOARD, IConnector);
|
---|
289 | if (fActive)
|
---|
290 | pDrv->u32DevCaps |= KEYBOARD_DEVCAP_ENABLED;
|
---|
291 | else
|
---|
292 | pDrv->u32DevCaps &= ~KEYBOARD_DEVCAP_ENABLED;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /**
|
---|
297 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
298 | */
|
---|
299 | DECLCALLBACK(void *) Keyboard::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
300 | {
|
---|
301 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
302 | PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
|
---|
303 |
|
---|
304 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
305 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDCONNECTOR, &pDrv->IConnector);
|
---|
306 | return NULL;
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * Destruct a keyboard driver instance.
|
---|
312 | *
|
---|
313 | * @returns VBox status.
|
---|
314 | * @param pDrvIns The driver instance data.
|
---|
315 | */
|
---|
316 | DECLCALLBACK(void) Keyboard::i_drvDestruct(PPDMDRVINS pDrvIns)
|
---|
317 | {
|
---|
318 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
319 | PDRVMAINKEYBOARD pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
|
---|
320 | LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
321 |
|
---|
322 | if (pThis->pKeyboard)
|
---|
323 | {
|
---|
324 | AutoWriteLock kbdLock(pThis->pKeyboard COMMA_LOCKVAL_SRC_POS);
|
---|
325 | for (unsigned cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
|
---|
326 | if (pThis->pKeyboard->mpDrv[cDev] == pThis)
|
---|
327 | {
|
---|
328 | pThis->pKeyboard->mpDrv[cDev] = NULL;
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | pThis->pKeyboard->mpVMMDev = NULL;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * Construct a keyboard driver instance.
|
---|
337 | *
|
---|
338 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
339 | */
|
---|
340 | DECLCALLBACK(int) Keyboard::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
341 | {
|
---|
342 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
343 | PDRVMAINKEYBOARD pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
|
---|
344 | LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Validate configuration.
|
---|
348 | */
|
---|
349 | if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
|
---|
350 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
351 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
352 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
353 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
354 |
|
---|
355 | /*
|
---|
356 | * IBase.
|
---|
357 | */
|
---|
358 | pDrvIns->IBase.pfnQueryInterface = Keyboard::i_drvQueryInterface;
|
---|
359 |
|
---|
360 | pThis->IConnector.pfnLedStatusChange = i_keyboardLedStatusChange;
|
---|
361 | pThis->IConnector.pfnSetActive = Keyboard::i_keyboardSetActive;
|
---|
362 |
|
---|
363 | /*
|
---|
364 | * Get the IKeyboardPort interface of the above driver/device.
|
---|
365 | */
|
---|
366 | pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIKEYBOARDPORT);
|
---|
367 | if (!pThis->pUpPort)
|
---|
368 | {
|
---|
369 | AssertMsgFailed(("Configuration error: No keyboard port interface above!\n"));
|
---|
370 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /*
|
---|
374 | * Get the Keyboard object pointer and update the mpDrv member.
|
---|
375 | */
|
---|
376 | void *pv;
|
---|
377 | int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
|
---|
378 | if (RT_FAILURE(rc))
|
---|
379 | {
|
---|
380 | AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
|
---|
381 | return rc;
|
---|
382 | }
|
---|
383 | pThis->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
|
---|
384 | unsigned cDev;
|
---|
385 | for (cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
|
---|
386 | if (!pThis->pKeyboard->mpDrv[cDev])
|
---|
387 | {
|
---|
388 | pThis->pKeyboard->mpDrv[cDev] = pThis;
|
---|
389 | break;
|
---|
390 | }
|
---|
391 | if (cDev == KEYBOARD_MAX_DEVICES)
|
---|
392 | return VERR_NO_MORE_HANDLES;
|
---|
393 |
|
---|
394 | return VINF_SUCCESS;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | /**
|
---|
399 | * Keyboard driver registration record.
|
---|
400 | */
|
---|
401 | const PDMDRVREG Keyboard::DrvReg =
|
---|
402 | {
|
---|
403 | /* u32Version */
|
---|
404 | PDM_DRVREG_VERSION,
|
---|
405 | /* szName */
|
---|
406 | "MainKeyboard",
|
---|
407 | /* szRCMod */
|
---|
408 | "",
|
---|
409 | /* szR0Mod */
|
---|
410 | "",
|
---|
411 | /* pszDescription */
|
---|
412 | "Main keyboard driver (Main as in the API).",
|
---|
413 | /* fFlags */
|
---|
414 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
415 | /* fClass. */
|
---|
416 | PDM_DRVREG_CLASS_KEYBOARD,
|
---|
417 | /* cMaxInstances */
|
---|
418 | ~0U,
|
---|
419 | /* cbInstance */
|
---|
420 | sizeof(DRVMAINKEYBOARD),
|
---|
421 | /* pfnConstruct */
|
---|
422 | Keyboard::i_drvConstruct,
|
---|
423 | /* pfnDestruct */
|
---|
424 | Keyboard::i_drvDestruct,
|
---|
425 | /* pfnRelocate */
|
---|
426 | NULL,
|
---|
427 | /* pfnIOCtl */
|
---|
428 | NULL,
|
---|
429 | /* pfnPowerOn */
|
---|
430 | NULL,
|
---|
431 | /* pfnReset */
|
---|
432 | NULL,
|
---|
433 | /* pfnSuspend */
|
---|
434 | NULL,
|
---|
435 | /* pfnResume */
|
---|
436 | NULL,
|
---|
437 | /* pfnAttach */
|
---|
438 | NULL,
|
---|
439 | /* pfnDetach */
|
---|
440 | NULL,
|
---|
441 | /* pfnPowerOff */
|
---|
442 | NULL,
|
---|
443 | /* pfnSoftReset */
|
---|
444 | NULL,
|
---|
445 | /* u32EndVersion */
|
---|
446 | PDM_DRVREG_VERSION
|
---|
447 | };
|
---|
448 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|