home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / os / WaitFor.c.orig < prev    next >
Encoding:
Text File  |  1991-07-03  |  7.7 KB  |  292 lines

  1. /***********************************************************
  2. Copyright 1987 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. /* $XConsortium: WaitFor.c,v 1.55 91/06/13 08:55:43 rws Exp $ */
  26.  
  27. /*****************************************************************
  28.  * OS Depedent input routines:
  29.  *
  30.  *  WaitForSomething,  GetEvent
  31.  *
  32.  *****************************************************************/
  33.  
  34. #include "Xos.h"            /* for strings, fcntl, time */
  35.  
  36. #include <errno.h>
  37. #include <stdio.h>
  38. #include "X.h"
  39. #include "misc.h"
  40.  
  41. #include <sys/param.h>
  42. #include <signal.h>
  43. #include "osdep.h"
  44. #include "dixstruct.h"
  45. #include "opaque.h"
  46.  
  47. extern long AllSockets[];
  48. extern long AllClients[];
  49. extern long LastSelectMask[];
  50. extern long WellKnownConnections;
  51. extern long EnabledDevices[];
  52. extern long ClientsWithInput[];
  53. extern long ClientsWriteBlocked[];
  54. extern long OutputPending[];
  55.  
  56. extern long ScreenSaverTime;               /* milliseconds */
  57. extern long ScreenSaverInterval;               /* milliseconds */
  58. extern int ConnectionTranslation[];
  59.  
  60. extern Bool NewOutputPending;
  61. extern Bool AnyClientsWriteBlocked;
  62.  
  63. extern WorkQueuePtr workQueue;
  64.  
  65. extern void CheckConnections();
  66. extern void EstablishNewConnections();
  67. extern void SaveScreens();
  68. extern void ResetOsBuffers();
  69. extern void ProcessInputEvents();
  70. extern void BlockHandler();
  71. extern void WakeupHandler();
  72.  
  73. extern int errno;
  74.  
  75. #ifdef apollo
  76. extern long apInputMask[];
  77.  
  78. static long LastWriteMask[mskcnt];
  79. #endif
  80.  
  81. #ifdef XTESTEXT1
  82. /*
  83.  * defined in xtestext1dd.c
  84.  */
  85. extern int playback_on;
  86. #endif /* XTESTEXT1 */
  87.  
  88. /*****************
  89.  * WaitForSomething:
  90.  *     Make the server suspend until there is
  91.  *    1. data from clients or
  92.  *    2. input events available or
  93.  *    3. ddx notices something of interest (graphics
  94.  *       queue ready, etc.) or
  95.  *    4. clients that have buffered replies/events are ready
  96.  *
  97.  *     If the time between INPUT events is
  98.  *     greater than ScreenSaverTime, the display is turned off (or
  99.  *     saved, depending on the hardware).  So, WaitForSomething()
  100.  *     has to handle this also (that's why the select() has a timeout.
  101.  *     For more info on ClientsWithInput, see ReadRequestFromClient().
  102.  *     pClientsReady is an array to store ready client->index values into.
  103.  *****************/
  104.  
  105. static long timeTilFrob = 0;        /* while screen saving */
  106.  
  107. int
  108. WaitForSomething(pClientsReady)
  109.     int *pClientsReady;
  110. {
  111.     int i;
  112.     struct timeval waittime, *wt;
  113.     long timeout;
  114.     long clientsReadable[mskcnt];
  115.     long clientsWritable[mskcnt];
  116.     long curclient;
  117.     int selecterr;
  118.     int nready;
  119.     long devicesReadable[mskcnt];
  120.  
  121.     CLEARBITS(clientsReadable);
  122.  
  123.     /* We need a while loop here to handle 
  124.        crashed connections and the screen saver timeout */
  125.     while (1)
  126.     {
  127.     /* deal with any blocked jobs */
  128.     if (workQueue)
  129.         ProcessWorkQueue();
  130.  
  131.     if (ANYSET(ClientsWithInput))
  132.     {
  133.         COPYBITS(ClientsWithInput, clientsReadable);
  134.         break;
  135.     }
  136.     if (ScreenSaverTime)
  137.     {
  138.         timeout = ScreenSaverTime - TimeSinceLastInputEvent();
  139.         if (timeout <= 0) /* may be forced by AutoResetServer() */
  140.         {
  141.         long timeSinceSave;
  142.  
  143.         timeSinceSave = -timeout;
  144.         if ((timeSinceSave >= timeTilFrob) && (timeTilFrob >= 0))
  145.         {
  146.             ResetOsBuffers(); /* not ideal, but better than nothing */
  147.             SaveScreens(SCREEN_SAVER_ON, ScreenSaverActive);
  148.             if (ScreenSaverInterval)
  149.             /* round up to the next ScreenSaverInterval */
  150.             timeTilFrob = ScreenSaverInterval *
  151.                 ((timeSinceSave + ScreenSaverInterval) /
  152.                     ScreenSaverInterval);
  153.             else
  154.             timeTilFrob = -1;
  155.         }
  156.         timeout = timeTilFrob - timeSinceSave;
  157.         }
  158.         else
  159.         {
  160.         if (timeout > ScreenSaverTime)
  161.             timeout = ScreenSaverTime;
  162.         timeTilFrob = 0;
  163.         }
  164.         if (timeTilFrob >= 0)
  165.         {
  166.         waittime.tv_sec = timeout / MILLI_PER_SECOND;
  167.         waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
  168.                     (1000000 / MILLI_PER_SECOND);
  169.         wt = &waittime;
  170.         }
  171.         else
  172.         {
  173.         wt = NULL;
  174.         }
  175.     }
  176.     else
  177.         wt = NULL;
  178.     COPYBITS(AllSockets, LastSelectMask);
  179. #ifdef apollo
  180.         COPYBITS(apInputMask, LastWriteMask);
  181. #endif
  182.     BlockHandler((pointer)&wt, (pointer)LastSelectMask);
  183.     if (NewOutputPending)
  184.         FlushAllOutput();
  185. #ifdef XTESTEXT1
  186.     /* XXX how does this interact with new write block handling? */
  187.     if (playback_on) {
  188.         wt = &waittime;
  189.         XTestComputeWaitTime (&waittime);
  190.     }
  191. #endif /* XTESTEXT1 */
  192.     /* keep this check close to select() call to minimize race */
  193.     if (dispatchException)
  194.         i = -1;
  195.     else if (AnyClientsWriteBlocked)
  196.     {
  197.         COPYBITS(ClientsWriteBlocked, clientsWritable);
  198.         i = select (MAXSOCKS, (int *)LastSelectMask,
  199.             (int *)clientsWritable, (int *) NULL, wt);
  200.     }
  201.     else
  202. #ifdef apollo
  203.         i = select (MAXSOCKS, (int *)LastSelectMask,
  204.             (int *)LastWriteMask, (int *) NULL, wt);
  205. #else
  206.         i = select (MAXSOCKS, (int *)LastSelectMask,
  207.             (int *) NULL, (int *) NULL, wt);
  208. #endif
  209.     selecterr = errno;
  210.     WakeupHandler((unsigned long)i, (pointer)LastSelectMask);
  211. #ifdef XTESTEXT1
  212.     if (playback_on) {
  213.         i = XTestProcessInputAction (i, &waittime);
  214.     }
  215. #endif /* XTESTEXT1 */
  216.     if (i <= 0) /* An error or timeout occurred */
  217.     {
  218.  
  219.         if (dispatchException)
  220.         return 0;
  221.         CLEARBITS(clientsWritable);
  222.         if (i < 0) 
  223.         if (selecterr == EBADF)    /* Some client disconnected */
  224.         {
  225.             CheckConnections ();
  226.             if (! ANYSET (AllClients))
  227.             return 0;
  228.         }
  229.         else if (selecterr != EINTR)
  230.             ErrorF("WaitForSomething(): select: errno=%d\n",
  231.             selecterr);
  232.         if (*checkForInput[0] != *checkForInput[1])
  233.         return 0;
  234.     }
  235.     else
  236.     {
  237.         if (AnyClientsWriteBlocked && ANYSET (clientsWritable))
  238.         {
  239.         NewOutputPending = TRUE;
  240.         ORBITS(OutputPending, clientsWritable, OutputPending);
  241.         UNSETBITS(ClientsWriteBlocked, clientsWritable);
  242.         if (! ANYSET(ClientsWriteBlocked))
  243.             AnyClientsWriteBlocked = FALSE;
  244.         }
  245.  
  246.         MASKANDSETBITS(devicesReadable, LastSelectMask, EnabledDevices);
  247. #ifdef    hpux
  248.             /* call the HIL driver to gather inputs.     */
  249.         if (ANYSET(devicesReadable)) store_inputs (devicesReadable);
  250. #endif /* hpux */
  251.  
  252.         MASKANDSETBITS(clientsReadable, LastSelectMask, AllClients); 
  253.         if (LastSelectMask[0] & WellKnownConnections) 
  254.         EstablishNewConnections();
  255.         if (ANYSET (devicesReadable) || ANYSET (clientsReadable))
  256.         break;
  257.     }
  258.     }
  259.  
  260.     nready = 0;
  261.     if (ANYSET(clientsReadable))
  262.     {
  263.     for (i=0; i<mskcnt; i++)
  264.     {
  265.         while (clientsReadable[i])
  266.         {
  267.         curclient = ffs (clientsReadable[i]) - 1;
  268.         pClientsReady[nready++] = 
  269.             ConnectionTranslation[curclient + (i << 5)];
  270.         clientsReadable[i] &= ~(1 << curclient);
  271.         }
  272.     }    
  273.     }
  274.     return nready;
  275. }
  276.  
  277. #ifndef ANYSET
  278. /*
  279.  * This is not always a macro.
  280.  */
  281. ANYSET(src)
  282.     long    *src;
  283. {
  284.     int i;
  285.  
  286.     for (i=0; i<mskcnt; i++)
  287.     if (src[ i ])
  288.         return (TRUE);
  289.     return (FALSE);
  290. }
  291. #endif
  292.