VirtualBox

source: vbox/trunk/src/VBox/Main/KeyboardImpl.cpp@ 31525

Last change on this file since 31525 was 30681, checked in by vboxsync, 14 years ago

Main: COM header cleanup (remove obscure and unused templates), second try

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1/* $Id: KeyboardImpl.cpp 30681 2010-07-06 17:20:20Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2008 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/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 * @{ */
38enum
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 */
49typedef 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/** Converts PDMIVMMDEVCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
64#define PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface) ( (PDRVMAINKEYBOARD) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINKEYBOARD, IConnector)) )
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 mpVMMDev = NULL;
83 mfVMMDevInited = false;
84 return S_OK;
85}
86
87void Keyboard::FinalRelease()
88{
89 uninit();
90}
91
92// public methods
93////////////////////////////////////////////////////////////////////////////////
94
95/**
96 * Initializes the keyboard object.
97 *
98 * @returns COM result indicator
99 * @param parent 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 /* Confirm a successful initialization */
114 autoInitSpan.setSucceeded();
115
116 return S_OK;
117}
118
119/**
120 * Uninitializes the instance and sets the ready flag to FALSE.
121 * Called either from FinalRelease() or by the parent when it gets destroyed.
122 */
123void Keyboard::uninit()
124{
125 LogFlowThisFunc(("\n"));
126
127 /* Enclose the state transition Ready->InUninit->NotReady */
128 AutoUninitSpan autoUninitSpan(this);
129 if (autoUninitSpan.uninitDone())
130 return;
131
132 for (unsigned i = 0; i < KEYBOARD_MAX_DEVICES; ++i)
133 {
134 if (mpDrv[i])
135 mpDrv[i]->pKeyboard = NULL;
136 mpDrv[i] = NULL;
137 }
138
139 mpVMMDev = NULL;
140 mfVMMDevInited = true;
141
142 unconst(mParent) = NULL;
143}
144
145/**
146 * Sends a scancode to the keyboard.
147 *
148 * @returns COM status code
149 * @param scancode The scancode to send
150 */
151STDMETHODIMP Keyboard::PutScancode (LONG scancode)
152{
153 HRESULT rc = S_OK;
154
155 AutoCaller autoCaller(this);
156 if (FAILED(autoCaller.rc())) return autoCaller.rc();
157
158 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
159
160 CHECK_CONSOLE_DRV(mpDrv[0]);
161
162 PPDMIKEYBOARDPORT pUpPort = NULL;
163 for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
164 {
165 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
166 {
167 pUpPort = mpDrv[i]->pUpPort;
168 break;
169 }
170 }
171 /* No enabled keyboard - throw the input away. */
172 if (!pUpPort)
173 return rc;
174
175 int vrc = pUpPort->pfnPutEvent(pUpPort, (uint8_t)scancode);
176
177 if (RT_FAILURE(vrc))
178 rc = setError(VBOX_E_IPRT_ERROR,
179 tr("Could not send scan code 0x%08X to the virtual keyboard (%Rrc)"),
180 scancode, vrc);
181
182 return rc;
183}
184
185/**
186 * Sends a list of scancodes to the keyboard.
187 *
188 * @returns COM status code
189 * @param scancodes Pointer to the first scancode
190 * @param count Number of scancodes
191 * @param codesStored Address of variable to store the number
192 * of scancodes that were sent to the keyboard.
193 This value can be NULL.
194 */
195STDMETHODIMP Keyboard::PutScancodes (ComSafeArrayIn (LONG, scancodes),
196 ULONG *codesStored)
197{
198 HRESULT rc = S_OK;
199
200 if (ComSafeArrayInIsNull (scancodes))
201 return E_INVALIDARG;
202
203 AutoCaller autoCaller(this);
204 if (FAILED(autoCaller.rc())) return autoCaller.rc();
205
206 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
207
208 CHECK_CONSOLE_DRV(mpDrv[0]);
209
210 /* Send input to the last enabled device. Relies on the fact that
211 * the USB keyboard is always initialized after the PS/2 keyboard.
212 */
213 PPDMIKEYBOARDPORT pUpPort = NULL;
214 for (int i = KEYBOARD_MAX_DEVICES - 1; i >= 0 ; --i)
215 {
216 if (mpDrv[i] && (mpDrv[i]->u32DevCaps & KEYBOARD_DEVCAP_ENABLED))
217 {
218 pUpPort = mpDrv[i]->pUpPort;
219 break;
220 }
221 }
222 /* No enabled keyboard - throw the input away. */
223 if (!pUpPort)
224 return rc;
225
226 com::SafeArray<LONG> keys (ComSafeArrayInArg (scancodes));
227 int vrc = VINF_SUCCESS;
228
229 for (uint32_t i = 0; (i < keys.size()) && RT_SUCCESS(vrc); i++)
230 vrc = pUpPort->pfnPutEvent(pUpPort, (uint8_t)keys[i]);
231
232 if (RT_FAILURE(vrc))
233 return setError(VBOX_E_IPRT_ERROR,
234 tr("Could not send all scan codes to the virtual keyboard (%Rrc)"),
235 vrc);
236
237 /// @todo is it actually possible that not all scancodes can be transmitted?
238 if (codesStored)
239 *codesStored = (uint32_t)keys.size();
240
241 return rc;
242}
243
244/**
245 * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
246 * but it's so common that we'll be nice and supply a convenience API.
247 *
248 * @returns COM status code
249 *
250 */
251STDMETHODIMP Keyboard::PutCAD()
252{
253 static com::SafeArray<LONG> cadSequence(6);
254
255 cadSequence[0] = 0x1d; // Ctrl down
256 cadSequence[1] = 0x38; // Alt down
257 cadSequence[2] = 0x53; // Del down
258 cadSequence[3] = 0xd3; // Del up
259 cadSequence[4] = 0xb8; // Alt up
260 cadSequence[5] = 0x9d; // Ctrl up
261
262 return PutScancodes(ComSafeArrayAsInParam(cadSequence), NULL);
263}
264
265//
266// private methods
267//
268
269/**
270 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
271 */
272DECLCALLBACK(void *) Keyboard::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
273{
274 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
275 PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
276
277 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
278 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDCONNECTOR, &pDrv->IConnector);
279 return NULL;
280}
281
282
283/**
284 * Destruct a keyboard driver instance.
285 *
286 * @returns VBox status.
287 * @param pDrvIns The driver instance data.
288 */
289DECLCALLBACK(void) Keyboard::drvDestruct (PPDMDRVINS pDrvIns)
290{
291 PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
292 LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
293 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
294
295 if (pData->pKeyboard)
296 {
297 AutoWriteLock kbdLock(pData->pKeyboard COMMA_LOCKVAL_SRC_POS);
298 for (unsigned cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
299 if (pData->pKeyboard->mpDrv[cDev] == pData)
300 {
301 pData->pKeyboard->mpDrv[cDev] = NULL;
302 break;
303 }
304 pData->pKeyboard->mpVMMDev = NULL;
305 }
306}
307
308DECLCALLBACK(void) keyboardLedStatusChange (PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
309{
310 PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD (pInterface);
311 pDrv->pKeyboard->getParent()->onKeyboardLedsChange (!!(enmLeds & PDMKEYBLEDS_NUMLOCK),
312 !!(enmLeds & PDMKEYBLEDS_CAPSLOCK),
313 !!(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
314}
315
316/**
317 * @interface_method_impl{PDMIKEYBOARDCONNECTOR,pfnSetActive}
318 */
319DECLCALLBACK(void) Keyboard::keyboardSetActive(PPDMIKEYBOARDCONNECTOR pInterface, bool fActive)
320{
321 PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface);
322 if (fActive)
323 pDrv->u32DevCaps |= KEYBOARD_DEVCAP_ENABLED;
324 else
325 pDrv->u32DevCaps &= ~KEYBOARD_DEVCAP_ENABLED;
326}
327
328/**
329 * Construct a keyboard driver instance.
330 *
331 * @copydoc FNPDMDRVCONSTRUCT
332 */
333DECLCALLBACK(int) Keyboard::drvConstruct (PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
334{
335 PDRVMAINKEYBOARD pData = PDMINS_2_DATA (pDrvIns, PDRVMAINKEYBOARD);
336 LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
337 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
338
339 /*
340 * Validate configuration.
341 */
342 if (!CFGMR3AreValuesValid (pCfg, "Object\0"))
343 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
344 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
345 ("Configuration error: Not possible to attach anything to this driver!\n"),
346 VERR_PDM_DRVINS_NO_ATTACH);
347
348 /*
349 * IBase.
350 */
351 pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
352
353 pData->IConnector.pfnLedStatusChange = keyboardLedStatusChange;
354 pData->IConnector.pfnSetActive = keyboardSetActive;
355
356 /*
357 * Get the IKeyboardPort interface of the above driver/device.
358 */
359 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIKEYBOARDPORT);
360 if (!pData->pUpPort)
361 {
362 AssertMsgFailed (("Configuration error: No keyboard port interface above!\n"));
363 return VERR_PDM_MISSING_INTERFACE_ABOVE;
364 }
365
366 /*
367 * Get the Keyboard object pointer and update the mpDrv member.
368 */
369 void *pv;
370 int rc = CFGMR3QueryPtr (pCfg, "Object", &pv);
371 if (RT_FAILURE(rc))
372 {
373 AssertMsgFailed (("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
374 return rc;
375 }
376 pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
377 unsigned cDev;
378 for (cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
379 if (!pData->pKeyboard->mpDrv[cDev])
380 {
381 pData->pKeyboard->mpDrv[cDev] = pData;
382 break;
383 }
384 if (cDev == KEYBOARD_MAX_DEVICES)
385 return VERR_NO_MORE_HANDLES;
386
387 return VINF_SUCCESS;
388}
389
390
391/**
392 * Keyboard driver registration record.
393 */
394const PDMDRVREG Keyboard::DrvReg =
395{
396 /* u32Version */
397 PDM_DRVREG_VERSION,
398 /* szName */
399 "MainKeyboard",
400 /* szRCMod */
401 "",
402 /* szR0Mod */
403 "",
404 /* pszDescription */
405 "Main keyboard driver (Main as in the API).",
406 /* fFlags */
407 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
408 /* fClass. */
409 PDM_DRVREG_CLASS_KEYBOARD,
410 /* cMaxInstances */
411 ~0,
412 /* cbInstance */
413 sizeof(DRVMAINKEYBOARD),
414 /* pfnConstruct */
415 Keyboard::drvConstruct,
416 /* pfnDestruct */
417 Keyboard::drvDestruct,
418 /* pfnRelocate */
419 NULL,
420 /* pfnIOCtl */
421 NULL,
422 /* pfnPowerOn */
423 NULL,
424 /* pfnReset */
425 NULL,
426 /* pfnSuspend */
427 NULL,
428 /* pfnResume */
429 NULL,
430 /* pfnAttach */
431 NULL,
432 /* pfnDetach */
433 NULL,
434 /* pfnPowerOff */
435 NULL,
436 /* pfnSoftReset */
437 NULL,
438 /* u32EndVersion */
439 PDM_DRVREG_VERSION
440};
441/* 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