4 * This event handling stuff was based on Tk.
5 * adapted from wevent.c
10 #include "../src/config.h"
12 #include <sys/types.h>
22 #ifdef HAVE_SYS_SELECT_H
23 # include <sys/select.h>
28 #ifndef X_GETTIMEOFDAY
29 #define X_GETTIMEOFDAY(t) gettimeofday(t, (struct timezone*)0)
35 typedef struct TimerHandler
{
36 WMCallback
*callback
; /* procedure to call */
37 struct timeval when
; /* when to call the callback */
39 struct TimerHandler
*next
;
43 typedef struct IdleHandler
{
46 struct IdleHandler
*next
;
50 typedef struct InputHandler
{
51 WMInputProc
*callback
;
55 struct InputHandler
*next
;
59 /* queue of timer event handlers */
60 static TimerHandler
*timerHandler
=NULL
;
62 static IdleHandler
*idleHandler
=NULL
;
64 static InputHandler
*inputHandler
=NULL
;
68 #define timerPending() (timerHandler)
70 #define idlePending() (idleHandler)
74 rightNow(struct timeval
*tv
) {
78 /* is t1 after t2 ? */
79 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
80 (((t1).tv_sec == (t2).tv_sec) \
81 && ((t1).tv_usec > (t2).tv_usec)))
85 addmillisecs(struct timeval
*tv
, int milliseconds
)
87 tv
->tv_usec
+= milliseconds
*1000;
89 tv
->tv_sec
+= tv
->tv_usec
/1000000;
90 tv
->tv_usec
= tv
->tv_usec
%1000000;
95 WMAddTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
97 TimerHandler
*handler
, *tmp
;
99 handler
= malloc(sizeof(TimerHandler
));
103 rightNow(&handler
->when
);
104 addmillisecs(&handler
->when
, milliseconds
);
105 handler
->callback
= callback
;
106 handler
->clientData
= cdata
;
107 /* insert callback in queue, sorted by time left */
108 if (!timerHandler
|| !IS_AFTER(handler
->when
, timerHandler
->when
)) {
109 /* first in the queue */
110 handler
->next
= timerHandler
;
111 timerHandler
= handler
;
114 while (tmp
->next
&& IS_AFTER(handler
->when
, tmp
->next
->when
)) {
117 handler
->next
= tmp
->next
;
126 WMDeleteTimerWithClientData(void *cdata
)
128 TimerHandler
*handler
, *tmp
;
130 if (!cdata
|| !timerHandler
)
134 if (tmp
->clientData
==cdata
) {
135 timerHandler
= tmp
->next
;
139 if (tmp
->next
->clientData
==cdata
) {
141 tmp
->next
= handler
->next
;
153 WMDeleteTimerHandler(WMHandlerID handlerID
)
155 TimerHandler
*tmp
, *handler
=(TimerHandler
*)handlerID
;
157 if (!handler
|| !timerHandler
)
162 timerHandler
= handler
->next
;
166 if (tmp
->next
==handler
) {
167 tmp
->next
=handler
->next
;
179 WMAddIdleHandler(WMCallback
*callback
, void *cdata
)
181 IdleHandler
*handler
, *tmp
;
183 handler
= malloc(sizeof(IdleHandler
));
187 handler
->callback
= callback
;
188 handler
->clientData
= cdata
;
189 handler
->next
= NULL
;
190 /* add callback at end of queue */
192 idleHandler
= handler
;
207 WMDeleteIdleHandler(WMHandlerID handlerID
)
209 IdleHandler
*tmp
, *handler
= (IdleHandler
*)handlerID
;
211 if (!handler
|| !idleHandler
)
215 if (tmp
== handler
) {
216 idleHandler
= handler
->next
;
220 if (tmp
->next
== handler
) {
221 tmp
->next
= handler
->next
;
233 WMAddInputHandler(int fd
, int condition
, WMInputProc
*proc
, void *clientData
)
235 InputHandler
*handler
;
237 handler
= wmalloc(sizeof(InputHandler
));
240 handler
->mask
= condition
;
241 handler
->callback
= proc
;
242 handler
->clientData
= clientData
;
244 handler
->next
= inputHandler
;
246 inputHandler
= handler
;
253 WMDeleteInputHandler(WMHandlerID handlerID
)
255 InputHandler
*tmp
, *handler
= (InputHandler
*)handlerID
;
257 if (!handler
|| !inputHandler
)
261 if (tmp
== handler
) {
262 inputHandler
= handler
->next
;
266 if (tmp
->next
== handler
) {
267 tmp
->next
= handler
->next
;
280 IdleHandler
*handler
, *tmp
;
283 W_FlushIdleNotificationQueue();
287 handler
= idleHandler
;
289 /* we will process all idleHandlers so, empty the handler list */
294 (*handler
->callback
)(handler
->clientData
);
295 /* remove the handler */
300 W_FlushIdleNotificationQueue();
308 TimerHandler
*handler
;
313 while (timerHandler
&& IS_AFTER(now
, timerHandler
->when
)) {
314 handler
= timerHandler
;
315 timerHandler
= timerHandler
->next
;
316 handler
->next
= NULL
;
317 (*handler
->callback
)(handler
->clientData
);
321 W_FlushASAPNotificationQueue();
327 delayUntilNextTimerEvent(struct timeval
*delay
)
332 /* The return value of this function is only valid if there _are_
340 if (IS_AFTER(now
, timerHandler
->when
)) {
344 delay
->tv_sec
= timerHandler
->when
.tv_sec
- now
.tv_sec
;
345 delay
->tv_usec
= timerHandler
->when
.tv_usec
- now
.tv_usec
;
346 if (delay
->tv_usec
< 0) {
347 delay
->tv_usec
+= 1000000;
355 * This functions will handle input events on all registered file descriptors.
357 * - waitForInput - True if we want the function to wait until an event
358 * appears on a file descriptor we watch, False if we
359 * want the function to immediately return if there is
360 * no data available on the file descriptors we watch.
362 * if waitForInput is False, the function will return False if there are no
363 * input handlers registered, or if there is no data
364 * available on the registered ones, and will return True
365 * if there is at least one input handler that has data
367 * if waitForInput is True, the function will return False if there are no
368 * input handlers registered, else it will block until an
369 * event appears on one of the file descriptors it watches
370 * and then it will return True.
372 * If the retured value is True, the input handlers for the corresponding file
373 * descriptors are also called.
377 handleInputEvents(Bool waitForInput
)
379 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
381 InputHandler
*handler
;
382 int count
, timeout
, nfds
, k
;
387 for (nfds
= 0, handler
= inputHandler
;
388 handler
!= 0; handler
= handler
->next
) nfds
++;
390 fds
= wmalloc(nfds
* sizeof(struct pollfd
));
392 for (k
= 0, handler
= inputHandler
;
394 handler
= handler
->next
, k
++) {
395 fds
[k
].fd
= handler
->fd
;
397 if (handler
->mask
& WIReadMask
)
398 fds
[k
].events
|= POLLIN
;
400 if (handler
->mask
& WIWriteMask
)
401 fds
[k
].events
|= POLLOUT
;
404 if (handler
->mask
& WIExceptMask
)
405 FD_SET(handler
->fd
, &eset
);
410 * If we don't wait for input, set timeout to return immediately,
411 * else setup the timeout to the estimated time until the
412 * next timer expires or if no timer is pending to infinite.
416 } else if (timerPending()) {
418 delayUntilNextTimerEvent(&tv
);
419 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
424 count
= poll(fds
, nfds
, timeout
);
427 handler
= inputHandler
;
435 if (fds
[k
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
))
438 if (fds
[k
].revents
& (POLLOUT
| POLLWRBAND
))
441 if (fds
[k
].revents
& (POLLHUP
| POLLNVAL
| POLLERR
))
442 mask
|= WIExceptMask
;
444 next
= handler
->next
;
446 if (mask
!=0 && handler
->callback
) {
447 (*handler
->callback
)(handler
->fd
, mask
,
448 handler
->clientData
);
458 W_FlushASAPNotificationQueue();
461 #else /* not HAVE_POLL */
463 struct timeval timeout
;
464 struct timeval
*timeoutPtr
;
465 fd_set rset
, wset
, eset
;
468 InputHandler
*handler
= inputHandler
;
480 if (handler
->mask
& WIReadMask
)
481 FD_SET(handler
->fd
, &rset
);
483 if (handler
->mask
& WIWriteMask
)
484 FD_SET(handler
->fd
, &wset
);
486 if (handler
->mask
& WIExceptMask
)
487 FD_SET(handler
->fd
, &eset
);
489 if (maxfd
< handler
->fd
)
492 handler
= handler
->next
;
496 * If we don't wait for input, set timeout to return immediately,
497 * else setup the timeout to the estimated time until the
498 * next timer expires or if no timer is pending to infinite.
503 timeoutPtr
= &timeout
;
504 } else if (timerPending()) {
505 delayUntilNextTimerEvent(&timeout
);
506 timeoutPtr
= &timeout
;
508 timeoutPtr
= (struct timeval
*)0;
511 count
= select(1 + maxfd
, &rset
, &wset
, &eset
, timeoutPtr
);
514 handler
= inputHandler
;
522 if (FD_ISSET(handler
->fd
, &rset
))
525 if (FD_ISSET(handler
->fd
, &wset
))
528 if (FD_ISSET(handler
->fd
, &eset
))
529 mask
|= WIExceptMask
;
531 next
= handler
->next
;
533 if (mask
!=0 && handler
->callback
) {
534 (*handler
->callback
)(handler
->fd
, mask
,
535 handler
->clientData
);
542 W_FlushASAPNotificationQueue();
545 #else /* not HAVE_SELECT, not HAVE_POLL */
546 Neither select nor poll
. You lose
.
547 #endif /* HAVE_SELECT */
548 #endif /* HAVE_POLL */
555 /* Check any expired timers */
556 if (timerPending()) {
557 checkTimerHandlers();
560 /* We need to make sure that we have some input handler before calling
561 * checkIdleHandlers() in a while loop, because else the while loop
562 * can run forever (if some idle handler reinitiates itself).
565 /* Do idle and timer stuff while there are no input events */
566 while (idlePending() && inputHandler
&& !handleInputEvents(False
)) {
569 /* dispatch timer events */
571 checkTimerHandlers();
576 /* dispatch timer events */
578 checkTimerHandlers();
581 handleInputEvents(True
);
583 /* Check any expired timers */
584 if (timerPending()) {
585 checkTimerHandlers();