1/* Portions of this file were derived from the following files:
2 *
3 **********************************************************************
4 *
5 * Xserver/hw/kdrive/linux/ps2.c
6 *
7 * Copyright (c) 1999 by Keith Packard
8 *
9 * Permission to use, copy, modify, distribute, and sell this software and its
10 * documentation for any purpose is hereby granted without fee, provided that
11 * the above copyright notice appear in all copies and that both that
12 * copyright notice and this permission notice appear in supporting
13 * documentation, and that the name of Keith Packard not be used in
14 * advertising or publicity pertaining to distribution of the software without
15 * specific, written prior permission.  Keith Packard makes no
16 * representations about the suitability of this software for any purpose.  It
17 * is provided "as is" without express or implied warranty.
18 *
19 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
21 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
22 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
23 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
24 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
25 * PERFORMANCE OF THIS SOFTWARE.
26 *
27 */
28
29/*
30 * Copyright 2001,2003 Red Hat Inc., Durham, North Carolina.
31 *
32 * All Rights Reserved.
33 *
34 * Permission is hereby granted, free of charge, to any person obtaining
35 * a copy of this software and associated documentation files (the
36 * "Software"), to deal in the Software without restriction, including
37 * without limitation on the rights to use, copy, modify, merge,
38 * publish, distribute, sublicense, and/or sell copies of the Software,
39 * and to permit persons to whom the Software is furnished to do so,
40 * subject to the following conditions:
41 *
42 * The above copyright notice and this permission notice (including the
43 * next paragraph) shall be included in all copies or substantial
44 * portions of the Software.
45 *
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
50 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 * SOFTWARE.
54 */
55
56/*
57 * Authors:
58 *   Rickard E. (Rik) Faith <faith@redhat.com>
59 *
60 */
61
62/** \file
63 *
64 * This code implements a low-level device driver for a serial MS mouse.
65 * The code is derived from code by Keith Packard (see the source code
66 * for complete references). */
67
68#ifdef HAVE_DMX_CONFIG_H
69#include <dmx-config.h>
70#endif
71
72#include "inputstr.h"
73#include <X11/Xos.h>
74#include <errno.h>
75#include <termios.h>
76
77/*****************************************************************************/
78/* Define some macros to make it easier to move this file to another
79 * part of the Xserver tree.  All calls to the dmx* layer are #defined
80 * here for the .c file.  The .h file will also have to be edited. */
81#include "dmxinputinit.h"
82#include "lnx-ps2.h"
83
84#define GETPRIV       myPrivate *priv                            \
85                      = ((DMXLocalInputInfoPtr)(pDev->devicePrivate))->private
86
87#define LOG0(f)       dmxLog(dmxDebug,f)
88#define LOG1(f,a)     dmxLog(dmxDebug,f,a)
89#define LOG2(f,a,b)   dmxLog(dmxDebug,f,a,b)
90#define LOG3(f,a,b,c) dmxLog(dmxDebug,f,a,b,c)
91#define FATAL0(f)     dmxLog(dmxFatal,f)
92#define FATAL1(f,a)   dmxLog(dmxFatal,f,a)
93#define FATAL2(f,a,b) dmxLog(dmxFatal,f,a,b)
94#define MOTIONPROC    dmxMotionProcPtr
95#define ENQUEUEPROC   dmxEnqueueProcPtr
96#define CHECKPROC     dmxCheckSpecialProcPtr
97#define BLOCK         DMXBlockType
98
99/* End of interface definitions. */
100/*****************************************************************************/
101
102/* Private area for PS/2 devices. */
103typedef struct _myPrivate {
104    DeviceIntPtr   pMouse;
105    int            fd;
106    enum {
107        button1 = 0x0001,
108        button2 = 0x0002,
109        button3 = 0x0004,
110        button4 = 0x0008,
111        button5 = 0x0010
112    }              buttons;
113} myPrivate;
114
115static int ps2LinuxReadBytes(int fd, unsigned char *buf, int len, int min)
116{
117    int		    n, tot;
118    fd_set	    set;
119    struct timeval  tv;
120
121    tot = 0;
122    while (len) {
123        n = read(fd, buf, len);
124        if (n > 0) {
125            tot += n;
126	    buf += n;
127	    len -= n;
128	}
129	if (tot % min == 0) break;
130	FD_ZERO(&set);
131	FD_SET(fd, &set);
132	tv.tv_sec = 0;
133	tv.tv_usec = 100 * 1000;
134	n = select(fd + 1, &set, 0, 0, &tv);
135	if (n <= 0) break;
136    }
137    return tot;
138}
139
140static void ps2LinuxButton(DevicePtr pDev, ENQUEUEPROC enqueue,
141                           int buttons, BLOCK block)
142{
143    GETPRIV;
144
145#define PRESS(b)                                         \
146    do {                                                 \
147        enqueue(pDev, ButtonPress, 0, 0, NULL, block);   \
148    } while (0)
149
150#define RELEASE(b)                                       \
151    do {                                                 \
152        enqueue(pDev, ButtonRelease, 0, 0, NULL, block); \
153    } while (0)
154
155    if ((buttons & button1) && !(priv->buttons & button1)) PRESS(1);
156    if (!(buttons & button1) && (priv->buttons & button1)) RELEASE(1);
157
158    if ((buttons & button2) && !(priv->buttons & button2)) PRESS(2);
159    if (!(buttons & button2) && (priv->buttons & button2)) RELEASE(2);
160
161    if ((buttons & button3) && !(priv->buttons & button3)) PRESS(3);
162    if (!(buttons & button3) && (priv->buttons & button3)) RELEASE(3);
163
164    if ((buttons & button4) && !(priv->buttons & button4)) PRESS(4);
165    if (!(buttons & button4) && (priv->buttons & button4)) RELEASE(4);
166
167    if ((buttons & button5) && !(priv->buttons & button5)) PRESS(5);
168    if (!(buttons & button5) && (priv->buttons & button5)) RELEASE(5);
169
170    priv->buttons = buttons;
171}
172
173/** Read an event from the \a pDev device.  If the event is a motion
174 * event, enqueue it with the \a motion function.  Otherwise, check for
175 * special keys with the \a checkspecial function and enqueue the event
176 * with the \a enqueue function.  The \a block type is passed to the
177 * functions so that they may block SIGIO handling as appropriate to the
178 * caller of this function. */
179void ps2LinuxRead(DevicePtr pDev, MOTIONPROC motion,
180                  ENQUEUEPROC enqueue, CHECKPROC checkspecial, BLOCK block)
181{
182    GETPRIV;
183    unsigned char   buf[3 * 200]; /* RATS: Use ok */
184    unsigned char   *b;
185    int		    n;
186    int		    dx, dy, v[2];
187
188    while ((n = ps2LinuxReadBytes(priv->fd, buf, sizeof(buf), 3)) > 0) {
189	b = buf;
190	while (n >= 3) {
191            dx   =  b[1] - ((b[0] & 0x10) ? 256 : 0);
192            dy   = -b[2] + ((b[0] & 0x20) ? 256 : 0);
193            v[0] = -dx;
194            v[1] = -dy;
195
196            motion(pDev, v, 0, 2, 1, block);
197            ps2LinuxButton(pDev, enqueue, (((b[0] & 4) ? button2 : 0)
198                                           | ((b[0] & 2) ? button3 : 0)
199                                           | ((b[0] & 1) ? button1 : 0)),
200                           block);
201            n -= 3;
202            b += 3;
203	}
204    }
205}
206
207/** Initialize \a pDev. */
208void ps2LinuxInit(DevicePtr pDev)
209{
210    GETPRIV;
211    const char *names[] = { "/dev/mouse", "/dev/psaux", NULL };
212    int        i;
213
214    if (priv->fd >=0) return;
215
216    for (i = 0; names[i]; i++) {
217        if ((priv->fd = open(names[i], O_RDWR | O_NONBLOCK, 0)) >= 0) break;
218    }
219    if (priv->fd < 0)
220        FATAL1("ps2LinuxInit: Cannot open mouse port (%s)\n",
221               strerror(errno));
222}
223
224/** Turn \a pDev on (i.e., take input from \a pDev). */
225int ps2LinuxOn(DevicePtr pDev)
226{
227    GETPRIV;
228
229    if (priv->fd < 0) ps2LinuxInit(pDev);
230    return priv->fd;
231}
232
233/** Turn \a pDev off (i.e., stop taking input from \a pDev). */
234void ps2LinuxOff(DevicePtr pDev)
235{
236    GETPRIV;
237
238    close(priv->fd);
239    priv->fd = -1;
240}
241
242static void ps2LinuxGetMap(DevicePtr pDev, unsigned char *map, int *nButtons)
243{
244    int i;
245
246    if (nButtons) *nButtons = 3;
247    if (map) for (i = 0; i <= *nButtons; i++) map[i] = i;
248}
249
250/** Currently unused hook called prior to an VT switch. */
251void ps2LinuxVTPreSwitch(pointer p)
252{
253}
254
255/** Currently unused hook called after returning from a VT switch. */
256void ps2LinuxVTPostSwitch(pointer p)
257{
258}
259
260/** Create a private structure for use within this file. */
261pointer ps2LinuxCreatePrivate(DeviceIntPtr pMouse)
262{
263    myPrivate *priv = calloc(1, sizeof(*priv));
264    priv->fd     = -1;
265    priv->pMouse = pMouse;
266    return priv;
267}
268
269/** Destroy a private structure. */
270void ps2LinuxDestroyPrivate(pointer priv)
271{
272    free(priv);
273}
274
275/** Fill the \a info structure with information needed to initialize \a
276 * pDev. */
277void ps2LinuxGetInfo(DevicePtr pDev, DMXLocalInitInfoPtr info)
278{
279    info->buttonClass      = 1;
280    ps2LinuxGetMap(pDev, info->map, &info->numButtons);
281    info->valuatorClass    = 1;
282    info->numRelAxes       = 2;
283    info->minval[0]        = 0;
284    info->maxval[0]        = 0;
285    info->res[0]           = 1;
286    info->minres[0]        = 0;
287    info->maxres[0]        = 1;
288    info->ptrFeedbackClass = 1;
289}
290