Use inotify to check for changes to the defaults database. Workaround for
[wmaker-crm.git] / src / event.c
1 /* event.c- event loop and handling
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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,
20  *  USA.
21  */
22
23 #include <sys/inotify.h>
24 #include "wconfig.h"
25
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31
32
33
34 #include <X11/Xlib.h>
35 #include <X11/Xutil.h>
36 #ifdef SHAPE
37 # include <X11/extensions/shape.h>
38 #endif
39 #ifdef XDND
40 #include "xdnd.h"
41 #endif
42
43 #ifdef KEEP_XKB_LOCK_STATUS
44 #include <X11/XKBlib.h>
45 #endif /* KEEP_XKB_LOCK_STATUS */
46
47 #include "WindowMaker.h"
48 #include "window.h"
49 #include "actions.h"
50 #include "client.h"
51 #include "funcs.h"
52 #include "keybind.h"
53 #include "application.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "dock.h"
58 #include "framewin.h"
59 #include "properties.h"
60 #include "balloon.h"
61 #include "xinerama.h"
62
63 #ifdef NETWM_HINTS
64 # include "wmspec.h"
65 #endif
66
67 /******** Global Variables **********/
68 extern XContext wWinContext;
69 extern XContext wVEdgeContext;
70
71 extern Cursor wCursor[WCUR_LAST];
72
73 extern WShortKey wKeyBindings[WKBD_LAST];
74 extern int wScreenCount;
75 extern Time LastTimestamp;
76 extern Time LastFocusChange;
77
78 extern WPreferences wPreferences;
79
80 #define MOD_MASK wPreferences.modifier_mask
81
82 extern Atom _XA_WM_COLORMAP_NOTIFY;
83
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;
91
92
93 #ifdef SHAPE
94 extern Bool wShapeSupported;
95 extern int wShapeEventBase;
96 #endif
97
98 #ifdef KEEP_XKB_LOCK_STATUS
99 extern int wXkbEventBase;
100 #endif
101
102 /* special flags */
103 /*extern char WDelayedActionSet;*/
104
105
106 /************ Local stuff ***********/
107
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();
126
127
128 #ifdef SHAPE
129 static void handleShapeNotify();
130 #endif
131
132 /* called from the signal handler */
133 void NotifyDeadProcess(pid_t pid, unsigned char status);
134
135 /* real dead process handler */
136 static void handleDeadProcess(void *foo);
137
138
139 typedef struct DeadProcesses {
140     pid_t               pid;
141     unsigned char       exit_status;
142 } DeadProcesses;
143
144 /* stack of dead processes */
145 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
146 static int deadProcessPtr=0;
147
148
149 typedef struct DeathHandler {
150     WDeathHandler       *callback;
151     pid_t               pid;
152     void                *client_data;
153 } DeathHandler;
154
155 static WMArray *deathHandlers=NULL;
156
157
158
159 WMagicNumber
160 wAddDeathHandler(pid_t pid, WDeathHandler *callback, void *cdata)
161 {
162     DeathHandler *handler;
163
164     handler = malloc(sizeof(DeathHandler));
165     if (!handler)
166         return 0;
167
168     handler->pid = pid;
169     handler->callback = callback;
170     handler->client_data = cdata;
171
172     if (!deathHandlers)
173         deathHandlers = WMCreateArrayWithDestructor(8, wfree);
174
175     WMAddToArray(deathHandlers, handler);
176
177     return handler;
178 }
179
180
181
182 void
183 wDeleteDeathHandler(WMagicNumber id)
184 {
185     DeathHandler *handler=(DeathHandler*)id;
186
187     if (!handler || !deathHandlers)
188         return;
189
190     /* array destructor will call wfree(handler) */
191     WMRemoveFromArray(deathHandlers, handler);
192 }
193
194
195 void
196 DispatchEvent(XEvent *event)
197 {
198     if (deathHandlers)
199         handleDeadProcess(NULL);
200
201     if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
202         WCHANGE_STATE(WSTATE_EXITING);
203         /* received SIGTERM */
204         /*
205          * WMHandleEvent() can't be called from anything
206          * executed inside here, or we can get in a infinite
207          * recursive loop.
208          */
209         Shutdown(WSExitMode);
210
211     } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
212         WCHANGE_STATE(WSTATE_RESTARTING);
213
214         Shutdown(WSRestartPreparationMode);
215         /* received SIGHUP */
216         Restart(NULL, True);
217     } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
218         WCHANGE_STATE(WSTATE_NORMAL);
219         wDefaultsCheckDomains("bla");
220     }
221
222     /* for the case that all that is wanted to be dispatched is
223      * the stuff above */
224     if (!event)
225         return;
226
227     saveTimestamp(event);
228     switch (event->type) {
229     case MapRequest:
230         handleMapRequest(event);
231         break;
232
233     case KeyPress:
234         handleKeyPress(event);
235         break;
236
237     case MotionNotify:
238         handleMotionNotify(event);
239         break;
240
241     case ConfigureRequest:
242         handleConfigureRequest(event);
243         break;
244
245     case DestroyNotify:
246         handleDestroyNotify(event);
247         break;
248
249     case MapNotify:
250         handleMapNotify(event);
251         break;
252
253     case UnmapNotify:
254         handleUnmapNotify(event);
255         break;
256
257     case ButtonPress:
258         handleButtonPress(event);
259         break;
260
261     case Expose:
262         handleExpose(event);
263         break;
264
265     case PropertyNotify:
266         handlePropertyNotify(event);
267         break;
268
269     case EnterNotify:
270         handleEnterNotify(event);
271         break;
272
273     case LeaveNotify:
274         handleLeaveNotify(event);
275         break;
276
277     case ClientMessage:
278         handleClientMessage(event);
279         break;
280
281     case ColormapNotify:
282         handleColormapNotify(event);
283         break;
284
285     case MappingNotify:
286         if (event->xmapping.request == MappingKeyboard
287             || event->xmapping.request == MappingModifier)
288             XRefreshKeyboardMapping(&event->xmapping);
289         break;
290
291     case FocusIn:
292         handleFocusIn(event);
293         break;
294
295     case VisibilityNotify:
296         handleVisibilityNotify(event);
297         break;
298     default:
299         handleExtensions(event);
300         break;
301     }
302 }
303
304 /*
305  *----------------------------------------------------------------------
306  * inotifyHandleEvents-
307  *      Check for inotify events
308  *
309  * Returns:
310  *      After reading events for the given file descriptor (fd) and
311  *     watch descriptor (wd)
312  *
313  * Side effects:
314  *      Calls wDefaultsCheckDomains if config database is updated
315  *----------------------------------------------------------------------
316  */
317
318 /* Allow for 1024 simultanious events */
319 #define BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX)*1024)
320 void inotifyHandleEvents (int fd, int wd)
321 {
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 */
326
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().
332      */
333     eventQLength = read (fd, buff, BUFF_SIZE);
334     
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];
339        
340         /*
341          * see inotify.h for event types.
342          */
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"));
347             close(fd);
348         }
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."));
353             close(fd);
354             wPreferences.flags.noupdates=1;
355         }
356         if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
357             fprintf(stdout,"wmaker: reading config files in defaults database.\n");
358             wDefaultsCheckDomains(NULL);
359         }
360         /* Check for filename (length of name (len) > 0) */
361         /* if (pevent->len) printf ("name=%s\n", pevent->name); */
362       
363          i += sizeof(struct inotify_event) + pevent->len; /* move to next event in the buffer */
364    }
365
366 }  /* inotifyHandleEvents */
367
368 /*
369  *----------------------------------------------------------------------
370  * EventLoop-
371  *      Processes X and internal events indefinitely.
372  *
373  * Returns:
374  *      Never returns
375  *
376  * Side effects:
377  *      The LastTimestamp global variable is updated.
378  *      Calls inotifyGetEvents if defaults database changes.
379  *----------------------------------------------------------------------
380  */
381 void
382 EventLoop()
383 {
384     XEvent event;
385     extern int inotifyFD;
386     extern int inotifyWD;
387     struct timeval time;
388     fd_set rfds;
389     int retVal=0;
390     
391     if (inotifyFD < 0 || inotifyWD < 0)
392         retVal = -1;
393
394     for(;;) {
395
396          WMNextEvent(dpy, &event); /* Blocks here */
397          WMHandleEvent(&event);
398
399          if (retVal != -1 ) {
400             time.tv_sec = 0;
401             time.tv_usec = 0;
402             FD_ZERO (&rfds);
403             FD_SET (inotifyFD, &rfds);
404
405             /* check for available read data from inotify - don't block! */
406             retVal = select (inotifyFD + 1, &rfds, NULL, NULL, &time);
407
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."));
412                 close(inotifyFD);
413                 continue;
414             }
415             if (FD_ISSET (inotifyFD, &rfds))
416                   inotifyHandleEvents(inotifyFD,inotifyWD);
417          }
418     }
419 }
420
421 /*
422  *----------------------------------------------------------------------
423  * ProcessPendingEvents --
424  *      Processes the events that are currently pending (at the time
425  *      this function is called) in the display's queue.
426  *
427  * Returns:
428  *      After the pending events that were present at the function call
429  *      are processed.
430  *
431  * Side effects:
432  *      Many -- whatever handling events may involve.
433  *
434  *----------------------------------------------------------------------
435  */
436 void
437 ProcessPendingEvents()
438 {
439     XEvent event;
440     int count;
441
442     XSync(dpy, False);
443
444     /* Take a snapshot of the event count in the queue */
445     count = XPending(dpy);
446
447     while (count>0 && XPending(dpy)) {
448         WMNextEvent(dpy, &event);
449         WMHandleEvent(&event);
450         count--;
451     }
452 }
453
454
455 Bool
456 IsDoubleClick(WScreen *scr, XEvent *event)
457 {
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)) {
462
463         scr->flags.next_click_is_not_double = 1;
464         scr->last_click_time = 0;
465         scr->last_click_window = event->xbutton.window;
466
467         return True;
468     }
469     return False;
470 }
471
472
473 void
474 NotifyDeadProcess(pid_t pid, unsigned char status)
475 {
476     if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
477         wwarning("stack overflow: too many dead processes");
478         return;
479     }
480     /* stack the process to be handled later,
481      * as this is called from the signal handler */
482     deadProcesses[deadProcessPtr].pid = pid;
483     deadProcesses[deadProcessPtr].exit_status = status;
484     deadProcessPtr++;
485 }
486
487
488 static void
489 handleDeadProcess(void *foo)
490 {
491     DeathHandler *tmp;
492     int i;
493
494     for (i=0; i<deadProcessPtr; i++) {
495         wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
496     }
497
498     if (!deathHandlers) {
499         deadProcessPtr=0;
500         return;
501     }
502
503     /* get the pids on the queue and call handlers */
504     while (deadProcessPtr>0) {
505         deadProcessPtr--;
506
507         for (i = WMGetArrayItemCount(deathHandlers)-1; i >= 0; i--) {
508             tmp = WMGetFromArray(deathHandlers, i);
509             if (!tmp)
510                 continue;
511
512             if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
513                 (*tmp->callback)(tmp->pid,
514                                  deadProcesses[deadProcessPtr].exit_status,
515                                  tmp->client_data);
516                 wDeleteDeathHandler(tmp);
517             }
518         }
519     }
520 }
521
522
523 static void
524 saveTimestamp(XEvent *event)
525 {
526     /*
527      * Never save CurrentTime as LastTimestamp because CurrentTime
528      * it's not a real timestamp (it's the 0L constant)
529      */
530
531     switch (event->type) {
532     case ButtonRelease:
533     case ButtonPress:
534         LastTimestamp = event->xbutton.time;
535         break;
536     case KeyPress:
537     case KeyRelease:
538         LastTimestamp = event->xkey.time;
539         break;
540     case MotionNotify:
541         LastTimestamp = event->xmotion.time;
542         break;
543     case PropertyNotify:
544         LastTimestamp = event->xproperty.time;
545         break;
546     case EnterNotify:
547     case LeaveNotify:
548         LastTimestamp = event->xcrossing.time;
549         break;
550     case SelectionClear:
551         LastTimestamp = event->xselectionclear.time;
552         break;
553     case SelectionRequest:
554         LastTimestamp = event->xselectionrequest.time;
555         break;
556     case SelectionNotify:
557         LastTimestamp = event->xselection.time;
558 #ifdef XDND
559         wXDNDProcessSelection(event);
560 #endif
561         break;
562     }
563 }
564
565
566 static int
567 matchWindow(void *item, void *cdata)
568 {
569     return (((WFakeGroupLeader*)item)->origLeader == (Window)cdata);
570 }
571
572
573 static void
574 handleExtensions(XEvent *event)
575 {
576 #ifdef KEEP_XKB_LOCK_STATUS
577     XkbEvent *xkbevent;
578     xkbevent = (XkbEvent *)event;
579 #endif /*KEEP_XKB_LOCK_STATUS*/
580 #ifdef SHAPE
581     if (wShapeSupported && event->type == (wShapeEventBase+ShapeNotify)) {
582         handleShapeNotify(event);
583     }
584 #endif
585 #ifdef KEEP_XKB_LOCK_STATUS
586     if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)){
587         handleXkbIndicatorStateNotify(event);
588     }
589 #endif /*KEEP_XKB_LOCK_STATUS*/
590 }
591
592
593 static void
594 handleMapRequest(XEvent *ev)
595 {
596     WWindow *wwin;
597     WScreen *scr = NULL;
598     Window window = ev->xmaprequest.window;
599
600 #ifdef DEBUG
601     printf("got map request for %x\n", (unsigned)window);
602 #endif
603     if ((wwin = wWindowFor(window))) {
604         if (wwin->flags.shaded) {
605             wUnshadeWindow(wwin);
606         }
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 */
613             if (wapp) {
614                 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
615             }
616             wUnhideApplication(wapp, False, False);
617         }
618         return;
619     }
620
621     scr = wScreenForRootWindow(ev->xmaprequest.parent);
622
623     wwin = wManageWindow(scr, window);
624
625     /*
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).
630      */
631     if (scr->last_dock) {
632         if (wwin && wwin->main_window!=None && wwin->main_window!=window)
633             wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
634         else
635             wDockTrackWindowLaunch(scr->last_dock, window);
636     }
637
638     if (wwin) {
639         wClientSetState(wwin, NormalState, None);
640         if (wwin->flags.maximized) {
641             wMaximizeWindow(wwin, wwin->flags.maximized);
642         }
643         if (wwin->flags.shaded) {
644             wwin->flags.shaded = 0;
645             wwin->flags.skip_next_animation = 1;
646             wShadeWindow(wwin);
647         }
648         if (wwin->flags.miniaturized) {
649             wwin->flags.miniaturized = 0;
650             wwin->flags.skip_next_animation = 1;
651             wIconifyWindow(wwin);
652         }
653         if (wwin->flags.fullscreen) {
654             wwin->flags.fullscreen = 0;
655             wFullscreenWindow(wwin);
656         }
657         if (wwin->flags.hidden) {
658             WApplication *wapp = wApplicationOf(wwin->main_window);
659
660             wwin->flags.hidden = 0;
661             wwin->flags.skip_next_animation = 1;
662             if (wapp) {
663                 wHideApplication(wapp);
664             }
665         }
666     }
667 }
668
669
670 static void
671 handleDestroyNotify(XEvent *event)
672 {
673     WWindow *wwin;
674     WApplication *app;
675     Window window = event->xdestroywindow.window;
676     WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
677     int index;
678
679 #ifdef DEBUG
680     printf("got destroy notify\n");
681 #endif
682     wwin = wWindowFor(window);
683     if (wwin) {
684         wUnmanageWindow(wwin, False, True);
685     }
686
687     if (scr != NULL) {
688         while ((index = WMFindInArray(scr->fakeGroupLeaders, matchWindow,
689                                       (void*)window)) != WANotFound) {
690             WFakeGroupLeader *fPtr;
691
692             fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
693             if (fPtr->retainCount > 0) {
694                 fPtr->retainCount--;
695                 if (fPtr->retainCount==0 && fPtr->leader!=None) {
696                     XDestroyWindow(dpy, fPtr->leader);
697                     fPtr->leader = None;
698                     XFlush(dpy);
699                 }
700             }
701             fPtr->origLeader = None;
702         }
703     }
704
705     app = wApplicationOf(window);
706     if (app) {
707         if (window == app->main_window) {
708             app->refcount = 0;
709             wwin = app->main_window_desc->screen_ptr->focused_window;
710             while (wwin) {
711                 if (wwin->main_window == window) {
712                     wwin->main_window = None;
713                 }
714                 wwin = wwin->prev;
715             }
716         }
717         wApplicationDestroy(app);
718     }
719 }
720
721
722
723 static void
724 handleExpose(XEvent *event)
725 {
726     WObjDescriptor *desc;
727     XEvent ev;
728
729 #ifdef DEBUG
730     printf("got expose\n");
731 #endif
732     while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev));
733
734     if (XFindContext(dpy, event->xexpose.window, wWinContext,
735                      (XPointer *)&desc)==XCNOENT) {
736         return;
737     }
738
739     if (desc->handle_expose) {
740         (*desc->handle_expose)(desc, event);
741     }
742 }
743
744 static void
745 executeButtonAction(WScreen *scr, XEvent *event, int action)
746 {
747     switch(action) {
748     case WA_SELECT_WINDOWS:
749         wUnselectWindows(scr);
750         wSelectWindows(scr, event);
751         break;
752     case WA_OPEN_APPMENU:
753         OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
754         /* ugly hack */
755         if (scr->root_menu) {
756             if (scr->root_menu->brother->flags.mapped)
757                 event->xbutton.window = scr->root_menu->brother->frame->core->window;
758             else
759                 event->xbutton.window = scr->root_menu->frame->core->window;
760         }
761         break;
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;
767             else
768                 event->xbutton.window = scr->switch_menu->frame->core->window;
769         }
770         break;
771     default:
772         break;
773     }
774 }
775
776
777 /* bindable */
778 static void
779 handleButtonPress(XEvent *event)
780 {
781     WObjDescriptor *desc;
782     WScreen *scr;
783
784 #ifdef DEBUG
785     printf("got button press\n");
786 #endif
787     scr = wScreenForRootWindow(event->xbutton.root);
788
789 #ifdef BALLOON_TEXT
790     wBalloonHide(scr);
791 #endif
792
793
794 #ifndef LITE
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);
811         }
812     }
813 #endif /* !LITE */
814
815     desc = NULL;
816     if (XFindContext(dpy, event->xbutton.subwindow, wWinContext,
817                      (XPointer *)&desc)==XCNOENT) {
818         if (XFindContext(dpy, event->xbutton.window, wWinContext,
819                          (XPointer *)&desc)==XCNOENT) {
820             return;
821         }
822     }
823
824     if (desc->parent_type == WCLASS_WINDOW) {
825         XSync(dpy, 0);
826
827         if (event->xbutton.state & MOD_MASK) {
828             XAllowEvents(dpy, AsyncPointer, CurrentTime);
829         }
830
831         /*      if (wPreferences.focus_mode == WKF_CLICK) {*/
832         if (wPreferences.ignore_focus_click) {
833             XAllowEvents(dpy, AsyncPointer, CurrentTime);
834         }
835         XAllowEvents(dpy, ReplayPointer, CurrentTime);
836         /*      }*/
837         XSync(dpy, 0);
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) {
842             XSync(dpy, 0);
843             XAllowEvents(dpy, AsyncPointer, CurrentTime);
844             XSync(dpy, 0);
845         }
846     }
847
848     if (desc->handle_mousedown!=NULL) {
849         (*desc->handle_mousedown)(desc, event);
850     }
851
852     /* save double-click information */
853     if (scr->flags.next_click_is_not_double) {
854         scr->flags.next_click_is_not_double = 0;
855     } else {
856         scr->last_click_time = event->xbutton.time;
857         scr->last_click_button = event->xbutton.button;
858         scr->last_click_window = event->xbutton.window;
859     }
860 }
861
862
863 static void
864 handleMapNotify(XEvent *event)
865 {
866     WWindow *wwin;
867 #ifdef DEBUG
868     printf("got map\n");
869 #endif
870     wwin = wWindowFor(event->xmap.event);
871     if (wwin && wwin->client_win == event->xmap.event) {
872         if (wwin->flags.miniaturized) {
873             wDeiconifyWindow(wwin);
874         } else {
875             XGrabServer(dpy);
876             wWindowMap(wwin);
877             wClientSetState(wwin, NormalState, None);
878             XUngrabServer(dpy);
879         }
880     }
881 }
882
883
884 static void
885 handleUnmapNotify(XEvent *event)
886 {
887     WWindow *wwin;
888     XEvent ev;
889     Bool withdraw = False;
890 #ifdef DEBUG
891     printf("got unmap\n");
892 #endif
893     /* only process windows with StructureNotify selected
894      * (ignore SubstructureNotify) */
895     wwin = wWindowFor(event->xunmap.window);
896     if (!wwin)
897         return;
898
899     /* whether the event is a Withdrawal request */
900     if (event->xunmap.event == wwin->screen_ptr->root_win
901         && event->xunmap.send_event)
902         withdraw = True;
903
904     if (wwin->client_win != event->xunmap.event && !withdraw)
905         return;
906
907     if (!wwin->flags.mapped && !withdraw
908         && wwin->frame->workspace == wwin->screen_ptr->current_workspace
909         && !wwin->flags.miniaturized && !wwin->flags.hidden)
910         return;
911
912     XGrabServer(dpy);
913     XUnmapWindow(dpy, wwin->frame->core->window);
914     wwin->flags.mapped = 0;
915     XSync(dpy, 0);
916     /* check if the window was destroyed */
917     if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify,&ev)) {
918         DispatchEvent(&ev);
919     } else {
920         Bool reparented = False;
921
922         if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
923             reparented = True;
924
925         /* withdraw window */
926         wwin->flags.mapped = 0;
927         if (!reparented)
928             wClientSetState(wwin, WithdrawnState, None);
929
930         /* if the window was reparented, do not reparent it back to the
931          * root window */
932         wUnmanageWindow(wwin, !reparented, False);
933     }
934     XUngrabServer(dpy);
935 }
936
937
938 static void
939 handleConfigureRequest(XEvent *event)
940 {
941     WWindow *wwin;
942 #ifdef DEBUG
943     printf("got configure request\n");
944 #endif
945     if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
946         /*
947          * Configure request for unmapped window
948          */
949         wClientConfigure(NULL, &(event->xconfigurerequest));
950     } else {
951         wClientConfigure(wwin, &(event->xconfigurerequest));
952     }
953 }
954
955
956 static void
957 handlePropertyNotify(XEvent *event)
958 {
959     WWindow *wwin;
960     WApplication *wapp;
961     Window jr;
962     int ji;
963     unsigned int ju;
964     WScreen *scr;
965 #ifdef DEBUG
966     printf("got property notify\n");
967 #endif
968     if ((wwin=wWindowFor(event->xproperty.window))) {
969         if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
970                           &ju, &ju, &ju, &ju)) {
971             return;
972         }
973         wClientCheckProperty(wwin, &event->xproperty);
974     }
975     wapp = wApplicationOf(event->xproperty.window);
976     if (wapp) {
977         wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
978     }
979
980     scr = wScreenForWindow(event->xproperty.window);
981 }
982
983
984 static void
985 handleClientMessage(XEvent *event)
986 {
987     WWindow *wwin;
988     WObjDescriptor *desc;
989 #ifdef DEBUG
990     printf("got client message\n");
991 #endif
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) {
996
997         wwin = wWindowFor(event->xclient.window);
998         if (!wwin) return;
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);
1004
1005         if (!scr)
1006             return;
1007
1008         if (event->xclient.data.l[1] == 1) {   /* starting */
1009             wColormapAllowClientInstallation(scr, True);
1010         } else {                       /* stopping */
1011             wColormapAllowClientInstallation(scr, False);
1012         }
1013     } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
1014
1015         wDefaultsCheckDomains("bla");
1016
1017     } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
1018         WApplication *wapp;
1019         int done=0;
1020         wapp = wApplicationOf(event->xclient.window);
1021         if (wapp) {
1022             switch (event->xclient.data.l[0]) {
1023             case WMFHideOtherApplications:
1024                 wHideOtherApplications(wapp->main_window_desc);
1025                 done = 1;
1026                 break;
1027
1028             case WMFHideApplication:
1029                 wHideApplication(wapp);
1030                 done = 1;
1031                 break;
1032             }
1033         }
1034         if (!done) {
1035             wwin = wWindowFor(event->xclient.window);
1036             if (wwin) {
1037                 switch (event->xclient.data.l[0]) {
1038                 case WMFHideOtherApplications:
1039                     wHideOtherApplications(wwin);
1040                     break;
1041
1042                 case WMFHideApplication:
1043                     wHideApplication(wApplicationOf(wwin->main_window));
1044                     break;
1045                 }
1046             }
1047         }
1048     } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1049         wwin = wWindowFor(event->xclient.window);
1050         if (!wwin) return;
1051         switch (event->xclient.data.l[0]) {
1052         case GSWindowLevelAttr:
1053             {
1054                 int level = (int)event->xclient.data.l[1];
1055
1056                 if (WINDOW_LEVEL(wwin) != level) {
1057                     ChangeStackingLevel(wwin->frame->core, level);
1058                 }
1059             }
1060             break;
1061         }
1062     } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1063         wwin = wWindowFor(event->xclient.window);
1064         if (!wwin) return;
1065         switch (event->xclient.data.l[0]) {
1066         case WMTitleBarNormal:
1067             wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1068             break;
1069         case WMTitleBarMain:
1070             wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1071             break;
1072         case WMTitleBarKey:
1073             wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1074             break;
1075         }
1076 #ifdef NETWM_HINTS
1077     } else if (wNETWMProcessClientMessage(&event->xclient)) {
1078         /* do nothing */
1079 #endif
1080 #ifdef XDND
1081     } else if (wXDNDProcessClientMessage(&event->xclient)) {
1082         /* do nothing */
1083 #endif /* XDND */
1084     } else {
1085         /*
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.
1089          */
1090         if (XFindContext(dpy, event->xbutton.window, wWinContext,
1091                          (XPointer *)&desc)!=XCNOENT) {
1092             struct WIcon *icon=NULL;
1093
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;
1099             }
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,
1104                                event);
1105                 }
1106             }
1107         }
1108     }
1109 }
1110
1111
1112 static void
1113 raiseWindow(WScreen *scr)
1114 {
1115     WWindow *wwin;
1116
1117     scr->autoRaiseTimer = NULL;
1118
1119     wwin = wWindowFor(scr->autoRaiseWindow);
1120     if (!wwin)
1121         return;
1122
1123     if (!wwin->flags.destroyed && wwin->flags.focused) {
1124         wRaiseFrame(wwin->frame->core);
1125         /* this is needed or a race condition will occur */
1126         XSync(dpy, False);
1127     }
1128 }
1129
1130
1131 static void
1132 handleEnterNotify(XEvent *event)
1133 {
1134     WWindow *wwin;
1135     WObjDescriptor *desc = NULL;
1136 #ifdef VIRTUAL_DESKTOP
1137     void (*vdHandler)(XEvent * event);
1138 #endif
1139     XEvent ev;
1140     WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1141 #ifdef DEBUG
1142     printf("got enter notify\n");
1143 #endif
1144
1145 #ifdef VIRTUAL_DESKTOP
1146     if (XFindContext(dpy, event->xcrossing.window, wVEdgeContext,
1147                      (XPointer *)&vdHandler)!=XCNOENT) {
1148         (*vdHandler)(event);
1149     }
1150 #endif
1151
1152     if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
1153                                &ev)) {
1154         /* already left the window... */
1155         saveTimestamp(&ev);
1156         if (ev.xcrossing.mode==event->xcrossing.mode
1157             && ev.xcrossing.detail==event->xcrossing.detail) {
1158             return;
1159         }
1160     }
1161
1162     if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1163                      (XPointer *)&desc)!=XCNOENT) {
1164         if(desc->handle_enternotify)
1165             (*desc->handle_enternotify)(desc, event);
1166     }
1167
1168     /* enter to window */
1169     wwin = wWindowFor(event->xcrossing.window);
1170     if (!wwin) {
1171         if (wPreferences.colormap_mode==WCM_POINTER) {
1172             wColormapInstallForWindow(scr, NULL);
1173         }
1174         if (scr->autoRaiseTimer
1175             && event->xcrossing.root==event->xcrossing.window) {
1176             WMDeleteTimerHandler(scr->autoRaiseTimer);
1177             scr->autoRaiseTimer = NULL;
1178         }
1179     } else {
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
1184          *
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) {
1190
1191             if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1192                 wSetFocusTo(scr, wwin);
1193
1194             if (scr->autoRaiseTimer)
1195                 WMDeleteTimerHandler(scr->autoRaiseTimer);
1196             scr->autoRaiseTimer = NULL;
1197
1198             if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1199                 scr->autoRaiseWindow = wwin->frame->core->window;
1200                 scr->autoRaiseTimer
1201                     = WMAddTimerHandler(wPreferences.raise_delay,
1202                                         (WMCallback*)raiseWindow, scr);
1203             }
1204         }
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);
1210             else
1211                 wColormapInstallForWindow(scr, NULL);
1212         }
1213     }
1214
1215     /* a little kluge to hide the clip balloon */
1216     if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1217         if (!desc) {
1218             XUnmapWindow(dpy, scr->clip_balloon);
1219             scr->flags.clip_balloon_mapped = 0;
1220         } else {
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;
1225             }
1226         }
1227     }
1228
1229     if (event->xcrossing.window == event->xcrossing.root
1230         && event->xcrossing.detail == NotifyNormal
1231         && event->xcrossing.detail != NotifyInferior
1232         && wPreferences.focus_mode != WKF_CLICK) {
1233
1234         wSetFocusTo(scr, scr->focused_window);
1235     }
1236
1237 #ifdef BALLOON_TEXT
1238     wBalloonEnteredObject(scr, desc);
1239 #endif
1240 }
1241
1242
1243 static void
1244 handleLeaveNotify(XEvent *event)
1245 {
1246     WObjDescriptor *desc = NULL;
1247
1248     if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1249                      (XPointer *)&desc)!=XCNOENT) {
1250         if(desc->handle_leavenotify)
1251             (*desc->handle_leavenotify)(desc, event);
1252     }
1253 }
1254
1255
1256 #ifdef SHAPE
1257 static void
1258 handleShapeNotify(XEvent *event)
1259 {
1260     XShapeEvent *shev = (XShapeEvent*)event;
1261     WWindow *wwin;
1262     XEvent ev;
1263 #ifdef DEBUG
1264     printf("got shape notify\n");
1265 #endif
1266     while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1267         XShapeEvent *sev = (XShapeEvent*)&ev;
1268
1269         if (sev->kind == ShapeBounding) {
1270             if (sev->shaped == shev->shaped) {
1271                 *shev = *sev;
1272             } else {
1273                 XPutBackEvent(dpy, &ev);
1274                 break;
1275             }
1276         }
1277     }
1278
1279     wwin = wWindowFor(shev->window);
1280     if (!wwin || shev->kind != ShapeBounding)
1281         return;
1282
1283     if (!shev->shaped && wwin->flags.shaped) {
1284
1285         wwin->flags.shaped = 0;
1286         wWindowClearShape(wwin);
1287
1288     } else if (shev->shaped) {
1289
1290         wwin->flags.shaped = 1;
1291         wWindowSetShape(wwin);
1292     }
1293 }
1294 #endif /* SHAPE */
1295
1296 #ifdef KEEP_XKB_LOCK_STATUS
1297 /* please help ]d if you know what to do */
1298 handleXkbIndicatorStateNotify(XEvent *event)
1299 {
1300     WWindow *wwin;
1301     WScreen *scr;
1302     XkbStateRec staterec;
1303     int i;
1304
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;
1313             }
1314 #ifdef XKB_BUTTON_HINT
1315             if (wwin->frame->titlebar) {
1316                 wFrameWindowPaint(wwin->frame);
1317             }
1318 #endif
1319         }
1320     }
1321 }
1322 #endif /*KEEP_XKB_LOCK_STATUS*/
1323
1324 static void
1325 handleColormapNotify(XEvent *event)
1326 {
1327     WWindow *wwin;
1328     WScreen *scr;
1329     Bool reinstall = False;
1330
1331     wwin = wWindowFor(event->xcolormap.window);
1332     if (!wwin)
1333         return;
1334
1335     scr = wwin->screen_ptr;
1336
1337     do {
1338         if (wwin) {
1339             if (event->xcolormap.new) {
1340                 XWindowAttributes attr;
1341
1342                 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1343
1344                 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1345                     scr->current_colormap = attr.colormap;
1346
1347                 reinstall = True;
1348             } else if (event->xcolormap.state == ColormapUninstalled &&
1349                        scr->current_colormap == event->xcolormap.colormap) {
1350
1351                 /* some bastard app (like XV) removed our colormap */
1352                 /*
1353                  * can't enforce or things like xscreensaver wont work
1354                  * reinstall = True;
1355                  */
1356             } else if (event->xcolormap.state == ColormapInstalled &&
1357                        scr->current_colormap == event->xcolormap.colormap) {
1358
1359                 /* someone has put our colormap back */
1360                 reinstall = False;
1361             }
1362         }
1363     } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1364              && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1365
1366     if (reinstall && scr->current_colormap!=None) {
1367         if (!scr->flags.colormap_stuff_blocked)
1368             XInstallColormap(dpy, scr->current_colormap);
1369     }
1370 }
1371
1372
1373
1374 static void
1375 handleFocusIn(XEvent *event)
1376 {
1377     WWindow *wwin;
1378
1379     /*
1380      * For applications that like stealing the focus.
1381      */
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) {
1387         return;
1388     }
1389
1390     wwin = wWindowFor(event->xfocus.window);
1391     if (wwin && !wwin->flags.focused) {
1392         if (wwin->flags.mapped)
1393             wSetFocusTo(wwin->screen_ptr, wwin);
1394         else
1395             wSetFocusTo(wwin->screen_ptr, NULL);
1396     } else if (!wwin) {
1397         WScreen *scr = wScreenForWindow(event->xfocus.window);
1398         if (scr)
1399             wSetFocusTo(scr, NULL);
1400     }
1401 }
1402
1403
1404 static WWindow*
1405 windowUnderPointer(WScreen *scr)
1406 {
1407     unsigned int mask;
1408     int foo;
1409     Window bar, win;
1410
1411     if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
1412                       &mask))
1413         return wWindowFor(win);
1414     return NULL;
1415 }
1416
1417
1418 static int CheckFullScreenWindowFocused(WScreen *scr)
1419 {
1420   if (scr->focused_window && scr->focused_window->flags.fullscreen)
1421       return 1;
1422   else
1423       return 0;
1424 }
1425
1426
1427 static void
1428 handleKeyPress(XEvent *event)
1429 {
1430     WScreen *scr = wScreenForRootWindow(event->xkey.root);
1431     WWindow *wwin = scr->focused_window;
1432     int i;
1433     int modifiers;
1434     int command=-1, index;
1435 #ifdef KEEP_XKB_LOCK_STATUS
1436     XkbStateRec staterec;
1437 #endif /*KEEP_XKB_LOCK_STATUS*/
1438
1439     /* ignore CapsLock */
1440     modifiers = event->xkey.state & ValidModMask;
1441
1442     for (i=0; i<WKBD_LAST; i++) {
1443         if (wKeyBindings[i].keycode==0)
1444             continue;
1445
1446         if (wKeyBindings[i].keycode==event->xkey.keycode
1447             && (/*wKeyBindings[i].modifier==0
1448             ||*/ wKeyBindings[i].modifier==modifiers)) {
1449             command = i;
1450             break;
1451         }
1452     }
1453
1454
1455     if (command < 0) {
1456 #ifdef LITE
1457         {
1458 #if 0
1459         }
1460 #endif
1461 #else
1462         if (!wRootMenuPerformShortcut(event)) {
1463 #endif
1464             static int dontLoop = 0;
1465
1466             if (dontLoop > 10) {
1467                 wwarning("problem with key event processing code");
1468                 return;
1469             }
1470             dontLoop++;
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);
1478             }
1479             dontLoop--;
1480         }
1481         return;
1482     }
1483
1484 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1485 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1486
1487     switch (command) {
1488 #ifndef LITE
1489     case WKBD_ROOTMENU:
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);
1494         }
1495         break;
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);
1500         }
1501         break;
1502 #endif /* !LITE */
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);
1507         break;
1508     case WKBD_MINIATURIZE:
1509         if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1510             && !WFLAGP(wwin, no_miniaturizable)) {
1511             CloseWindowMenu(scr);
1512
1513             if (wwin->protocols.MINIATURIZE_WINDOW)
1514                 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
1515                                     event->xbutton.time);
1516             else {
1517                 wIconifyWindow(wwin);
1518             }
1519         }
1520         break;
1521     case WKBD_HIDE:
1522         if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1523             WApplication *wapp = wApplicationOf(wwin->main_window);
1524             CloseWindowMenu(scr);
1525
1526             if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1527                 wHideApplication(wapp);
1528             }
1529         }
1530         break;
1531     case WKBD_HIDE_OTHERS:
1532         if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1533             CloseWindowMenu(scr);
1534
1535             wHideOtherApplications(wwin);
1536         }
1537         break;
1538     case WKBD_MAXIMIZE:
1539         if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1540             int newdir = (MAX_VERTICAL|MAX_HORIZONTAL);
1541
1542             CloseWindowMenu(scr);
1543
1544             if (wwin->flags.maximized == newdir) {
1545                 wUnmaximizeWindow(wwin);
1546             } else {
1547                 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1548             }
1549         }
1550         break;
1551     case WKBD_VMAXIMIZE:
1552         if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1553             int newdir = (MAX_VERTICAL ^ wwin->flags.maximized);
1554
1555             CloseWindowMenu(scr);
1556
1557             if (newdir) {
1558                 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1559             } else {
1560                 wUnmaximizeWindow(wwin);
1561             }
1562         }
1563         break;
1564     case WKBD_HMAXIMIZE:
1565         if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1566             int newdir = (MAX_HORIZONTAL ^ wwin->flags.maximized);
1567
1568             CloseWindowMenu(scr);
1569
1570             if (newdir) {
1571                 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1572             } else {
1573                 wUnmaximizeWindow(wwin);
1574             }
1575         }
1576         break;
1577     case WKBD_RAISE:
1578         if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1579             CloseWindowMenu(scr);
1580
1581             wRaiseFrame(wwin->frame->core);
1582         }
1583         break;
1584     case WKBD_LOWER:
1585         if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1586             CloseWindowMenu(scr);
1587
1588             wLowerFrame(wwin->frame->core);
1589         }
1590         break;
1591     case WKBD_RAISELOWER:
1592         /* raise or lower the window under the pointer, not the
1593          * focused one
1594          */
1595         wwin = windowUnderPointer(scr);
1596         if (wwin)
1597             wRaiseLowerFrame(wwin->frame->core);
1598         break;
1599     case WKBD_SHADE:
1600         if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1601             if (wwin->flags.shaded)
1602                 wUnshadeWindow(wwin);
1603             else
1604                 wShadeWindow(wwin);
1605         }
1606         break;
1607     case WKBD_MOVERESIZE:
1608         if (ISMAPPED(wwin) && ISFOCUSED(wwin) &&
1609             (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1610             CloseWindowMenu(scr);
1611
1612             wKeyboardMoveResizeWindow(wwin);
1613         }
1614         break;
1615     case WKBD_CLOSE:
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,
1620                                     event->xkey.time);
1621         }
1622         break;
1623     case WKBD_SELECT:
1624         if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1625             wSelectWindow(wwin, !wwin->flags.selected);
1626         }
1627         break;
1628     case WKBD_FOCUSNEXT:
1629         StartWindozeCycle(wwin, event, True);
1630         break;
1631
1632     case WKBD_FOCUSPREV:
1633         StartWindozeCycle(wwin, event, False);
1634         break;
1635
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);\
1641     break
1642 #else
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);\
1647     break
1648 #endif
1649         GOTOWORKS(1);
1650         GOTOWORKS(2);
1651         GOTOWORKS(3);
1652         GOTOWORKS(4);
1653         GOTOWORKS(5);
1654         GOTOWORKS(6);
1655         GOTOWORKS(7);
1656         GOTOWORKS(8);
1657         GOTOWORKS(9);
1658         GOTOWORKS(10);
1659 #undef GOTOWORKS
1660     case WKBD_NEXTWORKSPACE:
1661         wWorkspaceRelativeChange(scr, 1);
1662         break;
1663     case WKBD_PREVWORKSPACE:
1664         wWorkspaceRelativeChange(scr, -1);
1665         break;
1666     case WKBD_WINDOW1:
1667     case WKBD_WINDOW2:
1668     case WKBD_WINDOW3:
1669     case WKBD_WINDOW4:
1670     case WKBD_WINDOW5:
1671     case WKBD_WINDOW6:
1672     case WKBD_WINDOW7:
1673     case WKBD_WINDOW8:
1674     case WKBD_WINDOW9:
1675     case WKBD_WINDOW10:
1676
1677         index = command-WKBD_WINDOW1;
1678
1679         if (scr->shortcutWindows[index]) {
1680             WMArray *list = scr->shortcutWindows[index];
1681             int cw;
1682             int count = WMGetArrayItemCount(list);
1683             WWindow *twin;
1684             WMArrayIterator iter;
1685             WWindow *wwin;
1686
1687             wUnselectWindows(scr);
1688             cw = scr->current_workspace;
1689
1690             WM_ETARETI_ARRAY(list, wwin, iter) {
1691                 if (count > 1)
1692                     wWindowChangeWorkspace(wwin, cw);
1693
1694                 wMakeWindowVisible(wwin);
1695
1696                 if (count > 1)
1697                     wSelectWindow(wwin, True);
1698             }
1699
1700             /* rotate the order of windows, to create a cycling effect */
1701             twin = WMGetFromArray(list, 0);
1702             WMDeleteFromArray(list, 0);
1703             WMAddToArray(list, twin);
1704
1705         } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1706             if (scr->shortcutWindows[index]) {
1707                 WMFreeArray(scr->shortcutWindows[index]);
1708                 scr->shortcutWindows[index] = NULL;
1709             }
1710
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);*/
1716             } else {
1717                 scr->shortcutWindows[index] = WMCreateArray(4);
1718                 WMAddToArray(scr->shortcutWindows[index], wwin);
1719             }
1720
1721             wSelectWindow(wwin, !wwin->flags.selected);
1722             XFlush(dpy);
1723             wusleep(3000);
1724             wSelectWindow(wwin, !wwin->flags.selected);
1725             XFlush(dpy);
1726
1727         } else if (scr->selected_windows
1728                    && WMGetArrayItemCount(scr->selected_windows)) {
1729
1730             if (wwin->flags.selected && scr->selected_windows) {
1731                 if (scr->shortcutWindows[index]) {
1732                     WMFreeArray(scr->shortcutWindows[index]);
1733                 }
1734                 scr->shortcutWindows[index] =
1735                     WMDuplicateArray(scr->selected_windows);
1736             }
1737         }
1738
1739         break;
1740
1741     case WKBD_SWITCH_SCREEN:
1742         if (wScreenCount > 1) {
1743             WScreen *scr2;
1744             int i;
1745
1746             /* find index of this screen */
1747             for (i = 0; i < wScreenCount; i++) {
1748                 if (wScreenWithNumber(i) == scr)
1749                     break;
1750             }
1751             i++;
1752             if (i >= wScreenCount) {
1753                 i = 0;
1754             }
1755             scr2 = wScreenWithNumber(i);
1756
1757             if (scr2) {
1758                 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1759                              scr2->scr_width/2, scr2->scr_height/2);
1760             }
1761         }
1762         break;
1763
1764     case WKBD_NEXTWSLAYER:
1765     case WKBD_PREVWSLAYER:
1766         {
1767             int row, column;
1768
1769             row = scr->current_workspace/10;
1770             column = scr->current_workspace%10;
1771
1772             if (command==WKBD_NEXTWSLAYER) {
1773                 if ((row+1)*10 < scr->workspace_count)
1774                     wWorkspaceChange(scr, column+(row+1)*10);
1775             } else {
1776                 if (row > 0)
1777                     wWorkspaceChange(scr, column+(row-1)*10);
1778             }
1779         }
1780         break;
1781     case WKBD_CLIPLOWER:
1782         if (!wPreferences.flags.noclip)
1783             wDockLower(scr->workspaces[scr->current_workspace]->clip);
1784         break;
1785     case WKBD_CLIPRAISE:
1786         if (!wPreferences.flags.noclip)
1787             wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1788         break;
1789     case WKBD_CLIPRAISELOWER:
1790         if (!wPreferences.flags.noclip)
1791             wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1792         break;
1793 #ifdef KEEP_XKB_LOCK_STATUS
1794     case WKBD_TOGGLE:
1795         if(wPreferences.modelock) {
1796             /*toggle*/
1797             wwin = scr->focused_window;
1798
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);
1803
1804                 wwin->frame->languagemode = wwin->frame->last_languagemode;
1805                 wwin->frame->last_languagemode = staterec.group;
1806                 XkbLockGroup(dpy,XkbUseCoreKbd, wwin->frame->languagemode);
1807
1808             }
1809         }
1810         break;
1811 #endif /* KEEP_XKB_LOCK_STATUS */
1812 #ifdef VIRTUAL_DESKTOP
1813     case WKBD_VDESK_LEFT:
1814         wWorkspaceKeyboardMoveDesktop(scr, VEC_LEFT);
1815         break;
1816
1817     case WKBD_VDESK_RIGHT:
1818         wWorkspaceKeyboardMoveDesktop(scr, VEC_RIGHT);
1819         break;
1820
1821     case WKBD_VDESK_UP:
1822         wWorkspaceKeyboardMoveDesktop(scr, VEC_UP);
1823         break;
1824
1825     case WKBD_VDESK_DOWN:
1826         wWorkspaceKeyboardMoveDesktop(scr, VEC_DOWN);
1827         break;
1828 #endif
1829
1830     }
1831 }
1832
1833
1834 static void
1835 handleMotionNotify(XEvent *event)
1836 {
1837     WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1838
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));
1842
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)) {
1848             WMenu *menu;
1849 #ifdef DEBUG
1850             printf("pointer at screen edge\n");
1851 #endif
1852             menu = wMenuUnderPointer(scr);
1853             if (menu!=NULL)
1854                 wMenuScroll(menu, event);
1855         }
1856     }
1857 }
1858
1859
1860 static void
1861 handleVisibilityNotify(XEvent *event)
1862 {
1863     WWindow *wwin;
1864
1865     wwin = wWindowFor(event->xvisibility.window);
1866     if (!wwin)
1867         return;
1868     wwin->flags.obscured =
1869         (event->xvisibility.state == VisibilityFullyObscured);
1870 }
1871
1872