VirtualBox

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

Last change on this file since 98046 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1/* $Id: KeyboardImpl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_MAIN_KEYBOARD
29#include "LoggingNew.h"
30
31#include "KeyboardImpl.h"
32#include "ConsoleImpl.h"
33
34#include "AutoCaller.h"
35#include "VBoxEvents.h"
36
37#include <VBox/com/array.h>
38#include <VBox/vmm/pdmdrv.h>
39#include <VBox/err.h>
40
41#include <iprt/cpp/utils.h>
42
43
44// defines
45////////////////////////////////////////////////////////////////////////////////
46
47// globals
48////////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Keyboard device capabilities bitfield.
52 */
53enum
54{
55 /** The keyboard device does not wish to receive keystrokes. */
56 KEYBOARD_DEVCAP_DISABLED = 0,
57 /** The keyboard device does wishes to receive keystrokes. */
58 KEYBOARD_DEVCAP_ENABLED = 1
59};
60
61/**
62 * Keyboard driver instance data.
63 */
64typedef struct DRVMAINKEYBOARD
65{
66 /** Pointer to the keyboard object. */
67 Keyboard *pKeyboard;
68 /** Pointer to the driver instance structure. */
69 PPDMDRVINS pDrvIns;
70 /** Pointer to the keyboard port interface of the driver/device above us. */
71 PPDMIKEYBOARDPORT pUpPort;
72 /** Our keyboard connector interface. */
73 PDMIKEYBOARDCONNECTOR IConnector;
74 /** The capabilities of this device. */
75 uint32_t u32DevCaps;
76} DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
77
78
79// constructor / destructor
80////////////////////////////////////////////////////////////////////////////////
81
82Keyboard::Keyboard()
83 : mParent(NULL)
84{
85}
86
87Keyboard::~Keyboard()
88{
89}
90
91HRESULT Keyboard::FinalConstruct()
92{
93 RT_ZERO(mpDrv);
94 menmLeds = PDMKEYBLEDS_NONE;
95 return BaseFinalConstruct();
96}
97
98void Keyboard::FinalRelease()
99{
100 uninit();
101 BaseFinalRelease();
102}
103
104// public methods
105////////////////////////////////////////////////////////////////////////////////
106
107/**
108 * Initializes the keyboard object.
109 *
110 * @returns COM result indicator
111 * @param aParent handle of our parent object
112 */
113HRESULT Keyboard::init(Console *aParent)
114{
115 LogFlowThisFunc(("aParent=%p\n", aParent));
116
117 ComAssertRet(aParent, E_INVALIDARG);
118
119 /* Enclose the state transition NotReady->InInit->Ready */
120 AutoInitSpan autoInitSpan(this);
121 AssertReturn(autoInitSpan.isOk(), E_FAIL);
122
123 unconst(mParent) = aParent;
124
125 unconst(mEventSource).createObject();
126 HRESULT hrc = mEventSource->init();
127 AssertComRCReturnRC(hrc);
128
129 /* Confirm a successful initialization */
130 autoInitSpan.setSucceeded();
131
132 return S_OK;
133}
134
135/**
136 * Uninitializes the instance and sets the ready flag to FALSE.
137 * Called either from FinalRelease() or by the parent when it gets destroyed.
138 */
139void Keyboard::uninit()
140{
141 LogFlowThisFunc(("\n"));
142
143 /* Enclose the state transition Ready->InUninit->NotReady */
144 AutoUninitSpan autoUninitSpan(this);
145 if (autoUninitSpan.uninitDone())
146 return;
147
148 for (unsigned i = 0; i < KEYBOARD_MAX_DEVICES; ++i)
149 {
150 if (mpDrv[i])
151 mpDrv[i]->pKeyboard = NULL;
152 mpDrv[i] = NULL;
153 }
154
155 menmLeds = PDMKEYBLEDS_NONE;
156
157 unconst(mParent) = NULL;
158 unconst(mEventSource).setNull();
159}
160
161/**
162 * Sends a scancode to the keyboard.
163 *
164 * @returns COM status code
165 * @param aScancode The scancode to send
166 */
167HRESULT Keyboard::putScancode(LONG aScancode)
168{
169 std::vector<LONG> scancodes;
170 scancodes.resize(1);
171 scancodes[0] = aScancode;
172 return putScancodes(scancodes, NULL);
173}
174
175/**
176 * Sends a list of scancodes to the keyboard.
177 *
178 * @returns COM status code
179 * @param aScancodes Pointer to the first scancode
180 * @param aCodesStored Address of variable to store the number
181 * of scancodes that were sent to the keyboard.
182 This value can be NULL.
183 */
184HRESULT Keyboard::putScancodes(const std::vector<LONG> &aScancodes,
185 ULONG *aCodesStored)
186{
187 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
188
189 CHECK_CONSOLE_DRV(mpDrv[0]);
190
191 /* Send input to the last enabled device. Relies on the fact that
192 * the USB keyboard is always initialized after the PS/2 keyboard.
193 */
194 PPDMIKEYBOARDPORT pUpPort = NULL;
195 for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
196 {
197 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
198 {
199 pUpPort = mpDrv[i]->pUpPort;
200 break;
201 }
202 }
203
204 /* No enabled keyboard - throw the input away. */
205 if (!pUpPort)
206 {
207 if (aCodesStored)
208 *aCodesStored = (uint32_t)aScancodes.size();
209 return S_OK;
210 }
211
212 int vrc = VINF_SUCCESS;
213
214 uint32_t sent;
215 for (sent = 0; (sent < aScancodes.size()) && RT_SUCCESS(vrc); ++sent)
216 vrc = pUpPort->pfnPutEventScan(pUpPort, (uint8_t)aScancodes[sent]);
217
218 if (aCodesStored)
219 *aCodesStored = sent;
220
221 com::SafeArray<LONG> keys(aScancodes.size());
222 for (size_t i = 0; i < aScancodes.size(); ++i)
223 keys[i] = aScancodes[i];
224
225 ::FireGuestKeyboardEvent(mEventSource, ComSafeArrayAsInParam(keys));
226
227 if (RT_FAILURE(vrc))
228 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
229 tr("Could not send all scan codes to the virtual keyboard (%Rrc)"),
230 vrc);
231
232 return S_OK;
233}
234
235/**
236 * Sends a HID usage code and page to the keyboard.
237 *
238 * @returns COM status code
239 * @param aUsageCode The HID usage code to send
240 * @param aUsagePage The HID usage page corresponding to the code
241 * @param fKeyRelease The key release flag
242 */
243HRESULT Keyboard::putUsageCode(LONG aUsageCode, LONG aUsagePage, BOOL fKeyRelease)
244{
245 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
246
247 CHECK_CONSOLE_DRV(mpDrv[0]);
248
249 /* Send input to the last enabled device. Relies on the fact that
250 * the USB keyboard is always initialized after the PS/2 keyboard.
251 */
252 PPDMIKEYBOARDPORT pUpPort = NULL;
253 for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
254 {
255 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
256 {
257 pUpPort = mpDrv[i]->pUpPort;
258 break;
259 }
260 }
261
262 /* No enabled keyboard - throw the input away. */
263 if (!pUpPort)
264 return S_OK;
265
266 uint32_t idUsage = (uint16_t)aUsageCode | ((uint32_t)(uint8_t)aUsagePage << 16) | (fKeyRelease ? UINT32_C(0x80000000) : 0);
267 int vrc = pUpPort->pfnPutEventHid(pUpPort, idUsage);
268 if (RT_FAILURE(vrc))
269 return setErrorBoth(VBOX_E_IPRT_ERROR, vrc,
270 tr("Could not send usage code to the virtual keyboard (%Rrc)"),
271 vrc);
272
273 return S_OK;
274}
275
276/**
277 * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
278 * but it's so common that we'll be nice and supply a convenience API.
279 *
280 * @returns COM status code
281 *
282 */
283HRESULT Keyboard::putCAD()
284{
285 std::vector<LONG> cadSequence;
286 cadSequence.resize(8);
287
288 cadSequence[0] = 0x1d; // Ctrl down
289 cadSequence[1] = 0x38; // Alt down
290 cadSequence[2] = 0xe0; // Del down 1
291 cadSequence[3] = 0x53; // Del down 2
292 cadSequence[4] = 0xe0; // Del up 1
293 cadSequence[5] = 0xd3; // Del up 2
294 cadSequence[6] = 0xb8; // Alt up
295 cadSequence[7] = 0x9d; // Ctrl up
296
297 return putScancodes(cadSequence, NULL);
298}
299
300/**
301 * Releases all currently held keys in the virtual keyboard.
302 *
303 * @returns COM status code
304 *
305 */
306HRESULT Keyboard::releaseKeys()
307{
308 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
309
310 /* Release all keys on the active keyboard in order to start with a clean slate.
311 * Note that this should mirror the logic in Keyboard::putScancodes() when choosing
312 * which keyboard to send the release event to.
313 */
314 PPDMIKEYBOARDPORT pUpPort = NULL;
315 for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
316 {
317 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
318 {
319 pUpPort = mpDrv[i]->pUpPort;
320 break;
321 }
322 }
323
324 if (pUpPort)
325 {
326 int vrc = pUpPort->pfnReleaseKeys(pUpPort);
327 if (RT_FAILURE(vrc))
328 AssertMsgFailed(("Failed to release keys on all keyboards! vrc=%Rrc\n", vrc));
329 }
330
331 return S_OK;
332}
333
334HRESULT Keyboard::getKeyboardLEDs(std::vector<KeyboardLED_T> &aKeyboardLEDs)
335{
336 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
337
338 aKeyboardLEDs.resize(0);
339
340 if (menmLeds & PDMKEYBLEDS_NUMLOCK) aKeyboardLEDs.push_back(KeyboardLED_NumLock);
341 if (menmLeds & PDMKEYBLEDS_CAPSLOCK) aKeyboardLEDs.push_back(KeyboardLED_CapsLock);
342 if (menmLeds & PDMKEYBLEDS_SCROLLLOCK) aKeyboardLEDs.push_back(KeyboardLED_ScrollLock);
343
344 return S_OK;
345}
346
347HRESULT Keyboard::getEventSource(ComPtr<IEventSource> &aEventSource)
348{
349 // No need to lock - lifetime constant
350 mEventSource.queryInterfaceTo(aEventSource.asOutParam());
351
352 return S_OK;
353}
354
355//
356// private methods
357//
358void Keyboard::onKeyboardLedsChange(PDMKEYBLEDS enmLeds)
359{
360 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
361
362 /* Save the current status. */
363 menmLeds = enmLeds;
364
365 alock.release();
366
367 i_getParent()->i_onKeyboardLedsChange(RT_BOOL(enmLeds & PDMKEYBLEDS_NUMLOCK),
368 RT_BOOL(enmLeds & PDMKEYBLEDS_CAPSLOCK),
369 RT_BOOL(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
370}
371
372DECLCALLBACK(void) Keyboard::i_keyboardLedStatusChange(PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
373{
374 PDRVMAINKEYBOARD pDrv = RT_FROM_MEMBER(pInterface, DRVMAINKEYBOARD, IConnector);
375 pDrv->pKeyboard->onKeyboardLedsChange(enmLeds);
376}
377
378/**
379 * @interface_method_impl{PDMIKEYBOARDCONNECTOR,pfnSetActive}
380 */
381DECLCALLBACK(void) Keyboard::i_keyboardSetActive(PPDMIKEYBOARDCONNECTOR pInterface, bool fActive)
382{
383 PDRVMAINKEYBOARD pDrv = RT_FROM_MEMBER(pInterface, DRVMAINKEYBOARD, IConnector);
384
385 // Before activating a different keyboard, release all keys on the currently active one.
386 if (fActive)
387 pDrv->pKeyboard->releaseKeys();
388
389 if (fActive)
390 pDrv->u32DevCaps |= KEYBOARD_DEVCAP_ENABLED;
391 else
392 pDrv->u32DevCaps &= ~KEYBOARD_DEVCAP_ENABLED;
393}
394
395
396/**
397 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
398 */
399DECLCALLBACK(void *) Keyboard::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
400{
401 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
402 PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
403
404 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
405 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDCONNECTOR, &pDrv->IConnector);
406 return NULL;
407}
408
409
410/**
411 * Destruct a keyboard driver instance.
412 *
413 * @returns VBox status code.
414 * @param pDrvIns The driver instance data.
415 */
416DECLCALLBACK(void) Keyboard::i_drvDestruct(PPDMDRVINS pDrvIns)
417{
418 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
419 PDRVMAINKEYBOARD pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
420 LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
421
422 if (pThis->pKeyboard)
423 {
424 AutoWriteLock kbdLock(pThis->pKeyboard COMMA_LOCKVAL_SRC_POS);
425 for (unsigned cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
426 if (pThis->pKeyboard->mpDrv[cDev] == pThis)
427 {
428 pThis->pKeyboard->mpDrv[cDev] = NULL;
429 break;
430 }
431 }
432}
433
434/**
435 * Construct a keyboard driver instance.
436 *
437 * @copydoc FNPDMDRVCONSTRUCT
438 */
439DECLCALLBACK(int) Keyboard::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
440{
441 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
442 RT_NOREF(fFlags, pCfg);
443 PDRVMAINKEYBOARD pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
444 LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
445
446 /*
447 * Validate configuration.
448 */
449 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
450 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
451 ("Configuration error: Not possible to attach anything to this driver!\n"),
452 VERR_PDM_DRVINS_NO_ATTACH);
453
454 /*
455 * IBase.
456 */
457 pDrvIns->IBase.pfnQueryInterface = Keyboard::i_drvQueryInterface;
458
459 pThis->IConnector.pfnLedStatusChange = i_keyboardLedStatusChange;
460 pThis->IConnector.pfnSetActive = Keyboard::i_keyboardSetActive;
461
462 /*
463 * Get the IKeyboardPort interface of the above driver/device.
464 */
465 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIKEYBOARDPORT);
466 if (!pThis->pUpPort)
467 {
468 AssertMsgFailed(("Configuration error: No keyboard port interface above!\n"));
469 return VERR_PDM_MISSING_INTERFACE_ABOVE;
470 }
471
472 /*
473 * Get the Keyboard object pointer and update the mpDrv member.
474 */
475 com::Guid uuid(COM_IIDOF(IKeyboard));
476 IKeyboard *pIKeyboard = (IKeyboard *)PDMDrvHlpQueryGenericUserObject(pDrvIns, uuid.raw());
477 if (!pIKeyboard)
478 {
479 AssertMsgFailed(("Configuration error: No/bad Keyboard object!\n"));
480 return VERR_NOT_FOUND;
481 }
482 pThis->pKeyboard = static_cast<Keyboard *>(pIKeyboard);
483
484 unsigned cDev;
485 for (cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
486 if (!pThis->pKeyboard->mpDrv[cDev])
487 {
488 pThis->pKeyboard->mpDrv[cDev] = pThis;
489 break;
490 }
491 if (cDev == KEYBOARD_MAX_DEVICES)
492 return VERR_NO_MORE_HANDLES;
493
494 return VINF_SUCCESS;
495}
496
497
498/**
499 * Keyboard driver registration record.
500 */
501const PDMDRVREG Keyboard::DrvReg =
502{
503 /* u32Version */
504 PDM_DRVREG_VERSION,
505 /* szName */
506 "MainKeyboard",
507 /* szRCMod */
508 "",
509 /* szR0Mod */
510 "",
511 /* pszDescription */
512 "Main keyboard driver (Main as in the API).",
513 /* fFlags */
514 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
515 /* fClass. */
516 PDM_DRVREG_CLASS_KEYBOARD,
517 /* cMaxInstances */
518 ~0U,
519 /* cbInstance */
520 sizeof(DRVMAINKEYBOARD),
521 /* pfnConstruct */
522 Keyboard::i_drvConstruct,
523 /* pfnDestruct */
524 Keyboard::i_drvDestruct,
525 /* pfnRelocate */
526 NULL,
527 /* pfnIOCtl */
528 NULL,
529 /* pfnPowerOn */
530 NULL,
531 /* pfnReset */
532 NULL,
533 /* pfnSuspend */
534 NULL,
535 /* pfnResume */
536 NULL,
537 /* pfnAttach */
538 NULL,
539 /* pfnDetach */
540 NULL,
541 /* pfnPowerOff */
542 NULL,
543 /* pfnSoftReset */
544 NULL,
545 /* u32EndVersion */
546 PDM_DRVREG_VERSION
547};
548/* 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