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