sleepuntil.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. *
  3. Copyright 1992, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. *
  21. * Author: Keith Packard, MIT X Consortium
  22. */
  23. /* dixsleep.c - implement millisecond timeouts for X clients */
  24. #ifdef HAVE_DIX_CONFIG_H
  25. #include <dix-config.h>
  26. #endif
  27. #include "sleepuntil.h"
  28. #include <X11/X.h>
  29. #include <X11/Xmd.h>
  30. #include "misc.h"
  31. #include "windowstr.h"
  32. #include "dixstruct.h"
  33. #include "pixmapstr.h"
  34. #include "scrnintstr.h"
  35. typedef struct _Sertafied {
  36. struct _Sertafied *next;
  37. TimeStamp revive;
  38. ClientPtr pClient;
  39. XID id;
  40. void (*notifyFunc) (ClientPtr /* client */ ,
  41. pointer /* closure */
  42. );
  43. pointer closure;
  44. } SertafiedRec, *SertafiedPtr;
  45. static SertafiedPtr pPending;
  46. static RESTYPE SertafiedResType;
  47. static Bool BlockHandlerRegistered;
  48. static int SertafiedGeneration;
  49. static void ClientAwaken(ClientPtr /* client */ ,
  50. pointer /* closure */
  51. );
  52. static int SertafiedDelete(pointer /* value */ ,
  53. XID /* id */
  54. );
  55. static void SertafiedBlockHandler(pointer /* data */ ,
  56. OSTimePtr /* wt */ ,
  57. pointer /* LastSelectMask */
  58. );
  59. static void SertafiedWakeupHandler(pointer /* data */ ,
  60. int /* i */ ,
  61. pointer /* LastSelectMask */
  62. );
  63. _X_EXPORT int
  64. ClientSleepUntil(client, revive, notifyFunc, closure)
  65. ClientPtr client;
  66. TimeStamp *revive;
  67. void (*notifyFunc) (ClientPtr /* client */ ,
  68. pointer /* closure */ );
  69. pointer closure;
  70. {
  71. SertafiedPtr pRequest, pReq, pPrev;
  72. if (SertafiedGeneration != serverGeneration) {
  73. SertafiedResType = CreateNewResourceType(SertafiedDelete);
  74. if (!SertafiedResType)
  75. return FALSE;
  76. SertafiedGeneration = serverGeneration;
  77. BlockHandlerRegistered = FALSE;
  78. }
  79. pRequest = malloc(sizeof(SertafiedRec));
  80. if (!pRequest)
  81. return FALSE;
  82. pRequest->pClient = client;
  83. pRequest->revive = *revive;
  84. pRequest->id = FakeClientID(client->index);
  85. pRequest->closure = closure;
  86. if (!BlockHandlerRegistered) {
  87. if (!RegisterBlockAndWakeupHandlers(SertafiedBlockHandler,
  88. SertafiedWakeupHandler,
  89. (pointer) 0)) {
  90. free(pRequest);
  91. return FALSE;
  92. }
  93. BlockHandlerRegistered = TRUE;
  94. }
  95. pRequest->notifyFunc = 0;
  96. if (!AddResource(pRequest->id, SertafiedResType, (pointer) pRequest))
  97. return FALSE;
  98. if (!notifyFunc)
  99. notifyFunc = ClientAwaken;
  100. pRequest->notifyFunc = notifyFunc;
  101. /* Insert into time-ordered queue, with earliest activation time coming first. */
  102. pPrev = 0;
  103. for (pReq = pPending; pReq; pReq = pReq->next) {
  104. if (CompareTimeStamps(pReq->revive, *revive) == LATER)
  105. break;
  106. pPrev = pReq;
  107. }
  108. if (pPrev)
  109. pPrev->next = pRequest;
  110. else
  111. pPending = pRequest;
  112. pRequest->next = pReq;
  113. IgnoreClient(client);
  114. return TRUE;
  115. }
  116. static void
  117. ClientAwaken(client, closure)
  118. ClientPtr client;
  119. pointer closure;
  120. {
  121. if (!client->clientGone)
  122. AttendClient(client);
  123. }
  124. static int
  125. SertafiedDelete(value, id)
  126. pointer value;
  127. XID id;
  128. {
  129. SertafiedPtr pRequest = (SertafiedPtr) value;
  130. SertafiedPtr pReq, pPrev;
  131. pPrev = 0;
  132. for (pReq = pPending; pReq; pPrev = pReq, pReq = pReq->next)
  133. if (pReq == pRequest) {
  134. if (pPrev)
  135. pPrev->next = pReq->next;
  136. else
  137. pPending = pReq->next;
  138. break;
  139. }
  140. if (pRequest->notifyFunc)
  141. (*pRequest->notifyFunc) (pRequest->pClient, pRequest->closure);
  142. free(pRequest);
  143. return TRUE;
  144. }
  145. static void
  146. SertafiedBlockHandler(data, wt, LastSelectMask)
  147. pointer data; /* unused */
  148. OSTimePtr wt; /* wait time */
  149. pointer LastSelectMask;
  150. {
  151. SertafiedPtr pReq, pNext;
  152. unsigned long delay;
  153. TimeStamp now;
  154. if (!pPending)
  155. return;
  156. now.milliseconds = GetTimeInMillis();
  157. now.months = currentTime.months;
  158. if ((int) (now.milliseconds - currentTime.milliseconds) < 0)
  159. now.months++;
  160. for (pReq = pPending; pReq; pReq = pNext) {
  161. pNext = pReq->next;
  162. if (CompareTimeStamps(pReq->revive, now) == LATER)
  163. break;
  164. FreeResource(pReq->id, RT_NONE);
  165. /* AttendClient() may have been called via the resource delete
  166. * function so a client may have input to be processed and so
  167. * set delay to 0 to prevent blocking in WaitForSomething().
  168. */
  169. AdjustWaitForDelay(wt, 0);
  170. }
  171. pReq = pPending;
  172. if (!pReq)
  173. return;
  174. delay = pReq->revive.milliseconds - now.milliseconds;
  175. AdjustWaitForDelay(wt, delay);
  176. }
  177. static void
  178. SertafiedWakeupHandler(data, i, LastSelectMask)
  179. pointer data;
  180. int i;
  181. pointer LastSelectMask;
  182. {
  183. SertafiedPtr pReq, pNext;
  184. TimeStamp now;
  185. now.milliseconds = GetTimeInMillis();
  186. now.months = currentTime.months;
  187. if ((int) (now.milliseconds - currentTime.milliseconds) < 0)
  188. now.months++;
  189. for (pReq = pPending; pReq; pReq = pNext) {
  190. pNext = pReq->next;
  191. if (CompareTimeStamps(pReq->revive, now) == LATER)
  192. break;
  193. FreeResource(pReq->id, RT_NONE);
  194. }
  195. if (!pPending) {
  196. RemoveBlockAndWakeupHandlers(SertafiedBlockHandler,
  197. SertafiedWakeupHandler, (pointer) 0);
  198. BlockHandlerRegistered = FALSE;
  199. }
  200. }