1 /* event.c- event loop and handling
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1997-2003 Alfredo K. Kojima
6  *  Copyright (c) 2014 Window Maker Team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "wconfig.h"
24 
25 #ifdef HAVE_INOTIFY
26 #include <sys/select.h>
27 #include <sys/inotify.h>
28 #endif
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <time.h>
36 #include <errno.h>
37 
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xatom.h>
41 #ifdef USE_XSHAPE
42 # include <X11/extensions/shape.h>
43 #endif
44 #ifdef USE_DOCK_XDND
45 #include "xdnd.h"
46 #endif
47 
48 #ifdef USE_RANDR
49 #include <X11/extensions/Xrandr.h>
50 #endif
51 
52 #ifdef KEEP_XKB_LOCK_STATUS
53 #include <X11/XKBlib.h>
54 #endif				/* KEEP_XKB_LOCK_STATUS */
55 
56 #include "WindowMaker.h"
57 #include "window.h"
58 #include "actions.h"
59 #include "client.h"
60 #include "main.h"
61 #include "cycling.h"
62 #include "keybind.h"
63 #include "application.h"
64 #include "stacking.h"
65 #include "defaults.h"
66 #include "workspace.h"
67 #include "dock.h"
68 #include "framewin.h"
69 #include "properties.h"
70 #include "balloon.h"
71 #include "xinerama.h"
72 #include "wmspec.h"
73 #include "rootmenu.h"
74 #include "colormap.h"
75 #include "screen.h"
76 #include "shutdown.h"
77 #include "misc.h"
78 #include "event.h"
79 #include "winmenu.h"
80 #include "switchmenu.h"
81 #include "wsmap.h"
82 
83 
84 #define MOD_MASK wPreferences.modifier_mask
85 
86 /************ Local stuff ***********/
87 
88 static void saveTimestamp(XEvent *event);
89 static void handleColormapNotify(XEvent *event);
90 static void handleMapNotify(XEvent *event);
91 static void handleUnmapNotify(XEvent *event);
92 static void handleButtonPress(XEvent *event);
93 static void handleExpose(XEvent *event);
94 static void handleDestroyNotify(XEvent *event);
95 static void handleConfigureRequest(XEvent *event);
96 static void handleMapRequest(XEvent *event);
97 static void handlePropertyNotify(XEvent *event);
98 static void handleEnterNotify(XEvent *event);
99 static void handleLeaveNotify(XEvent *event);
100 static void handleExtensions(XEvent *event);
101 static void handleClientMessage(XEvent *event);
102 static void handleKeyPress(XEvent *event);
103 static void handleFocusIn(XEvent *event);
104 static void handleMotionNotify(XEvent *event);
105 static void handleVisibilityNotify(XEvent *event);
106 static void handle_inotify_events(void);
107 static void handle_selection_request(XSelectionRequestEvent *event);
108 static void handle_selection_clear(XSelectionClearEvent *event);
109 static void wdelete_death_handler(WMagicNumber id);
110 
111 
112 #ifdef USE_XSHAPE
113 static void handleShapeNotify(XEvent *event);
114 #endif
115 
116 #ifdef KEEP_XKB_LOCK_STATUS
117 static void handleXkbIndicatorStateNotify(XkbEvent *event);
118 #endif
119 
120 /* real dead process handler */
121 static void handleDeadProcess(void);
122 
123 typedef struct DeadProcesses {
124 	pid_t pid;
125 	unsigned char exit_status;
126 } DeadProcesses;
127 
128 /* stack of dead processes */
129 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
130 static int deadProcessPtr = 0;
131 
132 typedef struct DeathHandler {
133 	WDeathHandler *callback;
134 	pid_t pid;
135 	void *client_data;
136 } DeathHandler;
137 
138 static WMArray *deathHandlers = NULL;
139 
wAddDeathHandler(pid_t pid,WDeathHandler * callback,void * cdata)140 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
141 {
142 	DeathHandler *handler;
143 
144 	handler = malloc(sizeof(DeathHandler));
145 	if (!handler)
146 		return 0;
147 
148 	handler->pid = pid;
149 	handler->callback = callback;
150 	handler->client_data = cdata;
151 
152 	if (!deathHandlers)
153 		deathHandlers = WMCreateArrayWithDestructor(8, free);
154 
155 	WMAddToArray(deathHandlers, handler);
156 
157 	return handler;
158 }
159 
wdelete_death_handler(WMagicNumber id)160 static void wdelete_death_handler(WMagicNumber id)
161 {
162 	DeathHandler *handler = (DeathHandler *) id;
163 
164 	if (!handler || !deathHandlers)
165 		return;
166 
167 	/* array destructor will call free(handler) */
168 	WMRemoveFromArray(deathHandlers, handler);
169 }
170 
DispatchEvent(XEvent * event)171 void DispatchEvent(XEvent * event)
172 {
173 	if (deathHandlers)
174 		handleDeadProcess();
175 
176 	if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
177 		WCHANGE_STATE(WSTATE_EXITING);
178 		/* received SIGTERM */
179 		/*
180 		 * WMHandleEvent() can't be called from anything
181 		 * executed inside here, or we can get in a infinite
182 		 * recursive loop.
183 		 */
184 		Shutdown(WSExitMode);
185 
186 	} else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
187 		WCHANGE_STATE(WSTATE_RESTARTING);
188 
189 		Shutdown(WSRestartPreparationMode);
190 		/* received SIGHUP */
191 		Restart(NULL, True);
192 	} else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
193 		WCHANGE_STATE(WSTATE_NORMAL);
194 		wDefaultsCheckDomains(NULL);
195 	}
196 
197 	/* for the case that all that is wanted to be dispatched is
198 	 * the stuff above */
199 	if (!event)
200 		return;
201 
202 	saveTimestamp(event);
203 	switch (event->type) {
204 	case MapRequest:
205 		handleMapRequest(event);
206 		break;
207 
208 	case KeyPress:
209 		handleKeyPress(event);
210 		break;
211 
212 	case MotionNotify:
213 		handleMotionNotify(event);
214 		break;
215 
216 	case ConfigureRequest:
217 		handleConfigureRequest(event);
218 		break;
219 
220 	case DestroyNotify:
221 		handleDestroyNotify(event);
222 		break;
223 
224 	case MapNotify:
225 		handleMapNotify(event);
226 		break;
227 
228 	case UnmapNotify:
229 		handleUnmapNotify(event);
230 		break;
231 
232 	case ButtonPress:
233 		handleButtonPress(event);
234 		break;
235 
236 	case Expose:
237 		handleExpose(event);
238 		break;
239 
240 	case PropertyNotify:
241 		handlePropertyNotify(event);
242 		break;
243 
244 	case EnterNotify:
245 		handleEnterNotify(event);
246 		break;
247 
248 	case LeaveNotify:
249 		handleLeaveNotify(event);
250 		break;
251 
252 	case ClientMessage:
253 		handleClientMessage(event);
254 		break;
255 
256 	case ColormapNotify:
257 		handleColormapNotify(event);
258 		break;
259 
260 	case MappingNotify:
261 		if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
262 			XRefreshKeyboardMapping(&event->xmapping);
263 		break;
264 
265 	case FocusIn:
266 		handleFocusIn(event);
267 		break;
268 
269 	case VisibilityNotify:
270 		handleVisibilityNotify(event);
271 		break;
272 
273 	case ConfigureNotify:
274 #ifdef USE_RANDR
275 		if (event->xconfigure.window == DefaultRootWindow(dpy))
276 			XRRUpdateConfiguration(event);
277 #endif
278 		break;
279 
280 	case SelectionRequest:
281 		handle_selection_request(&event->xselectionrequest);
282 		break;
283 
284 	case SelectionClear:
285 		handle_selection_clear(&event->xselectionclear);
286 		break;
287 
288 	default:
289 		handleExtensions(event);
290 		break;
291 	}
292 }
293 
294 #ifdef HAVE_INOTIFY
295 /*
296  *----------------------------------------------------------------------
297  * handle_inotify_events-
298  * 	Check for inotify events
299  *
300  * Returns:
301  * 	After reading events for the given file descriptor (fd) and
302  *     watch descriptor (wd)
303  *
304  * Side effects:
305  * 	Calls wDefaultsCheckDomains if config database is updated
306  *----------------------------------------------------------------------
307  */
handle_inotify_events(void)308 static void handle_inotify_events(void)
309 {
310 	ssize_t eventQLength;
311 	size_t i = 0;
312 	/* Make room for at lease 5 simultaneous events, with path + filenames */
313 	char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
314 	/* Check config only once per read of the event queue */
315 	int oneShotFlag = 0;
316 
317 	/*
318 	 * Read off the queued events
319 	 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
320 	 * not occur; the block is on Xevents, but a config file change will normally
321 	 * occur as a result of an Xevent - so the event queue should never have more than
322 	 * a few entries before a read().
323 	 */
324 	eventQLength = read(w_global.inotify.fd_event_queue,
325 	                    buff, sizeof(buff) );
326 
327 	if (eventQLength < 0) {
328 		wwarning(_("read problem when trying to get INotify event: %s"), strerror(errno));
329 		return;
330 	}
331 
332 	/* check what events occurred */
333 	/* Should really check wd here too, but for now we only have one watch! */
334 	while (i < eventQLength) {
335 		struct inotify_event *pevent = (struct inotify_event *)&buff[i];
336 
337 		/*
338 		 * see inotify.h for event types.
339 		 */
340 		if (pevent->mask & IN_DELETE_SELF) {
341 			wwarning(_("the defaults database has been deleted!"
342 				   " Restart Window Maker to create the database" " with the default settings"));
343 
344 			if (w_global.inotify.fd_event_queue >= 0) {
345 				close(w_global.inotify.fd_event_queue);
346 				w_global.inotify.fd_event_queue = -1;
347 			}
348 		}
349 		if (pevent->mask & IN_UNMOUNT) {
350 			wwarning(_("the unit containing the defaults database has"
351 				   " been unmounted. Setting --static mode." " Any changes will not be saved."));
352 
353 			if (w_global.inotify.fd_event_queue >= 0) {
354 				close(w_global.inotify.fd_event_queue);
355 				w_global.inotify.fd_event_queue = -1;
356 			}
357 
358 			wPreferences.flags.noupdates = 1;
359 		}
360 		if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
361 			wwarning(_("Inotify: Reading config files in defaults database."));
362 			wDefaultsCheckDomains(NULL);
363 			oneShotFlag = 1;
364 		}
365 
366 		/* move to next event in the buffer */
367 		i += sizeof(struct inotify_event) + pevent->len;
368 	}
369 }
370 #endif /* HAVE_INOTIFY */
371 
372 /*
373  *----------------------------------------------------------------------
374  * EventLoop-
375  * 	Processes X and internal events indefinitely.
376  *
377  * Returns:
378  * 	Never returns
379  *
380  * Side effects:
381  * 	The LastTimestamp global variable is updated.
382  *      Calls inotifyGetEvents if defaults database changes.
383  *----------------------------------------------------------------------
384  */
EventLoop(void)385 noreturn void EventLoop(void)
386 {
387 	XEvent event;
388 #ifdef HAVE_INOTIFY
389 	struct timeval time;
390 	fd_set rfds;
391 	int retVal = 0;
392 
393 	if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
394 		retVal = -1;
395 #endif
396 
397 	for (;;) {
398 
399 		WMNextEvent(dpy, &event);	/* Blocks here */
400 		WMHandleEvent(&event);
401 #ifdef HAVE_INOTIFY
402 		if (retVal != -1) {
403 			time.tv_sec = 0;
404 			time.tv_usec = 0;
405 			FD_ZERO(&rfds);
406 			FD_SET(w_global.inotify.fd_event_queue, &rfds);
407 
408 			/* check for available read data from inotify - don't block! */
409 			retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
410 
411 			if (retVal < 0) {	/* an error has occurred */
412 				wwarning(_("select failed. The inotify instance will be closed."
413 					   " Changes to the defaults database will require"
414 					   " a restart to take effect."));
415 				close(w_global.inotify.fd_event_queue);
416 				w_global.inotify.fd_event_queue = -1;
417 				continue;
418 			}
419 			if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
420 				handle_inotify_events();
421 		}
422 #endif
423 	}
424 }
425 
426 /*
427  *----------------------------------------------------------------------
428  * ProcessPendingEvents --
429  * 	Processes the events that are currently pending (at the time
430  *      this function is called) in the display's queue.
431  *
432  * Returns:
433  *      After the pending events that were present at the function call
434  *      are processed.
435  *
436  * Side effects:
437  * 	Many -- whatever handling events may involve.
438  *
439  *----------------------------------------------------------------------
440  */
ProcessPendingEvents(void)441 void ProcessPendingEvents(void)
442 {
443 	XEvent event;
444 	int count;
445 
446 	XSync(dpy, False);
447 
448 	/* Take a snapshot of the event count in the queue */
449 	count = XPending(dpy);
450 
451 	while (count > 0 && XPending(dpy)) {
452 		WMNextEvent(dpy, &event);
453 		WMHandleEvent(&event);
454 		count--;
455 	}
456 }
457 
IsDoubleClick(WScreen * scr,XEvent * event)458 Bool IsDoubleClick(WScreen * scr, XEvent * event)
459 {
460 	if ((scr->last_click_time > 0) &&
461 	    (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
462 	    && (event->xbutton.button == scr->last_click_button)
463 	    && (event->xbutton.window == scr->last_click_window)) {
464 
465 		scr->flags.next_click_is_not_double = 1;
466 		scr->last_click_time = 0;
467 		scr->last_click_window = event->xbutton.window;
468 
469 		return True;
470 	}
471 	return False;
472 }
473 
NotifyDeadProcess(pid_t pid,unsigned char status)474 void NotifyDeadProcess(pid_t pid, unsigned char status)
475 {
476 	if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
477 		wwarning("stack overflow: too many dead processes");
478 		return;
479 	}
480 	/* stack the process to be handled later,
481 	 * as this is called from the signal handler */
482 	deadProcesses[deadProcessPtr].pid = pid;
483 	deadProcesses[deadProcessPtr].exit_status = status;
484 	deadProcessPtr++;
485 }
486 
handleDeadProcess(void)487 static void handleDeadProcess(void)
488 {
489 	DeathHandler *tmp;
490 	int i;
491 
492 	for (i = 0; i < deadProcessPtr; i++) {
493 		wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
494 	}
495 
496 	if (!deathHandlers) {
497 		deadProcessPtr = 0;
498 		return;
499 	}
500 
501 	/* get the pids on the queue and call handlers */
502 	while (deadProcessPtr > 0) {
503 		deadProcessPtr--;
504 
505 		for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
506 			tmp = WMGetFromArray(deathHandlers, i);
507 			if (!tmp)
508 				continue;
509 
510 			if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
511 				(*tmp->callback) (tmp->pid,
512 						  deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
513 				wdelete_death_handler(tmp);
514 			}
515 		}
516 	}
517 }
518 
saveTimestamp(XEvent * event)519 static void saveTimestamp(XEvent * event)
520 {
521 	/*
522 	 * Never save CurrentTime as LastTimestamp because CurrentTime
523 	 * it's not a real timestamp (it's the 0L constant)
524 	 */
525 
526 	switch (event->type) {
527 	case ButtonRelease:
528 	case ButtonPress:
529 		w_global.timestamp.last_event = event->xbutton.time;
530 		break;
531 	case KeyPress:
532 	case KeyRelease:
533 		w_global.timestamp.last_event = event->xkey.time;
534 		break;
535 	case MotionNotify:
536 		w_global.timestamp.last_event = event->xmotion.time;
537 		break;
538 	case PropertyNotify:
539 		w_global.timestamp.last_event = event->xproperty.time;
540 		break;
541 	case EnterNotify:
542 	case LeaveNotify:
543 		w_global.timestamp.last_event = event->xcrossing.time;
544 		break;
545 	case SelectionClear:
546 		w_global.timestamp.last_event = event->xselectionclear.time;
547 		break;
548 	case SelectionRequest:
549 		w_global.timestamp.last_event = event->xselectionrequest.time;
550 		break;
551 	case SelectionNotify:
552 		w_global.timestamp.last_event = event->xselection.time;
553 #ifdef USE_DOCK_XDND
554 		wXDNDProcessSelection(event);
555 #endif
556 		break;
557 	}
558 }
559 
matchWindow(const void * item,const void * cdata)560 static int matchWindow(const void *item, const void *cdata)
561 {
562 	return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
563 }
564 
handleExtensions(XEvent * event)565 static void handleExtensions(XEvent * event)
566 {
567 #ifdef USE_XSHAPE
568 	if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
569 		handleShapeNotify(event);
570 	}
571 #endif
572 #ifdef KEEP_XKB_LOCK_STATUS
573 	if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
574 		handleXkbIndicatorStateNotify((XkbEvent *) event);
575 	}
576 #endif				/*KEEP_XKB_LOCK_STATUS */
577 #ifdef USE_RANDR
578 	if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
579 		/* From xrandr man page: "Clients must call back into Xlib using
580 		 * XRRUpdateConfiguration when screen configuration change notify
581 		 * events are generated */
582 		XRRUpdateConfiguration(event);
583 		WCHANGE_STATE(WSTATE_RESTARTING);
584 		Shutdown(WSRestartPreparationMode);
585 		Restart(NULL,True);
586 	}
587 #endif
588 }
589 
handleMapRequest(XEvent * ev)590 static void handleMapRequest(XEvent * ev)
591 {
592 	WWindow *wwin;
593 	WScreen *scr = NULL;
594 	Window window = ev->xmaprequest.window;
595 
596 	wwin = wWindowFor(window);
597 	if (wwin != NULL) {
598 		if (wwin->flags.shaded) {
599 			wUnshadeWindow(wwin);
600 		}
601 		/* deiconify window */
602 		if (wwin->flags.miniaturized) {
603 			wDeiconifyWindow(wwin);
604 		} else if (wwin->flags.hidden) {
605 			WApplication *wapp = wApplicationOf(wwin->main_window);
606 			/* go to the last workspace that the user worked on the app */
607 			if (wapp) {
608 				wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
609 			}
610 			wUnhideApplication(wapp, False, False);
611 		}
612 		return;
613 	}
614 
615 	scr = wScreenForRootWindow(ev->xmaprequest.parent);
616 
617 	wwin = wManageWindow(scr, window);
618 
619 	/*
620 	 * This is to let the Dock know that the application it launched
621 	 * has already been mapped (eg: it has finished launching).
622 	 * It is not necessary for normally docked apps, but is needed for
623 	 * apps that were forcedly docked (like with dockit).
624 	 */
625 	if (scr->last_dock) {
626 		if (wwin && wwin->main_window != None && wwin->main_window != window)
627 			wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
628 		else
629 			wDockTrackWindowLaunch(scr->last_dock, window);
630 	}
631 
632 	if (wwin) {
633 		wClientSetState(wwin, NormalState, None);
634 		if (wwin->flags.maximized) {
635 			wMaximizeWindow(wwin, wwin->flags.maximized,
636 					wGetHeadForWindow(wwin));
637 		}
638 		if (wwin->flags.shaded) {
639 			wwin->flags.shaded = 0;
640 			wwin->flags.skip_next_animation = 1;
641 			wShadeWindow(wwin);
642 		}
643 		if (wwin->flags.miniaturized) {
644 			wwin->flags.miniaturized = 0;
645 			wwin->flags.skip_next_animation = 1;
646 			wIconifyWindow(wwin);
647 		}
648 		if (wwin->flags.fullscreen) {
649 			wwin->flags.fullscreen = 0;
650 			wFullscreenWindow(wwin);
651 		}
652 		if (wwin->flags.hidden) {
653 			WApplication *wapp = wApplicationOf(wwin->main_window);
654 
655 			wwin->flags.hidden = 0;
656 			wwin->flags.skip_next_animation = 1;
657 			if (wapp) {
658 				wHideApplication(wapp);
659 			}
660 		}
661 	}
662 }
663 
handleDestroyNotify(XEvent * event)664 static void handleDestroyNotify(XEvent * event)
665 {
666 	WWindow *wwin;
667 	WApplication *app;
668 	Window window = event->xdestroywindow.window;
669 	WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
670 	int widx;
671 
672 	wwin = wWindowFor(window);
673 	if (wwin) {
674 		wUnmanageWindow(wwin, False, True);
675 	}
676 
677 	if (scr != NULL) {
678 		while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
679 			WFakeGroupLeader *fPtr;
680 
681 			fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
682 			if (fPtr->retainCount > 0) {
683 				fPtr->retainCount--;
684 				if (fPtr->retainCount == 0 && fPtr->leader != None) {
685 					XDestroyWindow(dpy, fPtr->leader);
686 					fPtr->leader = None;
687 					XFlush(dpy);
688 				}
689 			}
690 			fPtr->origLeader = None;
691 		}
692 	}
693 
694 	app = wApplicationOf(window);
695 	if (app) {
696 		if (window == app->main_window) {
697 			app->refcount = 0;
698 			wwin = app->main_window_desc->screen_ptr->focused_window;
699 			while (wwin) {
700 				if (wwin->main_window == window) {
701 					wwin->main_window = None;
702 				}
703 				wwin = wwin->prev;
704 			}
705 		}
706 		wApplicationDestroy(app);
707 	}
708 }
709 
handleExpose(XEvent * event)710 static void handleExpose(XEvent * event)
711 {
712 	WObjDescriptor *desc;
713 	XEvent ev;
714 
715 	while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
716 
717 	if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
718 		return;
719 	}
720 
721 	if (desc->handle_expose) {
722 		(*desc->handle_expose) (desc, event);
723 	}
724 }
725 
executeWheelAction(WScreen * scr,XEvent * event,int action)726 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
727 {
728 	WWindow *wwin;
729 	Bool next_direction;
730 
731 	if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
732 		next_direction = False;
733 	else
734 		next_direction = True;
735 
736 	switch (action) {
737 	case WA_SWITCH_WORKSPACES:
738 		if (next_direction)
739 			wWorkspaceRelativeChange(scr, 1);
740 		else
741 			wWorkspaceRelativeChange(scr, -1);
742 		break;
743 
744 	case WA_SWITCH_WINDOWS:
745 		wwin = scr->focused_window;
746 		if (next_direction)
747 			wWindowFocusNext(wwin, True);
748 		else
749 			wWindowFocusPrev(wwin, True);
750 		break;
751 	}
752 }
753 
executeButtonAction(WScreen * scr,XEvent * event,int action)754 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
755 {
756 	WWindow *wwin;
757 
758 	switch (action) {
759 	case WA_SELECT_WINDOWS:
760 		wUnselectWindows(scr);
761 		wSelectWindows(scr, event);
762 		break;
763 	case WA_OPEN_APPMENU:
764 		OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
765 		/* ugly hack */
766 		if (scr->root_menu) {
767 			if (scr->root_menu->brother->flags.mapped)
768 				event->xbutton.window = scr->root_menu->brother->frame->core->window;
769 			else
770 				event->xbutton.window = scr->root_menu->frame->core->window;
771 		}
772 		break;
773 	case WA_OPEN_WINLISTMENU:
774 		OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
775 		if (scr->switch_menu) {
776 			if (scr->switch_menu->brother->flags.mapped)
777 				event->xbutton.window = scr->switch_menu->brother->frame->core->window;
778 			else
779 				event->xbutton.window = scr->switch_menu->frame->core->window;
780 		}
781 		break;
782 	case WA_MOVE_PREVWORKSPACE:
783 		wWorkspaceRelativeChange(scr, -1);
784 		break;
785 	case WA_MOVE_NEXTWORKSPACE:
786 		wWorkspaceRelativeChange(scr, 1);
787 		break;
788 	case WA_MOVE_PREVWINDOW:
789 		wwin = scr->focused_window;
790 		wWindowFocusPrev(wwin, True);
791 		break;
792 	case WA_MOVE_NEXTWINDOW:
793 		wwin = scr->focused_window;
794 		wWindowFocusNext(wwin, True);
795 		break;
796 	}
797 }
798 
799 /* bindable */
handleButtonPress(XEvent * event)800 static void handleButtonPress(XEvent * event)
801 {
802 	WObjDescriptor *desc;
803 	WScreen *scr;
804 
805 	scr = wScreenForRootWindow(event->xbutton.root);
806 
807 #ifdef BALLOON_TEXT
808 	wBalloonHide(scr);
809 #endif
810 
811 	if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
812 		if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
813 			executeButtonAction(scr, event, wPreferences.mouse_button1);
814 		} else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
815 			executeButtonAction(scr, event, wPreferences.mouse_button2);
816 		} else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
817 			executeButtonAction(scr, event, wPreferences.mouse_button3);
818 		} else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
819 			executeButtonAction(scr, event, wPreferences.mouse_button8);
820 		}else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
821 			executeButtonAction(scr, event, wPreferences.mouse_button9);
822 		} else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
823 			executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
824 		} else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
825 			executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
826 		} else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
827 			executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
828 		} else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
829 			executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
830 		}
831 	}
832 
833 	desc = NULL;
834 	if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
835 		if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
836 			return;
837 		}
838 	}
839 
840 	if (desc->parent_type == WCLASS_WINDOW) {
841 		XSync(dpy, 0);
842 
843 		if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
844 			XAllowEvents(dpy, AsyncPointer, CurrentTime);
845 		} else {
846 			/*      if (wPreferences.focus_mode == WKF_CLICK) { */
847 			if (wPreferences.ignore_focus_click) {
848 				XAllowEvents(dpy, AsyncPointer, CurrentTime);
849 			}
850 			XAllowEvents(dpy, ReplayPointer, CurrentTime);
851 			/*      } */
852 		}
853 		XSync(dpy, 0);
854 	} else if (desc->parent_type == WCLASS_APPICON
855 		   || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
856 		if (event->xbutton.state & MOD_MASK) {
857 			XSync(dpy, 0);
858 			XAllowEvents(dpy, AsyncPointer, CurrentTime);
859 			XSync(dpy, 0);
860 		}
861 	}
862 
863 	if (desc->handle_mousedown != NULL) {
864 		(*desc->handle_mousedown) (desc, event);
865 	}
866 
867 	/* save double-click information */
868 	if (scr->flags.next_click_is_not_double) {
869 		scr->flags.next_click_is_not_double = 0;
870 	} else {
871 		scr->last_click_time = event->xbutton.time;
872 		scr->last_click_button = event->xbutton.button;
873 		scr->last_click_window = event->xbutton.window;
874 	}
875 }
876 
handleMapNotify(XEvent * event)877 static void handleMapNotify(XEvent * event)
878 {
879 	WWindow *wwin;
880 
881 	wwin = wWindowFor(event->xmap.event);
882 	if (wwin && wwin->client_win == event->xmap.event) {
883 		if (wwin->flags.miniaturized) {
884 			wDeiconifyWindow(wwin);
885 		} else {
886 			XGrabServer(dpy);
887 			wWindowMap(wwin);
888 			wClientSetState(wwin, NormalState, None);
889 			XUngrabServer(dpy);
890 		}
891 	}
892 }
893 
handleUnmapNotify(XEvent * event)894 static void handleUnmapNotify(XEvent * event)
895 {
896 	WWindow *wwin;
897 	XEvent ev;
898 	Bool withdraw = False;
899 
900 	/* only process windows with StructureNotify selected
901 	 * (ignore SubstructureNotify) */
902 	wwin = wWindowFor(event->xunmap.window);
903 	if (!wwin)
904 		return;
905 
906 	/* whether the event is a Withdrawal request */
907 	if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
908 		withdraw = True;
909 
910 	if (wwin->client_win != event->xunmap.event && !withdraw)
911 		return;
912 
913 	if (!wwin->flags.mapped && !withdraw
914 	    && wwin->frame->workspace == wwin->screen_ptr->current_workspace
915 	    && !wwin->flags.miniaturized && !wwin->flags.hidden)
916 		return;
917 
918 	XGrabServer(dpy);
919 	XUnmapWindow(dpy, wwin->frame->core->window);
920 	wwin->flags.mapped = 0;
921 	XSync(dpy, 0);
922 	/* check if the window was destroyed */
923 	if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
924 		DispatchEvent(&ev);
925 	} else {
926 		Bool reparented = False;
927 
928 		if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
929 			reparented = True;
930 
931 		/* withdraw window */
932 		wwin->flags.mapped = 0;
933 		if (!reparented)
934 			wClientSetState(wwin, WithdrawnState, None);
935 
936 		/* if the window was reparented, do not reparent it back to the
937 		 * root window */
938 		wUnmanageWindow(wwin, !reparented, False);
939 	}
940 	XUngrabServer(dpy);
941 }
942 
handleConfigureRequest(XEvent * event)943 static void handleConfigureRequest(XEvent * event)
944 {
945 	WWindow *wwin;
946 
947 	wwin = wWindowFor(event->xconfigurerequest.window);
948 	if (wwin == NULL) {
949 		/*
950 		 * Configure request for unmapped window
951 		 */
952 		wClientConfigure(NULL, &(event->xconfigurerequest));
953 	} else {
954 		wClientConfigure(wwin, &(event->xconfigurerequest));
955 	}
956 }
957 
handlePropertyNotify(XEvent * event)958 static void handlePropertyNotify(XEvent * event)
959 {
960 	WWindow *wwin;
961 	WApplication *wapp;
962 	Window jr;
963 	int ji;
964 	unsigned int ju;
965 
966 	wwin = wWindowFor(event->xproperty.window);
967 	if (wwin) {
968 		if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
969 			return;
970 		}
971 		wClientCheckProperty(wwin, &event->xproperty);
972 	}
973 	wapp = wApplicationOf(event->xproperty.window);
974 	if (wapp) {
975 		wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
976 	}
977 }
978 
handleClientMessage(XEvent * event)979 static void handleClientMessage(XEvent * event)
980 {
981 	WWindow *wwin;
982 	WObjDescriptor *desc;
983 
984 	/* handle transition from Normal to Iconic state */
985 	if (event->xclient.message_type == w_global.atom.wm.change_state
986 	    && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
987 
988 		wwin = wWindowFor(event->xclient.window);
989 		if (!wwin)
990 			return;
991 		if (!wwin->flags.miniaturized)
992 			wIconifyWindow(wwin);
993 	} else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
994 		WScreen *scr = wScreenForRootWindow(event->xclient.window);
995 
996 		if (!scr)
997 			return;
998 
999 		if (event->xclient.data.l[1] == 1) {	/* starting */
1000 			wColormapAllowClientInstallation(scr, True);
1001 		} else {	/* stopping */
1002 			wColormapAllowClientInstallation(scr, False);
1003 		}
1004 	} else if (event->xclient.message_type == w_global.atom.wmaker.command) {
1005 
1006 		char *command;
1007 		size_t len;
1008 
1009 		len = sizeof(event->xclient.data.b);
1010 		command = wmalloc(len + 1);
1011 		strncpy(command, event->xclient.data.b, len);
1012 
1013 		if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1014 			wwarning(_("Got Reconfigure command"));
1015 			wDefaultsCheckDomains(NULL);
1016 		} else {
1017 			wwarning(_("Got unknown command %s"), command);
1018 		}
1019 
1020 		wfree(command);
1021 
1022 	} else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1023 		WApplication *wapp;
1024 		int done = 0;
1025 		wapp = wApplicationOf(event->xclient.window);
1026 		if (wapp) {
1027 			switch (event->xclient.data.l[0]) {
1028 			case WMFHideOtherApplications:
1029 				wHideOtherApplications(wapp->main_window_desc);
1030 				done = 1;
1031 				break;
1032 
1033 			case WMFHideApplication:
1034 				wHideApplication(wapp);
1035 				done = 1;
1036 				break;
1037 			}
1038 		}
1039 		if (!done) {
1040 			wwin = wWindowFor(event->xclient.window);
1041 			if (wwin) {
1042 				switch (event->xclient.data.l[0]) {
1043 				case WMFHideOtherApplications:
1044 					wHideOtherApplications(wwin);
1045 					break;
1046 
1047 				case WMFHideApplication:
1048 					wHideApplication(wApplicationOf(wwin->main_window));
1049 					break;
1050 				}
1051 			}
1052 		}
1053 	} else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1054 		wwin = wWindowFor(event->xclient.window);
1055 		if (!wwin)
1056 			return;
1057 		switch (event->xclient.data.l[0]) {
1058 		case GSWindowLevelAttr:
1059 			{
1060 				int level = (int)event->xclient.data.l[1];
1061 
1062 				if (WINDOW_LEVEL(wwin) != level) {
1063 					ChangeStackingLevel(wwin->frame->core, level);
1064 				}
1065 			}
1066 			break;
1067 		}
1068 	} else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1069 		wwin = wWindowFor(event->xclient.window);
1070 		if (!wwin)
1071 			return;
1072 		switch (event->xclient.data.l[0]) {
1073 		case WMTitleBarNormal:
1074 			wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1075 			break;
1076 		case WMTitleBarMain:
1077 			wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1078 			break;
1079 		case WMTitleBarKey:
1080 			wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1081 			break;
1082 		}
1083 	} else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1084 		WScreen *scr = wScreenForRootWindow(event->xclient.window);
1085 		if (!scr)
1086 			return;
1087 		scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1088 	} else if (wNETWMProcessClientMessage(&event->xclient)) {
1089 		/* do nothing */
1090 #ifdef USE_DOCK_XDND
1091 	} else if (wXDNDProcessClientMessage(&event->xclient)) {
1092 		/* do nothing */
1093 #endif	/* USE_DOCK_XDND */
1094 	} else {
1095 		/*
1096 		 * Non-standard thing, but needed by OffiX DND.
1097 		 * For when the icon frame gets a ClientMessage
1098 		 * that should have gone to the icon_window.
1099 		 */
1100 		if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1101 			struct WIcon *icon = NULL;
1102 
1103 			if (desc->parent_type == WCLASS_MINIWINDOW) {
1104 				icon = (WIcon *) desc->parent;
1105 			} else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1106 				icon = ((WAppIcon *) desc->parent)->icon;
1107 			}
1108 			if (icon && (wwin = icon->owner)) {
1109 				if (wwin->client_win != event->xclient.window) {
1110 					event->xclient.window = wwin->client_win;
1111 					XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1112 				}
1113 			}
1114 		}
1115 	}
1116 }
1117 
raiseWindow(WScreen * scr)1118 static void raiseWindow(WScreen * scr)
1119 {
1120 	WWindow *wwin;
1121 
1122 	scr->autoRaiseTimer = NULL;
1123 
1124 	wwin = wWindowFor(scr->autoRaiseWindow);
1125 	if (!wwin)
1126 		return;
1127 
1128 	if (!wwin->flags.destroyed && wwin->flags.focused) {
1129 		wRaiseFrame(wwin->frame->core);
1130 		/* this is needed or a race condition will occur */
1131 		XSync(dpy, False);
1132 	}
1133 }
1134 
handleEnterNotify(XEvent * event)1135 static void handleEnterNotify(XEvent * event)
1136 {
1137 	WWindow *wwin;
1138 	WObjDescriptor *desc = NULL;
1139 	XEvent ev;
1140 	WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1141 
1142 	if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1143 		/* already left the window... */
1144 		saveTimestamp(&ev);
1145 		if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1146 			return;
1147 		}
1148 	}
1149 
1150 	if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1151 		if (desc->handle_enternotify)
1152 			(*desc->handle_enternotify) (desc, event);
1153 	}
1154 
1155 	/* enter to window */
1156 	wwin = wWindowFor(event->xcrossing.window);
1157 	if (!wwin) {
1158 		if (wPreferences.colormap_mode == WCM_POINTER) {
1159 			wColormapInstallForWindow(scr, NULL);
1160 		}
1161 		if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1162 			WMDeleteTimerHandler(scr->autoRaiseTimer);
1163 			scr->autoRaiseTimer = NULL;
1164 		}
1165 	} else {
1166 		/* set auto raise timer even if in focus-follows-mouse mode
1167 		 * and the event is for the frame window, even if the window
1168 		 * has focus already.  useful if you move the pointer from a focused
1169 		 * window to the root window and back pretty fast
1170 		 *
1171 		 * set focus if in focus-follows-mouse mode and the event
1172 		 * is for the frame window and window doesn't have focus yet */
1173 		if (wPreferences.focus_mode == WKF_SLOPPY
1174 		    && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1175 
1176 			if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1177 				wSetFocusTo(scr, wwin);
1178 
1179 			if (scr->autoRaiseTimer)
1180 				WMDeleteTimerHandler(scr->autoRaiseTimer);
1181 			scr->autoRaiseTimer = NULL;
1182 
1183 			if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1184 				scr->autoRaiseWindow = wwin->frame->core->window;
1185 				scr->autoRaiseTimer
1186 				    = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1187 			}
1188 		}
1189 		/* Install colormap for window, if the colormap installation mode
1190 		 * is colormap_follows_mouse */
1191 		if (wPreferences.colormap_mode == WCM_POINTER) {
1192 			if (wwin->client_win == event->xcrossing.window)
1193 				wColormapInstallForWindow(scr, wwin);
1194 			else
1195 				wColormapInstallForWindow(scr, NULL);
1196 		}
1197 	}
1198 
1199 	if (event->xcrossing.window == event->xcrossing.root
1200 	    && event->xcrossing.detail == NotifyNormal
1201 	    && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1202 
1203 		wSetFocusTo(scr, scr->focused_window);
1204 	}
1205 #ifdef BALLOON_TEXT
1206 	wBalloonEnteredObject(scr, desc);
1207 #endif
1208 }
1209 
handleLeaveNotify(XEvent * event)1210 static void handleLeaveNotify(XEvent * event)
1211 {
1212 	WObjDescriptor *desc = NULL;
1213 
1214 	if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1215 		if (desc->handle_leavenotify)
1216 			(*desc->handle_leavenotify) (desc, event);
1217 	}
1218 }
1219 
1220 #ifdef USE_XSHAPE
handleShapeNotify(XEvent * event)1221 static void handleShapeNotify(XEvent * event)
1222 {
1223 	XShapeEvent *shev = (XShapeEvent *) event;
1224 	WWindow *wwin;
1225 	union {
1226 		XEvent xevent;
1227 		XShapeEvent xshape;
1228 	} ev;
1229 
1230 	while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1231 		if (ev.xshape.kind == ShapeBounding) {
1232 			if (ev.xshape.shaped == shev->shaped) {
1233 				*shev = ev.xshape;
1234 			} else {
1235 				XPutBackEvent(dpy, &ev.xevent);
1236 				break;
1237 			}
1238 		}
1239 	}
1240 
1241 	wwin = wWindowFor(shev->window);
1242 	if (!wwin || shev->kind != ShapeBounding)
1243 		return;
1244 
1245 	if (!shev->shaped && wwin->flags.shaped) {
1246 
1247 		wwin->flags.shaped = 0;
1248 		wWindowClearShape(wwin);
1249 
1250 	} else if (shev->shaped) {
1251 
1252 		wwin->flags.shaped = 1;
1253 		wWindowSetShape(wwin);
1254 	}
1255 }
1256 #endif				/* USE_XSHAPE */
1257 
1258 #ifdef KEEP_XKB_LOCK_STATUS
1259 /* please help ]d if you know what to do */
handleXkbIndicatorStateNotify(XkbEvent * event)1260 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1261 {
1262 	WWindow *wwin;
1263 	WScreen *scr;
1264 	XkbStateRec staterec;
1265 	int i;
1266 
1267 	for (i = 0; i < w_global.screen_count; i++) {
1268 		scr = wScreenWithNumber(i);
1269 		wwin = scr->focused_window;
1270 		if (wwin && wwin->flags.focused) {
1271 			XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1272 			if (wwin->frame->languagemode != staterec.group) {
1273 				wwin->frame->last_languagemode = wwin->frame->languagemode;
1274 				wwin->frame->languagemode = staterec.group;
1275 			}
1276 #ifdef XKB_BUTTON_HINT
1277 			if (wwin->frame->titlebar) {
1278 				wFrameWindowPaint(wwin->frame);
1279 			}
1280 #endif
1281 		}
1282 	}
1283 }
1284 #endif				/*KEEP_XKB_LOCK_STATUS */
1285 
handleColormapNotify(XEvent * event)1286 static void handleColormapNotify(XEvent * event)
1287 {
1288 	WWindow *wwin;
1289 	WScreen *scr;
1290 	Bool reinstall = False;
1291 
1292 	wwin = wWindowFor(event->xcolormap.window);
1293 	if (!wwin)
1294 		return;
1295 
1296 	scr = wwin->screen_ptr;
1297 
1298 	do {
1299 		if (wwin) {
1300 			if (event->xcolormap.new) {
1301 				XWindowAttributes attr;
1302 
1303 				XGetWindowAttributes(dpy, wwin->client_win, &attr);
1304 
1305 				if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1306 					scr->current_colormap = attr.colormap;
1307 
1308 				reinstall = True;
1309 			} else if (event->xcolormap.state == ColormapUninstalled &&
1310 				   scr->current_colormap == event->xcolormap.colormap) {
1311 
1312 				/* some bastard app (like XV) removed our colormap */
1313 				/*
1314 				 * can't enforce or things like xscreensaver won't work
1315 				 * reinstall = True;
1316 				 */
1317 			} else if (event->xcolormap.state == ColormapInstalled &&
1318 				   scr->current_colormap == event->xcolormap.colormap) {
1319 
1320 				/* someone has put our colormap back */
1321 				reinstall = False;
1322 			}
1323 		}
1324 	} while (XCheckTypedEvent(dpy, ColormapNotify, event)
1325 		 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1326 
1327 	if (reinstall && scr->current_colormap != None) {
1328 		if (!scr->flags.colormap_stuff_blocked)
1329 			XInstallColormap(dpy, scr->current_colormap);
1330 	}
1331 }
1332 
handleFocusIn(XEvent * event)1333 static void handleFocusIn(XEvent * event)
1334 {
1335 	WWindow *wwin;
1336 
1337 	/*
1338 	 * For applications that like stealing the focus.
1339 	 */
1340 	while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1341 	saveTimestamp(event);
1342 	if (event->xfocus.mode == NotifyUngrab
1343 	    || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1344 		return;
1345 	}
1346 
1347 	wwin = wWindowFor(event->xfocus.window);
1348 	if (wwin && !wwin->flags.focused) {
1349 		if (wwin->flags.mapped)
1350 			wSetFocusTo(wwin->screen_ptr, wwin);
1351 		else
1352 			wSetFocusTo(wwin->screen_ptr, NULL);
1353 	} else if (!wwin) {
1354 		WScreen *scr = wScreenForWindow(event->xfocus.window);
1355 		if (scr)
1356 			wSetFocusTo(scr, NULL);
1357 	}
1358 }
1359 
windowUnderPointer(WScreen * scr)1360 static WWindow *windowUnderPointer(WScreen * scr)
1361 {
1362 	unsigned int mask;
1363 	int foo;
1364 	Window bar, win;
1365 
1366 	if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1367 		return wWindowFor(win);
1368 	return NULL;
1369 }
1370 
CheckFullScreenWindowFocused(WScreen * scr)1371 static int CheckFullScreenWindowFocused(WScreen * scr)
1372 {
1373 	if (scr->focused_window && scr->focused_window->flags.fullscreen)
1374 		return 1;
1375 	else
1376 		return 0;
1377 }
1378 
handleKeyPress(XEvent * event)1379 static void handleKeyPress(XEvent * event)
1380 {
1381 	WScreen *scr = wScreenForRootWindow(event->xkey.root);
1382 	WWindow *wwin = scr->focused_window;
1383 	short i, widx;
1384 	int modifiers;
1385 	int command = -1;
1386 #ifdef KEEP_XKB_LOCK_STATUS
1387 	XkbStateRec staterec;
1388 #endif				/*KEEP_XKB_LOCK_STATUS */
1389 
1390 	/* ignore CapsLock */
1391 	modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1392 
1393 	for (i = 0; i < WKBD_LAST; i++) {
1394 		if (wKeyBindings[i].keycode == 0)
1395 			continue;
1396 
1397 		if (wKeyBindings[i].keycode == event->xkey.keycode && (	/*wKeyBindings[i].modifier==0
1398 									   || */ wKeyBindings[i].modifier ==
1399 									      modifiers)) {
1400 			command = i;
1401 			break;
1402 		}
1403 	}
1404 
1405 	if (command < 0) {
1406 
1407 		if (!wRootMenuPerformShortcut(event)) {
1408 			static int dontLoop = 0;
1409 
1410 			if (dontLoop > 10) {
1411 				wwarning("problem with key event processing code");
1412 				return;
1413 			}
1414 			dontLoop++;
1415 			/* if the focused window is an internal window, try redispatching
1416 			 * the event to the managed window, as it can be a WINGs window */
1417 			if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1418 				/* client_leader contains the WINGs toplevel */
1419 				event->xany.window = wwin->client_leader;
1420 				WMHandleEvent(event);
1421 			}
1422 			dontLoop--;
1423 		}
1424 		return;
1425 	}
1426 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1427 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1428 
1429 	switch (command) {
1430 
1431 	case WKBD_ROOTMENU:
1432 		/*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1433 		if (!CheckFullScreenWindowFocused(scr)) {
1434 			WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1435 			OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1436 				     True);
1437 		}
1438 		break;
1439 	case WKBD_WINDOWLIST:
1440 		if (!CheckFullScreenWindowFocused(scr)) {
1441 			WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1442 			OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1443 				       True);
1444 		}
1445 		break;
1446 
1447 	case WKBD_WINDOWMENU:
1448 		if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1449 			OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1450 		break;
1451 	case WKBD_MINIMIZEALL:
1452 		CloseWindowMenu(scr);
1453 		wHideAll(scr);
1454 		break;
1455 	case WKBD_MINIATURIZE:
1456 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1457 		    && !WFLAGP(wwin, no_miniaturizable)) {
1458 			CloseWindowMenu(scr);
1459 
1460 			if (wwin->protocols.MINIATURIZE_WINDOW)
1461 				wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1462 			else {
1463 				wIconifyWindow(wwin);
1464 			}
1465 		}
1466 		break;
1467 	case WKBD_HIDE:
1468 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1469 			WApplication *wapp = wApplicationOf(wwin->main_window);
1470 			CloseWindowMenu(scr);
1471 
1472 			if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1473 				wHideApplication(wapp);
1474 			}
1475 		}
1476 		break;
1477 	case WKBD_HIDE_OTHERS:
1478 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1479 			CloseWindowMenu(scr);
1480 
1481 			wHideOtherApplications(wwin);
1482 		}
1483 		break;
1484 	case WKBD_MAXIMIZE:
1485 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 			CloseWindowMenu(scr);
1487 
1488 			handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1489 		}
1490 		break;
1491 	case WKBD_VMAXIMIZE:
1492 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 			CloseWindowMenu(scr);
1494 
1495 			handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1496 		}
1497 		break;
1498 	case WKBD_HMAXIMIZE:
1499 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1500 			CloseWindowMenu(scr);
1501 
1502 			handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1503 			movePionterToWindowCenter(wwin);
1504 		}
1505 		break;
1506 	case WKBD_LHMAXIMIZE:
1507 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1508 			CloseWindowMenu(scr);
1509 
1510 			handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1511 			movePionterToWindowCenter(wwin);
1512 		}
1513 		break;
1514 	case WKBD_RHMAXIMIZE:
1515 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1516 			CloseWindowMenu(scr);
1517 
1518 			handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1519 			movePionterToWindowCenter(wwin);
1520 		}
1521 		break;
1522 	case WKBD_THMAXIMIZE:
1523 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1524 			CloseWindowMenu(scr);
1525 
1526 			handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1527 			movePionterToWindowCenter(wwin);
1528 		}
1529 		break;
1530 	case WKBD_BHMAXIMIZE:
1531 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1532 			CloseWindowMenu(scr);
1533 
1534 			handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1535 			movePionterToWindowCenter(wwin);
1536 		}
1537 		break;
1538 	case WKBD_LTCMAXIMIZE:
1539 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1540 			CloseWindowMenu(scr);
1541 
1542 			handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1543 			movePionterToWindowCenter(wwin);
1544 		}
1545 		break;
1546 	case WKBD_RTCMAXIMIZE:
1547 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1548 			CloseWindowMenu(scr);
1549 
1550 			handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1551 			movePionterToWindowCenter(wwin);
1552 		}
1553 		break;
1554 	case WKBD_LBCMAXIMIZE:
1555 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1556 			CloseWindowMenu(scr);
1557 
1558 			handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1559 			movePionterToWindowCenter(wwin);
1560 		}
1561 		 break;
1562 	case WKBD_RBCMAXIMIZE:
1563 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1564 			CloseWindowMenu(scr);
1565 
1566 			handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1567 			movePionterToWindowCenter(wwin);
1568 		}
1569 		break;
1570 	case WKBD_MAXIMUS:
1571 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1572 			CloseWindowMenu(scr);
1573 
1574 			handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1575 		}
1576 		break;
1577 	case WKBD_KEEP_ON_TOP:
1578 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1579 			CloseWindowMenu(scr);
1580 
1581 			if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
1582 				ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
1583 			else
1584 				ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1585 		}
1586 		break;
1587 
1588 	case WKBD_KEEP_AT_BOTTOM:
1589 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1590 			CloseWindowMenu(scr);
1591 
1592 			if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
1593 				ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
1594 			else
1595 				ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1596 		}
1597 		break;
1598 	case WKBD_OMNIPRESENT:
1599 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1600 			CloseWindowMenu(scr);
1601 
1602 			wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1603 		}
1604 		break;
1605 	case WKBD_RAISE:
1606 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1607 			CloseWindowMenu(scr);
1608 
1609 			wRaiseFrame(wwin->frame->core);
1610 		}
1611 		break;
1612 	case WKBD_LOWER:
1613 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1614 			CloseWindowMenu(scr);
1615 
1616 			wLowerFrame(wwin->frame->core);
1617 		}
1618 		break;
1619 	case WKBD_RAISELOWER:
1620 		/* raise or lower the window under the pointer, not the
1621 		 * focused one
1622 		 */
1623 		wwin = windowUnderPointer(scr);
1624 		if (wwin)
1625 			wRaiseLowerFrame(wwin->frame->core);
1626 		break;
1627 	case WKBD_SHADE:
1628 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1629 			if (wwin->flags.shaded)
1630 				wUnshadeWindow(wwin);
1631 			else
1632 				wShadeWindow(wwin);
1633 		}
1634 		break;
1635 	case WKBD_MOVERESIZE:
1636 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1637 			CloseWindowMenu(scr);
1638 
1639 			wKeyboardMoveResizeWindow(wwin);
1640 		}
1641 		break;
1642 	case WKBD_CLOSE:
1643 		if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1644 			CloseWindowMenu(scr);
1645 			if (wwin->protocols.DELETE_WINDOW)
1646 				wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1647 		}
1648 		break;
1649 	case WKBD_SELECT:
1650 		if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1651 			wSelectWindow(wwin, !wwin->flags.selected);
1652 		}
1653 		break;
1654 
1655 	case WKBD_WORKSPACEMAP:
1656 		if (wPreferences.enable_workspace_pager)
1657 			StartWorkspaceMap(scr);
1658 		break;
1659 
1660 	case WKBD_FOCUSNEXT:
1661 		StartWindozeCycle(wwin, event, True, False);
1662 		break;
1663 
1664 	case WKBD_FOCUSPREV:
1665 		StartWindozeCycle(wwin, event, False, False);
1666 		break;
1667 
1668 	case WKBD_GROUPNEXT:
1669 		StartWindozeCycle(wwin, event, True, True);
1670 		break;
1671 
1672 	case WKBD_GROUPPREV:
1673 		StartWindozeCycle(wwin, event, False, True);
1674 		break;
1675 
1676 	case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1677 		widx = command - WKBD_WORKSPACE1;
1678 		i = (scr->current_workspace / 10) * 10 + widx;
1679 		if (wPreferences.ws_advance || i < scr->workspace_count)
1680 			wWorkspaceChange(scr, i);
1681 		break;
1682 
1683 	case WKBD_NEXTWORKSPACE:
1684 		wWorkspaceRelativeChange(scr, 1);
1685 		break;
1686 	case WKBD_PREVWORKSPACE:
1687 		wWorkspaceRelativeChange(scr, -1);
1688 		break;
1689 	case WKBD_LASTWORKSPACE:
1690 		wWorkspaceChange(scr, scr->last_workspace);
1691 		break;
1692 
1693 	case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1694 		widx = command - WKBD_MOVE_WORKSPACE1;
1695 		i = (scr->current_workspace / 10) * 10 + widx;
1696 		if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1697 			wWindowChangeWorkspace(wwin, i);
1698 		break;
1699 
1700 	case WKBD_MOVE_NEXTWORKSPACE:
1701 		if (wwin)
1702 			wWindowChangeWorkspaceRelative(wwin, 1);
1703 		break;
1704 	case WKBD_MOVE_PREVWORKSPACE:
1705 		if (wwin)
1706 			wWindowChangeWorkspaceRelative(wwin, -1);
1707 		break;
1708 	case WKBD_MOVE_LASTWORKSPACE:
1709 		if (wwin)
1710 			wWindowChangeWorkspace(wwin, scr->last_workspace);
1711 		break;
1712 
1713 	case WKBD_MOVE_NEXTWSLAYER:
1714 	case WKBD_MOVE_PREVWSLAYER:
1715 		{
1716 			if (wwin) {
1717 				int row, column;
1718 
1719 				row = scr->current_workspace / 10;
1720 				column = scr->current_workspace % 10;
1721 
1722 				if (command == WKBD_MOVE_NEXTWSLAYER) {
1723 					if ((row + 1) * 10 < scr->workspace_count)
1724 						wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1725 				} else {
1726 					if (row > 0)
1727 						wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1728 				}
1729 			}
1730 		}
1731 		break;
1732 
1733 	case WKBD_WINDOW1:
1734 	case WKBD_WINDOW2:
1735 	case WKBD_WINDOW3:
1736 	case WKBD_WINDOW4:
1737 	case WKBD_WINDOW5:
1738 	case WKBD_WINDOW6:
1739 	case WKBD_WINDOW7:
1740 	case WKBD_WINDOW8:
1741 	case WKBD_WINDOW9:
1742 	case WKBD_WINDOW10:
1743 
1744 		widx = command - WKBD_WINDOW1;
1745 
1746 		if (scr->shortcutWindows[widx]) {
1747 			WMArray *list = scr->shortcutWindows[widx];
1748 			int cw;
1749 			int count = WMGetArrayItemCount(list);
1750 			WWindow *twin;
1751 			WMArrayIterator iter;
1752 			WWindow *wwin;
1753 
1754 			wUnselectWindows(scr);
1755 			cw = scr->current_workspace;
1756 
1757 			WM_ETARETI_ARRAY(list, wwin, iter) {
1758 				if (count > 1)
1759 					wWindowChangeWorkspace(wwin, cw);
1760 
1761 				wMakeWindowVisible(wwin);
1762 
1763 				if (count > 1)
1764 					wSelectWindow(wwin, True);
1765 			}
1766 
1767 			/* rotate the order of windows, to create a cycling effect */
1768 			twin = WMGetFromArray(list, 0);
1769 			WMDeleteFromArray(list, 0);
1770 			WMAddToArray(list, twin);
1771 
1772 		} else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1773 			if (scr->shortcutWindows[widx]) {
1774 				WMFreeArray(scr->shortcutWindows[widx]);
1775 				scr->shortcutWindows[widx] = NULL;
1776 			}
1777 
1778 			if (wwin->flags.selected && scr->selected_windows) {
1779 				scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1780 				/*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1781 				   WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1782 			} else {
1783 				scr->shortcutWindows[widx] = WMCreateArray(4);
1784 				WMAddToArray(scr->shortcutWindows[widx], wwin);
1785 			}
1786 
1787 			wSelectWindow(wwin, !wwin->flags.selected);
1788 			XFlush(dpy);
1789 			wusleep(3000);
1790 			wSelectWindow(wwin, !wwin->flags.selected);
1791 			XFlush(dpy);
1792 
1793 		} else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1794 
1795 			if (wwin->flags.selected && scr->selected_windows) {
1796 				if (scr->shortcutWindows[widx]) {
1797 					WMFreeArray(scr->shortcutWindows[widx]);
1798 				}
1799 				scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1800 			}
1801 		}
1802 
1803 		break;
1804 
1805 	case WKBD_MOVE_12_TO_6_HEAD:
1806 	case WKBD_MOVE_6_TO_12_HEAD:
1807 		if (wwin)
1808 			moveBetweenHeads(wwin, command - WKBD_MOVE_12_TO_6_HEAD);
1809 
1810 		break;
1811 
1812 	case WKBD_RELAUNCH:
1813 		if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1814 			(void) RelaunchWindow(wwin);
1815 
1816 		break;
1817 
1818 	case WKBD_SWITCH_SCREEN:
1819 		if (w_global.screen_count > 1) {
1820 			WScreen *scr2;
1821 			int i;
1822 
1823 			/* find index of this screen */
1824 			for (i = 0; i < w_global.screen_count; i++) {
1825 				if (wScreenWithNumber(i) == scr)
1826 					break;
1827 			}
1828 			i++;
1829 			if (i >= w_global.screen_count) {
1830 				i = 0;
1831 			}
1832 			scr2 = wScreenWithNumber(i);
1833 
1834 			if (scr2) {
1835 				XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1836 					     scr2->scr_width / 2, scr2->scr_height / 2);
1837 			}
1838 		}
1839 		break;
1840 
1841 	case WKBD_RUN:
1842 	{
1843 		char *cmdline;
1844 
1845 		cmdline = ExpandOptions(scr, _("exec %A(Run,Type command to run:)"));
1846 
1847 		if (cmdline) {
1848 			XGrabPointer(dpy, scr->root_win, True, 0,
1849 			     GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1850 			XSync(dpy, False);
1851 
1852 			ExecuteShellCommand(scr, cmdline);
1853 			wfree(cmdline);
1854 
1855 			XUngrabPointer(dpy, CurrentTime);
1856 			XSync(dpy, False);
1857 		}
1858 		break;
1859 	}
1860 
1861 	case WKBD_NEXTWSLAYER:
1862 	case WKBD_PREVWSLAYER:
1863 		{
1864 			int row, column;
1865 
1866 			row = scr->current_workspace / 10;
1867 			column = scr->current_workspace % 10;
1868 
1869 			if (command == WKBD_NEXTWSLAYER) {
1870 				if ((row + 1) * 10 < scr->workspace_count)
1871 					wWorkspaceChange(scr, column + (row + 1) * 10);
1872 			} else {
1873 				if (row > 0)
1874 					wWorkspaceChange(scr, column + (row - 1) * 10);
1875 			}
1876 		}
1877 		break;
1878 	case WKBD_CLIPRAISELOWER:
1879 		if (!wPreferences.flags.noclip)
1880 			wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1881 		break;
1882 	case WKBD_DOCKRAISELOWER:
1883 		if (!wPreferences.flags.nodock)
1884 			wDockRaiseLower(scr->dock);
1885 		break;
1886 #ifdef KEEP_XKB_LOCK_STATUS
1887 	case WKBD_TOGGLE:
1888 		if (wPreferences.modelock) {
1889 			/*toggle */
1890 			wwin = scr->focused_window;
1891 
1892 			if (wwin && wwin->flags.mapped
1893 			    && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1894 			    && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1895 				XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1896 
1897 				wwin->frame->languagemode = wwin->frame->last_languagemode;
1898 				wwin->frame->last_languagemode = staterec.group;
1899 				XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1900 
1901 			}
1902 		}
1903 		break;
1904 #endif	/* KEEP_XKB_LOCK_STATUS */
1905 	}
1906 }
1907 
handleMotionNotify(XEvent * event)1908 static void handleMotionNotify(XEvent * event)
1909 {
1910 	WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1911 
1912 	if (wPreferences.scrollable_menus) {
1913 		WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1914 		WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1915 
1916 		if (scr->flags.jump_back_pending ||
1917 		    p.x <= (rect.pos.x + 1) ||
1918 		    p.x >= (rect.pos.x + rect.size.width - 2) ||
1919 		    p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1920 			WMenu *menu;
1921 
1922 			menu = wMenuUnderPointer(scr);
1923 			if (menu != NULL)
1924 				wMenuScroll(menu);
1925 		}
1926 	}
1927 }
1928 
handleVisibilityNotify(XEvent * event)1929 static void handleVisibilityNotify(XEvent * event)
1930 {
1931 	WWindow *wwin;
1932 
1933 	wwin = wWindowFor(event->xvisibility.window);
1934 	if (!wwin)
1935 		return;
1936 	wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
1937 }
1938 
handle_selection_request(XSelectionRequestEvent * event)1939 static void handle_selection_request(XSelectionRequestEvent *event)
1940 {
1941 #ifdef USE_ICCCM_WMREPLACE
1942 	static Atom atom_version = None;
1943 	WScreen *scr;
1944 	XSelectionEvent notify;
1945 
1946 	/*
1947 	 * This event must be sent to the slection requester to not block him
1948 	 *
1949 	 * We create it with the answer 'there is no selection' by default
1950 	 */
1951 	notify.type = SelectionNotify;
1952 	notify.display = dpy;
1953 	notify.requestor = event->requestor;
1954 	notify.selection = event->selection;
1955 	notify.target = event->target;
1956 	notify.property = None; /* This says that there is no selection */
1957 	notify.time = event->time;
1958 
1959 	scr = wScreenForWindow(event->owner);
1960 	if (!scr)
1961 		goto not_our_selection;
1962 
1963 	if (event->owner != scr->info_window)
1964 		goto not_our_selection;
1965 
1966 	if (event->selection != scr->sn_atom)
1967 		goto not_our_selection;
1968 
1969 	if (atom_version == None)
1970 		atom_version = XInternAtom(dpy, "VERSION", False);
1971 
1972 	if (event->target == atom_version) {
1973 		static const long icccm_version[] = { 2, 0 };
1974 
1975 		/*
1976 		 * This protocol is defined in ICCCM 2.0:
1977 		 * http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html
1978 		 *  "Communication with the Window Manager by Means of Selections"
1979 		 */
1980 
1981 		/*
1982 		 * Setting the property means the content of the selection is available
1983 		 * According to the ICCCM spec, we need to support being asked for a property
1984 		 * set to 'None' for compatibility with old clients
1985 		 */
1986 		notify.property = (event->property == None)?(event->target):(event->property);
1987 
1988 		XChangeProperty(dpy, event->requestor, notify.property,
1989 		                XA_INTEGER, 32, PropModeReplace,
1990 		                (unsigned char *) icccm_version, wlengthof(icccm_version));
1991 	}
1992 
1993  not_our_selection:
1994 	if (notify.property == None)
1995 		wwarning("received SelectionRequest(%s) for target=\"%s\" from requestor 0x%lX but we have no answer",
1996 		         XGetAtomName(dpy, event->selection), XGetAtomName(dpy, event->target), (long) event->requestor);
1997 
1998 	/* Send the answer to the requestor */
1999 	XSendEvent(dpy, event->requestor, False, 0L, (XEvent *) &notify);
2000 
2001 #else
2002 	/*
2003 	 * If the support for ICCCM window manager replacement was not enabled, we should not receive
2004 	 * this kind of event, so we just ignore it (Conceptually, we should reply with 'SelectionNotify'
2005 	 * event with property set to 'None' to tell that we don't have this selection, but that is a bit
2006 	 * costly for an event that shall never happen).
2007 	 */
2008 	(void) event;
2009 #endif
2010 }
2011 
handle_selection_clear(XSelectionClearEvent * event)2012 static void handle_selection_clear(XSelectionClearEvent *event)
2013 {
2014 #ifdef USE_ICCCM_WMREPLACE
2015 	WScreen *scr = wScreenForWindow(event->window);
2016 
2017 	if (!scr)
2018 		return;
2019 
2020 	if (event->selection != scr->sn_atom)
2021 		return;
2022 
2023 	wmessage(_("another window manager is replacing us!"));
2024 	Shutdown(WSExitMode);
2025 #else
2026 	/*
2027 	 * If the support for ICCCM window manager replacement was not enabled, we should not receive
2028 	 * this kind of event, so we simply do nothing.
2029 	 */
2030 	(void) event;
2031 #endif
2032 }
2033