*** empty log message ***
[wmaker-crm.git] / WINGs / wevent.c
blob63d9bb7331f7ded01ef25cdf28271f32c7fe7ce4
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 SET_ZERO(handler->when);
395 (*handler->callback)(handler->clientData);
396 handler = handler->next;
399 while (timerHandler && IS_ZERO(handler->when)) {
400 handler = timerHandler;
401 timerHandler = timerHandler->next;
403 if (handler->nextDelay > 0) {
404 rightNow(&handler->when);
405 addmillisecs(&handler->when, handler->nextDelay);
406 enqueueTimerHandler(handler);
407 } else {
408 wfree(handler);
412 W_FlushASAPNotificationQueue();
417 static void
418 delayUntilNextTimerEvent(struct timeval *delay)
420 struct timeval now;
422 if (!timerHandler) {
423 /* The return value of this function is only valid if there _are_
424 timers active. */
425 delay->tv_sec = 0;
426 delay->tv_usec = 0;
427 return;
430 rightNow(&now);
431 if (IS_AFTER(now, timerHandler->when)) {
432 delay->tv_sec = 0;
433 delay->tv_usec = 0;
434 } else {
435 delay->tv_sec = timerHandler->when.tv_sec - now.tv_sec;
436 delay->tv_usec = timerHandler->when.tv_usec - now.tv_usec;
437 if (delay->tv_usec < 0) {
438 delay->tv_usec += 1000000;
439 delay->tv_sec--;
448 * WMCreateEventHandler--
449 * Create an event handler and put it in the event handler list for the
450 * view. If the same callback and clientdata are already used in another
451 * handler, the masks are swapped.
454 void
455 WMCreateEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
456 void *clientData)
458 W_EventHandler *handler, *ptr;
459 unsigned long eventMask;
460 WMBagIterator iter;
463 handler = NULL;
464 eventMask = mask;
466 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
467 if (ptr->clientData == clientData && ptr->proc == eventProc) {
468 handler = ptr;
469 eventMask |= ptr->eventMask;
472 if (!handler) {
473 handler = wmalloc(sizeof(W_EventHandler));
475 WMPutInBag(view->eventHandlers, handler);
477 /* select events for window */
478 handler->eventMask = eventMask;
479 handler->proc = eventProc;
480 handler->clientData = clientData;
485 * WMDeleteEventHandler--
486 * Delete event handler matching arguments from windows
487 * event handler list.
490 void
491 WMDeleteEventHandler(WMView *view, unsigned long mask, WMEventProc *eventProc,
492 void *clientData)
494 W_EventHandler *handler, *ptr;
495 WMBagIterator iter;
497 handler = NULL;
499 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
500 if (ptr->eventMask == mask && ptr->proc == eventProc
501 && ptr->clientData == clientData) {
502 handler = ptr;
503 break;
507 if (!handler)
508 return;
510 WMRemoveFromBag(view->eventHandlers, handler);
512 wfree(handler);
517 void
518 W_CleanUpEvents(WMView *view)
520 W_EventHandler *ptr;
521 WMBagIterator iter;
523 WM_ITERATE_BAG(view->eventHandlers, ptr, iter) {
524 wfree(ptr);
530 static Time
531 getEventTime(WMScreen *screen, XEvent *event)
533 switch (event->type) {
534 case ButtonPress:
535 case ButtonRelease:
536 return event->xbutton.time;
537 case KeyPress:
538 case KeyRelease:
539 return event->xkey.time;
540 case MotionNotify:
541 return event->xmotion.time;
542 case EnterNotify:
543 case LeaveNotify:
544 return event->xcrossing.time;
545 case PropertyNotify:
546 return event->xproperty.time;
547 case SelectionClear:
548 return event->xselectionclear.time;
549 case SelectionRequest:
550 return event->xselectionrequest.time;
551 case SelectionNotify:
552 return event->xselection.time;
553 default:
554 return screen->lastEventTime;
559 void
560 W_CallDestroyHandlers(W_View *view)
562 XEvent event;
563 WMBagIterator iter;
564 W_EventHandler *hPtr;
566 event.type = DestroyNotify;
567 event.xdestroywindow.window = view->window;
568 event.xdestroywindow.event = view->window;
570 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
571 if (hPtr->eventMask & StructureNotifyMask) {
572 (*hPtr->proc)(&event, hPtr->clientData);
579 void
580 WMSetViewNextResponder(WMView *view, WMView *responder)
582 /* set the widget to receive keyboard events that aren't handled
583 * by this widget */
585 view->nextResponder = responder;
589 void
590 WMRelayToNextResponder(WMView *view, XEvent *event)
592 unsigned long mask = eventMasks[event->xany.type];
594 if (view->nextResponder) {
595 WMView *next = view->nextResponder;
596 W_EventHandler *hPtr;
597 WMBagIterator iter;
599 WM_ITERATE_BAG(next->eventHandlers, hPtr, iter) {
600 if ((hPtr->eventMask & mask)) {
601 (*hPtr->proc)(event, hPtr->clientData);
609 WMHandleEvent(XEvent *event)
611 W_EventHandler *hPtr;
612 W_View *view, *vPtr, *toplevel;
613 unsigned long mask;
614 Window window;
615 WMBagIterator iter;
617 if (event->type == MappingNotify) {
618 XRefreshKeyboardMapping(&event->xmapping);
619 return True;
622 mask = eventMasks[event->xany.type];
624 window = event->xany.window;
626 /* diferentiate SubstructureNotify with StructureNotify */
627 if (mask == StructureNotifyMask) {
628 if (event->xmap.event != event->xmap.window) {
629 mask = SubstructureNotifyMask;
630 window = event->xmap.event;
633 view = W_GetViewForXWindow(event->xany.display, window);
635 if (!view) {
636 if (extraEventHandler)
637 (extraEventHandler)(event);
639 return False;
642 view->screen->lastEventTime = getEventTime(view->screen, event);
644 toplevel = W_TopLevelOfView(view);
646 if (event->type == SelectionNotify || event->type == SelectionClear
647 || event->type == SelectionRequest) {
648 /* handle selection related events */
649 W_HandleSelectionEvent(event);
651 } else if (event->type == ClientMessage) {
653 W_HandleDNDClientMessage(toplevel, &event->xclient);
656 /* if it's a key event, redispatch it to the focused control */
657 if (mask & (KeyPressMask|KeyReleaseMask)) {
658 W_View *focused = W_FocusedViewOfToplevel(toplevel);
660 if (focused) {
661 view = focused;
665 /* compress Motion events */
666 if (event->type == MotionNotify && !view->flags.dontCompressMotion) {
667 while (XPending(event->xmotion.display)) {
668 XEvent ev;
669 XPeekEvent(event->xmotion.display, &ev);
670 if (ev.type == MotionNotify
671 && event->xmotion.window == ev.xmotion.window
672 && event->xmotion.subwindow == ev.xmotion.subwindow) {
673 /* replace events */
674 XNextEvent(event->xmotion.display, event);
675 } else break;
679 /* compress expose events */
680 if (event->type == Expose && !view->flags.dontCompressExpose) {
681 while (XCheckTypedWindowEvent(event->xexpose.display, view->window,
682 Expose, event));
686 if (view->screen->modalLoop && toplevel!=view->screen->modalView
687 && !toplevel->flags.worksWhenModal) {
688 if (event->type == KeyPress || event->type == KeyRelease
689 || event->type == MotionNotify || event->type == ButtonPress
690 || event->type == ButtonRelease
691 || event->type == FocusIn || event->type == FocusOut) {
692 return True;
696 /* do balloon stuffs */
697 if (event->type == EnterNotify)
698 W_BalloonHandleEnterView(view);
699 else if (event->type == LeaveNotify)
700 W_BalloonHandleLeaveView(view);
702 /* This is a hack. It will make the panel be secure while
703 * the event handlers are handled, as some event handler
704 * might destroy the widget. */
705 W_RetainView(toplevel);
707 WM_ITERATE_BAG(view->eventHandlers, hPtr, iter) {
708 if ((hPtr->eventMask & mask)) {
709 (*hPtr->proc)(event, hPtr->clientData);
712 #if 0
713 /* pass the event to the top level window of the widget */
714 /* TODO: change this to a responder chain */
715 if (view->parent != NULL) {
716 vPtr = view;
717 while (vPtr->parent != NULL)
718 vPtr = vPtr->parent;
720 WM_ITERATE_BAG(vPtr->eventHandlers, hPtr, iter) {
721 if (hPtr->eventMask & mask) {
722 (*hPtr->proc)(event, hPtr->clientData);
726 #endif
727 /* save button click info to track double-clicks */
728 if (view->screen->ignoreNextDoubleClick) {
729 view->screen->ignoreNextDoubleClick = 0;
730 } else {
731 if (event->type == ButtonPress) {
732 view->screen->lastClickWindow = event->xbutton.window;
733 view->screen->lastClickTime = event->xbutton.time;
737 W_ReleaseView(toplevel);
739 return True;
744 WMIsDoubleClick(XEvent *event)
746 W_View *view;
748 if (event->type != ButtonPress)
749 return False;
751 view = W_GetViewForXWindow(event->xany.display, event->xbutton.window);
753 if (!view)
754 return False;
756 if (view->screen->lastClickWindow != event->xbutton.window)
757 return False;
759 if (event->xbutton.time - view->screen->lastClickTime
760 < WINGsConfiguration.doubleClickDelay) {
761 view->screen->lastClickTime = 0;
762 view->screen->lastClickWindow = None;
763 view->screen->ignoreNextDoubleClick = 1;
764 return True;
765 } else
766 return False;
770 Bool
771 W_WaitForEvent(Display *dpy, unsigned long xeventmask)
773 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
774 struct pollfd *fds;
775 InputHandler *handler;
776 int count, timeout, nfds, i, retval;
778 if (inputHandler)
779 nfds = WMGetBagItemCount(inputHandler);
780 else
781 nfds = 0;
783 fds = wmalloc(nfds+1 * sizeof(struct pollfd));
784 /* put this to the end of array to avoid using ranges from 1 to nfds+1 */
785 fds[nfds].fd = ConnectionNumber(dpy);
786 fds[nfds].events = POLLIN;
788 for (i = 0; i<nfds; i++) {
789 handler = WMGetFromBag(inputHandler, i);
790 fds[i].fd = handler->fd;
791 fds[i].events = 0;
792 if (handler->mask & WIReadMask)
793 fds[i].events |= POLLIN;
795 if (handler->mask & WIWriteMask)
796 fds[i].events |= POLLOUT;
798 #if 0 /* FIXME */
799 if (handler->mask & WIExceptMask)
800 FD_SET(handler->fd, &eset);
801 #endif
805 * Setup the select() timeout to the estimated time until the
806 * next timer expires.
808 if (timerPending()) {
809 struct timeval tv;
810 delayUntilNextTimerEvent(&tv);
811 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
812 } else {
813 timeout = -1;
816 if (xeventmask==0) {
817 if (XPending(dpy))
818 return True;
819 } else {
820 XEvent ev;
821 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
822 XPutBackEvent(dpy, &ev);
823 return True;
827 count = poll(fds, nfds, timeout);
829 if (count>0 && nfds>0) {
830 WMBag *handlerCopy = WMCreateBag(nfds);
832 for (i=0; i<nfds; i++)
833 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
835 for (i=0; i<nfds; i++) {
836 int mask;
838 handler = WMGetFromBag(handlerCopy, i);
839 /* check if the handler still exist or was removed by a callback */
840 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
841 continue;
843 mask = 0;
845 if ((handler->mask & WIReadMask) &&
846 (fds[i].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI)))
847 mask |= WIReadMask;
849 if ((handler->mask & WIWriteMask) &&
850 (fds[i].revents & (POLLOUT | POLLWRBAND)))
851 mask |= WIWriteMask;
853 if ((handler->mask & WIExceptMask) &&
854 (fds[i].revents & (POLLHUP | POLLNVAL | POLLERR)))
855 mask |= WIExceptMask;
857 if (mask!=0 && handler->callback) {
858 (*handler->callback)(handler->fd, mask,
859 handler->clientData);
863 WMFreeBag(handlerCopy);
866 retval = fds[nfds].revents & (POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI);
867 wfree(fds);
869 W_FlushASAPNotificationQueue();
871 return retval;
872 #else /* not HAVE_POLL */
873 #ifdef HAVE_SELECT
874 struct timeval timeout;
875 struct timeval *timeoutPtr;
876 fd_set rset, wset, eset;
877 int maxfd, nfds, i;
878 int count;
879 InputHandler *handler;
881 FD_ZERO(&rset);
882 FD_ZERO(&wset);
883 FD_ZERO(&eset);
885 FD_SET(ConnectionNumber(dpy), &rset);
886 maxfd = ConnectionNumber(dpy);
888 if (inputHandler)
889 nfds = WMGetBagItemCount(inputHandler);
890 else
891 nfds = 0;
893 for (i=0; i<nfds; i++) {
894 handler = WMGetFromBag(inputHandler, i);
895 if (handler->mask & WIReadMask)
896 FD_SET(handler->fd, &rset);
898 if (handler->mask & WIWriteMask)
899 FD_SET(handler->fd, &wset);
901 if (handler->mask & WIExceptMask)
902 FD_SET(handler->fd, &eset);
904 if (maxfd < handler->fd)
905 maxfd = handler->fd;
910 * Setup the select() timeout to the estimated time until the
911 * next timer expires.
913 if (timerPending()) {
914 delayUntilNextTimerEvent(&timeout);
915 timeoutPtr = &timeout;
916 } else {
917 timeoutPtr = (struct timeval*)0;
920 XSync(dpy, False);
921 if (xeventmask==0) {
922 if (XPending(dpy))
923 return True;
924 } else {
925 XEvent ev;
926 if (XCheckMaskEvent(dpy, xeventmask, &ev)) {
927 XPutBackEvent(dpy, &ev);
928 return True;
932 count = select(1 + maxfd, &rset, &wset, &eset, timeoutPtr);
934 if (count>0 && nfds>0) {
935 WMBag *handlerCopy = WMCreateBag(nfds);
937 for (i=0; i<nfds; i++)
938 WMPutInBag(handlerCopy, WMGetFromBag(inputHandler, i));
940 for (i=0; i<nfds; i++) {
941 int mask;
943 handler = WMGetFromBag(handlerCopy, i);
944 /* check if the handler still exist or was removed by a callback */
945 if (WMGetFirstInBag(inputHandler, handler) == WBNotFound)
946 continue;
948 mask = 0;
950 if ((handler->mask & WIReadMask) && FD_ISSET(handler->fd, &rset))
951 mask |= WIReadMask;
953 if ((handler->mask & WIWriteMask) && FD_ISSET(handler->fd, &wset))
954 mask |= WIWriteMask;
956 if ((handler->mask & WIExceptMask) && FD_ISSET(handler->fd, &eset))
957 mask |= WIExceptMask;
959 if (mask!=0 && handler->callback) {
960 (*handler->callback)(handler->fd, mask,
961 handler->clientData);
965 WMFreeBag(handlerCopy);
968 W_FlushASAPNotificationQueue();
970 return FD_ISSET(ConnectionNumber(dpy), &rset);
971 #else /* not HAVE_SELECT, not HAVE_POLL */
972 Neither select nor poll. You lose.
973 #endif /* HAVE_SELECT */
974 #endif /* HAVE_POLL */
978 void
979 WMNextEvent(Display *dpy, XEvent *event)
981 /* Check any expired timers */
982 checkTimerHandlers();
984 while (XPending(dpy) == 0) {
985 /* Do idle stuff */
986 /* Do idle and timer stuff while there are no timer or X events */
987 while (XPending(dpy) == 0 && checkIdleHandlers()) {
988 /* dispatch timer events */
989 checkTimerHandlers();
993 * Make sure that new events did not arrive while we were doing
994 * timer/idle stuff. Or we might block forever waiting for
995 * an event that already arrived.
997 /* wait for something to happen or a timer to expire */
998 W_WaitForEvent(dpy, 0);
1000 /* Check any expired timers */
1001 checkTimerHandlers();
1004 XNextEvent(dpy, event);
1007 #if 0
1008 void
1009 WMMaskEvent(Display *dpy, long mask, XEvent *event)
1011 unsigned long milliseconds;
1012 struct timeval timeout;
1013 struct timeval *timeoutOrInfty;
1014 fd_set readset;
1016 while (!XCheckMaskEvent(dpy, mask, event)) {
1017 /* Do idle stuff while there are no timer or X events */
1018 while (checkIdleHandlers()) {
1019 if (XCheckMaskEvent(dpy, mask, event))
1020 return;
1024 * Setup the select() timeout to the estimated time until the
1025 * next timer expires.
1027 if (timerPending()) {
1028 delayUntilNextTimerEvent(&timeout);
1029 timeoutOrInfty = &timeout;
1030 } else {
1031 timeoutOrInfty = (struct timeval*)0;
1034 if (XCheckMaskEvent(dpy, mask, event))
1035 return;
1037 /* Wait for input on the X connection socket */
1038 FD_ZERO(&readset);
1039 FD_SET(ConnectionNumber(dpy), &readset);
1040 select(1 + ConnectionNumber(dpy), &readset, (fd_set*)0, (fd_set*)0,
1041 timeoutOrInfty);
1043 /* Check any expired timers */
1044 checkTimerHandlers();
1047 #endif
1048 #if 1
1050 * Cant use this because XPending() will make W_WaitForEvent
1051 * return even if the event in the queue is not what we want,
1052 * and if we block until some new event arrives from the
1053 * server, other events already in the queue (like Expose)
1054 * will be deferred.
1056 void
1057 WMMaskEvent(Display *dpy, long mask, XEvent *event)
1059 while (!XCheckMaskEvent(dpy, mask, event)) {
1060 /* Do idle stuff while there are no timer or X events */
1061 while (checkIdleHandlers()) {
1062 if (XCheckMaskEvent(dpy, mask, event))
1063 return;
1066 /* Wait for input on the X connection socket */
1067 W_WaitForEvent(dpy, mask);
1069 /* Check any expired timers */
1070 checkTimerHandlers();
1073 #endif
1075 Bool
1076 WMScreenPending(WMScreen *scr)
1078 if (XPending(scr->display))
1079 return True;
1080 else
1081 return False;
1085 WMEventHook*
1086 WMHookEventHandler(WMEventHook *handler)
1088 WMEventHook *oldHandler = extraEventHandler;
1090 extraEventHandler = handler;
1092 return oldHandler;