1 | /* $Id: KeyboardImpl.cpp 15715 2008-12-22 13:15:30Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * VirtualBox COM class implementation
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2008 Sun Microsystems, Inc.
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
20 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
21 | * additional information or have any questions.
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "KeyboardImpl.h"
|
---|
25 | #include "ConsoleImpl.h"
|
---|
26 |
|
---|
27 | #include "Logging.h"
|
---|
28 |
|
---|
29 | #include <VBox/com/array.h>
|
---|
30 | #include <VBox/pdmdrv.h>
|
---|
31 | #include <iprt/asm.h>
|
---|
32 |
|
---|
33 | // defines
|
---|
34 | ////////////////////////////////////////////////////////////////////////////////
|
---|
35 |
|
---|
36 | // globals
|
---|
37 | ////////////////////////////////////////////////////////////////////////////////
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Keyboard driver instance data.
|
---|
41 | */
|
---|
42 | typedef struct DRVMAINKEYBOARD
|
---|
43 | {
|
---|
44 | /** Pointer to the keyboard object. */
|
---|
45 | Keyboard *pKeyboard;
|
---|
46 | /** Pointer to the driver instance structure. */
|
---|
47 | PPDMDRVINS pDrvIns;
|
---|
48 | /** Pointer to the keyboard port interface of the driver/device above us. */
|
---|
49 | PPDMIKEYBOARDPORT pUpPort;
|
---|
50 | /** Our mouse connector interface. */
|
---|
51 | PDMIKEYBOARDCONNECTOR Connector;
|
---|
52 | } DRVMAINKEYBOARD, *PDRVMAINKEYBOARD;
|
---|
53 |
|
---|
54 | /** Converts PDMIVMMDEVCONNECTOR pointer to a DRVMAINVMMDEV pointer. */
|
---|
55 | #define PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD(pInterface) ( (PDRVMAINKEYBOARD) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINKEYBOARD, Connector)) )
|
---|
56 |
|
---|
57 |
|
---|
58 | // constructor / destructor
|
---|
59 | ////////////////////////////////////////////////////////////////////////////////
|
---|
60 |
|
---|
61 | DEFINE_EMPTY_CTOR_DTOR (Keyboard)
|
---|
62 |
|
---|
63 | HRESULT Keyboard::FinalConstruct()
|
---|
64 | {
|
---|
65 | mpDrv = NULL;
|
---|
66 | mpVMMDev = NULL;
|
---|
67 | mfVMMDevInited = false;
|
---|
68 | return S_OK;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void Keyboard::FinalRelease()
|
---|
72 | {
|
---|
73 | uninit();
|
---|
74 | }
|
---|
75 |
|
---|
76 | // public methods
|
---|
77 | ////////////////////////////////////////////////////////////////////////////////
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Initializes the keyboard object.
|
---|
81 | *
|
---|
82 | * @returns COM result indicator
|
---|
83 | * @param parent handle of our parent object
|
---|
84 | */
|
---|
85 | HRESULT Keyboard::init (Console *aParent)
|
---|
86 | {
|
---|
87 | LogFlowThisFunc (("aParent=%p\n", aParent));
|
---|
88 |
|
---|
89 | ComAssertRet (aParent, E_INVALIDARG);
|
---|
90 |
|
---|
91 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
92 | AutoInitSpan autoInitSpan (this);
|
---|
93 | AssertReturn (autoInitSpan.isOk(), E_FAIL);
|
---|
94 |
|
---|
95 | unconst (mParent) = aParent;
|
---|
96 |
|
---|
97 | /* Confirm a successful initialization */
|
---|
98 | autoInitSpan.setSucceeded();
|
---|
99 |
|
---|
100 | return S_OK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
105 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
106 | */
|
---|
107 | void Keyboard::uninit()
|
---|
108 | {
|
---|
109 | LogFlowThisFunc (("\n"));
|
---|
110 |
|
---|
111 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
112 | AutoUninitSpan autoUninitSpan (this);
|
---|
113 | if (autoUninitSpan.uninitDone())
|
---|
114 | return;
|
---|
115 |
|
---|
116 | if (mpDrv)
|
---|
117 | mpDrv->pKeyboard = NULL;
|
---|
118 |
|
---|
119 | mpDrv = NULL;
|
---|
120 | mpVMMDev = NULL;
|
---|
121 | mfVMMDevInited = true;
|
---|
122 |
|
---|
123 | unconst (mParent).setNull();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Sends a scancode to the keyboard.
|
---|
128 | *
|
---|
129 | * @returns COM status code
|
---|
130 | * @param scancode The scancode to send
|
---|
131 | */
|
---|
132 | STDMETHODIMP Keyboard::PutScancode (LONG scancode)
|
---|
133 | {
|
---|
134 | HRESULT rc = S_OK;
|
---|
135 |
|
---|
136 | AutoCaller autoCaller (this);
|
---|
137 | CheckComRCReturnRC (autoCaller.rc());
|
---|
138 |
|
---|
139 | AutoWriteLock alock (this);
|
---|
140 |
|
---|
141 | CHECK_CONSOLE_DRV (mpDrv);
|
---|
142 |
|
---|
143 | int vrc = mpDrv->pUpPort->pfnPutEvent (mpDrv->pUpPort, (uint8_t)scancode);
|
---|
144 |
|
---|
145 | if (RT_FAILURE (vrc))
|
---|
146 | rc = setError (VBOX_E_IPRT_ERROR,
|
---|
147 | tr ("Could not send scan code 0x%08X to the virtual keyboard (%Rrc)"),
|
---|
148 | scancode, vrc);
|
---|
149 |
|
---|
150 | return rc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Sends a list of scancodes to the keyboard.
|
---|
155 | *
|
---|
156 | * @returns COM status code
|
---|
157 | * @param scancodes Pointer to the first scancode
|
---|
158 | * @param count Number of scancodes
|
---|
159 | * @param codesStored Address of variable to store the number
|
---|
160 | * of scancodes that were sent to the keyboard.
|
---|
161 | This value can be NULL.
|
---|
162 | */
|
---|
163 | STDMETHODIMP Keyboard::PutScancodes (ComSafeArrayIn (LONG, scancodes),
|
---|
164 | ULONG *codesStored)
|
---|
165 | {
|
---|
166 | HRESULT rc = S_OK;
|
---|
167 |
|
---|
168 | if (ComSafeArrayInIsNull (scancodes))
|
---|
169 | return E_INVALIDARG;
|
---|
170 |
|
---|
171 | AutoCaller autoCaller (this);
|
---|
172 | CheckComRCReturnRC (autoCaller.rc());
|
---|
173 |
|
---|
174 | AutoWriteLock alock (this);
|
---|
175 |
|
---|
176 | CHECK_CONSOLE_DRV (mpDrv);
|
---|
177 |
|
---|
178 | com::SafeArray <LONG> keys (ComSafeArrayInArg (scancodes));
|
---|
179 | int vrc = VINF_SUCCESS;
|
---|
180 |
|
---|
181 | for (uint32_t i = 0; (i < keys.size()) && RT_SUCCESS (vrc); i++)
|
---|
182 | vrc = mpDrv->pUpPort->pfnPutEvent (mpDrv->pUpPort, (uint8_t)keys [i]);
|
---|
183 |
|
---|
184 | if (RT_FAILURE (vrc))
|
---|
185 | return setError (VBOX_E_IPRT_ERROR,
|
---|
186 | tr ("Could not send all scan codes to the virtual keyboard (%Rrc)"),
|
---|
187 | vrc);
|
---|
188 |
|
---|
189 | /// @todo is it actually possible that not all scancodes can be transmitted?
|
---|
190 | if (codesStored)
|
---|
191 | *codesStored = (uint32_t)keys.size();
|
---|
192 |
|
---|
193 | return rc;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Sends Control-Alt-Delete to the keyboard. This could be done otherwise
|
---|
198 | * but it's so common that we'll be nice and supply a convenience API.
|
---|
199 | *
|
---|
200 | * @returns COM status code
|
---|
201 | *
|
---|
202 | */
|
---|
203 | STDMETHODIMP Keyboard::PutCAD()
|
---|
204 | {
|
---|
205 | static com::SafeArray<LONG> cadSequence (6);
|
---|
206 |
|
---|
207 | cadSequence [0] = 0x1d; // Ctrl down
|
---|
208 | cadSequence [1] = 0x38; // Alt down
|
---|
209 | cadSequence [2] = 0x53; // Del down
|
---|
210 | cadSequence [3] = 0xd3; // Del up
|
---|
211 | cadSequence [4] = 0xb8; // Alt up
|
---|
212 | cadSequence [5] = 0x9d; // Ctrl up
|
---|
213 |
|
---|
214 | return PutScancodes (ComSafeArrayAsInParam (cadSequence), NULL);
|
---|
215 | }
|
---|
216 |
|
---|
217 | //
|
---|
218 | // private methods
|
---|
219 | //
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Queries an interface to the driver.
|
---|
223 | *
|
---|
224 | * @returns Pointer to interface.
|
---|
225 | * @returns NULL if the interface was not supported by the driver.
|
---|
226 | * @param pInterface Pointer to this interface structure.
|
---|
227 | * @param enmInterface The requested interface identification.
|
---|
228 | */
|
---|
229 | DECLCALLBACK(void *) Keyboard::drvQueryInterface (PPDMIBASE pInterface, PDMINTERFACE enmInterface)
|
---|
230 | {
|
---|
231 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV (pInterface);
|
---|
232 | PDRVMAINKEYBOARD pDrv = PDMINS_2_DATA (pDrvIns, PDRVMAINKEYBOARD);
|
---|
233 | switch (enmInterface)
|
---|
234 | {
|
---|
235 | case PDMINTERFACE_BASE:
|
---|
236 | return &pDrvIns->IBase;
|
---|
237 | case PDMINTERFACE_KEYBOARD_CONNECTOR:
|
---|
238 | return &pDrv->Connector;
|
---|
239 | default:
|
---|
240 | return NULL;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Destruct a keyboard driver instance.
|
---|
247 | *
|
---|
248 | * @returns VBox status.
|
---|
249 | * @param pDrvIns The driver instance data.
|
---|
250 | */
|
---|
251 | DECLCALLBACK(void) Keyboard::drvDestruct (PPDMDRVINS pDrvIns)
|
---|
252 | {
|
---|
253 | PDRVMAINKEYBOARD pData = PDMINS_2_DATA(pDrvIns, PDRVMAINKEYBOARD);
|
---|
254 | LogFlow(("Keyboard::drvDestruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
255 | if (pData->pKeyboard)
|
---|
256 | {
|
---|
257 | AutoWriteLock kbdLock (pData->pKeyboard);
|
---|
258 | pData->pKeyboard->mpDrv = NULL;
|
---|
259 | pData->pKeyboard->mpVMMDev = NULL;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | DECLCALLBACK(void) keyboardLedStatusChange (PPDMIKEYBOARDCONNECTOR pInterface, PDMKEYBLEDS enmLeds)
|
---|
264 | {
|
---|
265 | PDRVMAINKEYBOARD pDrv = PPDMIKEYBOARDCONNECTOR_2_MAINKEYBOARD (pInterface);
|
---|
266 | pDrv->pKeyboard->getParent()->onKeyboardLedsChange (!!(enmLeds & PDMKEYBLEDS_NUMLOCK),
|
---|
267 | !!(enmLeds & PDMKEYBLEDS_CAPSLOCK),
|
---|
268 | !!(enmLeds & PDMKEYBLEDS_SCROLLLOCK));
|
---|
269 | }
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Construct a keyboard driver instance.
|
---|
273 | *
|
---|
274 | * @returns VBox status.
|
---|
275 | * @param pDrvIns The driver instance data.
|
---|
276 | * If the registration structure is needed, pDrvIns->pDrvReg points to it.
|
---|
277 | * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
|
---|
278 | * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
|
---|
279 | * iInstance it's expected to be used a bit in this function.
|
---|
280 | */
|
---|
281 | DECLCALLBACK(int) Keyboard::drvConstruct (PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
|
---|
282 | {
|
---|
283 | PDRVMAINKEYBOARD pData = PDMINS_2_DATA (pDrvIns, PDRVMAINKEYBOARD);
|
---|
284 | LogFlow(("Keyboard::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
|
---|
285 |
|
---|
286 | /*
|
---|
287 | * Validate configuration.
|
---|
288 | */
|
---|
289 | if (!CFGMR3AreValuesValid (pCfgHandle, "Object\0"))
|
---|
290 | return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
|
---|
291 | PPDMIBASE pBaseIgnore;
|
---|
292 | int rc = pDrvIns->pDrvHlp->pfnAttach (pDrvIns, &pBaseIgnore);
|
---|
293 | if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
|
---|
294 | {
|
---|
295 | AssertMsgFailed (("Configuration error: Not possible to attach anything to this driver!\n"));
|
---|
296 | return VERR_PDM_DRVINS_NO_ATTACH;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * IBase.
|
---|
301 | */
|
---|
302 | pDrvIns->IBase.pfnQueryInterface = Keyboard::drvQueryInterface;
|
---|
303 |
|
---|
304 | pData->Connector.pfnLedStatusChange = keyboardLedStatusChange;
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Get the IKeyboardPort interface of the above driver/device.
|
---|
308 | */
|
---|
309 | pData->pUpPort = (PPDMIKEYBOARDPORT)pDrvIns->pUpBase->pfnQueryInterface (pDrvIns->pUpBase, PDMINTERFACE_KEYBOARD_PORT);
|
---|
310 | if (!pData->pUpPort)
|
---|
311 | {
|
---|
312 | AssertMsgFailed (("Configuration error: No keyboard port interface above!\n"));
|
---|
313 | return VERR_PDM_MISSING_INTERFACE_ABOVE;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /*
|
---|
317 | * Get the Keyboard object pointer and update the mpDrv member.
|
---|
318 | */
|
---|
319 | void *pv;
|
---|
320 | rc = CFGMR3QueryPtr (pCfgHandle, "Object", &pv);
|
---|
321 | if (RT_FAILURE (rc))
|
---|
322 | {
|
---|
323 | AssertMsgFailed (("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
|
---|
324 | return rc;
|
---|
325 | }
|
---|
326 | pData->pKeyboard = (Keyboard *)pv; /** @todo Check this cast! */
|
---|
327 | pData->pKeyboard->mpDrv = pData;
|
---|
328 |
|
---|
329 | return VINF_SUCCESS;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Keyboard driver registration record.
|
---|
335 | */
|
---|
336 | const PDMDRVREG Keyboard::DrvReg =
|
---|
337 | {
|
---|
338 | /* u32Version */
|
---|
339 | PDM_DRVREG_VERSION,
|
---|
340 | /* szDriverName */
|
---|
341 | "MainKeyboard",
|
---|
342 | /* pszDescription */
|
---|
343 | "Main keyboard driver (Main as in the API).",
|
---|
344 | /* fFlags */
|
---|
345 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
346 | /* fClass. */
|
---|
347 | PDM_DRVREG_CLASS_KEYBOARD,
|
---|
348 | /* cMaxInstances */
|
---|
349 | ~0,
|
---|
350 | /* cbInstance */
|
---|
351 | sizeof(DRVMAINKEYBOARD),
|
---|
352 | /* pfnConstruct */
|
---|
353 | Keyboard::drvConstruct,
|
---|
354 | /* pfnDestruct */
|
---|
355 | Keyboard::drvDestruct,
|
---|
356 | /* pfnIOCtl */
|
---|
357 | NULL,
|
---|
358 | /* pfnPowerOn */
|
---|
359 | NULL,
|
---|
360 | /* pfnReset */
|
---|
361 | NULL,
|
---|
362 | /* pfnSuspend */
|
---|
363 | NULL,
|
---|
364 | /* pfnResume */
|
---|
365 | NULL,
|
---|
366 | /* pfnDetach */
|
---|
367 | NULL
|
---|
368 | };
|
---|
369 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|