usb-other.c revision 706f2543
1/*
2 * Copyright 2002 Red Hat Inc., Durham, North Carolina.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28/*
29 * Authors:
30 *   Rickard E. (Rik) Faith <faith@redhat.com>
31 *
32 */
33
34/** \file
35 *
36 * This code implements a low-level device driver for a non-keyboard,
37 * non-mouse USB device (e.g., a joystick or gamepad). */
38
39#ifdef HAVE_DMX_CONFIG_H
40#include <dmx-config.h>
41#endif
42
43#include "usb-private.h"
44
45/*****************************************************************************/
46/* Define some macros to make it easier to move this file to another
47 * part of the Xserver tree.  All calls to the dmx* layer are #defined
48 * here for the .c file.  The .h file will also have to be edited. */
49#include "dmxinputinit.h"
50#include "usb-other.h"
51
52#define GETPRIV       myPrivate *priv                            \
53                      = ((DMXLocalInputInfoPtr)(pDev->devicePrivate))->private
54
55#define LOG0(f)       dmxLog(dmxDebug,f)
56#define LOG1(f,a)     dmxLog(dmxDebug,f,a)
57#define LOG2(f,a,b)   dmxLog(dmxDebug,f,a,b)
58#define LOG3(f,a,b,c) dmxLog(dmxDebug,f,a,b,c)
59#define FATAL0(f)     dmxLog(dmxFatal,f)
60#define FATAL1(f,a)   dmxLog(dmxFatal,f,a)
61#define FATAL2(f,a,b) dmxLog(dmxFatal,f,a,b)
62#define MOTIONPROC    dmxMotionProcPtr
63#define ENQUEUEPROC   dmxEnqueueProcPtr
64#define CHECKPROC     dmxCheckSpecialProcPtr
65#define BLOCK         DMXBlockType
66
67/* End of interface definitions. */
68/*****************************************************************************/
69
70/** Read the USB device using #usbRead. */
71void othUSBRead(DevicePtr pDev,
72                MOTIONPROC motion,
73                ENQUEUEPROC enqueue,
74                CHECKPROC checkspecial,
75                BLOCK block)
76{
77    usbRead(pDev, motion, enqueue, 0xffff, block);
78}
79
80/** Initialize \a pDev using #usbInit. */
81void othUSBInit(DevicePtr pDev)
82{
83    usbInit(pDev, usbOther);
84}
85
86/** Turn \a pDev on (i.e., take input from \a pDev). */
87int othUSBOn(DevicePtr pDev)
88{
89    GETPRIV;
90
91    if (priv->fd < 0) othUSBInit(pDev);
92    return priv->fd;
93}
94
95/** Fill the \a info structure with information needed to initialize \a
96 * pDev. */
97void othUSBGetInfo(DevicePtr pDev, DMXLocalInitInfoPtr info)
98{
99    GETPRIV;
100    int           i, j;
101    static KeySym keyboard_mapping = NoSymbol;
102    int           absolute[5];
103
104#define test_bit(bit) (priv->mask[(bit)/8] & (1 << ((bit)%8)))
105
106                                /* Some USB mice devices return key
107                                 * events from their pair'd
108                                 * keyboard...  */
109    info->keyClass           = 1;
110    info->keySyms.minKeyCode = 8;
111    info->keySyms.maxKeyCode = 8;
112    info->keySyms.mapWidth   = 1;
113    info->keySyms.map        = &keyboard_mapping;
114
115    for (i = 0; i < EV_MAX; i++) {
116        if (test_bit(i)) {
117            switch (i) {
118            case EV_KEY:
119                                /* See above */
120                break;
121            case EV_REL:
122                info->valuatorClass      = 1;
123                if (info->numRelAxes + info->numAbsAxes > DMX_MAX_AXES - 1) {
124                    info->numRelAxes     = DMX_MAX_AXES - info->numAbsAxes - 1;
125                    dmxLog(dmxWarning, "Can only use %d relative axes\n",
126                           info->numRelAxes);
127                } else
128                    info->numRelAxes     = priv->numRel;
129                info->minval[0]          = 0;
130                info->maxval[0]          = 0;
131                info->res[0]             = 1;
132                info->minres[0]          = 0;
133                info->maxres[0]          = 1;
134                break;
135            case EV_ABS:
136                info->valuatorClass      = 1;
137                if (info->numRelAxes + info->numAbsAxes > DMX_MAX_AXES - 1) {
138                    info->numAbsAxes     = DMX_MAX_AXES - info->numRelAxes - 1;
139                    dmxLog(dmxWarning, "Can only use %d absolute axes\n",
140                           info->numAbsAxes);
141                } else
142                    info->numAbsAxes     = priv->numAbs;
143                for (j = 0; j < info->numAbsAxes; j++) {
144                    ioctl(priv->fd, EVIOCGABS(j), absolute);
145                    info->minval[1+j]    = absolute[1];
146                    info->maxval[1+j]    = absolute[2];
147                    info->res[1+j]       = absolute[3];
148                    info->minres[1+j]    = absolute[3];
149                    info->maxres[1+j]    = absolute[3];
150                }
151                break;
152            case EV_LED:
153                info->ledFeedbackClass   = 0; /* Not supported at this time */
154                break;
155            case EV_SND:
156                info->belFeedbackClass   = 0; /* Not supported at this time */
157                break;
158            }
159        }
160    }
161}
162