handleKeyPress: Fix shadowing of global 'index' variable
[wmaker-crm.git] / src / event.c
blob3d512c1453a0a094b607792673a74c0edb9379ed
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 #define BUFF_SIZE ((sizeof(struct inotify_event) + 16)*512)
319 void inotifyHandleEvents (int fd, int wd)
321 extern void wDefaultsCheckDomains();
322 ssize_t eventQLength, i = 0;
323 char buff[BUFF_SIZE] = {0};
324 /* Check config only once per read of the event queue */
325 int oneShotFlag = 0;
328 * Read off the queued events
329 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
330 * not occur; the block is on Xevents, but a config file change will normally
331 * occur as a result of an Xevent - so the event queue should never have more than
332 * a few entries before a read().
334 eventQLength = read(fd, buff, BUFF_SIZE);
336 /* check what events occured */
337 /* Should really check wd here too, but for now we only have one watch! */
338 while (i < eventQLength) {
339 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
342 * see inotify.h for event types.
344 if (pevent->mask & IN_DELETE_SELF) {
345 wwarning(_("the defaults database has been deleted!"
346 " Restart Window Maker to create the database"
347 " with the default settings"));
348 close(fd);
350 if (pevent->mask & IN_UNMOUNT) {
351 wwarning(_("the unit containing the defaults database has"
352 " been unmounted. Setting --static mode."
353 " Any changes will not be saved."));
354 close(fd);
355 wPreferences.flags.noupdates=1;
357 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
358 fprintf(stdout,"wmaker: reading config files in defaults database.\n");
359 wDefaultsCheckDomains(NULL);
362 /* move to next event in the buffer */
363 i += sizeof(struct inotify_event) + pevent->len;
368 *----------------------------------------------------------------------
369 * EventLoop-
370 * Processes X and internal events indefinitely.
372 * Returns:
373 * Never returns
375 * Side effects:
376 * The LastTimestamp global variable is updated.
377 * Calls inotifyGetEvents if defaults database changes.
378 *----------------------------------------------------------------------
380 void
381 EventLoop()
383 XEvent event;
384 extern int inotifyFD;
385 extern int inotifyWD;
386 struct timeval time;
387 fd_set rfds;
388 int retVal=0;
390 if (inotifyFD < 0 || inotifyWD < 0)
391 retVal = -1;
393 for(;;) {
395 WMNextEvent(dpy, &event); /* Blocks here */
396 WMHandleEvent(&event);
398 if (retVal != -1 ) {
399 time.tv_sec = 0;
400 time.tv_usec = 0;
401 FD_ZERO (&rfds);
402 FD_SET (inotifyFD, &rfds);
404 /* check for available read data from inotify - don't block! */
405 retVal = select (inotifyFD + 1, &rfds, NULL, NULL, &time);
407 if (retVal < 0) { /* an error has occured */
408 wwarning(_("select failed. The inotify instance will be closed."
409 " Changes to the defaults database will require"
410 " a restart to take effect."));
411 close(inotifyFD);
412 continue;
414 if (FD_ISSET (inotifyFD, &rfds))
415 inotifyHandleEvents(inotifyFD,inotifyWD);
421 *----------------------------------------------------------------------
422 * ProcessPendingEvents --
423 * Processes the events that are currently pending (at the time
424 * this function is called) in the display's queue.
426 * Returns:
427 * After the pending events that were present at the function call
428 * are processed.
430 * Side effects:
431 * Many -- whatever handling events may involve.
433 *----------------------------------------------------------------------
435 void
436 ProcessPendingEvents()
438 XEvent event;
439 int count;
441 XSync(dpy, False);
443 /* Take a snapshot of the event count in the queue */
444 count = XPending(dpy);
446 while (count>0 && XPending(dpy)) {
447 WMNextEvent(dpy, &event);
448 WMHandleEvent(&event);
449 count--;
454 Bool
455 IsDoubleClick(WScreen *scr, XEvent *event)
457 if ((scr->last_click_time>0) &&
458 (event->xbutton.time-scr->last_click_time<=wPreferences.dblclick_time)
459 && (event->xbutton.button == scr->last_click_button)
460 && (event->xbutton.window == scr->last_click_window)) {
462 scr->flags.next_click_is_not_double = 1;
463 scr->last_click_time = 0;
464 scr->last_click_window = event->xbutton.window;
466 return True;
468 return False;
472 void
473 NotifyDeadProcess(pid_t pid, unsigned char status)
475 if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
476 wwarning("stack overflow: too many dead processes");
477 return;
479 /* stack the process to be handled later,
480 * as this is called from the signal handler */
481 deadProcesses[deadProcessPtr].pid = pid;
482 deadProcesses[deadProcessPtr].exit_status = status;
483 deadProcessPtr++;
487 static void
488 handleDeadProcess(void *foo)
490 DeathHandler *tmp;
491 int i;
493 for (i=0; i<deadProcessPtr; i++) {
494 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
497 if (!deathHandlers) {
498 deadProcessPtr=0;
499 return;
502 /* get the pids on the queue and call handlers */
503 while (deadProcessPtr>0) {
504 deadProcessPtr--;
506 for (i = WMGetArrayItemCount(deathHandlers)-1; i >= 0; i--) {
507 tmp = WMGetFromArray(deathHandlers, i);
508 if (!tmp)
509 continue;
511 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
512 (*tmp->callback)(tmp->pid,
513 deadProcesses[deadProcessPtr].exit_status,
514 tmp->client_data);
515 wDeleteDeathHandler(tmp);
522 static void
523 saveTimestamp(XEvent *event)
526 * Never save CurrentTime as LastTimestamp because CurrentTime
527 * it's not a real timestamp (it's the 0L constant)
530 switch (event->type) {
531 case ButtonRelease:
532 case ButtonPress:
533 LastTimestamp = event->xbutton.time;
534 break;
535 case KeyPress:
536 case KeyRelease:
537 LastTimestamp = event->xkey.time;
538 break;
539 case MotionNotify:
540 LastTimestamp = event->xmotion.time;
541 break;
542 case PropertyNotify:
543 LastTimestamp = event->xproperty.time;
544 break;
545 case EnterNotify:
546 case LeaveNotify:
547 LastTimestamp = event->xcrossing.time;
548 break;
549 case SelectionClear:
550 LastTimestamp = event->xselectionclear.time;
551 break;
552 case SelectionRequest:
553 LastTimestamp = event->xselectionrequest.time;
554 break;
555 case SelectionNotify:
556 LastTimestamp = event->xselection.time;
557 #ifdef XDND
558 wXDNDProcessSelection(event);
559 #endif
560 break;
565 static int
566 matchWindow(void *item, void *cdata)
568 return (((WFakeGroupLeader*)item)->origLeader == (Window)cdata);
572 static void
573 handleExtensions(XEvent *event)
575 #ifdef KEEP_XKB_LOCK_STATUS
576 XkbEvent *xkbevent;
577 xkbevent = (XkbEvent *)event;
578 #endif /*KEEP_XKB_LOCK_STATUS*/
579 #ifdef SHAPE
580 if (wShapeSupported && event->type == (wShapeEventBase+ShapeNotify)) {
581 handleShapeNotify(event);
583 #endif
584 #ifdef KEEP_XKB_LOCK_STATUS
585 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)){
586 handleXkbIndicatorStateNotify(event);
588 #endif /*KEEP_XKB_LOCK_STATUS*/
592 static void
593 handleMapRequest(XEvent *ev)
595 WWindow *wwin;
596 WScreen *scr = NULL;
597 Window window = ev->xmaprequest.window;
599 #ifdef DEBUG
600 printf("got map request for %x\n", (unsigned)window);
601 #endif
602 if ((wwin = wWindowFor(window))) {
603 if (wwin->flags.shaded) {
604 wUnshadeWindow(wwin);
606 /* deiconify window */
607 if (wwin->flags.miniaturized) {
608 wDeiconifyWindow(wwin);
609 } else if (wwin->flags.hidden) {
610 WApplication *wapp = wApplicationOf(wwin->main_window);
611 /* go to the last workspace that the user worked on the app */
612 if (wapp) {
613 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
615 wUnhideApplication(wapp, False, False);
617 return;
620 scr = wScreenForRootWindow(ev->xmaprequest.parent);
622 wwin = wManageWindow(scr, window);
625 * This is to let the Dock know that the application it launched
626 * has already been mapped (eg: it has finished launching).
627 * It is not necessary for normally docked apps, but is needed for
628 * apps that were forcedly docked (like with dockit).
630 if (scr->last_dock) {
631 if (wwin && wwin->main_window!=None && wwin->main_window!=window)
632 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
633 else
634 wDockTrackWindowLaunch(scr->last_dock, window);
637 if (wwin) {
638 wClientSetState(wwin, NormalState, None);
639 if (wwin->flags.maximized) {
640 wMaximizeWindow(wwin, wwin->flags.maximized);
642 if (wwin->flags.shaded) {
643 wwin->flags.shaded = 0;
644 wwin->flags.skip_next_animation = 1;
645 wShadeWindow(wwin);
647 if (wwin->flags.miniaturized) {
648 wwin->flags.miniaturized = 0;
649 wwin->flags.skip_next_animation = 1;
650 wIconifyWindow(wwin);
652 if (wwin->flags.fullscreen) {
653 wwin->flags.fullscreen = 0;
654 wFullscreenWindow(wwin);
656 if (wwin->flags.hidden) {
657 WApplication *wapp = wApplicationOf(wwin->main_window);
659 wwin->flags.hidden = 0;
660 wwin->flags.skip_next_animation = 1;
661 if (wapp) {
662 wHideApplication(wapp);
669 static void
670 handleDestroyNotify(XEvent *event)
672 WWindow *wwin;
673 WApplication *app;
674 Window window = event->xdestroywindow.window;
675 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
676 int widx;
678 #ifdef DEBUG
679 printf("got destroy notify\n");
680 #endif
681 wwin = wWindowFor(window);
682 if (wwin) {
683 wUnmanageWindow(wwin, False, True);
686 if (scr != NULL) {
687 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow,
688 (void*)window)) != WANotFound) {
689 WFakeGroupLeader *fPtr;
691 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
692 if (fPtr->retainCount > 0) {
693 fPtr->retainCount--;
694 if (fPtr->retainCount==0 && fPtr->leader!=None) {
695 XDestroyWindow(dpy, fPtr->leader);
696 fPtr->leader = None;
697 XFlush(dpy);
700 fPtr->origLeader = None;
704 app = wApplicationOf(window);
705 if (app) {
706 if (window == app->main_window) {
707 app->refcount = 0;
708 wwin = app->main_window_desc->screen_ptr->focused_window;
709 while (wwin) {
710 if (wwin->main_window == window) {
711 wwin->main_window = None;
713 wwin = wwin->prev;
716 wApplicationDestroy(app);
722 static void
723 handleExpose(XEvent *event)
725 WObjDescriptor *desc;
726 XEvent ev;
728 #ifdef DEBUG
729 printf("got expose\n");
730 #endif
731 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev));
733 if (XFindContext(dpy, event->xexpose.window, wWinContext,
734 (XPointer *)&desc)==XCNOENT) {
735 return;
738 if (desc->handle_expose) {
739 (*desc->handle_expose)(desc, event);
743 static void
744 executeButtonAction(WScreen *scr, XEvent *event, int action)
746 switch(action) {
747 case WA_SELECT_WINDOWS:
748 wUnselectWindows(scr);
749 wSelectWindows(scr, event);
750 break;
751 case WA_OPEN_APPMENU:
752 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
753 /* ugly hack */
754 if (scr->root_menu) {
755 if (scr->root_menu->brother->flags.mapped)
756 event->xbutton.window = scr->root_menu->brother->frame->core->window;
757 else
758 event->xbutton.window = scr->root_menu->frame->core->window;
760 break;
761 case WA_OPEN_WINLISTMENU:
762 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
763 if (scr->switch_menu) {
764 if (scr->switch_menu->brother->flags.mapped)
765 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
766 else
767 event->xbutton.window = scr->switch_menu->frame->core->window;
769 break;
770 default:
771 break;
776 /* bindable */
777 static void
778 handleButtonPress(XEvent *event)
780 WObjDescriptor *desc;
781 WScreen *scr;
783 #ifdef DEBUG
784 printf("got button press\n");
785 #endif
786 scr = wScreenForRootWindow(event->xbutton.root);
788 #ifdef BALLOON_TEXT
789 wBalloonHide(scr);
790 #endif
793 #ifndef LITE
794 if (event->xbutton.window==scr->root_win) {
795 if (event->xbutton.button==Button1 &&
796 wPreferences.mouse_button1!=WA_NONE) {
797 executeButtonAction(scr, event, wPreferences.mouse_button1);
798 } else if (event->xbutton.button==Button2 &&
799 wPreferences.mouse_button2!=WA_NONE) {
800 executeButtonAction(scr, event, wPreferences.mouse_button2);
801 } else if (event->xbutton.button==Button3 &&
802 wPreferences.mouse_button3!=WA_NONE) {
803 executeButtonAction(scr, event, wPreferences.mouse_button3);
804 } else if (event->xbutton.button==Button4 &&
805 wPreferences.mouse_wheel!=WA_NONE) {
806 wWorkspaceRelativeChange(scr, 1);
807 } else if (event->xbutton.button==Button5 &&
808 wPreferences.mouse_wheel!=WA_NONE) {
809 wWorkspaceRelativeChange(scr, -1);
812 #endif /* !LITE */
814 desc = NULL;
815 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext,
816 (XPointer *)&desc)==XCNOENT) {
817 if (XFindContext(dpy, event->xbutton.window, wWinContext,
818 (XPointer *)&desc)==XCNOENT) {
819 return;
823 if (desc->parent_type == WCLASS_WINDOW) {
824 XSync(dpy, 0);
826 if (event->xbutton.state & MOD_MASK) {
827 XAllowEvents(dpy, AsyncPointer, CurrentTime);
830 /* if (wPreferences.focus_mode == WKF_CLICK) {*/
831 if (wPreferences.ignore_focus_click) {
832 XAllowEvents(dpy, AsyncPointer, CurrentTime);
834 XAllowEvents(dpy, ReplayPointer, CurrentTime);
835 /* }*/
836 XSync(dpy, 0);
837 } else if (desc->parent_type == WCLASS_APPICON
838 || desc->parent_type == WCLASS_MINIWINDOW
839 || desc->parent_type == WCLASS_DOCK_ICON) {
840 if (event->xbutton.state & MOD_MASK) {
841 XSync(dpy, 0);
842 XAllowEvents(dpy, AsyncPointer, CurrentTime);
843 XSync(dpy, 0);
847 if (desc->handle_mousedown!=NULL) {
848 (*desc->handle_mousedown)(desc, event);
851 /* save double-click information */
852 if (scr->flags.next_click_is_not_double) {
853 scr->flags.next_click_is_not_double = 0;
854 } else {
855 scr->last_click_time = event->xbutton.time;
856 scr->last_click_button = event->xbutton.button;
857 scr->last_click_window = event->xbutton.window;
862 static void
863 handleMapNotify(XEvent *event)
865 WWindow *wwin;
866 #ifdef DEBUG
867 printf("got map\n");
868 #endif
869 wwin = wWindowFor(event->xmap.event);
870 if (wwin && wwin->client_win == event->xmap.event) {
871 if (wwin->flags.miniaturized) {
872 wDeiconifyWindow(wwin);
873 } else {
874 XGrabServer(dpy);
875 wWindowMap(wwin);
876 wClientSetState(wwin, NormalState, None);
877 XUngrabServer(dpy);
883 static void
884 handleUnmapNotify(XEvent *event)
886 WWindow *wwin;
887 XEvent ev;
888 Bool withdraw = False;
889 #ifdef DEBUG
890 printf("got unmap\n");
891 #endif
892 /* only process windows with StructureNotify selected
893 * (ignore SubstructureNotify) */
894 wwin = wWindowFor(event->xunmap.window);
895 if (!wwin)
896 return;
898 /* whether the event is a Withdrawal request */
899 if (event->xunmap.event == wwin->screen_ptr->root_win
900 && event->xunmap.send_event)
901 withdraw = True;
903 if (wwin->client_win != event->xunmap.event && !withdraw)
904 return;
906 if (!wwin->flags.mapped && !withdraw
907 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
908 && !wwin->flags.miniaturized && !wwin->flags.hidden)
909 return;
911 XGrabServer(dpy);
912 XUnmapWindow(dpy, wwin->frame->core->window);
913 wwin->flags.mapped = 0;
914 XSync(dpy, 0);
915 /* check if the window was destroyed */
916 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify,&ev)) {
917 DispatchEvent(&ev);
918 } else {
919 Bool reparented = False;
921 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
922 reparented = True;
924 /* withdraw window */
925 wwin->flags.mapped = 0;
926 if (!reparented)
927 wClientSetState(wwin, WithdrawnState, None);
929 /* if the window was reparented, do not reparent it back to the
930 * root window */
931 wUnmanageWindow(wwin, !reparented, False);
933 XUngrabServer(dpy);
937 static void
938 handleConfigureRequest(XEvent *event)
940 WWindow *wwin;
941 #ifdef DEBUG
942 printf("got configure request\n");
943 #endif
944 if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
946 * Configure request for unmapped window
948 wClientConfigure(NULL, &(event->xconfigurerequest));
949 } else {
950 wClientConfigure(wwin, &(event->xconfigurerequest));
955 static void
956 handlePropertyNotify(XEvent *event)
958 WWindow *wwin;
959 WApplication *wapp;
960 Window jr;
961 int ji;
962 unsigned int ju;
963 WScreen *scr;
964 #ifdef DEBUG
965 printf("got property notify\n");
966 #endif
968 wwin = wWindowFor(event->xproperty.window);
969 if (wwin) {
970 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
971 &ju, &ju, &ju, &ju)) {
972 return;
974 wClientCheckProperty(wwin, &event->xproperty);
976 wapp = wApplicationOf(event->xproperty.window);
977 if (wapp) {
978 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
981 scr = wScreenForWindow(event->xproperty.window);
985 static void
986 handleClientMessage(XEvent *event)
988 WWindow *wwin;
989 WObjDescriptor *desc;
990 #ifdef DEBUG
991 printf("got client message\n");
992 #endif
993 /* handle transition from Normal to Iconic state */
994 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
995 && event->xclient.format == 32
996 && event->xclient.data.l[0] == IconicState) {
998 wwin = wWindowFor(event->xclient.window);
999 if (!wwin) return;
1000 if (!wwin->flags.miniaturized)
1001 wIconifyWindow(wwin);
1002 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY
1003 && event->xclient.format == 32) {
1004 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1006 if (!scr)
1007 return;
1009 if (event->xclient.data.l[1] == 1) { /* starting */
1010 wColormapAllowClientInstallation(scr, True);
1011 } else { /* stopping */
1012 wColormapAllowClientInstallation(scr, False);
1014 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
1016 wDefaultsCheckDomains("bla");
1018 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
1019 WApplication *wapp;
1020 int done=0;
1021 wapp = wApplicationOf(event->xclient.window);
1022 if (wapp) {
1023 switch (event->xclient.data.l[0]) {
1024 case WMFHideOtherApplications:
1025 wHideOtherApplications(wapp->main_window_desc);
1026 done = 1;
1027 break;
1029 case WMFHideApplication:
1030 wHideApplication(wapp);
1031 done = 1;
1032 break;
1035 if (!done) {
1036 wwin = wWindowFor(event->xclient.window);
1037 if (wwin) {
1038 switch (event->xclient.data.l[0]) {
1039 case WMFHideOtherApplications:
1040 wHideOtherApplications(wwin);
1041 break;
1043 case WMFHideApplication:
1044 wHideApplication(wApplicationOf(wwin->main_window));
1045 break;
1049 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1050 wwin = wWindowFor(event->xclient.window);
1051 if (!wwin) return;
1052 switch (event->xclient.data.l[0]) {
1053 case GSWindowLevelAttr:
1055 int level = (int)event->xclient.data.l[1];
1057 if (WINDOW_LEVEL(wwin) != level) {
1058 ChangeStackingLevel(wwin->frame->core, level);
1061 break;
1063 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1064 wwin = wWindowFor(event->xclient.window);
1065 if (!wwin) return;
1066 switch (event->xclient.data.l[0]) {
1067 case WMTitleBarNormal:
1068 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1069 break;
1070 case WMTitleBarMain:
1071 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1072 break;
1073 case WMTitleBarKey:
1074 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1075 break;
1077 #ifdef NETWM_HINTS
1078 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1079 /* do nothing */
1080 #endif
1081 #ifdef XDND
1082 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1083 /* do nothing */
1084 #endif /* XDND */
1085 } else {
1087 * Non-standard thing, but needed by OffiX DND.
1088 * For when the icon frame gets a ClientMessage
1089 * that should have gone to the icon_window.
1091 if (XFindContext(dpy, event->xbutton.window, wWinContext,
1092 (XPointer *)&desc)!=XCNOENT) {
1093 struct WIcon *icon=NULL;
1095 if (desc->parent_type == WCLASS_MINIWINDOW) {
1096 icon = (WIcon*)desc->parent;
1097 } else if (desc->parent_type == WCLASS_DOCK_ICON
1098 || desc->parent_type == WCLASS_APPICON) {
1099 icon = ((WAppIcon*)desc->parent)->icon;
1101 if (icon && (wwin=icon->owner)) {
1102 if (wwin->client_win!=event->xclient.window) {
1103 event->xclient.window = wwin->client_win;
1104 XSendEvent(dpy, wwin->client_win, False, NoEventMask,
1105 event);
1113 static void
1114 raiseWindow(WScreen *scr)
1116 WWindow *wwin;
1118 scr->autoRaiseTimer = NULL;
1120 wwin = wWindowFor(scr->autoRaiseWindow);
1121 if (!wwin)
1122 return;
1124 if (!wwin->flags.destroyed && wwin->flags.focused) {
1125 wRaiseFrame(wwin->frame->core);
1126 /* this is needed or a race condition will occur */
1127 XSync(dpy, False);
1132 static void
1133 handleEnterNotify(XEvent *event)
1135 WWindow *wwin;
1136 WObjDescriptor *desc = NULL;
1137 #ifdef VIRTUAL_DESKTOP
1138 void (*vdHandler)(XEvent * event);
1139 #endif
1140 XEvent ev;
1141 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1142 #ifdef DEBUG
1143 printf("got enter notify\n");
1144 #endif
1146 #ifdef VIRTUAL_DESKTOP
1147 if (XFindContext(dpy, event->xcrossing.window, wVEdgeContext,
1148 (XPointer *)&vdHandler)!=XCNOENT) {
1149 (*vdHandler)(event);
1151 #endif
1153 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
1154 &ev)) {
1155 /* already left the window... */
1156 saveTimestamp(&ev);
1157 if (ev.xcrossing.mode==event->xcrossing.mode
1158 && ev.xcrossing.detail==event->xcrossing.detail) {
1159 return;
1163 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1164 (XPointer *)&desc)!=XCNOENT) {
1165 if(desc->handle_enternotify)
1166 (*desc->handle_enternotify)(desc, event);
1169 /* enter to window */
1170 wwin = wWindowFor(event->xcrossing.window);
1171 if (!wwin) {
1172 if (wPreferences.colormap_mode==WCM_POINTER) {
1173 wColormapInstallForWindow(scr, NULL);
1175 if (scr->autoRaiseTimer
1176 && event->xcrossing.root==event->xcrossing.window) {
1177 WMDeleteTimerHandler(scr->autoRaiseTimer);
1178 scr->autoRaiseTimer = NULL;
1180 } else {
1181 /* set auto raise timer even if in focus-follows-mouse mode
1182 * and the event is for the frame window, even if the window
1183 * has focus already. useful if you move the pointer from a focused
1184 * window to the root window and back pretty fast
1186 * set focus if in focus-follows-mouse mode and the event
1187 * is for the frame window and window doesn't have focus yet */
1188 if (wPreferences.focus_mode==WKF_SLOPPY
1189 && wwin->frame->core->window==event->xcrossing.window
1190 && !scr->flags.doing_alt_tab) {
1192 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1193 wSetFocusTo(scr, wwin);
1195 if (scr->autoRaiseTimer)
1196 WMDeleteTimerHandler(scr->autoRaiseTimer);
1197 scr->autoRaiseTimer = NULL;
1199 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1200 scr->autoRaiseWindow = wwin->frame->core->window;
1201 scr->autoRaiseTimer
1202 = WMAddTimerHandler(wPreferences.raise_delay,
1203 (WMCallback*)raiseWindow, scr);
1206 /* Install colormap for window, if the colormap installation mode
1207 * is colormap_follows_mouse */
1208 if (wPreferences.colormap_mode==WCM_POINTER) {
1209 if (wwin->client_win==event->xcrossing.window)
1210 wColormapInstallForWindow(scr, wwin);
1211 else
1212 wColormapInstallForWindow(scr, NULL);
1216 /* a little kluge to hide the clip balloon */
1217 if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1218 if (!desc) {
1219 XUnmapWindow(dpy, scr->clip_balloon);
1220 scr->flags.clip_balloon_mapped = 0;
1221 } else {
1222 if (desc->parent_type!=WCLASS_DOCK_ICON
1223 || scr->clip_icon != desc->parent) {
1224 XUnmapWindow(dpy, scr->clip_balloon);
1225 scr->flags.clip_balloon_mapped = 0;
1230 if (event->xcrossing.window == event->xcrossing.root
1231 && event->xcrossing.detail == NotifyNormal
1232 && event->xcrossing.detail != NotifyInferior
1233 && wPreferences.focus_mode != WKF_CLICK) {
1235 wSetFocusTo(scr, scr->focused_window);
1238 #ifdef BALLOON_TEXT
1239 wBalloonEnteredObject(scr, desc);
1240 #endif
1244 static void
1245 handleLeaveNotify(XEvent *event)
1247 WObjDescriptor *desc = NULL;
1249 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1250 (XPointer *)&desc)!=XCNOENT) {
1251 if(desc->handle_leavenotify)
1252 (*desc->handle_leavenotify)(desc, event);
1257 #ifdef SHAPE
1258 static void
1259 handleShapeNotify(XEvent *event)
1261 XShapeEvent *shev = (XShapeEvent*)event;
1262 WWindow *wwin;
1263 XEvent ev;
1264 #ifdef DEBUG
1265 printf("got shape notify\n");
1266 #endif
1267 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1268 XShapeEvent *sev = (XShapeEvent*)&ev;
1270 if (sev->kind == ShapeBounding) {
1271 if (sev->shaped == shev->shaped) {
1272 *shev = *sev;
1273 } else {
1274 XPutBackEvent(dpy, &ev);
1275 break;
1280 wwin = wWindowFor(shev->window);
1281 if (!wwin || shev->kind != ShapeBounding)
1282 return;
1284 if (!shev->shaped && wwin->flags.shaped) {
1286 wwin->flags.shaped = 0;
1287 wWindowClearShape(wwin);
1289 } else if (shev->shaped) {
1291 wwin->flags.shaped = 1;
1292 wWindowSetShape(wwin);
1295 #endif /* SHAPE */
1297 #ifdef KEEP_XKB_LOCK_STATUS
1298 /* please help ]d if you know what to do */
1299 handleXkbIndicatorStateNotify(XEvent *event)
1301 WWindow *wwin;
1302 WScreen *scr;
1303 XkbStateRec staterec;
1304 int i;
1306 for (i=0; i<wScreenCount; i++) {
1307 scr = wScreenWithNumber(i);
1308 wwin = scr->focused_window;
1309 if (wwin && wwin->flags.focused) {
1310 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1311 if (wwin->frame->languagemode != staterec.group) {
1312 wwin->frame->last_languagemode = wwin->frame->languagemode;
1313 wwin->frame->languagemode = staterec.group;
1315 #ifdef XKB_BUTTON_HINT
1316 if (wwin->frame->titlebar) {
1317 wFrameWindowPaint(wwin->frame);
1319 #endif
1323 #endif /*KEEP_XKB_LOCK_STATUS*/
1325 static void
1326 handleColormapNotify(XEvent *event)
1328 WWindow *wwin;
1329 WScreen *scr;
1330 Bool reinstall = False;
1332 wwin = wWindowFor(event->xcolormap.window);
1333 if (!wwin)
1334 return;
1336 scr = wwin->screen_ptr;
1338 do {
1339 if (wwin) {
1340 if (event->xcolormap.new) {
1341 XWindowAttributes attr;
1343 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1345 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1346 scr->current_colormap = attr.colormap;
1348 reinstall = True;
1349 } else if (event->xcolormap.state == ColormapUninstalled &&
1350 scr->current_colormap == event->xcolormap.colormap) {
1352 /* some bastard app (like XV) removed our colormap */
1354 * can't enforce or things like xscreensaver wont work
1355 * reinstall = True;
1357 } else if (event->xcolormap.state == ColormapInstalled &&
1358 scr->current_colormap == event->xcolormap.colormap) {
1360 /* someone has put our colormap back */
1361 reinstall = False;
1364 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1365 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1367 if (reinstall && scr->current_colormap!=None) {
1368 if (!scr->flags.colormap_stuff_blocked)
1369 XInstallColormap(dpy, scr->current_colormap);
1375 static void
1376 handleFocusIn(XEvent *event)
1378 WWindow *wwin;
1381 * For applications that like stealing the focus.
1383 while (XCheckTypedEvent(dpy, FocusIn, event));
1384 saveTimestamp(event);
1385 if (event->xfocus.mode == NotifyUngrab
1386 || event->xfocus.mode == NotifyGrab
1387 || event->xfocus.detail > NotifyNonlinearVirtual) {
1388 return;
1391 wwin = wWindowFor(event->xfocus.window);
1392 if (wwin && !wwin->flags.focused) {
1393 if (wwin->flags.mapped)
1394 wSetFocusTo(wwin->screen_ptr, wwin);
1395 else
1396 wSetFocusTo(wwin->screen_ptr, NULL);
1397 } else if (!wwin) {
1398 WScreen *scr = wScreenForWindow(event->xfocus.window);
1399 if (scr)
1400 wSetFocusTo(scr, NULL);
1405 static WWindow*
1406 windowUnderPointer(WScreen *scr)
1408 unsigned int mask;
1409 int foo;
1410 Window bar, win;
1412 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
1413 &mask))
1414 return wWindowFor(win);
1415 return NULL;
1419 static int CheckFullScreenWindowFocused(WScreen *scr)
1421 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1422 return 1;
1423 else
1424 return 0;
1428 static void
1429 handleKeyPress(XEvent *event)
1431 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1432 WWindow *wwin = scr->focused_window;
1433 int i;
1434 int modifiers;
1435 int command=-1, widx;
1436 #ifdef KEEP_XKB_LOCK_STATUS
1437 XkbStateRec staterec;
1438 #endif /*KEEP_XKB_LOCK_STATUS*/
1440 /* ignore CapsLock */
1441 modifiers = event->xkey.state & ValidModMask;
1443 for (i=0; i<WKBD_LAST; i++) {
1444 if (wKeyBindings[i].keycode==0)
1445 continue;
1447 if (wKeyBindings[i].keycode==event->xkey.keycode
1448 && (/*wKeyBindings[i].modifier==0
1449 ||*/ wKeyBindings[i].modifier==modifiers)) {
1450 command = i;
1451 break;
1456 if (command < 0) {
1457 #ifdef LITE
1459 #if 0
1461 #endif
1462 #else
1463 if (!wRootMenuPerformShortcut(event)) {
1464 #endif
1465 static int dontLoop = 0;
1467 if (dontLoop > 10) {
1468 wwarning("problem with key event processing code");
1469 return;
1471 dontLoop++;
1472 /* if the focused window is an internal window, try redispatching
1473 * the event to the managed window, as it can be a WINGs window */
1474 if (wwin && wwin->flags.internal_window
1475 && wwin->client_leader!=None) {
1476 /* client_leader contains the WINGs toplevel */
1477 event->xany.window = wwin->client_leader;
1478 WMHandleEvent(event);
1480 dontLoop--;
1482 return;
1485 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1486 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1488 switch (command) {
1489 #ifndef LITE
1490 case WKBD_ROOTMENU:
1491 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True);*/
1492 if (!CheckFullScreenWindowFocused(scr)) {
1493 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1494 OpenRootMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1496 break;
1497 case WKBD_WINDOWLIST:
1498 if (!CheckFullScreenWindowFocused(scr)) {
1499 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1500 OpenSwitchMenu(scr, rect.pos.x + rect.size.width/2, rect.pos.y + rect.size.height/2, True);
1502 break;
1503 #endif /* !LITE */
1504 case WKBD_WINDOWMENU:
1505 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1506 OpenWindowMenu(wwin, wwin->frame_x,
1507 wwin->frame_y+wwin->frame->top_width, True);
1508 break;
1509 case WKBD_MINIATURIZE:
1510 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1511 && !WFLAGP(wwin, no_miniaturizable)) {
1512 CloseWindowMenu(scr);
1514 if (wwin->protocols.MINIATURIZE_WINDOW)
1515 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
1516 event->xbutton.time);
1517 else {
1518 wIconifyWindow(wwin);
1521 break;
1522 case WKBD_HIDE:
1523 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1524 WApplication *wapp = wApplicationOf(wwin->main_window);
1525 CloseWindowMenu(scr);
1527 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1528 wHideApplication(wapp);
1531 break;
1532 case WKBD_HIDE_OTHERS:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1534 CloseWindowMenu(scr);
1536 wHideOtherApplications(wwin);
1538 break;
1539 case WKBD_MAXIMIZE:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1541 int newdir = (MAX_VERTICAL|MAX_HORIZONTAL);
1543 CloseWindowMenu(scr);
1545 if (wwin->flags.maximized == newdir) {
1546 wUnmaximizeWindow(wwin);
1547 } else {
1548 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1551 break;
1552 case WKBD_VMAXIMIZE:
1553 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1554 int newdir = (MAX_VERTICAL ^ wwin->flags.maximized);
1556 CloseWindowMenu(scr);
1558 if (newdir) {
1559 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1560 } else {
1561 wUnmaximizeWindow(wwin);
1564 break;
1565 case WKBD_HMAXIMIZE:
1566 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1567 int newdir = (MAX_HORIZONTAL ^ wwin->flags.maximized);
1569 CloseWindowMenu(scr);
1571 if (newdir) {
1572 wMaximizeWindow(wwin, newdir|MAX_KEYBOARD);
1573 } else {
1574 wUnmaximizeWindow(wwin);
1577 break;
1578 case WKBD_RAISE:
1579 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1580 CloseWindowMenu(scr);
1582 wRaiseFrame(wwin->frame->core);
1584 break;
1585 case WKBD_LOWER:
1586 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1587 CloseWindowMenu(scr);
1589 wLowerFrame(wwin->frame->core);
1591 break;
1592 case WKBD_RAISELOWER:
1593 /* raise or lower the window under the pointer, not the
1594 * focused one
1596 wwin = windowUnderPointer(scr);
1597 if (wwin)
1598 wRaiseLowerFrame(wwin->frame->core);
1599 break;
1600 case WKBD_SHADE:
1601 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1602 if (wwin->flags.shaded)
1603 wUnshadeWindow(wwin);
1604 else
1605 wShadeWindow(wwin);
1607 break;
1608 case WKBD_MOVERESIZE:
1609 if (ISMAPPED(wwin) && ISFOCUSED(wwin) &&
1610 (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1611 CloseWindowMenu(scr);
1613 wKeyboardMoveResizeWindow(wwin);
1615 break;
1616 case WKBD_CLOSE:
1617 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1618 CloseWindowMenu(scr);
1619 if (wwin->protocols.DELETE_WINDOW)
1620 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW,
1621 event->xkey.time);
1623 break;
1624 case WKBD_SELECT:
1625 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1626 wSelectWindow(wwin, !wwin->flags.selected);
1628 break;
1629 case WKBD_FOCUSNEXT:
1630 StartWindozeCycle(wwin, event, True);
1631 break;
1633 case WKBD_FOCUSPREV:
1634 StartWindozeCycle(wwin, event, False);
1635 break;
1637 #if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
1638 #define GOTOWORKS(wk) case WKBD_WORKSPACE##wk:\
1639 i = (scr->current_workspace/10)*10 + wk - 1;\
1640 if (wPreferences.ws_advance || i<scr->workspace_count)\
1641 wWorkspaceChange(scr, i);\
1642 break
1643 #else
1644 #define GOTOWORKS(wk) case WKBD_WORKSPACE/**/wk:\
1645 i = (scr->current_workspace/10)*10 + wk - 1;\
1646 if (wPreferences.ws_advance || i<scr->workspace_count)\
1647 wWorkspaceChange(scr, i);\
1648 break
1649 #endif
1650 GOTOWORKS(1);
1651 GOTOWORKS(2);
1652 GOTOWORKS(3);
1653 GOTOWORKS(4);
1654 GOTOWORKS(5);
1655 GOTOWORKS(6);
1656 GOTOWORKS(7);
1657 GOTOWORKS(8);
1658 GOTOWORKS(9);
1659 GOTOWORKS(10);
1660 #undef GOTOWORKS
1661 case WKBD_NEXTWORKSPACE:
1662 wWorkspaceRelativeChange(scr, 1);
1663 break;
1664 case WKBD_PREVWORKSPACE:
1665 wWorkspaceRelativeChange(scr, -1);
1666 break;
1667 case WKBD_WINDOW1:
1668 case WKBD_WINDOW2:
1669 case WKBD_WINDOW3:
1670 case WKBD_WINDOW4:
1671 case WKBD_WINDOW5:
1672 case WKBD_WINDOW6:
1673 case WKBD_WINDOW7:
1674 case WKBD_WINDOW8:
1675 case WKBD_WINDOW9:
1676 case WKBD_WINDOW10:
1678 widx = command-WKBD_WINDOW1;
1680 if (scr->shortcutWindows[widx]) {
1681 WMArray *list = scr->shortcutWindows[widx];
1682 int cw;
1683 int count = WMGetArrayItemCount(list);
1684 WWindow *twin;
1685 WMArrayIterator iter;
1686 WWindow *wwin;
1688 wUnselectWindows(scr);
1689 cw = scr->current_workspace;
1691 WM_ETARETI_ARRAY(list, wwin, iter) {
1692 if (count > 1)
1693 wWindowChangeWorkspace(wwin, cw);
1695 wMakeWindowVisible(wwin);
1697 if (count > 1)
1698 wSelectWindow(wwin, True);
1701 /* rotate the order of windows, to create a cycling effect */
1702 twin = WMGetFromArray(list, 0);
1703 WMDeleteFromArray(list, 0);
1704 WMAddToArray(list, twin);
1706 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1707 if (scr->shortcutWindows[widx]) {
1708 WMFreeArray(scr->shortcutWindows[widx]);
1709 scr->shortcutWindows[widx] = NULL;
1712 if (wwin->flags.selected && scr->selected_windows) {
1713 scr->shortcutWindows[widx] =
1714 WMDuplicateArray(scr->selected_windows);
1715 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1716 WMInsertInArray(scr->shortcutWindows[index], 0, wwin);*/
1717 } else {
1718 scr->shortcutWindows[widx] = WMCreateArray(4);
1719 WMAddToArray(scr->shortcutWindows[widx], wwin);
1722 wSelectWindow(wwin, !wwin->flags.selected);
1723 XFlush(dpy);
1724 wusleep(3000);
1725 wSelectWindow(wwin, !wwin->flags.selected);
1726 XFlush(dpy);
1728 } else if (scr->selected_windows
1729 && WMGetArrayItemCount(scr->selected_windows)) {
1731 if (wwin->flags.selected && scr->selected_windows) {
1732 if (scr->shortcutWindows[widx]) {
1733 WMFreeArray(scr->shortcutWindows[widx]);
1735 scr->shortcutWindows[widx] =
1736 WMDuplicateArray(scr->selected_windows);
1740 break;
1742 case WKBD_SWITCH_SCREEN:
1743 if (wScreenCount > 1) {
1744 WScreen *scr2;
1745 int i;
1747 /* find index of this screen */
1748 for (i = 0; i < wScreenCount; i++) {
1749 if (wScreenWithNumber(i) == scr)
1750 break;
1752 i++;
1753 if (i >= wScreenCount) {
1754 i = 0;
1756 scr2 = wScreenWithNumber(i);
1758 if (scr2) {
1759 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1760 scr2->scr_width/2, scr2->scr_height/2);
1763 break;
1765 case WKBD_NEXTWSLAYER:
1766 case WKBD_PREVWSLAYER:
1768 int row, column;
1770 row = scr->current_workspace/10;
1771 column = scr->current_workspace%10;
1773 if (command==WKBD_NEXTWSLAYER) {
1774 if ((row+1)*10 < scr->workspace_count)
1775 wWorkspaceChange(scr, column+(row+1)*10);
1776 } else {
1777 if (row > 0)
1778 wWorkspaceChange(scr, column+(row-1)*10);
1781 break;
1782 case WKBD_CLIPLOWER:
1783 if (!wPreferences.flags.noclip)
1784 wDockLower(scr->workspaces[scr->current_workspace]->clip);
1785 break;
1786 case WKBD_CLIPRAISE:
1787 if (!wPreferences.flags.noclip)
1788 wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1789 break;
1790 case WKBD_CLIPRAISELOWER:
1791 if (!wPreferences.flags.noclip)
1792 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1793 break;
1794 #ifdef KEEP_XKB_LOCK_STATUS
1795 case WKBD_TOGGLE:
1796 if(wPreferences.modelock) {
1797 /*toggle*/
1798 wwin = scr->focused_window;
1800 if (wwin && wwin->flags.mapped
1801 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1802 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1803 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1805 wwin->frame->languagemode = wwin->frame->last_languagemode;
1806 wwin->frame->last_languagemode = staterec.group;
1807 XkbLockGroup(dpy,XkbUseCoreKbd, wwin->frame->languagemode);
1811 break;
1812 #endif /* KEEP_XKB_LOCK_STATUS */
1813 #ifdef VIRTUAL_DESKTOP
1814 case WKBD_VDESK_LEFT:
1815 wWorkspaceKeyboardMoveDesktop(scr, VEC_LEFT);
1816 break;
1818 case WKBD_VDESK_RIGHT:
1819 wWorkspaceKeyboardMoveDesktop(scr, VEC_RIGHT);
1820 break;
1822 case WKBD_VDESK_UP:
1823 wWorkspaceKeyboardMoveDesktop(scr, VEC_UP);
1824 break;
1826 case WKBD_VDESK_DOWN:
1827 wWorkspaceKeyboardMoveDesktop(scr, VEC_DOWN);
1828 break;
1829 #endif
1835 static void
1836 handleMotionNotify(XEvent *event)
1838 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1840 if (wPreferences.scrollable_menus) {
1841 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1842 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1844 if (scr->flags.jump_back_pending ||
1845 p.x <= (rect.pos.x + 1) ||
1846 p.x >= (rect.pos.x + rect.size.width - 2) ||
1847 p.y <= (rect.pos.y + 1) ||
1848 p.y >= (rect.pos.y + rect.size.height - 2)) {
1849 WMenu *menu;
1850 #ifdef DEBUG
1851 printf("pointer at screen edge\n");
1852 #endif
1853 menu = wMenuUnderPointer(scr);
1854 if (menu!=NULL)
1855 wMenuScroll(menu, event);
1861 static void
1862 handleVisibilityNotify(XEvent *event)
1864 WWindow *wwin;
1866 wwin = wWindowFor(event->xvisibility.window);
1867 if (!wwin)
1868 return;
1869 wwin->flags.obscured =
1870 (event->xvisibility.state == VisibilityFullyObscured);