VirtualBox

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

Last change on this file since 38723 was 38591, checked in by vboxsync, 13 years ago

Additions/x11/vboxmouse: this was never correct, r73785 is the correct (host-side) fix

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