Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / event.c
blob766492635f14da84e06014bebc947bcfa1b98c42
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 "funcs.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"
76 /******** Global Variables **********/
77 extern XContext wWinContext;
78 extern XContext wVEdgeContext;
80 extern Cursor wCursor[WCUR_LAST];
82 extern WShortKey wKeyBindings[WKBD_LAST];
83 extern int wScreenCount;
84 extern Time LastTimestamp;
85 extern Time LastFocusChange;
87 extern WPreferences wPreferences;
89 #define MOD_MASK wPreferences.modifier_mask
91 extern Atom _XA_WM_COLORMAP_NOTIFY;
93 extern Atom _XA_WM_CHANGE_STATE;
94 extern Atom _XA_WM_DELETE_WINDOW;
95 extern Atom _XA_GNUSTEP_WM_ATTR;
96 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
97 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
98 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
99 extern Atom _XA_WINDOWMAKER_COMMAND;
100 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
102 #ifdef SHAPE
103 extern Bool wShapeSupported;
104 extern int wShapeEventBase;
105 #endif
107 #ifdef KEEP_XKB_LOCK_STATUS
108 extern int wXkbEventBase;
109 #endif
111 /************ Local stuff ***********/
113 static void saveTimestamp(XEvent *event);
114 static void handleColormapNotify(XEvent *event);
115 static void handleMapNotify(XEvent *event);
116 static void handleUnmapNotify(XEvent *event);
117 static void handleButtonPress(XEvent *event);
118 static void handleExpose(XEvent *event);
119 static void handleDestroyNotify(XEvent *event);
120 static void handleConfigureRequest(XEvent *event);
121 static void handleMapRequest(XEvent *event);
122 static void handlePropertyNotify(XEvent *event);
123 static void handleEnterNotify(XEvent *event);
124 static void handleLeaveNotify(XEvent *event);
125 static void handleExtensions(XEvent *event);
126 static void handleClientMessage(XEvent *event);
127 static void handleKeyPress(XEvent *event);
128 static void handleFocusIn(XEvent *event);
129 static void handleMotionNotify(XEvent *event);
130 static void handleVisibilityNotify(XEvent *event);
131 static void handle_inotify_events(int fd, int wd);
132 static void wdelete_death_handler(WMagicNumber id);
135 #ifdef SHAPE
136 static void handleShapeNotify(XEvent *event);
137 #endif
139 #ifdef KEEP_XKB_LOCK_STATUS
140 static void handleXkbIndicatorStateNotify(XEvent *event);
141 #endif
143 /* called from the signal handler */
144 void NotifyDeadProcess(pid_t pid, unsigned char status);
146 /* real dead process handler */
147 static void handleDeadProcess(void *foo);
149 typedef struct DeadProcesses {
150 pid_t pid;
151 unsigned char exit_status;
152 } DeadProcesses;
154 /* stack of dead processes */
155 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
156 static int deadProcessPtr = 0;
158 typedef struct DeathHandler {
159 WDeathHandler *callback;
160 pid_t pid;
161 void *client_data;
162 } DeathHandler;
164 static WMArray *deathHandlers = NULL;
166 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
168 DeathHandler *handler;
170 handler = malloc(sizeof(DeathHandler));
171 if (!handler)
172 return 0;
174 handler->pid = pid;
175 handler->callback = callback;
176 handler->client_data = cdata;
178 if (!deathHandlers)
179 deathHandlers = WMCreateArrayWithDestructor(8, free);
181 WMAddToArray(deathHandlers, handler);
183 return handler;
186 static void wdelete_death_handler(WMagicNumber id)
188 DeathHandler *handler = (DeathHandler *) id;
190 if (!handler || !deathHandlers)
191 return;
193 /* array destructor will call free(handler) */
194 WMRemoveFromArray(deathHandlers, handler);
197 void DispatchEvent(XEvent * event)
199 if (deathHandlers)
200 handleDeadProcess(NULL);
202 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
203 WCHANGE_STATE(WSTATE_EXITING);
204 /* received SIGTERM */
206 * WMHandleEvent() can't be called from anything
207 * executed inside here, or we can get in a infinite
208 * recursive loop.
210 Shutdown(WSExitMode);
212 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
213 WCHANGE_STATE(WSTATE_RESTARTING);
215 Shutdown(WSRestartPreparationMode);
216 /* received SIGHUP */
217 Restart(NULL, True);
218 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
219 WCHANGE_STATE(WSTATE_NORMAL);
220 wDefaultsCheckDomains(NULL);
223 /* for the case that all that is wanted to be dispatched is
224 * the stuff above */
225 if (!event)
226 return;
228 saveTimestamp(event);
229 switch (event->type) {
230 case MapRequest:
231 handleMapRequest(event);
232 break;
234 case KeyPress:
235 handleKeyPress(event);
236 break;
238 case MotionNotify:
239 handleMotionNotify(event);
240 break;
242 case ConfigureRequest:
243 handleConfigureRequest(event);
244 break;
246 case DestroyNotify:
247 handleDestroyNotify(event);
248 break;
250 case MapNotify:
251 handleMapNotify(event);
252 break;
254 case UnmapNotify:
255 handleUnmapNotify(event);
256 break;
258 case ButtonPress:
259 handleButtonPress(event);
260 break;
262 case Expose:
263 handleExpose(event);
264 break;
266 case PropertyNotify:
267 handlePropertyNotify(event);
268 break;
270 case EnterNotify:
271 handleEnterNotify(event);
272 break;
274 case LeaveNotify:
275 handleLeaveNotify(event);
276 break;
278 case ClientMessage:
279 handleClientMessage(event);
280 break;
282 case ColormapNotify:
283 handleColormapNotify(event);
284 break;
286 case MappingNotify:
287 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
288 XRefreshKeyboardMapping(&event->xmapping);
289 break;
291 case FocusIn:
292 handleFocusIn(event);
293 break;
295 case VisibilityNotify:
296 handleVisibilityNotify(event);
297 break;
299 case ConfigureNotify:
300 #ifdef HAVE_XRANDR
301 if (event->xconfigure.window == DefaultRootWindow(dpy))
302 XRRUpdateConfiguration(event);
303 #endif
304 break;
306 default:
307 handleExtensions(event);
308 break;
312 #ifdef HAVE_INOTIFY
314 *----------------------------------------------------------------------
315 * handle_inotify_events-
316 * Check for inotify events
318 * Returns:
319 * After reading events for the given file descriptor (fd) and
320 * watch descriptor (wd)
322 * Side effects:
323 * Calls wDefaultsCheckDomains if config database is updated
324 *----------------------------------------------------------------------
326 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
327 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
328 static void handle_inotify_events(int fd, int wd)
330 ssize_t eventQLength, i = 0;
331 char buff[BUFF_SIZE] = { 0 };
332 /* Check config only once per read of the event queue */
333 int oneShotFlag = 0;
336 * Read off the queued events
337 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
338 * not occur; the block is on Xevents, but a config file change will normally
339 * occur as a result of an Xevent - so the event queue should never have more than
340 * a few entries before a read().
342 eventQLength = read(fd, buff, BUFF_SIZE);
344 /* check what events occured */
345 /* Should really check wd here too, but for now we only have one watch! */
346 while (i < eventQLength) {
347 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
350 * see inotify.h for event types.
352 if (pevent->mask & IN_DELETE_SELF) {
353 wwarning(_("the defaults database has been deleted!"
354 " Restart Window Maker to create the database" " with the default settings"));
355 close(fd);
357 if (pevent->mask & IN_UNMOUNT) {
358 wwarning(_("the unit containing the defaults database has"
359 " been unmounted. Setting --static mode." " Any changes will not be saved."));
360 close(fd);
361 wPreferences.flags.noupdates = 1;
363 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
364 wwarning(_("Inotify: Reading config files in defaults database."));
365 wDefaultsCheckDomains(NULL);
368 /* move to next event in the buffer */
369 i += sizeof(struct inotify_event) + pevent->len;
372 #endif /* HAVE_INOTIFY */
375 *----------------------------------------------------------------------
376 * EventLoop-
377 * Processes X and internal events indefinitely.
379 * Returns:
380 * Never returns
382 * Side effects:
383 * The LastTimestamp global variable is updated.
384 * Calls inotifyGetEvents if defaults database changes.
385 *----------------------------------------------------------------------
387 void EventLoop(void)
389 XEvent event;
390 #ifdef HAVE_INOTIFY
391 extern int inotifyFD;
392 extern int inotifyWD;
393 struct timeval time;
394 fd_set rfds;
395 int retVal = 0;
397 if (inotifyFD < 0 || inotifyWD < 0)
398 retVal = -1;
399 #endif
401 for (;;) {
403 WMNextEvent(dpy, &event); /* Blocks here */
404 WMHandleEvent(&event);
405 #ifdef HAVE_INOTIFY
406 if (retVal != -1) {
407 time.tv_sec = 0;
408 time.tv_usec = 0;
409 FD_ZERO(&rfds);
410 FD_SET(inotifyFD, &rfds);
412 /* check for available read data from inotify - don't block! */
413 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
415 if (retVal < 0) { /* an error has occured */
416 wwarning(_("select failed. The inotify instance will be closed."
417 " Changes to the defaults database will require"
418 " a restart to take effect."));
419 close(inotifyFD);
420 continue;
422 if (FD_ISSET(inotifyFD, &rfds))
423 handle_inotify_events(inotifyFD, inotifyWD);
425 #endif
430 *----------------------------------------------------------------------
431 * ProcessPendingEvents --
432 * Processes the events that are currently pending (at the time
433 * this function is called) in the display's queue.
435 * Returns:
436 * After the pending events that were present at the function call
437 * are processed.
439 * Side effects:
440 * Many -- whatever handling events may involve.
442 *----------------------------------------------------------------------
444 void ProcessPendingEvents(void)
446 XEvent event;
447 int count;
449 XSync(dpy, False);
451 /* Take a snapshot of the event count in the queue */
452 count = XPending(dpy);
454 while (count > 0 && XPending(dpy)) {
455 WMNextEvent(dpy, &event);
456 WMHandleEvent(&event);
457 count--;
461 Bool IsDoubleClick(WScreen * scr, XEvent * event)
463 if ((scr->last_click_time > 0) &&
464 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
465 && (event->xbutton.button == scr->last_click_button)
466 && (event->xbutton.window == scr->last_click_window)) {
468 scr->flags.next_click_is_not_double = 1;
469 scr->last_click_time = 0;
470 scr->last_click_window = event->xbutton.window;
472 return True;
474 return False;
477 void NotifyDeadProcess(pid_t pid, unsigned char status)
479 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
480 wwarning("stack overflow: too many dead processes");
481 return;
483 /* stack the process to be handled later,
484 * as this is called from the signal handler */
485 deadProcesses[deadProcessPtr].pid = pid;
486 deadProcesses[deadProcessPtr].exit_status = status;
487 deadProcessPtr++;
490 static void handleDeadProcess(void *foo)
492 DeathHandler *tmp;
493 int i;
495 for (i = 0; i < deadProcessPtr; i++) {
496 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
499 if (!deathHandlers) {
500 deadProcessPtr = 0;
501 return;
504 /* get the pids on the queue and call handlers */
505 while (deadProcessPtr > 0) {
506 deadProcessPtr--;
508 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
509 tmp = WMGetFromArray(deathHandlers, i);
510 if (!tmp)
511 continue;
513 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
514 (*tmp->callback) (tmp->pid,
515 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
516 wdelete_death_handler(tmp);
522 static void saveTimestamp(XEvent * event)
525 * Never save CurrentTime as LastTimestamp because CurrentTime
526 * it's not a real timestamp (it's the 0L constant)
529 switch (event->type) {
530 case ButtonRelease:
531 case ButtonPress:
532 LastTimestamp = event->xbutton.time;
533 break;
534 case KeyPress:
535 case KeyRelease:
536 LastTimestamp = event->xkey.time;
537 break;
538 case MotionNotify:
539 LastTimestamp = event->xmotion.time;
540 break;
541 case PropertyNotify:
542 LastTimestamp = event->xproperty.time;
543 break;
544 case EnterNotify:
545 case LeaveNotify:
546 LastTimestamp = event->xcrossing.time;
547 break;
548 case SelectionClear:
549 LastTimestamp = event->xselectionclear.time;
550 break;
551 case SelectionRequest:
552 LastTimestamp = event->xselectionrequest.time;
553 break;
554 case SelectionNotify:
555 LastTimestamp = event->xselection.time;
556 #ifdef XDND
557 wXDNDProcessSelection(event);
558 #endif
559 break;
563 static int matchWindow(const void *item, const void *cdata)
565 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
568 static void handleExtensions(XEvent * event)
570 #ifdef KEEP_XKB_LOCK_STATUS
571 XkbEvent *xkbevent;
572 xkbevent = (XkbEvent *) event;
573 #endif /*KEEP_XKB_LOCK_STATUS */
574 #ifdef SHAPE
575 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
576 handleShapeNotify(event);
578 #endif
579 #ifdef KEEP_XKB_LOCK_STATUS
580 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
581 handleXkbIndicatorStateNotify(event);
583 #endif /*KEEP_XKB_LOCK_STATUS */
584 #ifdef HAVE_XRANDR
585 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
586 /* From xrandr man page: "Clients must call back into Xlib using
587 * XRRUpdateConfiguration when screen configuration change notify
588 * events are generated */
589 XRRUpdateConfiguration(event);
590 WCHANGE_STATE(WSTATE_RESTARTING);
591 Shutdown(WSRestartPreparationMode);
592 Restart(NULL,True);
594 #endif
597 static void handleMapRequest(XEvent * ev)
599 WWindow *wwin;
600 WScreen *scr = NULL;
601 Window window = ev->xmaprequest.window;
603 if ((wwin = wWindowFor(window))) {
604 if (wwin->flags.shaded) {
605 wUnshadeWindow(wwin);
607 /* deiconify window */
608 if (wwin->flags.miniaturized) {
609 wDeiconifyWindow(wwin);
610 } else if (wwin->flags.hidden) {
611 WApplication *wapp = wApplicationOf(wwin->main_window);
612 /* go to the last workspace that the user worked on the app */
613 if (wapp) {
614 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
616 wUnhideApplication(wapp, False, False);
618 return;
621 scr = wScreenForRootWindow(ev->xmaprequest.parent);
623 wwin = wManageWindow(scr, window);
626 * This is to let the Dock know that the application it launched
627 * has already been mapped (eg: it has finished launching).
628 * It is not necessary for normally docked apps, but is needed for
629 * apps that were forcedly docked (like with dockit).
631 if (scr->last_dock) {
632 if (wwin && wwin->main_window != None && wwin->main_window != window)
633 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
634 else
635 wDockTrackWindowLaunch(scr->last_dock, window);
638 if (wwin) {
639 wClientSetState(wwin, NormalState, None);
640 if (wwin->flags.maximized) {
641 wMaximizeWindow(wwin, wwin->flags.maximized);
643 if (wwin->flags.shaded) {
644 wwin->flags.shaded = 0;
645 wwin->flags.skip_next_animation = 1;
646 wShadeWindow(wwin);
648 if (wwin->flags.miniaturized) {
649 wwin->flags.miniaturized = 0;
650 wwin->flags.skip_next_animation = 1;
651 wIconifyWindow(wwin);
653 if (wwin->flags.fullscreen) {
654 wwin->flags.fullscreen = 0;
655 wFullscreenWindow(wwin);
657 if (wwin->flags.hidden) {
658 WApplication *wapp = wApplicationOf(wwin->main_window);
660 wwin->flags.hidden = 0;
661 wwin->flags.skip_next_animation = 1;
662 if (wapp) {
663 wHideApplication(wapp);
669 static void handleDestroyNotify(XEvent * event)
671 WWindow *wwin;
672 WApplication *app;
673 Window window = event->xdestroywindow.window;
674 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
675 int widx;
677 wwin = wWindowFor(window);
678 if (wwin) {
679 wUnmanageWindow(wwin, False, True);
682 if (scr != NULL) {
683 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
684 WFakeGroupLeader *fPtr;
686 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
687 if (fPtr->retainCount > 0) {
688 fPtr->retainCount--;
689 if (fPtr->retainCount == 0 && fPtr->leader != None) {
690 XDestroyWindow(dpy, fPtr->leader);
691 fPtr->leader = None;
692 XFlush(dpy);
695 fPtr->origLeader = None;
699 app = wApplicationOf(window);
700 if (app) {
701 if (window == app->main_window) {
702 app->refcount = 0;
703 wwin = app->main_window_desc->screen_ptr->focused_window;
704 while (wwin) {
705 if (wwin->main_window == window) {
706 wwin->main_window = None;
708 wwin = wwin->prev;
711 wApplicationDestroy(app);
715 static void handleExpose(XEvent * event)
717 WObjDescriptor *desc;
718 XEvent ev;
720 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
722 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
723 return;
726 if (desc->handle_expose) {
727 (*desc->handle_expose) (desc, event);
731 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
733 switch (action) {
734 case WA_SELECT_WINDOWS:
735 wUnselectWindows(scr);
736 wSelectWindows(scr, event);
737 break;
738 case WA_OPEN_APPMENU:
739 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
740 /* ugly hack */
741 if (scr->root_menu) {
742 if (scr->root_menu->brother->flags.mapped)
743 event->xbutton.window = scr->root_menu->brother->frame->core->window;
744 else
745 event->xbutton.window = scr->root_menu->frame->core->window;
747 break;
748 case WA_OPEN_WINLISTMENU:
749 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
750 if (scr->switch_menu) {
751 if (scr->switch_menu->brother->flags.mapped)
752 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
753 else
754 event->xbutton.window = scr->switch_menu->frame->core->window;
756 break;
757 default:
758 break;
762 /* bindable */
763 static void handleButtonPress(XEvent * event)
765 WObjDescriptor *desc;
766 WScreen *scr;
768 scr = wScreenForRootWindow(event->xbutton.root);
770 #ifdef BALLOON_TEXT
771 wBalloonHide(scr);
772 #endif
774 if (event->xbutton.window == scr->root_win) {
775 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
776 executeButtonAction(scr, event, wPreferences.mouse_button1);
777 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
778 executeButtonAction(scr, event, wPreferences.mouse_button2);
779 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
780 executeButtonAction(scr, event, wPreferences.mouse_button3);
781 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
782 wWorkspaceRelativeChange(scr, 1);
783 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
784 wWorkspaceRelativeChange(scr, -1);
788 desc = NULL;
789 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
790 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
791 return;
795 if (desc->parent_type == WCLASS_WINDOW) {
796 XSync(dpy, 0);
798 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
799 XAllowEvents(dpy, AsyncPointer, CurrentTime);
800 } else {
801 /* if (wPreferences.focus_mode == WKF_CLICK) { */
802 if (wPreferences.ignore_focus_click) {
803 XAllowEvents(dpy, AsyncPointer, CurrentTime);
805 XAllowEvents(dpy, ReplayPointer, CurrentTime);
806 /* } */
808 XSync(dpy, 0);
809 } else if (desc->parent_type == WCLASS_APPICON
810 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
811 if (event->xbutton.state & MOD_MASK) {
812 XSync(dpy, 0);
813 XAllowEvents(dpy, AsyncPointer, CurrentTime);
814 XSync(dpy, 0);
818 if (desc->handle_mousedown != NULL) {
819 (*desc->handle_mousedown) (desc, event);
822 /* save double-click information */
823 if (scr->flags.next_click_is_not_double) {
824 scr->flags.next_click_is_not_double = 0;
825 } else {
826 scr->last_click_time = event->xbutton.time;
827 scr->last_click_button = event->xbutton.button;
828 scr->last_click_window = event->xbutton.window;
832 static void handleMapNotify(XEvent * event)
834 WWindow *wwin;
836 wwin = wWindowFor(event->xmap.event);
837 if (wwin && wwin->client_win == event->xmap.event) {
838 if (wwin->flags.miniaturized) {
839 wDeiconifyWindow(wwin);
840 } else {
841 XGrabServer(dpy);
842 wWindowMap(wwin);
843 wClientSetState(wwin, NormalState, None);
844 XUngrabServer(dpy);
849 static void handleUnmapNotify(XEvent * event)
851 WWindow *wwin;
852 XEvent ev;
853 Bool withdraw = False;
855 /* only process windows with StructureNotify selected
856 * (ignore SubstructureNotify) */
857 wwin = wWindowFor(event->xunmap.window);
858 if (!wwin)
859 return;
861 /* whether the event is a Withdrawal request */
862 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
863 withdraw = True;
865 if (wwin->client_win != event->xunmap.event && !withdraw)
866 return;
868 if (!wwin->flags.mapped && !withdraw
869 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
870 && !wwin->flags.miniaturized && !wwin->flags.hidden)
871 return;
873 XGrabServer(dpy);
874 XUnmapWindow(dpy, wwin->frame->core->window);
875 wwin->flags.mapped = 0;
876 XSync(dpy, 0);
877 /* check if the window was destroyed */
878 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
879 DispatchEvent(&ev);
880 } else {
881 Bool reparented = False;
883 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
884 reparented = True;
886 /* withdraw window */
887 wwin->flags.mapped = 0;
888 if (!reparented)
889 wClientSetState(wwin, WithdrawnState, None);
891 /* if the window was reparented, do not reparent it back to the
892 * root window */
893 wUnmanageWindow(wwin, !reparented, False);
895 XUngrabServer(dpy);
898 static void handleConfigureRequest(XEvent * event)
900 WWindow *wwin;
902 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
904 * Configure request for unmapped window
906 wClientConfigure(NULL, &(event->xconfigurerequest));
907 } else {
908 wClientConfigure(wwin, &(event->xconfigurerequest));
912 static void handlePropertyNotify(XEvent * event)
914 WWindow *wwin;
915 WApplication *wapp;
916 Window jr;
917 int ji;
918 unsigned int ju;
920 wwin = wWindowFor(event->xproperty.window);
921 if (wwin) {
922 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
923 return;
925 wClientCheckProperty(wwin, &event->xproperty);
927 wapp = wApplicationOf(event->xproperty.window);
928 if (wapp) {
929 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
933 static void handleClientMessage(XEvent * event)
935 WWindow *wwin;
936 WObjDescriptor *desc;
938 /* handle transition from Normal to Iconic state */
939 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
940 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
942 wwin = wWindowFor(event->xclient.window);
943 if (!wwin)
944 return;
945 if (!wwin->flags.miniaturized)
946 wIconifyWindow(wwin);
947 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
948 WScreen *scr = wScreenForRootWindow(event->xclient.window);
950 if (!scr)
951 return;
953 if (event->xclient.data.l[1] == 1) { /* starting */
954 wColormapAllowClientInstallation(scr, True);
955 } else { /* stopping */
956 wColormapAllowClientInstallation(scr, False);
958 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
960 char *command;
961 size_t len;
963 len = sizeof(event->xclient.data.b) + 1;
964 command = wmalloc(len);
965 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
967 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
968 wwarning(_("Got Reconfigure command"));
969 wDefaultsCheckDomains(NULL);
970 } else {
971 wwarning(_("Got unknown command %s"), command);
974 wfree(command);
976 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
977 WApplication *wapp;
978 int done = 0;
979 wapp = wApplicationOf(event->xclient.window);
980 if (wapp) {
981 switch (event->xclient.data.l[0]) {
982 case WMFHideOtherApplications:
983 wHideOtherApplications(wapp->main_window_desc);
984 done = 1;
985 break;
987 case WMFHideApplication:
988 wHideApplication(wapp);
989 done = 1;
990 break;
993 if (!done) {
994 wwin = wWindowFor(event->xclient.window);
995 if (wwin) {
996 switch (event->xclient.data.l[0]) {
997 case WMFHideOtherApplications:
998 wHideOtherApplications(wwin);
999 break;
1001 case WMFHideApplication:
1002 wHideApplication(wApplicationOf(wwin->main_window));
1003 break;
1007 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1008 wwin = wWindowFor(event->xclient.window);
1009 if (!wwin)
1010 return;
1011 switch (event->xclient.data.l[0]) {
1012 case GSWindowLevelAttr:
1014 int level = (int)event->xclient.data.l[1];
1016 if (WINDOW_LEVEL(wwin) != level) {
1017 ChangeStackingLevel(wwin->frame->core, level);
1020 break;
1022 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1023 wwin = wWindowFor(event->xclient.window);
1024 if (!wwin)
1025 return;
1026 switch (event->xclient.data.l[0]) {
1027 case WMTitleBarNormal:
1028 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1029 break;
1030 case WMTitleBarMain:
1031 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1032 break;
1033 case WMTitleBarKey:
1034 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1035 break;
1037 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1038 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1039 if (!scr)
1040 return;
1041 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1042 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1043 /* do nothing */
1044 #ifdef XDND
1045 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1046 /* do nothing */
1047 #endif /* XDND */
1048 } else {
1050 * Non-standard thing, but needed by OffiX DND.
1051 * For when the icon frame gets a ClientMessage
1052 * that should have gone to the icon_window.
1054 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1055 struct WIcon *icon = NULL;
1057 if (desc->parent_type == WCLASS_MINIWINDOW) {
1058 icon = (WIcon *) desc->parent;
1059 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1060 icon = ((WAppIcon *) desc->parent)->icon;
1062 if (icon && (wwin = icon->owner)) {
1063 if (wwin->client_win != event->xclient.window) {
1064 event->xclient.window = wwin->client_win;
1065 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1072 static void raiseWindow(WScreen * scr)
1074 WWindow *wwin;
1076 scr->autoRaiseTimer = NULL;
1078 wwin = wWindowFor(scr->autoRaiseWindow);
1079 if (!wwin)
1080 return;
1082 if (!wwin->flags.destroyed && wwin->flags.focused) {
1083 wRaiseFrame(wwin->frame->core);
1084 /* this is needed or a race condition will occur */
1085 XSync(dpy, False);
1089 static void handleEnterNotify(XEvent * event)
1091 WWindow *wwin;
1092 WObjDescriptor *desc = NULL;
1093 XEvent ev;
1094 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1096 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1097 /* already left the window... */
1098 saveTimestamp(&ev);
1099 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1100 return;
1104 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1105 if (desc->handle_enternotify)
1106 (*desc->handle_enternotify) (desc, event);
1109 /* enter to window */
1110 wwin = wWindowFor(event->xcrossing.window);
1111 if (!wwin) {
1112 if (wPreferences.colormap_mode == WCM_POINTER) {
1113 wColormapInstallForWindow(scr, NULL);
1115 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1116 WMDeleteTimerHandler(scr->autoRaiseTimer);
1117 scr->autoRaiseTimer = NULL;
1119 } else {
1120 /* set auto raise timer even if in focus-follows-mouse mode
1121 * and the event is for the frame window, even if the window
1122 * has focus already. useful if you move the pointer from a focused
1123 * window to the root window and back pretty fast
1125 * set focus if in focus-follows-mouse mode and the event
1126 * is for the frame window and window doesn't have focus yet */
1127 if (wPreferences.focus_mode == WKF_SLOPPY
1128 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1130 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1131 wSetFocusTo(scr, wwin);
1133 if (scr->autoRaiseTimer)
1134 WMDeleteTimerHandler(scr->autoRaiseTimer);
1135 scr->autoRaiseTimer = NULL;
1137 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1138 scr->autoRaiseWindow = wwin->frame->core->window;
1139 scr->autoRaiseTimer
1140 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1143 /* Install colormap for window, if the colormap installation mode
1144 * is colormap_follows_mouse */
1145 if (wPreferences.colormap_mode == WCM_POINTER) {
1146 if (wwin->client_win == event->xcrossing.window)
1147 wColormapInstallForWindow(scr, wwin);
1148 else
1149 wColormapInstallForWindow(scr, NULL);
1153 if (event->xcrossing.window == event->xcrossing.root
1154 && event->xcrossing.detail == NotifyNormal
1155 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1157 wSetFocusTo(scr, scr->focused_window);
1159 #ifdef BALLOON_TEXT
1160 wBalloonEnteredObject(scr, desc);
1161 #endif
1164 static void handleLeaveNotify(XEvent * event)
1166 WObjDescriptor *desc = NULL;
1168 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1169 if (desc->handle_leavenotify)
1170 (*desc->handle_leavenotify) (desc, event);
1174 #ifdef SHAPE
1175 static void handleShapeNotify(XEvent * event)
1177 XShapeEvent *shev = (XShapeEvent *) event;
1178 WWindow *wwin;
1179 union {
1180 XEvent xevent;
1181 XShapeEvent xshape;
1182 } ev;
1184 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1185 if (ev.xshape.kind == ShapeBounding) {
1186 if (ev.xshape.shaped == shev->shaped) {
1187 *shev = ev.xshape;
1188 } else {
1189 XPutBackEvent(dpy, &ev.xevent);
1190 break;
1195 wwin = wWindowFor(shev->window);
1196 if (!wwin || shev->kind != ShapeBounding)
1197 return;
1199 if (!shev->shaped && wwin->flags.shaped) {
1201 wwin->flags.shaped = 0;
1202 wWindowClearShape(wwin);
1204 } else if (shev->shaped) {
1206 wwin->flags.shaped = 1;
1207 wWindowSetShape(wwin);
1210 #endif /* SHAPE */
1212 #ifdef KEEP_XKB_LOCK_STATUS
1213 /* please help ]d if you know what to do */
1214 static void handleXkbIndicatorStateNotify(XEvent *event)
1216 WWindow *wwin;
1217 WScreen *scr;
1218 XkbStateRec staterec;
1219 int i;
1221 for (i = 0; i < wScreenCount; i++) {
1222 scr = wScreenWithNumber(i);
1223 wwin = scr->focused_window;
1224 if (wwin && wwin->flags.focused) {
1225 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1226 if (wwin->frame->languagemode != staterec.group) {
1227 wwin->frame->last_languagemode = wwin->frame->languagemode;
1228 wwin->frame->languagemode = staterec.group;
1230 #ifdef XKB_BUTTON_HINT
1231 if (wwin->frame->titlebar) {
1232 wFrameWindowPaint(wwin->frame);
1234 #endif
1238 #endif /*KEEP_XKB_LOCK_STATUS */
1240 static void handleColormapNotify(XEvent * event)
1242 WWindow *wwin;
1243 WScreen *scr;
1244 Bool reinstall = False;
1246 wwin = wWindowFor(event->xcolormap.window);
1247 if (!wwin)
1248 return;
1250 scr = wwin->screen_ptr;
1252 do {
1253 if (wwin) {
1254 if (event->xcolormap.new) {
1255 XWindowAttributes attr;
1257 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1259 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1260 scr->current_colormap = attr.colormap;
1262 reinstall = True;
1263 } else if (event->xcolormap.state == ColormapUninstalled &&
1264 scr->current_colormap == event->xcolormap.colormap) {
1266 /* some bastard app (like XV) removed our colormap */
1268 * can't enforce or things like xscreensaver wont work
1269 * reinstall = True;
1271 } else if (event->xcolormap.state == ColormapInstalled &&
1272 scr->current_colormap == event->xcolormap.colormap) {
1274 /* someone has put our colormap back */
1275 reinstall = False;
1278 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1279 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1281 if (reinstall && scr->current_colormap != None) {
1282 if (!scr->flags.colormap_stuff_blocked)
1283 XInstallColormap(dpy, scr->current_colormap);
1287 static void handleFocusIn(XEvent * event)
1289 WWindow *wwin;
1292 * For applications that like stealing the focus.
1294 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1295 saveTimestamp(event);
1296 if (event->xfocus.mode == NotifyUngrab
1297 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1298 return;
1301 wwin = wWindowFor(event->xfocus.window);
1302 if (wwin && !wwin->flags.focused) {
1303 if (wwin->flags.mapped)
1304 wSetFocusTo(wwin->screen_ptr, wwin);
1305 else
1306 wSetFocusTo(wwin->screen_ptr, NULL);
1307 } else if (!wwin) {
1308 WScreen *scr = wScreenForWindow(event->xfocus.window);
1309 if (scr)
1310 wSetFocusTo(scr, NULL);
1314 static WWindow *windowUnderPointer(WScreen * scr)
1316 unsigned int mask;
1317 int foo;
1318 Window bar, win;
1320 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1321 return wWindowFor(win);
1322 return NULL;
1325 static int CheckFullScreenWindowFocused(WScreen * scr)
1327 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1328 return 1;
1329 else
1330 return 0;
1333 static void handleKeyPress(XEvent * event)
1335 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1336 WWindow *wwin = scr->focused_window;
1337 short i, widx;
1338 int modifiers;
1339 int command = -1;
1340 #ifdef KEEP_XKB_LOCK_STATUS
1341 XkbStateRec staterec;
1342 #endif /*KEEP_XKB_LOCK_STATUS */
1344 /* ignore CapsLock */
1345 modifiers = event->xkey.state & ValidModMask;
1347 for (i = 0; i < WKBD_LAST; i++) {
1348 if (wKeyBindings[i].keycode == 0)
1349 continue;
1351 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1352 || */ wKeyBindings[i].modifier ==
1353 modifiers)) {
1354 command = i;
1355 break;
1359 if (command < 0) {
1361 if (!wRootMenuPerformShortcut(event)) {
1362 static int dontLoop = 0;
1364 if (dontLoop > 10) {
1365 wwarning("problem with key event processing code");
1366 return;
1368 dontLoop++;
1369 /* if the focused window is an internal window, try redispatching
1370 * the event to the managed window, as it can be a WINGs window */
1371 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1372 /* client_leader contains the WINGs toplevel */
1373 event->xany.window = wwin->client_leader;
1374 WMHandleEvent(event);
1376 dontLoop--;
1378 return;
1380 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1381 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1383 switch (command) {
1385 case WKBD_ROOTMENU:
1386 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1387 if (!CheckFullScreenWindowFocused(scr)) {
1388 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1389 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1390 True);
1392 break;
1393 case WKBD_WINDOWLIST:
1394 if (!CheckFullScreenWindowFocused(scr)) {
1395 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1396 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1397 True);
1399 break;
1401 case WKBD_WINDOWMENU:
1402 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1403 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1404 break;
1405 case WKBD_MINIMIZEALL:
1406 CloseWindowMenu(scr);
1407 wHideAll(scr);
1408 break;
1409 case WKBD_MINIATURIZE:
1410 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1411 && !WFLAGP(wwin, no_miniaturizable)) {
1412 CloseWindowMenu(scr);
1414 if (wwin->protocols.MINIATURIZE_WINDOW)
1415 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1416 else {
1417 wIconifyWindow(wwin);
1420 break;
1421 case WKBD_HIDE:
1422 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1423 WApplication *wapp = wApplicationOf(wwin->main_window);
1424 CloseWindowMenu(scr);
1426 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1427 wHideApplication(wapp);
1430 break;
1431 case WKBD_HIDE_OTHERS:
1432 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1433 CloseWindowMenu(scr);
1435 wHideOtherApplications(wwin);
1437 break;
1438 case WKBD_MAXIMIZE:
1439 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1440 CloseWindowMenu(scr);
1442 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1444 break;
1445 case WKBD_VMAXIMIZE:
1446 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1447 CloseWindowMenu(scr);
1449 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1451 break;
1452 case WKBD_HMAXIMIZE:
1453 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1454 CloseWindowMenu(scr);
1456 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1458 break;
1459 case WKBD_LHMAXIMIZE:
1460 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1461 CloseWindowMenu(scr);
1463 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1465 break;
1466 case WKBD_RHMAXIMIZE:
1467 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1468 CloseWindowMenu(scr);
1470 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1472 break;
1473 case WKBD_THMAXIMIZE:
1474 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1475 CloseWindowMenu(scr);
1477 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1479 break;
1480 case WKBD_BHMAXIMIZE:
1481 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1482 CloseWindowMenu(scr);
1484 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1486 break;
1487 case WKBD_LTCMAXIMIZE:
1488 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1489 CloseWindowMenu(scr);
1491 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1493 break;
1494 case WKBD_RTCMAXIMIZE:
1495 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1496 CloseWindowMenu(scr);
1498 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1500 break;
1501 case WKBD_LBCMAXIMIZE:
1502 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1503 CloseWindowMenu(scr);
1505 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1507 break;
1508 case WKBD_RBCMAXIMIZE:
1509 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1510 CloseWindowMenu(scr);
1512 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1514 break;
1515 case WKBD_MAXIMUS:
1516 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1517 CloseWindowMenu(scr);
1519 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1521 break;
1522 case WKBD_RAISE:
1523 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1524 CloseWindowMenu(scr);
1526 wRaiseFrame(wwin->frame->core);
1528 break;
1529 case WKBD_LOWER:
1530 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1531 CloseWindowMenu(scr);
1533 wLowerFrame(wwin->frame->core);
1535 break;
1536 case WKBD_RAISELOWER:
1537 /* raise or lower the window under the pointer, not the
1538 * focused one
1540 wwin = windowUnderPointer(scr);
1541 if (wwin)
1542 wRaiseLowerFrame(wwin->frame->core);
1543 break;
1544 case WKBD_SHADE:
1545 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1546 if (wwin->flags.shaded)
1547 wUnshadeWindow(wwin);
1548 else
1549 wShadeWindow(wwin);
1551 break;
1552 case WKBD_MOVERESIZE:
1553 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1554 CloseWindowMenu(scr);
1556 wKeyboardMoveResizeWindow(wwin);
1558 break;
1559 case WKBD_CLOSE:
1560 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1561 CloseWindowMenu(scr);
1562 if (wwin->protocols.DELETE_WINDOW)
1563 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1565 break;
1566 case WKBD_SELECT:
1567 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1568 wSelectWindow(wwin, !wwin->flags.selected);
1570 break;
1571 case WKBD_FOCUSNEXT:
1572 StartWindozeCycle(wwin, event, True, False);
1573 break;
1575 case WKBD_FOCUSPREV:
1576 StartWindozeCycle(wwin, event, False, False);
1577 break;
1579 case WKBD_GROUPNEXT:
1580 StartWindozeCycle(wwin, event, True, True);
1581 break;
1583 case WKBD_GROUPPREV:
1584 StartWindozeCycle(wwin, event, False, True);
1585 break;
1587 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1588 widx = command - WKBD_WORKSPACE1;
1589 i = (scr->current_workspace / 10) * 10 + widx;
1590 if (wPreferences.ws_advance || i < scr->workspace_count)
1591 wWorkspaceChange(scr, i);
1592 break;
1594 case WKBD_NEXTWORKSPACE:
1595 wWorkspaceRelativeChange(scr, 1);
1596 break;
1597 case WKBD_PREVWORKSPACE:
1598 wWorkspaceRelativeChange(scr, -1);
1599 break;
1600 case WKBD_LASTWORKSPACE:
1601 wWorkspaceChange(scr, scr->last_workspace);
1602 break;
1604 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1605 widx = command - WKBD_MOVE_WORKSPACE1;
1606 i = (scr->current_workspace / 10) * 10 + widx;
1607 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1608 wWindowChangeWorkspace(wwin, i);
1609 break;
1611 case WKBD_MOVE_NEXTWORKSPACE:
1612 if (wwin)
1613 wWindowChangeWorkspaceRelative(wwin, 1);
1614 break;
1615 case WKBD_MOVE_PREVWORKSPACE:
1616 if (wwin)
1617 wWindowChangeWorkspaceRelative(wwin, -1);
1618 break;
1619 case WKBD_MOVE_LASTWORKSPACE:
1620 if (wwin)
1621 wWindowChangeWorkspace(wwin, scr->last_workspace);
1622 break;
1624 case WKBD_MOVE_NEXTWSLAYER:
1625 case WKBD_MOVE_PREVWSLAYER:
1627 if (wwin) {
1628 int row, column;
1630 row = scr->current_workspace / 10;
1631 column = scr->current_workspace % 10;
1633 if (command == WKBD_MOVE_NEXTWSLAYER) {
1634 if ((row + 1) * 10 < scr->workspace_count)
1635 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1636 } else {
1637 if (row > 0)
1638 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1642 break;
1644 case WKBD_WINDOW1:
1645 case WKBD_WINDOW2:
1646 case WKBD_WINDOW3:
1647 case WKBD_WINDOW4:
1648 case WKBD_WINDOW5:
1649 case WKBD_WINDOW6:
1650 case WKBD_WINDOW7:
1651 case WKBD_WINDOW8:
1652 case WKBD_WINDOW9:
1653 case WKBD_WINDOW10:
1655 widx = command - WKBD_WINDOW1;
1657 if (scr->shortcutWindows[widx]) {
1658 WMArray *list = scr->shortcutWindows[widx];
1659 int cw;
1660 int count = WMGetArrayItemCount(list);
1661 WWindow *twin;
1662 WMArrayIterator iter;
1663 WWindow *wwin;
1665 wUnselectWindows(scr);
1666 cw = scr->current_workspace;
1668 WM_ETARETI_ARRAY(list, wwin, iter) {
1669 if (count > 1)
1670 wWindowChangeWorkspace(wwin, cw);
1672 wMakeWindowVisible(wwin);
1674 if (count > 1)
1675 wSelectWindow(wwin, True);
1678 /* rotate the order of windows, to create a cycling effect */
1679 twin = WMGetFromArray(list, 0);
1680 WMDeleteFromArray(list, 0);
1681 WMAddToArray(list, twin);
1683 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1684 if (scr->shortcutWindows[widx]) {
1685 WMFreeArray(scr->shortcutWindows[widx]);
1686 scr->shortcutWindows[widx] = NULL;
1689 if (wwin->flags.selected && scr->selected_windows) {
1690 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1691 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1692 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1693 } else {
1694 scr->shortcutWindows[widx] = WMCreateArray(4);
1695 WMAddToArray(scr->shortcutWindows[widx], wwin);
1698 wSelectWindow(wwin, !wwin->flags.selected);
1699 XFlush(dpy);
1700 wusleep(3000);
1701 wSelectWindow(wwin, !wwin->flags.selected);
1702 XFlush(dpy);
1704 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1706 if (wwin->flags.selected && scr->selected_windows) {
1707 if (scr->shortcutWindows[widx]) {
1708 WMFreeArray(scr->shortcutWindows[widx]);
1710 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1714 break;
1716 case WKBD_RELAUNCH:
1717 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1718 (void) RelaunchWindow(wwin);
1720 break;
1722 case WKBD_SWITCH_SCREEN:
1723 if (wScreenCount > 1) {
1724 WScreen *scr2;
1725 int i;
1727 /* find index of this screen */
1728 for (i = 0; i < wScreenCount; i++) {
1729 if (wScreenWithNumber(i) == scr)
1730 break;
1732 i++;
1733 if (i >= wScreenCount) {
1734 i = 0;
1736 scr2 = wScreenWithNumber(i);
1738 if (scr2) {
1739 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1740 scr2->scr_width / 2, scr2->scr_height / 2);
1743 break;
1745 case WKBD_NEXTWSLAYER:
1746 case WKBD_PREVWSLAYER:
1748 int row, column;
1750 row = scr->current_workspace / 10;
1751 column = scr->current_workspace % 10;
1753 if (command == WKBD_NEXTWSLAYER) {
1754 if ((row + 1) * 10 < scr->workspace_count)
1755 wWorkspaceChange(scr, column + (row + 1) * 10);
1756 } else {
1757 if (row > 0)
1758 wWorkspaceChange(scr, column + (row - 1) * 10);
1761 break;
1762 case WKBD_CLIPRAISELOWER:
1763 if (!wPreferences.flags.noclip)
1764 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1765 break;
1766 case WKBD_DOCKRAISELOWER:
1767 if (!wPreferences.flags.nodock)
1768 wDockRaiseLower(scr->dock);
1769 break;
1770 #ifdef KEEP_XKB_LOCK_STATUS
1771 case WKBD_TOGGLE:
1772 if (wPreferences.modelock) {
1773 /*toggle */
1774 wwin = scr->focused_window;
1776 if (wwin && wwin->flags.mapped
1777 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1778 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1779 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1781 wwin->frame->languagemode = wwin->frame->last_languagemode;
1782 wwin->frame->last_languagemode = staterec.group;
1783 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1787 break;
1788 #endif /* KEEP_XKB_LOCK_STATUS */
1792 static void handleMotionNotify(XEvent * event)
1794 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1796 if (wPreferences.scrollable_menus) {
1797 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1798 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1800 if (scr->flags.jump_back_pending ||
1801 p.x <= (rect.pos.x + 1) ||
1802 p.x >= (rect.pos.x + rect.size.width - 2) ||
1803 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1804 WMenu *menu;
1806 menu = wMenuUnderPointer(scr);
1807 if (menu != NULL)
1808 wMenuScroll(menu, event);
1813 static void handleVisibilityNotify(XEvent * event)
1815 WWindow *wwin;
1817 wwin = wWindowFor(event->xvisibility.window);
1818 if (!wwin)
1819 return;
1820 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);