VirtualBox

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

Last change on this file since 34373 was 34303, checked in by vboxsync, 14 years ago

Main/Keyboard: cannot resize weakly attached safearrays, need to create a real copy

  • 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 34303 2010-11-23 17:39:20Z 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 VBoxEventDesc evDesc;
221 evDesc.init(mEventSource, VBoxEventType_OnGuestKeyboardEvent, ComSafeArrayAsInParam(keys));
222 evDesc.fire(0);
223
224 if (RT_FAILURE(vrc))
225 return setError(VBOX_E_IPRT_ERROR,
226 tr("Could not send all scan codes to the virtual keyboard (%Rrc)"),
227 vrc);
228
229 return S_OK;
230}
231
232/**
233 * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
234 * but it's so common that we'll be nice and supply a convenience API.
235 *
236 * @returns COM status code
237 *
238 */
239STDMETHODIMP Keyboard::PutCAD()
240{
241 static com::SafeArray<LONG> cadSequence(6);
242
243 cadSequence[0] = 0x1d; // Ctrl down
244 cadSequence[1] = 0x38; // Alt down
245 cadSequence[2] = 0x53; // Del down
246 cadSequence[3] = 0xd3; // Del up
247 cadSequence[4] = 0xb8; // Alt up
248 cadSequence[5] = 0x9d; // Ctrl up
249
250 return PutScancodes(ComSafeArrayAsInParam(cadSequence), NULL);
251}
252
253STDMETHODIMP Keyboard::COMGETTER(EventSource)(IEventSource ** aEventSource)
254{
255 CheckComArgOutPointerValid(aEventSource);
256
257 AutoCaller autoCaller(this);
258 if (FAILED(autoCaller.rc())) return autoCaller.rc();
259
260 // no need to lock - lifetime constant
261 mEventSource.queryInterfaceTo(aEventSource);
262
263 return S_OK;
264}
265
266//
267// private methods
268//
269
270/**
271 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
272 */
273DECLCALLBACK(void *) Keyboard::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
274{
275 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
276 PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
277
278 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
279 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIKEYBOARDCONNECTOR, &pDrv->IConnector);
280 return NULL;
281}
282
283
284/**
285 * Destruct a keyboard driver instance.
286 *
287 * @returns VBox status.
288 * @param pDrvIns The driver instance data.
289 */
290DECLCALLBACK(void) Keyboard::drvDestruct(PPDMDRVINS pDrvIns)
291{
292 PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
293 LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
294 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
295
296 if (pData->pKeyboard)
297 {
298 AutoWriteLock kbdLock(pData->pKeyboard COMMA_LOCKVAL_SRC_POS);
299 for (unsigned cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
300 if (pData->pKeyboard->mpDrv[cDev] == pData)
301 {
302 pData->pKeyboard->mpDrv[cDev] = NULL;
303 break;
304 }
305 pData->pKeyboard->mpVMMDev = NULL;
306 }
307}
308
309DECLCALLBACK(void) keyboardLedStatusChange(PPDMIKEYBOARDCONNECTOR pInterface,
310 PDMKEYBLEDS enmLeds)
311{
312 PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface);
313 pDrv->pKeyboard->getParent()->onKeyboardLedsChange(!!(enmLeds & PDMKEYBLEDS_NUMLOCK),
314 !!(enmLeds & PDMKEYBLEDS_CAPSLOCK),
315 !!(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
316}
317
318/**
319 * @interface_method_impl{PDMIKEYBOARDCONNECTOR,pfnSetActive}
320 */
321DECLCALLBACK(void) Keyboard::keyboardSetActive(PPDMIKEYBOARDCONNECTOR pInterface, bool fActive)
322{
323 PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface);
324 if (fActive)
325 pDrv->u32DevCaps |= KEYBOARD_DEVCAP_ENABLED;
326 else
327 pDrv->u32DevCaps &= ~KEYBOARD_DEVCAP_ENABLED;
328}
329
330/**
331 * Construct a keyboard driver instance.
332 *
333 * @copydoc FNPDMDRVCONSTRUCT
334 */
335DECLCALLBACK(int) Keyboard::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg,
336 uint32_t fFlags)
337{
338 PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
339 LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
340 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
341
342 /*
343 * Validate configuration.
344 */
345 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
346 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
347 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
348 ("Configuration error: Not possible to attach anything to this driver!\n"),
349 VERR_PDM_DRVINS_NO_ATTACH);
350
351 /*
352 * IBase.
353 */
354 pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
355
356 pData->IConnector.pfnLedStatusChange = keyboardLedStatusChange;
357 pData->IConnector.pfnSetActive = keyboardSetActive;
358
359 /*
360 * Get the IKeyboardPort interface of the above driver/device.
361 */
362 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIKEYBOARDPORT);
363 if (!pData->pUpPort)
364 {
365 AssertMsgFailed(("Configuration error: No keyboard port interface above!\n"));
366 return VERR_PDM_MISSING_INTERFACE_ABOVE;
367 }
368
369 /*
370 * Get the Keyboard object pointer and update the mpDrv member.
371 */
372 void *pv;
373 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
374 if (RT_FAILURE(rc))
375 {
376 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
377 return rc;
378 }
379 pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
380 unsigned cDev;
381 for (cDev = 0; cDev < KEYBOARD_MAX_DEVICES; ++cDev)
382 if (!pData->pKeyboard->mpDrv[cDev])
383 {
384 pData->pKeyboard->mpDrv[cDev] = pData;
385 break;
386 }
387 if (cDev == KEYBOARD_MAX_DEVICES)
388 return VERR_NO_MORE_HANDLES;
389
390 return VINF_SUCCESS;
391}
392
393
394/**
395 * Keyboard driver registration record.
396 */
397const PDMDRVREG Keyboard::DrvReg =
398{
399 /* u32Version */
400 PDM_DRVREG_VERSION,
401 /* szName */
402 "MainKeyboard",
403 /* szRCMod */
404 "",
405 /* szR0Mod */
406 "",
407 /* pszDescription */
408 "Main keyboard driver (Main as in the API).",
409 /* fFlags */
410 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
411 /* fClass. */
412 PDM_DRVREG_CLASS_KEYBOARD,
413 /* cMaxInstances */
414 ~0,
415 /* cbInstance */
416 sizeof(DRVMAINKEYBOARD),
417 /* pfnConstruct */
418 Keyboard::drvConstruct,
419 /* pfnDestruct */
420 Keyboard::drvDestruct,
421 /* pfnRelocate */
422 NULL,
423 /* pfnIOCtl */
424 NULL,
425 /* pfnPowerOn */
426 NULL,
427 /* pfnReset */
428 NULL,
429 /* pfnSuspend */
430 NULL,
431 /* pfnResume */
432 NULL,
433 /* pfnAttach */
434 NULL,
435 /* pfnDetach */
436 NULL,
437 /* pfnPowerOff */
438 NULL,
439 /* pfnSoftReset */
440 NULL,
441 /* u32EndVersion */
442 PDM_DRVREG_VERSION
443};
444/* 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