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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
|
/* BOGL - Ben's Own Graphics Library.
Written by Ben Pfaff <pfaffben@debian.org>.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. */
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>
#include "bogl.h"
#include "boglP.h"
#include "boml.h"
#define M_GPM
#define M_INPUT
#ifdef M_GPM
#include <sys/socket.h>
#include <sys/un.h>
#endif
#if defined __alpha__
#define M_SERIAL
#define M_PS2
#elif defined __i386__
#define M_SERIAL
#define M_PS2
#define M_MSBUS
#elif defined __mc68000__
#define M_ADB
#elif defined __powerpc__
#define M_SERIAL
#define M_ADB
#define M_PS2
#elif defined __sparc__
#define M_SUN
#endif
struct mouse;
/* Packet drivers and detection routines. */
#define N_SERIAL 0
#define N_PS2 0
#define N_MSBUS 0
#define N_ADB 0
#define N_SUN 0
#ifdef M_GPM
static int detect_gpm (void);
# ifndef M_SERIAL
# define M_SERIAL_MS
static void ms_driver (struct mouse *);
# endif
#endif
#ifdef M_INPUT
# ifndef M_PS2
# define M_PS2_DRIVER
static void ps2_driver (struct mouse *);
# endif
static int input_mouse_found = 0;
static void detect_input (void);
#endif
#ifdef M_SERIAL
#undef N_SERIAL
#define N_SERIAL 8
#define M_SERIAL_MS
static void detect_serial (void);
static void ms_driver (struct mouse *);
static void msc_driver (struct mouse *);
static void mman_driver (struct mouse *);
#endif
/* These next three are actually the same protocol, but bind to
different files in /dev. Thus they use bm_driver. */
#if defined(M_MSBUS) || defined(M_ADB) || defined(M_SUN)
static void bm_driver (struct mouse *);
#endif
#ifdef M_MSBUS
#undef N_MSBUS
#define N_MSBUS 1
static void detect_msbus (void);
#endif
#ifdef M_ADB
#undef N_ADB
#define N_ADB 1
static void detect_adb (void);
#endif
#ifdef M_SUN
#undef N_SUN
#define N_SUN 1
static void detect_sun (void);
#endif
#ifdef M_PS2
#undef N_PS2
#define N_PS2 1
#define M_PS2_DRIVER
static void detect_ps2 (void);
static void ps2_driver (struct mouse *);
#endif
#define N_DETECT (N_SERIAL + N_MSBUS + N_PS2 + N_ADB + N_SUN)
#if N_DETECT > 0
/* Detection progress. */
static void inc (void);
#endif
static void (*detect_callback) (int);
static int detect_count;
/* Mouse types. */
enum
{
#ifdef M_SERIAL_MS
/* Serial mice using the MS protocol */
T_MS_SERIAL, /* Microsoft. */
T_MS3_SERIAL, /* Microsoft Intellimouse. */
#endif
#ifdef M_SERIAL
/* Other serial mice. */
T_MSC_SERIAL, /* Mouse Systems. */
T_MMAN_SERIAL, /* Logitech Mouseman. */
#endif
#ifdef M_MSBUS
/* Bus mice. */
T_MS_BUS, /* Microsoft/Logitech. */
#endif
#ifdef M_ADB
T_ADB, /* Apple ADB */
#endif
#ifdef M_SUN
T_SUN, /* Sun */
#endif
#ifdef M_PS2_DRIVER
/* PS/2 mice. */
T_PS2, /* Generic. */
#endif
};
/* Mouse attributes description. */
struct mouse_info
{
char *name; /* Name. */
int packet_size; /* (Initial) bytes per packet. */
unsigned char id[4]; /* Packet identification info. */
void (*driver) (struct mouse *); /* Packet driver. */
};
/* Some of the information below is borrowed from gpm. */
static struct mouse_info mouse_info[] =
{
#ifdef M_SERIAL_MS
{"Microsoft serial", 3, {0x40,0x40,0x40,0x00}, ms_driver},
{"Microsoft Intellimouse serial", 4, {0xc0,0x40,0xc0,0x00}, ms_driver},
#endif
#ifdef M_SERIAL
{"Mouse Systems serial", 5, {0xf8,0x80,0x00,0x00}, msc_driver},
{"Mouseman serial", 3, {0xe0,0x80,0x80,0x00}, mman_driver},
#endif
#ifdef M_MSBUS
{"Microsoft bus", 3, {0xf8,0x80,0x00,0x00}, bm_driver},
#endif
#ifdef M_ADB
{"Apple Desktop Bus", 3, {0xf8,0x80,0x00,0x00}, bm_driver},
#endif
#ifdef M_SUN
{"Sun", 3, {0xf8,0x80,0x00,0x00}, bm_driver},
#endif
#ifdef M_PS2_DRIVER
{"Generic PS/2", 3, {0xc0,0x00,0x00,0x00}, ps2_driver},
#endif
};
/* A mouse. */
struct mouse
{
struct mouse *next; /* Linked list. */
int type; /* Type of mouse, one of T_*. */
int fd; /* File descriptor. */
unsigned char pbuf[8]; /* Protocol buffer. */
int ppos; /* Number of bytes in buffer. */
int packet_size; /* Bytes per packet. */
};
/* All detected mice. */
static struct mouse *mice;
/* Current mouse state. */
static int x, y; /* Pointer location. */
static int show; /* >0: Show the pointer. */
static int drawn; /* !=0: Pointer is currently drawn. */
static int button; /* Button down? */
/* Event queue. */
struct event
{
int type; /* One of BOML_E_*. */
int x;
int y;
int btn;
};
/* Next or previous item in the queue after INDEX. */
#define q_next(INDEX) \
((INDEX) + 1 < QUEUE_SIZE ? (INDEX) + 1 : 0)
#define q_prev(INDEX) \
((INDEX) > 0 ? (INDEX) - 1 : QUEUE_SIZE - 1)
#define QUEUE_SIZE 8
static struct event queue[QUEUE_SIZE];
static int qh, qt;
#ifdef M_SERIAL
static int probe_com (char *port, int pnp);
#endif
static void state (int dx, int dy, int button);
/* Mouse pointer. */
static const struct bogl_pointer *pointer;
static int pointer_colors[2];
/* Public code. */
int
boml_quick_init (void)
{
static int inited = 0;
if (inited)
return inited - 1;
inited = 1;
#ifdef M_GPM
if(detect_gpm ())
{
inited++;
return (inited - 1);
}
#endif
#ifdef M_INPUT
detect_input ();
if (input_mouse_found)
{
inited++;
return (inited - 1);
}
#endif
return (inited - 1);
}
/* Detects mice and initializes the mouse library. */
void
boml_init (void (*callback) (int))
{
/* Assure idempotence. */
static int inited = 0;
if (inited)
return;
inited = 1;
detect_callback = callback;
detect_count = 0;
#ifdef M_PS2
detect_ps2 ();
inc ();
#endif
#ifdef M_MSBUS
detect_msbus ();
inc ();
#endif
#ifdef M_ADB
detect_adb ();
inc ();
#endif
#ifdef M_SUN
detect_sun ();
inc ();
#endif
#ifdef M_SERIAL
detect_serial ();
#endif
}
#if N_DETECT > 0
/* Calls the callback, if any, with a report of the progress of mouse
detection in percent, incremented to the next mark (out of N_DETECT
marks total). */
static void
inc (void)
{
detect_count++;
if (detect_callback)
detect_callback (100 * detect_count / N_DETECT);
}
#endif
/* Reads mouse activities from the proper port and update the screen
pointer position. */
void
boml_refresh (void)
{
int sx = x;
int sy = y;
/* How many bytes to read at once */
int howmany = 1;
struct mouse *mouse;
for (mouse = mice; mouse; mouse = mouse->next)
{
struct mouse_info *minfo = &mouse_info[mouse->type];
fcntl (mouse->fd, F_SETFL, O_NONBLOCK);
/* On bus mice (which means also all m68k mice) we have to read
three bytes instead of one... This might be true of other
mice as well. */
#if defined(M_ADB)
if (mouse->type == T_ADB)
howmany = 3;
#endif /* M_ADB */
#if defined (M_MSBUS)
if (mouse->type == T_MS_BUS)
howmany = 3;
#endif /* M_MSBUS */
while (read (mouse->fd, &mouse->pbuf[mouse->ppos], howmany) == howmany)
{
#ifdef M_SERIAL
/* The MouseMan protocol is extremely fscked up. Packets can
have 3 or 4 bytes. Attempt to detect changing from 3 byte to
4 byte packets. */
if (mouse->type == T_MMAN_SERIAL
&& mouse->ppos == 0
&& (mouse->pbuf[0] & minfo->id[0]) != minfo->id[1]
&& (mouse->pbuf[0] >> 4) <= 3)
{
int b = mouse->pbuf[0] >> 4;
mouse->packet_size = 4;
state (0, 0, b & 0x20);
mouse->ppos = 0;
continue;
}
#endif
if ((mouse->ppos == 0
&& (mouse->pbuf[0] & minfo->id[0]) != minfo->id[1])
|| (mouse->ppos == 1
&& (mouse->pbuf[1] & minfo->id[2]) != minfo->id[3]))
{
/* Nope, not a packet */
mouse->ppos = 0;
continue;
}
mouse->ppos += howmany;
if (mouse->ppos >= mouse->packet_size)
{
minfo->driver (mouse);
mouse->ppos = 0;
}
}
}
if ((sx != x || sy != y || !drawn) && show)
{
if (drawn)
bogl_pointer (0, sx, sy, pointer, pointer_colors);
bogl_pointer (1, x, y, pointer, pointer_colors);
drawn = 1;
}
}
/* Tells the library whether the mouse cursor is drawn. This is
different from whether it is *supposed* to be drawn. For instance,
if the screen got cleared by a VT switch, the mouse cursor may have
been erased inadvertently. */
void
boml_drawn (int is_drawn)
{
drawn = is_drawn;
}
/* Draw the mouse cursor if it's not drawn but it should be. */
void
boml_redraw (void)
{
if (show > 0 && !drawn)
{
bogl_pointer (1, x, y, pointer, pointer_colors);
drawn = 1;
}
}
/* Show the mouse cursor. The mouse cursor being `shown' is a
recursive property: if you call boml_show() N times to show the
cursor, then you must call boml_hide() N times in order to hide
it. */
void
boml_show (void)
{
if (!mice)
return;
show++;
if (show == 1 && !drawn)
{
bogl_pointer (1, x, y, pointer, pointer_colors);
drawn = 1;
}
}
/* Hide the mouse cursor. See comment above boml_show() for more
details. */
void
boml_hide (void)
{
if (!mice)
return;
show--;
if (show == 0 && drawn)
{
bogl_pointer (0, x, y, pointer, pointer_colors);
drawn = 0;
}
}
/* Change the mouse cursor to pointer P. The cursor is drawn in the
colors specified by COLORS. */
void
boml_pointer (const struct bogl_pointer *p, int colors[2])
{
boml_hide ();
pointer = p;
pointer_colors[0] = colors[0];
pointer_colors[1] = colors[1];
boml_show ();
}
/* If TEST == 0, sets all the mice' file descriptors into FDS. If
TEST != 0, returns if at least one file descriptor is set in
FDS. */
int
boml_fds (int test, fd_set *fds)
{
struct mouse *mouse;
for (mouse = mice; mouse; mouse = mouse->next)
if (test)
{
if (FD_ISSET (mouse->fd, fds))
return 1;
}
else
FD_SET (mouse->fd, fds);
return 0;
}
/* Check for mouse activity. Returns the mouse event type. Stores
the mouse location into (X,Y) and the button status into BTN, where
nonzero indicates the button is pressed. */
int
boml_event (int *x, int *y, int *btn)
{
int type;
if (qt == qh)
return BOML_E_NONE;
type = queue[qt].type;
if (x)
*x = queue[qt].x;
if (y)
*y = queue[qt].y;
if (btn)
*btn = queue[qt].btn;
qt = q_next (qt);
return type;
}
/* Bookkeeping. */
/* Add an event of the specified type to the event queue. */
static void
event (int type)
{
/* Merge multiple movement events into a single events. */
if (type == BOML_E_MOVE && qh != qt
&& queue[q_prev (qh)].type == BOML_E_MOVE)
{
queue[q_prev (qh)].x = x;
queue[q_prev (qh)].y = y;
return;
}
queue[qh].type = type;
queue[qh].x = x;
queue[qh].y = y;
queue[qh].btn = button;
qh = q_next (qh);
if (qt == qh)
qt = q_next (qt);
}
/* The mouse moved (DX,DY) units, and the button is in state BTN (!=0:
pressed). Record that fact. */
static void
state (int dx, int dy, int btn)
{
if (dx || dy)
{
x += dx;
y += dy;
if (x < 0)
x = 0;
if (x >= bogl_xres)
x = bogl_xres - 1;
if (y < 0)
y = 0;
if (y >= bogl_yres)
y = bogl_yres - 1;
}
if (btn != button)
{
button = btn;
if (button)
event (BOML_E_PRESS);
else
event (BOML_E_RELEASE);
}
else if (button)
event (BOML_E_MOVE);
}
/* Add a mouse of type TYPE to the list of mice. The mouse is open on
file descriptor FD. */
static void
add_mouse (int type, int fd)
{
struct mouse *mouse = malloc (sizeof (struct mouse));
mouse->next = mice;
mouse->type = type;
mouse->fd = fd;
mouse->ppos = 0;
mouse->packet_size = mouse_info[type].packet_size;
mice = mouse;
}
#ifdef M_GPM
/* If we have a gpm repeater, assume ms3. */
static int
detect_gpm (void)
{
int ret;
int fd;
struct sockaddr_un sock;
ret = 1;
/* Make sure GPM is answering requests... */
fd = socket (PF_UNIX, SOCK_STREAM, 0);
if(fd < 0)
ret = 0;
else
{
sock.sun_family = AF_UNIX;
strcpy(sock.sun_path, "/dev/gpmctl");
if(connect (fd, &sock, SUN_LEN(&sock)) < 0)
ret = 0;
close (fd);
}
fd = open ("/dev/gpmdata", O_RDONLY | O_NONBLOCK);
if (fd < 0)
return 0;
if (bogl_cloexec (fd) < 0) {
close (fd);
return 0;
}
/* Poll the mouse whether or not we could find gpm, in
case it starts up later; but keep searching in that case. */
add_mouse (T_MS3_SERIAL, fd);
return ret;
}
#endif
#ifdef M_INPUT
/* Check for /dev/input/mice, by opening mouse0 instead -
i.e. check that there's really a mouse there right now.
Keep it open anyway, but set the mouse found flag accordingly. */
static void
detect_input (void)
{
int fd;
fd = open("/dev/input/mouse0", O_RDONLY | O_NONBLOCK);
if (fd >= 0) {
input_mouse_found = 1;
close(fd);
}
fd = open("/dev/input/mice", O_RDONLY | O_NONBLOCK);
if(fd < 0)
return;
if (bogl_cloexec(fd) < 0) {
close(fd);
return;
}
add_mouse(T_PS2, fd);
}
#endif
/* PS/2 mouse code. */
#ifdef M_PS2
/* Attempt to detect presence of a PS/2 mouse. If successful, sets
`type' to indicate mouse type. */
static void
detect_ps2 (void)
{
static const unsigned char s2[] = { 246, 230, 244, 243, 100, 232, 3, };
int fd;
fd = open ("/dev/psaux", O_RDWR | O_NONBLOCK);
if (fd < 0)
return;
if (bogl_cloexec (fd) < 0) {
close (fd);
return;
}
write (fd, s2, sizeof s2);
usleep (30000);
tcflush (fd, TCIFLUSH);
add_mouse (T_PS2, fd);
}
#endif /* M_PS2 */
#ifdef M_PS2_DRIVER
static void
ps2_driver (struct mouse *m)
{
int x, y;
x = y = 0;
if (m->pbuf[1])
x = (m->pbuf[0] & 0x10) ? m->pbuf[1] - 256 : m->pbuf[1];
if (m->pbuf[2])
y = (m->pbuf[0] & 0x20) ? 256 - m->pbuf[2] : -m->pbuf[2];
state (x, y, m->pbuf[0] & 1);
}
#endif /* M_PS2_DRIVER */
/* Microsoft/Apple Busmouse and Sun mouse code. */
#ifdef M_MSBUS
/* Attempt to detect presence of a Microsoft bus mouse. If
successful, sets `type' to indicate mouse type. */
static void
detect_msbus (void)
{
int fd = open ("/dev/inportbm", O_RDONLY | O_NONBLOCK);
if (fd < 0)
return;
if (bogl_cloexec (fd) < 0) {
close (fd);
return;
}
add_mouse (T_MS_BUS, fd);
}
#endif /* M_MSBUS */
#ifdef M_ADB
static void
detect_adb (void)
{
int fd = open ("/dev/adbmouse", O_RDONLY | O_NONBLOCK);
if (fd < 0)
return;
if (bogl_cloexec (fd) < 0) {
close (fd);
return;
}
add_mouse (T_ADB, fd);
}
#endif /* M_ADB */
#ifdef M_SUN
/* FIXME: Reading the GPM code tells me that this is almost certainly
wrong. Some Sparc person will have to fix it. */
static void
detect_sun (void)
{
int fd = open ("/dev/sunmouse", O_RDONLY | O_NONBLOCK);
if (fd < 0)
return;
if (bogl_cloexec (fd) < 0) {
close (fd);
return;
}
add_mouse (T_SUN, fd);
}
#endif /* M_SUN */
/* The decoder is definitely the same for all three though */
#if defined(M_ADB) || defined(M_SUN) || defined(M_MSBUS)
static void
bm_driver (struct mouse *m)
{
signed char *p = (signed char *) (m->pbuf);
state (p[1], -p[2], !(p[0] & 0x04));
}
#endif /* M_ADB || M_SUN || M_MSBUS */
/* Serial mice. */
#ifdef M_SERIAL
/* Attempt to detect presence of a serial mouse. If successful, sets
`type' to indicate mouse type. */
static void
detect_serial (void)
{
static char device[] = "/dev/ttySx";
for (device[9] = '0'; device[9] <= '3'; device[9]++)
{
int success;
success = probe_com (device, 1);
inc ();
if (!success)
probe_com (device, 0);
inc ();
}
}
#endif /* M_SERIAL */
#ifdef M_SERIAL_MS
/* ms and ms3 protocols are the same except that ms3 has an extra byte
in each packet. */
static void
ms_driver (struct mouse *m)
{
state ((signed char) ((m->pbuf[0] & 0x03) << 6) | (m->pbuf[1] & 0x3f),
(signed char) ((m->pbuf[0] & 0x0c) << 4) | (m->pbuf[2] & 0x3f),
m->pbuf[0] & 0x20);
}
#endif
#ifdef M_SERIAL
static void
msc_driver (struct mouse *m)
{
signed char *p = (signed char *) (m->pbuf);
state (p[3] + p[1], p[4] - p[2], !(m->pbuf[0] & 0x04));
}
static void
mman_driver (struct mouse *m)
{
if (m->packet_size == 4 && (m->pbuf[3] >> 4) == 0)
m->packet_size = 3;
state ((signed char) (((m->pbuf[0] & 0x03) << 6) | (m->pbuf[1] & 0x3f)),
(signed char) (((m->pbuf[0] & 0x0c) << 4) | (m->pbuf[2] & 0xef)),
m->pbuf[0] & 0x20);
}
#if 0
static void
logi_driver (struct mouse *m)
{
state (m->pbuf[0] & 0x10 ? m->pbuf[1] : -m->pbuf[1],
m->pbuf[0] & 0x08 ? -m->pbuf[2] : m->pbuf[2],
m->pbuf[0] & 0x04);
}
#endif /* 0*/
#endif /* M_SERIAL_DRIVER */
#if 0
/* Test routine. */
int
main (void)
{
boml_init ();
return 0;
}
#endif
#ifdef M_SERIAL
/* The following code comes from mice.c in gpm-1.13, although it is
heavily modified. The original copyright notice is reproduced in
full below. */
/*
* mice.c - mouse definitions for gpm-Linux
*
* Copyright 1993 ajh@gec-mrc.co.uk (Andrew Haylett)
* Copyright 1994-1998 rubini@linux.it (Alessandro Rubini)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
********/
static void
set_speed (int fd, int old, unsigned short flags)
{
struct termios tty;
tcgetattr(fd, &tty);
tty.c_iflag = IGNBRK | IGNPAR;
tty.c_oflag = 0;
tty.c_lflag = 0;
tty.c_line = 0;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_cflag = flags | old;
tcsetattr(fd, TCSAFLUSH, &tty);
write(fd, "*n", 2);
usleep(100000);
tty.c_cflag = flags | B1200;
tcsetattr(fd, TCSAFLUSH, &tty);
}
static void
init_serial (int fd)
{
const int flags = CS7 | CREAD | CLOCAL | HUPCL;
/* Change to 1200 baud from any baud rate. */
set_speed (fd, B9600, flags);
set_speed (fd, B4800, flags);
set_speed (fd, B2400, flags);
set_speed (fd, B1200, flags);
/* Flush pending input. */
{
struct timeval timeout = {0, 0};
unsigned char c;
fd_set set;
FD_ZERO (&set);
for (;;)
{
FD_SET (fd, &set);
switch (select (fd+1, &set, NULL, NULL, &timeout))
{
case 1:
if (read(fd,&c,1)==0)
break;
case -1:
continue;
}
break;
}
}
}
/* The code below is taken from the following files in the Red Hat 5.2
mouseconfig-3.1.3 distribution. It's been essentially rewritten.
pnp_probe_com.c
pnp_probe_com.h
The original copyright notice is reproduced in full below. All
modifications are subject to the license at the top of this
file. */
/* probe serial port for PnP/Legacy devices
*
*
* Michael Fulbright (msf@redhat.com)
*
* Copyright 1997 Red Hat Software
*
* This software may be freely redistributed under the terms of the GNU
* public license.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
struct pnp_com_id
{
unsigned char other_id[17]; /* For pre-PNP compatibility. */
unsigned char other_len; /* Length of the other_id. */
unsigned char pnp_rev_major; /* PnP major revision number. */
unsigned char pnp_rev_minor; /* PnP minor revision number. */
unsigned char manufacturer[4]; /* EISA manufacturer (string). */
unsigned char product_id[5]; /* Mfr determined product ID (string) */
unsigned char serial_number[9]; /* Optional dev serial number (string) */
unsigned char class_name[33]; /* Optional PnP Class name (string) */
unsigned char driver_id[42]; /* Optional compat device IDs (string) */
unsigned char user_name[42]; /* Optional verbose product descr (string) */
};
/* There are two possible bytes to signify the start of a PnP ID
string. */
#define BeginPnP1 0x28
#define BeginPnP2 0x08
/* Likewise, two possible stop bytes. */
#define EndPnP1 0x29
#define EndPnP2 0x09
/* These chars indicate extensions to the base dev id exist. */
#define ExtendPnP1 0x5c
#define ExtendPnP2 0x3c
#define PNP_COM_MAXLEN 256
/* Wait until there is data available on fd, for up to TIMEOUT
microseconds. */
static int
wait_for_input (int fd, long timeout)
{
struct timeval tv;
fd_set ready;
int n;
tv.tv_sec = 0;
tv.tv_usec = timeout;
FD_ZERO (&ready);
FD_SET (fd, &ready);
n = select (fd + 1, &ready, NULL, &ready, &tv);
return n;
}
static int
open_serial_port (char *port)
{
int fd;
fd = open (port, O_RDWR | O_NONBLOCK);
if (fd < 0)
return fd;
if (bogl_cloexec (fd) < 0)
{
close (fd);
return -1;
}
/* Reset file so it is no longer in non-blocking mode. */
if (fcntl (fd, F_SETFL, 0) < 0)
{
close (fd);
return -1;
}
return fd;
}
/* <0 means ioctl error occurred. */
static int
get_serial_lines (int fd)
{
int modem_lines;
ioctl (fd, TIOCMGET, &modem_lines);
return modem_lines;
}
/* <0 means ioctl error occurred */
static int
set_serial_lines (int fd, int modem_lines)
{
return ioctl (fd, TIOCMSET, &modem_lines);
}
static int
get_serial_attr (int fd, struct termios *attr)
{
return tcgetattr (fd, attr);
}
static int
set_serial_attr (int fd, struct termios *attr)
{
return tcsetattr (fd, TCSANOW, attr);
}
/* Set serial port to 1200 baud, 'nbits' bits, 1 stop, no parity. */
static int
setup_serial_port (int fd, int nbits)
{
struct termios attr;
if (get_serial_attr (fd, &attr) < 0)
return 0;
attr.c_iflag = IGNBRK | IGNPAR;
attr.c_cflag = 0;
attr.c_cflag &= ~(CSIZE | CSTOPB | PARENB | PARODD | PARENB);
attr.c_cflag |= CREAD | CLOCAL; /*| CRTSCTS ; */
if (nbits == 7)
attr.c_cflag |= CS7 | CSTOPB;
else
attr.c_cflag |= CS8;
attr.c_oflag = 0;
attr.c_lflag = 0;
attr.c_cc[VMIN] = 1;
attr.c_cc[VTIME] = 5;
cfsetospeed (&attr, B1200);
cfsetispeed (&attr, B1200);
return set_serial_attr (fd, &attr) >= 0;
}
/* Request for PnP info from serial device. See page 6 of the pnpcom
doc from Microsoft. Returns nonzero only if successful. */
static int
init_pnp_com_seq1 (int fd)
{
int modem_lines = get_serial_lines (fd);
/* Turn off RTS and wait 200 ms for DSR to come up. */
set_serial_lines (fd, modem_lines & ~(TIOCM_RTS));
usleep (200000);
/* See if DSR came up. */
modem_lines = get_serial_lines(fd);
if (!(modem_lines & TIOCM_DSR))
{
set_serial_lines (fd, modem_lines | TIOCM_DTR | TIOCM_RTS);
return 0;
}
/* Com port setup, 1st phase. Now we set port to be 1200 baud, 7
bits, no parity, 1 stop bit. */
if (!setup_serial_port (fd, 7))
return 0;
/* Drop DTR and RTS. */
modem_lines &= ~(TIOCM_RTS | TIOCM_DTR);
set_serial_lines (fd, modem_lines);
usleep (200000);
/* Bring DTR back up. */
modem_lines |= TIOCM_DTR;
set_serial_lines (fd, modem_lines);
usleep (200000);
/* Enter next phase. */
modem_lines |= TIOCM_RTS;
set_serial_lines (fd, modem_lines);
usleep (200000);
return 1;
}
/* See if this is a legacy mouse device. Only called if the PnP probe
above failed. We turn off the mouse via RS232 lines, then turn it
on. If it spits out an 'M' character (at 1200 baud, 7N1) it could
be a mouse. Returns nonzero only if successful. */
static int
legacy_probe_com (int fd)
{
/* Now we set port to be 1200 baud, 7 bits, no parity, 1 stop bit. */
if (!setup_serial_port (fd, 7))
return 0;
/* Drop DTR and RTS, then bring them back up. */
{
int modem_lines = get_serial_lines (fd);
set_serial_lines (fd, modem_lines & ~(TIOCM_RTS | TIOCM_DTR));
usleep (200000);
set_serial_lines (fd, modem_lines | TIOCM_DTR | TIOCM_RTS);
}
/* Start reading - quit after first character. */
{
int starttime = (int) time (NULL);
for (;;)
{
if (wait_for_input (fd, 250000) <= 0)
return 0;
/* Read a character. */
{
unsigned char resp;
if (read (fd, &resp, 1) > 0)
return resp == 'M';
if (errno != EAGAIN)
return 0;
}
/* Shouldn't run more than 2 seconds. */
if (time (NULL) - starttime > 2)
return 0;
}
}
}
/* Retrieve the PnP ID string. Timeout after 3 seconds. Should
probably set a 200 msec timeout per char, as spec says. If no char
received, we're done. Returns number of characters retrieved. */
static int
read_pnp_string (int fd, unsigned char *pnp_string, int *pnp_len)
{
int pnp_index;
time_t starttime;
int end_char;
pnp_index = 0;
end_char = -1;
starttime = time (NULL);
for (;;)
{
unsigned char c;
/* Don't wait more than 3 seconds. */
if (time (NULL) - starttime > 4)
break;
/* Wait for a character to arrive. */
if (wait_for_input (fd, 250000) <= 0)
break;
/* Read a byte. */
{
ssize_t nbytes = read (fd, &c, 1);
if (nbytes < 0 && errno != EAGAIN)
break;
if (nbytes == 0)
continue;
}
/* Store the byte. */
if (pnp_index < 99)
pnp_string[pnp_index++] = c;
/* Check for end of string. */
if (end_char != -1)
{
if (c == end_char)
break;
}
else if (c == BeginPnP1)
end_char = EndPnP1;
else if (c == BeginPnP2)
end_char = EndPnP2;
}
pnp_string[pnp_index] = 0;
*pnp_len = pnp_index;
return pnp_index;
}
/* Parse the PnP ID string into components. Returns nonzero only if
successful. */
static int
parse_pnp_string (unsigned char *pnp_id_string, int pnp_len,
struct pnp_com_id *pnp_id)
{
unsigned char pnp_string[100];
unsigned char *p1, *p2;
unsigned char *start;
unsigned char *curpos;
int xlate_6bit;
int stage;
/* Clear out pnp_id and make a local copy of pnp_id_string. */
memset (pnp_id, 0, sizeof (*pnp_id));
memcpy (pnp_string, pnp_id_string, pnp_len + 1);
/* First find the start of the PnP part of string. Use the marker
which points nearest to start of the string and is actually
defined. The length of the initial part cannot be more than 17
bytes. */
{
p1 = memchr (pnp_string, BeginPnP1, pnp_len);
p2 = memchr (pnp_string, BeginPnP2, pnp_len);
start = p1;
if (!start || (p2 && p2 < start))
start = p2;
if (!start || start - pnp_string > 17)
return 0;
}
/* Copy everything before the start of the PnP block. */
memcpy (pnp_id->other_id, pnp_string, start - pnp_string);
pnp_id->other_len = start - pnp_string;
/* Translate data in PnP fields if necessary. */
if (start == p2)
{
unsigned char *cp;
for (cp = start; ; cp++)
{
/* Skip the revision fields (bytes 1 and 2 after start). */
if (cp != start + 1 && cp != start + 2)
*cp += 0x20;
putchar (*cp);
if (*cp == EndPnP1)
break;
}
xlate_6bit = 1;
}
else
xlate_6bit = 0;
/* Now we get the PnP fields - all were zeroed out above. */
curpos = start + 1;
{
int rev_tmp = ((curpos[0] & 0x3f) << 6) + (curpos[1] & 0x3f);
pnp_id->pnp_rev_major = rev_tmp / 100;
pnp_id->pnp_rev_minor = rev_tmp % 100;
}
curpos += 2;
memcpy (pnp_id->manufacturer, curpos, 3);
curpos += 3;
memcpy (pnp_id->product_id, curpos, 4);
curpos += 4;
/* Now read extension fields, if any. */
for (stage = 0; *curpos == ExtendPnP1 || *curpos == ExtendPnP2; stage++)
{
static const char extension_delims[] =
{EndPnP1, ExtendPnP1, ExtendPnP2, 0};
int len;
unsigned char *endfield = (unsigned char *) strpbrk ((char *) ++curpos, extension_delims);
if (!endfield)
return 0;
/* If we reached the end of all PnP data, back off since
there is a checksum at the end of extension data. */
len = endfield - curpos;
if (*endfield == EndPnP1)
len -= 2;
switch (stage)
{
case 0:
if (len != 8 && len != 0)
return 0;
memcpy (pnp_id->serial_number, curpos, len);
break;
case 1:
if (len > 33)
return 0;
memcpy (pnp_id->class_name, curpos, len);
break;
case 2:
if (len > 41)
return 0;
memcpy (pnp_id->driver_id, curpos, len);
break;
case 3:
if (len > 41)
return 0;
memcpy (pnp_id->user_name, curpos, len);
break;
default:
/* Ignore additional extension fields. */
break;
}
curpos += len;
if (*endfield == EndPnP1)
break;
}
/* If we had any extensions, we expect and check a checksum. */
if (stage)
{
unsigned int checksum;
const unsigned char *cp;
char hex_checksum[3];
checksum = curpos[2];
for (cp = start; cp < curpos; cp++)
checksum += *cp;
if (xlate_6bit)
checksum -= 0x20 * (curpos - start + 1 - 2);
sprintf (hex_checksum, "%.2X", checksum & 0xff);
if (strncmp (hex_checksum, (char *) curpos, 2))
return 0;
}
return 1;
}
static int
guess_mouse_type (struct pnp_com_id *pnp_id)
{
int type;
/* First, figure out whether it's a mouse or not. */
if (pnp_id->class_name[0] == 0)
{
char *model;
model = strstr ((char *) pnp_id->driver_id, "PNP");
if (model)
model += 3;
else
model = (char *) pnp_id->product_id;
if (strncmp (model, "0F", 2))
return -1;
}
else if (strcmp ((char *) pnp_id->class_name, "MOUSE"))
return -1;
/* It's a mouse. Default to Microsoft protocol--most common. */
type = T_MS_SERIAL;
/* Test for some common mouse types. Send me more! */
{
const char *mfg = (char *) pnp_id->manufacturer;
const char *model = (char *) pnp_id->product_id;
if (!strcmp (mfg, "MSH") && !strcmp (model, "0001"))
type = T_MS3_SERIAL;
else if (!strcmp (mfg, "LGI") && !strncmp (model, "80", 2))
type = T_MMAN_SERIAL;
else if (!strcmp (mfg, "KYE") && !strcmp (model, "0003"))
type = T_MS3_SERIAL;
}
return type;
}
/* If PNP != 0, checks for a plug-n-play mouse on device PORT; if PNP
== 0, checks for a legacy mouse on that device. Returns nonzero if
a device is found. */
static int
probe_com (char *port, int pnp)
{
struct termios origattr;
/* Open serial port. */
int fd = open_serial_port (port);
if (fd < 0)
return 0;
/* Save port attributes. */
if (get_serial_attr (fd, &origattr) < 0)
{
close (fd);
return 0;
}
/* Retrieve PnP string or check for legacy mouse. */
if (pnp)
{
struct pnp_com_id pnp_id;
unsigned char pnp_string[100];
int pnp_strlen;
if (init_pnp_com_seq1 (fd)
&& read_pnp_string (fd, pnp_string, &pnp_strlen)
&& parse_pnp_string (pnp_string, pnp_strlen, &pnp_id))
{
int type = guess_mouse_type (&pnp_id);
if (type != -1)
{
add_mouse (type, fd);
return 1;
}
}
}
else if (legacy_probe_com (fd))
{
init_serial (fd);
/* FIXME: Must detect MS, MSC, LOGI. */
add_mouse (T_MS_SERIAL, fd);
return 1;
}
else
{
/* Restore port attributes. */
set_serial_attr (fd, &origattr);
close (fd);
}
return 0;
}
#endif /* M_SERIAL */
|