wmaker: Moved definition of GNUStep-related XAtoms into the global variables structure
[wmaker-crm.git] / src / event.c
blobb38d630574bd6b8c591b4b109ac2d322b4e52c1b
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #ifdef HAVE_INOTIFY
25 #include <sys/select.h>
26 #include <sys/inotify.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #ifdef SHAPE
39 # include <X11/extensions/shape.h>
40 #endif
41 #ifdef XDND
42 #include "xdnd.h"
43 #endif
45 #ifdef HAVE_XRANDR
46 #include <X11/extensions/Xrandr.h>
47 #endif
49 #ifdef KEEP_XKB_LOCK_STATUS
50 #include <X11/XKBlib.h>
51 #endif /* KEEP_XKB_LOCK_STATUS */
53 #include "WindowMaker.h"
54 #include "window.h"
55 #include "actions.h"
56 #include "client.h"
57 #include "main.h"
58 #include "cycling.h"
59 #include "keybind.h"
60 #include "application.h"
61 #include "stacking.h"
62 #include "defaults.h"
63 #include "workspace.h"
64 #include "dock.h"
65 #include "framewin.h"
66 #include "properties.h"
67 #include "balloon.h"
68 #include "xinerama.h"
69 #include "wmspec.h"
70 #include "rootmenu.h"
71 #include "colormap.h"
72 #include "screen.h"
73 #include "shutdown.h"
74 #include "misc.h"
75 #include "event.h"
76 #include "winmenu.h"
77 #include "switchmenu.h"
79 /******** Global Variables **********/
80 extern XContext wWinContext;
81 extern XContext wVEdgeContext;
83 extern WShortKey wKeyBindings[WKBD_LAST];
84 extern int wScreenCount;
86 #define MOD_MASK wPreferences.modifier_mask
88 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
89 extern Atom _XA_WINDOWMAKER_COMMAND;
91 #ifdef SHAPE
92 extern Bool wShapeSupported;
93 extern int wShapeEventBase;
94 #endif
96 #ifdef KEEP_XKB_LOCK_STATUS
97 extern int wXkbEventBase;
98 #endif
100 /************ Local stuff ***********/
102 static void saveTimestamp(XEvent *event);
103 static void handleColormapNotify(XEvent *event);
104 static void handleMapNotify(XEvent *event);
105 static void handleUnmapNotify(XEvent *event);
106 static void handleButtonPress(XEvent *event);
107 static void handleExpose(XEvent *event);
108 static void handleDestroyNotify(XEvent *event);
109 static void handleConfigureRequest(XEvent *event);
110 static void handleMapRequest(XEvent *event);
111 static void handlePropertyNotify(XEvent *event);
112 static void handleEnterNotify(XEvent *event);
113 static void handleLeaveNotify(XEvent *event);
114 static void handleExtensions(XEvent *event);
115 static void handleClientMessage(XEvent *event);
116 static void handleKeyPress(XEvent *event);
117 static void handleFocusIn(XEvent *event);
118 static void handleMotionNotify(XEvent *event);
119 static void handleVisibilityNotify(XEvent *event);
120 static void handle_inotify_events(int fd, int wd);
121 static void wdelete_death_handler(WMagicNumber id);
124 #ifdef SHAPE
125 static void handleShapeNotify(XEvent *event);
126 #endif
128 #ifdef KEEP_XKB_LOCK_STATUS
129 static void handleXkbIndicatorStateNotify(XEvent *event);
130 #endif
132 /* real dead process handler */
133 static void handleDeadProcess(void *foo);
135 typedef struct DeadProcesses {
136 pid_t pid;
137 unsigned char exit_status;
138 } DeadProcesses;
140 /* stack of dead processes */
141 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
142 static int deadProcessPtr = 0;
144 typedef struct DeathHandler {
145 WDeathHandler *callback;
146 pid_t pid;
147 void *client_data;
148 } DeathHandler;
150 static WMArray *deathHandlers = NULL;
152 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
154 DeathHandler *handler;
156 handler = malloc(sizeof(DeathHandler));
157 if (!handler)
158 return 0;
160 handler->pid = pid;
161 handler->callback = callback;
162 handler->client_data = cdata;
164 if (!deathHandlers)
165 deathHandlers = WMCreateArrayWithDestructor(8, free);
167 WMAddToArray(deathHandlers, handler);
169 return handler;
172 static void wdelete_death_handler(WMagicNumber id)
174 DeathHandler *handler = (DeathHandler *) id;
176 if (!handler || !deathHandlers)
177 return;
179 /* array destructor will call free(handler) */
180 WMRemoveFromArray(deathHandlers, handler);
183 void DispatchEvent(XEvent * event)
185 if (deathHandlers)
186 handleDeadProcess(NULL);
188 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
189 WCHANGE_STATE(WSTATE_EXITING);
190 /* received SIGTERM */
192 * WMHandleEvent() can't be called from anything
193 * executed inside here, or we can get in a infinite
194 * recursive loop.
196 Shutdown(WSExitMode);
198 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
199 WCHANGE_STATE(WSTATE_RESTARTING);
201 Shutdown(WSRestartPreparationMode);
202 /* received SIGHUP */
203 Restart(NULL, True);
204 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
205 WCHANGE_STATE(WSTATE_NORMAL);
206 wDefaultsCheckDomains(NULL);
209 /* for the case that all that is wanted to be dispatched is
210 * the stuff above */
211 if (!event)
212 return;
214 saveTimestamp(event);
215 switch (event->type) {
216 case MapRequest:
217 handleMapRequest(event);
218 break;
220 case KeyPress:
221 handleKeyPress(event);
222 break;
224 case MotionNotify:
225 handleMotionNotify(event);
226 break;
228 case ConfigureRequest:
229 handleConfigureRequest(event);
230 break;
232 case DestroyNotify:
233 handleDestroyNotify(event);
234 break;
236 case MapNotify:
237 handleMapNotify(event);
238 break;
240 case UnmapNotify:
241 handleUnmapNotify(event);
242 break;
244 case ButtonPress:
245 handleButtonPress(event);
246 break;
248 case Expose:
249 handleExpose(event);
250 break;
252 case PropertyNotify:
253 handlePropertyNotify(event);
254 break;
256 case EnterNotify:
257 handleEnterNotify(event);
258 break;
260 case LeaveNotify:
261 handleLeaveNotify(event);
262 break;
264 case ClientMessage:
265 handleClientMessage(event);
266 break;
268 case ColormapNotify:
269 handleColormapNotify(event);
270 break;
272 case MappingNotify:
273 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
274 XRefreshKeyboardMapping(&event->xmapping);
275 break;
277 case FocusIn:
278 handleFocusIn(event);
279 break;
281 case VisibilityNotify:
282 handleVisibilityNotify(event);
283 break;
285 case ConfigureNotify:
286 #ifdef HAVE_XRANDR
287 if (event->xconfigure.window == DefaultRootWindow(dpy))
288 XRRUpdateConfiguration(event);
289 #endif
290 break;
292 default:
293 handleExtensions(event);
294 break;
298 #ifdef HAVE_INOTIFY
300 *----------------------------------------------------------------------
301 * handle_inotify_events-
302 * Check for inotify events
304 * Returns:
305 * After reading events for the given file descriptor (fd) and
306 * watch descriptor (wd)
308 * Side effects:
309 * Calls wDefaultsCheckDomains if config database is updated
310 *----------------------------------------------------------------------
312 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
313 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
314 static void handle_inotify_events(int fd, int wd)
316 ssize_t eventQLength, i = 0;
317 char buff[BUFF_SIZE] = { 0 };
318 /* Check config only once per read of the event queue */
319 int oneShotFlag = 0;
322 * Read off the queued events
323 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
324 * not occur; the block is on Xevents, but a config file change will normally
325 * occur as a result of an Xevent - so the event queue should never have more than
326 * a few entries before a read().
328 eventQLength = read(fd, buff, BUFF_SIZE);
330 /* check what events occured */
331 /* Should really check wd here too, but for now we only have one watch! */
332 while (i < eventQLength) {
333 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
336 * see inotify.h for event types.
338 if (pevent->mask & IN_DELETE_SELF) {
339 wwarning(_("the defaults database has been deleted!"
340 " Restart Window Maker to create the database" " with the default settings"));
341 close(fd);
343 if (pevent->mask & IN_UNMOUNT) {
344 wwarning(_("the unit containing the defaults database has"
345 " been unmounted. Setting --static mode." " Any changes will not be saved."));
346 close(fd);
347 wPreferences.flags.noupdates = 1;
349 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
350 wwarning(_("Inotify: Reading config files in defaults database."));
351 wDefaultsCheckDomains(NULL);
354 /* move to next event in the buffer */
355 i += sizeof(struct inotify_event) + pevent->len;
358 #endif /* HAVE_INOTIFY */
361 *----------------------------------------------------------------------
362 * EventLoop-
363 * Processes X and internal events indefinitely.
365 * Returns:
366 * Never returns
368 * Side effects:
369 * The LastTimestamp global variable is updated.
370 * Calls inotifyGetEvents if defaults database changes.
371 *----------------------------------------------------------------------
373 noreturn void EventLoop(void)
375 XEvent event;
376 #ifdef HAVE_INOTIFY
377 extern int inotifyFD;
378 extern int inotifyWD;
379 struct timeval time;
380 fd_set rfds;
381 int retVal = 0;
383 if (inotifyFD < 0 || inotifyWD < 0)
384 retVal = -1;
385 #endif
387 for (;;) {
389 WMNextEvent(dpy, &event); /* Blocks here */
390 WMHandleEvent(&event);
391 #ifdef HAVE_INOTIFY
392 if (retVal != -1) {
393 time.tv_sec = 0;
394 time.tv_usec = 0;
395 FD_ZERO(&rfds);
396 FD_SET(inotifyFD, &rfds);
398 /* check for available read data from inotify - don't block! */
399 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
401 if (retVal < 0) { /* an error has occured */
402 wwarning(_("select failed. The inotify instance will be closed."
403 " Changes to the defaults database will require"
404 " a restart to take effect."));
405 close(inotifyFD);
406 continue;
408 if (FD_ISSET(inotifyFD, &rfds))
409 handle_inotify_events(inotifyFD, inotifyWD);
411 #endif
416 *----------------------------------------------------------------------
417 * ProcessPendingEvents --
418 * Processes the events that are currently pending (at the time
419 * this function is called) in the display's queue.
421 * Returns:
422 * After the pending events that were present at the function call
423 * are processed.
425 * Side effects:
426 * Many -- whatever handling events may involve.
428 *----------------------------------------------------------------------
430 void ProcessPendingEvents(void)
432 XEvent event;
433 int count;
435 XSync(dpy, False);
437 /* Take a snapshot of the event count in the queue */
438 count = XPending(dpy);
440 while (count > 0 && XPending(dpy)) {
441 WMNextEvent(dpy, &event);
442 WMHandleEvent(&event);
443 count--;
447 Bool IsDoubleClick(WScreen * scr, XEvent * event)
449 if ((scr->last_click_time > 0) &&
450 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
451 && (event->xbutton.button == scr->last_click_button)
452 && (event->xbutton.window == scr->last_click_window)) {
454 scr->flags.next_click_is_not_double = 1;
455 scr->last_click_time = 0;
456 scr->last_click_window = event->xbutton.window;
458 return True;
460 return False;
463 void NotifyDeadProcess(pid_t pid, unsigned char status)
465 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
466 wwarning("stack overflow: too many dead processes");
467 return;
469 /* stack the process to be handled later,
470 * as this is called from the signal handler */
471 deadProcesses[deadProcessPtr].pid = pid;
472 deadProcesses[deadProcessPtr].exit_status = status;
473 deadProcessPtr++;
476 static void handleDeadProcess(void *foo)
478 DeathHandler *tmp;
479 int i;
481 for (i = 0; i < deadProcessPtr; i++) {
482 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
485 if (!deathHandlers) {
486 deadProcessPtr = 0;
487 return;
490 /* get the pids on the queue and call handlers */
491 while (deadProcessPtr > 0) {
492 deadProcessPtr--;
494 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
495 tmp = WMGetFromArray(deathHandlers, i);
496 if (!tmp)
497 continue;
499 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
500 (*tmp->callback) (tmp->pid,
501 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
502 wdelete_death_handler(tmp);
508 static void saveTimestamp(XEvent * event)
511 * Never save CurrentTime as LastTimestamp because CurrentTime
512 * it's not a real timestamp (it's the 0L constant)
515 switch (event->type) {
516 case ButtonRelease:
517 case ButtonPress:
518 w_global.timestamp.last_event = event->xbutton.time;
519 break;
520 case KeyPress:
521 case KeyRelease:
522 w_global.timestamp.last_event = event->xkey.time;
523 break;
524 case MotionNotify:
525 w_global.timestamp.last_event = event->xmotion.time;
526 break;
527 case PropertyNotify:
528 w_global.timestamp.last_event = event->xproperty.time;
529 break;
530 case EnterNotify:
531 case LeaveNotify:
532 w_global.timestamp.last_event = event->xcrossing.time;
533 break;
534 case SelectionClear:
535 w_global.timestamp.last_event = event->xselectionclear.time;
536 break;
537 case SelectionRequest:
538 w_global.timestamp.last_event = event->xselectionrequest.time;
539 break;
540 case SelectionNotify:
541 w_global.timestamp.last_event = event->xselection.time;
542 #ifdef XDND
543 wXDNDProcessSelection(event);
544 #endif
545 break;
549 static int matchWindow(const void *item, const void *cdata)
551 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
554 static void handleExtensions(XEvent * event)
556 #ifdef KEEP_XKB_LOCK_STATUS
557 XkbEvent *xkbevent;
558 xkbevent = (XkbEvent *) event;
559 #endif /*KEEP_XKB_LOCK_STATUS */
560 #ifdef SHAPE
561 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
562 handleShapeNotify(event);
564 #endif
565 #ifdef KEEP_XKB_LOCK_STATUS
566 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
567 handleXkbIndicatorStateNotify(event);
569 #endif /*KEEP_XKB_LOCK_STATUS */
570 #ifdef HAVE_XRANDR
571 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
572 /* From xrandr man page: "Clients must call back into Xlib using
573 * XRRUpdateConfiguration when screen configuration change notify
574 * events are generated */
575 XRRUpdateConfiguration(event);
576 WCHANGE_STATE(WSTATE_RESTARTING);
577 Shutdown(WSRestartPreparationMode);
578 Restart(NULL,True);
580 #endif
583 static void handleMapRequest(XEvent * ev)
585 WWindow *wwin;
586 WScreen *scr = NULL;
587 Window window = ev->xmaprequest.window;
589 if ((wwin = wWindowFor(window))) {
590 if (wwin->flags.shaded) {
591 wUnshadeWindow(wwin);
593 /* deiconify window */
594 if (wwin->flags.miniaturized) {
595 wDeiconifyWindow(wwin);
596 } else if (wwin->flags.hidden) {
597 WApplication *wapp = wApplicationOf(wwin->main_window);
598 /* go to the last workspace that the user worked on the app */
599 if (wapp) {
600 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
602 wUnhideApplication(wapp, False, False);
604 return;
607 scr = wScreenForRootWindow(ev->xmaprequest.parent);
609 wwin = wManageWindow(scr, window);
612 * This is to let the Dock know that the application it launched
613 * has already been mapped (eg: it has finished launching).
614 * It is not necessary for normally docked apps, but is needed for
615 * apps that were forcedly docked (like with dockit).
617 if (scr->last_dock) {
618 if (wwin && wwin->main_window != None && wwin->main_window != window)
619 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
620 else
621 wDockTrackWindowLaunch(scr->last_dock, window);
624 if (wwin) {
625 wClientSetState(wwin, NormalState, None);
626 if (wwin->flags.maximized) {
627 wMaximizeWindow(wwin, wwin->flags.maximized);
629 if (wwin->flags.shaded) {
630 wwin->flags.shaded = 0;
631 wwin->flags.skip_next_animation = 1;
632 wShadeWindow(wwin);
634 if (wwin->flags.miniaturized) {
635 wwin->flags.miniaturized = 0;
636 wwin->flags.skip_next_animation = 1;
637 wIconifyWindow(wwin);
639 if (wwin->flags.fullscreen) {
640 wwin->flags.fullscreen = 0;
641 wFullscreenWindow(wwin);
643 if (wwin->flags.hidden) {
644 WApplication *wapp = wApplicationOf(wwin->main_window);
646 wwin->flags.hidden = 0;
647 wwin->flags.skip_next_animation = 1;
648 if (wapp) {
649 wHideApplication(wapp);
655 static void handleDestroyNotify(XEvent * event)
657 WWindow *wwin;
658 WApplication *app;
659 Window window = event->xdestroywindow.window;
660 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
661 int widx;
663 wwin = wWindowFor(window);
664 if (wwin) {
665 wUnmanageWindow(wwin, False, True);
668 if (scr != NULL) {
669 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
670 WFakeGroupLeader *fPtr;
672 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
673 if (fPtr->retainCount > 0) {
674 fPtr->retainCount--;
675 if (fPtr->retainCount == 0 && fPtr->leader != None) {
676 XDestroyWindow(dpy, fPtr->leader);
677 fPtr->leader = None;
678 XFlush(dpy);
681 fPtr->origLeader = None;
685 app = wApplicationOf(window);
686 if (app) {
687 if (window == app->main_window) {
688 app->refcount = 0;
689 wwin = app->main_window_desc->screen_ptr->focused_window;
690 while (wwin) {
691 if (wwin->main_window == window) {
692 wwin->main_window = None;
694 wwin = wwin->prev;
697 wApplicationDestroy(app);
701 static void handleExpose(XEvent * event)
703 WObjDescriptor *desc;
704 XEvent ev;
706 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
708 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
709 return;
712 if (desc->handle_expose) {
713 (*desc->handle_expose) (desc, event);
717 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
719 switch (action) {
720 case WA_SELECT_WINDOWS:
721 wUnselectWindows(scr);
722 wSelectWindows(scr, event);
723 break;
724 case WA_OPEN_APPMENU:
725 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
726 /* ugly hack */
727 if (scr->root_menu) {
728 if (scr->root_menu->brother->flags.mapped)
729 event->xbutton.window = scr->root_menu->brother->frame->core->window;
730 else
731 event->xbutton.window = scr->root_menu->frame->core->window;
733 break;
734 case WA_OPEN_WINLISTMENU:
735 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
736 if (scr->switch_menu) {
737 if (scr->switch_menu->brother->flags.mapped)
738 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
739 else
740 event->xbutton.window = scr->switch_menu->frame->core->window;
742 break;
743 default:
744 break;
748 /* bindable */
749 static void handleButtonPress(XEvent * event)
751 WObjDescriptor *desc;
752 WScreen *scr;
754 scr = wScreenForRootWindow(event->xbutton.root);
756 #ifdef BALLOON_TEXT
757 wBalloonHide(scr);
758 #endif
760 if (event->xbutton.window == scr->root_win) {
761 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
762 executeButtonAction(scr, event, wPreferences.mouse_button1);
763 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
764 executeButtonAction(scr, event, wPreferences.mouse_button2);
765 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
766 executeButtonAction(scr, event, wPreferences.mouse_button3);
767 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
768 wWorkspaceRelativeChange(scr, 1);
769 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
770 wWorkspaceRelativeChange(scr, -1);
774 desc = NULL;
775 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
776 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
777 return;
781 if (desc->parent_type == WCLASS_WINDOW) {
782 XSync(dpy, 0);
784 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
785 XAllowEvents(dpy, AsyncPointer, CurrentTime);
786 } else {
787 /* if (wPreferences.focus_mode == WKF_CLICK) { */
788 if (wPreferences.ignore_focus_click) {
789 XAllowEvents(dpy, AsyncPointer, CurrentTime);
791 XAllowEvents(dpy, ReplayPointer, CurrentTime);
792 /* } */
794 XSync(dpy, 0);
795 } else if (desc->parent_type == WCLASS_APPICON
796 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
797 if (event->xbutton.state & MOD_MASK) {
798 XSync(dpy, 0);
799 XAllowEvents(dpy, AsyncPointer, CurrentTime);
800 XSync(dpy, 0);
804 if (desc->handle_mousedown != NULL) {
805 (*desc->handle_mousedown) (desc, event);
808 /* save double-click information */
809 if (scr->flags.next_click_is_not_double) {
810 scr->flags.next_click_is_not_double = 0;
811 } else {
812 scr->last_click_time = event->xbutton.time;
813 scr->last_click_button = event->xbutton.button;
814 scr->last_click_window = event->xbutton.window;
818 static void handleMapNotify(XEvent * event)
820 WWindow *wwin;
822 wwin = wWindowFor(event->xmap.event);
823 if (wwin && wwin->client_win == event->xmap.event) {
824 if (wwin->flags.miniaturized) {
825 wDeiconifyWindow(wwin);
826 } else {
827 XGrabServer(dpy);
828 wWindowMap(wwin);
829 wClientSetState(wwin, NormalState, None);
830 XUngrabServer(dpy);
835 static void handleUnmapNotify(XEvent * event)
837 WWindow *wwin;
838 XEvent ev;
839 Bool withdraw = False;
841 /* only process windows with StructureNotify selected
842 * (ignore SubstructureNotify) */
843 wwin = wWindowFor(event->xunmap.window);
844 if (!wwin)
845 return;
847 /* whether the event is a Withdrawal request */
848 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
849 withdraw = True;
851 if (wwin->client_win != event->xunmap.event && !withdraw)
852 return;
854 if (!wwin->flags.mapped && !withdraw
855 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
856 && !wwin->flags.miniaturized && !wwin->flags.hidden)
857 return;
859 XGrabServer(dpy);
860 XUnmapWindow(dpy, wwin->frame->core->window);
861 wwin->flags.mapped = 0;
862 XSync(dpy, 0);
863 /* check if the window was destroyed */
864 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
865 DispatchEvent(&ev);
866 } else {
867 Bool reparented = False;
869 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
870 reparented = True;
872 /* withdraw window */
873 wwin->flags.mapped = 0;
874 if (!reparented)
875 wClientSetState(wwin, WithdrawnState, None);
877 /* if the window was reparented, do not reparent it back to the
878 * root window */
879 wUnmanageWindow(wwin, !reparented, False);
881 XUngrabServer(dpy);
884 static void handleConfigureRequest(XEvent * event)
886 WWindow *wwin;
888 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
890 * Configure request for unmapped window
892 wClientConfigure(NULL, &(event->xconfigurerequest));
893 } else {
894 wClientConfigure(wwin, &(event->xconfigurerequest));
898 static void handlePropertyNotify(XEvent * event)
900 WWindow *wwin;
901 WApplication *wapp;
902 Window jr;
903 int ji;
904 unsigned int ju;
906 wwin = wWindowFor(event->xproperty.window);
907 if (wwin) {
908 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
909 return;
911 wClientCheckProperty(wwin, &event->xproperty);
913 wapp = wApplicationOf(event->xproperty.window);
914 if (wapp) {
915 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
919 static void handleClientMessage(XEvent * event)
921 WWindow *wwin;
922 WObjDescriptor *desc;
924 /* handle transition from Normal to Iconic state */
925 if (event->xclient.message_type == w_global.atom.wm.change_state
926 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
928 wwin = wWindowFor(event->xclient.window);
929 if (!wwin)
930 return;
931 if (!wwin->flags.miniaturized)
932 wIconifyWindow(wwin);
933 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
934 WScreen *scr = wScreenForRootWindow(event->xclient.window);
936 if (!scr)
937 return;
939 if (event->xclient.data.l[1] == 1) { /* starting */
940 wColormapAllowClientInstallation(scr, True);
941 } else { /* stopping */
942 wColormapAllowClientInstallation(scr, False);
944 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
946 char *command;
947 size_t len;
949 len = sizeof(event->xclient.data.b) + 1;
950 command = wmalloc(len);
951 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
953 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
954 wwarning(_("Got Reconfigure command"));
955 wDefaultsCheckDomains(NULL);
956 } else {
957 wwarning(_("Got unknown command %s"), command);
960 wfree(command);
962 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
963 WApplication *wapp;
964 int done = 0;
965 wapp = wApplicationOf(event->xclient.window);
966 if (wapp) {
967 switch (event->xclient.data.l[0]) {
968 case WMFHideOtherApplications:
969 wHideOtherApplications(wapp->main_window_desc);
970 done = 1;
971 break;
973 case WMFHideApplication:
974 wHideApplication(wapp);
975 done = 1;
976 break;
979 if (!done) {
980 wwin = wWindowFor(event->xclient.window);
981 if (wwin) {
982 switch (event->xclient.data.l[0]) {
983 case WMFHideOtherApplications:
984 wHideOtherApplications(wwin);
985 break;
987 case WMFHideApplication:
988 wHideApplication(wApplicationOf(wwin->main_window));
989 break;
993 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
994 wwin = wWindowFor(event->xclient.window);
995 if (!wwin)
996 return;
997 switch (event->xclient.data.l[0]) {
998 case GSWindowLevelAttr:
1000 int level = (int)event->xclient.data.l[1];
1002 if (WINDOW_LEVEL(wwin) != level) {
1003 ChangeStackingLevel(wwin->frame->core, level);
1006 break;
1008 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1009 wwin = wWindowFor(event->xclient.window);
1010 if (!wwin)
1011 return;
1012 switch (event->xclient.data.l[0]) {
1013 case WMTitleBarNormal:
1014 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1015 break;
1016 case WMTitleBarMain:
1017 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1018 break;
1019 case WMTitleBarKey:
1020 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1021 break;
1023 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1024 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1025 if (!scr)
1026 return;
1027 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1028 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1029 /* do nothing */
1030 #ifdef XDND
1031 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1032 /* do nothing */
1033 #endif /* XDND */
1034 } else {
1036 * Non-standard thing, but needed by OffiX DND.
1037 * For when the icon frame gets a ClientMessage
1038 * that should have gone to the icon_window.
1040 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1041 struct WIcon *icon = NULL;
1043 if (desc->parent_type == WCLASS_MINIWINDOW) {
1044 icon = (WIcon *) desc->parent;
1045 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1046 icon = ((WAppIcon *) desc->parent)->icon;
1048 if (icon && (wwin = icon->owner)) {
1049 if (wwin->client_win != event->xclient.window) {
1050 event->xclient.window = wwin->client_win;
1051 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1058 static void raiseWindow(WScreen * scr)
1060 WWindow *wwin;
1062 scr->autoRaiseTimer = NULL;
1064 wwin = wWindowFor(scr->autoRaiseWindow);
1065 if (!wwin)
1066 return;
1068 if (!wwin->flags.destroyed && wwin->flags.focused) {
1069 wRaiseFrame(wwin->frame->core);
1070 /* this is needed or a race condition will occur */
1071 XSync(dpy, False);
1075 static void handleEnterNotify(XEvent * event)
1077 WWindow *wwin;
1078 WObjDescriptor *desc = NULL;
1079 XEvent ev;
1080 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1082 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1083 /* already left the window... */
1084 saveTimestamp(&ev);
1085 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1086 return;
1090 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1091 if (desc->handle_enternotify)
1092 (*desc->handle_enternotify) (desc, event);
1095 /* enter to window */
1096 wwin = wWindowFor(event->xcrossing.window);
1097 if (!wwin) {
1098 if (wPreferences.colormap_mode == WCM_POINTER) {
1099 wColormapInstallForWindow(scr, NULL);
1101 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1102 WMDeleteTimerHandler(scr->autoRaiseTimer);
1103 scr->autoRaiseTimer = NULL;
1105 } else {
1106 /* set auto raise timer even if in focus-follows-mouse mode
1107 * and the event is for the frame window, even if the window
1108 * has focus already. useful if you move the pointer from a focused
1109 * window to the root window and back pretty fast
1111 * set focus if in focus-follows-mouse mode and the event
1112 * is for the frame window and window doesn't have focus yet */
1113 if (wPreferences.focus_mode == WKF_SLOPPY
1114 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1116 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1117 wSetFocusTo(scr, wwin);
1119 if (scr->autoRaiseTimer)
1120 WMDeleteTimerHandler(scr->autoRaiseTimer);
1121 scr->autoRaiseTimer = NULL;
1123 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1124 scr->autoRaiseWindow = wwin->frame->core->window;
1125 scr->autoRaiseTimer
1126 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1129 /* Install colormap for window, if the colormap installation mode
1130 * is colormap_follows_mouse */
1131 if (wPreferences.colormap_mode == WCM_POINTER) {
1132 if (wwin->client_win == event->xcrossing.window)
1133 wColormapInstallForWindow(scr, wwin);
1134 else
1135 wColormapInstallForWindow(scr, NULL);
1139 if (event->xcrossing.window == event->xcrossing.root
1140 && event->xcrossing.detail == NotifyNormal
1141 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1143 wSetFocusTo(scr, scr->focused_window);
1145 #ifdef BALLOON_TEXT
1146 wBalloonEnteredObject(scr, desc);
1147 #endif
1150 static void handleLeaveNotify(XEvent * event)
1152 WObjDescriptor *desc = NULL;
1154 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1155 if (desc->handle_leavenotify)
1156 (*desc->handle_leavenotify) (desc, event);
1160 #ifdef SHAPE
1161 static void handleShapeNotify(XEvent * event)
1163 XShapeEvent *shev = (XShapeEvent *) event;
1164 WWindow *wwin;
1165 union {
1166 XEvent xevent;
1167 XShapeEvent xshape;
1168 } ev;
1170 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1171 if (ev.xshape.kind == ShapeBounding) {
1172 if (ev.xshape.shaped == shev->shaped) {
1173 *shev = ev.xshape;
1174 } else {
1175 XPutBackEvent(dpy, &ev.xevent);
1176 break;
1181 wwin = wWindowFor(shev->window);
1182 if (!wwin || shev->kind != ShapeBounding)
1183 return;
1185 if (!shev->shaped && wwin->flags.shaped) {
1187 wwin->flags.shaped = 0;
1188 wWindowClearShape(wwin);
1190 } else if (shev->shaped) {
1192 wwin->flags.shaped = 1;
1193 wWindowSetShape(wwin);
1196 #endif /* SHAPE */
1198 #ifdef KEEP_XKB_LOCK_STATUS
1199 /* please help ]d if you know what to do */
1200 static void handleXkbIndicatorStateNotify(XEvent *event)
1202 WWindow *wwin;
1203 WScreen *scr;
1204 XkbStateRec staterec;
1205 int i;
1207 for (i = 0; i < wScreenCount; i++) {
1208 scr = wScreenWithNumber(i);
1209 wwin = scr->focused_window;
1210 if (wwin && wwin->flags.focused) {
1211 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1212 if (wwin->frame->languagemode != staterec.group) {
1213 wwin->frame->last_languagemode = wwin->frame->languagemode;
1214 wwin->frame->languagemode = staterec.group;
1216 #ifdef XKB_BUTTON_HINT
1217 if (wwin->frame->titlebar) {
1218 wFrameWindowPaint(wwin->frame);
1220 #endif
1224 #endif /*KEEP_XKB_LOCK_STATUS */
1226 static void handleColormapNotify(XEvent * event)
1228 WWindow *wwin;
1229 WScreen *scr;
1230 Bool reinstall = False;
1232 wwin = wWindowFor(event->xcolormap.window);
1233 if (!wwin)
1234 return;
1236 scr = wwin->screen_ptr;
1238 do {
1239 if (wwin) {
1240 if (event->xcolormap.new) {
1241 XWindowAttributes attr;
1243 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1245 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1246 scr->current_colormap = attr.colormap;
1248 reinstall = True;
1249 } else if (event->xcolormap.state == ColormapUninstalled &&
1250 scr->current_colormap == event->xcolormap.colormap) {
1252 /* some bastard app (like XV) removed our colormap */
1254 * can't enforce or things like xscreensaver wont work
1255 * reinstall = True;
1257 } else if (event->xcolormap.state == ColormapInstalled &&
1258 scr->current_colormap == event->xcolormap.colormap) {
1260 /* someone has put our colormap back */
1261 reinstall = False;
1264 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1265 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1267 if (reinstall && scr->current_colormap != None) {
1268 if (!scr->flags.colormap_stuff_blocked)
1269 XInstallColormap(dpy, scr->current_colormap);
1273 static void handleFocusIn(XEvent * event)
1275 WWindow *wwin;
1278 * For applications that like stealing the focus.
1280 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1281 saveTimestamp(event);
1282 if (event->xfocus.mode == NotifyUngrab
1283 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1284 return;
1287 wwin = wWindowFor(event->xfocus.window);
1288 if (wwin && !wwin->flags.focused) {
1289 if (wwin->flags.mapped)
1290 wSetFocusTo(wwin->screen_ptr, wwin);
1291 else
1292 wSetFocusTo(wwin->screen_ptr, NULL);
1293 } else if (!wwin) {
1294 WScreen *scr = wScreenForWindow(event->xfocus.window);
1295 if (scr)
1296 wSetFocusTo(scr, NULL);
1300 static WWindow *windowUnderPointer(WScreen * scr)
1302 unsigned int mask;
1303 int foo;
1304 Window bar, win;
1306 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1307 return wWindowFor(win);
1308 return NULL;
1311 static int CheckFullScreenWindowFocused(WScreen * scr)
1313 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1314 return 1;
1315 else
1316 return 0;
1319 static void handleKeyPress(XEvent * event)
1321 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1322 WWindow *wwin = scr->focused_window;
1323 short i, widx;
1324 int modifiers;
1325 int command = -1;
1326 #ifdef KEEP_XKB_LOCK_STATUS
1327 XkbStateRec staterec;
1328 #endif /*KEEP_XKB_LOCK_STATUS */
1330 /* ignore CapsLock */
1331 modifiers = event->xkey.state & ValidModMask;
1333 for (i = 0; i < WKBD_LAST; i++) {
1334 if (wKeyBindings[i].keycode == 0)
1335 continue;
1337 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1338 || */ wKeyBindings[i].modifier ==
1339 modifiers)) {
1340 command = i;
1341 break;
1345 if (command < 0) {
1347 if (!wRootMenuPerformShortcut(event)) {
1348 static int dontLoop = 0;
1350 if (dontLoop > 10) {
1351 wwarning("problem with key event processing code");
1352 return;
1354 dontLoop++;
1355 /* if the focused window is an internal window, try redispatching
1356 * the event to the managed window, as it can be a WINGs window */
1357 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1358 /* client_leader contains the WINGs toplevel */
1359 event->xany.window = wwin->client_leader;
1360 WMHandleEvent(event);
1362 dontLoop--;
1364 return;
1366 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1367 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1369 switch (command) {
1371 case WKBD_ROOTMENU:
1372 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1373 if (!CheckFullScreenWindowFocused(scr)) {
1374 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1375 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1376 True);
1378 break;
1379 case WKBD_WINDOWLIST:
1380 if (!CheckFullScreenWindowFocused(scr)) {
1381 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1382 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1383 True);
1385 break;
1387 case WKBD_WINDOWMENU:
1388 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1389 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1390 break;
1391 case WKBD_MINIMIZEALL:
1392 CloseWindowMenu(scr);
1393 wHideAll(scr);
1394 break;
1395 case WKBD_MINIATURIZE:
1396 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1397 && !WFLAGP(wwin, no_miniaturizable)) {
1398 CloseWindowMenu(scr);
1400 if (wwin->protocols.MINIATURIZE_WINDOW)
1401 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1402 else {
1403 wIconifyWindow(wwin);
1406 break;
1407 case WKBD_HIDE:
1408 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1409 WApplication *wapp = wApplicationOf(wwin->main_window);
1410 CloseWindowMenu(scr);
1412 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1413 wHideApplication(wapp);
1416 break;
1417 case WKBD_HIDE_OTHERS:
1418 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1419 CloseWindowMenu(scr);
1421 wHideOtherApplications(wwin);
1423 break;
1424 case WKBD_MAXIMIZE:
1425 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1426 CloseWindowMenu(scr);
1428 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1430 break;
1431 case WKBD_VMAXIMIZE:
1432 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1433 CloseWindowMenu(scr);
1435 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1437 break;
1438 case WKBD_HMAXIMIZE:
1439 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1440 CloseWindowMenu(scr);
1442 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1444 break;
1445 case WKBD_LHMAXIMIZE:
1446 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1447 CloseWindowMenu(scr);
1449 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1451 break;
1452 case WKBD_RHMAXIMIZE:
1453 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1454 CloseWindowMenu(scr);
1456 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1458 break;
1459 case WKBD_THMAXIMIZE:
1460 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1461 CloseWindowMenu(scr);
1463 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1465 break;
1466 case WKBD_BHMAXIMIZE:
1467 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1468 CloseWindowMenu(scr);
1470 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1472 break;
1473 case WKBD_LTCMAXIMIZE:
1474 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1475 CloseWindowMenu(scr);
1477 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1479 break;
1480 case WKBD_RTCMAXIMIZE:
1481 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1482 CloseWindowMenu(scr);
1484 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1486 break;
1487 case WKBD_LBCMAXIMIZE:
1488 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1489 CloseWindowMenu(scr);
1491 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1493 break;
1494 case WKBD_RBCMAXIMIZE:
1495 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1496 CloseWindowMenu(scr);
1498 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1500 break;
1501 case WKBD_MAXIMUS:
1502 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1503 CloseWindowMenu(scr);
1505 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1507 break;
1508 case WKBD_RAISE:
1509 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1510 CloseWindowMenu(scr);
1512 wRaiseFrame(wwin->frame->core);
1514 break;
1515 case WKBD_LOWER:
1516 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1517 CloseWindowMenu(scr);
1519 wLowerFrame(wwin->frame->core);
1521 break;
1522 case WKBD_RAISELOWER:
1523 /* raise or lower the window under the pointer, not the
1524 * focused one
1526 wwin = windowUnderPointer(scr);
1527 if (wwin)
1528 wRaiseLowerFrame(wwin->frame->core);
1529 break;
1530 case WKBD_SHADE:
1531 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1532 if (wwin->flags.shaded)
1533 wUnshadeWindow(wwin);
1534 else
1535 wShadeWindow(wwin);
1537 break;
1538 case WKBD_MOVERESIZE:
1539 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1540 CloseWindowMenu(scr);
1542 wKeyboardMoveResizeWindow(wwin);
1544 break;
1545 case WKBD_CLOSE:
1546 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1547 CloseWindowMenu(scr);
1548 if (wwin->protocols.DELETE_WINDOW)
1549 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1551 break;
1552 case WKBD_SELECT:
1553 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1554 wSelectWindow(wwin, !wwin->flags.selected);
1556 break;
1557 case WKBD_FOCUSNEXT:
1558 StartWindozeCycle(wwin, event, True, False);
1559 break;
1561 case WKBD_FOCUSPREV:
1562 StartWindozeCycle(wwin, event, False, False);
1563 break;
1565 case WKBD_GROUPNEXT:
1566 StartWindozeCycle(wwin, event, True, True);
1567 break;
1569 case WKBD_GROUPPREV:
1570 StartWindozeCycle(wwin, event, False, True);
1571 break;
1573 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1574 widx = command - WKBD_WORKSPACE1;
1575 i = (scr->current_workspace / 10) * 10 + widx;
1576 if (wPreferences.ws_advance || i < scr->workspace_count)
1577 wWorkspaceChange(scr, i);
1578 break;
1580 case WKBD_NEXTWORKSPACE:
1581 wWorkspaceRelativeChange(scr, 1);
1582 break;
1583 case WKBD_PREVWORKSPACE:
1584 wWorkspaceRelativeChange(scr, -1);
1585 break;
1586 case WKBD_LASTWORKSPACE:
1587 wWorkspaceChange(scr, scr->last_workspace);
1588 break;
1590 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1591 widx = command - WKBD_MOVE_WORKSPACE1;
1592 i = (scr->current_workspace / 10) * 10 + widx;
1593 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1594 wWindowChangeWorkspace(wwin, i);
1595 break;
1597 case WKBD_MOVE_NEXTWORKSPACE:
1598 if (wwin)
1599 wWindowChangeWorkspaceRelative(wwin, 1);
1600 break;
1601 case WKBD_MOVE_PREVWORKSPACE:
1602 if (wwin)
1603 wWindowChangeWorkspaceRelative(wwin, -1);
1604 break;
1605 case WKBD_MOVE_LASTWORKSPACE:
1606 if (wwin)
1607 wWindowChangeWorkspace(wwin, scr->last_workspace);
1608 break;
1610 case WKBD_MOVE_NEXTWSLAYER:
1611 case WKBD_MOVE_PREVWSLAYER:
1613 if (wwin) {
1614 int row, column;
1616 row = scr->current_workspace / 10;
1617 column = scr->current_workspace % 10;
1619 if (command == WKBD_MOVE_NEXTWSLAYER) {
1620 if ((row + 1) * 10 < scr->workspace_count)
1621 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1622 } else {
1623 if (row > 0)
1624 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1628 break;
1630 case WKBD_WINDOW1:
1631 case WKBD_WINDOW2:
1632 case WKBD_WINDOW3:
1633 case WKBD_WINDOW4:
1634 case WKBD_WINDOW5:
1635 case WKBD_WINDOW6:
1636 case WKBD_WINDOW7:
1637 case WKBD_WINDOW8:
1638 case WKBD_WINDOW9:
1639 case WKBD_WINDOW10:
1641 widx = command - WKBD_WINDOW1;
1643 if (scr->shortcutWindows[widx]) {
1644 WMArray *list = scr->shortcutWindows[widx];
1645 int cw;
1646 int count = WMGetArrayItemCount(list);
1647 WWindow *twin;
1648 WMArrayIterator iter;
1649 WWindow *wwin;
1651 wUnselectWindows(scr);
1652 cw = scr->current_workspace;
1654 WM_ETARETI_ARRAY(list, wwin, iter) {
1655 if (count > 1)
1656 wWindowChangeWorkspace(wwin, cw);
1658 wMakeWindowVisible(wwin);
1660 if (count > 1)
1661 wSelectWindow(wwin, True);
1664 /* rotate the order of windows, to create a cycling effect */
1665 twin = WMGetFromArray(list, 0);
1666 WMDeleteFromArray(list, 0);
1667 WMAddToArray(list, twin);
1669 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1670 if (scr->shortcutWindows[widx]) {
1671 WMFreeArray(scr->shortcutWindows[widx]);
1672 scr->shortcutWindows[widx] = NULL;
1675 if (wwin->flags.selected && scr->selected_windows) {
1676 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1677 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1678 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1679 } else {
1680 scr->shortcutWindows[widx] = WMCreateArray(4);
1681 WMAddToArray(scr->shortcutWindows[widx], wwin);
1684 wSelectWindow(wwin, !wwin->flags.selected);
1685 XFlush(dpy);
1686 wusleep(3000);
1687 wSelectWindow(wwin, !wwin->flags.selected);
1688 XFlush(dpy);
1690 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1692 if (wwin->flags.selected && scr->selected_windows) {
1693 if (scr->shortcutWindows[widx]) {
1694 WMFreeArray(scr->shortcutWindows[widx]);
1696 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1700 break;
1702 case WKBD_RELAUNCH:
1703 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1704 (void) RelaunchWindow(wwin);
1706 break;
1708 case WKBD_SWITCH_SCREEN:
1709 if (wScreenCount > 1) {
1710 WScreen *scr2;
1711 int i;
1713 /* find index of this screen */
1714 for (i = 0; i < wScreenCount; i++) {
1715 if (wScreenWithNumber(i) == scr)
1716 break;
1718 i++;
1719 if (i >= wScreenCount) {
1720 i = 0;
1722 scr2 = wScreenWithNumber(i);
1724 if (scr2) {
1725 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1726 scr2->scr_width / 2, scr2->scr_height / 2);
1729 break;
1731 case WKBD_NEXTWSLAYER:
1732 case WKBD_PREVWSLAYER:
1734 int row, column;
1736 row = scr->current_workspace / 10;
1737 column = scr->current_workspace % 10;
1739 if (command == WKBD_NEXTWSLAYER) {
1740 if ((row + 1) * 10 < scr->workspace_count)
1741 wWorkspaceChange(scr, column + (row + 1) * 10);
1742 } else {
1743 if (row > 0)
1744 wWorkspaceChange(scr, column + (row - 1) * 10);
1747 break;
1748 case WKBD_CLIPRAISELOWER:
1749 if (!wPreferences.flags.noclip)
1750 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1751 break;
1752 case WKBD_DOCKRAISELOWER:
1753 if (!wPreferences.flags.nodock)
1754 wDockRaiseLower(scr->dock);
1755 break;
1756 #ifdef KEEP_XKB_LOCK_STATUS
1757 case WKBD_TOGGLE:
1758 if (wPreferences.modelock) {
1759 /*toggle */
1760 wwin = scr->focused_window;
1762 if (wwin && wwin->flags.mapped
1763 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1764 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1765 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1767 wwin->frame->languagemode = wwin->frame->last_languagemode;
1768 wwin->frame->last_languagemode = staterec.group;
1769 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1773 break;
1774 #endif /* KEEP_XKB_LOCK_STATUS */
1778 static void handleMotionNotify(XEvent * event)
1780 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1782 if (wPreferences.scrollable_menus) {
1783 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1784 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1786 if (scr->flags.jump_back_pending ||
1787 p.x <= (rect.pos.x + 1) ||
1788 p.x >= (rect.pos.x + rect.size.width - 2) ||
1789 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1790 WMenu *menu;
1792 menu = wMenuUnderPointer(scr);
1793 if (menu != NULL)
1794 wMenuScroll(menu, event);
1799 static void handleVisibilityNotify(XEvent * event)
1801 WWindow *wwin;
1803 wwin = wWindowFor(event->xvisibility.window);
1804 if (!wwin)
1805 return;
1806 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);