wmaker: add new button and wheel mouse actions
[wmaker-crm.git] / src / event.c
blob6e50affcf3f8298d15ee736a65545c05a02b9517
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>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #ifdef USE_XSHAPE
40 # include <X11/extensions/shape.h>
41 #endif
42 #ifdef XDND
43 #include "xdnd.h"
44 #endif
46 #ifdef USE_RANDR
47 #include <X11/extensions/Xrandr.h>
48 #endif
50 #ifdef KEEP_XKB_LOCK_STATUS
51 #include <X11/XKBlib.h>
52 #endif /* KEEP_XKB_LOCK_STATUS */
54 #include "WindowMaker.h"
55 #include "window.h"
56 #include "actions.h"
57 #include "client.h"
58 #include "main.h"
59 #include "cycling.h"
60 #include "keybind.h"
61 #include "application.h"
62 #include "stacking.h"
63 #include "defaults.h"
64 #include "workspace.h"
65 #include "dock.h"
66 #include "framewin.h"
67 #include "properties.h"
68 #include "balloon.h"
69 #include "xinerama.h"
70 #include "wmspec.h"
71 #include "rootmenu.h"
72 #include "colormap.h"
73 #include "screen.h"
74 #include "shutdown.h"
75 #include "misc.h"
76 #include "event.h"
77 #include "winmenu.h"
78 #include "switchmenu.h"
81 #define MOD_MASK wPreferences.modifier_mask
83 /************ Local stuff ***********/
85 static void saveTimestamp(XEvent *event);
86 static void handleColormapNotify(XEvent *event);
87 static void handleMapNotify(XEvent *event);
88 static void handleUnmapNotify(XEvent *event);
89 static void handleButtonPress(XEvent *event);
90 static void handleExpose(XEvent *event);
91 static void handleDestroyNotify(XEvent *event);
92 static void handleConfigureRequest(XEvent *event);
93 static void handleMapRequest(XEvent *event);
94 static void handlePropertyNotify(XEvent *event);
95 static void handleEnterNotify(XEvent *event);
96 static void handleLeaveNotify(XEvent *event);
97 static void handleExtensions(XEvent *event);
98 static void handleClientMessage(XEvent *event);
99 static void handleKeyPress(XEvent *event);
100 static void handleFocusIn(XEvent *event);
101 static void handleMotionNotify(XEvent *event);
102 static void handleVisibilityNotify(XEvent *event);
103 static void handle_inotify_events(void);
104 static void wdelete_death_handler(WMagicNumber id);
107 #ifdef USE_XSHAPE
108 static void handleShapeNotify(XEvent *event);
109 #endif
111 #ifdef KEEP_XKB_LOCK_STATUS
112 static void handleXkbIndicatorStateNotify(XkbEvent *event);
113 #endif
115 /* real dead process handler */
116 static void handleDeadProcess(void);
118 typedef struct DeadProcesses {
119 pid_t pid;
120 unsigned char exit_status;
121 } DeadProcesses;
123 /* stack of dead processes */
124 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
125 static int deadProcessPtr = 0;
127 typedef struct DeathHandler {
128 WDeathHandler *callback;
129 pid_t pid;
130 void *client_data;
131 } DeathHandler;
133 static WMArray *deathHandlers = NULL;
135 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
137 DeathHandler *handler;
139 handler = malloc(sizeof(DeathHandler));
140 if (!handler)
141 return 0;
143 handler->pid = pid;
144 handler->callback = callback;
145 handler->client_data = cdata;
147 if (!deathHandlers)
148 deathHandlers = WMCreateArrayWithDestructor(8, free);
150 WMAddToArray(deathHandlers, handler);
152 return handler;
155 static void wdelete_death_handler(WMagicNumber id)
157 DeathHandler *handler = (DeathHandler *) id;
159 if (!handler || !deathHandlers)
160 return;
162 /* array destructor will call free(handler) */
163 WMRemoveFromArray(deathHandlers, handler);
166 void DispatchEvent(XEvent * event)
168 if (deathHandlers)
169 handleDeadProcess();
171 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
172 WCHANGE_STATE(WSTATE_EXITING);
173 /* received SIGTERM */
175 * WMHandleEvent() can't be called from anything
176 * executed inside here, or we can get in a infinite
177 * recursive loop.
179 Shutdown(WSExitMode);
181 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
182 WCHANGE_STATE(WSTATE_RESTARTING);
184 Shutdown(WSRestartPreparationMode);
185 /* received SIGHUP */
186 Restart(NULL, True);
187 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
188 WCHANGE_STATE(WSTATE_NORMAL);
189 wDefaultsCheckDomains(NULL);
192 /* for the case that all that is wanted to be dispatched is
193 * the stuff above */
194 if (!event)
195 return;
197 saveTimestamp(event);
198 switch (event->type) {
199 case MapRequest:
200 handleMapRequest(event);
201 break;
203 case KeyPress:
204 handleKeyPress(event);
205 break;
207 case MotionNotify:
208 handleMotionNotify(event);
209 break;
211 case ConfigureRequest:
212 handleConfigureRequest(event);
213 break;
215 case DestroyNotify:
216 handleDestroyNotify(event);
217 break;
219 case MapNotify:
220 handleMapNotify(event);
221 break;
223 case UnmapNotify:
224 handleUnmapNotify(event);
225 break;
227 case ButtonPress:
228 handleButtonPress(event);
229 break;
231 case Expose:
232 handleExpose(event);
233 break;
235 case PropertyNotify:
236 handlePropertyNotify(event);
237 break;
239 case EnterNotify:
240 handleEnterNotify(event);
241 break;
243 case LeaveNotify:
244 handleLeaveNotify(event);
245 break;
247 case ClientMessage:
248 handleClientMessage(event);
249 break;
251 case ColormapNotify:
252 handleColormapNotify(event);
253 break;
255 case MappingNotify:
256 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
257 XRefreshKeyboardMapping(&event->xmapping);
258 break;
260 case FocusIn:
261 handleFocusIn(event);
262 break;
264 case VisibilityNotify:
265 handleVisibilityNotify(event);
266 break;
268 case ConfigureNotify:
269 #ifdef USE_RANDR
270 if (event->xconfigure.window == DefaultRootWindow(dpy))
271 XRRUpdateConfiguration(event);
272 #endif
273 break;
275 default:
276 handleExtensions(event);
277 break;
281 #ifdef HAVE_INOTIFY
283 *----------------------------------------------------------------------
284 * handle_inotify_events-
285 * Check for inotify events
287 * Returns:
288 * After reading events for the given file descriptor (fd) and
289 * watch descriptor (wd)
291 * Side effects:
292 * Calls wDefaultsCheckDomains if config database is updated
293 *----------------------------------------------------------------------
295 static void handle_inotify_events(void)
297 ssize_t eventQLength, i = 0;
298 /* Make room for at lease 5 simultaneous events, with path + filenames */
299 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
300 /* Check config only once per read of the event queue */
301 int oneShotFlag = 0;
304 * Read off the queued events
305 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
306 * not occur; the block is on Xevents, but a config file change will normally
307 * occur as a result of an Xevent - so the event queue should never have more than
308 * a few entries before a read().
310 eventQLength = read(w_global.inotify.fd_event_queue,
311 buff, sizeof(buff) );
313 /* check what events occured */
314 /* Should really check wd here too, but for now we only have one watch! */
315 while (i < eventQLength) {
316 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
319 * see inotify.h for event types.
321 if (pevent->mask & IN_DELETE_SELF) {
322 wwarning(_("the defaults database has been deleted!"
323 " Restart Window Maker to create the database" " with the default settings"));
325 if (w_global.inotify.fd_event_queue >= 0) {
326 close(w_global.inotify.fd_event_queue);
327 w_global.inotify.fd_event_queue = -1;
330 if (pevent->mask & IN_UNMOUNT) {
331 wwarning(_("the unit containing the defaults database has"
332 " been unmounted. Setting --static mode." " Any changes will not be saved."));
334 if (w_global.inotify.fd_event_queue >= 0) {
335 close(w_global.inotify.fd_event_queue);
336 w_global.inotify.fd_event_queue = -1;
339 wPreferences.flags.noupdates = 1;
341 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
342 wwarning(_("Inotify: Reading config files in defaults database."));
343 wDefaultsCheckDomains(NULL);
344 oneShotFlag = 1;
347 /* move to next event in the buffer */
348 i += sizeof(struct inotify_event) + pevent->len;
351 #endif /* HAVE_INOTIFY */
354 *----------------------------------------------------------------------
355 * EventLoop-
356 * Processes X and internal events indefinitely.
358 * Returns:
359 * Never returns
361 * Side effects:
362 * The LastTimestamp global variable is updated.
363 * Calls inotifyGetEvents if defaults database changes.
364 *----------------------------------------------------------------------
366 noreturn void EventLoop(void)
368 XEvent event;
369 #ifdef HAVE_INOTIFY
370 struct timeval time;
371 fd_set rfds;
372 int retVal = 0;
374 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
375 retVal = -1;
376 #endif
378 for (;;) {
380 WMNextEvent(dpy, &event); /* Blocks here */
381 WMHandleEvent(&event);
382 #ifdef HAVE_INOTIFY
383 if (retVal != -1) {
384 time.tv_sec = 0;
385 time.tv_usec = 0;
386 FD_ZERO(&rfds);
387 FD_SET(w_global.inotify.fd_event_queue, &rfds);
389 /* check for available read data from inotify - don't block! */
390 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
392 if (retVal < 0) { /* an error has occured */
393 wwarning(_("select failed. The inotify instance will be closed."
394 " Changes to the defaults database will require"
395 " a restart to take effect."));
396 close(w_global.inotify.fd_event_queue);
397 w_global.inotify.fd_event_queue = -1;
398 continue;
400 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
401 handle_inotify_events();
403 #endif
408 *----------------------------------------------------------------------
409 * ProcessPendingEvents --
410 * Processes the events that are currently pending (at the time
411 * this function is called) in the display's queue.
413 * Returns:
414 * After the pending events that were present at the function call
415 * are processed.
417 * Side effects:
418 * Many -- whatever handling events may involve.
420 *----------------------------------------------------------------------
422 void ProcessPendingEvents(void)
424 XEvent event;
425 int count;
427 XSync(dpy, False);
429 /* Take a snapshot of the event count in the queue */
430 count = XPending(dpy);
432 while (count > 0 && XPending(dpy)) {
433 WMNextEvent(dpy, &event);
434 WMHandleEvent(&event);
435 count--;
439 Bool IsDoubleClick(WScreen * scr, XEvent * event)
441 if ((scr->last_click_time > 0) &&
442 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
443 && (event->xbutton.button == scr->last_click_button)
444 && (event->xbutton.window == scr->last_click_window)) {
446 scr->flags.next_click_is_not_double = 1;
447 scr->last_click_time = 0;
448 scr->last_click_window = event->xbutton.window;
450 return True;
452 return False;
455 void NotifyDeadProcess(pid_t pid, unsigned char status)
457 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
458 wwarning("stack overflow: too many dead processes");
459 return;
461 /* stack the process to be handled later,
462 * as this is called from the signal handler */
463 deadProcesses[deadProcessPtr].pid = pid;
464 deadProcesses[deadProcessPtr].exit_status = status;
465 deadProcessPtr++;
468 static void handleDeadProcess(void)
470 DeathHandler *tmp;
471 int i;
473 for (i = 0; i < deadProcessPtr; i++) {
474 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
477 if (!deathHandlers) {
478 deadProcessPtr = 0;
479 return;
482 /* get the pids on the queue and call handlers */
483 while (deadProcessPtr > 0) {
484 deadProcessPtr--;
486 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
487 tmp = WMGetFromArray(deathHandlers, i);
488 if (!tmp)
489 continue;
491 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
492 (*tmp->callback) (tmp->pid,
493 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
494 wdelete_death_handler(tmp);
500 static void saveTimestamp(XEvent * event)
503 * Never save CurrentTime as LastTimestamp because CurrentTime
504 * it's not a real timestamp (it's the 0L constant)
507 switch (event->type) {
508 case ButtonRelease:
509 case ButtonPress:
510 w_global.timestamp.last_event = event->xbutton.time;
511 break;
512 case KeyPress:
513 case KeyRelease:
514 w_global.timestamp.last_event = event->xkey.time;
515 break;
516 case MotionNotify:
517 w_global.timestamp.last_event = event->xmotion.time;
518 break;
519 case PropertyNotify:
520 w_global.timestamp.last_event = event->xproperty.time;
521 break;
522 case EnterNotify:
523 case LeaveNotify:
524 w_global.timestamp.last_event = event->xcrossing.time;
525 break;
526 case SelectionClear:
527 w_global.timestamp.last_event = event->xselectionclear.time;
528 break;
529 case SelectionRequest:
530 w_global.timestamp.last_event = event->xselectionrequest.time;
531 break;
532 case SelectionNotify:
533 w_global.timestamp.last_event = event->xselection.time;
534 #ifdef XDND
535 wXDNDProcessSelection(event);
536 #endif
537 break;
541 static int matchWindow(const void *item, const void *cdata)
543 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
546 static void handleExtensions(XEvent * event)
548 #ifdef USE_XSHAPE
549 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
550 handleShapeNotify(event);
552 #endif
553 #ifdef KEEP_XKB_LOCK_STATUS
554 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
555 handleXkbIndicatorStateNotify((XkbEvent *) event);
557 #endif /*KEEP_XKB_LOCK_STATUS */
558 #ifdef USE_RANDR
559 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
560 /* From xrandr man page: "Clients must call back into Xlib using
561 * XRRUpdateConfiguration when screen configuration change notify
562 * events are generated */
563 XRRUpdateConfiguration(event);
564 WCHANGE_STATE(WSTATE_RESTARTING);
565 Shutdown(WSRestartPreparationMode);
566 Restart(NULL,True);
568 #endif
571 static void handleMapRequest(XEvent * ev)
573 WWindow *wwin;
574 WScreen *scr = NULL;
575 Window window = ev->xmaprequest.window;
577 if ((wwin = wWindowFor(window))) {
578 if (wwin->flags.shaded) {
579 wUnshadeWindow(wwin);
581 /* deiconify window */
582 if (wwin->flags.miniaturized) {
583 wDeiconifyWindow(wwin);
584 } else if (wwin->flags.hidden) {
585 WApplication *wapp = wApplicationOf(wwin->main_window);
586 /* go to the last workspace that the user worked on the app */
587 if (wapp) {
588 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
590 wUnhideApplication(wapp, False, False);
592 return;
595 scr = wScreenForRootWindow(ev->xmaprequest.parent);
597 wwin = wManageWindow(scr, window);
600 * This is to let the Dock know that the application it launched
601 * has already been mapped (eg: it has finished launching).
602 * It is not necessary for normally docked apps, but is needed for
603 * apps that were forcedly docked (like with dockit).
605 if (scr->last_dock) {
606 if (wwin && wwin->main_window != None && wwin->main_window != window)
607 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
608 else
609 wDockTrackWindowLaunch(scr->last_dock, window);
612 if (wwin) {
613 wClientSetState(wwin, NormalState, None);
614 if (wwin->flags.maximized) {
615 wMaximizeWindow(wwin, wwin->flags.maximized);
617 if (wwin->flags.shaded) {
618 wwin->flags.shaded = 0;
619 wwin->flags.skip_next_animation = 1;
620 wShadeWindow(wwin);
622 if (wwin->flags.miniaturized) {
623 wwin->flags.miniaturized = 0;
624 wwin->flags.skip_next_animation = 1;
625 wIconifyWindow(wwin);
627 if (wwin->flags.fullscreen) {
628 wwin->flags.fullscreen = 0;
629 wFullscreenWindow(wwin);
631 if (wwin->flags.hidden) {
632 WApplication *wapp = wApplicationOf(wwin->main_window);
634 wwin->flags.hidden = 0;
635 wwin->flags.skip_next_animation = 1;
636 if (wapp) {
637 wHideApplication(wapp);
643 static void handleDestroyNotify(XEvent * event)
645 WWindow *wwin;
646 WApplication *app;
647 Window window = event->xdestroywindow.window;
648 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
649 int widx;
651 wwin = wWindowFor(window);
652 if (wwin) {
653 wUnmanageWindow(wwin, False, True);
656 if (scr != NULL) {
657 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
658 WFakeGroupLeader *fPtr;
660 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
661 if (fPtr->retainCount > 0) {
662 fPtr->retainCount--;
663 if (fPtr->retainCount == 0 && fPtr->leader != None) {
664 XDestroyWindow(dpy, fPtr->leader);
665 fPtr->leader = None;
666 XFlush(dpy);
669 fPtr->origLeader = None;
673 app = wApplicationOf(window);
674 if (app) {
675 if (window == app->main_window) {
676 app->refcount = 0;
677 wwin = app->main_window_desc->screen_ptr->focused_window;
678 while (wwin) {
679 if (wwin->main_window == window) {
680 wwin->main_window = None;
682 wwin = wwin->prev;
685 wApplicationDestroy(app);
689 static void handleExpose(XEvent * event)
691 WObjDescriptor *desc;
692 XEvent ev;
694 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
696 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
697 return;
700 if (desc->handle_expose) {
701 (*desc->handle_expose) (desc, event);
705 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
707 WWindow *wwin;
708 Bool next_direction;
710 if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
711 next_direction = False;
712 else
713 next_direction = True;
715 switch (action) {
716 case WA_SWITCH_WORKSPACES:
717 if (next_direction)
718 wWorkspaceRelativeChange(scr, 1);
719 else
720 wWorkspaceRelativeChange(scr, -1);
721 break;
723 case WA_SWITCH_WINDOWS:
724 wwin = scr->focused_window;
725 if (next_direction)
726 wWindowFocusNext(wwin, True);
727 else
728 wWindowFocusPrev(wwin, True);
729 break;
733 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
735 WWindow *wwin;
737 switch (action) {
738 case WA_SELECT_WINDOWS:
739 wUnselectWindows(scr);
740 wSelectWindows(scr, event);
741 break;
742 case WA_OPEN_APPMENU:
743 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
744 /* ugly hack */
745 if (scr->root_menu) {
746 if (scr->root_menu->brother->flags.mapped)
747 event->xbutton.window = scr->root_menu->brother->frame->core->window;
748 else
749 event->xbutton.window = scr->root_menu->frame->core->window;
751 break;
752 case WA_OPEN_WINLISTMENU:
753 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
754 if (scr->switch_menu) {
755 if (scr->switch_menu->brother->flags.mapped)
756 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
757 else
758 event->xbutton.window = scr->switch_menu->frame->core->window;
760 break;
761 case WA_MOVE_PREVWORKSPACE:
762 wWorkspaceRelativeChange(scr, -1);
763 break;
764 case WA_MOVE_NEXTWORKSPACE:
765 wWorkspaceRelativeChange(scr, 1);
766 break;
767 case WA_MOVE_PREVWINDOW:
768 wwin = scr->focused_window;
769 wWindowFocusPrev(wwin, True);
770 break;
771 case WA_MOVE_NEXTWINDOW:
772 wwin = scr->focused_window;
773 wWindowFocusNext(wwin, True);
774 break;
778 /* bindable */
779 static void handleButtonPress(XEvent * event)
781 WObjDescriptor *desc;
782 WScreen *scr;
784 scr = wScreenForRootWindow(event->xbutton.root);
786 #ifdef BALLOON_TEXT
787 wBalloonHide(scr);
788 #endif
790 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
791 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
792 executeButtonAction(scr, event, wPreferences.mouse_button1);
793 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
794 executeButtonAction(scr, event, wPreferences.mouse_button2);
795 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
796 executeButtonAction(scr, event, wPreferences.mouse_button3);
797 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
798 executeButtonAction(scr, event, wPreferences.mouse_button8);
799 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
800 executeButtonAction(scr, event, wPreferences.mouse_button9);
801 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
802 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
803 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
804 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
805 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
806 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
807 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
808 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
812 desc = NULL;
813 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
814 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
815 return;
819 if (desc->parent_type == WCLASS_WINDOW) {
820 XSync(dpy, 0);
822 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
823 XAllowEvents(dpy, AsyncPointer, CurrentTime);
824 } else {
825 /* if (wPreferences.focus_mode == WKF_CLICK) { */
826 if (wPreferences.ignore_focus_click) {
827 XAllowEvents(dpy, AsyncPointer, CurrentTime);
829 XAllowEvents(dpy, ReplayPointer, CurrentTime);
830 /* } */
832 XSync(dpy, 0);
833 } else if (desc->parent_type == WCLASS_APPICON
834 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
835 if (event->xbutton.state & MOD_MASK) {
836 XSync(dpy, 0);
837 XAllowEvents(dpy, AsyncPointer, CurrentTime);
838 XSync(dpy, 0);
842 if (desc->handle_mousedown != NULL) {
843 (*desc->handle_mousedown) (desc, event);
846 /* save double-click information */
847 if (scr->flags.next_click_is_not_double) {
848 scr->flags.next_click_is_not_double = 0;
849 } else {
850 scr->last_click_time = event->xbutton.time;
851 scr->last_click_button = event->xbutton.button;
852 scr->last_click_window = event->xbutton.window;
856 static void handleMapNotify(XEvent * event)
858 WWindow *wwin;
860 wwin = wWindowFor(event->xmap.event);
861 if (wwin && wwin->client_win == event->xmap.event) {
862 if (wwin->flags.miniaturized) {
863 wDeiconifyWindow(wwin);
864 } else {
865 XGrabServer(dpy);
866 wWindowMap(wwin);
867 wClientSetState(wwin, NormalState, None);
868 XUngrabServer(dpy);
873 static void handleUnmapNotify(XEvent * event)
875 WWindow *wwin;
876 XEvent ev;
877 Bool withdraw = False;
879 /* only process windows with StructureNotify selected
880 * (ignore SubstructureNotify) */
881 wwin = wWindowFor(event->xunmap.window);
882 if (!wwin)
883 return;
885 /* whether the event is a Withdrawal request */
886 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
887 withdraw = True;
889 if (wwin->client_win != event->xunmap.event && !withdraw)
890 return;
892 if (!wwin->flags.mapped && !withdraw
893 && wwin->frame->workspace == w_global.workspace.current
894 && !wwin->flags.miniaturized && !wwin->flags.hidden)
895 return;
897 XGrabServer(dpy);
898 XUnmapWindow(dpy, wwin->frame->core->window);
899 wwin->flags.mapped = 0;
900 XSync(dpy, 0);
901 /* check if the window was destroyed */
902 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
903 DispatchEvent(&ev);
904 } else {
905 Bool reparented = False;
907 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
908 reparented = True;
910 /* withdraw window */
911 wwin->flags.mapped = 0;
912 if (!reparented)
913 wClientSetState(wwin, WithdrawnState, None);
915 /* if the window was reparented, do not reparent it back to the
916 * root window */
917 wUnmanageWindow(wwin, !reparented, False);
919 XUngrabServer(dpy);
922 static void handleConfigureRequest(XEvent * event)
924 WWindow *wwin;
926 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
928 * Configure request for unmapped window
930 wClientConfigure(NULL, &(event->xconfigurerequest));
931 } else {
932 wClientConfigure(wwin, &(event->xconfigurerequest));
936 static void handlePropertyNotify(XEvent * event)
938 WWindow *wwin;
939 WApplication *wapp;
940 Window jr;
941 int ji;
942 unsigned int ju;
944 wwin = wWindowFor(event->xproperty.window);
945 if (wwin) {
946 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
947 return;
949 wClientCheckProperty(wwin, &event->xproperty);
951 wapp = wApplicationOf(event->xproperty.window);
952 if (wapp) {
953 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
957 static void handleClientMessage(XEvent * event)
959 WWindow *wwin;
960 WObjDescriptor *desc;
962 /* handle transition from Normal to Iconic state */
963 if (event->xclient.message_type == w_global.atom.wm.change_state
964 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
966 wwin = wWindowFor(event->xclient.window);
967 if (!wwin)
968 return;
969 if (!wwin->flags.miniaturized)
970 wIconifyWindow(wwin);
971 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
972 WScreen *scr = wScreenForRootWindow(event->xclient.window);
974 if (!scr)
975 return;
977 if (event->xclient.data.l[1] == 1) { /* starting */
978 wColormapAllowClientInstallation(scr, True);
979 } else { /* stopping */
980 wColormapAllowClientInstallation(scr, False);
982 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
984 char *command;
985 size_t len;
987 len = sizeof(event->xclient.data.b) + 1;
988 command = wmalloc(len);
989 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
991 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
992 wwarning(_("Got Reconfigure command"));
993 wDefaultsCheckDomains(NULL);
994 } else {
995 wwarning(_("Got unknown command %s"), command);
998 wfree(command);
1000 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1001 WApplication *wapp;
1002 int done = 0;
1003 wapp = wApplicationOf(event->xclient.window);
1004 if (wapp) {
1005 switch (event->xclient.data.l[0]) {
1006 case WMFHideOtherApplications:
1007 wHideOtherApplications(wapp->main_window_desc);
1008 done = 1;
1009 break;
1011 case WMFHideApplication:
1012 wHideApplication(wapp);
1013 done = 1;
1014 break;
1017 if (!done) {
1018 wwin = wWindowFor(event->xclient.window);
1019 if (wwin) {
1020 switch (event->xclient.data.l[0]) {
1021 case WMFHideOtherApplications:
1022 wHideOtherApplications(wwin);
1023 break;
1025 case WMFHideApplication:
1026 wHideApplication(wApplicationOf(wwin->main_window));
1027 break;
1031 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1032 wwin = wWindowFor(event->xclient.window);
1033 if (!wwin)
1034 return;
1035 switch (event->xclient.data.l[0]) {
1036 case GSWindowLevelAttr:
1038 int level = (int)event->xclient.data.l[1];
1040 if (WINDOW_LEVEL(wwin) != level) {
1041 ChangeStackingLevel(wwin->frame->core, level);
1044 break;
1046 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1047 wwin = wWindowFor(event->xclient.window);
1048 if (!wwin)
1049 return;
1050 switch (event->xclient.data.l[0]) {
1051 case WMTitleBarNormal:
1052 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1053 break;
1054 case WMTitleBarMain:
1055 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1056 break;
1057 case WMTitleBarKey:
1058 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1059 break;
1061 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1062 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1063 if (!scr)
1064 return;
1065 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1066 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1067 /* do nothing */
1068 #ifdef XDND
1069 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1070 /* do nothing */
1071 #endif /* XDND */
1072 } else {
1074 * Non-standard thing, but needed by OffiX DND.
1075 * For when the icon frame gets a ClientMessage
1076 * that should have gone to the icon_window.
1078 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1079 struct WIcon *icon = NULL;
1081 if (desc->parent_type == WCLASS_MINIWINDOW) {
1082 icon = (WIcon *) desc->parent;
1083 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1084 icon = ((WAppIcon *) desc->parent)->icon;
1086 if (icon && (wwin = icon->owner)) {
1087 if (wwin->client_win != event->xclient.window) {
1088 event->xclient.window = wwin->client_win;
1089 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1096 static void raiseWindow(WScreen * scr)
1098 WWindow *wwin;
1100 scr->autoRaiseTimer = NULL;
1102 wwin = wWindowFor(scr->autoRaiseWindow);
1103 if (!wwin)
1104 return;
1106 if (!wwin->flags.destroyed && wwin->flags.focused) {
1107 wRaiseFrame(wwin->frame->core);
1108 /* this is needed or a race condition will occur */
1109 XSync(dpy, False);
1113 static void handleEnterNotify(XEvent * event)
1115 WWindow *wwin;
1116 WObjDescriptor *desc = NULL;
1117 XEvent ev;
1118 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1120 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1121 /* already left the window... */
1122 saveTimestamp(&ev);
1123 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1124 return;
1128 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1129 if (desc->handle_enternotify)
1130 (*desc->handle_enternotify) (desc, event);
1133 /* enter to window */
1134 wwin = wWindowFor(event->xcrossing.window);
1135 if (!wwin) {
1136 if (wPreferences.colormap_mode == WCM_POINTER) {
1137 wColormapInstallForWindow(scr, NULL);
1139 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1140 WMDeleteTimerHandler(scr->autoRaiseTimer);
1141 scr->autoRaiseTimer = NULL;
1143 } else {
1144 /* set auto raise timer even if in focus-follows-mouse mode
1145 * and the event is for the frame window, even if the window
1146 * has focus already. useful if you move the pointer from a focused
1147 * window to the root window and back pretty fast
1149 * set focus if in focus-follows-mouse mode and the event
1150 * is for the frame window and window doesn't have focus yet */
1151 if (wPreferences.focus_mode == WKF_SLOPPY
1152 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1154 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1155 wSetFocusTo(scr, wwin);
1157 if (scr->autoRaiseTimer)
1158 WMDeleteTimerHandler(scr->autoRaiseTimer);
1159 scr->autoRaiseTimer = NULL;
1161 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1162 scr->autoRaiseWindow = wwin->frame->core->window;
1163 scr->autoRaiseTimer
1164 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1167 /* Install colormap for window, if the colormap installation mode
1168 * is colormap_follows_mouse */
1169 if (wPreferences.colormap_mode == WCM_POINTER) {
1170 if (wwin->client_win == event->xcrossing.window)
1171 wColormapInstallForWindow(scr, wwin);
1172 else
1173 wColormapInstallForWindow(scr, NULL);
1177 if (event->xcrossing.window == event->xcrossing.root
1178 && event->xcrossing.detail == NotifyNormal
1179 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1181 wSetFocusTo(scr, scr->focused_window);
1183 #ifdef BALLOON_TEXT
1184 wBalloonEnteredObject(scr, desc);
1185 #endif
1188 static void handleLeaveNotify(XEvent * event)
1190 WObjDescriptor *desc = NULL;
1192 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1193 if (desc->handle_leavenotify)
1194 (*desc->handle_leavenotify) (desc, event);
1198 #ifdef USE_XSHAPE
1199 static void handleShapeNotify(XEvent * event)
1201 XShapeEvent *shev = (XShapeEvent *) event;
1202 WWindow *wwin;
1203 union {
1204 XEvent xevent;
1205 XShapeEvent xshape;
1206 } ev;
1208 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1209 if (ev.xshape.kind == ShapeBounding) {
1210 if (ev.xshape.shaped == shev->shaped) {
1211 *shev = ev.xshape;
1212 } else {
1213 XPutBackEvent(dpy, &ev.xevent);
1214 break;
1219 wwin = wWindowFor(shev->window);
1220 if (!wwin || shev->kind != ShapeBounding)
1221 return;
1223 if (!shev->shaped && wwin->flags.shaped) {
1225 wwin->flags.shaped = 0;
1226 wWindowClearShape(wwin);
1228 } else if (shev->shaped) {
1230 wwin->flags.shaped = 1;
1231 wWindowSetShape(wwin);
1234 #endif /* USE_XSHAPE */
1236 #ifdef KEEP_XKB_LOCK_STATUS
1237 /* please help ]d if you know what to do */
1238 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1240 WWindow *wwin;
1241 WScreen *scr;
1242 XkbStateRec staterec;
1243 int i;
1245 for (i = 0; i < w_global.screen_count; i++) {
1246 scr = wScreenWithNumber(i);
1247 wwin = scr->focused_window;
1248 if (wwin && wwin->flags.focused) {
1249 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1250 if (wwin->frame->languagemode != staterec.group) {
1251 wwin->frame->last_languagemode = wwin->frame->languagemode;
1252 wwin->frame->languagemode = staterec.group;
1254 #ifdef XKB_BUTTON_HINT
1255 if (wwin->frame->titlebar) {
1256 wFrameWindowPaint(wwin->frame);
1258 #endif
1262 #endif /*KEEP_XKB_LOCK_STATUS */
1264 static void handleColormapNotify(XEvent * event)
1266 WWindow *wwin;
1267 WScreen *scr;
1268 Bool reinstall = False;
1270 wwin = wWindowFor(event->xcolormap.window);
1271 if (!wwin)
1272 return;
1274 scr = wwin->screen_ptr;
1276 do {
1277 if (wwin) {
1278 if (event->xcolormap.new) {
1279 XWindowAttributes attr;
1281 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1283 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1284 scr->current_colormap = attr.colormap;
1286 reinstall = True;
1287 } else if (event->xcolormap.state == ColormapUninstalled &&
1288 scr->current_colormap == event->xcolormap.colormap) {
1290 /* some bastard app (like XV) removed our colormap */
1292 * can't enforce or things like xscreensaver wont work
1293 * reinstall = True;
1295 } else if (event->xcolormap.state == ColormapInstalled &&
1296 scr->current_colormap == event->xcolormap.colormap) {
1298 /* someone has put our colormap back */
1299 reinstall = False;
1302 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1303 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1305 if (reinstall && scr->current_colormap != None) {
1306 if (!scr->flags.colormap_stuff_blocked)
1307 XInstallColormap(dpy, scr->current_colormap);
1311 static void handleFocusIn(XEvent * event)
1313 WWindow *wwin;
1316 * For applications that like stealing the focus.
1318 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1319 saveTimestamp(event);
1320 if (event->xfocus.mode == NotifyUngrab
1321 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1322 return;
1325 wwin = wWindowFor(event->xfocus.window);
1326 if (wwin && !wwin->flags.focused) {
1327 if (wwin->flags.mapped)
1328 wSetFocusTo(wwin->screen_ptr, wwin);
1329 else
1330 wSetFocusTo(wwin->screen_ptr, NULL);
1331 } else if (!wwin) {
1332 WScreen *scr = wScreenForWindow(event->xfocus.window);
1333 if (scr)
1334 wSetFocusTo(scr, NULL);
1338 static WWindow *windowUnderPointer(WScreen * scr)
1340 unsigned int mask;
1341 int foo;
1342 Window bar, win;
1344 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1345 return wWindowFor(win);
1346 return NULL;
1349 static int CheckFullScreenWindowFocused(WScreen * scr)
1351 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1352 return 1;
1353 else
1354 return 0;
1357 static void handleKeyPress(XEvent * event)
1359 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1360 WWindow *wwin = scr->focused_window;
1361 short i, widx;
1362 int modifiers;
1363 int command = -1;
1364 #ifdef KEEP_XKB_LOCK_STATUS
1365 XkbStateRec staterec;
1366 #endif /*KEEP_XKB_LOCK_STATUS */
1368 /* ignore CapsLock */
1369 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1371 for (i = 0; i < WKBD_LAST; i++) {
1372 if (wKeyBindings[i].keycode == 0)
1373 continue;
1375 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1376 || */ wKeyBindings[i].modifier ==
1377 modifiers)) {
1378 command = i;
1379 break;
1383 if (command < 0) {
1385 if (!wRootMenuPerformShortcut(event)) {
1386 static int dontLoop = 0;
1388 if (dontLoop > 10) {
1389 wwarning("problem with key event processing code");
1390 return;
1392 dontLoop++;
1393 /* if the focused window is an internal window, try redispatching
1394 * the event to the managed window, as it can be a WINGs window */
1395 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1396 /* client_leader contains the WINGs toplevel */
1397 event->xany.window = wwin->client_leader;
1398 WMHandleEvent(event);
1400 dontLoop--;
1402 return;
1404 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1405 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1407 switch (command) {
1409 case WKBD_ROOTMENU:
1410 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1411 if (!CheckFullScreenWindowFocused(scr)) {
1412 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1413 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1414 True);
1416 break;
1417 case WKBD_WINDOWLIST:
1418 if (!CheckFullScreenWindowFocused(scr)) {
1419 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1420 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1421 True);
1423 break;
1425 case WKBD_WINDOWMENU:
1426 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1427 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1428 break;
1429 case WKBD_MINIMIZEALL:
1430 CloseWindowMenu(scr);
1431 wHideAll(scr);
1432 break;
1433 case WKBD_MINIATURIZE:
1434 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1435 && !WFLAGP(wwin, no_miniaturizable)) {
1436 CloseWindowMenu(scr);
1438 if (wwin->protocols.MINIATURIZE_WINDOW)
1439 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1440 else {
1441 wIconifyWindow(wwin);
1444 break;
1445 case WKBD_HIDE:
1446 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1447 WApplication *wapp = wApplicationOf(wwin->main_window);
1448 CloseWindowMenu(scr);
1450 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1451 wHideApplication(wapp);
1454 break;
1455 case WKBD_HIDE_OTHERS:
1456 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1457 CloseWindowMenu(scr);
1459 wHideOtherApplications(wwin);
1461 break;
1462 case WKBD_MAXIMIZE:
1463 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1464 CloseWindowMenu(scr);
1466 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1468 break;
1469 case WKBD_VMAXIMIZE:
1470 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1471 CloseWindowMenu(scr);
1473 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1475 break;
1476 case WKBD_HMAXIMIZE:
1477 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1478 CloseWindowMenu(scr);
1480 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1482 break;
1483 case WKBD_LHMAXIMIZE:
1484 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1485 CloseWindowMenu(scr);
1487 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1489 break;
1490 case WKBD_RHMAXIMIZE:
1491 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1492 CloseWindowMenu(scr);
1494 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1496 break;
1497 case WKBD_THMAXIMIZE:
1498 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1499 CloseWindowMenu(scr);
1501 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1503 break;
1504 case WKBD_BHMAXIMIZE:
1505 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1506 CloseWindowMenu(scr);
1508 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1510 break;
1511 case WKBD_LTCMAXIMIZE:
1512 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1513 CloseWindowMenu(scr);
1515 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1517 break;
1518 case WKBD_RTCMAXIMIZE:
1519 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1520 CloseWindowMenu(scr);
1522 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1524 break;
1525 case WKBD_LBCMAXIMIZE:
1526 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1527 CloseWindowMenu(scr);
1529 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1531 break;
1532 case WKBD_RBCMAXIMIZE:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1534 CloseWindowMenu(scr);
1536 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1538 break;
1539 case WKBD_MAXIMUS:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1541 CloseWindowMenu(scr);
1543 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1545 break;
1546 case WKBD_RAISE:
1547 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1548 CloseWindowMenu(scr);
1550 wRaiseFrame(wwin->frame->core);
1552 break;
1553 case WKBD_LOWER:
1554 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1555 CloseWindowMenu(scr);
1557 wLowerFrame(wwin->frame->core);
1559 break;
1560 case WKBD_RAISELOWER:
1561 /* raise or lower the window under the pointer, not the
1562 * focused one
1564 wwin = windowUnderPointer(scr);
1565 if (wwin)
1566 wRaiseLowerFrame(wwin->frame->core);
1567 break;
1568 case WKBD_SHADE:
1569 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1570 if (wwin->flags.shaded)
1571 wUnshadeWindow(wwin);
1572 else
1573 wShadeWindow(wwin);
1575 break;
1576 case WKBD_MOVERESIZE:
1577 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1578 CloseWindowMenu(scr);
1580 wKeyboardMoveResizeWindow(wwin);
1582 break;
1583 case WKBD_CLOSE:
1584 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1585 CloseWindowMenu(scr);
1586 if (wwin->protocols.DELETE_WINDOW)
1587 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1589 break;
1590 case WKBD_SELECT:
1591 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1592 wSelectWindow(wwin, !wwin->flags.selected);
1594 break;
1595 case WKBD_FOCUSNEXT:
1596 StartWindozeCycle(wwin, event, True, False);
1597 break;
1599 case WKBD_FOCUSPREV:
1600 StartWindozeCycle(wwin, event, False, False);
1601 break;
1603 case WKBD_GROUPNEXT:
1604 StartWindozeCycle(wwin, event, True, True);
1605 break;
1607 case WKBD_GROUPPREV:
1608 StartWindozeCycle(wwin, event, False, True);
1609 break;
1611 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1612 widx = command - WKBD_WORKSPACE1;
1613 i = (w_global.workspace.current / 10) * 10 + widx;
1614 if (wPreferences.ws_advance || i < w_global.workspace.count)
1615 wWorkspaceChange(scr, i);
1616 break;
1618 case WKBD_NEXTWORKSPACE:
1619 wWorkspaceRelativeChange(scr, 1);
1620 break;
1621 case WKBD_PREVWORKSPACE:
1622 wWorkspaceRelativeChange(scr, -1);
1623 break;
1624 case WKBD_LASTWORKSPACE:
1625 wWorkspaceChange(scr, w_global.workspace.last_used);
1626 break;
1628 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1629 widx = command - WKBD_MOVE_WORKSPACE1;
1630 i = (w_global.workspace.current / 10) * 10 + widx;
1631 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1632 wWindowChangeWorkspace(wwin, i);
1633 break;
1635 case WKBD_MOVE_NEXTWORKSPACE:
1636 if (wwin)
1637 wWindowChangeWorkspaceRelative(wwin, 1);
1638 break;
1639 case WKBD_MOVE_PREVWORKSPACE:
1640 if (wwin)
1641 wWindowChangeWorkspaceRelative(wwin, -1);
1642 break;
1643 case WKBD_MOVE_LASTWORKSPACE:
1644 if (wwin)
1645 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1646 break;
1648 case WKBD_MOVE_NEXTWSLAYER:
1649 case WKBD_MOVE_PREVWSLAYER:
1651 if (wwin) {
1652 int row, column;
1654 row = w_global.workspace.current / 10;
1655 column = w_global.workspace.current % 10;
1657 if (command == WKBD_MOVE_NEXTWSLAYER) {
1658 if ((row + 1) * 10 < w_global.workspace.count)
1659 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1660 } else {
1661 if (row > 0)
1662 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1666 break;
1668 case WKBD_WINDOW1:
1669 case WKBD_WINDOW2:
1670 case WKBD_WINDOW3:
1671 case WKBD_WINDOW4:
1672 case WKBD_WINDOW5:
1673 case WKBD_WINDOW6:
1674 case WKBD_WINDOW7:
1675 case WKBD_WINDOW8:
1676 case WKBD_WINDOW9:
1677 case WKBD_WINDOW10:
1679 widx = command - WKBD_WINDOW1;
1681 if (w_global.shortcut.windows[widx]) {
1682 WMArray *list = w_global.shortcut.windows[widx];
1683 int cw;
1684 int count = WMGetArrayItemCount(list);
1685 WWindow *twin;
1686 WMArrayIterator iter;
1687 WWindow *wwin;
1689 wUnselectWindows(scr);
1690 cw = w_global.workspace.current;
1692 WM_ETARETI_ARRAY(list, wwin, iter) {
1693 if (count > 1)
1694 wWindowChangeWorkspace(wwin, cw);
1696 wMakeWindowVisible(wwin);
1698 if (count > 1)
1699 wSelectWindow(wwin, True);
1702 /* rotate the order of windows, to create a cycling effect */
1703 twin = WMGetFromArray(list, 0);
1704 WMDeleteFromArray(list, 0);
1705 WMAddToArray(list, twin);
1707 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1708 if (w_global.shortcut.windows[widx]) {
1709 WMFreeArray(w_global.shortcut.windows[widx]);
1710 w_global.shortcut.windows[widx] = NULL;
1713 if (wwin->flags.selected && scr->selected_windows) {
1714 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1715 } else {
1716 w_global.shortcut.windows[widx] = WMCreateArray(4);
1717 WMAddToArray(w_global.shortcut.windows[widx], wwin);
1720 wSelectWindow(wwin, !wwin->flags.selected);
1721 XFlush(dpy);
1722 wusleep(3000);
1723 wSelectWindow(wwin, !wwin->flags.selected);
1724 XFlush(dpy);
1726 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1727 if (wwin->flags.selected && scr->selected_windows) {
1728 if (w_global.shortcut.windows[widx])
1729 WMFreeArray(w_global.shortcut.windows[widx]);
1731 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1735 break;
1737 case WKBD_RELAUNCH:
1738 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1739 (void) RelaunchWindow(wwin);
1741 break;
1743 case WKBD_SWITCH_SCREEN:
1744 if (w_global.screen_count > 1) {
1745 WScreen *scr2;
1746 int i;
1748 /* find index of this screen */
1749 for (i = 0; i < w_global.screen_count; i++) {
1750 if (wScreenWithNumber(i) == scr)
1751 break;
1753 i++;
1754 if (i >= w_global.screen_count) {
1755 i = 0;
1757 scr2 = wScreenWithNumber(i);
1759 if (scr2) {
1760 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1761 scr2->scr_width / 2, scr2->scr_height / 2);
1764 break;
1766 case WKBD_RUN:
1768 char *cmdline;
1770 cmdline = ExpandOptions(scr, _("exec %a(Run,Type command to run:)"));
1772 if (cmdline) {
1773 XGrabPointer(dpy, scr->root_win, True, 0,
1774 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1775 XSync(dpy, False);
1777 ExecuteShellCommand(scr, cmdline);
1778 wfree(cmdline);
1780 XUngrabPointer(dpy, CurrentTime);
1781 XSync(dpy, False);
1783 break;
1786 case WKBD_NEXTWSLAYER:
1787 case WKBD_PREVWSLAYER:
1789 int row, column;
1791 row = w_global.workspace.current / 10;
1792 column = w_global.workspace.current % 10;
1794 if (command == WKBD_NEXTWSLAYER) {
1795 if ((row + 1) * 10 < w_global.workspace.count)
1796 wWorkspaceChange(scr, column + (row + 1) * 10);
1797 } else {
1798 if (row > 0)
1799 wWorkspaceChange(scr, column + (row - 1) * 10);
1802 break;
1803 case WKBD_CLIPRAISELOWER:
1804 if (!wPreferences.flags.noclip)
1805 wDockRaiseLower(w_global.workspace.array[w_global.workspace.current]->clip);
1806 break;
1807 case WKBD_DOCKRAISELOWER:
1808 if (!wPreferences.flags.nodock)
1809 wDockRaiseLower(scr->dock);
1810 break;
1811 #ifdef KEEP_XKB_LOCK_STATUS
1812 case WKBD_TOGGLE:
1813 if (wPreferences.modelock) {
1814 /*toggle */
1815 wwin = scr->focused_window;
1817 if (wwin && wwin->flags.mapped
1818 && wwin->frame->workspace == w_global.workspace.current
1819 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1820 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1822 wwin->frame->languagemode = wwin->frame->last_languagemode;
1823 wwin->frame->last_languagemode = staterec.group;
1824 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1828 break;
1829 #endif /* KEEP_XKB_LOCK_STATUS */
1833 static void handleMotionNotify(XEvent * event)
1835 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1837 if (wPreferences.scrollable_menus) {
1838 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1839 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1841 if (scr->flags.jump_back_pending ||
1842 p.x <= (rect.pos.x + 1) ||
1843 p.x >= (rect.pos.x + rect.size.width - 2) ||
1844 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1845 WMenu *menu;
1847 menu = wMenuUnderPointer(scr);
1848 if (menu != NULL)
1849 wMenuScroll(menu);
1854 static void handleVisibilityNotify(XEvent * event)
1856 WWindow *wwin;
1858 wwin = wWindowFor(event->xvisibility.window);
1859 if (!wwin)
1860 return;
1861 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);