Update for 0.51.0
[wmaker-crm.git] / src / event.c
blob92b06372bcbde35982912e9c4af1921dc84f3220
1 /* event.c- event loop and handling
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "wconfig.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
32 #include <X11/Xlib.h>
33 #include <X11/Xutil.h>
34 #ifdef SHAPE
35 #include <X11/extensions/shape.h>
36 #endif
37 #ifdef XDE_DND
38 #include "xde.h"
39 #endif
41 #ifdef KEEP_XKB_LOCK_STATUS
42 #include <X11/XKBlib.h>
43 #endif /* KEEP_XKB_LOCK_STATUS */
45 #include "WindowMaker.h"
46 #include "window.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "application.h"
52 #include "stacking.h"
53 #include "defaults.h"
54 #include "workspace.h"
55 #include "dock.h"
56 #include "framewin.h"
57 #include "properties.h"
58 #include "balloon.h"
59 #ifdef GNOME_STUFF
60 # include "gnome.h"
61 #endif
62 #ifdef KWM_HINTS
63 # include "kwm.h"
64 #endif
66 /******** Global Variables **********/
67 extern XContext wWinContext;
69 extern Cursor wCursor[WCUR_LAST];
71 extern WShortKey wKeyBindings[WKBD_LAST];
72 extern int wScreenCount;
73 extern Time LastTimestamp;
74 extern Time LastFocusChange;
76 extern WPreferences wPreferences;
78 #define MOD_MASK wPreferences.modifier_mask
80 extern Atom _XA_WM_COLORMAP_NOTIFY;
82 extern Atom _XA_WM_CHANGE_STATE;
83 extern Atom _XA_WM_DELETE_WINDOW;
84 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
85 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
87 #ifdef OFFIX_DND
88 extern Atom _XA_DND_PROTOCOL;
89 #endif
92 #ifdef SHAPE
93 extern Bool wShapeSupported;
94 extern int wShapeEventBase;
95 #endif
97 /* special flags */
98 extern char WProgramState;
99 extern char WDelayedActionSet;
102 /************ Local stuff ***********/
105 static void saveTimestamp(XEvent *event);
106 static void handleColormapNotify();
107 static void handleMapNotify(), handleUnmapNotify();
108 static void handleButtonPress(), handleExpose();
109 static void handleDestroyNotify();
110 static void handleConfigureRequest();
111 static void handleMapRequest();
112 static void handlePropertyNotify();
113 static void handleEnterNotify();
114 static void handleLeaveNotify();
115 static void handleExtensions();
116 static void handleClientMessage();
117 static void handleKeyPress();
118 static void handleFocusIn();
119 static void handleMotionNotify();
122 #ifdef SHAPE
123 static void handleShapeNotify();
124 #endif
126 /* called from the signal handler */
127 void NotifyDeadProcess(pid_t pid, unsigned char status);
129 /* real dead process handler */
130 static void handleDeadProcess(void *foo);
133 typedef struct DeadProcesses {
134 pid_t pid;
135 unsigned char exit_status;
136 } DeadProcesses;
138 /* stack of dead processes */
139 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
140 static int deadProcessPtr=0;
143 typedef struct DeathHandler {
144 WDeathHandler *callback;
145 pid_t pid;
146 struct DeathHandler *next;
147 void *client_data;
148 } DeathHandler;
150 static DeathHandler *deathHandler=NULL;
154 WMagicNumber
155 wAddDeathHandler(pid_t pid, WDeathHandler *callback, void *cdata)
157 DeathHandler *handler;
159 handler = malloc(sizeof(DeathHandler));
160 if (!handler)
161 return 0;
163 handler->pid = pid;
164 handler->callback = callback;
165 handler->client_data = cdata;
167 handler->next = deathHandler;
169 deathHandler = handler;
171 return handler;
176 void
177 wDeleteDeathHandler(WMagicNumber id)
179 DeathHandler *tmp, *handler=(DeathHandler*)id;
181 if (!handler || !deathHandler)
182 return;
184 tmp = deathHandler;
185 if (tmp==handler) {
186 deathHandler = handler->next;
187 free(handler);
188 } else {
189 while (tmp->next) {
190 if (tmp->next==handler) {
191 tmp->next=handler->next;
192 free(handler);
193 break;
195 tmp = tmp->next;
201 void
202 DispatchEvent(XEvent *event)
204 if (deathHandler)
205 handleDeadProcess(NULL);
207 if (WProgramState==WSTATE_NEED_EXIT) {
208 WProgramState = WSTATE_EXITING;
209 /* received SIGTERM */
211 * WMHandleEvent() can't be called from anything
212 * executed inside here, or we can get in a infinite
213 * recursive loop.
215 Shutdown(WSExitMode);
217 } else if (WProgramState == WSTATE_NEED_RESTART) {
218 WProgramState = WSTATE_RESTARTING;
220 Shutdown(WSRestartPreparationMode);
221 /* received SIGHUP */
222 Restart(NULL);
225 /* for the case that all that is wanted to be dispatched is
226 * the stuff above */
227 if (!event)
228 return;
230 saveTimestamp(event);
231 switch (event->type) {
232 case MapRequest:
233 handleMapRequest(event);
234 break;
236 case KeyPress:
237 handleKeyPress(event);
238 break;
240 case MotionNotify:
241 handleMotionNotify(event);
242 break;
244 case ConfigureRequest:
245 handleConfigureRequest(event);
246 break;
248 case DestroyNotify:
249 handleDestroyNotify(event);
250 break;
252 case MapNotify:
253 handleMapNotify(event->xmap.window);
254 break;
256 case UnmapNotify:
257 handleUnmapNotify(event);
258 break;
260 case ButtonPress:
261 handleButtonPress(event);
262 break;
264 case Expose:
265 handleExpose(event);
266 break;
268 case PropertyNotify:
269 handlePropertyNotify(event);
270 break;
272 case EnterNotify:
273 handleEnterNotify(event);
274 break;
276 case LeaveNotify:
277 handleLeaveNotify(event);
278 break;
280 case ClientMessage:
281 handleClientMessage(event);
282 break;
284 case ColormapNotify:
285 handleColormapNotify(event);
286 break;
288 case MappingNotify:
289 if (event->xmapping.request == MappingKeyboard
290 || event->xmapping.request == MappingModifier)
291 XRefreshKeyboardMapping(&event->xmapping);
292 break;
294 case FocusIn:
295 handleFocusIn(event);
296 break;
298 default:
299 handleExtensions(event);
300 break;
306 *----------------------------------------------------------------------
307 * EventLoop-
308 * Processes X and internal events indefinitely.
310 * Returns:
311 * Never returns
313 * Side effects:
314 * The LastTimestamp global variable is updated.
315 *----------------------------------------------------------------------
317 void
318 EventLoop()
320 XEvent event;
322 for(;;) {
323 WMNextEvent(dpy, &event);
324 WMHandleEvent(&event);
330 Bool
331 IsDoubleClick(WScreen *scr, XEvent *event)
333 if ((scr->last_click_time>0) &&
334 (event->xbutton.time-scr->last_click_time<=wPreferences.dblclick_time)
335 && (event->xbutton.button == scr->last_click_button)
336 && (event->xbutton.subwindow == scr->last_click_window)) {
338 scr->flags.next_click_is_not_double = 1;
339 scr->last_click_time = 0;
340 scr->last_click_window = None;
342 return True;
344 return False;
348 void
349 NotifyDeadProcess(pid_t pid, unsigned char status)
351 if (deadProcessPtr>=MAX_DEAD_PROCESSES-1) {
352 wwarning(_("stack overflow: too many dead processes"));
353 return;
355 /* stack the process to be handled later,
356 * as this is called from the signal handler */
357 deadProcesses[deadProcessPtr].pid = pid;
358 deadProcesses[deadProcessPtr].exit_status = status;
359 deadProcessPtr++;
363 static void
364 handleDeadProcess(void *foo)
366 DeathHandler *tmp;
367 int i;
369 for (i=0; i<deadProcessPtr; i++) {
370 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
373 if (!deathHandler) {
374 deadProcessPtr=0;
375 return;
378 /* get the pids on the queue and call handlers */
379 while (deadProcessPtr>0) {
380 deadProcessPtr--;
382 tmp = deathHandler;
383 while (tmp) {
384 DeathHandler *t;
386 t = tmp->next;
388 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
389 (*tmp->callback)(tmp->pid,
390 deadProcesses[deadProcessPtr].exit_status,
391 tmp->client_data);
392 wDeleteDeathHandler(tmp);
394 tmp = t;
400 static void
401 saveTimestamp(XEvent *event)
403 LastTimestamp = CurrentTime;
405 switch (event->type) {
406 case ButtonRelease:
407 case ButtonPress:
408 LastTimestamp = event->xbutton.time;
409 break;
410 case KeyPress:
411 case KeyRelease:
412 LastTimestamp = event->xkey.time;
413 break;
414 case MotionNotify:
415 LastTimestamp = event->xmotion.time;
416 break;
417 case PropertyNotify:
418 LastTimestamp = event->xproperty.time;
419 break;
420 case EnterNotify:
421 case LeaveNotify:
422 LastTimestamp = event->xcrossing.time;
423 break;
424 case SelectionClear:
425 LastTimestamp = event->xselectionclear.time;
426 break;
427 case SelectionRequest:
428 LastTimestamp = event->xselectionrequest.time;
429 break;
430 case SelectionNotify:
431 LastTimestamp = event->xselection.time;
432 break;
437 static void
438 handleExtensions(XEvent *event)
440 #ifdef SHAPE
441 if (wShapeSupported && event->type == (wShapeEventBase+ShapeNotify)) {
442 handleShapeNotify(event);
444 #endif
448 static void
449 handleMapRequest(XEvent *ev)
451 WWindow *wwin;
452 WScreen *scr = NULL;
453 Window window = ev->xmaprequest.window;
455 #ifdef DEBUG
456 printf("got map request for %x\n", (unsigned)window);
457 #endif
459 if ((wwin=wWindowFor(window))) {
460 if (wwin->flags.shaded) {
461 wUnshadeWindow(wwin);
463 /* deiconify window */
464 if (wwin->flags.miniaturized) {
465 wDeiconifyWindow(wwin);
466 } else if (wwin->flags.hidden) {
467 WApplication *wapp = wApplicationOf(wwin->main_window);
468 /* go to the last workspace that the user worked on the app */
469 #ifndef REDUCE_APPICONS
470 /* This severely breaks REDUCE_APPICONS. last_workspace is a neat
471 * concept but it needs to be reworked to handle REDUCE_APPICONS -cls
473 if (wapp) {
474 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
476 #endif
477 wUnhideApplication(wapp, False, False);
479 return;
482 scr = wScreenForRootWindow(ev->xmaprequest.parent);
484 wwin = wManageWindow(scr, window);
487 * This is to let the Dock know that the application it launched
488 * has already been mapped (eg: it has finished launching).
489 * It is not necessary for normally docked apps, but is needed for
490 * apps that were forcedly docked (like with dockit).
492 if (scr->last_dock) {
493 if (wwin && wwin->main_window!=None && wwin->main_window!=window)
494 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
495 else
496 wDockTrackWindowLaunch(scr->last_dock, window);
500 if (wwin) {
501 int state;
503 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
504 state = wwin->wm_hints->initial_state;
505 else
506 state = NormalState;
508 if (state==IconicState)
509 wwin->flags.miniaturized = 1;
511 if (state == WithdrawnState) {
512 wwin->flags.mapped = 0;
513 wClientSetState(wwin, WithdrawnState, None);
514 wUnmanageWindow(wwin, True, False);
515 } else {
516 wClientSetState(wwin, NormalState, None);
517 if (wwin->flags.maximized) {
518 wMaximizeWindow(wwin, wwin->flags.maximized);
520 if (wwin->flags.shaded) {
521 wwin->flags.shaded = 0;
522 wwin->flags.skip_next_animation = 1;
523 wwin->flags.ignore_next_unmap = 1; /* ??? */
524 wShadeWindow(wwin);
526 if (wwin->flags.miniaturized) {
527 wwin->flags.miniaturized = 0;
528 wwin->flags.hidden = 0;
529 wwin->flags.skip_next_animation = 1;
530 wwin->flags.ignore_next_unmap = 1;
531 wIconifyWindow(wwin);
532 } else if (wwin->flags.hidden) {
533 WApplication *wapp = wApplicationOf(wwin->main_window);
534 wwin->flags.hidden = 0;
535 wwin->flags.skip_next_animation = 1;
536 if (wapp) {
537 wHideApplication(wapp);
539 wwin->flags.ignore_next_unmap = 1;
546 static void
547 handleDestroyNotify(XEvent *event)
549 WWindow *wwin;
550 WApplication *app;
551 Window window = event->xdestroywindow.window;
553 #ifdef DEBUG
554 puts("got destroy notify");
555 #endif
557 wwin = wWindowFor(window);
558 if (wwin) {
559 wUnmanageWindow(wwin, False, True);
562 app = wApplicationOf(window);
563 if (app) {
564 if (window == app->main_window) {
565 app->refcount = 0;
566 wwin = app->main_window_desc->screen_ptr->focused_window;
567 while (wwin) {
568 if (wwin->main_window == window) {
569 wwin->main_window = None;
571 wwin = wwin->prev;
574 wApplicationDestroy(app);
577 #ifdef KWM_HINTS
578 wKWMCheckDestroy(&event->xdestroywindow);
579 #endif
584 static void
585 handleExpose(XEvent *event)
587 WObjDescriptor *desc;
589 #ifdef DEBUG
590 puts("got expose");
591 #endif
592 if (event->xexpose.count!=0) {
593 return;
596 if (XFindContext(dpy, event->xexpose.window, wWinContext,
597 (XPointer *)&desc)==XCNOENT) {
598 return;
601 if (desc->handle_expose) {
602 (*desc->handle_expose)(desc, event);
607 /* bindable */
608 static void
609 handleButtonPress(XEvent *event)
611 WObjDescriptor *desc;
612 WScreen *scr;
614 #ifdef DEBUG
615 puts("got button press");
616 #endif
618 scr = wScreenForRootWindow(event->xbutton.root);
620 #ifdef BALLOON_TEXT
621 wBalloonHide(scr);
622 #endif
625 #ifndef LITE
626 if (event->xbutton.window==scr->root_win) {
628 #ifdef GNOME_STUFF
629 if (wGNOMEProxyizeButtonEvent(scr, event))
630 return;
631 #endif
633 if (event->xbutton.button==wPreferences.menu_button) {
634 OpenRootMenu(scr, event->xbutton.x_root,
635 event->xbutton.y_root, False);
636 /* ugly hack */
637 if (scr->root_menu) {
638 if (scr->root_menu->brother->flags.mapped)
639 event->xbutton.window = scr->root_menu->brother->frame->core->window;
640 else
641 event->xbutton.window = scr->root_menu->frame->core->window;
643 } else if (event->xbutton.button==wPreferences.windowl_button) {
644 OpenSwitchMenu(scr, event->xbutton.x_root,
645 event->xbutton.y_root, False);
646 if (scr->switch_menu) {
647 if (scr->switch_menu->brother->flags.mapped)
648 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
649 else
650 event->xbutton.window = scr->switch_menu->frame->core->window;
652 } else if (event->xbutton.button==wPreferences.select_button) {
653 wUnselectWindows(scr);
654 wSelectWindows(scr, event);
656 #ifdef MOUSE_WS_SWITCH
657 else if (event->xbutton.button==Button4) {
659 wWorkspaceRelativeChange(scr, -1);
661 } else if (event->xbutton.button==Button5) {
663 wWorkspaceRelativeChange(scr, 1);
666 #endif /* MOUSE_WS_SWITCH */
668 #endif /* !LITE */
670 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext,
671 (XPointer *)&desc)==XCNOENT) {
672 if (XFindContext(dpy, event->xbutton.window, wWinContext,
673 (XPointer *)&desc)==XCNOENT) {
674 return;
678 if (desc->parent_type == WCLASS_WINDOW) {
679 XSync(dpy, 0);
681 if (event->xbutton.state & MOD_MASK) {
682 XAllowEvents(dpy, AsyncPointer, CurrentTime);
685 if (wPreferences.focus_mode == WKF_CLICK) {
686 if (wPreferences.ignore_focus_click) {
687 XAllowEvents(dpy, AsyncPointer, CurrentTime);
689 XAllowEvents(dpy, ReplayPointer, CurrentTime);
691 XSync(dpy, 0);
692 } else if (desc->parent_type == WCLASS_APPICON
693 || desc->parent_type == WCLASS_MINIWINDOW
694 || desc->parent_type == WCLASS_DOCK_ICON) {
695 if (event->xbutton.state & MOD_MASK) {
696 XSync(dpy, 0);
697 XAllowEvents(dpy, AsyncPointer, CurrentTime);
698 XSync(dpy, 0);
702 if (desc->handle_mousedown!=NULL) {
703 (*desc->handle_mousedown)(desc, event);
706 /* save double-click information */
707 if (scr->flags.next_click_is_not_double) {
708 scr->flags.next_click_is_not_double = 0;
709 } else {
710 scr->last_click_time = event->xbutton.time;
711 scr->last_click_button = event->xbutton.button;
712 scr->last_click_window = event->xbutton.subwindow;
717 static void
718 handleMapNotify(Window window)
720 WWindow *wwin;
722 #ifdef DEBUG
723 puts("got map");
724 #endif
725 wwin= wWindowFor(window);
726 if (wwin && wwin->client_win==window) {
727 if (wwin->flags.ignore_next_unmap) {
728 wwin->flags.ignore_next_unmap=0;
729 return;
731 if (wwin->flags.miniaturized) {
732 wDeiconifyWindow(wwin);
733 } else {
734 XGrabServer(dpy);
735 XSync(dpy,0);
736 XMapWindow(dpy, wwin->client_win);
737 XMapWindow(dpy, wwin->frame->core->window);
738 wwin->flags.mapped=1;
739 wClientSetState(wwin, NormalState, None);
740 XUngrabServer(dpy);
746 static void
747 handleUnmapNotify(XEvent *event)
749 WWindow *wwin;
750 XEvent ev;
752 #ifdef DEBUG
753 puts("got unmap");
754 #endif
755 wwin = wWindowFor(event->xunmap.window);
756 if (!wwin || wwin->client_win!=event->xunmap.window)
757 return;
759 if (!wwin->flags.mapped
760 && wwin->frame->workspace==wwin->screen_ptr->current_workspace
761 && !wwin->flags.miniaturized && !wwin->flags.hidden)
762 return;
764 if (wwin->flags.ignore_next_unmap) {
765 return;
767 XGrabServer(dpy);
768 XUnmapWindow(dpy, wwin->frame->core->window);
769 wwin->flags.mapped = 0;
770 XSync(dpy, 0);
771 /* check if the window was destroyed */
772 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify,&ev)) {
773 DispatchEvent(&ev);
774 } else {
775 Bool reparented = False;
777 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
778 reparented = True;
780 /* withdraw window */
781 wwin->flags.mapped = 0;
782 if (!reparented)
783 wClientSetState(wwin, WithdrawnState, None);
785 /* if the window was reparented, do not reparent it back to the
786 * root window */
787 wUnmanageWindow(wwin, !reparented, False);
789 XUngrabServer(dpy);
793 static void
794 handleConfigureRequest(XEvent *event)
796 WWindow *wwin;
798 #ifdef DEBUG
799 puts("got configure request");
800 #endif
801 if (!(wwin=wWindowFor(event->xconfigurerequest.window))) {
803 * Configure request for unmapped window
805 wClientConfigure(NULL, &(event->xconfigurerequest));
806 } else {
807 wClientConfigure(wwin, &(event->xconfigurerequest));
812 static void
813 handlePropertyNotify(XEvent *event)
815 WWindow *wwin;
816 WApplication *wapp;
817 Window jr;
818 int ji;
819 unsigned int ju;
820 WScreen *scr;
822 #ifdef DEBUG
823 puts("got property notify");
824 #endif
825 if ((wwin=wWindowFor(event->xproperty.window))) {
826 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji,
827 &ju, &ju, &ju, &ju)) {
828 return;
830 wClientCheckProperty(wwin, &event->xproperty);
832 wapp = wApplicationOf(event->xproperty.window);
833 if (wapp) {
834 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
837 scr = wScreenForWindow(event->xproperty.window);
838 if (scr && scr->root_win == event->xproperty.window) {
839 #ifdef KWM_HINTS
840 wKWMCheckRootHintChange(scr, &event->xproperty);
841 #endif
846 static void
847 handleClientMessage(XEvent *event)
849 WWindow *wwin;
850 WObjDescriptor *desc;
852 #ifdef DEBUG
853 puts("got client message");
854 #endif
855 /* handle transition from Normal to Iconic state */
856 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
857 && event->xclient.format == 32
858 && event->xclient.data.l[0] == IconicState) {
860 wwin = wWindowFor(event->xclient.window);
861 if (!wwin) return;
862 if (!wwin->flags.miniaturized)
863 wIconifyWindow(wwin);
864 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY
865 && event->xclient.format == 32) {
866 WScreen *scr = wScreenForRootWindow(event->xclient.window);
868 if (!scr)
869 return;
871 if (event->xclient.data.l[1] == 1) { /* starting */
872 wColormapAllowClientInstallation(scr, True);
873 } else { /* stopping */
874 wColormapAllowClientInstallation(scr, False);
876 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
877 WApplication *wapp;
878 int done=0;
879 wapp = wApplicationOf(event->xclient.window);
880 if (wapp) {
881 switch (event->xclient.data.l[0]) {
882 case WMFHideOtherApplications:
883 wHideOtherApplications(wapp->main_window_desc);
884 done = 1;
885 break;
887 case WMFHideApplication:
888 wHideApplication(wapp);
889 done = 1;
890 break;
893 if (!done) {
894 wwin = wWindowFor(event->xclient.window);
895 if (wwin) {
896 switch (event->xclient.data.l[0]) {
897 case WMFHideOtherApplications:
898 wHideOtherApplications(wwin);
899 break;
901 case WMFHideApplication:
902 wHideApplication(wApplicationOf(wwin->main_window));
903 break;
907 #ifdef GNOME_STUFF
908 } else if (wGNOMEProcessClientMessage(&event->xclient)) {
909 /* do nothing */
910 #endif /* GNOME_STUFF */
911 #ifdef KWM_HINTS
912 } else if (wKWMProcessClientMessage(&event->xclient)) {
913 /* do nothing */
914 #endif /* KWM_HINTS */
915 #ifdef XDE_DND
916 } else if (wXDEProcessClientMessage(&event->xclient)) {
917 /* do nothing */
918 #endif /* XDE_DND */
919 #ifdef OFFIX_DND
920 } else if (event->xclient.message_type==_XA_DND_PROTOCOL) {
921 WScreen *scr = wScreenForWindow(event->xclient.window);
922 if (scr && wDockReceiveDNDDrop(scr,event))
923 goto redirect_message;
924 #endif /* OFFIX_DND */
925 } else {
926 #ifdef OFFIX_DND
927 redirect_message:
928 #endif
930 * Non-standard thing, but needed by OffiX DND.
931 * For when the icon frame gets a ClientMessage
932 * that should have gone to the icon_window.
934 if (XFindContext(dpy, event->xbutton.window, wWinContext,
935 (XPointer *)&desc)!=XCNOENT) {
936 struct WIcon *icon=NULL;
938 if (desc->parent_type == WCLASS_MINIWINDOW) {
939 icon = (WIcon*)desc->parent;
940 } else if (desc->parent_type == WCLASS_DOCK_ICON
941 || desc->parent_type == WCLASS_APPICON) {
942 icon = ((WAppIcon*)desc->parent)->icon;
944 if (icon && (wwin=icon->owner)) {
945 if (wwin->client_win!=event->xclient.window) {
946 event->xclient.window = wwin->client_win;
947 XSendEvent(dpy, wwin->client_win, False, NoEventMask,
948 event);
956 static void
957 raiseWindow(WScreen *scr)
959 WWindow *wwin;
961 scr->autoRaiseTimer = NULL;
963 wwin = wWindowFor(scr->autoRaiseWindow);
964 if (!wwin)
965 return;
967 if (!wwin->flags.destroyed) {
968 wRaiseFrame(wwin->frame->core);
969 /* this is needed or a race condition will occur */
970 XSync(dpy, False);
975 static void
976 handleEnterNotify(XEvent *event)
978 WWindow *wwin;
979 WObjDescriptor *desc = NULL;
980 XEvent ev;
981 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
984 #ifdef DEBUG
985 puts("got enter notify");
986 #endif
988 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify,
989 &ev)) {
990 /* already left the window... */
991 saveTimestamp(&ev);
992 if (ev.xcrossing.mode==event->xcrossing.mode
993 && ev.xcrossing.detail==event->xcrossing.detail) {
994 return;
998 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
999 (XPointer *)&desc)!=XCNOENT) {
1000 if(desc->handle_enternotify)
1001 (*desc->handle_enternotify)(desc, event);
1004 /* enter to window */
1005 wwin = wWindowFor(event->xcrossing.window);
1006 if (!wwin) {
1007 if (wPreferences.focus_mode==WKF_POINTER
1008 && event->xcrossing.window==event->xcrossing.root) {
1009 wSetFocusTo(scr, NULL);
1011 if (wPreferences.colormap_mode==WKF_POINTER) {
1012 wColormapInstallForWindow(scr, NULL);
1014 if (scr->autoRaiseTimer
1015 && event->xcrossing.root==event->xcrossing.window) {
1016 WMDeleteTimerHandler(scr->autoRaiseTimer);
1017 scr->autoRaiseTimer = NULL;
1019 } else {
1020 /* set auto raise timer even if in focus-follows-mouse mode
1021 * and the event is for the frame window, even if the window
1022 * has focus already. useful if you move the pointer from a focused
1023 * window to the root window and back pretty fast
1025 * set focus if in focus-follows-mouse mode and the event
1026 * is for the frame window and window doesn't have focus yet */
1027 if ((wPreferences.focus_mode==WKF_POINTER
1028 || wPreferences.focus_mode==WKF_SLOPPY)
1029 && wwin->frame->core->window==event->xcrossing.window) {
1031 if (!wwin->flags.focused)
1032 wSetFocusTo(scr, wwin);
1034 if (scr->autoRaiseTimer)
1035 WMDeleteTimerHandler(scr->autoRaiseTimer);
1036 scr->autoRaiseTimer = NULL;
1038 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1039 scr->autoRaiseWindow = wwin->frame->core->window;
1040 scr->autoRaiseTimer
1041 = WMAddTimerHandler(wPreferences.raise_delay,
1042 (WMCallback*)raiseWindow, scr);
1045 /* Install colormap for window, if the colormap installation mode
1046 * is colormap_follows_mouse */
1047 if (wPreferences.colormap_mode==WKF_POINTER) {
1048 if (wwin->client_win==event->xcrossing.window)
1049 wColormapInstallForWindow(scr, wwin);
1050 else
1051 wColormapInstallForWindow(scr, NULL);
1055 /* a little kluge to hide the clip balloon */
1056 if (!wPreferences.flags.noclip && scr->flags.clip_balloon_mapped) {
1057 if (!desc) {
1058 XUnmapWindow(dpy, scr->clip_balloon);
1059 scr->flags.clip_balloon_mapped = 0;
1060 } else {
1061 if (desc->parent_type!=WCLASS_DOCK_ICON
1062 || scr->clip_icon != desc->parent) {
1063 XUnmapWindow(dpy, scr->clip_balloon);
1064 scr->flags.clip_balloon_mapped = 0;
1069 if (event->xcrossing.window == event->xcrossing.root
1070 && event->xcrossing.detail == NotifyNormal
1071 && event->xcrossing.detail != NotifyInferior
1072 && wPreferences.focus_mode != WKF_CLICK) {
1074 wSetFocusTo(scr, scr->focused_window);
1077 #ifdef BALLOON_TEXT
1078 wBalloonEnteredObject(scr, desc);
1079 #endif
1083 static void
1084 handleLeaveNotify(XEvent *event)
1086 WObjDescriptor *desc = NULL;
1088 if (XFindContext(dpy, event->xcrossing.window, wWinContext,
1089 (XPointer *)&desc)!=XCNOENT) {
1090 if(desc->handle_leavenotify)
1091 (*desc->handle_leavenotify)(desc, event);
1093 if (event->xcrossing.window == event->xcrossing.root
1094 && event->xcrossing.mode == NotifyNormal
1095 && event->xcrossing.detail != NotifyInferior
1096 && wPreferences.focus_mode != WKF_CLICK) {
1098 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1100 wSetFocusTo(scr, NULL);
1105 #ifdef SHAPE
1106 static void
1107 handleShapeNotify(XEvent *event)
1109 XShapeEvent *shev = (XShapeEvent*)event;
1110 WWindow *wwin;
1111 XEvent ev;
1113 #ifdef DEBUG
1114 puts("got shape notify");
1115 #endif
1117 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev)) {
1118 XShapeEvent *sev = (XShapeEvent*)&ev;
1120 if (sev->kind == ShapeBounding) {
1121 if (sev->shaped == shev->shaped) {
1122 *shev = *sev;
1123 } else {
1124 XPutBackEvent(dpy, &ev);
1125 break;
1130 wwin = wWindowFor(shev->window);
1131 if (!wwin || shev->kind != ShapeBounding)
1132 return;
1134 if (!shev->shaped && wwin->flags.shaped) {
1136 wwin->flags.shaped = 0;
1137 wWindowClearShape(wwin);
1139 } else if (shev->shaped) {
1141 wwin->flags.shaped = 1;
1142 wWindowSetShape(wwin);
1145 #endif /* SHAPE */
1148 static void
1149 handleColormapNotify(XEvent *event)
1151 WWindow *wwin;
1152 WScreen *scr;
1153 Bool reinstall = False;
1155 wwin = wWindowFor(event->xcolormap.window);
1156 if (!wwin)
1157 return;
1159 scr = wwin->screen_ptr;
1161 do {
1162 if (wwin) {
1163 if (event->xcolormap.new) {
1164 XWindowAttributes attr;
1166 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1168 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1169 scr->current_colormap = attr.colormap;
1171 reinstall = True;
1172 } else if (event->xcolormap.state == ColormapUninstalled &&
1173 scr->current_colormap == event->xcolormap.colormap) {
1175 /* some bastard app (like XV) removed our colormap */
1177 * can't enforce or things like xscreensaver wont work
1178 * reinstall = True;
1180 } else if (event->xcolormap.state == ColormapInstalled &&
1181 scr->current_colormap == event->xcolormap.colormap) {
1183 /* someone has put our colormap back */
1184 reinstall = False;
1187 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1188 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1190 if (reinstall && scr->current_colormap!=None) {
1191 if (!scr->flags.colormap_stuff_blocked)
1192 XInstallColormap(dpy, scr->current_colormap);
1198 static void
1199 handleFocusIn(XEvent *event)
1201 WWindow *wwin;
1204 * For applications that like stealing the focus.
1206 while (XCheckTypedEvent(dpy, FocusIn, event));
1207 saveTimestamp(event);
1208 if (event->xfocus.mode == NotifyUngrab
1209 || event->xfocus.mode == NotifyGrab
1210 || event->xfocus.detail > NotifyNonlinearVirtual) {
1211 return;
1214 wwin = wWindowFor(event->xfocus.window);
1215 if (wwin && !wwin->flags.focused) {
1216 if (wwin->flags.mapped)
1217 wSetFocusTo(wwin->screen_ptr, wwin);
1218 else
1219 wSetFocusTo(wwin->screen_ptr, NULL);
1220 } else if (!wwin) {
1221 WScreen *scr = wScreenForWindow(event->xfocus.window);
1222 if (scr)
1223 wSetFocusTo(scr, NULL);
1228 static WWindow*
1229 windowUnderPointer(WScreen *scr)
1231 unsigned int mask;
1232 int foo;
1233 Window bar, win;
1235 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo,
1236 &mask))
1237 return wWindowFor(win);
1238 return NULL;
1241 static void
1242 handleKeyPress(XEvent *event)
1244 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1245 WWindow *wwin = scr->focused_window;
1246 int i;
1247 int modifiers;
1248 int command=-1;
1249 #ifdef KEEP_XKB_LOCK_STATUS
1250 XkbStateRec staterec;
1251 #endif /*KEEP_XKB_LOCK_STATUS*/
1253 /* ignore CapsLock */
1254 modifiers = event->xkey.state & ValidModMask;
1256 for (i=0; i<WKBD_LAST; i++) {
1257 if (wKeyBindings[i].keycode==0)
1258 continue;
1260 if (wKeyBindings[i].keycode==event->xkey.keycode
1261 && (/*wKeyBindings[i].modifier==0
1262 ||*/ wKeyBindings[i].modifier==modifiers)) {
1263 command = i;
1264 break;
1268 if (command < 0) {
1269 #ifdef LITE
1271 #if 0
1273 #endif
1274 #else
1275 if (!wRootMenuPerformShortcut(event)) {
1276 #endif
1277 static int dontLoop = 0;
1279 if (dontLoop > 10) {
1280 wwarning("problem with key event processing code");
1281 return;
1283 dontLoop++;
1284 /* if the focused window is an internal window, try redispatching
1285 * the event to the managed window, as it can be a WINGs window */
1286 if (wwin && wwin->flags.internal_window
1287 && wwin->client_leader!=None) {
1288 /* client_leader contains the WINGs toplevel */
1289 event->xany.window = wwin->client_leader;
1290 WMHandleEvent(event);
1292 dontLoop--;
1294 return;
1297 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1298 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1300 switch (command) {
1301 #ifndef LITE
1302 case WKBD_ROOTMENU:
1303 OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True);
1304 break;
1305 case WKBD_WINDOWLIST:
1306 OpenSwitchMenu(scr, event->xkey.x_root, event->xkey.y_root, True);
1307 break;
1308 #endif /* !LITE */
1309 case WKBD_WINDOWMENU:
1310 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1311 OpenWindowMenu(wwin, wwin->frame_x,
1312 wwin->frame_y+wwin->frame->top_width, True);
1313 break;
1314 case WKBD_MINIATURIZE:
1315 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1316 && !WFLAGP(wwin, no_miniaturizable)) {
1317 CloseWindowMenu(scr);
1319 if (wwin->protocols.MINIATURIZE_WINDOW)
1320 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
1321 event->xbutton.time);
1322 else {
1323 wIconifyWindow(wwin);
1326 break;
1327 case WKBD_HIDE:
1328 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1329 WApplication *wapp = wApplicationOf(wwin->main_window);
1330 CloseWindowMenu(scr);
1332 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1333 wHideApplication(wapp);
1336 break;
1337 case WKBD_MAXIMIZE:
1338 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_resizable)) {
1339 CloseWindowMenu(scr);
1341 if (wwin->flags.maximized) {
1342 wUnmaximizeWindow(wwin);
1343 } else {
1344 wMaximizeWindow(wwin, MAX_VERTICAL|MAX_HORIZONTAL);
1347 break;
1348 case WKBD_VMAXIMIZE:
1349 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_resizable)) {
1350 CloseWindowMenu(scr);
1352 if (wwin->flags.maximized) {
1353 wUnmaximizeWindow(wwin);
1354 } else {
1355 wMaximizeWindow(wwin, MAX_VERTICAL);
1358 break;
1359 case WKBD_RAISE:
1360 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1361 CloseWindowMenu(scr);
1363 wRaiseFrame(wwin->frame->core);
1365 break;
1366 case WKBD_LOWER:
1367 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1368 CloseWindowMenu(scr);
1370 wLowerFrame(wwin->frame->core);
1372 break;
1373 case WKBD_RAISELOWER:
1374 /* raise or lower the window under the pointer, not the
1375 * focused one
1377 wwin = windowUnderPointer(scr);
1378 if (wwin)
1379 wRaiseLowerFrame(wwin->frame->core);
1380 break;
1381 case WKBD_SHADE:
1382 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1383 if (wwin->flags.shaded)
1384 wUnshadeWindow(wwin);
1385 else
1386 wShadeWindow(wwin);
1388 break;
1389 case WKBD_MOVERESIZE:
1390 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1391 CloseWindowMenu(scr);
1393 wKeyboardMoveResizeWindow(wwin);
1395 break;
1396 case WKBD_CLOSE:
1397 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1398 CloseWindowMenu(scr);
1399 if (wwin->protocols.DELETE_WINDOW)
1400 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW,
1401 event->xkey.time);
1403 break;
1404 case WKBD_SELECT:
1405 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1406 wSelectWindow(wwin, !wwin->flags.selected);
1408 break;
1409 case WKBD_FOCUSNEXT:
1410 wwin = NextFocusWindow(scr);
1411 if (wwin != NULL) {
1412 wSetFocusTo(scr, wwin);
1413 if (wPreferences.circ_raise)
1414 wRaiseFrame(wwin->frame->core);
1416 break;
1417 case WKBD_FOCUSPREV:
1418 wwin = PrevFocusWindow(scr);
1419 if (wwin != NULL) {
1420 wSetFocusTo(scr, wwin);
1421 if (wPreferences.circ_raise)
1422 wRaiseFrame(wwin->frame->core);
1424 break;
1425 #if (defined(__STDC__) && !defined(UNIXCPP)) || defined(ANSICPP)
1426 #define GOTOWORKS(wk) case WKBD_WORKSPACE##wk:\
1427 i = (scr->current_workspace/10)*10 + wk - 1;\
1428 if (wPreferences.ws_advance || i<scr->workspace_count)\
1429 wWorkspaceChange(scr, i);\
1430 break
1431 #else
1432 #define GOTOWORKS(wk) case WKBD_WORKSPACE/**/wk:\
1433 i = (scr->current_workspace/10)*10 + wk - 1;\
1434 if (wPreferences.ws_advance || i<scr->workspace_count)\
1435 wWorkspaceChange(scr, i);\
1436 break
1437 #endif
1438 GOTOWORKS(1);
1439 GOTOWORKS(2);
1440 GOTOWORKS(3);
1441 GOTOWORKS(4);
1442 GOTOWORKS(5);
1443 GOTOWORKS(6);
1444 GOTOWORKS(7);
1445 GOTOWORKS(8);
1446 GOTOWORKS(9);
1447 GOTOWORKS(10);
1448 #undef GOTOWORKS
1449 case WKBD_NEXTWORKSPACE:
1450 wWorkspaceRelativeChange(scr, 1);
1451 break;
1452 case WKBD_PREVWORKSPACE:
1453 wWorkspaceRelativeChange(scr, -1);
1454 break;
1455 case WKBD_WINDOW1:
1456 case WKBD_WINDOW2:
1457 case WKBD_WINDOW3:
1458 case WKBD_WINDOW4:
1459 #ifdef EXTEND_WINDOWSHORTCUT
1460 case WKBD_WINDOW5:
1461 case WKBD_WINDOW6:
1462 case WKBD_WINDOW7:
1463 case WKBD_WINDOW8:
1464 case WKBD_WINDOW9:
1465 case WKBD_WINDOW10:
1466 #endif
1467 if (scr->shortcutWindow[command-WKBD_WINDOW1]) {
1468 wMakeWindowVisible(scr->shortcutWindow[command-WKBD_WINDOW1]);
1469 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1470 scr->shortcutWindow[command-WKBD_WINDOW1] = wwin;
1471 wSelectWindow(wwin, !wwin->flags.selected);
1472 XFlush(dpy);
1473 wusleep(3000);
1474 wSelectWindow(wwin, !wwin->flags.selected);
1475 XFlush(dpy);
1477 break;
1478 case WKBD_NEXTWSLAYER:
1479 case WKBD_PREVWSLAYER:
1481 int row, column;
1483 row = scr->current_workspace/10;
1484 column = scr->current_workspace%10;
1486 if (command==WKBD_NEXTWSLAYER) {
1487 if ((row+1)*10 < scr->workspace_count)
1488 wWorkspaceChange(scr, column+(row+1)*10);
1489 } else {
1490 if (row > 0)
1491 wWorkspaceChange(scr, column+(row-1)*10);
1494 break;
1495 case WKBD_CLIPLOWER:
1496 if (!wPreferences.flags.noclip)
1497 wDockLower(scr->workspaces[scr->current_workspace]->clip);
1498 break;
1499 case WKBD_CLIPRAISE:
1500 if (!wPreferences.flags.noclip)
1501 wDockRaise(scr->workspaces[scr->current_workspace]->clip);
1502 break;
1503 case WKBD_CLIPRAISELOWER:
1504 if (!wPreferences.flags.noclip)
1505 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1506 break;
1507 #ifdef KEEP_XKB_LOCK_STATUS
1508 case WKBD_TOGGLE:
1509 if(wPreferences.modelock){
1510 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1511 /*toggle*/
1512 XkbLockGroup(dpy,XkbUseCoreKbd,
1513 wwin->languagemode=staterec.compat_state&32?0:1);
1515 break;
1516 #endif /* KEEP_XKB_LOCK_STATUS */
1522 static void
1523 handleMotionNotify(XEvent *event)
1525 WMenu *menu;
1526 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1528 if (wPreferences.scrollable_menus) {
1529 if (event->xmotion.x_root <= 1 ||
1530 event->xmotion.x_root >= (scr->scr_width - 2) ||
1531 event->xmotion.y_root <= 1 ||
1532 event->xmotion.y_root >= (scr->scr_height - 2)) {
1534 #ifdef DEBUG
1535 puts("pointer at screen edge");
1536 #endif
1538 menu = wMenuUnderPointer(scr);
1539 if (menu!=NULL)
1540 wMenuScroll(menu, event);