VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/KeyboardImpl.cpp@ 79133

Last change on this file since 79133 was 76760, checked in by vboxsync, 6 years ago

Main/Recording: thin out the excessive includes so that it doesn't hide missing includes in totally unrelated code, and adjust that code to do the right thing

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette