home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / omron / omronMouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-29  |  9.7 KB  |  447 lines

  1. /*
  2.  * $XConsortium: omronMouse.c,v 1.1 91/06/29 13:49:03 xguest Exp $
  3.  *
  4.  * Copyright 1991 by OMRON Corporation
  5.  * 
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of OMRON not be used in advertising or
  11.  * publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  OMRON makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * OMRON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL OMRON
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. #include "omron.h"
  25. #include "omronMouse.h"
  26.  
  27. static Bool omronMouseInit();
  28. static void omronCrossScreen();
  29. static void omronWarpCursor();
  30. static void omronMouseControl(); 
  31. static Bool omronCursorOffScreen();
  32.  
  33. miPointerScreenFuncRec omronPointerScreenFuncs = {
  34.     omronCursorOffScreen,
  35.     omronCrossScreen,
  36.     omronWarpCursor,
  37. };
  38.  
  39. int
  40. omronMouseProc(pMouse,what)
  41. DevicePtr     pMouse;
  42. int        what;
  43. {
  44.     static    Bool initFlag = FALSE;
  45.     static    omronMousePrv    prv[1];
  46.     BYTE    map[5];
  47.  
  48.     switch(what) {
  49.     case DEVICE_INIT:
  50.         pMouse->devicePrivate = (pointer)prv;
  51.         if(initFlag == FALSE) {
  52.             if (!omronMouseInit(prv))
  53.                 return (!Success);
  54. #ifdef uniosu
  55.             if (!omronResetTty(prv))
  56.                 return (!Success);
  57. #endif
  58.             initFlag = TRUE;
  59.         }
  60.         prv->button_state = 7;
  61.         map[1] = Button1;
  62.         map[2] = Button2;
  63.         map[3] = Button3;
  64.         InitPointerDeviceStruct( pMouse,map, 3, miPointerGetMotionEvents,
  65.                 omronMouseControl, miPointerGetMotionBufferSize());
  66. #ifndef UNUSE_DRV_TIME
  67.         omronSetDriverTimeMode(pMouse, NULL);
  68. #endif
  69.         break;    
  70.     case DEVICE_ON:
  71. #ifndef UNUSE_SIGIO_SIGNAL
  72.         prv->ctl_flags |= FASYNC;
  73.         if (fcntl(prv->fd, F_SETFL, prv->ctl_flags) < 0) {
  74.             Error("Can't enable the mouse SIGIO.");
  75.             return (!Success);
  76.         }
  77. #endif
  78.         AddEnabledDevice (prv->fd);
  79.         pMouse->on = TRUE;
  80.         break;
  81.     case DEVICE_OFF:
  82.     case DEVICE_CLOSE:
  83. #ifndef UNUSE_SIGIO_SIGNAL
  84.         prv->ctl_flags &= ~FASYNC;
  85.         if (fcntl(prv->fd, F_SETFL, prv->ctl_flags) < 0) {
  86.             Error("Can't disable the mouse SIGIO.");
  87.         }
  88. #endif
  89.         if (ioctl(prv->fd, MSDFLUSH, NULL) < 0) {
  90.             Error("Mouse ioctl MSDFLUSH fault.");
  91.         }
  92.         RemoveEnabledDevice(prv->fd);
  93.         pMouse->on = FALSE;
  94.         break;
  95.     }
  96.     return (Success);
  97. }
  98.  
  99. #ifdef uniosu
  100. static Bool
  101. omronResetTty(prv)
  102. omronMousePrvPtr prv;
  103. {
  104.     int dummy = 0;
  105.  
  106.     if ((prv->ttyfd = open("/dev/tty",O_RDWR,0) ) == -1 ) {
  107.         if ( errno != ENXIO ) {
  108.             Error("Can't open tty.");
  109.             return FALSE;
  110.         }
  111.     }
  112.         
  113.     if ( ioctl(prv->ttyfd,TIOCNOTTY,dummy) < 0 ) {
  114.         Error("Can't ioctl TIOCNOTTY.");
  115.         return FALSE;
  116.     } 
  117.     return TRUE;
  118. }
  119. #endif
  120.  
  121. static Bool
  122. omronMouseInit(prv)
  123. omronMousePrvPtr prv;
  124. {
  125. #ifdef    uniosu
  126.     struct mssetlimt limit;
  127. #endif    /* uniosu */
  128. #ifdef UNUSE_SIGIO_SIGNAL
  129.     int arg = 1;
  130. #endif
  131.  
  132.     if ((prv->fd = open("/dev/mouse",O_RDONLY)) == -1) {
  133.         Error("Can't open mouse device.");
  134.         return FALSE;
  135.     }
  136.  
  137. #ifdef UNUSE_SIGIO_SIGNAL
  138.     ioctl(prv->fd, FIONBIO, &arg);
  139. #else
  140.     if ((prv->ctl_flags = fcntl(prv->fd, F_GETFL, NULL)) < 0) {
  141.         Error("Mouse fcntl F_GETFL fault.");
  142.         return FALSE;
  143.     }
  144.     prv->ctl_flags |= FNDELAY;
  145.     if (fcntl(prv->fd, F_SETFL, prv->ctl_flags) < 0
  146.         || fcntl(prv->fd, F_SETOWN, getpid()) < 0) {
  147.         Error("Can't set up mouse to receive SIGIO.");
  148.         return FALSE;
  149.     }
  150. #endif
  151.  
  152.     if (ioctl(prv->fd,MSSETCURS,8) < 0) {
  153.         Error("Mouse ioctl MSSETCURS fault.");
  154.         return FALSE;
  155.     }
  156.  
  157.     if(ioctl(prv->fd, MSSETMODE,1) < 0) {
  158.         Error("Mouse ioctl MSSEMODE fault.");
  159.         return FALSE;
  160.  
  161.     }
  162. #ifdef    uniosu
  163.     limit.mode = 1;
  164.     if(ioctl(prv->fd, MSSETLIMT,&limit) < 0) {
  165.         Error("Mouse ioctl MSSETLMT fault.");
  166.         return FALSE;
  167.     }
  168. #endif    /* uniosu */
  169.     return TRUE;
  170. }
  171.  
  172. void
  173. omronMouseGiveUp()
  174. {
  175.     DevicePtr     pMouse;
  176.     omronMousePrvPtr prv;
  177.  
  178.     if(pMouse = LookupPointerDevice()) {
  179.         prv = (omronMousePrvPtr)(pMouse->devicePrivate);
  180.         if(prv) {
  181.             (void)close(prv->fd);
  182. #ifdef uniosu
  183.             (void)close(prv->ttyfd);
  184. #endif
  185.         }
  186.     }
  187. }
  188.  
  189. #define MAXEVENTS    1024
  190.  
  191. static Bool
  192. omronCursorOffScreen(pScreen, x, y)
  193.     ScreenPtr    *pScreen;
  194.     int        *x, *y;
  195. {
  196.     return FALSE;
  197. }
  198.  
  199. static void
  200. omronCrossScreen(pScreen,x,y)
  201. ScreenPtr    pScreen;
  202. int        x;
  203. int        y;
  204. {
  205. }
  206.  
  207.  
  208. static short
  209. omronMouseAccelerate (pMouse, delta)
  210. DevicePtr      pMouse;
  211. int              delta;
  212. {
  213.     register PtrCtrl *p;
  214.     register int  s;
  215.  
  216.  
  217.     p = &((DeviceIntPtr)pMouse)->ptrfeed->ctrl;
  218.   
  219.     if(delta > 0) {
  220.     s = 1;
  221.     } else {
  222.     s = -1;
  223.     delta = -delta;
  224.     }
  225.     
  226.     if (delta > p->threshold) {
  227.     return ((short)(s * (p->threshold +
  228.             ((delta - p->threshold) * p->num) / p->den)));
  229.     } else {
  230.     return ((short)(s * delta));
  231.     }
  232. }
  233.  
  234.  
  235. static void
  236. omronMouseControl()
  237. {
  238. }
  239.  
  240. static void
  241. omronWarpCursor (pScreen, x, y)
  242.     ScreenPtr   pScreen;
  243.     int         x, y;
  244. {
  245.     int oldmask;
  246.  
  247.     oldmask = sigblock (sigmask(SIGIO));
  248.     miPointerWarpCursor (pScreen, x, y);
  249.     sigsetmask (oldmask);
  250. }
  251.  
  252.  
  253.  
  254.  
  255. struct msdata *
  256. omronMouseGetEvents(pMouse, pNumEvents, pAgain)
  257. DevicePtr     pMouse;
  258. int           *pNumEvents;
  259. Bool          *pAgain;
  260. {
  261.     struct msdgeta    msdata;
  262.     static struct msdata data[MAXEVENTS];
  263.     omronMousePrvPtr prv =(omronMousePrvPtr) (pMouse->devicePrivate);
  264.  
  265.     msdata.mode = 1;
  266.     msdata.count = MAXEVENTS;
  267.     msdata.msdatap = data;
  268.  
  269.     if (ioctl(prv->fd,MSDGETA,&msdata) < 0) {
  270.         return FALSE;
  271.     }
  272.  
  273.     *pNumEvents = msdata.retval;
  274.     
  275.     *pAgain = (msdata.retval == MAXEVENTS);
  276.     
  277.     return(data);
  278. }
  279.  
  280. #ifndef UNUSE_DRV_TIME
  281. struct msdatat *
  282. omronMouseGetTEvents(pMouse, pNumEvents, pAgain)
  283. DevicePtr     pMouse;
  284. int           *pNumEvents;
  285. Bool          *pAgain;
  286. {
  287.     struct msdgetat    msdatat;
  288.     static struct msdatat datat[MAXEVENTS];
  289.     omronMousePrvPtr prv =(omronMousePrvPtr) (pMouse->devicePrivate);
  290.  
  291.     msdatat.mode = 1;
  292.     msdatat.count = MAXEVENTS;
  293.     msdatat.msdatatp = datat;
  294.     if (ioctl(prv->fd,MSDGETAT,&msdatat) < 0) {
  295.         return FALSE;
  296.     }
  297.  
  298.     *pNumEvents = msdatat.retval;
  299.     
  300.     *pAgain = (msdatat.retval == MAXEVENTS);
  301.     
  302.     return(datat);
  303. }
  304. #endif
  305.  
  306.  
  307. void
  308. omronMouseEnqueueEvent(pMouse, data)
  309. DevicePtr     pMouse;
  310. struct msdata           *data;
  311. {
  312.     register     int i;
  313.     register    int    button_state;
  314.     xEvent              xE;
  315.     short omronMouseAccelerate();
  316.     int delta_X,delta_Y;
  317.     omronMousePrvPtr prv =(omronMousePrvPtr) (pMouse->devicePrivate);
  318.  
  319.     button_state = prv->button_state;
  320.     lastEventTime = GetTimeInMillis();
  321.     xE.u.keyButtonPointer.time = lastEventTime;
  322.         
  323.     switch(data->type) {
  324.     case COORDEVNT:
  325.         xE.u.u.type = MotionNotify;
  326.         delta_X = omronMouseAccelerate(pMouse,
  327.                 data->event.coordinate.X_coord);
  328.         delta_Y = omronMouseAccelerate(pMouse,
  329.                 data->event.coordinate.Y_coord);
  330.         miPointerDeltaCursor (delta_X, delta_Y, lastEventTime);
  331.         break;
  332.     case BUTONEVNT:
  333.         i = ((data->event.button.L_button == 0) << 2) |
  334.             ((data->event.button.M_button == 0) << 1) |
  335.             (data->event.button.R_button == 0);
  336.         button_state = (((button_state & ~i) & 0x7) << 6) |
  337.                 (((~button_state & i) & 0x7) << 3) |
  338.                 (i & 0x7);
  339.  
  340.         if (BUTTON_PRESSED(button_state)) {
  341.             xE.u.u.type = ButtonPress;
  342.             if (LEFT_PRESSED(button_state)) {
  343.                 xE.u.u.detail = Button1; 
  344.                 mieqEnqueue(&xE);
  345.             }
  346.             if (RIGHT_PRESSED(button_state)) {
  347.                 xE.u.u.detail = Button3; 
  348.                 mieqEnqueue(&xE);
  349.             }
  350.             if (MIDDLE_PRESSED(button_state)) {
  351.                 xE.u.u.detail = Button2; 
  352.                 mieqEnqueue(&xE);
  353.             }
  354.         }
  355.         if (BUTTON_RELEASED(button_state)) {
  356.             xE.u.u.type = ButtonRelease;
  357.             if (LEFT_RELEASED(button_state)) {
  358.                 xE.u.u.detail = Button1; 
  359.                 mieqEnqueue(&xE);
  360.             }
  361.             if (RIGHT_RELEASED(button_state)) {
  362.                 xE.u.u.detail = Button3; 
  363.                 mieqEnqueue(&xE);
  364.             }
  365.             if (MIDDLE_RELEASED(button_state)) {
  366.                 xE.u.u.detail = Button2; 
  367.                 mieqEnqueue(&xE);
  368.             }
  369.         }
  370.         break;
  371.     }
  372.     prv->button_state = button_state;
  373. }
  374.  
  375. #ifndef UNUSE_DRV_TIME
  376. void
  377. omronMouseEnqueueTEvent(pMouse, data)
  378. DevicePtr     pMouse;
  379. struct msdatat           *data;
  380. {
  381.     register     int i;
  382.     register    int    button_state;
  383.     xEvent              xE;
  384.     short omronMouseAccelerate();
  385.     int delta_X,delta_Y;
  386.     register unsigned long  time;
  387.     omronMousePrvPtr prv =(omronMousePrvPtr) (pMouse->devicePrivate);
  388.  
  389.     button_state = prv->button_state;
  390.  
  391.     time = xE.u.keyButtonPointer.time = data->time;    
  392.  
  393.     switch(data->type) {
  394.     case COORDEVNT:
  395.         xE.u.u.type = MotionNotify;
  396.         delta_X = omronMouseAccelerate(pMouse,
  397.                 data->event.coordinate.X_coord);
  398.         delta_Y = omronMouseAccelerate(pMouse,
  399.                 data->event.coordinate.Y_coord);
  400.         miPointerDeltaCursor (delta_X, delta_Y, time);
  401.         break;
  402.     case BUTONEVNT:
  403.         i = ((data->event.button.L_button == 0) << 2) |
  404.             ((data->event.button.M_button == 0) << 1) |
  405.             (data->event.button.R_button == 0);
  406.         button_state = (((button_state & ~i) & 0x7) << 6) |
  407.                 (((~button_state & i) & 0x7) << 3) |
  408.                 (i & 0x7);
  409.  
  410.         if (BUTTON_PRESSED(button_state)) {
  411.             xE.u.u.type = ButtonPress;
  412.             if (LEFT_PRESSED(button_state)) {
  413.                 xE.u.u.detail = 1; 
  414.                 mieqEnqueue(&xE);
  415.             }
  416.             if (RIGHT_PRESSED(button_state)) {
  417.                 xE.u.u.detail = 3; 
  418.                 mieqEnqueue(&xE);
  419.             }
  420.             if (MIDDLE_PRESSED(button_state)) {
  421.                 xE.u.u.detail = 2; 
  422.                 mieqEnqueue(&xE);
  423.             }
  424.         }
  425.         if (BUTTON_RELEASED(button_state)) {
  426.             xE.u.u.type = ButtonRelease;
  427.             if (LEFT_RELEASED(button_state)) {
  428.                 xE.u.u.detail = 1; 
  429.                 mieqEnqueue(&xE);
  430.             }
  431.             if (RIGHT_RELEASED(button_state)) {
  432.                 xE.u.u.detail = 3; 
  433.                 mieqEnqueue(&xE);
  434.             }
  435.             if (MIDDLE_RELEASED(button_state)) {
  436.                 xE.u.u.detail = 2; 
  437.                 mieqEnqueue(&xE);
  438.             }
  439.         }
  440.         break;
  441.     }
  442.     prv->button_state = button_state;
  443. }
  444. #endif
  445.  
  446.  
  447.