4 * This event handling stuff was inspired on Tk.
9 #include "../src/config.h"
11 #include <sys/types.h>
21 #ifdef HAVE_SYS_SELECT_H
22 # include <sys/select.h>
27 #ifndef X_GETTIMEOFDAY
28 #define X_GETTIMEOFDAY(t) gettimeofday(t, (struct timezone*)0)
36 typedef struct TimerHandler
{
37 WMCallback
*callback
; /* procedure to call */
38 struct timeval when
; /* when to call the callback */
40 struct TimerHandler
*next
;
44 typedef struct IdleHandler
{
50 typedef struct InputHandler
{
51 WMInputProc
*callback
;
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
,
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)
119 rightNow(struct timeval
*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)))
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;
140 WMAddTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
142 TimerHandler
*handler
, *tmp
;
144 handler
= malloc(sizeof(TimerHandler
));
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
;
159 while (tmp
->next
&& IS_AFTER(handler
->when
, tmp
->next
->when
)) {
162 handler
->next
= tmp
->next
;
171 WMDeleteTimerWithClientData(void *cdata
)
173 TimerHandler
*handler
, *tmp
;
175 if (!cdata
|| !timerHandler
)
179 if (tmp
->clientData
==cdata
) {
180 timerHandler
= tmp
->next
;
184 if (tmp
->next
->clientData
==cdata
) {
186 tmp
->next
= handler
->next
;
198 WMDeleteTimerHandler(WMHandlerID handlerID
)
200 TimerHandler
*tmp
, *handler
=(TimerHandler
*)handlerID
;
202 if (!handler
|| !timerHandler
)
207 timerHandler
= handler
->next
;
211 if (tmp
->next
==handler
) {
212 tmp
->next
=handler
->next
;
224 WMAddIdleHandler(WMCallback
*callback
, void *cdata
)
226 IdleHandler
*handler
;
228 handler
= malloc(sizeof(IdleHandler
));
232 handler
->callback
= callback
;
233 handler
->clientData
= cdata
;
234 /* add handler at end of queue */
236 idleHandler
= WMCreateBag(16);
238 WMPutInBag(idleHandler
, handler
);
246 WMDeleteIdleHandler(WMHandlerID handlerID
)
248 IdleHandler
*handler
= (IdleHandler
*)handlerID
;
251 if (!handler
|| !idleHandler
)
254 pos
= WMGetFirstInBag(idleHandler
, handler
);
255 if (pos
!= WBNotFound
) {
257 WMDeleteFromBag(idleHandler
, pos
);
264 WMAddInputHandler(int fd
, int condition
, WMInputProc
*proc
, void *clientData
)
266 InputHandler
*handler
;
268 handler
= wmalloc(sizeof(InputHandler
));
271 handler
->mask
= condition
;
272 handler
->callback
= proc
;
273 handler
->clientData
= clientData
;
276 inputHandler
= WMCreateBag(16);
277 WMPutInBag(inputHandler
, handler
);
285 WMDeleteInputHandler(WMHandlerID handlerID
)
287 InputHandler
*handler
= (InputHandler
*)handlerID
;
290 if (!handler
|| !inputHandler
)
293 pos
= WMGetFirstInBag(inputHandler
, handler
);
294 if (pos
!= WBNotFound
) {
296 WMDeleteFromBag(inputHandler
, pos
);
305 IdleHandler
*handler
;
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
);
320 handler
= WMBagNext(handlerCopy
, &iter
)) {
321 /* check if the handler still exist or was removed by a callback */
322 if (WMGetFirstInBag(idleHandler
, handler
) == WBNotFound
)
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);
342 TimerHandler
*handler
;
346 W_FlushASAPNotificationQueue();
352 while (timerHandler
&& IS_AFTER(now
, timerHandler
->when
)) {
353 handler
= timerHandler
;
354 timerHandler
= timerHandler
->next
;
355 handler
->next
= NULL
;
356 (*handler
->callback
)(handler
->clientData
);
360 W_FlushASAPNotificationQueue();
366 delayUntilNextTimerEvent(struct timeval
*delay
)
371 /* The return value of this function is only valid if there _are_
379 if (IS_AFTER(now
, timerHandler
->when
)) {
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;
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.
403 WMCreateEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
406 W_EventHandler
*handler
, *ptr
;
407 unsigned long eventMask
;
414 WM_ITERATE_BAG(view
->eventHandlers
, ptr
, iter
) {
415 if (ptr
->clientData
== clientData
&& ptr
->proc
== eventProc
) {
417 eventMask
|= ptr
->eventMask
;
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.
439 WMDeleteEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
442 W_EventHandler
*handler
, *ptr
;
447 WM_ITERATE_BAG(view
->eventHandlers
, ptr
, iter
) {
448 if (ptr
->eventMask
== mask
&& ptr
->proc
== eventProc
449 && ptr
->clientData
== clientData
) {
458 WMRemoveFromBag(view
->eventHandlers
, handler
);
466 W_CleanUpEvents(WMView
*view
)
471 WM_ITERATE_BAG(view
->eventHandlers
, ptr
, iter
) {
479 getEventTime(WMScreen
*screen
, XEvent
*event
)
481 switch (event
->type
) {
484 return event
->xbutton
.time
;
487 return event
->xkey
.time
;
489 return event
->xmotion
.time
;
492 return event
->xcrossing
.time
;
494 return event
->xproperty
.time
;
496 return event
->xselectionclear
.time
;
497 case SelectionRequest
:
498 return event
->xselectionrequest
.time
;
499 case SelectionNotify
:
500 return event
->xselection
.time
;
502 return screen
->lastEventTime
;
508 W_CallDestroyHandlers(W_View
*view
)
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
;
535 if (event
->type
== MappingNotify
) {
536 XRefreshKeyboardMapping(&event
->xmapping
);
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
);
554 if (extraEventHandler
)
555 (extraEventHandler
)(event
);
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
);
583 /* compress Motion events */
584 if (event
->type
== MotionNotify
&& !view
->flags
.dontCompressMotion
) {
585 while (XPending(event
->xmotion
.display
)) {
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
) {
592 XNextEvent(event
->xmotion
.display
, event
);
597 /* compress expose events */
598 if (event
->type
== Expose
&& !view
->flags
.dontCompressExpose
) {
599 while (XCheckTypedWindowEvent(event
->xexpose
.display
, view
->window
,
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
) {
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
) {
635 while (vPtr
->parent
!= NULL
)
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;
649 if (event
->type
== ButtonPress
) {
650 view
->screen
->lastClickWindow
= event
->xbutton
.window
;
651 view
->screen
->lastClickTime
= event
->xbutton
.time
;
655 W_ReleaseView(toplevel
);
662 WMIsDoubleClick(XEvent
*event
)
666 if (event
->type
!= ButtonPress
)
669 view
= W_GetViewForXWindow(event
->xany
.display
, event
->xbutton
.window
);
674 if (view
->screen
->lastClickWindow
!= event
->xbutton
.window
)
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;
689 W_WaitForEvent(Display
*dpy
, unsigned long xeventmask
)
691 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
693 InputHandler
*handler
;
694 int count
, timeout
, nfds
, i
, retval
;
697 nfds
= WMGetBagItemCount(inputHandler
);
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
;
710 if (handler
->mask
& WIReadMask
)
711 fds
[i
].events
|= POLLIN
;
713 if (handler
->mask
& WIWriteMask
)
714 fds
[i
].events
|= POLLOUT
;
717 if (handler
->mask
& WIExceptMask
)
718 FD_SET(handler
->fd
, &eset
);
723 * Setup the select() timeout to the estimated time until the
724 * next timer expires.
726 if (timerPending()) {
728 delayUntilNextTimerEvent(&tv
);
729 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
739 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
740 XPutBackEvent(dpy
, &ev
);
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
++) {
756 handler
= WMGetFromBag(handlerCopy
, i
);
757 /* check if the handler still exist or was removed by a callback */
758 if (WMGetFirstInBag(inputHandler
, handler
) == WBNotFound
)
763 if ((handler
->mask
& WIReadMask
) &&
764 (fds
[i
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
)))
767 if ((handler
->mask
& WIWriteMask
) &&
768 (fds
[i
].revents
& (POLLOUT
| POLLWRBAND
)))
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
);
787 W_FlushASAPNotificationQueue();
790 #else /* not HAVE_POLL */
792 struct timeval timeout
;
793 struct timeval
*timeoutPtr
;
794 fd_set rset
, wset
, eset
;
797 InputHandler
*handler
;
803 FD_SET(ConnectionNumber(dpy
), &rset
);
804 maxfd
= ConnectionNumber(dpy
);
807 nfds
= WMGetBagItemCount(inputHandler
);
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
)
828 * Setup the select() timeout to the estimated time until the
829 * next timer expires.
831 if (timerPending()) {
832 delayUntilNextTimerEvent(&timeout
);
833 timeoutPtr
= &timeout
;
835 timeoutPtr
= (struct timeval
*)0;
844 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
845 XPutBackEvent(dpy
, &ev
);
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
++) {
861 handler
= WMGetFromBag(handlerCopy
, i
);
862 /* check if the handler still exist or was removed by a callback */
863 if (WMGetFirstInBag(inputHandler
, handler
) == WBNotFound
)
868 if ((handler
->mask
& WIReadMask
) && FD_ISSET(handler
->fd
, &rset
))
871 if ((handler
->mask
& WIWriteMask
) && FD_ISSET(handler
->fd
, &wset
))
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 */
897 WMNextEvent(Display
*dpy
, XEvent
*event
)
899 /* Check any expired timers */
900 checkTimerHandlers();
902 while (XPending(dpy
) == 0) {
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
);
927 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
929 unsigned long milliseconds
;
930 struct timeval timeout
;
931 struct timeval
*timeoutOrInfty
;
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
))
942 * Setup the select() timeout to the estimated time until the
943 * next timer expires.
945 if (timerPending()) {
946 delayUntilNextTimerEvent(&timeout
);
947 timeoutOrInfty
= &timeout
;
949 timeoutOrInfty
= (struct timeval
*)0;
952 if (XCheckMaskEvent(dpy
, mask
, event
))
955 /* Wait for input on the X connection socket */
957 FD_SET(ConnectionNumber(dpy
), &readset
);
958 select(1 + ConnectionNumber(dpy
), &readset
, (fd_set
*)0, (fd_set
*)0,
961 /* Check any expired timers */
962 checkTimerHandlers();
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)
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
))
984 /* Wait for input on the X connection socket */
985 W_WaitForEvent(dpy
, mask
);
987 /* Check any expired timers */
988 checkTimerHandlers();
994 WMScreenPending(WMScreen
*scr
)
996 if (XPending(scr
->display
))
1004 WMHookEventHandler(WMEventHook
*handler
)
1006 WMEventHook
*oldHandler
= extraEventHandler
;
1008 extraEventHandler
= handler
;