1706f2543Smrg/************************************************************
2706f2543Smrg
3706f2543SmrgCopyright 1989, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included in
12706f2543Smrgall copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15706f2543SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16706f2543SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17706f2543SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18706f2543SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19706f2543SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20706f2543Smrg
21706f2543SmrgExcept as contained in this notice, the name of The Open Group shall not be
22706f2543Smrgused in advertising or otherwise to promote the sale, use or other dealings
23706f2543Smrgin this Software without prior written authorization from The Open Group.
24706f2543Smrg
25706f2543SmrgCopyright 1989 by Hewlett-Packard Company, Palo Alto, California.
26706f2543Smrg
27706f2543Smrg			All Rights Reserved
28706f2543Smrg
29706f2543SmrgPermission to use, copy, modify, and distribute this software and its
30706f2543Smrgdocumentation for any purpose and without fee is hereby granted,
31706f2543Smrgprovided that the above copyright notice appear in all copies and that
32706f2543Smrgboth that copyright notice and this permission notice appear in
33706f2543Smrgsupporting documentation, and that the name of Hewlett-Packard not be
34706f2543Smrgused in advertising or publicity pertaining to distribution of the
35706f2543Smrgsoftware without specific, written prior permission.
36706f2543Smrg
37706f2543SmrgHEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38706f2543SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39706f2543SmrgHEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40706f2543SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41706f2543SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42706f2543SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
43706f2543SmrgSOFTWARE.
44706f2543Smrg
45706f2543Smrg********************************************************/
46706f2543Smrg
47706f2543Smrg/***********************************************************************
48706f2543Smrg *
49706f2543Smrg * Extension function to list the available input devices.
50706f2543Smrg *
51706f2543Smrg */
52706f2543Smrg
53706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
54706f2543Smrg#include <dix-config.h>
55706f2543Smrg#endif
56706f2543Smrg
57706f2543Smrg#include <X11/X.h>	/* for inputstr.h    */
58706f2543Smrg#include <X11/Xproto.h>	/* Request macro     */
59706f2543Smrg#include "inputstr.h"	/* DeviceIntPtr      */
60706f2543Smrg#include <X11/extensions/XI.h>
61706f2543Smrg#include <X11/extensions/XIproto.h>
62706f2543Smrg#include "XIstubs.h"
63706f2543Smrg#include "extnsionst.h"
64706f2543Smrg#include "exevents.h"
65706f2543Smrg#include "xace.h"
66706f2543Smrg#include "xkbsrv.h"
67706f2543Smrg#include "xkbstr.h"
68706f2543Smrg
69706f2543Smrg#include "listdev.h"
70706f2543Smrg
71706f2543Smrg
72706f2543Smrg/***********************************************************************
73706f2543Smrg *
74706f2543Smrg * This procedure lists the input devices available to the server.
75706f2543Smrg *
76706f2543Smrg */
77706f2543Smrg
78706f2543Smrgint
79706f2543SmrgSProcXListInputDevices(ClientPtr client)
80706f2543Smrg{
81706f2543Smrg    char n;
82706f2543Smrg
83706f2543Smrg    REQUEST(xListInputDevicesReq);
84706f2543Smrg    swaps(&stuff->length, n);
85706f2543Smrg    return (ProcXListInputDevices(client));
86706f2543Smrg}
87706f2543Smrg
88706f2543Smrg/***********************************************************************
89706f2543Smrg *
90706f2543Smrg * This procedure calculates the size of the information to be returned
91706f2543Smrg * for an input device.
92706f2543Smrg *
93706f2543Smrg */
94706f2543Smrg
95706f2543Smrgstatic void
96706f2543SmrgSizeDeviceInfo(DeviceIntPtr d, int *namesize, int *size)
97706f2543Smrg{
98706f2543Smrg    int chunks;
99706f2543Smrg
100706f2543Smrg    *namesize += 1;
101706f2543Smrg    if (d->name)
102706f2543Smrg	*namesize += strlen(d->name);
103706f2543Smrg    if (d->key != NULL)
104706f2543Smrg	*size += sizeof(xKeyInfo);
105706f2543Smrg    if (d->button != NULL)
106706f2543Smrg	*size += sizeof(xButtonInfo);
107706f2543Smrg    if (d->valuator != NULL) {
108706f2543Smrg	chunks = ((int)d->valuator->numAxes + 19) / VPC;
109706f2543Smrg	*size += (chunks * sizeof(xValuatorInfo) +
110706f2543Smrg		  d->valuator->numAxes * sizeof(xAxisInfo));
111706f2543Smrg    }
112706f2543Smrg}
113706f2543Smrg
114706f2543Smrg/***********************************************************************
115706f2543Smrg *
116706f2543Smrg * This procedure copies data to the DeviceInfo struct, swapping if necessary.
117706f2543Smrg *
118706f2543Smrg * We need the extra byte in the allocated buffer, because the trailing null
119706f2543Smrg * hammers one extra byte, which is overwritten by the next name except for
120706f2543Smrg * the last name copied.
121706f2543Smrg *
122706f2543Smrg */
123706f2543Smrg
124706f2543Smrgstatic void
125706f2543SmrgCopyDeviceName(char **namebuf, char *name)
126706f2543Smrg{
127706f2543Smrg    char *nameptr = (char *)*namebuf;
128706f2543Smrg
129706f2543Smrg    if (name) {
130706f2543Smrg	*nameptr++ = strlen(name);
131706f2543Smrg	strcpy(nameptr, name);
132706f2543Smrg	*namebuf += (strlen(name) + 1);
133706f2543Smrg    } else {
134706f2543Smrg	*nameptr++ = 0;
135706f2543Smrg	*namebuf += 1;
136706f2543Smrg    }
137706f2543Smrg}
138706f2543Smrg
139706f2543Smrg/***********************************************************************
140706f2543Smrg *
141706f2543Smrg * This procedure copies ButtonClass information, swapping if necessary.
142706f2543Smrg *
143706f2543Smrg */
144706f2543Smrg
145706f2543Smrgstatic void
146706f2543SmrgCopySwapButtonClass(ClientPtr client, ButtonClassPtr b, char **buf)
147706f2543Smrg{
148706f2543Smrg    char n;
149706f2543Smrg    xButtonInfoPtr b2;
150706f2543Smrg
151706f2543Smrg    b2 = (xButtonInfoPtr) * buf;
152706f2543Smrg    b2->class = ButtonClass;
153706f2543Smrg    b2->length = sizeof(xButtonInfo);
154706f2543Smrg    b2->num_buttons = b->numButtons;
155706f2543Smrg    if (client && client->swapped) {
156706f2543Smrg	swaps(&b2->num_buttons, n);	/* macro - braces are required */
157706f2543Smrg    }
158706f2543Smrg    *buf += sizeof(xButtonInfo);
159706f2543Smrg}
160706f2543Smrg
161706f2543Smrg/***********************************************************************
162706f2543Smrg *
163706f2543Smrg * This procedure copies data to the DeviceInfo struct, swapping if necessary.
164706f2543Smrg *
165706f2543Smrg */
166706f2543Smrg
167706f2543Smrgstatic void
168706f2543SmrgCopySwapDevice(ClientPtr client, DeviceIntPtr d, int num_classes,
169706f2543Smrg	       char **buf)
170706f2543Smrg{
171706f2543Smrg    char n;
172706f2543Smrg    xDeviceInfoPtr dev;
173706f2543Smrg
174706f2543Smrg    dev = (xDeviceInfoPtr) * buf;
175706f2543Smrg
176706f2543Smrg    dev->id = d->id;
177706f2543Smrg    dev->type = d->xinput_type;
178706f2543Smrg    dev->num_classes = num_classes;
179706f2543Smrg    if (IsMaster(d) && IsKeyboardDevice(d))
180706f2543Smrg	dev->use = IsXKeyboard;
181706f2543Smrg    else if (IsMaster(d) && IsPointerDevice(d))
182706f2543Smrg	dev->use = IsXPointer;
183706f2543Smrg    else if (d->valuator && d->button)
184706f2543Smrg        dev->use = IsXExtensionPointer;
185706f2543Smrg    else if (d->key && d->kbdfeed)
186706f2543Smrg        dev->use = IsXExtensionKeyboard;
187706f2543Smrg    else
188706f2543Smrg	dev->use = IsXExtensionDevice;
189706f2543Smrg
190706f2543Smrg    if (client->swapped) {
191706f2543Smrg	swapl(&dev->type, n);	/* macro - braces are required */
192706f2543Smrg    }
193706f2543Smrg    *buf += sizeof(xDeviceInfo);
194706f2543Smrg}
195706f2543Smrg
196706f2543Smrg/***********************************************************************
197706f2543Smrg *
198706f2543Smrg * This procedure copies KeyClass information, swapping if necessary.
199706f2543Smrg *
200706f2543Smrg */
201706f2543Smrg
202706f2543Smrgstatic void
203706f2543SmrgCopySwapKeyClass(ClientPtr client, KeyClassPtr k, char **buf)
204706f2543Smrg{
205706f2543Smrg    char n;
206706f2543Smrg    xKeyInfoPtr k2;
207706f2543Smrg
208706f2543Smrg    k2 = (xKeyInfoPtr) * buf;
209706f2543Smrg    k2->class = KeyClass;
210706f2543Smrg    k2->length = sizeof(xKeyInfo);
211706f2543Smrg    k2->min_keycode = k->xkbInfo->desc->min_key_code;
212706f2543Smrg    k2->max_keycode = k->xkbInfo->desc->max_key_code;
213706f2543Smrg    k2->num_keys = k2->max_keycode - k2->min_keycode + 1;
214706f2543Smrg    if (client && client->swapped) {
215706f2543Smrg	swaps(&k2->num_keys, n);
216706f2543Smrg    }
217706f2543Smrg    *buf += sizeof(xKeyInfo);
218706f2543Smrg}
219706f2543Smrg
220706f2543Smrg/***********************************************************************
221706f2543Smrg *
222706f2543Smrg * This procedure copies ValuatorClass information, swapping if necessary.
223706f2543Smrg *
224706f2543Smrg * Devices may have up to 255 valuators.  The length of a ValuatorClass is
225706f2543Smrg * defined to be sizeof(ValuatorClassInfo) + num_axes * sizeof (xAxisInfo).
226706f2543Smrg * The maximum length is therefore (8 + 255 * 12) = 3068.  However, the
227706f2543Smrg * length field is one byte.  If a device has more than 20 valuators, we
228706f2543Smrg * must therefore return multiple valuator classes to the client.
229706f2543Smrg *
230706f2543Smrg */
231706f2543Smrg
232706f2543Smrgstatic int
233706f2543SmrgCopySwapValuatorClass(ClientPtr client, DeviceIntPtr dev, char **buf)
234706f2543Smrg{
235706f2543Smrg    int i, j, axes, t_axes;
236706f2543Smrg    char n;
237706f2543Smrg    ValuatorClassPtr v = dev->valuator;
238706f2543Smrg    xValuatorInfoPtr v2;
239706f2543Smrg    AxisInfo *a;
240706f2543Smrg    xAxisInfoPtr a2;
241706f2543Smrg
242706f2543Smrg    for (i = 0, axes = v->numAxes; i < ((v->numAxes + 19) / VPC);
243706f2543Smrg	 i++, axes -= VPC) {
244706f2543Smrg	t_axes = axes < VPC ? axes : VPC;
245706f2543Smrg	if (t_axes < 0)
246706f2543Smrg	    t_axes = v->numAxes % VPC;
247706f2543Smrg	v2 = (xValuatorInfoPtr) * buf;
248706f2543Smrg	v2->class = ValuatorClass;
249706f2543Smrg	v2->length = sizeof(xValuatorInfo) + t_axes * sizeof(xAxisInfo);
250706f2543Smrg	v2->num_axes = t_axes;
251706f2543Smrg	v2->mode = valuator_get_mode(dev, 0);
252706f2543Smrg	v2->motion_buffer_size = v->numMotionEvents;
253706f2543Smrg	if (client && client->swapped) {
254706f2543Smrg	    swapl(&v2->motion_buffer_size, n);
255706f2543Smrg	}
256706f2543Smrg	*buf += sizeof(xValuatorInfo);
257706f2543Smrg	a = v->axes + (VPC * i);
258706f2543Smrg	a2 = (xAxisInfoPtr) * buf;
259706f2543Smrg	for (j = 0; j < t_axes; j++) {
260706f2543Smrg	    a2->min_value = a->min_value;
261706f2543Smrg	    a2->max_value = a->max_value;
262706f2543Smrg	    a2->resolution = a->resolution;
263706f2543Smrg	    if (client && client->swapped) {
264706f2543Smrg		swapl(&a2->min_value, n);
265706f2543Smrg		swapl(&a2->max_value, n);
266706f2543Smrg		swapl(&a2->resolution, n);
267706f2543Smrg	    }
268706f2543Smrg	    a2++;
269706f2543Smrg	    a++;
270706f2543Smrg	    *buf += sizeof(xAxisInfo);
271706f2543Smrg	}
272706f2543Smrg    }
273706f2543Smrg    return i;
274706f2543Smrg}
275706f2543Smrg
276706f2543Smrgstatic void
277706f2543SmrgCopySwapClasses(ClientPtr client, DeviceIntPtr dev, CARD8 *num_classes,
278706f2543Smrg                char** classbuf)
279706f2543Smrg{
280706f2543Smrg    if (dev->key != NULL) {
281706f2543Smrg	CopySwapKeyClass(client, dev->key, classbuf);
282706f2543Smrg	(*num_classes)++;
283706f2543Smrg    }
284706f2543Smrg    if (dev->button != NULL) {
285706f2543Smrg	CopySwapButtonClass(client, dev->button, classbuf);
286706f2543Smrg	(*num_classes)++;
287706f2543Smrg    }
288706f2543Smrg    if (dev->valuator != NULL) {
289706f2543Smrg	(*num_classes) +=
290706f2543Smrg	    CopySwapValuatorClass(client, dev, classbuf);
291706f2543Smrg    }
292706f2543Smrg}
293706f2543Smrg
294706f2543Smrg/***********************************************************************
295706f2543Smrg *
296706f2543Smrg * This procedure lists information to be returned for an input device.
297706f2543Smrg *
298706f2543Smrg */
299706f2543Smrg
300706f2543Smrgstatic void
301706f2543SmrgListDeviceInfo(ClientPtr client, DeviceIntPtr d, xDeviceInfoPtr dev,
302706f2543Smrg	       char **devbuf, char **classbuf, char **namebuf)
303706f2543Smrg{
304706f2543Smrg    CopyDeviceName(namebuf, d->name);
305706f2543Smrg    CopySwapDevice(client, d, 0, devbuf);
306706f2543Smrg    CopySwapClasses(client, d, &dev->num_classes, classbuf);
307706f2543Smrg}
308706f2543Smrg
309706f2543Smrg/***********************************************************************
310706f2543Smrg *
311706f2543Smrg * This procedure checks if a device should be left off the list.
312706f2543Smrg *
313706f2543Smrg */
314706f2543Smrg
315706f2543Smrgstatic Bool
316706f2543SmrgShouldSkipDevice(ClientPtr client, DeviceIntPtr d)
317706f2543Smrg{
318706f2543Smrg    /* don't send master devices other than VCP/VCK */
319706f2543Smrg    if (!IsMaster(d) || d == inputInfo.pointer || d == inputInfo.keyboard)
320706f2543Smrg    {
321706f2543Smrg        int rc = XaceHook(XACE_DEVICE_ACCESS, client, d, DixGetAttrAccess);
322706f2543Smrg        if (rc == Success)
323706f2543Smrg            return FALSE;
324706f2543Smrg    }
325706f2543Smrg    return TRUE;
326706f2543Smrg}
327706f2543Smrg
328706f2543Smrg
329706f2543Smrg/***********************************************************************
330706f2543Smrg *
331706f2543Smrg * This procedure lists the input devices available to the server.
332706f2543Smrg *
333706f2543Smrg * If this request is called by a client that has not issued a
334706f2543Smrg * GetExtensionVersion request with major/minor version set, we don't send the
335706f2543Smrg * complete device list. Instead, we only send the VCP, the VCK and floating
336706f2543Smrg * SDs. This resembles the setup found on XI 1.x machines.
337706f2543Smrg */
338706f2543Smrg
339706f2543Smrgint
340706f2543SmrgProcXListInputDevices(ClientPtr client)
341706f2543Smrg{
342706f2543Smrg    xListInputDevicesReply rep;
343706f2543Smrg    int numdevs = 0;
344706f2543Smrg    int namesize = 1;	/* need 1 extra byte for strcpy */
345706f2543Smrg    int i = 0, size = 0;
346706f2543Smrg    int total_length;
347706f2543Smrg    char *devbuf, *classbuf, *namebuf, *savbuf;
348706f2543Smrg    Bool *skip;
349706f2543Smrg    xDeviceInfo *dev;
350706f2543Smrg    DeviceIntPtr d;
351706f2543Smrg
352706f2543Smrg    REQUEST_SIZE_MATCH(xListInputDevicesReq);
353706f2543Smrg
354706f2543Smrg    memset(&rep, 0, sizeof(xListInputDevicesReply));
355706f2543Smrg    rep.repType = X_Reply;
356706f2543Smrg    rep.RepType = X_ListInputDevices;
357706f2543Smrg    rep.length = 0;
358706f2543Smrg    rep.sequenceNumber = client->sequence;
359706f2543Smrg
360706f2543Smrg    /* allocate space for saving skip value */
361706f2543Smrg    skip = calloc(sizeof(Bool), inputInfo.numDevices);
362706f2543Smrg    if (!skip)
363706f2543Smrg        return BadAlloc;
364706f2543Smrg
365706f2543Smrg    /* figure out which devices to skip */
366706f2543Smrg    numdevs = 0;
367706f2543Smrg    for (d = inputInfo.devices; d; d = d->next, i++) {
368706f2543Smrg        skip[i] = ShouldSkipDevice(client, d);
369706f2543Smrg        if (skip[i])
370706f2543Smrg            continue;
371706f2543Smrg
372706f2543Smrg        SizeDeviceInfo(d, &namesize, &size);
373706f2543Smrg        numdevs++;
374706f2543Smrg    }
375706f2543Smrg
376706f2543Smrg    for (d = inputInfo.off_devices; d; d = d->next, i++) {
377706f2543Smrg        skip[i] = ShouldSkipDevice(client, d);
378706f2543Smrg        if (skip[i])
379706f2543Smrg            continue;
380706f2543Smrg
381706f2543Smrg        SizeDeviceInfo(d, &namesize, &size);
382706f2543Smrg        numdevs++;
383706f2543Smrg    }
384706f2543Smrg
385706f2543Smrg    /* allocate space for reply */
386706f2543Smrg    total_length = numdevs * sizeof(xDeviceInfo) + size + namesize;
387706f2543Smrg    devbuf = (char *)calloc(1, total_length);
388706f2543Smrg    classbuf = devbuf + (numdevs * sizeof(xDeviceInfo));
389706f2543Smrg    namebuf = classbuf + size;
390706f2543Smrg    savbuf = devbuf;
391706f2543Smrg
392706f2543Smrg    /* fill in and send reply */
393706f2543Smrg    i = 0;
394706f2543Smrg    dev = (xDeviceInfoPtr) devbuf;
395706f2543Smrg    for (d = inputInfo.devices; d; d = d->next, i++) {
396706f2543Smrg        if (skip[i])
397706f2543Smrg            continue;
398706f2543Smrg
399706f2543Smrg        ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
400706f2543Smrg    }
401706f2543Smrg
402706f2543Smrg    for (d = inputInfo.off_devices; d; d = d->next, i++) {
403706f2543Smrg        if (skip[i])
404706f2543Smrg            continue;
405706f2543Smrg
406706f2543Smrg        ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
407706f2543Smrg    }
408706f2543Smrg    rep.ndevices = numdevs;
409706f2543Smrg    rep.length = bytes_to_int32(total_length);
410706f2543Smrg    WriteReplyToClient(client, sizeof(xListInputDevicesReply), &rep);
411706f2543Smrg    WriteToClient(client, total_length, savbuf);
412706f2543Smrg    free(savbuf);
413706f2543Smrg    free(skip);
414706f2543Smrg    return Success;
415706f2543Smrg}
416706f2543Smrg
417706f2543Smrg/***********************************************************************
418706f2543Smrg *
419706f2543Smrg * This procedure writes the reply for the XListInputDevices function,
420706f2543Smrg * if the client and server have a different byte ordering.
421706f2543Smrg *
422706f2543Smrg */
423706f2543Smrg
424706f2543Smrgvoid
425706f2543SmrgSRepXListInputDevices(ClientPtr client, int size, xListInputDevicesReply * rep)
426706f2543Smrg{
427706f2543Smrg    char n;
428706f2543Smrg
429706f2543Smrg    swaps(&rep->sequenceNumber, n);
430706f2543Smrg    swapl(&rep->length, n);
431706f2543Smrg    WriteToClient(client, size, (char *)rep);
432706f2543Smrg}
433