VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xmouse/VBoxUtils.c@ 6241

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

Guest r3 lib for x11 mouse. The solaris mouse driver doesn't yet work like it should.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette