4 * This event handling stuff was inspired on Tk.
10 /* table to map event types to event masks */
11 static unsigned long eventMasks
[] = {
14 KeyPressMask
, /* KeyPress */
15 KeyReleaseMask
, /* KeyRelease */
16 ButtonPressMask
, /* ButtonPress */
17 ButtonReleaseMask
, /* ButtonRelease */
18 PointerMotionMask
|PointerMotionHintMask
|ButtonMotionMask
19 |Button1MotionMask
|Button2MotionMask
|Button3MotionMask
20 |Button4MotionMask
|Button5MotionMask
,
22 EnterWindowMask
, /* EnterNotify */
23 LeaveWindowMask
, /* LeaveNotify */
24 FocusChangeMask
, /* FocusIn */
25 FocusChangeMask
, /* FocusOut */
26 KeymapStateMask
, /* KeymapNotify */
27 ExposureMask
, /* Expose */
28 ExposureMask
, /* GraphicsExpose */
29 ExposureMask
, /* NoExpose */
30 VisibilityChangeMask
, /* VisibilityNotify */
31 SubstructureNotifyMask
, /* CreateNotify */
32 StructureNotifyMask
, /* DestroyNotify */
33 StructureNotifyMask
, /* UnmapNotify */
34 StructureNotifyMask
, /* MapNotify */
35 SubstructureRedirectMask
, /* MapRequest */
36 StructureNotifyMask
, /* ReparentNotify */
37 StructureNotifyMask
, /* ConfigureNotify */
38 SubstructureRedirectMask
, /* ConfigureRequest */
39 StructureNotifyMask
, /* GravityNotify */
40 ResizeRedirectMask
, /* ResizeRequest */
41 StructureNotifyMask
, /* CirculateNotify */
42 SubstructureRedirectMask
, /* CirculateRequest */
43 PropertyChangeMask
, /* PropertyNotify */
44 0, /* SelectionClear */
45 0, /* SelectionRequest */
46 0, /* SelectionNotify */
47 ColormapChangeMask
, /* ColormapNotify */
48 ClientMessageMask
, /* ClientMessage */
49 0, /* Mapping Notify */
54 /* hook for other toolkits or wmaker process their events */
55 static WMEventHook
*extraEventHandler
=NULL
;
62 * WMCreateEventHandler--
63 * Create an event handler and put it in the event handler list for the
64 * view. If the same callback and clientdata are already used in another
65 * handler, the masks are swapped.
69 WMCreateEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
72 W_EventHandler
*handler
, *ptr
;
73 unsigned long eventMask
;
80 WM_ITERATE_ARRAY(view
->eventHandlers
, ptr
, iter
) {
81 if (ptr
->clientData
== clientData
&& ptr
->proc
== eventProc
) {
83 eventMask
|= ptr
->eventMask
;
87 handler
= wmalloc(sizeof(W_EventHandler
));
89 WMAddToArray(view
->eventHandlers
, handler
);
91 /* select events for window */
92 handler
->eventMask
= eventMask
;
93 handler
->proc
= eventProc
;
94 handler
->clientData
= clientData
;
99 matchHandler(void *item
, void *cdata
)
101 #define H1 ((W_EventHandler*)item)
102 #define H2 ((W_EventHandler*)cdata)
104 return (H1
->eventMask
==H2
->eventMask
&& H1
->proc
==H2
->proc
&&
105 H1
->clientData
==H2
->clientData
);
110 * WMDeleteEventHandler--
111 * Delete event handler matching arguments from windows
112 * event handler list.
116 WMDeleteEventHandler(WMView
*view
, unsigned long mask
, WMEventProc
*eventProc
,
121 tmp
.eventMask
= mask
;
122 tmp
.proc
= eventProc
;
123 tmp
.clientData
= clientData
;
124 WMRemoveFromArrayMatching(view
->eventHandlers
, matchHandler
, (void*)&tmp
);
129 getEventTime(WMScreen
*screen
, XEvent
*event
)
131 switch (event
->type
) {
134 return event
->xbutton
.time
;
137 return event
->xkey
.time
;
139 return event
->xmotion
.time
;
142 return event
->xcrossing
.time
;
144 return event
->xproperty
.time
;
146 return event
->xselectionclear
.time
;
147 case SelectionRequest
:
148 return event
->xselectionrequest
.time
;
149 case SelectionNotify
:
150 return event
->xselection
.time
;
152 return screen
->lastEventTime
;
158 W_CallDestroyHandlers(W_View
*view
)
161 WMArrayIterator iter
;
162 W_EventHandler
*hPtr
;
165 event
.type
= DestroyNotify
;
166 event
.xdestroywindow
.window
= view
->window
;
167 event
.xdestroywindow
.event
= view
->window
;
169 WM_ITERATE_ARRAY(view
->eventHandlers
, hPtr
, iter
) {
170 if (hPtr
->eventMask
& StructureNotifyMask
) {
171 (*hPtr
->proc
)(&event
, hPtr
->clientData
);
179 WMSetViewNextResponder(WMView
*view
, WMView
*responder
)
181 /* set the widget to receive keyboard events that aren't handled
184 view
->nextResponder
= responder
;
189 WMRelayToNextResponder(WMView
*view
, XEvent
*event
)
191 unsigned long mask
= eventMasks
[event
->xany
.type
];
193 if (view
->nextResponder
) {
194 WMView
*next
= view
->nextResponder
;
195 W_EventHandler
*hPtr
;
196 WMArrayIterator iter
;
198 WM_ITERATE_ARRAY(next
->eventHandlers
, hPtr
, iter
) {
199 if ((hPtr
->eventMask
& mask
)) {
200 (*hPtr
->proc
)(event
, hPtr
->clientData
);
208 WMHandleEvent(XEvent
*event
)
210 W_EventHandler
*hPtr
;
211 W_View
*view
, *toplevel
;
214 WMArrayIterator iter
;
216 if (event
->type
== MappingNotify
) {
217 XRefreshKeyboardMapping(&event
->xmapping
);
221 mask
= eventMasks
[event
->xany
.type
];
223 window
= event
->xany
.window
;
225 /* diferentiate SubstructureNotify with StructureNotify */
226 if (mask
== StructureNotifyMask
) {
227 if (event
->xmap
.event
!= event
->xmap
.window
) {
228 mask
= SubstructureNotifyMask
;
229 window
= event
->xmap
.event
;
232 view
= W_GetViewForXWindow(event
->xany
.display
, window
);
235 if (extraEventHandler
)
236 (extraEventHandler
)(event
);
241 view
->screen
->lastEventTime
= getEventTime(view
->screen
, event
);
243 toplevel
= W_TopLevelOfView(view
);
245 if (event
->type
== SelectionNotify
|| event
->type
== SelectionClear
246 || event
->type
== SelectionRequest
) {
247 /* handle selection related events */
248 W_HandleSelectionEvent(event
);
250 } else if (event
->type
== ClientMessage
) {
252 W_HandleDNDClientMessage(toplevel
, &event
->xclient
);
255 /* if it's a key event, redispatch it to the focused control */
256 if (mask
& (KeyPressMask
|KeyReleaseMask
)) {
257 W_View
*focused
= W_FocusedViewOfToplevel(toplevel
);
264 /* compress Motion events */
265 if (event
->type
== MotionNotify
&& !view
->flags
.dontCompressMotion
) {
266 while (XPending(event
->xmotion
.display
)) {
268 XPeekEvent(event
->xmotion
.display
, &ev
);
269 if (ev
.type
== MotionNotify
270 && event
->xmotion
.window
== ev
.xmotion
.window
271 && event
->xmotion
.subwindow
== ev
.xmotion
.subwindow
) {
273 XNextEvent(event
->xmotion
.display
, event
);
278 /* compress expose events */
279 if (event
->type
== Expose
&& !view
->flags
.dontCompressExpose
) {
280 while (XCheckTypedWindowEvent(event
->xexpose
.display
, view
->window
,
285 if (view
->screen
->modalLoop
&& toplevel
!=view
->screen
->modalView
286 && !toplevel
->flags
.worksWhenModal
) {
287 if (event
->type
== KeyPress
|| event
->type
== KeyRelease
288 || event
->type
== MotionNotify
|| event
->type
== ButtonPress
289 || event
->type
== ButtonRelease
290 || event
->type
== FocusIn
|| event
->type
== FocusOut
) {
295 /* do balloon stuffs */
296 if (event
->type
== EnterNotify
)
297 W_BalloonHandleEnterView(view
);
298 else if (event
->type
== LeaveNotify
)
299 W_BalloonHandleLeaveView(view
);
301 /* This is a hack. It will make the panel be secure while
302 * the event handlers are handled, as some event handler
303 * might destroy the widget. */
304 W_RetainView(toplevel
);
306 WM_ITERATE_ARRAY(view
->eventHandlers
, hPtr
, iter
) {
307 if ((hPtr
->eventMask
& mask
)) {
308 (*hPtr
->proc
)(event
, hPtr
->clientData
);
312 /* pass the event to the top level window of the widget */
313 /* TODO: change this to a responder chain */
314 if (view
->parent
!= NULL
) {
316 while (vPtr
->parent
!= NULL
)
319 WM_ITERATE_ARRAY(vPtr
->eventHandlers
, hPtr
, iter
) {
320 if (hPtr
->eventMask
& mask
) {
321 (*hPtr
->proc
)(event
, hPtr
->clientData
);
326 /* save button click info to track double-clicks */
327 if (view
->screen
->ignoreNextDoubleClick
) {
328 view
->screen
->ignoreNextDoubleClick
= 0;
330 if (event
->type
== ButtonPress
) {
331 view
->screen
->lastClickWindow
= event
->xbutton
.window
;
332 view
->screen
->lastClickTime
= event
->xbutton
.time
;
336 W_ReleaseView(toplevel
);
343 WMIsDoubleClick(XEvent
*event
)
347 if (event
->type
!= ButtonPress
)
350 view
= W_GetViewForXWindow(event
->xany
.display
, event
->xbutton
.window
);
355 if (view
->screen
->lastClickWindow
!= event
->xbutton
.window
)
358 if (event
->xbutton
.time
- view
->screen
->lastClickTime
359 < WINGsConfiguration
.doubleClickDelay
) {
360 view
->screen
->lastClickTime
= 0;
361 view
->screen
->lastClickWindow
= None
;
362 view
->screen
->ignoreNextDoubleClick
= 1;
370 * Check for X and input events. If X events are present input events will
373 * Return value: True if a X event is available or any input event was
374 * processed, false otherwise (including return because of
375 * some timer handler expired).
377 * If waitForInput is False, it will just peek for available input and return
378 * without processing. Return vaue will be True if input is available.
380 * If waitForInput is True, it will wait until an input event arrives on the
381 * registered input handlers and ConnectionNumber(dpy), or will return when
382 * a timer handler expires if no input event arrived until then.
385 waitForEvent(Display
*dpy
, unsigned long xeventmask
, Bool waitForInput
)
393 if (XCheckMaskEvent(dpy
, xeventmask
, &ev
)) {
394 XPutBackEvent(dpy
, &ev
);
399 return W_HandleInputEvents(waitForInput
, ConnectionNumber(dpy
));
404 WMNextEvent(Display
*dpy
, XEvent
*event
)
406 /* Check any expired timers */
407 W_CheckTimerHandlers();
409 while (XPending(dpy
) == 0) {
410 /* Do idle and timer stuff while there are no input or X events */
411 while (!waitForEvent(dpy
, 0, False
) && W_CheckIdleHandlers()) {
412 /* dispatch timer events */
413 W_CheckTimerHandlers();
417 * Make sure that new events did not arrive while we were doing
418 * timer/idle stuff. Or we might block forever waiting for
419 * an event that already arrived.
421 /* wait for something to happen or a timer to expire */
422 waitForEvent(dpy
, 0, True
);
424 /* Check any expired timers */
425 W_CheckTimerHandlers();
428 XNextEvent(dpy
, event
);
433 WMMaskEvent(Display
*dpy
, long mask
, XEvent
*event
)
435 /* Check any expired timers */
436 W_CheckTimerHandlers();
438 while (!XCheckMaskEvent(dpy
, mask
, event
)) {
439 /* Do idle and timer stuff while there are no input or X events */
440 while (!waitForEvent(dpy
, mask
, False
) && W_CheckIdleHandlers()) {
441 W_CheckTimerHandlers();
444 if (XCheckMaskEvent(dpy
, mask
, event
))
447 /* Wait for input on the X connection socket or another input handler */
448 waitForEvent(dpy
, mask
, True
);
450 /* Check any expired timers */
451 W_CheckTimerHandlers();
457 WMScreenPending(WMScreen
*scr
)
459 if (XPending(scr
->display
))
467 WMHookEventHandler(WMEventHook
*handler
)
469 WMEventHook
*oldHandler
= extraEventHandler
;
471 extraEventHandler
= handler
;