495d2b4619dd9899f4c79c0e13c66b8d3f445fda
[wmaker-crm.git] / src / event.c
blob495d2b4619dd9899f4c79c0e13c66b8d3f445fda
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 XContext wWinContext;
81 extern XContext wVEdgeContext;
83 extern Cursor wCursor[WCUR_LAST];
85 extern WShortKey wKeyBindings[WKBD_LAST];
86 extern int wScreenCount;
87 extern Time LastTimestamp;
88 extern Time LastFocusChange;
90 #define MOD_MASK wPreferences.modifier_mask
92 extern Atom _XA_WM_COLORMAP_NOTIFY;
94 extern Atom _XA_WM_CHANGE_STATE;
95 extern Atom _XA_WM_DELETE_WINDOW;
96 extern Atom _XA_GNUSTEP_WM_ATTR;
97 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
98 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
99 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
100 extern Atom _XA_WINDOWMAKER_COMMAND;
101 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
103 #ifdef SHAPE
104 extern Bool wShapeSupported;
105 extern int wShapeEventBase;
106 #endif
108 #ifdef KEEP_XKB_LOCK_STATUS
109 extern int wXkbEventBase;
110 #endif
112 /************ Local stuff ***********/
114 static void saveTimestamp(XEvent *event);
115 static void handleColormapNotify(XEvent *event);
116 static void handleMapNotify(XEvent *event);
117 static void handleUnmapNotify(XEvent *event);
118 static void handleButtonPress(XEvent *event);
119 static void handleExpose(XEvent *event);
120 static void handleDestroyNotify(XEvent *event);
121 static void handleConfigureRequest(XEvent *event);
122 static void handleMapRequest(XEvent *event);
123 static void handlePropertyNotify(XEvent *event);
124 static void handleEnterNotify(XEvent *event);
125 static void handleLeaveNotify(XEvent *event);
126 static void handleExtensions(XEvent *event);
127 static void handleClientMessage(XEvent *event);
128 static void handleKeyPress(XEvent *event);
129 static void handleFocusIn(XEvent *event);
130 static void handleMotionNotify(XEvent *event);
131 static void handleVisibilityNotify(XEvent *event);
132 static void handle_inotify_events(int fd, int wd);
133 static void wdelete_death_handler(WMagicNumber id);
136 #ifdef SHAPE
137 static void handleShapeNotify(XEvent *event);
138 #endif
140 #ifdef KEEP_XKB_LOCK_STATUS
141 static void handleXkbIndicatorStateNotify(XEvent *event);
142 #endif
144 /* called from the signal handler */
145 void NotifyDeadProcess(pid_t pid, unsigned char status);
147 /* real dead process handler */
148 static void handleDeadProcess(void *foo);
150 typedef struct DeadProcesses {
151 pid_t pid;
152 unsigned char exit_status;
153 } DeadProcesses;
155 /* stack of dead processes */
156 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
157 static int deadProcessPtr = 0;
159 typedef struct DeathHandler {
160 WDeathHandler *callback;
161 pid_t pid;
162 void *client_data;
163 } DeathHandler;
165 static WMArray *deathHandlers = NULL;
167 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
169 DeathHandler *handler;
171 handler = malloc(sizeof(DeathHandler));
172 if (!handler)
173 return 0;
175 handler->pid = pid;
176 handler->callback = callback;
177 handler->client_data = cdata;
179 if (!deathHandlers)
180 deathHandlers = WMCreateArrayWithDestructor(8, free);
182 WMAddToArray(deathHandlers, handler);
184 return handler;
187 static void wdelete_death_handler(WMagicNumber id)
189 DeathHandler *handler = (DeathHandler *) id;
191 if (!handler || !deathHandlers)
192 return;
194 /* array destructor will call free(handler) */
195 WMRemoveFromArray(deathHandlers, handler);
198 void DispatchEvent(XEvent * event)
200 if (deathHandlers)
201 handleDeadProcess(NULL);
203 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
204 WCHANGE_STATE(WSTATE_EXITING);
205 /* received SIGTERM */
207 * WMHandleEvent() can't be called from anything
208 * executed inside here, or we can get in a infinite
209 * recursive loop.
211 Shutdown(WSExitMode);
213 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
214 WCHANGE_STATE(WSTATE_RESTARTING);
216 Shutdown(WSRestartPreparationMode);
217 /* received SIGHUP */
218 Restart(NULL, True);
219 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
220 WCHANGE_STATE(WSTATE_NORMAL);
221 wDefaultsCheckDomains(NULL);
224 /* for the case that all that is wanted to be dispatched is
225 * the stuff above */
226 if (!event)
227 return;
229 saveTimestamp(event);
230 switch (event->type) {
231 case MapRequest:
232 handleMapRequest(event);
233 break;
235 case KeyPress:
236 handleKeyPress(event);
237 break;
239 case MotionNotify:
240 handleMotionNotify(event);
241 break;
243 case ConfigureRequest:
244 handleConfigureRequest(event);
245 break;
247 case DestroyNotify:
248 handleDestroyNotify(event);
249 break;
251 case MapNotify:
252 handleMapNotify(event);
253 break;
255 case UnmapNotify:
256 handleUnmapNotify(event);
257 break;
259 case ButtonPress:
260 handleButtonPress(event);
261 break;
263 case Expose:
264 handleExpose(event);
265 break;
267 case PropertyNotify:
268 handlePropertyNotify(event);
269 break;
271 case EnterNotify:
272 handleEnterNotify(event);
273 break;
275 case LeaveNotify:
276 handleLeaveNotify(event);
277 break;
279 case ClientMessage:
280 handleClientMessage(event);
281 break;
283 case ColormapNotify:
284 handleColormapNotify(event);
285 break;
287 case MappingNotify:
288 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
289 XRefreshKeyboardMapping(&event->xmapping);
290 break;
292 case FocusIn:
293 handleFocusIn(event);
294 break;
296 case VisibilityNotify:
297 handleVisibilityNotify(event);
298 break;
300 case ConfigureNotify:
301 #ifdef HAVE_XRANDR
302 if (event->xconfigure.window == DefaultRootWindow(dpy))
303 XRRUpdateConfiguration(event);
304 #endif
305 break;
307 default:
308 handleExtensions(event);
309 break;
313 #ifdef HAVE_INOTIFY
315 *----------------------------------------------------------------------
316 * handle_inotify_events-
317 * Check for inotify events
319 * Returns:
320 * After reading events for the given file descriptor (fd) and
321 * watch descriptor (wd)
323 * Side effects:
324 * Calls wDefaultsCheckDomains if config database is updated
325 *----------------------------------------------------------------------
327 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
328 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
329 static void handle_inotify_events(int fd, int wd)
331 ssize_t eventQLength, i = 0;
332 char buff[BUFF_SIZE] = { 0 };
333 /* Check config only once per read of the event queue */
334 int oneShotFlag = 0;
337 * Read off the queued events
338 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
339 * not occur; the block is on Xevents, but a config file change will normally
340 * occur as a result of an Xevent - so the event queue should never have more than
341 * a few entries before a read().
343 eventQLength = read(fd, buff, BUFF_SIZE);
345 /* check what events occured */
346 /* Should really check wd here too, but for now we only have one watch! */
347 while (i < eventQLength) {
348 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
351 * see inotify.h for event types.
353 if (pevent->mask & IN_DELETE_SELF) {
354 wwarning(_("the defaults database has been deleted!"
355 " Restart Window Maker to create the database" " with the default settings"));
356 close(fd);
358 if (pevent->mask & IN_UNMOUNT) {
359 wwarning(_("the unit containing the defaults database has"
360 " been unmounted. Setting --static mode." " Any changes will not be saved."));
361 close(fd);
362 wPreferences.flags.noupdates = 1;
364 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
365 wwarning(_("Inotify: Reading config files in defaults database."));
366 wDefaultsCheckDomains(NULL);
369 /* move to next event in the buffer */
370 i += sizeof(struct inotify_event) + pevent->len;
373 #endif /* HAVE_INOTIFY */
376 *----------------------------------------------------------------------
377 * EventLoop-
378 * Processes X and internal events indefinitely.
380 * Returns:
381 * Never returns
383 * Side effects:
384 * The LastTimestamp global variable is updated.
385 * Calls inotifyGetEvents if defaults database changes.
386 *----------------------------------------------------------------------
388 noreturn void EventLoop(void)
390 XEvent event;
391 #ifdef HAVE_INOTIFY
392 extern int inotifyFD;
393 extern int inotifyWD;
394 struct timeval time;
395 fd_set rfds;
396 int retVal = 0;
398 if (inotifyFD < 0 || inotifyWD < 0)
399 retVal = -1;
400 #endif
402 for (;;) {
404 WMNextEvent(dpy, &event); /* Blocks here */
405 WMHandleEvent(&event);
406 #ifdef HAVE_INOTIFY
407 if (retVal != -1) {
408 time.tv_sec = 0;
409 time.tv_usec = 0;
410 FD_ZERO(&rfds);
411 FD_SET(inotifyFD, &rfds);
413 /* check for available read data from inotify - don't block! */
414 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
416 if (retVal < 0) { /* an error has occured */
417 wwarning(_("select failed. The inotify instance will be closed."
418 " Changes to the defaults database will require"
419 " a restart to take effect."));
420 close(inotifyFD);
421 continue;
423 if (FD_ISSET(inotifyFD, &rfds))
424 handle_inotify_events(inotifyFD, inotifyWD);
426 #endif
431 *----------------------------------------------------------------------
432 * ProcessPendingEvents --
433 * Processes the events that are currently pending (at the time
434 * this function is called) in the display's queue.
436 * Returns:
437 * After the pending events that were present at the function call
438 * are processed.
440 * Side effects:
441 * Many -- whatever handling events may involve.
443 *----------------------------------------------------------------------
445 void ProcessPendingEvents(void)
447 XEvent event;
448 int count;
450 XSync(dpy, False);
452 /* Take a snapshot of the event count in the queue */
453 count = XPending(dpy);
455 while (count > 0 && XPending(dpy)) {
456 WMNextEvent(dpy, &event);
457 WMHandleEvent(&event);
458 count--;
462 Bool IsDoubleClick(WScreen * scr, XEvent * event)
464 if ((scr->last_click_time > 0) &&
465 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
466 && (event->xbutton.button == scr->last_click_button)
467 && (event->xbutton.window == scr->last_click_window)) {
469 scr->flags.next_click_is_not_double = 1;
470 scr->last_click_time = 0;
471 scr->last_click_window = event->xbutton.window;
473 return True;
475 return False;
478 void NotifyDeadProcess(pid_t pid, unsigned char status)
480 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
481 wwarning("stack overflow: too many dead processes");
482 return;
484 /* stack the process to be handled later,
485 * as this is called from the signal handler */
486 deadProcesses[deadProcessPtr].pid = pid;
487 deadProcesses[deadProcessPtr].exit_status = status;
488 deadProcessPtr++;
491 static void handleDeadProcess(void *foo)
493 DeathHandler *tmp;
494 int i;
496 for (i = 0; i < deadProcessPtr; i++) {
497 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
500 if (!deathHandlers) {
501 deadProcessPtr = 0;
502 return;
505 /* get the pids on the queue and call handlers */
506 while (deadProcessPtr > 0) {
507 deadProcessPtr--;
509 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
510 tmp = WMGetFromArray(deathHandlers, i);
511 if (!tmp)
512 continue;
514 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
515 (*tmp->callback) (tmp->pid,
516 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
517 wdelete_death_handler(tmp);
523 static void saveTimestamp(XEvent * event)
526 * Never save CurrentTime as LastTimestamp because CurrentTime
527 * it's not a real timestamp (it's the 0L constant)
530 switch (event->type) {
531 case ButtonRelease:
532 case ButtonPress:
533 LastTimestamp = event->xbutton.time;
534 break;
535 case KeyPress:
536 case KeyRelease:
537 LastTimestamp = event->xkey.time;
538 break;
539 case MotionNotify:
540 LastTimestamp = event->xmotion.time;
541 break;
542 case PropertyNotify:
543 LastTimestamp = event->xproperty.time;
544 break;
545 case EnterNotify:
546 case LeaveNotify:
547 LastTimestamp = event->xcrossing.time;
548 break;
549 case SelectionClear:
550 LastTimestamp = event->xselectionclear.time;
551 break;
552 case SelectionRequest:
553 LastTimestamp = event->xselectionrequest.time;
554 break;
555 case SelectionNotify:
556 LastTimestamp = event->xselection.time;
557 #ifdef XDND
558 wXDNDProcessSelection(event);
559 #endif
560 break;
564 static int matchWindow(const void *item, const void *cdata)
566 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
569 static void handleExtensions(XEvent * event)
571 #ifdef KEEP_XKB_LOCK_STATUS
572 XkbEvent *xkbevent;
573 xkbevent = (XkbEvent *) event;
574 #endif /*KEEP_XKB_LOCK_STATUS */
575 #ifdef SHAPE
576 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
577 handleShapeNotify(event);
579 #endif
580 #ifdef KEEP_XKB_LOCK_STATUS
581 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
582 handleXkbIndicatorStateNotify(event);
584 #endif /*KEEP_XKB_LOCK_STATUS */
585 #ifdef HAVE_XRANDR
586 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
587 /* From xrandr man page: "Clients must call back into Xlib using
588 * XRRUpdateConfiguration when screen configuration change notify
589 * events are generated */
590 XRRUpdateConfiguration(event);
591 WCHANGE_STATE(WSTATE_RESTARTING);
592 Shutdown(WSRestartPreparationMode);
593 Restart(NULL,True);
595 #endif
598 static void handleMapRequest(XEvent * ev)
600 WWindow *wwin;
601 WScreen *scr = NULL;
602 Window window = ev->xmaprequest.window;
604 if ((wwin = wWindowFor(window))) {
605 if (wwin->flags.shaded) {
606 wUnshadeWindow(wwin);
608 /* deiconify window */
609 if (wwin->flags.miniaturized) {
610 wDeiconifyWindow(wwin);
611 } else if (wwin->flags.hidden) {
612 WApplication *wapp = wApplicationOf(wwin->main_window);
613 /* go to the last workspace that the user worked on the app */
614 if (wapp) {
615 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
617 wUnhideApplication(wapp, False, False);
619 return;
622 scr = wScreenForRootWindow(ev->xmaprequest.parent);
624 wwin = wManageWindow(scr, window);
627 * This is to let the Dock know that the application it launched
628 * has already been mapped (eg: it has finished launching).
629 * It is not necessary for normally docked apps, but is needed for
630 * apps that were forcedly docked (like with dockit).
632 if (scr->last_dock) {
633 if (wwin && wwin->main_window != None && wwin->main_window != window)
634 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
635 else
636 wDockTrackWindowLaunch(scr->last_dock, window);
639 if (wwin) {
640 wClientSetState(wwin, NormalState, None);
641 if (wwin->flags.maximized) {
642 wMaximizeWindow(wwin, wwin->flags.maximized);
644 if (wwin->flags.shaded) {
645 wwin->flags.shaded = 0;
646 wwin->flags.skip_next_animation = 1;
647 wShadeWindow(wwin);
649 if (wwin->flags.miniaturized) {
650 wwin->flags.miniaturized = 0;
651 wwin->flags.skip_next_animation = 1;
652 wIconifyWindow(wwin);
654 if (wwin->flags.fullscreen) {
655 wwin->flags.fullscreen = 0;
656 wFullscreenWindow(wwin);
658 if (wwin->flags.hidden) {
659 WApplication *wapp = wApplicationOf(wwin->main_window);
661 wwin->flags.hidden = 0;
662 wwin->flags.skip_next_animation = 1;
663 if (wapp) {
664 wHideApplication(wapp);
670 static void handleDestroyNotify(XEvent * event)
672 WWindow *wwin;
673 WApplication *app;
674 Window window = event->xdestroywindow.window;
675 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
676 int widx;
678 wwin = wWindowFor(window);
679 if (wwin) {
680 wUnmanageWindow(wwin, False, True);
683 if (scr != NULL) {
684 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
685 WFakeGroupLeader *fPtr;
687 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
688 if (fPtr->retainCount > 0) {
689 fPtr->retainCount--;
690 if (fPtr->retainCount == 0 && fPtr->leader != None) {
691 XDestroyWindow(dpy, fPtr->leader);
692 fPtr->leader = None;
693 XFlush(dpy);
696 fPtr->origLeader = None;
700 app = wApplicationOf(window);
701 if (app) {
702 if (window == app->main_window) {
703 app->refcount = 0;
704 wwin = app->main_window_desc->screen_ptr->focused_window;
705 while (wwin) {
706 if (wwin->main_window == window) {
707 wwin->main_window = None;
709 wwin = wwin->prev;
712 wApplicationDestroy(app);
716 static void handleExpose(XEvent * event)
718 WObjDescriptor *desc;
719 XEvent ev;
721 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
723 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
724 return;
727 if (desc->handle_expose) {
728 (*desc->handle_expose) (desc, event);
732 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
734 switch (action) {
735 case WA_SELECT_WINDOWS:
736 wUnselectWindows(scr);
737 wSelectWindows(scr, event);
738 break;
739 case WA_OPEN_APPMENU:
740 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
741 /* ugly hack */
742 if (scr->root_menu) {
743 if (scr->root_menu->brother->flags.mapped)
744 event->xbutton.window = scr->root_menu->brother->frame->core->window;
745 else
746 event->xbutton.window = scr->root_menu->frame->core->window;
748 break;
749 case WA_OPEN_WINLISTMENU:
750 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
751 if (scr->switch_menu) {
752 if (scr->switch_menu->brother->flags.mapped)
753 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
754 else
755 event->xbutton.window = scr->switch_menu->frame->core->window;
757 break;
758 default:
759 break;
763 /* bindable */
764 static void handleButtonPress(XEvent * event)
766 WObjDescriptor *desc;
767 WScreen *scr;
769 scr = wScreenForRootWindow(event->xbutton.root);
771 #ifdef BALLOON_TEXT
772 wBalloonHide(scr);
773 #endif
775 if (event->xbutton.window == scr->root_win) {
776 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
777 executeButtonAction(scr, event, wPreferences.mouse_button1);
778 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
779 executeButtonAction(scr, event, wPreferences.mouse_button2);
780 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
781 executeButtonAction(scr, event, wPreferences.mouse_button3);
782 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
783 wWorkspaceRelativeChange(scr, 1);
784 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
785 wWorkspaceRelativeChange(scr, -1);
789 desc = NULL;
790 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
791 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
792 return;
796 if (desc->parent_type == WCLASS_WINDOW) {
797 XSync(dpy, 0);
799 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
800 XAllowEvents(dpy, AsyncPointer, CurrentTime);
801 } else {
802 /* if (wPreferences.focus_mode == WKF_CLICK) { */
803 if (wPreferences.ignore_focus_click) {
804 XAllowEvents(dpy, AsyncPointer, CurrentTime);
806 XAllowEvents(dpy, ReplayPointer, CurrentTime);
807 /* } */
809 XSync(dpy, 0);
810 } else if (desc->parent_type == WCLASS_APPICON
811 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
812 if (event->xbutton.state & MOD_MASK) {
813 XSync(dpy, 0);
814 XAllowEvents(dpy, AsyncPointer, CurrentTime);
815 XSync(dpy, 0);
819 if (desc->handle_mousedown != NULL) {
820 (*desc->handle_mousedown) (desc, event);
823 /* save double-click information */
824 if (scr->flags.next_click_is_not_double) {
825 scr->flags.next_click_is_not_double = 0;
826 } else {
827 scr->last_click_time = event->xbutton.time;
828 scr->last_click_button = event->xbutton.button;
829 scr->last_click_window = event->xbutton.window;
833 static void handleMapNotify(XEvent * event)
835 WWindow *wwin;
837 wwin = wWindowFor(event->xmap.event);
838 if (wwin && wwin->client_win == event->xmap.event) {
839 if (wwin->flags.miniaturized) {
840 wDeiconifyWindow(wwin);
841 } else {
842 XGrabServer(dpy);
843 wWindowMap(wwin);
844 wClientSetState(wwin, NormalState, None);
845 XUngrabServer(dpy);
850 static void handleUnmapNotify(XEvent * event)
852 WWindow *wwin;
853 XEvent ev;
854 Bool withdraw = False;
856 /* only process windows with StructureNotify selected
857 * (ignore SubstructureNotify) */
858 wwin = wWindowFor(event->xunmap.window);
859 if (!wwin)
860 return;
862 /* whether the event is a Withdrawal request */
863 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
864 withdraw = True;
866 if (wwin->client_win != event->xunmap.event && !withdraw)
867 return;
869 if (!wwin->flags.mapped && !withdraw
870 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
871 && !wwin->flags.miniaturized && !wwin->flags.hidden)
872 return;
874 XGrabServer(dpy);
875 XUnmapWindow(dpy, wwin->frame->core->window);
876 wwin->flags.mapped = 0;
877 XSync(dpy, 0);
878 /* check if the window was destroyed */
879 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
880 DispatchEvent(&ev);
881 } else {
882 Bool reparented = False;
884 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
885 reparented = True;
887 /* withdraw window */
888 wwin->flags.mapped = 0;
889 if (!reparented)
890 wClientSetState(wwin, WithdrawnState, None);
892 /* if the window was reparented, do not reparent it back to the
893 * root window */
894 wUnmanageWindow(wwin, !reparented, False);
896 XUngrabServer(dpy);
899 static void handleConfigureRequest(XEvent * event)
901 WWindow *wwin;
903 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
905 * Configure request for unmapped window
907 wClientConfigure(NULL, &(event->xconfigurerequest));
908 } else {
909 wClientConfigure(wwin, &(event->xconfigurerequest));
913 static void handlePropertyNotify(XEvent * event)
915 WWindow *wwin;
916 WApplication *wapp;
917 Window jr;
918 int ji;
919 unsigned int ju;
921 wwin = wWindowFor(event->xproperty.window);
922 if (wwin) {
923 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
924 return;
926 wClientCheckProperty(wwin, &event->xproperty);
928 wapp = wApplicationOf(event->xproperty.window);
929 if (wapp) {
930 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
934 static void handleClientMessage(XEvent * event)
936 WWindow *wwin;
937 WObjDescriptor *desc;
939 /* handle transition from Normal to Iconic state */
940 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
941 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
943 wwin = wWindowFor(event->xclient.window);
944 if (!wwin)
945 return;
946 if (!wwin->flags.miniaturized)
947 wIconifyWindow(wwin);
948 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
949 WScreen *scr = wScreenForRootWindow(event->xclient.window);
951 if (!scr)
952 return;
954 if (event->xclient.data.l[1] == 1) { /* starting */
955 wColormapAllowClientInstallation(scr, True);
956 } else { /* stopping */
957 wColormapAllowClientInstallation(scr, False);
959 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
961 char *command;
962 size_t len;
964 len = sizeof(event->xclient.data.b) + 1;
965 command = wmalloc(len);
966 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
968 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
969 wwarning(_("Got Reconfigure command"));
970 wDefaultsCheckDomains(NULL);
971 } else {
972 wwarning(_("Got unknown command %s"), command);
975 wfree(command);
977 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
978 WApplication *wapp;
979 int done = 0;
980 wapp = wApplicationOf(event->xclient.window);
981 if (wapp) {
982 switch (event->xclient.data.l[0]) {
983 case WMFHideOtherApplications:
984 wHideOtherApplications(wapp->main_window_desc);
985 done = 1;
986 break;
988 case WMFHideApplication:
989 wHideApplication(wapp);
990 done = 1;
991 break;
994 if (!done) {
995 wwin = wWindowFor(event->xclient.window);
996 if (wwin) {
997 switch (event->xclient.data.l[0]) {
998 case WMFHideOtherApplications:
999 wHideOtherApplications(wwin);
1000 break;
1002 case WMFHideApplication:
1003 wHideApplication(wApplicationOf(wwin->main_window));
1004 break;
1008 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1009 wwin = wWindowFor(event->xclient.window);
1010 if (!wwin)
1011 return;
1012 switch (event->xclient.data.l[0]) {
1013 case GSWindowLevelAttr:
1015 int level = (int)event->xclient.data.l[1];
1017 if (WINDOW_LEVEL(wwin) != level) {
1018 ChangeStackingLevel(wwin->frame->core, level);
1021 break;
1023 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1024 wwin = wWindowFor(event->xclient.window);
1025 if (!wwin)
1026 return;
1027 switch (event->xclient.data.l[0]) {
1028 case WMTitleBarNormal:
1029 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1030 break;
1031 case WMTitleBarMain:
1032 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1033 break;
1034 case WMTitleBarKey:
1035 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1036 break;
1038 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1039 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1040 if (!scr)
1041 return;
1042 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1043 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1044 /* do nothing */
1045 #ifdef XDND
1046 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1047 /* do nothing */
1048 #endif /* XDND */
1049 } else {
1051 * Non-standard thing, but needed by OffiX DND.
1052 * For when the icon frame gets a ClientMessage
1053 * that should have gone to the icon_window.
1055 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1056 struct WIcon *icon = NULL;
1058 if (desc->parent_type == WCLASS_MINIWINDOW) {
1059 icon = (WIcon *) desc->parent;
1060 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1061 icon = ((WAppIcon *) desc->parent)->icon;
1063 if (icon && (wwin = icon->owner)) {
1064 if (wwin->client_win != event->xclient.window) {
1065 event->xclient.window = wwin->client_win;
1066 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1073 static void raiseWindow(WScreen * scr)
1075 WWindow *wwin;
1077 scr->autoRaiseTimer = NULL;
1079 wwin = wWindowFor(scr->autoRaiseWindow);
1080 if (!wwin)
1081 return;
1083 if (!wwin->flags.destroyed && wwin->flags.focused) {
1084 wRaiseFrame(wwin->frame->core);
1085 /* this is needed or a race condition will occur */
1086 XSync(dpy, False);
1090 static void handleEnterNotify(XEvent * event)
1092 WWindow *wwin;
1093 WObjDescriptor *desc = NULL;
1094 XEvent ev;
1095 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1097 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1098 /* already left the window... */
1099 saveTimestamp(&ev);
1100 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1101 return;
1105 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1106 if (desc->handle_enternotify)
1107 (*desc->handle_enternotify) (desc, event);
1110 /* enter to window */
1111 wwin = wWindowFor(event->xcrossing.window);
1112 if (!wwin) {
1113 if (wPreferences.colormap_mode == WCM_POINTER) {
1114 wColormapInstallForWindow(scr, NULL);
1116 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1117 WMDeleteTimerHandler(scr->autoRaiseTimer);
1118 scr->autoRaiseTimer = NULL;
1120 } else {
1121 /* set auto raise timer even if in focus-follows-mouse mode
1122 * and the event is for the frame window, even if the window
1123 * has focus already. useful if you move the pointer from a focused
1124 * window to the root window and back pretty fast
1126 * set focus if in focus-follows-mouse mode and the event
1127 * is for the frame window and window doesn't have focus yet */
1128 if (wPreferences.focus_mode == WKF_SLOPPY
1129 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1131 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1132 wSetFocusTo(scr, wwin);
1134 if (scr->autoRaiseTimer)
1135 WMDeleteTimerHandler(scr->autoRaiseTimer);
1136 scr->autoRaiseTimer = NULL;
1138 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1139 scr->autoRaiseWindow = wwin->frame->core->window;
1140 scr->autoRaiseTimer
1141 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1144 /* Install colormap for window, if the colormap installation mode
1145 * is colormap_follows_mouse */
1146 if (wPreferences.colormap_mode == WCM_POINTER) {
1147 if (wwin->client_win == event->xcrossing.window)
1148 wColormapInstallForWindow(scr, wwin);
1149 else
1150 wColormapInstallForWindow(scr, NULL);
1154 if (event->xcrossing.window == event->xcrossing.root
1155 && event->xcrossing.detail == NotifyNormal
1156 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1158 wSetFocusTo(scr, scr->focused_window);
1160 #ifdef BALLOON_TEXT
1161 wBalloonEnteredObject(scr, desc);
1162 #endif
1165 static void handleLeaveNotify(XEvent * event)
1167 WObjDescriptor *desc = NULL;
1169 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1170 if (desc->handle_leavenotify)
1171 (*desc->handle_leavenotify) (desc, event);
1175 #ifdef SHAPE
1176 static void handleShapeNotify(XEvent * event)
1178 XShapeEvent *shev = (XShapeEvent *) event;
1179 WWindow *wwin;
1180 union {
1181 XEvent xevent;
1182 XShapeEvent xshape;
1183 } ev;
1185 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1186 if (ev.xshape.kind == ShapeBounding) {
1187 if (ev.xshape.shaped == shev->shaped) {
1188 *shev = ev.xshape;
1189 } else {
1190 XPutBackEvent(dpy, &ev.xevent);
1191 break;
1196 wwin = wWindowFor(shev->window);
1197 if (!wwin || shev->kind != ShapeBounding)
1198 return;
1200 if (!shev->shaped && wwin->flags.shaped) {
1202 wwin->flags.shaped = 0;
1203 wWindowClearShape(wwin);
1205 } else if (shev->shaped) {
1207 wwin->flags.shaped = 1;
1208 wWindowSetShape(wwin);
1211 #endif /* SHAPE */
1213 #ifdef KEEP_XKB_LOCK_STATUS
1214 /* please help ]d if you know what to do */
1215 static void handleXkbIndicatorStateNotify(XEvent *event)
1217 WWindow *wwin;
1218 WScreen *scr;
1219 XkbStateRec staterec;
1220 int i;
1222 for (i = 0; i < wScreenCount; i++) {
1223 scr = wScreenWithNumber(i);
1224 wwin = scr->focused_window;
1225 if (wwin && wwin->flags.focused) {
1226 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1227 if (wwin->frame->languagemode != staterec.group) {
1228 wwin->frame->last_languagemode = wwin->frame->languagemode;
1229 wwin->frame->languagemode = staterec.group;
1231 #ifdef XKB_BUTTON_HINT
1232 if (wwin->frame->titlebar) {
1233 wFrameWindowPaint(wwin->frame);
1235 #endif
1239 #endif /*KEEP_XKB_LOCK_STATUS */
1241 static void handleColormapNotify(XEvent * event)
1243 WWindow *wwin;
1244 WScreen *scr;
1245 Bool reinstall = False;
1247 wwin = wWindowFor(event->xcolormap.window);
1248 if (!wwin)
1249 return;
1251 scr = wwin->screen_ptr;
1253 do {
1254 if (wwin) {
1255 if (event->xcolormap.new) {
1256 XWindowAttributes attr;
1258 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1260 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1261 scr->current_colormap = attr.colormap;
1263 reinstall = True;
1264 } else if (event->xcolormap.state == ColormapUninstalled &&
1265 scr->current_colormap == event->xcolormap.colormap) {
1267 /* some bastard app (like XV) removed our colormap */
1269 * can't enforce or things like xscreensaver wont work
1270 * reinstall = True;
1272 } else if (event->xcolormap.state == ColormapInstalled &&
1273 scr->current_colormap == event->xcolormap.colormap) {
1275 /* someone has put our colormap back */
1276 reinstall = False;
1279 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1280 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1282 if (reinstall && scr->current_colormap != None) {
1283 if (!scr->flags.colormap_stuff_blocked)
1284 XInstallColormap(dpy, scr->current_colormap);
1288 static void handleFocusIn(XEvent * event)
1290 WWindow *wwin;
1293 * For applications that like stealing the focus.
1295 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1296 saveTimestamp(event);
1297 if (event->xfocus.mode == NotifyUngrab
1298 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1299 return;
1302 wwin = wWindowFor(event->xfocus.window);
1303 if (wwin && !wwin->flags.focused) {
1304 if (wwin->flags.mapped)
1305 wSetFocusTo(wwin->screen_ptr, wwin);
1306 else
1307 wSetFocusTo(wwin->screen_ptr, NULL);
1308 } else if (!wwin) {
1309 WScreen *scr = wScreenForWindow(event->xfocus.window);
1310 if (scr)
1311 wSetFocusTo(scr, NULL);
1315 static WWindow *windowUnderPointer(WScreen * scr)
1317 unsigned int mask;
1318 int foo;
1319 Window bar, win;
1321 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1322 return wWindowFor(win);
1323 return NULL;
1326 static int CheckFullScreenWindowFocused(WScreen * scr)
1328 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1329 return 1;
1330 else
1331 return 0;
1334 static void handleKeyPress(XEvent * event)
1336 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1337 WWindow *wwin = scr->focused_window;
1338 short i, widx;
1339 int modifiers;
1340 int command = -1;
1341 #ifdef KEEP_XKB_LOCK_STATUS
1342 XkbStateRec staterec;
1343 #endif /*KEEP_XKB_LOCK_STATUS */
1345 /* ignore CapsLock */
1346 modifiers = event->xkey.state & ValidModMask;
1348 for (i = 0; i < WKBD_LAST; i++) {
1349 if (wKeyBindings[i].keycode == 0)
1350 continue;
1352 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1353 || */ wKeyBindings[i].modifier ==
1354 modifiers)) {
1355 command = i;
1356 break;
1360 if (command < 0) {
1362 if (!wRootMenuPerformShortcut(event)) {
1363 static int dontLoop = 0;
1365 if (dontLoop > 10) {
1366 wwarning("problem with key event processing code");
1367 return;
1369 dontLoop++;
1370 /* if the focused window is an internal window, try redispatching
1371 * the event to the managed window, as it can be a WINGs window */
1372 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1373 /* client_leader contains the WINGs toplevel */
1374 event->xany.window = wwin->client_leader;
1375 WMHandleEvent(event);
1377 dontLoop--;
1379 return;
1381 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1382 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1384 switch (command) {
1386 case WKBD_ROOTMENU:
1387 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1388 if (!CheckFullScreenWindowFocused(scr)) {
1389 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1390 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1391 True);
1393 break;
1394 case WKBD_WINDOWLIST:
1395 if (!CheckFullScreenWindowFocused(scr)) {
1396 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1397 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1398 True);
1400 break;
1402 case WKBD_WINDOWMENU:
1403 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1404 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1405 break;
1406 case WKBD_MINIMIZEALL:
1407 CloseWindowMenu(scr);
1408 wHideAll(scr);
1409 break;
1410 case WKBD_MINIATURIZE:
1411 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1412 && !WFLAGP(wwin, no_miniaturizable)) {
1413 CloseWindowMenu(scr);
1415 if (wwin->protocols.MINIATURIZE_WINDOW)
1416 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1417 else {
1418 wIconifyWindow(wwin);
1421 break;
1422 case WKBD_HIDE:
1423 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1424 WApplication *wapp = wApplicationOf(wwin->main_window);
1425 CloseWindowMenu(scr);
1427 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1428 wHideApplication(wapp);
1431 break;
1432 case WKBD_HIDE_OTHERS:
1433 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1434 CloseWindowMenu(scr);
1436 wHideOtherApplications(wwin);
1438 break;
1439 case WKBD_MAXIMIZE:
1440 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1441 CloseWindowMenu(scr);
1443 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1445 break;
1446 case WKBD_VMAXIMIZE:
1447 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1448 CloseWindowMenu(scr);
1450 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1452 break;
1453 case WKBD_HMAXIMIZE:
1454 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1455 CloseWindowMenu(scr);
1457 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1459 break;
1460 case WKBD_LHMAXIMIZE:
1461 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1462 CloseWindowMenu(scr);
1464 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1466 break;
1467 case WKBD_RHMAXIMIZE:
1468 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1469 CloseWindowMenu(scr);
1471 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1473 break;
1474 case WKBD_THMAXIMIZE:
1475 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1476 CloseWindowMenu(scr);
1478 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1480 break;
1481 case WKBD_BHMAXIMIZE:
1482 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1483 CloseWindowMenu(scr);
1485 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1487 break;
1488 case WKBD_LTCMAXIMIZE:
1489 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1490 CloseWindowMenu(scr);
1492 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1494 break;
1495 case WKBD_RTCMAXIMIZE:
1496 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1497 CloseWindowMenu(scr);
1499 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1501 break;
1502 case WKBD_LBCMAXIMIZE:
1503 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1504 CloseWindowMenu(scr);
1506 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1508 break;
1509 case WKBD_RBCMAXIMIZE:
1510 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1511 CloseWindowMenu(scr);
1513 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1515 break;
1516 case WKBD_MAXIMUS:
1517 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1518 CloseWindowMenu(scr);
1520 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1522 break;
1523 case WKBD_RAISE:
1524 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1525 CloseWindowMenu(scr);
1527 wRaiseFrame(wwin->frame->core);
1529 break;
1530 case WKBD_LOWER:
1531 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1532 CloseWindowMenu(scr);
1534 wLowerFrame(wwin->frame->core);
1536 break;
1537 case WKBD_RAISELOWER:
1538 /* raise or lower the window under the pointer, not the
1539 * focused one
1541 wwin = windowUnderPointer(scr);
1542 if (wwin)
1543 wRaiseLowerFrame(wwin->frame->core);
1544 break;
1545 case WKBD_SHADE:
1546 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1547 if (wwin->flags.shaded)
1548 wUnshadeWindow(wwin);
1549 else
1550 wShadeWindow(wwin);
1552 break;
1553 case WKBD_MOVERESIZE:
1554 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1555 CloseWindowMenu(scr);
1557 wKeyboardMoveResizeWindow(wwin);
1559 break;
1560 case WKBD_CLOSE:
1561 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1562 CloseWindowMenu(scr);
1563 if (wwin->protocols.DELETE_WINDOW)
1564 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1566 break;
1567 case WKBD_SELECT:
1568 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1569 wSelectWindow(wwin, !wwin->flags.selected);
1571 break;
1572 case WKBD_FOCUSNEXT:
1573 StartWindozeCycle(wwin, event, True, False);
1574 break;
1576 case WKBD_FOCUSPREV:
1577 StartWindozeCycle(wwin, event, False, False);
1578 break;
1580 case WKBD_GROUPNEXT:
1581 StartWindozeCycle(wwin, event, True, True);
1582 break;
1584 case WKBD_GROUPPREV:
1585 StartWindozeCycle(wwin, event, False, True);
1586 break;
1588 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1589 widx = command - WKBD_WORKSPACE1;
1590 i = (scr->current_workspace / 10) * 10 + widx;
1591 if (wPreferences.ws_advance || i < scr->workspace_count)
1592 wWorkspaceChange(scr, i);
1593 break;
1595 case WKBD_NEXTWORKSPACE:
1596 wWorkspaceRelativeChange(scr, 1);
1597 break;
1598 case WKBD_PREVWORKSPACE:
1599 wWorkspaceRelativeChange(scr, -1);
1600 break;
1601 case WKBD_LASTWORKSPACE:
1602 wWorkspaceChange(scr, scr->last_workspace);
1603 break;
1605 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1606 widx = command - WKBD_MOVE_WORKSPACE1;
1607 i = (scr->current_workspace / 10) * 10 + widx;
1608 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1609 wWindowChangeWorkspace(wwin, i);
1610 break;
1612 case WKBD_MOVE_NEXTWORKSPACE:
1613 if (wwin)
1614 wWindowChangeWorkspaceRelative(wwin, 1);
1615 break;
1616 case WKBD_MOVE_PREVWORKSPACE:
1617 if (wwin)
1618 wWindowChangeWorkspaceRelative(wwin, -1);
1619 break;
1620 case WKBD_MOVE_LASTWORKSPACE:
1621 if (wwin)
1622 wWindowChangeWorkspace(wwin, scr->last_workspace);
1623 break;
1625 case WKBD_MOVE_NEXTWSLAYER:
1626 case WKBD_MOVE_PREVWSLAYER:
1628 if (wwin) {
1629 int row, column;
1631 row = scr->current_workspace / 10;
1632 column = scr->current_workspace % 10;
1634 if (command == WKBD_MOVE_NEXTWSLAYER) {
1635 if ((row + 1) * 10 < scr->workspace_count)
1636 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1637 } else {
1638 if (row > 0)
1639 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1643 break;
1645 case WKBD_WINDOW1:
1646 case WKBD_WINDOW2:
1647 case WKBD_WINDOW3:
1648 case WKBD_WINDOW4:
1649 case WKBD_WINDOW5:
1650 case WKBD_WINDOW6:
1651 case WKBD_WINDOW7:
1652 case WKBD_WINDOW8:
1653 case WKBD_WINDOW9:
1654 case WKBD_WINDOW10:
1656 widx = command - WKBD_WINDOW1;
1658 if (scr->shortcutWindows[widx]) {
1659 WMArray *list = scr->shortcutWindows[widx];
1660 int cw;
1661 int count = WMGetArrayItemCount(list);
1662 WWindow *twin;
1663 WMArrayIterator iter;
1664 WWindow *wwin;
1666 wUnselectWindows(scr);
1667 cw = scr->current_workspace;
1669 WM_ETARETI_ARRAY(list, wwin, iter) {
1670 if (count > 1)
1671 wWindowChangeWorkspace(wwin, cw);
1673 wMakeWindowVisible(wwin);
1675 if (count > 1)
1676 wSelectWindow(wwin, True);
1679 /* rotate the order of windows, to create a cycling effect */
1680 twin = WMGetFromArray(list, 0);
1681 WMDeleteFromArray(list, 0);
1682 WMAddToArray(list, twin);
1684 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1685 if (scr->shortcutWindows[widx]) {
1686 WMFreeArray(scr->shortcutWindows[widx]);
1687 scr->shortcutWindows[widx] = NULL;
1690 if (wwin->flags.selected && scr->selected_windows) {
1691 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1692 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1693 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1694 } else {
1695 scr->shortcutWindows[widx] = WMCreateArray(4);
1696 WMAddToArray(scr->shortcutWindows[widx], wwin);
1699 wSelectWindow(wwin, !wwin->flags.selected);
1700 XFlush(dpy);
1701 wusleep(3000);
1702 wSelectWindow(wwin, !wwin->flags.selected);
1703 XFlush(dpy);
1705 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1707 if (wwin->flags.selected && scr->selected_windows) {
1708 if (scr->shortcutWindows[widx]) {
1709 WMFreeArray(scr->shortcutWindows[widx]);
1711 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1715 break;
1717 case WKBD_RELAUNCH:
1718 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1719 (void) RelaunchWindow(wwin);
1721 break;
1723 case WKBD_SWITCH_SCREEN:
1724 if (wScreenCount > 1) {
1725 WScreen *scr2;
1726 int i;
1728 /* find index of this screen */
1729 for (i = 0; i < wScreenCount; i++) {
1730 if (wScreenWithNumber(i) == scr)
1731 break;
1733 i++;
1734 if (i >= wScreenCount) {
1735 i = 0;
1737 scr2 = wScreenWithNumber(i);
1739 if (scr2) {
1740 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1741 scr2->scr_width / 2, scr2->scr_height / 2);
1744 break;
1746 case WKBD_NEXTWSLAYER:
1747 case WKBD_PREVWSLAYER:
1749 int row, column;
1751 row = scr->current_workspace / 10;
1752 column = scr->current_workspace % 10;
1754 if (command == WKBD_NEXTWSLAYER) {
1755 if ((row + 1) * 10 < scr->workspace_count)
1756 wWorkspaceChange(scr, column + (row + 1) * 10);
1757 } else {
1758 if (row > 0)
1759 wWorkspaceChange(scr, column + (row - 1) * 10);
1762 break;
1763 case WKBD_CLIPRAISELOWER:
1764 if (!wPreferences.flags.noclip)
1765 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1766 break;
1767 case WKBD_DOCKRAISELOWER:
1768 if (!wPreferences.flags.nodock)
1769 wDockRaiseLower(scr->dock);
1770 break;
1771 #ifdef KEEP_XKB_LOCK_STATUS
1772 case WKBD_TOGGLE:
1773 if (wPreferences.modelock) {
1774 /*toggle */
1775 wwin = scr->focused_window;
1777 if (wwin && wwin->flags.mapped
1778 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1779 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1780 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1782 wwin->frame->languagemode = wwin->frame->last_languagemode;
1783 wwin->frame->last_languagemode = staterec.group;
1784 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1788 break;
1789 #endif /* KEEP_XKB_LOCK_STATUS */
1793 static void handleMotionNotify(XEvent * event)
1795 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1797 if (wPreferences.scrollable_menus) {
1798 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1799 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1801 if (scr->flags.jump_back_pending ||
1802 p.x <= (rect.pos.x + 1) ||
1803 p.x >= (rect.pos.x + rect.size.width - 2) ||
1804 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1805 WMenu *menu;
1807 menu = wMenuUnderPointer(scr);
1808 if (menu != NULL)
1809 wMenuScroll(menu, event);
1814 static void handleVisibilityNotify(XEvent * event)
1816 WWindow *wwin;
1818 wwin = wWindowFor(event->xvisibility.window);
1819 if (!wwin)
1820 return;
1821 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);