wmaker: Moved timestamp variable (#1) to the global variable structure
[wmaker-crm.git] / src / event.c
blobaa4c92ab1cb49f0b4a7a684cb40a40d2cef97254
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 SHAPE
39 # include <X11/extensions/shape.h>
40 #endif
41 #ifdef XDND
42 #include "xdnd.h"
43 #endif
45 #ifdef HAVE_XRANDR
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"
79 /******** Global Variables **********/
80 extern XContext wWinContext;
81 extern XContext wVEdgeContext;
83 extern Cursor wCursor[WCUR_LAST];
85 extern WShortKey wKeyBindings[WKBD_LAST];
86 extern int wScreenCount;
87 extern Time LastFocusChange;
89 #define MOD_MASK wPreferences.modifier_mask
91 extern Atom _XA_WM_COLORMAP_NOTIFY;
93 extern Atom _XA_WM_CHANGE_STATE;
94 extern Atom _XA_WM_DELETE_WINDOW;
95 extern Atom _XA_GNUSTEP_WM_ATTR;
96 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
97 extern Atom _XA_GNUSTEP_TITLEBAR_STATE;
98 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
99 extern Atom _XA_WINDOWMAKER_COMMAND;
100 extern Atom _XA_WM_IGNORE_FOCUS_EVENTS;
102 #ifdef SHAPE
103 extern Bool wShapeSupported;
104 extern int wShapeEventBase;
105 #endif
107 #ifdef KEEP_XKB_LOCK_STATUS
108 extern int wXkbEventBase;
109 #endif
111 /************ Local stuff ***********/
113 static void saveTimestamp(XEvent *event);
114 static void handleColormapNotify(XEvent *event);
115 static void handleMapNotify(XEvent *event);
116 static void handleUnmapNotify(XEvent *event);
117 static void handleButtonPress(XEvent *event);
118 static void handleExpose(XEvent *event);
119 static void handleDestroyNotify(XEvent *event);
120 static void handleConfigureRequest(XEvent *event);
121 static void handleMapRequest(XEvent *event);
122 static void handlePropertyNotify(XEvent *event);
123 static void handleEnterNotify(XEvent *event);
124 static void handleLeaveNotify(XEvent *event);
125 static void handleExtensions(XEvent *event);
126 static void handleClientMessage(XEvent *event);
127 static void handleKeyPress(XEvent *event);
128 static void handleFocusIn(XEvent *event);
129 static void handleMotionNotify(XEvent *event);
130 static void handleVisibilityNotify(XEvent *event);
131 static void handle_inotify_events(int fd, int wd);
132 static void wdelete_death_handler(WMagicNumber id);
135 #ifdef SHAPE
136 static void handleShapeNotify(XEvent *event);
137 #endif
139 #ifdef KEEP_XKB_LOCK_STATUS
140 static void handleXkbIndicatorStateNotify(XEvent *event);
141 #endif
143 /* real dead process handler */
144 static void handleDeadProcess(void *foo);
146 typedef struct DeadProcesses {
147 pid_t pid;
148 unsigned char exit_status;
149 } DeadProcesses;
151 /* stack of dead processes */
152 static DeadProcesses deadProcesses[MAX_DEAD_PROCESSES];
153 static int deadProcessPtr = 0;
155 typedef struct DeathHandler {
156 WDeathHandler *callback;
157 pid_t pid;
158 void *client_data;
159 } DeathHandler;
161 static WMArray *deathHandlers = NULL;
163 WMagicNumber wAddDeathHandler(pid_t pid, WDeathHandler * callback, void *cdata)
165 DeathHandler *handler;
167 handler = malloc(sizeof(DeathHandler));
168 if (!handler)
169 return 0;
171 handler->pid = pid;
172 handler->callback = callback;
173 handler->client_data = cdata;
175 if (!deathHandlers)
176 deathHandlers = WMCreateArrayWithDestructor(8, free);
178 WMAddToArray(deathHandlers, handler);
180 return handler;
183 static void wdelete_death_handler(WMagicNumber id)
185 DeathHandler *handler = (DeathHandler *) id;
187 if (!handler || !deathHandlers)
188 return;
190 /* array destructor will call free(handler) */
191 WMRemoveFromArray(deathHandlers, handler);
194 void DispatchEvent(XEvent * event)
196 if (deathHandlers)
197 handleDeadProcess(NULL);
199 if (WCHECK_STATE(WSTATE_NEED_EXIT)) {
200 WCHANGE_STATE(WSTATE_EXITING);
201 /* received SIGTERM */
203 * WMHandleEvent() can't be called from anything
204 * executed inside here, or we can get in a infinite
205 * recursive loop.
207 Shutdown(WSExitMode);
209 } else if (WCHECK_STATE(WSTATE_NEED_RESTART)) {
210 WCHANGE_STATE(WSTATE_RESTARTING);
212 Shutdown(WSRestartPreparationMode);
213 /* received SIGHUP */
214 Restart(NULL, True);
215 } else if (WCHECK_STATE(WSTATE_NEED_REREAD)) {
216 WCHANGE_STATE(WSTATE_NORMAL);
217 wDefaultsCheckDomains(NULL);
220 /* for the case that all that is wanted to be dispatched is
221 * the stuff above */
222 if (!event)
223 return;
225 saveTimestamp(event);
226 switch (event->type) {
227 case MapRequest:
228 handleMapRequest(event);
229 break;
231 case KeyPress:
232 handleKeyPress(event);
233 break;
235 case MotionNotify:
236 handleMotionNotify(event);
237 break;
239 case ConfigureRequest:
240 handleConfigureRequest(event);
241 break;
243 case DestroyNotify:
244 handleDestroyNotify(event);
245 break;
247 case MapNotify:
248 handleMapNotify(event);
249 break;
251 case UnmapNotify:
252 handleUnmapNotify(event);
253 break;
255 case ButtonPress:
256 handleButtonPress(event);
257 break;
259 case Expose:
260 handleExpose(event);
261 break;
263 case PropertyNotify:
264 handlePropertyNotify(event);
265 break;
267 case EnterNotify:
268 handleEnterNotify(event);
269 break;
271 case LeaveNotify:
272 handleLeaveNotify(event);
273 break;
275 case ClientMessage:
276 handleClientMessage(event);
277 break;
279 case ColormapNotify:
280 handleColormapNotify(event);
281 break;
283 case MappingNotify:
284 if (event->xmapping.request == MappingKeyboard || event->xmapping.request == MappingModifier)
285 XRefreshKeyboardMapping(&event->xmapping);
286 break;
288 case FocusIn:
289 handleFocusIn(event);
290 break;
292 case VisibilityNotify:
293 handleVisibilityNotify(event);
294 break;
296 case ConfigureNotify:
297 #ifdef HAVE_XRANDR
298 if (event->xconfigure.window == DefaultRootWindow(dpy))
299 XRRUpdateConfiguration(event);
300 #endif
301 break;
303 default:
304 handleExtensions(event);
305 break;
309 #ifdef HAVE_INOTIFY
311 *----------------------------------------------------------------------
312 * handle_inotify_events-
313 * Check for inotify events
315 * Returns:
316 * After reading events for the given file descriptor (fd) and
317 * watch descriptor (wd)
319 * Side effects:
320 * Calls wDefaultsCheckDomains if config database is updated
321 *----------------------------------------------------------------------
323 /* allow 5 simultaneous events, with path + filenames up to 64 chars */
324 #define BUFF_SIZE ((sizeof(struct inotify_event) + 64)*5)
325 static void handle_inotify_events(int fd, int wd)
327 ssize_t eventQLength, i = 0;
328 char buff[BUFF_SIZE] = { 0 };
329 /* Check config only once per read of the event queue */
330 int oneShotFlag = 0;
333 * Read off the queued events
334 * queue overflow is not checked (IN_Q_OVERFLOW). In practise this should
335 * not occur; the block is on Xevents, but a config file change will normally
336 * occur as a result of an Xevent - so the event queue should never have more than
337 * a few entries before a read().
339 eventQLength = read(fd, buff, BUFF_SIZE);
341 /* check what events occured */
342 /* Should really check wd here too, but for now we only have one watch! */
343 while (i < eventQLength) {
344 struct inotify_event *pevent = (struct inotify_event *)&buff[i];
347 * see inotify.h for event types.
349 if (pevent->mask & IN_DELETE_SELF) {
350 wwarning(_("the defaults database has been deleted!"
351 " Restart Window Maker to create the database" " with the default settings"));
352 close(fd);
354 if (pevent->mask & IN_UNMOUNT) {
355 wwarning(_("the unit containing the defaults database has"
356 " been unmounted. Setting --static mode." " Any changes will not be saved."));
357 close(fd);
358 wPreferences.flags.noupdates = 1;
360 if ((pevent->mask & IN_MODIFY) && oneShotFlag == 0) {
361 wwarning(_("Inotify: Reading config files in defaults database."));
362 wDefaultsCheckDomains(NULL);
365 /* move to next event in the buffer */
366 i += sizeof(struct inotify_event) + pevent->len;
369 #endif /* HAVE_INOTIFY */
372 *----------------------------------------------------------------------
373 * EventLoop-
374 * Processes X and internal events indefinitely.
376 * Returns:
377 * Never returns
379 * Side effects:
380 * The LastTimestamp global variable is updated.
381 * Calls inotifyGetEvents if defaults database changes.
382 *----------------------------------------------------------------------
384 noreturn void EventLoop(void)
386 XEvent event;
387 #ifdef HAVE_INOTIFY
388 extern int inotifyFD;
389 extern int inotifyWD;
390 struct timeval time;
391 fd_set rfds;
392 int retVal = 0;
394 if (inotifyFD < 0 || inotifyWD < 0)
395 retVal = -1;
396 #endif
398 for (;;) {
400 WMNextEvent(dpy, &event); /* Blocks here */
401 WMHandleEvent(&event);
402 #ifdef HAVE_INOTIFY
403 if (retVal != -1) {
404 time.tv_sec = 0;
405 time.tv_usec = 0;
406 FD_ZERO(&rfds);
407 FD_SET(inotifyFD, &rfds);
409 /* check for available read data from inotify - don't block! */
410 retVal = select(inotifyFD + 1, &rfds, NULL, NULL, &time);
412 if (retVal < 0) { /* an error has occured */
413 wwarning(_("select failed. The inotify instance will be closed."
414 " Changes to the defaults database will require"
415 " a restart to take effect."));
416 close(inotifyFD);
417 continue;
419 if (FD_ISSET(inotifyFD, &rfds))
420 handle_inotify_events(inotifyFD, inotifyWD);
422 #endif
427 *----------------------------------------------------------------------
428 * ProcessPendingEvents --
429 * Processes the events that are currently pending (at the time
430 * this function is called) in the display's queue.
432 * Returns:
433 * After the pending events that were present at the function call
434 * are processed.
436 * Side effects:
437 * Many -- whatever handling events may involve.
439 *----------------------------------------------------------------------
441 void ProcessPendingEvents(void)
443 XEvent event;
444 int count;
446 XSync(dpy, False);
448 /* Take a snapshot of the event count in the queue */
449 count = XPending(dpy);
451 while (count > 0 && XPending(dpy)) {
452 WMNextEvent(dpy, &event);
453 WMHandleEvent(&event);
454 count--;
458 Bool IsDoubleClick(WScreen * scr, XEvent * event)
460 if ((scr->last_click_time > 0) &&
461 (event->xbutton.time - scr->last_click_time <= wPreferences.dblclick_time)
462 && (event->xbutton.button == scr->last_click_button)
463 && (event->xbutton.window == scr->last_click_window)) {
465 scr->flags.next_click_is_not_double = 1;
466 scr->last_click_time = 0;
467 scr->last_click_window = event->xbutton.window;
469 return True;
471 return False;
474 void NotifyDeadProcess(pid_t pid, unsigned char status)
476 if (deadProcessPtr >= MAX_DEAD_PROCESSES - 1) {
477 wwarning("stack overflow: too many dead processes");
478 return;
480 /* stack the process to be handled later,
481 * as this is called from the signal handler */
482 deadProcesses[deadProcessPtr].pid = pid;
483 deadProcesses[deadProcessPtr].exit_status = status;
484 deadProcessPtr++;
487 static void handleDeadProcess(void *foo)
489 DeathHandler *tmp;
490 int i;
492 for (i = 0; i < deadProcessPtr; i++) {
493 wWindowDeleteSavedStatesForPID(deadProcesses[i].pid);
496 if (!deathHandlers) {
497 deadProcessPtr = 0;
498 return;
501 /* get the pids on the queue and call handlers */
502 while (deadProcessPtr > 0) {
503 deadProcessPtr--;
505 for (i = WMGetArrayItemCount(deathHandlers) - 1; i >= 0; i--) {
506 tmp = WMGetFromArray(deathHandlers, i);
507 if (!tmp)
508 continue;
510 if (tmp->pid == deadProcesses[deadProcessPtr].pid) {
511 (*tmp->callback) (tmp->pid,
512 deadProcesses[deadProcessPtr].exit_status, tmp->client_data);
513 wdelete_death_handler(tmp);
519 static void saveTimestamp(XEvent * event)
522 * Never save CurrentTime as LastTimestamp because CurrentTime
523 * it's not a real timestamp (it's the 0L constant)
526 switch (event->type) {
527 case ButtonRelease:
528 case ButtonPress:
529 w_global.timestamp.last_event = event->xbutton.time;
530 break;
531 case KeyPress:
532 case KeyRelease:
533 w_global.timestamp.last_event = event->xkey.time;
534 break;
535 case MotionNotify:
536 w_global.timestamp.last_event = event->xmotion.time;
537 break;
538 case PropertyNotify:
539 w_global.timestamp.last_event = event->xproperty.time;
540 break;
541 case EnterNotify:
542 case LeaveNotify:
543 w_global.timestamp.last_event = event->xcrossing.time;
544 break;
545 case SelectionClear:
546 w_global.timestamp.last_event = event->xselectionclear.time;
547 break;
548 case SelectionRequest:
549 w_global.timestamp.last_event = event->xselectionrequest.time;
550 break;
551 case SelectionNotify:
552 w_global.timestamp.last_event = event->xselection.time;
553 #ifdef XDND
554 wXDNDProcessSelection(event);
555 #endif
556 break;
560 static int matchWindow(const void *item, const void *cdata)
562 return (((WFakeGroupLeader *) item)->origLeader == (Window) cdata);
565 static void handleExtensions(XEvent * event)
567 #ifdef KEEP_XKB_LOCK_STATUS
568 XkbEvent *xkbevent;
569 xkbevent = (XkbEvent *) event;
570 #endif /*KEEP_XKB_LOCK_STATUS */
571 #ifdef SHAPE
572 if (wShapeSupported && event->type == (wShapeEventBase + ShapeNotify)) {
573 handleShapeNotify(event);
575 #endif
576 #ifdef KEEP_XKB_LOCK_STATUS
577 if (wPreferences.modelock && (xkbevent->type == wXkbEventBase)) {
578 handleXkbIndicatorStateNotify(event);
580 #endif /*KEEP_XKB_LOCK_STATUS */
581 #ifdef HAVE_XRANDR
582 if (has_randr && event->type == (randr_event_base + RRScreenChangeNotify)) {
583 /* From xrandr man page: "Clients must call back into Xlib using
584 * XRRUpdateConfiguration when screen configuration change notify
585 * events are generated */
586 XRRUpdateConfiguration(event);
587 WCHANGE_STATE(WSTATE_RESTARTING);
588 Shutdown(WSRestartPreparationMode);
589 Restart(NULL,True);
591 #endif
594 static void handleMapRequest(XEvent * ev)
596 WWindow *wwin;
597 WScreen *scr = NULL;
598 Window window = ev->xmaprequest.window;
600 if ((wwin = wWindowFor(window))) {
601 if (wwin->flags.shaded) {
602 wUnshadeWindow(wwin);
604 /* deiconify window */
605 if (wwin->flags.miniaturized) {
606 wDeiconifyWindow(wwin);
607 } else if (wwin->flags.hidden) {
608 WApplication *wapp = wApplicationOf(wwin->main_window);
609 /* go to the last workspace that the user worked on the app */
610 if (wapp) {
611 wWorkspaceChange(wwin->screen_ptr, wapp->last_workspace);
613 wUnhideApplication(wapp, False, False);
615 return;
618 scr = wScreenForRootWindow(ev->xmaprequest.parent);
620 wwin = wManageWindow(scr, window);
623 * This is to let the Dock know that the application it launched
624 * has already been mapped (eg: it has finished launching).
625 * It is not necessary for normally docked apps, but is needed for
626 * apps that were forcedly docked (like with dockit).
628 if (scr->last_dock) {
629 if (wwin && wwin->main_window != None && wwin->main_window != window)
630 wDockTrackWindowLaunch(scr->last_dock, wwin->main_window);
631 else
632 wDockTrackWindowLaunch(scr->last_dock, window);
635 if (wwin) {
636 wClientSetState(wwin, NormalState, None);
637 if (wwin->flags.maximized) {
638 wMaximizeWindow(wwin, wwin->flags.maximized);
640 if (wwin->flags.shaded) {
641 wwin->flags.shaded = 0;
642 wwin->flags.skip_next_animation = 1;
643 wShadeWindow(wwin);
645 if (wwin->flags.miniaturized) {
646 wwin->flags.miniaturized = 0;
647 wwin->flags.skip_next_animation = 1;
648 wIconifyWindow(wwin);
650 if (wwin->flags.fullscreen) {
651 wwin->flags.fullscreen = 0;
652 wFullscreenWindow(wwin);
654 if (wwin->flags.hidden) {
655 WApplication *wapp = wApplicationOf(wwin->main_window);
657 wwin->flags.hidden = 0;
658 wwin->flags.skip_next_animation = 1;
659 if (wapp) {
660 wHideApplication(wapp);
666 static void handleDestroyNotify(XEvent * event)
668 WWindow *wwin;
669 WApplication *app;
670 Window window = event->xdestroywindow.window;
671 WScreen *scr = wScreenForRootWindow(event->xdestroywindow.event);
672 int widx;
674 wwin = wWindowFor(window);
675 if (wwin) {
676 wUnmanageWindow(wwin, False, True);
679 if (scr != NULL) {
680 while ((widx = WMFindInArray(scr->fakeGroupLeaders, matchWindow, (void *)window)) != WANotFound) {
681 WFakeGroupLeader *fPtr;
683 fPtr = WMGetFromArray(scr->fakeGroupLeaders, widx);
684 if (fPtr->retainCount > 0) {
685 fPtr->retainCount--;
686 if (fPtr->retainCount == 0 && fPtr->leader != None) {
687 XDestroyWindow(dpy, fPtr->leader);
688 fPtr->leader = None;
689 XFlush(dpy);
692 fPtr->origLeader = None;
696 app = wApplicationOf(window);
697 if (app) {
698 if (window == app->main_window) {
699 app->refcount = 0;
700 wwin = app->main_window_desc->screen_ptr->focused_window;
701 while (wwin) {
702 if (wwin->main_window == window) {
703 wwin->main_window = None;
705 wwin = wwin->prev;
708 wApplicationDestroy(app);
712 static void handleExpose(XEvent * event)
714 WObjDescriptor *desc;
715 XEvent ev;
717 while (XCheckTypedWindowEvent(dpy, event->xexpose.window, Expose, &ev)) ;
719 if (XFindContext(dpy, event->xexpose.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
720 return;
723 if (desc->handle_expose) {
724 (*desc->handle_expose) (desc, event);
728 static void executeButtonAction(WScreen * scr, XEvent * event, int action)
730 switch (action) {
731 case WA_SELECT_WINDOWS:
732 wUnselectWindows(scr);
733 wSelectWindows(scr, event);
734 break;
735 case WA_OPEN_APPMENU:
736 OpenRootMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
737 /* ugly hack */
738 if (scr->root_menu) {
739 if (scr->root_menu->brother->flags.mapped)
740 event->xbutton.window = scr->root_menu->brother->frame->core->window;
741 else
742 event->xbutton.window = scr->root_menu->frame->core->window;
744 break;
745 case WA_OPEN_WINLISTMENU:
746 OpenSwitchMenu(scr, event->xbutton.x_root, event->xbutton.y_root, False);
747 if (scr->switch_menu) {
748 if (scr->switch_menu->brother->flags.mapped)
749 event->xbutton.window = scr->switch_menu->brother->frame->core->window;
750 else
751 event->xbutton.window = scr->switch_menu->frame->core->window;
753 break;
754 default:
755 break;
759 /* bindable */
760 static void handleButtonPress(XEvent * event)
762 WObjDescriptor *desc;
763 WScreen *scr;
765 scr = wScreenForRootWindow(event->xbutton.root);
767 #ifdef BALLOON_TEXT
768 wBalloonHide(scr);
769 #endif
771 if (event->xbutton.window == scr->root_win) {
772 if (event->xbutton.button == Button1 && wPreferences.mouse_button1 != WA_NONE) {
773 executeButtonAction(scr, event, wPreferences.mouse_button1);
774 } else if (event->xbutton.button == Button2 && wPreferences.mouse_button2 != WA_NONE) {
775 executeButtonAction(scr, event, wPreferences.mouse_button2);
776 } else if (event->xbutton.button == Button3 && wPreferences.mouse_button3 != WA_NONE) {
777 executeButtonAction(scr, event, wPreferences.mouse_button3);
778 } else if (event->xbutton.button == Button4 && wPreferences.mouse_wheel != WA_NONE) {
779 wWorkspaceRelativeChange(scr, 1);
780 } else if (event->xbutton.button == Button5 && wPreferences.mouse_wheel != WA_NONE) {
781 wWorkspaceRelativeChange(scr, -1);
785 desc = NULL;
786 if (XFindContext(dpy, event->xbutton.subwindow, wWinContext, (XPointer *) & desc) == XCNOENT) {
787 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) == XCNOENT) {
788 return;
792 if (desc->parent_type == WCLASS_WINDOW) {
793 XSync(dpy, 0);
795 if (event->xbutton.state & ( MOD_MASK | ControlMask )) {
796 XAllowEvents(dpy, AsyncPointer, CurrentTime);
797 } else {
798 /* if (wPreferences.focus_mode == WKF_CLICK) { */
799 if (wPreferences.ignore_focus_click) {
800 XAllowEvents(dpy, AsyncPointer, CurrentTime);
802 XAllowEvents(dpy, ReplayPointer, CurrentTime);
803 /* } */
805 XSync(dpy, 0);
806 } else if (desc->parent_type == WCLASS_APPICON
807 || desc->parent_type == WCLASS_MINIWINDOW || desc->parent_type == WCLASS_DOCK_ICON) {
808 if (event->xbutton.state & MOD_MASK) {
809 XSync(dpy, 0);
810 XAllowEvents(dpy, AsyncPointer, CurrentTime);
811 XSync(dpy, 0);
815 if (desc->handle_mousedown != NULL) {
816 (*desc->handle_mousedown) (desc, event);
819 /* save double-click information */
820 if (scr->flags.next_click_is_not_double) {
821 scr->flags.next_click_is_not_double = 0;
822 } else {
823 scr->last_click_time = event->xbutton.time;
824 scr->last_click_button = event->xbutton.button;
825 scr->last_click_window = event->xbutton.window;
829 static void handleMapNotify(XEvent * event)
831 WWindow *wwin;
833 wwin = wWindowFor(event->xmap.event);
834 if (wwin && wwin->client_win == event->xmap.event) {
835 if (wwin->flags.miniaturized) {
836 wDeiconifyWindow(wwin);
837 } else {
838 XGrabServer(dpy);
839 wWindowMap(wwin);
840 wClientSetState(wwin, NormalState, None);
841 XUngrabServer(dpy);
846 static void handleUnmapNotify(XEvent * event)
848 WWindow *wwin;
849 XEvent ev;
850 Bool withdraw = False;
852 /* only process windows with StructureNotify selected
853 * (ignore SubstructureNotify) */
854 wwin = wWindowFor(event->xunmap.window);
855 if (!wwin)
856 return;
858 /* whether the event is a Withdrawal request */
859 if (event->xunmap.event == wwin->screen_ptr->root_win && event->xunmap.send_event)
860 withdraw = True;
862 if (wwin->client_win != event->xunmap.event && !withdraw)
863 return;
865 if (!wwin->flags.mapped && !withdraw
866 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
867 && !wwin->flags.miniaturized && !wwin->flags.hidden)
868 return;
870 XGrabServer(dpy);
871 XUnmapWindow(dpy, wwin->frame->core->window);
872 wwin->flags.mapped = 0;
873 XSync(dpy, 0);
874 /* check if the window was destroyed */
875 if (XCheckTypedWindowEvent(dpy, wwin->client_win, DestroyNotify, &ev)) {
876 DispatchEvent(&ev);
877 } else {
878 Bool reparented = False;
880 if (XCheckTypedWindowEvent(dpy, wwin->client_win, ReparentNotify, &ev))
881 reparented = True;
883 /* withdraw window */
884 wwin->flags.mapped = 0;
885 if (!reparented)
886 wClientSetState(wwin, WithdrawnState, None);
888 /* if the window was reparented, do not reparent it back to the
889 * root window */
890 wUnmanageWindow(wwin, !reparented, False);
892 XUngrabServer(dpy);
895 static void handleConfigureRequest(XEvent * event)
897 WWindow *wwin;
899 if (!(wwin = wWindowFor(event->xconfigurerequest.window))) {
901 * Configure request for unmapped window
903 wClientConfigure(NULL, &(event->xconfigurerequest));
904 } else {
905 wClientConfigure(wwin, &(event->xconfigurerequest));
909 static void handlePropertyNotify(XEvent * event)
911 WWindow *wwin;
912 WApplication *wapp;
913 Window jr;
914 int ji;
915 unsigned int ju;
917 wwin = wWindowFor(event->xproperty.window);
918 if (wwin) {
919 if (!XGetGeometry(dpy, wwin->client_win, &jr, &ji, &ji, &ju, &ju, &ju, &ju)) {
920 return;
922 wClientCheckProperty(wwin, &event->xproperty);
924 wapp = wApplicationOf(event->xproperty.window);
925 if (wapp) {
926 wClientCheckProperty(wapp->main_window_desc, &event->xproperty);
930 static void handleClientMessage(XEvent * event)
932 WWindow *wwin;
933 WObjDescriptor *desc;
935 /* handle transition from Normal to Iconic state */
936 if (event->xclient.message_type == _XA_WM_CHANGE_STATE
937 && event->xclient.format == 32 && event->xclient.data.l[0] == IconicState) {
939 wwin = wWindowFor(event->xclient.window);
940 if (!wwin)
941 return;
942 if (!wwin->flags.miniaturized)
943 wIconifyWindow(wwin);
944 } else if (event->xclient.message_type == _XA_WM_COLORMAP_NOTIFY && event->xclient.format == 32) {
945 WScreen *scr = wScreenForRootWindow(event->xclient.window);
947 if (!scr)
948 return;
950 if (event->xclient.data.l[1] == 1) { /* starting */
951 wColormapAllowClientInstallation(scr, True);
952 } else { /* stopping */
953 wColormapAllowClientInstallation(scr, False);
955 } else if (event->xclient.message_type == _XA_WINDOWMAKER_COMMAND) {
957 char *command;
958 size_t len;
960 len = sizeof(event->xclient.data.b) + 1;
961 command = wmalloc(len);
962 strncpy(command, event->xclient.data.b, sizeof(event->xclient.data.b));
964 if (strncmp(command, "Reconfigure", sizeof("Reconfigure")) == 0) {
965 wwarning(_("Got Reconfigure command"));
966 wDefaultsCheckDomains(NULL);
967 } else {
968 wwarning(_("Got unknown command %s"), command);
971 wfree(command);
973 } else if (event->xclient.message_type == _XA_WINDOWMAKER_WM_FUNCTION) {
974 WApplication *wapp;
975 int done = 0;
976 wapp = wApplicationOf(event->xclient.window);
977 if (wapp) {
978 switch (event->xclient.data.l[0]) {
979 case WMFHideOtherApplications:
980 wHideOtherApplications(wapp->main_window_desc);
981 done = 1;
982 break;
984 case WMFHideApplication:
985 wHideApplication(wapp);
986 done = 1;
987 break;
990 if (!done) {
991 wwin = wWindowFor(event->xclient.window);
992 if (wwin) {
993 switch (event->xclient.data.l[0]) {
994 case WMFHideOtherApplications:
995 wHideOtherApplications(wwin);
996 break;
998 case WMFHideApplication:
999 wHideApplication(wApplicationOf(wwin->main_window));
1000 break;
1004 } else if (event->xclient.message_type == _XA_GNUSTEP_WM_ATTR) {
1005 wwin = wWindowFor(event->xclient.window);
1006 if (!wwin)
1007 return;
1008 switch (event->xclient.data.l[0]) {
1009 case GSWindowLevelAttr:
1011 int level = (int)event->xclient.data.l[1];
1013 if (WINDOW_LEVEL(wwin) != level) {
1014 ChangeStackingLevel(wwin->frame->core, level);
1017 break;
1019 } else if (event->xclient.message_type == _XA_GNUSTEP_TITLEBAR_STATE) {
1020 wwin = wWindowFor(event->xclient.window);
1021 if (!wwin)
1022 return;
1023 switch (event->xclient.data.l[0]) {
1024 case WMTitleBarNormal:
1025 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1026 break;
1027 case WMTitleBarMain:
1028 wFrameWindowChangeState(wwin->frame, WS_PFOCUSED);
1029 break;
1030 case WMTitleBarKey:
1031 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1032 break;
1034 } else if (event->xclient.message_type == _XA_WM_IGNORE_FOCUS_EVENTS) {
1035 WScreen *scr = wScreenForRootWindow(event->xclient.window);
1036 if (!scr)
1037 return;
1038 scr->flags.ignore_focus_events = event->xclient.data.l[0] ? 1 : 0;
1039 } else if (wNETWMProcessClientMessage(&event->xclient)) {
1040 /* do nothing */
1041 #ifdef XDND
1042 } else if (wXDNDProcessClientMessage(&event->xclient)) {
1043 /* do nothing */
1044 #endif /* XDND */
1045 } else {
1047 * Non-standard thing, but needed by OffiX DND.
1048 * For when the icon frame gets a ClientMessage
1049 * that should have gone to the icon_window.
1051 if (XFindContext(dpy, event->xbutton.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1052 struct WIcon *icon = NULL;
1054 if (desc->parent_type == WCLASS_MINIWINDOW) {
1055 icon = (WIcon *) desc->parent;
1056 } else if (desc->parent_type == WCLASS_DOCK_ICON || desc->parent_type == WCLASS_APPICON) {
1057 icon = ((WAppIcon *) desc->parent)->icon;
1059 if (icon && (wwin = icon->owner)) {
1060 if (wwin->client_win != event->xclient.window) {
1061 event->xclient.window = wwin->client_win;
1062 XSendEvent(dpy, wwin->client_win, False, NoEventMask, event);
1069 static void raiseWindow(WScreen * scr)
1071 WWindow *wwin;
1073 scr->autoRaiseTimer = NULL;
1075 wwin = wWindowFor(scr->autoRaiseWindow);
1076 if (!wwin)
1077 return;
1079 if (!wwin->flags.destroyed && wwin->flags.focused) {
1080 wRaiseFrame(wwin->frame->core);
1081 /* this is needed or a race condition will occur */
1082 XSync(dpy, False);
1086 static void handleEnterNotify(XEvent * event)
1088 WWindow *wwin;
1089 WObjDescriptor *desc = NULL;
1090 XEvent ev;
1091 WScreen *scr = wScreenForRootWindow(event->xcrossing.root);
1093 if (XCheckTypedWindowEvent(dpy, event->xcrossing.window, LeaveNotify, &ev)) {
1094 /* already left the window... */
1095 saveTimestamp(&ev);
1096 if (ev.xcrossing.mode == event->xcrossing.mode && ev.xcrossing.detail == event->xcrossing.detail) {
1097 return;
1101 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1102 if (desc->handle_enternotify)
1103 (*desc->handle_enternotify) (desc, event);
1106 /* enter to window */
1107 wwin = wWindowFor(event->xcrossing.window);
1108 if (!wwin) {
1109 if (wPreferences.colormap_mode == WCM_POINTER) {
1110 wColormapInstallForWindow(scr, NULL);
1112 if (scr->autoRaiseTimer && event->xcrossing.root == event->xcrossing.window) {
1113 WMDeleteTimerHandler(scr->autoRaiseTimer);
1114 scr->autoRaiseTimer = NULL;
1116 } else {
1117 /* set auto raise timer even if in focus-follows-mouse mode
1118 * and the event is for the frame window, even if the window
1119 * has focus already. useful if you move the pointer from a focused
1120 * window to the root window and back pretty fast
1122 * set focus if in focus-follows-mouse mode and the event
1123 * is for the frame window and window doesn't have focus yet */
1124 if (wPreferences.focus_mode == WKF_SLOPPY
1125 && wwin->frame->core->window == event->xcrossing.window && !scr->flags.doing_alt_tab) {
1127 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable))
1128 wSetFocusTo(scr, wwin);
1130 if (scr->autoRaiseTimer)
1131 WMDeleteTimerHandler(scr->autoRaiseTimer);
1132 scr->autoRaiseTimer = NULL;
1134 if (wPreferences.raise_delay && !WFLAGP(wwin, no_focusable)) {
1135 scr->autoRaiseWindow = wwin->frame->core->window;
1136 scr->autoRaiseTimer
1137 = WMAddTimerHandler(wPreferences.raise_delay, (WMCallback *) raiseWindow, scr);
1140 /* Install colormap for window, if the colormap installation mode
1141 * is colormap_follows_mouse */
1142 if (wPreferences.colormap_mode == WCM_POINTER) {
1143 if (wwin->client_win == event->xcrossing.window)
1144 wColormapInstallForWindow(scr, wwin);
1145 else
1146 wColormapInstallForWindow(scr, NULL);
1150 if (event->xcrossing.window == event->xcrossing.root
1151 && event->xcrossing.detail == NotifyNormal
1152 && event->xcrossing.detail != NotifyInferior && wPreferences.focus_mode != WKF_CLICK) {
1154 wSetFocusTo(scr, scr->focused_window);
1156 #ifdef BALLOON_TEXT
1157 wBalloonEnteredObject(scr, desc);
1158 #endif
1161 static void handleLeaveNotify(XEvent * event)
1163 WObjDescriptor *desc = NULL;
1165 if (XFindContext(dpy, event->xcrossing.window, wWinContext, (XPointer *) & desc) != XCNOENT) {
1166 if (desc->handle_leavenotify)
1167 (*desc->handle_leavenotify) (desc, event);
1171 #ifdef SHAPE
1172 static void handleShapeNotify(XEvent * event)
1174 XShapeEvent *shev = (XShapeEvent *) event;
1175 WWindow *wwin;
1176 union {
1177 XEvent xevent;
1178 XShapeEvent xshape;
1179 } ev;
1181 while (XCheckTypedWindowEvent(dpy, shev->window, event->type, &ev.xevent)) {
1182 if (ev.xshape.kind == ShapeBounding) {
1183 if (ev.xshape.shaped == shev->shaped) {
1184 *shev = ev.xshape;
1185 } else {
1186 XPutBackEvent(dpy, &ev.xevent);
1187 break;
1192 wwin = wWindowFor(shev->window);
1193 if (!wwin || shev->kind != ShapeBounding)
1194 return;
1196 if (!shev->shaped && wwin->flags.shaped) {
1198 wwin->flags.shaped = 0;
1199 wWindowClearShape(wwin);
1201 } else if (shev->shaped) {
1203 wwin->flags.shaped = 1;
1204 wWindowSetShape(wwin);
1207 #endif /* SHAPE */
1209 #ifdef KEEP_XKB_LOCK_STATUS
1210 /* please help ]d if you know what to do */
1211 static void handleXkbIndicatorStateNotify(XEvent *event)
1213 WWindow *wwin;
1214 WScreen *scr;
1215 XkbStateRec staterec;
1216 int i;
1218 for (i = 0; i < wScreenCount; i++) {
1219 scr = wScreenWithNumber(i);
1220 wwin = scr->focused_window;
1221 if (wwin && wwin->flags.focused) {
1222 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1223 if (wwin->frame->languagemode != staterec.group) {
1224 wwin->frame->last_languagemode = wwin->frame->languagemode;
1225 wwin->frame->languagemode = staterec.group;
1227 #ifdef XKB_BUTTON_HINT
1228 if (wwin->frame->titlebar) {
1229 wFrameWindowPaint(wwin->frame);
1231 #endif
1235 #endif /*KEEP_XKB_LOCK_STATUS */
1237 static void handleColormapNotify(XEvent * event)
1239 WWindow *wwin;
1240 WScreen *scr;
1241 Bool reinstall = False;
1243 wwin = wWindowFor(event->xcolormap.window);
1244 if (!wwin)
1245 return;
1247 scr = wwin->screen_ptr;
1249 do {
1250 if (wwin) {
1251 if (event->xcolormap.new) {
1252 XWindowAttributes attr;
1254 XGetWindowAttributes(dpy, wwin->client_win, &attr);
1256 if (wwin == scr->cmap_window && wwin->cmap_window_no == 0)
1257 scr->current_colormap = attr.colormap;
1259 reinstall = True;
1260 } else if (event->xcolormap.state == ColormapUninstalled &&
1261 scr->current_colormap == event->xcolormap.colormap) {
1263 /* some bastard app (like XV) removed our colormap */
1265 * can't enforce or things like xscreensaver wont work
1266 * reinstall = True;
1268 } else if (event->xcolormap.state == ColormapInstalled &&
1269 scr->current_colormap == event->xcolormap.colormap) {
1271 /* someone has put our colormap back */
1272 reinstall = False;
1275 } while (XCheckTypedEvent(dpy, ColormapNotify, event)
1276 && ((wwin = wWindowFor(event->xcolormap.window)) || 1));
1278 if (reinstall && scr->current_colormap != None) {
1279 if (!scr->flags.colormap_stuff_blocked)
1280 XInstallColormap(dpy, scr->current_colormap);
1284 static void handleFocusIn(XEvent * event)
1286 WWindow *wwin;
1289 * For applications that like stealing the focus.
1291 while (XCheckTypedEvent(dpy, FocusIn, event)) ;
1292 saveTimestamp(event);
1293 if (event->xfocus.mode == NotifyUngrab
1294 || event->xfocus.mode == NotifyGrab || event->xfocus.detail > NotifyNonlinearVirtual) {
1295 return;
1298 wwin = wWindowFor(event->xfocus.window);
1299 if (wwin && !wwin->flags.focused) {
1300 if (wwin->flags.mapped)
1301 wSetFocusTo(wwin->screen_ptr, wwin);
1302 else
1303 wSetFocusTo(wwin->screen_ptr, NULL);
1304 } else if (!wwin) {
1305 WScreen *scr = wScreenForWindow(event->xfocus.window);
1306 if (scr)
1307 wSetFocusTo(scr, NULL);
1311 static WWindow *windowUnderPointer(WScreen * scr)
1313 unsigned int mask;
1314 int foo;
1315 Window bar, win;
1317 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1318 return wWindowFor(win);
1319 return NULL;
1322 static int CheckFullScreenWindowFocused(WScreen * scr)
1324 if (scr->focused_window && scr->focused_window->flags.fullscreen)
1325 return 1;
1326 else
1327 return 0;
1330 static void handleKeyPress(XEvent * event)
1332 WScreen *scr = wScreenForRootWindow(event->xkey.root);
1333 WWindow *wwin = scr->focused_window;
1334 short i, widx;
1335 int modifiers;
1336 int command = -1;
1337 #ifdef KEEP_XKB_LOCK_STATUS
1338 XkbStateRec staterec;
1339 #endif /*KEEP_XKB_LOCK_STATUS */
1341 /* ignore CapsLock */
1342 modifiers = event->xkey.state & ValidModMask;
1344 for (i = 0; i < WKBD_LAST; i++) {
1345 if (wKeyBindings[i].keycode == 0)
1346 continue;
1348 if (wKeyBindings[i].keycode == event->xkey.keycode && ( /*wKeyBindings[i].modifier==0
1349 || */ wKeyBindings[i].modifier ==
1350 modifiers)) {
1351 command = i;
1352 break;
1356 if (command < 0) {
1358 if (!wRootMenuPerformShortcut(event)) {
1359 static int dontLoop = 0;
1361 if (dontLoop > 10) {
1362 wwarning("problem with key event processing code");
1363 return;
1365 dontLoop++;
1366 /* if the focused window is an internal window, try redispatching
1367 * the event to the managed window, as it can be a WINGs window */
1368 if (wwin && wwin->flags.internal_window && wwin->client_leader != None) {
1369 /* client_leader contains the WINGs toplevel */
1370 event->xany.window = wwin->client_leader;
1371 WMHandleEvent(event);
1373 dontLoop--;
1375 return;
1377 #define ISMAPPED(w) ((w) && !(w)->flags.miniaturized && ((w)->flags.mapped || (w)->flags.shaded))
1378 #define ISFOCUSED(w) ((w) && (w)->flags.focused)
1380 switch (command) {
1382 case WKBD_ROOTMENU:
1383 /*OpenRootMenu(scr, event->xkey.x_root, event->xkey.y_root, True); */
1384 if (!CheckFullScreenWindowFocused(scr)) {
1385 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1386 OpenRootMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1387 True);
1389 break;
1390 case WKBD_WINDOWLIST:
1391 if (!CheckFullScreenWindowFocused(scr)) {
1392 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1393 OpenSwitchMenu(scr, rect.pos.x + rect.size.width / 2, rect.pos.y + rect.size.height / 2,
1394 True);
1396 break;
1398 case WKBD_WINDOWMENU:
1399 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1400 OpenWindowMenu(wwin, wwin->frame_x, wwin->frame_y + wwin->frame->top_width, True);
1401 break;
1402 case WKBD_MINIMIZEALL:
1403 CloseWindowMenu(scr);
1404 wHideAll(scr);
1405 break;
1406 case WKBD_MINIATURIZE:
1407 if (ISMAPPED(wwin) && ISFOCUSED(wwin)
1408 && !WFLAGP(wwin, no_miniaturizable)) {
1409 CloseWindowMenu(scr);
1411 if (wwin->protocols.MINIATURIZE_WINDOW)
1412 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, event->xbutton.time);
1413 else {
1414 wIconifyWindow(wwin);
1417 break;
1418 case WKBD_HIDE:
1419 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1420 WApplication *wapp = wApplicationOf(wwin->main_window);
1421 CloseWindowMenu(scr);
1423 if (wapp && !WFLAGP(wapp->main_window_desc, no_appicon)) {
1424 wHideApplication(wapp);
1427 break;
1428 case WKBD_HIDE_OTHERS:
1429 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1430 CloseWindowMenu(scr);
1432 wHideOtherApplications(wwin);
1434 break;
1435 case WKBD_MAXIMIZE:
1436 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1437 CloseWindowMenu(scr);
1439 handleMaximize(wwin, MAX_VERTICAL | MAX_HORIZONTAL | MAX_KEYBOARD);
1441 break;
1442 case WKBD_VMAXIMIZE:
1443 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1444 CloseWindowMenu(scr);
1446 handleMaximize(wwin, MAX_VERTICAL | MAX_KEYBOARD);
1448 break;
1449 case WKBD_HMAXIMIZE:
1450 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1451 CloseWindowMenu(scr);
1453 handleMaximize(wwin, MAX_HORIZONTAL | MAX_KEYBOARD);
1455 break;
1456 case WKBD_LHMAXIMIZE:
1457 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1458 CloseWindowMenu(scr);
1460 handleMaximize(wwin, MAX_VERTICAL | MAX_LEFTHALF | MAX_KEYBOARD);
1462 break;
1463 case WKBD_RHMAXIMIZE:
1464 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1465 CloseWindowMenu(scr);
1467 handleMaximize(wwin, MAX_VERTICAL | MAX_RIGHTHALF | MAX_KEYBOARD);
1469 break;
1470 case WKBD_THMAXIMIZE:
1471 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1472 CloseWindowMenu(scr);
1474 handleMaximize(wwin, MAX_HORIZONTAL | MAX_TOPHALF | MAX_KEYBOARD);
1476 break;
1477 case WKBD_BHMAXIMIZE:
1478 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1479 CloseWindowMenu(scr);
1481 handleMaximize(wwin, MAX_HORIZONTAL | MAX_BOTTOMHALF | MAX_KEYBOARD);
1483 break;
1484 case WKBD_LTCMAXIMIZE:
1485 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1486 CloseWindowMenu(scr);
1488 handleMaximize(wwin, MAX_LEFTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1490 break;
1491 case WKBD_RTCMAXIMIZE:
1492 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1493 CloseWindowMenu(scr);
1495 handleMaximize(wwin, MAX_RIGHTHALF | MAX_TOPHALF | MAX_KEYBOARD);
1497 break;
1498 case WKBD_LBCMAXIMIZE:
1499 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1500 CloseWindowMenu(scr);
1502 handleMaximize(wwin, MAX_LEFTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1504 break;
1505 case WKBD_RBCMAXIMIZE:
1506 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1507 CloseWindowMenu(scr);
1509 handleMaximize(wwin, MAX_RIGHTHALF | MAX_BOTTOMHALF | MAX_KEYBOARD);
1511 break;
1512 case WKBD_MAXIMUS:
1513 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && IS_RESIZABLE(wwin)) {
1514 CloseWindowMenu(scr);
1516 handleMaximize(wwin, MAX_MAXIMUS | MAX_KEYBOARD);
1518 break;
1519 case WKBD_RAISE:
1520 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1521 CloseWindowMenu(scr);
1523 wRaiseFrame(wwin->frame->core);
1525 break;
1526 case WKBD_LOWER:
1527 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1528 CloseWindowMenu(scr);
1530 wLowerFrame(wwin->frame->core);
1532 break;
1533 case WKBD_RAISELOWER:
1534 /* raise or lower the window under the pointer, not the
1535 * focused one
1537 wwin = windowUnderPointer(scr);
1538 if (wwin)
1539 wRaiseLowerFrame(wwin->frame->core);
1540 break;
1541 case WKBD_SHADE:
1542 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_shadeable)) {
1543 if (wwin->flags.shaded)
1544 wUnshadeWindow(wwin);
1545 else
1546 wShadeWindow(wwin);
1548 break;
1549 case WKBD_MOVERESIZE:
1550 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && (IS_RESIZABLE(wwin) || IS_MOVABLE(wwin))) {
1551 CloseWindowMenu(scr);
1553 wKeyboardMoveResizeWindow(wwin);
1555 break;
1556 case WKBD_CLOSE:
1557 if (ISMAPPED(wwin) && ISFOCUSED(wwin) && !WFLAGP(wwin, no_closable)) {
1558 CloseWindowMenu(scr);
1559 if (wwin->protocols.DELETE_WINDOW)
1560 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, event->xkey.time);
1562 break;
1563 case WKBD_SELECT:
1564 if (ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1565 wSelectWindow(wwin, !wwin->flags.selected);
1567 break;
1568 case WKBD_FOCUSNEXT:
1569 StartWindozeCycle(wwin, event, True, False);
1570 break;
1572 case WKBD_FOCUSPREV:
1573 StartWindozeCycle(wwin, event, False, False);
1574 break;
1576 case WKBD_GROUPNEXT:
1577 StartWindozeCycle(wwin, event, True, True);
1578 break;
1580 case WKBD_GROUPPREV:
1581 StartWindozeCycle(wwin, event, False, True);
1582 break;
1584 case WKBD_WORKSPACE1 ... WKBD_WORKSPACE10:
1585 widx = command - WKBD_WORKSPACE1;
1586 i = (scr->current_workspace / 10) * 10 + widx;
1587 if (wPreferences.ws_advance || i < scr->workspace_count)
1588 wWorkspaceChange(scr, i);
1589 break;
1591 case WKBD_NEXTWORKSPACE:
1592 wWorkspaceRelativeChange(scr, 1);
1593 break;
1594 case WKBD_PREVWORKSPACE:
1595 wWorkspaceRelativeChange(scr, -1);
1596 break;
1597 case WKBD_LASTWORKSPACE:
1598 wWorkspaceChange(scr, scr->last_workspace);
1599 break;
1601 case WKBD_MOVE_WORKSPACE1 ... WKBD_MOVE_WORKSPACE10:
1602 widx = command - WKBD_MOVE_WORKSPACE1;
1603 i = (scr->current_workspace / 10) * 10 + widx;
1604 if (wwin && (wPreferences.ws_advance || i < scr->workspace_count))
1605 wWindowChangeWorkspace(wwin, i);
1606 break;
1608 case WKBD_MOVE_NEXTWORKSPACE:
1609 if (wwin)
1610 wWindowChangeWorkspaceRelative(wwin, 1);
1611 break;
1612 case WKBD_MOVE_PREVWORKSPACE:
1613 if (wwin)
1614 wWindowChangeWorkspaceRelative(wwin, -1);
1615 break;
1616 case WKBD_MOVE_LASTWORKSPACE:
1617 if (wwin)
1618 wWindowChangeWorkspace(wwin, scr->last_workspace);
1619 break;
1621 case WKBD_MOVE_NEXTWSLAYER:
1622 case WKBD_MOVE_PREVWSLAYER:
1624 if (wwin) {
1625 int row, column;
1627 row = scr->current_workspace / 10;
1628 column = scr->current_workspace % 10;
1630 if (command == WKBD_MOVE_NEXTWSLAYER) {
1631 if ((row + 1) * 10 < scr->workspace_count)
1632 wWindowChangeWorkspace(wwin, column + (row + 1) * 10);
1633 } else {
1634 if (row > 0)
1635 wWindowChangeWorkspace(wwin, column + (row - 1) * 10);
1639 break;
1641 case WKBD_WINDOW1:
1642 case WKBD_WINDOW2:
1643 case WKBD_WINDOW3:
1644 case WKBD_WINDOW4:
1645 case WKBD_WINDOW5:
1646 case WKBD_WINDOW6:
1647 case WKBD_WINDOW7:
1648 case WKBD_WINDOW8:
1649 case WKBD_WINDOW9:
1650 case WKBD_WINDOW10:
1652 widx = command - WKBD_WINDOW1;
1654 if (scr->shortcutWindows[widx]) {
1655 WMArray *list = scr->shortcutWindows[widx];
1656 int cw;
1657 int count = WMGetArrayItemCount(list);
1658 WWindow *twin;
1659 WMArrayIterator iter;
1660 WWindow *wwin;
1662 wUnselectWindows(scr);
1663 cw = scr->current_workspace;
1665 WM_ETARETI_ARRAY(list, wwin, iter) {
1666 if (count > 1)
1667 wWindowChangeWorkspace(wwin, cw);
1669 wMakeWindowVisible(wwin);
1671 if (count > 1)
1672 wSelectWindow(wwin, True);
1675 /* rotate the order of windows, to create a cycling effect */
1676 twin = WMGetFromArray(list, 0);
1677 WMDeleteFromArray(list, 0);
1678 WMAddToArray(list, twin);
1680 } else if (wwin && ISMAPPED(wwin) && ISFOCUSED(wwin)) {
1681 if (scr->shortcutWindows[widx]) {
1682 WMFreeArray(scr->shortcutWindows[widx]);
1683 scr->shortcutWindows[widx] = NULL;
1686 if (wwin->flags.selected && scr->selected_windows) {
1687 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1688 /*WMRemoveFromArray(scr->shortcutWindows[index], wwin);
1689 WMInsertInArray(scr->shortcutWindows[index], 0, wwin); */
1690 } else {
1691 scr->shortcutWindows[widx] = WMCreateArray(4);
1692 WMAddToArray(scr->shortcutWindows[widx], wwin);
1695 wSelectWindow(wwin, !wwin->flags.selected);
1696 XFlush(dpy);
1697 wusleep(3000);
1698 wSelectWindow(wwin, !wwin->flags.selected);
1699 XFlush(dpy);
1701 } else if (scr->selected_windows && WMGetArrayItemCount(scr->selected_windows)) {
1703 if (wwin->flags.selected && scr->selected_windows) {
1704 if (scr->shortcutWindows[widx]) {
1705 WMFreeArray(scr->shortcutWindows[widx]);
1707 scr->shortcutWindows[widx] = WMDuplicateArray(scr->selected_windows);
1711 break;
1713 case WKBD_RELAUNCH:
1714 if (ISMAPPED(wwin) && ISFOCUSED(wwin))
1715 (void) RelaunchWindow(wwin);
1717 break;
1719 case WKBD_SWITCH_SCREEN:
1720 if (wScreenCount > 1) {
1721 WScreen *scr2;
1722 int i;
1724 /* find index of this screen */
1725 for (i = 0; i < wScreenCount; i++) {
1726 if (wScreenWithNumber(i) == scr)
1727 break;
1729 i++;
1730 if (i >= wScreenCount) {
1731 i = 0;
1733 scr2 = wScreenWithNumber(i);
1735 if (scr2) {
1736 XWarpPointer(dpy, scr->root_win, scr2->root_win, 0, 0, 0, 0,
1737 scr2->scr_width / 2, scr2->scr_height / 2);
1740 break;
1742 case WKBD_NEXTWSLAYER:
1743 case WKBD_PREVWSLAYER:
1745 int row, column;
1747 row = scr->current_workspace / 10;
1748 column = scr->current_workspace % 10;
1750 if (command == WKBD_NEXTWSLAYER) {
1751 if ((row + 1) * 10 < scr->workspace_count)
1752 wWorkspaceChange(scr, column + (row + 1) * 10);
1753 } else {
1754 if (row > 0)
1755 wWorkspaceChange(scr, column + (row - 1) * 10);
1758 break;
1759 case WKBD_CLIPRAISELOWER:
1760 if (!wPreferences.flags.noclip)
1761 wDockRaiseLower(scr->workspaces[scr->current_workspace]->clip);
1762 break;
1763 case WKBD_DOCKRAISELOWER:
1764 if (!wPreferences.flags.nodock)
1765 wDockRaiseLower(scr->dock);
1766 break;
1767 #ifdef KEEP_XKB_LOCK_STATUS
1768 case WKBD_TOGGLE:
1769 if (wPreferences.modelock) {
1770 /*toggle */
1771 wwin = scr->focused_window;
1773 if (wwin && wwin->flags.mapped
1774 && wwin->frame->workspace == wwin->screen_ptr->current_workspace
1775 && !wwin->flags.miniaturized && !wwin->flags.hidden) {
1776 XkbGetState(dpy, XkbUseCoreKbd, &staterec);
1778 wwin->frame->languagemode = wwin->frame->last_languagemode;
1779 wwin->frame->last_languagemode = staterec.group;
1780 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1784 break;
1785 #endif /* KEEP_XKB_LOCK_STATUS */
1789 static void handleMotionNotify(XEvent * event)
1791 WScreen *scr = wScreenForRootWindow(event->xmotion.root);
1793 if (wPreferences.scrollable_menus) {
1794 WMPoint p = wmkpoint(event->xmotion.x_root, event->xmotion.y_root);
1795 WMRect rect = wGetRectForHead(scr, wGetHeadForPoint(scr, p));
1797 if (scr->flags.jump_back_pending ||
1798 p.x <= (rect.pos.x + 1) ||
1799 p.x >= (rect.pos.x + rect.size.width - 2) ||
1800 p.y <= (rect.pos.y + 1) || p.y >= (rect.pos.y + rect.size.height - 2)) {
1801 WMenu *menu;
1803 menu = wMenuUnderPointer(scr);
1804 if (menu != NULL)
1805 wMenuScroll(menu, event);
1810 static void handleVisibilityNotify(XEvent * event)
1812 WWindow *wwin;
1814 wwin = wWindowFor(event->xvisibility.window);
1815 if (!wwin)
1816 return;
1817 wwin->flags.obscured = (event->xvisibility.state == VisibilityFullyObscured);