105b261ecSmrg/************************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1989, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrgCopyright 1989 by Hewlett-Packard Company, Palo Alto, California.
2605b261ecSmrg
2705b261ecSmrg			All Rights Reserved
2805b261ecSmrg
2905b261ecSmrgPermission to use, copy, modify, and distribute this software and its
3005b261ecSmrgdocumentation for any purpose and without fee is hereby granted,
3105b261ecSmrgprovided that the above copyright notice appear in all copies and that
3205b261ecSmrgboth that copyright notice and this permission notice appear in
3305b261ecSmrgsupporting documentation, and that the name of Hewlett-Packard not be
3405b261ecSmrgused in advertising or publicity pertaining to distribution of the
3505b261ecSmrgsoftware without specific, written prior permission.
3605b261ecSmrg
3705b261ecSmrgHEWLETT-PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
3805b261ecSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
3905b261ecSmrgHEWLETT-PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
4005b261ecSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
4105b261ecSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
4205b261ecSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4305b261ecSmrgSOFTWARE.
4405b261ecSmrg
4505b261ecSmrg********************************************************/
4605b261ecSmrg
4705b261ecSmrg/***********************************************************************
4805b261ecSmrg *
4905b261ecSmrg * Extension function to list the available input devices.
5005b261ecSmrg *
5105b261ecSmrg */
5205b261ecSmrg
5305b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
5405b261ecSmrg#include <dix-config.h>
5505b261ecSmrg#endif
5605b261ecSmrg
5735c4bbdfSmrg#include <X11/X.h>              /* for inputstr.h    */
5835c4bbdfSmrg#include <X11/Xproto.h>         /* Request macro     */
5935c4bbdfSmrg#include "inputstr.h"           /* DeviceIntPtr      */
6005b261ecSmrg#include <X11/extensions/XI.h>
6105b261ecSmrg#include <X11/extensions/XIproto.h>
6205b261ecSmrg#include "XIstubs.h"
6305b261ecSmrg#include "extnsionst.h"
644642e01fSmrg#include "exevents.h"
654642e01fSmrg#include "xace.h"
666747b715Smrg#include "xkbsrv.h"
676747b715Smrg#include "xkbstr.h"
6805b261ecSmrg
6905b261ecSmrg#include "listdev.h"
7005b261ecSmrg
7105b261ecSmrg/***********************************************************************
7205b261ecSmrg *
7305b261ecSmrg * This procedure lists the input devices available to the server.
7405b261ecSmrg *
7505b261ecSmrg */
7605b261ecSmrg
771b5d61b8Smrgint _X_COLD
7805b261ecSmrgSProcXListInputDevices(ClientPtr client)
7905b261ecSmrg{
8005b261ecSmrg    REQUEST(xListInputDevicesReq);
8135c4bbdfSmrg    swaps(&stuff->length);
8205b261ecSmrg    return (ProcXListInputDevices(client));
8305b261ecSmrg}
8405b261ecSmrg
8505b261ecSmrg/***********************************************************************
8605b261ecSmrg *
8705b261ecSmrg * This procedure calculates the size of the information to be returned
8805b261ecSmrg * for an input device.
8905b261ecSmrg *
9005b261ecSmrg */
9105b261ecSmrg
926747b715Smrgstatic void
9305b261ecSmrgSizeDeviceInfo(DeviceIntPtr d, int *namesize, int *size)
9405b261ecSmrg{
9505b261ecSmrg    int chunks;
9605b261ecSmrg
9705b261ecSmrg    *namesize += 1;
9805b261ecSmrg    if (d->name)
9935c4bbdfSmrg        *namesize += strlen(d->name);
10005b261ecSmrg    if (d->key != NULL)
10135c4bbdfSmrg        *size += sizeof(xKeyInfo);
10205b261ecSmrg    if (d->button != NULL)
10335c4bbdfSmrg        *size += sizeof(xButtonInfo);
10405b261ecSmrg    if (d->valuator != NULL) {
10535c4bbdfSmrg        chunks = ((int) d->valuator->numAxes + 19) / VPC;
10635c4bbdfSmrg        *size += (chunks * sizeof(xValuatorInfo) +
10735c4bbdfSmrg                  d->valuator->numAxes * sizeof(xAxisInfo));
10805b261ecSmrg    }
10905b261ecSmrg}
11005b261ecSmrg
11105b261ecSmrg/***********************************************************************
11205b261ecSmrg *
11305b261ecSmrg * This procedure copies data to the DeviceInfo struct, swapping if necessary.
11405b261ecSmrg *
11505b261ecSmrg * We need the extra byte in the allocated buffer, because the trailing null
11605b261ecSmrg * hammers one extra byte, which is overwritten by the next name except for
11705b261ecSmrg * the last name copied.
11805b261ecSmrg *
11905b261ecSmrg */
12005b261ecSmrg
12105b261ecSmrgstatic void
12235c4bbdfSmrgCopyDeviceName(char **namebuf, const char *name)
12305b261ecSmrg{
12435c4bbdfSmrg    char *nameptr = *namebuf;
12505b261ecSmrg
12605b261ecSmrg    if (name) {
12735c4bbdfSmrg        *nameptr++ = strlen(name);
12835c4bbdfSmrg        strcpy(nameptr, name);
12935c4bbdfSmrg        *namebuf += (strlen(name) + 1);
13035c4bbdfSmrg    }
13135c4bbdfSmrg    else {
13235c4bbdfSmrg        *nameptr++ = 0;
13335c4bbdfSmrg        *namebuf += 1;
13405b261ecSmrg    }
13505b261ecSmrg}
13605b261ecSmrg
13705b261ecSmrg/***********************************************************************
13805b261ecSmrg *
13905b261ecSmrg * This procedure copies ButtonClass information, swapping if necessary.
14005b261ecSmrg *
14105b261ecSmrg */
14205b261ecSmrg
14305b261ecSmrgstatic void
14405b261ecSmrgCopySwapButtonClass(ClientPtr client, ButtonClassPtr b, char **buf)
14505b261ecSmrg{
14605b261ecSmrg    xButtonInfoPtr b2;
14705b261ecSmrg
14805b261ecSmrg    b2 = (xButtonInfoPtr) * buf;
14905b261ecSmrg    b2->class = ButtonClass;
15005b261ecSmrg    b2->length = sizeof(xButtonInfo);
15105b261ecSmrg    b2->num_buttons = b->numButtons;
1524642e01fSmrg    if (client && client->swapped) {
15335c4bbdfSmrg        swaps(&b2->num_buttons);
15405b261ecSmrg    }
15505b261ecSmrg    *buf += sizeof(xButtonInfo);
15605b261ecSmrg}
15705b261ecSmrg
15805b261ecSmrg/***********************************************************************
15905b261ecSmrg *
16005b261ecSmrg * This procedure copies data to the DeviceInfo struct, swapping if necessary.
16105b261ecSmrg *
16205b261ecSmrg */
16305b261ecSmrg
16405b261ecSmrgstatic void
16535c4bbdfSmrgCopySwapDevice(ClientPtr client, DeviceIntPtr d, int num_classes, char **buf)
16605b261ecSmrg{
16705b261ecSmrg    xDeviceInfoPtr dev;
16805b261ecSmrg
16905b261ecSmrg    dev = (xDeviceInfoPtr) * buf;
17005b261ecSmrg
17105b261ecSmrg    dev->id = d->id;
1726747b715Smrg    dev->type = d->xinput_type;
17305b261ecSmrg    dev->num_classes = num_classes;
1746747b715Smrg    if (IsMaster(d) && IsKeyboardDevice(d))
17535c4bbdfSmrg        dev->use = IsXKeyboard;
1766747b715Smrg    else if (IsMaster(d) && IsPointerDevice(d))
17735c4bbdfSmrg        dev->use = IsXPointer;
17805b261ecSmrg    else if (d->valuator && d->button)
17905b261ecSmrg        dev->use = IsXExtensionPointer;
1808223e2f2Smrg    else if (d->key && d->kbdfeed)
1818223e2f2Smrg        dev->use = IsXExtensionKeyboard;
18205b261ecSmrg    else
18335c4bbdfSmrg        dev->use = IsXExtensionDevice;
1844642e01fSmrg
18505b261ecSmrg    if (client->swapped) {
18635c4bbdfSmrg        swapl(&dev->type);
18705b261ecSmrg    }
18805b261ecSmrg    *buf += sizeof(xDeviceInfo);
18905b261ecSmrg}
19005b261ecSmrg
19105b261ecSmrg/***********************************************************************
19205b261ecSmrg *
19305b261ecSmrg * This procedure copies KeyClass information, swapping if necessary.
19405b261ecSmrg *
19505b261ecSmrg */
19605b261ecSmrg
19705b261ecSmrgstatic void
19805b261ecSmrgCopySwapKeyClass(ClientPtr client, KeyClassPtr k, char **buf)
19905b261ecSmrg{
20005b261ecSmrg    xKeyInfoPtr k2;
20105b261ecSmrg
20205b261ecSmrg    k2 = (xKeyInfoPtr) * buf;
20305b261ecSmrg    k2->class = KeyClass;
20405b261ecSmrg    k2->length = sizeof(xKeyInfo);
2056747b715Smrg    k2->min_keycode = k->xkbInfo->desc->min_key_code;
2066747b715Smrg    k2->max_keycode = k->xkbInfo->desc->max_key_code;
20705b261ecSmrg    k2->num_keys = k2->max_keycode - k2->min_keycode + 1;
2084642e01fSmrg    if (client && client->swapped) {
20935c4bbdfSmrg        swaps(&k2->num_keys);
21005b261ecSmrg    }
21105b261ecSmrg    *buf += sizeof(xKeyInfo);
21205b261ecSmrg}
21305b261ecSmrg
21405b261ecSmrg/***********************************************************************
21505b261ecSmrg *
21605b261ecSmrg * This procedure copies ValuatorClass information, swapping if necessary.
21705b261ecSmrg *
21805b261ecSmrg * Devices may have up to 255 valuators.  The length of a ValuatorClass is
21905b261ecSmrg * defined to be sizeof(ValuatorClassInfo) + num_axes * sizeof (xAxisInfo).
2204642e01fSmrg * The maximum length is therefore (8 + 255 * 12) = 3068.  However, the
22105b261ecSmrg * length field is one byte.  If a device has more than 20 valuators, we
22205b261ecSmrg * must therefore return multiple valuator classes to the client.
22305b261ecSmrg *
22405b261ecSmrg */
22505b261ecSmrg
22605b261ecSmrgstatic int
2279ace9065SmrgCopySwapValuatorClass(ClientPtr client, DeviceIntPtr dev, char **buf)
22805b261ecSmrg{
22905b261ecSmrg    int i, j, axes, t_axes;
2309ace9065Smrg    ValuatorClassPtr v = dev->valuator;
23105b261ecSmrg    xValuatorInfoPtr v2;
23205b261ecSmrg    AxisInfo *a;
23305b261ecSmrg    xAxisInfoPtr a2;
23405b261ecSmrg
23505b261ecSmrg    for (i = 0, axes = v->numAxes; i < ((v->numAxes + 19) / VPC);
23635c4bbdfSmrg         i++, axes -= VPC) {
23735c4bbdfSmrg        t_axes = axes < VPC ? axes : VPC;
23835c4bbdfSmrg        if (t_axes < 0)
23935c4bbdfSmrg            t_axes = v->numAxes % VPC;
24035c4bbdfSmrg        v2 = (xValuatorInfoPtr) * buf;
24135c4bbdfSmrg        v2->class = ValuatorClass;
24235c4bbdfSmrg        v2->length = sizeof(xValuatorInfo) + t_axes * sizeof(xAxisInfo);
24335c4bbdfSmrg        v2->num_axes = t_axes;
24435c4bbdfSmrg        v2->mode = valuator_get_mode(dev, 0);
24535c4bbdfSmrg        v2->motion_buffer_size = v->numMotionEvents;
24635c4bbdfSmrg        if (client && client->swapped) {
24735c4bbdfSmrg            swapl(&v2->motion_buffer_size);
24835c4bbdfSmrg        }
24935c4bbdfSmrg        *buf += sizeof(xValuatorInfo);
25035c4bbdfSmrg        a = v->axes + (VPC * i);
25135c4bbdfSmrg        a2 = (xAxisInfoPtr) * buf;
25235c4bbdfSmrg        for (j = 0; j < t_axes; j++) {
25335c4bbdfSmrg            a2->min_value = a->min_value;
25435c4bbdfSmrg            a2->max_value = a->max_value;
25535c4bbdfSmrg            a2->resolution = a->resolution;
25635c4bbdfSmrg            if (client && client->swapped) {
25735c4bbdfSmrg                swapl(&a2->min_value);
25835c4bbdfSmrg                swapl(&a2->max_value);
25935c4bbdfSmrg                swapl(&a2->resolution);
26035c4bbdfSmrg            }
26135c4bbdfSmrg            a2++;
26235c4bbdfSmrg            a++;
26335c4bbdfSmrg            *buf += sizeof(xAxisInfo);
26435c4bbdfSmrg        }
26505b261ecSmrg    }
2666747b715Smrg    return i;
2676747b715Smrg}
2686747b715Smrg
2696747b715Smrgstatic void
2706747b715SmrgCopySwapClasses(ClientPtr client, DeviceIntPtr dev, CARD8 *num_classes,
27135c4bbdfSmrg                char **classbuf)
2726747b715Smrg{
2736747b715Smrg    if (dev->key != NULL) {
27435c4bbdfSmrg        CopySwapKeyClass(client, dev->key, classbuf);
27535c4bbdfSmrg        (*num_classes)++;
2766747b715Smrg    }
2776747b715Smrg    if (dev->button != NULL) {
27835c4bbdfSmrg        CopySwapButtonClass(client, dev->button, classbuf);
27935c4bbdfSmrg        (*num_classes)++;
2806747b715Smrg    }
2816747b715Smrg    if (dev->valuator != NULL) {
28235c4bbdfSmrg        (*num_classes) += CopySwapValuatorClass(client, dev, classbuf);
2836747b715Smrg    }
28405b261ecSmrg}
28505b261ecSmrg
28605b261ecSmrg/***********************************************************************
28705b261ecSmrg *
28805b261ecSmrg * This procedure lists information to be returned for an input device.
28905b261ecSmrg *
29005b261ecSmrg */
29105b261ecSmrg
29205b261ecSmrgstatic void
29305b261ecSmrgListDeviceInfo(ClientPtr client, DeviceIntPtr d, xDeviceInfoPtr dev,
29435c4bbdfSmrg               char **devbuf, char **classbuf, char **namebuf)
29505b261ecSmrg{
29605b261ecSmrg    CopyDeviceName(namebuf, d->name);
29705b261ecSmrg    CopySwapDevice(client, d, 0, devbuf);
2984642e01fSmrg    CopySwapClasses(client, d, &dev->num_classes, classbuf);
2994642e01fSmrg}
3004642e01fSmrg
3016747b715Smrg/***********************************************************************
3026747b715Smrg *
3036747b715Smrg * This procedure checks if a device should be left off the list.
3046747b715Smrg *
3056747b715Smrg */
3066747b715Smrg
3076747b715Smrgstatic Bool
3086747b715SmrgShouldSkipDevice(ClientPtr client, DeviceIntPtr d)
3094642e01fSmrg{
3106747b715Smrg    /* don't send master devices other than VCP/VCK */
31135c4bbdfSmrg    if (!IsMaster(d) || d == inputInfo.pointer ||d == inputInfo.keyboard) {
3126747b715Smrg        int rc = XaceHook(XACE_DEVICE_ACCESS, client, d, DixGetAttrAccess);
31335c4bbdfSmrg
3146747b715Smrg        if (rc == Success)
3156747b715Smrg            return FALSE;
31605b261ecSmrg    }
3176747b715Smrg    return TRUE;
31805b261ecSmrg}
31905b261ecSmrg
32005b261ecSmrg/***********************************************************************
32105b261ecSmrg *
32205b261ecSmrg * This procedure lists the input devices available to the server.
32305b261ecSmrg *
3244642e01fSmrg * If this request is called by a client that has not issued a
3254642e01fSmrg * GetExtensionVersion request with major/minor version set, we don't send the
3264642e01fSmrg * complete device list. Instead, we only send the VCP, the VCK and floating
3274642e01fSmrg * SDs. This resembles the setup found on XI 1.x machines.
32805b261ecSmrg */
32905b261ecSmrg
33005b261ecSmrgint
33105b261ecSmrgProcXListInputDevices(ClientPtr client)
33205b261ecSmrg{
33305b261ecSmrg    xListInputDevicesReply rep;
33405b261ecSmrg    int numdevs = 0;
33535c4bbdfSmrg    int namesize = 1;           /* need 1 extra byte for strcpy */
3366747b715Smrg    int i = 0, size = 0;
33705b261ecSmrg    int total_length;
3386747b715Smrg    char *devbuf, *classbuf, *namebuf, *savbuf;
3396747b715Smrg    Bool *skip;
34005b261ecSmrg    xDeviceInfo *dev;
34105b261ecSmrg    DeviceIntPtr d;
34205b261ecSmrg
34305b261ecSmrg    REQUEST_SIZE_MATCH(xListInputDevicesReq);
34405b261ecSmrg
34535c4bbdfSmrg    rep = (xListInputDevicesReply) {
34635c4bbdfSmrg        .repType = X_Reply,
34735c4bbdfSmrg        .RepType = X_ListInputDevices,
34835c4bbdfSmrg        .sequenceNumber = client->sequence,
34935c4bbdfSmrg        .length = 0
35035c4bbdfSmrg    };
35105b261ecSmrg
3526747b715Smrg    /* allocate space for saving skip value */
3536747b715Smrg    skip = calloc(sizeof(Bool), inputInfo.numDevices);
3546747b715Smrg    if (!skip)
3556747b715Smrg        return BadAlloc;
3566747b715Smrg
3576747b715Smrg    /* figure out which devices to skip */
3586747b715Smrg    numdevs = 0;
3596747b715Smrg    for (d = inputInfo.devices; d; d = d->next, i++) {
3606747b715Smrg        skip[i] = ShouldSkipDevice(client, d);
3616747b715Smrg        if (skip[i])
3626747b715Smrg            continue;
3636747b715Smrg
3644642e01fSmrg        SizeDeviceInfo(d, &namesize, &size);
36505b261ecSmrg        numdevs++;
36605b261ecSmrg    }
3674642e01fSmrg
3686747b715Smrg    for (d = inputInfo.off_devices; d; d = d->next, i++) {
3696747b715Smrg        skip[i] = ShouldSkipDevice(client, d);
3706747b715Smrg        if (skip[i])
3716747b715Smrg            continue;
3726747b715Smrg
3734642e01fSmrg        SizeDeviceInfo(d, &namesize, &size);
37405b261ecSmrg        numdevs++;
37505b261ecSmrg    }
37605b261ecSmrg
3776747b715Smrg    /* allocate space for reply */
37805b261ecSmrg    total_length = numdevs * sizeof(xDeviceInfo) + size + namesize;
37935c4bbdfSmrg    devbuf = (char *) calloc(1, total_length);
38005b261ecSmrg    classbuf = devbuf + (numdevs * sizeof(xDeviceInfo));
38105b261ecSmrg    namebuf = classbuf + size;
38205b261ecSmrg    savbuf = devbuf;
38305b261ecSmrg
3846747b715Smrg    /* fill in and send reply */
3856747b715Smrg    i = 0;
38605b261ecSmrg    dev = (xDeviceInfoPtr) devbuf;
3876747b715Smrg    for (d = inputInfo.devices; d; d = d->next, i++) {
3886747b715Smrg        if (skip[i])
3896747b715Smrg            continue;
3906747b715Smrg
3914642e01fSmrg        ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
3924642e01fSmrg    }
39305b261ecSmrg
3946747b715Smrg    for (d = inputInfo.off_devices; d; d = d->next, i++) {
3956747b715Smrg        if (skip[i])
3966747b715Smrg            continue;
3976747b715Smrg
3984642e01fSmrg        ListDeviceInfo(client, d, dev++, &devbuf, &classbuf, &namebuf);
3994642e01fSmrg    }
40005b261ecSmrg    rep.ndevices = numdevs;
4016747b715Smrg    rep.length = bytes_to_int32(total_length);
40205b261ecSmrg    WriteReplyToClient(client, sizeof(xListInputDevicesReply), &rep);
40305b261ecSmrg    WriteToClient(client, total_length, savbuf);
4046747b715Smrg    free(savbuf);
4056747b715Smrg    free(skip);
40605b261ecSmrg    return Success;
40705b261ecSmrg}
40805b261ecSmrg
40905b261ecSmrg/***********************************************************************
41005b261ecSmrg *
41105b261ecSmrg * This procedure writes the reply for the XListInputDevices function,
41205b261ecSmrg * if the client and server have a different byte ordering.
41305b261ecSmrg *
41405b261ecSmrg */
41505b261ecSmrg
4161b5d61b8Smrgvoid _X_COLD
41705b261ecSmrgSRepXListInputDevices(ClientPtr client, int size, xListInputDevicesReply * rep)
41805b261ecSmrg{
41935c4bbdfSmrg    swaps(&rep->sequenceNumber);
42035c4bbdfSmrg    swapl(&rep->length);
42135c4bbdfSmrg    WriteToClient(client, size, rep);
42205b261ecSmrg}
423