VirtualBox

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

Last change on this file since 39107 was 38648, checked in by vboxsync, 13 years ago

Additions/common and X11: more VBOXGUEST_IOCTL_SET_MOUSE_STATUS adjustments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/** @file
2 *
3 * VirtualBox X11 Additions mouse driver utility functions
4 */
5
6/*
7 * Copyright (C) 2006-2007 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#include <iprt/assert.h>
19#include <VBox/VMMDev.h>
20#include <VBox/VBoxGuest.h>
21#include <VBox/VBoxGuestLib.h>
22#include "VBoxUtils.h"
23
24#include "xf86.h"
25#define NEED_XF86_TYPES
26#include "xf86_ansic.h"
27#include "compiler.h"
28
29#ifndef RT_OS_SOLARIS
30# include <asm/ioctl.h>
31#endif
32
33#ifndef RT_OS_LINUX /** @todo later Linux should also use R3 lib for this */
34int VBoxMouseInit(void)
35{
36 int rc = VbglR3Init();
37 if (RT_FAILURE(rc))
38 {
39 ErrorF("VbglR3Init failed.\n");
40 return 1;
41 }
42
43 rc = VbglR3SetMouseStatus(VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE | VMMDEV_MOUSE_GUEST_NEEDS_HOST_CURSOR);
44 if (RT_FAILURE(rc))
45 {
46 ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
47 errno, strerror(errno));
48 return 1;
49 }
50 xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
51 return 0;
52}
53
54
55int VBoxMouseQueryPosition(unsigned int *puAbsXPos, unsigned int *puAbsYPos)
56{
57 int rc;
58 uint32_t pointerXPos;
59 uint32_t pointerYPos;
60
61 AssertPtrReturn(puAbsXPos, VERR_INVALID_PARAMETER);
62 AssertPtrReturn(puAbsYPos, VERR_INVALID_PARAMETER);
63 rc = VbglR3GetMouseStatus(NULL, &pointerXPos, &pointerYPos);
64 if (RT_SUCCESS(rc))
65 {
66 *puAbsXPos = pointerXPos;
67 *puAbsYPos = pointerYPos;
68 return 0;
69 }
70 return 2;
71}
72
73
74int VBoxMouseFini(void)
75{
76 int rc = VbglR3SetMouseStatus(0);
77 VbglR3Term();
78 return rc;
79}
80#else /* RT_OS_LINUX */
81/* the vboxguest module file handle */
82static int g_vboxguestHandle = -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 uint32_t fFeatures;
93
94 /* return immediately if already initialized */
95 if (g_vboxguestHandle != -1)
96 return 0;
97
98 /* open the driver */
99 g_vboxguestHandle = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
100 if (g_vboxguestHandle < 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 fFeatures = VMMDEV_MOUSE_GUEST_CAN_ABSOLUTE;
118/** @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... */
119 if (ioctl(g_vboxguestHandle, VBOXGUEST_IOCTL_SET_MOUSE_STATUS, &fFeatures)
120 < 0)
121 {
122 ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
123 errno, strerror(errno));
124 return 1;
125 }
126 /* everything is fine, put out some branding */
127 xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
128 return 0;
129}
130
131/**
132 * Queries the absolute mouse position from the host.
133 *
134 * Returns 0 on success.
135 * Returns 1 when the host doesn't want absolute coordinates (no coordinates returned)
136 * Otherwise > 1 which means unsuccessful.
137 */
138int VBoxMouseQueryPosition(unsigned int *abs_x, unsigned int *abs_y)
139{
140 /* If we failed to initialise, say that we don't want absolute co-ordinates. */
141 if (g_vboxguestHandle < 0)
142 return 1;
143 /* perform VMM request */
144/** @todo r=bird: Michael, ditto. */
145 if (ioctl(g_vboxguestHandle, VBOXGUEST_IOCTL_VMMREQUEST(vmmdevGetRequestSize(VMMDevReq_GetMouseStatus)), (void*)g_vmmreqMouseStatus) >= 0)
146 {
147 if (RT_SUCCESS(g_vmmreqMouseStatus->header.rc))
148 {
149 /* does the host want absolute coordinates? */
150 if (g_vmmreqMouseStatus->mouseFeatures & VMMDEV_MOUSE_HOST_WANTS_ABSOLUTE)
151 {
152 *abs_x = g_vmmreqMouseStatus->pointerXPos;
153 *abs_y = g_vmmreqMouseStatus->pointerYPos;
154 return 0;
155 }
156 return 1;
157 }
158 ErrorF("Error querying host mouse position! header.rc = %d\n", g_vmmreqMouseStatus->header.rc);
159 }
160 else
161 {
162 ErrorF("Error performing VMM request! errno = %d (%s)\n",
163 errno, strerror(errno));
164 }
165 /* error! */
166 return 2;
167}
168
169int VBoxMouseFini(void)
170{
171 VMMDevReqMouseStatus req;
172 /* If we are not initialised, there is nothing to do */
173 if (g_vboxguestHandle < 0)
174 return 0;
175 /* Tell VMM that we no longer support absolute mouse handling - done
176 * automatically when we close the handle. */
177 free(g_vmmreqMouseStatus);
178 g_vmmreqMouseStatus = NULL;
179 close(g_vboxguestHandle);
180 g_vboxguestHandle = -1;
181 return 0;
182}
183#endif /* RT_OS_LINUX */
184
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