WaitFor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /***********************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ******************************************************************/
  36. /*****************************************************************
  37. * OS Dependent input routines:
  38. *
  39. * WaitForSomething
  40. * TimerForce, TimerSet, TimerFree
  41. *
  42. *****************************************************************/
  43. #ifdef HAVE_DIX_CONFIG_H
  44. #include <dix-config.h>
  45. #endif
  46. #include <X11/Xos.h> /* for strings, fcntl, time */
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <X11/X.h>
  50. #include "misc.h"
  51. #include "osdep.h"
  52. #include <X11/Xpoll.h>
  53. #include "dixstruct.h"
  54. #include "opaque.h"
  55. #ifdef DPMSExtension
  56. #include "dpmsproc.h"
  57. #endif
  58. /* This is just a fallback to errno to hide the differences between unix and
  59. Windows in the code */
  60. #define GetErrno() errno
  61. /* modifications by raphael */
  62. int
  63. mffs(fd_mask mask)
  64. {
  65. int i;
  66. if (!mask) return 0;
  67. i = 1;
  68. while (!(mask & 1))
  69. {
  70. i++;
  71. mask >>= 1;
  72. }
  73. return i;
  74. }
  75. #ifdef DPMSExtension
  76. #define DPMS_SERVER
  77. #include <X11/extensions/dpms.h>
  78. #endif
  79. struct _OsTimerRec {
  80. OsTimerPtr next;
  81. CARD32 expires;
  82. CARD32 delta;
  83. OsTimerCallback callback;
  84. pointer arg;
  85. };
  86. static void DoTimer(OsTimerPtr timer, CARD32 now, OsTimerPtr *prev);
  87. static void CheckAllTimers(CARD32 now);
  88. static OsTimerPtr timers = NULL;
  89. /*****************
  90. * WaitForSomething:
  91. * Make the server suspend until there is
  92. * 1. data from clients or
  93. * 2. input events available or
  94. * 3. ddx notices something of interest (graphics
  95. * queue ready, etc.) or
  96. * 4. clients that have buffered replies/events are ready
  97. *
  98. * If the time between INPUT events is
  99. * greater than ScreenSaverTime, the display is turned off (or
  100. * saved, depending on the hardware). So, WaitForSomething()
  101. * has to handle this also (that's why the select() has a timeout.
  102. * For more info on ClientsWithInput, see ReadRequestFromClient().
  103. * pClientsReady is an array to store ready client->index values into.
  104. *****************/
  105. int
  106. WaitForSomething(int *pClientsReady)
  107. {
  108. int i;
  109. struct timeval waittime, *wt;
  110. INT32 timeout = 0;
  111. fd_set clientsReadable;
  112. fd_set clientsWritable;
  113. int curclient;
  114. int selecterr;
  115. int nready;
  116. fd_set devicesReadable;
  117. CARD32 now = 0;
  118. #ifdef SMART_SCHEDULE
  119. Bool someReady = FALSE;
  120. #endif
  121. FD_ZERO(&clientsReadable);
  122. /* We need a while loop here to handle
  123. crashed connections and the screen saver timeout */
  124. while (1)
  125. {
  126. /* deal with any blocked jobs */
  127. if (workQueue)
  128. ProcessWorkQueue();
  129. if (XFD_ANYSET (&ClientsWithInput))
  130. {
  131. #ifdef SMART_SCHEDULE
  132. if (!SmartScheduleDisable)
  133. {
  134. someReady = TRUE;
  135. waittime.tv_sec = 0;
  136. waittime.tv_usec = 0;
  137. wt = &waittime;
  138. }
  139. else
  140. #endif
  141. {
  142. XFD_COPYSET (&ClientsWithInput, &clientsReadable);
  143. break;
  144. }
  145. }
  146. #ifdef SMART_SCHEDULE
  147. if (someReady)
  148. {
  149. XFD_COPYSET(&AllSockets, &LastSelectMask);
  150. XFD_UNSET(&LastSelectMask, &ClientsWithInput);
  151. }
  152. else
  153. {
  154. #endif
  155. wt = NULL;
  156. if (timers)
  157. {
  158. now = GetTimeInMillis();
  159. timeout = timers->expires - now;
  160. if (timeout > 0 && timeout > timers->delta + 250) {
  161. /* time has rewound. reset the timers. */
  162. CheckAllTimers(now);
  163. }
  164. if (timers) {
  165. timeout = timers->expires - now;
  166. if (timeout < 0)
  167. timeout = 0;
  168. waittime.tv_sec = timeout / MILLI_PER_SECOND;
  169. waittime.tv_usec = (timeout % MILLI_PER_SECOND) *
  170. (1000000 / MILLI_PER_SECOND);
  171. wt = &waittime;
  172. }
  173. }
  174. XFD_COPYSET(&AllSockets, &LastSelectMask);
  175. #ifdef SMART_SCHEDULE
  176. }
  177. SmartScheduleIdle = TRUE;
  178. #endif
  179. BlockHandler((pointer)&wt, (pointer)&LastSelectMask);
  180. if (NewOutputPending)
  181. FlushAllOutput();
  182. /* keep this check close to select() call to minimize race */
  183. if (dispatchException)
  184. i = -1;
  185. else if (AnyClientsWriteBlocked)
  186. {
  187. XFD_COPYSET(&ClientsWriteBlocked, &clientsWritable);
  188. i = Select (MaxClients, &LastSelectMask, &clientsWritable, NULL, wt);
  189. }
  190. else
  191. {
  192. i = Select (MaxClients, &LastSelectMask, NULL, NULL, wt);
  193. }
  194. selecterr = GetErrno();
  195. WakeupHandler(i, (pointer)&LastSelectMask);
  196. #ifdef SMART_SCHEDULE
  197. if (i >= 0)
  198. {
  199. SmartScheduleIdle = FALSE;
  200. SmartScheduleIdleCount = 0;
  201. if (SmartScheduleTimerStopped)
  202. (void) SmartScheduleStartTimer ();
  203. }
  204. #endif
  205. if (i <= 0) /* An error or timeout occurred */
  206. {
  207. if (dispatchException)
  208. return 0;
  209. if (i < 0)
  210. {
  211. if (selecterr == EBADF) /* Some client disconnected */
  212. {
  213. CheckConnections ();
  214. if (! XFD_ANYSET (&AllClients))
  215. return 0;
  216. }
  217. else if (selecterr == EINVAL)
  218. {
  219. FatalError("WaitForSomething(): select: errno=%d\n",
  220. selecterr);
  221. }
  222. else if (selecterr != EINTR)
  223. {
  224. ErrorF("WaitForSomething(): select: errno=%d\n",
  225. selecterr);
  226. }
  227. }
  228. #ifdef SMART_SCHEDULE
  229. else if (someReady)
  230. {
  231. /*
  232. * If no-one else is home, bail quickly
  233. */
  234. XFD_COPYSET(&ClientsWithInput, &LastSelectMask);
  235. XFD_COPYSET(&ClientsWithInput, &clientsReadable);
  236. break;
  237. }
  238. #endif
  239. if (*checkForInput[0] != *checkForInput[1])
  240. return 0;
  241. if (timers)
  242. {
  243. int expired = 0;
  244. now = GetTimeInMillis();
  245. if ((int) (timers->expires - now) <= 0)
  246. expired = 1;
  247. while (timers && (int) (timers->expires - now) <= 0)
  248. DoTimer(timers, now, &timers);
  249. if (expired)
  250. return 0;
  251. }
  252. }
  253. else
  254. {
  255. fd_set tmp_set;
  256. if (*checkForInput[0] == *checkForInput[1]) {
  257. if (timers)
  258. {
  259. int expired = 0;
  260. now = GetTimeInMillis();
  261. if ((int) (timers->expires - now) <= 0)
  262. expired = 1;
  263. while (timers && (int) (timers->expires - now) <= 0)
  264. DoTimer(timers, now, &timers);
  265. if (expired)
  266. return 0;
  267. }
  268. }
  269. #ifdef SMART_SCHEDULE
  270. if (someReady)
  271. XFD_ORSET(&LastSelectMask, &ClientsWithInput, &LastSelectMask);
  272. #endif
  273. if (AnyClientsWriteBlocked && XFD_ANYSET (&clientsWritable))
  274. {
  275. NewOutputPending = TRUE;
  276. XFD_ORSET(&OutputPending, &clientsWritable, &OutputPending);
  277. XFD_UNSET(&ClientsWriteBlocked, &clientsWritable);
  278. if (! XFD_ANYSET(&ClientsWriteBlocked))
  279. AnyClientsWriteBlocked = FALSE;
  280. }
  281. XFD_ANDSET(&devicesReadable, &LastSelectMask, &EnabledDevices);
  282. XFD_ANDSET(&clientsReadable, &LastSelectMask, &AllClients);
  283. XFD_ANDSET(&tmp_set, &LastSelectMask, &WellKnownConnections);
  284. if (XFD_ANYSET(&tmp_set))
  285. QueueWorkProc(EstablishNewConnections, NULL,
  286. (pointer)&LastSelectMask);
  287. #ifdef DPMSExtension
  288. if (XFD_ANYSET (&devicesReadable) && (DPMSPowerLevel != DPMSModeOn))
  289. DPMSSet(DPMSModeOn);
  290. #endif
  291. if (XFD_ANYSET (&devicesReadable) || XFD_ANYSET (&clientsReadable))
  292. break;
  293. }
  294. }
  295. nready = 0;
  296. if (XFD_ANYSET (&clientsReadable))
  297. {
  298. for (i=0; i<howmany(XFD_SETSIZE, NFDBITS); i++)
  299. {
  300. int highest_priority = 0;
  301. while (clientsReadable.fds_bits[i])
  302. {
  303. int client_priority, client_index;
  304. curclient = ffs (clientsReadable.fds_bits[i]) - 1;
  305. client_index = /* raphael: modified */
  306. ConnectionTranslation[curclient + (i * (sizeof(fd_mask) * 8))];
  307. /* We implement "strict" priorities.
  308. * Only the highest priority client is returned to
  309. * dix. If multiple clients at the same priority are
  310. * ready, they are all returned. This means that an
  311. * aggressive client could take over the server.
  312. * This was not considered a big problem because
  313. * aggressive clients can hose the server in so many
  314. * other ways :)
  315. */
  316. client_priority = clients[client_index]->priority;
  317. if (nready == 0 || client_priority > highest_priority)
  318. {
  319. /* Either we found the first client, or we found
  320. * a client whose priority is greater than all others
  321. * that have been found so far. Either way, we want
  322. * to initialize the list of clients to contain just
  323. * this client.
  324. */
  325. pClientsReady[0] = client_index;
  326. highest_priority = client_priority;
  327. nready = 1;
  328. }
  329. /* the following if makes sure that multiple same-priority
  330. * clients get batched together
  331. */
  332. else if (client_priority == highest_priority)
  333. {
  334. pClientsReady[nready++] = client_index;
  335. }
  336. clientsReadable.fds_bits[i] &= ~(((fd_mask)1L) << curclient);
  337. }
  338. }
  339. }
  340. return nready;
  341. }
  342. #if 0
  343. /*
  344. * This is not always a macro.
  345. */
  346. ANYSET(FdMask *src)
  347. {
  348. int i;
  349. for (i=0; i<mskcnt; i++)
  350. if (src[ i ])
  351. return (TRUE);
  352. return (FALSE);
  353. }
  354. #endif
  355. /* If time has rewound, re-run every affected timer.
  356. * Timers might drop out of the list, so we have to restart every time. */
  357. static void
  358. CheckAllTimers(CARD32 now)
  359. {
  360. OsTimerPtr timer;
  361. start:
  362. for (timer = timers; timer; timer = timer->next) {
  363. if (timer->expires - now > timer->delta + 250) {
  364. TimerForce(timer);
  365. goto start;
  366. }
  367. }
  368. }
  369. static void
  370. DoTimer(OsTimerPtr timer, CARD32 now, OsTimerPtr *prev)
  371. {
  372. CARD32 newTime;
  373. *prev = timer->next;
  374. timer->next = NULL;
  375. newTime = (*timer->callback)(timer, now, timer->arg);
  376. if (newTime)
  377. TimerSet(timer, 0, newTime, timer->callback, timer->arg);
  378. }
  379. _X_EXPORT OsTimerPtr
  380. TimerSet(OsTimerPtr timer, int flags, CARD32 millis,
  381. OsTimerCallback func, pointer arg)
  382. {
  383. OsTimerPtr *prev;
  384. CARD32 now = GetTimeInMillis();
  385. if (!timer)
  386. {
  387. timer = (OsTimerPtr)malloc(sizeof(struct _OsTimerRec));
  388. if (!timer)
  389. return NULL;
  390. }
  391. else
  392. {
  393. for (prev = &timers; *prev; prev = &(*prev)->next)
  394. {
  395. if (*prev == timer)
  396. {
  397. *prev = timer->next;
  398. if (flags & TimerForceOld)
  399. (void)(*timer->callback)(timer, now, timer->arg);
  400. break;
  401. }
  402. }
  403. }
  404. if (!millis)
  405. return timer;
  406. if (flags & TimerAbsolute) {
  407. timer->delta = millis - now;
  408. }
  409. else {
  410. timer->delta = millis;
  411. millis += now;
  412. }
  413. timer->expires = millis;
  414. timer->callback = func;
  415. timer->arg = arg;
  416. if ((int) (millis - now) <= 0)
  417. {
  418. timer->next = NULL;
  419. millis = (*timer->callback)(timer, now, timer->arg);
  420. if (!millis)
  421. return timer;
  422. }
  423. for (prev = &timers;
  424. *prev && (int) ((*prev)->expires - millis) <= 0;
  425. prev = &(*prev)->next)
  426. ;
  427. timer->next = *prev;
  428. *prev = timer;
  429. return timer;
  430. }
  431. Bool
  432. TimerForce(OsTimerPtr timer)
  433. {
  434. OsTimerPtr *prev;
  435. for (prev = &timers; *prev; prev = &(*prev)->next)
  436. {
  437. if (*prev == timer)
  438. {
  439. DoTimer(timer, GetTimeInMillis(), prev);
  440. return TRUE;
  441. }
  442. }
  443. return FALSE;
  444. }
  445. _X_EXPORT void
  446. TimerCancel(OsTimerPtr timer)
  447. {
  448. OsTimerPtr *prev;
  449. if (!timer)
  450. return;
  451. for (prev = &timers; *prev; prev = &(*prev)->next)
  452. {
  453. if (*prev == timer)
  454. {
  455. *prev = timer->next;
  456. break;
  457. }
  458. }
  459. }
  460. _X_EXPORT void
  461. TimerFree(OsTimerPtr timer)
  462. {
  463. if (!timer)
  464. return;
  465. TimerCancel(timer);
  466. free(timer);
  467. }
  468. void
  469. TimerInit(void)
  470. {
  471. OsTimerPtr timer;
  472. while ((timer = timers))
  473. {
  474. timers = timer->next;
  475. free(timer);
  476. }
  477. }
  478. #ifdef DPMSExtension
  479. #define DPMS_CHECK_MODE(mode,time)\
  480. if (time > 0 && DPMSPowerLevel < mode && timeout >= time)\
  481. DPMSSet(mode);
  482. #define DPMS_CHECK_TIMEOUT(time)\
  483. if (time > 0 && (time - timeout) > 0)\
  484. return time - timeout;
  485. static CARD32
  486. NextDPMSTimeout(INT32 timeout)
  487. {
  488. /*
  489. * Return the amount of time remaining until we should set
  490. * the next power level. Fallthroughs are intentional.
  491. */
  492. switch (DPMSPowerLevel)
  493. {
  494. case DPMSModeOn:
  495. DPMS_CHECK_TIMEOUT(DPMSStandbyTime)
  496. case DPMSModeStandby:
  497. DPMS_CHECK_TIMEOUT(DPMSSuspendTime)
  498. case DPMSModeSuspend:
  499. DPMS_CHECK_TIMEOUT(DPMSOffTime)
  500. default: /* DPMSModeOff */
  501. return 0;
  502. }
  503. }
  504. #endif /* DPMSExtension */
  505. static CARD32
  506. ScreenSaverTimeoutExpire(OsTimerPtr timer,CARD32 now,pointer arg)
  507. {
  508. INT32 timeout = now - lastDeviceEventTime.milliseconds;
  509. CARD32 nextTimeout = 0;
  510. #ifdef DPMSExtension
  511. /*
  512. * Check each mode lowest to highest, since a lower mode can
  513. * have the same timeout as a higher one.
  514. */
  515. if (DPMSEnabled)
  516. {
  517. DPMS_CHECK_MODE(DPMSModeOff, DPMSOffTime)
  518. DPMS_CHECK_MODE(DPMSModeSuspend, DPMSSuspendTime)
  519. DPMS_CHECK_MODE(DPMSModeStandby, DPMSStandbyTime)
  520. nextTimeout = NextDPMSTimeout(timeout);
  521. }
  522. /*
  523. * Only do the screensaver checks if we're not in a DPMS
  524. * power saving mode
  525. */
  526. if (DPMSPowerLevel != DPMSModeOn)
  527. return nextTimeout;
  528. #endif /* DPMSExtension */
  529. if (!ScreenSaverTime)
  530. return nextTimeout;
  531. if (timeout < ScreenSaverTime)
  532. {
  533. return nextTimeout > 0 ?
  534. min(ScreenSaverTime - timeout, nextTimeout) :
  535. ScreenSaverTime - timeout;
  536. }
  537. ResetOsBuffers(); /* not ideal, but better than nothing */
  538. SaveScreens(SCREEN_SAVER_ON, ScreenSaverActive);
  539. if (ScreenSaverInterval > 0)
  540. {
  541. nextTimeout = nextTimeout > 0 ?
  542. min(ScreenSaverInterval, nextTimeout) :
  543. ScreenSaverInterval;
  544. }
  545. return nextTimeout;
  546. }
  547. static OsTimerPtr ScreenSaverTimer = NULL;
  548. void
  549. FreeScreenSaverTimer(void)
  550. {
  551. if (ScreenSaverTimer) {
  552. TimerFree(ScreenSaverTimer);
  553. ScreenSaverTimer = NULL;
  554. }
  555. }
  556. void
  557. SetScreenSaverTimer(void)
  558. {
  559. CARD32 timeout = 0;
  560. #ifdef DPMSExtension
  561. if (DPMSEnabled)
  562. {
  563. /*
  564. * A higher DPMS level has a timeout that's either less
  565. * than or equal to that of a lower DPMS level.
  566. */
  567. if (DPMSStandbyTime > 0)
  568. timeout = DPMSStandbyTime;
  569. else if (DPMSSuspendTime > 0)
  570. timeout = DPMSSuspendTime;
  571. else if (DPMSOffTime > 0)
  572. timeout = DPMSOffTime;
  573. }
  574. #endif
  575. if (ScreenSaverTime > 0)
  576. {
  577. timeout = timeout > 0 ?
  578. min(ScreenSaverTime, timeout) :
  579. ScreenSaverTime;
  580. }
  581. #ifdef SCREENSAVER
  582. if (timeout && !screenSaverSuspended) {
  583. #else
  584. if (timeout) {
  585. #endif
  586. ScreenSaverTimer = TimerSet(ScreenSaverTimer, 0, timeout,
  587. ScreenSaverTimeoutExpire, NULL);
  588. }
  589. else if (ScreenSaverTimer) {
  590. FreeScreenSaverTimer();
  591. }
  592. }