WindowMaker: New get_texture_image function
[wmaker-crm.git] / src / event.c
blob50c5a5645c8e240e766ce9217c868ce4a827ef44
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 "funcs.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"
70 /******** Global Variables **********/
71 extern XContext wWinContext;
72 extern XContext wVEdgeContext;
74 extern Cursor wCursor[WCUR_LAST];
76 extern WShortKey wKeyBindings[WKBD_LAST];
77 extern int wScreenCount;
78 extern Time LastTimestamp;
79 extern Time LastFocusChange;
81 extern WPreferences wPreferences;
83 #define MOD_MASK wPreferences.modifier_mask
85 extern Atom _XA_WM_COLORMAP_NOTIFY;
87 extern Atom _XA_WM_CHANGE_STATE;
88 extern Atom _XA_WM_DELETE_WINDOW;
89 extern Atom _XA_GNUSTEP_WM_ATTR;
90 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
91 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
92 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
93 extern Atom _XA_WINDOWMAKER_COMMAND;
94 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
96 #ifdef SHAPE
97 extern Bool wShapeSupported;
98 extern int wShapeEventBase;
99 #endif
101 #ifdef KEEP_XKB_LOCK_STATUS
102 extern int wXkbEventBase;
103 #endif
105 #ifdef HAVE_XRANDR
106 extern Bool has_randr;
107 extern int randr_event_base;
108 #endif
110 /* special flags */
111 /*extern char WDelayedActionSet;*/
113 /************ Local stuff ***********/
115 static void saveTimestamp(XEvent *event);
116 static void handleColormapNotify(XEvent *event);
117 static void handleMapNotify(XEvent *event);
118 static void handleUnmapNotify(XEvent *event);
119 static void handleButtonPress(XEvent *event);
120 static void handleExpose(XEvent *event);
121 static void handleDestroyNotify(XEvent *event);
122 static void handleConfigureRequest(XEvent *event);
123 static void handleMapRequest(XEvent *event);
124 static void handlePropertyNotify(XEvent *event);
125 static void handleEnterNotify(XEvent *event);
126 static void handleLeaveNotify(XEvent *event);
127 static void handleExtensions(XEvent *event);
128 static void handleClientMessage(XEvent *event);
129 static void handleKeyPress(XEvent *event);
130 static void handleFocusIn(XEvent *event);
131 static void handleMotionNotify(XEvent *event);
132 static void handleVisibilityNotify(XEvent *event);
133 static void handle_inotify_events(int fd, int wd);
134 static void wdelete_death_handler(WMagicNumber id);
137 #ifdef SHAPE
138 static void handleShapeNotify(XEvent *event);
139 #endif
141 #ifdef KEEP_XKB_LOCK_STATUS
142 static void handleXkbIndicatorStateNotify(XEvent *event);
143 #endif
145 /* called from the signal handler */
146 void NotifyDeadProcess(pid_t pid, unsigned char status);
148 /* real dead process handler */
149 static void handleDeadProcess(void *foo);
151 typedef struct DeadProcesses {
152 pid_t pid;
153 unsigned char exit_status;
154 } DeadProcesses;
156 /* stack of dead processes */
157 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
158 static int deadProcessPtr = 0;
160 typedef struct DeathHandler {
161 WDeathHandler *callback;
162 pid_t pid;
163 void *client_data;
164 } DeathHandler;
166 static WMArray *deathHandlers = NULL;
168 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
170 DeathHandler *handler;
172 handler = malloc(sizeof(DeathHandler));
173 if (!handler)
174 return 0;
176 handler->pid = pid;
177 handler->callback = callback;
178 handler->client_data = cdata;
180 if (!deathHandlers)
181 deathHandlers = WMCreateArrayWithDestructor(8, wfree);
183 WMAddToArray(deathHandlers, handler);
185 return handler;
188 static void wdelete_death_handler(WMagicNumber id)
190 DeathHandler *handler = (DeathHandler *) id;
192 if (!handler || !deathHandlers)
193 return;
195 /* array destructor will call wfree(handler) */
196 WMRemoveFromArray(deathHandlers, handler);
199 void DispatchEvent(XEvent * event)
201 if (deathHandlers)
202 handleDeadProcess(NULL);
204 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
205 WCHANGE_STATE(WSTATE_EXITING);
206 /* received SIGTERM */
208 * WMHandleEvent() can't be called from anything
209 * executed inside here, or we can get in a infinite
210 * recursive loop.
212 Shutdown(WSExitMode);
214 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
215 WCHANGE_STATE(WSTATE_RESTARTING);
217 Shutdown(WSRestartPreparationMode);
218 /* received SIGHUP */
219 Restart(NULL, True);
220 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
221 WCHANGE_STATE(WSTATE_NORMAL);
222 wDefaultsCheckDomains(NULL);
225 /* for the case that all that is wanted to be dispatched is
226 * the stuff above */
227 if (!event)
228 return;
230 saveTimestamp(event);
231 switch (event->type) {
232 case MapRequest:
233 handleMapRequest(event);
234 break;
236 case KeyPress:
237 handleKeyPress(event);
238 break;
240 case MotionNotify:
241 handleMotionNotify(event);
242 break;
244 case ConfigureRequest:
245 handleConfigureRequest(event);
246 break;
248 case DestroyNotify:
249 handleDestroyNotify(event);
250 break;
252 case MapNotify:
253 handleMapNotify(event);
254 break;
256 case UnmapNotify:
257 handleUnmapNotify(event);
258 break;
260 case ButtonPress:
261 handleButtonPress(event);
262 break;
264 case Expose:
265 handleExpose(event);
266 break;
268 case PropertyNotify:
269 handlePropertyNotify(event);
270 break;
272 case EnterNotify:
273 handleEnterNotify(event);
274 break;
276 case LeaveNotify:
277 handleLeaveNotify(event);
278 break;
280 case ClientMessage:
281 handleClientMessage(event);
282 break;
284 case ColormapNotify:
285 handleColormapNotify(event);
286 break;
288 case MappingNotify:
289 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
290 XRefreshKeyboardMapping(&event->xmapping);
291 break;
293 case FocusIn:
294 handleFocusIn(event);
295 break;
297 case VisibilityNotify:
298 handleVisibilityNotify(event);
299 break;
301 case ConfigureNotify:
302 if (event->xconfigure.window == DefaultRootWindow(dpy)) {
303 #ifdef HAVE_XRANDR
304 XRRUpdateConfiguration(event);
305 #endif
307 break;
309 default:
310 handleExtensions(event);
311 break;
315 #ifdef HAVE_INOTIFY
317 *----------------------------------------------------------------------
318 * handle_inotify_events-
319 * Check for inotify events
321 * Returns:
322 * After reading events for the given file descriptor (fd) and
323 * watch descriptor (wd)
325 * Side effects:
326 * Calls wDefaultsCheckDomains if config database is updated
327 *----------------------------------------------------------------------
329 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
330 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
331 static void handle_inotify_events(int fd, int wd)
333 extern void wDefaultsCheckDomains(void *);
334 ssize_t eventQLength, i = 0;
335 char buff[BUFF_SIZE] = { 0 };
336 /* Check config only once per read of the event queue */
337 int oneShotFlag = 0;
340 * Read off the queued events
341 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
342 * not occur; the block is on Xevents, but a config file change will normally
343 * occur as a result of an Xevent - so the event queue should never have more than
344 * a few entries before a read().
346 eventQLength = read(fd, buff, BUFF_SIZE);
348 /* check what events occured */
349 /* Should really check wd here too, but for now we only have one watch! */
350 while (i < eventQLength) {
351 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
354 * see inotify.h for event types.
356 if (pevent->mask & IN_DELETE_SELF) {
357 wwarning(_("the defaults database has been deleted!"
358 " Restart Window Maker to create the database" " with the default settings"));
359 close(fd);
361 if (pevent->mask & IN_UNMOUNT) {
362 wwarning(_("the unit containing the defaults database has"
363 " been unmounted. Setting --static mode." " Any changes will not be saved."));
364 close(fd);
365 wPreferences.flags.noupdates = 1;
367 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
368 wwarning(_("Inotify: Reading config files in defaults database."));
369 wDefaultsCheckDomains(NULL);
372 /* move to next event in the buffer */
373 i += sizeof(struct inotify_event) + pevent->len;
376 #endif /* HAVE_INOTIFY */
379 *----------------------------------------------------------------------
380 * EventLoop-
381 * Processes X and internal events indefinitely.
383 * Returns:
384 * Never returns
386 * Side effects:
387 * The LastTimestamp global variable is updated.
388 * Calls inotifyGetEvents if defaults database changes.
389 *----------------------------------------------------------------------
391 void EventLoop(void)
393 XEvent event;
394 #ifdef HAVE_INOTIFY
395 extern int inotifyFD;
396 extern int inotifyWD;
397 struct timeval time;
398 fd_set rfds;
399 int retVal = 0;
401 if (inotifyFD < 0 || inotifyWD < 0)
402 retVal = -1;
403 #endif
405 for (;;) {
407 WMNextEvent(dpy, &event); /* Blocks here */
408 WMHandleEvent(&event);
409 #ifdef HAVE_INOTIFY
410 if (retVal != -1) {
411 time.tv_sec = 0;
412 time.tv_usec = 0;
413 FD_ZERO(&rfds);
414 FD_SET(inotifyFD, &rfds);
416 /* check for available read data from inotify - don't block! */
417 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
419 if (retVal < 0) { /* an error has occured */
420 wwarning(_("select failed. The inotify instance will be closed."
421 " Changes to the defaults database will require"
422 " a restart to take effect."));
423 close(inotifyFD);
424 continue;
426 if (FD_ISSET(inotifyFD, &rfds))
427 handle_inotify_events(inotifyFD, inotifyWD);
429 #endif
434 *----------------------------------------------------------------------
435 * ProcessPendingEvents --
436 * Processes the events that are currently pending (at the time
437 * this function is called) in the display's queue.
439 * Returns:
440 * After the pending events that were present at the function call
441 * are processed.
443 * Side effects:
444 * Many -- whatever handling events may involve.
446 *----------------------------------------------------------------------
448 void ProcessPendingEvents(void)
450 XEvent event;
451 int count;
453 XSync(dpy, False);
455 /* Take a snapshot of the event count in the queue */
456 count = XPending(dpy);
458 while (count > 0 && XPending(dpy)) {
459 WMNextEvent(dpy, &event);
460 WMHandleEvent(&event);
461 count--;
465 Bool IsDoubleClick(WScreen * scr, XEvent * event)
467 if ((scr->last_click_time > 0) &&
468 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
469 && (event->xbutton.button == scr->last_click_button)
470 && (event->xbutton.window == scr->last_click_window)) {
472 scr->flags.next_click_is_not_double = 1;
473 scr->last_click_time = 0;
474 scr->last_click_window = event->xbutton.window;
476 return True;
478 return False;
481 void NotifyDeadProcess(pid_t pid, unsigned char status)
483 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
484 wwarning("stack overflow: too many dead processes");
485 return;
487 /* stack the process to be handled later,
488 * as this is called from the signal handler */
489 deadProcesses[deadProcessPtr].pid = pid;
490 deadProcesses[deadProcessPtr].exit_status = status;
491 deadProcessPtr++;
494 static void handleDeadProcess(void *foo)
496 DeathHandler *tmp;
497 int i;
499 for (i = 0; i < deadProcessPtr; i++) {
500 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
503 if (!deathHandlers) {
504 deadProcessPtr = 0;
505 return;
508 /* get the pids on the queue and call handlers */
509 while (deadProcessPtr > 0) {
510 deadProcessPtr--;
512 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
513 tmp = WMGetFromArray(deathHandlers, i);
514 if (!tmp)
515 continue;
517 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
518 (*tmp->callback) (tmp->pid,
519 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
520 wdelete_death_handler(tmp);
526 static void saveTimestamp(XEvent * event)
529 * Never save CurrentTime as LastTimestamp because CurrentTime
530 * it's not a real timestamp (it's the 0L constant)
533 switch (event->type) {
534 case ButtonRelease:
535 case ButtonPress:
536 LastTimestamp = event->xbutton.time;
537 break;
538 case KeyPress:
539 case KeyRelease:
540 LastTimestamp = event->xkey.time;
541 break;
542 case MotionNotify:
543 LastTimestamp = event->xmotion.time;
544 break;
545 case PropertyNotify:
546 LastTimestamp = event->xproperty.time;
547 break;
548 case EnterNotify:
549 case LeaveNotify:
550 LastTimestamp = event->xcrossing.time;
551 break;
552 case SelectionClear:
553 LastTimestamp = event->xselectionclear.time;
554 break;
555 case SelectionRequest:
556 LastTimestamp = event->xselectionrequest.time;
557 break;
558 case SelectionNotify:
559 LastTimestamp = event->xselection.time;
560 #ifdef XDND
561 wXDNDProcessSelection(event);
562 #endif
563 break;
567 static int matchWindow(const void *item, const void *cdata)
569 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
572 static void handleExtensions(XEvent * event)
574 #ifdef KEEP_XKB_LOCK_STATUS
575 XkbEvent *xkbevent;
576 xkbevent = (XkbEvent *) event;
577 #endif /*KEEP_XKB_LOCK_STATUS */
578 #ifdef SHAPE
579 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
580 handleShapeNotify(event);
582 #endif
583 #ifdef KEEP_XKB_LOCK_STATUS
584 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
585 handleXkbIndicatorStateNotify(event);
587 #endif /*KEEP_XKB_LOCK_STATUS */
588 #ifdef HAVE_XRANDR
589 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
590 /* From xrandr man page: "Clients must call back into Xlib using
591 * XRRUpdateConfiguration when screen configuration change notify
592 * events are generated */
593 XRRUpdateConfiguration(event);
594 WCHANGE_STATE(WSTATE_RESTARTING);
595 Shutdown(WSRestartPreparationMode);
596 Restart(NULL,True);
598 #endif
601 static void handleMapRequest(XEvent * ev)
603 WWindow *wwin;
604 WScreen *scr = NULL;
605 Window window = ev->xmaprequest.window;
607 if ((wwin = wWindowFor(window))) {
608 if (wwin->flags.shaded) {
609 wUnshadeWindow(wwin);
611 /* deiconify window */
612 if (wwin->flags.miniaturized) {
613 wDeiconifyWindow(wwin);
614 } else if (wwin->flags.hidden) {
615 WApplication *wapp = wApplicationOf(wwin->main_window);
616 /* go to the last workspace that the user worked on the app */
617 if (wapp) {
618 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
620 wUnhideApplication(wapp, False, False);
622 return;
625 scr = wScreenForRootWindow(ev->xmaprequest.parent);
627 wwin = wManageWindow(scr, window);
630 * This is to let the Dock know that the application it launched
631 * has already been mapped (eg: it has finished launching).
632 * It is not necessary for normally docked apps, but is needed for
633 * apps that were forcedly docked (like with dockit).
635 if (scr->last_dock) {
636 if (wwin && wwin->main_window != None && wwin->main_window != window)
637 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
638 else
639 wDockTrackWindowLaunch(scr->last_dock, window);
642 if (wwin) {
643 wClientSetState(wwin, NormalState, None);
644 if (wwin->flags.maximized) {
645 wMaximizeWindow(wwin, wwin->flags.maximized);
647 if (wwin->flags.shaded) {
648 wwin->flags.shaded = 0;
649 wwin->flags.skip_next_animation = 1;
650 wShadeWindow(wwin);
652 if (wwin->flags.miniaturized) {
653 wwin->flags.miniaturized = 0;
654 wwin->flags.skip_next_animation = 1;
655 wIconifyWindow(wwin);
657 if (wwin->flags.fullscreen) {
658 wwin->flags.fullscreen = 0;
659 wFullscreenWindow(wwin);
661 if (wwin->flags.hidden) {
662 WApplication *wapp = wApplicationOf(wwin->main_window);
664 wwin->flags.hidden = 0;
665 wwin->flags.skip_next_animation = 1;
666 if (wapp) {
667 wHideApplication(wapp);
673 static void handleDestroyNotify(XEvent * event)
675 WWindow *wwin;
676 WApplication *app;
677 Window window = event->xdestroywindow.window;
678 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
679 int widx;
681 wwin = wWindowFor(window);
682 if (wwin) {
683 wUnmanageWindow(wwin, False, True);
686 if (scr != NULL) {
687 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
688 WFakeGroupLeader *fPtr;
690 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
691 if (fPtr->retainCount > 0) {
692 fPtr->retainCount--;
693 if (fPtr->retainCount == 0 && fPtr->leader != None) {
694 XDestroyWindow(dpy, fPtr->leader);
695 fPtr->leader = None;
696 XFlush(dpy);
699 fPtr->origLeader = None;
703 app = wApplicationOf(window);
704 if (app) {
705 if (window == app->main_window) {
706 app->refcount = 0;
707 wwin = app->main_window_desc->screen_ptr->focused_window;
708 while (wwin) {
709 if (wwin->main_window == window) {
710 wwin->main_window = None;
712 wwin = wwin->prev;
715 wApplicationDestroy(app);
719 static void handleExpose(XEvent * event)
721 WObjDescriptor *desc;
722 XEvent ev;
724 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
726 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
727 return;
730 if (desc->handle_expose) {
731 (*desc->handle_expose) (desc, event);
735 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
737 switch (action) {
738 case WA_SELECT_WINDOWS:
739 wUnselectWindows(scr);
740 wSelectWindows(scr, event);
741 break;
742 case WA_OPEN_APPMENU:
743 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
744 /* ugly hack */
745 if (scr->root_menu) {
746 if (scr->root_menu->brother->flags.mapped)
747 event->xbutton.window = scr->root_menu->brother->frame->core->window;
748 else
749 event->xbutton.window = scr->root_menu->frame->core->window;
751 break;
752 case WA_OPEN_WINLISTMENU:
753 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
754 if (scr->switch_menu) {
755 if (scr->switch_menu->brother->flags.mapped)
756 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
757 else
758 event->xbutton.window = scr->switch_menu->frame->core->window;
760 break;
761 default:
762 break;
766 /* bindable */
767 static void handleButtonPress(XEvent * event)
769 WObjDescriptor *desc;
770 WScreen *scr;
772 scr = wScreenForRootWindow(event->xbutton.root);
774 #ifdef BALLOON_TEXT
775 wBalloonHide(scr);
776 #endif
778 if (event->xbutton.window == scr->root_win) {
779 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
780 executeButtonAction(scr, event, wPreferences.mouse_button1);
781 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
782 executeButtonAction(scr, event, wPreferences.mouse_button2);
783 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
784 executeButtonAction(scr, event, wPreferences.mouse_button3);
785 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
786 wWorkspaceRelativeChange(scr, 1);
787 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
788 wWorkspaceRelativeChange(scr, -1);
792 desc = NULL;
793 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
794 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
795 return;
799 if (desc->parent_type == WCLASS_WINDOW) {
800 XSync(dpy, 0);
802 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
803 XAllowEvents(dpy, AsyncPointer, CurrentTime);
804 } else {
805 /* if (wPreferences.focus_mode == WKF_CLICK) { */
806 if (wPreferences.ignore_focus_click) {
807 XAllowEvents(dpy, AsyncPointer, CurrentTime);
809 XAllowEvents(dpy, ReplayPointer, CurrentTime);
810 /* } */
812 XSync(dpy, 0);
813 } else if (desc->parent_type == WCLASS_APPICON
814 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
815 if (event->xbutton.state & MOD_MASK) {
816 XSync(dpy, 0);
817 XAllowEvents(dpy, AsyncPointer, CurrentTime);
818 XSync(dpy, 0);
822 if (desc->handle_mousedown != NULL) {
823 (*desc->handle_mousedown) (desc, event);
826 /* save double-click information */
827 if (scr->flags.next_click_is_not_double) {
828 scr->flags.next_click_is_not_double = 0;
829 } else {
830 scr->last_click_time = event->xbutton.time;
831 scr->last_click_button = event->xbutton.button;
832 scr->last_click_window = event->xbutton.window;
836 static void handleMapNotify(XEvent * event)
838 WWindow *wwin;
840 wwin = wWindowFor(event->xmap.event);
841 if (wwin && wwin->client_win == event->xmap.event) {
842 if (wwin->flags.miniaturized) {
843 wDeiconifyWindow(wwin);
844 } else {
845 XGrabServer(dpy);
846 wWindowMap(wwin);
847 wClientSetState(wwin, NormalState, None);
848 XUngrabServer(dpy);
853 static void handleUnmapNotify(XEvent * event)
855 WWindow *wwin;
856 XEvent ev;
857 Bool withdraw = False;
859 /* only process windows with StructureNotify selected
860 * (ignore SubstructureNotify) */
861 wwin = wWindowFor(event->xunmap.window);
862 if (!wwin)
863 return;
865 /* whether the event is a Withdrawal request */
866 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
867 withdraw = True;
869 if (wwin->client_win != event->xunmap.event && !withdraw)
870 return;
872 if (!wwin->flags.mapped && !withdraw
873 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
874 && !wwin->flags.miniaturized && !wwin->flags.hidden)
875 return;
877 XGrabServer(dpy);
878 XUnmapWindow(dpy, wwin->frame->core->window);
879 wwin->flags.mapped = 0;
880 XSync(dpy, 0);
881 /* check if the window was destroyed */
882 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
883 DispatchEvent(&ev);
884 } else {
885 Bool reparented = False;
887 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
888 reparented = True;
890 /* withdraw window */
891 wwin->flags.mapped = 0;
892 if (!reparented)
893 wClientSetState(wwin, WithdrawnState, None);
895 /* if the window was reparented, do not reparent it back to the
896 * root window */
897 wUnmanageWindow(wwin, !reparented, False);
899 XUngrabServer(dpy);
902 static void handleConfigureRequest(XEvent * event)
904 WWindow *wwin;
906 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
908 * Configure request for unmapped window
910 wClientConfigure(NULL, &(event->xconfigurerequest));
911 } else {
912 wClientConfigure(wwin, &(event->xconfigurerequest));
916 static void handlePropertyNotify(XEvent * event)
918 WWindow *wwin;
919 WApplication *wapp;
920 Window jr;
921 int ji;
922 unsigned int ju;
924 wwin = wWindowFor(event->xproperty.window);
925 if (wwin) {
926 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
927 return;
929 wClientCheckProperty(wwin, &event->xproperty);
931 wapp = wApplicationOf(event->xproperty.window);
932 if (wapp) {
933 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
937 static void handleClientMessage(XEvent * event)
939 WWindow *wwin;
940 WObjDescriptor *desc;
942 /* handle transition from Normal to Iconic state */
943 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
944 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
946 wwin = wWindowFor(event->xclient.window);
947 if (!wwin)
948 return;
949 if (!wwin->flags.miniaturized)
950 wIconifyWindow(wwin);
951 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
952 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
954 if (!scr)
955 return;
957 if (event->xclient.data.l[1] == 1) { /* starting */
958 wColormapAllowClientInstallation(scr, True);
959 } else { /* stopping */
960 wColormapAllowClientInstallation(scr, False);
962 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
964 char *command;
965 size_t len;
967 len = sizeof(event->xclient.data.b) + 1;
968 command = wmalloc(len);
969 memset(command, 0, len);
970 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
972 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
973 wwarning(_("Got Reconfigure command"));
974 wDefaultsCheckDomains(NULL);
975 } else {
976 wwarning(_("Got unknown command %s"), command);
979 wfree(command);
981 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
982 WApplication *wapp;
983 int done = 0;
984 wapp = wApplicationOf(event->xclient.window);
985 if (wapp) {
986 switch (event->xclient.data.l[0]) {
987 case WMFHideOtherApplications:
988 wHideOtherApplications(wapp->main_window_desc);
989 done = 1;
990 break;
992 case WMFHideApplication:
993 wHideApplication(wapp);
994 done = 1;
995 break;
998 if (!done) {
999 wwin = wWindowFor(event->xclient.window);
1000 if (wwin) {
1001 switch (event->xclient.data.l[0]) {
1002 case WMFHideOtherApplications:
1003 wHideOtherApplications(wwin);
1004 break;
1006 case WMFHideApplication:
1007 wHideApplication(wApplicationOf(wwin->main_window));
1008 break;
1012 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1013 wwin = wWindowFor(event->xclient.window);
1014 if (!wwin)
1015 return;
1016 switch (event->xclient.data.l[0]) {
1017 case GSWindowLevelAttr:
1019 int level = (int)event->xclient.data.l[1];
1021 if (WINDOW_LEVEL(wwin) != level) {
1022 ChangeStackingLevel(wwin->frame->core, level);
1025 break;
1027 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1028 wwin = wWindowFor(event->xclient.window);
1029 if (!wwin)
1030 return;
1031 switch (event->xclient.data.l[0]) {
1032 case WMTitleBarNormal:
1033 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1034 break;
1035 case WMTitleBarMain:
1036 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1037 break;
1038 case WMTitleBarKey:
1039 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1040 break;
1042 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1043 WScreen *scr = wScreenSearchForRootWindow(event->xclient.window);
1044 if (!scr)
1045 return;
1046 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1047 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1048 /* do nothing */
1049 #ifdef XDND
1050 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1051 /* do nothing */
1052 #endif /* XDND */
1053 } else {
1055 * Non-standard thing, but needed by OffiX DND.
1056 * For when the icon frame gets a ClientMessage
1057 * that should have gone to the icon_window.
1059 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1060 struct WIcon *icon = NULL;
1062 if (desc->parent_type == WCLASS_MINIWINDOW) {
1063 icon = (WIcon *) desc->parent;
1064 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1065 icon = ((WAppIcon *) desc->parent)->icon;
1067 if (icon && (wwin = icon->owner)) {
1068 if (wwin->client_win != event->xclient.window) {
1069 event->xclient.window = wwin->client_win;
1070 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1077 static void raiseWindow(WScreen * scr)
1079 WWindow *wwin;
1081 scr->autoRaiseTimer = NULL;
1083 wwin = wWindowFor(scr->autoRaiseWindow);
1084 if (!wwin)
1085 return;
1087 if (!wwin->flags.destroyed && wwin->flags.focused) {
1088 wRaiseFrame(wwin->frame->core);
1089 /* this is needed or a race condition will occur */
1090 XSync(dpy, False);
1094 static void handleEnterNotify(XEvent * event)
1096 WWindow *wwin;
1097 WObjDescriptor *desc = NULL;
1098 XEvent ev;
1099 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1101 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1102 /* already left the window... */
1103 saveTimestamp(&ev);
1104 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1105 return;
1109 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1110 if (desc->handle_enternotify)
1111 (*desc->handle_enternotify) (desc, event);
1114 /* enter to window */
1115 wwin = wWindowFor(event->xcrossing.window);
1116 if (!wwin) {
1117 if (wPreferences.colormap_mode == WCM_POINTER) {
1118 wColormapInstallForWindow(scr, NULL);
1120 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1121 WMDeleteTimerHandler(scr->autoRaiseTimer);
1122 scr->autoRaiseTimer = NULL;
1124 } else {
1125 /* set auto raise timer even if in focus-follows-mouse mode
1126 * and the event is for the frame window, even if the window
1127 * has focus already. useful if you move the pointer from a focused
1128 * window to the root window and back pretty fast
1130 * set focus if in focus-follows-mouse mode and the event
1131 * is for the frame window and window doesn't have focus yet */
1132 if (wPreferences.focus_mode == WKF_SLOPPY
1133 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1135 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1136 wSetFocusTo(scr, wwin);
1138 if (scr->autoRaiseTimer)
1139 WMDeleteTimerHandler(scr->autoRaiseTimer);
1140 scr->autoRaiseTimer = NULL;
1142 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1143 scr->autoRaiseWindow = wwin->frame->core->window;
1144 scr->autoRaiseTimer
1145 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1148 /* Install colormap for window, if the colormap installation mode
1149 * is colormap_follows_mouse */
1150 if (wPreferences.colormap_mode == WCM_POINTER) {
1151 if (wwin->client_win == event->xcrossing.window)
1152 wColormapInstallForWindow(scr, wwin);
1153 else
1154 wColormapInstallForWindow(scr, NULL);
1158 if (event->xcrossing.window == event->xcrossing.root
1159 && event->xcrossing.detail == NotifyNormal
1160 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1162 wSetFocusTo(scr, scr->focused_window);
1164 #ifdef BALLOON_TEXT
1165 wBalloonEnteredObject(scr, desc);
1166 #endif
1169 static void handleLeaveNotify(XEvent * event)
1171 WObjDescriptor *desc = NULL;
1173 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1174 if (desc->handle_leavenotify)
1175 (*desc->handle_leavenotify) (desc, event);
1179 #ifdef SHAPE
1180 static void handleShapeNotify(XEvent * event)
1182 XShapeEvent *shev = (XShapeEvent *) event;
1183 WWindow *wwin;
1184 union {
1185 XEvent xevent;
1186 XShapeEvent xshape;
1187 } ev;
1189 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1190 if (ev.xshape.kind == ShapeBounding) {
1191 if (ev.xshape.shaped == shev->shaped) {
1192 *shev = ev.xshape;
1193 } else {
1194 XPutBackEvent(dpy, &ev.xevent);
1195 break;
1200 wwin = wWindowFor(shev->window);
1201 if (!wwin || shev->kind != ShapeBounding)
1202 return;
1204 if (!shev->shaped && wwin->flags.shaped) {
1206 wwin->flags.shaped = 0;
1207 wWindowClearShape(wwin);
1209 } else if (shev->shaped) {
1211 wwin->flags.shaped = 1;
1212 wWindowSetShape(wwin);
1215 #endif /* SHAPE */
1217 #ifdef KEEP_XKB_LOCK_STATUS
1218 /* please help ]d if you know what to do */
1219 static void handleXkbIndicatorStateNotify(XEvent *event)
1221 WWindow *wwin;
1222 WScreen *scr;
1223 XkbStateRec staterec;
1224 int i;
1226 for (i = 0; i < wScreenCount; i++) {
1227 scr = wScreenWithNumber(i);
1228 wwin = scr->focused_window;
1229 if (wwin && wwin->flags.focused) {
1230 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1231 if (wwin->frame->languagemode != staterec.group) {
1232 wwin->frame->last_languagemode = wwin->frame->languagemode;
1233 wwin->frame->languagemode = staterec.group;
1235 #ifdef XKB_BUTTON_HINT
1236 if (wwin->frame->titlebar) {
1237 wFrameWindowPaint(wwin->frame);
1239 #endif
1243 #endif /*KEEP_XKB_LOCK_STATUS */
1245 static void handleColormapNotify(XEvent * event)
1247 WWindow *wwin;
1248 WScreen *scr;
1249 Bool reinstall = False;
1251 wwin = wWindowFor(event->xcolormap.window);
1252 if (!wwin)
1253 return;
1255 scr = wwin->screen_ptr;
1257 do {
1258 if (wwin) {
1259 if (event->xcolormap.new) {
1260 XWindowAttributes attr;
1262 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1264 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1265 scr->current_colormap = attr.colormap;
1267 reinstall = True;
1268 } else if (event->xcolormap.state == ColormapUninstalled &&
1269 scr->current_colormap == event->xcolormap.colormap) {
1271 /* some bastard app (like XV) removed our colormap */
1273 * can't enforce or things like xscreensaver wont work
1274 * reinstall = True;
1276 } else if (event->xcolormap.state == ColormapInstalled &&
1277 scr->current_colormap == event->xcolormap.colormap) {
1279 /* someone has put our colormap back */
1280 reinstall = False;
1283 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1284 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1286 if (reinstall && scr->current_colormap != None) {
1287 if (!scr->flags.colormap_stuff_blocked)
1288 XInstallColormap(dpy, scr->current_colormap);
1292 static void handleFocusIn(XEvent * event)
1294 WWindow *wwin;
1297 * For applications that like stealing the focus.
1299 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1300 saveTimestamp(event);
1301 if (event->xfocus.mode == NotifyUngrab
1302 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1303 return;
1306 wwin = wWindowFor(event->xfocus.window);
1307 if (wwin && !wwin->flags.focused) {
1308 if (wwin->flags.mapped)
1309 wSetFocusTo(wwin->screen_ptr, wwin);
1310 else
1311 wSetFocusTo(wwin->screen_ptr, NULL);
1312 } else if (!wwin) {
1313 WScreen *scr = wScreenForWindow(event->xfocus.window);
1314 if (scr)
1315 wSetFocusTo(scr, NULL);
1319 static WWindow *windowUnderPointer(WScreen * scr)
1321 unsigned int mask;
1322 int foo;
1323 Window bar, win;
1325 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1326 return wWindowFor(win);
1327 return NULL;
1330 static int CheckFullScreenWindowFocused(WScreen * scr)
1332 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1333 return 1;
1334 else
1335 return 0;
1338 static void handleKeyPress(XEvent * event)
1340 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1341 WWindow *wwin = scr->focused_window;
1342 short i, widx;
1343 int modifiers;
1344 int command = -1;
1345 #ifdef KEEP_XKB_LOCK_STATUS
1346 XkbStateRec staterec;
1347 #endif /*KEEP_XKB_LOCK_STATUS */
1349 /* ignore CapsLock */
1350 modifiers = event->xkey.state & ValidModMask;
1352 for (i = 0; i < WKBD_LAST; i++) {
1353 if (wKeyBindings[i].keycode == 0)
1354 continue;
1356 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1357 || */ wKeyBindings[i].modifier ==
1358 modifiers)) {
1359 command = i;
1360 break;
1364 if (command < 0) {
1366 if (!wRootMenuPerformShortcut(event)) {
1367 static int dontLoop = 0;
1369 if (dontLoop > 10) {
1370 wwarning("problem with key event processing code");
1371 return;
1373 dontLoop++;
1374 /* if the focused window is an internal window, try redispatching
1375 * the event to the managed window, as it can be a WINGs window */
1376 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1377 /* client_leader contains the WINGs toplevel */
1378 event->xany.window = wwin->client_leader;
1379 WMHandleEvent(event);
1381 dontLoop--;
1383 return;
1385 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1386 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1388 switch (command) {
1390 case WKBD_ROOTMENU:
1391 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1392 if (!CheckFullScreenWindowFocused(scr)) {
1393 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1394 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1395 True);
1397 break;
1398 case WKBD_WINDOWLIST:
1399 if (!CheckFullScreenWindowFocused(scr)) {
1400 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1401 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1402 True);
1404 break;
1406 case WKBD_WINDOWMENU:
1407 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1408 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1409 break;
1410 case WKBD_MINIMIZEALL:
1411 CloseWindowMenu(scr);
1412 wHideAll(scr);
1413 break;
1414 case WKBD_MINIATURIZE:
1415 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1416 && !WFLAGP(wwin, no_miniaturizable)) {
1417 CloseWindowMenu(scr);
1419 if (wwin->protocols.MINIATURIZE_WINDOW)
1420 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1421 else {
1422 wIconifyWindow(wwin);
1425 break;
1426 case WKBD_HIDE:
1427 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1428 WApplication *wapp = wApplicationOf(wwin->main_window);
1429 CloseWindowMenu(scr);
1431 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1432 wHideApplication(wapp);
1435 break;
1436 case WKBD_HIDE_OTHERS:
1437 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1438 CloseWindowMenu(scr);
1440 wHideOtherApplications(wwin);
1442 break;
1443 case WKBD_MAXIMIZE:
1444 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1445 CloseWindowMenu(scr);
1447 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_HORIZONTAL))
1448 wUnmaximizeWindow(wwin);
1449 else
1450 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1452 break;
1453 case WKBD_VMAXIMIZE:
1454 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1455 CloseWindowMenu(scr);
1457 if (wwin->flags.maximized == MAX_VERTICAL)
1458 wUnmaximizeWindow(wwin);
1459 else
1460 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1462 break;
1463 case WKBD_HMAXIMIZE:
1464 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1465 CloseWindowMenu(scr);
1467 if (wwin->flags.maximized == MAX_HORIZONTAL)
1468 wUnmaximizeWindow(wwin);
1469 else
1470 wMaximizeWindow(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1472 break;
1473 case WKBD_LHMAXIMIZE:
1474 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1475 CloseWindowMenu(scr);
1477 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_LEFTHALF))
1478 wUnmaximizeWindow(wwin);
1479 else
1480 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1482 break;
1483 case WKBD_RHMAXIMIZE:
1484 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1485 CloseWindowMenu(scr);
1487 if (wwin->flags.maximized == (MAX_VERTICAL | MAX_RIGHTHALF))
1488 wUnmaximizeWindow(wwin);
1489 else
1490 wMaximizeWindow(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1492 break;
1493 case WKBD_MAXIMUS:
1494 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1495 CloseWindowMenu(scr);
1497 if (wwin->flags.maximized == MAX_MAXIMUS)
1498 wUnmaximizeWindow(wwin);
1499 else
1500 wMaximizeWindow(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1502 break;
1503 case WKBD_RAISE:
1504 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1505 CloseWindowMenu(scr);
1507 wRaiseFrame(wwin->frame->core);
1509 break;
1510 case WKBD_LOWER:
1511 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1512 CloseWindowMenu(scr);
1514 wLowerFrame(wwin->frame->core);
1516 break;
1517 case WKBD_RAISELOWER:
1518 /* raise or lower the window under the pointer, not the
1519 * focused one
1521 wwin = windowUnderPointer(scr);
1522 if (wwin)
1523 wRaiseLowerFrame(wwin->frame->core);
1524 break;
1525 case WKBD_SHADE:
1526 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1527 if (wwin->flags.shaded)
1528 wUnshadeWindow(wwin);
1529 else
1530 wShadeWindow(wwin);
1532 break;
1533 case WKBD_MOVERESIZE:
1534 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1535 CloseWindowMenu(scr);
1537 wKeyboardMoveResizeWindow(wwin);
1539 break;
1540 case WKBD_CLOSE:
1541 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1542 CloseWindowMenu(scr);
1543 if (wwin->protocols.DELETE_WINDOW)
1544 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1546 break;
1547 case WKBD_SELECT:
1548 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1549 wSelectWindow(wwin, !wwin->flags.selected);
1551 break;
1552 case WKBD_FOCUSNEXT:
1553 StartWindozeCycle(wwin, event, True, False);
1554 break;
1556 case WKBD_FOCUSPREV:
1557 StartWindozeCycle(wwin, event, False, False);
1558 break;
1560 case WKBD_GROUPNEXT:
1561 StartWindozeCycle(wwin, event, True, True);
1562 break;
1564 case WKBD_GROUPPREV:
1565 StartWindozeCycle(wwin, event, False, True);
1566 break;
1568 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1569 widx = command - WKBD_WORKSPACE1;
1570 i = (scr->current_workspace / 10) * 10 + widx;
1571 if (wPreferences.ws_advance || i < scr->workspace_count)
1572 wWorkspaceChange(scr, i);
1573 break;
1575 case WKBD_NEXTWORKSPACE:
1576 wWorkspaceRelativeChange(scr, 1);
1577 break;
1578 case WKBD_PREVWORKSPACE:
1579 wWorkspaceRelativeChange(scr, -1);
1580 break;
1582 case WKBD_WINDOW1:
1583 case WKBD_WINDOW2:
1584 case WKBD_WINDOW3:
1585 case WKBD_WINDOW4:
1586 case WKBD_WINDOW5:
1587 case WKBD_WINDOW6:
1588 case WKBD_WINDOW7:
1589 case WKBD_WINDOW8:
1590 case WKBD_WINDOW9:
1591 case WKBD_WINDOW10:
1593 widx = command - WKBD_WINDOW1;
1595 if (scr->shortcutWindows[widx]) {
1596 WMArray *list = scr->shortcutWindows[widx];
1597 int cw;
1598 int count = WMGetArrayItemCount(list);
1599 WWindow *twin;
1600 WMArrayIterator iter;
1601 WWindow *wwin;
1603 wUnselectWindows(scr);
1604 cw = scr->current_workspace;
1606 WM_ETARETI_ARRAY(list, wwin, iter) {
1607 if (count > 1)
1608 wWindowChangeWorkspace(wwin, cw);
1610 wMakeWindowVisible(wwin);
1612 if (count > 1)
1613 wSelectWindow(wwin, True);
1616 /* rotate the order of windows, to create a cycling effect */
1617 twin = WMGetFromArray(list, 0);
1618 WMDeleteFromArray(list, 0);
1619 WMAddToArray(list, twin);
1621 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1622 if (scr->shortcutWindows[widx]) {
1623 WMFreeArray(scr->shortcutWindows[widx]);
1624 scr->shortcutWindows[widx] = NULL;
1627 if (wwin->flags.selected && scr->selected_windows) {
1628 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1629 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1630 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1631 } else {
1632 scr->shortcutWindows[widx] = WMCreateArray(4);
1633 WMAddToArray(scr->shortcutWindows[widx], wwin);
1636 wSelectWindow(wwin, !wwin->flags.selected);
1637 XFlush(dpy);
1638 wusleep(3000);
1639 wSelectWindow(wwin, !wwin->flags.selected);
1640 XFlush(dpy);
1642 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1644 if (wwin->flags.selected && scr->selected_windows) {
1645 if (scr->shortcutWindows[widx]) {
1646 WMFreeArray(scr->shortcutWindows[widx]);
1648 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1652 break;
1654 case WKBD_RELAUNCH:
1655 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1656 (void) RelaunchWindow(wwin);
1658 break;
1660 case WKBD_SWITCH_SCREEN:
1661 if (wScreenCount > 1) {
1662 WScreen *scr2;
1663 int i;
1665 /* find index of this screen */
1666 for (i = 0; i < wScreenCount; i++) {
1667 if (wScreenWithNumber(i) == scr)
1668 break;
1670 i++;
1671 if (i >= wScreenCount) {
1672 i = 0;
1674 scr2 = wScreenWithNumber(i);
1676 if (scr2) {
1677 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1678 scr2->scr_width / 2, scr2->scr_height / 2);
1681 break;
1683 case WKBD_NEXTWSLAYER:
1684 case WKBD_PREVWSLAYER:
1686 int row, column;
1688 row = scr->current_workspace / 10;
1689 column = scr->current_workspace % 10;
1691 if (command == WKBD_NEXTWSLAYER) {
1692 if ((row + 1) * 10 < scr->workspace_count)
1693 wWorkspaceChange(scr, column + (row + 1) * 10);
1694 } else {
1695 if (row > 0)
1696 wWorkspaceChange(scr, column + (row - 1) * 10);
1699 break;
1700 case WKBD_CLIPRAISELOWER:
1701 if (!wPreferences.flags.noclip)
1702 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1703 break;
1704 case WKBD_DOCKRAISELOWER:
1705 if (!wPreferences.flags.nodock)
1706 wDockRaiseLower(scr->dock);
1707 break;
1708 #ifdef KEEP_XKB_LOCK_STATUS
1709 case WKBD_TOGGLE:
1710 if (wPreferences.modelock) {
1711 /*toggle */
1712 wwin = scr->focused_window;
1714 if (wwin && wwin->flags.mapped
1715 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1716 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1717 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1719 wwin->frame->languagemode = wwin->frame->last_languagemode;
1720 wwin->frame->last_languagemode = staterec.group;
1721 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1725 break;
1726 #endif /* KEEP_XKB_LOCK_STATUS */
1730 static void handleMotionNotify(XEvent * event)
1732 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1734 if (wPreferences.scrollable_menus) {
1735 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1736 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1738 if (scr->flags.jump_back_pending ||
1739 p.x <= (rect.pos.x + 1) ||
1740 p.x >= (rect.pos.x + rect.size.width - 2) ||
1741 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1742 WMenu *menu;
1744 menu = wMenuUnderPointer(scr);
1745 if (menu != NULL)
1746 wMenuScroll(menu, event);
1751 static void handleVisibilityNotify(XEvent * event)
1753 WWindow *wwin;
1755 wwin = wWindowFor(event->xvisibility.window);
1756 if (!wwin)
1757 return;
1758 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);