VirtualBox

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

Last change on this file since 34948 was 34396, checked in by vboxsync, 14 years ago

one more

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