wmaker: replace and be replaced (ICCCM protocol)
[wmaker-crm.git] / src / event.c
blob1e100f775ba30bfd5dad1e806973029a2e7bb4d5
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 USE_DOCK_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 handle_selection_clear(XSelectionClearEvent *event);
107 static void wdelete_death_handler(WMagicNumber id);
110 #ifdef USE_XSHAPE
111 static void handleShapeNotify(XEvent *event);
112 #endif
114 #ifdef KEEP_XKB_LOCK_STATUS
115 static void handleXkbIndicatorStateNotify(XkbEvent *event);
116 #endif
118 /* real dead process handler */
119 static void handleDeadProcess(void);
121 typedef struct DeadProcesses {
122 pid_t pid;
123 unsigned char exit_status;
124 } DeadProcesses;
126 /* stack of dead processes */
127 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
128 static int deadProcessPtr = 0;
130 typedef struct DeathHandler {
131 WDeathHandler *callback;
132 pid_t pid;
133 void *client_data;
134 } DeathHandler;
136 static WMArray *deathHandlers = NULL;
138 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
140 DeathHandler *handler;
142 handler = malloc(sizeof(DeathHandler));
143 if (!handler)
144 return 0;
146 handler->pid = pid;
147 handler->callback = callback;
148 handler->client_data = cdata;
150 if (!deathHandlers)
151 deathHandlers = WMCreateArrayWithDestructor(8, free);
153 WMAddToArray(deathHandlers, handler);
155 return handler;
158 static void wdelete_death_handler(WMagicNumber id)
160 DeathHandler *handler = (DeathHandler *) id;
162 if (!handler || !deathHandlers)
163 return;
165 /* array destructor will call free(handler) */
166 WMRemoveFromArray(deathHandlers, handler);
169 void DispatchEvent(XEvent * event)
171 if (deathHandlers)
172 handleDeadProcess();
174 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
175 WCHANGE_STATE(WSTATE_EXITING);
176 /* received SIGTERM */
178 * WMHandleEvent() can't be called from anything
179 * executed inside here, or we can get in a infinite
180 * recursive loop.
182 Shutdown(WSExitMode);
184 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
185 WCHANGE_STATE(WSTATE_RESTARTING);
187 Shutdown(WSRestartPreparationMode);
188 /* received SIGHUP */
189 Restart(NULL, True);
190 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
191 WCHANGE_STATE(WSTATE_NORMAL);
192 wDefaultsCheckDomains(NULL);
195 /* for the case that all that is wanted to be dispatched is
196 * the stuff above */
197 if (!event)
198 return;
200 saveTimestamp(event);
201 switch (event->type) {
202 case MapRequest:
203 handleMapRequest(event);
204 break;
206 case KeyPress:
207 handleKeyPress(event);
208 break;
210 case MotionNotify:
211 handleMotionNotify(event);
212 break;
214 case ConfigureRequest:
215 handleConfigureRequest(event);
216 break;
218 case DestroyNotify:
219 handleDestroyNotify(event);
220 break;
222 case MapNotify:
223 handleMapNotify(event);
224 break;
226 case UnmapNotify:
227 handleUnmapNotify(event);
228 break;
230 case ButtonPress:
231 handleButtonPress(event);
232 break;
234 case Expose:
235 handleExpose(event);
236 break;
238 case PropertyNotify:
239 handlePropertyNotify(event);
240 break;
242 case EnterNotify:
243 handleEnterNotify(event);
244 break;
246 case LeaveNotify:
247 handleLeaveNotify(event);
248 break;
250 case ClientMessage:
251 handleClientMessage(event);
252 break;
254 case ColormapNotify:
255 handleColormapNotify(event);
256 break;
258 case MappingNotify:
259 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
260 XRefreshKeyboardMapping(&event->xmapping);
261 break;
263 case FocusIn:
264 handleFocusIn(event);
265 break;
267 case VisibilityNotify:
268 handleVisibilityNotify(event);
269 break;
271 case ConfigureNotify:
272 #ifdef USE_RANDR
273 if (event->xconfigure.window == DefaultRootWindow(dpy))
274 XRRUpdateConfiguration(event);
275 #endif
276 break;
278 case SelectionClear:
279 handle_selection_clear(&event->xselectionclear);
280 break;
282 default:
283 handleExtensions(event);
284 break;
288 #ifdef HAVE_INOTIFY
290 *----------------------------------------------------------------------
291 * handle_inotify_events-
292 * Check for inotify events
294 * Returns:
295 * After reading events for the given file descriptor (fd) and
296 * watch descriptor (wd)
298 * Side effects:
299 * Calls wDefaultsCheckDomains if config database is updated
300 *----------------------------------------------------------------------
302 static void handle_inotify_events(void)
304 ssize_t eventQLength;
305 size_t i = 0;
306 /* Make room for at lease 5 simultaneous events, with path + filenames */
307 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
308 /* Check config only once per read of the event queue */
309 int oneShotFlag = 0;
312 * Read off the queued events
313 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
314 * not occur; the block is on Xevents, but a config file change will normally
315 * occur as a result of an Xevent - so the event queue should never have more than
316 * a few entries before a read().
318 eventQLength = read(w_global.inotify.fd_event_queue,
319 buff, sizeof(buff) );
321 if (eventQLength < 0) {
322 wwarning(_("read problem when trying to get INotify event: %s"), strerror(errno));
323 return;
326 /* check what events occured */
327 /* Should really check wd here too, but for now we only have one watch! */
328 while (i < eventQLength) {
329 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
332 * see inotify.h for event types.
334 if (pevent->mask & IN_DELETE_SELF) {
335 wwarning(_("the defaults database has been deleted!"
336 " Restart Window Maker to create the database" " with the default settings"));
338 if (w_global.inotify.fd_event_queue >= 0) {
339 close(w_global.inotify.fd_event_queue);
340 w_global.inotify.fd_event_queue = -1;
343 if (pevent->mask & IN_UNMOUNT) {
344 wwarning(_("the unit containing the defaults database has"
345 " been unmounted. Setting --static mode." " Any changes will not be saved."));
347 if (w_global.inotify.fd_event_queue >= 0) {
348 close(w_global.inotify.fd_event_queue);
349 w_global.inotify.fd_event_queue = -1;
352 wPreferences.flags.noupdates = 1;
354 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
355 wwarning(_("Inotify: Reading config files in defaults database."));
356 wDefaultsCheckDomains(NULL);
357 oneShotFlag = 1;
360 /* move to next event in the buffer */
361 i += sizeof(struct inotify_event) + pevent->len;
364 #endif /* HAVE_INOTIFY */
367 *----------------------------------------------------------------------
368 * EventLoop-
369 * Processes X and internal events indefinitely.
371 * Returns:
372 * Never returns
374 * Side effects:
375 * The LastTimestamp global variable is updated.
376 * Calls inotifyGetEvents if defaults database changes.
377 *----------------------------------------------------------------------
379 noreturn void EventLoop(void)
381 XEvent event;
382 #ifdef HAVE_INOTIFY
383 struct timeval time;
384 fd_set rfds;
385 int retVal = 0;
387 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
388 retVal = -1;
389 #endif
391 for (;;) {
393 WMNextEvent(dpy, &event); /* Blocks here */
394 WMHandleEvent(&event);
395 #ifdef HAVE_INOTIFY
396 if (retVal != -1) {
397 time.tv_sec = 0;
398 time.tv_usec = 0;
399 FD_ZERO(&rfds);
400 FD_SET(w_global.inotify.fd_event_queue, &rfds);
402 /* check for available read data from inotify - don't block! */
403 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
405 if (retVal < 0) { /* an error has occured */
406 wwarning(_("select failed. The inotify instance will be closed."
407 " Changes to the defaults database will require"
408 " a restart to take effect."));
409 close(w_global.inotify.fd_event_queue);
410 w_global.inotify.fd_event_queue = -1;
411 continue;
413 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
414 handle_inotify_events();
416 #endif
421 *----------------------------------------------------------------------
422 * ProcessPendingEvents --
423 * Processes the events that are currently pending (at the time
424 * this function is called) in the display's queue.
426 * Returns:
427 * After the pending events that were present at the function call
428 * are processed.
430 * Side effects:
431 * Many -- whatever handling events may involve.
433 *----------------------------------------------------------------------
435 void ProcessPendingEvents(void)
437 XEvent event;
438 int count;
440 XSync(dpy, False);
442 /* Take a snapshot of the event count in the queue */
443 count = XPending(dpy);
445 while (count > 0 && XPending(dpy)) {
446 WMNextEvent(dpy, &event);
447 WMHandleEvent(&event);
448 count--;
452 Bool IsDoubleClick(WScreen * scr, XEvent * event)
454 if ((scr->last_click_time > 0) &&
455 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
456 && (event->xbutton.button == scr->last_click_button)
457 && (event->xbutton.window == scr->last_click_window)) {
459 scr->flags.next_click_is_not_double = 1;
460 scr->last_click_time = 0;
461 scr->last_click_window = event->xbutton.window;
463 return True;
465 return False;
468 void NotifyDeadProcess(pid_t pid, unsigned char status)
470 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
471 wwarning("stack overflow: too many dead processes");
472 return;
474 /* stack the process to be handled later,
475 * as this is called from the signal handler */
476 deadProcesses[deadProcessPtr].pid = pid;
477 deadProcesses[deadProcessPtr].exit_status = status;
478 deadProcessPtr++;
481 static void handleDeadProcess(void)
483 DeathHandler *tmp;
484 int i;
486 for (i = 0; i < deadProcessPtr; i++) {
487 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
490 if (!deathHandlers) {
491 deadProcessPtr = 0;
492 return;
495 /* get the pids on the queue and call handlers */
496 while (deadProcessPtr > 0) {
497 deadProcessPtr--;
499 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
500 tmp = WMGetFromArray(deathHandlers, i);
501 if (!tmp)
502 continue;
504 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
505 (*tmp->callback) (tmp->pid,
506 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
507 wdelete_death_handler(tmp);
513 static void saveTimestamp(XEvent * event)
516 * Never save CurrentTime as LastTimestamp because CurrentTime
517 * it's not a real timestamp (it's the 0L constant)
520 switch (event->type) {
521 case ButtonRelease:
522 case ButtonPress:
523 w_global.timestamp.last_event = event->xbutton.time;
524 break;
525 case KeyPress:
526 case KeyRelease:
527 w_global.timestamp.last_event = event->xkey.time;
528 break;
529 case MotionNotify:
530 w_global.timestamp.last_event = event->xmotion.time;
531 break;
532 case PropertyNotify:
533 w_global.timestamp.last_event = event->xproperty.time;
534 break;
535 case EnterNotify:
536 case LeaveNotify:
537 w_global.timestamp.last_event = event->xcrossing.time;
538 break;
539 case SelectionClear:
540 w_global.timestamp.last_event = event->xselectionclear.time;
541 break;
542 case SelectionRequest:
543 w_global.timestamp.last_event = event->xselectionrequest.time;
544 break;
545 case SelectionNotify:
546 w_global.timestamp.last_event = event->xselection.time;
547 #ifdef USE_DOCK_XDND
548 wXDNDProcessSelection(event);
549 #endif
550 break;
554 static int matchWindow(const void *item, const void *cdata)
556 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
559 static void handleExtensions(XEvent * event)
561 #ifdef USE_XSHAPE
562 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
563 handleShapeNotify(event);
565 #endif
566 #ifdef KEEP_XKB_LOCK_STATUS
567 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
568 handleXkbIndicatorStateNotify((XkbEvent *) event);
570 #endif /*KEEP_XKB_LOCK_STATUS */
571 #ifdef USE_RANDR
572 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
573 /* From xrandr man page: "Clients must call back into Xlib using
574 * XRRUpdateConfiguration when screen configuration change notify
575 * events are generated */
576 XRRUpdateConfiguration(event);
577 WCHANGE_STATE(WSTATE_RESTARTING);
578 Shutdown(WSRestartPreparationMode);
579 Restart(NULL,True);
581 #endif
584 static void handleMapRequest(XEvent * ev)
586 WWindow *wwin;
587 WScreen *scr = NULL;
588 Window window = ev->xmaprequest.window;
590 wwin = wWindowFor(window);
591 if (wwin != NULL) {
592 if (wwin->flags.shaded) {
593 wUnshadeWindow(wwin);
595 /* deiconify window */
596 if (wwin->flags.miniaturized) {
597 wDeiconifyWindow(wwin);
598 } else if (wwin->flags.hidden) {
599 WApplication *wapp = wApplicationOf(wwin->main_window);
600 /* go to the last workspace that the user worked on the app */
601 if (wapp) {
602 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
604 wUnhideApplication(wapp, False, False);
606 return;
609 scr = wScreenForRootWindow(ev->xmaprequest.parent);
611 wwin = wManageWindow(scr, window);
614 * This is to let the Dock know that the application it launched
615 * has already been mapped (eg: it has finished launching).
616 * It is not necessary for normally docked apps, but is needed for
617 * apps that were forcedly docked (like with dockit).
619 if (scr->last_dock) {
620 if (wwin && wwin->main_window != None && wwin->main_window != window)
621 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
622 else
623 wDockTrackWindowLaunch(scr->last_dock, window);
626 if (wwin) {
627 wClientSetState(wwin, NormalState, None);
628 if (wwin->flags.maximized) {
629 wMaximizeWindow(wwin, wwin->flags.maximized);
631 if (wwin->flags.shaded) {
632 wwin->flags.shaded = 0;
633 wwin->flags.skip_next_animation = 1;
634 wShadeWindow(wwin);
636 if (wwin->flags.miniaturized) {
637 wwin->flags.miniaturized = 0;
638 wwin->flags.skip_next_animation = 1;
639 wIconifyWindow(wwin);
641 if (wwin->flags.fullscreen) {
642 wwin->flags.fullscreen = 0;
643 wFullscreenWindow(wwin);
645 if (wwin->flags.hidden) {
646 WApplication *wapp = wApplicationOf(wwin->main_window);
648 wwin->flags.hidden = 0;
649 wwin->flags.skip_next_animation = 1;
650 if (wapp) {
651 wHideApplication(wapp);
657 static void handleDestroyNotify(XEvent * event)
659 WWindow *wwin;
660 WApplication *app;
661 Window window = event->xdestroywindow.window;
662 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
663 int widx;
665 wwin = wWindowFor(window);
666 if (wwin) {
667 wUnmanageWindow(wwin, False, True);
670 if (scr != NULL) {
671 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
672 WFakeGroupLeader *fPtr;
674 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
675 if (fPtr->retainCount > 0) {
676 fPtr->retainCount--;
677 if (fPtr->retainCount == 0 && fPtr->leader != None) {
678 XDestroyWindow(dpy, fPtr->leader);
679 fPtr->leader = None;
680 XFlush(dpy);
683 fPtr->origLeader = None;
687 app = wApplicationOf(window);
688 if (app) {
689 if (window == app->main_window) {
690 app->refcount = 0;
691 wwin = app->main_window_desc->screen_ptr->focused_window;
692 while (wwin) {
693 if (wwin->main_window == window) {
694 wwin->main_window = None;
696 wwin = wwin->prev;
699 wApplicationDestroy(app);
703 static void handleExpose(XEvent * event)
705 WObjDescriptor *desc;
706 XEvent ev;
708 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
710 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
711 return;
714 if (desc->handle_expose) {
715 (*desc->handle_expose) (desc, event);
719 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
721 WWindow *wwin;
722 Bool next_direction;
724 if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
725 next_direction = False;
726 else
727 next_direction = True;
729 switch (action) {
730 case WA_SWITCH_WORKSPACES:
731 if (next_direction)
732 wWorkspaceRelativeChange(scr, 1);
733 else
734 wWorkspaceRelativeChange(scr, -1);
735 break;
737 case WA_SWITCH_WINDOWS:
738 wwin = scr->focused_window;
739 if (next_direction)
740 wWindowFocusNext(wwin, True);
741 else
742 wWindowFocusPrev(wwin, True);
743 break;
747 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
749 WWindow *wwin;
751 switch (action) {
752 case WA_SELECT_WINDOWS:
753 wUnselectWindows(scr);
754 wSelectWindows(scr, event);
755 break;
756 case WA_OPEN_APPMENU:
757 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
758 /* ugly hack */
759 if (scr->root_menu) {
760 if (scr->root_menu->brother->flags.mapped)
761 event->xbutton.window = scr->root_menu->brother->frame->core->window;
762 else
763 event->xbutton.window = scr->root_menu->frame->core->window;
765 break;
766 case WA_OPEN_WINLISTMENU:
767 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
768 if (scr->switch_menu) {
769 if (scr->switch_menu->brother->flags.mapped)
770 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
771 else
772 event->xbutton.window = scr->switch_menu->frame->core->window;
774 break;
775 case WA_MOVE_PREVWORKSPACE:
776 wWorkspaceRelativeChange(scr, -1);
777 break;
778 case WA_MOVE_NEXTWORKSPACE:
779 wWorkspaceRelativeChange(scr, 1);
780 break;
781 case WA_MOVE_PREVWINDOW:
782 wwin = scr->focused_window;
783 wWindowFocusPrev(wwin, True);
784 break;
785 case WA_MOVE_NEXTWINDOW:
786 wwin = scr->focused_window;
787 wWindowFocusNext(wwin, True);
788 break;
792 /* bindable */
793 static void handleButtonPress(XEvent * event)
795 WObjDescriptor *desc;
796 WScreen *scr;
798 scr = wScreenForRootWindow(event->xbutton.root);
800 #ifdef BALLOON_TEXT
801 wBalloonHide(scr);
802 #endif
804 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
805 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
806 executeButtonAction(scr, event, wPreferences.mouse_button1);
807 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
808 executeButtonAction(scr, event, wPreferences.mouse_button2);
809 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
810 executeButtonAction(scr, event, wPreferences.mouse_button3);
811 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
812 executeButtonAction(scr, event, wPreferences.mouse_button8);
813 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
814 executeButtonAction(scr, event, wPreferences.mouse_button9);
815 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
816 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
817 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
818 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
819 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
820 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
821 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
822 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
826 desc = NULL;
827 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
828 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
829 return;
833 if (desc->parent_type == WCLASS_WINDOW) {
834 XSync(dpy, 0);
836 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
837 XAllowEvents(dpy, AsyncPointer, CurrentTime);
838 } else {
839 /* if (wPreferences.focus_mode == WKF_CLICK) { */
840 if (wPreferences.ignore_focus_click) {
841 XAllowEvents(dpy, AsyncPointer, CurrentTime);
843 XAllowEvents(dpy, ReplayPointer, CurrentTime);
844 /* } */
846 XSync(dpy, 0);
847 } else if (desc->parent_type == WCLASS_APPICON
848 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
849 if (event->xbutton.state & MOD_MASK) {
850 XSync(dpy, 0);
851 XAllowEvents(dpy, AsyncPointer, CurrentTime);
852 XSync(dpy, 0);
856 if (desc->handle_mousedown != NULL) {
857 (*desc->handle_mousedown) (desc, event);
860 /* save double-click information */
861 if (scr->flags.next_click_is_not_double) {
862 scr->flags.next_click_is_not_double = 0;
863 } else {
864 scr->last_click_time = event->xbutton.time;
865 scr->last_click_button = event->xbutton.button;
866 scr->last_click_window = event->xbutton.window;
870 static void handleMapNotify(XEvent * event)
872 WWindow *wwin;
874 wwin = wWindowFor(event->xmap.event);
875 if (wwin && wwin->client_win == event->xmap.event) {
876 if (wwin->flags.miniaturized) {
877 wDeiconifyWindow(wwin);
878 } else {
879 XGrabServer(dpy);
880 wWindowMap(wwin);
881 wClientSetState(wwin, NormalState, None);
882 XUngrabServer(dpy);
887 static void handleUnmapNotify(XEvent * event)
889 WWindow *wwin;
890 XEvent ev;
891 Bool withdraw = False;
893 /* only process windows with StructureNotify selected
894 * (ignore SubstructureNotify) */
895 wwin = wWindowFor(event->xunmap.window);
896 if (!wwin)
897 return;
899 /* whether the event is a Withdrawal request */
900 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
901 withdraw = True;
903 if (wwin->client_win != event->xunmap.event && !withdraw)
904 return;
906 if (!wwin->flags.mapped && !withdraw
907 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
908 && !wwin->flags.miniaturized && !wwin->flags.hidden)
909 return;
911 XGrabServer(dpy);
912 XUnmapWindow(dpy, wwin->frame->core->window);
913 wwin->flags.mapped = 0;
914 XSync(dpy, 0);
915 /* check if the window was destroyed */
916 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
917 DispatchEvent(&ev);
918 } else {
919 Bool reparented = False;
921 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
922 reparented = True;
924 /* withdraw window */
925 wwin->flags.mapped = 0;
926 if (!reparented)
927 wClientSetState(wwin, WithdrawnState, None);
929 /* if the window was reparented, do not reparent it back to the
930 * root window */
931 wUnmanageWindow(wwin, !reparented, False);
933 XUngrabServer(dpy);
936 static void handleConfigureRequest(XEvent * event)
938 WWindow *wwin;
940 wwin = wWindowFor(event->xconfigurerequest.window);
941 if (wwin == NULL) {
943 * Configure request for unmapped window
945 wClientConfigure(NULL, &(event->xconfigurerequest));
946 } else {
947 wClientConfigure(wwin, &(event->xconfigurerequest));
951 static void handlePropertyNotify(XEvent * event)
953 WWindow *wwin;
954 WApplication *wapp;
955 Window jr;
956 int ji;
957 unsigned int ju;
959 wwin = wWindowFor(event->xproperty.window);
960 if (wwin) {
961 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
962 return;
964 wClientCheckProperty(wwin, &event->xproperty);
966 wapp = wApplicationOf(event->xproperty.window);
967 if (wapp) {
968 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
972 static void handleClientMessage(XEvent * event)
974 WWindow *wwin;
975 WObjDescriptor *desc;
977 /* handle transition from Normal to Iconic state */
978 if (event->xclient.message_type == w_global.atom.wm.change_state
979 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
981 wwin = wWindowFor(event->xclient.window);
982 if (!wwin)
983 return;
984 if (!wwin->flags.miniaturized)
985 wIconifyWindow(wwin);
986 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
987 WScreen *scr = wScreenForRootWindow(event->xclient.window);
989 if (!scr)
990 return;
992 if (event->xclient.data.l[1] == 1) { /* starting */
993 wColormapAllowClientInstallation(scr, True);
994 } else { /* stopping */
995 wColormapAllowClientInstallation(scr, False);
997 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
999 char *command;
1000 size_t len;
1002 len = sizeof(event->xclient.data.b) + 1;
1003 command = wmalloc(len);
1004 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
1006 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1007 wwarning(_("Got Reconfigure command"));
1008 wDefaultsCheckDomains(NULL);
1009 } else {
1010 wwarning(_("Got unknown command %s"), command);
1013 wfree(command);
1015 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1016 WApplication *wapp;
1017 int done = 0;
1018 wapp = wApplicationOf(event->xclient.window);
1019 if (wapp) {
1020 switch (event->xclient.data.l[0]) {
1021 case WMFHideOtherApplications:
1022 wHideOtherApplications(wapp->main_window_desc);
1023 done = 1;
1024 break;
1026 case WMFHideApplication:
1027 wHideApplication(wapp);
1028 done = 1;
1029 break;
1032 if (!done) {
1033 wwin = wWindowFor(event->xclient.window);
1034 if (wwin) {
1035 switch (event->xclient.data.l[0]) {
1036 case WMFHideOtherApplications:
1037 wHideOtherApplications(wwin);
1038 break;
1040 case WMFHideApplication:
1041 wHideApplication(wApplicationOf(wwin->main_window));
1042 break;
1046 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1047 wwin = wWindowFor(event->xclient.window);
1048 if (!wwin)
1049 return;
1050 switch (event->xclient.data.l[0]) {
1051 case GSWindowLevelAttr:
1053 int level = (int)event->xclient.data.l[1];
1055 if (WINDOW_LEVEL(wwin) != level) {
1056 ChangeStackingLevel(wwin->frame->core, level);
1059 break;
1061 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1062 wwin = wWindowFor(event->xclient.window);
1063 if (!wwin)
1064 return;
1065 switch (event->xclient.data.l[0]) {
1066 case WMTitleBarNormal:
1067 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1068 break;
1069 case WMTitleBarMain:
1070 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1071 break;
1072 case WMTitleBarKey:
1073 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1074 break;
1076 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1077 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1078 if (!scr)
1079 return;
1080 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1081 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1082 /* do nothing */
1083 #ifdef USE_DOCK_XDND
1084 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1085 /* do nothing */
1086 #endif /* USE_DOCK_XDND */
1087 } else {
1089 * Non-standard thing, but needed by OffiX DND.
1090 * For when the icon frame gets a ClientMessage
1091 * that should have gone to the icon_window.
1093 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1094 struct WIcon *icon = NULL;
1096 if (desc->parent_type == WCLASS_MINIWINDOW) {
1097 icon = (WIcon *) desc->parent;
1098 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1099 icon = ((WAppIcon *) desc->parent)->icon;
1101 if (icon && (wwin = icon->owner)) {
1102 if (wwin->client_win != event->xclient.window) {
1103 event->xclient.window = wwin->client_win;
1104 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1111 static void raiseWindow(WScreen * scr)
1113 WWindow *wwin;
1115 scr->autoRaiseTimer = NULL;
1117 wwin = wWindowFor(scr->autoRaiseWindow);
1118 if (!wwin)
1119 return;
1121 if (!wwin->flags.destroyed && wwin->flags.focused) {
1122 wRaiseFrame(wwin->frame->core);
1123 /* this is needed or a race condition will occur */
1124 XSync(dpy, False);
1128 static void handleEnterNotify(XEvent * event)
1130 WWindow *wwin;
1131 WObjDescriptor *desc = NULL;
1132 XEvent ev;
1133 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1135 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1136 /* already left the window... */
1137 saveTimestamp(&ev);
1138 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1139 return;
1143 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1144 if (desc->handle_enternotify)
1145 (*desc->handle_enternotify) (desc, event);
1148 /* enter to window */
1149 wwin = wWindowFor(event->xcrossing.window);
1150 if (!wwin) {
1151 if (wPreferences.colormap_mode == WCM_POINTER) {
1152 wColormapInstallForWindow(scr, NULL);
1154 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1155 WMDeleteTimerHandler(scr->autoRaiseTimer);
1156 scr->autoRaiseTimer = NULL;
1158 } else {
1159 /* set auto raise timer even if in focus-follows-mouse mode
1160 * and the event is for the frame window, even if the window
1161 * has focus already. useful if you move the pointer from a focused
1162 * window to the root window and back pretty fast
1164 * set focus if in focus-follows-mouse mode and the event
1165 * is for the frame window and window doesn't have focus yet */
1166 if (wPreferences.focus_mode == WKF_SLOPPY
1167 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1169 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1170 wSetFocusTo(scr, wwin);
1172 if (scr->autoRaiseTimer)
1173 WMDeleteTimerHandler(scr->autoRaiseTimer);
1174 scr->autoRaiseTimer = NULL;
1176 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1177 scr->autoRaiseWindow = wwin->frame->core->window;
1178 scr->autoRaiseTimer
1179 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1182 /* Install colormap for window, if the colormap installation mode
1183 * is colormap_follows_mouse */
1184 if (wPreferences.colormap_mode == WCM_POINTER) {
1185 if (wwin->client_win == event->xcrossing.window)
1186 wColormapInstallForWindow(scr, wwin);
1187 else
1188 wColormapInstallForWindow(scr, NULL);
1192 if (event->xcrossing.window == event->xcrossing.root
1193 && event->xcrossing.detail == NotifyNormal
1194 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1196 wSetFocusTo(scr, scr->focused_window);
1198 #ifdef BALLOON_TEXT
1199 wBalloonEnteredObject(scr, desc);
1200 #endif
1203 static void handleLeaveNotify(XEvent * event)
1205 WObjDescriptor *desc = NULL;
1207 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1208 if (desc->handle_leavenotify)
1209 (*desc->handle_leavenotify) (desc, event);
1213 #ifdef USE_XSHAPE
1214 static void handleShapeNotify(XEvent * event)
1216 XShapeEvent *shev = (XShapeEvent *) event;
1217 WWindow *wwin;
1218 union {
1219 XEvent xevent;
1220 XShapeEvent xshape;
1221 } ev;
1223 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1224 if (ev.xshape.kind == ShapeBounding) {
1225 if (ev.xshape.shaped == shev->shaped) {
1226 *shev = ev.xshape;
1227 } else {
1228 XPutBackEvent(dpy, &ev.xevent);
1229 break;
1234 wwin = wWindowFor(shev->window);
1235 if (!wwin || shev->kind != ShapeBounding)
1236 return;
1238 if (!shev->shaped && wwin->flags.shaped) {
1240 wwin->flags.shaped = 0;
1241 wWindowClearShape(wwin);
1243 } else if (shev->shaped) {
1245 wwin->flags.shaped = 1;
1246 wWindowSetShape(wwin);
1249 #endif /* USE_XSHAPE */
1251 #ifdef KEEP_XKB_LOCK_STATUS
1252 /* please help ]d if you know what to do */
1253 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1255 WWindow *wwin;
1256 WScreen *scr;
1257 XkbStateRec staterec;
1258 int i;
1260 for (i = 0; i < w_global.screen_count; i++) {
1261 scr = wScreenWithNumber(i);
1262 wwin = scr->focused_window;
1263 if (wwin && wwin->flags.focused) {
1264 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1265 if (wwin->frame->languagemode != staterec.group) {
1266 wwin->frame->last_languagemode = wwin->frame->languagemode;
1267 wwin->frame->languagemode = staterec.group;
1269 #ifdef XKB_BUTTON_HINT
1270 if (wwin->frame->titlebar) {
1271 wFrameWindowPaint(wwin->frame);
1273 #endif
1277 #endif /*KEEP_XKB_LOCK_STATUS */
1279 static void handleColormapNotify(XEvent * event)
1281 WWindow *wwin;
1282 WScreen *scr;
1283 Bool reinstall = False;
1285 wwin = wWindowFor(event->xcolormap.window);
1286 if (!wwin)
1287 return;
1289 scr = wwin->screen_ptr;
1291 do {
1292 if (wwin) {
1293 if (event->xcolormap.new) {
1294 XWindowAttributes attr;
1296 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1298 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1299 scr->current_colormap = attr.colormap;
1301 reinstall = True;
1302 } else if (event->xcolormap.state == ColormapUninstalled &&
1303 scr->current_colormap == event->xcolormap.colormap) {
1305 /* some bastard app (like XV) removed our colormap */
1307 * can't enforce or things like xscreensaver wont work
1308 * reinstall = True;
1310 } else if (event->xcolormap.state == ColormapInstalled &&
1311 scr->current_colormap == event->xcolormap.colormap) {
1313 /* someone has put our colormap back */
1314 reinstall = False;
1317 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1318 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1320 if (reinstall && scr->current_colormap != None) {
1321 if (!scr->flags.colormap_stuff_blocked)
1322 XInstallColormap(dpy, scr->current_colormap);
1326 static void handleFocusIn(XEvent * event)
1328 WWindow *wwin;
1331 * For applications that like stealing the focus.
1333 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1334 saveTimestamp(event);
1335 if (event->xfocus.mode == NotifyUngrab
1336 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1337 return;
1340 wwin = wWindowFor(event->xfocus.window);
1341 if (wwin && !wwin->flags.focused) {
1342 if (wwin->flags.mapped)
1343 wSetFocusTo(wwin->screen_ptr, wwin);
1344 else
1345 wSetFocusTo(wwin->screen_ptr, NULL);
1346 } else if (!wwin) {
1347 WScreen *scr = wScreenForWindow(event->xfocus.window);
1348 if (scr)
1349 wSetFocusTo(scr, NULL);
1353 static WWindow *windowUnderPointer(WScreen * scr)
1355 unsigned int mask;
1356 int foo;
1357 Window bar, win;
1359 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1360 return wWindowFor(win);
1361 return NULL;
1364 static int CheckFullScreenWindowFocused(WScreen * scr)
1366 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1367 return 1;
1368 else
1369 return 0;
1372 static void handleKeyPress(XEvent * event)
1374 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1375 WWindow *wwin = scr->focused_window;
1376 short i, widx;
1377 int modifiers;
1378 int command = -1;
1379 #ifdef KEEP_XKB_LOCK_STATUS
1380 XkbStateRec staterec;
1381 #endif /*KEEP_XKB_LOCK_STATUS */
1383 /* ignore CapsLock */
1384 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1386 for (i = 0; i < WKBD_LAST; i++) {
1387 if (wKeyBindings[i].keycode == 0)
1388 continue;
1390 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1391 || */ wKeyBindings[i].modifier ==
1392 modifiers)) {
1393 command = i;
1394 break;
1398 if (command < 0) {
1400 if (!wRootMenuPerformShortcut(event)) {
1401 static int dontLoop = 0;
1403 if (dontLoop > 10) {
1404 wwarning("problem with key event processing code");
1405 return;
1407 dontLoop++;
1408 /* if the focused window is an internal window, try redispatching
1409 * the event to the managed window, as it can be a WINGs window */
1410 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1411 /* client_leader contains the WINGs toplevel */
1412 event->xany.window = wwin->client_leader;
1413 WMHandleEvent(event);
1415 dontLoop--;
1417 return;
1419 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1420 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1422 switch (command) {
1424 case WKBD_ROOTMENU:
1425 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1426 if (!CheckFullScreenWindowFocused(scr)) {
1427 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1428 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1429 True);
1431 break;
1432 case WKBD_WINDOWLIST:
1433 if (!CheckFullScreenWindowFocused(scr)) {
1434 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1435 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1436 True);
1438 break;
1440 case WKBD_WINDOWMENU:
1441 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1442 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1443 break;
1444 case WKBD_MINIMIZEALL:
1445 CloseWindowMenu(scr);
1446 wHideAll(scr);
1447 break;
1448 case WKBD_MINIATURIZE:
1449 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1450 && !WFLAGP(wwin, no_miniaturizable)) {
1451 CloseWindowMenu(scr);
1453 if (wwin->protocols.MINIATURIZE_WINDOW)
1454 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1455 else {
1456 wIconifyWindow(wwin);
1459 break;
1460 case WKBD_HIDE:
1461 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1462 WApplication *wapp = wApplicationOf(wwin->main_window);
1463 CloseWindowMenu(scr);
1465 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1466 wHideApplication(wapp);
1469 break;
1470 case WKBD_HIDE_OTHERS:
1471 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1472 CloseWindowMenu(scr);
1474 wHideOtherApplications(wwin);
1476 break;
1477 case WKBD_MAXIMIZE:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1479 CloseWindowMenu(scr);
1481 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1483 break;
1484 case WKBD_VMAXIMIZE:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1490 break;
1491 case WKBD_HMAXIMIZE:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 CloseWindowMenu(scr);
1495 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1497 break;
1498 case WKBD_LHMAXIMIZE:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1500 CloseWindowMenu(scr);
1502 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1504 break;
1505 case WKBD_RHMAXIMIZE:
1506 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1507 CloseWindowMenu(scr);
1509 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1511 break;
1512 case WKBD_THMAXIMIZE:
1513 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1514 CloseWindowMenu(scr);
1516 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1518 break;
1519 case WKBD_BHMAXIMIZE:
1520 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1521 CloseWindowMenu(scr);
1523 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1525 break;
1526 case WKBD_LTCMAXIMIZE:
1527 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1528 CloseWindowMenu(scr);
1530 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1532 break;
1533 case WKBD_RTCMAXIMIZE:
1534 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1535 CloseWindowMenu(scr);
1537 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1539 break;
1540 case WKBD_LBCMAXIMIZE:
1541 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1542 CloseWindowMenu(scr);
1544 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1546 break;
1547 case WKBD_RBCMAXIMIZE:
1548 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1549 CloseWindowMenu(scr);
1551 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1553 break;
1554 case WKBD_MAXIMUS:
1555 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1556 CloseWindowMenu(scr);
1558 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1560 break;
1561 case WKBD_OMNIPRESENT:
1562 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1563 CloseWindowMenu(scr);
1565 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1567 break;
1568 case WKBD_RAISE:
1569 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1570 CloseWindowMenu(scr);
1572 wRaiseFrame(wwin->frame->core);
1574 break;
1575 case WKBD_LOWER:
1576 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1577 CloseWindowMenu(scr);
1579 wLowerFrame(wwin->frame->core);
1581 break;
1582 case WKBD_RAISELOWER:
1583 /* raise or lower the window under the pointer, not the
1584 * focused one
1586 wwin = windowUnderPointer(scr);
1587 if (wwin)
1588 wRaiseLowerFrame(wwin->frame->core);
1589 break;
1590 case WKBD_SHADE:
1591 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1592 if (wwin->flags.shaded)
1593 wUnshadeWindow(wwin);
1594 else
1595 wShadeWindow(wwin);
1597 break;
1598 case WKBD_MOVERESIZE:
1599 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1600 CloseWindowMenu(scr);
1602 wKeyboardMoveResizeWindow(wwin);
1604 break;
1605 case WKBD_CLOSE:
1606 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1607 CloseWindowMenu(scr);
1608 if (wwin->protocols.DELETE_WINDOW)
1609 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1611 break;
1612 case WKBD_SELECT:
1613 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1614 wSelectWindow(wwin, !wwin->flags.selected);
1616 break;
1618 case WKBD_WORKSPACEMAP:
1619 if (!wPreferences.disable_workspace_pager)
1620 StartWorkspaceMap(scr);
1621 break;
1623 case WKBD_FOCUSNEXT:
1624 StartWindozeCycle(wwin, event, True, False);
1625 break;
1627 case WKBD_FOCUSPREV:
1628 StartWindozeCycle(wwin, event, False, False);
1629 break;
1631 case WKBD_GROUPNEXT:
1632 StartWindozeCycle(wwin, event, True, True);
1633 break;
1635 case WKBD_GROUPPREV:
1636 StartWindozeCycle(wwin, event, False, True);
1637 break;
1639 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1640 widx = command - WKBD_WORKSPACE1;
1641 i = (scr->current_workspace / 10) * 10 + widx;
1642 if (wPreferences.ws_advance || i < scr->workspace_count)
1643 wWorkspaceChange(scr, i);
1644 break;
1646 case WKBD_NEXTWORKSPACE:
1647 wWorkspaceRelativeChange(scr, 1);
1648 break;
1649 case WKBD_PREVWORKSPACE:
1650 wWorkspaceRelativeChange(scr, -1);
1651 break;
1652 case WKBD_LASTWORKSPACE:
1653 wWorkspaceChange(scr, scr->last_workspace);
1654 break;
1656 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1657 widx = command - WKBD_MOVE_WORKSPACE1;
1658 i = (scr->current_workspace / 10) * 10 + widx;
1659 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1660 wWindowChangeWorkspace(wwin, i);
1661 break;
1663 case WKBD_MOVE_NEXTWORKSPACE:
1664 if (wwin)
1665 wWindowChangeWorkspaceRelative(wwin, 1);
1666 break;
1667 case WKBD_MOVE_PREVWORKSPACE:
1668 if (wwin)
1669 wWindowChangeWorkspaceRelative(wwin, -1);
1670 break;
1671 case WKBD_MOVE_LASTWORKSPACE:
1672 if (wwin)
1673 wWindowChangeWorkspace(wwin, scr->last_workspace);
1674 break;
1676 case WKBD_MOVE_NEXTWSLAYER:
1677 case WKBD_MOVE_PREVWSLAYER:
1679 if (wwin) {
1680 int row, column;
1682 row = scr->current_workspace / 10;
1683 column = scr->current_workspace % 10;
1685 if (command == WKBD_MOVE_NEXTWSLAYER) {
1686 if ((row + 1) * 10 < scr->workspace_count)
1687 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1688 } else {
1689 if (row > 0)
1690 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1694 break;
1696 case WKBD_WINDOW1:
1697 case WKBD_WINDOW2:
1698 case WKBD_WINDOW3:
1699 case WKBD_WINDOW4:
1700 case WKBD_WINDOW5:
1701 case WKBD_WINDOW6:
1702 case WKBD_WINDOW7:
1703 case WKBD_WINDOW8:
1704 case WKBD_WINDOW9:
1705 case WKBD_WINDOW10:
1707 widx = command - WKBD_WINDOW1;
1709 if (scr->shortcutWindows[widx]) {
1710 WMArray *list = scr->shortcutWindows[widx];
1711 int cw;
1712 int count = WMGetArrayItemCount(list);
1713 WWindow *twin;
1714 WMArrayIterator iter;
1715 WWindow *wwin;
1717 wUnselectWindows(scr);
1718 cw = scr->current_workspace;
1720 WM_ETARETI_ARRAY(list, wwin, iter) {
1721 if (count > 1)
1722 wWindowChangeWorkspace(wwin, cw);
1724 wMakeWindowVisible(wwin);
1726 if (count > 1)
1727 wSelectWindow(wwin, True);
1730 /* rotate the order of windows, to create a cycling effect */
1731 twin = WMGetFromArray(list, 0);
1732 WMDeleteFromArray(list, 0);
1733 WMAddToArray(list, twin);
1735 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1736 if (scr->shortcutWindows[widx]) {
1737 WMFreeArray(scr->shortcutWindows[widx]);
1738 scr->shortcutWindows[widx] = NULL;
1741 if (wwin->flags.selected && scr->selected_windows) {
1742 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1743 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1744 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1745 } else {
1746 scr->shortcutWindows[widx] = WMCreateArray(4);
1747 WMAddToArray(scr->shortcutWindows[widx], wwin);
1750 wSelectWindow(wwin, !wwin->flags.selected);
1751 XFlush(dpy);
1752 wusleep(3000);
1753 wSelectWindow(wwin, !wwin->flags.selected);
1754 XFlush(dpy);
1756 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1758 if (wwin->flags.selected && scr->selected_windows) {
1759 if (scr->shortcutWindows[widx]) {
1760 WMFreeArray(scr->shortcutWindows[widx]);
1762 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1766 break;
1768 case WKBD_RELAUNCH:
1769 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1770 (void) RelaunchWindow(wwin);
1772 break;
1774 case WKBD_SWITCH_SCREEN:
1775 if (w_global.screen_count > 1) {
1776 WScreen *scr2;
1777 int i;
1779 /* find index of this screen */
1780 for (i = 0; i < w_global.screen_count; i++) {
1781 if (wScreenWithNumber(i) == scr)
1782 break;
1784 i++;
1785 if (i >= w_global.screen_count) {
1786 i = 0;
1788 scr2 = wScreenWithNumber(i);
1790 if (scr2) {
1791 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1792 scr2->scr_width / 2, scr2->scr_height / 2);
1795 break;
1797 case WKBD_RUN:
1799 char *cmdline;
1801 cmdline = ExpandOptions(scr, _("exec %A(Run,Type command to run:)"));
1803 if (cmdline) {
1804 XGrabPointer(dpy, scr->root_win, True, 0,
1805 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1806 XSync(dpy, False);
1808 ExecuteShellCommand(scr, cmdline);
1809 wfree(cmdline);
1811 XUngrabPointer(dpy, CurrentTime);
1812 XSync(dpy, False);
1814 break;
1817 case WKBD_NEXTWSLAYER:
1818 case WKBD_PREVWSLAYER:
1820 int row, column;
1822 row = scr->current_workspace / 10;
1823 column = scr->current_workspace % 10;
1825 if (command == WKBD_NEXTWSLAYER) {
1826 if ((row + 1) * 10 < scr->workspace_count)
1827 wWorkspaceChange(scr, column + (row + 1) * 10);
1828 } else {
1829 if (row > 0)
1830 wWorkspaceChange(scr, column + (row - 1) * 10);
1833 break;
1834 case WKBD_CLIPRAISELOWER:
1835 if (!wPreferences.flags.noclip)
1836 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1837 break;
1838 case WKBD_DOCKRAISELOWER:
1839 if (!wPreferences.flags.nodock)
1840 wDockRaiseLower(scr->dock);
1841 break;
1842 #ifdef KEEP_XKB_LOCK_STATUS
1843 case WKBD_TOGGLE:
1844 if (wPreferences.modelock) {
1845 /*toggle */
1846 wwin = scr->focused_window;
1848 if (wwin && wwin->flags.mapped
1849 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1850 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1851 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1853 wwin->frame->languagemode = wwin->frame->last_languagemode;
1854 wwin->frame->last_languagemode = staterec.group;
1855 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1859 break;
1860 #endif /* KEEP_XKB_LOCK_STATUS */
1864 static void handleMotionNotify(XEvent * event)
1866 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1868 if (wPreferences.scrollable_menus) {
1869 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1870 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1872 if (scr->flags.jump_back_pending ||
1873 p.x <= (rect.pos.x + 1) ||
1874 p.x >= (rect.pos.x + rect.size.width - 2) ||
1875 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1876 WMenu *menu;
1878 menu = wMenuUnderPointer(scr);
1879 if (menu != NULL)
1880 wMenuScroll(menu);
1885 static void handleVisibilityNotify(XEvent * event)
1887 WWindow *wwin;
1889 wwin = wWindowFor(event->xvisibility.window);
1890 if (!wwin)
1891 return;
1892 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
1895 static void handle_selection_clear(XSelectionClearEvent *event)
1897 WScreen *scr = wScreenForWindow(event->window);
1899 if (!scr)
1900 return;
1902 if (event->selection != scr->sn_atom)
1903 return;
1905 wmessage(_("another window manager is replacing us!"));
1906 Shutdown(WSExitMode);