Trivial cleanup
[wmaker-crm.git] / src / event.c
blob53e36d92c3a7cf62cc78c0e4527e48bb945b1c80
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,
20 * USA.
23 #include <sys/inotify.h>
24 #include "wconfig.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
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
43 #ifdef KEEP_XKB_LOCK_STATUS
44 #include <X11/XKBlib.h>
45 #endif /* KEEP_XKB_LOCK_STATUS */
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"
63 #ifdef NETWM_HINTS
64 # include "wmspec.h"
65 #endif
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;
93 #ifdef SHAPE
94 extern Bool wShapeSupported;
95 extern int wShapeEventBase;
96 #endif
98 #ifdef KEEP_XKB_LOCK_STATUS
99 extern int wXkbEventBase;
100 #endif
102 /* special flags */
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();
128 #ifdef SHAPE
129 static void handleShapeNotify();
130 #endif
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 {
140 pid_t pid;
141 unsigned char exit_status;
142 } DeadProcesses;
144 /* stack of dead processes */
145 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
146 static int deadProcessPtr=0;
149 typedef struct DeathHandler {
150 WDeathHandler *callback;
151 pid_t pid;
152 void *client_data;
153 } DeathHandler;
155 static WMArray *deathHandlers=NULL;
159 WMagicNumber
160 wAddDeathHandler(pid_t pid, WDeathHandler *callback, void *cdata)
162 DeathHandler *handler;
164 handler = malloc(sizeof(DeathHandler));
165 if (!handler)
166 return 0;
168 handler->pid = pid;
169 handler->callback = callback;
170 handler->client_data = cdata;
172 if (!deathHandlers)
173 deathHandlers = WMCreateArrayWithDestructor(8, wfree);
175 WMAddToArray(deathHandlers, handler);
177 return handler;
182 void
183 wDeleteDeathHandler(WMagicNumber id)
185 DeathHandler *handler=(DeathHandler*)id;
187 if (!handler || !deathHandlers)
188 return;
190 /* array destructor will call wfree(handler) */
191 WMRemoveFromArray(deathHandlers, handler);
195 void
196 DispatchEvent(XEvent *event)
198 if (deathHandlers)
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
207 * recursive loop.
209 Shutdown(WSExitMode);
211 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
212 WCHANGE_STATE(WSTATE_RESTARTING);
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");
222 /* for the case that all that is wanted to be dispatched is
223 * the stuff above */
224 if (!event)
225 return;
227 saveTimestamp(event);
228 switch (event->type) {
229 case MapRequest:
230 handleMapRequest(event);
231 break;
233 case KeyPress:
234 handleKeyPress(event);
235 break;
237 case MotionNotify:
238 handleMotionNotify(event);
239 break;
241 case ConfigureRequest:
242 handleConfigureRequest(event);
243 break;
245 case DestroyNotify:
246 handleDestroyNotify(event);
247 break;
249 case MapNotify:
250 handleMapNotify(event);
251 break;
253 case UnmapNotify:
254 handleUnmapNotify(event);
255 break;
257 case ButtonPress:
258 handleButtonPress(event);
259 break;
261 case Expose:
262 handleExpose(event);
263 break;
265 case PropertyNotify:
266 handlePropertyNotify(event);
267 break;
269 case EnterNotify:
270 handleEnterNotify(event);
271 break;
273 case LeaveNotify:
274 handleLeaveNotify(event);
275 break;
277 case ClientMessage:
278 handleClientMessage(event);
279 break;
281 case ColormapNotify:
282 handleColormapNotify(event);
283 break;
285 case MappingNotify:
286 if (event->xmapping.request == MappingKeyboard
287 || event->xmapping.request == MappingModifier)
288 XRefreshKeyboardMapping(&event->xmapping);
289 break;
291 case FocusIn:
292 handleFocusIn(event);
293 break;
295 case VisibilityNotify:
296 handleVisibilityNotify(event);
297 break;
298 default:
299 handleExtensions(event);
300 break;
305 *----------------------------------------------------------------------
306 * inotifyHandleEvents-
307 * Check for inotify events
309 * Returns:
310 * After reading events for the given file descriptor (fd) and
311 * watch descriptor (wd)
313 * Side effects:
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"));
347 close(fd);
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;
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 *----------------------------------------------------------------------
370 * EventLoop-
371 * Processes X and internal events indefinitely.
373 * Returns:
374 * Never returns
376 * Side effects:
377 * The LastTimestamp global variable is updated.
378 * Calls inotifyGetEvents if defaults database changes.
379 *----------------------------------------------------------------------
381 void
382 EventLoop()
384 XEvent event;
385 extern int inotifyFD;
386 extern int inotifyWD;
387 struct timeval time;
388 fd_set rfds;
389 int retVal=0;
391 if (inotifyFD < 0 || inotifyWD < 0)
392 retVal = -1;
394 for(;;) {
396 WMNextEvent(dpy, &event); /* Blocks here */
397 WMHandleEvent(&event);
399 if (retVal != -1 ) {
400 time.tv_sec = 0;
401 time.tv_usec = 0;
402 FD_ZERO (&rfds);
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."));
412 close(inotifyFD);
413 continue;
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.
427 * Returns:
428 * After the pending events that were present at the function call
429 * are processed.
431 * Side effects:
432 * Many -- whatever handling events may involve.
434 *----------------------------------------------------------------------
436 void
437 ProcessPendingEvents()
439 XEvent event;
440 int count;
442 XSync(dpy, False);
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);
450 count--;
455 Bool
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;
467 return True;
469 return False;
473 void
474 NotifyDeadProcess(pid_t pid, unsigned char status)
476 if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
477 wwarning("stack overflow: too many dead processes");
478 return;
480 /* stack the process to be handled later,
481 * as this is called from the signal handler */
482 deadProcesses[deadProcessPtr].pid = pid;
483 deadProcesses[deadProcessPtr].exit_status = status;
484 deadProcessPtr++;
488 static void
489 handleDeadProcess(void *foo)
491 DeathHandler *tmp;
492 int i;
494 for (i=0; i<deadProcessPtr; i++) {
495 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
498 if (!deathHandlers) {
499 deadProcessPtr=0;
500 return;
503 /* get the pids on the queue and call handlers */
504 while (deadProcessPtr>0) {
505 deadProcessPtr--;
507 for (i = WMGetArrayItemCount(deathHandlers)-1; i >= 0; i--) {
508 tmp = WMGetFromArray(deathHandlers, i);
509 if (!tmp)
510 continue;
512 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
513 (*tmp->callback)(tmp->pid,
514 deadProcesses[deadProcessPtr].exit_status,
515 tmp->client_data);
516 wDeleteDeathHandler(tmp);
523 static void
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) {
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;
566 static int
567 matchWindow(void *item, void *cdata)
569 return (((WFakeGroupLeader*)item)->origLeader == (Window)cdata);
573 static void
574 handleExtensions(XEvent *event)
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);
584 #endif
585 #ifdef KEEP_XKB_LOCK_STATUS
586 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)){
587 handleXkbIndicatorStateNotify(event);
589 #endif /*KEEP_XKB_LOCK_STATUS*/
593 static void
594 handleMapRequest(XEvent *ev)
596 WWindow *wwin;
597 WScreen *scr = NULL;
598 Window window = ev->xmaprequest.window;
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);
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);
616 wUnhideApplication(wapp, False, False);
618 return;
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);
634 else
635 wDockTrackWindowLaunch(scr->last_dock, window);
638 if (wwin) {
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;
646 wShadeWindow(wwin);
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;
662 if (wapp) {
663 wHideApplication(wapp);
670 static void
671 handleDestroyNotify(XEvent *event)
673 WWindow *wwin;
674 WApplication *app;
675 Window window = event->xdestroywindow.window;
676 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
677 int index;
679 #ifdef DEBUG
680 printf("got destroy notify\n");
681 #endif
682 wwin = wWindowFor(window);
683 if (wwin) {
684 wUnmanageWindow(wwin, False, True);
687 if (scr != NULL) {
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) {
694 fPtr->retainCount--;
695 if (fPtr->retainCount==0 && fPtr->leader!=None) {
696 XDestroyWindow(dpy, fPtr->leader);
697 fPtr->leader = None;
698 XFlush(dpy);
701 fPtr->origLeader = None;
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;
714 wwin = wwin->prev;
717 wApplicationDestroy(app);
723 static void
724 handleExpose(XEvent *event)
726 WObjDescriptor *desc;
727 XEvent ev;
729 #ifdef DEBUG
730 printf("got expose\n");
731 #endif
732 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev));
734 if (XFindContext(dpy, event->xexpose.window, wWinContext,
735 (XPointer *)&desc)==XCNOENT) {
736 return;
739 if (desc->handle_expose) {
740 (*desc->handle_expose)(desc, event);
744 static void
745 executeButtonAction(WScreen *scr, XEvent *event, int action)
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;
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;
770 break;
771 default:
772 break;
777 /* bindable */
778 static void
779 handleButtonPress(XEvent *event)
781 WObjDescriptor *desc;
782 WScreen *scr;
784 #ifdef DEBUG
785 printf("got button press\n");
786 #endif
787 scr = wScreenForRootWindow(event->xbutton.root);
789 #ifdef BALLOON_TEXT
790 wBalloonHide(scr);
791 #endif
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);
813 #endif /* !LITE */
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;
824 if (desc->parent_type == WCLASS_WINDOW) {
825 XSync(dpy, 0);
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);
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);
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;
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;
863 static void
864 handleMapNotify(XEvent *event)
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);
884 static void
885 handleUnmapNotify(XEvent *event)
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;
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;
904 if (wwin->client_win != event->xunmap.event && !withdraw)
905 return;
907 if (!wwin->flags.mapped && !withdraw
908 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
909 && !wwin->flags.miniaturized && !wwin->flags.hidden)
910 return;
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;
922 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
923 reparented = True;
925 /* withdraw window */
926 wwin->flags.mapped = 0;
927 if (!reparented)
928 wClientSetState(wwin, WithdrawnState, None);
930 /* if the window was reparented, do not reparent it back to the
931 * root window */
932 wUnmanageWindow(wwin, !reparented, False);
934 XUngrabServer(dpy);
938 static void
939 handleConfigureRequest(XEvent *event)
941 WWindow *wwin;
942 #ifdef DEBUG
943 printf("got configure request\n");
944 #endif
945 if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
947 * Configure request for unmapped window
949 wClientConfigure(NULL, &(event->xconfigurerequest));
950 } else {
951 wClientConfigure(wwin, &(event->xconfigurerequest));
956 static void
957 handlePropertyNotify(XEvent *event)
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
969 wwin = wWindowFor(event->xproperty.window);
970 if (wwin) {
971 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
972 &ju, &ju, &ju, &ju)) {
973 return;
975 wClientCheckProperty(wwin, &event->xproperty);
977 wapp = wApplicationOf(event->xproperty.window);
978 if (wapp) {
979 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
982 scr = wScreenForWindow(event->xproperty.window);
986 static void
987 handleClientMessage(XEvent *event)
989 WWindow *wwin;
990 WObjDescriptor *desc;
991 #ifdef DEBUG
992 printf("got client message\n");
993 #endif
994 /* handle transition from Normal to Iconic state */
995 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
996 && event->xclient.format == 32
997 && event->xclient.data.l[0] == IconicState) {
999 wwin = wWindowFor(event->xclient.window);
1000 if (!wwin) return;
1001 if (!wwin->flags.miniaturized)
1002 wIconifyWindow(wwin);
1003 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY
1004 && event->xclient.format == 32) {
1005 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1007 if (!scr)
1008 return;
1010 if (event->xclient.data.l[1] == 1) { /* starting */
1011 wColormapAllowClientInstallation(scr, True);
1012 } else { /* stopping */
1013 wColormapAllowClientInstallation(scr, False);
1015 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
1017 wDefaultsCheckDomains("bla");
1019 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
1020 WApplication *wapp;
1021 int done=0;
1022 wapp = wApplicationOf(event->xclient.window);
1023 if (wapp) {
1024 switch (event->xclient.data.l[0]) {
1025 case WMFHideOtherApplications:
1026 wHideOtherApplications(wapp->main_window_desc);
1027 done = 1;
1028 break;
1030 case WMFHideApplication:
1031 wHideApplication(wapp);
1032 done = 1;
1033 break;
1036 if (!done) {
1037 wwin = wWindowFor(event->xclient.window);
1038 if (wwin) {
1039 switch (event->xclient.data.l[0]) {
1040 case WMFHideOtherApplications:
1041 wHideOtherApplications(wwin);
1042 break;
1044 case WMFHideApplication:
1045 wHideApplication(wApplicationOf(wwin->main_window));
1046 break;
1050 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1051 wwin = wWindowFor(event->xclient.window);
1052 if (!wwin) return;
1053 switch (event->xclient.data.l[0]) {
1054 case GSWindowLevelAttr:
1056 int level = (int)event->xclient.data.l[1];
1058 if (WINDOW_LEVEL(wwin) != level) {
1059 ChangeStackingLevel(wwin->frame->core, level);
1062 break;
1064 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1065 wwin = wWindowFor(event->xclient.window);
1066 if (!wwin) return;
1067 switch (event->xclient.data.l[0]) {
1068 case WMTitleBarNormal:
1069 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1070 break;
1071 case WMTitleBarMain:
1072 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1073 break;
1074 case WMTitleBarKey:
1075 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1076 break;
1078 #ifdef NETWM_HINTS
1079 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1080 /* do nothing */
1081 #endif
1082 #ifdef XDND
1083 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1084 /* do nothing */
1085 #endif /* XDND */
1086 } else {
1088 * Non-standard thing, but needed by OffiX DND.
1089 * For when the icon frame gets a ClientMessage
1090 * that should have gone to the icon_window.
1092 if (XFindContext(dpy, event->xbutton.window, wWinContext,
1093 (XPointer *)&desc)!=XCNOENT) {
1094 struct WIcon *icon=NULL;
1096 if (desc->parent_type == WCLASS_MINIWINDOW) {
1097 icon = (WIcon*)desc->parent;
1098 } else if (desc->parent_type == WCLASS_DOCK_ICON
1099 || desc->parent_type == WCLASS_APPICON) {
1100 icon = ((WAppIcon*)desc->parent)->icon;
1102 if (icon && (wwin=icon->owner)) {
1103 if (wwin->client_win!=event->xclient.window) {
1104 event->xclient.window = wwin->client_win;
1105 XSendEvent(dpy, wwin->client_win, False, NoEventMask,
1106 event);
1114 static void
1115 raiseWindow(WScreen *scr)
1117 WWindow *wwin;
1119 scr->autoRaiseTimer = NULL;
1121 wwin = wWindowFor(scr->autoRaiseWindow);
1122 if (!wwin)
1123 return;
1125 if (!wwin->flags.destroyed && wwin->flags.focused) {
1126 wRaiseFrame(wwin->frame->core);
1127 /* this is needed or a race condition will occur */
1128 XSync(dpy, False);
1133 static void
1134 handleEnterNotify(XEvent *event)
1136 WWindow *wwin;
1137 WObjDescriptor *desc = NULL;
1138 #ifdef VIRTUAL_DESKTOP
1139 void (*vdHandler)(XEvent * event);
1140 #endif
1141 XEvent ev;
1142 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1143 #ifdef DEBUG
1144 printf("got enter notify\n");
1145 #endif
1147 #ifdef VIRTUAL_DESKTOP
1148 if (XFindContext(dpy, event->xcrossing.window, wVEdgeContext,
1149 (XPointer *)&vdHandler)!=XCNOENT) {
1150 (*vdHandler)(event);
1152 #endif
1154 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
1155 &ev)) {
1156 /* already left the window... */
1157 saveTimestamp(&ev);
1158 if (ev.xcrossing.mode==event->xcrossing.mode
1159 && ev.xcrossing.detail==event->xcrossing.detail) {
1160 return;
1164 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1165 (XPointer *)&desc)!=XCNOENT) {
1166 if(desc->handle_enternotify)
1167 (*desc->handle_enternotify)(desc, event);
1170 /* enter to window */
1171 wwin = wWindowFor(event->xcrossing.window);
1172 if (!wwin) {
1173 if (wPreferences.colormap_mode==WCM_POINTER) {
1174 wColormapInstallForWindow(scr, NULL);
1176 if (scr->autoRaiseTimer
1177 && event->xcrossing.root==event->xcrossing.window) {
1178 WMDeleteTimerHandler(scr->autoRaiseTimer);
1179 scr->autoRaiseTimer = NULL;
1181 } else {
1182 /* set auto raise timer even if in focus-follows-mouse mode
1183 * and the event is for the frame window, even if the window
1184 * has focus already. useful if you move the pointer from a focused
1185 * window to the root window and back pretty fast
1187 * set focus if in focus-follows-mouse mode and the event
1188 * is for the frame window and window doesn't have focus yet */
1189 if (wPreferences.focus_mode==WKF_SLOPPY
1190 && wwin->frame->core->window==event->xcrossing.window
1191 && !scr->flags.doing_alt_tab) {
1193 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1194 wSetFocusTo(scr, wwin);
1196 if (scr->autoRaiseTimer)
1197 WMDeleteTimerHandler(scr->autoRaiseTimer);
1198 scr->autoRaiseTimer = NULL;
1200 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1201 scr->autoRaiseWindow = wwin->frame->core->window;
1202 scr->autoRaiseTimer
1203 = WMAddTimerHandler(wPreferences.raise_delay,
1204 (WMCallback*)raiseWindow, scr);
1207 /* Install colormap for window, if the colormap installation mode
1208 * is colormap_follows_mouse */
1209 if (wPreferences.colormap_mode==WCM_POINTER) {
1210 if (wwin->client_win==event->xcrossing.window)
1211 wColormapInstallForWindow(scr, wwin);
1212 else
1213 wColormapInstallForWindow(scr, NULL);
1217 /* a little kluge to hide the clip balloon */
1218 if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1219 if (!desc) {
1220 XUnmapWindow(dpy, scr->clip_balloon);
1221 scr->flags.clip_balloon_mapped = 0;
1222 } else {
1223 if (desc->parent_type!=WCLASS_DOCK_ICON
1224 || scr->clip_icon != desc->parent) {
1225 XUnmapWindow(dpy, scr->clip_balloon);
1226 scr->flags.clip_balloon_mapped = 0;
1231 if (event->xcrossing.window == event->xcrossing.root
1232 && event->xcrossing.detail == NotifyNormal
1233 && event->xcrossing.detail != NotifyInferior
1234 && wPreferences.focus_mode != WKF_CLICK) {
1236 wSetFocusTo(scr, scr->focused_window);
1239 #ifdef BALLOON_TEXT
1240 wBalloonEnteredObject(scr, desc);
1241 #endif
1245 static void
1246 handleLeaveNotify(XEvent *event)
1248 WObjDescriptor *desc = NULL;
1250 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1251 (XPointer *)&desc)!=XCNOENT) {
1252 if(desc->handle_leavenotify)
1253 (*desc->handle_leavenotify)(desc, event);
1258 #ifdef SHAPE
1259 static void
1260 handleShapeNotify(XEvent *event)
1262 XShapeEvent *shev = (XShapeEvent*)event;
1263 WWindow *wwin;
1264 XEvent ev;
1265 #ifdef DEBUG
1266 printf("got shape notify\n");
1267 #endif
1268 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1269 XShapeEvent *sev = (XShapeEvent*)&ev;
1271 if (sev->kind == ShapeBounding) {
1272 if (sev->shaped == shev->shaped) {
1273 *shev = *sev;
1274 } else {
1275 XPutBackEvent(dpy, &ev);
1276 break;
1281 wwin = wWindowFor(shev->window);
1282 if (!wwin || shev->kind != ShapeBounding)
1283 return;
1285 if (!shev->shaped && wwin->flags.shaped) {
1287 wwin->flags.shaped = 0;
1288 wWindowClearShape(wwin);
1290 } else if (shev->shaped) {
1292 wwin->flags.shaped = 1;
1293 wWindowSetShape(wwin);
1296 #endif /* SHAPE */
1298 #ifdef KEEP_XKB_LOCK_STATUS
1299 /* please help ]d if you know what to do */
1300 handleXkbIndicatorStateNotify(XEvent *event)
1302 WWindow *wwin;
1303 WScreen *scr;
1304 XkbStateRec staterec;
1305 int i;
1307 for (i=0; i<wScreenCount; i++) {
1308 scr = wScreenWithNumber(i);
1309 wwin = scr->focused_window;
1310 if (wwin && wwin->flags.focused) {
1311 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1312 if (wwin->frame->languagemode != staterec.group) {
1313 wwin->frame->last_languagemode = wwin->frame->languagemode;
1314 wwin->frame->languagemode = staterec.group;
1316 #ifdef XKB_BUTTON_HINT
1317 if (wwin->frame->titlebar) {
1318 wFrameWindowPaint(wwin->frame);
1320 #endif
1324 #endif /*KEEP_XKB_LOCK_STATUS*/
1326 static void
1327 handleColormapNotify(XEvent *event)
1329 WWindow *wwin;
1330 WScreen *scr;
1331 Bool reinstall = False;
1333 wwin = wWindowFor(event->xcolormap.window);
1334 if (!wwin)
1335 return;
1337 scr = wwin->screen_ptr;
1339 do {
1340 if (wwin) {
1341 if (event->xcolormap.new) {
1342 XWindowAttributes attr;
1344 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1346 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1347 scr->current_colormap = attr.colormap;
1349 reinstall = True;
1350 } else if (event->xcolormap.state == ColormapUninstalled &&
1351 scr->current_colormap == event->xcolormap.colormap) {
1353 /* some bastard app (like XV) removed our colormap */
1355 * can't enforce or things like xscreensaver wont work
1356 * reinstall = True;
1358 } else if (event->xcolormap.state == ColormapInstalled &&
1359 scr->current_colormap == event->xcolormap.colormap) {
1361 /* someone has put our colormap back */
1362 reinstall = False;
1365 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1366 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1368 if (reinstall && scr->current_colormap!=None) {
1369 if (!scr->flags.colormap_stuff_blocked)
1370 XInstallColormap(dpy, scr->current_colormap);
1376 static void
1377 handleFocusIn(XEvent *event)
1379 WWindow *wwin;
1382 * For applications that like stealing the focus.
1384 while (XCheckTypedEvent(dpy, FocusIn, event));
1385 saveTimestamp(event);
1386 if (event->xfocus.mode == NotifyUngrab
1387 || event->xfocus.mode == NotifyGrab
1388 || event->xfocus.detail > NotifyNonlinearVirtual) {
1389 return;
1392 wwin = wWindowFor(event->xfocus.window);
1393 if (wwin && !wwin->flags.focused) {
1394 if (wwin->flags.mapped)
1395 wSetFocusTo(wwin->screen_ptr, wwin);
1396 else
1397 wSetFocusTo(wwin->screen_ptr, NULL);
1398 } else if (!wwin) {
1399 WScreen *scr = wScreenForWindow(event->xfocus.window);
1400 if (scr)
1401 wSetFocusTo(scr, NULL);
1406 static WWindow*
1407 windowUnderPointer(WScreen *scr)
1409 unsigned int mask;
1410 int foo;
1411 Window bar, win;
1413 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
1414 &mask))
1415 return wWindowFor(win);
1416 return NULL;
1420 static int CheckFullScreenWindowFocused(WScreen *scr)
1422 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1423 return 1;
1424 else
1425 return 0;
1429 static void
1430 handleKeyPress(XEvent *event)
1432 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1433 WWindow *wwin = scr->focused_window;
1434 int i;
1435 int modifiers;
1436 int command=-1, index;
1437 #ifdef KEEP_XKB_LOCK_STATUS
1438 XkbStateRec staterec;
1439 #endif /*KEEP_XKB_LOCK_STATUS*/
1441 /* ignore CapsLock */
1442 modifiers = event->xkey.state & ValidModMask;
1444 for (i=0; i<WKBD_LAST; i++) {
1445 if (wKeyBindings[i].keycode==0)
1446 continue;
1448 if (wKeyBindings[i].keycode==event->xkey.keycode
1449 && (/*wKeyBindings[i].modifier==0
1450 ||*/ wKeyBindings[i].modifier==modifiers)) {
1451 command = i;
1452 break;
1457 if (command < 0) {
1458 #ifdef LITE
1460 #if 0
1462 #endif
1463 #else
1464 if (!wRootMenuPerformShortcut(event)) {
1465 #endif
1466 static int dontLoop = 0;
1468 if (dontLoop > 10) {
1469 wwarning("problem with key event processing code");
1470 return;
1472 dontLoop++;
1473 /* if the focused window is an internal window, try redispatching
1474 * the event to the managed window, as it can be a WINGs window */
1475 if (wwin && wwin->flags.internal_window
1476 && wwin->client_leader!=None) {
1477 /* client_leader contains the WINGs toplevel */
1478 event->xany.window = wwin->client_leader;
1479 WMHandleEvent(event);
1481 dontLoop--;
1483 return;
1486 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1487 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1489 switch (command) {
1490 #ifndef LITE
1491 case WKBD_ROOTMENU:
1492 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True);*/
1493 if (!CheckFullScreenWindowFocused(scr)) {
1494 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1495 OpenRootMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1497 break;
1498 case WKBD_WINDOWLIST:
1499 if (!CheckFullScreenWindowFocused(scr)) {
1500 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1501 OpenSwitchMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1503 break;
1504 #endif /* !LITE */
1505 case WKBD_WINDOWMENU:
1506 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1507 OpenWindowMenu(wwin, wwin->frame_x,
1508 wwin->frame_y+wwin->frame->top_width, True);
1509 break;
1510 case WKBD_MINIATURIZE:
1511 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1512 && !WFLAGP(wwin, no_miniaturizable)) {
1513 CloseWindowMenu(scr);
1515 if (wwin->protocols.MINIATURIZE_WINDOW)
1516 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
1517 event->xbutton.time);
1518 else {
1519 wIconifyWindow(wwin);
1522 break;
1523 case WKBD_HIDE:
1524 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1525 WApplication *wapp = wApplicationOf(wwin->main_window);
1526 CloseWindowMenu(scr);
1528 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1529 wHideApplication(wapp);
1532 break;
1533 case WKBD_HIDE_OTHERS:
1534 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1535 CloseWindowMenu(scr);
1537 wHideOtherApplications(wwin);
1539 break;
1540 case WKBD_MAXIMIZE:
1541 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1542 int newdir = (MAX_VERTICAL|MAX_HORIZONTAL);
1544 CloseWindowMenu(scr);
1546 if (wwin->flags.maximized == newdir) {
1547 wUnmaximizeWindow(wwin);
1548 } else {
1549 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1552 break;
1553 case WKBD_VMAXIMIZE:
1554 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1555 int newdir = (MAX_VERTICAL ^ wwin->flags.maximized);
1557 CloseWindowMenu(scr);
1559 if (newdir) {
1560 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1561 } else {
1562 wUnmaximizeWindow(wwin);
1565 break;
1566 case WKBD_HMAXIMIZE:
1567 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1568 int newdir = (MAX_HORIZONTAL ^ wwin->flags.maximized);
1570 CloseWindowMenu(scr);
1572 if (newdir) {
1573 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1574 } else {
1575 wUnmaximizeWindow(wwin);
1578 break;
1579 case WKBD_RAISE:
1580 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1581 CloseWindowMenu(scr);
1583 wRaiseFrame(wwin->frame->core);
1585 break;
1586 case WKBD_LOWER:
1587 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1588 CloseWindowMenu(scr);
1590 wLowerFrame(wwin->frame->core);
1592 break;
1593 case WKBD_RAISELOWER:
1594 /* raise or lower the window under the pointer, not the
1595 * focused one
1597 wwin = windowUnderPointer(scr);
1598 if (wwin)
1599 wRaiseLowerFrame(wwin->frame->core);
1600 break;
1601 case WKBD_SHADE:
1602 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1603 if (wwin->flags.shaded)
1604 wUnshadeWindow(wwin);
1605 else
1606 wShadeWindow(wwin);
1608 break;
1609 case WKBD_MOVERESIZE:
1610 if (ISMAPPED(wwin) && ISFOCUSED(wwin) &&
1611 (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1612 CloseWindowMenu(scr);
1614 wKeyboardMoveResizeWindow(wwin);
1616 break;
1617 case WKBD_CLOSE:
1618 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1619 CloseWindowMenu(scr);
1620 if (wwin->protocols.DELETE_WINDOW)
1621 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW,
1622 event->xkey.time);
1624 break;
1625 case WKBD_SELECT:
1626 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1627 wSelectWindow(wwin, !wwin->flags.selected);
1629 break;
1630 case WKBD_FOCUSNEXT:
1631 StartWindozeCycle(wwin, event, True);
1632 break;
1634 case WKBD_FOCUSPREV:
1635 StartWindozeCycle(wwin, event, False);
1636 break;
1638 #if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
1639 #define GOTOWORKS(wk) case WKBD_WORKSPACE##wk:\
1640 i = (scr->current_workspace/10)*10 + wk - 1;\
1641 if (wPreferences.ws_advance || i<scr->workspace_count)\
1642 wWorkspaceChange(scr, i);\
1643 break
1644 #else
1645 #define GOTOWORKS(wk) case WKBD_WORKSPACE/**/wk:\
1646 i = (scr->current_workspace/10)*10 + wk - 1;\
1647 if (wPreferences.ws_advance || i<scr->workspace_count)\
1648 wWorkspaceChange(scr, i);\
1649 break
1650 #endif
1651 GOTOWORKS(1);
1652 GOTOWORKS(2);
1653 GOTOWORKS(3);
1654 GOTOWORKS(4);
1655 GOTOWORKS(5);
1656 GOTOWORKS(6);
1657 GOTOWORKS(7);
1658 GOTOWORKS(8);
1659 GOTOWORKS(9);
1660 GOTOWORKS(10);
1661 #undef GOTOWORKS
1662 case WKBD_NEXTWORKSPACE:
1663 wWorkspaceRelativeChange(scr, 1);
1664 break;
1665 case WKBD_PREVWORKSPACE:
1666 wWorkspaceRelativeChange(scr, -1);
1667 break;
1668 case WKBD_WINDOW1:
1669 case WKBD_WINDOW2:
1670 case WKBD_WINDOW3:
1671 case WKBD_WINDOW4:
1672 case WKBD_WINDOW5:
1673 case WKBD_WINDOW6:
1674 case WKBD_WINDOW7:
1675 case WKBD_WINDOW8:
1676 case WKBD_WINDOW9:
1677 case WKBD_WINDOW10:
1679 index = command-WKBD_WINDOW1;
1681 if (scr->shortcutWindows[index]) {
1682 WMArray *list = scr->shortcutWindows[index];
1683 int cw;
1684 int count = WMGetArrayItemCount(list);
1685 WWindow *twin;
1686 WMArrayIterator iter;
1687 WWindow *wwin;
1689 wUnselectWindows(scr);
1690 cw = scr->current_workspace;
1692 WM_ETARETI_ARRAY(list, wwin, iter) {
1693 if (count > 1)
1694 wWindowChangeWorkspace(wwin, cw);
1696 wMakeWindowVisible(wwin);
1698 if (count > 1)
1699 wSelectWindow(wwin, True);
1702 /* rotate the order of windows, to create a cycling effect */
1703 twin = WMGetFromArray(list, 0);
1704 WMDeleteFromArray(list, 0);
1705 WMAddToArray(list, twin);
1707 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1708 if (scr->shortcutWindows[index]) {
1709 WMFreeArray(scr->shortcutWindows[index]);
1710 scr->shortcutWindows[index] = NULL;
1713 if (wwin->flags.selected && scr->selected_windows) {
1714 scr->shortcutWindows[index] =
1715 WMDuplicateArray(scr->selected_windows);
1716 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1717 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
1718 } else {
1719 scr->shortcutWindows[index] = WMCreateArray(4);
1720 WMAddToArray(scr->shortcutWindows[index], wwin);
1723 wSelectWindow(wwin, !wwin->flags.selected);
1724 XFlush(dpy);
1725 wusleep(3000);
1726 wSelectWindow(wwin, !wwin->flags.selected);
1727 XFlush(dpy);
1729 } else if (scr->selected_windows
1730 && WMGetArrayItemCount(scr->selected_windows)) {
1732 if (wwin->flags.selected && scr->selected_windows) {
1733 if (scr->shortcutWindows[index]) {
1734 WMFreeArray(scr->shortcutWindows[index]);
1736 scr->shortcutWindows[index] =
1737 WMDuplicateArray(scr->selected_windows);
1741 break;
1743 case WKBD_SWITCH_SCREEN:
1744 if (wScreenCount > 1) {
1745 WScreen *scr2;
1746 int i;
1748 /* find index of this screen */
1749 for (i = 0; i < wScreenCount; i++) {
1750 if (wScreenWithNumber(i) == scr)
1751 break;
1753 i++;
1754 if (i >= wScreenCount) {
1755 i = 0;
1757 scr2 = wScreenWithNumber(i);
1759 if (scr2) {
1760 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1761 scr2->scr_width/2, scr2->scr_height/2);
1764 break;
1766 case WKBD_NEXTWSLAYER:
1767 case WKBD_PREVWSLAYER:
1769 int row, column;
1771 row = scr->current_workspace/10;
1772 column = scr->current_workspace%10;
1774 if (command==WKBD_NEXTWSLAYER) {
1775 if ((row+1)*10 < scr->workspace_count)
1776 wWorkspaceChange(scr, column+(row+1)*10);
1777 } else {
1778 if (row > 0)
1779 wWorkspaceChange(scr, column+(row-1)*10);
1782 break;
1783 case WKBD_CLIPLOWER:
1784 if (!wPreferences.flags.noclip)
1785 wDockLower(scr->workspaces[scr->current_workspace]->clip);
1786 break;
1787 case WKBD_CLIPRAISE:
1788 if (!wPreferences.flags.noclip)
1789 wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1790 break;
1791 case WKBD_CLIPRAISELOWER:
1792 if (!wPreferences.flags.noclip)
1793 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1794 break;
1795 #ifdef KEEP_XKB_LOCK_STATUS
1796 case WKBD_TOGGLE:
1797 if(wPreferences.modelock) {
1798 /*toggle*/
1799 wwin = scr->focused_window;
1801 if (wwin && wwin->flags.mapped
1802 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1803 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1804 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1806 wwin->frame->languagemode = wwin->frame->last_languagemode;
1807 wwin->frame->last_languagemode = staterec.group;
1808 XkbLockGroup(dpy,XkbUseCoreKbd, wwin->frame->languagemode);
1812 break;
1813 #endif /* KEEP_XKB_LOCK_STATUS */
1814 #ifdef VIRTUAL_DESKTOP
1815 case WKBD_VDESK_LEFT:
1816 wWorkspaceKeyboardMoveDesktop(scr, VEC_LEFT);
1817 break;
1819 case WKBD_VDESK_RIGHT:
1820 wWorkspaceKeyboardMoveDesktop(scr, VEC_RIGHT);
1821 break;
1823 case WKBD_VDESK_UP:
1824 wWorkspaceKeyboardMoveDesktop(scr, VEC_UP);
1825 break;
1827 case WKBD_VDESK_DOWN:
1828 wWorkspaceKeyboardMoveDesktop(scr, VEC_DOWN);
1829 break;
1830 #endif
1836 static void
1837 handleMotionNotify(XEvent *event)
1839 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1841 if (wPreferences.scrollable_menus) {
1842 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1843 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1845 if (scr->flags.jump_back_pending ||
1846 p.x <= (rect.pos.x + 1) ||
1847 p.x >= (rect.pos.x + rect.size.width - 2) ||
1848 p.y <= (rect.pos.y + 1) ||
1849 p.y >= (rect.pos.y + rect.size.height - 2)) {
1850 WMenu *menu;
1851 #ifdef DEBUG
1852 printf("pointer at screen edge\n");
1853 #endif
1854 menu = wMenuUnderPointer(scr);
1855 if (menu!=NULL)
1856 wMenuScroll(menu, event);
1862 static void
1863 handleVisibilityNotify(XEvent *event)
1865 WWindow *wwin;
1867 wwin = wWindowFor(event->xvisibility.window);
1868 if (!wwin)
1869 return;
1870 wwin->flags.obscured =
1871 (event->xvisibility.state == VisibilityFullyObscured);