wmaker: Moved variables for Inotify into the global namespace
[wmaker-crm.git] / src / event.c
blob8629c6c860c7df57243c17cabc07b43d5177a40b
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];
82 #define MOD_MASK wPreferences.modifier_mask
84 /************ Local stuff ***********/
86 static void saveTimestamp(XEvent *event);
87 static void handleColormapNotify(XEvent *event);
88 static void handleMapNotify(XEvent *event);
89 static void handleUnmapNotify(XEvent *event);
90 static void handleButtonPress(XEvent *event);
91 static void handleExpose(XEvent *event);
92 static void handleDestroyNotify(XEvent *event);
93 static void handleConfigureRequest(XEvent *event);
94 static void handleMapRequest(XEvent *event);
95 static void handlePropertyNotify(XEvent *event);
96 static void handleEnterNotify(XEvent *event);
97 static void handleLeaveNotify(XEvent *event);
98 static void handleExtensions(XEvent *event);
99 static void handleClientMessage(XEvent *event);
100 static void handleKeyPress(XEvent *event);
101 static void handleFocusIn(XEvent *event);
102 static void handleMotionNotify(XEvent *event);
103 static void handleVisibilityNotify(XEvent *event);
104 static void handle_inotify_events(int fd, int wd);
105 static void wdelete_death_handler(WMagicNumber id);
108 #ifdef SHAPE
109 static void handleShapeNotify(XEvent *event);
110 #endif
112 #ifdef KEEP_XKB_LOCK_STATUS
113 static void handleXkbIndicatorStateNotify(XEvent *event);
114 #endif
116 /* real dead process handler */
117 static void handleDeadProcess(void *foo);
119 typedef struct DeadProcesses {
120 pid_t pid;
121 unsigned char exit_status;
122 } DeadProcesses;
124 /* stack of dead processes */
125 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
126 static int deadProcessPtr = 0;
128 typedef struct DeathHandler {
129 WDeathHandler *callback;
130 pid_t pid;
131 void *client_data;
132 } DeathHandler;
134 static WMArray *deathHandlers = NULL;
136 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
138 DeathHandler *handler;
140 handler = malloc(sizeof(DeathHandler));
141 if (!handler)
142 return 0;
144 handler->pid = pid;
145 handler->callback = callback;
146 handler->client_data = cdata;
148 if (!deathHandlers)
149 deathHandlers = WMCreateArrayWithDestructor(8, free);
151 WMAddToArray(deathHandlers, handler);
153 return handler;
156 static void wdelete_death_handler(WMagicNumber id)
158 DeathHandler *handler = (DeathHandler *) id;
160 if (!handler || !deathHandlers)
161 return;
163 /* array destructor will call free(handler) */
164 WMRemoveFromArray(deathHandlers, handler);
167 void DispatchEvent(XEvent * event)
169 if (deathHandlers)
170 handleDeadProcess(NULL);
172 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
173 WCHANGE_STATE(WSTATE_EXITING);
174 /* received SIGTERM */
176 * WMHandleEvent() can't be called from anything
177 * executed inside here, or we can get in a infinite
178 * recursive loop.
180 Shutdown(WSExitMode);
182 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
183 WCHANGE_STATE(WSTATE_RESTARTING);
185 Shutdown(WSRestartPreparationMode);
186 /* received SIGHUP */
187 Restart(NULL, True);
188 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
189 WCHANGE_STATE(WSTATE_NORMAL);
190 wDefaultsCheckDomains(NULL);
193 /* for the case that all that is wanted to be dispatched is
194 * the stuff above */
195 if (!event)
196 return;
198 saveTimestamp(event);
199 switch (event->type) {
200 case MapRequest:
201 handleMapRequest(event);
202 break;
204 case KeyPress:
205 handleKeyPress(event);
206 break;
208 case MotionNotify:
209 handleMotionNotify(event);
210 break;
212 case ConfigureRequest:
213 handleConfigureRequest(event);
214 break;
216 case DestroyNotify:
217 handleDestroyNotify(event);
218 break;
220 case MapNotify:
221 handleMapNotify(event);
222 break;
224 case UnmapNotify:
225 handleUnmapNotify(event);
226 break;
228 case ButtonPress:
229 handleButtonPress(event);
230 break;
232 case Expose:
233 handleExpose(event);
234 break;
236 case PropertyNotify:
237 handlePropertyNotify(event);
238 break;
240 case EnterNotify:
241 handleEnterNotify(event);
242 break;
244 case LeaveNotify:
245 handleLeaveNotify(event);
246 break;
248 case ClientMessage:
249 handleClientMessage(event);
250 break;
252 case ColormapNotify:
253 handleColormapNotify(event);
254 break;
256 case MappingNotify:
257 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
258 XRefreshKeyboardMapping(&event->xmapping);
259 break;
261 case FocusIn:
262 handleFocusIn(event);
263 break;
265 case VisibilityNotify:
266 handleVisibilityNotify(event);
267 break;
269 case ConfigureNotify:
270 #ifdef HAVE_XRANDR
271 if (event->xconfigure.window == DefaultRootWindow(dpy))
272 XRRUpdateConfiguration(event);
273 #endif
274 break;
276 default:
277 handleExtensions(event);
278 break;
282 #ifdef HAVE_INOTIFY
284 *----------------------------------------------------------------------
285 * handle_inotify_events-
286 * Check for inotify events
288 * Returns:
289 * After reading events for the given file descriptor (fd) and
290 * watch descriptor (wd)
292 * Side effects:
293 * Calls wDefaultsCheckDomains if config database is updated
294 *----------------------------------------------------------------------
296 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
297 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
298 static void handle_inotify_events(int fd, int wd)
300 ssize_t eventQLength, i = 0;
301 char buff[BUFF_SIZE] = { 0 };
302 /* Check config only once per read of the event queue */
303 int oneShotFlag = 0;
306 * Read off the queued events
307 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
308 * not occur; the block is on Xevents, but a config file change will normally
309 * occur as a result of an Xevent - so the event queue should never have more than
310 * a few entries before a read().
312 eventQLength = read(fd, buff, BUFF_SIZE);
314 /* check what events occured */
315 /* Should really check wd here too, but for now we only have one watch! */
316 while (i < eventQLength) {
317 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
320 * see inotify.h for event types.
322 if (pevent->mask & IN_DELETE_SELF) {
323 wwarning(_("the defaults database has been deleted!"
324 " Restart Window Maker to create the database" " with the default settings"));
325 close(fd);
327 if (pevent->mask & IN_UNMOUNT) {
328 wwarning(_("the unit containing the defaults database has"
329 " been unmounted. Setting --static mode." " Any changes will not be saved."));
330 close(fd);
331 wPreferences.flags.noupdates = 1;
333 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
334 wwarning(_("Inotify: Reading config files in defaults database."));
335 wDefaultsCheckDomains(NULL);
338 /* move to next event in the buffer */
339 i += sizeof(struct inotify_event) + pevent->len;
342 #endif /* HAVE_INOTIFY */
345 *----------------------------------------------------------------------
346 * EventLoop-
347 * Processes X and internal events indefinitely.
349 * Returns:
350 * Never returns
352 * Side effects:
353 * The LastTimestamp global variable is updated.
354 * Calls inotifyGetEvents if defaults database changes.
355 *----------------------------------------------------------------------
357 noreturn void EventLoop(void)
359 XEvent event;
360 #ifdef HAVE_INOTIFY
361 struct timeval time;
362 fd_set rfds;
363 int retVal = 0;
365 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
366 retVal = -1;
367 #endif
369 for (;;) {
371 WMNextEvent(dpy, &event); /* Blocks here */
372 WMHandleEvent(&event);
373 #ifdef HAVE_INOTIFY
374 if (retVal != -1) {
375 time.tv_sec = 0;
376 time.tv_usec = 0;
377 FD_ZERO(&rfds);
378 FD_SET(w_global.inotify.fd_event_queue, &rfds);
380 /* check for available read data from inotify - don't block! */
381 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
383 if (retVal < 0) { /* an error has occured */
384 wwarning(_("select failed. The inotify instance will be closed."
385 " Changes to the defaults database will require"
386 " a restart to take effect."));
387 close(w_global.inotify.fd_event_queue);
388 w_global.inotify.fd_event_queue = -1;
389 continue;
391 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
392 handle_inotify_events(w_global.inotify.fd_event_queue, w_global.inotify.wd_defaults);
394 #endif
399 *----------------------------------------------------------------------
400 * ProcessPendingEvents --
401 * Processes the events that are currently pending (at the time
402 * this function is called) in the display's queue.
404 * Returns:
405 * After the pending events that were present at the function call
406 * are processed.
408 * Side effects:
409 * Many -- whatever handling events may involve.
411 *----------------------------------------------------------------------
413 void ProcessPendingEvents(void)
415 XEvent event;
416 int count;
418 XSync(dpy, False);
420 /* Take a snapshot of the event count in the queue */
421 count = XPending(dpy);
423 while (count > 0 && XPending(dpy)) {
424 WMNextEvent(dpy, &event);
425 WMHandleEvent(&event);
426 count--;
430 Bool IsDoubleClick(WScreen * scr, XEvent * event)
432 if ((scr->last_click_time > 0) &&
433 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
434 && (event->xbutton.button == scr->last_click_button)
435 && (event->xbutton.window == scr->last_click_window)) {
437 scr->flags.next_click_is_not_double = 1;
438 scr->last_click_time = 0;
439 scr->last_click_window = event->xbutton.window;
441 return True;
443 return False;
446 void NotifyDeadProcess(pid_t pid, unsigned char status)
448 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
449 wwarning("stack overflow: too many dead processes");
450 return;
452 /* stack the process to be handled later,
453 * as this is called from the signal handler */
454 deadProcesses[deadProcessPtr].pid = pid;
455 deadProcesses[deadProcessPtr].exit_status = status;
456 deadProcessPtr++;
459 static void handleDeadProcess(void *foo)
461 DeathHandler *tmp;
462 int i;
464 for (i = 0; i < deadProcessPtr; i++) {
465 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
468 if (!deathHandlers) {
469 deadProcessPtr = 0;
470 return;
473 /* get the pids on the queue and call handlers */
474 while (deadProcessPtr > 0) {
475 deadProcessPtr--;
477 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
478 tmp = WMGetFromArray(deathHandlers, i);
479 if (!tmp)
480 continue;
482 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
483 (*tmp->callback) (tmp->pid,
484 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
485 wdelete_death_handler(tmp);
491 static void saveTimestamp(XEvent * event)
494 * Never save CurrentTime as LastTimestamp because CurrentTime
495 * it's not a real timestamp (it's the 0L constant)
498 switch (event->type) {
499 case ButtonRelease:
500 case ButtonPress:
501 w_global.timestamp.last_event = event->xbutton.time;
502 break;
503 case KeyPress:
504 case KeyRelease:
505 w_global.timestamp.last_event = event->xkey.time;
506 break;
507 case MotionNotify:
508 w_global.timestamp.last_event = event->xmotion.time;
509 break;
510 case PropertyNotify:
511 w_global.timestamp.last_event = event->xproperty.time;
512 break;
513 case EnterNotify:
514 case LeaveNotify:
515 w_global.timestamp.last_event = event->xcrossing.time;
516 break;
517 case SelectionClear:
518 w_global.timestamp.last_event = event->xselectionclear.time;
519 break;
520 case SelectionRequest:
521 w_global.timestamp.last_event = event->xselectionrequest.time;
522 break;
523 case SelectionNotify:
524 w_global.timestamp.last_event = event->xselection.time;
525 #ifdef XDND
526 wXDNDProcessSelection(event);
527 #endif
528 break;
532 static int matchWindow(const void *item, const void *cdata)
534 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
537 static void handleExtensions(XEvent * event)
539 #ifdef KEEP_XKB_LOCK_STATUS
540 XkbEvent *xkbevent;
541 xkbevent = (XkbEvent *) event;
542 #endif /*KEEP_XKB_LOCK_STATUS */
543 #ifdef SHAPE
544 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
545 handleShapeNotify(event);
547 #endif
548 #ifdef KEEP_XKB_LOCK_STATUS
549 if (wPreferences.modelock && (xkbevent->type == w_global.xext.xkb.event_base)) {
550 handleXkbIndicatorStateNotify(event);
552 #endif /*KEEP_XKB_LOCK_STATUS */
553 #ifdef HAVE_XRANDR
554 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
555 /* From xrandr man page: "Clients must call back into Xlib using
556 * XRRUpdateConfiguration when screen configuration change notify
557 * events are generated */
558 XRRUpdateConfiguration(event);
559 WCHANGE_STATE(WSTATE_RESTARTING);
560 Shutdown(WSRestartPreparationMode);
561 Restart(NULL,True);
563 #endif
566 static void handleMapRequest(XEvent * ev)
568 WWindow *wwin;
569 WScreen *scr = NULL;
570 Window window = ev->xmaprequest.window;
572 if ((wwin = wWindowFor(window))) {
573 if (wwin->flags.shaded) {
574 wUnshadeWindow(wwin);
576 /* deiconify window */
577 if (wwin->flags.miniaturized) {
578 wDeiconifyWindow(wwin);
579 } else if (wwin->flags.hidden) {
580 WApplication *wapp = wApplicationOf(wwin->main_window);
581 /* go to the last workspace that the user worked on the app */
582 if (wapp) {
583 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
585 wUnhideApplication(wapp, False, False);
587 return;
590 scr = wScreenForRootWindow(ev->xmaprequest.parent);
592 wwin = wManageWindow(scr, window);
595 * This is to let the Dock know that the application it launched
596 * has already been mapped (eg: it has finished launching).
597 * It is not necessary for normally docked apps, but is needed for
598 * apps that were forcedly docked (like with dockit).
600 if (scr->last_dock) {
601 if (wwin && wwin->main_window != None && wwin->main_window != window)
602 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
603 else
604 wDockTrackWindowLaunch(scr->last_dock, window);
607 if (wwin) {
608 wClientSetState(wwin, NormalState, None);
609 if (wwin->flags.maximized) {
610 wMaximizeWindow(wwin, wwin->flags.maximized);
612 if (wwin->flags.shaded) {
613 wwin->flags.shaded = 0;
614 wwin->flags.skip_next_animation = 1;
615 wShadeWindow(wwin);
617 if (wwin->flags.miniaturized) {
618 wwin->flags.miniaturized = 0;
619 wwin->flags.skip_next_animation = 1;
620 wIconifyWindow(wwin);
622 if (wwin->flags.fullscreen) {
623 wwin->flags.fullscreen = 0;
624 wFullscreenWindow(wwin);
626 if (wwin->flags.hidden) {
627 WApplication *wapp = wApplicationOf(wwin->main_window);
629 wwin->flags.hidden = 0;
630 wwin->flags.skip_next_animation = 1;
631 if (wapp) {
632 wHideApplication(wapp);
638 static void handleDestroyNotify(XEvent * event)
640 WWindow *wwin;
641 WApplication *app;
642 Window window = event->xdestroywindow.window;
643 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
644 int widx;
646 wwin = wWindowFor(window);
647 if (wwin) {
648 wUnmanageWindow(wwin, False, True);
651 if (scr != NULL) {
652 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
653 WFakeGroupLeader *fPtr;
655 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
656 if (fPtr->retainCount > 0) {
657 fPtr->retainCount--;
658 if (fPtr->retainCount == 0 && fPtr->leader != None) {
659 XDestroyWindow(dpy, fPtr->leader);
660 fPtr->leader = None;
661 XFlush(dpy);
664 fPtr->origLeader = None;
668 app = wApplicationOf(window);
669 if (app) {
670 if (window == app->main_window) {
671 app->refcount = 0;
672 wwin = app->main_window_desc->screen_ptr->focused_window;
673 while (wwin) {
674 if (wwin->main_window == window) {
675 wwin->main_window = None;
677 wwin = wwin->prev;
680 wApplicationDestroy(app);
684 static void handleExpose(XEvent * event)
686 WObjDescriptor *desc;
687 XEvent ev;
689 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
691 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
692 return;
695 if (desc->handle_expose) {
696 (*desc->handle_expose) (desc, event);
700 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
702 switch (action) {
703 case WA_SELECT_WINDOWS:
704 wUnselectWindows(scr);
705 wSelectWindows(scr, event);
706 break;
707 case WA_OPEN_APPMENU:
708 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
709 /* ugly hack */
710 if (scr->root_menu) {
711 if (scr->root_menu->brother->flags.mapped)
712 event->xbutton.window = scr->root_menu->brother->frame->core->window;
713 else
714 event->xbutton.window = scr->root_menu->frame->core->window;
716 break;
717 case WA_OPEN_WINLISTMENU:
718 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
719 if (scr->switch_menu) {
720 if (scr->switch_menu->brother->flags.mapped)
721 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
722 else
723 event->xbutton.window = scr->switch_menu->frame->core->window;
725 break;
726 default:
727 break;
731 /* bindable */
732 static void handleButtonPress(XEvent * event)
734 WObjDescriptor *desc;
735 WScreen *scr;
737 scr = wScreenForRootWindow(event->xbutton.root);
739 #ifdef BALLOON_TEXT
740 wBalloonHide(scr);
741 #endif
743 if (event->xbutton.window == scr->root_win) {
744 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
745 executeButtonAction(scr, event, wPreferences.mouse_button1);
746 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
747 executeButtonAction(scr, event, wPreferences.mouse_button2);
748 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
749 executeButtonAction(scr, event, wPreferences.mouse_button3);
750 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
751 wWorkspaceRelativeChange(scr, 1);
752 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
753 wWorkspaceRelativeChange(scr, -1);
757 desc = NULL;
758 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
759 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
760 return;
764 if (desc->parent_type == WCLASS_WINDOW) {
765 XSync(dpy, 0);
767 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
768 XAllowEvents(dpy, AsyncPointer, CurrentTime);
769 } else {
770 /* if (wPreferences.focus_mode == WKF_CLICK) { */
771 if (wPreferences.ignore_focus_click) {
772 XAllowEvents(dpy, AsyncPointer, CurrentTime);
774 XAllowEvents(dpy, ReplayPointer, CurrentTime);
775 /* } */
777 XSync(dpy, 0);
778 } else if (desc->parent_type == WCLASS_APPICON
779 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
780 if (event->xbutton.state & MOD_MASK) {
781 XSync(dpy, 0);
782 XAllowEvents(dpy, AsyncPointer, CurrentTime);
783 XSync(dpy, 0);
787 if (desc->handle_mousedown != NULL) {
788 (*desc->handle_mousedown) (desc, event);
791 /* save double-click information */
792 if (scr->flags.next_click_is_not_double) {
793 scr->flags.next_click_is_not_double = 0;
794 } else {
795 scr->last_click_time = event->xbutton.time;
796 scr->last_click_button = event->xbutton.button;
797 scr->last_click_window = event->xbutton.window;
801 static void handleMapNotify(XEvent * event)
803 WWindow *wwin;
805 wwin = wWindowFor(event->xmap.event);
806 if (wwin && wwin->client_win == event->xmap.event) {
807 if (wwin->flags.miniaturized) {
808 wDeiconifyWindow(wwin);
809 } else {
810 XGrabServer(dpy);
811 wWindowMap(wwin);
812 wClientSetState(wwin, NormalState, None);
813 XUngrabServer(dpy);
818 static void handleUnmapNotify(XEvent * event)
820 WWindow *wwin;
821 XEvent ev;
822 Bool withdraw = False;
824 /* only process windows with StructureNotify selected
825 * (ignore SubstructureNotify) */
826 wwin = wWindowFor(event->xunmap.window);
827 if (!wwin)
828 return;
830 /* whether the event is a Withdrawal request */
831 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
832 withdraw = True;
834 if (wwin->client_win != event->xunmap.event && !withdraw)
835 return;
837 if (!wwin->flags.mapped && !withdraw
838 && wwin->frame->workspace == w_global.workspace.current
839 && !wwin->flags.miniaturized && !wwin->flags.hidden)
840 return;
842 XGrabServer(dpy);
843 XUnmapWindow(dpy, wwin->frame->core->window);
844 wwin->flags.mapped = 0;
845 XSync(dpy, 0);
846 /* check if the window was destroyed */
847 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
848 DispatchEvent(&ev);
849 } else {
850 Bool reparented = False;
852 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
853 reparented = True;
855 /* withdraw window */
856 wwin->flags.mapped = 0;
857 if (!reparented)
858 wClientSetState(wwin, WithdrawnState, None);
860 /* if the window was reparented, do not reparent it back to the
861 * root window */
862 wUnmanageWindow(wwin, !reparented, False);
864 XUngrabServer(dpy);
867 static void handleConfigureRequest(XEvent * event)
869 WWindow *wwin;
871 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
873 * Configure request for unmapped window
875 wClientConfigure(NULL, &(event->xconfigurerequest));
876 } else {
877 wClientConfigure(wwin, &(event->xconfigurerequest));
881 static void handlePropertyNotify(XEvent * event)
883 WWindow *wwin;
884 WApplication *wapp;
885 Window jr;
886 int ji;
887 unsigned int ju;
889 wwin = wWindowFor(event->xproperty.window);
890 if (wwin) {
891 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
892 return;
894 wClientCheckProperty(wwin, &event->xproperty);
896 wapp = wApplicationOf(event->xproperty.window);
897 if (wapp) {
898 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
902 static void handleClientMessage(XEvent * event)
904 WWindow *wwin;
905 WObjDescriptor *desc;
907 /* handle transition from Normal to Iconic state */
908 if (event->xclient.message_type == w_global.atom.wm.change_state
909 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
911 wwin = wWindowFor(event->xclient.window);
912 if (!wwin)
913 return;
914 if (!wwin->flags.miniaturized)
915 wIconifyWindow(wwin);
916 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
917 WScreen *scr = wScreenForRootWindow(event->xclient.window);
919 if (!scr)
920 return;
922 if (event->xclient.data.l[1] == 1) { /* starting */
923 wColormapAllowClientInstallation(scr, True);
924 } else { /* stopping */
925 wColormapAllowClientInstallation(scr, False);
927 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
929 char *command;
930 size_t len;
932 len = sizeof(event->xclient.data.b) + 1;
933 command = wmalloc(len);
934 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
936 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
937 wwarning(_("Got Reconfigure command"));
938 wDefaultsCheckDomains(NULL);
939 } else {
940 wwarning(_("Got unknown command %s"), command);
943 wfree(command);
945 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
946 WApplication *wapp;
947 int done = 0;
948 wapp = wApplicationOf(event->xclient.window);
949 if (wapp) {
950 switch (event->xclient.data.l[0]) {
951 case WMFHideOtherApplications:
952 wHideOtherApplications(wapp->main_window_desc);
953 done = 1;
954 break;
956 case WMFHideApplication:
957 wHideApplication(wapp);
958 done = 1;
959 break;
962 if (!done) {
963 wwin = wWindowFor(event->xclient.window);
964 if (wwin) {
965 switch (event->xclient.data.l[0]) {
966 case WMFHideOtherApplications:
967 wHideOtherApplications(wwin);
968 break;
970 case WMFHideApplication:
971 wHideApplication(wApplicationOf(wwin->main_window));
972 break;
976 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
977 wwin = wWindowFor(event->xclient.window);
978 if (!wwin)
979 return;
980 switch (event->xclient.data.l[0]) {
981 case GSWindowLevelAttr:
983 int level = (int)event->xclient.data.l[1];
985 if (WINDOW_LEVEL(wwin) != level) {
986 ChangeStackingLevel(wwin->frame->core, level);
989 break;
991 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
992 wwin = wWindowFor(event->xclient.window);
993 if (!wwin)
994 return;
995 switch (event->xclient.data.l[0]) {
996 case WMTitleBarNormal:
997 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
998 break;
999 case WMTitleBarMain:
1000 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1001 break;
1002 case WMTitleBarKey:
1003 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1004 break;
1006 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1007 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1008 if (!scr)
1009 return;
1010 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1011 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1012 /* do nothing */
1013 #ifdef XDND
1014 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1015 /* do nothing */
1016 #endif /* XDND */
1017 } else {
1019 * Non-standard thing, but needed by OffiX DND.
1020 * For when the icon frame gets a ClientMessage
1021 * that should have gone to the icon_window.
1023 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1024 struct WIcon *icon = NULL;
1026 if (desc->parent_type == WCLASS_MINIWINDOW) {
1027 icon = (WIcon *) desc->parent;
1028 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1029 icon = ((WAppIcon *) desc->parent)->icon;
1031 if (icon && (wwin = icon->owner)) {
1032 if (wwin->client_win != event->xclient.window) {
1033 event->xclient.window = wwin->client_win;
1034 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1041 static void raiseWindow(WScreen * scr)
1043 WWindow *wwin;
1045 scr->autoRaiseTimer = NULL;
1047 wwin = wWindowFor(scr->autoRaiseWindow);
1048 if (!wwin)
1049 return;
1051 if (!wwin->flags.destroyed && wwin->flags.focused) {
1052 wRaiseFrame(wwin->frame->core);
1053 /* this is needed or a race condition will occur */
1054 XSync(dpy, False);
1058 static void handleEnterNotify(XEvent * event)
1060 WWindow *wwin;
1061 WObjDescriptor *desc = NULL;
1062 XEvent ev;
1063 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1065 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1066 /* already left the window... */
1067 saveTimestamp(&ev);
1068 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1069 return;
1073 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1074 if (desc->handle_enternotify)
1075 (*desc->handle_enternotify) (desc, event);
1078 /* enter to window */
1079 wwin = wWindowFor(event->xcrossing.window);
1080 if (!wwin) {
1081 if (wPreferences.colormap_mode == WCM_POINTER) {
1082 wColormapInstallForWindow(scr, NULL);
1084 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1085 WMDeleteTimerHandler(scr->autoRaiseTimer);
1086 scr->autoRaiseTimer = NULL;
1088 } else {
1089 /* set auto raise timer even if in focus-follows-mouse mode
1090 * and the event is for the frame window, even if the window
1091 * has focus already. useful if you move the pointer from a focused
1092 * window to the root window and back pretty fast
1094 * set focus if in focus-follows-mouse mode and the event
1095 * is for the frame window and window doesn't have focus yet */
1096 if (wPreferences.focus_mode == WKF_SLOPPY
1097 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1099 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1100 wSetFocusTo(scr, wwin);
1102 if (scr->autoRaiseTimer)
1103 WMDeleteTimerHandler(scr->autoRaiseTimer);
1104 scr->autoRaiseTimer = NULL;
1106 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1107 scr->autoRaiseWindow = wwin->frame->core->window;
1108 scr->autoRaiseTimer
1109 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1112 /* Install colormap for window, if the colormap installation mode
1113 * is colormap_follows_mouse */
1114 if (wPreferences.colormap_mode == WCM_POINTER) {
1115 if (wwin->client_win == event->xcrossing.window)
1116 wColormapInstallForWindow(scr, wwin);
1117 else
1118 wColormapInstallForWindow(scr, NULL);
1122 if (event->xcrossing.window == event->xcrossing.root
1123 && event->xcrossing.detail == NotifyNormal
1124 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1126 wSetFocusTo(scr, scr->focused_window);
1128 #ifdef BALLOON_TEXT
1129 wBalloonEnteredObject(scr, desc);
1130 #endif
1133 static void handleLeaveNotify(XEvent * event)
1135 WObjDescriptor *desc = NULL;
1137 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1138 if (desc->handle_leavenotify)
1139 (*desc->handle_leavenotify) (desc, event);
1143 #ifdef SHAPE
1144 static void handleShapeNotify(XEvent * event)
1146 XShapeEvent *shev = (XShapeEvent *) event;
1147 WWindow *wwin;
1148 union {
1149 XEvent xevent;
1150 XShapeEvent xshape;
1151 } ev;
1153 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1154 if (ev.xshape.kind == ShapeBounding) {
1155 if (ev.xshape.shaped == shev->shaped) {
1156 *shev = ev.xshape;
1157 } else {
1158 XPutBackEvent(dpy, &ev.xevent);
1159 break;
1164 wwin = wWindowFor(shev->window);
1165 if (!wwin || shev->kind != ShapeBounding)
1166 return;
1168 if (!shev->shaped && wwin->flags.shaped) {
1170 wwin->flags.shaped = 0;
1171 wWindowClearShape(wwin);
1173 } else if (shev->shaped) {
1175 wwin->flags.shaped = 1;
1176 wWindowSetShape(wwin);
1179 #endif /* SHAPE */
1181 #ifdef KEEP_XKB_LOCK_STATUS
1182 /* please help ]d if you know what to do */
1183 static void handleXkbIndicatorStateNotify(XEvent *event)
1185 WWindow *wwin;
1186 WScreen *scr;
1187 XkbStateRec staterec;
1188 int i;
1190 for (i = 0; i < w_global.screen_count; i++) {
1191 scr = wScreenWithNumber(i);
1192 wwin = scr->focused_window;
1193 if (wwin && wwin->flags.focused) {
1194 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1195 if (wwin->frame->languagemode != staterec.group) {
1196 wwin->frame->last_languagemode = wwin->frame->languagemode;
1197 wwin->frame->languagemode = staterec.group;
1199 #ifdef XKB_BUTTON_HINT
1200 if (wwin->frame->titlebar) {
1201 wFrameWindowPaint(wwin->frame);
1203 #endif
1207 #endif /*KEEP_XKB_LOCK_STATUS */
1209 static void handleColormapNotify(XEvent * event)
1211 WWindow *wwin;
1212 WScreen *scr;
1213 Bool reinstall = False;
1215 wwin = wWindowFor(event->xcolormap.window);
1216 if (!wwin)
1217 return;
1219 scr = wwin->screen_ptr;
1221 do {
1222 if (wwin) {
1223 if (event->xcolormap.new) {
1224 XWindowAttributes attr;
1226 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1228 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1229 scr->current_colormap = attr.colormap;
1231 reinstall = True;
1232 } else if (event->xcolormap.state == ColormapUninstalled &&
1233 scr->current_colormap == event->xcolormap.colormap) {
1235 /* some bastard app (like XV) removed our colormap */
1237 * can't enforce or things like xscreensaver wont work
1238 * reinstall = True;
1240 } else if (event->xcolormap.state == ColormapInstalled &&
1241 scr->current_colormap == event->xcolormap.colormap) {
1243 /* someone has put our colormap back */
1244 reinstall = False;
1247 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1248 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1250 if (reinstall && scr->current_colormap != None) {
1251 if (!scr->flags.colormap_stuff_blocked)
1252 XInstallColormap(dpy, scr->current_colormap);
1256 static void handleFocusIn(XEvent * event)
1258 WWindow *wwin;
1261 * For applications that like stealing the focus.
1263 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1264 saveTimestamp(event);
1265 if (event->xfocus.mode == NotifyUngrab
1266 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1267 return;
1270 wwin = wWindowFor(event->xfocus.window);
1271 if (wwin && !wwin->flags.focused) {
1272 if (wwin->flags.mapped)
1273 wSetFocusTo(wwin->screen_ptr, wwin);
1274 else
1275 wSetFocusTo(wwin->screen_ptr, NULL);
1276 } else if (!wwin) {
1277 WScreen *scr = wScreenForWindow(event->xfocus.window);
1278 if (scr)
1279 wSetFocusTo(scr, NULL);
1283 static WWindow *windowUnderPointer(WScreen * scr)
1285 unsigned int mask;
1286 int foo;
1287 Window bar, win;
1289 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1290 return wWindowFor(win);
1291 return NULL;
1294 static int CheckFullScreenWindowFocused(WScreen * scr)
1296 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1297 return 1;
1298 else
1299 return 0;
1302 static void handleKeyPress(XEvent * event)
1304 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1305 WWindow *wwin = scr->focused_window;
1306 short i, widx;
1307 int modifiers;
1308 int command = -1;
1309 #ifdef KEEP_XKB_LOCK_STATUS
1310 XkbStateRec staterec;
1311 #endif /*KEEP_XKB_LOCK_STATUS */
1313 /* ignore CapsLock */
1314 modifiers = event->xkey.state & ValidModMask;
1316 for (i = 0; i < WKBD_LAST; i++) {
1317 if (wKeyBindings[i].keycode == 0)
1318 continue;
1320 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1321 || */ wKeyBindings[i].modifier ==
1322 modifiers)) {
1323 command = i;
1324 break;
1328 if (command < 0) {
1330 if (!wRootMenuPerformShortcut(event)) {
1331 static int dontLoop = 0;
1333 if (dontLoop > 10) {
1334 wwarning("problem with key event processing code");
1335 return;
1337 dontLoop++;
1338 /* if the focused window is an internal window, try redispatching
1339 * the event to the managed window, as it can be a WINGs window */
1340 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1341 /* client_leader contains the WINGs toplevel */
1342 event->xany.window = wwin->client_leader;
1343 WMHandleEvent(event);
1345 dontLoop--;
1347 return;
1349 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1350 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1352 switch (command) {
1354 case WKBD_ROOTMENU:
1355 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1356 if (!CheckFullScreenWindowFocused(scr)) {
1357 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1358 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1359 True);
1361 break;
1362 case WKBD_WINDOWLIST:
1363 if (!CheckFullScreenWindowFocused(scr)) {
1364 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1365 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1366 True);
1368 break;
1370 case WKBD_WINDOWMENU:
1371 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1372 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1373 break;
1374 case WKBD_MINIMIZEALL:
1375 CloseWindowMenu(scr);
1376 wHideAll(scr);
1377 break;
1378 case WKBD_MINIATURIZE:
1379 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1380 && !WFLAGP(wwin, no_miniaturizable)) {
1381 CloseWindowMenu(scr);
1383 if (wwin->protocols.MINIATURIZE_WINDOW)
1384 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1385 else {
1386 wIconifyWindow(wwin);
1389 break;
1390 case WKBD_HIDE:
1391 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1392 WApplication *wapp = wApplicationOf(wwin->main_window);
1393 CloseWindowMenu(scr);
1395 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1396 wHideApplication(wapp);
1399 break;
1400 case WKBD_HIDE_OTHERS:
1401 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1402 CloseWindowMenu(scr);
1404 wHideOtherApplications(wwin);
1406 break;
1407 case WKBD_MAXIMIZE:
1408 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1409 CloseWindowMenu(scr);
1411 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1413 break;
1414 case WKBD_VMAXIMIZE:
1415 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1416 CloseWindowMenu(scr);
1418 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1420 break;
1421 case WKBD_HMAXIMIZE:
1422 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1423 CloseWindowMenu(scr);
1425 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1427 break;
1428 case WKBD_LHMAXIMIZE:
1429 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1430 CloseWindowMenu(scr);
1432 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1434 break;
1435 case WKBD_RHMAXIMIZE:
1436 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1437 CloseWindowMenu(scr);
1439 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1441 break;
1442 case WKBD_THMAXIMIZE:
1443 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1444 CloseWindowMenu(scr);
1446 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1448 break;
1449 case WKBD_BHMAXIMIZE:
1450 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1451 CloseWindowMenu(scr);
1453 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1455 break;
1456 case WKBD_LTCMAXIMIZE:
1457 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1458 CloseWindowMenu(scr);
1460 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1462 break;
1463 case WKBD_RTCMAXIMIZE:
1464 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1465 CloseWindowMenu(scr);
1467 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1469 break;
1470 case WKBD_LBCMAXIMIZE:
1471 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1472 CloseWindowMenu(scr);
1474 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1476 break;
1477 case WKBD_RBCMAXIMIZE:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1479 CloseWindowMenu(scr);
1481 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1483 break;
1484 case WKBD_MAXIMUS:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1490 break;
1491 case WKBD_RAISE:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1493 CloseWindowMenu(scr);
1495 wRaiseFrame(wwin->frame->core);
1497 break;
1498 case WKBD_LOWER:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1500 CloseWindowMenu(scr);
1502 wLowerFrame(wwin->frame->core);
1504 break;
1505 case WKBD_RAISELOWER:
1506 /* raise or lower the window under the pointer, not the
1507 * focused one
1509 wwin = windowUnderPointer(scr);
1510 if (wwin)
1511 wRaiseLowerFrame(wwin->frame->core);
1512 break;
1513 case WKBD_SHADE:
1514 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1515 if (wwin->flags.shaded)
1516 wUnshadeWindow(wwin);
1517 else
1518 wShadeWindow(wwin);
1520 break;
1521 case WKBD_MOVERESIZE:
1522 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1523 CloseWindowMenu(scr);
1525 wKeyboardMoveResizeWindow(wwin);
1527 break;
1528 case WKBD_CLOSE:
1529 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1530 CloseWindowMenu(scr);
1531 if (wwin->protocols.DELETE_WINDOW)
1532 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1534 break;
1535 case WKBD_SELECT:
1536 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1537 wSelectWindow(wwin, !wwin->flags.selected);
1539 break;
1540 case WKBD_FOCUSNEXT:
1541 StartWindozeCycle(wwin, event, True, False);
1542 break;
1544 case WKBD_FOCUSPREV:
1545 StartWindozeCycle(wwin, event, False, False);
1546 break;
1548 case WKBD_GROUPNEXT:
1549 StartWindozeCycle(wwin, event, True, True);
1550 break;
1552 case WKBD_GROUPPREV:
1553 StartWindozeCycle(wwin, event, False, True);
1554 break;
1556 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1557 widx = command - WKBD_WORKSPACE1;
1558 i = (w_global.workspace.current / 10) * 10 + widx;
1559 if (wPreferences.ws_advance || i < w_global.workspace.count)
1560 wWorkspaceChange(scr, i);
1561 break;
1563 case WKBD_NEXTWORKSPACE:
1564 wWorkspaceRelativeChange(scr, 1);
1565 break;
1566 case WKBD_PREVWORKSPACE:
1567 wWorkspaceRelativeChange(scr, -1);
1568 break;
1569 case WKBD_LASTWORKSPACE:
1570 wWorkspaceChange(scr, w_global.workspace.last_used);
1571 break;
1573 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1574 widx = command - WKBD_MOVE_WORKSPACE1;
1575 i = (w_global.workspace.current / 10) * 10 + widx;
1576 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1577 wWindowChangeWorkspace(wwin, i);
1578 break;
1580 case WKBD_MOVE_NEXTWORKSPACE:
1581 if (wwin)
1582 wWindowChangeWorkspaceRelative(wwin, 1);
1583 break;
1584 case WKBD_MOVE_PREVWORKSPACE:
1585 if (wwin)
1586 wWindowChangeWorkspaceRelative(wwin, -1);
1587 break;
1588 case WKBD_MOVE_LASTWORKSPACE:
1589 if (wwin)
1590 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1591 break;
1593 case WKBD_MOVE_NEXTWSLAYER:
1594 case WKBD_MOVE_PREVWSLAYER:
1596 if (wwin) {
1597 int row, column;
1599 row = w_global.workspace.current / 10;
1600 column = w_global.workspace.current % 10;
1602 if (command == WKBD_MOVE_NEXTWSLAYER) {
1603 if ((row + 1) * 10 < w_global.workspace.count)
1604 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1605 } else {
1606 if (row > 0)
1607 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1611 break;
1613 case WKBD_WINDOW1:
1614 case WKBD_WINDOW2:
1615 case WKBD_WINDOW3:
1616 case WKBD_WINDOW4:
1617 case WKBD_WINDOW5:
1618 case WKBD_WINDOW6:
1619 case WKBD_WINDOW7:
1620 case WKBD_WINDOW8:
1621 case WKBD_WINDOW9:
1622 case WKBD_WINDOW10:
1624 widx = command - WKBD_WINDOW1;
1626 if (w_global.shortcut.windows[widx]) {
1627 WMArray *list = w_global.shortcut.windows[widx];
1628 int cw;
1629 int count = WMGetArrayItemCount(list);
1630 WWindow *twin;
1631 WMArrayIterator iter;
1632 WWindow *wwin;
1634 wUnselectWindows(scr);
1635 cw = w_global.workspace.current;
1637 WM_ETARETI_ARRAY(list, wwin, iter) {
1638 if (count > 1)
1639 wWindowChangeWorkspace(wwin, cw);
1641 wMakeWindowVisible(wwin);
1643 if (count > 1)
1644 wSelectWindow(wwin, True);
1647 /* rotate the order of windows, to create a cycling effect */
1648 twin = WMGetFromArray(list, 0);
1649 WMDeleteFromArray(list, 0);
1650 WMAddToArray(list, twin);
1652 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1653 if (w_global.shortcut.windows[widx]) {
1654 WMFreeArray(w_global.shortcut.windows[widx]);
1655 w_global.shortcut.windows[widx] = NULL;
1658 if (wwin->flags.selected && scr->selected_windows) {
1659 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1660 } else {
1661 w_global.shortcut.windows[widx] = WMCreateArray(4);
1662 WMAddToArray(w_global.shortcut.windows[widx], wwin);
1665 wSelectWindow(wwin, !wwin->flags.selected);
1666 XFlush(dpy);
1667 wusleep(3000);
1668 wSelectWindow(wwin, !wwin->flags.selected);
1669 XFlush(dpy);
1671 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1672 if (wwin->flags.selected && scr->selected_windows) {
1673 if (w_global.shortcut.windows[widx])
1674 WMFreeArray(w_global.shortcut.windows[widx]);
1676 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1680 break;
1682 case WKBD_RELAUNCH:
1683 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1684 (void) RelaunchWindow(wwin);
1686 break;
1688 case WKBD_SWITCH_SCREEN:
1689 if (w_global.screen_count > 1) {
1690 WScreen *scr2;
1691 int i;
1693 /* find index of this screen */
1694 for (i = 0; i < w_global.screen_count; i++) {
1695 if (wScreenWithNumber(i) == scr)
1696 break;
1698 i++;
1699 if (i >= w_global.screen_count) {
1700 i = 0;
1702 scr2 = wScreenWithNumber(i);
1704 if (scr2) {
1705 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1706 scr2->scr_width / 2, scr2->scr_height / 2);
1709 break;
1711 case WKBD_NEXTWSLAYER:
1712 case WKBD_PREVWSLAYER:
1714 int row, column;
1716 row = w_global.workspace.current / 10;
1717 column = w_global.workspace.current % 10;
1719 if (command == WKBD_NEXTWSLAYER) {
1720 if ((row + 1) * 10 < w_global.workspace.count)
1721 wWorkspaceChange(scr, column + (row + 1) * 10);
1722 } else {
1723 if (row > 0)
1724 wWorkspaceChange(scr, column + (row - 1) * 10);
1727 break;
1728 case WKBD_CLIPRAISELOWER:
1729 if (!wPreferences.flags.noclip)
1730 wDockRaiseLower(w_global.workspace.array[w_global.workspace.current]->clip);
1731 break;
1732 case WKBD_DOCKRAISELOWER:
1733 if (!wPreferences.flags.nodock)
1734 wDockRaiseLower(scr->dock);
1735 break;
1736 #ifdef KEEP_XKB_LOCK_STATUS
1737 case WKBD_TOGGLE:
1738 if (wPreferences.modelock) {
1739 /*toggle */
1740 wwin = scr->focused_window;
1742 if (wwin && wwin->flags.mapped
1743 && wwin->frame->workspace == w_global.workspace.current
1744 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1745 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1747 wwin->frame->languagemode = wwin->frame->last_languagemode;
1748 wwin->frame->last_languagemode = staterec.group;
1749 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1753 break;
1754 #endif /* KEEP_XKB_LOCK_STATUS */
1758 static void handleMotionNotify(XEvent * event)
1760 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1762 if (wPreferences.scrollable_menus) {
1763 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1764 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1766 if (scr->flags.jump_back_pending ||
1767 p.x <= (rect.pos.x + 1) ||
1768 p.x >= (rect.pos.x + rect.size.width - 2) ||
1769 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1770 WMenu *menu;
1772 menu = wMenuUnderPointer(scr);
1773 if (menu != NULL)
1774 wMenuScroll(menu, event);
1779 static void handleVisibilityNotify(XEvent * event)
1781 WWindow *wwin;
1783 wwin = wWindowFor(event->xvisibility.window);
1784 if (!wwin)
1785 return;
1786 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);