📄 waitfor.c
字号:
/***********************************************************Copyright (c) 1987 X ConsortiumPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEX CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER INAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shall not beused in advertising or otherwise to promote the sale, use or other dealingsin this Software without prior written authorization from the X Consortium.Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts. All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of Digital not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* $XConsortium: WaitFor.c /main/55 1996/12/02 10:22:24 lehors $ *//* $XFree86: xc/programs/Xserver/os/WaitFor.c,v 3.11.2.3 1998/01/31 14:23:33 hohndel Exp $ *//***************************************************************** * OS Dependent input routines: * * WaitForSomething * TimerForce, TimerSet, TimerCheck, TimerFree * *****************************************************************/#ifdef WIN32#include <X11/Xwinsock.h>#endif#include "Xos.h" /* for strings, fcntl, time */#include <errno.h>#ifdef X_NOT_STDC_ENVextern int errno;#endif#include <stdio.h>#include "X.h"#include "misc.h"#ifdef MINIX#include <sys/nbio.h>#define select(n,r,w,x,t) nbio_select(n,r,w,x,t)#endif#ifdef __EMX__#define select(n,r,w,x,t) os2PseudoSelect(n,r,w,x,t)#endif#include <X11/Xpoll.h>#include "osdep.h"#include "dixstruct.h"#include "opaque.h"#ifdef DPMSExtension#include "dpms.h"extern void DPMSSet();#endifextern fd_set AllSockets;extern fd_set AllClients;extern fd_set LastSelectMask;extern fd_set WellKnownConnections;extern fd_set EnabledDevices;extern fd_set ClientsWithInput;extern fd_set ClientsWriteBlocked;extern fd_set OutputPending;extern int ConnectionTranslation[];extern Bool NewOutputPending;extern Bool AnyClientsWriteBlocked;extern WorkQueuePtr workQueue;#ifdef XTESTEXT1/* * defined in xtestext1dd.c */extern int playback_on;#endif /* XTESTEXT1 */struct _OsTimerRec { OsTimerPtr next; CARD32 expires; OsTimerCallback callback; pointer arg;};static void DoTimer();static OsTimerPtr timers;/***************** * WaitForSomething: * Make the server suspend until there is * 1. data from clients or * 2. input events available or * 3. ddx notices something of interest (graphics * queue ready, etc.) or * 4. clients that have buffered replies/events are ready * * If the time between INPUT events is * greater than ScreenSaverTime, the display is turned off (or * saved, depending on the hardware). So, WaitForSomething() * has to handle this also (that's why the select() has a timeout. * For more info on ClientsWithInput, see ReadRequestFromClient(). * pClientsReady is an array to store ready client->index values into. *****************/static INT32 timeTilFrob = 0; /* while screen saving */#if !defined(AMOEBA)intWaitForSomething(pClientsReady) int *pClientsReady;{ int i; struct timeval waittime, *wt; INT32 timeout;#ifdef DPMSExtension INT32 standbyTimeout, suspendTimeout, offTimeout;#endif fd_set clientsReadable; fd_set clientsWritable; int curclient; int selecterr; int nready; fd_set devicesReadable; CARD32 now; FD_ZERO(&clientsReadable); /* We need a while loop here to handle crashed connections and the screen saver timeout */ while (1) { /* deal with any blocked jobs */ if (workQueue) ProcessWorkQueue(); if (XFD_ANYSET (&ClientsWithInput)) { XFD_COPYSET (&ClientsWithInput, &clientsReadable); break; }#ifdef DPMSExtension if (ScreenSaverTime > 0 || DPMSEnabled || timers)#else if (ScreenSaverTime > 0 || timers)#endif now = GetTimeInMillis(); wt = NULL; if (timers) { while (timers && timers->expires <= now) DoTimer(timers, now, &timers); if (timers) { timeout = timers->expires - now; waittime.tv_sec = timeout / MILLI_PER_SECOND; waittime.tv_usec = (timeout % MILLI_PER_SECOND) * (1000000 / MILLI_PER_SECOND); wt = &waittime; } }#ifdef DPMSExtension if (ScreenSaverTime > 0 || (DPMSEnabled && (DPMSStandbyTime > 0 || DPMSSuspendTime > 0 || DPMSOffTime > 0)))#else if (ScreenSaverTime > 0)#endif {#ifdef DPMSExtension if (ScreenSaverTime > 0) timeout = (ScreenSaverTime - (now - lastDeviceEventTime.milliseconds)); if (DPMSStandbyTime > 0) standbyTimeout = (DPMSStandbyTime - (now - lastDeviceEventTime.milliseconds)); if (DPMSSuspendTime > 0) suspendTimeout = (DPMSSuspendTime - (now - lastDeviceEventTime.milliseconds)); if (DPMSOffTime > 0) offTimeout = (DPMSOffTime - (now - lastDeviceEventTime.milliseconds));#else timeout = (ScreenSaverTime - (now - lastDeviceEventTime.milliseconds));#endif /* DPMSExtension */#ifdef DPMSExtension if (timeout <= 0 && ScreenSaverTime > 0)#else if (timeout <= 0) /* may be forced by AutoResetServer() */#endif /* DPMSExtension */ { INT32 timeSinceSave; timeSinceSave = -timeout; if (timeSinceSave >= timeTilFrob && timeTilFrob >= 0) { ResetOsBuffers(); /* not ideal, but better than nothing */ SaveScreens(SCREEN_SAVER_ON, ScreenSaverActive);#ifdef DPMSExtension if (ScreenSaverInterval > 0 && DPMSPowerLevel == DPMSModeOn)#else if (ScreenSaverInterval)#endif /* DPMSExtension */ /* round up to the next ScreenSaverInterval */ timeTilFrob = ScreenSaverInterval * ((timeSinceSave + ScreenSaverInterval) / ScreenSaverInterval); else timeTilFrob = -1; } timeout = timeTilFrob - timeSinceSave; } else { if (ScreenSaverTime > 0 && timeout > ScreenSaverTime) timeout = ScreenSaverTime; timeTilFrob = 0; }#ifdef DPMSExtension if (DPMSEnabled) { if (standbyTimeout > 0 && (timeout <= 0 || timeout > standbyTimeout)) timeout = standbyTimeout; if (suspendTimeout > 0 && (timeout <= 0 || timeout > suspendTimeout)) timeout = suspendTimeout; if (offTimeout > 0 && (timeout <= 0 || timeout > offTimeout)) timeout = offTimeout; }#endif if (timeout > 0 && (!wt || timeout < (timers->expires - now))) { waittime.tv_sec = timeout / MILLI_PER_SECOND; waittime.tv_usec = (timeout % MILLI_PER_SECOND) * (1000000 / MILLI_PER_SECOND); wt = &waittime; }#ifdef DPMSExtension /* don't bother unless it's switched on */ if (DPMSEnabled) { /* * If this mode's enabled, and if the time's come * and if we're still at a lesser mode, do it now. */ if (DPMSStandbyTime > 0) { if (standbyTimeout <= 0) { if (DPMSPowerLevel < DPMSModeStandby) { DPMSSet(DPMSModeStandby); } } } /* * and ditto. Note that since these modes can have the * same timeouts, they can happen at the same time. */ if (DPMSSuspendTime > 0) { if (suspendTimeout <= 0) { if (DPMSPowerLevel < DPMSModeSuspend) { DPMSSet(DPMSModeSuspend); } } } if (DPMSOffTime > 0) { if (offTimeout <= 0) { if (DPMSPowerLevel < DPMSModeOff) { DPMSSet(DPMSModeOff); } } } }#endif } XFD_COPYSET(&AllSockets, &LastSelectMask); BlockHandler((pointer)&wt, (pointer)&LastSelectMask); if (NewOutputPending) FlushAllOutput();#ifdef XTESTEXT1 /* XXX how does this interact with new write block handling? */ if (playback_on) { wt = &waittime; XTestComputeWaitTime (&waittime); }#endif /* XTESTEXT1 */ /* keep this check close to select() call to minimize race */ if (dispatchException) i = -1; else if (AnyClientsWriteBlocked) { XFD_COPYSET(&ClientsWriteBlocked, &clientsWritable); i = Select (MAXSOCKS, &LastSelectMask, &clientsWritable, NULL, wt); } else i = Select (MAXSOCKS, &LastSelectMask, NULL, NULL, wt); selecterr = errno; WakeupHandler(i, (pointer)&LastSelectMask);#ifdef XTESTEXT1 if (playback_on) { i = XTestProcessInputAction (i, &waittime); }#endif /* XTESTEXT1 */ if (i <= 0) /* An error or timeout occurred */ { if (dispatchException) return 0; FD_ZERO(&clientsWritable); if (i < 0) if (selecterr == EBADF) /* Some client disconnected */ { CheckConnections (); if (! XFD_ANYSET (&AllClients)) return 0; } else if (selecterr == EINVAL) { FatalError("WaitForSomething(): select: errno=%d\n", selecterr); } else if (selecterr != EINTR) { ErrorF("WaitForSomething(): select: errno=%d\n", selecterr); } if (timers) { now = GetTimeInMillis(); while (timers && timers->expires <= now) DoTimer(timers, now, &timers); } if (*checkForInput[0] != *checkForInput[1]) return 0; } else {#ifdef WIN32 fd_set tmp_set;#endif if (AnyClientsWriteBlocked && XFD_ANYSET (&clientsWritable)) { NewOutputPending = TRUE; XFD_ORSET(&OutputPending, &clientsWritable, &OutputPending); XFD_UNSET(&ClientsWriteBlocked, &clientsWritable); if (! XFD_ANYSET(&ClientsWriteBlocked)) AnyClientsWriteBlocked = FALSE; } XFD_ANDSET(&devicesReadable, &LastSelectMask, &EnabledDevices); XFD_ANDSET(&clientsReadable, &LastSelectMask, &AllClients); #ifndef WIN32 if (LastSelectMask.fds_bits[0] & WellKnownConnections.fds_bits[0]) #else XFD_ANDSET(&tmp_set, &LastSelectMask, &WellKnownConnections);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -