home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / sun / sunIo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-22  |  9.8 KB  |  375 lines

  1. /*-
  2.  * sunIo.c --
  3.  *    Functions to handle input from the keyboard and mouse.
  4.  *
  5.  * Copyright (c) 1987 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17.  
  18. /************************************************************
  19. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  20.  
  21.                     All Rights Reserved
  22.  
  23. Permission  to  use,  copy,  modify,  and  distribute   this
  24. software  and  its documentation for any purpose and without
  25. fee is hereby granted, provided that the above copyright no-
  26. tice  appear  in all copies and that both that copyright no-
  27. tice and this permission notice appear in  supporting  docu-
  28. mentation,  and  that the names of Sun or MIT not be used in
  29. advertising or publicity pertaining to distribution  of  the
  30. software  without specific prior written permission. Sun and
  31. M.I.T. make no representations about the suitability of this
  32. software for any purpose. It is provided "as is" without any
  33. express or implied warranty.
  34.  
  35. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  36. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  37. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  38. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  39. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  40. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  41. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  42. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  43.  
  44. ********************************************************/
  45.  
  46. #ifndef    lint
  47. static char sccsid[] = "%W %G Copyright 1987 Sun Micro";
  48. #endif
  49.  
  50. #include    "sun.h"
  51. #include    "opaque.h"
  52.  
  53. int            lastEventTime = 0;
  54. extern int      screenIsSaved;
  55. extern void    SaveScreens();
  56.  
  57. #ifdef SUN_WINDOWS
  58. int    windowFd = 0;
  59. int    sunIgnoreEvent = TRUE;
  60. #define    INPBUFSIZE    128
  61. #endif SUN_WINDOWS
  62.  
  63. /*-
  64.  *-----------------------------------------------------------------------
  65.  * TimeSinceLastInputEvent --
  66.  *    Function used for screensaver purposes by the os module.
  67.  *
  68.  * Results:
  69.  *    The time in milliseconds since there last was any
  70.  *    input.
  71.  *
  72.  * Side Effects:
  73.  *    None.
  74.  *
  75.  *-----------------------------------------------------------------------
  76.  */
  77. int
  78. TimeSinceLastInputEvent()
  79. {
  80.     struct timeval    now;
  81.  
  82.     gettimeofday (&now, (struct timezone *)0);
  83.  
  84.     if (lastEventTime == 0) {
  85.     lastEventTime = TVTOMILLI(now);
  86.     }
  87.     return TVTOMILLI(now) - lastEventTime;
  88. }
  89.  
  90. /*-
  91.  *-----------------------------------------------------------------------
  92.  * ProcessInputEvents --
  93.  *    Retrieve all waiting input events and pass them to DIX in their
  94.  *    correct chronological order. Only reads from the system pointer
  95.  *    and keyboard.
  96.  *
  97.  * Results:
  98.  *    None.
  99.  *
  100.  * Side Effects:
  101.  *    Events are passed to the DIX layer.
  102.  *
  103.  *-----------------------------------------------------------------------
  104.  */
  105. void
  106. ProcessInputEvents ()
  107. {
  108.     mieqProcessInputEvents ();
  109.     miPointerUpdate ();
  110. }
  111.  
  112. /*
  113.  *-----------------------------------------------------------------------
  114.  * sunEnqueueEvents
  115.  *    When a SIGIO is received, read device hard events and
  116.  *    enqueue them using the mi event queue
  117.  */
  118.  
  119. sunEnqueueEvents ()
  120. {
  121.     register Firm_event    *ptrEvents,        /* Current pointer event */
  122.                *kbdEvents;            /* Current keyboard event */
  123.     register int        numPtrEvents,     /* Number of remaining pointer
  124.                          * events */
  125.                 numKbdEvents;       /* Number of remaining
  126.                          * keyboard events */
  127.     int                      nPE,                /* Original number of pointer
  128.                          * events */
  129.                 nKE;                /* Original number of
  130.                          * keyboard events */
  131.     Bool            PtrAgain,        /* need to (re)read */
  132.                 KbdAgain;        /* need to (re)read */
  133.     DevicePtr            pPointer;
  134.     DevicePtr            pKeyboard;
  135.     register PtrPrivPtr     ptrPriv;
  136.     register KbPrivPtr        kbdPriv;
  137.     Firm_event              *lastEvent;            /* Last event processed */
  138.     enum {
  139.     NoneYet, Ptr, Kbd
  140.     }                lastType = NoneYet;    /* Type of last event */
  141.  
  142. #ifdef SUN_WINDOWS
  143.     struct inputevent sunevents[INPBUFSIZE];
  144.     register struct inputevent *se = sunevents, *seL;
  145.     int         n;
  146. #endif SUN_WINDOWS
  147.  
  148.     pPointer = LookupPointerDevice();
  149.     pKeyboard = LookupKeyboardDevice();
  150.     if (!pPointer->on || !pKeyboard->on)
  151.     return;
  152.  
  153.     if ( sunUseSunWindows() ) {
  154. #ifdef SUN_WINDOWS
  155.     if ((n=read(windowFd,sunevents,INPBUFSIZE*sizeof sunevents[0])) < 0 
  156.                 && errno != EWOULDBLOCK) {
  157.         /*
  158.          * Error reading events; should do something. XXX
  159.          */
  160. /*debug*/
  161.     ErrorF("ProcessInputEvents: read(windowFd)  n=%d\n",n);
  162.         return;
  163.     }
  164.  
  165.     for (seL = sunevents + (n/(sizeof sunevents[0]));  se < seL; se++) {
  166.         lastEventTime = TVTOMILLI(event_time(se));
  167.  
  168.         /*
  169.          * Decide whether or not to pay attention to events.
  170.          * Ignore the events if the locator has exited X Display.
  171.          */
  172.         switch (event_id(se)) {
  173.         case KBD_DONE:
  174.             sunChangeKbdTranslation( pKeyboard, FALSE );
  175.             break;
  176.         case KBD_USE:
  177.             sunChangeKbdTranslation( pKeyboard, TRUE );
  178.             break;
  179.         case LOC_WINENTER:
  180.             sunIgnoreEvent = FALSE;
  181.             break;
  182.         case LOC_WINEXIT:
  183.             sunIgnoreEvent = TRUE;
  184.             break;
  185.         }
  186.  
  187.         if (sunIgnoreEvent) {
  188.         continue;
  189.         }
  190.  
  191.         /*
  192.          * Figure out the X device this event should be reported on.
  193.          */
  194.         switch (event_id(se)) {
  195.         case LOC_MOVE:
  196.         case MS_LEFT:
  197.         case MS_MIDDLE:
  198.         case MS_RIGHT:
  199.             sunMouseEnqueueEventSunWin(pPointer,se);
  200.             break;
  201.         case LOC_WINEXIT:
  202.         case LOC_WINENTER:
  203.         case KBD_DONE:
  204.         case KBD_USE:
  205.             break;
  206.         default:
  207.             sunKbdEnqueueEventSunWin(pKeyboard,se);
  208.             break;
  209.         }
  210.     }
  211. #endif SUN_WINDOWS
  212.     } 
  213.     else {
  214.     ptrPriv = (PtrPrivPtr)pPointer->devicePrivate;
  215.     kbdPriv = (KbPrivPtr)pKeyboard->devicePrivate;
  216.     
  217.     numPtrEvents = 0;
  218.     PtrAgain = TRUE;
  219.     numKbdEvents = 0;
  220.     KbdAgain = TRUE;
  221.     lastEvent = (Firm_event *)0;
  222.  
  223.     /*
  224.      * So long as one event from either device remains unprocess, we loop:
  225.      * Take the oldest remaining event and pass it to the proper module
  226.      * for processing. The DDXEvent will be sent to ProcessInput by the
  227.      * function called.
  228.      */
  229.     while (1) {
  230.         /*
  231.          * Get events from both the pointer and the keyboard, storing the number
  232.          * of events gotten in nPE and nKE and keeping the start of both arrays
  233.          * in pE and kE
  234.          */
  235.         if ((numPtrEvents == 0) && PtrAgain) {
  236.         ptrEvents = (* ptrPriv->GetEvents) (pPointer, &nPE, &PtrAgain);
  237.         numPtrEvents = nPE;
  238.         }
  239.         if ((numKbdEvents == 0) && KbdAgain) {
  240.         kbdEvents = (* kbdPriv->GetEvents) (pKeyboard, &nKE, &KbdAgain);
  241.         numKbdEvents = nKE;
  242.         }
  243.         if ((numPtrEvents == 0) && (numKbdEvents == 0))
  244.         break;
  245.         if (numPtrEvents && numKbdEvents) {
  246.         if (timercmp (&kbdEvents->time, &ptrEvents->time, <)) {
  247.             (* kbdPriv->EnqueueEvent) (pKeyboard, kbdEvents);
  248.             numKbdEvents--;
  249.             lastEvent = kbdEvents++;
  250.             lastType = Kbd;
  251.         } else {
  252.             (* ptrPriv->EnqueueEvent) (pPointer, ptrEvents);
  253.             numPtrEvents--;
  254.             lastEvent = ptrEvents++;
  255.             lastType = Ptr;
  256.         }
  257.         } else if (numKbdEvents) {
  258.         (* kbdPriv->EnqueueEvent) (pKeyboard, kbdEvents);
  259.         numKbdEvents--;
  260.         lastEvent = kbdEvents++;
  261.         lastType = Kbd;
  262.         } else {
  263.         (* ptrPriv->EnqueueEvent) (pPointer, ptrEvents);
  264.         numPtrEvents--;
  265.         lastEvent = ptrEvents++;
  266.         lastType = Ptr;
  267.         }
  268.     }
  269.     if (lastEvent)
  270.         lastEventTime = TVTOMILLI(lastEvent->time);
  271.     }
  272. }
  273.  
  274.  
  275. /*-
  276.  *-----------------------------------------------------------------------
  277.  * SetTimeSinceLastInputEvent --
  278.  *    Set the lastEventTime to now.
  279.  *
  280.  * Results:
  281.  *    None.
  282.  *
  283.  * Side Effects:
  284.  *    lastEventTime is altered.
  285.  *
  286.  *-----------------------------------------------------------------------
  287.  */
  288. void
  289. SetTimeSinceLastInputEvent()
  290. {
  291.     struct timeval now;
  292.  
  293.     gettimeofday (&now, (struct timezone *)0);
  294.     lastEventTime = TVTOMILLI(now);
  295. }
  296.  
  297. /*
  298.  * DDX - specific abort routine.  Called by AbortServer().
  299.  */
  300. void
  301. AbortDDX()
  302. {
  303.     int        i;
  304.     ScreenPtr    pScreen;
  305.  
  306.     signal (SIGIO, SIG_IGN);
  307.     sunChangeKbdTranslation (LookupKeyboardDevice (), FALSE);
  308.     sunNonBlockConsoleOff ((char *) 0);
  309.     for (i = 0; i < screenInfo.numScreens; i++)
  310.     {
  311.     pScreen = screenInfo.screens[i];
  312.     (*pScreen->SaveScreen) (pScreen, SCREEN_SAVER_OFF);
  313.     sunDisableCursor (pScreen);
  314.     }
  315. }
  316.  
  317. /* Called by GiveUp(). */
  318. void
  319. ddxGiveUp()
  320. {
  321.     AbortDDX ();
  322. }
  323.  
  324. int
  325. ddxProcessArgument (argc, argv, i)
  326.     int    argc;
  327.     char *argv[];
  328.     int    i;
  329. {
  330.     extern void UseMsg();
  331.     extern Bool ActiveZaphod;
  332.     extern Bool FlipPixels;
  333.  
  334.     if (strcmp (argv[i], "-ar1") == 0) {    /* -ar1 int */
  335.     if (++i >= argc) UseMsg ();
  336.     autoRepeatInitiate = 1000 * (long)atoi(argv[i]);
  337.     return 2;
  338.     }
  339.     if (strcmp (argv[i], "-ar2") == 0) {    /* -ar2 int */
  340.     if (++i >= argc) UseMsg ();
  341.     autoRepeatDelay = 1000 * (long)atoi(argv[i]);
  342.     return 2;
  343.     }
  344.     if (strcmp (argv[i], "-debug") == 0) {    /* -debug */
  345.     return 1;
  346.     }
  347.     if (strcmp (argv[i], "-dev") == 0) {    /* -dev /dev/mumble */
  348.     if (++i >= argc) UseMsg ();
  349.     return 2;
  350.     }
  351.     if (strcmp (argv[i], "-mono") == 0) {    /* -mono */
  352.     return 1;
  353.     }
  354.     if (strcmp (argv[i], "-zaphod") == 0) {    /* -zaphod */
  355.     ActiveZaphod = FALSE;
  356.     return 1;
  357.     }
  358.     if (strcmp (argv[i], "-flipPixels") == 0) {    /* -flipPixels */
  359.     FlipPixels = TRUE;
  360.     return 1;
  361.     }
  362.     return 0;
  363. }
  364.  
  365. void
  366. ddxUseMsg()
  367. {
  368.     ErrorF("-ar1 int               set autorepeat initiate time\n");
  369.     ErrorF("-ar2 int               set autorepeat interval time\n");
  370.     ErrorF("-debug                 disable non-blocking console mode\n");
  371.     ErrorF("-dev filename          name of device to open\n");
  372.     ErrorF("-mono                  force monochrome-only screen\n");
  373.     ErrorF("-zaphod                disable active Zaphod mode\n");
  374. }
  375.