Variable last_workspace moved to workspace object in global namespace
[wmaker-crm.git] / src / event.c
blob9f76a4f7dc92ac331684e7e341fd43e1bc549a2a
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 "cycling.h"
59 #include "keybind.h"
60 #include "application.h"
61 #include "stacking.h"
62 #include "defaults.h"
63 #include "workspace.h"
64 #include "dock.h"
65 #include "framewin.h"
66 #include "properties.h"
67 #include "balloon.h"
68 #include "xinerama.h"
69 #include "wmspec.h"
70 #include "rootmenu.h"
71 #include "colormap.h"
72 #include "screen.h"
73 #include "shutdown.h"
74 #include "misc.h"
75 #include "event.h"
76 #include "winmenu.h"
77 #include "switchmenu.h"
79 /******** Global Variables **********/
80 extern WShortKey wKeyBindings[WKBD_LAST];
81 extern int wScreenCount;
83 #define MOD_MASK wPreferences.modifier_mask
85 #ifdef SHAPE
86 extern Bool wShapeSupported;
87 extern int wShapeEventBase;
88 #endif
90 #ifdef KEEP_XKB_LOCK_STATUS
91 extern int wXkbEventBase;
92 #endif
94 /************ Local stuff ***********/
96 static void saveTimestamp(XEvent *event);
97 static void handleColormapNotify(XEvent *event);
98 static void handleMapNotify(XEvent *event);
99 static void handleUnmapNotify(XEvent *event);
100 static void handleButtonPress(XEvent *event);
101 static void handleExpose(XEvent *event);
102 static void handleDestroyNotify(XEvent *event);
103 static void handleConfigureRequest(XEvent *event);
104 static void handleMapRequest(XEvent *event);
105 static void handlePropertyNotify(XEvent *event);
106 static void handleEnterNotify(XEvent *event);
107 static void handleLeaveNotify(XEvent *event);
108 static void handleExtensions(XEvent *event);
109 static void handleClientMessage(XEvent *event);
110 static void handleKeyPress(XEvent *event);
111 static void handleFocusIn(XEvent *event);
112 static void handleMotionNotify(XEvent *event);
113 static void handleVisibilityNotify(XEvent *event);
114 static void handle_inotify_events(int fd, int wd);
115 static void wdelete_death_handler(WMagicNumber id);
118 #ifdef SHAPE
119 static void handleShapeNotify(XEvent *event);
120 #endif
122 #ifdef KEEP_XKB_LOCK_STATUS
123 static void handleXkbIndicatorStateNotify(XEvent *event);
124 #endif
126 /* real dead process handler */
127 static void handleDeadProcess(void *foo);
129 typedef struct DeadProcesses {
130 pid_t pid;
131 unsigned char exit_status;
132 } DeadProcesses;
134 /* stack of dead processes */
135 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
136 static int deadProcessPtr = 0;
138 typedef struct DeathHandler {
139 WDeathHandler *callback;
140 pid_t pid;
141 void *client_data;
142 } DeathHandler;
144 static WMArray *deathHandlers = NULL;
146 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
148 DeathHandler *handler;
150 handler = malloc(sizeof(DeathHandler));
151 if (!handler)
152 return 0;
154 handler->pid = pid;
155 handler->callback = callback;
156 handler->client_data = cdata;
158 if (!deathHandlers)
159 deathHandlers = WMCreateArrayWithDestructor(8, free);
161 WMAddToArray(deathHandlers, handler);
163 return handler;
166 static void wdelete_death_handler(WMagicNumber id)
168 DeathHandler *handler = (DeathHandler *) id;
170 if (!handler || !deathHandlers)
171 return;
173 /* array destructor will call free(handler) */
174 WMRemoveFromArray(deathHandlers, handler);
177 void DispatchEvent(XEvent * event)
179 if (deathHandlers)
180 handleDeadProcess(NULL);
182 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
183 WCHANGE_STATE(WSTATE_EXITING);
184 /* received SIGTERM */
186 * WMHandleEvent() can't be called from anything
187 * executed inside here, or we can get in a infinite
188 * recursive loop.
190 Shutdown(WSExitMode);
192 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
193 WCHANGE_STATE(WSTATE_RESTARTING);
195 Shutdown(WSRestartPreparationMode);
196 /* received SIGHUP */
197 Restart(NULL, True);
198 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
199 WCHANGE_STATE(WSTATE_NORMAL);
200 wDefaultsCheckDomains(NULL);
203 /* for the case that all that is wanted to be dispatched is
204 * the stuff above */
205 if (!event)
206 return;
208 saveTimestamp(event);
209 switch (event->type) {
210 case MapRequest:
211 handleMapRequest(event);
212 break;
214 case KeyPress:
215 handleKeyPress(event);
216 break;
218 case MotionNotify:
219 handleMotionNotify(event);
220 break;
222 case ConfigureRequest:
223 handleConfigureRequest(event);
224 break;
226 case DestroyNotify:
227 handleDestroyNotify(event);
228 break;
230 case MapNotify:
231 handleMapNotify(event);
232 break;
234 case UnmapNotify:
235 handleUnmapNotify(event);
236 break;
238 case ButtonPress:
239 handleButtonPress(event);
240 break;
242 case Expose:
243 handleExpose(event);
244 break;
246 case PropertyNotify:
247 handlePropertyNotify(event);
248 break;
250 case EnterNotify:
251 handleEnterNotify(event);
252 break;
254 case LeaveNotify:
255 handleLeaveNotify(event);
256 break;
258 case ClientMessage:
259 handleClientMessage(event);
260 break;
262 case ColormapNotify:
263 handleColormapNotify(event);
264 break;
266 case MappingNotify:
267 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
268 XRefreshKeyboardMapping(&event->xmapping);
269 break;
271 case FocusIn:
272 handleFocusIn(event);
273 break;
275 case VisibilityNotify:
276 handleVisibilityNotify(event);
277 break;
279 case ConfigureNotify:
280 #ifdef HAVE_XRANDR
281 if (event->xconfigure.window == DefaultRootWindow(dpy))
282 XRRUpdateConfiguration(event);
283 #endif
284 break;
286 default:
287 handleExtensions(event);
288 break;
292 #ifdef HAVE_INOTIFY
294 *----------------------------------------------------------------------
295 * handle_inotify_events-
296 * Check for inotify events
298 * Returns:
299 * After reading events for the given file descriptor (fd) and
300 * watch descriptor (wd)
302 * Side effects:
303 * Calls wDefaultsCheckDomains if config database is updated
304 *----------------------------------------------------------------------
306 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
307 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
308 static void handle_inotify_events(int fd, int wd)
310 ssize_t eventQLength, i = 0;
311 char buff[BUFF_SIZE] = { 0 };
312 /* Check config only once per read of the event queue */
313 int oneShotFlag = 0;
316 * Read off the queued events
317 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
318 * not occur; the block is on Xevents, but a config file change will normally
319 * occur as a result of an Xevent - so the event queue should never have more than
320 * a few entries before a read().
322 eventQLength = read(fd, buff, BUFF_SIZE);
324 /* check what events occured */
325 /* Should really check wd here too, but for now we only have one watch! */
326 while (i < eventQLength) {
327 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
330 * see inotify.h for event types.
332 if (pevent->mask & IN_DELETE_SELF) {
333 wwarning(_("the defaults database has been deleted!"
334 " Restart Window Maker to create the database" " with the default settings"));
335 close(fd);
337 if (pevent->mask & IN_UNMOUNT) {
338 wwarning(_("the unit containing the defaults database has"
339 " been unmounted. Setting --static mode." " Any changes will not be saved."));
340 close(fd);
341 wPreferences.flags.noupdates = 1;
343 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
344 wwarning(_("Inotify: Reading config files in defaults database."));
345 wDefaultsCheckDomains(NULL);
348 /* move to next event in the buffer */
349 i += sizeof(struct inotify_event) + pevent->len;
352 #endif /* HAVE_INOTIFY */
355 *----------------------------------------------------------------------
356 * EventLoop-
357 * Processes X and internal events indefinitely.
359 * Returns:
360 * Never returns
362 * Side effects:
363 * The LastTimestamp global variable is updated.
364 * Calls inotifyGetEvents if defaults database changes.
365 *----------------------------------------------------------------------
367 noreturn void EventLoop(void)
369 XEvent event;
370 #ifdef HAVE_INOTIFY
371 extern int inotifyFD;
372 extern int inotifyWD;
373 struct timeval time;
374 fd_set rfds;
375 int retVal = 0;
377 if (inotifyFD < 0 || inotifyWD < 0)
378 retVal = -1;
379 #endif
381 for (;;) {
383 WMNextEvent(dpy, &event); /* Blocks here */
384 WMHandleEvent(&event);
385 #ifdef HAVE_INOTIFY
386 if (retVal != -1) {
387 time.tv_sec = 0;
388 time.tv_usec = 0;
389 FD_ZERO(&rfds);
390 FD_SET(inotifyFD, &rfds);
392 /* check for available read data from inotify - don't block! */
393 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
395 if (retVal < 0) { /* an error has occured */
396 wwarning(_("select failed. The inotify instance will be closed."
397 " Changes to the defaults database will require"
398 " a restart to take effect."));
399 close(inotifyFD);
400 continue;
402 if (FD_ISSET(inotifyFD, &rfds))
403 handle_inotify_events(inotifyFD, inotifyWD);
405 #endif
410 *----------------------------------------------------------------------
411 * ProcessPendingEvents --
412 * Processes the events that are currently pending (at the time
413 * this function is called) in the display's queue.
415 * Returns:
416 * After the pending events that were present at the function call
417 * are processed.
419 * Side effects:
420 * Many -- whatever handling events may involve.
422 *----------------------------------------------------------------------
424 void ProcessPendingEvents(void)
426 XEvent event;
427 int count;
429 XSync(dpy, False);
431 /* Take a snapshot of the event count in the queue */
432 count = XPending(dpy);
434 while (count > 0 && XPending(dpy)) {
435 WMNextEvent(dpy, &event);
436 WMHandleEvent(&event);
437 count--;
441 Bool IsDoubleClick(WScreen * scr, XEvent * event)
443 if ((scr->last_click_time > 0) &&
444 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
445 && (event->xbutton.button == scr->last_click_button)
446 && (event->xbutton.window == scr->last_click_window)) {
448 scr->flags.next_click_is_not_double = 1;
449 scr->last_click_time = 0;
450 scr->last_click_window = event->xbutton.window;
452 return True;
454 return False;
457 void NotifyDeadProcess(pid_t pid, unsigned char status)
459 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
460 wwarning("stack overflow: too many dead processes");
461 return;
463 /* stack the process to be handled later,
464 * as this is called from the signal handler */
465 deadProcesses[deadProcessPtr].pid = pid;
466 deadProcesses[deadProcessPtr].exit_status = status;
467 deadProcessPtr++;
470 static void handleDeadProcess(void *foo)
472 DeathHandler *tmp;
473 int i;
475 for (i = 0; i < deadProcessPtr; i++) {
476 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
479 if (!deathHandlers) {
480 deadProcessPtr = 0;
481 return;
484 /* get the pids on the queue and call handlers */
485 while (deadProcessPtr > 0) {
486 deadProcessPtr--;
488 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
489 tmp = WMGetFromArray(deathHandlers, i);
490 if (!tmp)
491 continue;
493 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
494 (*tmp->callback) (tmp->pid,
495 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
496 wdelete_death_handler(tmp);
502 static void saveTimestamp(XEvent * event)
505 * Never save CurrentTime as LastTimestamp because CurrentTime
506 * it's not a real timestamp (it's the 0L constant)
509 switch (event->type) {
510 case ButtonRelease:
511 case ButtonPress:
512 w_global.timestamp.last_event = event->xbutton.time;
513 break;
514 case KeyPress:
515 case KeyRelease:
516 w_global.timestamp.last_event = event->xkey.time;
517 break;
518 case MotionNotify:
519 w_global.timestamp.last_event = event->xmotion.time;
520 break;
521 case PropertyNotify:
522 w_global.timestamp.last_event = event->xproperty.time;
523 break;
524 case EnterNotify:
525 case LeaveNotify:
526 w_global.timestamp.last_event = event->xcrossing.time;
527 break;
528 case SelectionClear:
529 w_global.timestamp.last_event = event->xselectionclear.time;
530 break;
531 case SelectionRequest:
532 w_global.timestamp.last_event = event->xselectionrequest.time;
533 break;
534 case SelectionNotify:
535 w_global.timestamp.last_event = event->xselection.time;
536 #ifdef XDND
537 wXDNDProcessSelection(event);
538 #endif
539 break;
543 static int matchWindow(const void *item, const void *cdata)
545 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
548 static void handleExtensions(XEvent * event)
550 #ifdef KEEP_XKB_LOCK_STATUS
551 XkbEvent *xkbevent;
552 xkbevent = (XkbEvent *) event;
553 #endif /*KEEP_XKB_LOCK_STATUS */
554 #ifdef SHAPE
555 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
556 handleShapeNotify(event);
558 #endif
559 #ifdef KEEP_XKB_LOCK_STATUS
560 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
561 handleXkbIndicatorStateNotify(event);
563 #endif /*KEEP_XKB_LOCK_STATUS */
564 #ifdef HAVE_XRANDR
565 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
566 /* From xrandr man page: "Clients must call back into Xlib using
567 * XRRUpdateConfiguration when screen configuration change notify
568 * events are generated */
569 XRRUpdateConfiguration(event);
570 WCHANGE_STATE(WSTATE_RESTARTING);
571 Shutdown(WSRestartPreparationMode);
572 Restart(NULL,True);
574 #endif
577 static void handleMapRequest(XEvent * ev)
579 WWindow *wwin;
580 WScreen *scr = NULL;
581 Window window = ev->xmaprequest.window;
583 if ((wwin = wWindowFor(window))) {
584 if (wwin->flags.shaded) {
585 wUnshadeWindow(wwin);
587 /* deiconify window */
588 if (wwin->flags.miniaturized) {
589 wDeiconifyWindow(wwin);
590 } else if (wwin->flags.hidden) {
591 WApplication *wapp = wApplicationOf(wwin->main_window);
592 /* go to the last workspace that the user worked on the app */
593 if (wapp) {
594 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
596 wUnhideApplication(wapp, False, False);
598 return;
601 scr = wScreenForRootWindow(ev->xmaprequest.parent);
603 wwin = wManageWindow(scr, window);
606 * This is to let the Dock know that the application it launched
607 * has already been mapped (eg: it has finished launching).
608 * It is not necessary for normally docked apps, but is needed for
609 * apps that were forcedly docked (like with dockit).
611 if (scr->last_dock) {
612 if (wwin && wwin->main_window != None && wwin->main_window != window)
613 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
614 else
615 wDockTrackWindowLaunch(scr->last_dock, window);
618 if (wwin) {
619 wClientSetState(wwin, NormalState, None);
620 if (wwin->flags.maximized) {
621 wMaximizeWindow(wwin, wwin->flags.maximized);
623 if (wwin->flags.shaded) {
624 wwin->flags.shaded = 0;
625 wwin->flags.skip_next_animation = 1;
626 wShadeWindow(wwin);
628 if (wwin->flags.miniaturized) {
629 wwin->flags.miniaturized = 0;
630 wwin->flags.skip_next_animation = 1;
631 wIconifyWindow(wwin);
633 if (wwin->flags.fullscreen) {
634 wwin->flags.fullscreen = 0;
635 wFullscreenWindow(wwin);
637 if (wwin->flags.hidden) {
638 WApplication *wapp = wApplicationOf(wwin->main_window);
640 wwin->flags.hidden = 0;
641 wwin->flags.skip_next_animation = 1;
642 if (wapp) {
643 wHideApplication(wapp);
649 static void handleDestroyNotify(XEvent * event)
651 WWindow *wwin;
652 WApplication *app;
653 Window window = event->xdestroywindow.window;
654 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
655 int widx;
657 wwin = wWindowFor(window);
658 if (wwin) {
659 wUnmanageWindow(wwin, False, True);
662 if (scr != NULL) {
663 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
664 WFakeGroupLeader *fPtr;
666 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
667 if (fPtr->retainCount > 0) {
668 fPtr->retainCount--;
669 if (fPtr->retainCount == 0 && fPtr->leader != None) {
670 XDestroyWindow(dpy, fPtr->leader);
671 fPtr->leader = None;
672 XFlush(dpy);
675 fPtr->origLeader = None;
679 app = wApplicationOf(window);
680 if (app) {
681 if (window == app->main_window) {
682 app->refcount = 0;
683 wwin = app->main_window_desc->screen_ptr->focused_window;
684 while (wwin) {
685 if (wwin->main_window == window) {
686 wwin->main_window = None;
688 wwin = wwin->prev;
691 wApplicationDestroy(app);
695 static void handleExpose(XEvent * event)
697 WObjDescriptor *desc;
698 XEvent ev;
700 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
702 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
703 return;
706 if (desc->handle_expose) {
707 (*desc->handle_expose) (desc, event);
711 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
713 switch (action) {
714 case WA_SELECT_WINDOWS:
715 wUnselectWindows(scr);
716 wSelectWindows(scr, event);
717 break;
718 case WA_OPEN_APPMENU:
719 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
720 /* ugly hack */
721 if (scr->root_menu) {
722 if (scr->root_menu->brother->flags.mapped)
723 event->xbutton.window = scr->root_menu->brother->frame->core->window;
724 else
725 event->xbutton.window = scr->root_menu->frame->core->window;
727 break;
728 case WA_OPEN_WINLISTMENU:
729 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
730 if (scr->switch_menu) {
731 if (scr->switch_menu->brother->flags.mapped)
732 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
733 else
734 event->xbutton.window = scr->switch_menu->frame->core->window;
736 break;
737 default:
738 break;
742 /* bindable */
743 static void handleButtonPress(XEvent * event)
745 WObjDescriptor *desc;
746 WScreen *scr;
748 scr = wScreenForRootWindow(event->xbutton.root);
750 #ifdef BALLOON_TEXT
751 wBalloonHide(scr);
752 #endif
754 if (event->xbutton.window == scr->root_win) {
755 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
756 executeButtonAction(scr, event, wPreferences.mouse_button1);
757 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
758 executeButtonAction(scr, event, wPreferences.mouse_button2);
759 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
760 executeButtonAction(scr, event, wPreferences.mouse_button3);
761 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
762 wWorkspaceRelativeChange(scr, 1);
763 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
764 wWorkspaceRelativeChange(scr, -1);
768 desc = NULL;
769 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
770 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
771 return;
775 if (desc->parent_type == WCLASS_WINDOW) {
776 XSync(dpy, 0);
778 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
779 XAllowEvents(dpy, AsyncPointer, CurrentTime);
780 } else {
781 /* if (wPreferences.focus_mode == WKF_CLICK) { */
782 if (wPreferences.ignore_focus_click) {
783 XAllowEvents(dpy, AsyncPointer, CurrentTime);
785 XAllowEvents(dpy, ReplayPointer, CurrentTime);
786 /* } */
788 XSync(dpy, 0);
789 } else if (desc->parent_type == WCLASS_APPICON
790 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
791 if (event->xbutton.state & MOD_MASK) {
792 XSync(dpy, 0);
793 XAllowEvents(dpy, AsyncPointer, CurrentTime);
794 XSync(dpy, 0);
798 if (desc->handle_mousedown != NULL) {
799 (*desc->handle_mousedown) (desc, event);
802 /* save double-click information */
803 if (scr->flags.next_click_is_not_double) {
804 scr->flags.next_click_is_not_double = 0;
805 } else {
806 scr->last_click_time = event->xbutton.time;
807 scr->last_click_button = event->xbutton.button;
808 scr->last_click_window = event->xbutton.window;
812 static void handleMapNotify(XEvent * event)
814 WWindow *wwin;
816 wwin = wWindowFor(event->xmap.event);
817 if (wwin && wwin->client_win == event->xmap.event) {
818 if (wwin->flags.miniaturized) {
819 wDeiconifyWindow(wwin);
820 } else {
821 XGrabServer(dpy);
822 wWindowMap(wwin);
823 wClientSetState(wwin, NormalState, None);
824 XUngrabServer(dpy);
829 static void handleUnmapNotify(XEvent * event)
831 WWindow *wwin;
832 XEvent ev;
833 Bool withdraw = False;
835 /* only process windows with StructureNotify selected
836 * (ignore SubstructureNotify) */
837 wwin = wWindowFor(event->xunmap.window);
838 if (!wwin)
839 return;
841 /* whether the event is a Withdrawal request */
842 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
843 withdraw = True;
845 if (wwin->client_win != event->xunmap.event && !withdraw)
846 return;
848 if (!wwin->flags.mapped && !withdraw
849 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
850 && !wwin->flags.miniaturized && !wwin->flags.hidden)
851 return;
853 XGrabServer(dpy);
854 XUnmapWindow(dpy, wwin->frame->core->window);
855 wwin->flags.mapped = 0;
856 XSync(dpy, 0);
857 /* check if the window was destroyed */
858 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
859 DispatchEvent(&ev);
860 } else {
861 Bool reparented = False;
863 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
864 reparented = True;
866 /* withdraw window */
867 wwin->flags.mapped = 0;
868 if (!reparented)
869 wClientSetState(wwin, WithdrawnState, None);
871 /* if the window was reparented, do not reparent it back to the
872 * root window */
873 wUnmanageWindow(wwin, !reparented, False);
875 XUngrabServer(dpy);
878 static void handleConfigureRequest(XEvent * event)
880 WWindow *wwin;
882 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
884 * Configure request for unmapped window
886 wClientConfigure(NULL, &(event->xconfigurerequest));
887 } else {
888 wClientConfigure(wwin, &(event->xconfigurerequest));
892 static void handlePropertyNotify(XEvent * event)
894 WWindow *wwin;
895 WApplication *wapp;
896 Window jr;
897 int ji;
898 unsigned int ju;
900 wwin = wWindowFor(event->xproperty.window);
901 if (wwin) {
902 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
903 return;
905 wClientCheckProperty(wwin, &event->xproperty);
907 wapp = wApplicationOf(event->xproperty.window);
908 if (wapp) {
909 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
913 static void handleClientMessage(XEvent * event)
915 WWindow *wwin;
916 WObjDescriptor *desc;
918 /* handle transition from Normal to Iconic state */
919 if (event->xclient.message_type == w_global.atom.wm.change_state
920 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
922 wwin = wWindowFor(event->xclient.window);
923 if (!wwin)
924 return;
925 if (!wwin->flags.miniaturized)
926 wIconifyWindow(wwin);
927 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
928 WScreen *scr = wScreenForRootWindow(event->xclient.window);
930 if (!scr)
931 return;
933 if (event->xclient.data.l[1] == 1) { /* starting */
934 wColormapAllowClientInstallation(scr, True);
935 } else { /* stopping */
936 wColormapAllowClientInstallation(scr, False);
938 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
940 char *command;
941 size_t len;
943 len = sizeof(event->xclient.data.b) + 1;
944 command = wmalloc(len);
945 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
947 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
948 wwarning(_("Got Reconfigure command"));
949 wDefaultsCheckDomains(NULL);
950 } else {
951 wwarning(_("Got unknown command %s"), command);
954 wfree(command);
956 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
957 WApplication *wapp;
958 int done = 0;
959 wapp = wApplicationOf(event->xclient.window);
960 if (wapp) {
961 switch (event->xclient.data.l[0]) {
962 case WMFHideOtherApplications:
963 wHideOtherApplications(wapp->main_window_desc);
964 done = 1;
965 break;
967 case WMFHideApplication:
968 wHideApplication(wapp);
969 done = 1;
970 break;
973 if (!done) {
974 wwin = wWindowFor(event->xclient.window);
975 if (wwin) {
976 switch (event->xclient.data.l[0]) {
977 case WMFHideOtherApplications:
978 wHideOtherApplications(wwin);
979 break;
981 case WMFHideApplication:
982 wHideApplication(wApplicationOf(wwin->main_window));
983 break;
987 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
988 wwin = wWindowFor(event->xclient.window);
989 if (!wwin)
990 return;
991 switch (event->xclient.data.l[0]) {
992 case GSWindowLevelAttr:
994 int level = (int)event->xclient.data.l[1];
996 if (WINDOW_LEVEL(wwin) != level) {
997 ChangeStackingLevel(wwin->frame->core, level);
1000 break;
1002 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1003 wwin = wWindowFor(event->xclient.window);
1004 if (!wwin)
1005 return;
1006 switch (event->xclient.data.l[0]) {
1007 case WMTitleBarNormal:
1008 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1009 break;
1010 case WMTitleBarMain:
1011 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1012 break;
1013 case WMTitleBarKey:
1014 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1015 break;
1017 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1018 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1019 if (!scr)
1020 return;
1021 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1022 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1023 /* do nothing */
1024 #ifdef XDND
1025 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1026 /* do nothing */
1027 #endif /* XDND */
1028 } else {
1030 * Non-standard thing, but needed by OffiX DND.
1031 * For when the icon frame gets a ClientMessage
1032 * that should have gone to the icon_window.
1034 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1035 struct WIcon *icon = NULL;
1037 if (desc->parent_type == WCLASS_MINIWINDOW) {
1038 icon = (WIcon *) desc->parent;
1039 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1040 icon = ((WAppIcon *) desc->parent)->icon;
1042 if (icon && (wwin = icon->owner)) {
1043 if (wwin->client_win != event->xclient.window) {
1044 event->xclient.window = wwin->client_win;
1045 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1052 static void raiseWindow(WScreen * scr)
1054 WWindow *wwin;
1056 scr->autoRaiseTimer = NULL;
1058 wwin = wWindowFor(scr->autoRaiseWindow);
1059 if (!wwin)
1060 return;
1062 if (!wwin->flags.destroyed && wwin->flags.focused) {
1063 wRaiseFrame(wwin->frame->core);
1064 /* this is needed or a race condition will occur */
1065 XSync(dpy, False);
1069 static void handleEnterNotify(XEvent * event)
1071 WWindow *wwin;
1072 WObjDescriptor *desc = NULL;
1073 XEvent ev;
1074 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1076 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1077 /* already left the window... */
1078 saveTimestamp(&ev);
1079 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1080 return;
1084 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1085 if (desc->handle_enternotify)
1086 (*desc->handle_enternotify) (desc, event);
1089 /* enter to window */
1090 wwin = wWindowFor(event->xcrossing.window);
1091 if (!wwin) {
1092 if (wPreferences.colormap_mode == WCM_POINTER) {
1093 wColormapInstallForWindow(scr, NULL);
1095 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1096 WMDeleteTimerHandler(scr->autoRaiseTimer);
1097 scr->autoRaiseTimer = NULL;
1099 } else {
1100 /* set auto raise timer even if in focus-follows-mouse mode
1101 * and the event is for the frame window, even if the window
1102 * has focus already. useful if you move the pointer from a focused
1103 * window to the root window and back pretty fast
1105 * set focus if in focus-follows-mouse mode and the event
1106 * is for the frame window and window doesn't have focus yet */
1107 if (wPreferences.focus_mode == WKF_SLOPPY
1108 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1110 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1111 wSetFocusTo(scr, wwin);
1113 if (scr->autoRaiseTimer)
1114 WMDeleteTimerHandler(scr->autoRaiseTimer);
1115 scr->autoRaiseTimer = NULL;
1117 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1118 scr->autoRaiseWindow = wwin->frame->core->window;
1119 scr->autoRaiseTimer
1120 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1123 /* Install colormap for window, if the colormap installation mode
1124 * is colormap_follows_mouse */
1125 if (wPreferences.colormap_mode == WCM_POINTER) {
1126 if (wwin->client_win == event->xcrossing.window)
1127 wColormapInstallForWindow(scr, wwin);
1128 else
1129 wColormapInstallForWindow(scr, NULL);
1133 if (event->xcrossing.window == event->xcrossing.root
1134 && event->xcrossing.detail == NotifyNormal
1135 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1137 wSetFocusTo(scr, scr->focused_window);
1139 #ifdef BALLOON_TEXT
1140 wBalloonEnteredObject(scr, desc);
1141 #endif
1144 static void handleLeaveNotify(XEvent * event)
1146 WObjDescriptor *desc = NULL;
1148 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1149 if (desc->handle_leavenotify)
1150 (*desc->handle_leavenotify) (desc, event);
1154 #ifdef SHAPE
1155 static void handleShapeNotify(XEvent * event)
1157 XShapeEvent *shev = (XShapeEvent *) event;
1158 WWindow *wwin;
1159 union {
1160 XEvent xevent;
1161 XShapeEvent xshape;
1162 } ev;
1164 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1165 if (ev.xshape.kind == ShapeBounding) {
1166 if (ev.xshape.shaped == shev->shaped) {
1167 *shev = ev.xshape;
1168 } else {
1169 XPutBackEvent(dpy, &ev.xevent);
1170 break;
1175 wwin = wWindowFor(shev->window);
1176 if (!wwin || shev->kind != ShapeBounding)
1177 return;
1179 if (!shev->shaped && wwin->flags.shaped) {
1181 wwin->flags.shaped = 0;
1182 wWindowClearShape(wwin);
1184 } else if (shev->shaped) {
1186 wwin->flags.shaped = 1;
1187 wWindowSetShape(wwin);
1190 #endif /* SHAPE */
1192 #ifdef KEEP_XKB_LOCK_STATUS
1193 /* please help ]d if you know what to do */
1194 static void handleXkbIndicatorStateNotify(XEvent *event)
1196 WWindow *wwin;
1197 WScreen *scr;
1198 XkbStateRec staterec;
1199 int i;
1201 for (i = 0; i < wScreenCount; i++) {
1202 scr = wScreenWithNumber(i);
1203 wwin = scr->focused_window;
1204 if (wwin && wwin->flags.focused) {
1205 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1206 if (wwin->frame->languagemode != staterec.group) {
1207 wwin->frame->last_languagemode = wwin->frame->languagemode;
1208 wwin->frame->languagemode = staterec.group;
1210 #ifdef XKB_BUTTON_HINT
1211 if (wwin->frame->titlebar) {
1212 wFrameWindowPaint(wwin->frame);
1214 #endif
1218 #endif /*KEEP_XKB_LOCK_STATUS */
1220 static void handleColormapNotify(XEvent * event)
1222 WWindow *wwin;
1223 WScreen *scr;
1224 Bool reinstall = False;
1226 wwin = wWindowFor(event->xcolormap.window);
1227 if (!wwin)
1228 return;
1230 scr = wwin->screen_ptr;
1232 do {
1233 if (wwin) {
1234 if (event->xcolormap.new) {
1235 XWindowAttributes attr;
1237 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1239 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1240 scr->current_colormap = attr.colormap;
1242 reinstall = True;
1243 } else if (event->xcolormap.state == ColormapUninstalled &&
1244 scr->current_colormap == event->xcolormap.colormap) {
1246 /* some bastard app (like XV) removed our colormap */
1248 * can't enforce or things like xscreensaver wont work
1249 * reinstall = True;
1251 } else if (event->xcolormap.state == ColormapInstalled &&
1252 scr->current_colormap == event->xcolormap.colormap) {
1254 /* someone has put our colormap back */
1255 reinstall = False;
1258 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1259 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1261 if (reinstall && scr->current_colormap != None) {
1262 if (!scr->flags.colormap_stuff_blocked)
1263 XInstallColormap(dpy, scr->current_colormap);
1267 static void handleFocusIn(XEvent * event)
1269 WWindow *wwin;
1272 * For applications that like stealing the focus.
1274 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1275 saveTimestamp(event);
1276 if (event->xfocus.mode == NotifyUngrab
1277 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1278 return;
1281 wwin = wWindowFor(event->xfocus.window);
1282 if (wwin && !wwin->flags.focused) {
1283 if (wwin->flags.mapped)
1284 wSetFocusTo(wwin->screen_ptr, wwin);
1285 else
1286 wSetFocusTo(wwin->screen_ptr, NULL);
1287 } else if (!wwin) {
1288 WScreen *scr = wScreenForWindow(event->xfocus.window);
1289 if (scr)
1290 wSetFocusTo(scr, NULL);
1294 static WWindow *windowUnderPointer(WScreen * scr)
1296 unsigned int mask;
1297 int foo;
1298 Window bar, win;
1300 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1301 return wWindowFor(win);
1302 return NULL;
1305 static int CheckFullScreenWindowFocused(WScreen * scr)
1307 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1308 return 1;
1309 else
1310 return 0;
1313 static void handleKeyPress(XEvent * event)
1315 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1316 WWindow *wwin = scr->focused_window;
1317 short i, widx;
1318 int modifiers;
1319 int command = -1;
1320 #ifdef KEEP_XKB_LOCK_STATUS
1321 XkbStateRec staterec;
1322 #endif /*KEEP_XKB_LOCK_STATUS */
1324 /* ignore CapsLock */
1325 modifiers = event->xkey.state & ValidModMask;
1327 for (i = 0; i < WKBD_LAST; i++) {
1328 if (wKeyBindings[i].keycode == 0)
1329 continue;
1331 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1332 || */ wKeyBindings[i].modifier ==
1333 modifiers)) {
1334 command = i;
1335 break;
1339 if (command < 0) {
1341 if (!wRootMenuPerformShortcut(event)) {
1342 static int dontLoop = 0;
1344 if (dontLoop > 10) {
1345 wwarning("problem with key event processing code");
1346 return;
1348 dontLoop++;
1349 /* if the focused window is an internal window, try redispatching
1350 * the event to the managed window, as it can be a WINGs window */
1351 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1352 /* client_leader contains the WINGs toplevel */
1353 event->xany.window = wwin->client_leader;
1354 WMHandleEvent(event);
1356 dontLoop--;
1358 return;
1360 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1361 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1363 switch (command) {
1365 case WKBD_ROOTMENU:
1366 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1367 if (!CheckFullScreenWindowFocused(scr)) {
1368 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1369 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1370 True);
1372 break;
1373 case WKBD_WINDOWLIST:
1374 if (!CheckFullScreenWindowFocused(scr)) {
1375 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1376 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1377 True);
1379 break;
1381 case WKBD_WINDOWMENU:
1382 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1383 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1384 break;
1385 case WKBD_MINIMIZEALL:
1386 CloseWindowMenu(scr);
1387 wHideAll(scr);
1388 break;
1389 case WKBD_MINIATURIZE:
1390 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1391 && !WFLAGP(wwin, no_miniaturizable)) {
1392 CloseWindowMenu(scr);
1394 if (wwin->protocols.MINIATURIZE_WINDOW)
1395 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1396 else {
1397 wIconifyWindow(wwin);
1400 break;
1401 case WKBD_HIDE:
1402 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1403 WApplication *wapp = wApplicationOf(wwin->main_window);
1404 CloseWindowMenu(scr);
1406 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1407 wHideApplication(wapp);
1410 break;
1411 case WKBD_HIDE_OTHERS:
1412 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1413 CloseWindowMenu(scr);
1415 wHideOtherApplications(wwin);
1417 break;
1418 case WKBD_MAXIMIZE:
1419 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1420 CloseWindowMenu(scr);
1422 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1424 break;
1425 case WKBD_VMAXIMIZE:
1426 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1427 CloseWindowMenu(scr);
1429 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1431 break;
1432 case WKBD_HMAXIMIZE:
1433 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1434 CloseWindowMenu(scr);
1436 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1438 break;
1439 case WKBD_LHMAXIMIZE:
1440 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1441 CloseWindowMenu(scr);
1443 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1445 break;
1446 case WKBD_RHMAXIMIZE:
1447 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1448 CloseWindowMenu(scr);
1450 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1452 break;
1453 case WKBD_THMAXIMIZE:
1454 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1455 CloseWindowMenu(scr);
1457 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1459 break;
1460 case WKBD_BHMAXIMIZE:
1461 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1462 CloseWindowMenu(scr);
1464 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1466 break;
1467 case WKBD_LTCMAXIMIZE:
1468 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1469 CloseWindowMenu(scr);
1471 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1473 break;
1474 case WKBD_RTCMAXIMIZE:
1475 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1476 CloseWindowMenu(scr);
1478 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1480 break;
1481 case WKBD_LBCMAXIMIZE:
1482 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1483 CloseWindowMenu(scr);
1485 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1487 break;
1488 case WKBD_RBCMAXIMIZE:
1489 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1490 CloseWindowMenu(scr);
1492 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1494 break;
1495 case WKBD_MAXIMUS:
1496 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1497 CloseWindowMenu(scr);
1499 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1501 break;
1502 case WKBD_RAISE:
1503 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1504 CloseWindowMenu(scr);
1506 wRaiseFrame(wwin->frame->core);
1508 break;
1509 case WKBD_LOWER:
1510 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1511 CloseWindowMenu(scr);
1513 wLowerFrame(wwin->frame->core);
1515 break;
1516 case WKBD_RAISELOWER:
1517 /* raise or lower the window under the pointer, not the
1518 * focused one
1520 wwin = windowUnderPointer(scr);
1521 if (wwin)
1522 wRaiseLowerFrame(wwin->frame->core);
1523 break;
1524 case WKBD_SHADE:
1525 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1526 if (wwin->flags.shaded)
1527 wUnshadeWindow(wwin);
1528 else
1529 wShadeWindow(wwin);
1531 break;
1532 case WKBD_MOVERESIZE:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1534 CloseWindowMenu(scr);
1536 wKeyboardMoveResizeWindow(wwin);
1538 break;
1539 case WKBD_CLOSE:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1541 CloseWindowMenu(scr);
1542 if (wwin->protocols.DELETE_WINDOW)
1543 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1545 break;
1546 case WKBD_SELECT:
1547 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1548 wSelectWindow(wwin, !wwin->flags.selected);
1550 break;
1551 case WKBD_FOCUSNEXT:
1552 StartWindozeCycle(wwin, event, True, False);
1553 break;
1555 case WKBD_FOCUSPREV:
1556 StartWindozeCycle(wwin, event, False, False);
1557 break;
1559 case WKBD_GROUPNEXT:
1560 StartWindozeCycle(wwin, event, True, True);
1561 break;
1563 case WKBD_GROUPPREV:
1564 StartWindozeCycle(wwin, event, False, True);
1565 break;
1567 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1568 widx = command - WKBD_WORKSPACE1;
1569 i = (scr->current_workspace / 10) * 10 + widx;
1570 if (wPreferences.ws_advance || i < w_global.workspace.count)
1571 wWorkspaceChange(scr, i);
1572 break;
1574 case WKBD_NEXTWORKSPACE:
1575 wWorkspaceRelativeChange(scr, 1);
1576 break;
1577 case WKBD_PREVWORKSPACE:
1578 wWorkspaceRelativeChange(scr, -1);
1579 break;
1580 case WKBD_LASTWORKSPACE:
1581 wWorkspaceChange(scr, w_global.workspace.last_used);
1582 break;
1584 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1585 widx = command - WKBD_MOVE_WORKSPACE1;
1586 i = (scr->current_workspace / 10) * 10 + widx;
1587 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1588 wWindowChangeWorkspace(wwin, i);
1589 break;
1591 case WKBD_MOVE_NEXTWORKSPACE:
1592 if (wwin)
1593 wWindowChangeWorkspaceRelative(wwin, 1);
1594 break;
1595 case WKBD_MOVE_PREVWORKSPACE:
1596 if (wwin)
1597 wWindowChangeWorkspaceRelative(wwin, -1);
1598 break;
1599 case WKBD_MOVE_LASTWORKSPACE:
1600 if (wwin)
1601 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1602 break;
1604 case WKBD_MOVE_NEXTWSLAYER:
1605 case WKBD_MOVE_PREVWSLAYER:
1607 if (wwin) {
1608 int row, column;
1610 row = scr->current_workspace / 10;
1611 column = scr->current_workspace % 10;
1613 if (command == WKBD_MOVE_NEXTWSLAYER) {
1614 if ((row + 1) * 10 < w_global.workspace.count)
1615 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1616 } else {
1617 if (row > 0)
1618 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1622 break;
1624 case WKBD_WINDOW1:
1625 case WKBD_WINDOW2:
1626 case WKBD_WINDOW3:
1627 case WKBD_WINDOW4:
1628 case WKBD_WINDOW5:
1629 case WKBD_WINDOW6:
1630 case WKBD_WINDOW7:
1631 case WKBD_WINDOW8:
1632 case WKBD_WINDOW9:
1633 case WKBD_WINDOW10:
1635 widx = command - WKBD_WINDOW1;
1637 if (scr->shortcutWindows[widx]) {
1638 WMArray *list = scr->shortcutWindows[widx];
1639 int cw;
1640 int count = WMGetArrayItemCount(list);
1641 WWindow *twin;
1642 WMArrayIterator iter;
1643 WWindow *wwin;
1645 wUnselectWindows(scr);
1646 cw = scr->current_workspace;
1648 WM_ETARETI_ARRAY(list, wwin, iter) {
1649 if (count > 1)
1650 wWindowChangeWorkspace(wwin, cw);
1652 wMakeWindowVisible(wwin);
1654 if (count > 1)
1655 wSelectWindow(wwin, True);
1658 /* rotate the order of windows, to create a cycling effect */
1659 twin = WMGetFromArray(list, 0);
1660 WMDeleteFromArray(list, 0);
1661 WMAddToArray(list, twin);
1663 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1664 if (scr->shortcutWindows[widx]) {
1665 WMFreeArray(scr->shortcutWindows[widx]);
1666 scr->shortcutWindows[widx] = NULL;
1669 if (wwin->flags.selected && scr->selected_windows) {
1670 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1671 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1672 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1673 } else {
1674 scr->shortcutWindows[widx] = WMCreateArray(4);
1675 WMAddToArray(scr->shortcutWindows[widx], wwin);
1678 wSelectWindow(wwin, !wwin->flags.selected);
1679 XFlush(dpy);
1680 wusleep(3000);
1681 wSelectWindow(wwin, !wwin->flags.selected);
1682 XFlush(dpy);
1684 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1686 if (wwin->flags.selected && scr->selected_windows) {
1687 if (scr->shortcutWindows[widx]) {
1688 WMFreeArray(scr->shortcutWindows[widx]);
1690 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1694 break;
1696 case WKBD_RELAUNCH:
1697 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1698 (void) RelaunchWindow(wwin);
1700 break;
1702 case WKBD_SWITCH_SCREEN:
1703 if (wScreenCount > 1) {
1704 WScreen *scr2;
1705 int i;
1707 /* find index of this screen */
1708 for (i = 0; i < wScreenCount; i++) {
1709 if (wScreenWithNumber(i) == scr)
1710 break;
1712 i++;
1713 if (i >= wScreenCount) {
1714 i = 0;
1716 scr2 = wScreenWithNumber(i);
1718 if (scr2) {
1719 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1720 scr2->scr_width / 2, scr2->scr_height / 2);
1723 break;
1725 case WKBD_NEXTWSLAYER:
1726 case WKBD_PREVWSLAYER:
1728 int row, column;
1730 row = scr->current_workspace / 10;
1731 column = scr->current_workspace % 10;
1733 if (command == WKBD_NEXTWSLAYER) {
1734 if ((row + 1) * 10 < w_global.workspace.count)
1735 wWorkspaceChange(scr, column + (row + 1) * 10);
1736 } else {
1737 if (row > 0)
1738 wWorkspaceChange(scr, column + (row - 1) * 10);
1741 break;
1742 case WKBD_CLIPRAISELOWER:
1743 if (!wPreferences.flags.noclip)
1744 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1745 break;
1746 case WKBD_DOCKRAISELOWER:
1747 if (!wPreferences.flags.nodock)
1748 wDockRaiseLower(scr->dock);
1749 break;
1750 #ifdef KEEP_XKB_LOCK_STATUS
1751 case WKBD_TOGGLE:
1752 if (wPreferences.modelock) {
1753 /*toggle */
1754 wwin = scr->focused_window;
1756 if (wwin && wwin->flags.mapped
1757 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1758 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1759 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1761 wwin->frame->languagemode = wwin->frame->last_languagemode;
1762 wwin->frame->last_languagemode = staterec.group;
1763 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1767 break;
1768 #endif /* KEEP_XKB_LOCK_STATUS */
1772 static void handleMotionNotify(XEvent * event)
1774 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1776 if (wPreferences.scrollable_menus) {
1777 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1778 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1780 if (scr->flags.jump_back_pending ||
1781 p.x <= (rect.pos.x + 1) ||
1782 p.x >= (rect.pos.x + rect.size.width - 2) ||
1783 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1784 WMenu *menu;
1786 menu = wMenuUnderPointer(scr);
1787 if (menu != NULL)
1788 wMenuScroll(menu, event);
1793 static void handleVisibilityNotify(XEvent * event)
1795 WWindow *wwin;
1797 wwin = wWindowFor(event->xvisibility.window);
1798 if (!wwin)
1799 return;
1800 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);