*** empty log message ***
[wmaker-crm.git] / WINGs / wevent.c
blob5146bb0fc0d023f4187ecbf64901dacd2e738ea3
3 /*
4 * This event handling stuff was inspired on Tk.
5 */
7 #include "WINGsP.h"
9 #include "../src/config.h"
11 #include <sys/types.h>
12 #include <unistd.h>
14 #ifdef HAVE_POLL_H
15 #include <poll.h>
16 #endif
19 #include <X11/Xos.h>
21 #ifdef HAVE_SYS_SELECT_H
22 # include <sys/select.h>
23 #endif
25 #include <time.h>
27 #ifndef X_GETTIMEOFDAY
28 #define X_GETTIMEOFDAY(t) gettimeofday(t, (struct timezone*)0)
29 #endif
36 typedef struct TimerHandler {
37 WMCallback *callback; /* procedure to call */
38 struct timeval when; /* when to call the callback */
39 void *clientData;
40 struct TimerHandler *next;
41 int nextDelay; /* 0 if it's one-shot */
42 } TimerHandler;
45 typedef struct IdleHandler {
46 WMCallback *callback;
47 void *clientData;
48 } IdleHandler;
51 typedef struct InputHandler {
52 WMInputProc *callback;
53 void *clientData;
54 int fd;
55 int mask;
56 } InputHandler;
59 /* table to map event types to event masks */
60 static unsigned long eventMasks[] = {
63 KeyPressMask, /* KeyPress */
64 KeyReleaseMask, /* KeyRelease */
65 ButtonPressMask, /* ButtonPress */
66 ButtonReleaseMask, /* ButtonRelease */
67 PointerMotionMask|PointerMotionHintMask|ButtonMotionMask
68 |Button1MotionMask|Button2MotionMask|Button3MotionMask
69 |Button4MotionMask|Button5MotionMask,
70 /* MotionNotify */
71 EnterWindowMask, /* EnterNotify */
72 LeaveWindowMask, /* LeaveNotify */
73 FocusChangeMask, /* FocusIn */
74 FocusChangeMask, /* FocusOut */
75 KeymapStateMask, /* KeymapNotify */
76 ExposureMask, /* Expose */
77 ExposureMask, /* GraphicsExpose */
78 ExposureMask, /* NoExpose */
79 VisibilityChangeMask, /* VisibilityNotify */
80 SubstructureNotifyMask, /* CreateNotify */
81 StructureNotifyMask, /* DestroyNotify */
82 StructureNotifyMask, /* UnmapNotify */
83 StructureNotifyMask, /* MapNotify */
84 SubstructureRedirectMask, /* MapRequest */
85 StructureNotifyMask, /* ReparentNotify */
86 StructureNotifyMask, /* ConfigureNotify */
87 SubstructureRedirectMask, /* ConfigureRequest */
88 StructureNotifyMask, /* GravityNotify */
89 ResizeRedirectMask, /* ResizeRequest */
90 StructureNotifyMask, /* CirculateNotify */
91 SubstructureRedirectMask, /* CirculateRequest */
92 PropertyChangeMask, /* PropertyNotify */
93 0, /* SelectionClear */
94 0, /* SelectionRequest */
95 0, /* SelectionNotify */
96 ColormapChangeMask, /* ColormapNotify */
97 ClientMessageMask, /* ClientMessage */
98 0, /* Mapping Notify */
103 /* queue of timer event handlers */
104 static TimerHandler *timerHandler=NULL;
106 static WMBag *idleHandler=NULL;
108 static WMBag *inputHandler=NULL;
110 /* hook for other toolkits or wmaker process their events */
111 static WMEventHook *extraEventHandler=NULL;
115 #define timerPending() (timerHandler)
119 static void
120 rightNow(struct timeval *tv) {
121 X_GETTIMEOFDAY(tv);
124 /* is t1 after t2 ? */
125 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
126 (((t1).tv_sec == (t2).tv_sec) \
127 && ((t1).tv_usec > (t2).tv_usec)))
129 #define IS_ZERO(tv) (tv.tv_sec == 0 && tv.tv_usec == 0)
131 #define SET_ZERO(tv) tv.tv_sec = 0, tv.tv_usec = 0
133 static void
134 addmillisecs(struct timeval *tv, int milliseconds)
136 tv->tv_usec += milliseconds*1000;
138 tv->tv_sec += tv->tv_usec/1000000;
139 tv->tv_usec = tv->tv_usec%1000000;
143 static void
144 enqueueTimerHandler(TimerHandler *handler)
146 TimerHandler *tmp;
148 /* insert callback in queue, sorted by time left */
149 if (!timerHandler || !IS_AFTER(handler->when, timerHandler->when)) {
150 /* first in the queue */
151 handler->next = timerHandler;
152 timerHandler = handler;
153 } else {
154 tmp = timerHandler;
155 while (tmp->next && IS_AFTER(handler->when, tmp->next->when)) {
156 tmp = tmp->next;
158 handler->next = tmp->next;
159 tmp->next = handler;
164 WMHandlerID
165 WMAddTimerHandler(int milliseconds, WMCallback *callback, void *cdata)
167 TimerHandler *handler;
169 handler = malloc(sizeof(TimerHandler));
170 if (!handler)
171 return NULL;
173 rightNow(&handler->when);
174 addmillisecs(&handler->when, milliseconds);
175 handler->callback = callback;
176 handler->clientData = cdata;
177 handler->nextDelay = 0;
179 enqueueTimerHandler(handler);
181 return handler;
185 WMHandlerID
186 WMAddEternalTimerHandler(int milliseconds, WMCallback *callback, void *cdata)
188 TimerHandler *handler = WMAddTimerHandler(milliseconds, callback, cdata);
190 if (handler != NULL)
191 handler->nextDelay = milliseconds;
193 return handler;
198 void
199 WMDeleteTimerWithClientData(void *cdata)
201 TimerHandler *handler, *tmp;
203 if (!cdata || !timerHandler)
204 return;
206 tmp = timerHandler;
207 if (tmp->clientData==cdata) {
208 tmp->nextDelay = 0;
209 if (!IS_ZERO(tmp->when)) {
210 timerHandler = tmp->next;
211 wfree(tmp);
213 } else {
214 while (tmp->next) {
215 if (tmp->next->clientData==cdata) {
216 handler = tmp->next;
217 handler->nextDelay = 0;
218 if (IS_ZERO(handler->when))
219 break;
220 tmp->next = handler->next;
221 wfree(handler);
222 break;
224 tmp = tmp->next;
231 void
232 WMDeleteTimerHandler(WMHandlerID handlerID)
234 TimerHandler *tmp, *handler=(TimerHandler*)handlerID;
236 if (!handler || !timerHandler)
237 return;
239 tmp = timerHandler;
241 handler->nextDelay = 0;
243 if (IS_ZERO(handler->when))
244 return;
246 if (tmp==handler) {
247 timerHandler = handler->next;
248 wfree(handler);
249 } else {
250 while (tmp->next) {
251 if (tmp->next==handler) {
252 tmp->next=handler->next;
253 wfree(handler);
254 break;
256 tmp = tmp->next;
263 WMHandlerID
264 WMAddIdleHandler(WMCallback *callback, void *cdata)
266 IdleHandler *handler;
268 handler = malloc(sizeof(IdleHandler));
269 if (!handler)
270 return NULL;
272 handler->callback = callback;
273 handler->clientData = cdata;
274 /* add handler at end of queue */
275 if (!idleHandler) {
276 idleHandler = WMCreateBag(16);
278 WMPutInBag(idleHandler, handler);
280 return handler;
284 void
285 WMDeleteIdleHandler(WMHandlerID handlerID)
287 IdleHandler *handler = (IdleHandler*)handlerID;
288 int pos;
290 if (!handler || !idleHandler)
291 return;
293 pos = WMGetFirstInBag(idleHandler, handler);
294 if (pos != WBNotFound) {
295 wfree(handler);
296 WMDeleteFromBag(idleHandler, pos);
302 WMHandlerID
303 WMAddInputHandler(int fd, int condition, WMInputProc *proc, void *clientData)
305 InputHandler *handler;
307 handler = wmalloc(sizeof(InputHandler));
309 handler->fd = fd;
310 handler->mask = condition;
311 handler->callback = proc;
312 handler->clientData = clientData;
314 if (!inputHandler)
315 inputHandler = WMCreateBag(16);
316 WMPutInBag(inputHandler, handler);
318 return handler;
323 void
324 WMDeleteInputHandler(WMHandlerID handlerID)
326 InputHandler *handler = (InputHandler*)handlerID;
327 int pos;
329 if (!handler || !inputHandler)
330 return;
332 pos = WMGetFirstInBag(inputHandler, handler);
333 if (pos != WBNotFound) {
334 wfree(handler);
335 WMDeleteFromBag(inputHandler, pos);
341 static Bool
342 checkIdleHandlers()
344 IdleHandler *handler;
345 WMBag *handlerCopy;
346 WMBagIterator iter;
349 if (!idleHandler || WMGetBagItemCount(idleHandler)==0) {
350 W_FlushIdleNotificationQueue();
351 /* make sure an observer in queue didn't added an idle handler */
352 return (idleHandler!=NULL && WMGetBagItemCount(idleHandler)>0);
355 handlerCopy = WMCreateBag(WMGetBagItemCount(idleHandler));
356 WMAppendBag(handlerCopy, idleHandler);
358 for (handler = WMBagFirst(handlerCopy, &iter);
359 iter != NULL;
360 handler = WMBagNext(handlerCopy, &iter)) {
361 /* check if the handler still exist or was removed by a callback */
362 if (WMGetFirstInBag(idleHandler, handler) == WBNotFound)
363 continue;
365 (*handler->callback)(handler->clientData);
366 WMDeleteIdleHandler(handler);
369 WMFreeBag(handlerCopy);
371 W_FlushIdleNotificationQueue();
373 /* this is not necesarrily False, because one handler can re-add itself */
374 return (WMGetBagItemCount(idleHandler)>0);
379 static void
380 checkTimerHandlers()
382 TimerHandler *handler;
383 struct timeval now;
385 if (!timerHandler) {
386 W_FlushASAPNotificationQueue();
387 return;
390 rightNow(&now);
392 handler = timerHandler;
393 while (handler && IS_AFTER(now, handler->when)) {
394 if (!IS_ZERO(handler->when)) {
395 SET_ZERO(handler->when);
396 (*handler->callback)(handler->clientData);
398 handler = handler->next;
401 while (timerHandler && IS_ZERO(handler->when)) {
402 handler = timerHandler;
403 timerHandler = timerHandler->next;
405 if (handler->nextDelay > 0) {
406 handler->when = now;
407 addmillisecs(&handler->when, handler->nextDelay);
408 enqueueTimerHandler(handler);
409 } else {
410 wfree(handler);
414 W_FlushASAPNotificationQueue();
419 static void
420 delayUntilNextTimerEvent(struct timeval *delay)
422 struct timeval now;
424 if (!timerHandler) {
425 /* The return value of this function is only valid if there _are_
426 timers active. */
427 delay->tv_sec = 0;
428 delay->tv_usec = 0;
429 return;
432 rightNow(&now);
433 if (IS_AFTER(now, timerHandler->when)) {
434 delay->tv_sec = 0;
435 delay->tv_usec = 0;
436 } else {
437 delay->tv_sec = timerHandler->when.tv_sec - now.tv_sec;
438 delay->tv_usec = timerHandler->when.tv_usec - now.tv_usec;
439 if (delay->tv_usec < 0) {
440 delay->tv_usec += 1000000;
441 delay->tv_sec--;
450 * WMCreateEventHandler--
451 * Create an event handler and put it in the event handler list for the
452 * view. If the same callback and clientdata are already used in another
453 * handler, the masks are swapped.
456 void
457 WMCreateEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
458 void *clientData)
460 W_EventHandler *handler, *ptr;
461 unsigned long eventMask;
462 WMBagIterator iter;
465 handler = NULL;
466 eventMask = mask;
468 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
469 if (ptr->clientData == clientData && ptr->proc == eventProc) {
470 handler = ptr;
471 eventMask |= ptr->eventMask;
474 if (!handler) {
475 handler = wmalloc(sizeof(W_EventHandler));
477 WMPutInBag(view->eventHandlers, handler);
479 /* select events for window */
480 handler->eventMask = eventMask;
481 handler->proc = eventProc;
482 handler->clientData = clientData;
487 * WMDeleteEventHandler--
488 * Delete event handler matching arguments from windows
489 * event handler list.
492 void
493 WMDeleteEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
494 void *clientData)
496 W_EventHandler *handler, *ptr;
497 WMBagIterator iter;
499 handler = NULL;
501 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
502 if (ptr->eventMask == mask && ptr->proc == eventProc
503 && ptr->clientData == clientData) {
504 handler = ptr;
505 break;
509 if (!handler)
510 return;
512 WMRemoveFromBag(view->eventHandlers, handler);
514 wfree(handler);
519 void
520 W_CleanUpEvents(WMView *view)
522 W_EventHandler *ptr;
523 WMBagIterator iter;
525 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
526 wfree(ptr);
532 static Time
533 getEventTime(WMScreen *screen, XEvent *event)
535 switch (event->type) {
536 case ButtonPress:
537 case ButtonRelease:
538 return event->xbutton.time;
539 case KeyPress:
540 case KeyRelease:
541 return event->xkey.time;
542 case MotionNotify:
543 return event->xmotion.time;
544 case EnterNotify:
545 case LeaveNotify:
546 return event->xcrossing.time;
547 case PropertyNotify:
548 return event->xproperty.time;
549 case SelectionClear:
550 return event->xselectionclear.time;
551 case SelectionRequest:
552 return event->xselectionrequest.time;
553 case SelectionNotify:
554 return event->xselection.time;
555 default:
556 return screen->lastEventTime;
561 void
562 W_CallDestroyHandlers(W_View *view)
564 XEvent event;
565 WMBagIterator iter;
566 W_EventHandler *hPtr;
568 event.type = DestroyNotify;
569 event.xdestroywindow.window = view->window;
570 event.xdestroywindow.event = view->window;
572 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
573 if (hPtr->eventMask & StructureNotifyMask) {
574 (*hPtr->proc)(&event, hPtr->clientData);
581 void
582 WMSetViewNextResponder(WMView *view, WMView *responder)
584 /* set the widget to receive keyboard events that aren't handled
585 * by this widget */
587 view->nextResponder = responder;
591 void
592 WMRelayToNextResponder(WMView *view, XEvent *event)
594 unsigned long mask = eventMasks[event->xany.type];
596 if (view->nextResponder) {
597 WMView *next = view->nextResponder;
598 W_EventHandler *hPtr;
599 WMBagIterator iter;
601 WM_ITERATE_BAG(next->eventHandlers, hPtr, iter) {
602 if ((hPtr->eventMask & mask)) {
603 (*hPtr->proc)(event, hPtr->clientData);
611 WMHandleEvent(XEvent *event)
613 W_EventHandler *hPtr;
614 W_View *view, *vPtr, *toplevel;
615 unsigned long mask;
616 Window window;
617 WMBagIterator iter;
619 if (event->type == MappingNotify) {
620 XRefreshKeyboardMapping(&event->xmapping);
621 return True;
624 mask = eventMasks[event->xany.type];
626 window = event->xany.window;
628 /* diferentiate SubstructureNotify with StructureNotify */
629 if (mask == StructureNotifyMask) {
630 if (event->xmap.event != event->xmap.window) {
631 mask = SubstructureNotifyMask;
632 window = event->xmap.event;
635 view = W_GetViewForXWindow(event->xany.display, window);
637 if (!view) {
638 if (extraEventHandler)
639 (extraEventHandler)(event);
641 return False;
644 view->screen->lastEventTime = getEventTime(view->screen, event);
646 toplevel = W_TopLevelOfView(view);
648 if (event->type == SelectionNotify || event->type == SelectionClear
649 || event->type == SelectionRequest) {
650 /* handle selection related events */
651 W_HandleSelectionEvent(event);
653 } else if (event->type == ClientMessage) {
655 W_HandleDNDClientMessage(toplevel, &event->xclient);
658 /* if it's a key event, redispatch it to the focused control */
659 if (mask & (KeyPressMask|KeyReleaseMask)) {
660 W_View *focused = W_FocusedViewOfToplevel(toplevel);
662 if (focused) {
663 view = focused;
667 /* compress Motion events */
668 if (event->type == MotionNotify && !view->flags.dontCompressMotion) {
669 while (XPending(event->xmotion.display)) {
670 XEvent ev;
671 XPeekEvent(event->xmotion.display, &ev);
672 if (ev.type == MotionNotify
673 && event->xmotion.window == ev.xmotion.window
674 && event->xmotion.subwindow == ev.xmotion.subwindow) {
675 /* replace events */
676 XNextEvent(event->xmotion.display, event);
677 } else break;
681 /* compress expose events */
682 if (event->type == Expose && !view->flags.dontCompressExpose) {
683 while (XCheckTypedWindowEvent(event->xexpose.display, view->window,
684 Expose, event));
688 if (view->screen->modalLoop && toplevel!=view->screen->modalView
689 && !toplevel->flags.worksWhenModal) {
690 if (event->type == KeyPress || event->type == KeyRelease
691 || event->type == MotionNotify || event->type == ButtonPress
692 || event->type == ButtonRelease
693 || event->type == FocusIn || event->type == FocusOut) {
694 return True;
698 /* do balloon stuffs */
699 if (event->type == EnterNotify)
700 W_BalloonHandleEnterView(view);
701 else if (event->type == LeaveNotify)
702 W_BalloonHandleLeaveView(view);
704 /* This is a hack. It will make the panel be secure while
705 * the event handlers are handled, as some event handler
706 * might destroy the widget. */
707 W_RetainView(toplevel);
709 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
710 if ((hPtr->eventMask & mask)) {
711 (*hPtr->proc)(event, hPtr->clientData);
714 #if 0
715 /* pass the event to the top level window of the widget */
716 /* TODO: change this to a responder chain */
717 if (view->parent != NULL) {
718 vPtr = view;
719 while (vPtr->parent != NULL)
720 vPtr = vPtr->parent;
722 WM_ITERATE_BAG(vPtr->eventHandlers, hPtr, iter) {
723 if (hPtr->eventMask & mask) {
724 (*hPtr->proc)(event, hPtr->clientData);
728 #endif
729 /* save button click info to track double-clicks */
730 if (view->screen->ignoreNextDoubleClick) {
731 view->screen->ignoreNextDoubleClick = 0;
732 } else {
733 if (event->type == ButtonPress) {
734 view->screen->lastClickWindow = event->xbutton.window;
735 view->screen->lastClickTime = event->xbutton.time;
739 W_ReleaseView(toplevel);
741 return True;
746 WMIsDoubleClick(XEvent *event)
748 W_View *view;
750 if (event->type != ButtonPress)
751 return False;
753 view = W_GetViewForXWindow(event->xany.display, event->xbutton.window);
755 if (!view)
756 return False;
758 if (view->screen->lastClickWindow != event->xbutton.window)
759 return False;
761 if (event->xbutton.time - view->screen->lastClickTime
762 < WINGsConfiguration.doubleClickDelay) {
763 view->screen->lastClickTime = 0;
764 view->screen->lastClickWindow = None;
765 view->screen->ignoreNextDoubleClick = 1;
766 return True;
767 } else
768 return False;
772 Bool
773 W_WaitForEvent(Display *dpy, unsigned long xeventmask)
775 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
776 struct pollfd *fds;
777 InputHandler *handler;
778 int count, timeout, nfds, i, retval;
780 if (inputHandler)
781 nfds = WMGetBagItemCount(inputHandler);
782 else
783 nfds = 0;
785 fds = wmalloc(nfds+1 * sizeof(struct pollfd));
786 /* put this to the end of array to avoid using ranges from 1 to nfds+1 */
787 fds[nfds].fd = ConnectionNumber(dpy);
788 fds[nfds].events = POLLIN;
790 for (i = 0; i<nfds; i++) {
791 handler = WMGetFromBag(inputHandler, i);
792 fds[i].fd = handler->fd;
793 fds[i].events = 0;
794 if (handler->mask & WIReadMask)
795 fds[i].events |= POLLIN;
797 if (handler->mask & WIWriteMask)
798 fds[i].events |= POLLOUT;
800 #if 0 /* FIXME */
801 if (handler->mask & WIExceptMask)
802 FD_SET(handler->fd, &eset);
803 #endif
807 * Setup the select() timeout to the estimated time until the
808 * next timer expires.
810 if (timerPending()) {
811 struct timeval tv;
812 delayUntilNextTimerEvent(&tv);
813 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
814 } else {
815 timeout = -1;
818 if (xeventmask==0) {
819 if (XPending(dpy))
820 return True;
821 } else {
822 XEvent ev;
823 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
824 XPutBackEvent(dpy, &ev);
825 return True;
829 count = poll(fds, nfds, timeout);
831 if (count>0 && nfds>0) {
832 WMBag *handlerCopy = WMCreateBag(nfds);
834 for (i=0; i<nfds; i++)
835 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
837 for (i=0; i<nfds; i++) {
838 int mask;
840 handler = WMGetFromBag(handlerCopy, i);
841 /* check if the handler still exist or was removed by a callback */
842 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
843 continue;
845 mask = 0;
847 if ((handler->mask & WIReadMask) &&
848 (fds[i].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI)))
849 mask |= WIReadMask;
851 if ((handler->mask & WIWriteMask) &&
852 (fds[i].revents & (POLLOUT | POLLWRBAND)))
853 mask |= WIWriteMask;
855 if ((handler->mask & WIExceptMask) &&
856 (fds[i].revents & (POLLHUP | POLLNVAL | POLLERR)))
857 mask |= WIExceptMask;
859 if (mask!=0 && handler->callback) {
860 (*handler->callback)(handler->fd, mask,
861 handler->clientData);
865 WMFreeBag(handlerCopy);
868 retval = fds[nfds].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI);
869 wfree(fds);
871 W_FlushASAPNotificationQueue();
873 return retval;
874 #else /* not HAVE_POLL */
875 #ifdef HAVE_SELECT
876 struct timeval timeout;
877 struct timeval *timeoutPtr;
878 fd_set rset, wset, eset;
879 int maxfd, nfds, i;
880 int count;
881 InputHandler *handler;
883 FD_ZERO(&rset);
884 FD_ZERO(&wset);
885 FD_ZERO(&eset);
887 FD_SET(ConnectionNumber(dpy), &rset);
888 maxfd = ConnectionNumber(dpy);
890 if (inputHandler)
891 nfds = WMGetBagItemCount(inputHandler);
892 else
893 nfds = 0;
895 for (i=0; i<nfds; i++) {
896 handler = WMGetFromBag(inputHandler, i);
897 if (handler->mask & WIReadMask)
898 FD_SET(handler->fd, &rset);
900 if (handler->mask & WIWriteMask)
901 FD_SET(handler->fd, &wset);
903 if (handler->mask & WIExceptMask)
904 FD_SET(handler->fd, &eset);
906 if (maxfd < handler->fd)
907 maxfd = handler->fd;
912 * Setup the select() timeout to the estimated time until the
913 * next timer expires.
915 if (timerPending()) {
916 delayUntilNextTimerEvent(&timeout);
917 timeoutPtr = &timeout;
918 } else {
919 timeoutPtr = (struct timeval*)0;
922 XSync(dpy, False);
923 if (xeventmask==0) {
924 if (XPending(dpy))
925 return True;
926 } else {
927 XEvent ev;
928 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
929 XPutBackEvent(dpy, &ev);
930 return True;
934 count = select(1 + maxfd, &rset, &wset, &eset, timeoutPtr);
936 if (count>0 && nfds>0) {
937 WMBag *handlerCopy = WMCreateBag(nfds);
939 for (i=0; i<nfds; i++)
940 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
942 for (i=0; i<nfds; i++) {
943 int mask;
945 handler = WMGetFromBag(handlerCopy, i);
946 /* check if the handler still exist or was removed by a callback */
947 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
948 continue;
950 mask = 0;
952 if ((handler->mask & WIReadMask) && FD_ISSET(handler->fd, &rset))
953 mask |= WIReadMask;
955 if ((handler->mask & WIWriteMask) && FD_ISSET(handler->fd, &wset))
956 mask |= WIWriteMask;
958 if ((handler->mask & WIExceptMask) && FD_ISSET(handler->fd, &eset))
959 mask |= WIExceptMask;
961 if (mask!=0 && handler->callback) {
962 (*handler->callback)(handler->fd, mask,
963 handler->clientData);
967 WMFreeBag(handlerCopy);
970 W_FlushASAPNotificationQueue();
972 return FD_ISSET(ConnectionNumber(dpy), &rset);
973 #else /* not HAVE_SELECT, not HAVE_POLL */
974 Neither select nor poll. You lose.
975 #endif /* HAVE_SELECT */
976 #endif /* HAVE_POLL */
980 void
981 WMNextEvent(Display *dpy, XEvent *event)
983 /* Check any expired timers */
984 checkTimerHandlers();
986 while (XPending(dpy) == 0) {
987 /* Do idle stuff */
988 /* Do idle and timer stuff while there are no timer or X events */
989 while (XPending(dpy) == 0 && checkIdleHandlers()) {
990 /* dispatch timer events */
991 checkTimerHandlers();
995 * Make sure that new events did not arrive while we were doing
996 * timer/idle stuff. Or we might block forever waiting for
997 * an event that already arrived.
999 /* wait for something to happen or a timer to expire */
1000 W_WaitForEvent(dpy, 0);
1002 /* Check any expired timers */
1003 checkTimerHandlers();
1006 XNextEvent(dpy, event);
1009 #if 0
1010 void
1011 WMMaskEvent(Display *dpy, long mask, XEvent *event)
1013 unsigned long milliseconds;
1014 struct timeval timeout;
1015 struct timeval *timeoutOrInfty;
1016 fd_set readset;
1018 while (!XCheckMaskEvent(dpy, mask, event)) {
1019 /* Do idle stuff while there are no timer or X events */
1020 while (checkIdleHandlers()) {
1021 if (XCheckMaskEvent(dpy, mask, event))
1022 return;
1026 * Setup the select() timeout to the estimated time until the
1027 * next timer expires.
1029 if (timerPending()) {
1030 delayUntilNextTimerEvent(&timeout);
1031 timeoutOrInfty = &timeout;
1032 } else {
1033 timeoutOrInfty = (struct timeval*)0;
1036 if (XCheckMaskEvent(dpy, mask, event))
1037 return;
1039 /* Wait for input on the X connection socket */
1040 FD_ZERO(&readset);
1041 FD_SET(ConnectionNumber(dpy), &readset);
1042 select(1 + ConnectionNumber(dpy), &readset, (fd_set*)0, (fd_set*)0,
1043 timeoutOrInfty);
1045 /* Check any expired timers */
1046 checkTimerHandlers();
1049 #endif
1050 #if 1
1052 * Cant use this because XPending() will make W_WaitForEvent
1053 * return even if the event in the queue is not what we want,
1054 * and if we block until some new event arrives from the
1055 * server, other events already in the queue (like Expose)
1056 * will be deferred.
1058 void
1059 WMMaskEvent(Display *dpy, long mask, XEvent *event)
1061 while (!XCheckMaskEvent(dpy, mask, event)) {
1062 /* Do idle stuff while there are no timer or X events */
1063 while (checkIdleHandlers()) {
1064 if (XCheckMaskEvent(dpy, mask, event))
1065 return;
1068 /* Wait for input on the X connection socket */
1069 W_WaitForEvent(dpy, mask);
1071 /* Check any expired timers */
1072 checkTimerHandlers();
1075 #endif
1077 Bool
1078 WMScreenPending(WMScreen *scr)
1080 if (XPending(scr->display))
1081 return True;
1082 else
1083 return False;
1087 WMEventHook*
1088 WMHookEventHandler(WMEventHook *handler)
1090 WMEventHook *oldHandler = extraEventHandler;
1092 extraEventHandler = handler;
1094 return oldHandler;