VirtualBox

source: vbox/trunk/src/VBox/Devices/Input/DrvMouseQueue.cpp@ 93609

Last change on this file since 93609 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.4 KB
Line 
1/* $Id: DrvMouseQueue.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBox input devices: Mouse queue driver
4 */
5
6/*
7 * Copyright (C) 2006-2022 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_MOUSE_QUEUE
23#include <VBox/vmm/pdmdrv.h>
24#include <iprt/assert.h>
25#include <iprt/uuid.h>
26
27#include "VBoxDD.h"
28
29
30
31/*********************************************************************************************************************************
32* Structures and Typedefs *
33*********************************************************************************************************************************/
34/**
35 * Mouse queue driver instance data.
36 *
37 * @implements PDMIMOUSECONNECTOR
38 * @implements PDMIMOUSEPORT
39 */
40typedef struct DRVMOUSEQUEUE
41{
42 /** Pointer to the driver instance structure. */
43 PPDMDRVINS pDrvIns;
44 /** Pointer to the mouse port interface of the driver/device above us. */
45 PPDMIMOUSEPORT pUpPort;
46 /** Pointer to the mouse port interface of the driver/device below us. */
47 PPDMIMOUSECONNECTOR pDownConnector;
48 /** Our mouse connector interface. */
49 PDMIMOUSECONNECTOR IConnector;
50 /** Our mouse port interface. */
51 PDMIMOUSEPORT IPort;
52 /** The queue handle. */
53 PDMQUEUEHANDLE hQueue;
54 /** Discard input when this flag is set.
55 * We only accept input when the VM is running. */
56 bool fInactive;
57} DRVMOUSEQUEUE, *PDRVMOUSEQUEUE;
58
59
60/**
61 * Event type for @a DRVMOUSEQUEUEITEM
62 */
63enum EVENTTYPE { RELATIVE, ABSOLUTE };
64
65/**
66 * Mouse queue item.
67 */
68typedef struct DRVMOUSEQUEUEITEM
69{
70 /** The core part owned by the queue manager. */
71 PDMQUEUEITEMCORE Core;
72 enum EVENTTYPE enmType;
73 union
74 {
75 uint32_t padding[5];
76 struct
77 {
78 uint32_t fButtons;
79 int32_t dx;
80 int32_t dy;
81 int32_t dz;
82 int32_t dw;
83 } Relative;
84 struct
85 {
86 uint32_t fButtons;
87 uint32_t x;
88 uint32_t y;
89 int32_t dz;
90 int32_t dw;
91 } Absolute;
92 } u;
93} DRVMOUSEQUEUEITEM, *PDRVMOUSEQUEUEITEM;
94
95
96
97/* -=-=-=-=- IBase -=-=-=-=- */
98
99/**
100 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
101 */
102static DECLCALLBACK(void *) drvMouseQueueQueryInterface(PPDMIBASE pInterface, const char *pszIID)
103{
104 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
105 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
106 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
107 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSEPORT, &pThis->IPort);
108 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUSECONNECTOR, &pThis->IConnector);
109 return NULL;
110}
111
112
113/* -=-=-=-=- IMousePort -=-=-=-=- */
114
115/** Converts a pointer to DRVMOUSEQUEUE::Port to a DRVMOUSEQUEUE pointer. */
116#define IMOUSEPORT_2_DRVMOUSEQUEUE(pInterface) ( (PDRVMOUSEQUEUE)((char *)(pInterface) - RT_UOFFSETOF(DRVMOUSEQUEUE, IPort)) )
117
118
119/**
120 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEvent}
121 */
122static DECLCALLBACK(int) drvMouseQueuePutEvent(PPDMIMOUSEPORT pInterface,
123 int32_t dx, int32_t dy,
124 int32_t dz, int32_t dw,
125 uint32_t fButtons)
126{
127 PDRVMOUSEQUEUE pDrv = IMOUSEPORT_2_DRVMOUSEQUEUE(pInterface);
128 if (pDrv->fInactive)
129 return VINF_SUCCESS;
130
131 PDRVMOUSEQUEUEITEM pItem = (PDRVMOUSEQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
132 if (pItem)
133 {
134 RT_ZERO(pItem->u.padding);
135 pItem->enmType = RELATIVE;
136 pItem->u.Relative.dx = dx;
137 pItem->u.Relative.dy = dy;
138 pItem->u.Relative.dz = dz;
139 pItem->u.Relative.dw = dw;
140 pItem->u.Relative.fButtons = fButtons;
141 PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem->Core);
142 return VINF_SUCCESS;
143 }
144 return VERR_PDM_NO_QUEUE_ITEMS;
145}
146
147/**
148 * @interface_method_impl{PDMIMOUSEPORT,pfnPutEventAbs}
149 */
150static DECLCALLBACK(int) drvMouseQueuePutEventAbs(PPDMIMOUSEPORT pInterface,
151 uint32_t x, uint32_t y,
152 int32_t dz, int32_t dw,
153 uint32_t fButtons)
154{
155 PDRVMOUSEQUEUE pDrv = IMOUSEPORT_2_DRVMOUSEQUEUE(pInterface);
156 if (pDrv->fInactive)
157 return VINF_SUCCESS;
158
159 PDRVMOUSEQUEUEITEM pItem = (PDRVMOUSEQUEUEITEM)PDMDrvHlpQueueAlloc(pDrv->pDrvIns, pDrv->hQueue);
160 if (pItem)
161 {
162 RT_ZERO(pItem->u.padding);
163 pItem->enmType = ABSOLUTE;
164 pItem->u.Absolute.x = x;
165 pItem->u.Absolute.y = y;
166 pItem->u.Absolute.dz = dz;
167 pItem->u.Absolute.dw = dw;
168 pItem->u.Absolute.fButtons = fButtons;
169 PDMDrvHlpQueueInsert(pDrv->pDrvIns, pDrv->hQueue, &pItem->Core);
170 return VINF_SUCCESS;
171 }
172 return VERR_PDM_NO_QUEUE_ITEMS;
173}
174
175
176static DECLCALLBACK(int) drvMouseQueuePutEventMultiTouch(PPDMIMOUSEPORT pInterface,
177 uint8_t cContacts,
178 const uint64_t *pau64Contacts,
179 uint32_t u32ScanTime)
180{
181 PDRVMOUSEQUEUE pThis = IMOUSEPORT_2_DRVMOUSEQUEUE(pInterface);
182 return pThis->pUpPort->pfnPutEventMultiTouch(pThis->pUpPort, cContacts, pau64Contacts, u32ScanTime);
183}
184
185/* -=-=-=-=- IConnector -=-=-=-=- */
186
187#define PPDMIMOUSECONNECTOR_2_DRVMOUSEQUEUE(pInterface) ( (PDRVMOUSEQUEUE)((char *)(pInterface) - RT_UOFFSETOF(DRVMOUSEQUEUE, IConnector)) )
188
189
190/**
191 * Pass absolute mode status changes from the guest through to the frontend
192 * driver.
193 *
194 * @param pInterface Pointer to the mouse connector interface structure.
195 * @param fRel Is relative reporting supported?
196 * @param fAbs Is absolute reporting supported?
197 * @param fMT Is multi-touch reporting supported?
198 */
199static DECLCALLBACK(void) drvMousePassThruReportModes(PPDMIMOUSECONNECTOR pInterface, bool fRel, bool fAbs, bool fMT)
200{
201 PDRVMOUSEQUEUE pDrv = PPDMIMOUSECONNECTOR_2_DRVMOUSEQUEUE(pInterface);
202 pDrv->pDownConnector->pfnReportModes(pDrv->pDownConnector, fRel, fAbs, fMT);
203}
204
205
206/**
207 * Flush the mouse queue if there are pending events.
208 *
209 * @param pInterface Pointer to the mouse connector interface structure.
210 */
211static DECLCALLBACK(void) drvMouseFlushQueue(PPDMIMOUSECONNECTOR pInterface)
212{
213 PDRVMOUSEQUEUE pDrv = PPDMIMOUSECONNECTOR_2_DRVMOUSEQUEUE(pInterface);
214
215 PDMDrvHlpQueueFlushIfNecessary(pDrv->pDrvIns, pDrv->hQueue);
216}
217
218
219
220/* -=-=-=-=- queue -=-=-=-=- */
221
222/**
223 * Queue callback for processing a queued item.
224 *
225 * @returns Success indicator.
226 * If false the item will not be removed and the flushing will stop.
227 * @param pDrvIns The driver instance.
228 * @param pItemCore Pointer to the queue item to process.
229 */
230static DECLCALLBACK(bool) drvMouseQueueConsumer(PPDMDRVINS pDrvIns, PPDMQUEUEITEMCORE pItemCore)
231{
232 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
233 PDRVMOUSEQUEUEITEM pItem = (PDRVMOUSEQUEUEITEM)pItemCore;
234 int rc;
235 if (pItem->enmType == RELATIVE)
236 rc = pThis->pUpPort->pfnPutEvent(pThis->pUpPort,
237 pItem->u.Relative.dx,
238 pItem->u.Relative.dy,
239 pItem->u.Relative.dz,
240 pItem->u.Relative.dw,
241 pItem->u.Relative.fButtons);
242 else if (pItem->enmType == ABSOLUTE)
243 rc = pThis->pUpPort->pfnPutEventAbs(pThis->pUpPort,
244 pItem->u.Absolute.x,
245 pItem->u.Absolute.y,
246 pItem->u.Absolute.dz,
247 pItem->u.Absolute.dw,
248 pItem->u.Absolute.fButtons);
249 else
250 AssertMsgFailedReturn(("enmType=%d\n", pItem->enmType), true /* remove buggy data */);
251 return rc != VERR_TRY_AGAIN;
252}
253
254
255/* -=-=-=-=- driver interface -=-=-=-=- */
256
257/**
258 * Power On notification.
259 *
260 * @returns VBox status code.
261 * @param pDrvIns The drive instance data.
262 */
263static DECLCALLBACK(void) drvMouseQueuePowerOn(PPDMDRVINS pDrvIns)
264{
265 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
266 pThis->fInactive = false;
267}
268
269
270/**
271 * Reset notification.
272 *
273 * @returns VBox status code.
274 * @param pDrvIns The drive instance data.
275 */
276static DECLCALLBACK(void) drvMouseQueueReset(PPDMDRVINS pDrvIns)
277{
278 //PDRVKBDQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVKBDQUEUE);
279 /** @todo purge the queue on reset. */
280 RT_NOREF(pDrvIns);
281}
282
283
284/**
285 * Suspend notification.
286 *
287 * @returns VBox status code.
288 * @param pDrvIns The drive instance data.
289 */
290static DECLCALLBACK(void) drvMouseQueueSuspend(PPDMDRVINS pDrvIns)
291{
292 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
293 pThis->fInactive = true;
294}
295
296
297/**
298 * Resume notification.
299 *
300 * @returns VBox status code.
301 * @param pDrvIns The drive instance data.
302 */
303static DECLCALLBACK(void) drvMouseQueueResume(PPDMDRVINS pDrvIns)
304{
305 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
306 pThis->fInactive = false;
307}
308
309
310/**
311 * Power Off notification.
312 *
313 * @param pDrvIns The drive instance data.
314 */
315static DECLCALLBACK(void) drvMouseQueuePowerOff(PPDMDRVINS pDrvIns)
316{
317 PDRVMOUSEQUEUE pThis = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
318 pThis->fInactive = true;
319}
320
321
322/**
323 * Construct a mouse driver instance.
324 *
325 * @copydoc FNPDMDRVCONSTRUCT
326 */
327static DECLCALLBACK(int) drvMouseQueueConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
328{
329 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
330 PDRVMOUSEQUEUE pDrv = PDMINS_2_DATA(pDrvIns, PDRVMOUSEQUEUE);
331 PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
332
333 LogFlow(("drvMouseQueueConstruct: iInstance=%d\n", pDrvIns->iInstance));
334
335 /*
336 * Validate configuration.
337 */
338 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "QueueSize|Interval", "");
339
340 /*
341 * Init basic data members and interfaces.
342 */
343 pDrv->pDrvIns = pDrvIns;
344 pDrv->fInactive = true;
345 /* IBase. */
346 pDrvIns->IBase.pfnQueryInterface = drvMouseQueueQueryInterface;
347 /* IMouseConnector. */
348 pDrv->IConnector.pfnReportModes = drvMousePassThruReportModes;
349 pDrv->IConnector.pfnFlushQueue = drvMouseFlushQueue;
350 /* IMousePort. */
351 pDrv->IPort.pfnPutEvent = drvMouseQueuePutEvent;
352 pDrv->IPort.pfnPutEventAbs = drvMouseQueuePutEventAbs;
353 pDrv->IPort.pfnPutEventMultiTouch = drvMouseQueuePutEventMultiTouch;
354
355 /*
356 * Get the IMousePort interface of the above driver/device.
357 */
358 pDrv->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMOUSEPORT);
359 AssertMsgReturn(pDrv->pUpPort, ("Configuration error: No mouse port interface above!\n"), VERR_PDM_MISSING_INTERFACE_ABOVE);
360
361 /*
362 * Attach driver below and query it's connector interface.
363 */
364 PPDMIBASE pDownBase;
365 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
366 AssertMsgRCReturn(rc, ("Failed to attach driver below us! rc=%Rra\n", rc), rc);
367
368 pDrv->pDownConnector = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIMOUSECONNECTOR);
369 AssertMsgReturn(pDrv->pDownConnector, ("Configuration error: No mouse connector interface below!\n"),
370 VERR_PDM_MISSING_INTERFACE_BELOW);
371
372 /*
373 * Create the queue.
374 */
375 uint32_t cMilliesInterval = 0;
376 rc = pHlp->pfnCFGMQueryU32(pCfg, "Interval", &cMilliesInterval);
377 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
378 cMilliesInterval = 0;
379 else
380 AssertMsgRCReturn(rc, ("Configuration error: 32-bit \"Interval\" -> rc=%Rrc\n", rc), rc);
381
382 uint32_t cItems = 0;
383 rc = pHlp->pfnCFGMQueryU32(pCfg, "QueueSize", &cItems);
384 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
385 cItems = 128;
386 else
387 AssertMsgRCReturn(rc, ("Configuration error: 32-bit \"QueueSize\" -> rc=%Rrc\n", rc), rc);
388
389 rc = PDMDrvHlpQueueCreate(pDrvIns, sizeof(DRVMOUSEQUEUEITEM), cItems, cMilliesInterval,
390 drvMouseQueueConsumer, "Mouse", &pDrv->hQueue);
391 AssertMsgRCReturn(rc, ("Failed to create driver: cItems=%d cMilliesInterval=%d rc=%Rrc\n", cItems, cMilliesInterval, rc), rc);
392
393 return VINF_SUCCESS;
394}
395
396
397/**
398 * Mouse queue driver registration record.
399 */
400const PDMDRVREG g_DrvMouseQueue =
401{
402 /* u32Version */
403 PDM_DRVREG_VERSION,
404 /* szName */
405 "MouseQueue",
406 /* szRCMod */
407 "",
408 /* szR0Mod */
409 "",
410 /* pszDescription */
411 "Mouse queue driver to plug in between the key source and the device to do queueing and inter-thread transport.",
412 /* fFlags */
413 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
414 /* fClass. */
415 PDM_DRVREG_CLASS_MOUSE,
416 /* cMaxInstances */
417 ~0U,
418 /* cbInstance */
419 sizeof(DRVMOUSEQUEUE),
420 /* pfnConstruct */
421 drvMouseQueueConstruct,
422 /* pfnRelocate */
423 NULL,
424 /* pfnDestruct */
425 NULL,
426 /* pfnIOCtl */
427 NULL,
428 /* pfnPowerOn */
429 drvMouseQueuePowerOn,
430 /* pfnReset */
431 drvMouseQueueReset,
432 /* pfnSuspend */
433 drvMouseQueueSuspend,
434 /* pfnResume */
435 drvMouseQueueResume,
436 /* pfnAttach */
437 NULL,
438 /* pfnDetach */
439 NULL,
440 /* pfnPowerOff */
441 drvMouseQueuePowerOff,
442 /* pfnSoftReset */
443 NULL,
444 /* u32EndVersion */
445 PDM_DRVREG_VERSION
446};
447
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