New Adwaita style which approximates the look of the default GTK theme.
[wmaker-crm.git] / src / event.c
blob440cc95f2c7f40dd26a2f3a0aa58c6f11ba39cac
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 #include <X11/Xatom.h>
41 #ifdef USE_XSHAPE
42 # include <X11/extensions/shape.h>
43 #endif
44 #ifdef USE_DOCK_XDND
45 #include "xdnd.h"
46 #endif
48 #ifdef USE_RANDR
49 #include <X11/extensions/Xrandr.h>
50 #endif
52 #ifdef KEEP_XKB_LOCK_STATUS
53 #include <X11/XKBlib.h>
54 #endif /* KEEP_XKB_LOCK_STATUS */
56 #include "WindowMaker.h"
57 #include "window.h"
58 #include "actions.h"
59 #include "client.h"
60 #include "main.h"
61 #include "cycling.h"
62 #include "keybind.h"
63 #include "application.h"
64 #include "stacking.h"
65 #include "defaults.h"
66 #include "workspace.h"
67 #include "dock.h"
68 #include "framewin.h"
69 #include "properties.h"
70 #include "balloon.h"
71 #include "xinerama.h"
72 #include "wmspec.h"
73 #include "rootmenu.h"
74 #include "colormap.h"
75 #include "screen.h"
76 #include "shutdown.h"
77 #include "misc.h"
78 #include "event.h"
79 #include "winmenu.h"
80 #include "switchmenu.h"
81 #include "wsmap.h"
84 #define MOD_MASK wPreferences.modifier_mask
86 /************ Local stuff ***********/
88 static void saveTimestamp(XEvent *event);
89 static void handleColormapNotify(XEvent *event);
90 static void handleMapNotify(XEvent *event);
91 static void handleUnmapNotify(XEvent *event);
92 static void handleButtonPress(XEvent *event);
93 static void handleExpose(XEvent *event);
94 static void handleDestroyNotify(XEvent *event);
95 static void handleConfigureRequest(XEvent *event);
96 static void handleMapRequest(XEvent *event);
97 static void handlePropertyNotify(XEvent *event);
98 static void handleEnterNotify(XEvent *event);
99 static void handleLeaveNotify(XEvent *event);
100 static void handleExtensions(XEvent *event);
101 static void handleClientMessage(XEvent *event);
102 static void handleKeyPress(XEvent *event);
103 static void handleFocusIn(XEvent *event);
104 static void handleMotionNotify(XEvent *event);
105 static void handleVisibilityNotify(XEvent *event);
106 static void handle_inotify_events(void);
107 static void handle_selection_request(XSelectionRequestEvent *event);
108 static void handle_selection_clear(XSelectionClearEvent *event);
109 static void wdelete_death_handler(WMagicNumber id);
112 #ifdef USE_XSHAPE
113 static void handleShapeNotify(XEvent *event);
114 #endif
116 #ifdef KEEP_XKB_LOCK_STATUS
117 static void handleXkbIndicatorStateNotify(XkbEvent *event);
118 #endif
120 /* real dead process handler */
121 static void handleDeadProcess(void);
123 typedef struct DeadProcesses {
124 pid_t pid;
125 unsigned char exit_status;
126 } DeadProcesses;
128 /* stack of dead processes */
129 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
130 static int deadProcessPtr = 0;
132 typedef struct DeathHandler {
133 WDeathHandler *callback;
134 pid_t pid;
135 void *client_data;
136 } DeathHandler;
138 static WMArray *deathHandlers = NULL;
140 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
142 DeathHandler *handler;
144 handler = malloc(sizeof(DeathHandler));
145 if (!handler)
146 return 0;
148 handler->pid = pid;
149 handler->callback = callback;
150 handler->client_data = cdata;
152 if (!deathHandlers)
153 deathHandlers = WMCreateArrayWithDestructor(8, free);
155 WMAddToArray(deathHandlers, handler);
157 return handler;
160 static void wdelete_death_handler(WMagicNumber id)
162 DeathHandler *handler = (DeathHandler *) id;
164 if (!handler || !deathHandlers)
165 return;
167 /* array destructor will call free(handler) */
168 WMRemoveFromArray(deathHandlers, handler);
171 void DispatchEvent(XEvent * event)
173 if (deathHandlers)
174 handleDeadProcess();
176 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
177 WCHANGE_STATE(WSTATE_EXITING);
178 /* received SIGTERM */
180 * WMHandleEvent() can't be called from anything
181 * executed inside here, or we can get in a infinite
182 * recursive loop.
184 Shutdown(WSExitMode);
186 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
187 WCHANGE_STATE(WSTATE_RESTARTING);
189 Shutdown(WSRestartPreparationMode);
190 /* received SIGHUP */
191 Restart(NULL, True);
192 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
193 WCHANGE_STATE(WSTATE_NORMAL);
194 wDefaultsCheckDomains(NULL);
197 /* for the case that all that is wanted to be dispatched is
198 * the stuff above */
199 if (!event)
200 return;
202 saveTimestamp(event);
203 switch (event->type) {
204 case MapRequest:
205 handleMapRequest(event);
206 break;
208 case KeyPress:
209 handleKeyPress(event);
210 break;
212 case MotionNotify:
213 handleMotionNotify(event);
214 break;
216 case ConfigureRequest:
217 handleConfigureRequest(event);
218 break;
220 case DestroyNotify:
221 handleDestroyNotify(event);
222 break;
224 case MapNotify:
225 handleMapNotify(event);
226 break;
228 case UnmapNotify:
229 handleUnmapNotify(event);
230 break;
232 case ButtonPress:
233 handleButtonPress(event);
234 break;
236 case Expose:
237 handleExpose(event);
238 break;
240 case PropertyNotify:
241 handlePropertyNotify(event);
242 break;
244 case EnterNotify:
245 handleEnterNotify(event);
246 break;
248 case LeaveNotify:
249 handleLeaveNotify(event);
250 break;
252 case ClientMessage:
253 handleClientMessage(event);
254 break;
256 case ColormapNotify:
257 handleColormapNotify(event);
258 break;
260 case MappingNotify:
261 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
262 XRefreshKeyboardMapping(&event->xmapping);
263 break;
265 case FocusIn:
266 handleFocusIn(event);
267 break;
269 case VisibilityNotify:
270 handleVisibilityNotify(event);
271 break;
273 case ConfigureNotify:
274 #ifdef USE_RANDR
275 if (event->xconfigure.window == DefaultRootWindow(dpy))
276 XRRUpdateConfiguration(event);
277 #endif
278 break;
280 case SelectionRequest:
281 handle_selection_request(&event->xselectionrequest);
282 break;
284 case SelectionClear:
285 handle_selection_clear(&event->xselectionclear);
286 break;
288 default:
289 handleExtensions(event);
290 break;
294 #ifdef HAVE_INOTIFY
296 *----------------------------------------------------------------------
297 * handle_inotify_events-
298 * Check for inotify events
300 * Returns:
301 * After reading events for the given file descriptor (fd) and
302 * watch descriptor (wd)
304 * Side effects:
305 * Calls wDefaultsCheckDomains if config database is updated
306 *----------------------------------------------------------------------
308 static void handle_inotify_events(void)
310 ssize_t eventQLength;
311 size_t i = 0;
312 /* Make room for at lease 5 simultaneous events, with path + filenames */
313 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
314 /* Check config only once per read of the event queue */
315 int oneShotFlag = 0;
318 * Read off the queued events
319 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
320 * not occur; the block is on Xevents, but a config file change will normally
321 * occur as a result of an Xevent - so the event queue should never have more than
322 * a few entries before a read().
324 eventQLength = read(w_global.inotify.fd_event_queue,
325 buff, sizeof(buff) );
327 if (eventQLength < 0) {
328 wwarning(_("read problem when trying to get INotify event: %s"), strerror(errno));
329 return;
332 /* check what events occurred */
333 /* Should really check wd here too, but for now we only have one watch! */
334 while (i < eventQLength) {
335 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
338 * see inotify.h for event types.
340 if (pevent->mask & IN_DELETE_SELF) {
341 wwarning(_("the defaults database has been deleted!"
342 " Restart Window Maker to create the database" " with the default settings"));
344 if (w_global.inotify.fd_event_queue >= 0) {
345 close(w_global.inotify.fd_event_queue);
346 w_global.inotify.fd_event_queue = -1;
349 if (pevent->mask & IN_UNMOUNT) {
350 wwarning(_("the unit containing the defaults database has"
351 " been unmounted. Setting --static mode." " Any changes will not be saved."));
353 if (w_global.inotify.fd_event_queue >= 0) {
354 close(w_global.inotify.fd_event_queue);
355 w_global.inotify.fd_event_queue = -1;
358 wPreferences.flags.noupdates = 1;
360 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
361 wwarning(_("Inotify: Reading config files in defaults database."));
362 wDefaultsCheckDomains(NULL);
363 oneShotFlag = 1;
366 /* move to next event in the buffer */
367 i += sizeof(struct inotify_event) + pevent->len;
370 #endif /* HAVE_INOTIFY */
373 *----------------------------------------------------------------------
374 * EventLoop-
375 * Processes X and internal events indefinitely.
377 * Returns:
378 * Never returns
380 * Side effects:
381 * The LastTimestamp global variable is updated.
382 * Calls inotifyGetEvents if defaults database changes.
383 *----------------------------------------------------------------------
385 noreturn void EventLoop(void)
387 XEvent event;
388 #ifdef HAVE_INOTIFY
389 struct timeval time;
390 fd_set rfds;
391 int retVal = 0;
393 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
394 retVal = -1;
395 #endif
397 for (;;) {
399 WMNextEvent(dpy, &event); /* Blocks here */
400 WMHandleEvent(&event);
401 #ifdef HAVE_INOTIFY
402 if (retVal != -1) {
403 time.tv_sec = 0;
404 time.tv_usec = 0;
405 FD_ZERO(&rfds);
406 FD_SET(w_global.inotify.fd_event_queue, &rfds);
408 /* check for available read data from inotify - don't block! */
409 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
411 if (retVal < 0) { /* an error has occurred */
412 wwarning(_("select failed. The inotify instance will be closed."
413 " Changes to the defaults database will require"
414 " a restart to take effect."));
415 close(w_global.inotify.fd_event_queue);
416 w_global.inotify.fd_event_queue = -1;
417 continue;
419 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
420 handle_inotify_events();
422 #endif
427 *----------------------------------------------------------------------
428 * ProcessPendingEvents --
429 * Processes the events that are currently pending (at the time
430 * this function is called) in the display's queue.
432 * Returns:
433 * After the pending events that were present at the function call
434 * are processed.
436 * Side effects:
437 * Many -- whatever handling events may involve.
439 *----------------------------------------------------------------------
441 void ProcessPendingEvents(void)
443 XEvent event;
444 int count;
446 XSync(dpy, False);
448 /* Take a snapshot of the event count in the queue */
449 count = XPending(dpy);
451 while (count > 0 && XPending(dpy)) {
452 WMNextEvent(dpy, &event);
453 WMHandleEvent(&event);
454 count--;
458 Bool IsDoubleClick(WScreen * scr, XEvent * event)
460 if ((scr->last_click_time > 0) &&
461 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
462 && (event->xbutton.button == scr->last_click_button)
463 && (event->xbutton.window == scr->last_click_window)) {
465 scr->flags.next_click_is_not_double = 1;
466 scr->last_click_time = 0;
467 scr->last_click_window = event->xbutton.window;
469 return True;
471 return False;
474 void NotifyDeadProcess(pid_t pid, unsigned char status)
476 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
477 wwarning("stack overflow: too many dead processes");
478 return;
480 /* stack the process to be handled later,
481 * as this is called from the signal handler */
482 deadProcesses[deadProcessPtr].pid = pid;
483 deadProcesses[deadProcessPtr].exit_status = status;
484 deadProcessPtr++;
487 static void handleDeadProcess(void)
489 DeathHandler *tmp;
490 int i;
492 for (i = 0; i < deadProcessPtr; i++) {
493 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
496 if (!deathHandlers) {
497 deadProcessPtr = 0;
498 return;
501 /* get the pids on the queue and call handlers */
502 while (deadProcessPtr > 0) {
503 deadProcessPtr--;
505 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
506 tmp = WMGetFromArray(deathHandlers, i);
507 if (!tmp)
508 continue;
510 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
511 (*tmp->callback) (tmp->pid,
512 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
513 wdelete_death_handler(tmp);
519 static void saveTimestamp(XEvent * event)
522 * Never save CurrentTime as LastTimestamp because CurrentTime
523 * it's not a real timestamp (it's the 0L constant)
526 switch (event->type) {
527 case ButtonRelease:
528 case ButtonPress:
529 w_global.timestamp.last_event = event->xbutton.time;
530 break;
531 case KeyPress:
532 case KeyRelease:
533 w_global.timestamp.last_event = event->xkey.time;
534 break;
535 case MotionNotify:
536 w_global.timestamp.last_event = event->xmotion.time;
537 break;
538 case PropertyNotify:
539 w_global.timestamp.last_event = event->xproperty.time;
540 break;
541 case EnterNotify:
542 case LeaveNotify:
543 w_global.timestamp.last_event = event->xcrossing.time;
544 break;
545 case SelectionClear:
546 w_global.timestamp.last_event = event->xselectionclear.time;
547 break;
548 case SelectionRequest:
549 w_global.timestamp.last_event = event->xselectionrequest.time;
550 break;
551 case SelectionNotify:
552 w_global.timestamp.last_event = event->xselection.time;
553 #ifdef USE_DOCK_XDND
554 wXDNDProcessSelection(event);
555 #endif
556 break;
560 static int matchWindow(const void *item, const void *cdata)
562 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
565 static void handleExtensions(XEvent * event)
567 #ifdef USE_XSHAPE
568 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
569 handleShapeNotify(event);
571 #endif
572 #ifdef KEEP_XKB_LOCK_STATUS
573 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
574 handleXkbIndicatorStateNotify((XkbEvent *) event);
576 #endif /*KEEP_XKB_LOCK_STATUS */
577 #ifdef USE_RANDR
578 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
579 /* From xrandr man page: "Clients must call back into Xlib using
580 * XRRUpdateConfiguration when screen configuration change notify
581 * events are generated */
582 XRRUpdateConfiguration(event);
583 WCHANGE_STATE(WSTATE_RESTARTING);
584 Shutdown(WSRestartPreparationMode);
585 Restart(NULL,True);
587 #endif
590 static void handleMapRequest(XEvent * ev)
592 WWindow *wwin;
593 WScreen *scr = NULL;
594 Window window = ev->xmaprequest.window;
596 wwin = wWindowFor(window);
597 if (wwin != NULL) {
598 if (wwin->flags.shaded) {
599 wUnshadeWindow(wwin);
601 /* deiconify window */
602 if (wwin->flags.miniaturized) {
603 wDeiconifyWindow(wwin);
604 } else if (wwin->flags.hidden) {
605 WApplication *wapp = wApplicationOf(wwin->main_window);
606 /* go to the last workspace that the user worked on the app */
607 if (wapp) {
608 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
610 wUnhideApplication(wapp, False, False);
612 return;
615 scr = wScreenForRootWindow(ev->xmaprequest.parent);
617 wwin = wManageWindow(scr, window);
620 * This is to let the Dock know that the application it launched
621 * has already been mapped (eg: it has finished launching).
622 * It is not necessary for normally docked apps, but is needed for
623 * apps that were forcedly docked (like with dockit).
625 if (scr->last_dock) {
626 if (wwin && wwin->main_window != None && wwin->main_window != window)
627 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
628 else
629 wDockTrackWindowLaunch(scr->last_dock, window);
632 if (wwin) {
633 wClientSetState(wwin, NormalState, None);
634 if (wwin->flags.maximized) {
635 wMaximizeWindow(wwin, wwin->flags.maximized,
636 wGetHeadForWindow(wwin));
638 if (wwin->flags.shaded) {
639 wwin->flags.shaded = 0;
640 wwin->flags.skip_next_animation = 1;
641 wShadeWindow(wwin);
643 if (wwin->flags.miniaturized) {
644 wwin->flags.miniaturized = 0;
645 wwin->flags.skip_next_animation = 1;
646 wIconifyWindow(wwin);
648 if (wwin->flags.fullscreen) {
649 wwin->flags.fullscreen = 0;
650 wFullscreenWindow(wwin);
652 if (wwin->flags.hidden) {
653 WApplication *wapp = wApplicationOf(wwin->main_window);
655 wwin->flags.hidden = 0;
656 wwin->flags.skip_next_animation = 1;
657 if (wapp) {
658 wHideApplication(wapp);
664 static void handleDestroyNotify(XEvent * event)
666 WWindow *wwin;
667 WApplication *app;
668 Window window = event->xdestroywindow.window;
669 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
670 int widx;
672 wwin = wWindowFor(window);
673 if (wwin) {
674 wUnmanageWindow(wwin, False, True);
677 if (scr != NULL) {
678 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
679 WFakeGroupLeader *fPtr;
681 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
682 if (fPtr->retainCount > 0) {
683 fPtr->retainCount--;
684 if (fPtr->retainCount == 0 && fPtr->leader != None) {
685 XDestroyWindow(dpy, fPtr->leader);
686 fPtr->leader = None;
687 XFlush(dpy);
690 fPtr->origLeader = None;
694 app = wApplicationOf(window);
695 if (app) {
696 if (window == app->main_window) {
697 app->refcount = 0;
698 wwin = app->main_window_desc->screen_ptr->focused_window;
699 while (wwin) {
700 if (wwin->main_window == window) {
701 wwin->main_window = None;
703 wwin = wwin->prev;
706 wApplicationDestroy(app);
710 static void handleExpose(XEvent * event)
712 WObjDescriptor *desc;
713 XEvent ev;
715 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
717 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
718 return;
721 if (desc->handle_expose) {
722 (*desc->handle_expose) (desc, event);
726 static void executeWheelAction(WScreen *scr, XEvent *event, int action)
728 WWindow *wwin;
729 Bool next_direction;
731 if (event->xbutton.button == Button5 || event->xbutton.button == Button6)
732 next_direction = False;
733 else
734 next_direction = True;
736 switch (action) {
737 case WA_SWITCH_WORKSPACES:
738 if (next_direction)
739 wWorkspaceRelativeChange(scr, 1);
740 else
741 wWorkspaceRelativeChange(scr, -1);
742 break;
744 case WA_SWITCH_WINDOWS:
745 wwin = scr->focused_window;
746 if (next_direction)
747 wWindowFocusNext(wwin, True);
748 else
749 wWindowFocusPrev(wwin, True);
750 break;
754 static void executeButtonAction(WScreen *scr, XEvent *event, int action)
756 WWindow *wwin;
758 switch (action) {
759 case WA_SELECT_WINDOWS:
760 wUnselectWindows(scr);
761 wSelectWindows(scr, event);
762 break;
763 case WA_OPEN_APPMENU:
764 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
765 /* ugly hack */
766 if (scr->root_menu) {
767 if (scr->root_menu->brother->flags.mapped)
768 event->xbutton.window = scr->root_menu->brother->frame->core->window;
769 else
770 event->xbutton.window = scr->root_menu->frame->core->window;
772 break;
773 case WA_OPEN_WINLISTMENU:
774 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
775 if (scr->switch_menu) {
776 if (scr->switch_menu->brother->flags.mapped)
777 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
778 else
779 event->xbutton.window = scr->switch_menu->frame->core->window;
781 break;
782 case WA_MOVE_PREVWORKSPACE:
783 wWorkspaceRelativeChange(scr, -1);
784 break;
785 case WA_MOVE_NEXTWORKSPACE:
786 wWorkspaceRelativeChange(scr, 1);
787 break;
788 case WA_MOVE_PREVWINDOW:
789 wwin = scr->focused_window;
790 wWindowFocusPrev(wwin, True);
791 break;
792 case WA_MOVE_NEXTWINDOW:
793 wwin = scr->focused_window;
794 wWindowFocusNext(wwin, True);
795 break;
799 /* bindable */
800 static void handleButtonPress(XEvent * event)
802 WObjDescriptor *desc;
803 WScreen *scr;
805 scr = wScreenForRootWindow(event->xbutton.root);
807 #ifdef BALLOON_TEXT
808 wBalloonHide(scr);
809 #endif
811 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
812 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
813 executeButtonAction(scr, event, wPreferences.mouse_button1);
814 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
815 executeButtonAction(scr, event, wPreferences.mouse_button2);
816 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
817 executeButtonAction(scr, event, wPreferences.mouse_button3);
818 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
819 executeButtonAction(scr, event, wPreferences.mouse_button8);
820 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
821 executeButtonAction(scr, event, wPreferences.mouse_button9);
822 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
823 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
824 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
825 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
826 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
827 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
828 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
829 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
833 desc = NULL;
834 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
835 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
836 return;
840 if (desc->parent_type == WCLASS_WINDOW) {
841 XSync(dpy, 0);
843 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
844 XAllowEvents(dpy, AsyncPointer, CurrentTime);
845 } else {
846 /* if (wPreferences.focus_mode == WKF_CLICK) { */
847 if (wPreferences.ignore_focus_click) {
848 XAllowEvents(dpy, AsyncPointer, CurrentTime);
850 XAllowEvents(dpy, ReplayPointer, CurrentTime);
851 /* } */
853 XSync(dpy, 0);
854 } else if (desc->parent_type == WCLASS_APPICON
855 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
856 if (event->xbutton.state & MOD_MASK) {
857 XSync(dpy, 0);
858 XAllowEvents(dpy, AsyncPointer, CurrentTime);
859 XSync(dpy, 0);
863 if (desc->handle_mousedown != NULL) {
864 (*desc->handle_mousedown) (desc, event);
867 /* save double-click information */
868 if (scr->flags.next_click_is_not_double) {
869 scr->flags.next_click_is_not_double = 0;
870 } else {
871 scr->last_click_time = event->xbutton.time;
872 scr->last_click_button = event->xbutton.button;
873 scr->last_click_window = event->xbutton.window;
877 static void handleMapNotify(XEvent * event)
879 WWindow *wwin;
881 wwin = wWindowFor(event->xmap.event);
882 if (wwin && wwin->client_win == event->xmap.event) {
883 if (wwin->flags.miniaturized) {
884 wDeiconifyWindow(wwin);
885 } else {
886 XGrabServer(dpy);
887 wWindowMap(wwin);
888 wClientSetState(wwin, NormalState, None);
889 XUngrabServer(dpy);
894 static void handleUnmapNotify(XEvent * event)
896 WWindow *wwin;
897 XEvent ev;
898 Bool withdraw = False;
900 /* only process windows with StructureNotify selected
901 * (ignore SubstructureNotify) */
902 wwin = wWindowFor(event->xunmap.window);
903 if (!wwin)
904 return;
906 /* whether the event is a Withdrawal request */
907 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
908 withdraw = True;
910 if (wwin->client_win != event->xunmap.event && !withdraw)
911 return;
913 if (!wwin->flags.mapped && !withdraw
914 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
915 && !wwin->flags.miniaturized && !wwin->flags.hidden)
916 return;
918 XGrabServer(dpy);
919 XUnmapWindow(dpy, wwin->frame->core->window);
920 wwin->flags.mapped = 0;
921 XSync(dpy, 0);
922 /* check if the window was destroyed */
923 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
924 DispatchEvent(&ev);
925 } else {
926 Bool reparented = False;
928 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
929 reparented = True;
931 /* withdraw window */
932 wwin->flags.mapped = 0;
933 if (!reparented)
934 wClientSetState(wwin, WithdrawnState, None);
936 /* if the window was reparented, do not reparent it back to the
937 * root window */
938 wUnmanageWindow(wwin, !reparented, False);
940 XUngrabServer(dpy);
943 static void handleConfigureRequest(XEvent * event)
945 WWindow *wwin;
947 wwin = wWindowFor(event->xconfigurerequest.window);
948 if (wwin == NULL) {
950 * Configure request for unmapped window
952 wClientConfigure(NULL, &(event->xconfigurerequest));
953 } else {
954 wClientConfigure(wwin, &(event->xconfigurerequest));
958 static void handlePropertyNotify(XEvent * event)
960 WWindow *wwin;
961 WApplication *wapp;
962 Window jr;
963 int ji;
964 unsigned int ju;
966 wwin = wWindowFor(event->xproperty.window);
967 if (wwin) {
968 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
969 return;
971 wClientCheckProperty(wwin, &event->xproperty);
973 wapp = wApplicationOf(event->xproperty.window);
974 if (wapp) {
975 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
979 static void handleClientMessage(XEvent * event)
981 WWindow *wwin;
982 WObjDescriptor *desc;
984 /* handle transition from Normal to Iconic state */
985 if (event->xclient.message_type == w_global.atom.wm.change_state
986 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
988 wwin = wWindowFor(event->xclient.window);
989 if (!wwin)
990 return;
991 if (!wwin->flags.miniaturized)
992 wIconifyWindow(wwin);
993 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
994 WScreen *scr = wScreenForRootWindow(event->xclient.window);
996 if (!scr)
997 return;
999 if (event->xclient.data.l[1] == 1) { /* starting */
1000 wColormapAllowClientInstallation(scr, True);
1001 } else { /* stopping */
1002 wColormapAllowClientInstallation(scr, False);
1004 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
1006 char *command;
1007 size_t len;
1009 len = sizeof(event->xclient.data.b) + 1;
1010 command = wmalloc(len);
1011 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
1013 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1014 wwarning(_("Got Reconfigure command"));
1015 wDefaultsCheckDomains(NULL);
1016 } else {
1017 wwarning(_("Got unknown command %s"), command);
1020 wfree(command);
1022 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1023 WApplication *wapp;
1024 int done = 0;
1025 wapp = wApplicationOf(event->xclient.window);
1026 if (wapp) {
1027 switch (event->xclient.data.l[0]) {
1028 case WMFHideOtherApplications:
1029 wHideOtherApplications(wapp->main_window_desc);
1030 done = 1;
1031 break;
1033 case WMFHideApplication:
1034 wHideApplication(wapp);
1035 done = 1;
1036 break;
1039 if (!done) {
1040 wwin = wWindowFor(event->xclient.window);
1041 if (wwin) {
1042 switch (event->xclient.data.l[0]) {
1043 case WMFHideOtherApplications:
1044 wHideOtherApplications(wwin);
1045 break;
1047 case WMFHideApplication:
1048 wHideApplication(wApplicationOf(wwin->main_window));
1049 break;
1053 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1054 wwin = wWindowFor(event->xclient.window);
1055 if (!wwin)
1056 return;
1057 switch (event->xclient.data.l[0]) {
1058 case GSWindowLevelAttr:
1060 int level = (int)event->xclient.data.l[1];
1062 if (WINDOW_LEVEL(wwin) != level) {
1063 ChangeStackingLevel(wwin->frame->core, level);
1066 break;
1068 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1069 wwin = wWindowFor(event->xclient.window);
1070 if (!wwin)
1071 return;
1072 switch (event->xclient.data.l[0]) {
1073 case WMTitleBarNormal:
1074 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1075 break;
1076 case WMTitleBarMain:
1077 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1078 break;
1079 case WMTitleBarKey:
1080 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1081 break;
1083 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1084 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1085 if (!scr)
1086 return;
1087 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1088 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1089 /* do nothing */
1090 #ifdef USE_DOCK_XDND
1091 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1092 /* do nothing */
1093 #endif /* USE_DOCK_XDND */
1094 } else {
1096 * Non-standard thing, but needed by OffiX DND.
1097 * For when the icon frame gets a ClientMessage
1098 * that should have gone to the icon_window.
1100 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1101 struct WIcon *icon = NULL;
1103 if (desc->parent_type == WCLASS_MINIWINDOW) {
1104 icon = (WIcon *) desc->parent;
1105 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1106 icon = ((WAppIcon *) desc->parent)->icon;
1108 if (icon && (wwin = icon->owner)) {
1109 if (wwin->client_win != event->xclient.window) {
1110 event->xclient.window = wwin->client_win;
1111 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1118 static void raiseWindow(WScreen * scr)
1120 WWindow *wwin;
1122 scr->autoRaiseTimer = NULL;
1124 wwin = wWindowFor(scr->autoRaiseWindow);
1125 if (!wwin)
1126 return;
1128 if (!wwin->flags.destroyed && wwin->flags.focused) {
1129 wRaiseFrame(wwin->frame->core);
1130 /* this is needed or a race condition will occur */
1131 XSync(dpy, False);
1135 static void handleEnterNotify(XEvent * event)
1137 WWindow *wwin;
1138 WObjDescriptor *desc = NULL;
1139 XEvent ev;
1140 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1142 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1143 /* already left the window... */
1144 saveTimestamp(&ev);
1145 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1146 return;
1150 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1151 if (desc->handle_enternotify)
1152 (*desc->handle_enternotify) (desc, event);
1155 /* enter to window */
1156 wwin = wWindowFor(event->xcrossing.window);
1157 if (!wwin) {
1158 if (wPreferences.colormap_mode == WCM_POINTER) {
1159 wColormapInstallForWindow(scr, NULL);
1161 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1162 WMDeleteTimerHandler(scr->autoRaiseTimer);
1163 scr->autoRaiseTimer = NULL;
1165 } else {
1166 /* set auto raise timer even if in focus-follows-mouse mode
1167 * and the event is for the frame window, even if the window
1168 * has focus already. useful if you move the pointer from a focused
1169 * window to the root window and back pretty fast
1171 * set focus if in focus-follows-mouse mode and the event
1172 * is for the frame window and window doesn't have focus yet */
1173 if (wPreferences.focus_mode == WKF_SLOPPY
1174 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1176 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1177 wSetFocusTo(scr, wwin);
1179 if (scr->autoRaiseTimer)
1180 WMDeleteTimerHandler(scr->autoRaiseTimer);
1181 scr->autoRaiseTimer = NULL;
1183 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1184 scr->autoRaiseWindow = wwin->frame->core->window;
1185 scr->autoRaiseTimer
1186 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1189 /* Install colormap for window, if the colormap installation mode
1190 * is colormap_follows_mouse */
1191 if (wPreferences.colormap_mode == WCM_POINTER) {
1192 if (wwin->client_win == event->xcrossing.window)
1193 wColormapInstallForWindow(scr, wwin);
1194 else
1195 wColormapInstallForWindow(scr, NULL);
1199 if (event->xcrossing.window == event->xcrossing.root
1200 && event->xcrossing.detail == NotifyNormal
1201 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1203 wSetFocusTo(scr, scr->focused_window);
1205 #ifdef BALLOON_TEXT
1206 wBalloonEnteredObject(scr, desc);
1207 #endif
1210 static void handleLeaveNotify(XEvent * event)
1212 WObjDescriptor *desc = NULL;
1214 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1215 if (desc->handle_leavenotify)
1216 (*desc->handle_leavenotify) (desc, event);
1220 #ifdef USE_XSHAPE
1221 static void handleShapeNotify(XEvent * event)
1223 XShapeEvent *shev = (XShapeEvent *) event;
1224 WWindow *wwin;
1225 union {
1226 XEvent xevent;
1227 XShapeEvent xshape;
1228 } ev;
1230 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1231 if (ev.xshape.kind == ShapeBounding) {
1232 if (ev.xshape.shaped == shev->shaped) {
1233 *shev = ev.xshape;
1234 } else {
1235 XPutBackEvent(dpy, &ev.xevent);
1236 break;
1241 wwin = wWindowFor(shev->window);
1242 if (!wwin || shev->kind != ShapeBounding)
1243 return;
1245 if (!shev->shaped && wwin->flags.shaped) {
1247 wwin->flags.shaped = 0;
1248 wWindowClearShape(wwin);
1250 } else if (shev->shaped) {
1252 wwin->flags.shaped = 1;
1253 wWindowSetShape(wwin);
1256 #endif /* USE_XSHAPE */
1258 #ifdef KEEP_XKB_LOCK_STATUS
1259 /* please help ]d if you know what to do */
1260 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1262 WWindow *wwin;
1263 WScreen *scr;
1264 XkbStateRec staterec;
1265 int i;
1267 for (i = 0; i < w_global.screen_count; i++) {
1268 scr = wScreenWithNumber(i);
1269 wwin = scr->focused_window;
1270 if (wwin && wwin->flags.focused) {
1271 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1272 if (wwin->frame->languagemode != staterec.group) {
1273 wwin->frame->last_languagemode = wwin->frame->languagemode;
1274 wwin->frame->languagemode = staterec.group;
1276 #ifdef XKB_BUTTON_HINT
1277 if (wwin->frame->titlebar) {
1278 wFrameWindowPaint(wwin->frame);
1280 #endif
1284 #endif /*KEEP_XKB_LOCK_STATUS */
1286 static void handleColormapNotify(XEvent * event)
1288 WWindow *wwin;
1289 WScreen *scr;
1290 Bool reinstall = False;
1292 wwin = wWindowFor(event->xcolormap.window);
1293 if (!wwin)
1294 return;
1296 scr = wwin->screen_ptr;
1298 do {
1299 if (wwin) {
1300 if (event->xcolormap.new) {
1301 XWindowAttributes attr;
1303 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1305 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1306 scr->current_colormap = attr.colormap;
1308 reinstall = True;
1309 } else if (event->xcolormap.state == ColormapUninstalled &&
1310 scr->current_colormap == event->xcolormap.colormap) {
1312 /* some bastard app (like XV) removed our colormap */
1314 * can't enforce or things like xscreensaver won't work
1315 * reinstall = True;
1317 } else if (event->xcolormap.state == ColormapInstalled &&
1318 scr->current_colormap == event->xcolormap.colormap) {
1320 /* someone has put our colormap back */
1321 reinstall = False;
1324 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1325 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1327 if (reinstall && scr->current_colormap != None) {
1328 if (!scr->flags.colormap_stuff_blocked)
1329 XInstallColormap(dpy, scr->current_colormap);
1333 static void handleFocusIn(XEvent * event)
1335 WWindow *wwin;
1338 * For applications that like stealing the focus.
1340 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1341 saveTimestamp(event);
1342 if (event->xfocus.mode == NotifyUngrab
1343 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1344 return;
1347 wwin = wWindowFor(event->xfocus.window);
1348 if (wwin && !wwin->flags.focused) {
1349 if (wwin->flags.mapped)
1350 wSetFocusTo(wwin->screen_ptr, wwin);
1351 else
1352 wSetFocusTo(wwin->screen_ptr, NULL);
1353 } else if (!wwin) {
1354 WScreen *scr = wScreenForWindow(event->xfocus.window);
1355 if (scr)
1356 wSetFocusTo(scr, NULL);
1360 static WWindow *windowUnderPointer(WScreen * scr)
1362 unsigned int mask;
1363 int foo;
1364 Window bar, win;
1366 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1367 return wWindowFor(win);
1368 return NULL;
1371 static int CheckFullScreenWindowFocused(WScreen * scr)
1373 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1374 return 1;
1375 else
1376 return 0;
1379 static void handleKeyPress(XEvent * event)
1381 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1382 WWindow *wwin = scr->focused_window;
1383 short i, widx;
1384 int modifiers;
1385 int command = -1;
1386 #ifdef KEEP_XKB_LOCK_STATUS
1387 XkbStateRec staterec;
1388 #endif /*KEEP_XKB_LOCK_STATUS */
1390 /* ignore CapsLock */
1391 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1393 for (i = 0; i < WKBD_LAST; i++) {
1394 if (wKeyBindings[i].keycode == 0)
1395 continue;
1397 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1398 || */ wKeyBindings[i].modifier ==
1399 modifiers)) {
1400 command = i;
1401 break;
1405 if (command < 0) {
1407 if (!wRootMenuPerformShortcut(event)) {
1408 static int dontLoop = 0;
1410 if (dontLoop > 10) {
1411 wwarning("problem with key event processing code");
1412 return;
1414 dontLoop++;
1415 /* if the focused window is an internal window, try redispatching
1416 * the event to the managed window, as it can be a WINGs window */
1417 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1418 /* client_leader contains the WINGs toplevel */
1419 event->xany.window = wwin->client_leader;
1420 WMHandleEvent(event);
1422 dontLoop--;
1424 return;
1426 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1427 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1429 switch (command) {
1431 case WKBD_ROOTMENU:
1432 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1433 if (!CheckFullScreenWindowFocused(scr)) {
1434 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1435 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1436 True);
1438 break;
1439 case WKBD_WINDOWLIST:
1440 if (!CheckFullScreenWindowFocused(scr)) {
1441 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1442 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1443 True);
1445 break;
1447 case WKBD_WINDOWMENU:
1448 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1449 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1450 break;
1451 case WKBD_MINIMIZEALL:
1452 CloseWindowMenu(scr);
1453 wHideAll(scr);
1454 break;
1455 case WKBD_MINIATURIZE:
1456 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1457 && !WFLAGP(wwin, no_miniaturizable)) {
1458 CloseWindowMenu(scr);
1460 if (wwin->protocols.MINIATURIZE_WINDOW)
1461 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1462 else {
1463 wIconifyWindow(wwin);
1466 break;
1467 case WKBD_HIDE:
1468 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1469 WApplication *wapp = wApplicationOf(wwin->main_window);
1470 CloseWindowMenu(scr);
1472 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1473 wHideApplication(wapp);
1476 break;
1477 case WKBD_HIDE_OTHERS:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1479 CloseWindowMenu(scr);
1481 wHideOtherApplications(wwin);
1483 break;
1484 case WKBD_MAXIMIZE:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1490 break;
1491 case WKBD_VMAXIMIZE:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 CloseWindowMenu(scr);
1495 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1497 break;
1498 case WKBD_HMAXIMIZE:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1500 CloseWindowMenu(scr);
1502 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1503 movePionterToWindowCenter(wwin);
1505 break;
1506 case WKBD_LHMAXIMIZE:
1507 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1508 CloseWindowMenu(scr);
1510 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1511 movePionterToWindowCenter(wwin);
1513 break;
1514 case WKBD_RHMAXIMIZE:
1515 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1516 CloseWindowMenu(scr);
1518 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1519 movePionterToWindowCenter(wwin);
1521 break;
1522 case WKBD_THMAXIMIZE:
1523 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1524 CloseWindowMenu(scr);
1526 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1527 movePionterToWindowCenter(wwin);
1529 break;
1530 case WKBD_BHMAXIMIZE:
1531 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1532 CloseWindowMenu(scr);
1534 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1535 movePionterToWindowCenter(wwin);
1537 break;
1538 case WKBD_LTCMAXIMIZE:
1539 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1540 CloseWindowMenu(scr);
1542 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1543 movePionterToWindowCenter(wwin);
1545 break;
1546 case WKBD_RTCMAXIMIZE:
1547 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1548 CloseWindowMenu(scr);
1550 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1551 movePionterToWindowCenter(wwin);
1553 break;
1554 case WKBD_LBCMAXIMIZE:
1555 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1556 CloseWindowMenu(scr);
1558 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1559 movePionterToWindowCenter(wwin);
1561 break;
1562 case WKBD_RBCMAXIMIZE:
1563 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1564 CloseWindowMenu(scr);
1566 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1567 movePionterToWindowCenter(wwin);
1569 break;
1570 case WKBD_MAXIMUS:
1571 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1572 CloseWindowMenu(scr);
1574 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1576 break;
1577 case WKBD_KEEP_ON_TOP:
1578 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1579 CloseWindowMenu(scr);
1581 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
1582 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
1583 else
1584 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1586 break;
1588 case WKBD_KEEP_AT_BOTTOM:
1589 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1590 CloseWindowMenu(scr);
1592 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
1593 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
1594 else
1595 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1597 break;
1598 case WKBD_OMNIPRESENT:
1599 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1600 CloseWindowMenu(scr);
1602 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1604 break;
1605 case WKBD_RAISE:
1606 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1607 CloseWindowMenu(scr);
1609 wRaiseFrame(wwin->frame->core);
1611 break;
1612 case WKBD_LOWER:
1613 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1614 CloseWindowMenu(scr);
1616 wLowerFrame(wwin->frame->core);
1618 break;
1619 case WKBD_RAISELOWER:
1620 /* raise or lower the window under the pointer, not the
1621 * focused one
1623 wwin = windowUnderPointer(scr);
1624 if (wwin)
1625 wRaiseLowerFrame(wwin->frame->core);
1626 break;
1627 case WKBD_SHADE:
1628 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1629 if (wwin->flags.shaded)
1630 wUnshadeWindow(wwin);
1631 else
1632 wShadeWindow(wwin);
1634 break;
1635 case WKBD_MOVERESIZE:
1636 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1637 CloseWindowMenu(scr);
1639 wKeyboardMoveResizeWindow(wwin);
1641 break;
1642 case WKBD_CLOSE:
1643 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1644 CloseWindowMenu(scr);
1645 if (wwin->protocols.DELETE_WINDOW)
1646 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1648 break;
1649 case WKBD_SELECT:
1650 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1651 wSelectWindow(wwin, !wwin->flags.selected);
1653 break;
1655 case WKBD_WORKSPACEMAP:
1656 if (wPreferences.enable_workspace_pager)
1657 StartWorkspaceMap(scr);
1658 break;
1660 case WKBD_FOCUSNEXT:
1661 StartWindozeCycle(wwin, event, True, False);
1662 break;
1664 case WKBD_FOCUSPREV:
1665 StartWindozeCycle(wwin, event, False, False);
1666 break;
1668 case WKBD_GROUPNEXT:
1669 StartWindozeCycle(wwin, event, True, True);
1670 break;
1672 case WKBD_GROUPPREV:
1673 StartWindozeCycle(wwin, event, False, True);
1674 break;
1676 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1677 widx = command - WKBD_WORKSPACE1;
1678 i = (scr->current_workspace / 10) * 10 + widx;
1679 if (wPreferences.ws_advance || i < scr->workspace_count)
1680 wWorkspaceChange(scr, i);
1681 break;
1683 case WKBD_NEXTWORKSPACE:
1684 wWorkspaceRelativeChange(scr, 1);
1685 break;
1686 case WKBD_PREVWORKSPACE:
1687 wWorkspaceRelativeChange(scr, -1);
1688 break;
1689 case WKBD_LASTWORKSPACE:
1690 wWorkspaceChange(scr, scr->last_workspace);
1691 break;
1693 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1694 widx = command - WKBD_MOVE_WORKSPACE1;
1695 i = (scr->current_workspace / 10) * 10 + widx;
1696 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1697 wWindowChangeWorkspace(wwin, i);
1698 break;
1700 case WKBD_MOVE_NEXTWORKSPACE:
1701 if (wwin)
1702 wWindowChangeWorkspaceRelative(wwin, 1);
1703 break;
1704 case WKBD_MOVE_PREVWORKSPACE:
1705 if (wwin)
1706 wWindowChangeWorkspaceRelative(wwin, -1);
1707 break;
1708 case WKBD_MOVE_LASTWORKSPACE:
1709 if (wwin)
1710 wWindowChangeWorkspace(wwin, scr->last_workspace);
1711 break;
1713 case WKBD_MOVE_NEXTWSLAYER:
1714 case WKBD_MOVE_PREVWSLAYER:
1716 if (wwin) {
1717 int row, column;
1719 row = scr->current_workspace / 10;
1720 column = scr->current_workspace % 10;
1722 if (command == WKBD_MOVE_NEXTWSLAYER) {
1723 if ((row + 1) * 10 < scr->workspace_count)
1724 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1725 } else {
1726 if (row > 0)
1727 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1731 break;
1733 case WKBD_WINDOW1:
1734 case WKBD_WINDOW2:
1735 case WKBD_WINDOW3:
1736 case WKBD_WINDOW4:
1737 case WKBD_WINDOW5:
1738 case WKBD_WINDOW6:
1739 case WKBD_WINDOW7:
1740 case WKBD_WINDOW8:
1741 case WKBD_WINDOW9:
1742 case WKBD_WINDOW10:
1744 widx = command - WKBD_WINDOW1;
1746 if (scr->shortcutWindows[widx]) {
1747 WMArray *list = scr->shortcutWindows[widx];
1748 int cw;
1749 int count = WMGetArrayItemCount(list);
1750 WWindow *twin;
1751 WMArrayIterator iter;
1752 WWindow *wwin;
1754 wUnselectWindows(scr);
1755 cw = scr->current_workspace;
1757 WM_ETARETI_ARRAY(list, wwin, iter) {
1758 if (count > 1)
1759 wWindowChangeWorkspace(wwin, cw);
1761 wMakeWindowVisible(wwin);
1763 if (count > 1)
1764 wSelectWindow(wwin, True);
1767 /* rotate the order of windows, to create a cycling effect */
1768 twin = WMGetFromArray(list, 0);
1769 WMDeleteFromArray(list, 0);
1770 WMAddToArray(list, twin);
1772 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1773 if (scr->shortcutWindows[widx]) {
1774 WMFreeArray(scr->shortcutWindows[widx]);
1775 scr->shortcutWindows[widx] = NULL;
1778 if (wwin->flags.selected && scr->selected_windows) {
1779 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1780 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1781 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1782 } else {
1783 scr->shortcutWindows[widx] = WMCreateArray(4);
1784 WMAddToArray(scr->shortcutWindows[widx], wwin);
1787 wSelectWindow(wwin, !wwin->flags.selected);
1788 XFlush(dpy);
1789 wusleep(3000);
1790 wSelectWindow(wwin, !wwin->flags.selected);
1791 XFlush(dpy);
1793 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1795 if (wwin->flags.selected && scr->selected_windows) {
1796 if (scr->shortcutWindows[widx]) {
1797 WMFreeArray(scr->shortcutWindows[widx]);
1799 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1803 break;
1805 case WKBD_RELAUNCH:
1806 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1807 (void) RelaunchWindow(wwin);
1809 break;
1811 case WKBD_SWITCH_SCREEN:
1812 if (w_global.screen_count > 1) {
1813 WScreen *scr2;
1814 int i;
1816 /* find index of this screen */
1817 for (i = 0; i < w_global.screen_count; i++) {
1818 if (wScreenWithNumber(i) == scr)
1819 break;
1821 i++;
1822 if (i >= w_global.screen_count) {
1823 i = 0;
1825 scr2 = wScreenWithNumber(i);
1827 if (scr2) {
1828 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1829 scr2->scr_width / 2, scr2->scr_height / 2);
1832 break;
1834 case WKBD_RUN:
1836 char *cmdline;
1838 cmdline = ExpandOptions(scr, _("exec %A(Run,Type command to run:)"));
1840 if (cmdline) {
1841 XGrabPointer(dpy, scr->root_win, True, 0,
1842 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1843 XSync(dpy, False);
1845 ExecuteShellCommand(scr, cmdline);
1846 wfree(cmdline);
1848 XUngrabPointer(dpy, CurrentTime);
1849 XSync(dpy, False);
1851 break;
1854 case WKBD_NEXTWSLAYER:
1855 case WKBD_PREVWSLAYER:
1857 int row, column;
1859 row = scr->current_workspace / 10;
1860 column = scr->current_workspace % 10;
1862 if (command == WKBD_NEXTWSLAYER) {
1863 if ((row + 1) * 10 < scr->workspace_count)
1864 wWorkspaceChange(scr, column + (row + 1) * 10);
1865 } else {
1866 if (row > 0)
1867 wWorkspaceChange(scr, column + (row - 1) * 10);
1870 break;
1871 case WKBD_CLIPRAISELOWER:
1872 if (!wPreferences.flags.noclip)
1873 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1874 break;
1875 case WKBD_DOCKRAISELOWER:
1876 if (!wPreferences.flags.nodock)
1877 wDockRaiseLower(scr->dock);
1878 break;
1879 #ifdef KEEP_XKB_LOCK_STATUS
1880 case WKBD_TOGGLE:
1881 if (wPreferences.modelock) {
1882 /*toggle */
1883 wwin = scr->focused_window;
1885 if (wwin && wwin->flags.mapped
1886 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1887 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1888 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1890 wwin->frame->languagemode = wwin->frame->last_languagemode;
1891 wwin->frame->last_languagemode = staterec.group;
1892 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1896 break;
1897 #endif /* KEEP_XKB_LOCK_STATUS */
1901 static void handleMotionNotify(XEvent * event)
1903 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1905 if (wPreferences.scrollable_menus) {
1906 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1907 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1909 if (scr->flags.jump_back_pending ||
1910 p.x <= (rect.pos.x + 1) ||
1911 p.x >= (rect.pos.x + rect.size.width - 2) ||
1912 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1913 WMenu *menu;
1915 menu = wMenuUnderPointer(scr);
1916 if (menu != NULL)
1917 wMenuScroll(menu);
1922 static void handleVisibilityNotify(XEvent * event)
1924 WWindow *wwin;
1926 wwin = wWindowFor(event->xvisibility.window);
1927 if (!wwin)
1928 return;
1929 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
1932 static void handle_selection_request(XSelectionRequestEvent *event)
1934 #ifdef USE_ICCCM_WMREPLACE
1935 static Atom atom_version = None;
1936 WScreen *scr;
1937 XSelectionEvent notify;
1940 * This event must be sent to the slection requester to not block him
1942 * We create it with the answer 'there is no selection' by default
1944 notify.type = SelectionNotify;
1945 notify.display = dpy;
1946 notify.requestor = event->requestor;
1947 notify.selection = event->selection;
1948 notify.target = event->target;
1949 notify.property = None; /* This says that there is no selection */
1950 notify.time = event->time;
1952 scr = wScreenForWindow(event->owner);
1953 if (!scr)
1954 goto not_our_selection;
1956 if (event->owner != scr->info_window)
1957 goto not_our_selection;
1959 if (event->selection != scr->sn_atom)
1960 goto not_our_selection;
1962 if (atom_version == None)
1963 atom_version = XInternAtom(dpy, "VERSION", False);
1965 if (event->target == atom_version) {
1966 static const long icccm_version[] = { 2, 0 };
1969 * This protocol is defined in ICCCM 2.0:
1970 * http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html
1971 * "Communication with the Window Manager by Means of Selections"
1975 * Setting the property means the content of the selection is available
1976 * According to the ICCCM spec, we need to support being asked for a property
1977 * set to 'None' for compatibility with old clients
1979 notify.property = (event->property == None)?(event->target):(event->property);
1981 XChangeProperty(dpy, event->requestor, notify.property,
1982 XA_INTEGER, 32, PropModeReplace,
1983 (unsigned char *) icccm_version, wlengthof(icccm_version));
1986 not_our_selection:
1987 if (notify.property == None)
1988 wwarning("received SelectionRequest(%s) for target=\"%s\" from requestor 0x%lX but we have no answer",
1989 XGetAtomName(dpy, event->selection), XGetAtomName(dpy, event->target), (long) event->requestor);
1991 /* Send the answer to the requestor */
1992 XSendEvent(dpy, event->requestor, False, 0L, (XEvent *) &notify);
1994 #else
1996 * If the support for ICCCM window manager replacement was not enabled, we should not receive
1997 * this kind of event, so we just ignore it (Conceptually, we should reply with 'SelectionNotify'
1998 * event with property set to 'None' to tell that we don't have this selection, but that is a bit
1999 * costly for an event that shall never happen).
2001 (void) event;
2002 #endif
2005 static void handle_selection_clear(XSelectionClearEvent *event)
2007 #ifdef USE_ICCCM_WMREPLACE
2008 WScreen *scr = wScreenForWindow(event->window);
2010 if (!scr)
2011 return;
2013 if (event->selection != scr->sn_atom)
2014 return;
2016 wmessage(_("another window manager is replacing us!"));
2017 Shutdown(WSExitMode);
2018 #else
2020 * If the support for ICCCM window manager replacement was not enabled, we should not receive
2021 * this kind of event, so we simply do nothing.
2023 (void) event;
2024 #endif