util: clarify a bit of the code for parsing commands in wmgenmenu
[wmaker-crm.git] / src / event.c
blob9757dadce4e48bf58b0a4651597b7c074df963f7
1 /* event.c- event loop and handling
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #ifdef HAVE_INOTIFY
25 #include <sys/select.h>
26 #include <sys/inotify.h>
27 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xutil.h>
38 #ifdef USE_XSHAPE
39 # include <X11/extensions/shape.h>
40 #endif
41 #ifdef XDND
42 #include "xdnd.h"
43 #endif
45 #ifdef USE_RANDR
46 #include <X11/extensions/Xrandr.h>
47 #endif
49 #ifdef KEEP_XKB_LOCK_STATUS
50 #include <X11/XKBlib.h>
51 #endif /* KEEP_XKB_LOCK_STATUS */
53 #include "WindowMaker.h"
54 #include "window.h"
55 #include "actions.h"
56 #include "client.h"
57 #include "main.h"
58 #include "cycling.h"
59 #include "keybind.h"
60 #include "application.h"
61 #include "stacking.h"
62 #include "defaults.h"
63 #include "workspace.h"
64 #include "dock.h"
65 #include "framewin.h"
66 #include "properties.h"
67 #include "balloon.h"
68 #include "xinerama.h"
69 #include "wmspec.h"
70 #include "rootmenu.h"
71 #include "colormap.h"
72 #include "screen.h"
73 #include "shutdown.h"
74 #include "misc.h"
75 #include "event.h"
76 #include "winmenu.h"
77 #include "switchmenu.h"
80 #define MOD_MASK wPreferences.modifier_mask
82 /************ Local stuff ***********/
84 static void saveTimestamp(XEvent *event);
85 static void handleColormapNotify(XEvent *event);
86 static void handleMapNotify(XEvent *event);
87 static void handleUnmapNotify(XEvent *event);
88 static void handleButtonPress(XEvent *event);
89 static void handleExpose(XEvent *event);
90 static void handleDestroyNotify(XEvent *event);
91 static void handleConfigureRequest(XEvent *event);
92 static void handleMapRequest(XEvent *event);
93 static void handlePropertyNotify(XEvent *event);
94 static void handleEnterNotify(XEvent *event);
95 static void handleLeaveNotify(XEvent *event);
96 static void handleExtensions(XEvent *event);
97 static void handleClientMessage(XEvent *event);
98 static void handleKeyPress(XEvent *event);
99 static void handleFocusIn(XEvent *event);
100 static void handleMotionNotify(XEvent *event);
101 static void handleVisibilityNotify(XEvent *event);
102 static void handle_inotify_events(void);
103 static void wdelete_death_handler(WMagicNumber id);
106 #ifdef USE_XSHAPE
107 static void handleShapeNotify(XEvent *event);
108 #endif
110 #ifdef KEEP_XKB_LOCK_STATUS
111 static void handleXkbIndicatorStateNotify(XkbEvent *event);
112 #endif
114 /* real dead process handler */
115 static void handleDeadProcess(void);
117 typedef struct DeadProcesses {
118 pid_t pid;
119 unsigned char exit_status;
120 } DeadProcesses;
122 /* stack of dead processes */
123 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
124 static int deadProcessPtr = 0;
126 typedef struct DeathHandler {
127 WDeathHandler *callback;
128 pid_t pid;
129 void *client_data;
130 } DeathHandler;
132 static WMArray *deathHandlers = NULL;
134 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
136 DeathHandler *handler;
138 handler = malloc(sizeof(DeathHandler));
139 if (!handler)
140 return 0;
142 handler->pid = pid;
143 handler->callback = callback;
144 handler->client_data = cdata;
146 if (!deathHandlers)
147 deathHandlers = WMCreateArrayWithDestructor(8, free);
149 WMAddToArray(deathHandlers, handler);
151 return handler;
154 static void wdelete_death_handler(WMagicNumber id)
156 DeathHandler *handler = (DeathHandler *) id;
158 if (!handler || !deathHandlers)
159 return;
161 /* array destructor will call free(handler) */
162 WMRemoveFromArray(deathHandlers, handler);
165 void DispatchEvent(XEvent * event)
167 if (deathHandlers)
168 handleDeadProcess();
170 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
171 WCHANGE_STATE(WSTATE_EXITING);
172 /* received SIGTERM */
174 * WMHandleEvent() can't be called from anything
175 * executed inside here, or we can get in a infinite
176 * recursive loop.
178 Shutdown(WSExitMode);
180 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
181 WCHANGE_STATE(WSTATE_RESTARTING);
183 Shutdown(WSRestartPreparationMode);
184 /* received SIGHUP */
185 Restart(NULL, True);
186 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
187 WCHANGE_STATE(WSTATE_NORMAL);
188 wDefaultsCheckDomains(NULL);
191 /* for the case that all that is wanted to be dispatched is
192 * the stuff above */
193 if (!event)
194 return;
196 saveTimestamp(event);
197 switch (event->type) {
198 case MapRequest:
199 handleMapRequest(event);
200 break;
202 case KeyPress:
203 handleKeyPress(event);
204 break;
206 case MotionNotify:
207 handleMotionNotify(event);
208 break;
210 case ConfigureRequest:
211 handleConfigureRequest(event);
212 break;
214 case DestroyNotify:
215 handleDestroyNotify(event);
216 break;
218 case MapNotify:
219 handleMapNotify(event);
220 break;
222 case UnmapNotify:
223 handleUnmapNotify(event);
224 break;
226 case ButtonPress:
227 handleButtonPress(event);
228 break;
230 case Expose:
231 handleExpose(event);
232 break;
234 case PropertyNotify:
235 handlePropertyNotify(event);
236 break;
238 case EnterNotify:
239 handleEnterNotify(event);
240 break;
242 case LeaveNotify:
243 handleLeaveNotify(event);
244 break;
246 case ClientMessage:
247 handleClientMessage(event);
248 break;
250 case ColormapNotify:
251 handleColormapNotify(event);
252 break;
254 case MappingNotify:
255 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
256 XRefreshKeyboardMapping(&event->xmapping);
257 break;
259 case FocusIn:
260 handleFocusIn(event);
261 break;
263 case VisibilityNotify:
264 handleVisibilityNotify(event);
265 break;
267 case ConfigureNotify:
268 #ifdef USE_RANDR
269 if (event->xconfigure.window == DefaultRootWindow(dpy))
270 XRRUpdateConfiguration(event);
271 #endif
272 break;
274 default:
275 handleExtensions(event);
276 break;
280 #ifdef HAVE_INOTIFY
282 *----------------------------------------------------------------------
283 * handle_inotify_events-
284 * Check for inotify events
286 * Returns:
287 * After reading events for the given file descriptor (fd) and
288 * watch descriptor (wd)
290 * Side effects:
291 * Calls wDefaultsCheckDomains if config database is updated
292 *----------------------------------------------------------------------
294 static void handle_inotify_events(void)
296 ssize_t eventQLength, i = 0;
297 /* Make room for at lease 5 simultaneous events, with path + filenames */
298 char buff[ (sizeof(struct inotify_event) + NAME_MAX + 1) * 5 ];
299 /* Check config only once per read of the event queue */
300 int oneShotFlag = 0;
303 * Read off the queued events
304 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
305 * not occur; the block is on Xevents, but a config file change will normally
306 * occur as a result of an Xevent - so the event queue should never have more than
307 * a few entries before a read().
309 eventQLength = read(w_global.inotify.fd_event_queue,
310 buff, sizeof(buff) );
312 /* check what events occured */
313 /* Should really check wd here too, but for now we only have one watch! */
314 while (i < eventQLength) {
315 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
318 * see inotify.h for event types.
320 if (pevent->mask & IN_DELETE_SELF) {
321 wwarning(_("the defaults database has been deleted!"
322 " Restart Window Maker to create the database" " with the default settings"));
324 if (w_global.inotify.fd_event_queue >= 0) {
325 close(w_global.inotify.fd_event_queue);
326 w_global.inotify.fd_event_queue = -1;
329 if (pevent->mask & IN_UNMOUNT) {
330 wwarning(_("the unit containing the defaults database has"
331 " been unmounted. Setting --static mode." " Any changes will not be saved."));
333 if (w_global.inotify.fd_event_queue >= 0) {
334 close(w_global.inotify.fd_event_queue);
335 w_global.inotify.fd_event_queue = -1;
338 wPreferences.flags.noupdates = 1;
340 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
341 wwarning(_("Inotify: Reading config files in defaults database."));
342 wDefaultsCheckDomains(NULL);
343 oneShotFlag = 1;
346 /* move to next event in the buffer */
347 i += sizeof(struct inotify_event) + pevent->len;
350 #endif /* HAVE_INOTIFY */
353 *----------------------------------------------------------------------
354 * EventLoop-
355 * Processes X and internal events indefinitely.
357 * Returns:
358 * Never returns
360 * Side effects:
361 * The LastTimestamp global variable is updated.
362 * Calls inotifyGetEvents if defaults database changes.
363 *----------------------------------------------------------------------
365 noreturn void EventLoop(void)
367 XEvent event;
368 #ifdef HAVE_INOTIFY
369 struct timeval time;
370 fd_set rfds;
371 int retVal = 0;
373 if (w_global.inotify.fd_event_queue < 0 || w_global.inotify.wd_defaults < 0)
374 retVal = -1;
375 #endif
377 for (;;) {
379 WMNextEvent(dpy, &event); /* Blocks here */
380 WMHandleEvent(&event);
381 #ifdef HAVE_INOTIFY
382 if (retVal != -1) {
383 time.tv_sec = 0;
384 time.tv_usec = 0;
385 FD_ZERO(&rfds);
386 FD_SET(w_global.inotify.fd_event_queue, &rfds);
388 /* check for available read data from inotify - don't block! */
389 retVal = select(w_global.inotify.fd_event_queue + 1, &rfds, NULL, NULL, &time);
391 if (retVal < 0) { /* an error has occured */
392 wwarning(_("select failed. The inotify instance will be closed."
393 " Changes to the defaults database will require"
394 " a restart to take effect."));
395 close(w_global.inotify.fd_event_queue);
396 w_global.inotify.fd_event_queue = -1;
397 continue;
399 if (FD_ISSET(w_global.inotify.fd_event_queue, &rfds))
400 handle_inotify_events();
402 #endif
407 *----------------------------------------------------------------------
408 * ProcessPendingEvents --
409 * Processes the events that are currently pending (at the time
410 * this function is called) in the display's queue.
412 * Returns:
413 * After the pending events that were present at the function call
414 * are processed.
416 * Side effects:
417 * Many -- whatever handling events may involve.
419 *----------------------------------------------------------------------
421 void ProcessPendingEvents(void)
423 XEvent event;
424 int count;
426 XSync(dpy, False);
428 /* Take a snapshot of the event count in the queue */
429 count = XPending(dpy);
431 while (count > 0 && XPending(dpy)) {
432 WMNextEvent(dpy, &event);
433 WMHandleEvent(&event);
434 count--;
438 Bool IsDoubleClick(WScreen * scr, XEvent * event)
440 if ((scr->last_click_time > 0) &&
441 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
442 && (event->xbutton.button == scr->last_click_button)
443 && (event->xbutton.window == scr->last_click_window)) {
445 scr->flags.next_click_is_not_double = 1;
446 scr->last_click_time = 0;
447 scr->last_click_window = event->xbutton.window;
449 return True;
451 return False;
454 void NotifyDeadProcess(pid_t pid, unsigned char status)
456 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
457 wwarning("stack overflow: too many dead processes");
458 return;
460 /* stack the process to be handled later,
461 * as this is called from the signal handler */
462 deadProcesses[deadProcessPtr].pid = pid;
463 deadProcesses[deadProcessPtr].exit_status = status;
464 deadProcessPtr++;
467 static void handleDeadProcess(void)
469 DeathHandler *tmp;
470 int i;
472 for (i = 0; i < deadProcessPtr; i++) {
473 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
476 if (!deathHandlers) {
477 deadProcessPtr = 0;
478 return;
481 /* get the pids on the queue and call handlers */
482 while (deadProcessPtr > 0) {
483 deadProcessPtr--;
485 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
486 tmp = WMGetFromArray(deathHandlers, i);
487 if (!tmp)
488 continue;
490 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
491 (*tmp->callback) (tmp->pid,
492 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
493 wdelete_death_handler(tmp);
499 static void saveTimestamp(XEvent * event)
502 * Never save CurrentTime as LastTimestamp because CurrentTime
503 * it's not a real timestamp (it's the 0L constant)
506 switch (event->type) {
507 case ButtonRelease:
508 case ButtonPress:
509 w_global.timestamp.last_event = event->xbutton.time;
510 break;
511 case KeyPress:
512 case KeyRelease:
513 w_global.timestamp.last_event = event->xkey.time;
514 break;
515 case MotionNotify:
516 w_global.timestamp.last_event = event->xmotion.time;
517 break;
518 case PropertyNotify:
519 w_global.timestamp.last_event = event->xproperty.time;
520 break;
521 case EnterNotify:
522 case LeaveNotify:
523 w_global.timestamp.last_event = event->xcrossing.time;
524 break;
525 case SelectionClear:
526 w_global.timestamp.last_event = event->xselectionclear.time;
527 break;
528 case SelectionRequest:
529 w_global.timestamp.last_event = event->xselectionrequest.time;
530 break;
531 case SelectionNotify:
532 w_global.timestamp.last_event = event->xselection.time;
533 #ifdef XDND
534 wXDNDProcessSelection(event);
535 #endif
536 break;
540 static int matchWindow(const void *item, const void *cdata)
542 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
545 static void handleExtensions(XEvent * event)
547 #ifdef USE_XSHAPE
548 if (w_global.xext.shape.supported && event->type == (w_global.xext.shape.event_base + ShapeNotify)) {
549 handleShapeNotify(event);
551 #endif
552 #ifdef KEEP_XKB_LOCK_STATUS
553 if (wPreferences.modelock && (event->type == w_global.xext.xkb.event_base)) {
554 handleXkbIndicatorStateNotify((XkbEvent *) event);
556 #endif /*KEEP_XKB_LOCK_STATUS */
557 #ifdef USE_RANDR
558 if (w_global.xext.randr.supported && event->type == (w_global.xext.randr.event_base + RRScreenChangeNotify)) {
559 /* From xrandr man page: "Clients must call back into Xlib using
560 * XRRUpdateConfiguration when screen configuration change notify
561 * events are generated */
562 XRRUpdateConfiguration(event);
563 WCHANGE_STATE(WSTATE_RESTARTING);
564 Shutdown(WSRestartPreparationMode);
565 Restart(NULL,True);
567 #endif
570 static void handleMapRequest(XEvent * ev)
572 WWindow *wwin;
573 WScreen *scr = NULL;
574 Window window = ev->xmaprequest.window;
576 if ((wwin = wWindowFor(window))) {
577 if (wwin->flags.shaded) {
578 wUnshadeWindow(wwin);
580 /* deiconify window */
581 if (wwin->flags.miniaturized) {
582 wDeiconifyWindow(wwin);
583 } else if (wwin->flags.hidden) {
584 WApplication *wapp = wApplicationOf(wwin->main_window);
585 /* go to the last workspace that the user worked on the app */
586 if (wapp) {
587 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
589 wUnhideApplication(wapp, False, False);
591 return;
594 scr = wScreenForRootWindow(ev->xmaprequest.parent);
596 wwin = wManageWindow(scr, window);
599 * This is to let the Dock know that the application it launched
600 * has already been mapped (eg: it has finished launching).
601 * It is not necessary for normally docked apps, but is needed for
602 * apps that were forcedly docked (like with dockit).
604 if (scr->last_dock) {
605 if (wwin && wwin->main_window != None && wwin->main_window != window)
606 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
607 else
608 wDockTrackWindowLaunch(scr->last_dock, window);
611 if (wwin) {
612 wClientSetState(wwin, NormalState, None);
613 if (wwin->flags.maximized) {
614 wMaximizeWindow(wwin, wwin->flags.maximized);
616 if (wwin->flags.shaded) {
617 wwin->flags.shaded = 0;
618 wwin->flags.skip_next_animation = 1;
619 wShadeWindow(wwin);
621 if (wwin->flags.miniaturized) {
622 wwin->flags.miniaturized = 0;
623 wwin->flags.skip_next_animation = 1;
624 wIconifyWindow(wwin);
626 if (wwin->flags.fullscreen) {
627 wwin->flags.fullscreen = 0;
628 wFullscreenWindow(wwin);
630 if (wwin->flags.hidden) {
631 WApplication *wapp = wApplicationOf(wwin->main_window);
633 wwin->flags.hidden = 0;
634 wwin->flags.skip_next_animation = 1;
635 if (wapp) {
636 wHideApplication(wapp);
642 static void handleDestroyNotify(XEvent * event)
644 WWindow *wwin;
645 WApplication *app;
646 Window window = event->xdestroywindow.window;
647 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
648 int widx;
650 wwin = wWindowFor(window);
651 if (wwin) {
652 wUnmanageWindow(wwin, False, True);
655 if (scr != NULL) {
656 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
657 WFakeGroupLeader *fPtr;
659 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
660 if (fPtr->retainCount > 0) {
661 fPtr->retainCount--;
662 if (fPtr->retainCount == 0 && fPtr->leader != None) {
663 XDestroyWindow(dpy, fPtr->leader);
664 fPtr->leader = None;
665 XFlush(dpy);
668 fPtr->origLeader = None;
672 app = wApplicationOf(window);
673 if (app) {
674 if (window == app->main_window) {
675 app->refcount = 0;
676 wwin = app->main_window_desc->screen_ptr->focused_window;
677 while (wwin) {
678 if (wwin->main_window == window) {
679 wwin->main_window = None;
681 wwin = wwin->prev;
684 wApplicationDestroy(app);
688 static void handleExpose(XEvent * event)
690 WObjDescriptor *desc;
691 XEvent ev;
693 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
695 if (XFindContext(dpy, event->xexpose.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
696 return;
699 if (desc->handle_expose) {
700 (*desc->handle_expose) (desc, event);
704 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
706 switch (action) {
707 case WA_SELECT_WINDOWS:
708 wUnselectWindows(scr);
709 wSelectWindows(scr, event);
710 break;
711 case WA_OPEN_APPMENU:
712 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
713 /* ugly hack */
714 if (scr->root_menu) {
715 if (scr->root_menu->brother->flags.mapped)
716 event->xbutton.window = scr->root_menu->brother->frame->core->window;
717 else
718 event->xbutton.window = scr->root_menu->frame->core->window;
720 break;
721 case WA_OPEN_WINLISTMENU:
722 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
723 if (scr->switch_menu) {
724 if (scr->switch_menu->brother->flags.mapped)
725 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
726 else
727 event->xbutton.window = scr->switch_menu->frame->core->window;
729 break;
730 default:
731 break;
735 /* bindable */
736 static void handleButtonPress(XEvent * event)
738 WObjDescriptor *desc;
739 WScreen *scr;
741 scr = wScreenForRootWindow(event->xbutton.root);
743 #ifdef BALLOON_TEXT
744 wBalloonHide(scr);
745 #endif
747 if (event->xbutton.window == scr->root_win) {
748 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
749 executeButtonAction(scr, event, wPreferences.mouse_button1);
750 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
751 executeButtonAction(scr, event, wPreferences.mouse_button2);
752 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
753 executeButtonAction(scr, event, wPreferences.mouse_button3);
754 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
755 wWorkspaceRelativeChange(scr, 1);
756 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
757 wWorkspaceRelativeChange(scr, -1);
761 desc = NULL;
762 if (XFindContext(dpy, event->xbutton.subwindow, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
763 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT) {
764 return;
768 if (desc->parent_type == WCLASS_WINDOW) {
769 XSync(dpy, 0);
771 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
772 XAllowEvents(dpy, AsyncPointer, CurrentTime);
773 } else {
774 /* if (wPreferences.focus_mode == WKF_CLICK) { */
775 if (wPreferences.ignore_focus_click) {
776 XAllowEvents(dpy, AsyncPointer, CurrentTime);
778 XAllowEvents(dpy, ReplayPointer, CurrentTime);
779 /* } */
781 XSync(dpy, 0);
782 } else if (desc->parent_type == WCLASS_APPICON
783 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
784 if (event->xbutton.state & MOD_MASK) {
785 XSync(dpy, 0);
786 XAllowEvents(dpy, AsyncPointer, CurrentTime);
787 XSync(dpy, 0);
791 if (desc->handle_mousedown != NULL) {
792 (*desc->handle_mousedown) (desc, event);
795 /* save double-click information */
796 if (scr->flags.next_click_is_not_double) {
797 scr->flags.next_click_is_not_double = 0;
798 } else {
799 scr->last_click_time = event->xbutton.time;
800 scr->last_click_button = event->xbutton.button;
801 scr->last_click_window = event->xbutton.window;
805 static void handleMapNotify(XEvent * event)
807 WWindow *wwin;
809 wwin = wWindowFor(event->xmap.event);
810 if (wwin && wwin->client_win == event->xmap.event) {
811 if (wwin->flags.miniaturized) {
812 wDeiconifyWindow(wwin);
813 } else {
814 XGrabServer(dpy);
815 wWindowMap(wwin);
816 wClientSetState(wwin, NormalState, None);
817 XUngrabServer(dpy);
822 static void handleUnmapNotify(XEvent * event)
824 WWindow *wwin;
825 XEvent ev;
826 Bool withdraw = False;
828 /* only process windows with StructureNotify selected
829 * (ignore SubstructureNotify) */
830 wwin = wWindowFor(event->xunmap.window);
831 if (!wwin)
832 return;
834 /* whether the event is a Withdrawal request */
835 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
836 withdraw = True;
838 if (wwin->client_win != event->xunmap.event && !withdraw)
839 return;
841 if (!wwin->flags.mapped && !withdraw
842 && wwin->frame->workspace == w_global.workspace.current
843 && !wwin->flags.miniaturized && !wwin->flags.hidden)
844 return;
846 XGrabServer(dpy);
847 XUnmapWindow(dpy, wwin->frame->core->window);
848 wwin->flags.mapped = 0;
849 XSync(dpy, 0);
850 /* check if the window was destroyed */
851 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
852 DispatchEvent(&ev);
853 } else {
854 Bool reparented = False;
856 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
857 reparented = True;
859 /* withdraw window */
860 wwin->flags.mapped = 0;
861 if (!reparented)
862 wClientSetState(wwin, WithdrawnState, None);
864 /* if the window was reparented, do not reparent it back to the
865 * root window */
866 wUnmanageWindow(wwin, !reparented, False);
868 XUngrabServer(dpy);
871 static void handleConfigureRequest(XEvent * event)
873 WWindow *wwin;
875 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
877 * Configure request for unmapped window
879 wClientConfigure(NULL, &(event->xconfigurerequest));
880 } else {
881 wClientConfigure(wwin, &(event->xconfigurerequest));
885 static void handlePropertyNotify(XEvent * event)
887 WWindow *wwin;
888 WApplication *wapp;
889 Window jr;
890 int ji;
891 unsigned int ju;
893 wwin = wWindowFor(event->xproperty.window);
894 if (wwin) {
895 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
896 return;
898 wClientCheckProperty(wwin, &event->xproperty);
900 wapp = wApplicationOf(event->xproperty.window);
901 if (wapp) {
902 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
906 static void handleClientMessage(XEvent * event)
908 WWindow *wwin;
909 WObjDescriptor *desc;
911 /* handle transition from Normal to Iconic state */
912 if (event->xclient.message_type == w_global.atom.wm.change_state
913 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
915 wwin = wWindowFor(event->xclient.window);
916 if (!wwin)
917 return;
918 if (!wwin->flags.miniaturized)
919 wIconifyWindow(wwin);
920 } else if (event->xclient.message_type == w_global.atom.wm.colormap_notify && event->xclient.format == 32) {
921 WScreen *scr = wScreenForRootWindow(event->xclient.window);
923 if (!scr)
924 return;
926 if (event->xclient.data.l[1] == 1) { /* starting */
927 wColormapAllowClientInstallation(scr, True);
928 } else { /* stopping */
929 wColormapAllowClientInstallation(scr, False);
931 } else if (event->xclient.message_type == w_global.atom.wmaker.command) {
933 char *command;
934 size_t len;
936 len = sizeof(event->xclient.data.b) + 1;
937 command = wmalloc(len);
938 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
940 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
941 wwarning(_("Got Reconfigure command"));
942 wDefaultsCheckDomains(NULL);
943 } else {
944 wwarning(_("Got unknown command %s"), command);
947 wfree(command);
949 } else if (event->xclient.message_type == w_global.atom.wmaker.wm_function) {
950 WApplication *wapp;
951 int done = 0;
952 wapp = wApplicationOf(event->xclient.window);
953 if (wapp) {
954 switch (event->xclient.data.l[0]) {
955 case WMFHideOtherApplications:
956 wHideOtherApplications(wapp->main_window_desc);
957 done = 1;
958 break;
960 case WMFHideApplication:
961 wHideApplication(wapp);
962 done = 1;
963 break;
966 if (!done) {
967 wwin = wWindowFor(event->xclient.window);
968 if (wwin) {
969 switch (event->xclient.data.l[0]) {
970 case WMFHideOtherApplications:
971 wHideOtherApplications(wwin);
972 break;
974 case WMFHideApplication:
975 wHideApplication(wApplicationOf(wwin->main_window));
976 break;
980 } else if (event->xclient.message_type == w_global.atom.gnustep.wm_attr) {
981 wwin = wWindowFor(event->xclient.window);
982 if (!wwin)
983 return;
984 switch (event->xclient.data.l[0]) {
985 case GSWindowLevelAttr:
987 int level = (int)event->xclient.data.l[1];
989 if (WINDOW_LEVEL(wwin) != level) {
990 ChangeStackingLevel(wwin->frame->core, level);
993 break;
995 } else if (event->xclient.message_type == w_global.atom.gnustep.titlebar_state) {
996 wwin = wWindowFor(event->xclient.window);
997 if (!wwin)
998 return;
999 switch (event->xclient.data.l[0]) {
1000 case WMTitleBarNormal:
1001 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1002 break;
1003 case WMTitleBarMain:
1004 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1005 break;
1006 case WMTitleBarKey:
1007 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1008 break;
1010 } else if (event->xclient.message_type == w_global.atom.wm.ignore_focus_events) {
1011 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1012 if (!scr)
1013 return;
1014 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1015 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1016 /* do nothing */
1017 #ifdef XDND
1018 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1019 /* do nothing */
1020 #endif /* XDND */
1021 } else {
1023 * Non-standard thing, but needed by OffiX DND.
1024 * For when the icon frame gets a ClientMessage
1025 * that should have gone to the icon_window.
1027 if (XFindContext(dpy, event->xbutton.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1028 struct WIcon *icon = NULL;
1030 if (desc->parent_type == WCLASS_MINIWINDOW) {
1031 icon = (WIcon *) desc->parent;
1032 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1033 icon = ((WAppIcon *) desc->parent)->icon;
1035 if (icon && (wwin = icon->owner)) {
1036 if (wwin->client_win != event->xclient.window) {
1037 event->xclient.window = wwin->client_win;
1038 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1045 static void raiseWindow(WScreen * scr)
1047 WWindow *wwin;
1049 scr->autoRaiseTimer = NULL;
1051 wwin = wWindowFor(scr->autoRaiseWindow);
1052 if (!wwin)
1053 return;
1055 if (!wwin->flags.destroyed && wwin->flags.focused) {
1056 wRaiseFrame(wwin->frame->core);
1057 /* this is needed or a race condition will occur */
1058 XSync(dpy, False);
1062 static void handleEnterNotify(XEvent * event)
1064 WWindow *wwin;
1065 WObjDescriptor *desc = NULL;
1066 XEvent ev;
1067 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1069 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1070 /* already left the window... */
1071 saveTimestamp(&ev);
1072 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1073 return;
1077 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1078 if (desc->handle_enternotify)
1079 (*desc->handle_enternotify) (desc, event);
1082 /* enter to window */
1083 wwin = wWindowFor(event->xcrossing.window);
1084 if (!wwin) {
1085 if (wPreferences.colormap_mode == WCM_POINTER) {
1086 wColormapInstallForWindow(scr, NULL);
1088 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1089 WMDeleteTimerHandler(scr->autoRaiseTimer);
1090 scr->autoRaiseTimer = NULL;
1092 } else {
1093 /* set auto raise timer even if in focus-follows-mouse mode
1094 * and the event is for the frame window, even if the window
1095 * has focus already. useful if you move the pointer from a focused
1096 * window to the root window and back pretty fast
1098 * set focus if in focus-follows-mouse mode and the event
1099 * is for the frame window and window doesn't have focus yet */
1100 if (wPreferences.focus_mode == WKF_SLOPPY
1101 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1103 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1104 wSetFocusTo(scr, wwin);
1106 if (scr->autoRaiseTimer)
1107 WMDeleteTimerHandler(scr->autoRaiseTimer);
1108 scr->autoRaiseTimer = NULL;
1110 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1111 scr->autoRaiseWindow = wwin->frame->core->window;
1112 scr->autoRaiseTimer
1113 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1116 /* Install colormap for window, if the colormap installation mode
1117 * is colormap_follows_mouse */
1118 if (wPreferences.colormap_mode == WCM_POINTER) {
1119 if (wwin->client_win == event->xcrossing.window)
1120 wColormapInstallForWindow(scr, wwin);
1121 else
1122 wColormapInstallForWindow(scr, NULL);
1126 if (event->xcrossing.window == event->xcrossing.root
1127 && event->xcrossing.detail == NotifyNormal
1128 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1130 wSetFocusTo(scr, scr->focused_window);
1132 #ifdef BALLOON_TEXT
1133 wBalloonEnteredObject(scr, desc);
1134 #endif
1137 static void handleLeaveNotify(XEvent * event)
1139 WObjDescriptor *desc = NULL;
1141 if (XFindContext(dpy, event->xcrossing.window, w_global.context.client_win, (XPointer *) & desc) != XCNOENT) {
1142 if (desc->handle_leavenotify)
1143 (*desc->handle_leavenotify) (desc, event);
1147 #ifdef USE_XSHAPE
1148 static void handleShapeNotify(XEvent * event)
1150 XShapeEvent *shev = (XShapeEvent *) event;
1151 WWindow *wwin;
1152 union {
1153 XEvent xevent;
1154 XShapeEvent xshape;
1155 } ev;
1157 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1158 if (ev.xshape.kind == ShapeBounding) {
1159 if (ev.xshape.shaped == shev->shaped) {
1160 *shev = ev.xshape;
1161 } else {
1162 XPutBackEvent(dpy, &ev.xevent);
1163 break;
1168 wwin = wWindowFor(shev->window);
1169 if (!wwin || shev->kind != ShapeBounding)
1170 return;
1172 if (!shev->shaped && wwin->flags.shaped) {
1174 wwin->flags.shaped = 0;
1175 wWindowClearShape(wwin);
1177 } else if (shev->shaped) {
1179 wwin->flags.shaped = 1;
1180 wWindowSetShape(wwin);
1183 #endif /* USE_XSHAPE */
1185 #ifdef KEEP_XKB_LOCK_STATUS
1186 /* please help ]d if you know what to do */
1187 static void handleXkbIndicatorStateNotify(XkbEvent *event)
1189 WWindow *wwin;
1190 WScreen *scr;
1191 XkbStateRec staterec;
1192 int i;
1194 for (i = 0; i < w_global.screen_count; i++) {
1195 scr = wScreenWithNumber(i);
1196 wwin = scr->focused_window;
1197 if (wwin && wwin->flags.focused) {
1198 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1199 if (wwin->frame->languagemode != staterec.group) {
1200 wwin->frame->last_languagemode = wwin->frame->languagemode;
1201 wwin->frame->languagemode = staterec.group;
1203 #ifdef XKB_BUTTON_HINT
1204 if (wwin->frame->titlebar) {
1205 wFrameWindowPaint(wwin->frame);
1207 #endif
1211 #endif /*KEEP_XKB_LOCK_STATUS */
1213 static void handleColormapNotify(XEvent * event)
1215 WWindow *wwin;
1216 WScreen *scr;
1217 Bool reinstall = False;
1219 wwin = wWindowFor(event->xcolormap.window);
1220 if (!wwin)
1221 return;
1223 scr = wwin->screen_ptr;
1225 do {
1226 if (wwin) {
1227 if (event->xcolormap.new) {
1228 XWindowAttributes attr;
1230 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1232 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1233 scr->current_colormap = attr.colormap;
1235 reinstall = True;
1236 } else if (event->xcolormap.state == ColormapUninstalled &&
1237 scr->current_colormap == event->xcolormap.colormap) {
1239 /* some bastard app (like XV) removed our colormap */
1241 * can't enforce or things like xscreensaver wont work
1242 * reinstall = True;
1244 } else if (event->xcolormap.state == ColormapInstalled &&
1245 scr->current_colormap == event->xcolormap.colormap) {
1247 /* someone has put our colormap back */
1248 reinstall = False;
1251 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1252 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1254 if (reinstall && scr->current_colormap != None) {
1255 if (!scr->flags.colormap_stuff_blocked)
1256 XInstallColormap(dpy, scr->current_colormap);
1260 static void handleFocusIn(XEvent * event)
1262 WWindow *wwin;
1265 * For applications that like stealing the focus.
1267 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1268 saveTimestamp(event);
1269 if (event->xfocus.mode == NotifyUngrab
1270 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1271 return;
1274 wwin = wWindowFor(event->xfocus.window);
1275 if (wwin && !wwin->flags.focused) {
1276 if (wwin->flags.mapped)
1277 wSetFocusTo(wwin->screen_ptr, wwin);
1278 else
1279 wSetFocusTo(wwin->screen_ptr, NULL);
1280 } else if (!wwin) {
1281 WScreen *scr = wScreenForWindow(event->xfocus.window);
1282 if (scr)
1283 wSetFocusTo(scr, NULL);
1287 static WWindow *windowUnderPointer(WScreen * scr)
1289 unsigned int mask;
1290 int foo;
1291 Window bar, win;
1293 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1294 return wWindowFor(win);
1295 return NULL;
1298 static int CheckFullScreenWindowFocused(WScreen * scr)
1300 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1301 return 1;
1302 else
1303 return 0;
1306 static void handleKeyPress(XEvent * event)
1308 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1309 WWindow *wwin = scr->focused_window;
1310 short i, widx;
1311 int modifiers;
1312 int command = -1;
1313 #ifdef KEEP_XKB_LOCK_STATUS
1314 XkbStateRec staterec;
1315 #endif /*KEEP_XKB_LOCK_STATUS */
1317 /* ignore CapsLock */
1318 modifiers = event->xkey.state & w_global.shortcut.modifiers_mask;
1320 for (i = 0; i < WKBD_LAST; i++) {
1321 if (wKeyBindings[i].keycode == 0)
1322 continue;
1324 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1325 || */ wKeyBindings[i].modifier ==
1326 modifiers)) {
1327 command = i;
1328 break;
1332 if (command < 0) {
1334 if (!wRootMenuPerformShortcut(event)) {
1335 static int dontLoop = 0;
1337 if (dontLoop > 10) {
1338 wwarning("problem with key event processing code");
1339 return;
1341 dontLoop++;
1342 /* if the focused window is an internal window, try redispatching
1343 * the event to the managed window, as it can be a WINGs window */
1344 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1345 /* client_leader contains the WINGs toplevel */
1346 event->xany.window = wwin->client_leader;
1347 WMHandleEvent(event);
1349 dontLoop--;
1351 return;
1353 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1354 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1356 switch (command) {
1358 case WKBD_ROOTMENU:
1359 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1360 if (!CheckFullScreenWindowFocused(scr)) {
1361 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1362 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1363 True);
1365 break;
1366 case WKBD_WINDOWLIST:
1367 if (!CheckFullScreenWindowFocused(scr)) {
1368 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1369 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1370 True);
1372 break;
1374 case WKBD_WINDOWMENU:
1375 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1376 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1377 break;
1378 case WKBD_MINIMIZEALL:
1379 CloseWindowMenu(scr);
1380 wHideAll(scr);
1381 break;
1382 case WKBD_MINIATURIZE:
1383 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1384 && !WFLAGP(wwin, no_miniaturizable)) {
1385 CloseWindowMenu(scr);
1387 if (wwin->protocols.MINIATURIZE_WINDOW)
1388 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window, event->xbutton.time);
1389 else {
1390 wIconifyWindow(wwin);
1393 break;
1394 case WKBD_HIDE:
1395 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1396 WApplication *wapp = wApplicationOf(wwin->main_window);
1397 CloseWindowMenu(scr);
1399 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1400 wHideApplication(wapp);
1403 break;
1404 case WKBD_HIDE_OTHERS:
1405 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1406 CloseWindowMenu(scr);
1408 wHideOtherApplications(wwin);
1410 break;
1411 case WKBD_MAXIMIZE:
1412 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1413 CloseWindowMenu(scr);
1415 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1417 break;
1418 case WKBD_VMAXIMIZE:
1419 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1420 CloseWindowMenu(scr);
1422 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1424 break;
1425 case WKBD_HMAXIMIZE:
1426 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1427 CloseWindowMenu(scr);
1429 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1431 break;
1432 case WKBD_LHMAXIMIZE:
1433 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1434 CloseWindowMenu(scr);
1436 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1438 break;
1439 case WKBD_RHMAXIMIZE:
1440 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1441 CloseWindowMenu(scr);
1443 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1445 break;
1446 case WKBD_THMAXIMIZE:
1447 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1448 CloseWindowMenu(scr);
1450 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1452 break;
1453 case WKBD_BHMAXIMIZE:
1454 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1455 CloseWindowMenu(scr);
1457 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1459 break;
1460 case WKBD_LTCMAXIMIZE:
1461 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1462 CloseWindowMenu(scr);
1464 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1466 break;
1467 case WKBD_RTCMAXIMIZE:
1468 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1469 CloseWindowMenu(scr);
1471 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1473 break;
1474 case WKBD_LBCMAXIMIZE:
1475 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1476 CloseWindowMenu(scr);
1478 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1480 break;
1481 case WKBD_RBCMAXIMIZE:
1482 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1483 CloseWindowMenu(scr);
1485 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1487 break;
1488 case WKBD_MAXIMUS:
1489 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1490 CloseWindowMenu(scr);
1492 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1494 break;
1495 case WKBD_RAISE:
1496 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1497 CloseWindowMenu(scr);
1499 wRaiseFrame(wwin->frame->core);
1501 break;
1502 case WKBD_LOWER:
1503 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1504 CloseWindowMenu(scr);
1506 wLowerFrame(wwin->frame->core);
1508 break;
1509 case WKBD_RAISELOWER:
1510 /* raise or lower the window under the pointer, not the
1511 * focused one
1513 wwin = windowUnderPointer(scr);
1514 if (wwin)
1515 wRaiseLowerFrame(wwin->frame->core);
1516 break;
1517 case WKBD_SHADE:
1518 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1519 if (wwin->flags.shaded)
1520 wUnshadeWindow(wwin);
1521 else
1522 wShadeWindow(wwin);
1524 break;
1525 case WKBD_MOVERESIZE:
1526 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1527 CloseWindowMenu(scr);
1529 wKeyboardMoveResizeWindow(wwin);
1531 break;
1532 case WKBD_CLOSE:
1533 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1534 CloseWindowMenu(scr);
1535 if (wwin->protocols.DELETE_WINDOW)
1536 wClientSendProtocol(wwin, w_global.atom.wm.delete_window, event->xkey.time);
1538 break;
1539 case WKBD_SELECT:
1540 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1541 wSelectWindow(wwin, !wwin->flags.selected);
1543 break;
1544 case WKBD_FOCUSNEXT:
1545 StartWindozeCycle(wwin, event, True, False);
1546 break;
1548 case WKBD_FOCUSPREV:
1549 StartWindozeCycle(wwin, event, False, False);
1550 break;
1552 case WKBD_GROUPNEXT:
1553 StartWindozeCycle(wwin, event, True, True);
1554 break;
1556 case WKBD_GROUPPREV:
1557 StartWindozeCycle(wwin, event, False, True);
1558 break;
1560 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1561 widx = command - WKBD_WORKSPACE1;
1562 i = (w_global.workspace.current / 10) * 10 + widx;
1563 if (wPreferences.ws_advance || i < w_global.workspace.count)
1564 wWorkspaceChange(scr, i);
1565 break;
1567 case WKBD_NEXTWORKSPACE:
1568 wWorkspaceRelativeChange(scr, 1);
1569 break;
1570 case WKBD_PREVWORKSPACE:
1571 wWorkspaceRelativeChange(scr, -1);
1572 break;
1573 case WKBD_LASTWORKSPACE:
1574 wWorkspaceChange(scr, w_global.workspace.last_used);
1575 break;
1577 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1578 widx = command - WKBD_MOVE_WORKSPACE1;
1579 i = (w_global.workspace.current / 10) * 10 + widx;
1580 if (wwin && (wPreferences.ws_advance || i < w_global.workspace.count))
1581 wWindowChangeWorkspace(wwin, i);
1582 break;
1584 case WKBD_MOVE_NEXTWORKSPACE:
1585 if (wwin)
1586 wWindowChangeWorkspaceRelative(wwin, 1);
1587 break;
1588 case WKBD_MOVE_PREVWORKSPACE:
1589 if (wwin)
1590 wWindowChangeWorkspaceRelative(wwin, -1);
1591 break;
1592 case WKBD_MOVE_LASTWORKSPACE:
1593 if (wwin)
1594 wWindowChangeWorkspace(wwin, w_global.workspace.last_used);
1595 break;
1597 case WKBD_MOVE_NEXTWSLAYER:
1598 case WKBD_MOVE_PREVWSLAYER:
1600 if (wwin) {
1601 int row, column;
1603 row = w_global.workspace.current / 10;
1604 column = w_global.workspace.current % 10;
1606 if (command == WKBD_MOVE_NEXTWSLAYER) {
1607 if ((row + 1) * 10 < w_global.workspace.count)
1608 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1609 } else {
1610 if (row > 0)
1611 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1615 break;
1617 case WKBD_WINDOW1:
1618 case WKBD_WINDOW2:
1619 case WKBD_WINDOW3:
1620 case WKBD_WINDOW4:
1621 case WKBD_WINDOW5:
1622 case WKBD_WINDOW6:
1623 case WKBD_WINDOW7:
1624 case WKBD_WINDOW8:
1625 case WKBD_WINDOW9:
1626 case WKBD_WINDOW10:
1628 widx = command - WKBD_WINDOW1;
1630 if (w_global.shortcut.windows[widx]) {
1631 WMArray *list = w_global.shortcut.windows[widx];
1632 int cw;
1633 int count = WMGetArrayItemCount(list);
1634 WWindow *twin;
1635 WMArrayIterator iter;
1636 WWindow *wwin;
1638 wUnselectWindows(scr);
1639 cw = w_global.workspace.current;
1641 WM_ETARETI_ARRAY(list, wwin, iter) {
1642 if (count > 1)
1643 wWindowChangeWorkspace(wwin, cw);
1645 wMakeWindowVisible(wwin);
1647 if (count > 1)
1648 wSelectWindow(wwin, True);
1651 /* rotate the order of windows, to create a cycling effect */
1652 twin = WMGetFromArray(list, 0);
1653 WMDeleteFromArray(list, 0);
1654 WMAddToArray(list, twin);
1656 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1657 if (w_global.shortcut.windows[widx]) {
1658 WMFreeArray(w_global.shortcut.windows[widx]);
1659 w_global.shortcut.windows[widx] = NULL;
1662 if (wwin->flags.selected && scr->selected_windows) {
1663 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1664 } else {
1665 w_global.shortcut.windows[widx] = WMCreateArray(4);
1666 WMAddToArray(w_global.shortcut.windows[widx], wwin);
1669 wSelectWindow(wwin, !wwin->flags.selected);
1670 XFlush(dpy);
1671 wusleep(3000);
1672 wSelectWindow(wwin, !wwin->flags.selected);
1673 XFlush(dpy);
1675 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1676 if (wwin->flags.selected && scr->selected_windows) {
1677 if (w_global.shortcut.windows[widx])
1678 WMFreeArray(w_global.shortcut.windows[widx]);
1680 w_global.shortcut.windows[widx] = WMDuplicateArray(scr->selected_windows);
1684 break;
1686 case WKBD_RELAUNCH:
1687 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1688 (void) RelaunchWindow(wwin);
1690 break;
1692 case WKBD_SWITCH_SCREEN:
1693 if (w_global.screen_count > 1) {
1694 WScreen *scr2;
1695 int i;
1697 /* find index of this screen */
1698 for (i = 0; i < w_global.screen_count; i++) {
1699 if (wScreenWithNumber(i) == scr)
1700 break;
1702 i++;
1703 if (i >= w_global.screen_count) {
1704 i = 0;
1706 scr2 = wScreenWithNumber(i);
1708 if (scr2) {
1709 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1710 scr2->scr_width / 2, scr2->scr_height / 2);
1713 break;
1715 case WKBD_RUN:
1717 char *cmdline;
1719 cmdline = ExpandOptions(scr, _("exec %a(Run,Type command to run:)"));
1721 if (cmdline) {
1722 XGrabPointer(dpy, scr->root_win, True, 0,
1723 GrabModeAsync, GrabModeAsync, None, wPreferences.cursor[WCUR_WAIT], CurrentTime);
1724 XSync(dpy, False);
1726 ExecuteShellCommand(scr, cmdline);
1727 wfree(cmdline);
1729 XUngrabPointer(dpy, CurrentTime);
1730 XSync(dpy, False);
1732 break;
1735 case WKBD_NEXTWSLAYER:
1736 case WKBD_PREVWSLAYER:
1738 int row, column;
1740 row = w_global.workspace.current / 10;
1741 column = w_global.workspace.current % 10;
1743 if (command == WKBD_NEXTWSLAYER) {
1744 if ((row + 1) * 10 < w_global.workspace.count)
1745 wWorkspaceChange(scr, column + (row + 1) * 10);
1746 } else {
1747 if (row > 0)
1748 wWorkspaceChange(scr, column + (row - 1) * 10);
1751 break;
1752 case WKBD_CLIPRAISELOWER:
1753 if (!wPreferences.flags.noclip)
1754 wDockRaiseLower(w_global.workspace.array[w_global.workspace.current]->clip);
1755 break;
1756 case WKBD_DOCKRAISELOWER:
1757 if (!wPreferences.flags.nodock)
1758 wDockRaiseLower(scr->dock);
1759 break;
1760 #ifdef KEEP_XKB_LOCK_STATUS
1761 case WKBD_TOGGLE:
1762 if (wPreferences.modelock) {
1763 /*toggle */
1764 wwin = scr->focused_window;
1766 if (wwin && wwin->flags.mapped
1767 && wwin->frame->workspace == w_global.workspace.current
1768 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1769 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1771 wwin->frame->languagemode = wwin->frame->last_languagemode;
1772 wwin->frame->last_languagemode = staterec.group;
1773 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1777 break;
1778 #endif /* KEEP_XKB_LOCK_STATUS */
1782 static void handleMotionNotify(XEvent * event)
1784 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1786 if (wPreferences.scrollable_menus) {
1787 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1788 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1790 if (scr->flags.jump_back_pending ||
1791 p.x <= (rect.pos.x + 1) ||
1792 p.x >= (rect.pos.x + rect.size.width - 2) ||
1793 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1794 WMenu *menu;
1796 menu = wMenuUnderPointer(scr);
1797 if (menu != NULL)
1798 wMenuScroll(menu);
1803 static void handleVisibilityNotify(XEvent * event)
1805 WWindow *wwin;
1807 wwin = wWindowFor(event->xvisibility.window);
1808 if (!wwin)
1809 return;
1810 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);