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 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
|
/* @configure_input@ */
/*-----------------------------------------------------------------------------
*
* (C) 1998 Spacetec IMC Corporation ("Spacetec").
*
* Permission to use, copy, modify, and distribute this software for all
* purposes and without fees is hereby granted provided that this copyright
* notice appears in all copies. Permission to modify this software is granted
* and Spacetec will support such modifications only if said modifications are
* approved by Spacetec.
*
*/
/* Some code cleanup by the Coin team. Also added configure tests for
* header files and added two methods:
*
* int SPW_CheckForSpaceballX11(void * display, int winid, char * product);
* int SPW_TranslateEventX11(void * display, void * xevent, SPW_InputEvent * sbEvent);
*/
/* FIXME: isn't this really "pure" C code? If so, don't use a C++
suffix on the file -- so we don't get tempted to pollute
it. 20010821 mortene. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#if SO@GUI@_DEBUG && 0 // debug
#define SPW_DEBUG 1
#endif // debug
#include "spwinput.h"
/* The setting of this define needs to be added manually to
configure.in for all relevant projects. */
#ifndef HAVE_X11_AVAILABLE
/* just provide empty methods if X is not available */
int
SPW_CheckForSpaceballX11(void *, int, char *)
{
return 0;
}
int
SPW_TranslateEventX11(void *, void *, SPW_InputEvent *)
{
return 0;
}
#else /* HAVE_X11_AVAILABLE */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif // HAVE_UNISTD_H
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif /* HAVE_NETINET_IN_H */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif /* HAVE_SYS_TIME_H */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */
#ifdef HAVE_SELECT_H
#include <select.h>
#endif /* HAVE_SELECT_H */
#define XLIB_ILLEGAL_ACCESS
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#ifdef HAVE_X11_EXTENSIONS_SGIMISC_H
#include <X11/extensions/SGIMisc.h>
#elif HAVE_X11_XPROTO_H
#include <X11/Xproto.h>
#endif /* HAVE_X11_XPROTO */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
#include <X11/extensions/XInput.h>
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/* Callbacks for handling motion and button events */
typedef void (*SPW_InputMotionEventHandler)(Display *,float *,void *);
typedef void (*SPW_InputButtonEventHandler)(Display *,int,void *);
typedef int (*SPW_InputOtherEventHandler)(Display *,XEvent *,void *);
/* Dispatch structure for SPW_InputDispatch */
typedef struct {
Display * display;
XEvent * xevent;
SPW_InputEvent * spwevent;
int delay;
SPW_InputMotionEventHandler handle_motion;
SPW_InputButtonEventHandler handle_bpress;
SPW_InputButtonEventHandler handle_brelease;
SPW_InputOtherEventHandler handle_other;
void * pMotionAppData;
void * pButtonAppData;
void * pOtherAppData;
} SPW_InputDispatchStruct;
/* ------------------------------------------------------------------------ */
static int SPW_InputCheckForSpaceball(Display *, Window, char *);
static int SPW_InputIsSpaceballEvent (Display *, XEvent *,
SPW_InputEvent *);
static int SPW_InputXPending (Display *, int);
static void SPW_InputInitDispatchStruct (SPW_InputDispatchStruct *);
static int SPW_InputDispatch (SPW_InputDispatchStruct *);
static int SPW_InputDispatchEx (SPW_InputDispatchStruct *);
static void SPW_InputBeep (Display *, char *);
static void SPW_InputRezero (Display *);
static int SPW_GrabDevice(Display *display, Window window,
int exclusive);
static int SPW_ReleaseDevice(Display * display);
#define SPW_InputDefaultDelay 0 /* 0 Hz update */
/*
Wrapper function which makes it possible to avoid including X11 in the
header file.
*/
int
SPW_CheckForSpaceballX11(void * display, int winid, char * product)
{
return SPW_InputCheckForSpaceball((Display*) display, (Window) winid, product);
}
/*
If xevent is a spaceball event, translates xevent to sbEvent and return 1.
If xevent is not a spaceball event returns 0. void * pointers are
passed to avoid including the X header files in the spwinput.h header file.
*/
int
SPW_TranslateEventX11(void * display, void * xevent, SPW_InputEvent * sbEvent)
{
return SPW_InputIsSpaceballEvent((Display*) display, (XEvent*) xevent, sbEvent);
}
/*
* This is the number of events before we stop asking the
* driver for handshaking information.
*
* 25 events/sec * 60 sec * 1 min = 1500
*/
#define SPW_MaxEventCount 1500
/* these are all the Spaceball XCM atom names */
#define SPW_InputMotionAtomName "SpaceballMotionEventType"
#define SPW_InputButtonPressAtomName "SpaceballButtonPressEventType"
#define SPW_InputButtonReleaseAtomName "SpaceballButtonReleaseEventType"
#define SPW_InputPassThruAtomName "SpaceWarePassThruCommandEventType"
/* found the spaceball */
static int SpaceballFound = 0;
/* the spaceball is using XIE */
static int SpaceballInputExtension = 0;
/* the spaceball is using XCM */
static int SpaceWareAtomsDefined = 0;
/* the magic XCM window used for communication */
static Window SpaceWareXCMWindowID = 0;
/* the window ID that the spaceball data is sent to */
static Window ReturnWindowID = 0;
/* XSendEvent uses these types and Atoms to communicate with the driver */
static int SPW_DevMotionEventType; /* Spaceball motion type */
static int SPW_DevButtonPressEventType; /* Spaceball button press type */
static int SPW_DevButtonReleaseEventType; /* Spaceball button release type */
static Atom SPW_InputMotionAtom; /* Spaceball motion Atom */
static Atom SPW_InputButtonPressAtom; /* Spaceball button press Atom */
static Atom SPW_InputButtonReleaseAtom; /* Spaceball button release Atom */
static Atom SPW_InputPassThruAtom; /* Main daemon communication Atom */
static Atom WM_SAVE_YOURSELF; /* Atoms daemon will use */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
static XID SpaceballDevID = 0; /* XIE ID for the Spaceball device */
static XDevice *pSpaceballDev = NULL; /* The spaceball XIE structure */
static XEventClass SPW_SpaceballEventClass[3]; /* XIE Events classes */
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
static char strData[19]; /* buffer used to store the string that
is to be sent to XCM daemon */
static int SPW_strLength = 0; /* length of the strData string */
/*-----------------------------------------------------------------------------
*
* static Window FindXCMWindow (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* The window ID of the XCM window.
*
* Description:
* Finds the window named "sballd_XCM" and returns it's window ID
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static Window
FindXCMWindow (Display *display)
{
Window root; /* the root window for the display */
Window parent; /* parent window in the search tree */
Window *children; /* children windows of the parrent window */
unsigned int nChildren; /* number of children the parent window has */
int i; /* counter */
/* if we already have the daemon window don't look again */
if (SpaceWareXCMWindowID != 0) {
return SpaceWareXCMWindowID;
}
XQueryTree(display, RootWindowOfScreen(DefaultScreenOfDisplay(display)),
&root, &parent, &children, &nChildren);
for (i = 0; i < (int) nChildren; i++){
char *name;
int found = 0;
XFetchName (display, children[i], &name);
if (name) {
#ifdef SPW_DEBUG
fprintf(stderr,"FindXCMWindow: %s\n", name);
#endif
if (strcmp(name, "sballd_XCM") == 0) {
found = 1;
}
XFree(name);
}
if (found == 1) {
break;
}
}
if (i == (int)nChildren) {
SpaceWareXCMWindowID = 0;
}
else {
SpaceWareXCMWindowID = children[i];
}
XFree((char *) children);
if (SpaceWareXCMWindowID == 0) {
return False;
}
else {
return SpaceWareXCMWindowID;
}
} /* end of FindXCMWindow */
/*-----------------------------------------------------------------------------
*
* static void StringFlush (Display *display, Window win)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* win (r/o) The window that the XCM daemon replies to
*
* Return Value:
* None.
*
* Description:
* This function sends the string that is in the strData buffer to the
* XCM window. Once it is send it clears the strData buffer.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static void StringFlush (Display *display, Window win)
{
XClientMessageEvent event; /* event used to send string to XCM daemon */
int i; /* counter used to step through the string */
/* if we don't already have the XCM window id find it */
if (SpaceWareXCMWindowID == 0) {
FindXCMWindow (display);
}
if (SpaceWareXCMWindowID == 0) {
return;
}
if (SPW_strLength == 0) { /* if the string is empty do nothing */
return;
}
/* set up the event structure for sending */
event.type = ClientMessage;
event.display = display;
event.window = SpaceWareXCMWindowID;
event.message_type = SPW_InputPassThruAtom;
event.format = 8;
/* copy the string into the event */
for (i = 0; i < 15; i++) {
event.data.b[i] = strData[i];
}
if (SPW_strLength > 15) {
SPW_strLength = 15;
}
/* force zero on end of data */
event.data.b[SPW_strLength] = 0;
event.data.l[4] = (long)htonl(win);
#ifdef SPW_DEBUG
fprintf (stderr,"StringFlush %s\n", event.data.b);
#endif
/* send the string to the XCM daemon */
if (XSendEvent (display, SpaceWareXCMWindowID, True, 0,
(XEvent *)&event) == 0) {
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_Input: XSendEvent failed!\n");
#endif
}
XSync (display,False);
/* clear the buffer */
SPW_strLength = 0;
} /* end of StringFlush */
/*-----------------------------------------------------------------------------
*
* static void SendString (Display *display, char *string)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* string (r/o) The string that is sent to the XCM daemon
*
* Return Value:
* None.
*
* Description:
* This function sends string to the XCM window and flushs the
* internal buffer.
*
* Notes:
* To flush the string call StringFlush.
*
*---------------------------------------------------------------------------*/
static void SendString (Display *display, char *string)
{
int i; /* counter used to step through the string */
/* copy the string into strData and send strData to the XCM driver */
for (i = 0; i < (int) strlen (string); i++) {
strData[SPW_strLength++] = string[i];
}
StringFlush (display, ReturnWindowID);
} /* end of String */
/*-----------------------------------------------------------------------------
*
* void SPW_InputString (Display *display, char *string)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* string (r/o) The string that is sent to the XCM daemon
*
* Return Value:
* None.
*
* Description:
* This function sends string to the XCM window using SendString.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputString (Display *display, char *string)
{
SPW_strLength = 0; /* flush the internal buffer */
SendString (display, string); /* setup the string for sending */
}
/*-----------------------------------------------------------------------------
*
* void SPW_InputResetGlobalVars (void)
*
* Args:
* None.
*
* Return Value:
* None.
*
* Description:
* This function resets all the static global variable used in the Spaceball
* input function.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputResetGlobalVars (void)
{
SpaceballFound = 0;
SpaceballInputExtension = 0;
SpaceWareAtomsDefined = 0;
SpaceWareXCMWindowID = 0;
} /* end of SPW_InputResetGlobalVars */
/*-----------------------------------------------------------------------------
*
* void SPW_InputResetSpaceball (Display *display)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
*
* Return Value:
* None.
*
* Description:
* This function resets the Spaceball attached to the display defined in
* the display variable. The daemon will reset all internal Spaceball
* variables to their default and rezeros the Spaceball itself.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputResetSpaceball (Display *display)
{
SPW_strLength = 0;
static char RESET[] = "RESET";
SendString (display, RESET);
} /* end of SPW_InputResetSpaceball */
/*-----------------------------------------------------------------------------
*
* void SPW_InputCloseXIE (Display *display)
*
* Args:
* display (r/o) The X Display to which the Spaceball is attached
*
* Return Value:
* None.
*
* Description:
* This function closes all comunication with the Spaceball XIE driver.
*
* Notes:
* When the XIE driver is closed the Spaceball may lose power.
*
*---------------------------------------------------------------------------*/
void SPW_InputCloseXIE (Display *display)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
if (SpaceballInputExtension == 1) {
XCloseDevice (display, pSpaceballDev);
SpaceballInputExtension = 0;
SpaceballFound = 0;
}
#endif
} /* end of SPW_InputCloseXIE */
/*-----------------------------------------------------------------------------
*
* int SPW_InputIsXIE (void)
*
* Args:
* None.
*
* Return Value:
* 0 if XIE is disabled or 1 if it is enabled.
*
* Description:
* This function determines if the Spaceball XIE driver is enabled.
*
* Notes:
* This is exported from this library so users of this library
* can determine the method of communication used.
*
*---------------------------------------------------------------------------*/
int SPW_InputIsXIE (void)
{
return SpaceballInputExtension;
} /* end of SPW_InputIsXIE */
/*-----------------------------------------------------------------------------
*
* int SPW_InputIsXCM (void)
*
* Args:
* None.
*
* Return Value:
* 0 if XCM is disabled or 1 if it is enabled.
*
* Description:
* This function determines f the Spaceball XCM driver is enabled.
*
* Notes:
* This is exported from this library so users of this library
* can determine the method of communication used.
*
*---------------------------------------------------------------------------*/
int SPW_InputIsXCM (void)
{
return SpaceWareAtomsDefined;
} /* end of SPW_InputIsXCM */
/*-----------------------------------------------------------------------------
*
* void SPW_InputSelectExtensionEvent (Display *display, Window window)
*
* Args:
* display (r/o) The X Display to which the Spaceball is attached
* window (r/o) What window to check for extension events
*
* Return Value:
* None.
*
* Description:
* Check to see if the Spaceball XInput Extension is enabled.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputSelectExtensionEvent (Display *display, Window window)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
XSelectExtensionEvent (display, window, SPW_SpaceballEventClass, 3);
#endif
} /* end of SPW_InputSelectExtensionEvent */
/*-----------------------------------------------------------------------------
*
* void SPW_SendHandshake(Display *display)
*
* Args:
* display (r/o) The display on which to send the message
*
* Return Value:
* None
*
* Description:
* Sends a handshaking packet to the Xdaemon with what version of
* the software we are running. Eventually we may even use this
* information. Right now it's mainly to make sure that the soft
* button window gets the correct cache values.
*
* Notes:
* This function returns nothing, but sends a message to the Xdaemon.
* The message is sent to guaranty the daemon gets the window id
* of the application.
*
*---------------------------------------------------------------------------*/
void SPW_SendHandshake (Display *display)
{
static char pHandshake[20]; /* store some space */
if (SpaceWareXCMWindowID != 0) /* using daemon */
{
SPW_strLength = 0;
pHandshake[0] = '\0';
strcat (pHandshake, "~~SLIM V");
strcat (pHandshake, SPW_INPUT_VERSION);
strcat (pHandshake, "\r");
SendString (display, pHandshake);
}
} /* end of SPW_SendHandshake */
/*-----------------------------------------------------------------------------
*
* int SPW_GrabDevice(Display *display, Window window, int exclusive)
*
* Args:
* display (r/o) The current display pointer
* window (r/o) The window to send to
* exclusive (r/o) Specify if the grab is exclusive
*
* Return Value:
* None
*
* Description:
* This function is used to signal the driver that the application wishes
* to "grab" the Spaceball device. This prevents other applications from
* getting data from the device. There are two possible grab modes: An
* exclusive grab forces the driver to send data to the application window
* regardless of what window has focus. A non-exclusive grab forces the
* driver to send Spaceball data to the application window when no other
* Spaceball enabled window has focus.
*
* Notes:
* This function does not work with XIE.
* It is recommended that exclusive grab not be used.
* This function will change the ReturnWindowID if the window parameter
* is not NULL.
*
*---------------------------------------------------------------------------*/
int SPW_GrabDevice(Display *display, Window window,
int exclusive)
{
if (window != 0) {
ReturnWindowID = window;
}
if ((ReturnWindowID == 0) || /* return window not set */
(SpaceWareXCMWindowID == 0)) {/* daemon not running */
return 0;
}
SPW_strLength = 0;
if (exclusive == 1)
{
static char hard[] = "~hard";
SendString (display, hard);
}
else
{
static char soft[] = "~soft";
SendString (display, soft);
}
return 1;
} /* end of SPW_GrabDevice */
/*-----------------------------------------------------------------------------
*
* void SPW_ReleaseDevice(Display *display)
*
* Args:
* display (r/o) The current display pointer
*
* Return Value:
* None
*
* Description:
* This function is used to release a grabbed device. This frees the
* driver to send data to the application with focus regardless if it has
* been Spaceball enabled.
*
* Notes:
* This function does not work with XIE.
*
*---------------------------------------------------------------------------*/
int SPW_ReleaseDevice(Display *display)
{
if ((ReturnWindowID == 0) || /* return window not set */
(SpaceWareXCMWindowID == 0)) /* daemon not running */
{
return 0;
}
SPW_strLength = 0;
static char relgrab[] = "~relgrab";
SendString (display, relgrab);
return 1;
} /* end of SPW_ReleaseDevice */
/*-----------------------------------------------------------------------------
*
* static int FindXCMAtoms (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM atoms
*
* Return Value:
* 1 if the XCM atoms were found, 0 if not.
*
* Description:
* Returns True if all the XCM atoms are found.
*
* Notes:
* Has the side effect of looking up the atoms and putting the
* values in the SPW_Input...Atom variables
*
*---------------------------------------------------------------------------*/
static int FindXCMAtoms (Display *display)
{
/* if we already have the atoms don't look again */
if (SpaceWareAtomsDefined == 1)
{
return 1;
}
/*
* Try to find the atoms; if X does not find the atoms it will try to
* create them.
*/
SPW_InputMotionAtom = XInternAtom(display, SPW_InputMotionAtomName, True);
SPW_InputButtonPressAtom = XInternAtom(display, SPW_InputButtonPressAtomName,
True);
SPW_InputButtonReleaseAtom = XInternAtom(display,
SPW_InputButtonReleaseAtomName,True);
SPW_InputPassThruAtom = XInternAtom(display,
SPW_InputPassThruAtomName, True);
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputMotionAtom %d\n", SPW_InputMotionAtom);
fprintf(stderr,"SPW_InputButtonPressAtom %d\n", SPW_InputButtonPressAtom);
fprintf(stderr,"SPW_InputButtonReleaseAtom %d\n",SPW_InputButtonReleaseAtom);
fprintf(stderr,"SPW_InputPassThruAtom %d\n", SPW_InputPassThruAtom);
#endif
/* if any one of the atoms does not exist return a failure */
if ((SPW_InputMotionAtom == None) ||
(SPW_InputButtonPressAtom == None) ||
(SPW_InputButtonReleaseAtom == None) ||
(SPW_InputPassThruAtom == None))
{
return 0;
}
/* set the global that tells us the atoms are defined and found */
SpaceWareAtomsDefined = 1;
return 1;
} /* end of FindXCMAtoms */
/*-----------------------------------------------------------------------------
*
* static int FindXCM (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* Returns 1 if the XCM driver is found, 0 if not.
*
* Description:
* Find the XCM driver (window and atoms). This function is used
* to check if the driver is running because if the atoms exist
* then the driver is running.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
static int FindXCM (Display *display)
{
unsigned long win; /* the XCM window */
int atom; /* boolean that tell us if we found the atoms */
win = FindXCMWindow (display);
atom = FindXCMAtoms (display);
#ifdef SPW_DEBUG
fprintf(stderr, "win %d atom %d\n", win, atom);
#endif
if ((win != 0) && (atom == 1)) {
return 1;
}
return 0;
} /* end of FindXCM */
/*-----------------------------------------------------------------------------
*
* int SPW_FindXIE (Display *display)
*
* Args:
* display (r/o) The display on which to search for the XCM window
*
* Return Value:
* 1 if the XIE driver was found, 0 if not.
*
* Description:
* Find the XIE Spaceball extension.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
int SPW_FindXIE (Display *display)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
int ieMajor;
int ieFirstEvent;
int ieFirstError;
int nDev;
XDeviceInfo *pDev;
int i,j;
char *ptr;
/* if we have already found the XIE driver don't look again */
if (SpaceballInputExtension == 1)
{
return 1;
}
/* See if XIE is configured */
if (XQueryExtension (display, "XInputExtension", &ieMajor,
&ieFirstEvent, &ieFirstError) == 0)
{
return 0;
}
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_FindXIE() Extension found\n");
#endif
/* get a list of available XIE devices */
pDev = XListInputDevices (display, &nDev);
/* Make sure there are valid devices */
if (pDev == 0) {
return 0;
}
/* Try to find the Spaceball device in the list */
for (i = 0; i < nDev; i++) {
#ifdef SPW_DEBUG
fprintf (stderr, "SPW_FindXIE() device %s\n", pDev[i].name);
#endif
/* Compare against type name per device */
if (strcmp(pDev[i].name, XI_SPACEBALL) == 0) break;
}
/*
* if we have gone past the end of the list then no Spaceball XIE
* driver was found
*/
if (i == nDev) {
XFreeDeviceList (pDev);
return 0;
}
/* record all necessary info on the Spaceball XIE driver */
SpaceballDevID = pDev[i].id;
pSpaceballDev = XOpenDevice (display, SpaceballDevID);
/* clean up the device list */
XFreeDeviceList (pDev);
/* enable the Spaceball callbacks */
DeviceMotionNotify (pSpaceballDev, SPW_DevMotionEventType,
SPW_SpaceballEventClass[0]);
DeviceButtonPress (pSpaceballDev, SPW_DevButtonPressEventType,
SPW_SpaceballEventClass[1]);
DeviceButtonRelease (pSpaceballDev, SPW_DevButtonReleaseEventType,
SPW_SpaceballEventClass[2]);
SpaceballInputExtension = 1;
return 1;
#else
return 0;
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
} /* end of SPW_FindXIE */
/*-----------------------------------------------------------------------------
*
* static void InitializeSpaceball (Display *display, char *version)
*
* Args:
* display (r/o) The display on which to search for the XCM window
* version (r/o) the XCM driver version.
*
* Return Value:
* None.
*
* Description:
* Find the Spaceball via XIE or XCM.
*
* Notes:
* The returnWindow parameter is not set if the Spaceball is configured
* using XIE.
*
*---------------------------------------------------------------------------*/
static void InitializeSpaceball (Display *display, char *version)
{
/* if we already found the Spaceball don't search again */
if (SpaceballFound == 1) {
return;
}
/* Create some more Atoms the Daemon uses */
(void)XInternAtom (display, "WM_PROTOCOLS", False);
(void)XInternAtom (display, "WM_DELETE_WINDOW", False);
/* search for the XIE driver first */
if (SPW_FindXIE (display) == 1) {
SpaceballFound = 1;
#ifdef SPW_DEBUG
fprintf(stderr, "\n");
fprintf(stderr, "Spaceball (R) device found,");
fprintf(stderr, " using SpaceWare (R) XIE interface.\n");
fprintf(stderr, "SpaceWare Version %s\n", version);
fprintf(stderr, "Copyright (c) 1998 Spacetec IMC Corporation\n");
fprintf(stderr, "All Rights Reserved\n");
#endif
}
/* if we don't find the XIE driver search for the XCM driver */
else if (FindXCM (display) == 1) {
SpaceballFound = 1;
#ifdef SPW_DEBUG
fprintf(stderr, "\n");
fprintf(stderr, "Spaceball (R) device found,");
fprintf(stderr, " using SpaceWare (R) XCM interface.\n");
fprintf(stderr, "SpaceWare Version %s\n", version);
fprintf(stderr, "Copyright (c) 1998 Spacetec IMC Corporation\n");
fprintf(stderr, "All Rights Reserved\n");
#endif
}
} /* end of InitializeSpaceball */
/*-----------------------------------------------------------------------------
*
* int SPW_InputCheckForSpaceball (Display *display, Window window,
* char *product)
*
* Args:
* display (r/o) The display on which to search for the XCM window
* window (r/o) the window on the application side to send messages to.
* product (r/o) name of the application that is opening the Spaceball.
*
* Return Value:
* 1 if the Spaceball was opened, 0 if not.
*
* Description:
* Find the Spaceball via XIE or XCM.
*
* Notes:
* The window parameter is not set if the Spaceball is configured
* using XIE.
*
*---------------------------------------------------------------------------*/
int SPW_InputCheckForSpaceball (Display *display,
Window window,
char *product)
{
char version[256]; /* The version of SpaceWare */
int productlen; /* length of the product string */
#ifdef SPW_DEBUG
fprintf(stderr, "SPW_InputCheckForSpaceball window 0x%x product %s\n",
window, product);
#endif
/* check to make sure the corect parameters were passed */
if ((display == NULL) || (window == 0)) {
return 0;
}
/* build the SpaceWare version and product info string */
strcpy (version, SPW_INPUT_VERSION);
if (product != NULL) {
productlen = strlen (product);
if ((productlen > 0) && (productlen < 200)) {
strcat(version, ".");
strcat(version, product);
}
}
/* save off the application window for future reference */
if (ReturnWindowID == 0) {
ReturnWindowID = window;
}
/* try to open the Spaceball */
InitializeSpaceball (display, version);
if (SpaceballFound == 0) {
return 0;
}
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
/* If we're using XIE, try to enable Spaceball events */
if (SPW_FindXIE (display) == 1) {
XSelectExtensionEvent (display, window, SPW_SpaceballEventClass, 3);
}
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/* Send version info to the daemon */
SPW_SendHandshake (display);
/* we found the Spaceball so return a success message */
return 1;
} /* end of SPW_InputCheckForSpaceball */
/*-----------------------------------------------------------------------------
*
* int SPW_InputIsSpaceballEvent (Display *display, XEvent *event,
* SPW_InputEvent *spwev)
*
* Args:
* display (r/o) The display the Spaceball is attached to
* event (r/o) The event that we want to check
* spwev (w/o) The spaceball return structure contaning the spaceball data
*
* Return Value:
* True if the event is a spaceball event, False if not.
*
* Description:
* Determine if the event is a spaceball event and pack it into
* the spwev_ret structure.
*
* Notes:
* This function handles XCM as well as XIE events.
*
*---------------------------------------------------------------------------*/
int SPW_InputIsSpaceballEvent (Display *display, XEvent *event,
SPW_InputEvent *spwev)
{
static int shake_count = 0; /* how many events have passed without */
/* the daemon sending us a handshake */
static int last_shake = 0; /* the last time we sent a handshake */
static int shake_delay = 7; /* number of events we wait before */
/* sending another handshake message */
int isSpaceball=0;
static int have_handshake = 0; /* did we get a handshake
from daemon? */
static int have_tune = 0; /* do we have the tune data yet */
static float sbtune[6]; /* the tune multipliers */
char *scaleVar; /* environment variable containg
tune data */
int i; /* counter */
/* if the event, display or spwev variables are not allocated then return */
if ((display == NULL) || (event == NULL) || (spwev == NULL)) {
return 0;
}
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputIsSpaceballEvent type %d\n", event->type);
#endif
/* the event is a spaceball event so deal with it */
if (event->type == ClientMessage) {
XClientMessageEvent *clientMessage = (XClientMessageEvent *) event;
#ifdef SPW_DEBUG
fprintf(stderr," message_type %d\n", clientMessage->message_type);
#endif
/* the event is a motion event */
if (clientMessage->message_type == SPW_InputMotionAtom) {
/* specify we found a spaceball event */
isSpaceball = 1;
/* specify the spaceball event is a motion event */
spwev->type = SPW_InputMotionEvent;
/* copy the spaceball data out of the event into the spwev structure */
for (i = 0; i < 7; i++) {
spwev->sData[i] = clientMessage->data.s[i + 2];
spwev->fData[i] = (float) ((int) clientMessage->data.s[i + 2]);
}
}
/* if the event is a button press event process it */
else if (clientMessage->message_type == SPW_InputButtonPressAtom) {
/* specify we found a spaceball event */
isSpaceball = 1;
/* specify the spaceball event is a button event */
spwev->type = SPW_InputButtonPressEvent;
/* copy the spaceball data out of the event into the spwev structure */
spwev->buttonNumber = clientMessage->data.s[2];
}
/* if the event is a button release event process it */
else if (clientMessage->message_type == SPW_InputButtonReleaseAtom) {
/* specify we found a spaceball event */
isSpaceball = 1;
/* specify the spaceball event is a button event */
spwev->type = SPW_InputButtonReleaseEvent;
/* copy the spaceball data out of the event into the spwev structure */
spwev->buttonNumber = clientMessage->data.s[2];
}
/* the event is a pass through event */
else if (clientMessage->message_type == SPW_InputPassThruAtom) {
char *str = clientMessage->data.b; /* data in the passthrough event */
/* check if the pass through event is handshaking from the daemon */
if ((str[0] == '~') && (str[1] == '~')) {
have_handshake = 1;
}
}
/* if we did get a spaceball event save the window ID we got it from */
if (isSpaceball == 1) {
/* if we don't know what the XCM window is yet save it off */
if (SpaceWareXCMWindowID == 0) {
SpaceWareXCMWindowID = ntohl(clientMessage->data.l[0]);
}
}
} /* if (event->type == ClientMessage */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
/* check to see if the event is a spaceball XIE motion event */
else if ((event->type == SPW_DevMotionEventType) &&
(((XDeviceMotionEvent *)event)->deviceid == SpaceballDevID)) {
/* cast the event to a XIE event */
XDeviceMotionEvent *motion = (XDeviceMotionEvent *)event;
int i; /* Counter loop index */
static float sbData[7]; /* the spaceball data */
/*
* There is not enough room in an SGI X Input Extension event for all
* 7 motion values (6 DOF data + period) the Spaceball produces.
* Therefore, when we implement the Spaceball XIE we say it has
* 7 axes and we send them in two separate XIE events. The
* period, axis 6, is sent alone in the first event; the 6 dof data
* are sent in the next event. First_axis and axes_count are set
* correctly so the client code can figure out when it has a
* complete event.
*
* This function returns 1 to indicate it was a Spaceball event.
* In other words, it was not some other type of event the application
* should handle. BUT the spwev.type is set to 0 if it is not a
* complete Spaceball motion event. This should cause the switch
* statement that is suppose to follow a call to this function to
* ignore the event.
*/
/* specify we found a spaceball event */
isSpaceball = 1;
/* specify the event is a motion event */
spwev->type = SPW_InputMotionEvent;
/* extract the spaceball motion data from the event */
for (i = 0; i < (int) (motion->axes_count); i++) {
sbData[motion->first_axis+i] = motion->axis_data[i];
}
/* the first axis will always be one, so this will work */
if (motion->first_axis != 6) {
for (i = 0; i < 7; i++) {
spwev->fData[i] = sbData[i];
spwev->sData[i] = (short)((int)sbData[i]);
}
}
else {
/* Only the period has been received, ignore event */
spwev->type = 0;
}
}
/* check to see if the event is a spaceball XIE button press event */
else if ((event->type == SPW_DevButtonPressEventType) &&
(((XDeviceButtonEvent *)event)->deviceid == SpaceballDevID)) {
/* cast the event to a XIE event */
XDeviceButtonEvent *button = (XDeviceButtonEvent *) event;
/* specify we found a spaceball message */
isSpaceball = 1;
/* specify the message was a button press event */
spwev->type = SPW_InputButtonPressEvent;
/* copy the button data out of the event */
spwev->buttonNumber = button->button;
}
/* check to see if the event is a spaceball XIE button release event */
else if ((event->type==SPW_DevButtonReleaseEventType) &&
(((XDeviceButtonEvent *)event)->deviceid == SpaceballDevID)) {
/* cast the event to a XIE event */
XDeviceButtonEvent *button = (XDeviceButtonEvent *) event;
/* specify we found a spaceball message */
isSpaceball = 1;
/* specify the message was a button press event */
spwev->type = SPW_InputButtonReleaseEvent;
/* specify the message was a button release event */
spwev->buttonNumber = button->button;
}
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/*
* if after all that we haven't determined the event to be a spaceball
* event return a failure
*/
if (isSpaceball == 0) {
return 0;
}
/*
* If we haven't gotten a handshake packet from the daemon
* and it's still early in the process try again, otherwise
* say the daemon must be earlier than 7.0. Basically we'll
* have sent out SPW_MaxEventCount/shake_delay handshake messages to the
* daemon without a response.
*/
if ((have_handshake == 0) && (shake_count < SPW_MaxEventCount)) {
if ((last_shake + shake_delay) == shake_count) {
SPW_SendHandshake (display);
last_shake = shake_count;
}
shake_count++;
}
/*
* Tuning: we want to have the option to tune an application without
* having to recompile it each time. We will do this by setting the
* SBALL_TUNING variable and scaling the data by it. This should only
* be used to get the constants to acutally put into the application.
*/
if (spwev->type == SPW_InputMotionEvent) {
if (have_tune == 0) {
scaleVar = getenv("SBALL_TUNING");
if (scaleVar != NULL) {
sscanf(scaleVar, "%f %f %f %f %f %f", &sbtune[0],
&sbtune[1],
&sbtune[2],
&sbtune[3],
&sbtune[4],
&sbtune[5]);
}
else {
sbtune[0] = 1.0f;
sbtune[1] = 1.0f;
sbtune[2] = 1.0f;
sbtune[3] = 1.0f;
sbtune[4] = 1.0f;
sbtune[5] = 1.0f;
}
have_tune = 1;
}
spwev->fData[0] *= sbtune[0];
spwev->fData[1] *= sbtune[1];
spwev->fData[2] *= sbtune[2];
spwev->fData[3] *= sbtune[3];
spwev->fData[4] *= sbtune[4];
spwev->fData[5] *= sbtune[5];
spwev->sData[0] = (int)(spwev->sData[0] * sbtune[0]);
spwev->sData[1] = (int)(spwev->sData[1] * sbtune[1]);
spwev->sData[2] = (int)(spwev->sData[2] * sbtune[2]);
spwev->sData[3] = (int)(spwev->sData[3] * sbtune[3]);
spwev->sData[4] = (int)(spwev->sData[4] * sbtune[4]);
spwev->sData[5] = (int)(spwev->sData[5] * sbtune[5]);
}
return 1;
} /* end of SPW_InputIsSpaceballEvent */
/*-----------------------------------------------------------------------------
*
* void SPW_InputBeep (Display *display, char *string)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* string (r/o) the Spaceball beep string
*
* Return Value:
* None.
*
* Description:
* This function send a string to the XCM driver that will cause
* the spaceball to beep. Each lower case character (a-z) results
* in a tone, and each upper case character (A-Z) results in a pause
* between tones.
* Example: cCcC - cause the ball to beep twice.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputBeep (Display *display, char *string)
{
char sndStr[15]; /* the control string to be sent to the driver */
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
/* if we are connected via XIE use that to beep the ball */
if (SpaceballInputExtension == 1) {
#ifdef HAVE_X11_EXTENSIONS_SGIMISC_H
/* SGI beep */
int slen = strlen (string);
static char buf[32];
char *cp = buf;
if (slen > 29) slen = 29;
*cp++ = '\r';
*cp++ = 'B';
strcpy (cp, string);
cp += slen;
*cp++ = '\r';
*cp = NULL;
XSGIDeviceControl (display, (int)SpaceballDevID, "sbprivate", buf);
XFlush (display);
#elif HAVE_X11_XPROTO_H
/* HP beep */
int i = 0;
int slen = strlen (string);
XBellFeedbackControl cntrl;
#if defined(__cplusplus) || defined(c_plusplus)
cntrl.c_class = BellFeedbackClass;
#else /* ! __cplusplus */
cntrl.class = BellFeedbackClass;
#endif /* __cplusplus */
cntrl.pitch = 'B';
cntrl.percent = 0;
while (i < slen) {
cntrl.duration = (short) string[i++];
XChangeFeedbackControl(display, pSpaceballDev,
DvPercent | DvPitch | DvDuration,
(XFeedbackControl *)(void *)&cntrl);
}
#endif /* HAVE_XCHANGEFEEDBACKCONTROL */
}
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/* if we are not using XIE use XCM to communicate with the ball */
if (SpaceballInputExtension == 0) {
/* setup the string to be sent */
SPW_strLength = 0;
sndStr[0] = '\0';
strcat (sndStr, "B");
strcat (sndStr, string);
strcat (sndStr, "\r");
/* send the string */
SendString (display, sndStr);
}
} /* end of SPW_InputBeep */
/*-----------------------------------------------------------------------------
*
* void SPW_InputRezero (Display *display)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
*
* Return Value:
* None.
*
* Description:
* This function causes the Spaceball to rezero at its current location.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputRezero (Display *display)
{
#ifdef HAVE_X11_EXTENSIONS_XINPUT_H
/* if we are connected via XIE use that to rezero the ball */
if (SpaceballInputExtension == 1)
{
#ifdef HAVE_X11_EXTENSIONS_SGIMISC_H
/* SGI rezero */
static char *cmd = "\rZ\r";
XSGIDeviceControl (display, (int)SpaceballDevID, "sbprivate", cmd);
XFlush (display);
#elif HAVE_X11_XPROTO_H
XBellFeedbackControl cntrl;
#if defined(__cplusplus) || defined(c_plusplus)
cntrl.c_class = BellFeedbackClass;
#else /* ! __cplusplus */
cntrl.class = BellFeedbackClass;
#endif /* __cplusplus */
cntrl.pitch = 'Z';
cntrl.percent = 0;
cntrl.duration = 0;
XChangeFeedbackControl (display, pSpaceballDev,
DvPercent | DvPitch | DvDuration,
(XFeedbackControl *)(void *)&cntrl);
#endif /* HAVE_X11_XPROTO_H */
}
#endif /* HAVE_X11_EXTENSIONS_XINPUT_H */
/* if we are not using XIE use XCM to communicate with the ball */
if (SpaceballInputExtension == 0) {
/* setup the rezero string and send it */
SPW_strLength = 0;
static char Z[] = "Z\r";
SendString (display, Z);
}
} /* end of SPW_InputRezero */
/*-----------------------------------------------------------------------------
*
* void SPW_InputSetPulseRate (Display *display, unsigned short rate)
*
* Args:
* display (r/o) The X Display of which the XCM window is a resident
* rate (r/o) The rate at which the spaceball data is sent
*
* Return Value:
* None.
*
* Description:
* This function sets the Spaceball transmission rate. Currently
* it is just stubed out since we don't want the transmission rate
* of the Spaceball to change.
*
* Notes:
* None.
*
*---------------------------------------------------------------------------*/
void SPW_InputSetPulseRate (Display *display, unsigned short rate)
{
}
/*-----------------------------------------------------------------------------
*
* int SPW_InputXPending (Display *display, int delay)
*
* Args:
* display (r/o) The X Display on which the queue is resident
* delay (r/o) amount of time to wait for the next event in microseconds
*
* Return Value:
* Returns the number of event on the X queue.
*
* Description:
* Wait for another Spaceball event to come in within the time delay.
*
* Notes:
* If delay is less than or equal to zero this just checks the queue
* without waiting.
*
*---------------------------------------------------------------------------*/
int SPW_InputXPending (Display *display, int delay)
{
int numevents; /* the number of event on the X queue */
struct timeval short_wait; /* time struct needed for select call */
fd_set Xfds;
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputXPending\n");
#endif
/* get the number of event waiting on the event queue */
numevents = XPending (display);
#ifdef SPW_DEBUG
fprintf(stderr," numevents %d delay %d\n", numevents, delay);
#endif
/*
* if there are already event waiting or we have exceded the delay
* return the number of events on the queue
*/
if ((numevents > 0) || (delay <= 0)) {
return numevents;
}
/* use select to wait for the designated delay period */
short_wait.tv_sec = 0;
short_wait.tv_usec = (long) delay;
FD_ZERO (&Xfds);
FD_SET (ConnectionNumber (display), &Xfds);
(void)select (ConnectionNumber (display) + 1, &Xfds, NULL,
NULL, &short_wait);
/* check the event queue again */
numevents = XPending (display);
#ifdef SPW_DEBUG
fprintf(stderr," numevents %d\n", numevents);
#endif
/* return the number of event on the queue */
return numevents;
} /* end of SPW_InputXPending*/
/*-----------------------------------------------------------------------------
*
* void SPW_InputInitDispatchStruct (SPW_InputDispatchStruct *ds)
*
* Args:
* ds (w/o) - input dispatch structure
*
* Return Value:
* None
*
* Description:
* Zeroizes a new dispatch structure and sets the default delay.
*
* Notes:
*
*----------------------------------------------------------------------------*/
void SPW_InputInitDispatchStruct (SPW_InputDispatchStruct * ds)
{
memset(ds, (unsigned char)0, sizeof(SPW_InputDispatchStruct));
ds->delay = SPW_InputDefaultDelay;
}
/*-----------------------------------------------------------------------------
*
* int SPW_InputDispatch (SPW_InputDispatchStruct *ds)
*
* Args:
* ds (r/o) - input dispatch structure
*
* Return Value:
* 1 if a redraw is required, 0 if a redraw is not required.
*
* Description:
* Determines what type of event has been returned by the Spaceball and calls
* the appropriate dispatch routine.
* SPW_InputMotionEventHandler - called if a motion event occured
* SPW_InputButtonEventHandler - called if a button event occured
* SPW_InputOtherEventHandler - called if a non-spaceball event occured
*
* Notes:
* if SPW_InputOtherEventHandler return a non-zero SPW_InputDispatch
* will dispose of the event and continue processing events.
*
*----------------------------------------------------------------------------*/
int SPW_InputDispatch (SPW_InputDispatchStruct *ds)
{
float data[7]; /* place to store Spaceball event data */
int i; /* loop counter */
int motion_events; /* flag tells if we have motion event*/
int event_pending; /* flag tells if ther's an event pending */
int all_zero_event; /* flag set if there's an all zero event */
int redraw; /* redraw flag */
int continue_loop; /* flag to keep while loop running */
/* initialize variable */
redraw = 0;
motion_events=0;
event_pending=1;
all_zero_event=0;
redraw = 0;
continue_loop=1;
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputDispatch\n"); /* show where we are */
#endif
/* if dispatch struct is empty, return to caller */
if (ds == NULL)
{
return 0;
}
/* initialize local event data structure */
for (i = 0; i < 7; i++)
{
data[i] = 0.0f;
}
/* compress event data while it keeps on coming */
while (continue_loop == 1)
{
#ifdef SPW_DEBUG
fprintf(stderr," spwevent type %d\n", ds->spwevent->type);
#endif
/* see what type of event we have */
switch(ds->spwevent->type)
{
case SPW_InputMotionEvent:
motion_events++;
/* Check for a no motion event */
if ((ds->spwevent->fData[0] == 0.0) &&
(ds->spwevent->fData[1] == 0.0) &&
(ds->spwevent->fData[2] == 0.0) &&
(ds->spwevent->fData[3] == 0.0) &&
(ds->spwevent->fData[4] == 0.0) &&
(ds->spwevent->fData[5] == 0.0))
{
all_zero_event++; /* keep track of how many we have */
}
/* add data into local structure */
for (i = 0; i < 7; i++)
{
data[i] += ds->spwevent->fData[i];
}
#ifdef SPW_DEBUG
fprintf(stderr," %f %f %f %f %f %f %f\n", data[0],data[1],data[2],
data[3],data[4],data[5],data[6]);
#endif
break;
case SPW_InputButtonPressEvent: /* button press event */
/* if button press event handler function pointer is present */
if (ds->handle_bpress != 0)
{
(*(ds->handle_bpress)) (ds->display, ds->spwevent->buttonNumber,
ds->pButtonAppData);
}
/* check for rezero button */
if (ds->spwevent->buttonNumber == 9)
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* zero data structure */
}
motion_events = 0; /* reset number of motion events */
}
break;
case SPW_InputButtonReleaseEvent: /* button release event */
if (ds->handle_brelease != 0)
{
(*(ds->handle_brelease)) (ds->display,ds->spwevent->buttonNumber,
ds->pButtonAppData);
}
/* check for rezero button */
if (ds->spwevent->buttonNumber == 9)
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* clear local data */
}
motion_events = 0; /* reset number of motion events */
redraw = 1;
}
break;
default:
break;
} /* end of case statement */
/* check if input is pending */
if (SPW_InputXPending (ds->display, ds->delay))
{
XNextEvent (ds->display, ds->xevent); /* get an event */
/* if it's not a Spaceball event see if we should continue */
if (SPW_InputIsSpaceballEvent (ds->display, ds->xevent,
ds->spwevent) == 0)
{
/* if we have an event handler...*/
if (ds->handle_other != 0)
{
/* handle event and get loop status */
continue_loop = (ds->handle_other)
(ds->display, ds->xevent, ds->pOtherAppData);
}
else
{
continue_loop = 0; /* no event handler -> bail */
}
}
}
else /* no more events to compress */
{
event_pending = 0;
continue_loop = 0;
}
} /* end of while loop */
#ifdef SPW_DEBUG
fprintf(stderr," event_pending %d motion_events %d all_zero_event %d\n",
event_pending, motion_events, all_zero_event);
#endif
/* push unwanted event back onto the stack */
if (event_pending == 1)
{
XPutBackEvent(ds->display, ds->xevent);
}
/* average event data by the period of the Spaceball data */
if (data[6] != 0.0)
{
for (i = 0; i < 6; i++)
{
data[i] /= data[6];
}
}
#ifdef SPW_DEBUG
fprintf(stderr,"ave %f %f %f %f %f %f\n\n", data[0], data[1], data[2],
data[3], data[4], data[5]);
#endif
/* if we have a motion event handler... */
if (ds->handle_motion != 0)
{
/* if there are motion events, handle them and force a redraw */
if (motion_events != 0)
{
(*(ds->handle_motion)) (ds->display, data, ds->pMotionAppData);
redraw = 1;
}
if ((all_zero_event != 0) && (motion_events > 1))
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* reset data */
}
/* handle motion and force a redraw */
(ds->handle_motion) (ds->display, data, ds->pMotionAppData);
redraw = 1;
}
}
return redraw;
} /* end of SPW_InputDispatch */
/*-----------------------------------------------------------------------------
*
* int SPW_InputDispatchEx (SPW_InputDispatchStruct *ds)
*
* Args:
* ds (r/o) - input dispatch structure
*
* Return Value:
* 1 if a redraw is required, 0 if a redraw is not required.
*
* Description:
* Determines what type of event has been returned by the Spaceball and calls
* the appropriate dispatch routine.
* SPW_InputMotionEventHandler - called if a motion event occured
* SPW_InputButtonEventHandler - called if a button event occured
* SPW_InputOtherEventHandler - called if a non-spaceball event occured
*
* Notes:
* An optimized and more cooperative version of SPW_InputDispatch().
* if SPW_InputOtherEventHandler returns non-zero, SPW_InputDispatchEx
* will dispose of the event and continue compressing motion events.
* If zero is returned, this function will dump all accumalated motion
* events (unlike its predecessor) and quickly return control back to
* the app. Also, the standard Xlib call XPending() is used instead of
* SPW_InputXPending().
*
*----------------------------------------------------------------------------*/
int SPW_InputDispatchEx (SPW_InputDispatchStruct *ds)
{
float data[7]; /* place to store Spaceball event data */
int i; /* loop counter */
int motion_events; /* flag tells if we have motion event*/
int all_zero_event; /* flag set if there's an all zero event */
int redraw; /* redraw flag */
int continue_loop; /* flag to keep while loop running */
/* initialize variable */
redraw = 0;
motion_events=0;
all_zero_event=0;
redraw = 0;
continue_loop=1;
#ifdef SPW_DEBUG
fprintf(stderr,"SPW_InputDispatch\n"); /* show where we are */
#endif
/* if dispatch struct is empty, return to caller */
if (ds == NULL)
{
return 0;
}
/* initialize local event data structure */
for (i = 0; i < 7; i++)
{
data[i] = 0.0f;
}
/* compress event data while it keeps on coming */
while (continue_loop == 1)
{
#ifdef SPW_DEBUG
fprintf(stderr," spwevent type %d\n", ds->spwevent->type);
#endif
/* make sure we have ball data */
SPW_InputIsSpaceballEvent (ds->display, ds->xevent, ds->spwevent);
/* see what type of event we have */
switch(ds->spwevent->type)
{
case SPW_InputMotionEvent:
motion_events++;
/* Check for a no motion event */
if ((ds->spwevent->fData[0] == 0.0) &&
(ds->spwevent->fData[1] == 0.0) &&
(ds->spwevent->fData[2] == 0.0) &&
(ds->spwevent->fData[3] == 0.0) &&
(ds->spwevent->fData[4] == 0.0) &&
(ds->spwevent->fData[5] == 0.0))
{
all_zero_event++; /* keep track of how many we have */
}
/* add data into local structure */
for (i = 0; i < 7; i++)
{
data[i] += ds->spwevent->fData[i];
}
#ifdef SPW_DEBUG
fprintf(stderr," %f %f %f %f %f %f %f\n",
data[0],data[1],data[2],
data[3],data[4],data[5],data[6]);
#endif
break;
case SPW_InputButtonPressEvent: /* button press event */
/* if button press event handler function pointer is present */
if (ds->handle_bpress != 0)
{
(*(ds->handle_bpress)) (ds->display, ds->spwevent->buttonNumber,
ds->pButtonAppData);
}
/* check for rezero button */
if (ds->spwevent->buttonNumber == 9)
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* zero data structure */
}
motion_events = 0; /* reset number of motion events */
}
break;
case SPW_InputButtonReleaseEvent: /* button release event */
if (ds->handle_brelease != 0)
{
(*(ds->handle_brelease)) (ds->display,ds->spwevent->buttonNumber,
ds->pButtonAppData);
}
/* check for rezero button */
if (ds->spwevent->buttonNumber == 9)
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* clear local data */
}
motion_events = 0; /* reset number of motion events */
redraw = 1;
}
break;
default:
break;
} /* end of case statement */
/* check if input is pending */
if (XPending (ds->display) > 0)
{
XPeekEvent (ds->display, ds->xevent); /* look at an event */
if (SPW_InputIsSpaceballEvent (ds->display, ds->xevent,
ds->spwevent) == 1)
{
XEvent throw_away;
XNextEvent (ds->display, &throw_away); /* remove the event */
}
else /* if it's not a Spaceball event see if we should continue */
{
/* if we have an event handler...*/
if (ds->handle_other != NULL)
{
/* handle event and get loop status */
continue_loop = (ds->handle_other)
(ds->display, ds->xevent, ds->pOtherAppData);
if (continue_loop == 1)
{
XNextEvent (ds->display, ds->xevent); /* get next event */
}
else
{
motion_events = 0; /* app needs control back, fast! */
}
}
else
{
continue_loop = 0; /* no event handler -> bail */
}
}
}
else /* no more events to compress */
{
continue_loop = 0;
}
} /* end of while loop */
#if defined(SPW_DEBUG) && 0
fprintf(stderr," event_pending %d motion_events %d all_zero_event %d\n",
event_pending, motion_events, all_zero_event);
#endif
/* average event data by the period of the Spaceball data */
if (data[6] != 0.0)
{
for (i = 0; i < 6; i++)
{
data[i] /= data[6];
}
}
#ifdef SPW_DEBUG
fprintf(stderr,"ave %f %f %f %f %f %f\n\n", data[0], data[1],
data[2],
data[3], data[4], data[5]);
#endif
/* if we have a motion event handler... */
if (ds->handle_motion != 0)
{
/* if there are motion events, handle them and force a redraw */
if (motion_events != 0)
{
(*(ds->handle_motion)) (ds->display, data, ds->pMotionAppData);
redraw = 1;
}
if ((all_zero_event != 0) && (motion_events > 1))
{
for (i = 0; i < 6; i++)
{
data[i] = 0.0f; /* reset data */
}
/* handle motion and force a redraw */
(ds->handle_motion) (ds->display, data, ds->pMotionAppData);
redraw = 1;
}
}
return redraw;
} /* end of SPW_InputDispatchEx */
#endif /* HAVE_X11_AVAILABLE */
|