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)
33 extern _WINGsConfiguration WINGsConfiguration
;
37 typedef struct TimerHandler
{
38 WMCallback
*callback
; /* procedure to call */
39 struct timeval when
; /* when to call the callback */
41 struct TimerHandler
*next
;
45 typedef struct IdleHandler
{
48 struct IdleHandler
*next
;
52 typedef struct InputHandler
{
53 WMInputProc
*callback
;
57 struct InputHandler
*next
;
61 /* table to map event types to event masks */
62 static unsigned long eventMasks
[] = {
65 KeyPressMask
, /* KeyPress */
66 KeyReleaseMask
, /* KeyRelease */
67 ButtonPressMask
, /* ButtonPress */
68 ButtonReleaseMask
, /* ButtonRelease */
69 PointerMotionMask
|PointerMotionHintMask
|ButtonMotionMask
70 |Button1MotionMask
|Button2MotionMask
|Button3MotionMask
71 |Button4MotionMask
|Button5MotionMask
,
73 EnterWindowMask
, /* EnterNotify */
74 LeaveWindowMask
, /* LeaveNotify */
75 FocusChangeMask
, /* FocusIn */
76 FocusChangeMask
, /* FocusOut */
77 KeymapStateMask
, /* KeymapNotify */
78 ExposureMask
, /* Expose */
79 ExposureMask
, /* GraphicsExpose */
80 ExposureMask
, /* NoExpose */
81 VisibilityChangeMask
, /* VisibilityNotify */
82 SubstructureNotifyMask
, /* CreateNotify */
83 StructureNotifyMask
, /* DestroyNotify */
84 StructureNotifyMask
, /* UnmapNotify */
85 StructureNotifyMask
, /* MapNotify */
86 SubstructureRedirectMask
, /* MapRequest */
87 StructureNotifyMask
, /* ReparentNotify */
88 StructureNotifyMask
, /* ConfigureNotify */
89 SubstructureRedirectMask
, /* ConfigureRequest */
90 StructureNotifyMask
, /* GravityNotify */
91 ResizeRedirectMask
, /* ResizeRequest */
92 StructureNotifyMask
, /* CirculateNotify */
93 SubstructureRedirectMask
, /* CirculateRequest */
94 PropertyChangeMask
, /* PropertyNotify */
95 0, /* SelectionClear */
96 0, /* SelectionRequest */
97 0, /* SelectionNotify */
98 ColormapChangeMask
, /* ColormapNotify */
99 ClientMessageMask
, /* ClientMessage */
100 0, /* Mapping Notify */
105 /* queue of timer event handlers */
106 static TimerHandler
*timerHandler
=NULL
;
108 static IdleHandler
*idleHandler
=NULL
;
110 static InputHandler
*inputHandler
=NULL
;
112 /* hook for other toolkits or wmaker process their events */
113 static WMEventHook
*extraEventHandler
=NULL
;
117 #define timerPending() (timerHandler)
119 #define idlePending() (idleHandler)
123 rightNow(struct timeval
*tv
) {
127 /* is t1 after t2 ? */
128 #define IS_AFTER(t1, t2) (((t1).tv_sec > (t2).tv_sec) || \
129 (((t1).tv_sec == (t2).tv_sec) \
130 && ((t1).tv_usec > (t2).tv_usec)))
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;
144 WMAddTimerHandler(int milliseconds
, WMCallback
*callback
, void *cdata
)
146 TimerHandler
*handler
, *tmp
;
148 handler
= malloc(sizeof(TimerHandler
));
152 rightNow(&handler
->when
);
153 addmillisecs(&handler
->when
, milliseconds
);
154 handler
->callback
= callback
;
155 handler
->clientData
= cdata
;
156 /* insert callback in queue, sorted by time left */
157 if (!timerHandler
|| !IS_AFTER(handler
->when
, timerHandler
->when
)) {
158 /* first in the queue */
159 handler
->next
= timerHandler
;
160 timerHandler
= handler
;
163 while (tmp
->next
&& IS_AFTER(handler
->when
, tmp
->next
->when
)) {
166 handler
->next
= tmp
->next
;
175 WMDeleteTimerWithClientData(void *cdata
)
177 TimerHandler
*handler
, *tmp
;
179 if (!cdata
|| !timerHandler
)
183 if (tmp
->clientData
==cdata
) {
184 timerHandler
= tmp
->next
;
188 if (tmp
->next
->clientData
==cdata
) {
190 tmp
->next
= handler
->next
;
202 WMDeleteTimerHandler(WMHandlerID handlerID
)
204 TimerHandler
*tmp
, *handler
=(TimerHandler
*)handlerID
;
206 if (!handler
|| !timerHandler
)
211 timerHandler
= handler
->next
;
215 if (tmp
->next
==handler
) {
216 tmp
->next
=handler
->next
;
228 WMAddIdleHandler(WMCallback
*callback
, void *cdata
)
230 IdleHandler
*handler
, *tmp
;
232 handler
= malloc(sizeof(IdleHandler
));
236 handler
->callback
= callback
;
237 handler
->clientData
= cdata
;
238 handler
->next
= NULL
;
239 /* add callback at end of queue */
241 idleHandler
= handler
;
256 WMDeleteIdleHandler(WMHandlerID handlerID
)
258 IdleHandler
*tmp
, *handler
= (IdleHandler
*)handlerID
;
260 if (!handler
|| !idleHandler
)
264 if (tmp
== handler
) {
265 idleHandler
= handler
->next
;
269 if (tmp
->next
== handler
) {
270 tmp
->next
= handler
->next
;
282 WMAddInputHandler(int fd
, int condition
, WMInputProc
*proc
, void *clientData
)
284 InputHandler
*handler
;
286 handler
= wmalloc(sizeof(InputHandler
));
289 handler
->mask
= condition
;
290 handler
->callback
= proc
;
291 handler
->clientData
= clientData
;
293 handler
->next
= inputHandler
;
295 inputHandler
= handler
;
302 WMDeleteInputHandler(WMHandlerID handlerID
)
304 InputHandler
*tmp
, *handler
= (InputHandler
*)handlerID
;
306 if (!handler
|| !inputHandler
)
310 if (tmp
== handler
) {
311 inputHandler
= handler
->next
;
315 if (tmp
->next
== handler
) {
316 tmp
->next
= handler
->next
;
329 IdleHandler
*handler
, *tmp
;
332 W_FlushIdleNotificationQueue();
336 handler
= idleHandler
;
338 /* we will process all idleHandlers so, empty the handler list */
343 (*handler
->callback
)(handler
->clientData
);
344 /* remove the handler */
349 W_FlushIdleNotificationQueue();
357 TimerHandler
*handler
;
362 while (timerHandler
&& IS_AFTER(now
, timerHandler
->when
)) {
363 handler
= timerHandler
;
364 timerHandler
= timerHandler
->next
;
365 handler
->next
= NULL
;
366 (*handler
->callback
)(handler
->clientData
);
370 W_FlushASAPNotificationQueue();
376 delayUntilNextTimerEvent(struct timeval
*delay
)
381 /* The return value of this function is only valid if there _are_
389 if (IS_AFTER(now
, timerHandler
->when
)) {
393 delay
->tv_sec
= timerHandler
->when
.tv_sec
- now
.tv_sec
;
394 delay
->tv_usec
= timerHandler
->when
.tv_usec
- now
.tv_usec
;
395 if (delay
->tv_usec
< 0) {
396 delay
->tv_usec
+= 1000000;
406 * WMCreateEventHandler--
407 * Create an event handler and put it in the event handler list for the
408 * view. If the same callback and clientdata are already used in another
409 * handler, the masks are swapped.
413 WMCreateEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
416 W_EventHandler
*handler
;
417 W_EventHandler
*ptr
= view
->handlerList
;
418 unsigned long eventMask
;
421 handler
= wmalloc(sizeof(W_EventHandler
));
423 handler
->nextHandler
= NULL
;
425 view
->handlerList
= handler
;
431 while (ptr
!= NULL
) {
432 if (ptr
->clientData
== clientData
&& ptr
->proc
== eventProc
) {
435 eventMask
|= ptr
->eventMask
;
437 ptr
= ptr
->nextHandler
;
440 handler
= wmalloc(sizeof(W_EventHandler
));
441 handler
->nextHandler
= view
->handlerList
;
442 view
->handlerList
= handler
;
446 /* select events for window */
447 handler
->eventMask
= mask
;
448 handler
->proc
= eventProc
;
449 handler
->clientData
= clientData
;
454 * WMDeleteEventHandler--
455 * Delete event handler matching arguments from windows
456 * event handler list.
460 WMDeleteEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
463 W_EventHandler
*handler
, *ptr
, *pptr
;
465 ptr
= view
->handlerList
;
471 if (ptr
->eventMask
== mask
&& ptr
->proc
== eventProc
472 && ptr
->clientData
== clientData
) {
477 ptr
= ptr
->nextHandler
;
484 view
->handlerList
= handler
->nextHandler
;
486 pptr
->nextHandler
= handler
->nextHandler
;
494 W_CleanUpEvents(WMView
*view
)
496 W_EventHandler
*ptr
, *nptr
;
498 ptr
= view
->handlerList
;
501 nptr
= ptr
->nextHandler
;
510 getEventTime(WMScreen
*screen
, XEvent
*event
)
512 switch (event
->type
) {
515 return event
->xbutton
.time
;
518 return event
->xkey
.time
;
520 return event
->xmotion
.time
;
523 return event
->xcrossing
.time
;
525 return event
->xproperty
.time
;
527 return event
->xselectionclear
.time
;
528 case SelectionRequest
:
529 return event
->xselectionrequest
.time
;
530 case SelectionNotify
:
531 return event
->xselection
.time
;
533 return screen
->lastEventTime
;
539 W_CallDestroyHandlers(W_View
*view
)
542 W_EventHandler
*hPtr
;
544 event
.type
= DestroyNotify
;
545 event
.xdestroywindow
.window
= view
->window
;
546 event
.xdestroywindow
.event
= view
->window
;
547 hPtr
= view
->handlerList
;
549 if (hPtr
->eventMask
& StructureNotifyMask
) {
550 (*hPtr
->proc
)(&event
, hPtr
->clientData
);
553 hPtr
= hPtr
->nextHandler
;
559 WMHandleEvent(XEvent
*event
)
561 W_EventHandler
*hPtr
;
562 W_View
*view
, *vPtr
, *toplevel
;
566 if (event
->type
== MappingNotify
) {
567 XRefreshKeyboardMapping(&event
->xmapping
);
571 mask
= eventMasks
[event
->xany
.type
];
573 window
= event
->xany
.window
;
575 /* diferentiate SubstructureNotify with StructureNotify */
576 if (mask
== StructureNotifyMask
) {
577 if (event
->xmap
.event
!= event
->xmap
.window
) {
578 mask
= SubstructureNotifyMask
;
579 window
= event
->xmap
.event
;
582 view
= W_GetViewForXWindow(event
->xany
.display
, window
);
584 if (extraEventHandler
)
585 (extraEventHandler
)(event
);
590 view
->screen
->lastEventTime
= getEventTime(view
->screen
, event
);
592 toplevel
= W_TopLevelOfView(view
);
594 if (event
->type
== SelectionNotify
|| event
->type
== SelectionClear
595 || event
->type
== SelectionRequest
) {
596 /* handle selection related events */
597 W_HandleSelectionEvent(event
);
600 /* if it's a key event, redispatch it to the focused control */
601 if (mask
& (KeyPressMask
|KeyReleaseMask
)) {
602 W_View
*focused
= W_FocusedViewOfToplevel(toplevel
);
609 /* compress Motion events */
610 if (event
->type
== MotionNotify
&& !view
->flags
.dontCompressMotion
) {
611 while (XPending(event
->xmotion
.display
)) {
613 XPeekEvent(event
->xmotion
.display
, &ev
);
614 if (ev
.type
== MotionNotify
615 && event
->xmotion
.window
== ev
.xmotion
.window
616 && event
->xmotion
.subwindow
== ev
.xmotion
.subwindow
) {
618 XNextEvent(event
->xmotion
.display
, event
);
623 /* compress expose events */
624 if (event
->type
== Expose
&& !view
->flags
.dontCompressExpose
) {
625 while (XCheckTypedWindowEvent(event
->xexpose
.display
, view
->window
,
630 if (view
->screen
->modal
&& toplevel
!=view
->screen
->modalView
631 && !toplevel
->flags
.worksWhenModal
) {
632 if (event
->type
== KeyPress
|| event
->type
== KeyRelease
633 || event
->type
== MotionNotify
|| event
->type
== ButtonPress
634 || event
->type
== ButtonRelease
635 || event
->type
== FocusIn
|| event
->type
== FocusOut
) {
640 /* This is a hack. It will make the panel be secure while
641 * the event handlers are handled, as some event handler
642 * might destroy the widget. */
643 W_RetainView(toplevel
);
645 hPtr
= view
->handlerList
;
650 tmp
= hPtr
->nextHandler
;
652 if ((hPtr
->eventMask
& mask
)) {
653 (*hPtr
->proc
)(event
, hPtr
->clientData
);
659 /* pass the event to the top level window of the widget */
660 if (view
->parent
!=NULL
) {
662 while (vPtr
->parent
!=NULL
)
665 hPtr
= vPtr
->handlerList
;
669 if (hPtr
->eventMask
& mask
) {
670 (*hPtr
->proc
)(event
, hPtr
->clientData
);
672 hPtr
= hPtr
->nextHandler
;
676 /* save button click info to track double-clicks */
677 if (view
->screen
->ignoreNextDoubleClick
) {
678 view
->screen
->ignoreNextDoubleClick
= 0;
680 if (event
->type
== ButtonPress
) {
681 view
->screen
->lastClickWindow
= event
->xbutton
.window
;
682 view
->screen
->lastClickTime
= event
->xbutton
.time
;
686 W_ReleaseView(toplevel
);
693 WMIsDoubleClick(XEvent
*event
)
697 if (event
->type
!= ButtonPress
)
700 view
= W_GetViewForXWindow(event
->xany
.display
, event
->xbutton
.window
);
705 if (view
->screen
->lastClickWindow
!= event
->xbutton
.window
)
708 if (event
->xbutton
.time
- view
->screen
->lastClickTime
709 < WINGsConfiguration
.doubleClickDelay
) {
710 view
->screen
->lastClickTime
= 0;
711 view
->screen
->lastClickWindow
= None
;
712 view
->screen
->ignoreNextDoubleClick
= 1;
720 W_WaitForEvent(Display
*dpy
, unsigned long xeventmask
)
722 #if defined(HAVE_POLL) && defined(HAVE_POLL_H) && !defined(HAVE_SELECT)
724 InputHandler
*handler
;
725 int count
, timeout
, nfds
, k
, retval
;
727 for (nfds
= 1, handler
= inputHandler
;
728 handler
!= 0; handler
= handler
->next
) nfds
++;
730 fds
= wmalloc(nfds
* sizeof(struct pollfd
));
731 fds
[0].fd
= ConnectionNumber(dpy
);
732 fds
[0].events
= POLLIN
;
734 for (k
= 1, handler
= inputHandler
;
736 handler
= handler
->next
, k
++) {
737 fds
[k
].fd
= handler
->fd
;
739 if (handler
->mask
& WIReadMask
)
740 fds
[k
].events
|= POLLIN
;
742 if (handler
->mask
& WIWriteMask
)
743 fds
[k
].events
|= POLLOUT
;
746 if (handler
->mask
& WIExceptMask
)
747 FD_SET(handler
->fd
, &eset
);
752 * Setup the select() timeout to the estimated time until the
753 * next timer expires.
755 if (timerPending()) {
757 delayUntilNextTimerEvent(&tv
);
758 timeout
= tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
768 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
769 XPutBackEvent(dpy
, &ev
);
774 count
= poll(fds
, nfds
, timeout
);
777 handler
= inputHandler
;
784 if (fds
[k
].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
))
787 if (fds
[k
].revents
& (POLLOUT
| POLLWRBAND
))
790 if (fds
[k
].revents
& (POLLHUP
| POLLNVAL
| POLLERR
))
791 mask
|= WIExceptMask
;
793 if (mask
!=0 && handler
->callback
) {
794 (*handler
->callback
)(handler
->fd
, mask
,
795 handler
->clientData
);
798 handler
= handler
->next
;
803 retval
= fds
[0].revents
& (POLLIN
|POLLRDNORM
|POLLRDBAND
|POLLPRI
);
806 W_FlushASAPNotificationQueue();
809 #else /* not HAVE_POLL */
811 struct timeval timeout
;
812 struct timeval
*timeoutPtr
;
813 fd_set rset
, wset
, eset
;
816 InputHandler
*handler
= inputHandler
;
822 FD_SET(ConnectionNumber(dpy
), &rset
);
823 maxfd
= ConnectionNumber(dpy
);
826 if (handler
->mask
& WIReadMask
)
827 FD_SET(handler
->fd
, &rset
);
829 if (handler
->mask
& WIWriteMask
)
830 FD_SET(handler
->fd
, &wset
);
832 if (handler
->mask
& WIExceptMask
)
833 FD_SET(handler
->fd
, &eset
);
835 if (maxfd
< handler
->fd
)
838 handler
= handler
->next
;
843 * Setup the select() timeout to the estimated time until the
844 * next timer expires.
846 if (timerPending()) {
847 delayUntilNextTimerEvent(&timeout
);
848 timeoutPtr
= &timeout
;
850 timeoutPtr
= (struct timeval
*)0;
859 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
860 XPutBackEvent(dpy
, &ev
);
865 count
= select(1 + maxfd
, &rset
, &wset
, &eset
, timeoutPtr
);
868 handler
= inputHandler
;
875 if (FD_ISSET(handler
->fd
, &rset
))
878 if (FD_ISSET(handler
->fd
, &wset
))
881 if (FD_ISSET(handler
->fd
, &eset
))
882 mask
|= WIExceptMask
;
884 if (mask
!=0 && handler
->callback
) {
885 (*handler
->callback
)(handler
->fd
, mask
,
886 handler
->clientData
);
889 handler
= handler
->next
;
893 W_FlushASAPNotificationQueue();
895 return FD_ISSET(ConnectionNumber(dpy
), &rset
);
896 #else /* not HAVE_SELECT, not HAVE_POLL */
897 Neither select nor poll
. You lose
.
898 #endif /* HAVE_SELECT */
899 #endif /* HAVE_POLL */
904 WMNextEvent(Display
*dpy
, XEvent
*event
)
906 /* Check any expired timers */
907 if (timerPending()) {
908 checkTimerHandlers();
911 while (XPending(dpy
) == 0) {
913 /* Do idle and timer stuff while there are no timer or X events */
914 while (!XPending(dpy
) && idlePending()) {
917 /* dispatch timer events */
919 checkTimerHandlers();
923 * Make sure that new events did not arrive while we were doing
924 * timer/idle stuff. Or we might block forever waiting for
925 * an event that already arrived.
927 /* wait to something happen */
928 W_WaitForEvent(dpy
, 0);
930 /* Check any expired timers */
931 if (timerPending()) {
932 checkTimerHandlers();
936 XNextEvent(dpy
, event
);
941 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
943 unsigned long milliseconds
;
944 struct timeval timeout
;
945 struct timeval
*timeoutOrInfty
;
948 while (!XCheckMaskEvent(dpy
, mask
, event
)) {
949 /* Do idle stuff while there are no timer or X events */
950 while (idlePending()) {
952 if (XCheckMaskEvent(dpy
, mask
, event
))
957 * Setup the select() timeout to the estimated time until the
958 * next timer expires.
960 if (timerPending()) {
961 delayUntilNextTimerEvent(&timeout
);
962 timeoutOrInfty
= &timeout
;
964 timeoutOrInfty
= (struct timeval
*)0;
967 if (XCheckMaskEvent(dpy
, mask
, event
))
970 /* Wait for input on the X connection socket */
972 FD_SET(ConnectionNumber(dpy
), &readset
);
973 select(1 + ConnectionNumber(dpy
), &readset
, (fd_set
*)0, (fd_set
*)0,
976 /* Check any expired timers */
977 if (timerPending()) {
978 checkTimerHandlers();
985 * Cant use this because XPending() will make W_WaitForEvent
986 * return even if the event in the queue is not what we want,
987 * and if we block until some new event arrives from the
988 * server, other events already in the queue (like Expose)
992 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
994 while (!XCheckMaskEvent(dpy
, mask
, event
)) {
995 /* Do idle stuff while there are no timer or X events */
996 while (idlePending()) {
998 if (XCheckMaskEvent(dpy
, mask
, event
))
1002 /* Wait for input on the X connection socket */
1003 W_WaitForEvent(dpy
, mask
);
1005 /* Check any expired timers */
1006 if (timerPending()) {
1007 checkTimerHandlers();
1014 WMScreenPending(WMScreen
*scr
)
1016 if (XPending(scr
->display
))
1024 WMHookEventHandler(WMEventHook
*handler
)
1026 WMEventHook
*oldHandler
= extraEventHandler
;
1028 extraEventHandler
= handler
;