1 /************************************************************
3 Copyright 1989, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
25 Copyright 1989 by Hewlett-Packard Company, Palo Alto, California.
29 Permission to use, copy, modify, and distribute this software and its
30 documentation for any purpose and without fee is hereby granted,
31 provided that the above copyright notice appear in all copies and that
32 both that copyright notice and this permission notice appear in
33 supporting documentation, and that the name of Hewlett-Packard not be
34 used in advertising or publicity pertaining to distribution of the
35 software without specific, written prior permission.
37 HEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 HEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ********************************************************/
47 /***********************************************************************
49 * Extension function to list the available input devices.
53 #include <dix-config.h>
55 #include <X11/X.h> /* for inputstr.h */
56 #include <X11/Xproto.h> /* Request macro */
57 #include <X11/extensions/XI.h>
58 #include <X11/extensions/XIproto.h>
60 #include "dix/input_priv.h"
62 #include "inputstr.h" /* DeviceIntPtr */
64 #include "extnsionst.h"
71 /***********************************************************************
73 * This procedure lists the input devices available to the server.
78 SProcXListInputDevices(ClientPtr client
)
80 REQUEST(xListInputDevicesReq
);
81 swaps(&stuff
->length
);
82 return (ProcXListInputDevices(client
));
85 /***********************************************************************
87 * This procedure calculates the size of the information to be returned
88 * for an input device.
93 SizeDeviceInfo(DeviceIntPtr d
, int *namesize
, int *size
)
99 *namesize
+= strlen(d
->name
);
101 *size
+= sizeof(xKeyInfo
);
102 if (d
->button
!= NULL
)
103 *size
+= sizeof(xButtonInfo
);
104 if (d
->valuator
!= NULL
) {
105 chunks
= ((int) d
->valuator
->numAxes
+ 19) / VPC
;
106 *size
+= (chunks
* sizeof(xValuatorInfo
) +
107 d
->valuator
->numAxes
* sizeof(xAxisInfo
));
111 /***********************************************************************
113 * This procedure copies data to the DeviceInfo struct, swapping if necessary.
115 * We need the extra byte in the allocated buffer, because the trailing null
116 * hammers one extra byte, which is overwritten by the next name except for
117 * the last name copied.
122 CopyDeviceName(char **namebuf
, const char *name
)
124 char *nameptr
= *namebuf
;
127 *nameptr
++ = strlen(name
);
128 strcpy(nameptr
, name
);
129 *namebuf
+= (strlen(name
) + 1);
137 /***********************************************************************
139 * This procedure copies ButtonClass information, swapping if necessary.
144 CopySwapButtonClass(ClientPtr client
, ButtonClassPtr b
, char **buf
)
148 b2
= (xButtonInfoPtr
) * buf
;
149 b2
->class = ButtonClass
;
150 b2
->length
= sizeof(xButtonInfo
);
151 b2
->num_buttons
= b
->numButtons
;
152 if (client
&& client
->swapped
) {
153 swaps(&b2
->num_buttons
);
155 *buf
+= sizeof(xButtonInfo
);
158 /***********************************************************************
160 * This procedure copies data to the DeviceInfo struct, swapping if necessary.
165 CopySwapDevice(ClientPtr client
, DeviceIntPtr d
, int num_classes
, char **buf
)
169 dev
= (xDeviceInfoPtr
) * buf
;
172 dev
->type
= d
->xinput_type
;
173 dev
->num_classes
= num_classes
;
174 if (IsMaster(d
) && IsKeyboardDevice(d
))
175 dev
->use
= IsXKeyboard
;
176 else if (IsMaster(d
) && IsPointerDevice(d
))
177 dev
->use
= IsXPointer
;
178 else if (d
->valuator
&& d
->button
)
179 dev
->use
= IsXExtensionPointer
;
180 else if (d
->key
&& d
->kbdfeed
)
181 dev
->use
= IsXExtensionKeyboard
;
183 dev
->use
= IsXExtensionDevice
;
185 if (client
->swapped
) {
188 *buf
+= sizeof(xDeviceInfo
);
191 /***********************************************************************
193 * This procedure copies KeyClass information, swapping if necessary.
198 CopySwapKeyClass(ClientPtr client
, KeyClassPtr k
, char **buf
)
202 k2
= (xKeyInfoPtr
) * buf
;
203 k2
->class = KeyClass
;
204 k2
->length
= sizeof(xKeyInfo
);
205 k2
->min_keycode
= k
->xkbInfo
->desc
->min_key_code
;
206 k2
->max_keycode
= k
->xkbInfo
->desc
->max_key_code
;
207 k2
->num_keys
= k2
->max_keycode
- k2
->min_keycode
+ 1;
208 if (client
&& client
->swapped
) {
209 swaps(&k2
->num_keys
);
211 *buf
+= sizeof(xKeyInfo
);
214 /***********************************************************************
216 * This procedure copies ValuatorClass information, swapping if necessary.
218 * Devices may have up to 255 valuators. The length of a ValuatorClass is
219 * defined to be sizeof(ValuatorClassInfo) + num_axes * sizeof (xAxisInfo).
220 * The maximum length is therefore (8 + 255 * 12) = 3068. However, the
221 * length field is one byte. If a device has more than 20 valuators, we
222 * must therefore return multiple valuator classes to the client.
227 CopySwapValuatorClass(ClientPtr client
, DeviceIntPtr dev
, char **buf
)
229 int i
, j
, axes
, t_axes
;
230 ValuatorClassPtr v
= dev
->valuator
;
235 for (i
= 0, axes
= v
->numAxes
; i
< ((v
->numAxes
+ 19) / VPC
);
237 t_axes
= axes
< VPC
? axes
: VPC
;
239 t_axes
= v
->numAxes
% VPC
;
240 v2
= (xValuatorInfoPtr
) * buf
;
241 v2
->class = ValuatorClass
;
242 v2
->length
= sizeof(xValuatorInfo
) + t_axes
* sizeof(xAxisInfo
);
243 v2
->num_axes
= t_axes
;
244 v2
->mode
= valuator_get_mode(dev
, 0);
245 v2
->motion_buffer_size
= v
->numMotionEvents
;
246 if (client
&& client
->swapped
) {
247 swapl(&v2
->motion_buffer_size
);
249 *buf
+= sizeof(xValuatorInfo
);
250 a
= v
->axes
+ (VPC
* i
);
251 a2
= (xAxisInfoPtr
) * buf
;
252 for (j
= 0; j
< t_axes
; j
++) {
253 a2
->min_value
= a
->min_value
;
254 a2
->max_value
= a
->max_value
;
255 a2
->resolution
= a
->resolution
;
256 if (client
&& client
->swapped
) {
257 swapl(&a2
->min_value
);
258 swapl(&a2
->max_value
);
259 swapl(&a2
->resolution
);
263 *buf
+= sizeof(xAxisInfo
);
270 CopySwapClasses(ClientPtr client
, DeviceIntPtr dev
, CARD8
*num_classes
,
273 if (dev
->key
!= NULL
) {
274 CopySwapKeyClass(client
, dev
->key
, classbuf
);
277 if (dev
->button
!= NULL
) {
278 CopySwapButtonClass(client
, dev
->button
, classbuf
);
281 if (dev
->valuator
!= NULL
) {
282 (*num_classes
) += CopySwapValuatorClass(client
, dev
, classbuf
);
286 /***********************************************************************
288 * This procedure lists information to be returned for an input device.
293 ListDeviceInfo(ClientPtr client
, DeviceIntPtr d
, xDeviceInfoPtr dev
,
294 char **devbuf
, char **classbuf
, char **namebuf
)
296 CopyDeviceName(namebuf
, d
->name
);
297 CopySwapDevice(client
, d
, 0, devbuf
);
298 CopySwapClasses(client
, d
, &dev
->num_classes
, classbuf
);
301 /***********************************************************************
303 * This procedure checks if a device should be left off the list.
308 ShouldSkipDevice(ClientPtr client
, DeviceIntPtr d
)
310 /* don't send master devices other than VCP/VCK */
311 if (!IsMaster(d
) || d
== inputInfo
.pointer
||d
== inputInfo
.keyboard
) {
312 int rc
= XaceHookDeviceAccess(client
, d
, DixGetAttrAccess
);
320 /***********************************************************************
322 * This procedure lists the input devices available to the server.
324 * If this request is called by a client that has not issued a
325 * GetExtensionVersion request with major/minor version set, we don't send the
326 * complete device list. Instead, we only send the VCP, the VCK and floating
327 * SDs. This resembles the setup found on XI 1.x machines.
331 ProcXListInputDevices(ClientPtr client
)
333 xListInputDevicesReply rep
;
335 int namesize
= 1; /* need 1 extra byte for strcpy */
338 char *devbuf
, *classbuf
, *namebuf
, *savbuf
;
343 REQUEST_SIZE_MATCH(xListInputDevicesReq
);
345 rep
= (xListInputDevicesReply
) {
347 .RepType
= X_ListInputDevices
,
348 .sequenceNumber
= client
->sequence
,
352 /* allocate space for saving skip value */
353 skip
= calloc(inputInfo
.numDevices
, sizeof(Bool
));
357 /* figure out which devices to skip */
359 for (d
= inputInfo
.devices
; d
; d
= d
->next
, i
++) {
360 skip
[i
] = ShouldSkipDevice(client
, d
);
364 SizeDeviceInfo(d
, &namesize
, &size
);
368 for (d
= inputInfo
.off_devices
; d
; d
= d
->next
, i
++) {
369 skip
[i
] = ShouldSkipDevice(client
, d
);
373 SizeDeviceInfo(d
, &namesize
, &size
);
377 /* allocate space for reply */
378 total_length
= numdevs
* sizeof(xDeviceInfo
) + size
+ namesize
;
379 devbuf
= (char *) calloc(1, total_length
);
380 classbuf
= devbuf
+ (numdevs
* sizeof(xDeviceInfo
));
381 namebuf
= classbuf
+ size
;
384 /* fill in and send reply */
386 dev
= (xDeviceInfoPtr
) devbuf
;
387 for (d
= inputInfo
.devices
; d
; d
= d
->next
, i
++) {
391 ListDeviceInfo(client
, d
, dev
++, &devbuf
, &classbuf
, &namebuf
);
394 for (d
= inputInfo
.off_devices
; d
; d
= d
->next
, i
++) {
398 ListDeviceInfo(client
, d
, dev
++, &devbuf
, &classbuf
, &namebuf
);
400 rep
.ndevices
= numdevs
;
401 rep
.length
= bytes_to_int32(total_length
);
402 WriteReplyToClient(client
, sizeof(xListInputDevicesReply
), &rep
);
403 WriteToClient(client
, total_length
, savbuf
);
409 /***********************************************************************
411 * This procedure writes the reply for the XListInputDevices function,
412 * if the client and server have a different byte ordering.
417 SRepXListInputDevices(ClientPtr client
, int size
, xListInputDevicesReply
* rep
)
419 swaps(&rep
->sequenceNumber
);
421 WriteToClient(client
, size
, rep
);