VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/vboxmouse/vboxmouse.c@ 55401

Last change on this file since 55401 was 55401, checked in by vboxsync, 10 years ago

added a couple of missing Id headers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.3 KB
Line 
1/* $Id: vboxmouse.c 55401 2015-04-23 10:03:17Z vboxsync $ */
2/** @file
3 * VirtualBox X11 Guest Additions, mouse driver for X.Org server 1.5
4 */
5
6/*
7 * Copyright (C) 2006-2013 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 * This code is based on evdev.c from X.Org with the following copyright
19 * and permission notice:
20 *
21 * Copyright © 2004-2008 Red Hat, Inc.
22 *
23 * Permission to use, copy, modify, distribute, and sell this software
24 * and its documentation for any purpose is hereby granted without
25 * fee, provided that the above copyright notice appear in all copies
26 * and that both that copyright notice and this permission notice
27 * appear in supporting documentation, and that the name of Red Hat
28 * not be used in advertising or publicity pertaining to distribution
29 * of the software without specific, written prior permission. Red
30 * Hat makes no representations about the suitability of this software
31 * for any purpose. It is provided "as is" without express or implied
32 * warranty.
33 *
34 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
35 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
36 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
37 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
38 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
39 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
40 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 *
42 * Authors:
43 * Kristian Høgsberg ([email protected])
44 * Adam Jackson ([email protected])
45 */
46
47#include <VBox/VMMDev.h>
48#include <VBox/VBoxGuestLib.h>
49#include <iprt/err.h>
50#include <xf86.h>
51#include <xf86Xinput.h>
52#include <mipointer.h>
53
54#include <xf86Module.h>
55
56#ifdef VBOX_GUESTR3XF86MOD
57# define _X_EXPORT
58#else
59# include <errno.h>
60# include <fcntl.h>
61# include <unistd.h>
62#endif
63
64#include "product-generated.h"
65
66static void
67VBoxReadInput(InputInfoPtr pInfo)
68{
69 uint32_t cx, cy, fFeatures;
70
71 /* Read a byte from the device to acknowledge the event */
72 char c;
73 (void) read(pInfo->fd, &c, 1);
74 /* The first test here is a workaround for an apparent bug in Xorg Server 1.5 */
75 if (
76#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 2
77 miPointerCurrentScreen() != NULL
78#else
79 miPointerGetScreen(pInfo->dev) != NULL
80#endif
81 && RT_SUCCESS(VbglR3GetMouseStatus(&fFeatures, &cx, &cy))
82 && (fFeatures & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE))
83 {
84#if ABI_XINPUT_VERSION == SET_ABI_VERSION(2, 0)
85 /* Bug in the 1.4 X server series - conversion_proc was no longer
86 * called, but the server didn't yet do the conversion itself. */
87 cx = (cx * screenInfo.screens[0]->width) / 65535;
88 cy = (cy * screenInfo.screens[0]->height) / 65535;
89#endif
90 /* send absolute movement */
91 xf86PostMotionEvent(pInfo->dev, 1, 0, 2, cx, cy);
92 }
93}
94
95static void
96VBoxPtrCtrlProc(DeviceIntPtr device, PtrCtrl *ctrl)
97{
98 /* Nothing to do, dix handles all settings */
99}
100
101static int
102VBoxInit(DeviceIntPtr device)
103{
104 CARD8 map[2] = { 0, 1 };
105 Atom axis_labels[2] = { 0, 0 };
106 Atom button_labels[2] = { 0, 0 };
107 if (!InitPointerDeviceStruct((DevicePtr)device, map, 2,
108#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
109 button_labels,
110#endif
111#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 2
112 miPointerGetMotionEvents, VBoxPtrCtrlProc,
113 miPointerGetMotionBufferSize()
114#elif GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 3
115 GetMotionHistory, VBoxPtrCtrlProc,
116 GetMotionHistorySize(), 2 /* Number of axes */
117
118#elif GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 3
119 VBoxPtrCtrlProc, GetMotionHistorySize(),
120 2 /* Number of axes */
121#else
122# error Unsupported version of X.Org
123#endif
124#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
125 , axis_labels
126#endif
127 ))
128 return !Success;
129
130 /* Tell the server about the range of axis values we report */
131#if ABI_XINPUT_VERSION <= SET_ABI_VERSION(2, 0)
132 xf86InitValuatorAxisStruct(device, 0, 0, -1, 1, 0, 1);
133 xf86InitValuatorAxisStruct(device, 1, 0, -1, 1, 0, 1);
134#else
135 xf86InitValuatorAxisStruct(device, 0,
136# if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
137 axis_labels[0],
138# endif
139 VMMDEV_MOUSE_RANGE_MIN /* min X */, VMMDEV_MOUSE_RANGE_MAX /* max X */,
140 10000, 0, 10000
141# if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
142 , Absolute
143# endif
144 );
145
146 xf86InitValuatorAxisStruct(device, 1,
147# if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 7
148 axis_labels[1],
149# endif
150 VMMDEV_MOUSE_RANGE_MIN /* min Y */, VMMDEV_MOUSE_RANGE_MAX /* max Y */,
151 10000, 0, 10000
152# if GET_ABI_MAJOR(ABI_XINPUT_VERSION) >= 12
153 , Absolute
154# endif
155 );
156#endif
157 xf86InitValuatorDefaults(device, 0);
158 xf86InitValuatorDefaults(device, 1);
159 xf86MotionHistoryAllocate(device->public.devicePrivate);
160
161 return Success;
162}
163
164static int
165VBoxProc(DeviceIntPtr device, int what)
166{
167 InputInfoPtr pInfo;
168 int rc, xrc;
169 uint32_t fFeatures = 0;
170
171 pInfo = device->public.devicePrivate;
172
173 switch (what)
174 {
175 case DEVICE_INIT:
176 xrc = VBoxInit(device);
177 if (xrc != Success) {
178 VbglR3Term();
179 return xrc;
180 }
181 break;
182
183 case DEVICE_ON:
184 xf86Msg(X_INFO, "%s: On.\n", pInfo->name);
185 if (device->public.on)
186 break;
187 /* Tell the host that we want absolute co-ordinates */
188 rc = VbglR3SetMouseStatus( VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
189 | VMMDEV_MOUSE_NEW_PROTOCOL);
190 if (!RT_SUCCESS(rc)) {
191 xf86Msg(X_ERROR, "%s: Failed to switch guest mouse into absolute mode\n",
192 pInfo->name);
193 return !Success;
194 }
195
196 xf86AddEnabledDevice(pInfo);
197 device->public.on = TRUE;
198 break;
199
200 case DEVICE_OFF:
201 xf86Msg(X_INFO, "%s: Off.\n", pInfo->name);
202 rc = VbglR3GetMouseStatus(&fFeatures, NULL, NULL);
203 if (RT_SUCCESS(rc))
204 rc = VbglR3SetMouseStatus( fFeatures
205 & ~VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE
206 & ~VMMDEV_MOUSE_NEW_PROTOCOL);
207 xf86RemoveEnabledDevice(pInfo);
208 device->public.on = FALSE;
209 break;
210
211 case DEVICE_CLOSE:
212 VbglR3Term();
213 xf86Msg(X_INFO, "%s: Close\n", pInfo->name);
214 break;
215
216 default:
217 return BadValue;
218 }
219
220 return Success;
221}
222
223static int
224VBoxProbe(InputInfoPtr pInfo)
225{
226 int rc = VbglR3Init();
227 if (!RT_SUCCESS(rc)) {
228 xf86Msg(X_ERROR, "%s: Failed to open the VirtualBox device (error %d)\n",
229 pInfo->name, rc);
230 return BadMatch;
231 }
232
233 return Success;
234}
235
236static Bool
237VBoxConvert(InputInfoPtr pInfo, int first, int num, int v0, int v1, int v2,
238 int v3, int v4, int v5, int *x, int *y)
239{
240 if (first == 0) {
241 *x = xf86ScaleAxis(v0, 0, screenInfo.screens[0]->width, 0, 65536);
242 *y = xf86ScaleAxis(v1, 0, screenInfo.screens[0]->height, 0, 65536);
243 return TRUE;
244 } else
245 return FALSE;
246}
247
248static int
249VBoxPreInitInfo(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
250{
251 const char *device;
252 int rc;
253
254 /* Initialise the InputInfoRec. */
255 pInfo->device_control = VBoxProc;
256 pInfo->read_input = VBoxReadInput;
257 /* Unlike evdev, we set this unconditionally, as we don't handle keyboards. */
258 pInfo->type_name = XI_MOUSE;
259 pInfo->flags |= XI86_ALWAYS_CORE;
260
261 device = xf86SetStrOption(pInfo->options, "Device",
262 "/dev/vboxguest");
263
264 xf86Msg(X_CONFIG, "%s: Device: \"%s\"\n", pInfo->name, device);
265 do {
266 pInfo->fd = open(device, O_RDWR, 0);
267 }
268 while (pInfo->fd < 0 && errno == EINTR);
269
270 if (pInfo->fd < 0) {
271 xf86Msg(X_ERROR, "Unable to open VirtualBox device \"%s\".\n", device);
272 return BadMatch;
273 }
274
275 rc = VBoxProbe(pInfo);
276 if (rc != Success)
277 return rc;
278
279 return Success;
280}
281
282#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
283static InputInfoPtr
284VBoxPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
285{
286 InputInfoPtr pInfo;
287 const char *device;
288
289 if (!(pInfo = xf86AllocateInput(drv, 0)))
290 return NULL;
291
292 /* Initialise the InputInfoRec. */
293 pInfo->name = dev->identifier;
294 pInfo->conf_idev = dev;
295 pInfo->conversion_proc = VBoxConvert;
296 pInfo->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
297
298 xf86CollectInputOptions(pInfo, NULL, NULL);
299 xf86ProcessCommonOptions(pInfo, pInfo->options);
300
301 if (VBoxPreInitInfo(drv, pInfo, flags) != Success) {
302 xf86DeleteInput(pInfo, 0);
303 return NULL;
304 }
305
306 pInfo->flags |= XI86_CONFIGURED;
307 return pInfo;
308}
309#endif
310
311_X_EXPORT InputDriverRec VBOXMOUSE = {
312 1,
313 "vboxmouse",
314 NULL,
315#if GET_ABI_MAJOR(ABI_XINPUT_VERSION) < 12
316 VBoxPreInit,
317#else
318 VBoxPreInitInfo,
319#endif
320 NULL,
321 NULL,
322 0
323};
324
325static pointer
326VBoxPlug(pointer module,
327 pointer options,
328 int *errmaj,
329 int *errmin)
330{
331 xf86AddInputDriver(&VBOXMOUSE, module, 0);
332 xf86Msg(X_CONFIG, "Load address of symbol \"VBOXMOUSE\" is %p\n",
333 (void *)&VBOXMOUSE);
334 return module;
335}
336
337static XF86ModuleVersionInfo VBoxVersionRec =
338{
339 "vboxmouse",
340 VBOX_VENDOR,
341 MODINFOSTRING1,
342 MODINFOSTRING2,
343 0, /* Missing from SDK: XORG_VERSION_CURRENT, */
344 1, 0, 0,
345 ABI_CLASS_XINPUT,
346 ABI_XINPUT_VERSION,
347 MOD_CLASS_XINPUT,
348 {0, 0, 0, 0}
349};
350
351_X_EXPORT XF86ModuleData vboxmouseModuleData =
352{
353 &VBoxVersionRec,
354 VBoxPlug,
355 NULL
356};
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