4 * This event handling stuff was based 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
);
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
);
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 n
= WMGetBagItemCount(idleHandler
);
316 handlerCopy
= WMCreateBag(n
);
318 WMPutInBag(handlerCopy
, WMGetFromBag(idleHandler
, i
));
320 for (i
=0; i
<n
; i
++) {
321 handler
= WMGetFromBag(handlerCopy
, i
);
322 /* check if the handler still exist or was removed by a callback */
323 if (WMGetFirstInBag(idleHandler
, handler
)<0)
326 (*handler
->callback
)(handler
->clientData
);
327 WMDeleteIdleHandler(handler
);
330 WMFreeBag(handlerCopy
);
332 W_FlushIdleNotificationQueue();
334 /* this is not necesarrily False, because one handler can re-add itself */
335 return (WMGetBagItemCount(idleHandler
)>0);
343 TimerHandler
*handler
;
347 W_FlushASAPNotificationQueue();
353 while (timerHandler
&& IS_AFTER(now
, timerHandler
->when
)) {
354 handler
= timerHandler
;
355 timerHandler
= timerHandler
->next
;
356 handler
->next
= NULL
;
357 (*handler
->callback
)(handler
->clientData
);
361 W_FlushASAPNotificationQueue();
367 delayUntilNextTimerEvent(struct timeval
*delay
)
372 /* The return value of this function is only valid if there _are_
380 if (IS_AFTER(now
, timerHandler
->when
)) {
384 delay
->tv_sec
= timerHandler
->when
.tv_sec
- now
.tv_sec
;
385 delay
->tv_usec
= timerHandler
->when
.tv_usec
- now
.tv_usec
;
386 if (delay
->tv_usec
< 0) {
387 delay
->tv_usec
+= 1000000;
397 * WMCreateEventHandler--
398 * Create an event handler and put it in the event handler list for the
399 * view. If the same callback and clientdata are already used in another
400 * handler, the masks are swapped.
404 WMCreateEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
407 W_EventHandler
*handler
;
408 W_EventHandler
*ptr
= view
->handlerList
;
409 unsigned long eventMask
;
412 handler
= wmalloc(sizeof(W_EventHandler
));
414 handler
->nextHandler
= NULL
;
416 view
->handlerList
= handler
;
422 while (ptr
!= NULL
) {
423 if (ptr
->clientData
== clientData
&& ptr
->proc
== eventProc
) {
426 eventMask
|= ptr
->eventMask
;
428 ptr
= ptr
->nextHandler
;
431 handler
= wmalloc(sizeof(W_EventHandler
));
432 handler
->nextHandler
= view
->handlerList
;
433 view
->handlerList
= handler
;
437 /* select events for window */
438 handler
->eventMask
= mask
;
439 handler
->proc
= eventProc
;
440 handler
->clientData
= clientData
;
445 * WMDeleteEventHandler--
446 * Delete event handler matching arguments from windows
447 * event handler list.
451 WMDeleteEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
454 W_EventHandler
*handler
, *ptr
, *pptr
;
456 ptr
= view
->handlerList
;
462 if (ptr
->eventMask
== mask
&& ptr
->proc
== eventProc
463 && ptr
->clientData
== clientData
) {
468 ptr
= ptr
->nextHandler
;
475 view
->handlerList
= handler
->nextHandler
;
477 pptr
->nextHandler
= handler
->nextHandler
;
485 W_CleanUpEvents(WMView
*view
)
487 W_EventHandler
*ptr
, *nptr
;
489 ptr
= view
->handlerList
;
492 nptr
= ptr
->nextHandler
;
501 getEventTime(WMScreen
*screen
, XEvent
*event
)
503 switch (event
->type
) {
506 return event
->xbutton
.time
;
509 return event
->xkey
.time
;
511 return event
->xmotion
.time
;
514 return event
->xcrossing
.time
;
516 return event
->xproperty
.time
;
518 return event
->xselectionclear
.time
;
519 case SelectionRequest
:
520 return event
->xselectionrequest
.time
;
521 case SelectionNotify
:
522 return event
->xselection
.time
;
524 return screen
->lastEventTime
;
530 W_CallDestroyHandlers(W_View
*view
)
533 W_EventHandler
*hPtr
;
535 event
.type
= DestroyNotify
;
536 event
.xdestroywindow
.window
= view
->window
;
537 event
.xdestroywindow
.event
= view
->window
;
538 hPtr
= view
->handlerList
;
540 if (hPtr
->eventMask
& StructureNotifyMask
) {
541 (*hPtr
->proc
)(&event
, hPtr
->clientData
);
544 hPtr
= hPtr
->nextHandler
;
550 WMHandleEvent(XEvent
*event
)
552 W_EventHandler
*hPtr
;
553 W_View
*view
, *vPtr
, *toplevel
;
557 if (event
->type
== MappingNotify
) {
558 XRefreshKeyboardMapping(&event
->xmapping
);
562 mask
= eventMasks
[event
->xany
.type
];
564 window
= event
->xany
.window
;
566 /* diferentiate SubstructureNotify with StructureNotify */
567 if (mask
== StructureNotifyMask
) {
568 if (event
->xmap
.event
!= event
->xmap
.window
) {
569 mask
= SubstructureNotifyMask
;
570 window
= event
->xmap
.event
;
573 view
= W_GetViewForXWindow(event
->xany
.display
, window
);
575 if (extraEventHandler
)
576 (extraEventHandler
)(event
);
581 view
->screen
->lastEventTime
= getEventTime(view
->screen
, event
);
583 toplevel
= W_TopLevelOfView(view
);
585 if (event
->type
== SelectionNotify
|| event
->type
== SelectionClear
586 || event
->type
== SelectionRequest
) {
587 /* handle selection related events */
588 W_HandleSelectionEvent(event
);
591 /* if it's a key event, redispatch it to the focused control */
592 if (mask
& (KeyPressMask
|KeyReleaseMask
)) {
593 W_View
*focused
= W_FocusedViewOfToplevel(toplevel
);
600 /* compress Motion events */
601 if (event
->type
== MotionNotify
&& !view
->flags
.dontCompressMotion
) {
602 while (XPending(event
->xmotion
.display
)) {
604 XPeekEvent(event
->xmotion
.display
, &ev
);
605 if (ev
.type
== MotionNotify
606 && event
->xmotion
.window
== ev
.xmotion
.window
607 && event
->xmotion
.subwindow
== ev
.xmotion
.subwindow
) {
609 XNextEvent(event
->xmotion
.display
, event
);
614 /* compress expose events */
615 if (event
->type
== Expose
&& !view
->flags
.dontCompressExpose
) {
616 while (XCheckTypedWindowEvent(event
->xexpose
.display
, view
->window
,
621 if (view
->screen
->modal
&& toplevel
!=view
->screen
->modalView
622 && !toplevel
->flags
.worksWhenModal
) {
623 if (event
->type
== KeyPress
|| event
->type
== KeyRelease
624 || event
->type
== MotionNotify
|| event
->type
== ButtonPress
625 || event
->type
== ButtonRelease
626 || event
->type
== FocusIn
|| event
->type
== FocusOut
) {
631 /* do balloon stuffs */
632 if (event
->type
== EnterNotify
)
633 W_BalloonHandleEnterView(view
);
634 else if (event
->type
== LeaveNotify
)
635 W_BalloonHandleLeaveView(view
);
637 /* This is a hack. It will make the panel be secure while
638 * the event handlers are handled, as some event handler
639 * might destroy the widget. */
640 W_RetainView(toplevel
);
642 hPtr
= view
->handlerList
;
647 tmp
= hPtr
->nextHandler
;
649 if ((hPtr
->eventMask
& mask
)) {
650 (*hPtr
->proc
)(event
, hPtr
->clientData
);
656 /* pass the event to the top level window of the widget */
657 if (view
->parent
!=NULL
) {
659 while (vPtr
->parent
!=NULL
)
662 hPtr
= vPtr
->handlerList
;
666 if (hPtr
->eventMask
& mask
) {
667 (*hPtr
->proc
)(event
, hPtr
->clientData
);
669 hPtr
= hPtr
->nextHandler
;
673 /* save button click info to track double-clicks */
674 if (view
->screen
->ignoreNextDoubleClick
) {
675 view
->screen
->ignoreNextDoubleClick
= 0;
677 if (event
->type
== ButtonPress
) {
678 view
->screen
->lastClickWindow
= event
->xbutton
.window
;
679 view
->screen
->lastClickTime
= event
->xbutton
.time
;
683 W_ReleaseView(toplevel
);
690 WMIsDoubleClick(XEvent
*event
)
694 if (event
->type
!= ButtonPress
)
697 view
= W_GetViewForXWindow(event
->xany
.display
, event
->xbutton
.window
);
702 if (view
->screen
->lastClickWindow
!= event
->xbutton
.window
)
705 if (event
->xbutton
.time
- view
->screen
->lastClickTime
706 < WINGsConfiguration
.doubleClickDelay
) {
707 view
->screen
->lastClickTime
= 0;
708 view
->screen
->lastClickWindow
= None
;
709 view
->screen
->ignoreNextDoubleClick
= 1;
717 W_WaitForEvent(Display
*dpy
, unsigned long xeventmask
)
719 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
721 InputHandler
*handler
;
722 int count
, timeout
, nfds
, i
, retval
;
725 nfds
= WMGetBagItemCount(inputHandler
);
729 fds
= wmalloc(nfds
+1 * sizeof(struct pollfd
));
730 /* put this to the end of array to avoid using ranges from 1 to nfds+1 */
731 fds
[nfds
].fd
= ConnectionNumber(dpy
);
732 fds
[nfds
].events
= POLLIN
;
734 for (i
= 0; i
<nfds
; i
++) {
735 handler
= WMGetFromBag(inputHandler
, i
);
736 fds
[i
].fd
= handler
->fd
;
738 if (handler
->mask
& WIReadMask
)
739 fds
[i
].events
|= POLLIN
;
741 if (handler
->mask
& WIWriteMask
)
742 fds
[i
].events
|= POLLOUT
;
745 if (handler
->mask
& WIExceptMask
)
746 FD_SET(handler
->fd
, &eset
);
751 * Setup the select() timeout to the estimated time until the
752 * next timer expires.
754 if (timerPending()) {
756 delayUntilNextTimerEvent(&tv
);
757 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
767 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
768 XPutBackEvent(dpy
, &ev
);
773 count
= poll(fds
, nfds
, timeout
);
775 if (count
>0 && nfds
>0) {
776 WMBag
*handlerCopy
= WMCreateBag(nfds
);
778 for (i
=0; i
<nfds
; i
++)
779 WMPutInBag(handlerCopy
, WMGetFromBag(inputHandler
, i
));
781 for (i
=0; i
<nfds
; i
++) {
784 handler
= WMGetFromBag(handlerCopy
, i
);
785 /* check if the handler still exist or was removed by a callback */
786 if (WMGetFirstInBag(inputHandler
, handler
)<0)
791 if ((handler
->mask
& WIReadMask
) &&
792 (fds
[i
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
)))
795 if ((handler
->mask
& WIWriteMask
) &&
796 (fds
[i
].revents
& (POLLOUT
| POLLWRBAND
)))
799 if ((handler
->mask
& WIExceptMask
) &&
800 (fds
[i
].revents
& (POLLHUP
| POLLNVAL
| POLLERR
)))
801 mask
|= WIExceptMask
;
803 if (mask
!=0 && handler
->callback
) {
804 (*handler
->callback
)(handler
->fd
, mask
,
805 handler
->clientData
);
809 WMFreeBag(handlerCopy
);
812 retval
= fds
[nfds
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
);
815 W_FlushASAPNotificationQueue();
818 #else /* not HAVE_POLL */
820 struct timeval timeout
;
821 struct timeval
*timeoutPtr
;
822 fd_set rset
, wset
, eset
;
825 InputHandler
*handler
;
831 FD_SET(ConnectionNumber(dpy
), &rset
);
832 maxfd
= ConnectionNumber(dpy
);
835 nfds
= WMGetBagItemCount(inputHandler
);
839 for (i
=0; i
<nfds
; i
++) {
840 handler
= WMGetFromBag(inputHandler
, i
);
841 if (handler
->mask
& WIReadMask
)
842 FD_SET(handler
->fd
, &rset
);
844 if (handler
->mask
& WIWriteMask
)
845 FD_SET(handler
->fd
, &wset
);
847 if (handler
->mask
& WIExceptMask
)
848 FD_SET(handler
->fd
, &eset
);
850 if (maxfd
< handler
->fd
)
856 * Setup the select() timeout to the estimated time until the
857 * next timer expires.
859 if (timerPending()) {
860 delayUntilNextTimerEvent(&timeout
);
861 timeoutPtr
= &timeout
;
863 timeoutPtr
= (struct timeval
*)0;
872 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
873 XPutBackEvent(dpy
, &ev
);
878 count
= select(1 + maxfd
, &rset
, &wset
, &eset
, timeoutPtr
);
880 if (count
>0 && nfds
>0) {
881 WMBag
*handlerCopy
= WMCreateBag(nfds
);
883 for (i
=0; i
<nfds
; i
++)
884 WMPutInBag(handlerCopy
, WMGetFromBag(inputHandler
, i
));
886 for (i
=0; i
<nfds
; i
++) {
889 handler
= WMGetFromBag(handlerCopy
, i
);
890 /* check if the handler still exist or was removed by a callback */
891 if (WMGetFirstInBag(inputHandler
, handler
)<0)
896 if ((handler
->mask
& WIReadMask
) && FD_ISSET(handler
->fd
, &rset
))
899 if ((handler
->mask
& WIWriteMask
) && FD_ISSET(handler
->fd
, &wset
))
902 if ((handler
->mask
& WIExceptMask
) && FD_ISSET(handler
->fd
, &eset
))
903 mask
|= WIExceptMask
;
905 if (mask
!=0 && handler
->callback
) {
906 (*handler
->callback
)(handler
->fd
, mask
,
907 handler
->clientData
);
911 WMFreeBag(handlerCopy
);
914 W_FlushASAPNotificationQueue();
916 return FD_ISSET(ConnectionNumber(dpy
), &rset
);
917 #else /* not HAVE_SELECT, not HAVE_POLL */
918 Neither select nor poll
. You lose
.
919 #endif /* HAVE_SELECT */
920 #endif /* HAVE_POLL */
925 WMNextEvent(Display
*dpy
, XEvent
*event
)
927 /* Check any expired timers */
928 checkTimerHandlers();
930 while (XPending(dpy
) == 0) {
932 /* Do idle and timer stuff while there are no timer or X events */
933 while (!XPending(dpy
) && checkIdleHandlers()) {
934 /* dispatch timer events */
935 checkTimerHandlers();
939 * Make sure that new events did not arrive while we were doing
940 * timer/idle stuff. Or we might block forever waiting for
941 * an event that already arrived.
943 /* wait to something happen */
944 W_WaitForEvent(dpy
, 0);
946 /* Check any expired timers */
947 checkTimerHandlers();
950 XNextEvent(dpy
, event
);
955 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
957 unsigned long milliseconds
;
958 struct timeval timeout
;
959 struct timeval
*timeoutOrInfty
;
962 while (!XCheckMaskEvent(dpy
, mask
, event
)) {
963 /* Do idle stuff while there are no timer or X events */
964 while (checkIdleHandlers()) {
965 if (XCheckMaskEvent(dpy
, mask
, event
))
970 * Setup the select() timeout to the estimated time until the
971 * next timer expires.
973 if (timerPending()) {
974 delayUntilNextTimerEvent(&timeout
);
975 timeoutOrInfty
= &timeout
;
977 timeoutOrInfty
= (struct timeval
*)0;
980 if (XCheckMaskEvent(dpy
, mask
, event
))
983 /* Wait for input on the X connection socket */
985 FD_SET(ConnectionNumber(dpy
), &readset
);
986 select(1 + ConnectionNumber(dpy
), &readset
, (fd_set
*)0, (fd_set
*)0,
989 /* Check any expired timers */
990 checkTimerHandlers();
996 * Cant use this because XPending() will make W_WaitForEvent
997 * return even if the event in the queue is not what we want,
998 * and if we block until some new event arrives from the
999 * server, other events already in the queue (like Expose)
1003 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
1005 while (!XCheckMaskEvent(dpy
, mask
, event
)) {
1006 /* Do idle stuff while there are no timer or X events */
1007 while (checkIdleHandlers()) {
1008 if (XCheckMaskEvent(dpy
, mask
, event
))
1012 /* Wait for input on the X connection socket */
1013 W_WaitForEvent(dpy
, mask
);
1015 /* Check any expired timers */
1016 checkTimerHandlers();
1022 WMScreenPending(WMScreen
*scr
)
1024 if (XPending(scr
->display
))
1032 WMHookEventHandler(WMEventHook
*handler
)
1034 WMEventHook
*oldHandler
= extraEventHandler
;
1036 extraEventHandler
= handler
;