Small fix for an enum
[wmaker-crm.git] / WINGs / wevent.c
blobab646eab17674c4851131cd9f0d162152a0a701e
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 } TimerHandler;
44 typedef struct IdleHandler {
45 WMCallback *callback;
46 void *clientData;
47 } IdleHandler;
50 typedef struct InputHandler {
51 WMInputProc *callback;
52 void *clientData;
53 int fd;
54 int mask;
55 } InputHandler;
58 /* table to map event types to event masks */
59 static unsigned long eventMasks[] = {
62 KeyPressMask, /* KeyPress */
63 KeyReleaseMask, /* KeyRelease */
64 ButtonPressMask, /* ButtonPress */
65 ButtonReleaseMask, /* ButtonRelease */
66 PointerMotionMask|PointerMotionHintMask|ButtonMotionMask
67 |Button1MotionMask|Button2MotionMask|Button3MotionMask
68 |Button4MotionMask|Button5MotionMask,
69 /* MotionNotify */
70 EnterWindowMask, /* EnterNotify */
71 LeaveWindowMask, /* LeaveNotify */
72 FocusChangeMask, /* FocusIn */
73 FocusChangeMask, /* FocusOut */
74 KeymapStateMask, /* KeymapNotify */
75 ExposureMask, /* Expose */
76 ExposureMask, /* GraphicsExpose */
77 ExposureMask, /* NoExpose */
78 VisibilityChangeMask, /* VisibilityNotify */
79 SubstructureNotifyMask, /* CreateNotify */
80 StructureNotifyMask, /* DestroyNotify */
81 StructureNotifyMask, /* UnmapNotify */
82 StructureNotifyMask, /* MapNotify */
83 SubstructureRedirectMask, /* MapRequest */
84 StructureNotifyMask, /* ReparentNotify */
85 StructureNotifyMask, /* ConfigureNotify */
86 SubstructureRedirectMask, /* ConfigureRequest */
87 StructureNotifyMask, /* GravityNotify */
88 ResizeRedirectMask, /* ResizeRequest */
89 StructureNotifyMask, /* CirculateNotify */
90 SubstructureRedirectMask, /* CirculateRequest */
91 PropertyChangeMask, /* PropertyNotify */
92 0, /* SelectionClear */
93 0, /* SelectionRequest */
94 0, /* SelectionNotify */
95 ColormapChangeMask, /* ColormapNotify */
96 ClientMessageMask, /* ClientMessage */
97 0, /* Mapping Notify */
102 /* queue of timer event handlers */
103 static TimerHandler *timerHandler=NULL;
105 static WMBag *idleHandler=NULL;
107 static WMBag *inputHandler=NULL;
109 /* hook for other toolkits or wmaker process their events */
110 static WMEventHook *extraEventHandler=NULL;
114 #define timerPending() (timerHandler)
118 static void
119 rightNow(struct timeval *tv) {
120 X_GETTIMEOFDAY(tv);
123 /* is t1 after t2 ? */
124 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
125 (((t1).tv_sec == (t2).tv_sec) \
126 && ((t1).tv_usec > (t2).tv_usec)))
129 static void
130 addmillisecs(struct timeval *tv, int milliseconds)
132 tv->tv_usec += milliseconds*1000;
134 tv->tv_sec += tv->tv_usec/1000000;
135 tv->tv_usec = tv->tv_usec%1000000;
139 WMHandlerID
140 WMAddTimerHandler(int milliseconds, WMCallback *callback, void *cdata)
142 TimerHandler *handler, *tmp;
144 handler = malloc(sizeof(TimerHandler));
145 if (!handler)
146 return NULL;
148 rightNow(&handler->when);
149 addmillisecs(&handler->when, milliseconds);
150 handler->callback = callback;
151 handler->clientData = cdata;
152 /* insert callback in queue, sorted by time left */
153 if (!timerHandler || !IS_AFTER(handler->when, timerHandler->when)) {
154 /* first in the queue */
155 handler->next = timerHandler;
156 timerHandler = handler;
157 } else {
158 tmp = timerHandler;
159 while (tmp->next && IS_AFTER(handler->when, tmp->next->when)) {
160 tmp = tmp->next;
162 handler->next = tmp->next;
163 tmp->next = handler;
165 return handler;
170 void
171 WMDeleteTimerWithClientData(void *cdata)
173 TimerHandler *handler, *tmp;
175 if (!cdata || !timerHandler)
176 return;
178 tmp = timerHandler;
179 if (tmp->clientData==cdata) {
180 timerHandler = tmp->next;
181 wfree(tmp);
182 } else {
183 while (tmp->next) {
184 if (tmp->next->clientData==cdata) {
185 handler = tmp->next;
186 tmp->next = handler->next;
187 wfree(handler);
188 break;
190 tmp = tmp->next;
197 void
198 WMDeleteTimerHandler(WMHandlerID handlerID)
200 TimerHandler *tmp, *handler=(TimerHandler*)handlerID;
202 if (!handler || !timerHandler)
203 return;
205 tmp = timerHandler;
206 if (tmp==handler) {
207 timerHandler = handler->next;
208 wfree(handler);
209 } else {
210 while (tmp->next) {
211 if (tmp->next==handler) {
212 tmp->next=handler->next;
213 wfree(handler);
214 break;
216 tmp = tmp->next;
223 WMHandlerID
224 WMAddIdleHandler(WMCallback *callback, void *cdata)
226 IdleHandler *handler;
228 handler = malloc(sizeof(IdleHandler));
229 if (!handler)
230 return NULL;
232 handler->callback = callback;
233 handler->clientData = cdata;
234 /* add handler at end of queue */
235 if (!idleHandler) {
236 idleHandler = WMCreateBag(16);
238 WMPutInBag(idleHandler, handler);
240 return handler;
245 void
246 WMDeleteIdleHandler(WMHandlerID handlerID)
248 IdleHandler *handler = (IdleHandler*)handlerID;
249 int pos;
251 if (!handler || !idleHandler)
252 return;
254 pos = WMGetFirstInBag(idleHandler, handler);
255 if (pos != WBNotFound) {
256 wfree(handler);
257 WMDeleteFromBag(idleHandler, pos);
263 WMHandlerID
264 WMAddInputHandler(int fd, int condition, WMInputProc *proc, void *clientData)
266 InputHandler *handler;
268 handler = wmalloc(sizeof(InputHandler));
270 handler->fd = fd;
271 handler->mask = condition;
272 handler->callback = proc;
273 handler->clientData = clientData;
275 if (!inputHandler)
276 inputHandler = WMCreateBag(16);
277 WMPutInBag(inputHandler, handler);
279 return handler;
284 void
285 WMDeleteInputHandler(WMHandlerID handlerID)
287 InputHandler *handler = (InputHandler*)handlerID;
288 int pos;
290 if (!handler || !inputHandler)
291 return;
293 pos = WMGetFirstInBag(inputHandler, handler);
294 if (pos != WBNotFound) {
295 wfree(handler);
296 WMDeleteFromBag(inputHandler, pos);
302 static Bool
303 checkIdleHandlers()
305 IdleHandler *handler;
306 WMBag *handlerCopy;
307 WMBagIterator iter;
309 if (!idleHandler || WMGetBagItemCount(idleHandler)==0) {
310 W_FlushIdleNotificationQueue();
311 /* make sure an observer in queue didn't added an idle handler */
312 return (idleHandler!=NULL && WMGetBagItemCount(idleHandler)>0);
315 handlerCopy = WMCreateBag(WMGetBagItemCount(idleHandler));
316 WMAppendBag(handlerCopy, idleHandler);
318 for (handler = WMBagFirst(handlerCopy, &iter);
319 iter != NULL;
320 handler = WMBagNext(handlerCopy, &iter)) {
321 /* check if the handler still exist or was removed by a callback */
322 if (WMGetFirstInBag(idleHandler, handler) == WBNotFound)
323 continue;
325 (*handler->callback)(handler->clientData);
326 WMDeleteIdleHandler(handler);
329 WMFreeBag(handlerCopy);
331 W_FlushIdleNotificationQueue();
333 /* this is not necesarrily False, because one handler can re-add itself */
334 return (WMGetBagItemCount(idleHandler)>0);
339 static void
340 checkTimerHandlers()
342 TimerHandler *handler;
343 struct timeval now;
345 if (!timerHandler) {
346 W_FlushASAPNotificationQueue();
347 return;
350 rightNow(&now);
352 while (timerHandler && IS_AFTER(now, timerHandler->when)) {
353 handler = timerHandler;
354 timerHandler = timerHandler->next;
355 handler->next = NULL;
356 (*handler->callback)(handler->clientData);
357 wfree(handler);
360 W_FlushASAPNotificationQueue();
365 static void
366 delayUntilNextTimerEvent(struct timeval *delay)
368 struct timeval now;
370 if (!timerHandler) {
371 /* The return value of this function is only valid if there _are_
372 timers active. */
373 delay->tv_sec = 0;
374 delay->tv_usec = 0;
375 return;
378 rightNow(&now);
379 if (IS_AFTER(now, timerHandler->when)) {
380 delay->tv_sec = 0;
381 delay->tv_usec = 0;
382 } else {
383 delay->tv_sec = timerHandler->when.tv_sec - now.tv_sec;
384 delay->tv_usec = timerHandler->when.tv_usec - now.tv_usec;
385 if (delay->tv_usec < 0) {
386 delay->tv_usec += 1000000;
387 delay->tv_sec--;
396 * WMCreateEventHandler--
397 * Create an event handler and put it in the event handler list for the
398 * view. If the same callback and clientdata are already used in another
399 * handler, the masks are swapped.
402 void
403 WMCreateEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
404 void *clientData)
406 W_EventHandler *handler, *ptr;
407 unsigned long eventMask;
408 WMBagIterator iter;
411 handler = NULL;
412 eventMask = mask;
414 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
415 if (ptr->clientData == clientData && ptr->proc == eventProc) {
416 handler = ptr;
417 eventMask |= ptr->eventMask;
420 if (!handler) {
421 handler = wmalloc(sizeof(W_EventHandler));
423 WMPutInBag(view->eventHandlers, handler);
425 /* select events for window */
426 handler->eventMask = eventMask;
427 handler->proc = eventProc;
428 handler->clientData = clientData;
433 * WMDeleteEventHandler--
434 * Delete event handler matching arguments from windows
435 * event handler list.
438 void
439 WMDeleteEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
440 void *clientData)
442 W_EventHandler *handler, *ptr;
443 WMBagIterator iter;
445 handler = NULL;
447 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
448 if (ptr->eventMask == mask && ptr->proc == eventProc
449 && ptr->clientData == clientData) {
450 handler = ptr;
451 break;
455 if (!handler)
456 return;
458 WMRemoveFromBag(view->eventHandlers, handler);
460 wfree(handler);
465 void
466 W_CleanUpEvents(WMView *view)
468 W_EventHandler *ptr;
469 WMBagIterator iter;
471 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
472 wfree(ptr);
478 static Time
479 getEventTime(WMScreen *screen, XEvent *event)
481 switch (event->type) {
482 case ButtonPress:
483 case ButtonRelease:
484 return event->xbutton.time;
485 case KeyPress:
486 case KeyRelease:
487 return event->xkey.time;
488 case MotionNotify:
489 return event->xmotion.time;
490 case EnterNotify:
491 case LeaveNotify:
492 return event->xcrossing.time;
493 case PropertyNotify:
494 return event->xproperty.time;
495 case SelectionClear:
496 return event->xselectionclear.time;
497 case SelectionRequest:
498 return event->xselectionrequest.time;
499 case SelectionNotify:
500 return event->xselection.time;
501 default:
502 return screen->lastEventTime;
507 void
508 W_CallDestroyHandlers(W_View *view)
510 XEvent event;
511 WMBagIterator iter;
512 W_EventHandler *hPtr;
514 event.type = DestroyNotify;
515 event.xdestroywindow.window = view->window;
516 event.xdestroywindow.event = view->window;
518 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
519 if (hPtr->eventMask & StructureNotifyMask) {
520 (*hPtr->proc)(&event, hPtr->clientData);
527 WMHandleEvent(XEvent *event)
529 W_EventHandler *hPtr;
530 W_View *view, *vPtr, *toplevel;
531 unsigned long mask;
532 Window window;
533 WMBagIterator iter;
535 if (event->type == MappingNotify) {
536 XRefreshKeyboardMapping(&event->xmapping);
537 return True;
540 mask = eventMasks[event->xany.type];
542 window = event->xany.window;
544 /* diferentiate SubstructureNotify with StructureNotify */
545 if (mask == StructureNotifyMask) {
546 if (event->xmap.event != event->xmap.window) {
547 mask = SubstructureNotifyMask;
548 window = event->xmap.event;
551 view = W_GetViewForXWindow(event->xany.display, window);
553 if (!view) {
554 if (extraEventHandler)
555 (extraEventHandler)(event);
557 return False;
560 view->screen->lastEventTime = getEventTime(view->screen, event);
562 toplevel = W_TopLevelOfView(view);
564 if (event->type == SelectionNotify || event->type == SelectionClear
565 || event->type == SelectionRequest) {
566 /* handle selection related events */
567 W_HandleSelectionEvent(event);
569 } else if (event->type == ClientMessage) {
571 W_HandleDNDClientMessage(toplevel, &event->xclient);
574 /* if it's a key event, redispatch it to the focused control */
575 if (mask & (KeyPressMask|KeyReleaseMask)) {
576 W_View *focused = W_FocusedViewOfToplevel(toplevel);
578 if (focused) {
579 view = focused;
583 /* compress Motion events */
584 if (event->type == MotionNotify && !view->flags.dontCompressMotion) {
585 while (XPending(event->xmotion.display)) {
586 XEvent ev;
587 XPeekEvent(event->xmotion.display, &ev);
588 if (ev.type == MotionNotify
589 && event->xmotion.window == ev.xmotion.window
590 && event->xmotion.subwindow == ev.xmotion.subwindow) {
591 /* replace events */
592 XNextEvent(event->xmotion.display, event);
593 } else break;
597 /* compress expose events */
598 if (event->type == Expose && !view->flags.dontCompressExpose) {
599 while (XCheckTypedWindowEvent(event->xexpose.display, view->window,
600 Expose, event));
604 if (view->screen->modal && toplevel!=view->screen->modalView
605 && !toplevel->flags.worksWhenModal) {
606 if (event->type == KeyPress || event->type == KeyRelease
607 || event->type == MotionNotify || event->type == ButtonPress
608 || event->type == ButtonRelease
609 || event->type == FocusIn || event->type == FocusOut) {
610 return True;
614 /* do balloon stuffs */
615 if (event->type == EnterNotify)
616 W_BalloonHandleEnterView(view);
617 else if (event->type == LeaveNotify)
618 W_BalloonHandleLeaveView(view);
620 /* This is a hack. It will make the panel be secure while
621 * the event handlers are handled, as some event handler
622 * might destroy the widget. */
623 W_RetainView(toplevel);
625 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
626 if ((hPtr->eventMask & mask)) {
627 (*hPtr->proc)(event, hPtr->clientData);
631 /* pass the event to the top level window of the widget */
632 /* TODO: change this to a responder chain */
633 if (view->parent != NULL) {
634 vPtr = view;
635 while (vPtr->parent != NULL)
636 vPtr = vPtr->parent;
638 WM_ITERATE_BAG(vPtr->eventHandlers, hPtr, iter) {
639 if (hPtr->eventMask & mask) {
640 (*hPtr->proc)(event, hPtr->clientData);
645 /* save button click info to track double-clicks */
646 if (view->screen->ignoreNextDoubleClick) {
647 view->screen->ignoreNextDoubleClick = 0;
648 } else {
649 if (event->type == ButtonPress) {
650 view->screen->lastClickWindow = event->xbutton.window;
651 view->screen->lastClickTime = event->xbutton.time;
655 W_ReleaseView(toplevel);
657 return True;
662 WMIsDoubleClick(XEvent *event)
664 W_View *view;
666 if (event->type != ButtonPress)
667 return False;
669 view = W_GetViewForXWindow(event->xany.display, event->xbutton.window);
671 if (!view)
672 return False;
674 if (view->screen->lastClickWindow != event->xbutton.window)
675 return False;
677 if (event->xbutton.time - view->screen->lastClickTime
678 < WINGsConfiguration.doubleClickDelay) {
679 view->screen->lastClickTime = 0;
680 view->screen->lastClickWindow = None;
681 view->screen->ignoreNextDoubleClick = 1;
682 return True;
683 } else
684 return False;
688 Bool
689 W_WaitForEvent(Display *dpy, unsigned long xeventmask)
691 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
692 struct pollfd *fds;
693 InputHandler *handler;
694 int count, timeout, nfds, i, retval;
696 if (inputHandler)
697 nfds = WMGetBagItemCount(inputHandler);
698 else
699 nfds = 0;
701 fds = wmalloc(nfds+1 * sizeof(struct pollfd));
702 /* put this to the end of array to avoid using ranges from 1 to nfds+1 */
703 fds[nfds].fd = ConnectionNumber(dpy);
704 fds[nfds].events = POLLIN;
706 for (i = 0; i<nfds; i++) {
707 handler = WMGetFromBag(inputHandler, i);
708 fds[i].fd = handler->fd;
709 fds[i].events = 0;
710 if (handler->mask & WIReadMask)
711 fds[i].events |= POLLIN;
713 if (handler->mask & WIWriteMask)
714 fds[i].events |= POLLOUT;
716 #if 0 /* FIXME */
717 if (handler->mask & WIExceptMask)
718 FD_SET(handler->fd, &eset);
719 #endif
723 * Setup the select() timeout to the estimated time until the
724 * next timer expires.
726 if (timerPending()) {
727 struct timeval tv;
728 delayUntilNextTimerEvent(&tv);
729 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
730 } else {
731 timeout = -1;
734 if (xeventmask==0) {
735 if (XPending(dpy))
736 return True;
737 } else {
738 XEvent ev;
739 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
740 XPutBackEvent(dpy, &ev);
741 return True;
745 count = poll(fds, nfds, timeout);
747 if (count>0 && nfds>0) {
748 WMBag *handlerCopy = WMCreateBag(nfds);
750 for (i=0; i<nfds; i++)
751 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
753 for (i=0; i<nfds; i++) {
754 int mask;
756 handler = WMGetFromBag(handlerCopy, i);
757 /* check if the handler still exist or was removed by a callback */
758 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
759 continue;
761 mask = 0;
763 if ((handler->mask & WIReadMask) &&
764 (fds[i].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI)))
765 mask |= WIReadMask;
767 if ((handler->mask & WIWriteMask) &&
768 (fds[i].revents & (POLLOUT | POLLWRBAND)))
769 mask |= WIWriteMask;
771 if ((handler->mask & WIExceptMask) &&
772 (fds[i].revents & (POLLHUP | POLLNVAL | POLLERR)))
773 mask |= WIExceptMask;
775 if (mask!=0 && handler->callback) {
776 (*handler->callback)(handler->fd, mask,
777 handler->clientData);
781 WMFreeBag(handlerCopy);
784 retval = fds[nfds].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI);
785 wfree(fds);
787 W_FlushASAPNotificationQueue();
789 return retval;
790 #else /* not HAVE_POLL */
791 #ifdef HAVE_SELECT
792 struct timeval timeout;
793 struct timeval *timeoutPtr;
794 fd_set rset, wset, eset;
795 int maxfd, nfds, i;
796 int count;
797 InputHandler *handler;
799 FD_ZERO(&rset);
800 FD_ZERO(&wset);
801 FD_ZERO(&eset);
803 FD_SET(ConnectionNumber(dpy), &rset);
804 maxfd = ConnectionNumber(dpy);
806 if (inputHandler)
807 nfds = WMGetBagItemCount(inputHandler);
808 else
809 nfds = 0;
811 for (i=0; i<nfds; i++) {
812 handler = WMGetFromBag(inputHandler, i);
813 if (handler->mask & WIReadMask)
814 FD_SET(handler->fd, &rset);
816 if (handler->mask & WIWriteMask)
817 FD_SET(handler->fd, &wset);
819 if (handler->mask & WIExceptMask)
820 FD_SET(handler->fd, &eset);
822 if (maxfd < handler->fd)
823 maxfd = handler->fd;
828 * Setup the select() timeout to the estimated time until the
829 * next timer expires.
831 if (timerPending()) {
832 delayUntilNextTimerEvent(&timeout);
833 timeoutPtr = &timeout;
834 } else {
835 timeoutPtr = (struct timeval*)0;
838 XSync(dpy, False);
839 if (xeventmask==0) {
840 if (XPending(dpy))
841 return True;
842 } else {
843 XEvent ev;
844 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
845 XPutBackEvent(dpy, &ev);
846 return True;
850 count = select(1 + maxfd, &rset, &wset, &eset, timeoutPtr);
852 if (count>0 && nfds>0) {
853 WMBag *handlerCopy = WMCreateBag(nfds);
855 for (i=0; i<nfds; i++)
856 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
858 for (i=0; i<nfds; i++) {
859 int mask;
861 handler = WMGetFromBag(handlerCopy, i);
862 /* check if the handler still exist or was removed by a callback */
863 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
864 continue;
866 mask = 0;
868 if ((handler->mask & WIReadMask) && FD_ISSET(handler->fd, &rset))
869 mask |= WIReadMask;
871 if ((handler->mask & WIWriteMask) && FD_ISSET(handler->fd, &wset))
872 mask |= WIWriteMask;
874 if ((handler->mask & WIExceptMask) && FD_ISSET(handler->fd, &eset))
875 mask |= WIExceptMask;
877 if (mask!=0 && handler->callback) {
878 (*handler->callback)(handler->fd, mask,
879 handler->clientData);
883 WMFreeBag(handlerCopy);
886 W_FlushASAPNotificationQueue();
888 return FD_ISSET(ConnectionNumber(dpy), &rset);
889 #else /* not HAVE_SELECT, not HAVE_POLL */
890 Neither select nor poll. You lose.
891 #endif /* HAVE_SELECT */
892 #endif /* HAVE_POLL */
896 void
897 WMNextEvent(Display *dpy, XEvent *event)
899 /* Check any expired timers */
900 checkTimerHandlers();
902 while (XPending(dpy) == 0) {
903 /* Do idle stuff */
904 /* Do idle and timer stuff while there are no timer or X events */
905 while (!XPending(dpy) && checkIdleHandlers()) {
906 /* dispatch timer events */
907 checkTimerHandlers();
911 * Make sure that new events did not arrive while we were doing
912 * timer/idle stuff. Or we might block forever waiting for
913 * an event that already arrived.
915 /* wait to something happen */
916 W_WaitForEvent(dpy, 0);
918 /* Check any expired timers */
919 checkTimerHandlers();
922 XNextEvent(dpy, event);
925 #if 0
926 void
927 WMMaskEvent(Display *dpy, long mask, XEvent *event)
929 unsigned long milliseconds;
930 struct timeval timeout;
931 struct timeval *timeoutOrInfty;
932 fd_set readset;
934 while (!XCheckMaskEvent(dpy, mask, event)) {
935 /* Do idle stuff while there are no timer or X events */
936 while (checkIdleHandlers()) {
937 if (XCheckMaskEvent(dpy, mask, event))
938 return;
942 * Setup the select() timeout to the estimated time until the
943 * next timer expires.
945 if (timerPending()) {
946 delayUntilNextTimerEvent(&timeout);
947 timeoutOrInfty = &timeout;
948 } else {
949 timeoutOrInfty = (struct timeval*)0;
952 if (XCheckMaskEvent(dpy, mask, event))
953 return;
955 /* Wait for input on the X connection socket */
956 FD_ZERO(&readset);
957 FD_SET(ConnectionNumber(dpy), &readset);
958 select(1 + ConnectionNumber(dpy), &readset, (fd_set*)0, (fd_set*)0,
959 timeoutOrInfty);
961 /* Check any expired timers */
962 checkTimerHandlers();
965 #endif
966 #if 1
968 * Cant use this because XPending() will make W_WaitForEvent
969 * return even if the event in the queue is not what we want,
970 * and if we block until some new event arrives from the
971 * server, other events already in the queue (like Expose)
972 * will be deferred.
974 void
975 WMMaskEvent(Display *dpy, long mask, XEvent *event)
977 while (!XCheckMaskEvent(dpy, mask, event)) {
978 /* Do idle stuff while there are no timer or X events */
979 while (checkIdleHandlers()) {
980 if (XCheckMaskEvent(dpy, mask, event))
981 return;
984 /* Wait for input on the X connection socket */
985 W_WaitForEvent(dpy, mask);
987 /* Check any expired timers */
988 checkTimerHandlers();
991 #endif
993 Bool
994 WMScreenPending(WMScreen *scr)
996 if (XPending(scr->display))
997 return True;
998 else
999 return False;
1003 WMEventHook*
1004 WMHookEventHandler(WMEventHook *handler)
1006 WMEventHook *oldHandler = extraEventHandler;
1008 extraEventHandler = handler;
1010 return oldHandler;