wmaker: Fix typos (used codespell).
[wmaker-crm.git] / src / event.c
blob800cc87bb723ea9534aa2bfb998f78e1b608d15e
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 2014 Window Maker Team
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.
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.
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.
23 #include "wconfig.h"
25 #ifdef HAVE_INOTIFY
26 #include <sys/select.h>
27 #include <sys/inotify.h>
28 #endif
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>
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
48 #ifdef USE_RANDR
49 #include <X11/extensions/Xrandr.h>
50 #endif
52 #ifdef KEEP_XKB_LOCK_STATUS
53 #include <X11/XKBlib.h>
54 #endif /* KEEP_XKB_LOCK_STATUS */
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"
84 #define MOD_MASK wPreferences.modifier_mask
86 /************ Local stuff ***********/
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);
112 #ifdef USE_XSHAPE
113 static void handleShapeNotify(XEvent *event);
114 #endif
116 #ifdef KEEP_XKB_LOCK_STATUS
117 static void handleXkbIndicatorStateNotify(XkbEvent *event);
118 #endif
120 /* real dead process handler */
121 static void handleDeadProcess(void);
123 typedef struct DeadProcesses {
124 pid_t pid;
125 unsigned char exit_status;
126 } DeadProcesses;
128 /* stack of dead processes */
129 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
130 static int deadProcessPtr = 0;
132 typedef struct DeathHandler {
133 WDeathHandler *callback;
134 pid_t pid;
135 void *client_data;
136 } DeathHandler;
138 static WMArray *deathHandlers = NULL;
140 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
142 DeathHandler *handler;
144 handler = malloc(sizeof(DeathHandler));
145 if (!handler)
146 return 0;
148 handler->pid = pid;
149 handler->callback = callback;
150 handler->client_data = cdata;
152 if (!deathHandlers)
153 deathHandlers = WMCreateArrayWithDestructor(8, free);
155 WMAddToArray(deathHandlers, handler);
157 return handler;
160 static void wdelete_death_handler(WMagicNumber id)
162 DeathHandler *handler = (DeathHandler *) id;
164 if (!handler || !deathHandlers)
165 return;
167 /* array destructor will call free(handler) */
168 WMRemoveFromArray(deathHandlers, handler);
171 void DispatchEvent(XEvent * event)
173 if (deathHandlers)
174 handleDeadProcess();
176 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
177 WCHANGE_STATE(WSTATE_EXITING);
178 /* received SIGTERM */
180 * WMHandleEvent() can't be called from anything
181 * executed inside here, or we can get in a infinite
182 * recursive loop.
184 Shutdown(WSExitMode);
186 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
187 WCHANGE_STATE(WSTATE_RESTARTING);
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);
197 /* for the case that all that is wanted to be dispatched is
198 * the stuff above */
199 if (!event)
200 return;
202 saveTimestamp(event);
203 switch (event->type) {
204 case MapRequest:
205 handleMapRequest(event);
206 break;
208 case KeyPress:
209 handleKeyPress(event);
210 break;
212 case MotionNotify:
213 handleMotionNotify(event);
214 break;
216 case ConfigureRequest:
217 handleConfigureRequest(event);
218 break;
220 case DestroyNotify:
221 handleDestroyNotify(event);
222 break;
224 case MapNotify:
225 handleMapNotify(event);
226 break;
228 case UnmapNotify:
229 handleUnmapNotify(event);
230 break;
232 case ButtonPress:
233 handleButtonPress(event);
234 break;
236 case Expose:
237 handleExpose(event);
238 break;
240 case PropertyNotify:
241 handlePropertyNotify(event);
242 break;
244 case EnterNotify:
245 handleEnterNotify(event);
246 break;
248 case LeaveNotify:
249 handleLeaveNotify(event);
250 break;
252 case ClientMessage:
253 handleClientMessage(event);
254 break;
256 case ColormapNotify:
257 handleColormapNotify(event);
258 break;
260 case MappingNotify:
261 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
262 XRefreshKeyboardMapping(&event->xmapping);
263 break;
265 case FocusIn:
266 handleFocusIn(event);
267 break;
269 case VisibilityNotify:
270 handleVisibilityNotify(event);
271 break;
273 case ConfigureNotify:
274 #ifdef USE_RANDR
275 if (event->xconfigure.window == DefaultRootWindow(dpy))
276 XRRUpdateConfiguration(event);
277 #endif
278 break;
280 case SelectionRequest:
281 handle_selection_request(&event->xselectionrequest);
282 break;
284 case SelectionClear:
285 handle_selection_clear(&event->xselectionclear);
286 break;
288 default:
289 handleExtensions(event);
290 break;
294 #ifdef HAVE_INOTIFY
296 *----------------------------------------------------------------------
297 * handle_inotify_events-
298 * Check for inotify events
300 * Returns:
301 * After reading events for the given file descriptor (fd) and
302 * watch descriptor (wd)
304 * Side effects:
305 * Calls wDefaultsCheckDomains if config database is updated
306 *----------------------------------------------------------------------
308 static void handle_inotify_events(void)
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;
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().
324 eventQLength = read(w_global.inotify.fd_event_queue,
325 buff, sizeof(buff) );
327 if (eventQLength < 0) {
328 wwarning(_("read problem when trying to get INotify event: %s"), strerror(errno));
329 return;
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];
338 * see inotify.h for event types.
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"));
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;
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."));
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;
358 wPreferences.flags.noupdates = 1;
360 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
361 wwarning(_("Inotify: Reading config files in defaults database."));
362 wDefaultsCheckDomains(NULL);
363 oneShotFlag = 1;
366 /* move to next event in the buffer */
367 i += sizeof(struct inotify_event) + pevent->len;
370 #endif /* HAVE_INOTIFY */
373 *----------------------------------------------------------------------
374 * EventLoop-
375 * Processes X and internal events indefinitely.
377 * Returns:
378 * Never returns
380 * Side effects:
381 * The LastTimestamp global variable is updated.
382 * Calls inotifyGetEvents if defaults database changes.
383 *----------------------------------------------------------------------
385 noreturn void EventLoop(void)
387 XEvent event;
388 #ifdef HAVE_INOTIFY
389 struct timeval time;
390 fd_set rfds;
391 int retVal = 0;
393 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
394 retVal = -1;
395 #endif
397 for (;;) {
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);
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);
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;
419 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
420 handle_inotify_events();
422 #endif
427 *----------------------------------------------------------------------
428 * ProcessPendingEvents --
429 * Processes the events that are currently pending (at the time
430 * this function is called) in the display's queue.
432 * Returns:
433 * After the pending events that were present at the function call
434 * are processed.
436 * Side effects:
437 * Many -- whatever handling events may involve.
439 *----------------------------------------------------------------------
441 void ProcessPendingEvents(void)
443 XEvent event;
444 int count;
446 XSync(dpy, False);
448 /* Take a snapshot of the event count in the queue */
449 count = XPending(dpy);
451 while (count > 0 && XPending(dpy)) {
452 WMNextEvent(dpy, &event);
453 WMHandleEvent(&event);
454 count--;
458 Bool IsDoubleClick(WScreen * scr, XEvent * event)
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)) {
465 scr->flags.next_click_is_not_double = 1;
466 scr->last_click_time = 0;
467 scr->last_click_window = event->xbutton.window;
469 return True;
471 return False;
474 void NotifyDeadProcess(pid_t pid, unsigned char status)
476 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
477 wwarning("stack overflow: too many dead processes");
478 return;
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++;
487 static void handleDeadProcess(void)
489 DeathHandler *tmp;
490 int i;
492 for (i = 0; i < deadProcessPtr; i++) {
493 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
496 if (!deathHandlers) {
497 deadProcessPtr = 0;
498 return;
501 /* get the pids on the queue and call handlers */
502 while (deadProcessPtr > 0) {
503 deadProcessPtr--;
505 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
506 tmp = WMGetFromArray(deathHandlers, i);
507 if (!tmp)
508 continue;
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);
519 static void saveTimestamp(XEvent * event)
522 * Never save CurrentTime as LastTimestamp because CurrentTime
523 * it's not a real timestamp (it's the 0L constant)
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;
560 static int matchWindow(const void *item, const void *cdata)
562 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
565 static void handleExtensions(XEvent * event)
567 #ifdef USE_XSHAPE
568 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
569 handleShapeNotify(event);
571 #endif
572 #ifdef KEEP_XKB_LOCK_STATUS
573 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
574 handleXkbIndicatorStateNotify((XkbEvent *) event);
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);
587 #endif
590 static void handleMapRequest(XEvent * ev)
592 WWindow *wwin;
593 WScreen *scr = NULL;
594 Window window = ev->xmaprequest.window;
596 wwin = wWindowFor(window);
597 if (wwin != NULL) {
598 if (wwin->flags.shaded) {
599 wUnshadeWindow(wwin);
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);
610 wUnhideApplication(wapp, False, False);
612 return;
615 scr = wScreenForRootWindow(ev->xmaprequest.parent);
617 wwin = wManageWindow(scr, window);
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).
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);
632 if (wwin) {
633 wClientSetState(wwin, NormalState, None);
634 if (wwin->flags.maximized) {
635 wMaximizeWindow(wwin, wwin->flags.maximized);
637 if (wwin->flags.shaded) {
638 wwin->flags.shaded = 0;
639 wwin->flags.skip_next_animation = 1;
640 wShadeWindow(wwin);
642 if (wwin->flags.miniaturized) {
643 wwin->flags.miniaturized = 0;
644 wwin->flags.skip_next_animation = 1;
645 wIconifyWindow(wwin);
647 if (wwin->flags.fullscreen) {
648 wwin->flags.fullscreen = 0;
649 wFullscreenWindow(wwin);
651 if (wwin->flags.hidden) {
652 WApplication *wapp = wApplicationOf(wwin->main_window);
654 wwin->flags.hidden = 0;
655 wwin->flags.skip_next_animation = 1;
656 if (wapp) {
657 wHideApplication(wapp);
663 static void handleDestroyNotify(XEvent * event)
665 WWindow *wwin;
666 WApplication *app;
667 Window window = event->xdestroywindow.window;
668 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
669 int widx;
671 wwin = wWindowFor(window);
672 if (wwin) {
673 wUnmanageWindow(wwin, False, True);
676 if (scr != NULL) {
677 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
678 WFakeGroupLeader *fPtr;
680 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
681 if (fPtr->retainCount > 0) {
682 fPtr->retainCount--;
683 if (fPtr->retainCount == 0 && fPtr->leader != None) {
684 XDestroyWindow(dpy, fPtr->leader);
685 fPtr->leader = None;
686 XFlush(dpy);
689 fPtr->origLeader = None;
693 app = wApplicationOf(window);
694 if (app) {
695 if (window == app->main_window) {
696 app->refcount = 0;
697 wwin = app->main_window_desc->screen_ptr->focused_window;
698 while (wwin) {
699 if (wwin->main_window == window) {
700 wwin->main_window = None;
702 wwin = wwin->prev;
705 wApplicationDestroy(app);
709 static void handleExpose(XEvent * event)
711 WObjDescriptor *desc;
712 XEvent ev;
714 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
716 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
717 return;
720 if (desc->handle_expose) {
721 (*desc->handle_expose) (desc, event);
725 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
727 WWindow *wwin;
728 Bool next_direction;
730 if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
731 next_direction = False;
732 else
733 next_direction = True;
735 switch (action) {
736 case WA_SWITCH_WORKSPACES:
737 if (next_direction)
738 wWorkspaceRelativeChange(scr, 1);
739 else
740 wWorkspaceRelativeChange(scr, -1);
741 break;
743 case WA_SWITCH_WINDOWS:
744 wwin = scr->focused_window;
745 if (next_direction)
746 wWindowFocusNext(wwin, True);
747 else
748 wWindowFocusPrev(wwin, True);
749 break;
753 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
755 WWindow *wwin;
757 switch (action) {
758 case WA_SELECT_WINDOWS:
759 wUnselectWindows(scr);
760 wSelectWindows(scr, event);
761 break;
762 case WA_OPEN_APPMENU:
763 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
764 /* ugly hack */
765 if (scr->root_menu) {
766 if (scr->root_menu->brother->flags.mapped)
767 event->xbutton.window = scr->root_menu->brother->frame->core->window;
768 else
769 event->xbutton.window = scr->root_menu->frame->core->window;
771 break;
772 case WA_OPEN_WINLISTMENU:
773 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
774 if (scr->switch_menu) {
775 if (scr->switch_menu->brother->flags.mapped)
776 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
777 else
778 event->xbutton.window = scr->switch_menu->frame->core->window;
780 break;
781 case WA_MOVE_PREVWORKSPACE:
782 wWorkspaceRelativeChange(scr, -1);
783 break;
784 case WA_MOVE_NEXTWORKSPACE:
785 wWorkspaceRelativeChange(scr, 1);
786 break;
787 case WA_MOVE_PREVWINDOW:
788 wwin = scr->focused_window;
789 wWindowFocusPrev(wwin, True);
790 break;
791 case WA_MOVE_NEXTWINDOW:
792 wwin = scr->focused_window;
793 wWindowFocusNext(wwin, True);
794 break;
798 /* bindable */
799 static void handleButtonPress(XEvent * event)
801 WObjDescriptor *desc;
802 WScreen *scr;
804 scr = wScreenForRootWindow(event->xbutton.root);
806 #ifdef BALLOON_TEXT
807 wBalloonHide(scr);
808 #endif
810 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
811 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
812 executeButtonAction(scr, event, wPreferences.mouse_button1);
813 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
814 executeButtonAction(scr, event, wPreferences.mouse_button2);
815 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
816 executeButtonAction(scr, event, wPreferences.mouse_button3);
817 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
818 executeButtonAction(scr, event, wPreferences.mouse_button8);
819 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
820 executeButtonAction(scr, event, wPreferences.mouse_button9);
821 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
822 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
823 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
824 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
825 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
826 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
827 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
828 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
832 desc = NULL;
833 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
834 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
835 return;
839 if (desc->parent_type == WCLASS_WINDOW) {
840 XSync(dpy, 0);
842 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
843 XAllowEvents(dpy, AsyncPointer, CurrentTime);
844 } else {
845 /* if (wPreferences.focus_mode == WKF_CLICK) { */
846 if (wPreferences.ignore_focus_click) {
847 XAllowEvents(dpy, AsyncPointer, CurrentTime);
849 XAllowEvents(dpy, ReplayPointer, CurrentTime);
850 /* } */
852 XSync(dpy, 0);
853 } else if (desc->parent_type == WCLASS_APPICON
854 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
855 if (event->xbutton.state & MOD_MASK) {
856 XSync(dpy, 0);
857 XAllowEvents(dpy, AsyncPointer, CurrentTime);
858 XSync(dpy, 0);
862 if (desc->handle_mousedown != NULL) {
863 (*desc->handle_mousedown) (desc, event);
866 /* save double-click information */
867 if (scr->flags.next_click_is_not_double) {
868 scr->flags.next_click_is_not_double = 0;
869 } else {
870 scr->last_click_time = event->xbutton.time;
871 scr->last_click_button = event->xbutton.button;
872 scr->last_click_window = event->xbutton.window;
876 static void handleMapNotify(XEvent * event)
878 WWindow *wwin;
880 wwin = wWindowFor(event->xmap.event);
881 if (wwin && wwin->client_win == event->xmap.event) {
882 if (wwin->flags.miniaturized) {
883 wDeiconifyWindow(wwin);
884 } else {
885 XGrabServer(dpy);
886 wWindowMap(wwin);
887 wClientSetState(wwin, NormalState, None);
888 XUngrabServer(dpy);
893 static void handleUnmapNotify(XEvent * event)
895 WWindow *wwin;
896 XEvent ev;
897 Bool withdraw = False;
899 /* only process windows with StructureNotify selected
900 * (ignore SubstructureNotify) */
901 wwin = wWindowFor(event->xunmap.window);
902 if (!wwin)
903 return;
905 /* whether the event is a Withdrawal request */
906 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
907 withdraw = True;
909 if (wwin->client_win != event->xunmap.event && !withdraw)
910 return;
912 if (!wwin->flags.mapped && !withdraw
913 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
914 && !wwin->flags.miniaturized && !wwin->flags.hidden)
915 return;
917 XGrabServer(dpy);
918 XUnmapWindow(dpy, wwin->frame->core->window);
919 wwin->flags.mapped = 0;
920 XSync(dpy, 0);
921 /* check if the window was destroyed */
922 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
923 DispatchEvent(&ev);
924 } else {
925 Bool reparented = False;
927 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
928 reparented = True;
930 /* withdraw window */
931 wwin->flags.mapped = 0;
932 if (!reparented)
933 wClientSetState(wwin, WithdrawnState, None);
935 /* if the window was reparented, do not reparent it back to the
936 * root window */
937 wUnmanageWindow(wwin, !reparented, False);
939 XUngrabServer(dpy);
942 static void handleConfigureRequest(XEvent * event)
944 WWindow *wwin;
946 wwin = wWindowFor(event->xconfigurerequest.window);
947 if (wwin == NULL) {
949 * Configure request for unmapped window
951 wClientConfigure(NULL, &(event->xconfigurerequest));
952 } else {
953 wClientConfigure(wwin, &(event->xconfigurerequest));
957 static void handlePropertyNotify(XEvent * event)
959 WWindow *wwin;
960 WApplication *wapp;
961 Window jr;
962 int ji;
963 unsigned int ju;
965 wwin = wWindowFor(event->xproperty.window);
966 if (wwin) {
967 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
968 return;
970 wClientCheckProperty(wwin, &event->xproperty);
972 wapp = wApplicationOf(event->xproperty.window);
973 if (wapp) {
974 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
978 static void handleClientMessage(XEvent * event)
980 WWindow *wwin;
981 WObjDescriptor *desc;
983 /* handle transition from Normal to Iconic state */
984 if (event->xclient.message_type == w_global.atom.wm.change_state
985 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
987 wwin = wWindowFor(event->xclient.window);
988 if (!wwin)
989 return;
990 if (!wwin->flags.miniaturized)
991 wIconifyWindow(wwin);
992 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
993 WScreen *scr = wScreenForRootWindow(event->xclient.window);
995 if (!scr)
996 return;
998 if (event->xclient.data.l[1] == 1) { /* starting */
999 wColormapAllowClientInstallation(scr, True);
1000 } else { /* stopping */
1001 wColormapAllowClientInstallation(scr, False);
1003 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
1005 char *command;
1006 size_t len;
1008 len = sizeof(event->xclient.data.b) + 1;
1009 command = wmalloc(len);
1010 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
1012 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1013 wwarning(_("Got Reconfigure command"));
1014 wDefaultsCheckDomains(NULL);
1015 } else {
1016 wwarning(_("Got unknown command %s"), command);
1019 wfree(command);
1021 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1022 WApplication *wapp;
1023 int done = 0;
1024 wapp = wApplicationOf(event->xclient.window);
1025 if (wapp) {
1026 switch (event->xclient.data.l[0]) {
1027 case WMFHideOtherApplications:
1028 wHideOtherApplications(wapp->main_window_desc);
1029 done = 1;
1030 break;
1032 case WMFHideApplication:
1033 wHideApplication(wapp);
1034 done = 1;
1035 break;
1038 if (!done) {
1039 wwin = wWindowFor(event->xclient.window);
1040 if (wwin) {
1041 switch (event->xclient.data.l[0]) {
1042 case WMFHideOtherApplications:
1043 wHideOtherApplications(wwin);
1044 break;
1046 case WMFHideApplication:
1047 wHideApplication(wApplicationOf(wwin->main_window));
1048 break;
1052 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1053 wwin = wWindowFor(event->xclient.window);
1054 if (!wwin)
1055 return;
1056 switch (event->xclient.data.l[0]) {
1057 case GSWindowLevelAttr:
1059 int level = (int)event->xclient.data.l[1];
1061 if (WINDOW_LEVEL(wwin) != level) {
1062 ChangeStackingLevel(wwin->frame->core, level);
1065 break;
1067 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1068 wwin = wWindowFor(event->xclient.window);
1069 if (!wwin)
1070 return;
1071 switch (event->xclient.data.l[0]) {
1072 case WMTitleBarNormal:
1073 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1074 break;
1075 case WMTitleBarMain:
1076 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1077 break;
1078 case WMTitleBarKey:
1079 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1080 break;
1082 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1083 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1084 if (!scr)
1085 return;
1086 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1087 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1088 /* do nothing */
1089 #ifdef USE_DOCK_XDND
1090 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1091 /* do nothing */
1092 #endif /* USE_DOCK_XDND */
1093 } else {
1095 * Non-standard thing, but needed by OffiX DND.
1096 * For when the icon frame gets a ClientMessage
1097 * that should have gone to the icon_window.
1099 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1100 struct WIcon *icon = NULL;
1102 if (desc->parent_type == WCLASS_MINIWINDOW) {
1103 icon = (WIcon *) desc->parent;
1104 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1105 icon = ((WAppIcon *) desc->parent)->icon;
1107 if (icon && (wwin = icon->owner)) {
1108 if (wwin->client_win != event->xclient.window) {
1109 event->xclient.window = wwin->client_win;
1110 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1117 static void raiseWindow(WScreen * scr)
1119 WWindow *wwin;
1121 scr->autoRaiseTimer = NULL;
1123 wwin = wWindowFor(scr->autoRaiseWindow);
1124 if (!wwin)
1125 return;
1127 if (!wwin->flags.destroyed && wwin->flags.focused) {
1128 wRaiseFrame(wwin->frame->core);
1129 /* this is needed or a race condition will occur */
1130 XSync(dpy, False);
1134 static void handleEnterNotify(XEvent * event)
1136 WWindow *wwin;
1137 WObjDescriptor *desc = NULL;
1138 XEvent ev;
1139 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1141 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1142 /* already left the window... */
1143 saveTimestamp(&ev);
1144 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1145 return;
1149 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1150 if (desc->handle_enternotify)
1151 (*desc->handle_enternotify) (desc, event);
1154 /* enter to window */
1155 wwin = wWindowFor(event->xcrossing.window);
1156 if (!wwin) {
1157 if (wPreferences.colormap_mode == WCM_POINTER) {
1158 wColormapInstallForWindow(scr, NULL);
1160 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1161 WMDeleteTimerHandler(scr->autoRaiseTimer);
1162 scr->autoRaiseTimer = NULL;
1164 } else {
1165 /* set auto raise timer even if in focus-follows-mouse mode
1166 * and the event is for the frame window, even if the window
1167 * has focus already. useful if you move the pointer from a focused
1168 * window to the root window and back pretty fast
1170 * set focus if in focus-follows-mouse mode and the event
1171 * is for the frame window and window doesn't have focus yet */
1172 if (wPreferences.focus_mode == WKF_SLOPPY
1173 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1175 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1176 wSetFocusTo(scr, wwin);
1178 if (scr->autoRaiseTimer)
1179 WMDeleteTimerHandler(scr->autoRaiseTimer);
1180 scr->autoRaiseTimer = NULL;
1182 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1183 scr->autoRaiseWindow = wwin->frame->core->window;
1184 scr->autoRaiseTimer
1185 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1188 /* Install colormap for window, if the colormap installation mode
1189 * is colormap_follows_mouse */
1190 if (wPreferences.colormap_mode == WCM_POINTER) {
1191 if (wwin->client_win == event->xcrossing.window)
1192 wColormapInstallForWindow(scr, wwin);
1193 else
1194 wColormapInstallForWindow(scr, NULL);
1198 if (event->xcrossing.window == event->xcrossing.root
1199 && event->xcrossing.detail == NotifyNormal
1200 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1202 wSetFocusTo(scr, scr->focused_window);
1204 #ifdef BALLOON_TEXT
1205 wBalloonEnteredObject(scr, desc);
1206 #endif
1209 static void handleLeaveNotify(XEvent * event)
1211 WObjDescriptor *desc = NULL;
1213 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1214 if (desc->handle_leavenotify)
1215 (*desc->handle_leavenotify) (desc, event);
1219 #ifdef USE_XSHAPE
1220 static void handleShapeNotify(XEvent * event)
1222 XShapeEvent *shev = (XShapeEvent *) event;
1223 WWindow *wwin;
1224 union {
1225 XEvent xevent;
1226 XShapeEvent xshape;
1227 } ev;
1229 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1230 if (ev.xshape.kind == ShapeBounding) {
1231 if (ev.xshape.shaped == shev->shaped) {
1232 *shev = ev.xshape;
1233 } else {
1234 XPutBackEvent(dpy, &ev.xevent);
1235 break;
1240 wwin = wWindowFor(shev->window);
1241 if (!wwin || shev->kind != ShapeBounding)
1242 return;
1244 if (!shev->shaped && wwin->flags.shaped) {
1246 wwin->flags.shaped = 0;
1247 wWindowClearShape(wwin);
1249 } else if (shev->shaped) {
1251 wwin->flags.shaped = 1;
1252 wWindowSetShape(wwin);
1255 #endif /* USE_XSHAPE */
1257 #ifdef KEEP_XKB_LOCK_STATUS
1258 /* please help ]d if you know what to do */
1259 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1261 WWindow *wwin;
1262 WScreen *scr;
1263 XkbStateRec staterec;
1264 int i;
1266 for (i = 0; i < w_global.screen_count; i++) {
1267 scr = wScreenWithNumber(i);
1268 wwin = scr->focused_window;
1269 if (wwin && wwin->flags.focused) {
1270 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1271 if (wwin->frame->languagemode != staterec.group) {
1272 wwin->frame->last_languagemode = wwin->frame->languagemode;
1273 wwin->frame->languagemode = staterec.group;
1275 #ifdef XKB_BUTTON_HINT
1276 if (wwin->frame->titlebar) {
1277 wFrameWindowPaint(wwin->frame);
1279 #endif
1283 #endif /*KEEP_XKB_LOCK_STATUS */
1285 static void handleColormapNotify(XEvent * event)
1287 WWindow *wwin;
1288 WScreen *scr;
1289 Bool reinstall = False;
1291 wwin = wWindowFor(event->xcolormap.window);
1292 if (!wwin)
1293 return;
1295 scr = wwin->screen_ptr;
1297 do {
1298 if (wwin) {
1299 if (event->xcolormap.new) {
1300 XWindowAttributes attr;
1302 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1304 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1305 scr->current_colormap = attr.colormap;
1307 reinstall = True;
1308 } else if (event->xcolormap.state == ColormapUninstalled &&
1309 scr->current_colormap == event->xcolormap.colormap) {
1311 /* some bastard app (like XV) removed our colormap */
1313 * can't enforce or things like xscreensaver won't work
1314 * reinstall = True;
1316 } else if (event->xcolormap.state == ColormapInstalled &&
1317 scr->current_colormap == event->xcolormap.colormap) {
1319 /* someone has put our colormap back */
1320 reinstall = False;
1323 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1324 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1326 if (reinstall && scr->current_colormap != None) {
1327 if (!scr->flags.colormap_stuff_blocked)
1328 XInstallColormap(dpy, scr->current_colormap);
1332 static void handleFocusIn(XEvent * event)
1334 WWindow *wwin;
1337 * For applications that like stealing the focus.
1339 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1340 saveTimestamp(event);
1341 if (event->xfocus.mode == NotifyUngrab
1342 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1343 return;
1346 wwin = wWindowFor(event->xfocus.window);
1347 if (wwin && !wwin->flags.focused) {
1348 if (wwin->flags.mapped)
1349 wSetFocusTo(wwin->screen_ptr, wwin);
1350 else
1351 wSetFocusTo(wwin->screen_ptr, NULL);
1352 } else if (!wwin) {
1353 WScreen *scr = wScreenForWindow(event->xfocus.window);
1354 if (scr)
1355 wSetFocusTo(scr, NULL);
1359 static WWindow *windowUnderPointer(WScreen * scr)
1361 unsigned int mask;
1362 int foo;
1363 Window bar, win;
1365 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1366 return wWindowFor(win);
1367 return NULL;
1370 static int CheckFullScreenWindowFocused(WScreen * scr)
1372 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1373 return 1;
1374 else
1375 return 0;
1378 static void handleKeyPress(XEvent * event)
1380 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1381 WWindow *wwin = scr->focused_window;
1382 short i, widx;
1383 int modifiers;
1384 int command = -1;
1385 #ifdef KEEP_XKB_LOCK_STATUS
1386 XkbStateRec staterec;
1387 #endif /*KEEP_XKB_LOCK_STATUS */
1389 /* ignore CapsLock */
1390 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1392 for (i = 0; i < WKBD_LAST; i++) {
1393 if (wKeyBindings[i].keycode == 0)
1394 continue;
1396 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1397 || */ wKeyBindings[i].modifier ==
1398 modifiers)) {
1399 command = i;
1400 break;
1404 if (command < 0) {
1406 if (!wRootMenuPerformShortcut(event)) {
1407 static int dontLoop = 0;
1409 if (dontLoop > 10) {
1410 wwarning("problem with key event processing code");
1411 return;
1413 dontLoop++;
1414 /* if the focused window is an internal window, try redispatching
1415 * the event to the managed window, as it can be a WINGs window */
1416 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1417 /* client_leader contains the WINGs toplevel */
1418 event->xany.window = wwin->client_leader;
1419 WMHandleEvent(event);
1421 dontLoop--;
1423 return;
1425 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1426 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1428 switch (command) {
1430 case WKBD_ROOTMENU:
1431 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1432 if (!CheckFullScreenWindowFocused(scr)) {
1433 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1434 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1435 True);
1437 break;
1438 case WKBD_WINDOWLIST:
1439 if (!CheckFullScreenWindowFocused(scr)) {
1440 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1441 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1442 True);
1444 break;
1446 case WKBD_WINDOWMENU:
1447 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1448 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1449 break;
1450 case WKBD_MINIMIZEALL:
1451 CloseWindowMenu(scr);
1452 wHideAll(scr);
1453 break;
1454 case WKBD_MINIATURIZE:
1455 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1456 && !WFLAGP(wwin, no_miniaturizable)) {
1457 CloseWindowMenu(scr);
1459 if (wwin->protocols.MINIATURIZE_WINDOW)
1460 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1461 else {
1462 wIconifyWindow(wwin);
1465 break;
1466 case WKBD_HIDE:
1467 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1468 WApplication *wapp = wApplicationOf(wwin->main_window);
1469 CloseWindowMenu(scr);
1471 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1472 wHideApplication(wapp);
1475 break;
1476 case WKBD_HIDE_OTHERS:
1477 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1478 CloseWindowMenu(scr);
1480 wHideOtherApplications(wwin);
1482 break;
1483 case WKBD_MAXIMIZE:
1484 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1485 CloseWindowMenu(scr);
1487 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1489 break;
1490 case WKBD_VMAXIMIZE:
1491 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1492 CloseWindowMenu(scr);
1494 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1496 break;
1497 case WKBD_HMAXIMIZE:
1498 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1499 CloseWindowMenu(scr);
1501 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1503 break;
1504 case WKBD_LHMAXIMIZE:
1505 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1506 CloseWindowMenu(scr);
1508 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1510 break;
1511 case WKBD_RHMAXIMIZE:
1512 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1513 CloseWindowMenu(scr);
1515 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1517 break;
1518 case WKBD_THMAXIMIZE:
1519 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1520 CloseWindowMenu(scr);
1522 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1524 break;
1525 case WKBD_BHMAXIMIZE:
1526 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1527 CloseWindowMenu(scr);
1529 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1531 break;
1532 case WKBD_LTCMAXIMIZE:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1534 CloseWindowMenu(scr);
1536 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1538 break;
1539 case WKBD_RTCMAXIMIZE:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1541 CloseWindowMenu(scr);
1543 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1545 break;
1546 case WKBD_LBCMAXIMIZE:
1547 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1548 CloseWindowMenu(scr);
1550 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1552 break;
1553 case WKBD_RBCMAXIMIZE:
1554 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1555 CloseWindowMenu(scr);
1557 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1559 break;
1560 case WKBD_MAXIMUS:
1561 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1562 CloseWindowMenu(scr);
1564 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1566 break;
1567 case WKBD_OMNIPRESENT:
1568 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1569 CloseWindowMenu(scr);
1571 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1573 break;
1574 case WKBD_RAISE:
1575 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1576 CloseWindowMenu(scr);
1578 wRaiseFrame(wwin->frame->core);
1580 break;
1581 case WKBD_LOWER:
1582 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1583 CloseWindowMenu(scr);
1585 wLowerFrame(wwin->frame->core);
1587 break;
1588 case WKBD_RAISELOWER:
1589 /* raise or lower the window under the pointer, not the
1590 * focused one
1592 wwin = windowUnderPointer(scr);
1593 if (wwin)
1594 wRaiseLowerFrame(wwin->frame->core);
1595 break;
1596 case WKBD_SHADE:
1597 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1598 if (wwin->flags.shaded)
1599 wUnshadeWindow(wwin);
1600 else
1601 wShadeWindow(wwin);
1603 break;
1604 case WKBD_MOVERESIZE:
1605 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1606 CloseWindowMenu(scr);
1608 wKeyboardMoveResizeWindow(wwin);
1610 break;
1611 case WKBD_CLOSE:
1612 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1613 CloseWindowMenu(scr);
1614 if (wwin->protocols.DELETE_WINDOW)
1615 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1617 break;
1618 case WKBD_SELECT:
1619 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1620 wSelectWindow(wwin, !wwin->flags.selected);
1622 break;
1624 case WKBD_WORKSPACEMAP:
1625 if (wPreferences.enable_workspace_pager)
1626 StartWorkspaceMap(scr);
1627 break;
1629 case WKBD_FOCUSNEXT:
1630 StartWindozeCycle(wwin, event, True, False);
1631 break;
1633 case WKBD_FOCUSPREV:
1634 StartWindozeCycle(wwin, event, False, False);
1635 break;
1637 case WKBD_GROUPNEXT:
1638 StartWindozeCycle(wwin, event, True, True);
1639 break;
1641 case WKBD_GROUPPREV:
1642 StartWindozeCycle(wwin, event, False, True);
1643 break;
1645 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1646 widx = command - WKBD_WORKSPACE1;
1647 i = (scr->current_workspace / 10) * 10 + widx;
1648 if (wPreferences.ws_advance || i < scr->workspace_count)
1649 wWorkspaceChange(scr, i);
1650 break;
1652 case WKBD_NEXTWORKSPACE:
1653 wWorkspaceRelativeChange(scr, 1);
1654 break;
1655 case WKBD_PREVWORKSPACE:
1656 wWorkspaceRelativeChange(scr, -1);
1657 break;
1658 case WKBD_LASTWORKSPACE:
1659 wWorkspaceChange(scr, scr->last_workspace);
1660 break;
1662 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1663 widx = command - WKBD_MOVE_WORKSPACE1;
1664 i = (scr->current_workspace / 10) * 10 + widx;
1665 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1666 wWindowChangeWorkspace(wwin, i);
1667 break;
1669 case WKBD_MOVE_NEXTWORKSPACE:
1670 if (wwin)
1671 wWindowChangeWorkspaceRelative(wwin, 1);
1672 break;
1673 case WKBD_MOVE_PREVWORKSPACE:
1674 if (wwin)
1675 wWindowChangeWorkspaceRelative(wwin, -1);
1676 break;
1677 case WKBD_MOVE_LASTWORKSPACE:
1678 if (wwin)
1679 wWindowChangeWorkspace(wwin, scr->last_workspace);
1680 break;
1682 case WKBD_MOVE_NEXTWSLAYER:
1683 case WKBD_MOVE_PREVWSLAYER:
1685 if (wwin) {
1686 int row, column;
1688 row = scr->current_workspace / 10;
1689 column = scr->current_workspace % 10;
1691 if (command == WKBD_MOVE_NEXTWSLAYER) {
1692 if ((row + 1) * 10 < scr->workspace_count)
1693 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1694 } else {
1695 if (row > 0)
1696 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1700 break;
1702 case WKBD_WINDOW1:
1703 case WKBD_WINDOW2:
1704 case WKBD_WINDOW3:
1705 case WKBD_WINDOW4:
1706 case WKBD_WINDOW5:
1707 case WKBD_WINDOW6:
1708 case WKBD_WINDOW7:
1709 case WKBD_WINDOW8:
1710 case WKBD_WINDOW9:
1711 case WKBD_WINDOW10:
1713 widx = command - WKBD_WINDOW1;
1715 if (scr->shortcutWindows[widx]) {
1716 WMArray *list = scr->shortcutWindows[widx];
1717 int cw;
1718 int count = WMGetArrayItemCount(list);
1719 WWindow *twin;
1720 WMArrayIterator iter;
1721 WWindow *wwin;
1723 wUnselectWindows(scr);
1724 cw = scr->current_workspace;
1726 WM_ETARETI_ARRAY(list, wwin, iter) {
1727 if (count > 1)
1728 wWindowChangeWorkspace(wwin, cw);
1730 wMakeWindowVisible(wwin);
1732 if (count > 1)
1733 wSelectWindow(wwin, True);
1736 /* rotate the order of windows, to create a cycling effect */
1737 twin = WMGetFromArray(list, 0);
1738 WMDeleteFromArray(list, 0);
1739 WMAddToArray(list, twin);
1741 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1742 if (scr->shortcutWindows[widx]) {
1743 WMFreeArray(scr->shortcutWindows[widx]);
1744 scr->shortcutWindows[widx] = NULL;
1747 if (wwin->flags.selected && scr->selected_windows) {
1748 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1749 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1750 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1751 } else {
1752 scr->shortcutWindows[widx] = WMCreateArray(4);
1753 WMAddToArray(scr->shortcutWindows[widx], wwin);
1756 wSelectWindow(wwin, !wwin->flags.selected);
1757 XFlush(dpy);
1758 wusleep(3000);
1759 wSelectWindow(wwin, !wwin->flags.selected);
1760 XFlush(dpy);
1762 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1764 if (wwin->flags.selected && scr->selected_windows) {
1765 if (scr->shortcutWindows[widx]) {
1766 WMFreeArray(scr->shortcutWindows[widx]);
1768 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1772 break;
1774 case WKBD_RELAUNCH:
1775 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1776 (void) RelaunchWindow(wwin);
1778 break;
1780 case WKBD_SWITCH_SCREEN:
1781 if (w_global.screen_count > 1) {
1782 WScreen *scr2;
1783 int i;
1785 /* find index of this screen */
1786 for (i = 0; i < w_global.screen_count; i++) {
1787 if (wScreenWithNumber(i) == scr)
1788 break;
1790 i++;
1791 if (i >= w_global.screen_count) {
1792 i = 0;
1794 scr2 = wScreenWithNumber(i);
1796 if (scr2) {
1797 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1798 scr2->scr_width / 2, scr2->scr_height / 2);
1801 break;
1803 case WKBD_RUN:
1805 char *cmdline;
1807 cmdline = ExpandOptions(scr, _("exec %A(Run,Type command to run:)"));
1809 if (cmdline) {
1810 XGrabPointer(dpy, scr->root_win, True, 0,
1811 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1812 XSync(dpy, False);
1814 ExecuteShellCommand(scr, cmdline);
1815 wfree(cmdline);
1817 XUngrabPointer(dpy, CurrentTime);
1818 XSync(dpy, False);
1820 break;
1823 case WKBD_NEXTWSLAYER:
1824 case WKBD_PREVWSLAYER:
1826 int row, column;
1828 row = scr->current_workspace / 10;
1829 column = scr->current_workspace % 10;
1831 if (command == WKBD_NEXTWSLAYER) {
1832 if ((row + 1) * 10 < scr->workspace_count)
1833 wWorkspaceChange(scr, column + (row + 1) * 10);
1834 } else {
1835 if (row > 0)
1836 wWorkspaceChange(scr, column + (row - 1) * 10);
1839 break;
1840 case WKBD_CLIPRAISELOWER:
1841 if (!wPreferences.flags.noclip)
1842 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1843 break;
1844 case WKBD_DOCKRAISELOWER:
1845 if (!wPreferences.flags.nodock)
1846 wDockRaiseLower(scr->dock);
1847 break;
1848 #ifdef KEEP_XKB_LOCK_STATUS
1849 case WKBD_TOGGLE:
1850 if (wPreferences.modelock) {
1851 /*toggle */
1852 wwin = scr->focused_window;
1854 if (wwin && wwin->flags.mapped
1855 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1856 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1857 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1859 wwin->frame->languagemode = wwin->frame->last_languagemode;
1860 wwin->frame->last_languagemode = staterec.group;
1861 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1865 break;
1866 #endif /* KEEP_XKB_LOCK_STATUS */
1870 static void handleMotionNotify(XEvent * event)
1872 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1874 if (wPreferences.scrollable_menus) {
1875 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1876 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1878 if (scr->flags.jump_back_pending ||
1879 p.x <= (rect.pos.x + 1) ||
1880 p.x >= (rect.pos.x + rect.size.width - 2) ||
1881 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1882 WMenu *menu;
1884 menu = wMenuUnderPointer(scr);
1885 if (menu != NULL)
1886 wMenuScroll(menu);
1891 static void handleVisibilityNotify(XEvent * event)
1893 WWindow *wwin;
1895 wwin = wWindowFor(event->xvisibility.window);
1896 if (!wwin)
1897 return;
1898 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
1901 static void handle_selection_request(XSelectionRequestEvent *event)
1903 #ifdef USE_ICCCM_WMREPLACE
1904 static Atom atom_version = None;
1905 WScreen *scr;
1906 XSelectionEvent notify;
1909 * This event must be sent to the slection requester to not block him
1911 * We create it with the answer 'there is no selection' by default
1913 notify.type = SelectionNotify;
1914 notify.display = dpy;
1915 notify.requestor = event->requestor;
1916 notify.selection = event->selection;
1917 notify.target = event->target;
1918 notify.property = None; /* This says that there is no selection */
1919 notify.time = event->time;
1921 scr = wScreenForWindow(event->owner);
1922 if (!scr)
1923 goto not_our_selection;
1925 if (event->owner != scr->info_window)
1926 goto not_our_selection;
1928 if (event->selection != scr->sn_atom)
1929 goto not_our_selection;
1931 if (atom_version == None)
1932 atom_version = XInternAtom(dpy, "VERSION", False);
1934 if (event->target == atom_version) {
1935 static const long icccm_version[] = { 2, 0 };
1938 * This protocol is defined in ICCCM 2.0:
1939 * http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html
1940 * "Communication with the Window Manager by Means of Selections"
1944 * Setting the property means the content of the selection is available
1945 * According to the ICCCM spec, we need to support being asked for a property
1946 * set to 'None' for compatibility with old clients
1948 notify.property = (event->property == None)?(event->target):(event->property);
1950 XChangeProperty(dpy, event->requestor, notify.property,
1951 XA_INTEGER, 32, PropModeReplace,
1952 (unsigned char *) icccm_version, wlengthof(icccm_version));
1955 not_our_selection:
1956 if (notify.property == None)
1957 wwarning("received SelectionRequest(%s) for target=\"%s\" from requestor 0x%lX but we have no answer",
1958 XGetAtomName(dpy, event->selection), XGetAtomName(dpy, event->target), (long) event->requestor);
1960 /* Send the answer to the requestor */
1961 XSendEvent(dpy, event->requestor, False, 0L, (XEvent *) &notify);
1963 #else
1965 * If the support for ICCCM window manager replacement was not enabled, we should not receive
1966 * this kind of event, so we just ignore it (Conceptually, we should reply with 'SelectionNotify'
1967 * event with property set to 'None' to tell that we don't have this selection, but that is a bit
1968 * costly for an event that shall never happen).
1970 (void) event;
1971 #endif
1974 static void handle_selection_clear(XSelectionClearEvent *event)
1976 #ifdef USE_ICCCM_WMREPLACE
1977 WScreen *scr = wScreenForWindow(event->window);
1979 if (!scr)
1980 return;
1982 if (event->selection != scr->sn_atom)
1983 return;
1985 wmessage(_("another window manager is replacing us!"));
1986 Shutdown(WSExitMode);
1987 #else
1989 * If the support for ICCCM window manager replacement was not enabled, we should not receive
1990 * this kind of event, so we simply do nothing.
1992 (void) event;
1993 #endif