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