Commit | Line | Data |
---|---|---|
a09e091a JB |
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. */ | |
103 | typedef 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 | ||
115 | static int | |
116 | ps2LinuxReadBytes(int fd, unsigned char *buf, int len, int min) | |
117 | { | |
118 | int n, tot; | |
119 | fd_set set; | |
120 | struct timeval tv; | |
121 | ||
122 | tot = 0; | |
123 | while (len) { | |
124 | n = read(fd, buf, len); | |
125 | if (n > 0) { | |
126 | tot += n; | |
127 | buf += n; | |
128 | len -= n; | |
129 | } | |
130 | if (tot % min == 0) | |
131 | break; | |
132 | FD_ZERO(&set); | |
133 | FD_SET(fd, &set); | |
134 | tv.tv_sec = 0; | |
135 | tv.tv_usec = 100 * 1000; | |
136 | n = select(fd + 1, &set, 0, 0, &tv); | |
137 | if (n <= 0) | |
138 | break; | |
139 | } | |
140 | return tot; | |
141 | } | |
142 | ||
143 | static void | |
144 | ps2LinuxButton(DevicePtr pDev, ENQUEUEPROC enqueue, int buttons, BLOCK block) | |
145 | { | |
146 | GETPRIV; | |
147 | ||
148 | #define PRESS(b) \ | |
149 | do { \ | |
150 | enqueue(pDev, ButtonPress, 0, 0, NULL, block); \ | |
151 | } while (0) | |
152 | ||
153 | #define RELEASE(b) \ | |
154 | do { \ | |
155 | enqueue(pDev, ButtonRelease, 0, 0, NULL, block); \ | |
156 | } while (0) | |
157 | ||
158 | if ((buttons & button1) && !(priv->buttons & button1)) | |
159 | PRESS(1); | |
160 | if (!(buttons & button1) && (priv->buttons & button1)) | |
161 | RELEASE(1); | |
162 | ||
163 | if ((buttons & button2) && !(priv->buttons & button2)) | |
164 | PRESS(2); | |
165 | if (!(buttons & button2) && (priv->buttons & button2)) | |
166 | RELEASE(2); | |
167 | ||
168 | if ((buttons & button3) && !(priv->buttons & button3)) | |
169 | PRESS(3); | |
170 | if (!(buttons & button3) && (priv->buttons & button3)) | |
171 | RELEASE(3); | |
172 | ||
173 | if ((buttons & button4) && !(priv->buttons & button4)) | |
174 | PRESS(4); | |
175 | if (!(buttons & button4) && (priv->buttons & button4)) | |
176 | RELEASE(4); | |
177 | ||
178 | if ((buttons & button5) && !(priv->buttons & button5)) | |
179 | PRESS(5); | |
180 | if (!(buttons & button5) && (priv->buttons & button5)) | |
181 | RELEASE(5); | |
182 | ||
183 | priv->buttons = buttons; | |
184 | } | |
185 | ||
186 | /** Read an event from the \a pDev device. If the event is a motion | |
187 | * event, enqueue it with the \a motion function. Otherwise, check for | |
188 | * special keys with the \a checkspecial function and enqueue the event | |
189 | * with the \a enqueue function. The \a block type is passed to the | |
190 | * functions so that they may block SIGIO handling as appropriate to the | |
191 | * caller of this function. */ | |
192 | void | |
193 | ps2LinuxRead(DevicePtr pDev, MOTIONPROC motion, | |
194 | ENQUEUEPROC enqueue, CHECKPROC checkspecial, BLOCK block) | |
195 | { | |
196 | GETPRIV; | |
197 | unsigned char buf[3 * 200]; /* RATS: Use ok */ | |
198 | unsigned char *b; | |
199 | int n; | |
200 | int dx, dy, v[2]; | |
201 | ||
202 | while ((n = ps2LinuxReadBytes(priv->fd, buf, sizeof(buf), 3)) > 0) { | |
203 | b = buf; | |
204 | while (n >= 3) { | |
205 | dx = b[1] - ((b[0] & 0x10) ? 256 : 0); | |
206 | dy = -b[2] + ((b[0] & 0x20) ? 256 : 0); | |
207 | v[0] = -dx; | |
208 | v[1] = -dy; | |
209 | ||
210 | motion(pDev, v, 0, 2, 1, block); | |
211 | ps2LinuxButton(pDev, enqueue, (((b[0] & 4) ? button2 : 0) | |
212 | | ((b[0] & 2) ? button3 : 0) | |
213 | | ((b[0] & 1) ? button1 : 0)), | |
214 | block); | |
215 | n -= 3; | |
216 | b += 3; | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | /** Initialize \a pDev. */ | |
222 | void | |
223 | ps2LinuxInit(DevicePtr pDev) | |
224 | { | |
225 | GETPRIV; | |
226 | const char *names[] = { "/dev/mouse", "/dev/psaux", NULL }; | |
227 | int i; | |
228 | ||
229 | if (priv->fd >= 0) | |
230 | return; | |
231 | ||
232 | for (i = 0; names[i]; i++) { | |
233 | if ((priv->fd = open(names[i], O_RDWR | O_NONBLOCK, 0)) >= 0) | |
234 | break; | |
235 | } | |
236 | if (priv->fd < 0) | |
237 | FATAL1("ps2LinuxInit: Cannot open mouse port (%s)\n", strerror(errno)); | |
238 | } | |
239 | ||
240 | /** Turn \a pDev on (i.e., take input from \a pDev). */ | |
241 | int | |
242 | ps2LinuxOn(DevicePtr pDev) | |
243 | { | |
244 | GETPRIV; | |
245 | ||
246 | if (priv->fd < 0) | |
247 | ps2LinuxInit(pDev); | |
248 | return priv->fd; | |
249 | } | |
250 | ||
251 | /** Turn \a pDev off (i.e., stop taking input from \a pDev). */ | |
252 | void | |
253 | ps2LinuxOff(DevicePtr pDev) | |
254 | { | |
255 | GETPRIV; | |
256 | ||
257 | close(priv->fd); | |
258 | priv->fd = -1; | |
259 | } | |
260 | ||
261 | static void | |
262 | ps2LinuxGetMap(DevicePtr pDev, unsigned char *map, int *nButtons) | |
263 | { | |
264 | int i; | |
265 | ||
266 | if (nButtons) | |
267 | *nButtons = 3; | |
268 | if (map) | |
269 | for (i = 0; i <= *nButtons; i++) | |
270 | map[i] = i; | |
271 | } | |
272 | ||
273 | /** Currently unused hook called prior to an VT switch. */ | |
274 | void | |
275 | ps2LinuxVTPreSwitch(pointer p) | |
276 | { | |
277 | } | |
278 | ||
279 | /** Currently unused hook called after returning from a VT switch. */ | |
280 | void | |
281 | ps2LinuxVTPostSwitch(pointer p) | |
282 | { | |
283 | } | |
284 | ||
285 | /** Create a private structure for use within this file. */ | |
286 | pointer | |
287 | ps2LinuxCreatePrivate(DeviceIntPtr pMouse) | |
288 | { | |
289 | myPrivate *priv = calloc(1, sizeof(*priv)); | |
290 | ||
291 | priv->fd = -1; | |
292 | priv->pMouse = pMouse; | |
293 | return priv; | |
294 | } | |
295 | ||
296 | /** Destroy a private structure. */ | |
297 | void | |
298 | ps2LinuxDestroyPrivate(pointer priv) | |
299 | { | |
300 | free(priv); | |
301 | } | |
302 | ||
303 | /** Fill the \a info structure with information needed to initialize \a | |
304 | * pDev. */ | |
305 | void | |
306 | ps2LinuxGetInfo(DevicePtr pDev, DMXLocalInitInfoPtr info) | |
307 | { | |
308 | info->buttonClass = 1; | |
309 | ps2LinuxGetMap(pDev, info->map, &info->numButtons); | |
310 | info->valuatorClass = 1; | |
311 | info->numRelAxes = 2; | |
312 | info->minval[0] = 0; | |
313 | info->maxval[0] = 0; | |
314 | info->res[0] = 1; | |
315 | info->minres[0] = 0; | |
316 | info->maxres[0] = 1; | |
317 | info->ptrFeedbackClass = 1; | |
318 | } |