New shutdown.h file
[wmaker-crm.git] / src / event.c
blobace2a3cf3a5b02e29459673272b189d1066699fa
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #ifdef HAVE_INOTIFY
25 #include <sys/select.h>
26 #include <sys/inotify.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #ifdef SHAPE
39 # include <X11/extensions/shape.h>
40 #endif
41 #ifdef XDND
42 #include "xdnd.h"
43 #endif
45 #ifdef HAVE_XRANDR
46 #include <X11/extensions/Xrandr.h>
47 #endif
49 #ifdef KEEP_XKB_LOCK_STATUS
50 #include <X11/XKBlib.h>
51 #endif /* KEEP_XKB_LOCK_STATUS */
53 #include "WindowMaker.h"
54 #include "window.h"
55 #include "actions.h"
56 #include "client.h"
57 #include "main.h"
58 #include "keybind.h"
59 #include "application.h"
60 #include "stacking.h"
61 #include "defaults.h"
62 #include "workspace.h"
63 #include "dock.h"
64 #include "framewin.h"
65 #include "properties.h"
66 #include "balloon.h"
67 #include "xinerama.h"
68 #include "wmspec.h"
69 #include "rootmenu.h"
70 #include "colormap.h"
71 #include "screen.h"
72 #include "shutdown.h"
74 /******** Global Variables **********/
75 extern XContext wWinContext;
76 extern XContext wVEdgeContext;
78 extern Cursor wCursor[WCUR_LAST];
80 extern WShortKey wKeyBindings[WKBD_LAST];
81 extern int wScreenCount;
82 extern Time LastTimestamp;
83 extern Time LastFocusChange;
85 extern WPreferences wPreferences;
87 #define MOD_MASK wPreferences.modifier_mask
89 extern Atom _XA_WM_COLORMAP_NOTIFY;
91 extern Atom _XA_WM_CHANGE_STATE;
92 extern Atom _XA_WM_DELETE_WINDOW;
93 extern Atom _XA_GNUSTEP_WM_ATTR;
94 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
95 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
96 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
97 extern Atom _XA_WINDOWMAKER_COMMAND;
98 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
100 #ifdef SHAPE
101 extern Bool wShapeSupported;
102 extern int wShapeEventBase;
103 #endif
105 #ifdef KEEP_XKB_LOCK_STATUS
106 extern int wXkbEventBase;
107 #endif
109 /************ Local stuff ***********/
111 static void saveTimestamp(XEvent *event);
112 static void handleColormapNotify(XEvent *event);
113 static void handleMapNotify(XEvent *event);
114 static void handleUnmapNotify(XEvent *event);
115 static void handleButtonPress(XEvent *event);
116 static void handleExpose(XEvent *event);
117 static void handleDestroyNotify(XEvent *event);
118 static void handleConfigureRequest(XEvent *event);
119 static void handleMapRequest(XEvent *event);
120 static void handlePropertyNotify(XEvent *event);
121 static void handleEnterNotify(XEvent *event);
122 static void handleLeaveNotify(XEvent *event);
123 static void handleExtensions(XEvent *event);
124 static void handleClientMessage(XEvent *event);
125 static void handleKeyPress(XEvent *event);
126 static void handleFocusIn(XEvent *event);
127 static void handleMotionNotify(XEvent *event);
128 static void handleVisibilityNotify(XEvent *event);
129 static void handle_inotify_events(int fd, int wd);
130 static void wdelete_death_handler(WMagicNumber id);
133 #ifdef SHAPE
134 static void handleShapeNotify(XEvent *event);
135 #endif
137 #ifdef KEEP_XKB_LOCK_STATUS
138 static void handleXkbIndicatorStateNotify(XEvent *event);
139 #endif
141 /* called from the signal handler */
142 void NotifyDeadProcess(pid_t pid, unsigned char status);
144 /* real dead process handler */
145 static void handleDeadProcess(void *foo);
147 typedef struct DeadProcesses {
148 pid_t pid;
149 unsigned char exit_status;
150 } DeadProcesses;
152 /* stack of dead processes */
153 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
154 static int deadProcessPtr = 0;
156 typedef struct DeathHandler {
157 WDeathHandler *callback;
158 pid_t pid;
159 void *client_data;
160 } DeathHandler;
162 static WMArray *deathHandlers = NULL;
164 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
166 DeathHandler *handler;
168 handler = malloc(sizeof(DeathHandler));
169 if (!handler)
170 return 0;
172 handler->pid = pid;
173 handler->callback = callback;
174 handler->client_data = cdata;
176 if (!deathHandlers)
177 deathHandlers = WMCreateArrayWithDestructor(8, free);
179 WMAddToArray(deathHandlers, handler);
181 return handler;
184 static void wdelete_death_handler(WMagicNumber id)
186 DeathHandler *handler = (DeathHandler *) id;
188 if (!handler || !deathHandlers)
189 return;
191 /* array destructor will call free(handler) */
192 WMRemoveFromArray(deathHandlers, handler);
195 void DispatchEvent(XEvent * event)
197 if (deathHandlers)
198 handleDeadProcess(NULL);
200 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
201 WCHANGE_STATE(WSTATE_EXITING);
202 /* received SIGTERM */
204 * WMHandleEvent() can't be called from anything
205 * executed inside here, or we can get in a infinite
206 * recursive loop.
208 Shutdown(WSExitMode);
210 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
211 WCHANGE_STATE(WSTATE_RESTARTING);
213 Shutdown(WSRestartPreparationMode);
214 /* received SIGHUP */
215 Restart(NULL, True);
216 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
217 WCHANGE_STATE(WSTATE_NORMAL);
218 wDefaultsCheckDomains(NULL);
221 /* for the case that all that is wanted to be dispatched is
222 * the stuff above */
223 if (!event)
224 return;
226 saveTimestamp(event);
227 switch (event->type) {
228 case MapRequest:
229 handleMapRequest(event);
230 break;
232 case KeyPress:
233 handleKeyPress(event);
234 break;
236 case MotionNotify:
237 handleMotionNotify(event);
238 break;
240 case ConfigureRequest:
241 handleConfigureRequest(event);
242 break;
244 case DestroyNotify:
245 handleDestroyNotify(event);
246 break;
248 case MapNotify:
249 handleMapNotify(event);
250 break;
252 case UnmapNotify:
253 handleUnmapNotify(event);
254 break;
256 case ButtonPress:
257 handleButtonPress(event);
258 break;
260 case Expose:
261 handleExpose(event);
262 break;
264 case PropertyNotify:
265 handlePropertyNotify(event);
266 break;
268 case EnterNotify:
269 handleEnterNotify(event);
270 break;
272 case LeaveNotify:
273 handleLeaveNotify(event);
274 break;
276 case ClientMessage:
277 handleClientMessage(event);
278 break;
280 case ColormapNotify:
281 handleColormapNotify(event);
282 break;
284 case MappingNotify:
285 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
286 XRefreshKeyboardMapping(&event->xmapping);
287 break;
289 case FocusIn:
290 handleFocusIn(event);
291 break;
293 case VisibilityNotify:
294 handleVisibilityNotify(event);
295 break;
297 case ConfigureNotify:
298 #ifdef HAVE_XRANDR
299 if (event->xconfigure.window == DefaultRootWindow(dpy))
300 XRRUpdateConfiguration(event);
301 #endif
302 break;
304 default:
305 handleExtensions(event);
306 break;
310 #ifdef HAVE_INOTIFY
312 *----------------------------------------------------------------------
313 * handle_inotify_events-
314 * Check for inotify events
316 * Returns:
317 * After reading events for the given file descriptor (fd) and
318 * watch descriptor (wd)
320 * Side effects:
321 * Calls wDefaultsCheckDomains if config database is updated
322 *----------------------------------------------------------------------
324 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
325 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
326 static void handle_inotify_events(int fd, int wd)
328 extern void wDefaultsCheckDomains(void *);
329 ssize_t eventQLength, i = 0;
330 char buff[BUFF_SIZE] = { 0 };
331 /* Check config only once per read of the event queue */
332 int oneShotFlag = 0;
335 * Read off the queued events
336 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
337 * not occur; the block is on Xevents, but a config file change will normally
338 * occur as a result of an Xevent - so the event queue should never have more than
339 * a few entries before a read().
341 eventQLength = read(fd, buff, BUFF_SIZE);
343 /* check what events occured */
344 /* Should really check wd here too, but for now we only have one watch! */
345 while (i < eventQLength) {
346 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
349 * see inotify.h for event types.
351 if (pevent->mask & IN_DELETE_SELF) {
352 wwarning(_("the defaults database has been deleted!"
353 " Restart Window Maker to create the database" " with the default settings"));
354 close(fd);
356 if (pevent->mask & IN_UNMOUNT) {
357 wwarning(_("the unit containing the defaults database has"
358 " been unmounted. Setting --static mode." " Any changes will not be saved."));
359 close(fd);
360 wPreferences.flags.noupdates = 1;
362 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
363 wwarning(_("Inotify: Reading config files in defaults database."));
364 wDefaultsCheckDomains(NULL);
367 /* move to next event in the buffer */
368 i += sizeof(struct inotify_event) + pevent->len;
371 #endif /* HAVE_INOTIFY */
374 *----------------------------------------------------------------------
375 * EventLoop-
376 * Processes X and internal events indefinitely.
378 * Returns:
379 * Never returns
381 * Side effects:
382 * The LastTimestamp global variable is updated.
383 * Calls inotifyGetEvents if defaults database changes.
384 *----------------------------------------------------------------------
386 void EventLoop(void)
388 XEvent event;
389 #ifdef HAVE_INOTIFY
390 extern int inotifyFD;
391 extern int inotifyWD;
392 struct timeval time;
393 fd_set rfds;
394 int retVal = 0;
396 if (inotifyFD < 0 || inotifyWD < 0)
397 retVal = -1;
398 #endif
400 for (;;) {
402 WMNextEvent(dpy, &event); /* Blocks here */
403 WMHandleEvent(&event);
404 #ifdef HAVE_INOTIFY
405 if (retVal != -1) {
406 time.tv_sec = 0;
407 time.tv_usec = 0;
408 FD_ZERO(&rfds);
409 FD_SET(inotifyFD, &rfds);
411 /* check for available read data from inotify - don't block! */
412 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
414 if (retVal < 0) { /* an error has occured */
415 wwarning(_("select failed. The inotify instance will be closed."
416 " Changes to the defaults database will require"
417 " a restart to take effect."));
418 close(inotifyFD);
419 continue;
421 if (FD_ISSET(inotifyFD, &rfds))
422 handle_inotify_events(inotifyFD, inotifyWD);
424 #endif
429 *----------------------------------------------------------------------
430 * ProcessPendingEvents --
431 * Processes the events that are currently pending (at the time
432 * this function is called) in the display's queue.
434 * Returns:
435 * After the pending events that were present at the function call
436 * are processed.
438 * Side effects:
439 * Many -- whatever handling events may involve.
441 *----------------------------------------------------------------------
443 void ProcessPendingEvents(void)
445 XEvent event;
446 int count;
448 XSync(dpy, False);
450 /* Take a snapshot of the event count in the queue */
451 count = XPending(dpy);
453 while (count > 0 && XPending(dpy)) {
454 WMNextEvent(dpy, &event);
455 WMHandleEvent(&event);
456 count--;
460 Bool IsDoubleClick(WScreen * scr, XEvent * event)
462 if ((scr->last_click_time > 0) &&
463 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
464 && (event->xbutton.button == scr->last_click_button)
465 && (event->xbutton.window == scr->last_click_window)) {
467 scr->flags.next_click_is_not_double = 1;
468 scr->last_click_time = 0;
469 scr->last_click_window = event->xbutton.window;
471 return True;
473 return False;
476 void NotifyDeadProcess(pid_t pid, unsigned char status)
478 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
479 wwarning("stack overflow: too many dead processes");
480 return;
482 /* stack the process to be handled later,
483 * as this is called from the signal handler */
484 deadProcesses[deadProcessPtr].pid = pid;
485 deadProcesses[deadProcessPtr].exit_status = status;
486 deadProcessPtr++;
489 static void 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, tmp->client_data);
515 wdelete_death_handler(tmp);
521 static void saveTimestamp(XEvent * event)
524 * Never save CurrentTime as LastTimestamp because CurrentTime
525 * it's not a real timestamp (it's the 0L constant)
528 switch (event->type) {
529 case ButtonRelease:
530 case ButtonPress:
531 LastTimestamp = event->xbutton.time;
532 break;
533 case KeyPress:
534 case KeyRelease:
535 LastTimestamp = event->xkey.time;
536 break;
537 case MotionNotify:
538 LastTimestamp = event->xmotion.time;
539 break;
540 case PropertyNotify:
541 LastTimestamp = event->xproperty.time;
542 break;
543 case EnterNotify:
544 case LeaveNotify:
545 LastTimestamp = event->xcrossing.time;
546 break;
547 case SelectionClear:
548 LastTimestamp = event->xselectionclear.time;
549 break;
550 case SelectionRequest:
551 LastTimestamp = event->xselectionrequest.time;
552 break;
553 case SelectionNotify:
554 LastTimestamp = event->xselection.time;
555 #ifdef XDND
556 wXDNDProcessSelection(event);
557 #endif
558 break;
562 static int matchWindow(const void *item, const void *cdata)
564 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
567 static void handleExtensions(XEvent * event)
569 #ifdef KEEP_XKB_LOCK_STATUS
570 XkbEvent *xkbevent;
571 xkbevent = (XkbEvent *) event;
572 #endif /*KEEP_XKB_LOCK_STATUS */
573 #ifdef SHAPE
574 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
575 handleShapeNotify(event);
577 #endif
578 #ifdef KEEP_XKB_LOCK_STATUS
579 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
580 handleXkbIndicatorStateNotify(event);
582 #endif /*KEEP_XKB_LOCK_STATUS */
583 #ifdef HAVE_XRANDR
584 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
585 /* From xrandr man page: "Clients must call back into Xlib using
586 * XRRUpdateConfiguration when screen configuration change notify
587 * events are generated */
588 XRRUpdateConfiguration(event);
589 WCHANGE_STATE(WSTATE_RESTARTING);
590 Shutdown(WSRestartPreparationMode);
591 Restart(NULL,True);
593 #endif
596 static void handleMapRequest(XEvent * ev)
598 WWindow *wwin;
599 WScreen *scr = NULL;
600 Window window = ev->xmaprequest.window;
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);
668 static void handleDestroyNotify(XEvent * event)
670 WWindow *wwin;
671 WApplication *app;
672 Window window = event->xdestroywindow.window;
673 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
674 int widx;
676 wwin = wWindowFor(window);
677 if (wwin) {
678 wUnmanageWindow(wwin, False, True);
681 if (scr != NULL) {
682 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
683 WFakeGroupLeader *fPtr;
685 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
686 if (fPtr->retainCount > 0) {
687 fPtr->retainCount--;
688 if (fPtr->retainCount == 0 && fPtr->leader != None) {
689 XDestroyWindow(dpy, fPtr->leader);
690 fPtr->leader = None;
691 XFlush(dpy);
694 fPtr->origLeader = None;
698 app = wApplicationOf(window);
699 if (app) {
700 if (window == app->main_window) {
701 app->refcount = 0;
702 wwin = app->main_window_desc->screen_ptr->focused_window;
703 while (wwin) {
704 if (wwin->main_window == window) {
705 wwin->main_window = None;
707 wwin = wwin->prev;
710 wApplicationDestroy(app);
714 static void handleExpose(XEvent * event)
716 WObjDescriptor *desc;
717 XEvent ev;
719 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
721 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
722 return;
725 if (desc->handle_expose) {
726 (*desc->handle_expose) (desc, event);
730 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
732 switch (action) {
733 case WA_SELECT_WINDOWS:
734 wUnselectWindows(scr);
735 wSelectWindows(scr, event);
736 break;
737 case WA_OPEN_APPMENU:
738 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
739 /* ugly hack */
740 if (scr->root_menu) {
741 if (scr->root_menu->brother->flags.mapped)
742 event->xbutton.window = scr->root_menu->brother->frame->core->window;
743 else
744 event->xbutton.window = scr->root_menu->frame->core->window;
746 break;
747 case WA_OPEN_WINLISTMENU:
748 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
749 if (scr->switch_menu) {
750 if (scr->switch_menu->brother->flags.mapped)
751 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
752 else
753 event->xbutton.window = scr->switch_menu->frame->core->window;
755 break;
756 default:
757 break;
761 /* bindable */
762 static void handleButtonPress(XEvent * event)
764 WObjDescriptor *desc;
765 WScreen *scr;
767 scr = wScreenForRootWindow(event->xbutton.root);
769 #ifdef BALLOON_TEXT
770 wBalloonHide(scr);
771 #endif
773 if (event->xbutton.window == scr->root_win) {
774 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
775 executeButtonAction(scr, event, wPreferences.mouse_button1);
776 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
777 executeButtonAction(scr, event, wPreferences.mouse_button2);
778 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
779 executeButtonAction(scr, event, wPreferences.mouse_button3);
780 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
781 wWorkspaceRelativeChange(scr, 1);
782 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
783 wWorkspaceRelativeChange(scr, -1);
787 desc = NULL;
788 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
789 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
790 return;
794 if (desc->parent_type == WCLASS_WINDOW) {
795 XSync(dpy, 0);
797 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
798 XAllowEvents(dpy, AsyncPointer, CurrentTime);
799 } else {
800 /* if (wPreferences.focus_mode == WKF_CLICK) { */
801 if (wPreferences.ignore_focus_click) {
802 XAllowEvents(dpy, AsyncPointer, CurrentTime);
804 XAllowEvents(dpy, ReplayPointer, CurrentTime);
805 /* } */
807 XSync(dpy, 0);
808 } else if (desc->parent_type == WCLASS_APPICON
809 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
810 if (event->xbutton.state & MOD_MASK) {
811 XSync(dpy, 0);
812 XAllowEvents(dpy, AsyncPointer, CurrentTime);
813 XSync(dpy, 0);
817 if (desc->handle_mousedown != NULL) {
818 (*desc->handle_mousedown) (desc, event);
821 /* save double-click information */
822 if (scr->flags.next_click_is_not_double) {
823 scr->flags.next_click_is_not_double = 0;
824 } else {
825 scr->last_click_time = event->xbutton.time;
826 scr->last_click_button = event->xbutton.button;
827 scr->last_click_window = event->xbutton.window;
831 static void handleMapNotify(XEvent * event)
833 WWindow *wwin;
835 wwin = wWindowFor(event->xmap.event);
836 if (wwin && wwin->client_win == event->xmap.event) {
837 if (wwin->flags.miniaturized) {
838 wDeiconifyWindow(wwin);
839 } else {
840 XGrabServer(dpy);
841 wWindowMap(wwin);
842 wClientSetState(wwin, NormalState, None);
843 XUngrabServer(dpy);
848 static void handleUnmapNotify(XEvent * event)
850 WWindow *wwin;
851 XEvent ev;
852 Bool withdraw = False;
854 /* only process windows with StructureNotify selected
855 * (ignore SubstructureNotify) */
856 wwin = wWindowFor(event->xunmap.window);
857 if (!wwin)
858 return;
860 /* whether the event is a Withdrawal request */
861 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
862 withdraw = True;
864 if (wwin->client_win != event->xunmap.event && !withdraw)
865 return;
867 if (!wwin->flags.mapped && !withdraw
868 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
869 && !wwin->flags.miniaturized && !wwin->flags.hidden)
870 return;
872 XGrabServer(dpy);
873 XUnmapWindow(dpy, wwin->frame->core->window);
874 wwin->flags.mapped = 0;
875 XSync(dpy, 0);
876 /* check if the window was destroyed */
877 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
878 DispatchEvent(&ev);
879 } else {
880 Bool reparented = False;
882 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
883 reparented = True;
885 /* withdraw window */
886 wwin->flags.mapped = 0;
887 if (!reparented)
888 wClientSetState(wwin, WithdrawnState, None);
890 /* if the window was reparented, do not reparent it back to the
891 * root window */
892 wUnmanageWindow(wwin, !reparented, False);
894 XUngrabServer(dpy);
897 static void handleConfigureRequest(XEvent * event)
899 WWindow *wwin;
901 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
903 * Configure request for unmapped window
905 wClientConfigure(NULL, &(event->xconfigurerequest));
906 } else {
907 wClientConfigure(wwin, &(event->xconfigurerequest));
911 static void handlePropertyNotify(XEvent * event)
913 WWindow *wwin;
914 WApplication *wapp;
915 Window jr;
916 int ji;
917 unsigned int ju;
919 wwin = wWindowFor(event->xproperty.window);
920 if (wwin) {
921 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
922 return;
924 wClientCheckProperty(wwin, &event->xproperty);
926 wapp = wApplicationOf(event->xproperty.window);
927 if (wapp) {
928 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
932 static void handleClientMessage(XEvent * event)
934 WWindow *wwin;
935 WObjDescriptor *desc;
937 /* handle transition from Normal to Iconic state */
938 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
939 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
941 wwin = wWindowFor(event->xclient.window);
942 if (!wwin)
943 return;
944 if (!wwin->flags.miniaturized)
945 wIconifyWindow(wwin);
946 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
947 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
949 if (!scr)
950 return;
952 if (event->xclient.data.l[1] == 1) { /* starting */
953 wColormapAllowClientInstallation(scr, True);
954 } else { /* stopping */
955 wColormapAllowClientInstallation(scr, False);
957 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
959 char *command;
960 size_t len;
962 len = sizeof(event->xclient.data.b) + 1;
963 command = wmalloc(len);
964 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
966 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
967 wwarning(_("Got Reconfigure command"));
968 wDefaultsCheckDomains(NULL);
969 } else {
970 wwarning(_("Got unknown command %s"), command);
973 wfree(command);
975 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
976 WApplication *wapp;
977 int done = 0;
978 wapp = wApplicationOf(event->xclient.window);
979 if (wapp) {
980 switch (event->xclient.data.l[0]) {
981 case WMFHideOtherApplications:
982 wHideOtherApplications(wapp->main_window_desc);
983 done = 1;
984 break;
986 case WMFHideApplication:
987 wHideApplication(wapp);
988 done = 1;
989 break;
992 if (!done) {
993 wwin = wWindowFor(event->xclient.window);
994 if (wwin) {
995 switch (event->xclient.data.l[0]) {
996 case WMFHideOtherApplications:
997 wHideOtherApplications(wwin);
998 break;
1000 case WMFHideApplication:
1001 wHideApplication(wApplicationOf(wwin->main_window));
1002 break;
1006 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1007 wwin = wWindowFor(event->xclient.window);
1008 if (!wwin)
1009 return;
1010 switch (event->xclient.data.l[0]) {
1011 case GSWindowLevelAttr:
1013 int level = (int)event->xclient.data.l[1];
1015 if (WINDOW_LEVEL(wwin) != level) {
1016 ChangeStackingLevel(wwin->frame->core, level);
1019 break;
1021 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1022 wwin = wWindowFor(event->xclient.window);
1023 if (!wwin)
1024 return;
1025 switch (event->xclient.data.l[0]) {
1026 case WMTitleBarNormal:
1027 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1028 break;
1029 case WMTitleBarMain:
1030 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1031 break;
1032 case WMTitleBarKey:
1033 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1034 break;
1036 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1037 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1038 if (!scr)
1039 return;
1040 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1041 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1042 /* do nothing */
1043 #ifdef XDND
1044 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1045 /* do nothing */
1046 #endif /* XDND */
1047 } else {
1049 * Non-standard thing, but needed by OffiX DND.
1050 * For when the icon frame gets a ClientMessage
1051 * that should have gone to the icon_window.
1053 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1054 struct WIcon *icon = NULL;
1056 if (desc->parent_type == WCLASS_MINIWINDOW) {
1057 icon = (WIcon *) desc->parent;
1058 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1059 icon = ((WAppIcon *) desc->parent)->icon;
1061 if (icon && (wwin = icon->owner)) {
1062 if (wwin->client_win != event->xclient.window) {
1063 event->xclient.window = wwin->client_win;
1064 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1071 static void raiseWindow(WScreen * scr)
1073 WWindow *wwin;
1075 scr->autoRaiseTimer = NULL;
1077 wwin = wWindowFor(scr->autoRaiseWindow);
1078 if (!wwin)
1079 return;
1081 if (!wwin->flags.destroyed && wwin->flags.focused) {
1082 wRaiseFrame(wwin->frame->core);
1083 /* this is needed or a race condition will occur */
1084 XSync(dpy, False);
1088 static void handleEnterNotify(XEvent * event)
1090 WWindow *wwin;
1091 WObjDescriptor *desc = NULL;
1092 XEvent ev;
1093 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1095 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1096 /* already left the window... */
1097 saveTimestamp(&ev);
1098 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1099 return;
1103 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1104 if (desc->handle_enternotify)
1105 (*desc->handle_enternotify) (desc, event);
1108 /* enter to window */
1109 wwin = wWindowFor(event->xcrossing.window);
1110 if (!wwin) {
1111 if (wPreferences.colormap_mode == WCM_POINTER) {
1112 wColormapInstallForWindow(scr, NULL);
1114 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1115 WMDeleteTimerHandler(scr->autoRaiseTimer);
1116 scr->autoRaiseTimer = NULL;
1118 } else {
1119 /* set auto raise timer even if in focus-follows-mouse mode
1120 * and the event is for the frame window, even if the window
1121 * has focus already. useful if you move the pointer from a focused
1122 * window to the root window and back pretty fast
1124 * set focus if in focus-follows-mouse mode and the event
1125 * is for the frame window and window doesn't have focus yet */
1126 if (wPreferences.focus_mode == WKF_SLOPPY
1127 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1129 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1130 wSetFocusTo(scr, wwin);
1132 if (scr->autoRaiseTimer)
1133 WMDeleteTimerHandler(scr->autoRaiseTimer);
1134 scr->autoRaiseTimer = NULL;
1136 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1137 scr->autoRaiseWindow = wwin->frame->core->window;
1138 scr->autoRaiseTimer
1139 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1142 /* Install colormap for window, if the colormap installation mode
1143 * is colormap_follows_mouse */
1144 if (wPreferences.colormap_mode == WCM_POINTER) {
1145 if (wwin->client_win == event->xcrossing.window)
1146 wColormapInstallForWindow(scr, wwin);
1147 else
1148 wColormapInstallForWindow(scr, NULL);
1152 if (event->xcrossing.window == event->xcrossing.root
1153 && event->xcrossing.detail == NotifyNormal
1154 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1156 wSetFocusTo(scr, scr->focused_window);
1158 #ifdef BALLOON_TEXT
1159 wBalloonEnteredObject(scr, desc);
1160 #endif
1163 static void handleLeaveNotify(XEvent * event)
1165 WObjDescriptor *desc = NULL;
1167 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1168 if (desc->handle_leavenotify)
1169 (*desc->handle_leavenotify) (desc, event);
1173 #ifdef SHAPE
1174 static void handleShapeNotify(XEvent * event)
1176 XShapeEvent *shev = (XShapeEvent *) event;
1177 WWindow *wwin;
1178 union {
1179 XEvent xevent;
1180 XShapeEvent xshape;
1181 } ev;
1183 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1184 if (ev.xshape.kind == ShapeBounding) {
1185 if (ev.xshape.shaped == shev->shaped) {
1186 *shev = ev.xshape;
1187 } else {
1188 XPutBackEvent(dpy, &ev.xevent);
1189 break;
1194 wwin = wWindowFor(shev->window);
1195 if (!wwin || shev->kind != ShapeBounding)
1196 return;
1198 if (!shev->shaped && wwin->flags.shaped) {
1200 wwin->flags.shaped = 0;
1201 wWindowClearShape(wwin);
1203 } else if (shev->shaped) {
1205 wwin->flags.shaped = 1;
1206 wWindowSetShape(wwin);
1209 #endif /* SHAPE */
1211 #ifdef KEEP_XKB_LOCK_STATUS
1212 /* please help ]d if you know what to do */
1213 static void handleXkbIndicatorStateNotify(XEvent *event)
1215 WWindow *wwin;
1216 WScreen *scr;
1217 XkbStateRec staterec;
1218 int i;
1220 for (i = 0; i < wScreenCount; i++) {
1221 scr = wScreenWithNumber(i);
1222 wwin = scr->focused_window;
1223 if (wwin && wwin->flags.focused) {
1224 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1225 if (wwin->frame->languagemode != staterec.group) {
1226 wwin->frame->last_languagemode = wwin->frame->languagemode;
1227 wwin->frame->languagemode = staterec.group;
1229 #ifdef XKB_BUTTON_HINT
1230 if (wwin->frame->titlebar) {
1231 wFrameWindowPaint(wwin->frame);
1233 #endif
1237 #endif /*KEEP_XKB_LOCK_STATUS */
1239 static void handleColormapNotify(XEvent * event)
1241 WWindow *wwin;
1242 WScreen *scr;
1243 Bool reinstall = False;
1245 wwin = wWindowFor(event->xcolormap.window);
1246 if (!wwin)
1247 return;
1249 scr = wwin->screen_ptr;
1251 do {
1252 if (wwin) {
1253 if (event->xcolormap.new) {
1254 XWindowAttributes attr;
1256 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1258 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1259 scr->current_colormap = attr.colormap;
1261 reinstall = True;
1262 } else if (event->xcolormap.state == ColormapUninstalled &&
1263 scr->current_colormap == event->xcolormap.colormap) {
1265 /* some bastard app (like XV) removed our colormap */
1267 * can't enforce or things like xscreensaver wont work
1268 * reinstall = True;
1270 } else if (event->xcolormap.state == ColormapInstalled &&
1271 scr->current_colormap == event->xcolormap.colormap) {
1273 /* someone has put our colormap back */
1274 reinstall = False;
1277 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1278 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1280 if (reinstall && scr->current_colormap != None) {
1281 if (!scr->flags.colormap_stuff_blocked)
1282 XInstallColormap(dpy, scr->current_colormap);
1286 static void handleFocusIn(XEvent * event)
1288 WWindow *wwin;
1291 * For applications that like stealing the focus.
1293 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1294 saveTimestamp(event);
1295 if (event->xfocus.mode == NotifyUngrab
1296 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1297 return;
1300 wwin = wWindowFor(event->xfocus.window);
1301 if (wwin && !wwin->flags.focused) {
1302 if (wwin->flags.mapped)
1303 wSetFocusTo(wwin->screen_ptr, wwin);
1304 else
1305 wSetFocusTo(wwin->screen_ptr, NULL);
1306 } else if (!wwin) {
1307 WScreen *scr = wScreenForWindow(event->xfocus.window);
1308 if (scr)
1309 wSetFocusTo(scr, NULL);
1313 static WWindow *windowUnderPointer(WScreen * scr)
1315 unsigned int mask;
1316 int foo;
1317 Window bar, win;
1319 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1320 return wWindowFor(win);
1321 return NULL;
1324 static int CheckFullScreenWindowFocused(WScreen * scr)
1326 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1327 return 1;
1328 else
1329 return 0;
1332 static void handleKeyPress(XEvent * event)
1334 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1335 WWindow *wwin = scr->focused_window;
1336 short i, widx;
1337 int modifiers;
1338 int command = -1;
1339 #ifdef KEEP_XKB_LOCK_STATUS
1340 XkbStateRec staterec;
1341 #endif /*KEEP_XKB_LOCK_STATUS */
1343 /* ignore CapsLock */
1344 modifiers = event->xkey.state & ValidModMask;
1346 for (i = 0; i < WKBD_LAST; i++) {
1347 if (wKeyBindings[i].keycode == 0)
1348 continue;
1350 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1351 || */ wKeyBindings[i].modifier ==
1352 modifiers)) {
1353 command = i;
1354 break;
1358 if (command < 0) {
1360 if (!wRootMenuPerformShortcut(event)) {
1361 static int dontLoop = 0;
1363 if (dontLoop > 10) {
1364 wwarning("problem with key event processing code");
1365 return;
1367 dontLoop++;
1368 /* if the focused window is an internal window, try redispatching
1369 * the event to the managed window, as it can be a WINGs window */
1370 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1371 /* client_leader contains the WINGs toplevel */
1372 event->xany.window = wwin->client_leader;
1373 WMHandleEvent(event);
1375 dontLoop--;
1377 return;
1379 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1380 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1382 switch (command) {
1384 case WKBD_ROOTMENU:
1385 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1386 if (!CheckFullScreenWindowFocused(scr)) {
1387 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1388 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1389 True);
1391 break;
1392 case WKBD_WINDOWLIST:
1393 if (!CheckFullScreenWindowFocused(scr)) {
1394 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1395 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1396 True);
1398 break;
1400 case WKBD_WINDOWMENU:
1401 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1402 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1403 break;
1404 case WKBD_MINIMIZEALL:
1405 CloseWindowMenu(scr);
1406 wHideAll(scr);
1407 break;
1408 case WKBD_MINIATURIZE:
1409 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1410 && !WFLAGP(wwin, no_miniaturizable)) {
1411 CloseWindowMenu(scr);
1413 if (wwin->protocols.MINIATURIZE_WINDOW)
1414 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1415 else {
1416 wIconifyWindow(wwin);
1419 break;
1420 case WKBD_HIDE:
1421 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1422 WApplication *wapp = wApplicationOf(wwin->main_window);
1423 CloseWindowMenu(scr);
1425 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1426 wHideApplication(wapp);
1429 break;
1430 case WKBD_HIDE_OTHERS:
1431 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1432 CloseWindowMenu(scr);
1434 wHideOtherApplications(wwin);
1436 break;
1437 case WKBD_MAXIMIZE:
1438 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1439 CloseWindowMenu(scr);
1441 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1443 break;
1444 case WKBD_VMAXIMIZE:
1445 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1446 CloseWindowMenu(scr);
1448 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1450 break;
1451 case WKBD_HMAXIMIZE:
1452 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1453 CloseWindowMenu(scr);
1455 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1457 break;
1458 case WKBD_LHMAXIMIZE:
1459 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1460 CloseWindowMenu(scr);
1462 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1464 break;
1465 case WKBD_RHMAXIMIZE:
1466 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1467 CloseWindowMenu(scr);
1469 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1471 break;
1472 case WKBD_MAXIMUS:
1473 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1474 CloseWindowMenu(scr);
1476 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1478 break;
1479 case WKBD_RAISE:
1480 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1481 CloseWindowMenu(scr);
1483 wRaiseFrame(wwin->frame->core);
1485 break;
1486 case WKBD_LOWER:
1487 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1488 CloseWindowMenu(scr);
1490 wLowerFrame(wwin->frame->core);
1492 break;
1493 case WKBD_RAISELOWER:
1494 /* raise or lower the window under the pointer, not the
1495 * focused one
1497 wwin = windowUnderPointer(scr);
1498 if (wwin)
1499 wRaiseLowerFrame(wwin->frame->core);
1500 break;
1501 case WKBD_SHADE:
1502 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1503 if (wwin->flags.shaded)
1504 wUnshadeWindow(wwin);
1505 else
1506 wShadeWindow(wwin);
1508 break;
1509 case WKBD_MOVERESIZE:
1510 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1511 CloseWindowMenu(scr);
1513 wKeyboardMoveResizeWindow(wwin);
1515 break;
1516 case WKBD_CLOSE:
1517 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1518 CloseWindowMenu(scr);
1519 if (wwin->protocols.DELETE_WINDOW)
1520 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1522 break;
1523 case WKBD_SELECT:
1524 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1525 wSelectWindow(wwin, !wwin->flags.selected);
1527 break;
1528 case WKBD_FOCUSNEXT:
1529 StartWindozeCycle(wwin, event, True, False);
1530 break;
1532 case WKBD_FOCUSPREV:
1533 StartWindozeCycle(wwin, event, False, False);
1534 break;
1536 case WKBD_GROUPNEXT:
1537 StartWindozeCycle(wwin, event, True, True);
1538 break;
1540 case WKBD_GROUPPREV:
1541 StartWindozeCycle(wwin, event, False, True);
1542 break;
1544 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1545 widx = command - WKBD_WORKSPACE1;
1546 i = (scr->current_workspace / 10) * 10 + widx;
1547 if (wPreferences.ws_advance || i < scr->workspace_count)
1548 wWorkspaceChange(scr, i);
1549 break;
1551 case WKBD_NEXTWORKSPACE:
1552 wWorkspaceRelativeChange(scr, 1);
1553 break;
1554 case WKBD_PREVWORKSPACE:
1555 wWorkspaceRelativeChange(scr, -1);
1556 break;
1558 case WKBD_WINDOW1:
1559 case WKBD_WINDOW2:
1560 case WKBD_WINDOW3:
1561 case WKBD_WINDOW4:
1562 case WKBD_WINDOW5:
1563 case WKBD_WINDOW6:
1564 case WKBD_WINDOW7:
1565 case WKBD_WINDOW8:
1566 case WKBD_WINDOW9:
1567 case WKBD_WINDOW10:
1569 widx = command - WKBD_WINDOW1;
1571 if (scr->shortcutWindows[widx]) {
1572 WMArray *list = scr->shortcutWindows[widx];
1573 int cw;
1574 int count = WMGetArrayItemCount(list);
1575 WWindow *twin;
1576 WMArrayIterator iter;
1577 WWindow *wwin;
1579 wUnselectWindows(scr);
1580 cw = scr->current_workspace;
1582 WM_ETARETI_ARRAY(list, wwin, iter) {
1583 if (count > 1)
1584 wWindowChangeWorkspace(wwin, cw);
1586 wMakeWindowVisible(wwin);
1588 if (count > 1)
1589 wSelectWindow(wwin, True);
1592 /* rotate the order of windows, to create a cycling effect */
1593 twin = WMGetFromArray(list, 0);
1594 WMDeleteFromArray(list, 0);
1595 WMAddToArray(list, twin);
1597 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1598 if (scr->shortcutWindows[widx]) {
1599 WMFreeArray(scr->shortcutWindows[widx]);
1600 scr->shortcutWindows[widx] = NULL;
1603 if (wwin->flags.selected && scr->selected_windows) {
1604 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1605 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1606 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1607 } else {
1608 scr->shortcutWindows[widx] = WMCreateArray(4);
1609 WMAddToArray(scr->shortcutWindows[widx], wwin);
1612 wSelectWindow(wwin, !wwin->flags.selected);
1613 XFlush(dpy);
1614 wusleep(3000);
1615 wSelectWindow(wwin, !wwin->flags.selected);
1616 XFlush(dpy);
1618 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1620 if (wwin->flags.selected && scr->selected_windows) {
1621 if (scr->shortcutWindows[widx]) {
1622 WMFreeArray(scr->shortcutWindows[widx]);
1624 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1628 break;
1630 case WKBD_RELAUNCH:
1631 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1632 (void) RelaunchWindow(wwin);
1634 break;
1636 case WKBD_SWITCH_SCREEN:
1637 if (wScreenCount > 1) {
1638 WScreen *scr2;
1639 int i;
1641 /* find index of this screen */
1642 for (i = 0; i < wScreenCount; i++) {
1643 if (wScreenWithNumber(i) == scr)
1644 break;
1646 i++;
1647 if (i >= wScreenCount) {
1648 i = 0;
1650 scr2 = wScreenWithNumber(i);
1652 if (scr2) {
1653 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1654 scr2->scr_width / 2, scr2->scr_height / 2);
1657 break;
1659 case WKBD_NEXTWSLAYER:
1660 case WKBD_PREVWSLAYER:
1662 int row, column;
1664 row = scr->current_workspace / 10;
1665 column = scr->current_workspace % 10;
1667 if (command == WKBD_NEXTWSLAYER) {
1668 if ((row + 1) * 10 < scr->workspace_count)
1669 wWorkspaceChange(scr, column + (row + 1) * 10);
1670 } else {
1671 if (row > 0)
1672 wWorkspaceChange(scr, column + (row - 1) * 10);
1675 break;
1676 case WKBD_CLIPRAISELOWER:
1677 if (!wPreferences.flags.noclip)
1678 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1679 break;
1680 case WKBD_DOCKRAISELOWER:
1681 if (!wPreferences.flags.nodock)
1682 wDockRaiseLower(scr->dock);
1683 break;
1684 #ifdef KEEP_XKB_LOCK_STATUS
1685 case WKBD_TOGGLE:
1686 if (wPreferences.modelock) {
1687 /*toggle */
1688 wwin = scr->focused_window;
1690 if (wwin && wwin->flags.mapped
1691 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1692 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1693 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1695 wwin->frame->languagemode = wwin->frame->last_languagemode;
1696 wwin->frame->last_languagemode = staterec.group;
1697 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1701 break;
1702 #endif /* KEEP_XKB_LOCK_STATUS */
1706 static void handleMotionNotify(XEvent * event)
1708 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1710 if (wPreferences.scrollable_menus) {
1711 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1712 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1714 if (scr->flags.jump_back_pending ||
1715 p.x <= (rect.pos.x + 1) ||
1716 p.x >= (rect.pos.x + rect.size.width - 2) ||
1717 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1718 WMenu *menu;
1720 menu = wMenuUnderPointer(scr);
1721 if (menu != NULL)
1722 wMenuScroll(menu, event);
1727 static void handleVisibilityNotify(XEvent * event)
1729 WWindow *wwin;
1731 wwin = wWindowFor(event->xvisibility.window);
1732 if (!wwin)
1733 return;
1734 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);