VirtualBox

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

Last change on this file since 23789 was 21412, checked in by vboxsync, 16 years ago

Add,++: Switch to common addition kernel module.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/** @file
2 *
3 * VirtualBox X11 Additions mouse driver utility functions
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22#include <iprt/assert.h>
23#include <VBox/VMMDev.h>
24#include <VBox/VBoxGuest.h>
25#include <VBox/VBoxGuestLib.h>
26#include "VBoxUtils.h"
27
28#include "xf86.h"
29#define NEED_XF86_TYPES
30#include "xf86_ansic.h"
31#include "compiler.h"
32
33#ifndef RT_OS_SOLARIS
34# include <asm/ioctl.h>
35#endif
36
37#ifndef RT_OS_LINUX /** @todo later Linux should also use R3 lib for this */
38int VBoxMouseInit(void)
39{
40 int rc = VbglR3Init();
41 if (RT_FAILURE(rc))
42 {
43 ErrorF("VbglR3Init failed.\n");
44 return 1;
45 }
46
47 rc = VbglR3SetMouseStatus(VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
48 if (RT_FAILURE(rc))
49 {
50 ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
51 errno, strerror(errno));
52 return 1;
53 }
54 xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
55 return 0;
56}
57
58
59int VBoxMouseQueryPosition(unsigned int *puAbsXPos, unsigned int *puAbsYPos)
60{
61 int rc;
62 uint32_t pointerXPos;
63 uint32_t pointerYPos;
64
65 AssertPtrReturn(puAbsXPos, VERR_INVALID_PARAMETER);
66 AssertPtrReturn(puAbsYPos, VERR_INVALID_PARAMETER);
67 rc = VbglR3GetMouseStatus(NULL, &pointerXPos, &pointerYPos);
68 if (RT_SUCCESS(rc))
69 {
70 *puAbsXPos = pointerXPos;
71 *puAbsYPos = pointerYPos;
72 return 0;
73 }
74 return 2;
75}
76
77
78int VBoxMouseFini(void)
79{
80 int rc = VbglR3SetMouseStatus(0);
81 VbglR3Term();
82 return rc;
83}
84#else /* RT_OS_LINUX */
85/* the vboxguest module file handle */
86static int g_vboxguestHandle = -1;
87/* the request structure */
88static VMMDevReqMouseStatus *g_vmmreqMouseStatus = NULL;
89
90/**
91 * Initialise mouse integration. Returns 0 on success and 1 on failure
92 * (for example, if the VBox kernel module is not loaded).
93 */
94int VBoxMouseInit(void)
95{
96 VMMDevReqMouseStatus req;
97
98 /* return immediately if already initialized */
99 if (g_vboxguestHandle != -1)
100 return 0;
101
102 /* open the driver */
103 g_vboxguestHandle = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
104 if (g_vboxguestHandle < 0)
105 {
106 ErrorF("Unable to open the virtual machine device: %s\n",
107 strerror(errno));
108 return 1;
109 }
110
111 /* prepare the request structure */
112 g_vmmreqMouseStatus = malloc(vmmdevGetRequestSize(VMMDevReq_GetMouseStatus));
113 if (!g_vmmreqMouseStatus)
114 {
115 ErrorF("Ran out of memory while querying the virtual machine for the mouse capabilities.\n");
116 return 1;
117 }
118 vmmdevInitRequest((VMMDevRequestHeader*)g_vmmreqMouseStatus, VMMDevReq_GetMouseStatus);
119
120 /* tell the host that we want absolute coordinates */
121 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
122 req.mouseFeatures = VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR;
123 req.pointerXPos = 0;
124 req.pointerYPos = 0;
125/** @todo r=bird: Michael, I thought we decided a long time ago that all these should be replaced by VbglR3. I assume this is just a leftover... */
126 if (ioctl(g_vboxguestHandle, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(req)), (void*)&req) < 0)
127 {
128 ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
129 errno, strerror(errno));
130 return 1;
131 }
132 /* everything is fine, put out some branding */
133 xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
134 return 0;
135}
136
137/**
138 * Queries the absolute mouse position from the host.
139 *
140 * Returns 0 on success.
141 * Returns 1 when the host doesn't want absolute coordinates (no coordinates returned)
142 * Otherwise > 1 which means unsuccessful.
143 */
144int VBoxMouseQueryPosition(unsigned int *abs_x, unsigned int *abs_y)
145{
146 /* If we failed to initialise, say that we don't want absolute co-ordinates. */
147 if (g_vboxguestHandle < 0)
148 return 1;
149 /* perform VMM request */
150/** @todo r=bird: Michael, ditto. */
151 if (ioctl(g_vboxguestHandle, VBOXGUEST_IOCTL_VMMREQUEST(vmmdevGetRequestSize(VMMDevReq_GetMouseStatus)), (void*)g_vmmreqMouseStatus) >= 0)
152 {
153 if (RT_SUCCESS(g_vmmreqMouseStatus->header.rc))
154 {
155 /* does the host want absolute coordinates? */
156 if (g_vmmreqMouseStatus->mouseFeatures & VMMDEV_MOUSE_HOST_CAN_ABSOLUTE)
157 {
158 *abs_x = g_vmmreqMouseStatus->pointerXPos;
159 *abs_y = g_vmmreqMouseStatus->pointerYPos;
160 return 0;
161 }
162 return 1;
163 }
164 ErrorF("Error querying host mouse position! header.rc = %d\n", g_vmmreqMouseStatus->header.rc);
165 }
166 else
167 {
168 ErrorF("Error performing VMM request! errno = %d (%s)\n",
169 errno, strerror(errno));
170 }
171 /* error! */
172 return 2;
173}
174
175int VBoxMouseFini(void)
176{
177 VMMDevReqMouseStatus req;
178 /* If we are not initialised, there is nothing to do */
179 if (g_vboxguestHandle < 0)
180 return 0;
181 /* tell VMM that we no longer support absolute mouse handling */
182 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
183 req.mouseFeatures = 0;
184 req.pointerXPos = 0;
185 req.pointerYPos = 0;
186/** @todo r=bird: Michael, ditto. */
187 if (ioctl(g_vboxguestHandle, VBOXGUEST_IOCTL_VMMREQUEST(sizeof(req)), (void*)&req) < 0)
188 {
189 ErrorF("ioctl to vboxguest module failed, rc = %d (%s)\n",
190 errno, strerror(errno));
191 }
192
193 free(g_vmmreqMouseStatus);
194 g_vmmreqMouseStatus = NULL;
195 close(g_vboxguestHandle);
196 g_vboxguestHandle = -1;
197 return 0;
198}
199#endif /* RT_OS_LINUX */
200
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