2
/* Portions of this file were derived from the following files:
4
**********************************************************************
6
* Xserver/hw/kdrive/linux/ms.c
8
* Copyright (c) 2001 by Juliusz Chroboczek
9
* Copyright (c) 1999 by Keith Packard
11
* Permission is hereby granted, free of charge, to any person obtaining
12
* a copy of this software and associated documentation files (the
13
* "Software"), to deal in the Software without restriction, including
14
* without limitation the rights to use, copy, modify, merge, publish,
15
* distribute, sublicense, and/or sell copies of the Software, and to
16
* permit persons to whom the Software is furnished to do so, subject to
17
* the following conditions:
19
* The above copyright notice and this permission notice shall be
20
* included in all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
* Copyright 2001-2003 Red Hat Inc., Durham, North Carolina.
36
* All Rights Reserved.
38
* Permission is hereby granted, free of charge, to any person obtaining
39
* a copy of this software and associated documentation files (the
40
* "Software"), to deal in the Software without restriction, including
41
* without limitation on the rights to use, copy, modify, merge,
42
* publish, distribute, sublicense, and/or sell copies of the Software,
43
* and to permit persons to whom the Software is furnished to do so,
44
* subject to the following conditions:
46
* The above copyright notice and this permission notice (including the
47
* next paragraph) shall be included in all copies or substantial
48
* portions of the Software.
50
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
51
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
52
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
53
* NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
54
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
55
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
56
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
62
* Rickard E. (Rik) Faith <faith@redhat.com>
68
* This code implements a low-level device driver for a serial MS mouse.
69
* The code is derived from code by Juliusz Chroboczek and Keith Packard
70
* (see the source code for complete references). */
72
#ifdef HAVE_DMX_CONFIG_H
73
#include <dmx-config.h>
81
/*****************************************************************************/
82
/* Define some macros to make it easier to move this file to another
83
* part of the Xserver tree. All calls to the dmx* layer are #defined
84
* here for the .c file. The .h file will also have to be edited. */
85
#include "dmxinputinit.h"
88
#define GETPRIV myPrivate *priv \
89
= ((DMXLocalInputInfoPtr)(pDev->devicePrivate))->private
91
#define LOG0(f) dmxLog(dmxDebug,f)
92
#define LOG1(f,a) dmxLog(dmxDebug,f,a)
93
#define LOG2(f,a,b) dmxLog(dmxDebug,f,a,b)
94
#define LOG3(f,a,b,c) dmxLog(dmxDebug,f,a,b,c)
95
#define FATAL0(f) dmxLog(dmxFatal,f)
96
#define FATAL1(f,a) dmxLog(dmxFatal,f,a)
97
#define FATAL2(f,a,b) dmxLog(dmxFatal,f,a,b)
98
#define MOTIONPROC dmxMotionProcPtr
99
#define ENQUEUEPROC dmxEnqueueProcPtr
100
#define CHECKPROC dmxCheckSpecialProcPtr
101
#define BLOCK DMXBlockType
103
/* End of interface definitions. */
104
/*****************************************************************************/
106
/* Private area for MS mouse devices. */
107
typedef struct _myPrivate {
120
static int msLinuxReadBytes(int fd, unsigned char *buf, int len, int min)
128
n = read(fd, buf, len);
134
if (tot % min == 0) break;
138
tv.tv_usec = 100 * 1000;
139
n = select(fd + 1, &set, 0, 0, &tv);
145
static void msLinuxButton(DevicePtr pDev, ENQUEUEPROC enqueue, int buttons,
152
enqueue(pDev, ButtonPress, 0, 0, NULL, block); \
157
enqueue(pDev, ButtonRelease, 0, 0, NULL, block); \
160
if ((buttons & button1) && !(priv->buttons & button1)) PRESS(1);
161
if (!(buttons & button1) && (priv->buttons & button1)) RELEASE(1);
163
if ((buttons & button2) && !(priv->buttons & button2)) PRESS(2);
164
if (!(buttons & button2) && (priv->buttons & button2)) RELEASE(2);
166
if ((buttons & button3) && !(priv->buttons & button3)) PRESS(3);
167
if (!(buttons & button3) && (priv->buttons & button3)) RELEASE(3);
169
if ((buttons & button4) && !(priv->buttons & button4)) PRESS(4);
170
if (!(buttons & button4) && (priv->buttons & button4)) RELEASE(4);
172
if ((buttons & button5) && !(priv->buttons & button5)) PRESS(5);
173
if (!(buttons & button5) && (priv->buttons & button5)) RELEASE(5);
175
priv->buttons = buttons;
178
/** Read an event from the \a pDev device. If the event is a motion
179
* event, enqueue it with the \a motion function. Otherwise, check for
180
* special keys with the \a checkspecial function and enqueue the event
181
* with the \a enqueue function. The \a block type is passed to the
182
* functions so that they may block SIGIO handling as appropriate to the
183
* caller of this function. */
184
void msLinuxRead(DevicePtr pDev,
187
CHECKPROC checkspecial,
191
unsigned char buf[3 * 200]; /* RATS: Use ok */
196
while ((n = msLinuxReadBytes(priv->fd, buf, sizeof(buf), 3)) > 0) {
199
dx = (char)(((b[0] & 0x03) << 6) | (b[1] & 0x3f));
200
dy = (char)(((b[0] & 0x0c) << 4) | (b[2] & 0x3f));
204
motion(pDev, v, 0, 2, 1, block);
205
msLinuxButton(pDev, enqueue, (((b[0] & 0x10) ? button3 : 0)
206
| ((b[0] & 0x20) ? button1 : 0)),
214
/** Initialize \a pDev. */
215
void msLinuxInit(DevicePtr pDev)
218
const char *names[] = { "/dev/serialmouse", "/dev/mouse", NULL };
221
if (priv->fd >=0) return;
223
for (i = 0; names[i]; i++) {
224
if ((priv->fd = open(names[i], O_RDWR | O_NONBLOCK, 0)) >= 0) break;
227
FATAL1("msLinuxInit: Cannot open mouse port (%s)\n",
230
if (!isatty(priv->fd))
231
FATAL1("msLinuxInit: Mouse port %s is not a tty\n", names[i]);
233
if (tcgetattr(priv->fd, &priv->tty) < 0)
234
FATAL1("msLinuxInit: tcgetattr failed (%s)\n", strerror(errno));
236
write(priv->fd, "*n", 2); /* 1200 baud */
240
/** Turn \a pDev on (i.e., take input from \a pDev). */
241
int msLinuxOn(DevicePtr pDev)
246
if (priv->fd < 0) msLinuxInit(pDev);
249
nTty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR
250
| IGNCR | ICRNL | IXON | IXOFF);
251
nTty.c_oflag &= ~OPOST;
252
nTty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
253
nTty.c_cflag &= ~(CSIZE | PARENB);
254
nTty.c_cflag |= CS8 | CLOCAL | CSTOPB;
255
nTty.c_cc[VTIME] = 0;
257
cfsetispeed (&nTty, B1200);
258
cfsetospeed (&nTty, B1200);
259
if (tcsetattr(priv->fd, TCSANOW, &nTty) < 0)
260
FATAL1("msLinuxInit: tcsetattr failed (%s)\n", strerror(errno));
261
write(priv->fd, "*V", 2); /* 2 button 3 byte protocol */
265
/** Turn \a pDev off (i.e., stop taking input from \a pDev). */
266
void msLinuxOff(DevicePtr pDev)
270
tcsetattr(priv->fd, TCSANOW, &priv->tty);
275
static void msLinuxGetMap(DevicePtr pDev, unsigned char *map, int *nButtons)
279
if (nButtons) *nButtons = 3;
280
if (map) for (i = 0; i <= *nButtons; i++) map[i] = i;
283
/** Currently unused hook called prior to an VT switch. */
284
void msLinuxVTPreSwitch(pointer p)
288
/** Currently unused hook called after returning from a VT switch. */
289
void msLinuxVTPostSwitch(pointer p)
293
/** Create a private structure for use within this file. */
294
pointer msLinuxCreatePrivate(DeviceIntPtr pMouse)
296
myPrivate *priv = xalloc(sizeof(*priv));
297
memset(priv, 0, sizeof(*priv));
299
priv->pMouse = pMouse;
303
/** Destroy a private structure. */
304
void msLinuxDestroyPrivate(pointer priv)
306
if (priv) xfree(priv);
309
/** Fill the \a info structure with information needed to initialize \a
311
void msLinuxGetInfo(DevicePtr pDev, DMXLocalInitInfoPtr info)
313
info->buttonClass = 1;
314
msLinuxGetMap(pDev, info->map, &info->numButtons);
315
info->valuatorClass = 1;
316
info->numRelAxes = 2;
322
info->ptrFeedbackClass = 1;