VirtualBox

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

Last change on this file since 4041 was 4015, checked in by vboxsync, 17 years ago

Use pdmdrv.h or pdmdev.h where appropriate.

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