VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xmouse/VBoxUtils_68.c@ 9642

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

The Big Sun Rebranding Header Change

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