First import
[xorg_rtime.git] / xorg-server-1.4 / hw / kdrive / linux / ms.c
blobd0b47a3ee0d9226b797273ecfae98fa04e40a1cd
1 /*
2 Copyright (c) 2001 by Juliusz Chroboczek
3 Copyright (c) 1999 by Keith Packard
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 THE SOFTWARE.
24 #ifdef HAVE_CONFIG_H
25 #include <kdrive-config.h>
26 #endif
27 #define NEED_EVENTS
28 #include <errno.h>
29 #include <termios.h>
30 #include <X11/X.h>
31 #include <X11/Xproto.h>
32 #include <X11/Xpoll.h>
33 #include "inputstr.h"
34 #include "scrnintstr.h"
35 #include "kdrive.h"
37 static int
38 MsReadBytes (int fd, char *buf, int len, int min)
40 int n, tot;
41 fd_set set;
42 struct timeval tv;
44 tot = 0;
45 while (len)
47 n = read (fd, buf, len);
48 if (n > 0)
50 tot += n;
51 buf += n;
52 len -= n;
54 if (tot % min == 0)
55 break;
56 FD_ZERO (&set);
57 FD_SET (fd, &set);
58 tv.tv_sec = 0;
59 tv.tv_usec = 100 * 1000;
60 n = select (fd + 1, &set, 0, 0, &tv);
61 if (n <= 0)
62 break;
64 return tot;
67 static void
68 MsRead (int port, void *closure)
70 unsigned char buf[3 * 200];
71 unsigned char *b;
72 int n;
73 int dx, dy;
74 unsigned long flags;
76 while ((n = MsReadBytes (port, (char *) buf, sizeof (buf), 3)) > 0)
78 b = buf;
79 while (n >= 3)
81 flags = KD_MOUSE_DELTA;
83 if (b[0] & 0x20)
84 flags |= KD_BUTTON_1;
85 if (b[0] & 0x10)
86 flags |= KD_BUTTON_3;
88 dx = (char)(((b[0] & 0x03) << 6) | (b[1] & 0x3F));
89 dy = (char)(((b[0] & 0x0C) << 4) | (b[2] & 0x3F));
90 n -= 3;
91 b += 3;
92 KdEnqueuePointerEvent (closure, flags, dx, dy, 0);
97 static Status
98 MsInit (KdPointerInfo *pi)
100 if (!pi)
101 return BadImplementation;
103 if (!pi->path || strcmp(pi->path, "auto"))
104 pi->path = KdSaveString("/dev/mouse");
105 if (!pi->name)
106 pi->name = KdSaveString("Microsoft protocol mouse");
108 return Success;
111 static Status
112 MsEnable (KdPointerInfo *pi)
114 int port;
115 struct termios t;
116 int ret;
118 port = open (pi->path, O_RDWR | O_NONBLOCK);
119 if(port < 0) {
120 ErrorF("Couldn't open %s (%d)\n", pi->path, (int)errno);
121 return 0;
122 } else if (port == 0) {
123 ErrorF("Opening %s returned 0! Please complain to Keith.\n",
124 pi->path);
125 goto bail;
128 if(!isatty(port)) {
129 ErrorF("%s is not a tty\n", pi->path);
130 goto bail;
133 ret = tcgetattr(port, &t);
134 if(ret < 0) {
135 ErrorF("Couldn't tcgetattr(%s): %d\n", pi->path, errno);
136 goto bail;
138 t.c_iflag &= ~ (IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR |
139 IGNCR | ICRNL | IXON | IXOFF);
140 t.c_oflag &= ~ OPOST;
141 t.c_lflag &= ~ (ECHO | ECHONL | ICANON | ISIG | IEXTEN);
142 t.c_cflag &= ~ (CSIZE | PARENB);
143 t.c_cflag |= CS8 | CLOCAL | CSTOPB;
145 cfsetispeed (&t, B1200);
146 cfsetospeed (&t, B1200);
147 t.c_cc[VMIN] = 1;
148 t.c_cc[VTIME] = 0;
149 ret = tcsetattr(port, TCSANOW, &t);
150 if(ret < 0) {
151 ErrorF("Couldn't tcsetattr(%s): %d\n", pi->path, errno);
152 goto bail;
154 if (KdRegisterFd (port, MsRead, pi))
155 return TRUE;
156 pi->driverPrivate = (void *)port;
158 return Success;
160 bail:
161 close(port);
162 return BadMatch;
165 static void
166 MsDisable (KdPointerInfo *pi)
168 KdUnregisterFd (pi, (int)pi->driverPrivate, TRUE);
171 static void
172 MsFini (KdPointerInfo *pi)
176 KdPointerDriver MsMouseDriver = {
177 "ms",
178 MsInit,
179 MsEnable,
180 MsDisable,
181 MsFini,
182 NULL,