Make two text strings translatable
[wmaker-crm.git] / src / event.c
blob0ec1b770b2edd64b9942690ba5c2d996e70cdf71
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 2014 Window Maker Team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #ifdef HAVE_INOTIFY
26 #include <sys/select.h>
27 #include <sys/inotify.h>
28 #endif
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <time.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #ifdef USE_XSHAPE
40 # include <X11/extensions/shape.h>
41 #endif
42 #ifdef XDND
43 #include "xdnd.h"
44 #endif
46 #ifdef USE_RANDR
47 #include <X11/extensions/Xrandr.h>
48 #endif
50 #ifdef KEEP_XKB_LOCK_STATUS
51 #include <X11/XKBlib.h>
52 #endif /* KEEP_XKB_LOCK_STATUS */
54 #include "WindowMaker.h"
55 #include "window.h"
56 #include "actions.h"
57 #include "client.h"
58 #include "main.h"
59 #include "cycling.h"
60 #include "keybind.h"
61 #include "application.h"
62 #include "stacking.h"
63 #include "defaults.h"
64 #include "workspace.h"
65 #include "dock.h"
66 #include "framewin.h"
67 #include "properties.h"
68 #include "balloon.h"
69 #include "xinerama.h"
70 #include "wmspec.h"
71 #include "rootmenu.h"
72 #include "colormap.h"
73 #include "screen.h"
74 #include "shutdown.h"
75 #include "misc.h"
76 #include "event.h"
77 #include "winmenu.h"
78 #include "switchmenu.h"
81 #define MOD_MASK wPreferences.modifier_mask
83 /************ Local stuff ***********/
85 static void saveTimestamp(XEvent *event);
86 static void handleColormapNotify(XEvent *event);
87 static void handleMapNotify(XEvent *event);
88 static void handleUnmapNotify(XEvent *event);
89 static void handleButtonPress(XEvent *event);
90 static void handleExpose(XEvent *event);
91 static void handleDestroyNotify(XEvent *event);
92 static void handleConfigureRequest(XEvent *event);
93 static void handleMapRequest(XEvent *event);
94 static void handlePropertyNotify(XEvent *event);
95 static void handleEnterNotify(XEvent *event);
96 static void handleLeaveNotify(XEvent *event);
97 static void handleExtensions(XEvent *event);
98 static void handleClientMessage(XEvent *event);
99 static void handleKeyPress(XEvent *event);
100 static void handleFocusIn(XEvent *event);
101 static void handleMotionNotify(XEvent *event);
102 static void handleVisibilityNotify(XEvent *event);
103 static void handle_inotify_events(void);
104 static void wdelete_death_handler(WMagicNumber id);
107 #ifdef USE_XSHAPE
108 static void handleShapeNotify(XEvent *event);
109 #endif
111 #ifdef KEEP_XKB_LOCK_STATUS
112 static void handleXkbIndicatorStateNotify(XkbEvent *event);
113 #endif
115 /* real dead process handler */
116 static void handleDeadProcess(void);
118 typedef struct DeadProcesses {
119 pid_t pid;
120 unsigned char exit_status;
121 } DeadProcesses;
123 /* stack of dead processes */
124 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
125 static int deadProcessPtr = 0;
127 typedef struct DeathHandler {
128 WDeathHandler *callback;
129 pid_t pid;
130 void *client_data;
131 } DeathHandler;
133 static WMArray *deathHandlers = NULL;
135 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
137 DeathHandler *handler;
139 handler = malloc(sizeof(DeathHandler));
140 if (!handler)
141 return 0;
143 handler->pid = pid;
144 handler->callback = callback;
145 handler->client_data = cdata;
147 if (!deathHandlers)
148 deathHandlers = WMCreateArrayWithDestructor(8, free);
150 WMAddToArray(deathHandlers, handler);
152 return handler;
155 static void wdelete_death_handler(WMagicNumber id)
157 DeathHandler *handler = (DeathHandler *) id;
159 if (!handler || !deathHandlers)
160 return;
162 /* array destructor will call free(handler) */
163 WMRemoveFromArray(deathHandlers, handler);
166 void DispatchEvent(XEvent * event)
168 if (deathHandlers)
169 handleDeadProcess();
171 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
172 WCHANGE_STATE(WSTATE_EXITING);
173 /* received SIGTERM */
175 * WMHandleEvent() can't be called from anything
176 * executed inside here, or we can get in a infinite
177 * recursive loop.
179 Shutdown(WSExitMode);
181 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
182 WCHANGE_STATE(WSTATE_RESTARTING);
184 Shutdown(WSRestartPreparationMode);
185 /* received SIGHUP */
186 Restart(NULL, True);
187 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
188 WCHANGE_STATE(WSTATE_NORMAL);
189 wDefaultsCheckDomains(NULL);
192 /* for the case that all that is wanted to be dispatched is
193 * the stuff above */
194 if (!event)
195 return;
197 saveTimestamp(event);
198 switch (event->type) {
199 case MapRequest:
200 handleMapRequest(event);
201 break;
203 case KeyPress:
204 handleKeyPress(event);
205 break;
207 case MotionNotify:
208 handleMotionNotify(event);
209 break;
211 case ConfigureRequest:
212 handleConfigureRequest(event);
213 break;
215 case DestroyNotify:
216 handleDestroyNotify(event);
217 break;
219 case MapNotify:
220 handleMapNotify(event);
221 break;
223 case UnmapNotify:
224 handleUnmapNotify(event);
225 break;
227 case ButtonPress:
228 handleButtonPress(event);
229 break;
231 case Expose:
232 handleExpose(event);
233 break;
235 case PropertyNotify:
236 handlePropertyNotify(event);
237 break;
239 case EnterNotify:
240 handleEnterNotify(event);
241 break;
243 case LeaveNotify:
244 handleLeaveNotify(event);
245 break;
247 case ClientMessage:
248 handleClientMessage(event);
249 break;
251 case ColormapNotify:
252 handleColormapNotify(event);
253 break;
255 case MappingNotify:
256 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
257 XRefreshKeyboardMapping(&event->xmapping);
258 break;
260 case FocusIn:
261 handleFocusIn(event);
262 break;
264 case VisibilityNotify:
265 handleVisibilityNotify(event);
266 break;
268 case ConfigureNotify:
269 #ifdef USE_RANDR
270 if (event->xconfigure.window == DefaultRootWindow(dpy))
271 XRRUpdateConfiguration(event);
272 #endif
273 break;
275 default:
276 handleExtensions(event);
277 break;
281 #ifdef HAVE_INOTIFY
283 *----------------------------------------------------------------------
284 * handle_inotify_events-
285 * Check for inotify events
287 * Returns:
288 * After reading events for the given file descriptor (fd) and
289 * watch descriptor (wd)
291 * Side effects:
292 * Calls wDefaultsCheckDomains if config database is updated
293 *----------------------------------------------------------------------
295 static void handle_inotify_events(void)
297 ssize_t eventQLength, i = 0;
298 /* Make room for at lease 5 simultaneous events, with path + filenames */
299 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
300 /* Check config only once per read of the event queue */
301 int oneShotFlag = 0;
304 * Read off the queued events
305 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
306 * not occur; the block is on Xevents, but a config file change will normally
307 * occur as a result of an Xevent - so the event queue should never have more than
308 * a few entries before a read().
310 eventQLength = read(w_global.inotify.fd_event_queue,
311 buff, sizeof(buff) );
313 /* check what events occured */
314 /* Should really check wd here too, but for now we only have one watch! */
315 while (i < eventQLength) {
316 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
319 * see inotify.h for event types.
321 if (pevent->mask & IN_DELETE_SELF) {
322 wwarning(_("the defaults database has been deleted!"
323 " Restart Window Maker to create the database" " with the default settings"));
325 if (w_global.inotify.fd_event_queue >= 0) {
326 close(w_global.inotify.fd_event_queue);
327 w_global.inotify.fd_event_queue = -1;
330 if (pevent->mask & IN_UNMOUNT) {
331 wwarning(_("the unit containing the defaults database has"
332 " been unmounted. Setting --static mode." " Any changes will not be saved."));
334 if (w_global.inotify.fd_event_queue >= 0) {
335 close(w_global.inotify.fd_event_queue);
336 w_global.inotify.fd_event_queue = -1;
339 wPreferences.flags.noupdates = 1;
341 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
342 wwarning(_("Inotify: Reading config files in defaults database."));
343 wDefaultsCheckDomains(NULL);
344 oneShotFlag = 1;
347 /* move to next event in the buffer */
348 i += sizeof(struct inotify_event) + pevent->len;
351 #endif /* HAVE_INOTIFY */
354 *----------------------------------------------------------------------
355 * EventLoop-
356 * Processes X and internal events indefinitely.
358 * Returns:
359 * Never returns
361 * Side effects:
362 * The LastTimestamp global variable is updated.
363 * Calls inotifyGetEvents if defaults database changes.
364 *----------------------------------------------------------------------
366 noreturn void EventLoop(void)
368 XEvent event;
369 #ifdef HAVE_INOTIFY
370 struct timeval time;
371 fd_set rfds;
372 int retVal = 0;
374 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
375 retVal = -1;
376 #endif
378 for (;;) {
380 WMNextEvent(dpy, &event); /* Blocks here */
381 WMHandleEvent(&event);
382 #ifdef HAVE_INOTIFY
383 if (retVal != -1) {
384 time.tv_sec = 0;
385 time.tv_usec = 0;
386 FD_ZERO(&rfds);
387 FD_SET(w_global.inotify.fd_event_queue, &rfds);
389 /* check for available read data from inotify - don't block! */
390 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
392 if (retVal < 0) { /* an error has occured */
393 wwarning(_("select failed. The inotify instance will be closed."
394 " Changes to the defaults database will require"
395 " a restart to take effect."));
396 close(w_global.inotify.fd_event_queue);
397 w_global.inotify.fd_event_queue = -1;
398 continue;
400 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
401 handle_inotify_events();
403 #endif
408 *----------------------------------------------------------------------
409 * ProcessPendingEvents --
410 * Processes the events that are currently pending (at the time
411 * this function is called) in the display's queue.
413 * Returns:
414 * After the pending events that were present at the function call
415 * are processed.
417 * Side effects:
418 * Many -- whatever handling events may involve.
420 *----------------------------------------------------------------------
422 void ProcessPendingEvents(void)
424 XEvent event;
425 int count;
427 XSync(dpy, False);
429 /* Take a snapshot of the event count in the queue */
430 count = XPending(dpy);
432 while (count > 0 && XPending(dpy)) {
433 WMNextEvent(dpy, &event);
434 WMHandleEvent(&event);
435 count--;
439 Bool IsDoubleClick(WScreen * scr, XEvent * event)
441 if ((scr->last_click_time > 0) &&
442 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
443 && (event->xbutton.button == scr->last_click_button)
444 && (event->xbutton.window == scr->last_click_window)) {
446 scr->flags.next_click_is_not_double = 1;
447 scr->last_click_time = 0;
448 scr->last_click_window = event->xbutton.window;
450 return True;
452 return False;
455 void NotifyDeadProcess(pid_t pid, unsigned char status)
457 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
458 wwarning("stack overflow: too many dead processes");
459 return;
461 /* stack the process to be handled later,
462 * as this is called from the signal handler */
463 deadProcesses[deadProcessPtr].pid = pid;
464 deadProcesses[deadProcessPtr].exit_status = status;
465 deadProcessPtr++;
468 static void handleDeadProcess(void)
470 DeathHandler *tmp;
471 int i;
473 for (i = 0; i < deadProcessPtr; i++) {
474 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
477 if (!deathHandlers) {
478 deadProcessPtr = 0;
479 return;
482 /* get the pids on the queue and call handlers */
483 while (deadProcessPtr > 0) {
484 deadProcessPtr--;
486 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
487 tmp = WMGetFromArray(deathHandlers, i);
488 if (!tmp)
489 continue;
491 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
492 (*tmp->callback) (tmp->pid,
493 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
494 wdelete_death_handler(tmp);
500 static void saveTimestamp(XEvent * event)
503 * Never save CurrentTime as LastTimestamp because CurrentTime
504 * it's not a real timestamp (it's the 0L constant)
507 switch (event->type) {
508 case ButtonRelease:
509 case ButtonPress:
510 w_global.timestamp.last_event = event->xbutton.time;
511 break;
512 case KeyPress:
513 case KeyRelease:
514 w_global.timestamp.last_event = event->xkey.time;
515 break;
516 case MotionNotify:
517 w_global.timestamp.last_event = event->xmotion.time;
518 break;
519 case PropertyNotify:
520 w_global.timestamp.last_event = event->xproperty.time;
521 break;
522 case EnterNotify:
523 case LeaveNotify:
524 w_global.timestamp.last_event = event->xcrossing.time;
525 break;
526 case SelectionClear:
527 w_global.timestamp.last_event = event->xselectionclear.time;
528 break;
529 case SelectionRequest:
530 w_global.timestamp.last_event = event->xselectionrequest.time;
531 break;
532 case SelectionNotify:
533 w_global.timestamp.last_event = event->xselection.time;
534 #ifdef XDND
535 wXDNDProcessSelection(event);
536 #endif
537 break;
541 static int matchWindow(const void *item, const void *cdata)
543 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
546 static void handleExtensions(XEvent * event)
548 #ifdef USE_XSHAPE
549 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
550 handleShapeNotify(event);
552 #endif
553 #ifdef KEEP_XKB_LOCK_STATUS
554 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
555 handleXkbIndicatorStateNotify((XkbEvent *) event);
557 #endif /*KEEP_XKB_LOCK_STATUS */
558 #ifdef USE_RANDR
559 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
560 /* From xrandr man page: "Clients must call back into Xlib using
561 * XRRUpdateConfiguration when screen configuration change notify
562 * events are generated */
563 XRRUpdateConfiguration(event);
564 WCHANGE_STATE(WSTATE_RESTARTING);
565 Shutdown(WSRestartPreparationMode);
566 Restart(NULL,True);
568 #endif
571 static void handleMapRequest(XEvent * ev)
573 WWindow *wwin;
574 WScreen *scr = NULL;
575 Window window = ev->xmaprequest.window;
577 if ((wwin = wWindowFor(window))) {
578 if (wwin->flags.shaded) {
579 wUnshadeWindow(wwin);
581 /* deiconify window */
582 if (wwin->flags.miniaturized) {
583 wDeiconifyWindow(wwin);
584 } else if (wwin->flags.hidden) {
585 WApplication *wapp = wApplicationOf(wwin->main_window);
586 /* go to the last workspace that the user worked on the app */
587 if (wapp) {
588 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
590 wUnhideApplication(wapp, False, False);
592 return;
595 scr = wScreenForRootWindow(ev->xmaprequest.parent);
597 wwin = wManageWindow(scr, window);
600 * This is to let the Dock know that the application it launched
601 * has already been mapped (eg: it has finished launching).
602 * It is not necessary for normally docked apps, but is needed for
603 * apps that were forcedly docked (like with dockit).
605 if (scr->last_dock) {
606 if (wwin && wwin->main_window != None && wwin->main_window != window)
607 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
608 else
609 wDockTrackWindowLaunch(scr->last_dock, window);
612 if (wwin) {
613 wClientSetState(wwin, NormalState, None);
614 if (wwin->flags.maximized) {
615 wMaximizeWindow(wwin, wwin->flags.maximized);
617 if (wwin->flags.shaded) {
618 wwin->flags.shaded = 0;
619 wwin->flags.skip_next_animation = 1;
620 wShadeWindow(wwin);
622 if (wwin->flags.miniaturized) {
623 wwin->flags.miniaturized = 0;
624 wwin->flags.skip_next_animation = 1;
625 wIconifyWindow(wwin);
627 if (wwin->flags.fullscreen) {
628 wwin->flags.fullscreen = 0;
629 wFullscreenWindow(wwin);
631 if (wwin->flags.hidden) {
632 WApplication *wapp = wApplicationOf(wwin->main_window);
634 wwin->flags.hidden = 0;
635 wwin->flags.skip_next_animation = 1;
636 if (wapp) {
637 wHideApplication(wapp);
643 static void handleDestroyNotify(XEvent * event)
645 WWindow *wwin;
646 WApplication *app;
647 Window window = event->xdestroywindow.window;
648 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
649 int widx;
651 wwin = wWindowFor(window);
652 if (wwin) {
653 wUnmanageWindow(wwin, False, True);
656 if (scr != NULL) {
657 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
658 WFakeGroupLeader *fPtr;
660 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
661 if (fPtr->retainCount > 0) {
662 fPtr->retainCount--;
663 if (fPtr->retainCount == 0 && fPtr->leader != None) {
664 XDestroyWindow(dpy, fPtr->leader);
665 fPtr->leader = None;
666 XFlush(dpy);
669 fPtr->origLeader = None;
673 app = wApplicationOf(window);
674 if (app) {
675 if (window == app->main_window) {
676 app->refcount = 0;
677 wwin = app->main_window_desc->screen_ptr->focused_window;
678 while (wwin) {
679 if (wwin->main_window == window) {
680 wwin->main_window = None;
682 wwin = wwin->prev;
685 wApplicationDestroy(app);
689 static void handleExpose(XEvent * event)
691 WObjDescriptor *desc;
692 XEvent ev;
694 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
696 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
697 return;
700 if (desc->handle_expose) {
701 (*desc->handle_expose) (desc, event);
705 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
707 switch (action) {
708 case WA_SELECT_WINDOWS:
709 wUnselectWindows(scr);
710 wSelectWindows(scr, event);
711 break;
712 case WA_OPEN_APPMENU:
713 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
714 /* ugly hack */
715 if (scr->root_menu) {
716 if (scr->root_menu->brother->flags.mapped)
717 event->xbutton.window = scr->root_menu->brother->frame->core->window;
718 else
719 event->xbutton.window = scr->root_menu->frame->core->window;
721 break;
722 case WA_OPEN_WINLISTMENU:
723 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
724 if (scr->switch_menu) {
725 if (scr->switch_menu->brother->flags.mapped)
726 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
727 else
728 event->xbutton.window = scr->switch_menu->frame->core->window;
730 break;
731 default:
732 break;
736 /* bindable */
737 static void handleButtonPress(XEvent * event)
739 WObjDescriptor *desc;
740 WScreen *scr;
742 scr = wScreenForRootWindow(event->xbutton.root);
744 #ifdef BALLOON_TEXT
745 wBalloonHide(scr);
746 #endif
748 if (!wPreferences.disable_root_mouse && event->xbutton.window == scr->root_win) {
749 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
750 executeButtonAction(scr, event, wPreferences.mouse_button1);
751 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
752 executeButtonAction(scr, event, wPreferences.mouse_button2);
753 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
754 executeButtonAction(scr, event, wPreferences.mouse_button3);
755 } else if (event->xbutton.button == Button8 && wPreferences.mouse_button8 != WA_NONE) {
756 executeButtonAction(scr, event, wPreferences.mouse_button8);
757 }else if (event->xbutton.button == Button9 && wPreferences.mouse_button9 != WA_NONE) {
758 executeButtonAction(scr, event, wPreferences.mouse_button9);
759 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel_scroll != WA_NONE) {
760 wWorkspaceRelativeChange(scr, 1);
761 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel_scroll != WA_NONE) {
762 wWorkspaceRelativeChange(scr, -1);
763 } else if (event->xbutton.button == Button6 && wPreferences.mouse_wheel_tilt != WA_NONE) {
764 wWorkspaceRelativeChange(scr, -1);
765 } else if (event->xbutton.button == Button7 && wPreferences.mouse_wheel_tilt != WA_NONE) {
766 wWorkspaceRelativeChange(scr, 1);
770 desc = NULL;
771 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
772 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
773 return;
777 if (desc->parent_type == WCLASS_WINDOW) {
778 XSync(dpy, 0);
780 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
781 XAllowEvents(dpy, AsyncPointer, CurrentTime);
782 } else {
783 /* if (wPreferences.focus_mode == WKF_CLICK) { */
784 if (wPreferences.ignore_focus_click) {
785 XAllowEvents(dpy, AsyncPointer, CurrentTime);
787 XAllowEvents(dpy, ReplayPointer, CurrentTime);
788 /* } */
790 XSync(dpy, 0);
791 } else if (desc->parent_type == WCLASS_APPICON
792 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
793 if (event->xbutton.state & MOD_MASK) {
794 XSync(dpy, 0);
795 XAllowEvents(dpy, AsyncPointer, CurrentTime);
796 XSync(dpy, 0);
800 if (desc->handle_mousedown != NULL) {
801 (*desc->handle_mousedown) (desc, event);
804 /* save double-click information */
805 if (scr->flags.next_click_is_not_double) {
806 scr->flags.next_click_is_not_double = 0;
807 } else {
808 scr->last_click_time = event->xbutton.time;
809 scr->last_click_button = event->xbutton.button;
810 scr->last_click_window = event->xbutton.window;
814 static void handleMapNotify(XEvent * event)
816 WWindow *wwin;
818 wwin = wWindowFor(event->xmap.event);
819 if (wwin && wwin->client_win == event->xmap.event) {
820 if (wwin->flags.miniaturized) {
821 wDeiconifyWindow(wwin);
822 } else {
823 XGrabServer(dpy);
824 wWindowMap(wwin);
825 wClientSetState(wwin, NormalState, None);
826 XUngrabServer(dpy);
831 static void handleUnmapNotify(XEvent * event)
833 WWindow *wwin;
834 XEvent ev;
835 Bool withdraw = False;
837 /* only process windows with StructureNotify selected
838 * (ignore SubstructureNotify) */
839 wwin = wWindowFor(event->xunmap.window);
840 if (!wwin)
841 return;
843 /* whether the event is a Withdrawal request */
844 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
845 withdraw = True;
847 if (wwin->client_win != event->xunmap.event && !withdraw)
848 return;
850 if (!wwin->flags.mapped && !withdraw
851 && wwin->frame->workspace == w_global.workspace.current
852 && !wwin->flags.miniaturized && !wwin->flags.hidden)
853 return;
855 XGrabServer(dpy);
856 XUnmapWindow(dpy, wwin->frame->core->window);
857 wwin->flags.mapped = 0;
858 XSync(dpy, 0);
859 /* check if the window was destroyed */
860 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
861 DispatchEvent(&ev);
862 } else {
863 Bool reparented = False;
865 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
866 reparented = True;
868 /* withdraw window */
869 wwin->flags.mapped = 0;
870 if (!reparented)
871 wClientSetState(wwin, WithdrawnState, None);
873 /* if the window was reparented, do not reparent it back to the
874 * root window */
875 wUnmanageWindow(wwin, !reparented, False);
877 XUngrabServer(dpy);
880 static void handleConfigureRequest(XEvent * event)
882 WWindow *wwin;
884 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
886 * Configure request for unmapped window
888 wClientConfigure(NULL, &(event->xconfigurerequest));
889 } else {
890 wClientConfigure(wwin, &(event->xconfigurerequest));
894 static void handlePropertyNotify(XEvent * event)
896 WWindow *wwin;
897 WApplication *wapp;
898 Window jr;
899 int ji;
900 unsigned int ju;
902 wwin = wWindowFor(event->xproperty.window);
903 if (wwin) {
904 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
905 return;
907 wClientCheckProperty(wwin, &event->xproperty);
909 wapp = wApplicationOf(event->xproperty.window);
910 if (wapp) {
911 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
915 static void handleClientMessage(XEvent * event)
917 WWindow *wwin;
918 WObjDescriptor *desc;
920 /* handle transition from Normal to Iconic state */
921 if (event->xclient.message_type == w_global.atom.wm.change_state
922 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
924 wwin = wWindowFor(event->xclient.window);
925 if (!wwin)
926 return;
927 if (!wwin->flags.miniaturized)
928 wIconifyWindow(wwin);
929 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
930 WScreen *scr = wScreenForRootWindow(event->xclient.window);
932 if (!scr)
933 return;
935 if (event->xclient.data.l[1] == 1) { /* starting */
936 wColormapAllowClientInstallation(scr, True);
937 } else { /* stopping */
938 wColormapAllowClientInstallation(scr, False);
940 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
942 char *command;
943 size_t len;
945 len = sizeof(event->xclient.data.b) + 1;
946 command = wmalloc(len);
947 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
949 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
950 wwarning(_("Got Reconfigure command"));
951 wDefaultsCheckDomains(NULL);
952 } else {
953 wwarning(_("Got unknown command %s"), command);
956 wfree(command);
958 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
959 WApplication *wapp;
960 int done = 0;
961 wapp = wApplicationOf(event->xclient.window);
962 if (wapp) {
963 switch (event->xclient.data.l[0]) {
964 case WMFHideOtherApplications:
965 wHideOtherApplications(wapp->main_window_desc);
966 done = 1;
967 break;
969 case WMFHideApplication:
970 wHideApplication(wapp);
971 done = 1;
972 break;
975 if (!done) {
976 wwin = wWindowFor(event->xclient.window);
977 if (wwin) {
978 switch (event->xclient.data.l[0]) {
979 case WMFHideOtherApplications:
980 wHideOtherApplications(wwin);
981 break;
983 case WMFHideApplication:
984 wHideApplication(wApplicationOf(wwin->main_window));
985 break;
989 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
990 wwin = wWindowFor(event->xclient.window);
991 if (!wwin)
992 return;
993 switch (event->xclient.data.l[0]) {
994 case GSWindowLevelAttr:
996 int level = (int)event->xclient.data.l[1];
998 if (WINDOW_LEVEL(wwin) != level) {
999 ChangeStackingLevel(wwin->frame->core, level);
1002 break;
1004 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
1005 wwin = wWindowFor(event->xclient.window);
1006 if (!wwin)
1007 return;
1008 switch (event->xclient.data.l[0]) {
1009 case WMTitleBarNormal:
1010 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1011 break;
1012 case WMTitleBarMain:
1013 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1014 break;
1015 case WMTitleBarKey:
1016 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1017 break;
1019 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1020 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1021 if (!scr)
1022 return;
1023 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1024 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1025 /* do nothing */
1026 #ifdef XDND
1027 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1028 /* do nothing */
1029 #endif /* XDND */
1030 } else {
1032 * Non-standard thing, but needed by OffiX DND.
1033 * For when the icon frame gets a ClientMessage
1034 * that should have gone to the icon_window.
1036 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1037 struct WIcon *icon = NULL;
1039 if (desc->parent_type == WCLASS_MINIWINDOW) {
1040 icon = (WIcon *) desc->parent;
1041 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1042 icon = ((WAppIcon *) desc->parent)->icon;
1044 if (icon && (wwin = icon->owner)) {
1045 if (wwin->client_win != event->xclient.window) {
1046 event->xclient.window = wwin->client_win;
1047 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1054 static void raiseWindow(WScreen * scr)
1056 WWindow *wwin;
1058 scr->autoRaiseTimer = NULL;
1060 wwin = wWindowFor(scr->autoRaiseWindow);
1061 if (!wwin)
1062 return;
1064 if (!wwin->flags.destroyed && wwin->flags.focused) {
1065 wRaiseFrame(wwin->frame->core);
1066 /* this is needed or a race condition will occur */
1067 XSync(dpy, False);
1071 static void handleEnterNotify(XEvent * event)
1073 WWindow *wwin;
1074 WObjDescriptor *desc = NULL;
1075 XEvent ev;
1076 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1078 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1079 /* already left the window... */
1080 saveTimestamp(&ev);
1081 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1082 return;
1086 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1087 if (desc->handle_enternotify)
1088 (*desc->handle_enternotify) (desc, event);
1091 /* enter to window */
1092 wwin = wWindowFor(event->xcrossing.window);
1093 if (!wwin) {
1094 if (wPreferences.colormap_mode == WCM_POINTER) {
1095 wColormapInstallForWindow(scr, NULL);
1097 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1098 WMDeleteTimerHandler(scr->autoRaiseTimer);
1099 scr->autoRaiseTimer = NULL;
1101 } else {
1102 /* set auto raise timer even if in focus-follows-mouse mode
1103 * and the event is for the frame window, even if the window
1104 * has focus already. useful if you move the pointer from a focused
1105 * window to the root window and back pretty fast
1107 * set focus if in focus-follows-mouse mode and the event
1108 * is for the frame window and window doesn't have focus yet */
1109 if (wPreferences.focus_mode == WKF_SLOPPY
1110 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1112 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1113 wSetFocusTo(scr, wwin);
1115 if (scr->autoRaiseTimer)
1116 WMDeleteTimerHandler(scr->autoRaiseTimer);
1117 scr->autoRaiseTimer = NULL;
1119 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1120 scr->autoRaiseWindow = wwin->frame->core->window;
1121 scr->autoRaiseTimer
1122 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1125 /* Install colormap for window, if the colormap installation mode
1126 * is colormap_follows_mouse */
1127 if (wPreferences.colormap_mode == WCM_POINTER) {
1128 if (wwin->client_win == event->xcrossing.window)
1129 wColormapInstallForWindow(scr, wwin);
1130 else
1131 wColormapInstallForWindow(scr, NULL);
1135 if (event->xcrossing.window == event->xcrossing.root
1136 && event->xcrossing.detail == NotifyNormal
1137 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1139 wSetFocusTo(scr, scr->focused_window);
1141 #ifdef BALLOON_TEXT
1142 wBalloonEnteredObject(scr, desc);
1143 #endif
1146 static void handleLeaveNotify(XEvent * event)
1148 WObjDescriptor *desc = NULL;
1150 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1151 if (desc->handle_leavenotify)
1152 (*desc->handle_leavenotify) (desc, event);
1156 #ifdef USE_XSHAPE
1157 static void handleShapeNotify(XEvent * event)
1159 XShapeEvent *shev = (XShapeEvent *) event;
1160 WWindow *wwin;
1161 union {
1162 XEvent xevent;
1163 XShapeEvent xshape;
1164 } ev;
1166 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1167 if (ev.xshape.kind == ShapeBounding) {
1168 if (ev.xshape.shaped == shev->shaped) {
1169 *shev = ev.xshape;
1170 } else {
1171 XPutBackEvent(dpy, &ev.xevent);
1172 break;
1177 wwin = wWindowFor(shev->window);
1178 if (!wwin || shev->kind != ShapeBounding)
1179 return;
1181 if (!shev->shaped && wwin->flags.shaped) {
1183 wwin->flags.shaped = 0;
1184 wWindowClearShape(wwin);
1186 } else if (shev->shaped) {
1188 wwin->flags.shaped = 1;
1189 wWindowSetShape(wwin);
1192 #endif /* USE_XSHAPE */
1194 #ifdef KEEP_XKB_LOCK_STATUS
1195 /* please help ]d if you know what to do */
1196 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1198 WWindow *wwin;
1199 WScreen *scr;
1200 XkbStateRec staterec;
1201 int i;
1203 for (i = 0; i < w_global.screen_count; i++) {
1204 scr = wScreenWithNumber(i);
1205 wwin = scr->focused_window;
1206 if (wwin && wwin->flags.focused) {
1207 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1208 if (wwin->frame->languagemode != staterec.group) {
1209 wwin->frame->last_languagemode = wwin->frame->languagemode;
1210 wwin->frame->languagemode = staterec.group;
1212 #ifdef XKB_BUTTON_HINT
1213 if (wwin->frame->titlebar) {
1214 wFrameWindowPaint(wwin->frame);
1216 #endif
1220 #endif /*KEEP_XKB_LOCK_STATUS */
1222 static void handleColormapNotify(XEvent * event)
1224 WWindow *wwin;
1225 WScreen *scr;
1226 Bool reinstall = False;
1228 wwin = wWindowFor(event->xcolormap.window);
1229 if (!wwin)
1230 return;
1232 scr = wwin->screen_ptr;
1234 do {
1235 if (wwin) {
1236 if (event->xcolormap.new) {
1237 XWindowAttributes attr;
1239 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1241 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1242 scr->current_colormap = attr.colormap;
1244 reinstall = True;
1245 } else if (event->xcolormap.state == ColormapUninstalled &&
1246 scr->current_colormap == event->xcolormap.colormap) {
1248 /* some bastard app (like XV) removed our colormap */
1250 * can't enforce or things like xscreensaver wont work
1251 * reinstall = True;
1253 } else if (event->xcolormap.state == ColormapInstalled &&
1254 scr->current_colormap == event->xcolormap.colormap) {
1256 /* someone has put our colormap back */
1257 reinstall = False;
1260 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1261 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1263 if (reinstall && scr->current_colormap != None) {
1264 if (!scr->flags.colormap_stuff_blocked)
1265 XInstallColormap(dpy, scr->current_colormap);
1269 static void handleFocusIn(XEvent * event)
1271 WWindow *wwin;
1274 * For applications that like stealing the focus.
1276 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1277 saveTimestamp(event);
1278 if (event->xfocus.mode == NotifyUngrab
1279 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1280 return;
1283 wwin = wWindowFor(event->xfocus.window);
1284 if (wwin && !wwin->flags.focused) {
1285 if (wwin->flags.mapped)
1286 wSetFocusTo(wwin->screen_ptr, wwin);
1287 else
1288 wSetFocusTo(wwin->screen_ptr, NULL);
1289 } else if (!wwin) {
1290 WScreen *scr = wScreenForWindow(event->xfocus.window);
1291 if (scr)
1292 wSetFocusTo(scr, NULL);
1296 static WWindow *windowUnderPointer(WScreen * scr)
1298 unsigned int mask;
1299 int foo;
1300 Window bar, win;
1302 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1303 return wWindowFor(win);
1304 return NULL;
1307 static int CheckFullScreenWindowFocused(WScreen * scr)
1309 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1310 return 1;
1311 else
1312 return 0;
1315 static void handleKeyPress(XEvent * event)
1317 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1318 WWindow *wwin = scr->focused_window;
1319 short i, widx;
1320 int modifiers;
1321 int command = -1;
1322 #ifdef KEEP_XKB_LOCK_STATUS
1323 XkbStateRec staterec;
1324 #endif /*KEEP_XKB_LOCK_STATUS */
1326 /* ignore CapsLock */
1327 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1329 for (i = 0; i < WKBD_LAST; i++) {
1330 if (wKeyBindings[i].keycode == 0)
1331 continue;
1333 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1334 || */ wKeyBindings[i].modifier ==
1335 modifiers)) {
1336 command = i;
1337 break;
1341 if (command < 0) {
1343 if (!wRootMenuPerformShortcut(event)) {
1344 static int dontLoop = 0;
1346 if (dontLoop > 10) {
1347 wwarning("problem with key event processing code");
1348 return;
1350 dontLoop++;
1351 /* if the focused window is an internal window, try redispatching
1352 * the event to the managed window, as it can be a WINGs window */
1353 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1354 /* client_leader contains the WINGs toplevel */
1355 event->xany.window = wwin->client_leader;
1356 WMHandleEvent(event);
1358 dontLoop--;
1360 return;
1362 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1363 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1365 switch (command) {
1367 case WKBD_ROOTMENU:
1368 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1369 if (!CheckFullScreenWindowFocused(scr)) {
1370 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1371 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1372 True);
1374 break;
1375 case WKBD_WINDOWLIST:
1376 if (!CheckFullScreenWindowFocused(scr)) {
1377 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1378 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1379 True);
1381 break;
1383 case WKBD_WINDOWMENU:
1384 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1385 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1386 break;
1387 case WKBD_MINIMIZEALL:
1388 CloseWindowMenu(scr);
1389 wHideAll(scr);
1390 break;
1391 case WKBD_MINIATURIZE:
1392 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1393 && !WFLAGP(wwin, no_miniaturizable)) {
1394 CloseWindowMenu(scr);
1396 if (wwin->protocols.MINIATURIZE_WINDOW)
1397 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1398 else {
1399 wIconifyWindow(wwin);
1402 break;
1403 case WKBD_HIDE:
1404 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1405 WApplication *wapp = wApplicationOf(wwin->main_window);
1406 CloseWindowMenu(scr);
1408 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1409 wHideApplication(wapp);
1412 break;
1413 case WKBD_HIDE_OTHERS:
1414 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1415 CloseWindowMenu(scr);
1417 wHideOtherApplications(wwin);
1419 break;
1420 case WKBD_MAXIMIZE:
1421 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1422 CloseWindowMenu(scr);
1424 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1426 break;
1427 case WKBD_VMAXIMIZE:
1428 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1429 CloseWindowMenu(scr);
1431 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1433 break;
1434 case WKBD_HMAXIMIZE:
1435 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1436 CloseWindowMenu(scr);
1438 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1440 break;
1441 case WKBD_LHMAXIMIZE:
1442 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1443 CloseWindowMenu(scr);
1445 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1447 break;
1448 case WKBD_RHMAXIMIZE:
1449 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1450 CloseWindowMenu(scr);
1452 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1454 break;
1455 case WKBD_THMAXIMIZE:
1456 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1457 CloseWindowMenu(scr);
1459 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1461 break;
1462 case WKBD_BHMAXIMIZE:
1463 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1464 CloseWindowMenu(scr);
1466 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1468 break;
1469 case WKBD_LTCMAXIMIZE:
1470 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1471 CloseWindowMenu(scr);
1473 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1475 break;
1476 case WKBD_RTCMAXIMIZE:
1477 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1478 CloseWindowMenu(scr);
1480 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1482 break;
1483 case WKBD_LBCMAXIMIZE:
1484 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1485 CloseWindowMenu(scr);
1487 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1489 break;
1490 case WKBD_RBCMAXIMIZE:
1491 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1492 CloseWindowMenu(scr);
1494 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1496 break;
1497 case WKBD_MAXIMUS:
1498 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1499 CloseWindowMenu(scr);
1501 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1503 break;
1504 case WKBD_RAISE:
1505 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1506 CloseWindowMenu(scr);
1508 wRaiseFrame(wwin->frame->core);
1510 break;
1511 case WKBD_LOWER:
1512 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1513 CloseWindowMenu(scr);
1515 wLowerFrame(wwin->frame->core);
1517 break;
1518 case WKBD_RAISELOWER:
1519 /* raise or lower the window under the pointer, not the
1520 * focused one
1522 wwin = windowUnderPointer(scr);
1523 if (wwin)
1524 wRaiseLowerFrame(wwin->frame->core);
1525 break;
1526 case WKBD_SHADE:
1527 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1528 if (wwin->flags.shaded)
1529 wUnshadeWindow(wwin);
1530 else
1531 wShadeWindow(wwin);
1533 break;
1534 case WKBD_MOVERESIZE:
1535 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1536 CloseWindowMenu(scr);
1538 wKeyboardMoveResizeWindow(wwin);
1540 break;
1541 case WKBD_CLOSE:
1542 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1543 CloseWindowMenu(scr);
1544 if (wwin->protocols.DELETE_WINDOW)
1545 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1547 break;
1548 case WKBD_SELECT:
1549 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1550 wSelectWindow(wwin, !wwin->flags.selected);
1552 break;
1553 case WKBD_FOCUSNEXT:
1554 StartWindozeCycle(wwin, event, True, False);
1555 break;
1557 case WKBD_FOCUSPREV:
1558 StartWindozeCycle(wwin, event, False, False);
1559 break;
1561 case WKBD_GROUPNEXT:
1562 StartWindozeCycle(wwin, event, True, True);
1563 break;
1565 case WKBD_GROUPPREV:
1566 StartWindozeCycle(wwin, event, False, True);
1567 break;
1569 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1570 widx = command - WKBD_WORKSPACE1;
1571 i = (w_global.workspace.current / 10) * 10 + widx;
1572 if (wPreferences.ws_advance || i < w_global.workspace.count)
1573 wWorkspaceChange(scr, i);
1574 break;
1576 case WKBD_NEXTWORKSPACE:
1577 wWorkspaceRelativeChange(scr, 1);
1578 break;
1579 case WKBD_PREVWORKSPACE:
1580 wWorkspaceRelativeChange(scr, -1);
1581 break;
1582 case WKBD_LASTWORKSPACE:
1583 wWorkspaceChange(scr, w_global.workspace.last_used);
1584 break;
1586 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1587 widx = command - WKBD_MOVE_WORKSPACE1;
1588 i = (w_global.workspace.current / 10) * 10 + widx;
1589 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1590 wWindowChangeWorkspace(wwin, i);
1591 break;
1593 case WKBD_MOVE_NEXTWORKSPACE:
1594 if (wwin)
1595 wWindowChangeWorkspaceRelative(wwin, 1);
1596 break;
1597 case WKBD_MOVE_PREVWORKSPACE:
1598 if (wwin)
1599 wWindowChangeWorkspaceRelative(wwin, -1);
1600 break;
1601 case WKBD_MOVE_LASTWORKSPACE:
1602 if (wwin)
1603 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1604 break;
1606 case WKBD_MOVE_NEXTWSLAYER:
1607 case WKBD_MOVE_PREVWSLAYER:
1609 if (wwin) {
1610 int row, column;
1612 row = w_global.workspace.current / 10;
1613 column = w_global.workspace.current % 10;
1615 if (command == WKBD_MOVE_NEXTWSLAYER) {
1616 if ((row + 1) * 10 < w_global.workspace.count)
1617 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1618 } else {
1619 if (row > 0)
1620 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1624 break;
1626 case WKBD_WINDOW1:
1627 case WKBD_WINDOW2:
1628 case WKBD_WINDOW3:
1629 case WKBD_WINDOW4:
1630 case WKBD_WINDOW5:
1631 case WKBD_WINDOW6:
1632 case WKBD_WINDOW7:
1633 case WKBD_WINDOW8:
1634 case WKBD_WINDOW9:
1635 case WKBD_WINDOW10:
1637 widx = command - WKBD_WINDOW1;
1639 if (w_global.shortcut.windows[widx]) {
1640 WMArray *list = w_global.shortcut.windows[widx];
1641 int cw;
1642 int count = WMGetArrayItemCount(list);
1643 WWindow *twin;
1644 WMArrayIterator iter;
1645 WWindow *wwin;
1647 wUnselectWindows(scr);
1648 cw = w_global.workspace.current;
1650 WM_ETARETI_ARRAY(list, wwin, iter) {
1651 if (count > 1)
1652 wWindowChangeWorkspace(wwin, cw);
1654 wMakeWindowVisible(wwin);
1656 if (count > 1)
1657 wSelectWindow(wwin, True);
1660 /* rotate the order of windows, to create a cycling effect */
1661 twin = WMGetFromArray(list, 0);
1662 WMDeleteFromArray(list, 0);
1663 WMAddToArray(list, twin);
1665 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1666 if (w_global.shortcut.windows[widx]) {
1667 WMFreeArray(w_global.shortcut.windows[widx]);
1668 w_global.shortcut.windows[widx] = NULL;
1671 if (wwin->flags.selected && scr->selected_windows) {
1672 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1673 } else {
1674 w_global.shortcut.windows[widx] = WMCreateArray(4);
1675 WMAddToArray(w_global.shortcut.windows[widx], wwin);
1678 wSelectWindow(wwin, !wwin->flags.selected);
1679 XFlush(dpy);
1680 wusleep(3000);
1681 wSelectWindow(wwin, !wwin->flags.selected);
1682 XFlush(dpy);
1684 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1685 if (wwin->flags.selected && scr->selected_windows) {
1686 if (w_global.shortcut.windows[widx])
1687 WMFreeArray(w_global.shortcut.windows[widx]);
1689 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1693 break;
1695 case WKBD_RELAUNCH:
1696 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1697 (void) RelaunchWindow(wwin);
1699 break;
1701 case WKBD_SWITCH_SCREEN:
1702 if (w_global.screen_count > 1) {
1703 WScreen *scr2;
1704 int i;
1706 /* find index of this screen */
1707 for (i = 0; i < w_global.screen_count; i++) {
1708 if (wScreenWithNumber(i) == scr)
1709 break;
1711 i++;
1712 if (i >= w_global.screen_count) {
1713 i = 0;
1715 scr2 = wScreenWithNumber(i);
1717 if (scr2) {
1718 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1719 scr2->scr_width / 2, scr2->scr_height / 2);
1722 break;
1724 case WKBD_RUN:
1726 char *cmdline;
1728 cmdline = ExpandOptions(scr, _("exec %a(Run,Type command to run:)"));
1730 if (cmdline) {
1731 XGrabPointer(dpy, scr->root_win, True, 0,
1732 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1733 XSync(dpy, False);
1735 ExecuteShellCommand(scr, cmdline);
1736 wfree(cmdline);
1738 XUngrabPointer(dpy, CurrentTime);
1739 XSync(dpy, False);
1741 break;
1744 case WKBD_NEXTWSLAYER:
1745 case WKBD_PREVWSLAYER:
1747 int row, column;
1749 row = w_global.workspace.current / 10;
1750 column = w_global.workspace.current % 10;
1752 if (command == WKBD_NEXTWSLAYER) {
1753 if ((row + 1) * 10 < w_global.workspace.count)
1754 wWorkspaceChange(scr, column + (row + 1) * 10);
1755 } else {
1756 if (row > 0)
1757 wWorkspaceChange(scr, column + (row - 1) * 10);
1760 break;
1761 case WKBD_CLIPRAISELOWER:
1762 if (!wPreferences.flags.noclip)
1763 wDockRaiseLower(w_global.workspace.array[w_global.workspace.current]->clip);
1764 break;
1765 case WKBD_DOCKRAISELOWER:
1766 if (!wPreferences.flags.nodock)
1767 wDockRaiseLower(scr->dock);
1768 break;
1769 #ifdef KEEP_XKB_LOCK_STATUS
1770 case WKBD_TOGGLE:
1771 if (wPreferences.modelock) {
1772 /*toggle */
1773 wwin = scr->focused_window;
1775 if (wwin && wwin->flags.mapped
1776 && wwin->frame->workspace == w_global.workspace.current
1777 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1778 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1780 wwin->frame->languagemode = wwin->frame->last_languagemode;
1781 wwin->frame->last_languagemode = staterec.group;
1782 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1786 break;
1787 #endif /* KEEP_XKB_LOCK_STATUS */
1791 static void handleMotionNotify(XEvent * event)
1793 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1795 if (wPreferences.scrollable_menus) {
1796 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1797 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1799 if (scr->flags.jump_back_pending ||
1800 p.x <= (rect.pos.x + 1) ||
1801 p.x >= (rect.pos.x + rect.size.width - 2) ||
1802 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1803 WMenu *menu;
1805 menu = wMenuUnderPointer(scr);
1806 if (menu != NULL)
1807 wMenuScroll(menu);
1812 static void handleVisibilityNotify(XEvent * event)
1814 WWindow *wwin;
1816 wwin = wWindowFor(event->xvisibility.window);
1817 if (!wwin)
1818 return;
1819 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);