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 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 |
|
---|
18 | #include <VBox/VBoxGuest.h>
|
---|
19 | #include "VBoxUtils.h"
|
---|
20 |
|
---|
21 | #include "xf86.h"
|
---|
22 | #define NEED_XF86_TYPES
|
---|
23 | #include "xf86_ansic.h"
|
---|
24 | #include "compiler.h"
|
---|
25 |
|
---|
26 | #include <asm/ioctl.h>
|
---|
27 |
|
---|
28 | /* the vboxadd module file handle */
|
---|
29 | static int g_vboxaddHandle = -1;
|
---|
30 | /* the request structure */
|
---|
31 | static VMMDevReqMouseStatus *g_vmmreqMouseStatus = NULL;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Initialise mouse integration. Returns 0 on success and 1 on failure
|
---|
35 | * (for example, if the VBox kernel module is not loaded).
|
---|
36 | */
|
---|
37 | int VBoxMouseInit(void)
|
---|
38 | {
|
---|
39 | VMMDevReqMouseStatus req;
|
---|
40 |
|
---|
41 | /* return immediately if already initialized */
|
---|
42 | if (g_vboxaddHandle != -1)
|
---|
43 | return 0;
|
---|
44 |
|
---|
45 | /* open the driver */
|
---|
46 | g_vboxaddHandle = open(VBOXGUEST_DEVICE_NAME, O_RDWR, 0);
|
---|
47 | if (g_vboxaddHandle < 0)
|
---|
48 | {
|
---|
49 | ErrorF("Unable to open the virtual machine device: %s\n",
|
---|
50 | strerror(errno));
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* prepare the request structure */
|
---|
55 | g_vmmreqMouseStatus = malloc(vmmdevGetRequestSize(VMMDevReq_GetMouseStatus));
|
---|
56 | if (!g_vmmreqMouseStatus)
|
---|
57 | {
|
---|
58 | ErrorF("Ran out of memory while querying the virtual machine for the mouse capabilities.\n");
|
---|
59 | return 1;
|
---|
60 | }
|
---|
61 | vmmdevInitRequest((VMMDevRequestHeader*)g_vmmreqMouseStatus, VMMDevReq_GetMouseStatus);
|
---|
62 |
|
---|
63 | /* tell the host that we want absolute coordinates */
|
---|
64 | vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
|
---|
65 | req.mouseFeatures = VBOXGUEST_MOUSE_GUEST_CAN_ABSOLUTE | VBOXGUEST_MOUSE_GUEST_NEEDS_HOST_CURSOR;
|
---|
66 | req.pointerXPos = 0;
|
---|
67 | req.pointerYPos = 0;
|
---|
68 | if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) < 0)
|
---|
69 | {
|
---|
70 | ErrorF("Error sending mouse pointer capabilities to VMM! rc = %d (%s)\n",
|
---|
71 | errno, strerror(errno));
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 | /* everything is fine, put out some branding */
|
---|
75 | xf86Msg(X_INFO, "VirtualBox mouse pointer integration available.\n");
|
---|
76 | return 0;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Queries the absolute mouse position from the host.
|
---|
81 | *
|
---|
82 | * Returns 0 on success.
|
---|
83 | * Returns 1 when the host doesn't want absolute coordinates (no coordinates returned)
|
---|
84 | * Otherwise > 1 which means unsuccessful.
|
---|
85 | */
|
---|
86 | int VBoxMouseQueryPosition(unsigned int *abs_x, unsigned int *abs_y)
|
---|
87 | {
|
---|
88 | /* If we failed to initialise, say that we don't want absolute co-ordinates. */
|
---|
89 | if (g_vboxaddHandle < 0)
|
---|
90 | return 1;
|
---|
91 | /* perform VMM request */
|
---|
92 | if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)g_vmmreqMouseStatus) >= 0)
|
---|
93 | {
|
---|
94 | if (VBOX_SUCCESS(g_vmmreqMouseStatus->header.rc))
|
---|
95 | {
|
---|
96 | /* does the host want absolute coordinates? */
|
---|
97 | if (g_vmmreqMouseStatus->mouseFeatures & VBOXGUEST_MOUSE_HOST_CAN_ABSOLUTE)
|
---|
98 | {
|
---|
99 | *abs_x = g_vmmreqMouseStatus->pointerXPos;
|
---|
100 | *abs_y = g_vmmreqMouseStatus->pointerYPos;
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 | else
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | ErrorF("Error querying host mouse position! header.rc = %d\n", g_vmmreqMouseStatus->header.rc);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else
|
---|
112 | {
|
---|
113 | ErrorF("Error performing VMM request! errno = %d (%s)\n",
|
---|
114 | errno, strerror(errno));
|
---|
115 | }
|
---|
116 | /* error! */
|
---|
117 | return 2;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int VBoxMouseFini(void)
|
---|
121 | {
|
---|
122 | VMMDevReqMouseStatus req;
|
---|
123 | /* If we are not initialised, there is nothing to do */
|
---|
124 | if (g_vboxaddHandle < 0)
|
---|
125 | return 0;
|
---|
126 | /* tell VMM that we no longer support absolute mouse handling */
|
---|
127 | vmmdevInitRequest((VMMDevRequestHeader*)&req, VMMDevReq_SetMouseStatus);
|
---|
128 | req.mouseFeatures = 0;
|
---|
129 | req.pointerXPos = 0;
|
---|
130 | req.pointerYPos = 0;
|
---|
131 | if (ioctl(g_vboxaddHandle, IOCTL_VBOXGUEST_VMMREQUEST, (void*)&req) < 0)
|
---|
132 | {
|
---|
133 | ErrorF("ioctl to vboxadd module failed, rc = %d (%s)\n",
|
---|
134 | errno, strerror(errno));
|
---|
135 | }
|
---|
136 |
|
---|
137 | free(g_vmmreqMouseStatus);
|
---|
138 | g_vmmreqMouseStatus = NULL;
|
---|
139 | close(g_vboxaddHandle);
|
---|
140 | g_vboxaddHandle = -1;
|
---|
141 | return 0;
|
---|
142 | }
|
---|