3 * WINGs internal handlers: timer, idle and input handlers
8 #include "../src/config.h"
10 #include <sys/types.h>
15 #ifdef HAVE_SYS_SELECT_H
16 # include <sys/select.h>
21 #ifndef X_GETTIMEOFDAY
22 #define X_GETTIMEOFDAY(t) gettimeofday(t, (struct timezone*)0)
28 typedef struct TimerHandler
{
29 WMCallback
*callback
; /* procedure to call */
30 struct timeval when
; /* when to call the callback */
32 struct TimerHandler
*next
;
33 int nextDelay
; /* 0 if it's one-shot */
37 typedef struct IdleHandler
{
43 typedef struct InputHandler
{
44 WMInputProc
*callback
;
51 /* queue of timer event handlers */
52 static TimerHandler
*timerHandler
=NULL
;
54 static WMArray
*idleHandler
=NULL
;
56 static WMArray
*inputHandler
=NULL
;
58 #define timerPending() (timerHandler)
63 rightNow(struct timeval
*tv
) {
67 /* is t1 after t2 ? */
68 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
69 (((t1).tv_sec == (t2).tv_sec) \
70 && ((t1).tv_usec > (t2).tv_usec)))
72 #define IS_ZERO(tv) (tv.tv_sec == 0 && tv.tv_usec == 0)
74 #define SET_ZERO(tv) tv.tv_sec = 0, tv.tv_usec = 0
77 addmillisecs(struct timeval
*tv
, int milliseconds
)
79 tv
->tv_usec
+= milliseconds
*1000;
81 tv
->tv_sec
+= tv
->tv_usec
/1000000;
82 tv
->tv_usec
= tv
->tv_usec
%1000000;
87 enqueueTimerHandler(TimerHandler
*handler
)
91 /* insert callback in queue, sorted by time left */
92 if (!timerHandler
|| !IS_AFTER(handler
->when
, timerHandler
->when
)) {
93 /* first in the queue */
94 handler
->next
= timerHandler
;
95 timerHandler
= handler
;
98 while (tmp
->next
&& IS_AFTER(handler
->when
, tmp
->next
->when
)) {
101 handler
->next
= tmp
->next
;
108 delayUntilNextTimerEvent(struct timeval
*delay
)
111 TimerHandler
*handler
;
113 handler
= timerHandler
;
114 while (handler
&& IS_ZERO(handler
->when
)) handler
= handler
->next
;
117 /* The return value of this function is only valid if there _are_
125 if (IS_AFTER(now
, handler
->when
)) {
129 delay
->tv_sec
= handler
->when
.tv_sec
- now
.tv_sec
;
130 delay
->tv_usec
= handler
->when
.tv_usec
- now
.tv_usec
;
131 if (delay
->tv_usec
< 0) {
132 delay
->tv_usec
+= 1000000;
140 WMAddTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
142 TimerHandler
*handler
;
144 handler
= malloc(sizeof(TimerHandler
));
148 rightNow(&handler
->when
);
149 addmillisecs(&handler
->when
, milliseconds
);
150 handler
->callback
= callback
;
151 handler
->clientData
= cdata
;
152 handler
->nextDelay
= 0;
154 enqueueTimerHandler(handler
);
161 WMAddPersistentTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
163 TimerHandler
*handler
= WMAddTimerHandler(milliseconds
, callback
, cdata
);
166 handler
->nextDelay
= milliseconds
;
174 WMDeleteTimerWithClientData(void *cdata
)
176 TimerHandler
*handler
, *tmp
;
178 if (!cdata
|| !timerHandler
)
182 if (tmp
->clientData
==cdata
) {
184 if (!IS_ZERO(tmp
->when
)) {
185 timerHandler
= tmp
->next
;
190 if (tmp
->next
->clientData
==cdata
) {
192 handler
->nextDelay
= 0;
193 if (IS_ZERO(handler
->when
))
195 tmp
->next
= handler
->next
;
207 WMDeleteTimerHandler(WMHandlerID handlerID
)
209 TimerHandler
*tmp
, *handler
=(TimerHandler
*)handlerID
;
211 if (!handler
|| !timerHandler
)
216 handler
->nextDelay
= 0;
218 if (IS_ZERO(handler
->when
))
222 timerHandler
= handler
->next
;
226 if (tmp
->next
==handler
) {
227 tmp
->next
=handler
->next
;
239 WMAddIdleHandler(WMCallback
*callback
, void *cdata
)
241 IdleHandler
*handler
;
243 handler
= malloc(sizeof(IdleHandler
));
247 handler
->callback
= callback
;
248 handler
->clientData
= cdata
;
249 /* add handler at end of queue */
251 idleHandler
= WMCreateArrayWithDestructor(16, wfree
);
253 WMAddToArray(idleHandler
, handler
);
260 WMDeleteIdleHandler(WMHandlerID handlerID
)
262 IdleHandler
*handler
= (IdleHandler
*)handlerID
;
264 if (!handler
|| !idleHandler
)
267 WMRemoveFromArray(idleHandler
, handler
);
273 WMAddInputHandler(int fd
, int condition
, WMInputProc
*proc
, void *clientData
)
275 InputHandler
*handler
;
277 handler
= wmalloc(sizeof(InputHandler
));
280 handler
->mask
= condition
;
281 handler
->callback
= proc
;
282 handler
->clientData
= clientData
;
285 inputHandler
= WMCreateArrayWithDestructor(16, wfree
);
286 WMAddToArray(inputHandler
, handler
);
294 WMDeleteInputHandler(WMHandlerID handlerID
)
296 InputHandler
*handler
= (InputHandler
*)handlerID
;
298 if (!handler
|| !inputHandler
)
301 WMRemoveFromArray(inputHandler
, handler
);
306 W_CheckIdleHandlers(void)
308 IdleHandler
*handler
;
309 WMArray
*handlerCopy
;
310 WMArrayIterator iter
;
312 if (!idleHandler
|| WMGetArrayItemCount(idleHandler
)==0) {
313 W_FlushIdleNotificationQueue();
314 /* make sure an observer in queue didn't added an idle handler */
315 return (idleHandler
!=NULL
&& WMGetArrayItemCount(idleHandler
)>0);
318 handlerCopy
= WMDuplicateArray(idleHandler
);
320 WM_ITERATE_ARRAY(handlerCopy
, handler
, iter
) {
321 /* check if the handler still exist or was removed by a callback */
322 if (WMGetFirstInArray(idleHandler
, handler
) == WANotFound
)
325 (*handler
->callback
)(handler
->clientData
);
326 WMDeleteIdleHandler(handler
);
329 WMFreeArray(handlerCopy
);
331 W_FlushIdleNotificationQueue();
333 /* this is not necesarrily False, because one handler can re-add itself */
334 return (WMGetArrayItemCount(idleHandler
)>0);
340 W_CheckTimerHandlers(void)
342 TimerHandler
*handler
;
346 W_FlushASAPNotificationQueue();
352 handler
= timerHandler
;
353 while (handler
&& IS_AFTER(now
, handler
->when
)) {
354 if (!IS_ZERO(handler
->when
)) {
355 SET_ZERO(handler
->when
);
356 (*handler
->callback
)(handler
->clientData
);
358 handler
= handler
->next
;
361 while (timerHandler
&& IS_ZERO(timerHandler
->when
)) {
362 handler
= timerHandler
;
363 timerHandler
= timerHandler
->next
;
365 if (handler
->nextDelay
> 0) {
367 addmillisecs(&handler
->when
, handler
->nextDelay
);
368 enqueueTimerHandler(handler
);
374 W_FlushASAPNotificationQueue();
379 * This functions will handle input events on all registered file descriptors.
381 * - waitForInput - True if we want the function to wait until an event
382 * appears on a file descriptor we watch, False if we
383 * want the function to immediately return if there is
384 * no data available on the file descriptors we watch.
385 * - inputfd - Extra input file descriptor to watch for input.
386 * This is only used when called from wevent.c to watch
387 * on ConnectionNumber(dpy) to avoid blocking of X events
388 * if we wait for input from other file handlers.
390 * if waitForInput is False, the function will return False if there are no
391 * input handlers registered, or if there is no data
392 * available on the registered ones, and will return True
393 * if there is at least one input handler that has data
395 * if waitForInput is True, the function will return False if there are no
396 * input handlers registered, else it will block until an
397 * event appears on one of the file descriptors it watches
398 * and then it will return True.
400 * If the retured value is True, the input handlers for the corresponding file
401 * descriptors are also called.
403 * Parametersshould be passed like this:
405 * waitForInput - apropriate value passed by the function who called us
406 * inputfd = ConnectionNumber(dpy)
408 * waitForInput - apropriate value passed by the function who called us
413 W_HandleInputEvents(Bool waitForInput
, int inputfd
)
415 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
417 InputHandler
*handler
;
418 int count
, timeout
, nfds
, i
, extrafd
;
420 extrafd
= (inputfd
< 0) ? 0 : 1;
423 nfds
= WMGetArrayItemCount(inputHandler
);
427 if (!extrafd
&& nfds
==0) {
428 W_FlushASAPNotificationQueue();
432 fds
= wmalloc((nfds
+extrafd
) * sizeof(struct pollfd
));
434 /* put this to the end of array to avoid using ranges from 1 to nfds+1 */
435 fds
[nfds
].fd
= inputfd
;
436 fds
[nfds
].events
= POLLIN
;
439 /* use WM_ITERATE_ARRAY() here */
440 for (i
= 0; i
<nfds
; i
++) {
441 handler
= WMGetFromArray(inputHandler
, i
);
442 fds
[i
].fd
= handler
->fd
;
444 if (handler
->mask
& WIReadMask
)
445 fds
[i
].events
|= POLLIN
;
447 if (handler
->mask
& WIWriteMask
)
448 fds
[i
].events
|= POLLOUT
;
451 if (handler
->mask
& WIExceptMask
)
452 FD_SET(handler
->fd
, &eset
);
457 * Setup the timeout to the estimated time until the
458 * next timer expires.
462 } else if (timerPending()) {
464 delayUntilNextTimerEvent(&tv
);
465 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
470 count
= poll(fds
, nfds
+extrafd
, timeout
);
472 if (count
>0 && nfds
>0) {
473 WMArray
*handlerCopy
= WMDuplicateArray(inputHandler
);
476 /* use WM_ITERATE_ARRAY() here */
477 for (i
=0; i
<nfds
; i
++) {
478 handler
= WMGetFromArray(handlerCopy
, i
);
479 /* check if the handler still exist or was removed by a callback */
480 if (WMGetFirstInArray(inputHandler
, handler
) == WANotFound
)
485 if ((handler
->mask
& WIReadMask
) &&
486 (fds
[i
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
)))
489 if ((handler
->mask
& WIWriteMask
) &&
490 (fds
[i
].revents
& (POLLOUT
| POLLWRBAND
)))
493 if ((handler
->mask
& WIExceptMask
) &&
494 (fds
[i
].revents
& (POLLHUP
| POLLNVAL
| POLLERR
)))
495 mask
|= WIExceptMask
;
497 if (mask
!=0 && handler
->callback
) {
498 (*handler
->callback
)(handler
->fd
, mask
,
499 handler
->clientData
);
503 WMFreeArray(handlerCopy
);
508 W_FlushASAPNotificationQueue();
513 struct timeval timeout
;
514 struct timeval
*timeoutPtr
;
515 fd_set rset
, wset
, eset
;
518 InputHandler
*handler
;
521 nfds
= WMGetArrayItemCount(inputHandler
);
525 if (inputfd
<0 && nfds
==0) {
526 W_FlushASAPNotificationQueue();
537 FD_SET(inputfd
, &rset
);
541 /* use WM_ITERATE_ARRAY() here */
542 for (i
=0; i
<nfds
; i
++) {
543 handler
= WMGetFromArray(inputHandler
, i
);
544 if (handler
->mask
& WIReadMask
)
545 FD_SET(handler
->fd
, &rset
);
547 if (handler
->mask
& WIWriteMask
)
548 FD_SET(handler
->fd
, &wset
);
550 if (handler
->mask
& WIExceptMask
)
551 FD_SET(handler
->fd
, &eset
);
553 if (maxfd
< handler
->fd
)
558 * Setup the timeout to the estimated time until the
559 * next timer expires.
563 timeoutPtr
= &timeout
;
564 } else if (timerPending()) {
565 delayUntilNextTimerEvent(&timeout
);
566 timeoutPtr
= &timeout
;
568 timeoutPtr
= (struct timeval
*)0;
571 count
= select(1 + maxfd
, &rset
, &wset
, &eset
, timeoutPtr
);
573 if (count
>0 && nfds
>0) {
574 WMArray
*handlerCopy
= WMDuplicateArray(inputHandler
);
577 /* use WM_ITERATE_ARRAY() here */
578 for (i
=0; i
<nfds
; i
++) {
579 handler
= WMGetFromArray(handlerCopy
, i
);
580 /* check if the handler still exist or was removed by a callback */
581 if (WMGetFirstInArray(inputHandler
, handler
) == WANotFound
)
586 if ((handler
->mask
& WIReadMask
) && FD_ISSET(handler
->fd
, &rset
))
589 if ((handler
->mask
& WIWriteMask
) && FD_ISSET(handler
->fd
, &wset
))
592 if ((handler
->mask
& WIExceptMask
) && FD_ISSET(handler
->fd
, &eset
))
593 mask
|= WIExceptMask
;
595 if (mask
!=0 && handler
->callback
) {
596 (*handler
->callback
)(handler
->fd
, mask
,
597 handler
->clientData
);
601 WMFreeArray(handlerCopy
);
604 W_FlushASAPNotificationQueue();
607 #else /* not HAVE_SELECT, not HAVE_POLL */
608 Neither select nor poll
. You lose
.
609 #endif /* HAVE_SELECT */
610 #endif /* HAVE_POLL */