home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume14 / ultrix.patch / part01 / server / ddx / dec / ws / ext_device.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-26  |  12.3 KB  |  510 lines

  1. /***********************************************************
  2. Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*
  26.  * This file holds the hook routines for adding extension input devices.
  27.  *
  28.  *    Jeffrey Hsu, Digital Equipment Corporation,
  29.  *
  30.  */
  31.  
  32. /* $Header: ext_device.c,v 1.1 91/05/11 10:51:05 rws Exp $ */
  33.  
  34. #ifdef XINPUT
  35.  
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <sys/file.h>
  39. #include <sys/time.h>
  40. #include <sys/tty.h>
  41. #include <sys/devio.h>
  42.  
  43. #include "misc.h"
  44. #include "X.h"
  45. #define NEED_EVENTS
  46. #include "Xproto.h"
  47. #include "scrnintstr.h"
  48. #include "pixmap.h"
  49. #include "input.h"
  50. #include "cursorstr.h"
  51. #include "regionstr.h"
  52. #include "resource.h"
  53. #include "dixstruct.h"
  54. #include <sys/workstation.h>
  55. #include <sys/inputdevice.h>
  56. #include <sys/wsdevice.h>
  57. #include "ws.h"
  58.  
  59. #include <sys/tablet.h>
  60. #include <sys/pcm.h>
  61. #include <sys/termio.h>
  62.  
  63. #include "XI.h"            /* for Absolute and Relative #define */
  64. #include "XIproto.h"        /* for deviceKeyButtonPointer */
  65.  
  66. /* XXX copied from ws_io.c, get it into some header file somewhere */
  67. #define MOTION_BUFFER_SIZE 100
  68. extern int wsGetMotionEvents();
  69.  
  70. extern Bool commandLinePairMatch();    /* secret function */
  71.  
  72. extern int DeviceButtonPress, DeviceButtonRelease, DeviceButtonRelease,
  73.     DeviceMotionNotify, ProximityIn, ProximityOut, DeviceValuator;
  74.  
  75. static DevicePtr        wsTablet, wsButtonBox, wsDialBox;
  76.  
  77. static void wsButtonBoxControl(), wsDialBoxControl();
  78. static int wsButtonBoxProc(), wsDialBoxProc(), wsTabletProc();
  79.  
  80. int tbFd;    /* tablet file descriptor */
  81. int pcmFd;
  82.  
  83. /* return TRUE if recognized device type */
  84. Bool
  85. ExtProcessInputEvents(x, e)
  86. xEvent *x;
  87. ws_event *e;
  88. {
  89.  
  90.     /*
  91.      * X Input Extension events can have more than one event structure per
  92.      * device event
  93.      */
  94.     xEvent eventarray[3];    /* make this 3 until some device needs more */
  95.     deviceValuator *vev;
  96.     deviceValuator valuatorevent;
  97.     int eventcount;
  98.  
  99.     deviceKeyButtonPointer *inputEv = (deviceKeyButtonPointer *) x;
  100.     tablet_event *tabev = (tablet_event *) e;
  101.     dial_event *dialev = (dial_event *) e;
  102.  
  103.     switch (e->device_type) {
  104.       case STABLET_DEVICE:
  105.         switch (e->type) {
  106.         case BUTTON_DOWN_TYPE: 
  107.             x->u.u.type = DeviceButtonPress;
  108.             break;
  109.         case BUTTON_UP_TYPE: 
  110.             x->u.u.type = DeviceButtonRelease;
  111.             break;
  112.         case MOTION_TYPE: 
  113.             x->u.u.type = DeviceMotionNotify;
  114.             break;
  115.         case PROXIMITY_IN:
  116.             x->u.u.type = ProximityIn;
  117.             break;
  118.         case PROXIMITY_OUT:
  119.             x->u.u.type = ProximityOut;
  120.             break;
  121.         default: 
  122.             printf("Unknown tablet event = %d\n", e->type);
  123.                 return(TRUE);
  124.         }
  125.         vev = (deviceValuator *) &eventarray[1];
  126.         vev->type = DeviceValuator;
  127.         vev->num_valuators = 2;
  128.         vev->first_valuator = 0;
  129.         vev->valuator0 = tabev->axis_data[0];
  130.         vev->valuator1 = tabev->axis_data[1];
  131.         vev->device_state = e->e.pointer.buttons;
  132.         vev->deviceid = e->device;
  133.         eventcount = 2;
  134.         inputEv->deviceid = e->device | MORE_EVENTS;
  135.         eventarray[0] = *x;
  136.         (*wsTablet->processInputProc) (eventarray, wsTablet, eventcount);
  137.         break;
  138.       case BUTTONBOX_DEVICE:
  139.         switch (e->type) {
  140.         case BUTTON_DOWN_TYPE: 
  141.             x->u.u.type = DeviceButtonPress;
  142.             break;
  143.         case BUTTON_UP_TYPE: 
  144.             x->u.u.type = DeviceButtonRelease;
  145.             break;
  146.         default: 
  147.             printf("Unknown button box event = %d\n", e->type);
  148.                 return(TRUE);
  149.         }
  150.         (*wsButtonBox->processInputProc) (x, wsButtonBox, 1);
  151.         break;
  152.       case KNOBBOX_DEVICE:
  153.         switch (e->type) {
  154.         case MOTION_TYPE:
  155.             x->u.u.type = DeviceMotionNotify;
  156.             break;
  157.         default: 
  158.             printf("Unknown dial box event = %d\n", e->type);
  159.                 return(TRUE);
  160.         }
  161.  
  162.         vev = (deviceValuator *) &eventarray[1];
  163.         vev->type = DeviceValuator;
  164.         vev->num_valuators = 6;
  165.         vev->first_valuator = 0;
  166.         vev->valuator0 = dialev->axis_data[0];
  167.         vev->valuator1 = dialev->axis_data[1];
  168.         vev->valuator2 = dialev->axis_data[2];
  169.         vev->valuator3 = dialev->axis_data[3];
  170.         vev->valuator4 = dialev->axis_data[4];
  171.         vev->valuator5 = dialev->axis_data[5];
  172.         vev->device_state = e->e.pointer.buttons;
  173.         vev->deviceid = e->device;
  174.  
  175.         vev = (deviceValuator *) &eventarray[2];
  176.         vev->type = DeviceValuator;
  177.         vev->num_valuators = 2;
  178.         vev->first_valuator = 6;
  179.         vev->valuator0 = dialev->axis_data[6];
  180.         vev->valuator1 = dialev->axis_data[7];
  181.         vev->device_state = e->e.pointer.buttons;
  182.         vev->deviceid = e->device;
  183.  
  184.         eventcount = 3;
  185.         inputEv->deviceid = e->device;
  186.         eventarray[0] = *x;
  187.         (*wsDialBox->processInputProc) (eventarray, wsDialBox, eventcount);
  188.         break;
  189.       default:
  190.         return FALSE;
  191.     }
  192.     return TRUE;
  193. }
  194.  
  195. int
  196. ExtddxProcessArgument (argc, argv, i)
  197. register int argc;
  198. register char *argv[];
  199. register int i;
  200. {
  201.  
  202.     int argind=i;
  203.     int skip = 0;
  204.  
  205.     if (strcmp(argv[argind], "-tb") == 0) {
  206.       if (++argind < argc)  {
  207.         /* defer processing to InitInput */
  208.         /* should think up cleaner solution XXX */
  209.         skip = 2;
  210.       }
  211.       else return 0;
  212.     } else if (strcmp(argv[argind], "-pcm") == 0) {
  213.       if (++argind < argc)  {
  214.         /* defer processing to InitInput */
  215.         /* should think up cleaner solution XXX */
  216.         skip = 2;
  217.       }
  218.       else return 0;
  219.     }
  220.  
  221.     return skip;
  222.  
  223. }
  224.  
  225. void
  226. ExtddxUseMsg()
  227. {
  228.     ErrorF("-tb <tty number n>    open /dev/tty0n for tablet\n");
  229.     ErrorF("-pcm <tty number n>    open /dev/tty0n for pcm\n");
  230. }
  231.  
  232. void
  233. ExtInitInput(argc, argv)
  234. {
  235.  
  236.     DevicePtr t;
  237.     char *tabletline, buf[256], *pcmline;
  238.     int linenumber;
  239.     int i;
  240.     struct termio termio;
  241.  
  242.     if (commandLinePairMatch( argc, argv, "-tb", &tabletline)) {
  243.       sscanf(tabletline, "%d", &linenumber);
  244.       sprintf(buf, "/dev/tty0%d", linenumber);
  245.       if ((tbFd = open(buf,  O_RDWR, 0)) < 0) {
  246.         Error("couldn't open device");
  247.         return;
  248.       }
  249.  
  250.       if (ioctl(tbFd, TCGETA, &termio) < 0) {
  251.         Error("TCGETA");
  252.         return;
  253.       }
  254.  
  255.       /* someday I will do this in the line discpline open XXX */
  256.       termio.c_cflag = (B4800 | PARENB | PARODD | CREAD | CS8);
  257.       termio.c_iflag &= ~ISTRIP;
  258.  
  259.       if (ioctl(tbFd, TCSETA, &termio) < 0) {
  260.         Error("TCSETA");
  261.         return;
  262.       }
  263.  
  264.       i = TABLDISC;
  265.       if (ioctl(tbFd, TIOCSETD, &i) < 0) {
  266.         Error("couldn't set line discipline");
  267.         return;
  268.       }
  269.  
  270.       i = 0;
  271.       if (ioctl(tbFd, BIOSTYPE, &i) < 0) {
  272.         Error("couldn't set line discipline");
  273.         return;
  274.       }
  275.  
  276.       t = AddInputDevice(wsTabletProc, TRUE);
  277.  
  278.       RegisterOtherDevice(t);
  279.  
  280.     }
  281.  
  282.     if (commandLinePairMatch( argc, argv, "-pcm", &pcmline)) {
  283.       sscanf(pcmline, "%d", &linenumber);
  284.       sprintf(buf, "/dev/tty0%d", linenumber);
  285.       if ((pcmFd = open(buf,  O_RDWR, 0)) < 0) {
  286.         Error("couldn't open device");
  287.         return;
  288.       }
  289.  
  290.       if (ioctl(pcmFd, TCGETA, &termio) < 0) {
  291.         Error("TCGETA");
  292.         return;
  293.       }
  294.  
  295.       /* someday I will do this in the line discpline open XXX */
  296.       termio.c_cflag =  (B9600 | CREAD | CS8 | CLOCAL);
  297.       termio.c_iflag &= ~ISTRIP;
  298.       termio.c_iflag |= IXOFF;
  299.  
  300.       if (ioctl(pcmFd, TCSETA, &termio) < 0) {
  301.         Error("TCSETA");
  302.         return;
  303.       }
  304.  
  305.       i = PCMDISC;
  306.       if (ioctl(pcmFd, TIOCSETD, &i) < 0) {
  307.         Error("couldn't set line discipline");
  308.         return;
  309.       }
  310.  
  311.       t = AddInputDevice(wsButtonBoxProc, TRUE);
  312.       RegisterOtherDevice(t);
  313.  
  314.       t = AddInputDevice(wsDialBoxProc, TRUE);
  315.       RegisterOtherDevice(t);
  316.     }
  317.  
  318. }
  319.  
  320. void
  321. ExtInitOutput(screenInfo, argc, argv)
  322. ScreenInfo *screenInfo;
  323. int argc;
  324. char **argv;
  325. {
  326. /* nothing to do yet */
  327. }
  328.  
  329. static void
  330. wsButtonBoxControl(device, ctrl)
  331. DevicePtr device;
  332. LedCtrl *ctrl;
  333. {
  334.     int mask = ctrl->led_values;
  335.  
  336.     if (ioctl(pcmFd, PIOSETLED, &mask) < 0) {
  337.       Error("wsButtonBoxControl");
  338.     }
  339. }
  340.  
  341. static int
  342. wsButtonBoxProc(pDev, onoff, argc, argv)
  343. DevicePtr pDev;
  344. int onoff, argc;
  345. char *argv[];
  346. {
  347.     BYTE map[33];
  348.  
  349.     int i;
  350.     Atom typeatom;
  351.  
  352.     switch (onoff)
  353.     {
  354.       case DEVICE_INIT: 
  355.         wsButtonBox = pDev;
  356.         for (i=1; i<=32; i++)
  357.           map[i] = i;
  358.         InitButtonClassDeviceStruct(pDev, 32, map);
  359.         InitLedFeedbackClassDeviceStruct(pDev, wsButtonBoxControl);
  360.         InitFocusClassDeviceStruct(pDev);
  361.         typeatom = MakeAtom("BUTTONBOX", 9, FALSE);
  362.         AssignTypeAndName(pDev, typeatom, "BUTTONBOX");
  363.         break;
  364.       case DEVICE_ON: 
  365.         pDev->on = TRUE;
  366.         break;
  367.       case DEVICE_OFF: 
  368.         pDev->on = FALSE;
  369.         break;
  370.       case DEVICE_CLOSE:
  371.         if (pcmFd) {
  372.           close(pcmFd);
  373.           pcmFd = 0;
  374.         }
  375.         break;
  376.     }
  377.     return Success;
  378. }
  379.  
  380. static void
  381. wsDialBoxControl(device, ctrl)
  382. DevicePtr device;
  383. PtrCtrl *ctrl;
  384. {
  385.     struct dial_control control;
  386.  
  387.     control.smoothing = ctrl->num / ctrl->den;
  388.     control.threshold = ctrl->threshold;
  389.  
  390.     /*
  391.        InitPtrFeedbackClassDeviceStruct should not call the control
  392.          routine for extension devices with defaultPointerControl values,
  393.          which may be wrong, don't think there is a fix, so live w/ it
  394.     */
  395.  
  396.     /* hardware should not freak out when given a threshold of 0
  397.          and a smoothing factor of 0 */
  398.     if (!control.smoothing)
  399.       control.smoothing = 1;
  400.     if (!control.threshold)
  401.       control.threshold = 1;
  402.  
  403.     if (ioctl(pcmFd, PIOSETKNOB, &control) < 0) {
  404.       Error("wsDialBoxControl");
  405.     }
  406. }
  407.  
  408. static int
  409. wsDialBoxProc(pDev, onoff, argc, argv)
  410. DevicePtr pDev;
  411. int onoff, argc;
  412. char *argv[];
  413. {
  414.     Atom typeatom;
  415.  
  416.     switch (onoff)
  417.     {
  418.       case DEVICE_INIT: 
  419.         wsDialBox = pDev;
  420.         InitValuatorClassDeviceStruct(pDev, 8, wsGetMotionEvents,
  421.         MOTION_BUFFER_SIZE, Relative);
  422.         InitValuatorAxisStruct(pDev, 0, -32768, 32767, 1);
  423.         InitValuatorAxisStruct(pDev, 1, -32768, 32767, 1);
  424.         InitValuatorAxisStruct(pDev, 2, -32768, 32767, 1);
  425.         InitValuatorAxisStruct(pDev, 3, -32768, 32767, 1);
  426.         InitValuatorAxisStruct(pDev, 4, -32768, 32767, 1);
  427.         InitValuatorAxisStruct(pDev, 5, -32768, 32767, 1);
  428.         InitValuatorAxisStruct(pDev, 6, -32768, 32767, 1);
  429.         InitValuatorAxisStruct(pDev, 7, -32768, 32767, 1);
  430.         InitPtrFeedbackClassDeviceStruct(pDev, wsDialBoxControl);
  431.         InitFocusClassDeviceStruct(pDev);
  432.         typeatom = MakeAtom("KNOB_BOX", 8, FALSE);
  433.         AssignTypeAndName(pDev, typeatom, "KNOB_BOX");
  434.         break;
  435.       case DEVICE_ON: 
  436.         pDev->on = TRUE;
  437.         break;
  438.       case DEVICE_OFF: 
  439.         pDev->on = FALSE;
  440.         break;
  441.       case DEVICE_CLOSE:
  442.         if (pcmFd) {
  443.           close(pcmFd);
  444.           pcmFd = 0;
  445.         }
  446.         break;
  447.     }
  448.     return Success;
  449. }
  450.  
  451. static int
  452. wsTabletProc(pDev, onoff, argc, argv)
  453. DevicePtr pDev;
  454. int onoff, argc;
  455. char *argv[];
  456. {
  457.  
  458.     BYTE    map[5];
  459.     Atom typeatom;
  460.  
  461.     switch (onoff)
  462.     {
  463.       case DEVICE_INIT: 
  464.         wsTablet = pDev;
  465.         map[1] = 1;
  466.         map[2] = 2;
  467.         map[3] = 3;
  468.         map[4] = 4;
  469.         /* InitPointerDeviceStruct(
  470.         pDev, map, 4, wsGetMotionEvents,
  471.         wsChangeTabletControl, MOTION_BUFFER_SIZE); */
  472.         InitButtonClassDeviceStruct(pDev, 4, map);
  473.         InitValuatorClassDeviceStruct(pDev, 2, wsGetMotionEvents,
  474.         MOTION_BUFFER_SIZE, Absolute);
  475.         InitValuatorAxisStruct(pDev, 0, 0, 2199, 7874);
  476.         InitValuatorAxisStruct(pDev, 1, 0, 2199, 7874);
  477.                             /* 200 * 39.37 XXX */
  478. /*        InitPtrFeedbackClassDeviceStruct(pDev, wsTabletControl); */
  479.         InitProximityClassDeviceStruct(pDev);
  480.         InitFocusClassDeviceStruct(pDev);
  481.         typeatom = MakeAtom("TABLET", 6, FALSE);
  482.         AssignTypeAndName(pDev, typeatom, "TABLET");
  483.         break;
  484.       case DEVICE_ON: 
  485.         pDev->on = TRUE;
  486. /* only need to select off mouse file descriptor */
  487. /*        AddEnabledDevice(tbFd);  */
  488.         break;
  489.       case DEVICE_OFF: 
  490.         pDev->on = FALSE;
  491. /*        RemoveEnabledDevice(tbFd); */
  492.         break;
  493.       case DEVICE_CLOSE:
  494.         close(tbFd);
  495.         break;
  496.     }
  497.     return Success;
  498. }
  499.  
  500. #ifdef notdef
  501. void
  502. wsChangeTabletControl(device, ctrl)
  503.     DevicePtr device;
  504.     TabletCtrl   *ctrl;
  505. {
  506. /* XXX */
  507. }
  508. #endif
  509. #endif
  510.