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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 #include <sys/inotify.h>
35 #include <X11/Xutil.h>
37 # include <X11/extensions/shape.h>
43 #ifdef KEEP_XKB_LOCK_STATUS
44 #include <X11/XKBlib.h>
45 #endif /* KEEP_XKB_LOCK_STATUS */
47 #include "WindowMaker.h"
53 #include "application.h"
56 #include "workspace.h"
59 #include "properties.h"
67 /******** Global Variables **********/
68 extern XContext wWinContext;
69 extern XContext wVEdgeContext;
71 extern Cursor wCursor[WCUR_LAST];
73 extern WShortKey wKeyBindings[WKBD_LAST];
74 extern int wScreenCount;
75 extern Time LastTimestamp;
76 extern Time LastFocusChange;
78 extern WPreferences wPreferences;
80 #define MOD_MASK wPreferences.modifier_mask
82 extern Atom _XA_WM_COLORMAP_NOTIFY;
84 extern Atom _XA_WM_CHANGE_STATE;
85 extern Atom _XA_WM_DELETE_WINDOW;
86 extern Atom _XA_GNUSTEP_WM_ATTR;
87 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
88 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
89 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
90 extern Atom _XA_WINDOWMAKER_COMMAND;
94 extern Bool wShapeSupported;
95 extern int wShapeEventBase;
98 #ifdef KEEP_XKB_LOCK_STATUS
99 extern int wXkbEventBase;
103 /*extern char WDelayedActionSet;*/
106 /************ Local stuff ***********/
108 static void saveTimestamp(XEvent *event);
109 static void handleColormapNotify();
110 static void handleMapNotify();
111 static void handleUnmapNotify();
112 static void handleButtonPress();
113 static void handleExpose();
114 static void handleDestroyNotify();
115 static void handleConfigureRequest();
116 static void handleMapRequest();
117 static void handlePropertyNotify();
118 static void handleEnterNotify();
119 static void handleLeaveNotify();
120 static void handleExtensions();
121 static void handleClientMessage();
122 static void handleKeyPress();
123 static void handleFocusIn();
124 static void handleMotionNotify();
125 static void handleVisibilityNotify();
129 static void handleShapeNotify();
132 /* called from the signal handler */
133 void NotifyDeadProcess(pid_t pid, unsigned char status);
135 /* real dead process handler */
136 static void handleDeadProcess(void *foo);
139 typedef struct DeadProcesses {
141 unsigned char exit_status;
144 /* stack of dead processes */
145 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
146 static int deadProcessPtr=0;
149 typedef struct DeathHandler {
150 WDeathHandler *callback;
155 static WMArray *deathHandlers=NULL;
160 wAddDeathHandler(pid_t pid, WDeathHandler *callback, void *cdata)
162 DeathHandler *handler;
164 handler = malloc(sizeof(DeathHandler));
169 handler->callback = callback;
170 handler->client_data = cdata;
173 deathHandlers = WMCreateArrayWithDestructor(8, wfree);
175 WMAddToArray(deathHandlers, handler);
183 wDeleteDeathHandler(WMagicNumber id)
185 DeathHandler *handler=(DeathHandler*)id;
187 if (!handler || !deathHandlers)
190 /* array destructor will call wfree(handler) */
191 WMRemoveFromArray(deathHandlers, handler);
196 DispatchEvent(XEvent *event)
199 handleDeadProcess(NULL);
201 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
202 WCHANGE_STATE(WSTATE_EXITING);
203 /* received SIGTERM */
205 * WMHandleEvent() can't be called from anything
206 * executed inside here, or we can get in a infinite
209 Shutdown(WSExitMode);
211 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
212 WCHANGE_STATE(WSTATE_RESTARTING);
214 Shutdown(WSRestartPreparationMode);
215 /* received SIGHUP */
217 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
218 WCHANGE_STATE(WSTATE_NORMAL);
219 wDefaultsCheckDomains("bla");
222 /* for the case that all that is wanted to be dispatched is
227 saveTimestamp(event);
228 switch (event->type) {
230 handleMapRequest(event);
234 handleKeyPress(event);
238 handleMotionNotify(event);
241 case ConfigureRequest:
242 handleConfigureRequest(event);
246 handleDestroyNotify(event);
250 handleMapNotify(event);
254 handleUnmapNotify(event);
258 handleButtonPress(event);
266 handlePropertyNotify(event);
270 handleEnterNotify(event);
274 handleLeaveNotify(event);
278 handleClientMessage(event);
282 handleColormapNotify(event);
286 if (event->xmapping.request == MappingKeyboard
287 || event->xmapping.request == MappingModifier)
288 XRefreshKeyboardMapping(&event->xmapping);
292 handleFocusIn(event);
295 case VisibilityNotify:
296 handleVisibilityNotify(event);
299 handleExtensions(event);
305 *----------------------------------------------------------------------
306 * inotifyHandleEvents-
307 * Check for inotify events
310 * After reading events for the given file descriptor (fd) and
311 * watch descriptor (wd)
314 * Calls wDefaultsCheckDomains if config database is updated
315 *----------------------------------------------------------------------
318 /* Allow for 1024 simultanious events */
319 #define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
320 void inotifyHandleEvents (int fd, int wd)
322 ssize_t eventQLength, i = 0;
323 char buff[BUFF_SIZE] = {0};
324 extern void wDefaultsCheckDomains();
325 int oneShotFlag=0; /* Only check config once per read of the event queue */
327 /* Read off the queued events
328 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
329 * not occur; the block is on Xevents, but a config file change will normally
330 * occur as a result of an Xevent - so the event queue should never have more than
331 * a few entries before a read().
333 eventQLength = read (fd, buff, BUFF_SIZE);
335 /* check what events occured */
336 /* Should really check wd here too, but for now we only have one watch! */
337 while (i < eventQLength) {
338 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
341 * see inotify.h for event types.
343 if (pevent->mask & IN_DELETE_SELF) {
344 wwarning(_("the defaults database has been deleted!"
345 " Restart Window Maker to create the database"
346 " with the default settings"));
349 if (pevent->mask & IN_UNMOUNT) {
350 wwarning(_("the unit containing the defaults database has"
351 " been unmounted. Setting --static mode."
352 " Any changes will not be saved."));
354 wPreferences.flags.noupdates=1;
356 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
357 fprintf(stdout,"wmaker: reading config files in defaults database.\n");
358 wDefaultsCheckDomains(NULL);
360 /* Check for filename (length of name (len) > 0) */
361 /* if (pevent->len) printf ("name=%s\n", pevent->name); */
363 i += sizeof(struct inotify_event) + pevent->len; /* move to next event in the buffer */
366 } /* inotifyHandleEvents */
369 *----------------------------------------------------------------------
371 * Processes X and internal events indefinitely.
377 * The LastTimestamp global variable is updated.
378 * Calls inotifyGetEvents if defaults database changes.
379 *----------------------------------------------------------------------
385 extern int inotifyFD;
386 extern int inotifyWD;
391 if (inotifyFD < 0 || inotifyWD < 0)
396 WMNextEvent(dpy, &event); /* Blocks here */
397 WMHandleEvent(&event);
403 FD_SET (inotifyFD, &rfds);
405 /* check for available read data from inotify - don't block! */
406 retVal = select (inotifyFD + 1, &rfds, NULL, NULL, &time);
408 if (retVal < 0) { /* an error has occured */
409 wwarning(_("select failed. The inotify instance will be closed."
410 " Changes to the defaults database will require"
411 " a restart to take effect."));
415 if (FD_ISSET (inotifyFD, &rfds))
416 inotifyHandleEvents(inotifyFD,inotifyWD);
422 *----------------------------------------------------------------------
423 * ProcessPendingEvents --
424 * Processes the events that are currently pending (at the time
425 * this function is called) in the display's queue.
428 * After the pending events that were present at the function call
432 * Many -- whatever handling events may involve.
434 *----------------------------------------------------------------------
437 ProcessPendingEvents()
444 /* Take a snapshot of the event count in the queue */
445 count = XPending(dpy);
447 while (count>0 && XPending(dpy)) {
448 WMNextEvent(dpy, &event);
449 WMHandleEvent(&event);
456 IsDoubleClick(WScreen *scr, XEvent *event)
458 if ((scr->last_click_time>0) &&
459 (event->xbutton.time-scr->last_click_time<=wPreferences.dblclick_time)
460 && (event->xbutton.button == scr->last_click_button)
461 && (event->xbutton.window == scr->last_click_window)) {
463 scr->flags.next_click_is_not_double = 1;
464 scr->last_click_time = 0;
465 scr->last_click_window = event->xbutton.window;
474 NotifyDeadProcess(pid_t pid, unsigned char status)
476 if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
477 wwarning("stack overflow: too many dead processes");
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;
489 handleDeadProcess(void *foo)
494 for (i=0; i<deadProcessPtr; i++) {
495 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
498 if (!deathHandlers) {
503 /* get the pids on the queue and call handlers */
504 while (deadProcessPtr>0) {
507 for (i = WMGetArrayItemCount(deathHandlers)-1; i >= 0; i--) {
508 tmp = WMGetFromArray(deathHandlers, i);
512 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
513 (*tmp->callback)(tmp->pid,
514 deadProcesses[deadProcessPtr].exit_status,
516 wDeleteDeathHandler(tmp);
524 saveTimestamp(XEvent *event)
527 * Never save CurrentTime as LastTimestamp because CurrentTime
528 * it's not a real timestamp (it's the 0L constant)
531 switch (event->type) {
534 LastTimestamp = event->xbutton.time;
538 LastTimestamp = event->xkey.time;
541 LastTimestamp = event->xmotion.time;
544 LastTimestamp = event->xproperty.time;
548 LastTimestamp = event->xcrossing.time;
551 LastTimestamp = event->xselectionclear.time;
553 case SelectionRequest:
554 LastTimestamp = event->xselectionrequest.time;
556 case SelectionNotify:
557 LastTimestamp = event->xselection.time;
559 wXDNDProcessSelection(event);
567 matchWindow(void *item, void *cdata)
569 return (((WFakeGroupLeader*)item)->origLeader == (Window)cdata);
574 handleExtensions(XEvent *event)
576 #ifdef KEEP_XKB_LOCK_STATUS
578 xkbevent = (XkbEvent *)event;
579 #endif /*KEEP_XKB_LOCK_STATUS*/
581 if (wShapeSupported && event->type == (wShapeEventBase+ShapeNotify)) {
582 handleShapeNotify(event);
585 #ifdef KEEP_XKB_LOCK_STATUS
586 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)){
587 handleXkbIndicatorStateNotify(event);
589 #endif /*KEEP_XKB_LOCK_STATUS*/
594 handleMapRequest(XEvent *ev)
598 Window window = ev->xmaprequest.window;
601 printf("got map request for %x\n", (unsigned)window);
603 if ((wwin = wWindowFor(window))) {
604 if (wwin->flags.shaded) {
605 wUnshadeWindow(wwin);
607 /* deiconify window */
608 if (wwin->flags.miniaturized) {
609 wDeiconifyWindow(wwin);
610 } else if (wwin->flags.hidden) {
611 WApplication *wapp = wApplicationOf(wwin->main_window);
612 /* go to the last workspace that the user worked on the app */
614 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
616 wUnhideApplication(wapp, False, False);
621 scr = wScreenForRootWindow(ev->xmaprequest.parent);
623 wwin = wManageWindow(scr, window);
626 * This is to let the Dock know that the application it launched
627 * has already been mapped (eg: it has finished launching).
628 * It is not necessary for normally docked apps, but is needed for
629 * apps that were forcedly docked (like with dockit).
631 if (scr->last_dock) {
632 if (wwin && wwin->main_window!=None && wwin->main_window!=window)
633 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
635 wDockTrackWindowLaunch(scr->last_dock, window);
639 wClientSetState(wwin, NormalState, None);
640 if (wwin->flags.maximized) {
641 wMaximizeWindow(wwin, wwin->flags.maximized);
643 if (wwin->flags.shaded) {
644 wwin->flags.shaded = 0;
645 wwin->flags.skip_next_animation = 1;
648 if (wwin->flags.miniaturized) {
649 wwin->flags.miniaturized = 0;
650 wwin->flags.skip_next_animation = 1;
651 wIconifyWindow(wwin);
653 if (wwin->flags.fullscreen) {
654 wwin->flags.fullscreen = 0;
655 wFullscreenWindow(wwin);
657 if (wwin->flags.hidden) {
658 WApplication *wapp = wApplicationOf(wwin->main_window);
660 wwin->flags.hidden = 0;
661 wwin->flags.skip_next_animation = 1;
663 wHideApplication(wapp);
671 handleDestroyNotify(XEvent *event)
675 Window window = event->xdestroywindow.window;
676 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
680 printf("got destroy notify\n");
682 wwin = wWindowFor(window);
684 wUnmanageWindow(wwin, False, True);
688 while ((index = WMFindInArray(scr->fakeGroupLeaders, matchWindow,
689 (void*)window)) != WANotFound) {
690 WFakeGroupLeader *fPtr;
692 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
693 if (fPtr->retainCount > 0) {
695 if (fPtr->retainCount==0 && fPtr->leader!=None) {
696 XDestroyWindow(dpy, fPtr->leader);
701 fPtr->origLeader = None;
705 app = wApplicationOf(window);
707 if (window == app->main_window) {
709 wwin = app->main_window_desc->screen_ptr->focused_window;
711 if (wwin->main_window == window) {
712 wwin->main_window = None;
717 wApplicationDestroy(app);
724 handleExpose(XEvent *event)
726 WObjDescriptor *desc;
730 printf("got expose\n");
732 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev));
734 if (XFindContext(dpy, event->xexpose.window, wWinContext,
735 (XPointer *)&desc)==XCNOENT) {
739 if (desc->handle_expose) {
740 (*desc->handle_expose)(desc, event);
745 executeButtonAction(WScreen *scr, XEvent *event, int action)
748 case WA_SELECT_WINDOWS:
749 wUnselectWindows(scr);
750 wSelectWindows(scr, event);
752 case WA_OPEN_APPMENU:
753 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
755 if (scr->root_menu) {
756 if (scr->root_menu->brother->flags.mapped)
757 event->xbutton.window = scr->root_menu->brother->frame->core->window;
759 event->xbutton.window = scr->root_menu->frame->core->window;
762 case WA_OPEN_WINLISTMENU:
763 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
764 if (scr->switch_menu) {
765 if (scr->switch_menu->brother->flags.mapped)
766 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
768 event->xbutton.window = scr->switch_menu->frame->core->window;
779 handleButtonPress(XEvent *event)
781 WObjDescriptor *desc;
785 printf("got button press\n");
787 scr = wScreenForRootWindow(event->xbutton.root);
795 if (event->xbutton.window==scr->root_win) {
796 if (event->xbutton.button==Button1 &&
797 wPreferences.mouse_button1!=WA_NONE) {
798 executeButtonAction(scr, event, wPreferences.mouse_button1);
799 } else if (event->xbutton.button==Button2 &&
800 wPreferences.mouse_button2!=WA_NONE) {
801 executeButtonAction(scr, event, wPreferences.mouse_button2);
802 } else if (event->xbutton.button==Button3 &&
803 wPreferences.mouse_button3!=WA_NONE) {
804 executeButtonAction(scr, event, wPreferences.mouse_button3);
805 } else if (event->xbutton.button==Button4 &&
806 wPreferences.mouse_wheel!=WA_NONE) {
807 wWorkspaceRelativeChange(scr, 1);
808 } else if (event->xbutton.button==Button5 &&
809 wPreferences.mouse_wheel!=WA_NONE) {
810 wWorkspaceRelativeChange(scr, -1);
816 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext,
817 (XPointer *)&desc)==XCNOENT) {
818 if (XFindContext(dpy, event->xbutton.window, wWinContext,
819 (XPointer *)&desc)==XCNOENT) {
824 if (desc->parent_type == WCLASS_WINDOW) {
827 if (event->xbutton.state & MOD_MASK) {
828 XAllowEvents(dpy, AsyncPointer, CurrentTime);
831 /* if (wPreferences.focus_mode == WKF_CLICK) {*/
832 if (wPreferences.ignore_focus_click) {
833 XAllowEvents(dpy, AsyncPointer, CurrentTime);
835 XAllowEvents(dpy, ReplayPointer, CurrentTime);
838 } else if (desc->parent_type == WCLASS_APPICON
839 || desc->parent_type == WCLASS_MINIWINDOW
840 || desc->parent_type == WCLASS_DOCK_ICON) {
841 if (event->xbutton.state & MOD_MASK) {
843 XAllowEvents(dpy, AsyncPointer, CurrentTime);
848 if (desc->handle_mousedown!=NULL) {
849 (*desc->handle_mousedown)(desc, event);
852 /* save double-click information */
853 if (scr->flags.next_click_is_not_double) {
854 scr->flags.next_click_is_not_double = 0;
856 scr->last_click_time = event->xbutton.time;
857 scr->last_click_button = event->xbutton.button;
858 scr->last_click_window = event->xbutton.window;
864 handleMapNotify(XEvent *event)
870 wwin = wWindowFor(event->xmap.event);
871 if (wwin && wwin->client_win == event->xmap.event) {
872 if (wwin->flags.miniaturized) {
873 wDeiconifyWindow(wwin);
877 wClientSetState(wwin, NormalState, None);
885 handleUnmapNotify(XEvent *event)
889 Bool withdraw = False;
891 printf("got unmap\n");
893 /* only process windows with StructureNotify selected
894 * (ignore SubstructureNotify) */
895 wwin = wWindowFor(event->xunmap.window);
899 /* whether the event is a Withdrawal request */
900 if (event->xunmap.event == wwin->screen_ptr->root_win
901 && event->xunmap.send_event)
904 if (wwin->client_win != event->xunmap.event && !withdraw)
907 if (!wwin->flags.mapped && !withdraw
908 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
909 && !wwin->flags.miniaturized && !wwin->flags.hidden)
913 XUnmapWindow(dpy, wwin->frame->core->window);
914 wwin->flags.mapped = 0;
916 /* check if the window was destroyed */
917 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify,&ev)) {
920 Bool reparented = False;
922 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
925 /* withdraw window */
926 wwin->flags.mapped = 0;
928 wClientSetState(wwin, WithdrawnState, None);
930 /* if the window was reparented, do not reparent it back to the
932 wUnmanageWindow(wwin, !reparented, False);
939 handleConfigureRequest(XEvent *event)
943 printf("got configure request\n");
945 if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
947 * Configure request for unmapped window
949 wClientConfigure(NULL, &(event->xconfigurerequest));
951 wClientConfigure(wwin, &(event->xconfigurerequest));
957 handlePropertyNotify(XEvent *event)
966 printf("got property notify\n");
968 if ((wwin=wWindowFor(event->xproperty.window))) {
969 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
970 &ju, &ju, &ju, &ju)) {
973 wClientCheckProperty(wwin, &event->xproperty);
975 wapp = wApplicationOf(event->xproperty.window);
977 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
980 scr = wScreenForWindow(event->xproperty.window);
985 handleClientMessage(XEvent *event)
988 WObjDescriptor *desc;
990 printf("got client message\n");
992 /* handle transition from Normal to Iconic state */
993 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
994 && event->xclient.format == 32
995 && event->xclient.data.l[0] == IconicState) {
997 wwin = wWindowFor(event->xclient.window);
999 if (!wwin->flags.miniaturized)
1000 wIconifyWindow(wwin);
1001 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY
1002 && event->xclient.format == 32) {
1003 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1008 if (event->xclient.data.l[1] == 1) { /* starting */
1009 wColormapAllowClientInstallation(scr, True);
1010 } else { /* stopping */
1011 wColormapAllowClientInstallation(scr, False);
1013 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
1015 wDefaultsCheckDomains("bla");
1017 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
1020 wapp = wApplicationOf(event->xclient.window);
1022 switch (event->xclient.data.l[0]) {
1023 case WMFHideOtherApplications:
1024 wHideOtherApplications(wapp->main_window_desc);
1028 case WMFHideApplication:
1029 wHideApplication(wapp);
1035 wwin = wWindowFor(event->xclient.window);
1037 switch (event->xclient.data.l[0]) {
1038 case WMFHideOtherApplications:
1039 wHideOtherApplications(wwin);
1042 case WMFHideApplication:
1043 wHideApplication(wApplicationOf(wwin->main_window));
1048 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1049 wwin = wWindowFor(event->xclient.window);
1051 switch (event->xclient.data.l[0]) {
1052 case GSWindowLevelAttr:
1054 int level = (int)event->xclient.data.l[1];
1056 if (WINDOW_LEVEL(wwin) != level) {
1057 ChangeStackingLevel(wwin->frame->core, level);
1062 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1063 wwin = wWindowFor(event->xclient.window);
1065 switch (event->xclient.data.l[0]) {
1066 case WMTitleBarNormal:
1067 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1069 case WMTitleBarMain:
1070 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1073 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1077 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1081 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1086 * Non-standard thing, but needed by OffiX DND.
1087 * For when the icon frame gets a ClientMessage
1088 * that should have gone to the icon_window.
1090 if (XFindContext(dpy, event->xbutton.window, wWinContext,
1091 (XPointer *)&desc)!=XCNOENT) {
1092 struct WIcon *icon=NULL;
1094 if (desc->parent_type == WCLASS_MINIWINDOW) {
1095 icon = (WIcon*)desc->parent;
1096 } else if (desc->parent_type == WCLASS_DOCK_ICON
1097 || desc->parent_type == WCLASS_APPICON) {
1098 icon = ((WAppIcon*)desc->parent)->icon;
1100 if (icon && (wwin=icon->owner)) {
1101 if (wwin->client_win!=event->xclient.window) {
1102 event->xclient.window = wwin->client_win;
1103 XSendEvent(dpy, wwin->client_win, False, NoEventMask,
1113 raiseWindow(WScreen *scr)
1117 scr->autoRaiseTimer = NULL;
1119 wwin = wWindowFor(scr->autoRaiseWindow);
1123 if (!wwin->flags.destroyed && wwin->flags.focused) {
1124 wRaiseFrame(wwin->frame->core);
1125 /* this is needed or a race condition will occur */
1132 handleEnterNotify(XEvent *event)
1135 WObjDescriptor *desc = NULL;
1136 #ifdef VIRTUAL_DESKTOP
1137 void (*vdHandler)(XEvent * event);
1140 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1142 printf("got enter notify\n");
1145 #ifdef VIRTUAL_DESKTOP
1146 if (XFindContext(dpy, event->xcrossing.window, wVEdgeContext,
1147 (XPointer *)&vdHandler)!=XCNOENT) {
1148 (*vdHandler)(event);
1152 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
1154 /* already left the window... */
1156 if (ev.xcrossing.mode==event->xcrossing.mode
1157 && ev.xcrossing.detail==event->xcrossing.detail) {
1162 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1163 (XPointer *)&desc)!=XCNOENT) {
1164 if(desc->handle_enternotify)
1165 (*desc->handle_enternotify)(desc, event);
1168 /* enter to window */
1169 wwin = wWindowFor(event->xcrossing.window);
1171 if (wPreferences.colormap_mode==WCM_POINTER) {
1172 wColormapInstallForWindow(scr, NULL);
1174 if (scr->autoRaiseTimer
1175 && event->xcrossing.root==event->xcrossing.window) {
1176 WMDeleteTimerHandler(scr->autoRaiseTimer);
1177 scr->autoRaiseTimer = NULL;
1180 /* set auto raise timer even if in focus-follows-mouse mode
1181 * and the event is for the frame window, even if the window
1182 * has focus already. useful if you move the pointer from a focused
1183 * window to the root window and back pretty fast
1185 * set focus if in focus-follows-mouse mode and the event
1186 * is for the frame window and window doesn't have focus yet */
1187 if (wPreferences.focus_mode==WKF_SLOPPY
1188 && wwin->frame->core->window==event->xcrossing.window
1189 && !scr->flags.doing_alt_tab) {
1191 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1192 wSetFocusTo(scr, wwin);
1194 if (scr->autoRaiseTimer)
1195 WMDeleteTimerHandler(scr->autoRaiseTimer);
1196 scr->autoRaiseTimer = NULL;
1198 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1199 scr->autoRaiseWindow = wwin->frame->core->window;
1201 = WMAddTimerHandler(wPreferences.raise_delay,
1202 (WMCallback*)raiseWindow, scr);
1205 /* Install colormap for window, if the colormap installation mode
1206 * is colormap_follows_mouse */
1207 if (wPreferences.colormap_mode==WCM_POINTER) {
1208 if (wwin->client_win==event->xcrossing.window)
1209 wColormapInstallForWindow(scr, wwin);
1211 wColormapInstallForWindow(scr, NULL);
1215 /* a little kluge to hide the clip balloon */
1216 if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1218 XUnmapWindow(dpy, scr->clip_balloon);
1219 scr->flags.clip_balloon_mapped = 0;
1221 if (desc->parent_type!=WCLASS_DOCK_ICON
1222 || scr->clip_icon != desc->parent) {
1223 XUnmapWindow(dpy, scr->clip_balloon);
1224 scr->flags.clip_balloon_mapped = 0;
1229 if (event->xcrossing.window == event->xcrossing.root
1230 && event->xcrossing.detail == NotifyNormal
1231 && event->xcrossing.detail != NotifyInferior
1232 && wPreferences.focus_mode != WKF_CLICK) {
1234 wSetFocusTo(scr, scr->focused_window);
1238 wBalloonEnteredObject(scr, desc);
1244 handleLeaveNotify(XEvent *event)
1246 WObjDescriptor *desc = NULL;
1248 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1249 (XPointer *)&desc)!=XCNOENT) {
1250 if(desc->handle_leavenotify)
1251 (*desc->handle_leavenotify)(desc, event);
1258 handleShapeNotify(XEvent *event)
1260 XShapeEvent *shev = (XShapeEvent*)event;
1264 printf("got shape notify\n");
1266 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1267 XShapeEvent *sev = (XShapeEvent*)&ev;
1269 if (sev->kind == ShapeBounding) {
1270 if (sev->shaped == shev->shaped) {
1273 XPutBackEvent(dpy, &ev);
1279 wwin = wWindowFor(shev->window);
1280 if (!wwin || shev->kind != ShapeBounding)
1283 if (!shev->shaped && wwin->flags.shaped) {
1285 wwin->flags.shaped = 0;
1286 wWindowClearShape(wwin);
1288 } else if (shev->shaped) {
1290 wwin->flags.shaped = 1;
1291 wWindowSetShape(wwin);
1296 #ifdef KEEP_XKB_LOCK_STATUS
1297 /* please help ]d if you know what to do */
1298 handleXkbIndicatorStateNotify(XEvent *event)
1302 XkbStateRec staterec;
1305 for (i=0; i<wScreenCount; i++) {
1306 scr = wScreenWithNumber(i);
1307 wwin = scr->focused_window;
1308 if (wwin && wwin->flags.focused) {
1309 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1310 if (wwin->frame->languagemode != staterec.group) {
1311 wwin->frame->last_languagemode = wwin->frame->languagemode;
1312 wwin->frame->languagemode = staterec.group;
1314 #ifdef XKB_BUTTON_HINT
1315 if (wwin->frame->titlebar) {
1316 wFrameWindowPaint(wwin->frame);
1322 #endif /*KEEP_XKB_LOCK_STATUS*/
1325 handleColormapNotify(XEvent *event)
1329 Bool reinstall = False;
1331 wwin = wWindowFor(event->xcolormap.window);
1335 scr = wwin->screen_ptr;
1339 if (event->xcolormap.new) {
1340 XWindowAttributes attr;
1342 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1344 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1345 scr->current_colormap = attr.colormap;
1348 } else if (event->xcolormap.state == ColormapUninstalled &&
1349 scr->current_colormap == event->xcolormap.colormap) {
1351 /* some bastard app (like XV) removed our colormap */
1353 * can't enforce or things like xscreensaver wont work
1356 } else if (event->xcolormap.state == ColormapInstalled &&
1357 scr->current_colormap == event->xcolormap.colormap) {
1359 /* someone has put our colormap back */
1363 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1364 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1366 if (reinstall && scr->current_colormap!=None) {
1367 if (!scr->flags.colormap_stuff_blocked)
1368 XInstallColormap(dpy, scr->current_colormap);
1375 handleFocusIn(XEvent *event)
1380 * For applications that like stealing the focus.
1382 while (XCheckTypedEvent(dpy, FocusIn, event));
1383 saveTimestamp(event);
1384 if (event->xfocus.mode == NotifyUngrab
1385 || event->xfocus.mode == NotifyGrab
1386 || event->xfocus.detail > NotifyNonlinearVirtual) {
1390 wwin = wWindowFor(event->xfocus.window);
1391 if (wwin && !wwin->flags.focused) {
1392 if (wwin->flags.mapped)
1393 wSetFocusTo(wwin->screen_ptr, wwin);
1395 wSetFocusTo(wwin->screen_ptr, NULL);
1397 WScreen *scr = wScreenForWindow(event->xfocus.window);
1399 wSetFocusTo(scr, NULL);
1405 windowUnderPointer(WScreen *scr)
1411 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
1413 return wWindowFor(win);
1418 static int CheckFullScreenWindowFocused(WScreen *scr)
1420 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1428 handleKeyPress(XEvent *event)
1430 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1431 WWindow *wwin = scr->focused_window;
1434 int command=-1, index;
1435 #ifdef KEEP_XKB_LOCK_STATUS
1436 XkbStateRec staterec;
1437 #endif /*KEEP_XKB_LOCK_STATUS*/
1439 /* ignore CapsLock */
1440 modifiers = event->xkey.state & ValidModMask;
1442 for (i=0; i<WKBD_LAST; i++) {
1443 if (wKeyBindings[i].keycode==0)
1446 if (wKeyBindings[i].keycode==event->xkey.keycode
1447 && (/*wKeyBindings[i].modifier==0
1448 ||*/ wKeyBindings[i].modifier==modifiers)) {
1462 if (!wRootMenuPerformShortcut(event)) {
1464 static int dontLoop = 0;
1466 if (dontLoop > 10) {
1467 wwarning("problem with key event processing code");
1471 /* if the focused window is an internal window, try redispatching
1472 * the event to the managed window, as it can be a WINGs window */
1473 if (wwin && wwin->flags.internal_window
1474 && wwin->client_leader!=None) {
1475 /* client_leader contains the WINGs toplevel */
1476 event->xany.window = wwin->client_leader;
1477 WMHandleEvent(event);
1484 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1485 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1490 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True);*/
1491 if (!CheckFullScreenWindowFocused(scr)) {
1492 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1493 OpenRootMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1496 case WKBD_WINDOWLIST:
1497 if (!CheckFullScreenWindowFocused(scr)) {
1498 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1499 OpenSwitchMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1503 case WKBD_WINDOWMENU:
1504 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1505 OpenWindowMenu(wwin, wwin->frame_x,
1506 wwin->frame_y+wwin->frame->top_width, True);
1508 case WKBD_MINIATURIZE:
1509 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1510 && !WFLAGP(wwin, no_miniaturizable)) {
1511 CloseWindowMenu(scr);
1513 if (wwin->protocols.MINIATURIZE_WINDOW)
1514 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
1515 event->xbutton.time);
1517 wIconifyWindow(wwin);
1522 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1523 WApplication *wapp = wApplicationOf(wwin->main_window);
1524 CloseWindowMenu(scr);
1526 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1527 wHideApplication(wapp);
1531 case WKBD_HIDE_OTHERS:
1532 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1533 CloseWindowMenu(scr);
1535 wHideOtherApplications(wwin);
1539 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1540 int newdir = (MAX_VERTICAL|MAX_HORIZONTAL);
1542 CloseWindowMenu(scr);
1544 if (wwin->flags.maximized == newdir) {
1545 wUnmaximizeWindow(wwin);
1547 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1551 case WKBD_VMAXIMIZE:
1552 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1553 int newdir = (MAX_VERTICAL ^ wwin->flags.maximized);
1555 CloseWindowMenu(scr);
1558 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1560 wUnmaximizeWindow(wwin);
1564 case WKBD_HMAXIMIZE:
1565 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1566 int newdir = (MAX_HORIZONTAL ^ wwin->flags.maximized);
1568 CloseWindowMenu(scr);
1571 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1573 wUnmaximizeWindow(wwin);
1578 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1579 CloseWindowMenu(scr);
1581 wRaiseFrame(wwin->frame->core);
1585 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1586 CloseWindowMenu(scr);
1588 wLowerFrame(wwin->frame->core);
1591 case WKBD_RAISELOWER:
1592 /* raise or lower the window under the pointer, not the
1595 wwin = windowUnderPointer(scr);
1597 wRaiseLowerFrame(wwin->frame->core);
1600 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1601 if (wwin->flags.shaded)
1602 wUnshadeWindow(wwin);
1607 case WKBD_MOVERESIZE:
1608 if (ISMAPPED(wwin) && ISFOCUSED(wwin) &&
1609 (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1610 CloseWindowMenu(scr);
1612 wKeyboardMoveResizeWindow(wwin);
1616 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1617 CloseWindowMenu(scr);
1618 if (wwin->protocols.DELETE_WINDOW)
1619 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW,
1624 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1625 wSelectWindow(wwin, !wwin->flags.selected);
1628 case WKBD_FOCUSNEXT:
1629 StartWindozeCycle(wwin, event, True);
1632 case WKBD_FOCUSPREV:
1633 StartWindozeCycle(wwin, event, False);
1636 #if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
1637 #define GOTOWORKS(wk) case WKBD_WORKSPACE##wk:\
1638 i = (scr->current_workspace/10)*10 + wk - 1;\
1639 if (wPreferences.ws_advance || i<scr->workspace_count)\
1640 wWorkspaceChange(scr, i);\
1643 #define GOTOWORKS(wk) case WKBD_WORKSPACE/**/wk:\
1644 i = (scr->current_workspace/10)*10 + wk - 1;\
1645 if (wPreferences.ws_advance || i<scr->workspace_count)\
1646 wWorkspaceChange(scr, i);\
1660 case WKBD_NEXTWORKSPACE:
1661 wWorkspaceRelativeChange(scr, 1);
1663 case WKBD_PREVWORKSPACE:
1664 wWorkspaceRelativeChange(scr, -1);
1677 index = command-WKBD_WINDOW1;
1679 if (scr->shortcutWindows[index]) {
1680 WMArray *list = scr->shortcutWindows[index];
1682 int count = WMGetArrayItemCount(list);
1684 WMArrayIterator iter;
1687 wUnselectWindows(scr);
1688 cw = scr->current_workspace;
1690 WM_ETARETI_ARRAY(list, wwin, iter) {
1692 wWindowChangeWorkspace(wwin, cw);
1694 wMakeWindowVisible(wwin);
1697 wSelectWindow(wwin, True);
1700 /* rotate the order of windows, to create a cycling effect */
1701 twin = WMGetFromArray(list, 0);
1702 WMDeleteFromArray(list, 0);
1703 WMAddToArray(list, twin);
1705 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1706 if (scr->shortcutWindows[index]) {
1707 WMFreeArray(scr->shortcutWindows[index]);
1708 scr->shortcutWindows[index] = NULL;
1711 if (wwin->flags.selected && scr->selected_windows) {
1712 scr->shortcutWindows[index] =
1713 WMDuplicateArray(scr->selected_windows);
1714 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1715 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
1717 scr->shortcutWindows[index] = WMCreateArray(4);
1718 WMAddToArray(scr->shortcutWindows[index], wwin);
1721 wSelectWindow(wwin, !wwin->flags.selected);
1724 wSelectWindow(wwin, !wwin->flags.selected);
1727 } else if (scr->selected_windows
1728 && WMGetArrayItemCount(scr->selected_windows)) {
1730 if (wwin->flags.selected && scr->selected_windows) {
1731 if (scr->shortcutWindows[index]) {
1732 WMFreeArray(scr->shortcutWindows[index]);
1734 scr->shortcutWindows[index] =
1735 WMDuplicateArray(scr->selected_windows);
1741 case WKBD_SWITCH_SCREEN:
1742 if (wScreenCount > 1) {
1746 /* find index of this screen */
1747 for (i = 0; i < wScreenCount; i++) {
1748 if (wScreenWithNumber(i) == scr)
1752 if (i >= wScreenCount) {
1755 scr2 = wScreenWithNumber(i);
1758 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1759 scr2->scr_width/2, scr2->scr_height/2);
1764 case WKBD_NEXTWSLAYER:
1765 case WKBD_PREVWSLAYER:
1769 row = scr->current_workspace/10;
1770 column = scr->current_workspace%10;
1772 if (command==WKBD_NEXTWSLAYER) {
1773 if ((row+1)*10 < scr->workspace_count)
1774 wWorkspaceChange(scr, column+(row+1)*10);
1777 wWorkspaceChange(scr, column+(row-1)*10);
1781 case WKBD_CLIPLOWER:
1782 if (!wPreferences.flags.noclip)
1783 wDockLower(scr->workspaces[scr->current_workspace]->clip);
1785 case WKBD_CLIPRAISE:
1786 if (!wPreferences.flags.noclip)
1787 wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1789 case WKBD_CLIPRAISELOWER:
1790 if (!wPreferences.flags.noclip)
1791 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1793 #ifdef KEEP_XKB_LOCK_STATUS
1795 if(wPreferences.modelock) {
1797 wwin = scr->focused_window;
1799 if (wwin && wwin->flags.mapped
1800 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1801 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1802 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1804 wwin->frame->languagemode = wwin->frame->last_languagemode;
1805 wwin->frame->last_languagemode = staterec.group;
1806 XkbLockGroup(dpy,XkbUseCoreKbd, wwin->frame->languagemode);
1811 #endif /* KEEP_XKB_LOCK_STATUS */
1812 #ifdef VIRTUAL_DESKTOP
1813 case WKBD_VDESK_LEFT:
1814 wWorkspaceKeyboardMoveDesktop(scr, VEC_LEFT);
1817 case WKBD_VDESK_RIGHT:
1818 wWorkspaceKeyboardMoveDesktop(scr, VEC_RIGHT);
1822 wWorkspaceKeyboardMoveDesktop(scr, VEC_UP);
1825 case WKBD_VDESK_DOWN:
1826 wWorkspaceKeyboardMoveDesktop(scr, VEC_DOWN);
1835 handleMotionNotify(XEvent *event)
1837 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1839 if (wPreferences.scrollable_menus) {
1840 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1841 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1843 if (scr->flags.jump_back_pending ||
1844 p.x <= (rect.pos.x + 1) ||
1845 p.x >= (rect.pos.x + rect.size.width - 2) ||
1846 p.y <= (rect.pos.y + 1) ||
1847 p.y >= (rect.pos.y + rect.size.height - 2)) {
1850 printf("pointer at screen edge\n");
1852 menu = wMenuUnderPointer(scr);
1854 wMenuScroll(menu, event);
1861 handleVisibilityNotify(XEvent *event)
1865 wwin = wWindowFor(event->xvisibility.window);
1868 wwin->flags.obscured =
1869 (event->xvisibility.state == VisibilityFullyObscured);