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)
72 rightNow(struct timeval
*tv
) {
76 /* is t1 after t2 ? */
77 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
78 (((t1).tv_sec == (t2).tv_sec) \
79 && ((t1).tv_usec > (t2).tv_usec)))
83 addmillisecs(struct timeval
*tv
, int milliseconds
)
85 tv
->tv_usec
+= milliseconds
*1000;
87 tv
->tv_sec
+= tv
->tv_usec
/1000000;
88 tv
->tv_usec
= tv
->tv_usec
%1000000;
93 WMAddTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
95 TimerHandler
*handler
, *tmp
;
97 handler
= malloc(sizeof(TimerHandler
));
101 rightNow(&handler
->when
);
102 addmillisecs(&handler
->when
, milliseconds
);
103 handler
->callback
= callback
;
104 handler
->clientData
= cdata
;
105 /* insert callback in queue, sorted by time left */
106 if (!timerHandler
|| !IS_AFTER(handler
->when
, timerHandler
->when
)) {
107 /* first in the queue */
108 handler
->next
= timerHandler
;
109 timerHandler
= handler
;
112 while (tmp
->next
&& IS_AFTER(handler
->when
, tmp
->next
->when
)) {
115 handler
->next
= tmp
->next
;
124 WMDeleteTimerWithClientData(void *cdata
)
126 TimerHandler
*handler
, *tmp
;
128 if (!cdata
|| !timerHandler
)
132 if (tmp
->clientData
==cdata
) {
133 timerHandler
= tmp
->next
;
137 if (tmp
->next
->clientData
==cdata
) {
139 tmp
->next
= handler
->next
;
151 WMDeleteTimerHandler(WMHandlerID handlerID
)
153 TimerHandler
*tmp
, *handler
=(TimerHandler
*)handlerID
;
155 if (!handler
|| !timerHandler
)
160 timerHandler
= handler
->next
;
164 if (tmp
->next
==handler
) {
165 tmp
->next
=handler
->next
;
177 WMAddIdleHandler(WMCallback
*callback
, void *cdata
)
179 IdleHandler
*handler
, *tmp
;
181 handler
= malloc(sizeof(IdleHandler
));
185 handler
->callback
= callback
;
186 handler
->clientData
= cdata
;
187 handler
->next
= NULL
;
188 /* add callback at end of queue */
190 idleHandler
= handler
;
205 WMDeleteIdleHandler(WMHandlerID handlerID
)
207 IdleHandler
*tmp
, *handler
= (IdleHandler
*)handlerID
;
209 if (!handler
|| !idleHandler
)
213 if (tmp
== handler
) {
214 idleHandler
= handler
->next
;
218 if (tmp
->next
== handler
) {
219 tmp
->next
= handler
->next
;
231 WMAddInputHandler(int fd
, int condition
, WMInputProc
*proc
, void *clientData
)
233 InputHandler
*handler
;
235 handler
= wmalloc(sizeof(InputHandler
));
238 handler
->mask
= condition
;
239 handler
->callback
= proc
;
240 handler
->clientData
= clientData
;
242 handler
->next
= inputHandler
;
244 inputHandler
= handler
;
251 WMDeleteInputHandler(WMHandlerID handlerID
)
253 InputHandler
*tmp
, *handler
= (InputHandler
*)handlerID
;
255 if (!handler
|| !inputHandler
)
259 if (tmp
== handler
) {
260 inputHandler
= handler
->next
;
264 if (tmp
->next
== handler
) {
265 tmp
->next
= handler
->next
;
278 IdleHandler
*handler
, *tmp
;
281 W_FlushIdleNotificationQueue();
282 /* make sure an observer in queue didn't added an idle handler */
283 return (idleHandler
!=NULL
);
286 handler
= idleHandler
;
288 /* we will process all idleHandlers so, empty the handler list */
293 (*handler
->callback
)(handler
->clientData
);
294 /* remove the handler */
300 W_FlushIdleNotificationQueue();
302 /* this is not necesarrily False, because one handler can re-add itself */
303 return (idleHandler
!=NULL
);
311 TimerHandler
*handler
;
316 while (timerHandler
&& IS_AFTER(now
, timerHandler
->when
)) {
317 handler
= timerHandler
;
318 timerHandler
= timerHandler
->next
;
319 handler
->next
= NULL
;
320 (*handler
->callback
)(handler
->clientData
);
324 W_FlushASAPNotificationQueue();
330 delayUntilNextTimerEvent(struct timeval
*delay
)
335 /* The return value of this function is only valid if there _are_
343 if (IS_AFTER(now
, timerHandler
->when
)) {
347 delay
->tv_sec
= timerHandler
->when
.tv_sec
- now
.tv_sec
;
348 delay
->tv_usec
= timerHandler
->when
.tv_usec
- now
.tv_usec
;
349 if (delay
->tv_usec
< 0) {
350 delay
->tv_usec
+= 1000000;
358 * This functions will handle input events on all registered file descriptors.
360 * - waitForInput - True if we want the function to wait until an event
361 * appears on a file descriptor we watch, False if we
362 * want the function to immediately return if there is
363 * no data available on the file descriptors we watch.
365 * if waitForInput is False, the function will return False if there are no
366 * input handlers registered, or if there is no data
367 * available on the registered ones, and will return True
368 * if there is at least one input handler that has data
370 * if waitForInput is True, the function will return False if there are no
371 * input handlers registered, else it will block until an
372 * event appears on one of the file descriptors it watches
373 * and then it will return True.
375 * If the retured value is True, the input handlers for the corresponding file
376 * descriptors are also called.
380 handleInputEvents(Bool waitForInput
)
382 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
384 InputHandler
*handler
;
385 int count
, timeout
, nfds
, k
;
390 for (nfds
= 0, handler
= inputHandler
;
391 handler
!= 0; handler
= handler
->next
) nfds
++;
393 fds
= wmalloc(nfds
* sizeof(struct pollfd
));
395 for (k
= 0, handler
= inputHandler
;
397 handler
= handler
->next
, k
++) {
398 fds
[k
].fd
= handler
->fd
;
400 if (handler
->mask
& WIReadMask
)
401 fds
[k
].events
|= POLLIN
;
403 if (handler
->mask
& WIWriteMask
)
404 fds
[k
].events
|= POLLOUT
;
407 if (handler
->mask
& WIExceptMask
)
408 FD_SET(handler
->fd
, &eset
);
413 * If we don't wait for input, set timeout to return immediately,
414 * else setup the timeout to the estimated time until the
415 * next timer expires or if no timer is pending to infinite.
419 } else if (timerPending()) {
421 delayUntilNextTimerEvent(&tv
);
422 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
427 count
= poll(fds
, nfds
, timeout
);
430 handler
= inputHandler
;
438 if (fds
[k
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
))
441 if (fds
[k
].revents
& (POLLOUT
| POLLWRBAND
))
444 if (fds
[k
].revents
& (POLLHUP
| POLLNVAL
| POLLERR
))
445 mask
|= WIExceptMask
;
447 next
= handler
->next
;
449 if (mask
!=0 && handler
->callback
) {
450 (*handler
->callback
)(handler
->fd
, mask
,
451 handler
->clientData
);
461 W_FlushASAPNotificationQueue();
464 #else /* not HAVE_POLL */
466 struct timeval timeout
;
467 struct timeval
*timeoutPtr
;
468 fd_set rset
, wset
, eset
;
471 InputHandler
*handler
= inputHandler
;
483 if (handler
->mask
& WIReadMask
)
484 FD_SET(handler
->fd
, &rset
);
486 if (handler
->mask
& WIWriteMask
)
487 FD_SET(handler
->fd
, &wset
);
489 if (handler
->mask
& WIExceptMask
)
490 FD_SET(handler
->fd
, &eset
);
492 if (maxfd
< handler
->fd
)
495 handler
= handler
->next
;
499 * If we don't wait for input, set timeout to return immediately,
500 * else setup the timeout to the estimated time until the
501 * next timer expires or if no timer is pending to infinite.
506 timeoutPtr
= &timeout
;
507 } else if (timerPending()) {
508 delayUntilNextTimerEvent(&timeout
);
509 timeoutPtr
= &timeout
;
511 timeoutPtr
= (struct timeval
*)0;
514 count
= select(1 + maxfd
, &rset
, &wset
, &eset
, timeoutPtr
);
517 handler
= inputHandler
;
525 if (FD_ISSET(handler
->fd
, &rset
))
528 if (FD_ISSET(handler
->fd
, &wset
))
531 if (FD_ISSET(handler
->fd
, &eset
))
532 mask
|= WIExceptMask
;
534 next
= handler
->next
;
536 if (mask
!=0 && handler
->callback
) {
537 (*handler
->callback
)(handler
->fd
, mask
,
538 handler
->clientData
);
545 W_FlushASAPNotificationQueue();
548 #else /* not HAVE_SELECT, not HAVE_POLL */
549 Neither select nor poll
. You lose
.
550 #endif /* HAVE_SELECT */
551 #endif /* HAVE_POLL */
558 /* Check any expired timers */
559 if (timerPending()) {
560 checkTimerHandlers();
563 /* We need to make sure that we have some input handler before calling
564 * checkIdleHandlers() in a while loop, because else the while loop
565 * can run forever (if some idle handler reinitiates itself).
568 /* Do idle and timer stuff while there are no input events */
569 while (checkIdleHandlers() && inputHandler
&& !handleInputEvents(False
)) {
570 /* dispatch timer events */
572 checkTimerHandlers();
576 /* dispatch timer events */
578 checkTimerHandlers();
581 handleInputEvents(True
);
583 /* Check any expired timers */
584 if (timerPending()) {
585 checkTimerHandlers();