wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / src / event.c
blob6b2e0db02525dacc33050cf9141e0873ac696bf4
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 2014 Window Maker Team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #ifdef HAVE_INOTIFY
26 #include <sys/select.h>
27 #include <sys/inotify.h>
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <time.h>
36 #include <errno.h>
38 #include <X11/Xlib.h>
39 #include <X11/Xutil.h>
40 #ifdef USE_XSHAPE
41 # include <X11/extensions/shape.h>
42 #endif
43 #ifdef XDND
44 #include "xdnd.h"
45 #endif
47 #ifdef USE_RANDR
48 #include <X11/extensions/Xrandr.h>
49 #endif
51 #ifdef KEEP_XKB_LOCK_STATUS
52 #include <X11/XKBlib.h>
53 #endif /* KEEP_XKB_LOCK_STATUS */
55 #include "WindowMaker.h"
56 #include "window.h"
57 #include "actions.h"
58 #include "client.h"
59 #include "main.h"
60 #include "cycling.h"
61 #include "keybind.h"
62 #include "application.h"
63 #include "stacking.h"
64 #include "defaults.h"
65 #include "workspace.h"
66 #include "dock.h"
67 #include "framewin.h"
68 #include "properties.h"
69 #include "balloon.h"
70 #include "xinerama.h"
71 #include "wmspec.h"
72 #include "rootmenu.h"
73 #include "colormap.h"
74 #include "screen.h"
75 #include "shutdown.h"
76 #include "misc.h"
77 #include "event.h"
78 #include "winmenu.h"
79 #include "switchmenu.h"
80 #include "wsmap.h"
83 #define MOD_MASK wPreferences.modifier_mask
85 /************ Local stuff ***********/
87 static void saveTimestamp(XEvent *event);
88 static void handleColormapNotify(XEvent *event);
89 static void handleMapNotify(XEvent *event);
90 static void handleUnmapNotify(XEvent *event);
91 static void handleButtonPress(XEvent *event);
92 static void handleExpose(XEvent *event);
93 static void handleDestroyNotify(XEvent *event);
94 static void handleConfigureRequest(XEvent *event);
95 static void handleMapRequest(XEvent *event);
96 static void handlePropertyNotify(XEvent *event);
97 static void handleEnterNotify(XEvent *event);
98 static void handleLeaveNotify(XEvent *event);
99 static void handleExtensions(XEvent *event);
100 static void handleClientMessage(XEvent *event);
101 static void handleKeyPress(XEvent *event);
102 static void handleFocusIn(XEvent *event);
103 static void handleMotionNotify(XEvent *event);
104 static void handleVisibilityNotify(XEvent *event);
105 static void handle_inotify_events(void);
106 static void wdelete_death_handler(WMagicNumber id);
109 #ifdef USE_XSHAPE
110 static void handleShapeNotify(XEvent *event);
111 #endif
113 #ifdef KEEP_XKB_LOCK_STATUS
114 static void handleXkbIndicatorStateNotify(XkbEvent *event);
115 #endif
117 /* real dead process handler */
118 static void handleDeadProcess(void);
120 typedef struct DeadProcesses {
121 pid_t pid;
122 unsigned char exit_status;
123 } DeadProcesses;
125 /* stack of dead processes */
126 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
127 static int deadProcessPtr = 0;
129 typedef struct DeathHandler {
130 WDeathHandler *callback;
131 pid_t pid;
132 void *client_data;
133 } DeathHandler;
135 static WMArray *deathHandlers = NULL;
137 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
139 DeathHandler *handler;
141 handler = malloc(sizeof(DeathHandler));
142 if (!handler)
143 return 0;
145 handler->pid = pid;
146 handler->callback = callback;
147 handler->client_data = cdata;
149 if (!deathHandlers)
150 deathHandlers = WMCreateArrayWithDestructor(8, free);
152 WMAddToArray(deathHandlers, handler);
154 return handler;
157 static void wdelete_death_handler(WMagicNumber id)
159 DeathHandler *handler = (DeathHandler *) id;
161 if (!handler || !deathHandlers)
162 return;
164 /* array destructor will call free(handler) */
165 WMRemoveFromArray(deathHandlers, handler);
168 void DispatchEvent(XEvent * event)
170 if (deathHandlers)
171 handleDeadProcess();
173 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
174 WCHANGE_STATE(WSTATE_EXITING);
175 /* received SIGTERM */
177 * WMHandleEvent() can't be called from anything
178 * executed inside here, or we can get in a infinite
179 * recursive loop.
181 Shutdown(WSExitMode);
183 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
184 WCHANGE_STATE(WSTATE_RESTARTING);
186 Shutdown(WSRestartPreparationMode);
187 /* received SIGHUP */
188 Restart(NULL, True);
189 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
190 WCHANGE_STATE(WSTATE_NORMAL);
191 wDefaultsCheckDomains(NULL);
194 /* for the case that all that is wanted to be dispatched is
195 * the stuff above */
196 if (!event)
197 return;
199 saveTimestamp(event);
200 switch (event->type) {
201 case MapRequest:
202 handleMapRequest(event);
203 break;
205 case KeyPress:
206 handleKeyPress(event);
207 break;
209 case MotionNotify:
210 handleMotionNotify(event);
211 break;
213 case ConfigureRequest:
214 handleConfigureRequest(event);
215 break;
217 case DestroyNotify:
218 handleDestroyNotify(event);
219 break;
221 case MapNotify:
222 handleMapNotify(event);
223 break;
225 case UnmapNotify:
226 handleUnmapNotify(event);
227 break;
229 case ButtonPress:
230 handleButtonPress(event);
231 break;
233 case Expose:
234 handleExpose(event);
235 break;
237 case PropertyNotify:
238 handlePropertyNotify(event);
239 break;
241 case EnterNotify:
242 handleEnterNotify(event);
243 break;
245 case LeaveNotify:
246 handleLeaveNotify(event);
247 break;
249 case ClientMessage:
250 handleClientMessage(event);
251 break;
253 case ColormapNotify:
254 handleColormapNotify(event);
255 break;
257 case MappingNotify:
258 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
259 XRefreshKeyboardMapping(&event->xmapping);
260 break;
262 case FocusIn:
263 handleFocusIn(event);
264 break;
266 case VisibilityNotify:
267 handleVisibilityNotify(event);
268 break;
270 case ConfigureNotify:
271 #ifdef USE_RANDR
272 if (event->xconfigure.window == DefaultRootWindow(dpy))
273 XRRUpdateConfiguration(event);
274 #endif
275 break;
277 default:
278 handleExtensions(event);
279 break;
283 #ifdef HAVE_INOTIFY
285 *----------------------------------------------------------------------
286 * handle_inotify_events-
287 * Check for inotify events
289 * Returns:
290 * After reading events for the given file descriptor (fd) and
291 * watch descriptor (wd)
293 * Side effects:
294 * Calls wDefaultsCheckDomains if config database is updated
295 *----------------------------------------------------------------------
297 static void handle_inotify_events(void)
299 ssize_t eventQLength;
300 size_t i = 0;
301 /* Make room for at lease 5 simultaneous events, with path + filenames */
302 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
303 /* Check config only once per read of the event queue */
304 int oneShotFlag = 0;
307 * Read off the queued events
308 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
309 * not occur; the block is on Xevents, but a config file change will normally
310 * occur as a result of an Xevent - so the event queue should never have more than
311 * a few entries before a read().
313 eventQLength = read(w_global.inotify.fd_event_queue,
314 buff, sizeof(buff) );
316 if (eventQLength < 0) {
317 wwarning(_("read problem when trying to get INotify event: %s"), strerror(errno));
318 return;
321 /* check what events occured */
322 /* Should really check wd here too, but for now we only have one watch! */
323 while (i < eventQLength) {
324 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
327 * see inotify.h for event types.
329 if (pevent->mask & IN_DELETE_SELF) {
330 wwarning(_("the defaults database has been deleted!"
331 " Restart Window Maker to create the database" " with the default settings"));
333 if (w_global.inotify.fd_event_queue >= 0) {
334 close(w_global.inotify.fd_event_queue);
335 w_global.inotify.fd_event_queue = -1;
338 if (pevent->mask & IN_UNMOUNT) {
339 wwarning(_("the unit containing the defaults database has"
340 " been unmounted. Setting --static mode." " Any changes will not be saved."));
342 if (w_global.inotify.fd_event_queue >= 0) {
343 close(w_global.inotify.fd_event_queue);
344 w_global.inotify.fd_event_queue = -1;
347 wPreferences.flags.noupdates = 1;
349 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
350 wwarning(_("Inotify: Reading config files in defaults database."));
351 wDefaultsCheckDomains(NULL);
352 oneShotFlag = 1;
355 /* move to next event in the buffer */
356 i += sizeof(struct inotify_event) + pevent->len;
359 #endif /* HAVE_INOTIFY */
362 *----------------------------------------------------------------------
363 * EventLoop-
364 * Processes X and internal events indefinitely.
366 * Returns:
367 * Never returns
369 * Side effects:
370 * The LastTimestamp global variable is updated.
371 * Calls inotifyGetEvents if defaults database changes.
372 *----------------------------------------------------------------------
374 noreturn void EventLoop(void)
376 XEvent event;
377 #ifdef HAVE_INOTIFY
378 struct timeval time;
379 fd_set rfds;
380 int retVal = 0;
382 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
383 retVal = -1;
384 #endif
386 for (;;) {
388 WMNextEvent(dpy, &event); /* Blocks here */
389 WMHandleEvent(&event);
390 #ifdef HAVE_INOTIFY
391 if (retVal != -1) {
392 time.tv_sec = 0;
393 time.tv_usec = 0;
394 FD_ZERO(&rfds);
395 FD_SET(w_global.inotify.fd_event_queue, &rfds);
397 /* check for available read data from inotify - don't block! */
398 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
400 if (retVal < 0) { /* an error has occured */
401 wwarning(_("select failed. The inotify instance will be closed."
402 " Changes to the defaults database will require"
403 " a restart to take effect."));
404 close(w_global.inotify.fd_event_queue);
405 w_global.inotify.fd_event_queue = -1;
406 continue;
408 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
409 handle_inotify_events();
411 #endif
416 *----------------------------------------------------------------------
417 * ProcessPendingEvents --
418 * Processes the events that are currently pending (at the time
419 * this function is called) in the display's queue.
421 * Returns:
422 * After the pending events that were present at the function call
423 * are processed.
425 * Side effects:
426 * Many -- whatever handling events may involve.
428 *----------------------------------------------------------------------
430 void ProcessPendingEvents(void)
432 XEvent event;
433 int count;
435 XSync(dpy, False);
437 /* Take a snapshot of the event count in the queue */
438 count = XPending(dpy);
440 while (count > 0 && XPending(dpy)) {
441 WMNextEvent(dpy, &event);
442 WMHandleEvent(&event);
443 count--;
447 Bool IsDoubleClick(WScreen * scr, XEvent * event)
449 if ((scr->last_click_time > 0) &&
450 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
451 && (event->xbutton.button == scr->last_click_button)
452 && (event->xbutton.window == scr->last_click_window)) {
454 scr->flags.next_click_is_not_double = 1;
455 scr->last_click_time = 0;
456 scr->last_click_window = event->xbutton.window;
458 return True;
460 return False;
463 void NotifyDeadProcess(pid_t pid, unsigned char status)
465 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
466 wwarning("stack overflow: too many dead processes");
467 return;
469 /* stack the process to be handled later,
470 * as this is called from the signal handler */
471 deadProcesses[deadProcessPtr].pid = pid;
472 deadProcesses[deadProcessPtr].exit_status = status;
473 deadProcessPtr++;
476 static void handleDeadProcess(void)
478 DeathHandler *tmp;
479 int i;
481 for (i = 0; i < deadProcessPtr; i++) {
482 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
485 if (!deathHandlers) {
486 deadProcessPtr = 0;
487 return;
490 /* get the pids on the queue and call handlers */
491 while (deadProcessPtr > 0) {
492 deadProcessPtr--;
494 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
495 tmp = WMGetFromArray(deathHandlers, i);
496 if (!tmp)
497 continue;
499 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
500 (*tmp->callback) (tmp->pid,
501 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
502 wdelete_death_handler(tmp);
508 static void saveTimestamp(XEvent * event)
511 * Never save CurrentTime as LastTimestamp because CurrentTime
512 * it's not a real timestamp (it's the 0L constant)
515 switch (event->type) {
516 case ButtonRelease:
517 case ButtonPress:
518 w_global.timestamp.last_event = event->xbutton.time;
519 break;
520 case KeyPress:
521 case KeyRelease:
522 w_global.timestamp.last_event = event->xkey.time;
523 break;
524 case MotionNotify:
525 w_global.timestamp.last_event = event->xmotion.time;
526 break;
527 case PropertyNotify:
528 w_global.timestamp.last_event = event->xproperty.time;
529 break;
530 case EnterNotify:
531 case LeaveNotify:
532 w_global.timestamp.last_event = event->xcrossing.time;
533 break;
534 case SelectionClear:
535 w_global.timestamp.last_event = event->xselectionclear.time;
536 break;
537 case SelectionRequest:
538 w_global.timestamp.last_event = event->xselectionrequest.time;
539 break;
540 case SelectionNotify:
541 w_global.timestamp.last_event = event->xselection.time;
542 #ifdef XDND
543 wXDNDProcessSelection(event);
544 #endif
545 break;
549 static int matchWindow(const void *item, const void *cdata)
551 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
554 static void handleExtensions(XEvent * event)
556 #ifdef USE_XSHAPE
557 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
558 handleShapeNotify(event);
560 #endif
561 #ifdef KEEP_XKB_LOCK_STATUS
562 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
563 handleXkbIndicatorStateNotify((XkbEvent *) event);
565 #endif /*KEEP_XKB_LOCK_STATUS */
566 #ifdef USE_RANDR
567 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
568 /* From xrandr man page: "Clients must call back into Xlib using
569 * XRRUpdateConfiguration when screen configuration change notify
570 * events are generated */
571 XRRUpdateConfiguration(event);
572 WCHANGE_STATE(WSTATE_RESTARTING);
573 Shutdown(WSRestartPreparationMode);
574 Restart(NULL,True);
576 #endif
579 static void handleMapRequest(XEvent * ev)
581 WWindow *wwin;
582 WScreen *scr = NULL;
583 Window window = ev->xmaprequest.window;
585 if ((wwin = wWindowFor(window))) {
586 if (wwin->flags.shaded) {
587 wUnshadeWindow(wwin);
589 /* deiconify window */
590 if (wwin->flags.miniaturized) {
591 wDeiconifyWindow(wwin);
592 } else if (wwin->flags.hidden) {
593 WApplication *wapp = wApplicationOf(wwin->main_window);
594 /* go to the last workspace that the user worked on the app */
595 if (wapp) {
596 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
598 wUnhideApplication(wapp, False, False);
600 return;
603 scr = wScreenForRootWindow(ev->xmaprequest.parent);
605 wwin = wManageWindow(scr, window);
608 * This is to let the Dock know that the application it launched
609 * has already been mapped (eg: it has finished launching).
610 * It is not necessary for normally docked apps, but is needed for
611 * apps that were forcedly docked (like with dockit).
613 if (scr->last_dock) {
614 if (wwin && wwin->main_window != None && wwin->main_window != window)
615 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
616 else
617 wDockTrackWindowLaunch(scr->last_dock, window);
620 if (wwin) {
621 wClientSetState(wwin, NormalState, None);
622 if (wwin->flags.maximized) {
623 wMaximizeWindow(wwin, wwin->flags.maximized);
625 if (wwin->flags.shaded) {
626 wwin->flags.shaded = 0;
627 wwin->flags.skip_next_animation = 1;
628 wShadeWindow(wwin);
630 if (wwin->flags.miniaturized) {
631 wwin->flags.miniaturized = 0;
632 wwin->flags.skip_next_animation = 1;
633 wIconifyWindow(wwin);
635 if (wwin->flags.fullscreen) {
636 wwin->flags.fullscreen = 0;
637 wFullscreenWindow(wwin);
639 if (wwin->flags.hidden) {
640 WApplication *wapp = wApplicationOf(wwin->main_window);
642 wwin->flags.hidden = 0;
643 wwin->flags.skip_next_animation = 1;
644 if (wapp) {
645 wHideApplication(wapp);
651 static void handleDestroyNotify(XEvent * event)
653 WWindow *wwin;
654 WApplication *app;
655 Window window = event->xdestroywindow.window;
656 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
657 int widx;
659 wwin = wWindowFor(window);
660 if (wwin) {
661 wUnmanageWindow(wwin, False, True);
664 if (scr != NULL) {
665 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
666 WFakeGroupLeader *fPtr;
668 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
669 if (fPtr->retainCount > 0) {
670 fPtr->retainCount--;
671 if (fPtr->retainCount == 0 && fPtr->leader != None) {
672 XDestroyWindow(dpy, fPtr->leader);
673 fPtr->leader = None;
674 XFlush(dpy);
677 fPtr->origLeader = None;
681 app = wApplicationOf(window);
682 if (app) {
683 if (window == app->main_window) {
684 app->refcount = 0;
685 wwin = app->main_window_desc->screen_ptr->focused_window;
686 while (wwin) {
687 if (wwin->main_window == window) {
688 wwin->main_window = None;
690 wwin = wwin->prev;
693 wApplicationDestroy(app);
697 static void handleExpose(XEvent * event)
699 WObjDescriptor *desc;
700 XEvent ev;
702 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
704 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
705 return;
708 if (desc->handle_expose) {
709 (*desc->handle_expose) (desc, event);
713 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
715 WWindow *wwin;
716 Bool next_direction;
718 if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
719 next_direction = False;
720 else
721 next_direction = True;
723 switch (action) {
724 case WA_SWITCH_WORKSPACES:
725 if (next_direction)
726 wWorkspaceRelativeChange(scr, 1);
727 else
728 wWorkspaceRelativeChange(scr, -1);
729 break;
731 case WA_SWITCH_WINDOWS:
732 wwin = scr->focused_window;
733 if (next_direction)
734 wWindowFocusNext(wwin, True);
735 else
736 wWindowFocusPrev(wwin, True);
737 break;
741 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
743 WWindow *wwin;
745 switch (action) {
746 case WA_SELECT_WINDOWS:
747 wUnselectWindows(scr);
748 wSelectWindows(scr, event);
749 break;
750 case WA_OPEN_APPMENU:
751 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
752 /* ugly hack */
753 if (scr->root_menu) {
754 if (scr->root_menu->brother->flags.mapped)
755 event->xbutton.window = scr->root_menu->brother->frame->core->window;
756 else
757 event->xbutton.window = scr->root_menu->frame->core->window;
759 break;
760 case WA_OPEN_WINLISTMENU:
761 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
762 if (scr->switch_menu) {
763 if (scr->switch_menu->brother->flags.mapped)
764 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
765 else
766 event->xbutton.window = scr->switch_menu->frame->core->window;
768 break;
769 case WA_MOVE_PREVWORKSPACE:
770 wWorkspaceRelativeChange(scr, -1);
771 break;
772 case WA_MOVE_NEXTWORKSPACE:
773 wWorkspaceRelativeChange(scr, 1);
774 break;
775 case WA_MOVE_PREVWINDOW:
776 wwin = scr->focused_window;
777 wWindowFocusPrev(wwin, True);
778 break;
779 case WA_MOVE_NEXTWINDOW:
780 wwin = scr->focused_window;
781 wWindowFocusNext(wwin, True);
782 break;
786 /* bindable */
787 static void handleButtonPress(XEvent * event)
789 WObjDescriptor *desc;
790 WScreen *scr;
792 scr = wScreenForRootWindow(event->xbutton.root);
794 #ifdef BALLOON_TEXT
795 wBalloonHide(scr);
796 #endif
798 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
799 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
800 executeButtonAction(scr, event, wPreferences.mouse_button1);
801 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
802 executeButtonAction(scr, event, wPreferences.mouse_button2);
803 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
804 executeButtonAction(scr, event, wPreferences.mouse_button3);
805 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
806 executeButtonAction(scr, event, wPreferences.mouse_button8);
807 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
808 executeButtonAction(scr, event, wPreferences.mouse_button9);
809 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
810 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
811 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
812 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
813 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
814 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
815 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
816 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
820 desc = NULL;
821 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
822 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
823 return;
827 if (desc->parent_type == WCLASS_WINDOW) {
828 XSync(dpy, 0);
830 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
831 XAllowEvents(dpy, AsyncPointer, CurrentTime);
832 } else {
833 /* if (wPreferences.focus_mode == WKF_CLICK) { */
834 if (wPreferences.ignore_focus_click) {
835 XAllowEvents(dpy, AsyncPointer, CurrentTime);
837 XAllowEvents(dpy, ReplayPointer, CurrentTime);
838 /* } */
840 XSync(dpy, 0);
841 } else if (desc->parent_type == WCLASS_APPICON
842 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
843 if (event->xbutton.state & MOD_MASK) {
844 XSync(dpy, 0);
845 XAllowEvents(dpy, AsyncPointer, CurrentTime);
846 XSync(dpy, 0);
850 if (desc->handle_mousedown != NULL) {
851 (*desc->handle_mousedown) (desc, event);
854 /* save double-click information */
855 if (scr->flags.next_click_is_not_double) {
856 scr->flags.next_click_is_not_double = 0;
857 } else {
858 scr->last_click_time = event->xbutton.time;
859 scr->last_click_button = event->xbutton.button;
860 scr->last_click_window = event->xbutton.window;
864 static void handleMapNotify(XEvent * event)
866 WWindow *wwin;
868 wwin = wWindowFor(event->xmap.event);
869 if (wwin && wwin->client_win == event->xmap.event) {
870 if (wwin->flags.miniaturized) {
871 wDeiconifyWindow(wwin);
872 } else {
873 XGrabServer(dpy);
874 wWindowMap(wwin);
875 wClientSetState(wwin, NormalState, None);
876 XUngrabServer(dpy);
881 static void handleUnmapNotify(XEvent * event)
883 WWindow *wwin;
884 XEvent ev;
885 Bool withdraw = False;
887 /* only process windows with StructureNotify selected
888 * (ignore SubstructureNotify) */
889 wwin = wWindowFor(event->xunmap.window);
890 if (!wwin)
891 return;
893 /* whether the event is a Withdrawal request */
894 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
895 withdraw = True;
897 if (wwin->client_win != event->xunmap.event && !withdraw)
898 return;
900 if (!wwin->flags.mapped && !withdraw
901 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
902 && !wwin->flags.miniaturized && !wwin->flags.hidden)
903 return;
905 XGrabServer(dpy);
906 XUnmapWindow(dpy, wwin->frame->core->window);
907 wwin->flags.mapped = 0;
908 XSync(dpy, 0);
909 /* check if the window was destroyed */
910 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
911 DispatchEvent(&ev);
912 } else {
913 Bool reparented = False;
915 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
916 reparented = True;
918 /* withdraw window */
919 wwin->flags.mapped = 0;
920 if (!reparented)
921 wClientSetState(wwin, WithdrawnState, None);
923 /* if the window was reparented, do not reparent it back to the
924 * root window */
925 wUnmanageWindow(wwin, !reparented, False);
927 XUngrabServer(dpy);
930 static void handleConfigureRequest(XEvent * event)
932 WWindow *wwin;
934 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
936 * Configure request for unmapped window
938 wClientConfigure(NULL, &(event->xconfigurerequest));
939 } else {
940 wClientConfigure(wwin, &(event->xconfigurerequest));
944 static void handlePropertyNotify(XEvent * event)
946 WWindow *wwin;
947 WApplication *wapp;
948 Window jr;
949 int ji;
950 unsigned int ju;
952 wwin = wWindowFor(event->xproperty.window);
953 if (wwin) {
954 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
955 return;
957 wClientCheckProperty(wwin, &event->xproperty);
959 wapp = wApplicationOf(event->xproperty.window);
960 if (wapp) {
961 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
965 static void handleClientMessage(XEvent * event)
967 WWindow *wwin;
968 WObjDescriptor *desc;
970 /* handle transition from Normal to Iconic state */
971 if (event->xclient.message_type == w_global.atom.wm.change_state
972 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
974 wwin = wWindowFor(event->xclient.window);
975 if (!wwin)
976 return;
977 if (!wwin->flags.miniaturized)
978 wIconifyWindow(wwin);
979 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
980 WScreen *scr = wScreenForRootWindow(event->xclient.window);
982 if (!scr)
983 return;
985 if (event->xclient.data.l[1] == 1) { /* starting */
986 wColormapAllowClientInstallation(scr, True);
987 } else { /* stopping */
988 wColormapAllowClientInstallation(scr, False);
990 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
992 char *command;
993 size_t len;
995 len = sizeof(event->xclient.data.b) + 1;
996 command = wmalloc(len);
997 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
999 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1000 wwarning(_("Got Reconfigure command"));
1001 wDefaultsCheckDomains(NULL);
1002 } else {
1003 wwarning(_("Got unknown command %s"), command);
1006 wfree(command);
1008 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1009 WApplication *wapp;
1010 int done = 0;
1011 wapp = wApplicationOf(event->xclient.window);
1012 if (wapp) {
1013 switch (event->xclient.data.l[0]) {
1014 case WMFHideOtherApplications:
1015 wHideOtherApplications(wapp->main_window_desc);
1016 done = 1;
1017 break;
1019 case WMFHideApplication:
1020 wHideApplication(wapp);
1021 done = 1;
1022 break;
1025 if (!done) {
1026 wwin = wWindowFor(event->xclient.window);
1027 if (wwin) {
1028 switch (event->xclient.data.l[0]) {
1029 case WMFHideOtherApplications:
1030 wHideOtherApplications(wwin);
1031 break;
1033 case WMFHideApplication:
1034 wHideApplication(wApplicationOf(wwin->main_window));
1035 break;
1039 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1040 wwin = wWindowFor(event->xclient.window);
1041 if (!wwin)
1042 return;
1043 switch (event->xclient.data.l[0]) {
1044 case GSWindowLevelAttr:
1046 int level = (int)event->xclient.data.l[1];
1048 if (WINDOW_LEVEL(wwin) != level) {
1049 ChangeStackingLevel(wwin->frame->core, level);
1052 break;
1054 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1055 wwin = wWindowFor(event->xclient.window);
1056 if (!wwin)
1057 return;
1058 switch (event->xclient.data.l[0]) {
1059 case WMTitleBarNormal:
1060 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1061 break;
1062 case WMTitleBarMain:
1063 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1064 break;
1065 case WMTitleBarKey:
1066 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1067 break;
1069 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1070 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1071 if (!scr)
1072 return;
1073 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1074 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1075 /* do nothing */
1076 #ifdef XDND
1077 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1078 /* do nothing */
1079 #endif /* XDND */
1080 } else {
1082 * Non-standard thing, but needed by OffiX DND.
1083 * For when the icon frame gets a ClientMessage
1084 * that should have gone to the icon_window.
1086 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1087 struct WIcon *icon = NULL;
1089 if (desc->parent_type == WCLASS_MINIWINDOW) {
1090 icon = (WIcon *) desc->parent;
1091 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1092 icon = ((WAppIcon *) desc->parent)->icon;
1094 if (icon && (wwin = icon->owner)) {
1095 if (wwin->client_win != event->xclient.window) {
1096 event->xclient.window = wwin->client_win;
1097 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1104 static void raiseWindow(WScreen * scr)
1106 WWindow *wwin;
1108 scr->autoRaiseTimer = NULL;
1110 wwin = wWindowFor(scr->autoRaiseWindow);
1111 if (!wwin)
1112 return;
1114 if (!wwin->flags.destroyed && wwin->flags.focused) {
1115 wRaiseFrame(wwin->frame->core);
1116 /* this is needed or a race condition will occur */
1117 XSync(dpy, False);
1121 static void handleEnterNotify(XEvent * event)
1123 WWindow *wwin;
1124 WObjDescriptor *desc = NULL;
1125 XEvent ev;
1126 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1128 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1129 /* already left the window... */
1130 saveTimestamp(&ev);
1131 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1132 return;
1136 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1137 if (desc->handle_enternotify)
1138 (*desc->handle_enternotify) (desc, event);
1141 /* enter to window */
1142 wwin = wWindowFor(event->xcrossing.window);
1143 if (!wwin) {
1144 if (wPreferences.colormap_mode == WCM_POINTER) {
1145 wColormapInstallForWindow(scr, NULL);
1147 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1148 WMDeleteTimerHandler(scr->autoRaiseTimer);
1149 scr->autoRaiseTimer = NULL;
1151 } else {
1152 /* set auto raise timer even if in focus-follows-mouse mode
1153 * and the event is for the frame window, even if the window
1154 * has focus already. useful if you move the pointer from a focused
1155 * window to the root window and back pretty fast
1157 * set focus if in focus-follows-mouse mode and the event
1158 * is for the frame window and window doesn't have focus yet */
1159 if (wPreferences.focus_mode == WKF_SLOPPY
1160 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1162 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1163 wSetFocusTo(scr, wwin);
1165 if (scr->autoRaiseTimer)
1166 WMDeleteTimerHandler(scr->autoRaiseTimer);
1167 scr->autoRaiseTimer = NULL;
1169 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1170 scr->autoRaiseWindow = wwin->frame->core->window;
1171 scr->autoRaiseTimer
1172 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1175 /* Install colormap for window, if the colormap installation mode
1176 * is colormap_follows_mouse */
1177 if (wPreferences.colormap_mode == WCM_POINTER) {
1178 if (wwin->client_win == event->xcrossing.window)
1179 wColormapInstallForWindow(scr, wwin);
1180 else
1181 wColormapInstallForWindow(scr, NULL);
1185 if (event->xcrossing.window == event->xcrossing.root
1186 && event->xcrossing.detail == NotifyNormal
1187 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1189 wSetFocusTo(scr, scr->focused_window);
1191 #ifdef BALLOON_TEXT
1192 wBalloonEnteredObject(scr, desc);
1193 #endif
1196 static void handleLeaveNotify(XEvent * event)
1198 WObjDescriptor *desc = NULL;
1200 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1201 if (desc->handle_leavenotify)
1202 (*desc->handle_leavenotify) (desc, event);
1206 #ifdef USE_XSHAPE
1207 static void handleShapeNotify(XEvent * event)
1209 XShapeEvent *shev = (XShapeEvent *) event;
1210 WWindow *wwin;
1211 union {
1212 XEvent xevent;
1213 XShapeEvent xshape;
1214 } ev;
1216 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1217 if (ev.xshape.kind == ShapeBounding) {
1218 if (ev.xshape.shaped == shev->shaped) {
1219 *shev = ev.xshape;
1220 } else {
1221 XPutBackEvent(dpy, &ev.xevent);
1222 break;
1227 wwin = wWindowFor(shev->window);
1228 if (!wwin || shev->kind != ShapeBounding)
1229 return;
1231 if (!shev->shaped && wwin->flags.shaped) {
1233 wwin->flags.shaped = 0;
1234 wWindowClearShape(wwin);
1236 } else if (shev->shaped) {
1238 wwin->flags.shaped = 1;
1239 wWindowSetShape(wwin);
1242 #endif /* USE_XSHAPE */
1244 #ifdef KEEP_XKB_LOCK_STATUS
1245 /* please help ]d if you know what to do */
1246 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1248 WWindow *wwin;
1249 WScreen *scr;
1250 XkbStateRec staterec;
1251 int i;
1253 for (i = 0; i < w_global.screen_count; i++) {
1254 scr = wScreenWithNumber(i);
1255 wwin = scr->focused_window;
1256 if (wwin && wwin->flags.focused) {
1257 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1258 if (wwin->frame->languagemode != staterec.group) {
1259 wwin->frame->last_languagemode = wwin->frame->languagemode;
1260 wwin->frame->languagemode = staterec.group;
1262 #ifdef XKB_BUTTON_HINT
1263 if (wwin->frame->titlebar) {
1264 wFrameWindowPaint(wwin->frame);
1266 #endif
1270 #endif /*KEEP_XKB_LOCK_STATUS */
1272 static void handleColormapNotify(XEvent * event)
1274 WWindow *wwin;
1275 WScreen *scr;
1276 Bool reinstall = False;
1278 wwin = wWindowFor(event->xcolormap.window);
1279 if (!wwin)
1280 return;
1282 scr = wwin->screen_ptr;
1284 do {
1285 if (wwin) {
1286 if (event->xcolormap.new) {
1287 XWindowAttributes attr;
1289 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1291 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1292 scr->current_colormap = attr.colormap;
1294 reinstall = True;
1295 } else if (event->xcolormap.state == ColormapUninstalled &&
1296 scr->current_colormap == event->xcolormap.colormap) {
1298 /* some bastard app (like XV) removed our colormap */
1300 * can't enforce or things like xscreensaver wont work
1301 * reinstall = True;
1303 } else if (event->xcolormap.state == ColormapInstalled &&
1304 scr->current_colormap == event->xcolormap.colormap) {
1306 /* someone has put our colormap back */
1307 reinstall = False;
1310 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1311 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1313 if (reinstall && scr->current_colormap != None) {
1314 if (!scr->flags.colormap_stuff_blocked)
1315 XInstallColormap(dpy, scr->current_colormap);
1319 static void handleFocusIn(XEvent * event)
1321 WWindow *wwin;
1324 * For applications that like stealing the focus.
1326 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1327 saveTimestamp(event);
1328 if (event->xfocus.mode == NotifyUngrab
1329 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1330 return;
1333 wwin = wWindowFor(event->xfocus.window);
1334 if (wwin && !wwin->flags.focused) {
1335 if (wwin->flags.mapped)
1336 wSetFocusTo(wwin->screen_ptr, wwin);
1337 else
1338 wSetFocusTo(wwin->screen_ptr, NULL);
1339 } else if (!wwin) {
1340 WScreen *scr = wScreenForWindow(event->xfocus.window);
1341 if (scr)
1342 wSetFocusTo(scr, NULL);
1346 static WWindow *windowUnderPointer(WScreen * scr)
1348 unsigned int mask;
1349 int foo;
1350 Window bar, win;
1352 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1353 return wWindowFor(win);
1354 return NULL;
1357 static int CheckFullScreenWindowFocused(WScreen * scr)
1359 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1360 return 1;
1361 else
1362 return 0;
1365 static void handleKeyPress(XEvent * event)
1367 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1368 WWindow *wwin = scr->focused_window;
1369 short i, widx;
1370 int modifiers;
1371 int command = -1;
1372 #ifdef KEEP_XKB_LOCK_STATUS
1373 XkbStateRec staterec;
1374 #endif /*KEEP_XKB_LOCK_STATUS */
1376 /* ignore CapsLock */
1377 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1379 for (i = 0; i < WKBD_LAST; i++) {
1380 if (wKeyBindings[i].keycode == 0)
1381 continue;
1383 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1384 || */ wKeyBindings[i].modifier ==
1385 modifiers)) {
1386 command = i;
1387 break;
1391 if (command < 0) {
1393 if (!wRootMenuPerformShortcut(event)) {
1394 static int dontLoop = 0;
1396 if (dontLoop > 10) {
1397 wwarning("problem with key event processing code");
1398 return;
1400 dontLoop++;
1401 /* if the focused window is an internal window, try redispatching
1402 * the event to the managed window, as it can be a WINGs window */
1403 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1404 /* client_leader contains the WINGs toplevel */
1405 event->xany.window = wwin->client_leader;
1406 WMHandleEvent(event);
1408 dontLoop--;
1410 return;
1412 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1413 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1415 switch (command) {
1417 case WKBD_ROOTMENU:
1418 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1419 if (!CheckFullScreenWindowFocused(scr)) {
1420 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1421 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1422 True);
1424 break;
1425 case WKBD_WINDOWLIST:
1426 if (!CheckFullScreenWindowFocused(scr)) {
1427 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1428 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1429 True);
1431 break;
1433 case WKBD_WINDOWMENU:
1434 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1435 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1436 break;
1437 case WKBD_MINIMIZEALL:
1438 CloseWindowMenu(scr);
1439 wHideAll(scr);
1440 break;
1441 case WKBD_MINIATURIZE:
1442 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1443 && !WFLAGP(wwin, no_miniaturizable)) {
1444 CloseWindowMenu(scr);
1446 if (wwin->protocols.MINIATURIZE_WINDOW)
1447 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1448 else {
1449 wIconifyWindow(wwin);
1452 break;
1453 case WKBD_HIDE:
1454 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1455 WApplication *wapp = wApplicationOf(wwin->main_window);
1456 CloseWindowMenu(scr);
1458 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1459 wHideApplication(wapp);
1462 break;
1463 case WKBD_HIDE_OTHERS:
1464 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1465 CloseWindowMenu(scr);
1467 wHideOtherApplications(wwin);
1469 break;
1470 case WKBD_MAXIMIZE:
1471 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1472 CloseWindowMenu(scr);
1474 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1476 break;
1477 case WKBD_VMAXIMIZE:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1479 CloseWindowMenu(scr);
1481 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1483 break;
1484 case WKBD_HMAXIMIZE:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1490 break;
1491 case WKBD_LHMAXIMIZE:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 CloseWindowMenu(scr);
1495 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1497 break;
1498 case WKBD_RHMAXIMIZE:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1500 CloseWindowMenu(scr);
1502 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1504 break;
1505 case WKBD_THMAXIMIZE:
1506 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1507 CloseWindowMenu(scr);
1509 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1511 break;
1512 case WKBD_BHMAXIMIZE:
1513 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1514 CloseWindowMenu(scr);
1516 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1518 break;
1519 case WKBD_LTCMAXIMIZE:
1520 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1521 CloseWindowMenu(scr);
1523 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1525 break;
1526 case WKBD_RTCMAXIMIZE:
1527 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1528 CloseWindowMenu(scr);
1530 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1532 break;
1533 case WKBD_LBCMAXIMIZE:
1534 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1535 CloseWindowMenu(scr);
1537 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1539 break;
1540 case WKBD_RBCMAXIMIZE:
1541 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1542 CloseWindowMenu(scr);
1544 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1546 break;
1547 case WKBD_MAXIMUS:
1548 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1549 CloseWindowMenu(scr);
1551 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1553 break;
1554 case WKBD_OMNIPRESENT:
1555 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1556 CloseWindowMenu(scr);
1558 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1560 break;
1561 case WKBD_RAISE:
1562 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1563 CloseWindowMenu(scr);
1565 wRaiseFrame(wwin->frame->core);
1567 break;
1568 case WKBD_LOWER:
1569 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1570 CloseWindowMenu(scr);
1572 wLowerFrame(wwin->frame->core);
1574 break;
1575 case WKBD_RAISELOWER:
1576 /* raise or lower the window under the pointer, not the
1577 * focused one
1579 wwin = windowUnderPointer(scr);
1580 if (wwin)
1581 wRaiseLowerFrame(wwin->frame->core);
1582 break;
1583 case WKBD_SHADE:
1584 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1585 if (wwin->flags.shaded)
1586 wUnshadeWindow(wwin);
1587 else
1588 wShadeWindow(wwin);
1590 break;
1591 case WKBD_MOVERESIZE:
1592 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1593 CloseWindowMenu(scr);
1595 wKeyboardMoveResizeWindow(wwin);
1597 break;
1598 case WKBD_CLOSE:
1599 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1600 CloseWindowMenu(scr);
1601 if (wwin->protocols.DELETE_WINDOW)
1602 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1604 break;
1605 case WKBD_SELECT:
1606 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1607 wSelectWindow(wwin, !wwin->flags.selected);
1609 break;
1611 case WKBD_WORKSPACEMAP:
1612 if (!wPreferences.disable_workspace_pager)
1613 StartWorkspaceMap(scr);
1614 break;
1616 case WKBD_FOCUSNEXT:
1617 StartWindozeCycle(wwin, event, True, False);
1618 break;
1620 case WKBD_FOCUSPREV:
1621 StartWindozeCycle(wwin, event, False, False);
1622 break;
1624 case WKBD_GROUPNEXT:
1625 StartWindozeCycle(wwin, event, True, True);
1626 break;
1628 case WKBD_GROUPPREV:
1629 StartWindozeCycle(wwin, event, False, True);
1630 break;
1632 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1633 widx = command - WKBD_WORKSPACE1;
1634 i = (scr->current_workspace / 10) * 10 + widx;
1635 if (wPreferences.ws_advance || i < scr->workspace_count)
1636 wWorkspaceChange(scr, i);
1637 break;
1639 case WKBD_NEXTWORKSPACE:
1640 wWorkspaceRelativeChange(scr, 1);
1641 break;
1642 case WKBD_PREVWORKSPACE:
1643 wWorkspaceRelativeChange(scr, -1);
1644 break;
1645 case WKBD_LASTWORKSPACE:
1646 wWorkspaceChange(scr, scr->last_workspace);
1647 break;
1649 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1650 widx = command - WKBD_MOVE_WORKSPACE1;
1651 i = (scr->current_workspace / 10) * 10 + widx;
1652 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1653 wWindowChangeWorkspace(wwin, i);
1654 break;
1656 case WKBD_MOVE_NEXTWORKSPACE:
1657 if (wwin)
1658 wWindowChangeWorkspaceRelative(wwin, 1);
1659 break;
1660 case WKBD_MOVE_PREVWORKSPACE:
1661 if (wwin)
1662 wWindowChangeWorkspaceRelative(wwin, -1);
1663 break;
1664 case WKBD_MOVE_LASTWORKSPACE:
1665 if (wwin)
1666 wWindowChangeWorkspace(wwin, scr->last_workspace);
1667 break;
1669 case WKBD_MOVE_NEXTWSLAYER:
1670 case WKBD_MOVE_PREVWSLAYER:
1672 if (wwin) {
1673 int row, column;
1675 row = scr->current_workspace / 10;
1676 column = scr->current_workspace % 10;
1678 if (command == WKBD_MOVE_NEXTWSLAYER) {
1679 if ((row + 1) * 10 < scr->workspace_count)
1680 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1681 } else {
1682 if (row > 0)
1683 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1687 break;
1689 case WKBD_WINDOW1:
1690 case WKBD_WINDOW2:
1691 case WKBD_WINDOW3:
1692 case WKBD_WINDOW4:
1693 case WKBD_WINDOW5:
1694 case WKBD_WINDOW6:
1695 case WKBD_WINDOW7:
1696 case WKBD_WINDOW8:
1697 case WKBD_WINDOW9:
1698 case WKBD_WINDOW10:
1700 widx = command - WKBD_WINDOW1;
1702 if (scr->shortcutWindows[widx]) {
1703 WMArray *list = scr->shortcutWindows[widx];
1704 int cw;
1705 int count = WMGetArrayItemCount(list);
1706 WWindow *twin;
1707 WMArrayIterator iter;
1708 WWindow *wwin;
1710 wUnselectWindows(scr);
1711 cw = scr->current_workspace;
1713 WM_ETARETI_ARRAY(list, wwin, iter) {
1714 if (count > 1)
1715 wWindowChangeWorkspace(wwin, cw);
1717 wMakeWindowVisible(wwin);
1719 if (count > 1)
1720 wSelectWindow(wwin, True);
1723 /* rotate the order of windows, to create a cycling effect */
1724 twin = WMGetFromArray(list, 0);
1725 WMDeleteFromArray(list, 0);
1726 WMAddToArray(list, twin);
1728 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1729 if (scr->shortcutWindows[widx]) {
1730 WMFreeArray(scr->shortcutWindows[widx]);
1731 scr->shortcutWindows[widx] = NULL;
1734 if (wwin->flags.selected && scr->selected_windows) {
1735 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1736 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1737 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1738 } else {
1739 scr->shortcutWindows[widx] = WMCreateArray(4);
1740 WMAddToArray(scr->shortcutWindows[widx], wwin);
1743 wSelectWindow(wwin, !wwin->flags.selected);
1744 XFlush(dpy);
1745 wusleep(3000);
1746 wSelectWindow(wwin, !wwin->flags.selected);
1747 XFlush(dpy);
1749 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1751 if (wwin->flags.selected && scr->selected_windows) {
1752 if (scr->shortcutWindows[widx]) {
1753 WMFreeArray(scr->shortcutWindows[widx]);
1755 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1759 break;
1761 case WKBD_RELAUNCH:
1762 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1763 (void) RelaunchWindow(wwin);
1765 break;
1767 case WKBD_SWITCH_SCREEN:
1768 if (w_global.screen_count > 1) {
1769 WScreen *scr2;
1770 int i;
1772 /* find index of this screen */
1773 for (i = 0; i < w_global.screen_count; i++) {
1774 if (wScreenWithNumber(i) == scr)
1775 break;
1777 i++;
1778 if (i >= w_global.screen_count) {
1779 i = 0;
1781 scr2 = wScreenWithNumber(i);
1783 if (scr2) {
1784 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1785 scr2->scr_width / 2, scr2->scr_height / 2);
1788 break;
1790 case WKBD_RUN:
1792 char *cmdline;
1794 cmdline = ExpandOptions(scr, _("exec %a(Run,Type command to run:)"));
1796 if (cmdline) {
1797 XGrabPointer(dpy, scr->root_win, True, 0,
1798 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1799 XSync(dpy, False);
1801 ExecuteShellCommand(scr, cmdline);
1802 wfree(cmdline);
1804 XUngrabPointer(dpy, CurrentTime);
1805 XSync(dpy, False);
1807 break;
1810 case WKBD_NEXTWSLAYER:
1811 case WKBD_PREVWSLAYER:
1813 int row, column;
1815 row = scr->current_workspace / 10;
1816 column = scr->current_workspace % 10;
1818 if (command == WKBD_NEXTWSLAYER) {
1819 if ((row + 1) * 10 < scr->workspace_count)
1820 wWorkspaceChange(scr, column + (row + 1) * 10);
1821 } else {
1822 if (row > 0)
1823 wWorkspaceChange(scr, column + (row - 1) * 10);
1826 break;
1827 case WKBD_CLIPRAISELOWER:
1828 if (!wPreferences.flags.noclip)
1829 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1830 break;
1831 case WKBD_DOCKRAISELOWER:
1832 if (!wPreferences.flags.nodock)
1833 wDockRaiseLower(scr->dock);
1834 break;
1835 #ifdef KEEP_XKB_LOCK_STATUS
1836 case WKBD_TOGGLE:
1837 if (wPreferences.modelock) {
1838 /*toggle */
1839 wwin = scr->focused_window;
1841 if (wwin && wwin->flags.mapped
1842 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1843 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1844 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1846 wwin->frame->languagemode = wwin->frame->last_languagemode;
1847 wwin->frame->last_languagemode = staterec.group;
1848 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1852 break;
1853 #endif /* KEEP_XKB_LOCK_STATUS */
1857 static void handleMotionNotify(XEvent * event)
1859 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1861 if (wPreferences.scrollable_menus) {
1862 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1863 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1865 if (scr->flags.jump_back_pending ||
1866 p.x <= (rect.pos.x + 1) ||
1867 p.x >= (rect.pos.x + rect.size.width - 2) ||
1868 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1869 WMenu *menu;
1871 menu = wMenuUnderPointer(scr);
1872 if (menu != NULL)
1873 wMenuScroll(menu);
1878 static void handleVisibilityNotify(XEvent * event)
1880 WWindow *wwin;
1882 wwin = wWindowFor(event->xvisibility.window);
1883 if (!wwin)
1884 return;
1885 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);