VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/xmouse/VBoxUtils.c@ 1284

Last change on this file since 1284 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/** @file
2 *
3 * VirtualBox Linux Additions mouse driver utility functions
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#include <VBox/VBoxGuest.h>
23#include "VBoxUtils.h"
24
25#include "xf86.h"
26#define NEED_XF86_TYPES
27#include "xf86_ansic.h"
28#include "compiler.h"
29
30#include <asm/ioctl.h>
31
32/* the vboxadd module file handle */
33static int g_vboxaddHandle = -1;
34/* the request structure */
35static VMMDevReqMouseStatus *g_vmmreqMouseStatus = NULL;
36
37/**
38 * Initialise mouse integration. Returns 0 on success and 1 on failure
39 * (for example, if the VBox kernel module is not loaded).
40 */
41int VBoxMouseInit(void)
42{
43 VMMDevReqMouseStatus req;
44
45 /* return immediately if already initialized */
46 if (g_vboxaddHandle != -1)
47 return 0;
48
49 /* open the driver */
50 g_vboxaddHandle = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
51 if (g_vboxaddHandle < 0)
52 {
53 ErrorF("Unable to open the virtual machine device: %s\n",
54 strerror(errno));
55 return 1;
56 }
57
58 /* prepare the request structure */
59 g_vmmreqMouseStatus = malloc(vmmdevGetRequestSize(VMMDevReq_GetMouseStatus));
60 if (!g_vmmreqMouseStatus)
61 {
62 ErrorF("Ran out of memory while querying the virtual machine for the mouse capabilities.\n");
63 return 1;
64 }
65 vmmdevInitRequest((VMMDevRequestHeader*)g_vmmreqMouseStatus, VMMDevReq_GetMouseStatus);
66
67 /* tell the host that we want absolute coordinates */
68 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
69 req.mouseFeatures = VBOXGUEST_MOUSE_GUEST_CAN_ABSOLUTE | VBOXGUEST_MOUSE_GUEST_NEEDS_HOST_CURSOR;
70 req.pointerXPos = 0;
71 req.pointerYPos = 0;
72 if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) < 0)
73 {
74 ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
75 errno, strerror(errno));
76 return 1;
77 }
78 /* everything is fine, put out some branding */
79 xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
80 return 0;
81}
82
83/**
84 * Queries the absolute mouse position from the host.
85 *
86 * Returns 0 on success.
87 * Returns 1 when the host doesn't want absolute coordinates (no coordinates returned)
88 * Otherwise > 1 which means unsuccessful.
89 */
90int VBoxMouseQueryPosition(unsigned int *abs_x, unsigned int *abs_y)
91{
92 /* If we failed to initialise, say that we don't want absolute co-ordinates. */
93 if (g_vboxaddHandle < 0)
94 return 1;
95 /* perform VMM request */
96 if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)g_vmmreqMouseStatus) >= 0)
97 {
98 if (VBOX_SUCCESS(g_vmmreqMouseStatus->header.rc))
99 {
100 /* does the host want absolute coordinates? */
101 if (g_vmmreqMouseStatus->mouseFeatures & VBOXGUEST_MOUSE_HOST_CAN_ABSOLUTE)
102 {
103 *abs_x = g_vmmreqMouseStatus->pointerXPos;
104 *abs_y = g_vmmreqMouseStatus->pointerYPos;
105 return 0;
106 }
107 else
108 return 1;
109 }
110 else
111 {
112 ErrorF("Error querying host mouse position! header.rc = %d\n", g_vmmreqMouseStatus->header.rc);
113 }
114 }
115 else
116 {
117 ErrorF("Error performing VMM request! errno = %d (%s)\n",
118 errno, strerror(errno));
119 }
120 /* error! */
121 return 2;
122}
123
124int VBoxMouseFini(void)
125{
126 VMMDevReqMouseStatus req;
127 /* If we are not initialised, there is nothing to do */
128 if (g_vboxaddHandle < 0)
129 return 0;
130 /* tell VMM that we no longer support absolute mouse handling */
131 vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
132 req.mouseFeatures = 0;
133 req.pointerXPos = 0;
134 req.pointerYPos = 0;
135 if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) < 0)
136 {
137 ErrorF("ioctl to vboxadd module failed, rc = %d (%s)\n",
138 errno, strerror(errno));
139 }
140
141 free(g_vmmreqMouseStatus);
142 g_vmmreqMouseStatus = NULL;
143 close(g_vboxaddHandle);
144 g_vboxaddHandle = -1;
145 return 0;
146}
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