Update Serbian translation from master branch
[wmaker-crm.git] / src / event.c
blob67d300988da0c98de2fc4c3c5d8ab00e9d42b5ce
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 2014-2023 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 if (wPreferences.close_rootmenu_left_right_click){
763 WMenu *menu = NULL;
764 WMPropList *definition;
765 menu = scr->root_menu;
766 if (scr->root_menu){
767 wMenuDestroy(menu,True);
768 scr->root_menu = NULL;
769 definition = w_global.domain.root_menu->dictionary;
770 menu = configureMenu(scr, definition);
771 scr->root_menu = menu;
774 break;
775 case WA_OPEN_APPMENU:
776 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
777 /* ugly hack */
778 if (scr->root_menu) {
779 if (scr->root_menu->brother->flags.mapped)
780 event->xbutton.window = scr->root_menu->brother->frame->core->window;
781 else
782 event->xbutton.window = scr->root_menu->frame->core->window;
784 break;
785 case WA_OPEN_WINLISTMENU:
786 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
787 if (scr->switch_menu) {
788 if (scr->switch_menu->brother->flags.mapped)
789 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
790 else
791 event->xbutton.window = scr->switch_menu->frame->core->window;
793 break;
794 case WA_MOVE_PREVWORKSPACE:
795 wWorkspaceRelativeChange(scr, -1);
796 break;
797 case WA_MOVE_NEXTWORKSPACE:
798 wWorkspaceRelativeChange(scr, 1);
799 break;
800 case WA_MOVE_PREVWINDOW:
801 wwin = scr->focused_window;
802 wWindowFocusPrev(wwin, True);
803 break;
804 case WA_MOVE_NEXTWINDOW:
805 wwin = scr->focused_window;
806 wWindowFocusNext(wwin, True);
807 break;
811 /* bindable */
812 static void handleButtonPress(XEvent * event)
814 WObjDescriptor *desc;
815 WScreen *scr;
817 scr = wScreenForRootWindow(event->xbutton.root);
819 #ifdef BALLOON_TEXT
820 wBalloonHide(scr);
821 #endif
823 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
824 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
825 executeButtonAction(scr, event, wPreferences.mouse_button1);
826 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
827 executeButtonAction(scr, event, wPreferences.mouse_button2);
828 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
829 executeButtonAction(scr, event, wPreferences.mouse_button3);
830 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
831 executeButtonAction(scr, event, wPreferences.mouse_button8);
832 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
833 executeButtonAction(scr, event, wPreferences.mouse_button9);
834 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
835 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
836 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
837 executeWheelAction(scr, event, wPreferences.mouse_wheel_scroll);
838 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
839 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
840 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
841 executeWheelAction(scr, event, wPreferences.mouse_wheel_tilt);
845 desc = NULL;
846 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
847 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
848 return;
852 if (desc->parent_type == WCLASS_WINDOW) {
853 XSync(dpy, 0);
855 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
856 XAllowEvents(dpy, AsyncPointer, CurrentTime);
857 } else {
858 /* if (wPreferences.focus_mode == WKF_CLICK) { */
859 if (wPreferences.ignore_focus_click) {
860 XAllowEvents(dpy, AsyncPointer, CurrentTime);
862 XAllowEvents(dpy, ReplayPointer, CurrentTime);
863 /* } */
865 XSync(dpy, 0);
866 } else if (desc->parent_type == WCLASS_APPICON
867 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
868 if (event->xbutton.state & MOD_MASK) {
869 XSync(dpy, 0);
870 XAllowEvents(dpy, AsyncPointer, CurrentTime);
871 XSync(dpy, 0);
875 if (desc->handle_mousedown != NULL) {
876 (*desc->handle_mousedown) (desc, event);
879 /* save double-click information */
880 if (scr->flags.next_click_is_not_double) {
881 scr->flags.next_click_is_not_double = 0;
882 } else {
883 scr->last_click_time = event->xbutton.time;
884 scr->last_click_button = event->xbutton.button;
885 scr->last_click_window = event->xbutton.window;
889 static void handleMapNotify(XEvent * event)
891 WWindow *wwin;
893 wwin = wWindowFor(event->xmap.event);
894 if (wwin && wwin->client_win == event->xmap.event) {
895 if (wwin->flags.miniaturized) {
896 wDeiconifyWindow(wwin);
897 } else {
898 XGrabServer(dpy);
899 wWindowMap(wwin);
900 wClientSetState(wwin, NormalState, None);
901 XUngrabServer(dpy);
906 static void handleUnmapNotify(XEvent * event)
908 WWindow *wwin;
909 XEvent ev;
910 Bool withdraw = False;
912 /* only process windows with StructureNotify selected
913 * (ignore SubstructureNotify) */
914 wwin = wWindowFor(event->xunmap.window);
915 if (!wwin)
916 return;
918 /* whether the event is a Withdrawal request */
919 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
920 withdraw = True;
922 if (wwin->client_win != event->xunmap.event && !withdraw)
923 return;
925 if (!wwin->flags.mapped && !withdraw
926 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
927 && !wwin->flags.miniaturized && !wwin->flags.hidden)
928 return;
930 XGrabServer(dpy);
931 XUnmapWindow(dpy, wwin->frame->core->window);
932 wwin->flags.mapped = 0;
933 XSync(dpy, 0);
934 /* check if the window was destroyed */
935 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
936 DispatchEvent(&ev);
937 } else {
938 Bool reparented = False;
940 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
941 reparented = True;
943 /* withdraw window */
944 wwin->flags.mapped = 0;
945 if (!reparented)
946 wClientSetState(wwin, WithdrawnState, None);
948 /* if the window was reparented, do not reparent it back to the
949 * root window */
950 wUnmanageWindow(wwin, !reparented, False);
952 XUngrabServer(dpy);
955 static void handleConfigureRequest(XEvent * event)
957 WWindow *wwin;
959 wwin = wWindowFor(event->xconfigurerequest.window);
960 if (wwin == NULL) {
962 * Configure request for unmapped window
964 wClientConfigure(NULL, &(event->xconfigurerequest));
965 } else {
966 wClientConfigure(wwin, &(event->xconfigurerequest));
970 static void handlePropertyNotify(XEvent * event)
972 WWindow *wwin;
973 WApplication *wapp;
974 Window jr;
975 int ji;
976 unsigned int ju;
978 wwin = wWindowFor(event->xproperty.window);
979 if (wwin) {
980 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
981 return;
983 wClientCheckProperty(wwin, &event->xproperty);
985 wapp = wApplicationOf(event->xproperty.window);
986 if (wapp) {
987 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
991 static void handleClientMessage(XEvent * event)
993 WWindow *wwin;
994 WObjDescriptor *desc;
996 /* handle transition from Normal to Iconic state */
997 if (event->xclient.message_type == w_global.atom.wm.change_state
998 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
1000 wwin = wWindowFor(event->xclient.window);
1001 if (!wwin)
1002 return;
1003 if (!wwin->flags.miniaturized)
1004 wIconifyWindow(wwin);
1005 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
1006 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1008 if (!scr)
1009 return;
1011 if (event->xclient.data.l[1] == 1) { /* starting */
1012 wColormapAllowClientInstallation(scr, True);
1013 } else { /* stopping */
1014 wColormapAllowClientInstallation(scr, False);
1016 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
1018 char *command;
1019 size_t len;
1021 len = sizeof(event->xclient.data.b);
1022 command = wmalloc(len + 1);
1023 strncpy(command, event->xclient.data.b, len);
1025 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
1026 wwarning(_("Got Reconfigure command"));
1027 wDefaultsCheckDomains(NULL);
1028 } else {
1029 wwarning(_("Got unknown command %s"), command);
1032 wfree(command);
1034 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
1035 WApplication *wapp;
1036 int done = 0;
1037 wapp = wApplicationOf(event->xclient.window);
1038 if (wapp) {
1039 switch (event->xclient.data.l[0]) {
1040 case WMFHideOtherApplications:
1041 wHideOtherApplications(wapp->main_window_desc);
1042 done = 1;
1043 break;
1045 case WMFHideApplication:
1046 wHideApplication(wapp);
1047 done = 1;
1048 break;
1051 if (!done) {
1052 wwin = wWindowFor(event->xclient.window);
1053 if (wwin) {
1054 switch (event->xclient.data.l[0]) {
1055 case WMFHideOtherApplications:
1056 wHideOtherApplications(wwin);
1057 break;
1059 case WMFHideApplication:
1060 wHideApplication(wApplicationOf(wwin->main_window));
1061 break;
1065 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
1066 wwin = wWindowFor(event->xclient.window);
1067 if (!wwin)
1068 return;
1069 switch (event->xclient.data.l[0]) {
1070 case GSWindowLevelAttr:
1072 int level = (int)event->xclient.data.l[1];
1074 if (WINDOW_LEVEL(wwin) != level) {
1075 ChangeStackingLevel(wwin->frame->core, level);
1078 break;
1080 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1081 wwin = wWindowFor(event->xclient.window);
1082 if (!wwin)
1083 return;
1084 switch (event->xclient.data.l[0]) {
1085 case WMTitleBarNormal:
1086 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1087 break;
1088 case WMTitleBarMain:
1089 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1090 break;
1091 case WMTitleBarKey:
1092 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1093 break;
1095 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1096 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1097 if (!scr)
1098 return;
1099 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1100 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1101 /* do nothing */
1102 #ifdef USE_DOCK_XDND
1103 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1104 /* do nothing */
1105 #endif /* USE_DOCK_XDND */
1106 } else {
1108 * Non-standard thing, but needed by OffiX DND.
1109 * For when the icon frame gets a ClientMessage
1110 * that should have gone to the icon_window.
1112 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1113 struct WIcon *icon = NULL;
1115 if (desc->parent_type == WCLASS_MINIWINDOW) {
1116 icon = (WIcon *) desc->parent;
1117 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1118 icon = ((WAppIcon *) desc->parent)->icon;
1120 if (icon && (wwin = icon->owner)) {
1121 if (wwin->client_win != event->xclient.window) {
1122 event->xclient.window = wwin->client_win;
1123 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1130 static void raiseWindow(WScreen * scr)
1132 WWindow *wwin;
1134 scr->autoRaiseTimer = NULL;
1136 wwin = wWindowFor(scr->autoRaiseWindow);
1137 if (!wwin)
1138 return;
1140 if (!wwin->flags.destroyed && wwin->flags.focused) {
1141 wRaiseFrame(wwin->frame->core);
1142 /* this is needed or a race condition will occur */
1143 XSync(dpy, False);
1147 static void handleEnterNotify(XEvent * event)
1149 WWindow *wwin;
1150 WObjDescriptor *desc = NULL;
1151 XEvent ev;
1152 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1154 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1155 /* already left the window... */
1156 saveTimestamp(&ev);
1157 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1158 return;
1162 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1163 if (desc->handle_enternotify)
1164 (*desc->handle_enternotify) (desc, event);
1167 /* enter to window */
1168 wwin = wWindowFor(event->xcrossing.window);
1169 if (!wwin) {
1170 if (wPreferences.colormap_mode == WCM_POINTER) {
1171 wColormapInstallForWindow(scr, NULL);
1173 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1174 WMDeleteTimerHandler(scr->autoRaiseTimer);
1175 scr->autoRaiseTimer = NULL;
1177 } else {
1178 /* set auto raise timer even if in focus-follows-mouse mode
1179 * and the event is for the frame window, even if the window
1180 * has focus already. useful if you move the pointer from a focused
1181 * window to the root window and back pretty fast
1183 * set focus if in focus-follows-mouse mode and the event
1184 * is for the frame window and window doesn't have focus yet */
1185 if (wPreferences.focus_mode == WKF_SLOPPY
1186 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1188 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1189 wSetFocusTo(scr, wwin);
1191 if (scr->autoRaiseTimer)
1192 WMDeleteTimerHandler(scr->autoRaiseTimer);
1193 scr->autoRaiseTimer = NULL;
1195 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1196 scr->autoRaiseWindow = wwin->frame->core->window;
1197 scr->autoRaiseTimer
1198 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1201 /* Install colormap for window, if the colormap installation mode
1202 * is colormap_follows_mouse */
1203 if (wPreferences.colormap_mode == WCM_POINTER) {
1204 if (wwin->client_win == event->xcrossing.window)
1205 wColormapInstallForWindow(scr, wwin);
1206 else
1207 wColormapInstallForWindow(scr, NULL);
1211 if (event->xcrossing.window == event->xcrossing.root
1212 && event->xcrossing.detail == NotifyNormal
1213 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1215 wSetFocusTo(scr, scr->focused_window);
1217 #ifdef BALLOON_TEXT
1218 wBalloonEnteredObject(scr, desc);
1219 #endif
1222 static void handleLeaveNotify(XEvent * event)
1224 WObjDescriptor *desc = NULL;
1226 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1227 if (desc->handle_leavenotify)
1228 (*desc->handle_leavenotify) (desc, event);
1232 #ifdef USE_XSHAPE
1233 static void handleShapeNotify(XEvent * event)
1235 XShapeEvent *shev = (XShapeEvent *) event;
1236 WWindow *wwin;
1237 union {
1238 XEvent xevent;
1239 XShapeEvent xshape;
1240 } ev;
1242 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1243 if (ev.xshape.kind == ShapeBounding) {
1244 if (ev.xshape.shaped == shev->shaped) {
1245 *shev = ev.xshape;
1246 } else {
1247 XPutBackEvent(dpy, &ev.xevent);
1248 break;
1253 wwin = wWindowFor(shev->window);
1254 if (!wwin || shev->kind != ShapeBounding)
1255 return;
1257 if (!shev->shaped && wwin->flags.shaped) {
1259 wwin->flags.shaped = 0;
1260 wWindowClearShape(wwin);
1262 } else if (shev->shaped) {
1264 wwin->flags.shaped = 1;
1265 wWindowSetShape(wwin);
1268 #endif /* USE_XSHAPE */
1270 #ifdef KEEP_XKB_LOCK_STATUS
1271 /* please help ]d if you know what to do */
1272 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1274 WWindow *wwin;
1275 WScreen *scr;
1276 XkbStateRec staterec;
1277 int i;
1279 for (i = 0; i < w_global.screen_count; i++) {
1280 scr = wScreenWithNumber(i);
1281 wwin = scr->focused_window;
1282 if (wwin && wwin->flags.focused) {
1283 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1284 if (wwin->frame->languagemode != staterec.group) {
1285 wwin->frame->last_languagemode = wwin->frame->languagemode;
1286 wwin->frame->languagemode = staterec.group;
1288 #ifdef XKB_BUTTON_HINT
1289 if (wwin->frame->titlebar) {
1290 wFrameWindowPaint(wwin->frame);
1292 #endif
1296 #endif /*KEEP_XKB_LOCK_STATUS */
1298 static void handleColormapNotify(XEvent * event)
1300 WWindow *wwin;
1301 WScreen *scr;
1302 Bool reinstall = False;
1304 wwin = wWindowFor(event->xcolormap.window);
1305 if (!wwin)
1306 return;
1308 scr = wwin->screen_ptr;
1310 do {
1311 if (wwin) {
1312 if (event->xcolormap.new) {
1313 XWindowAttributes attr;
1315 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1317 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1318 scr->current_colormap = attr.colormap;
1320 reinstall = True;
1321 } else if (event->xcolormap.state == ColormapUninstalled &&
1322 scr->current_colormap == event->xcolormap.colormap) {
1324 /* some bastard app (like XV) removed our colormap */
1326 * can't enforce or things like xscreensaver won't work
1327 * reinstall = True;
1329 } else if (event->xcolormap.state == ColormapInstalled &&
1330 scr->current_colormap == event->xcolormap.colormap) {
1332 /* someone has put our colormap back */
1333 reinstall = False;
1336 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1337 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1339 if (reinstall && scr->current_colormap != None) {
1340 if (!scr->flags.colormap_stuff_blocked)
1341 XInstallColormap(dpy, scr->current_colormap);
1345 static void handleFocusIn(XEvent * event)
1347 WWindow *wwin;
1350 * For applications that like stealing the focus.
1352 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1353 saveTimestamp(event);
1354 if (event->xfocus.mode == NotifyUngrab
1355 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1356 return;
1359 wwin = wWindowFor(event->xfocus.window);
1360 if (wwin && !wwin->flags.focused) {
1361 if (wwin->flags.mapped)
1362 wSetFocusTo(wwin->screen_ptr, wwin);
1363 else
1364 wSetFocusTo(wwin->screen_ptr, NULL);
1365 } else if (!wwin) {
1366 WScreen *scr = wScreenForWindow(event->xfocus.window);
1367 if (scr)
1368 wSetFocusTo(scr, NULL);
1372 static WWindow *windowUnderPointer(WScreen * scr)
1374 unsigned int mask;
1375 int foo;
1376 Window bar, win;
1378 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1379 return wWindowFor(win);
1380 return NULL;
1383 static int CheckFullScreenWindowFocused(WScreen * scr)
1385 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1386 return 1;
1387 else
1388 return 0;
1391 static void handleKeyPress(XEvent * event)
1393 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1394 WWindow *wwin = scr->focused_window;
1395 short i, widx;
1396 int modifiers;
1397 int command = -1;
1398 #ifdef KEEP_XKB_LOCK_STATUS
1399 XkbStateRec staterec;
1400 #endif /*KEEP_XKB_LOCK_STATUS */
1402 /* ignore CapsLock */
1403 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1405 for (i = 0; i < WKBD_LAST; i++) {
1406 if (wKeyBindings[i].keycode == 0)
1407 continue;
1409 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1410 || */ wKeyBindings[i].modifier ==
1411 modifiers)) {
1412 command = i;
1413 break;
1417 if (command < 0) {
1419 if (!wRootMenuPerformShortcut(event)) {
1420 static int dontLoop = 0;
1422 if (dontLoop > 10) {
1423 wwarning("problem with key event processing code");
1424 return;
1426 dontLoop++;
1427 /* if the focused window is an internal window, try redispatching
1428 * the event to the managed window, as it can be a WINGs window */
1429 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1430 /* client_leader contains the WINGs toplevel */
1431 event->xany.window = wwin->client_leader;
1432 WMHandleEvent(event);
1434 dontLoop--;
1436 return;
1438 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1439 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1441 switch (command) {
1443 case WKBD_ROOTMENU:
1444 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1445 if (!CheckFullScreenWindowFocused(scr)) {
1446 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1447 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1448 True);
1450 break;
1451 case WKBD_WINDOWLIST:
1452 if (!CheckFullScreenWindowFocused(scr)) {
1453 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1454 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1455 True);
1457 break;
1459 case WKBD_WINDOWMENU:
1460 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1461 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1462 break;
1463 case WKBD_MINIMIZEALL:
1464 CloseWindowMenu(scr);
1465 wHideAll(scr);
1466 break;
1467 case WKBD_MINIATURIZE:
1468 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1469 && !WFLAGP(wwin, no_miniaturizable)) {
1470 CloseWindowMenu(scr);
1472 if (wwin->protocols.MINIATURIZE_WINDOW)
1473 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1474 else {
1475 wIconifyWindow(wwin);
1478 break;
1479 case WKBD_HIDE:
1480 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1481 WApplication *wapp = wApplicationOf(wwin->main_window);
1482 CloseWindowMenu(scr);
1484 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1485 wHideApplication(wapp);
1488 break;
1489 case WKBD_HIDE_OTHERS:
1490 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1491 CloseWindowMenu(scr);
1493 wHideOtherApplications(wwin);
1495 break;
1496 case WKBD_MAXIMIZE:
1497 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1498 CloseWindowMenu(scr);
1500 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1502 break;
1503 case WKBD_VMAXIMIZE:
1504 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1505 CloseWindowMenu(scr);
1507 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1509 break;
1510 case WKBD_HMAXIMIZE:
1511 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1512 CloseWindowMenu(scr);
1514 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1515 movePointerToWindowCenter(wwin);
1517 break;
1518 case WKBD_CENTRAL:
1519 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1520 CloseWindowMenu(scr);
1522 handleMaximize(wwin, MAX_CENTRAL | MAX_KEYBOARD);
1523 movePointerToWindowCenter(wwin);
1525 break;
1526 case WKBD_LHMAXIMIZE:
1527 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1528 CloseWindowMenu(scr);
1530 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1531 movePointerToWindowCenter(wwin);
1533 break;
1534 case WKBD_RHMAXIMIZE:
1535 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1536 CloseWindowMenu(scr);
1538 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1539 movePointerToWindowCenter(wwin);
1541 break;
1542 case WKBD_THMAXIMIZE:
1543 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1544 CloseWindowMenu(scr);
1546 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1547 movePointerToWindowCenter(wwin);
1549 break;
1550 case WKBD_BHMAXIMIZE:
1551 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1552 CloseWindowMenu(scr);
1554 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1555 movePointerToWindowCenter(wwin);
1557 break;
1558 case WKBD_LTCMAXIMIZE:
1559 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1560 CloseWindowMenu(scr);
1562 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1563 movePointerToWindowCenter(wwin);
1565 break;
1566 case WKBD_RTCMAXIMIZE:
1567 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1568 CloseWindowMenu(scr);
1570 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1571 movePointerToWindowCenter(wwin);
1573 break;
1574 case WKBD_LBCMAXIMIZE:
1575 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1576 CloseWindowMenu(scr);
1578 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1579 movePointerToWindowCenter(wwin);
1581 break;
1582 case WKBD_RBCMAXIMIZE:
1583 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1584 CloseWindowMenu(scr);
1586 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1587 movePointerToWindowCenter(wwin);
1589 break;
1590 case WKBD_MAXIMUS:
1591 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1592 CloseWindowMenu(scr);
1594 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1596 break;
1597 case WKBD_KEEP_ON_TOP:
1598 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1599 CloseWindowMenu(scr);
1601 if (wwin->frame->core->stacking->window_level != WMFloatingLevel)
1602 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
1603 else
1604 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1606 break;
1608 case WKBD_KEEP_AT_BOTTOM:
1609 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1610 CloseWindowMenu(scr);
1612 if (wwin->frame->core->stacking->window_level != WMSunkenLevel)
1613 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
1614 else
1615 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
1617 break;
1618 case WKBD_OMNIPRESENT:
1619 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1620 CloseWindowMenu(scr);
1622 wWindowSetOmnipresent(wwin, !wwin->flags.omnipresent);
1624 break;
1625 case WKBD_RAISE:
1626 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1627 CloseWindowMenu(scr);
1629 wRaiseFrame(wwin->frame->core);
1631 break;
1632 case WKBD_LOWER:
1633 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1634 CloseWindowMenu(scr);
1636 wLowerFrame(wwin->frame->core);
1638 break;
1639 case WKBD_RAISELOWER:
1640 /* raise or lower the window under the pointer, not the
1641 * focused one
1643 wwin = windowUnderPointer(scr);
1644 if (wwin)
1645 wRaiseLowerFrame(wwin->frame->core);
1646 break;
1647 case WKBD_SHADE:
1648 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1649 if (wwin->flags.shaded)
1650 wUnshadeWindow(wwin);
1651 else
1652 wShadeWindow(wwin);
1654 break;
1655 case WKBD_MOVERESIZE:
1656 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1657 CloseWindowMenu(scr);
1659 wKeyboardMoveResizeWindow(wwin);
1661 break;
1662 case WKBD_CLOSE:
1663 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1664 CloseWindowMenu(scr);
1665 if (wwin->protocols.DELETE_WINDOW)
1666 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1668 break;
1669 case WKBD_SELECT:
1670 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1671 wSelectWindow(wwin, !wwin->flags.selected);
1673 break;
1675 case WKBD_WORKSPACEMAP:
1676 if (wPreferences.enable_workspace_pager)
1677 StartWorkspaceMap(scr);
1678 break;
1680 case WKBD_FOCUSNEXT:
1681 StartWindozeCycle(wwin, event, True, False);
1682 break;
1684 case WKBD_FOCUSPREV:
1685 StartWindozeCycle(wwin, event, False, False);
1686 break;
1688 case WKBD_GROUPNEXT:
1689 StartWindozeCycle(wwin, event, True, True);
1690 break;
1692 case WKBD_GROUPPREV:
1693 StartWindozeCycle(wwin, event, False, True);
1694 break;
1696 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1697 widx = command - WKBD_WORKSPACE1;
1698 i = (scr->current_workspace / 10) * 10 + widx;
1699 if (wPreferences.ws_advance || i < scr->workspace_count)
1700 wWorkspaceChange(scr, i);
1701 break;
1703 case WKBD_NEXTWORKSPACE:
1704 wWorkspaceRelativeChange(scr, 1);
1705 break;
1706 case WKBD_PREVWORKSPACE:
1707 wWorkspaceRelativeChange(scr, -1);
1708 break;
1709 case WKBD_LASTWORKSPACE:
1710 wWorkspaceChange(scr, scr->last_workspace);
1711 break;
1713 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1714 widx = command - WKBD_MOVE_WORKSPACE1;
1715 i = (scr->current_workspace / 10) * 10 + widx;
1716 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1717 wWindowChangeWorkspace(wwin, i);
1718 break;
1720 case WKBD_MOVE_NEXTWORKSPACE:
1721 if (wwin)
1722 wWindowChangeWorkspaceRelative(wwin, 1);
1723 break;
1724 case WKBD_MOVE_PREVWORKSPACE:
1725 if (wwin)
1726 wWindowChangeWorkspaceRelative(wwin, -1);
1727 break;
1728 case WKBD_MOVE_LASTWORKSPACE:
1729 if (wwin)
1730 wWindowChangeWorkspace(wwin, scr->last_workspace);
1731 break;
1733 case WKBD_MOVE_NEXTWSLAYER:
1734 case WKBD_MOVE_PREVWSLAYER:
1736 if (wwin) {
1737 int row, column;
1739 row = scr->current_workspace / 10;
1740 column = scr->current_workspace % 10;
1742 if (command == WKBD_MOVE_NEXTWSLAYER) {
1743 if ((row + 1) * 10 < scr->workspace_count)
1744 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1745 } else {
1746 if (row > 0)
1747 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1751 break;
1753 case WKBD_WINDOW1:
1754 case WKBD_WINDOW2:
1755 case WKBD_WINDOW3:
1756 case WKBD_WINDOW4:
1757 case WKBD_WINDOW5:
1758 case WKBD_WINDOW6:
1759 case WKBD_WINDOW7:
1760 case WKBD_WINDOW8:
1761 case WKBD_WINDOW9:
1762 case WKBD_WINDOW10:
1764 widx = command - WKBD_WINDOW1;
1766 if (scr->shortcutWindows[widx]) {
1767 WMArray *list = scr->shortcutWindows[widx];
1768 int cw;
1769 int count = WMGetArrayItemCount(list);
1770 WWindow *twin;
1771 WMArrayIterator iter;
1772 WWindow *wwin;
1774 wUnselectWindows(scr);
1775 cw = scr->current_workspace;
1777 WM_ETARETI_ARRAY(list, wwin, iter) {
1778 if (count > 1)
1779 wWindowChangeWorkspace(wwin, cw);
1781 wMakeWindowVisible(wwin);
1783 if (count > 1)
1784 wSelectWindow(wwin, True);
1787 /* rotate the order of windows, to create a cycling effect */
1788 twin = WMGetFromArray(list, 0);
1789 WMDeleteFromArray(list, 0);
1790 WMAddToArray(list, twin);
1792 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1793 if (scr->shortcutWindows[widx]) {
1794 WMFreeArray(scr->shortcutWindows[widx]);
1795 scr->shortcutWindows[widx] = NULL;
1798 if (wwin->flags.selected && scr->selected_windows) {
1799 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1800 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1801 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1802 } else {
1803 scr->shortcutWindows[widx] = WMCreateArray(4);
1804 WMAddToArray(scr->shortcutWindows[widx], wwin);
1807 wSelectWindow(wwin, !wwin->flags.selected);
1808 XFlush(dpy);
1809 wusleep(3000);
1810 wSelectWindow(wwin, !wwin->flags.selected);
1811 XFlush(dpy);
1813 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1815 if (wwin->flags.selected && scr->selected_windows) {
1816 if (scr->shortcutWindows[widx]) {
1817 WMFreeArray(scr->shortcutWindows[widx]);
1819 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1823 break;
1825 case WKBD_MOVE_12_TO_6_HEAD:
1826 case WKBD_MOVE_6_TO_12_HEAD:
1827 if (wwin)
1828 moveBetweenHeads(wwin, command - WKBD_MOVE_12_TO_6_HEAD);
1830 break;
1832 case WKBD_RELAUNCH:
1833 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1834 (void) RelaunchWindow(wwin);
1836 break;
1838 case WKBD_SWITCH_SCREEN:
1839 if (w_global.screen_count > 1) {
1840 WScreen *scr2;
1841 int i;
1843 /* find index of this screen */
1844 for (i = 0; i < w_global.screen_count; i++) {
1845 if (wScreenWithNumber(i) == scr)
1846 break;
1848 i++;
1849 if (i >= w_global.screen_count) {
1850 i = 0;
1852 scr2 = wScreenWithNumber(i);
1854 if (scr2) {
1855 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1856 scr2->scr_width / 2, scr2->scr_height / 2);
1859 break;
1861 case WKBD_RUN:
1863 ExecuteInputCommand(scr, _("exec %A(Run, Type command:)"));
1864 break;
1867 case WKBD_EXIT:
1869 /* quick mode is not allowed to prevent inadvertently call */
1870 ExecuteExitCommand(scr, 0);
1871 break;
1874 case WKBD_PRINTS:
1876 ScreenCapture(scr, PRINT_SCREEN);
1877 break;
1880 case WKBD_PRINTW:
1882 ScreenCapture(scr, PRINT_WINDOW);
1883 break;
1886 case WKBD_PRINTP:
1888 ScreenCapture(scr, PRINT_PARTIAL);
1889 break;
1892 case WKBD_NEXTWSLAYER:
1893 case WKBD_PREVWSLAYER:
1895 int row, column;
1897 row = scr->current_workspace / 10;
1898 column = scr->current_workspace % 10;
1900 if (command == WKBD_NEXTWSLAYER) {
1901 if ((row + 1) * 10 < scr->workspace_count)
1902 wWorkspaceChange(scr, column + (row + 1) * 10);
1903 } else {
1904 if (row > 0)
1905 wWorkspaceChange(scr, column + (row - 1) * 10);
1908 break;
1909 case WKBD_CLIPRAISELOWER:
1910 if (!wPreferences.flags.noclip)
1911 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1912 break;
1913 case WKBD_DOCKRAISELOWER:
1914 if (!wPreferences.flags.nodock)
1915 wDockRaiseLower(scr->dock);
1916 break;
1917 #ifdef KEEP_XKB_LOCK_STATUS
1918 case WKBD_TOGGLE:
1919 if (wPreferences.modelock) {
1920 /*toggle */
1921 wwin = scr->focused_window;
1923 if (wwin && wwin->flags.mapped
1924 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1925 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1926 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1928 wwin->frame->languagemode = wwin->frame->last_languagemode;
1929 wwin->frame->last_languagemode = staterec.group;
1930 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1934 break;
1935 #endif /* KEEP_XKB_LOCK_STATUS */
1939 #define CORNER_NONE 0
1940 #define CORNER_TOPLEFT 1
1941 #define CORNER_TOPRIGHT 2
1942 #define CORNER_BOTTOMLEFT 3
1943 #define CORNER_BOTTOMRIGHT 4
1945 static int get_corner(WMRect rect, WMPoint p)
1947 if (p.x <= (rect.pos.x + wPreferences.hot_corner_edge) && p.y <= (rect.pos.y + wPreferences.hot_corner_edge))
1948 return CORNER_TOPLEFT;
1949 if (p.x >= (rect.pos.x + rect.size.width - wPreferences.hot_corner_edge) && p.y <= (rect.pos.y + wPreferences.hot_corner_edge))
1950 return CORNER_TOPRIGHT;
1951 if (p.x <= (rect.pos.x + wPreferences.hot_corner_edge) && p.y >= (rect.pos.y + rect.size.height - wPreferences.hot_corner_edge))
1952 return CORNER_BOTTOMLEFT;
1953 if (p.x >= (rect.pos.x + rect.size.width - wPreferences.hot_corner_edge) && p.y >= (rect.pos.y + rect.size.height - wPreferences.hot_corner_edge))
1954 return CORNER_BOTTOMRIGHT;
1955 return CORNER_NONE;
1958 static void hotCornerDelay(void *data)
1960 WScreen *scr = (WScreen *) data;
1961 if (scr->flags.in_hot_corner && wPreferences.hot_corner_actions[scr->flags.in_hot_corner - 1])
1962 ExecuteShellCommand(scr, wPreferences.hot_corner_actions[scr->flags.in_hot_corner - 1]);
1963 WMDeleteTimerHandler(scr->hot_corner_timer);
1964 scr->hot_corner_timer = NULL;
1967 static void handleMotionNotify(XEvent *event)
1969 if (wPreferences.scrollable_menus || wPreferences.hot_corners) {
1970 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1971 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1972 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1974 if (wPreferences.hot_corners) {
1975 if (!scr->flags.in_hot_corner) {
1976 scr->flags.in_hot_corner = get_corner(rect, p);
1977 if (scr->flags.in_hot_corner && !scr->hot_corner_timer)
1978 scr->hot_corner_timer = WMAddTimerHandler(wPreferences.hot_corner_delay, hotCornerDelay, scr);
1979 } else {
1980 int out_hot_corner = 0;
1982 switch (scr->flags.in_hot_corner) {
1983 case CORNER_TOPLEFT:
1984 if ((p.x > (rect.pos.x + wPreferences.hot_corner_edge)) ||
1985 (p.y > (rect.pos.y + wPreferences.hot_corner_edge)))
1986 out_hot_corner = 1;
1987 break;
1988 case CORNER_TOPRIGHT:
1989 if ((p.x < (rect.pos.x + rect.size.width - wPreferences.hot_corner_edge)) ||
1990 (p.y > (rect.pos.y + wPreferences.hot_corner_edge)))
1991 out_hot_corner = 1;
1992 break;
1993 case CORNER_BOTTOMLEFT:
1994 if ((p.x > (rect.pos.x + wPreferences.hot_corner_edge)) ||
1995 (p.y < (rect.pos.y + rect.size.height - wPreferences.hot_corner_edge)))
1996 out_hot_corner = 1;
1997 break;
1998 case CORNER_BOTTOMRIGHT:
1999 if ((p.x < (rect.pos.x + rect.size.width - wPreferences.hot_corner_edge)) ||
2000 (p.y < (rect.pos.y + rect.size.height - wPreferences.hot_corner_edge)))
2001 out_hot_corner = 1;
2002 break;
2005 if (out_hot_corner) {
2006 scr->flags.in_hot_corner = CORNER_NONE;
2007 if (scr->hot_corner_timer) {
2008 WMDeleteTimerHandler(scr->hot_corner_timer);
2009 scr->hot_corner_timer = NULL;
2016 if (wPreferences.scrollable_menus) {
2017 if (scr->flags.jump_back_pending ||
2018 p.x <= (rect.pos.x + 1) ||
2019 p.x >= (rect.pos.x + rect.size.width - 2) ||
2020 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
2021 WMenu *menu;
2023 menu = wMenuUnderPointer(scr);
2024 if (menu != NULL)
2025 wMenuScroll(menu);
2031 #undef CORNER_NONE
2032 #undef CORNER_TOPLEFT
2033 #undef CORNER_TOPRIGHT
2034 #undef CORNER_BOTTOMLEFT
2035 #undef CORNER_BOTTOMRIGHT
2037 static void handleVisibilityNotify(XEvent * event)
2039 WWindow *wwin;
2041 wwin = wWindowFor(event->xvisibility.window);
2042 if (!wwin)
2043 return;
2044 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);
2047 static void handle_selection_request(XSelectionRequestEvent *event)
2049 #ifdef USE_ICCCM_WMREPLACE
2050 static Atom atom_version = None;
2051 WScreen *scr;
2052 XSelectionEvent notify;
2055 * This event must be sent to the slection requester to not block him
2057 * We create it with the answer 'there is no selection' by default
2059 notify.type = SelectionNotify;
2060 notify.display = dpy;
2061 notify.requestor = event->requestor;
2062 notify.selection = event->selection;
2063 notify.target = event->target;
2064 notify.property = None; /* This says that there is no selection */
2065 notify.time = event->time;
2067 scr = wScreenForWindow(event->owner);
2068 if (!scr)
2069 goto not_our_selection;
2071 if (event->owner != scr->info_window)
2072 goto not_our_selection;
2074 if (event->selection != scr->sn_atom)
2075 goto not_our_selection;
2077 if (atom_version == None)
2078 atom_version = XInternAtom(dpy, "VERSION", False);
2080 if (event->target == atom_version) {
2081 static const long icccm_version[] = { 2, 0 };
2084 * This protocol is defined in ICCCM 2.0:
2085 * http://www.x.org/releases/X11R7.7/doc/xorg-docs/icccm/icccm.html
2086 * "Communication with the Window Manager by Means of Selections"
2090 * Setting the property means the content of the selection is available
2091 * According to the ICCCM spec, we need to support being asked for a property
2092 * set to 'None' for compatibility with old clients
2094 notify.property = (event->property == None)?(event->target):(event->property);
2096 XChangeProperty(dpy, event->requestor, notify.property,
2097 XA_INTEGER, 32, PropModeReplace,
2098 (unsigned char *) icccm_version, wlengthof(icccm_version));
2101 not_our_selection:
2102 if (notify.property == None)
2103 wwarning("received SelectionRequest(%s) for target=\"%s\" from requestor 0x%lX but we have no answer",
2104 XGetAtomName(dpy, event->selection), XGetAtomName(dpy, event->target), (long) event->requestor);
2106 /* Send the answer to the requestor */
2107 XSendEvent(dpy, event->requestor, False, 0L, (XEvent *) &notify);
2109 #else
2111 * If the support for ICCCM window manager replacement was not enabled, we should not receive
2112 * this kind of event, so we just ignore it (Conceptually, we should reply with 'SelectionNotify'
2113 * event with property set to 'None' to tell that we don't have this selection, but that is a bit
2114 * costly for an event that shall never happen).
2116 (void) event;
2117 #endif
2120 static void handle_selection_clear(XSelectionClearEvent *event)
2122 #ifdef USE_ICCCM_WMREPLACE
2123 WScreen *scr = wScreenForWindow(event->window);
2125 if (!scr)
2126 return;
2128 if (event->selection != scr->sn_atom)
2129 return;
2131 wmessage(_("another window manager is replacing us!"));
2132 Shutdown(WSExitMode);
2133 #else
2135 * If the support for ICCCM window manager replacement was not enabled, we should not receive
2136 * this kind of event, so we simply do nothing.
2138 (void) event;
2139 #endif