da75cf306c7028af8acf9547ccfbbaa68124ea6d
[wmaker-crm.git] / src / event.c
blobda75cf306c7028af8acf9547ccfbbaa68124ea6d
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 USE_XSHAPE
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"
80 #define MOD_MASK wPreferences.modifier_mask
82 /************ Local stuff ***********/
84 static void saveTimestamp(XEvent *event);
85 static void handleColormapNotify(XEvent *event);
86 static void handleMapNotify(XEvent *event);
87 static void handleUnmapNotify(XEvent *event);
88 static void handleButtonPress(XEvent *event);
89 static void handleExpose(XEvent *event);
90 static void handleDestroyNotify(XEvent *event);
91 static void handleConfigureRequest(XEvent *event);
92 static void handleMapRequest(XEvent *event);
93 static void handlePropertyNotify(XEvent *event);
94 static void handleEnterNotify(XEvent *event);
95 static void handleLeaveNotify(XEvent *event);
96 static void handleExtensions(XEvent *event);
97 static void handleClientMessage(XEvent *event);
98 static void handleKeyPress(XEvent *event);
99 static void handleFocusIn(XEvent *event);
100 static void handleMotionNotify(XEvent *event);
101 static void handleVisibilityNotify(XEvent *event);
102 static void handle_inotify_events(void);
103 static void wdelete_death_handler(WMagicNumber id);
106 #ifdef USE_XSHAPE
107 static void handleShapeNotify(XEvent *event);
108 #endif
110 #ifdef KEEP_XKB_LOCK_STATUS
111 static void handleXkbIndicatorStateNotify(XEvent *event);
112 #endif
114 /* real dead process handler */
115 static void handleDeadProcess(void);
117 typedef struct DeadProcesses {
118 pid_t pid;
119 unsigned char exit_status;
120 } DeadProcesses;
122 /* stack of dead processes */
123 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
124 static int deadProcessPtr = 0;
126 typedef struct DeathHandler {
127 WDeathHandler *callback;
128 pid_t pid;
129 void *client_data;
130 } DeathHandler;
132 static WMArray *deathHandlers = NULL;
134 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
136 DeathHandler *handler;
138 handler = malloc(sizeof(DeathHandler));
139 if (!handler)
140 return 0;
142 handler->pid = pid;
143 handler->callback = callback;
144 handler->client_data = cdata;
146 if (!deathHandlers)
147 deathHandlers = WMCreateArrayWithDestructor(8, free);
149 WMAddToArray(deathHandlers, handler);
151 return handler;
154 static void wdelete_death_handler(WMagicNumber id)
156 DeathHandler *handler = (DeathHandler *) id;
158 if (!handler || !deathHandlers)
159 return;
161 /* array destructor will call free(handler) */
162 WMRemoveFromArray(deathHandlers, handler);
165 void DispatchEvent(XEvent * event)
167 if (deathHandlers)
168 handleDeadProcess();
170 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
171 WCHANGE_STATE(WSTATE_EXITING);
172 /* received SIGTERM */
174 * WMHandleEvent() can't be called from anything
175 * executed inside here, or we can get in a infinite
176 * recursive loop.
178 Shutdown(WSExitMode);
180 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
181 WCHANGE_STATE(WSTATE_RESTARTING);
183 Shutdown(WSRestartPreparationMode);
184 /* received SIGHUP */
185 Restart(NULL, True);
186 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
187 WCHANGE_STATE(WSTATE_NORMAL);
188 wDefaultsCheckDomains(NULL);
191 /* for the case that all that is wanted to be dispatched is
192 * the stuff above */
193 if (!event)
194 return;
196 saveTimestamp(event);
197 switch (event->type) {
198 case MapRequest:
199 handleMapRequest(event);
200 break;
202 case KeyPress:
203 handleKeyPress(event);
204 break;
206 case MotionNotify:
207 handleMotionNotify(event);
208 break;
210 case ConfigureRequest:
211 handleConfigureRequest(event);
212 break;
214 case DestroyNotify:
215 handleDestroyNotify(event);
216 break;
218 case MapNotify:
219 handleMapNotify(event);
220 break;
222 case UnmapNotify:
223 handleUnmapNotify(event);
224 break;
226 case ButtonPress:
227 handleButtonPress(event);
228 break;
230 case Expose:
231 handleExpose(event);
232 break;
234 case PropertyNotify:
235 handlePropertyNotify(event);
236 break;
238 case EnterNotify:
239 handleEnterNotify(event);
240 break;
242 case LeaveNotify:
243 handleLeaveNotify(event);
244 break;
246 case ClientMessage:
247 handleClientMessage(event);
248 break;
250 case ColormapNotify:
251 handleColormapNotify(event);
252 break;
254 case MappingNotify:
255 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
256 XRefreshKeyboardMapping(&event->xmapping);
257 break;
259 case FocusIn:
260 handleFocusIn(event);
261 break;
263 case VisibilityNotify:
264 handleVisibilityNotify(event);
265 break;
267 case ConfigureNotify:
268 #ifdef HAVE_XRANDR
269 if (event->xconfigure.window == DefaultRootWindow(dpy))
270 XRRUpdateConfiguration(event);
271 #endif
272 break;
274 default:
275 handleExtensions(event);
276 break;
280 #ifdef HAVE_INOTIFY
282 *----------------------------------------------------------------------
283 * handle_inotify_events-
284 * Check for inotify events
286 * Returns:
287 * After reading events for the given file descriptor (fd) and
288 * watch descriptor (wd)
290 * Side effects:
291 * Calls wDefaultsCheckDomains if config database is updated
292 *----------------------------------------------------------------------
294 static void handle_inotify_events(void)
296 ssize_t eventQLength, i = 0;
297 /* Make room for at lease 5 simultaneous events, with path + filenames */
298 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
299 /* Check config only once per read of the event queue */
300 int oneShotFlag = 0;
303 * Read off the queued events
304 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
305 * not occur; the block is on Xevents, but a config file change will normally
306 * occur as a result of an Xevent - so the event queue should never have more than
307 * a few entries before a read().
309 eventQLength = read(w_global.inotify.fd_event_queue,
310 buff, sizeof(buff) );
312 /* check what events occured */
313 /* Should really check wd here too, but for now we only have one watch! */
314 while (i < eventQLength) {
315 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
318 * see inotify.h for event types.
320 if (pevent->mask & IN_DELETE_SELF) {
321 wwarning(_("the defaults database has been deleted!"
322 " Restart Window Maker to create the database" " with the default settings"));
324 if (w_global.inotify.fd_event_queue >= 0) {
325 close(w_global.inotify.fd_event_queue);
326 w_global.inotify.fd_event_queue = -1;
329 if (pevent->mask & IN_UNMOUNT) {
330 wwarning(_("the unit containing the defaults database has"
331 " been unmounted. Setting --static mode." " Any changes will not be saved."));
333 if (w_global.inotify.fd_event_queue >= 0) {
334 close(w_global.inotify.fd_event_queue);
335 w_global.inotify.fd_event_queue = -1;
338 wPreferences.flags.noupdates = 1;
340 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
341 wwarning(_("Inotify: Reading config files in defaults database."));
342 wDefaultsCheckDomains(NULL);
345 /* move to next event in the buffer */
346 i += sizeof(struct inotify_event) + pevent->len;
349 #endif /* HAVE_INOTIFY */
352 *----------------------------------------------------------------------
353 * EventLoop-
354 * Processes X and internal events indefinitely.
356 * Returns:
357 * Never returns
359 * Side effects:
360 * The LastTimestamp global variable is updated.
361 * Calls inotifyGetEvents if defaults database changes.
362 *----------------------------------------------------------------------
364 noreturn void EventLoop(void)
366 XEvent event;
367 #ifdef HAVE_INOTIFY
368 struct timeval time;
369 fd_set rfds;
370 int retVal = 0;
372 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
373 retVal = -1;
374 #endif
376 for (;;) {
378 WMNextEvent(dpy, &event); /* Blocks here */
379 WMHandleEvent(&event);
380 #ifdef HAVE_INOTIFY
381 if (retVal != -1) {
382 time.tv_sec = 0;
383 time.tv_usec = 0;
384 FD_ZERO(&rfds);
385 FD_SET(w_global.inotify.fd_event_queue, &rfds);
387 /* check for available read data from inotify - don't block! */
388 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
390 if (retVal < 0) { /* an error has occured */
391 wwarning(_("select failed. The inotify instance will be closed."
392 " Changes to the defaults database will require"
393 " a restart to take effect."));
394 close(w_global.inotify.fd_event_queue);
395 w_global.inotify.fd_event_queue = -1;
396 continue;
398 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
399 handle_inotify_events();
401 #endif
406 *----------------------------------------------------------------------
407 * ProcessPendingEvents --
408 * Processes the events that are currently pending (at the time
409 * this function is called) in the display's queue.
411 * Returns:
412 * After the pending events that were present at the function call
413 * are processed.
415 * Side effects:
416 * Many -- whatever handling events may involve.
418 *----------------------------------------------------------------------
420 void ProcessPendingEvents(void)
422 XEvent event;
423 int count;
425 XSync(dpy, False);
427 /* Take a snapshot of the event count in the queue */
428 count = XPending(dpy);
430 while (count > 0 && XPending(dpy)) {
431 WMNextEvent(dpy, &event);
432 WMHandleEvent(&event);
433 count--;
437 Bool IsDoubleClick(WScreen * scr, XEvent * event)
439 if ((scr->last_click_time > 0) &&
440 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
441 && (event->xbutton.button == scr->last_click_button)
442 && (event->xbutton.window == scr->last_click_window)) {
444 scr->flags.next_click_is_not_double = 1;
445 scr->last_click_time = 0;
446 scr->last_click_window = event->xbutton.window;
448 return True;
450 return False;
453 void NotifyDeadProcess(pid_t pid, unsigned char status)
455 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
456 wwarning("stack overflow: too many dead processes");
457 return;
459 /* stack the process to be handled later,
460 * as this is called from the signal handler */
461 deadProcesses[deadProcessPtr].pid = pid;
462 deadProcesses[deadProcessPtr].exit_status = status;
463 deadProcessPtr++;
466 static void handleDeadProcess(void)
468 DeathHandler *tmp;
469 int i;
471 for (i = 0; i < deadProcessPtr; i++) {
472 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
475 if (!deathHandlers) {
476 deadProcessPtr = 0;
477 return;
480 /* get the pids on the queue and call handlers */
481 while (deadProcessPtr > 0) {
482 deadProcessPtr--;
484 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
485 tmp = WMGetFromArray(deathHandlers, i);
486 if (!tmp)
487 continue;
489 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
490 (*tmp->callback) (tmp->pid,
491 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
492 wdelete_death_handler(tmp);
498 static void saveTimestamp(XEvent * event)
501 * Never save CurrentTime as LastTimestamp because CurrentTime
502 * it's not a real timestamp (it's the 0L constant)
505 switch (event->type) {
506 case ButtonRelease:
507 case ButtonPress:
508 w_global.timestamp.last_event = event->xbutton.time;
509 break;
510 case KeyPress:
511 case KeyRelease:
512 w_global.timestamp.last_event = event->xkey.time;
513 break;
514 case MotionNotify:
515 w_global.timestamp.last_event = event->xmotion.time;
516 break;
517 case PropertyNotify:
518 w_global.timestamp.last_event = event->xproperty.time;
519 break;
520 case EnterNotify:
521 case LeaveNotify:
522 w_global.timestamp.last_event = event->xcrossing.time;
523 break;
524 case SelectionClear:
525 w_global.timestamp.last_event = event->xselectionclear.time;
526 break;
527 case SelectionRequest:
528 w_global.timestamp.last_event = event->xselectionrequest.time;
529 break;
530 case SelectionNotify:
531 w_global.timestamp.last_event = event->xselection.time;
532 #ifdef XDND
533 wXDNDProcessSelection(event);
534 #endif
535 break;
539 static int matchWindow(const void *item, const void *cdata)
541 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
544 static void handleExtensions(XEvent * event)
546 #ifdef KEEP_XKB_LOCK_STATUS
547 XkbEvent *xkbevent;
548 xkbevent = (XkbEvent *) event;
549 #endif /*KEEP_XKB_LOCK_STATUS */
550 #ifdef USE_XSHAPE
551 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
552 handleShapeNotify(event);
554 #endif
555 #ifdef KEEP_XKB_LOCK_STATUS
556 if (wPreferences.modelock && (xkbevent->type == w_global.xext.xkb.event_base)) {
557 handleXkbIndicatorStateNotify(event);
559 #endif /*KEEP_XKB_LOCK_STATUS */
560 #ifdef HAVE_XRANDR
561 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
562 /* From xrandr man page: "Clients must call back into Xlib using
563 * XRRUpdateConfiguration when screen configuration change notify
564 * events are generated */
565 XRRUpdateConfiguration(event);
566 WCHANGE_STATE(WSTATE_RESTARTING);
567 Shutdown(WSRestartPreparationMode);
568 Restart(NULL,True);
570 #endif
573 static void handleMapRequest(XEvent * ev)
575 WWindow *wwin;
576 WScreen *scr = NULL;
577 Window window = ev->xmaprequest.window;
579 if ((wwin = wWindowFor(window))) {
580 if (wwin->flags.shaded) {
581 wUnshadeWindow(wwin);
583 /* deiconify window */
584 if (wwin->flags.miniaturized) {
585 wDeiconifyWindow(wwin);
586 } else if (wwin->flags.hidden) {
587 WApplication *wapp = wApplicationOf(wwin->main_window);
588 /* go to the last workspace that the user worked on the app */
589 if (wapp) {
590 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
592 wUnhideApplication(wapp, False, False);
594 return;
597 scr = wScreenForRootWindow(ev->xmaprequest.parent);
599 wwin = wManageWindow(scr, window);
602 * This is to let the Dock know that the application it launched
603 * has already been mapped (eg: it has finished launching).
604 * It is not necessary for normally docked apps, but is needed for
605 * apps that were forcedly docked (like with dockit).
607 if (scr->last_dock) {
608 if (wwin && wwin->main_window != None && wwin->main_window != window)
609 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
610 else
611 wDockTrackWindowLaunch(scr->last_dock, window);
614 if (wwin) {
615 wClientSetState(wwin, NormalState, None);
616 if (wwin->flags.maximized) {
617 wMaximizeWindow(wwin, wwin->flags.maximized);
619 if (wwin->flags.shaded) {
620 wwin->flags.shaded = 0;
621 wwin->flags.skip_next_animation = 1;
622 wShadeWindow(wwin);
624 if (wwin->flags.miniaturized) {
625 wwin->flags.miniaturized = 0;
626 wwin->flags.skip_next_animation = 1;
627 wIconifyWindow(wwin);
629 if (wwin->flags.fullscreen) {
630 wwin->flags.fullscreen = 0;
631 wFullscreenWindow(wwin);
633 if (wwin->flags.hidden) {
634 WApplication *wapp = wApplicationOf(wwin->main_window);
636 wwin->flags.hidden = 0;
637 wwin->flags.skip_next_animation = 1;
638 if (wapp) {
639 wHideApplication(wapp);
645 static void handleDestroyNotify(XEvent * event)
647 WWindow *wwin;
648 WApplication *app;
649 Window window = event->xdestroywindow.window;
650 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
651 int widx;
653 wwin = wWindowFor(window);
654 if (wwin) {
655 wUnmanageWindow(wwin, False, True);
658 if (scr != NULL) {
659 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
660 WFakeGroupLeader *fPtr;
662 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
663 if (fPtr->retainCount > 0) {
664 fPtr->retainCount--;
665 if (fPtr->retainCount == 0 && fPtr->leader != None) {
666 XDestroyWindow(dpy, fPtr->leader);
667 fPtr->leader = None;
668 XFlush(dpy);
671 fPtr->origLeader = None;
675 app = wApplicationOf(window);
676 if (app) {
677 if (window == app->main_window) {
678 app->refcount = 0;
679 wwin = app->main_window_desc->screen_ptr->focused_window;
680 while (wwin) {
681 if (wwin->main_window == window) {
682 wwin->main_window = None;
684 wwin = wwin->prev;
687 wApplicationDestroy(app);
691 static void handleExpose(XEvent * event)
693 WObjDescriptor *desc;
694 XEvent ev;
696 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
698 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
699 return;
702 if (desc->handle_expose) {
703 (*desc->handle_expose) (desc, event);
707 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
709 switch (action) {
710 case WA_SELECT_WINDOWS:
711 wUnselectWindows(scr);
712 wSelectWindows(scr, event);
713 break;
714 case WA_OPEN_APPMENU:
715 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
716 /* ugly hack */
717 if (scr->root_menu) {
718 if (scr->root_menu->brother->flags.mapped)
719 event->xbutton.window = scr->root_menu->brother->frame->core->window;
720 else
721 event->xbutton.window = scr->root_menu->frame->core->window;
723 break;
724 case WA_OPEN_WINLISTMENU:
725 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
726 if (scr->switch_menu) {
727 if (scr->switch_menu->brother->flags.mapped)
728 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
729 else
730 event->xbutton.window = scr->switch_menu->frame->core->window;
732 break;
733 default:
734 break;
738 /* bindable */
739 static void handleButtonPress(XEvent * event)
741 WObjDescriptor *desc;
742 WScreen *scr;
744 scr = wScreenForRootWindow(event->xbutton.root);
746 #ifdef BALLOON_TEXT
747 wBalloonHide(scr);
748 #endif
750 if (event->xbutton.window == scr->root_win) {
751 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
752 executeButtonAction(scr, event, wPreferences.mouse_button1);
753 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
754 executeButtonAction(scr, event, wPreferences.mouse_button2);
755 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
756 executeButtonAction(scr, event, wPreferences.mouse_button3);
757 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
758 wWorkspaceRelativeChange(scr, 1);
759 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
760 wWorkspaceRelativeChange(scr, -1);
764 desc = NULL;
765 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
766 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
767 return;
771 if (desc->parent_type == WCLASS_WINDOW) {
772 XSync(dpy, 0);
774 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
775 XAllowEvents(dpy, AsyncPointer, CurrentTime);
776 } else {
777 /* if (wPreferences.focus_mode == WKF_CLICK) { */
778 if (wPreferences.ignore_focus_click) {
779 XAllowEvents(dpy, AsyncPointer, CurrentTime);
781 XAllowEvents(dpy, ReplayPointer, CurrentTime);
782 /* } */
784 XSync(dpy, 0);
785 } else if (desc->parent_type == WCLASS_APPICON
786 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
787 if (event->xbutton.state & MOD_MASK) {
788 XSync(dpy, 0);
789 XAllowEvents(dpy, AsyncPointer, CurrentTime);
790 XSync(dpy, 0);
794 if (desc->handle_mousedown != NULL) {
795 (*desc->handle_mousedown) (desc, event);
798 /* save double-click information */
799 if (scr->flags.next_click_is_not_double) {
800 scr->flags.next_click_is_not_double = 0;
801 } else {
802 scr->last_click_time = event->xbutton.time;
803 scr->last_click_button = event->xbutton.button;
804 scr->last_click_window = event->xbutton.window;
808 static void handleMapNotify(XEvent * event)
810 WWindow *wwin;
812 wwin = wWindowFor(event->xmap.event);
813 if (wwin && wwin->client_win == event->xmap.event) {
814 if (wwin->flags.miniaturized) {
815 wDeiconifyWindow(wwin);
816 } else {
817 XGrabServer(dpy);
818 wWindowMap(wwin);
819 wClientSetState(wwin, NormalState, None);
820 XUngrabServer(dpy);
825 static void handleUnmapNotify(XEvent * event)
827 WWindow *wwin;
828 XEvent ev;
829 Bool withdraw = False;
831 /* only process windows with StructureNotify selected
832 * (ignore SubstructureNotify) */
833 wwin = wWindowFor(event->xunmap.window);
834 if (!wwin)
835 return;
837 /* whether the event is a Withdrawal request */
838 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
839 withdraw = True;
841 if (wwin->client_win != event->xunmap.event && !withdraw)
842 return;
844 if (!wwin->flags.mapped && !withdraw
845 && wwin->frame->workspace == w_global.workspace.current
846 && !wwin->flags.miniaturized && !wwin->flags.hidden)
847 return;
849 XGrabServer(dpy);
850 XUnmapWindow(dpy, wwin->frame->core->window);
851 wwin->flags.mapped = 0;
852 XSync(dpy, 0);
853 /* check if the window was destroyed */
854 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
855 DispatchEvent(&ev);
856 } else {
857 Bool reparented = False;
859 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
860 reparented = True;
862 /* withdraw window */
863 wwin->flags.mapped = 0;
864 if (!reparented)
865 wClientSetState(wwin, WithdrawnState, None);
867 /* if the window was reparented, do not reparent it back to the
868 * root window */
869 wUnmanageWindow(wwin, !reparented, False);
871 XUngrabServer(dpy);
874 static void handleConfigureRequest(XEvent * event)
876 WWindow *wwin;
878 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
880 * Configure request for unmapped window
882 wClientConfigure(NULL, &(event->xconfigurerequest));
883 } else {
884 wClientConfigure(wwin, &(event->xconfigurerequest));
888 static void handlePropertyNotify(XEvent * event)
890 WWindow *wwin;
891 WApplication *wapp;
892 Window jr;
893 int ji;
894 unsigned int ju;
896 wwin = wWindowFor(event->xproperty.window);
897 if (wwin) {
898 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
899 return;
901 wClientCheckProperty(wwin, &event->xproperty);
903 wapp = wApplicationOf(event->xproperty.window);
904 if (wapp) {
905 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
909 static void handleClientMessage(XEvent * event)
911 WWindow *wwin;
912 WObjDescriptor *desc;
914 /* handle transition from Normal to Iconic state */
915 if (event->xclient.message_type == w_global.atom.wm.change_state
916 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
918 wwin = wWindowFor(event->xclient.window);
919 if (!wwin)
920 return;
921 if (!wwin->flags.miniaturized)
922 wIconifyWindow(wwin);
923 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
924 WScreen *scr = wScreenForRootWindow(event->xclient.window);
926 if (!scr)
927 return;
929 if (event->xclient.data.l[1] == 1) { /* starting */
930 wColormapAllowClientInstallation(scr, True);
931 } else { /* stopping */
932 wColormapAllowClientInstallation(scr, False);
934 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
936 char *command;
937 size_t len;
939 len = sizeof(event->xclient.data.b) + 1;
940 command = wmalloc(len);
941 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
943 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
944 wwarning(_("Got Reconfigure command"));
945 wDefaultsCheckDomains(NULL);
946 } else {
947 wwarning(_("Got unknown command %s"), command);
950 wfree(command);
952 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
953 WApplication *wapp;
954 int done = 0;
955 wapp = wApplicationOf(event->xclient.window);
956 if (wapp) {
957 switch (event->xclient.data.l[0]) {
958 case WMFHideOtherApplications:
959 wHideOtherApplications(wapp->main_window_desc);
960 done = 1;
961 break;
963 case WMFHideApplication:
964 wHideApplication(wapp);
965 done = 1;
966 break;
969 if (!done) {
970 wwin = wWindowFor(event->xclient.window);
971 if (wwin) {
972 switch (event->xclient.data.l[0]) {
973 case WMFHideOtherApplications:
974 wHideOtherApplications(wwin);
975 break;
977 case WMFHideApplication:
978 wHideApplication(wApplicationOf(wwin->main_window));
979 break;
983 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
984 wwin = wWindowFor(event->xclient.window);
985 if (!wwin)
986 return;
987 switch (event->xclient.data.l[0]) {
988 case GSWindowLevelAttr:
990 int level = (int)event->xclient.data.l[1];
992 if (WINDOW_LEVEL(wwin) != level) {
993 ChangeStackingLevel(wwin->frame->core, level);
996 break;
998 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
999 wwin = wWindowFor(event->xclient.window);
1000 if (!wwin)
1001 return;
1002 switch (event->xclient.data.l[0]) {
1003 case WMTitleBarNormal:
1004 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1005 break;
1006 case WMTitleBarMain:
1007 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1008 break;
1009 case WMTitleBarKey:
1010 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1011 break;
1013 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1014 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1015 if (!scr)
1016 return;
1017 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1018 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1019 /* do nothing */
1020 #ifdef XDND
1021 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1022 /* do nothing */
1023 #endif /* XDND */
1024 } else {
1026 * Non-standard thing, but needed by OffiX DND.
1027 * For when the icon frame gets a ClientMessage
1028 * that should have gone to the icon_window.
1030 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1031 struct WIcon *icon = NULL;
1033 if (desc->parent_type == WCLASS_MINIWINDOW) {
1034 icon = (WIcon *) desc->parent;
1035 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1036 icon = ((WAppIcon *) desc->parent)->icon;
1038 if (icon && (wwin = icon->owner)) {
1039 if (wwin->client_win != event->xclient.window) {
1040 event->xclient.window = wwin->client_win;
1041 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1048 static void raiseWindow(WScreen * scr)
1050 WWindow *wwin;
1052 scr->autoRaiseTimer = NULL;
1054 wwin = wWindowFor(scr->autoRaiseWindow);
1055 if (!wwin)
1056 return;
1058 if (!wwin->flags.destroyed && wwin->flags.focused) {
1059 wRaiseFrame(wwin->frame->core);
1060 /* this is needed or a race condition will occur */
1061 XSync(dpy, False);
1065 static void handleEnterNotify(XEvent * event)
1067 WWindow *wwin;
1068 WObjDescriptor *desc = NULL;
1069 XEvent ev;
1070 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1072 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1073 /* already left the window... */
1074 saveTimestamp(&ev);
1075 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1076 return;
1080 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1081 if (desc->handle_enternotify)
1082 (*desc->handle_enternotify) (desc, event);
1085 /* enter to window */
1086 wwin = wWindowFor(event->xcrossing.window);
1087 if (!wwin) {
1088 if (wPreferences.colormap_mode == WCM_POINTER) {
1089 wColormapInstallForWindow(scr, NULL);
1091 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1092 WMDeleteTimerHandler(scr->autoRaiseTimer);
1093 scr->autoRaiseTimer = NULL;
1095 } else {
1096 /* set auto raise timer even if in focus-follows-mouse mode
1097 * and the event is for the frame window, even if the window
1098 * has focus already. useful if you move the pointer from a focused
1099 * window to the root window and back pretty fast
1101 * set focus if in focus-follows-mouse mode and the event
1102 * is for the frame window and window doesn't have focus yet */
1103 if (wPreferences.focus_mode == WKF_SLOPPY
1104 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1106 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1107 wSetFocusTo(scr, wwin);
1109 if (scr->autoRaiseTimer)
1110 WMDeleteTimerHandler(scr->autoRaiseTimer);
1111 scr->autoRaiseTimer = NULL;
1113 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1114 scr->autoRaiseWindow = wwin->frame->core->window;
1115 scr->autoRaiseTimer
1116 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1119 /* Install colormap for window, if the colormap installation mode
1120 * is colormap_follows_mouse */
1121 if (wPreferences.colormap_mode == WCM_POINTER) {
1122 if (wwin->client_win == event->xcrossing.window)
1123 wColormapInstallForWindow(scr, wwin);
1124 else
1125 wColormapInstallForWindow(scr, NULL);
1129 if (event->xcrossing.window == event->xcrossing.root
1130 && event->xcrossing.detail == NotifyNormal
1131 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1133 wSetFocusTo(scr, scr->focused_window);
1135 #ifdef BALLOON_TEXT
1136 wBalloonEnteredObject(scr, desc);
1137 #endif
1140 static void handleLeaveNotify(XEvent * event)
1142 WObjDescriptor *desc = NULL;
1144 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1145 if (desc->handle_leavenotify)
1146 (*desc->handle_leavenotify) (desc, event);
1150 #ifdef USE_XSHAPE
1151 static void handleShapeNotify(XEvent * event)
1153 XShapeEvent *shev = (XShapeEvent *) event;
1154 WWindow *wwin;
1155 union {
1156 XEvent xevent;
1157 XShapeEvent xshape;
1158 } ev;
1160 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1161 if (ev.xshape.kind == ShapeBounding) {
1162 if (ev.xshape.shaped == shev->shaped) {
1163 *shev = ev.xshape;
1164 } else {
1165 XPutBackEvent(dpy, &ev.xevent);
1166 break;
1171 wwin = wWindowFor(shev->window);
1172 if (!wwin || shev->kind != ShapeBounding)
1173 return;
1175 if (!shev->shaped && wwin->flags.shaped) {
1177 wwin->flags.shaped = 0;
1178 wWindowClearShape(wwin);
1180 } else if (shev->shaped) {
1182 wwin->flags.shaped = 1;
1183 wWindowSetShape(wwin);
1186 #endif /* USE_XSHAPE */
1188 #ifdef KEEP_XKB_LOCK_STATUS
1189 /* please help ]d if you know what to do */
1190 static void handleXkbIndicatorStateNotify(XEvent *event)
1192 WWindow *wwin;
1193 WScreen *scr;
1194 XkbStateRec staterec;
1195 int i;
1197 for (i = 0; i < w_global.screen_count; i++) {
1198 scr = wScreenWithNumber(i);
1199 wwin = scr->focused_window;
1200 if (wwin && wwin->flags.focused) {
1201 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1202 if (wwin->frame->languagemode != staterec.group) {
1203 wwin->frame->last_languagemode = wwin->frame->languagemode;
1204 wwin->frame->languagemode = staterec.group;
1206 #ifdef XKB_BUTTON_HINT
1207 if (wwin->frame->titlebar) {
1208 wFrameWindowPaint(wwin->frame);
1210 #endif
1214 #endif /*KEEP_XKB_LOCK_STATUS */
1216 static void handleColormapNotify(XEvent * event)
1218 WWindow *wwin;
1219 WScreen *scr;
1220 Bool reinstall = False;
1222 wwin = wWindowFor(event->xcolormap.window);
1223 if (!wwin)
1224 return;
1226 scr = wwin->screen_ptr;
1228 do {
1229 if (wwin) {
1230 if (event->xcolormap.new) {
1231 XWindowAttributes attr;
1233 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1235 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1236 scr->current_colormap = attr.colormap;
1238 reinstall = True;
1239 } else if (event->xcolormap.state == ColormapUninstalled &&
1240 scr->current_colormap == event->xcolormap.colormap) {
1242 /* some bastard app (like XV) removed our colormap */
1244 * can't enforce or things like xscreensaver wont work
1245 * reinstall = True;
1247 } else if (event->xcolormap.state == ColormapInstalled &&
1248 scr->current_colormap == event->xcolormap.colormap) {
1250 /* someone has put our colormap back */
1251 reinstall = False;
1254 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1255 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1257 if (reinstall && scr->current_colormap != None) {
1258 if (!scr->flags.colormap_stuff_blocked)
1259 XInstallColormap(dpy, scr->current_colormap);
1263 static void handleFocusIn(XEvent * event)
1265 WWindow *wwin;
1268 * For applications that like stealing the focus.
1270 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1271 saveTimestamp(event);
1272 if (event->xfocus.mode == NotifyUngrab
1273 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1274 return;
1277 wwin = wWindowFor(event->xfocus.window);
1278 if (wwin && !wwin->flags.focused) {
1279 if (wwin->flags.mapped)
1280 wSetFocusTo(wwin->screen_ptr, wwin);
1281 else
1282 wSetFocusTo(wwin->screen_ptr, NULL);
1283 } else if (!wwin) {
1284 WScreen *scr = wScreenForWindow(event->xfocus.window);
1285 if (scr)
1286 wSetFocusTo(scr, NULL);
1290 static WWindow *windowUnderPointer(WScreen * scr)
1292 unsigned int mask;
1293 int foo;
1294 Window bar, win;
1296 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1297 return wWindowFor(win);
1298 return NULL;
1301 static int CheckFullScreenWindowFocused(WScreen * scr)
1303 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1304 return 1;
1305 else
1306 return 0;
1309 static void handleKeyPress(XEvent * event)
1311 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1312 WWindow *wwin = scr->focused_window;
1313 short i, widx;
1314 int modifiers;
1315 int command = -1;
1316 #ifdef KEEP_XKB_LOCK_STATUS
1317 XkbStateRec staterec;
1318 #endif /*KEEP_XKB_LOCK_STATUS */
1320 /* ignore CapsLock */
1321 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1323 for (i = 0; i < WKBD_LAST; i++) {
1324 if (wKeyBindings[i].keycode == 0)
1325 continue;
1327 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1328 || */ wKeyBindings[i].modifier ==
1329 modifiers)) {
1330 command = i;
1331 break;
1335 if (command < 0) {
1337 if (!wRootMenuPerformShortcut(event)) {
1338 static int dontLoop = 0;
1340 if (dontLoop > 10) {
1341 wwarning("problem with key event processing code");
1342 return;
1344 dontLoop++;
1345 /* if the focused window is an internal window, try redispatching
1346 * the event to the managed window, as it can be a WINGs window */
1347 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1348 /* client_leader contains the WINGs toplevel */
1349 event->xany.window = wwin->client_leader;
1350 WMHandleEvent(event);
1352 dontLoop--;
1354 return;
1356 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1357 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1359 switch (command) {
1361 case WKBD_ROOTMENU:
1362 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1363 if (!CheckFullScreenWindowFocused(scr)) {
1364 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1365 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1366 True);
1368 break;
1369 case WKBD_WINDOWLIST:
1370 if (!CheckFullScreenWindowFocused(scr)) {
1371 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1372 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1373 True);
1375 break;
1377 case WKBD_WINDOWMENU:
1378 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1379 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1380 break;
1381 case WKBD_MINIMIZEALL:
1382 CloseWindowMenu(scr);
1383 wHideAll(scr);
1384 break;
1385 case WKBD_MINIATURIZE:
1386 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1387 && !WFLAGP(wwin, no_miniaturizable)) {
1388 CloseWindowMenu(scr);
1390 if (wwin->protocols.MINIATURIZE_WINDOW)
1391 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1392 else {
1393 wIconifyWindow(wwin);
1396 break;
1397 case WKBD_HIDE:
1398 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1399 WApplication *wapp = wApplicationOf(wwin->main_window);
1400 CloseWindowMenu(scr);
1402 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1403 wHideApplication(wapp);
1406 break;
1407 case WKBD_HIDE_OTHERS:
1408 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1409 CloseWindowMenu(scr);
1411 wHideOtherApplications(wwin);
1413 break;
1414 case WKBD_MAXIMIZE:
1415 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1416 CloseWindowMenu(scr);
1418 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1420 break;
1421 case WKBD_VMAXIMIZE:
1422 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1423 CloseWindowMenu(scr);
1425 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1427 break;
1428 case WKBD_HMAXIMIZE:
1429 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1430 CloseWindowMenu(scr);
1432 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1434 break;
1435 case WKBD_LHMAXIMIZE:
1436 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1437 CloseWindowMenu(scr);
1439 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1441 break;
1442 case WKBD_RHMAXIMIZE:
1443 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1444 CloseWindowMenu(scr);
1446 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1448 break;
1449 case WKBD_THMAXIMIZE:
1450 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1451 CloseWindowMenu(scr);
1453 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1455 break;
1456 case WKBD_BHMAXIMIZE:
1457 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1458 CloseWindowMenu(scr);
1460 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1462 break;
1463 case WKBD_LTCMAXIMIZE:
1464 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1465 CloseWindowMenu(scr);
1467 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1469 break;
1470 case WKBD_RTCMAXIMIZE:
1471 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1472 CloseWindowMenu(scr);
1474 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1476 break;
1477 case WKBD_LBCMAXIMIZE:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1479 CloseWindowMenu(scr);
1481 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1483 break;
1484 case WKBD_RBCMAXIMIZE:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1490 break;
1491 case WKBD_MAXIMUS:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 CloseWindowMenu(scr);
1495 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1497 break;
1498 case WKBD_RAISE:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1500 CloseWindowMenu(scr);
1502 wRaiseFrame(wwin->frame->core);
1504 break;
1505 case WKBD_LOWER:
1506 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1507 CloseWindowMenu(scr);
1509 wLowerFrame(wwin->frame->core);
1511 break;
1512 case WKBD_RAISELOWER:
1513 /* raise or lower the window under the pointer, not the
1514 * focused one
1516 wwin = windowUnderPointer(scr);
1517 if (wwin)
1518 wRaiseLowerFrame(wwin->frame->core);
1519 break;
1520 case WKBD_SHADE:
1521 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1522 if (wwin->flags.shaded)
1523 wUnshadeWindow(wwin);
1524 else
1525 wShadeWindow(wwin);
1527 break;
1528 case WKBD_MOVERESIZE:
1529 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1530 CloseWindowMenu(scr);
1532 wKeyboardMoveResizeWindow(wwin);
1534 break;
1535 case WKBD_CLOSE:
1536 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1537 CloseWindowMenu(scr);
1538 if (wwin->protocols.DELETE_WINDOW)
1539 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1541 break;
1542 case WKBD_SELECT:
1543 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1544 wSelectWindow(wwin, !wwin->flags.selected);
1546 break;
1547 case WKBD_FOCUSNEXT:
1548 StartWindozeCycle(wwin, event, True, False);
1549 break;
1551 case WKBD_FOCUSPREV:
1552 StartWindozeCycle(wwin, event, False, False);
1553 break;
1555 case WKBD_GROUPNEXT:
1556 StartWindozeCycle(wwin, event, True, True);
1557 break;
1559 case WKBD_GROUPPREV:
1560 StartWindozeCycle(wwin, event, False, True);
1561 break;
1563 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1564 widx = command - WKBD_WORKSPACE1;
1565 i = (w_global.workspace.current / 10) * 10 + widx;
1566 if (wPreferences.ws_advance || i < w_global.workspace.count)
1567 wWorkspaceChange(scr, i);
1568 break;
1570 case WKBD_NEXTWORKSPACE:
1571 wWorkspaceRelativeChange(scr, 1);
1572 break;
1573 case WKBD_PREVWORKSPACE:
1574 wWorkspaceRelativeChange(scr, -1);
1575 break;
1576 case WKBD_LASTWORKSPACE:
1577 wWorkspaceChange(scr, w_global.workspace.last_used);
1578 break;
1580 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1581 widx = command - WKBD_MOVE_WORKSPACE1;
1582 i = (w_global.workspace.current / 10) * 10 + widx;
1583 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1584 wWindowChangeWorkspace(wwin, i);
1585 break;
1587 case WKBD_MOVE_NEXTWORKSPACE:
1588 if (wwin)
1589 wWindowChangeWorkspaceRelative(wwin, 1);
1590 break;
1591 case WKBD_MOVE_PREVWORKSPACE:
1592 if (wwin)
1593 wWindowChangeWorkspaceRelative(wwin, -1);
1594 break;
1595 case WKBD_MOVE_LASTWORKSPACE:
1596 if (wwin)
1597 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1598 break;
1600 case WKBD_MOVE_NEXTWSLAYER:
1601 case WKBD_MOVE_PREVWSLAYER:
1603 if (wwin) {
1604 int row, column;
1606 row = w_global.workspace.current / 10;
1607 column = w_global.workspace.current % 10;
1609 if (command == WKBD_MOVE_NEXTWSLAYER) {
1610 if ((row + 1) * 10 < w_global.workspace.count)
1611 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1612 } else {
1613 if (row > 0)
1614 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1618 break;
1620 case WKBD_WINDOW1:
1621 case WKBD_WINDOW2:
1622 case WKBD_WINDOW3:
1623 case WKBD_WINDOW4:
1624 case WKBD_WINDOW5:
1625 case WKBD_WINDOW6:
1626 case WKBD_WINDOW7:
1627 case WKBD_WINDOW8:
1628 case WKBD_WINDOW9:
1629 case WKBD_WINDOW10:
1631 widx = command - WKBD_WINDOW1;
1633 if (w_global.shortcut.windows[widx]) {
1634 WMArray *list = w_global.shortcut.windows[widx];
1635 int cw;
1636 int count = WMGetArrayItemCount(list);
1637 WWindow *twin;
1638 WMArrayIterator iter;
1639 WWindow *wwin;
1641 wUnselectWindows(scr);
1642 cw = w_global.workspace.current;
1644 WM_ETARETI_ARRAY(list, wwin, iter) {
1645 if (count > 1)
1646 wWindowChangeWorkspace(wwin, cw);
1648 wMakeWindowVisible(wwin);
1650 if (count > 1)
1651 wSelectWindow(wwin, True);
1654 /* rotate the order of windows, to create a cycling effect */
1655 twin = WMGetFromArray(list, 0);
1656 WMDeleteFromArray(list, 0);
1657 WMAddToArray(list, twin);
1659 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1660 if (w_global.shortcut.windows[widx]) {
1661 WMFreeArray(w_global.shortcut.windows[widx]);
1662 w_global.shortcut.windows[widx] = NULL;
1665 if (wwin->flags.selected && scr->selected_windows) {
1666 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1667 } else {
1668 w_global.shortcut.windows[widx] = WMCreateArray(4);
1669 WMAddToArray(w_global.shortcut.windows[widx], wwin);
1672 wSelectWindow(wwin, !wwin->flags.selected);
1673 XFlush(dpy);
1674 wusleep(3000);
1675 wSelectWindow(wwin, !wwin->flags.selected);
1676 XFlush(dpy);
1678 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1679 if (wwin->flags.selected && scr->selected_windows) {
1680 if (w_global.shortcut.windows[widx])
1681 WMFreeArray(w_global.shortcut.windows[widx]);
1683 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1687 break;
1689 case WKBD_RELAUNCH:
1690 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1691 (void) RelaunchWindow(wwin);
1693 break;
1695 case WKBD_SWITCH_SCREEN:
1696 if (w_global.screen_count > 1) {
1697 WScreen *scr2;
1698 int i;
1700 /* find index of this screen */
1701 for (i = 0; i < w_global.screen_count; i++) {
1702 if (wScreenWithNumber(i) == scr)
1703 break;
1705 i++;
1706 if (i >= w_global.screen_count) {
1707 i = 0;
1709 scr2 = wScreenWithNumber(i);
1711 if (scr2) {
1712 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1713 scr2->scr_width / 2, scr2->scr_height / 2);
1716 break;
1718 case WKBD_NEXTWSLAYER:
1719 case WKBD_PREVWSLAYER:
1721 int row, column;
1723 row = w_global.workspace.current / 10;
1724 column = w_global.workspace.current % 10;
1726 if (command == WKBD_NEXTWSLAYER) {
1727 if ((row + 1) * 10 < w_global.workspace.count)
1728 wWorkspaceChange(scr, column + (row + 1) * 10);
1729 } else {
1730 if (row > 0)
1731 wWorkspaceChange(scr, column + (row - 1) * 10);
1734 break;
1735 case WKBD_CLIPRAISELOWER:
1736 if (!wPreferences.flags.noclip)
1737 wDockRaiseLower(w_global.workspace.array[w_global.workspace.current]->clip);
1738 break;
1739 case WKBD_DOCKRAISELOWER:
1740 if (!wPreferences.flags.nodock)
1741 wDockRaiseLower(scr->dock);
1742 break;
1743 #ifdef KEEP_XKB_LOCK_STATUS
1744 case WKBD_TOGGLE:
1745 if (wPreferences.modelock) {
1746 /*toggle */
1747 wwin = scr->focused_window;
1749 if (wwin && wwin->flags.mapped
1750 && wwin->frame->workspace == w_global.workspace.current
1751 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1752 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1754 wwin->frame->languagemode = wwin->frame->last_languagemode;
1755 wwin->frame->last_languagemode = staterec.group;
1756 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1760 break;
1761 #endif /* KEEP_XKB_LOCK_STATUS */
1765 static void handleMotionNotify(XEvent * event)
1767 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1769 if (wPreferences.scrollable_menus) {
1770 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1771 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1773 if (scr->flags.jump_back_pending ||
1774 p.x <= (rect.pos.x + 1) ||
1775 p.x >= (rect.pos.x + rect.size.width - 2) ||
1776 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1777 WMenu *menu;
1779 menu = wMenuUnderPointer(scr);
1780 if (menu != NULL)
1781 wMenuScroll(menu);
1786 static void handleVisibilityNotify(XEvent * event)
1788 WWindow *wwin;
1790 wwin = wWindowFor(event->xvisibility.window);
1791 if (!wwin)
1792 return;
1793 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);