bug fix for balloons and some other minor things
[wmaker-crm.git] / src / window.c
blobe4dbcc1d33424059a43309fd227b0390b3b3b5fc
1 /* window.c - client window managing stuffs
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.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "texture.h"
43 #include "window.h"
44 #include "winspector.h"
45 #include "icon.h"
46 #include "properties.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "stacking.h"
52 #include "defaults.h"
53 #include "workspace.h"
55 #ifdef MWM_HINTS
56 # include "motif.h"
57 #endif
58 #ifdef KWM_HINTS
59 # include "kwm.h"
60 #endif
61 #ifdef GNOME_STUFF
62 # include "gnome.h"
63 #endif
64 #ifdef OLWM_HINTS
65 # include "openlook.h"
66 #endif
68 /****** Global Variables ******/
70 extern WShortKey wKeyBindings[WKBD_LAST];
72 #ifdef SHAPE
73 extern Bool wShapeSupported;
74 #endif
76 /* contexts */
77 extern XContext wWinContext;
79 /* cursors */
80 extern Cursor wCursor[WCUR_LAST];
82 /* protocol atoms */
83 extern Atom _XA_WM_DELETE_WINDOW;
84 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
86 extern Atom _XA_WINDOWMAKER_STATE;
88 extern WPreferences wPreferences;
90 #define MOD_MASK wPreferences.modifier_mask
92 extern Time LastTimestamp;
94 /* superfluous... */
95 extern void DoWindowBirth(WWindow*);
98 /***** Local Stuff *****/
101 static WWindowState *windowState=NULL;
105 /* local functions */
106 static FocusMode getFocusMode(WWindow *wwin);
108 static int getSavedState(Window window, WSavedState **state);
110 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
112 /* event handlers */
115 /* frame window (during window grabs) */
116 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
118 /* close button */
119 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
120 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
122 /* iconify button */
123 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
127 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
129 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
132 /****** Notification Observers ******/
134 static void
135 appearanceObserver(void *self, WMNotification *notif)
137 WWindow *wwin = (WWindow*)self;
138 int flags = (int)WMGetNotificationClientData(notif);
140 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
141 return;
143 if (flags & WFontSettings) {
144 wWindowConfigureBorders(wwin);
146 if (flags & WTextureSettings) {
147 wwin->frame->flags.need_texture_remake = 1;
149 if (flags & (WTextureSettings | WColorSettings)) {
150 if (wwin->frame->titlebar)
151 XClearWindow(dpy, wwin->frame->titlebar->window);
153 wFrameWindowPaint(wwin->frame);
157 /************************************/
159 WWindow*
160 wWindowFor(Window window)
162 WObjDescriptor *desc;
164 if (window==None)
165 return NULL;
167 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
168 return NULL;
170 if (desc->parent_type==WCLASS_WINDOW)
171 return desc->parent;
172 else if (desc->parent_type==WCLASS_FRAME) {
173 WFrameWindow *frame = (WFrameWindow*)desc->parent;
174 if (frame->flags.is_client_window_frame)
175 return frame->child;
178 return NULL;
182 WWindow*
183 wWindowCreate()
185 WWindow *wwin;
187 wwin = wmalloc(sizeof(WWindow));
188 wretain(wwin);
190 memset(wwin, 0, sizeof(WWindow));
192 wwin->client_descriptor.handle_mousedown = frameMouseDown;
193 wwin->client_descriptor.parent = wwin;
194 wwin->client_descriptor.self = wwin;
195 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
197 return wwin;
201 void
202 wWindowDestroy(WWindow *wwin)
204 int i;
206 WMRemoveNotificationObserver(wwin);
208 wwin->flags.destroyed = 1;
210 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
211 if (wwin->screen_ptr->shortcutWindow[i] == wwin) {
212 wwin->screen_ptr->shortcutWindow[i] = NULL;
216 if (wwin->normal_hints)
217 free(wwin->normal_hints);
219 if (wwin->wm_hints)
220 XFree(wwin->wm_hints);
222 if (wwin->wm_instance)
223 XFree(wwin->wm_instance);
225 if (wwin->wm_class)
226 XFree(wwin->wm_class);
228 if (wwin->wm_gnustep_attr)
229 free(wwin->wm_gnustep_attr);
231 if (wwin->cmap_windows)
232 XFree(wwin->cmap_windows);
234 XDeleteContext(dpy, wwin->client_win, wWinContext);
236 if (wwin->frame)
237 wFrameWindowDestroy(wwin->frame);
239 if (wwin->icon) {
240 RemoveFromStackList(wwin->icon->core);
241 wIconDestroy(wwin->icon);
242 if (wPreferences.auto_arrange_icons)
243 wArrangeIcons(wwin->screen_ptr, True);
245 wrelease(wwin);
251 static void
252 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
254 if (gs_hints->flags & GSWindowStyleAttr) {
255 wwin->client_flags.no_titlebar =
256 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
258 wwin->client_flags.no_close_button =
259 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
261 wwin->client_flags.no_closable =
262 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
264 wwin->client_flags.no_miniaturize_button =
265 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
267 wwin->client_flags.no_miniaturizable =
268 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
270 wwin->client_flags.no_resizebar =
271 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
273 wwin->client_flags.no_resizable =
274 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
275 } else {
276 /* setup the defaults */
277 wwin->client_flags.no_titlebar = 0;
278 wwin->client_flags.no_closable = 0;
279 wwin->client_flags.no_miniaturizable = 0;
280 wwin->client_flags.no_resizable = 0;
281 wwin->client_flags.no_close_button = 0;
282 wwin->client_flags.no_miniaturize_button = 0;
283 wwin->client_flags.no_resizebar = 0;
285 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
286 wwin->client_flags.no_appicon = 1;
291 void
292 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
293 WWindowAttributes *mask)
295 if (wflags->no_appicon && mask->no_appicon)
296 wflags->emulate_appicon = 0;
298 if (wwin->main_window!=None) {
299 WApplication *wapp = wApplicationOf(wwin->main_window);
300 if (wapp && !wapp->flags.emulated)
301 wflags->emulate_appicon = 0;
304 if (wwin->transient_for!=None
305 && wwin->transient_for!=wwin->screen_ptr->root_win)
306 wflags->emulate_appicon = 0;
308 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
309 wflags->sunken = 0;
314 void
315 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
317 WScreen *scr = wwin->screen_ptr;
319 /* sets global default stuff */
320 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
321 &wwin->client_flags, NULL, True);
323 * Decoration setting is done in this precedence (lower to higher)
324 * - use global default in the resource database
325 * - guess some settings
326 * - use GNUstep/external window attributes
327 * - set hints specified for the app in the resource DB
330 WSETUFLAG(wwin, broken_close, 0);
332 if (wwin->protocols.DELETE_WINDOW)
333 WSETUFLAG(wwin, kill_close, 0);
334 else
335 WSETUFLAG(wwin, kill_close, 1);
337 /* transients can't be iconified or maximized */
338 if (wwin->transient_for) {
339 WSETUFLAG(wwin, no_miniaturizable, 1);
340 WSETUFLAG(wwin, no_miniaturize_button, 1);
343 /* if the window can't be resized, remove the resizebar */
344 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
345 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
346 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
347 WSETUFLAG(wwin, no_resizable, 1);
348 WSETUFLAG(wwin, no_resizebar, 1);
351 /* set GNUstep window attributes */
352 if (wwin->wm_gnustep_attr) {
353 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
355 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
357 switch (wwin->wm_gnustep_attr->window_level) {
358 case WMNormalWindowLevel:
359 *level = WMNormalLevel;
360 break;
361 case WMFloatingWindowLevel:
362 *level = WMFloatingLevel;
363 break;
364 case WMDockWindowLevel:
365 *level = WMDockLevel;
366 break;
367 case WMSubmenuWindowLevel:
368 *level = WMSubmenuLevel;
369 break;
370 case WMMainMenuWindowLevel:
371 *level = WMMainMenuLevel;
372 break;
373 default:
374 *level = WMNormalLevel;
375 break;
377 } else {
378 /* setup defaults */
379 *level = WMNormalLevel;
381 } else {
382 int tmp_workspace = -1;
383 int tmp_level = -1;
385 #ifdef MWM_HINTS
386 wMWMCheckClientHints(wwin);
387 #endif /* MWM_HINTS */
389 #ifdef KWM_HINTS
390 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
391 #endif /* KWM_HINTS */
393 #ifdef GNOME_STUFF
394 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
395 #endif /* GNOME_STUFF */
397 #ifdef OLWM_HINTS
398 wOLWMCheckClientHints(wwin);
399 #endif /* OLWM_HINTS */
401 if (tmp_level < 0) {
402 if (WFLAGP(wwin, floating))
403 *level = WMFloatingLevel;
404 else if (WFLAGP(wwin, sunken))
405 *level = WMSunkenLevel;
406 else
407 *level = WMNormalLevel;
408 } else {
409 *level = tmp_level;
412 if (tmp_workspace >= 0) {
413 *workspace = tmp_workspace % scr->workspace_count;
418 * Set attributes specified only for that window/class.
419 * This might do duplicate work with the 1st wDefaultFillAttributes().
421 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
422 &wwin->user_flags, &wwin->defined_user_flags,
423 False);
425 * Sanity checks for attributes that depend on other attributes
427 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
428 wwin->user_flags.emulate_appicon = 0;
430 if (wwin->main_window!=None) {
431 WApplication *wapp = wApplicationOf(wwin->main_window);
432 if (wapp && !wapp->flags.emulated)
433 wwin->user_flags.emulate_appicon = 0;
436 if (wwin->transient_for!=None
437 && wwin->transient_for!=wwin->screen_ptr->root_win)
438 wwin->user_flags.emulate_appicon = 0;
440 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
441 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
442 wwin->user_flags.sunken = 0;
444 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
450 Bool
451 wWindowCanReceiveFocus(WWindow *wwin)
453 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
454 return False;
455 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
456 return False;
457 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
458 return False;
460 return True;
464 Bool
465 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
467 int w1, h1, w2, h2;
469 w1 = wwin->frame->core->width;
470 h1 = wwin->frame->core->height;
471 w2 = obscured->frame->core->width;
472 h2 = obscured->frame->core->height;
474 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
475 && wwin->frame->workspace != obscured->frame->workspace)
476 return False;
478 if (wwin->frame_x + w1 < obscured->frame_x
479 || wwin->frame_y + h1 < obscured->frame_y
480 || wwin->frame_x > obscured->frame_x + w2
481 || wwin->frame_y > obscured->frame_y + h2) {
482 return False;
485 return True;
490 *----------------------------------------------------------------
491 * wManageWindow--
492 * reparents the window and allocates a descriptor for it.
493 * Window manager hints and other hints are fetched to configure
494 * the window decoration attributes and others. User preferences
495 * for the window are used if available, to configure window
496 * decorations and some behaviour.
497 * If in startup, windows that are override redirect,
498 * unmapped and never were managed and are Withdrawn are not
499 * managed.
501 * Returns:
502 * the new window descriptor
504 * Side effects:
505 * The window is reparented and appropriate notification
506 * is done to the client. Input mask for the window is setup.
507 * The window descriptor is also associated with various window
508 * contexts and inserted in the head of the window list.
509 * Event handler contexts are associated for some objects
510 * (buttons, titlebar and resizebar)
512 *----------------------------------------------------------------
514 WWindow*
515 wManageWindow(WScreen *scr, Window window)
517 WWindow *wwin;
518 int x, y;
519 unsigned width, height;
520 XWindowAttributes wattribs;
521 XSetWindowAttributes attribs;
522 WWindowState *win_state;
523 WWindow *transientOwner = NULL;
524 int window_level;
525 int wm_state;
526 int foo;
527 int workspace = -1;
528 char *title;
529 Bool withdraw = False;
531 /* mutex. */
532 /* XGrabServer(dpy); */
533 XSync(dpy, False);
534 /* make sure the window is still there */
535 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
536 XUngrabServer(dpy);
537 return NULL;
540 /* if it's an override-redirect, ignore it */
541 if (wattribs.override_redirect) {
542 XUngrabServer(dpy);
543 return NULL;
546 wm_state = PropGetWindowState(window);
548 /* if it's startup and the window is unmapped, don't manage it */
549 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
550 XUngrabServer(dpy);
551 return NULL;
554 if (!wFetchName(dpy, window, &title)) {
555 title = NULL;
558 #ifdef KWM_HINTS
559 if (title && !wKWMManageableClient(scr, window, title)) {
560 XFree(title);
561 XUngrabServer(dpy);
562 return NULL;
564 #endif /* KWM_HINTS */
567 wwin = wWindowCreate();
569 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
571 #ifdef DEBUG
572 printf("managing window %x\n", (unsigned)window);
573 #endif
575 #ifdef SHAPE
576 if (wShapeSupported) {
577 int junk;
578 unsigned int ujunk;
579 int b_shaped;
581 XShapeSelectInput(dpy, window, ShapeNotifyMask);
582 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
583 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
584 wwin->flags.shaped = b_shaped;
586 #endif
589 *--------------------------------------------------
591 * Get hints and other information in properties
593 *--------------------------------------------------
595 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
597 /* setup descriptor */
598 wwin->client_win = window;
599 wwin->screen_ptr = scr;
601 wwin->old_border_width = wattribs.border_width;
603 wwin->event_mask = CLIENT_EVENTS;
604 attribs.event_mask = CLIENT_EVENTS;
605 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
606 attribs.save_under = False;
607 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
608 |CWSaveUnder, &attribs);
609 XSetWindowBorderWidth(dpy, window, 0);
611 /* get hints from GNUstep app */
612 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
613 wwin->wm_gnustep_attr = NULL;
616 wwin->client_leader = PropGetClientLeader(window);
617 if (wwin->client_leader!=None)
618 wwin->main_window = wwin->client_leader;
620 wwin->wm_hints = XGetWMHints(dpy, window);
622 if (wwin->wm_hints) {
623 if (wwin->wm_hints->flags & StateHint) {
625 if (wwin->wm_hints->initial_state == IconicState) {
627 wwin->flags.miniaturized = 1;
629 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
631 withdraw = True;
635 if (wwin->wm_hints->flags & WindowGroupHint) {
636 wwin->group_id = wwin->wm_hints->window_group;
637 /* window_group has priority over CLIENT_LEADER */
638 wwin->main_window = wwin->group_id;
639 } else {
640 wwin->group_id = None;
643 if (wwin->wm_hints->flags & UrgencyHint)
644 wwin->flags.urgent = 1;
645 } else {
646 wwin->group_id = None;
649 PropGetProtocols(window, &wwin->protocols);
651 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
652 wwin->transient_for = None;
653 } else {
654 if (wwin->transient_for==None || wwin->transient_for==window) {
655 wwin->transient_for = scr->root_win;
656 } else {
657 transientOwner = wWindowFor(wwin->transient_for);
658 if (transientOwner && transientOwner->main_window!=None) {
659 wwin->main_window = transientOwner->main_window;
660 } /*else {
661 wwin->main_window = None;
666 /* guess the focus mode */
667 wwin->focus_mode = getFocusMode(wwin);
669 /* get geometry stuff */
670 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
672 /* get colormap windows */
673 GetColormapWindows(wwin);
676 *--------------------------------------------------
678 * Setup the decoration/window attributes and
679 * geometry
681 *--------------------------------------------------
684 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
686 #ifdef OLWM_HINTS
687 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
688 && wwin->group_id != None && wwin->group_id != window) {
690 transientOwner = wWindowFor(wwin->group_id);
692 if (transientOwner) {
693 wwin->transient_for = wwin->group_id;
695 /* transients can't be iconified or maximized */
696 if (wwin->transient_for) {
697 WSETUFLAG(wwin, no_miniaturizable, 1);
698 WSETUFLAG(wwin, no_miniaturize_button, 1);
702 #endif /* OLWM_HINTS */
705 * Make broken apps behave as a nice app.
707 if (WFLAGP(wwin, emulate_appicon)) {
708 wwin->main_window = wwin->client_win;
712 *------------------------------------------------------------
714 * Setup the initial state of the window
716 *------------------------------------------------------------
719 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
720 wwin->flags.miniaturized = 1;
723 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
724 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
727 #ifdef GNOME_STUFF
728 wGNOMECheckInitialClientState(wwin);
729 #endif
730 #ifdef KWM_HINTS
731 wKWMCheckClientInitialState(wwin);
732 #endif
734 /* apply previous state if it exists and we're in startup */
735 if (scr->flags.startup && wm_state >= 0) {
737 if (wm_state == IconicState) {
739 wwin->flags.miniaturized = 1;
741 } else if (wm_state == WithdrawnState) {
743 withdraw = True;
747 /* if there is a saved state (from file), restore it */
748 win_state = NULL;
749 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
750 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
751 } else {
752 win_state = (WWindowState*)wWindowGetSavedState(window);
754 if (win_state && !withdraw) {
756 if (win_state->state->hidden>0)
757 wwin->flags.hidden = win_state->state->hidden;
759 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
760 wwin->flags.shaded = win_state->state->shaded;
762 if (win_state->state->miniaturized>0 &&
763 !WFLAGP(wwin, no_miniaturizable)) {
764 wwin->flags.miniaturized = win_state->state->miniaturized;
766 if (!IS_OMNIPRESENT(wwin)) {
767 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
768 wwin->wm_class);
769 if (w < 0 || w >= scr->workspace_count) {
770 workspace = win_state->state->workspace;
771 if (workspace >= scr->workspace_count)
772 workspace = scr->current_workspace;
773 } else {
774 workspace = w;
776 } else {
777 workspace = scr->current_workspace;
781 /* if we're restarting, restore saved state (from hints).
782 * This will overwrite previous */
784 WSavedState *wstate;
786 if (getSavedState(window, &wstate)) {
787 wwin->flags.shaded = wstate->shaded;
788 wwin->flags.hidden = wstate->hidden;
789 wwin->flags.miniaturized = wstate->miniaturized;
790 workspace = wstate->workspace;
792 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
793 int i;
795 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
796 if (wstate->window_shortcuts & (1<<i))
797 scr->shortcutWindow[i] = wwin;
800 free(wstate);
804 /* don't let transients start miniaturized if their owners are not */
805 if (transientOwner && !transientOwner->flags.miniaturized
806 && wwin->flags.miniaturized && !withdraw) {
807 wwin->flags.miniaturized = 0;
808 if (wwin->wm_hints)
809 wwin->wm_hints->initial_state = NormalState;
812 /* set workspace on which the window starts */
813 if (workspace >= 0) {
814 if (workspace > scr->workspace_count-1) {
815 workspace = workspace % scr->workspace_count;
817 } else {
818 int w;
820 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
822 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
824 workspace = w;
826 } else {
827 if (wPreferences.open_transients_with_parent && transientOwner) {
829 workspace = transientOwner->frame->workspace;
831 } else {
833 workspace = scr->current_workspace;
838 /* setup window geometry */
839 if (win_state && win_state->state->use_geometry) {
840 width = win_state->state->w;
841 height = win_state->state->h;
843 wWindowConstrainSize(wwin, &width, &height);
845 /* do not ask for window placement if the window is
846 * transient, during startup, if the initial workspace is another one
847 * or if the window wants to start iconic.
848 * If geometry was saved, restore it. */
850 Bool dontBring = False;
852 if (win_state && win_state->state->use_geometry) {
853 x = win_state->state->x;
854 y = win_state->state->y;
855 } else if ((wwin->transient_for==None
856 || wPreferences.window_placement!=WPM_MANUAL)
857 && !scr->flags.startup
858 && workspace == scr->current_workspace
859 && !wwin->flags.miniaturized
860 && !wwin->flags.maximized
861 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
862 PlaceWindow(wwin, &x, &y, width, height);
863 if (wPreferences.window_placement == WPM_MANUAL)
864 dontBring = True;
867 if (WFLAGP(wwin, dont_move_off) && dontBring)
868 wScreenBringInside(scr, &x, &y, width, height);
871 if (wwin->flags.urgent) {
872 if (!IS_OMNIPRESENT(wwin))
873 wwin->flags.omnipresent ^= 1;
877 *--------------------------------------------------
879 * Create frame, borders and do reparenting
881 *--------------------------------------------------
883 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
884 if (!WFLAGP(wwin, no_titlebar))
885 foo |= WFF_TITLEBAR;
886 if (!WFLAGP(wwin, no_resizebar))
887 foo |= WFF_RESIZEBAR;
889 wwin->frame = wFrameWindowCreate(scr, window_level,
890 x, y, width, height, foo,
891 scr->window_title_texture,
892 scr->resizebar_texture,
893 scr->window_title_pixel,
894 &scr->window_title_gc,
895 &scr->title_font);
897 wwin->frame->flags.is_client_window_frame = 1;
898 wwin->frame->flags.justification = wPreferences.title_justification;
900 /* setup button images */
901 wWindowUpdateButtonImages(wwin);
903 /* hide unused buttons */
904 foo = 0;
905 if (WFLAGP(wwin, no_close_button))
906 foo |= WFF_RIGHT_BUTTON;
907 if (WFLAGP(wwin, no_miniaturize_button))
908 foo |= WFF_LEFT_BUTTON;
909 if (foo!=0)
910 wFrameWindowHideButton(wwin->frame, foo);
912 wwin->frame->child = wwin;
914 #ifdef OLWM_HINTS
915 /* emulate olwm push pin. Make the button look as pushed-in for
916 * the pinned-out state. When the button is clicked, it will
917 * revert to the normal position, which means the pin is pinned-in.
919 if (wwin->flags.olwm_push_pin_out)
920 wFrameWindowUpdatePushButton(wwin->frame, True);
921 #endif /* OLWM_HINTS */
923 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
924 if (title)
925 XFree(title);
927 wwin->frame->workspace = workspace;
929 wwin->frame->on_click_left = windowIconifyClick;
931 wwin->frame->on_click_right = windowCloseClick;
932 wwin->frame->on_dblclick_right = windowCloseDblClick;
934 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
935 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
937 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
940 XSelectInput(dpy, wwin->client_win,
941 wwin->event_mask & ~StructureNotifyMask);
943 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
944 0, wwin->frame->top_width);
946 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
950 int gx, gy;
952 wClientGetGravityOffsets(wwin, &gx, &gy);
954 /* if gravity is to the south, account for the border sizes */
955 if (gy > 0)
956 y -= wwin->frame->top_width + wwin->frame->bottom_width;
960 * wWindowConfigure() will init the client window's size
961 * (wwin->client.{width,height}) and all other geometry
962 * related variables (frame_x,frame_y)
964 wWindowConfigure(wwin, x, y, width, height);
966 /* to make sure the window receives it's new position after reparenting */
967 wWindowSynthConfigureNotify(wwin);
970 *--------------------------------------------------
972 * Setup descriptors and save window to internal
973 * lists
975 *--------------------------------------------------
978 if (wwin->main_window!=None) {
979 WApplication *app;
980 WWindow *leader;
982 /* Leader windows do not necessary set themselves as leaders.
983 * If this is the case, point the leader of this window to
984 * itself */
985 leader = wWindowFor(wwin->main_window);
986 if (leader && leader->main_window==None) {
987 leader->main_window = leader->client_win;
989 app = wApplicationCreate(scr, wwin->main_window);
990 if (app) {
991 app->last_workspace = workspace;
994 * Do application specific stuff, like setting application
995 * wide attributes.
998 if (wwin->flags.hidden) {
999 /* if the window was set to hidden because it was hidden
1000 * in a previous incarnation and that state was restored */
1001 app->flags.hidden = 1;
1004 if (app->flags.hidden) {
1005 wwin->flags.hidden = 1;
1010 /* setup the frame descriptor */
1011 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1012 wwin->frame->core->descriptor.parent = wwin;
1013 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1015 /* don't let windows go away if we die */
1016 XAddToSaveSet(dpy, window);
1018 XLowerWindow(dpy, window);
1020 /* if window is in this workspace and should be mapped, then map it */
1021 if (!wwin->flags.miniaturized
1022 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1023 && !wwin->flags.hidden && !withdraw) {
1025 /* The following "if" is to avoid crashing of clients that expect
1026 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1027 * after the return from this function.
1029 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1030 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1031 } else {
1032 wClientSetState(wwin, NormalState, None);
1035 #if 0
1036 /* if not auto focus, then map the window under the currently
1037 * focused window */
1038 #define _WIDTH(w) (w)->frame->core->width
1039 #define _HEIGHT(w) (w)->frame->core->height
1040 if (!wPreferences.auto_focus && scr->focused_window
1041 && !scr->flags.startup && !transientOwner
1042 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1043 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1044 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1045 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1046 || wwin->flags.maximized)) {
1047 MoveInStackListUnder(scr->focused_window->frame->core,
1048 wwin->frame->core);
1050 #undef _WIDTH
1051 #undef _HEIGHT
1053 #endif
1055 if (wPreferences.superfluous && !wPreferences.no_animations
1056 && !scr->flags.startup && wwin->transient_for==None
1058 * The brain damaged idiotic non-click to focus modes will
1059 * have trouble with this because:
1061 * 1. window is created and mapped by the client
1062 * 2. window is mapped by wmaker in small size
1063 * 3. window is animated to grow to normal size
1064 * 4. this function returns to normal event loop
1065 * 5. eventually, the EnterNotify event that would trigger
1066 * the window focusing (if the mouse is over that window)
1067 * will be processed by wmaker.
1068 * But since this event will be rather delayed
1069 * (step 3 has a large delay) the time when the event ocurred
1070 * and when it is processed, the client that owns that window
1071 * will reject the XSetInputFocus() for it.
1073 && (wPreferences.focus_mode==WKF_CLICK
1074 || wPreferences.auto_focus)) {
1075 DoWindowBirth(wwin);
1078 wWindowMap(wwin);
1081 /* setup stacking descriptor */
1082 if (transientOwner) {
1083 /* && wPreferences.on_top_transients */
1084 if (transientOwner) {
1085 wwin->frame->core->stacking->child_of =
1086 transientOwner->frame->core;
1088 } else {
1089 wwin->frame->core->stacking->child_of = NULL;
1093 if (!scr->focused_window) {
1094 /* first window on the list */
1095 wwin->next = NULL;
1096 wwin->prev = NULL;
1097 scr->focused_window = wwin;
1098 } else {
1099 WWindow *tmp;
1101 /* add window at beginning of focus window list */
1102 tmp = scr->focused_window;
1103 while (tmp->prev)
1104 tmp = tmp->prev;
1105 tmp->prev = wwin;
1106 wwin->next = tmp;
1107 wwin->prev = NULL;
1110 #ifdef GNOME_STUFF
1111 wGNOMEUpdateClientStateHint(wwin, True);
1112 #endif
1113 #ifdef KWM_HINTS
1114 wKWMUpdateClientWorkspace(wwin);
1115 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1116 #endif
1118 XUngrabServer(dpy);
1121 *--------------------------------------------------
1123 * Final preparations before window is ready to go
1125 *--------------------------------------------------
1128 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1131 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1132 && !wwin->flags.hidden) {
1133 if ((transientOwner && transientOwner->flags.focused)
1134 || wPreferences.auto_focus)
1135 wSetFocusTo(scr, wwin);
1137 wWindowResetMouseGrabs(wwin);
1139 if (!WFLAGP(wwin, no_bind_keys)) {
1140 wWindowSetKeyGrabs(wwin);
1142 #ifdef GNOME_STUFF
1143 wGNOMEUpdateClientListHint(scr);
1144 #endif
1145 #ifdef KWM_HINTS
1146 wwin->flags.kwm_managed = 1;
1148 wKWMSendEventMessage(wwin, WKWMAddWindow);
1149 #endif
1151 wColormapInstallForWindow(scr, scr->cmap_window);
1153 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1155 #ifdef OLWM_HINTS
1156 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1157 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1159 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1160 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1161 wwin->frame_y);
1163 #endif
1166 *------------------------------------------------------------
1167 * Setup Notification Observers
1168 *------------------------------------------------------------
1170 WMAddNotificationObserver(appearanceObserver, wwin,
1171 WNWindowAppearanceSettingsChanged, wwin);
1175 *--------------------------------------------------
1177 * Cleanup temporary stuff
1179 *--------------------------------------------------
1182 if (win_state)
1183 wWindowDeleteSavedState(win_state);
1185 /* If the window must be withdrawed, then do it now.
1186 * Must do some optimization, 'though */
1187 if (withdraw) {
1188 wwin->flags.mapped = 0;
1189 wClientSetState(wwin, WithdrawnState, None);
1190 wUnmanageWindow(wwin, True, False);
1191 wwin = NULL;
1194 return wwin;
1201 WWindow*
1202 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1203 char *title, int x, int y, int width, int height)
1205 WWindow *wwin;
1206 int foo;
1208 wwin = wWindowCreate();
1210 WMAddNotificationObserver(appearanceObserver, wwin,
1211 WNWindowAppearanceSettingsChanged, wwin);
1213 wwin->flags.internal_window = 1;
1215 WSETUFLAG(wwin, omnipresent, 1);
1216 WSETUFLAG(wwin, no_shadeable, 1);
1217 WSETUFLAG(wwin, no_resizable, 1);
1218 WSETUFLAG(wwin, no_miniaturizable, 1);
1220 wwin->focus_mode = WFM_PASSIVE;
1222 wwin->client_win = window;
1223 wwin->screen_ptr = scr;
1225 wwin->transient_for = owner;
1227 wwin->client.x = x;
1228 wwin->client.y = y;
1229 wwin->client.width = width;
1230 wwin->client.height = height;
1232 wwin->frame_x = wwin->client.x;
1233 wwin->frame_y = wwin->client.y;
1236 foo = WFF_RIGHT_BUTTON;
1237 foo |= WFF_TITLEBAR;
1239 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1240 wwin->frame_x, wwin->frame_y,
1241 width, height, foo,
1242 scr->window_title_texture,
1243 scr->resizebar_texture,
1244 scr->window_title_pixel,
1245 &scr->window_title_gc,
1246 &scr->title_font);
1248 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1250 wwin->frame->flags.is_client_window_frame = 1;
1251 wwin->frame->flags.justification = wPreferences.title_justification;
1253 wFrameWindowChangeTitle(wwin->frame, title);
1255 /* setup button images */
1256 wWindowUpdateButtonImages(wwin);
1258 /* hide buttons */
1259 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1261 wwin->frame->child = wwin;
1263 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1265 wwin->frame->on_click_right = windowCloseClick;
1267 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1268 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1270 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1272 wwin->client.y += wwin->frame->top_width;
1273 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1274 0, wwin->frame->top_width);
1276 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1277 wwin->client.width, wwin->client.height);
1279 /* setup the frame descriptor */
1280 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1281 wwin->frame->core->descriptor.parent = wwin;
1282 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1285 XLowerWindow(dpy, window);
1286 XMapSubwindows(dpy, wwin->frame->core->window);
1288 /* setup stacking descriptor */
1289 if (
1290 #ifdef removed
1291 wPreferences.on_top_transients &&
1292 #endif
1293 wwin->transient_for!=None
1294 && wwin->transient_for!=scr->root_win) {
1295 WWindow *tmp;
1296 tmp = wWindowFor(wwin->transient_for);
1297 if (tmp)
1298 wwin->frame->core->stacking->child_of = tmp->frame->core;
1299 } else {
1300 wwin->frame->core->stacking->child_of = NULL;
1304 if (!scr->focused_window) {
1305 /* first window on the list */
1306 wwin->next = NULL;
1307 wwin->prev = NULL;
1308 scr->focused_window = wwin;
1309 } else {
1310 WWindow *tmp;
1312 /* add window at beginning of focus window list */
1313 tmp = scr->focused_window;
1314 while (tmp->prev)
1315 tmp = tmp->prev;
1316 tmp->prev = wwin;
1317 wwin->next = tmp;
1318 wwin->prev = NULL;
1321 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1323 /* if (wPreferences.auto_focus)*/
1324 wSetFocusTo(scr, wwin);
1326 wWindowResetMouseGrabs(wwin);
1328 wWindowSetKeyGrabs(wwin);
1330 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1332 return wwin;
1337 *----------------------------------------------------------------------
1338 * wUnmanageWindow--
1339 * Removes the frame window from a window and destroys all data
1340 * related to it. The window will be reparented back to the root window
1341 * if restore is True.
1343 * Side effects:
1344 * Everything related to the window is destroyed and the window
1345 * is removed from the window lists. Focus is set to the previous on the
1346 * window list.
1347 *----------------------------------------------------------------------
1349 void
1350 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1352 WCoreWindow *frame = wwin->frame->core;
1353 WWindow *owner = NULL;
1354 WWindow *newFocusedWindow = NULL;
1355 int wasFocused;
1356 WScreen *scr = wwin->screen_ptr;
1359 #ifdef KWM_HINTS
1360 wwin->frame->workspace = -1;
1362 wKWMUpdateClientWorkspace(wwin);
1363 #endif
1365 /* First close attribute editor window if open */
1366 if (wwin->flags.inspector_open) {
1367 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1368 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1371 /* Close window menu if it's open for this window */
1372 if (wwin->flags.menu_open_for_me) {
1373 CloseWindowMenu(scr);
1376 if (!destroyed) {
1377 if (!wwin->flags.internal_window)
1378 XRemoveFromSaveSet(dpy, wwin->client_win);
1380 XSelectInput(dpy, wwin->client_win, NoEventMask);
1382 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1383 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1386 XUnmapWindow(dpy, frame->window);
1388 XUnmapWindow(dpy, wwin->client_win);
1390 /* deselect window */
1391 wSelectWindow(wwin, False);
1393 /* remove all pending events on window */
1394 /* I think this only matters for autoraise */
1395 if (wPreferences.raise_delay)
1396 WMDeleteTimerWithClientData(wwin->frame->core);
1398 XFlush(dpy);
1400 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1402 /* reparent the window back to the root */
1403 if (restore)
1404 wClientRestore(wwin);
1406 if (wwin->transient_for!=scr->root_win) {
1407 owner = wWindowFor(wwin->transient_for);
1408 if (owner) {
1409 if (!owner->flags.semi_focused) {
1410 owner = NULL;
1411 } else {
1412 owner->flags.semi_focused = 0;
1417 wasFocused = wwin->flags.focused;
1419 /* remove from window focus list */
1420 if (!wwin->prev && !wwin->next) {
1421 /* was the only window */
1422 scr->focused_window = NULL;
1423 newFocusedWindow = NULL;
1424 } else {
1425 WWindow *tmp;
1427 if (wwin->prev)
1428 wwin->prev->next = wwin->next;
1429 if (wwin->next)
1430 wwin->next->prev = wwin->prev;
1431 else {
1432 scr->focused_window = wwin->prev;
1433 scr->focused_window->next = NULL;
1436 /* if in click to focus mode and the window
1437 * was a transient, focus the owner window
1439 tmp = NULL;
1440 if (wPreferences.focus_mode==WKF_CLICK) {
1441 tmp = wWindowFor(wwin->transient_for);
1442 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1443 tmp = NULL;
1446 /* otherwise, focus the next one in the focus list */
1447 if (!tmp) {
1448 tmp = scr->focused_window;
1449 while (tmp) {
1450 if (!WFLAGP(tmp, no_focusable)
1451 && (tmp->flags.mapped || tmp->flags.shaded))
1452 break;
1453 tmp = tmp->prev;
1456 if (wPreferences.focus_mode==WKF_CLICK) {
1457 newFocusedWindow = tmp;
1458 } else if (wPreferences.focus_mode==WKF_SLOPPY
1459 || wPreferences.focus_mode==WKF_POINTER) {
1460 unsigned int mask;
1461 int foo;
1462 Window bar, win;
1464 /* This is to let the root window get the keyboard input
1465 * if Sloppy focus mode and no other window get focus.
1466 * This way keybindings will not freeze.
1468 tmp = NULL;
1469 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1470 &foo, &foo, &foo, &foo, &mask))
1471 tmp = wWindowFor(win);
1472 if (tmp == wwin)
1473 tmp = NULL;
1474 newFocusedWindow = tmp;
1475 } else {
1476 newFocusedWindow = NULL;
1480 if (!wwin->flags.internal_window) {
1481 #ifdef GNOME_STUFF
1482 wGNOMERemoveClient(wwin);
1483 #endif
1484 #ifdef KWM_HINTS
1485 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1486 #endif
1489 #ifdef DEBUG
1490 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1491 (unsigned)frame->window);
1492 #endif
1494 if (wasFocused) {
1495 if (newFocusedWindow != owner && owner) {
1496 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1498 wSetFocusTo(scr, newFocusedWindow);
1500 wWindowDestroy(wwin);
1501 XFlush(dpy);
1505 void
1506 wWindowMap(WWindow *wwin)
1508 XMapWindow(dpy, wwin->frame->core->window);
1509 if (!wwin->flags.shaded) {
1510 /* window will be remapped when getting MapNotify */
1511 XSelectInput(dpy, wwin->client_win,
1512 wwin->event_mask & ~StructureNotifyMask);
1513 XMapWindow(dpy, wwin->client_win);
1514 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1516 wwin->flags.mapped = 1;
1521 void
1522 wWindowUnmap(WWindow *wwin)
1524 wwin->flags.mapped = 0;
1526 /* prevent window withdrawal when getting UnmapNotify */
1527 XSelectInput(dpy, wwin->client_win,
1528 wwin->event_mask & ~StructureNotifyMask);
1529 XUnmapWindow(dpy, wwin->client_win);
1530 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1532 XUnmapWindow(dpy, wwin->frame->core->window);
1537 void
1538 wWindowFocus(WWindow *wwin, WWindow *owin)
1540 WWindow *nowner;
1541 WWindow *oowner;
1543 #ifdef KEEP_XKB_LOCK_STATUS
1544 if (wPreferences.modelock) {
1545 if (!wwin->flags.focused) {
1546 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1549 #endif /* KEEP_XKB_LOCK_STATUS */
1551 wwin->flags.semi_focused = 0;
1553 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1555 wwin->flags.focused = 1;
1557 wWindowResetMouseGrabs(wwin);
1559 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1561 if (owin == wwin || !owin)
1562 return;
1564 nowner = wWindowFor(wwin->transient_for);
1566 /* new window is a transient for the old window */
1567 if (nowner == owin) {
1568 owin->flags.semi_focused = 1;
1569 wWindowUnfocus(nowner);
1570 return;
1573 oowner = wWindowFor(owin->transient_for);
1575 /* new window is owner of old window */
1576 if (wwin == oowner) {
1577 wWindowUnfocus(owin);
1578 return;
1581 if (!nowner) {
1582 wWindowUnfocus(owin);
1583 return;
1586 /* new window has same owner of old window */
1587 if (oowner == nowner) {
1588 /* prevent unfocusing of owner */
1589 oowner->flags.semi_focused = 0;
1590 wWindowUnfocus(owin);
1591 oowner->flags.semi_focused = 1;
1593 return;
1596 /* nowner != NULL && oowner != nowner */
1597 nowner->flags.semi_focused = 1;
1598 wWindowUnfocus(nowner);
1599 wWindowUnfocus(owin);
1603 void
1604 wWindowUnfocus(WWindow *wwin)
1606 CloseWindowMenu(wwin->screen_ptr);
1608 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1609 ? WS_PFOCUSED : WS_UNFOCUSED);
1611 if (wwin->transient_for!=None
1612 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1613 WWindow *owner;
1614 owner = wWindowFor(wwin->transient_for);
1615 if (owner && owner->flags.semi_focused) {
1616 owner->flags.semi_focused = 0;
1617 if (owner->flags.mapped || owner->flags.shaded) {
1618 wWindowUnfocus(owner);
1619 wFrameWindowPaint(owner->frame);
1623 wwin->flags.focused = 0;
1625 wWindowResetMouseGrabs(wwin);
1627 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1634 *----------------------------------------------------------------------
1636 * wWindowConstrainSize--
1637 * Constrains size for the client window, taking the maximal size,
1638 * window resize increments and other size hints into account.
1640 * Returns:
1641 * The closest size to what was given that the client window can
1642 * have.
1644 *----------------------------------------------------------------------
1646 void
1647 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1649 XSizeHints *sizeh = wwin->normal_hints;
1650 int width = *nwidth;
1651 int height = *nheight;
1652 int winc = sizeh->width_inc;
1653 int hinc = sizeh->height_inc;
1655 if (width < sizeh->min_width)
1656 width = sizeh->min_width;
1657 if (height < sizeh->min_height)
1658 height = sizeh->min_height;
1660 if (width > sizeh->max_width)
1661 width = sizeh->max_width;
1662 if (height > sizeh->max_height)
1663 height = sizeh->max_height;
1665 /* aspect ratio code borrowed from olwm */
1666 if (sizeh->flags & PAspect) {
1667 /* adjust max aspect ratio */
1668 if (!(sizeh->max_aspect.x==1 && sizeh->max_aspect.y==1)
1669 && width * sizeh->max_aspect.y > height * sizeh->max_aspect.x) {
1670 if (sizeh->max_aspect.x > sizeh->max_aspect.y) {
1671 height = (width * sizeh->max_aspect.y) / sizeh->max_aspect.x;
1672 if (height > sizeh->max_height) {
1673 height = sizeh->max_height;
1674 width = (height*sizeh->max_aspect.x) / sizeh->max_aspect.y;
1676 } else {
1677 width = (height * sizeh->max_aspect.x) / sizeh->max_aspect.y;
1678 if (width > sizeh->max_width) {
1679 width = sizeh->max_width;
1680 height = (width*sizeh->max_aspect.y) / sizeh->max_aspect.x;
1685 /* adjust min aspect ratio */
1686 if (!(sizeh->min_aspect.x==1 && sizeh->min_aspect.y==1)
1687 && width * sizeh->min_aspect.y < height * sizeh->min_aspect.x) {
1688 if (sizeh->min_aspect.x > sizeh->min_aspect.y) {
1689 height = (width * sizeh->min_aspect.y) / sizeh->min_aspect.x;
1690 if (height < sizeh->min_height) {
1691 height = sizeh->min_height;
1692 width = (height*sizeh->min_aspect.x) / sizeh->min_aspect.y;
1694 } else {
1695 width = (height * sizeh->min_aspect.x) / sizeh->min_aspect.y;
1696 if (width < sizeh->min_width) {
1697 width = sizeh->min_width;
1698 height = (width*sizeh->min_aspect.y) / sizeh->min_aspect.x;
1704 if (sizeh->base_width != 0) {
1705 width = (((width - sizeh->base_width) / winc) * winc)
1706 + sizeh->base_width;
1707 } else {
1708 width = (((width - sizeh->min_width) / winc) * winc)
1709 + sizeh->min_width;
1712 if (sizeh->base_width != 0) {
1713 height = (((height - sizeh->base_height) / hinc) * hinc)
1714 + sizeh->base_height;
1715 } else {
1716 height = (((height - sizeh->min_height) / hinc) * hinc)
1717 + sizeh->min_height;
1720 *nwidth = width;
1721 *nheight = height;
1725 void
1726 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1728 WScreen *scr = wwin->screen_ptr;
1729 WApplication *wapp;
1730 int unmap = 0;
1732 if (workspace >= scr->workspace_count || workspace < 0
1733 || workspace == wwin->frame->workspace)
1734 return;
1736 if (workspace != scr->current_workspace) {
1737 /* Sent to other workspace. Unmap window */
1738 if ((wwin->flags.mapped
1739 || wwin->flags.shaded
1740 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1741 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1743 wapp = wApplicationOf(wwin->main_window);
1744 if (wapp) {
1745 wapp->last_workspace = workspace;
1747 if (wwin->flags.miniaturized) {
1748 XUnmapWindow(dpy, wwin->icon->core->window);
1749 wwin->icon->mapped = 0;
1750 } else {
1751 unmap = 1;
1752 wSetFocusTo(scr, NULL);
1755 } else {
1756 /* brought to current workspace. Map window */
1757 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1758 XMapWindow(dpy, wwin->icon->core->window);
1759 wwin->icon->mapped = 1;
1760 } else if (!wwin->flags.mapped &&
1761 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1762 wWindowMap(wwin);
1765 if (!IS_OMNIPRESENT(wwin)) {
1766 wwin->frame->workspace = workspace;
1767 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1769 #ifdef GNOME_STUFF
1770 wGNOMEUpdateClientStateHint(wwin, True);
1771 #endif
1772 #ifdef KWM_HINTS
1773 wKWMUpdateClientWorkspace(wwin);
1774 wKWMSendEventMessage(wwin, WKWMChangedClient);
1775 #endif
1776 if (unmap) {
1777 wWindowUnmap(wwin);
1782 void
1783 wWindowSynthConfigureNotify(WWindow *wwin)
1785 XEvent sevent;
1787 sevent.type = ConfigureNotify;
1788 sevent.xconfigure.display = dpy;
1789 sevent.xconfigure.event = wwin->client_win;
1790 sevent.xconfigure.window = wwin->client_win;
1792 sevent.xconfigure.x = wwin->client.x;
1793 sevent.xconfigure.y = wwin->client.y;
1794 sevent.xconfigure.width = wwin->client.width;
1795 sevent.xconfigure.height = wwin->client.height;
1797 sevent.xconfigure.border_width = wwin->old_border_width;
1798 if (WFLAGP(wwin, no_titlebar))
1799 sevent.xconfigure.above = None;
1800 else
1801 sevent.xconfigure.above = wwin->frame->titlebar->window;
1803 sevent.xconfigure.override_redirect = False;
1804 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1805 #ifdef KWM_HINTS
1806 wKWMSendEventMessage(wwin, WKWMChangedClient);
1807 #endif
1808 XFlush(dpy);
1813 *----------------------------------------------------------------------
1814 * wWindowConfigure--
1815 * Configures the frame, decorations and client window to the
1816 * specified geometry. The geometry is not checked for validity,
1817 * wWindowConstrainSize() must be used for that.
1818 * The size parameters are for the client window, but the position is
1819 * for the frame.
1820 * The client window receives a ConfigureNotify event, according
1821 * to what ICCCM says.
1823 * Returns:
1824 * None
1826 * Side effects:
1827 * Window size and position are changed and client window receives
1828 * a ConfigureNotify event.
1829 *----------------------------------------------------------------------
1831 void
1832 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1833 WWindow *wwin;
1834 int req_x, req_y; /* new position of the frame */
1835 int req_width, req_height; /* new size of the client */
1837 int synth_notify = False;
1838 int resize;
1840 resize = (req_width!=wwin->client.width
1841 || req_height!=wwin->client.height);
1843 * if the window is being moved but not resized then
1844 * send a synthetic ConfigureNotify
1846 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1847 synth_notify = True;
1850 if (WFLAGP(wwin, dont_move_off))
1851 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1852 req_width, req_height);
1853 if (resize) {
1854 if (req_width < MIN_WINDOW_SIZE)
1855 req_width = MIN_WINDOW_SIZE;
1856 if (req_height < MIN_WINDOW_SIZE)
1857 req_height = MIN_WINDOW_SIZE;
1859 /* If growing, resize inner part before frame,
1860 * if shrinking, resize frame before.
1861 * This will prevent the frame (that can have a different color)
1862 * to be exposed, causing flicker */
1863 if (req_height > wwin->frame->core->height
1864 || req_width > wwin->frame->core->width)
1865 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1867 if (wwin->flags.shaded) {
1868 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1869 req_width, wwin->frame->core->height);
1870 wwin->old_geometry.height = req_height;
1871 } else {
1872 int h;
1874 h = req_height + wwin->frame->top_width
1875 + wwin->frame->bottom_width;
1877 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1880 if (!(req_height > wwin->frame->core->height
1881 || req_width > wwin->frame->core->width))
1882 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1884 wwin->client.x = req_x;
1885 wwin->client.y = req_y + wwin->frame->top_width;
1886 wwin->client.width = req_width;
1887 wwin->client.height = req_height;
1888 } else {
1889 wwin->client.x = req_x;
1890 wwin->client.y = req_y + wwin->frame->top_width;
1892 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1894 wwin->frame_x = req_x;
1895 wwin->frame_y = req_y;
1897 #ifdef SHAPE
1898 if (wShapeSupported && wwin->flags.shaped && resize) {
1899 wWindowSetShape(wwin);
1901 #endif
1903 if (synth_notify)
1904 wWindowSynthConfigureNotify(wwin);
1905 XFlush(dpy);
1909 void
1910 wWindowMove(wwin, req_x, req_y)
1911 WWindow *wwin;
1912 int req_x, req_y; /* new position of the frame */
1914 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1915 int synth_notify = False;
1917 /* Send a synthetic ConfigureNotify event for every window movement. */
1918 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1919 synth_notify = True;
1921 #else
1922 /* A single synthetic ConfigureNotify event is sent at the end of
1923 * a completed (opaque) movement in moveres.c */
1924 #endif
1926 if (WFLAGP(wwin, dont_move_off))
1927 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1928 wwin->frame->core->width, wwin->frame->core->height);
1930 wwin->client.x = req_x;
1931 wwin->client.y = req_y + wwin->frame->top_width;
1933 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1935 wwin->frame_x = req_x;
1936 wwin->frame_y = req_y;
1938 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1939 if (synth_notify)
1940 wWindowSynthConfigureNotify(wwin);
1941 #endif
1945 void
1946 wWindowUpdateButtonImages(WWindow *wwin)
1948 WScreen *scr = wwin->screen_ptr;
1949 Pixmap pixmap, mask;
1950 WFrameWindow *fwin = wwin->frame;
1952 if (WFLAGP(wwin, no_titlebar))
1953 return;
1955 /* miniaturize button */
1957 if (!WFLAGP(wwin, no_miniaturize_button)) {
1958 if (wwin->wm_gnustep_attr
1959 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
1960 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
1962 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
1963 mask = wwin->wm_gnustep_attr->miniaturize_mask;
1964 } else {
1965 mask = None;
1968 if (fwin->lbutton_image
1969 && (fwin->lbutton_image->image != pixmap
1970 || fwin->lbutton_image->mask != mask)) {
1971 wPixmapDestroy(fwin->lbutton_image);
1972 fwin->lbutton_image = NULL;
1975 if (!fwin->lbutton_image) {
1976 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
1977 fwin->lbutton_image->client_owned = 1;
1978 fwin->lbutton_image->client_owned_mask = 1;
1980 } else {
1981 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
1982 wPixmapDestroy(fwin->lbutton_image);
1984 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
1988 /* close button */
1990 if (!WFLAGP(wwin, no_close_button)) {
1991 if (wwin->wm_gnustep_attr
1992 && wwin->wm_gnustep_attr->flags&GSClosePixmapAttr) {
1993 pixmap = wwin->wm_gnustep_attr->close_pixmap;
1995 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr) {
1996 mask = wwin->wm_gnustep_attr->close_mask;
1997 } else {
1998 mask = None;
2001 if (fwin->rbutton_image
2002 && (fwin->rbutton_image->image != pixmap
2003 || fwin->rbutton_image->mask != mask)) {
2004 wPixmapDestroy(fwin->rbutton_image);
2005 fwin->rbutton_image = NULL;
2008 if (!fwin->rbutton_image) {
2009 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2010 fwin->rbutton_image->client_owned = 1;
2011 fwin->rbutton_image->client_owned_mask = 1;
2013 } else if (WFLAGP(wwin, kill_close)) {
2014 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2015 wPixmapDestroy(fwin->rbutton_image);
2017 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2018 } else if (WFLAGP(wwin, broken_close)) {
2019 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2020 wPixmapDestroy(fwin->rbutton_image);
2022 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2023 } else {
2024 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2025 wPixmapDestroy(fwin->rbutton_image);
2027 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2031 /* force buttons to be redrawn */
2032 fwin->flags.need_texture_change = 1;
2033 wFrameWindowPaint(fwin);
2038 *---------------------------------------------------------------------------
2039 * wWindowConfigureBorders--
2040 * Update window border configuration according to attribute flags.
2042 *---------------------------------------------------------------------------
2044 void
2045 wWindowConfigureBorders(WWindow *wwin)
2047 if (wwin->frame) {
2048 int flags;
2049 int newy, oldh;
2051 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2052 if (!WFLAGP(wwin, no_titlebar))
2053 flags |= WFF_TITLEBAR;
2054 if (!WFLAGP(wwin, no_resizebar))
2055 flags |= WFF_RESIZEBAR;
2056 if (wwin->flags.shaded)
2057 flags |= WFF_IS_SHADED;
2059 oldh = wwin->frame->top_width;
2060 wFrameWindowUpdateBorders(wwin->frame, flags);
2061 if (oldh != wwin->frame->top_width) {
2062 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2064 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2065 wWindowConfigure(wwin, wwin->frame_x, newy,
2066 wwin->client.width, wwin->client.height);
2069 flags = 0;
2070 if (!WFLAGP(wwin, no_miniaturize_button)
2071 && wwin->frame->flags.hide_left_button)
2072 flags |= WFF_LEFT_BUTTON;
2074 if (!WFLAGP(wwin, no_close_button)
2075 && wwin->frame->flags.hide_right_button)
2076 flags |= WFF_RIGHT_BUTTON;
2078 if (flags!=0) {
2079 wWindowUpdateButtonImages(wwin);
2080 wFrameWindowShowButton(wwin->frame, flags);
2083 flags = 0;
2084 if (WFLAGP(wwin, no_miniaturize_button)
2085 && !wwin->frame->flags.hide_left_button)
2086 flags |= WFF_LEFT_BUTTON;
2088 if (WFLAGP(wwin, no_close_button)
2089 && !wwin->frame->flags.hide_right_button)
2090 flags |= WFF_RIGHT_BUTTON;
2092 if (flags!=0)
2093 wFrameWindowHideButton(wwin->frame, flags);
2095 #ifdef SHAPE
2096 if (wShapeSupported && wwin->flags.shaped) {
2097 wWindowSetShape(wwin);
2099 #endif
2104 void
2105 wWindowSaveState(WWindow *wwin)
2107 CARD32 data[10];
2108 int i;
2110 memset(data, 0, sizeof(CARD32)*10);
2111 data[0] = wwin->frame->workspace;
2112 data[1] = wwin->flags.miniaturized;
2113 data[2] = wwin->flags.shaded;
2114 data[3] = wwin->flags.hidden;
2116 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2117 if (wwin->screen_ptr->shortcutWindow[i] == wwin)
2118 data[9] |= 1<<i;
2120 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2121 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2122 (unsigned char *)data, 10);
2126 static int
2127 getSavedState(Window window, WSavedState **state)
2129 Atom type_ret;
2130 int fmt_ret;
2131 unsigned long nitems_ret;
2132 unsigned long bytes_after_ret;
2133 CARD32 *data;
2135 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2136 True, _XA_WINDOWMAKER_STATE,
2137 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2138 (unsigned char **)&data)!=Success || !data)
2139 return 0;
2141 *state = malloc(sizeof(WSavedState));
2143 if (*state) {
2144 (*state)->workspace = data[0];
2145 (*state)->miniaturized = data[1];
2146 (*state)->shaded = data[2];
2147 (*state)->hidden = data[3];
2148 (*state)->use_geometry = data[4];
2149 (*state)->x = data[5];
2150 (*state)->y = data[6];
2151 (*state)->w = data[7];
2152 (*state)->h = data[8];
2153 (*state)->window_shortcuts = data[9];
2155 XFree(data);
2157 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2158 return 1;
2159 else
2160 return 0;
2164 #ifdef SHAPE
2165 void
2166 wWindowClearShape(WWindow *wwin)
2168 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2169 0, wwin->frame->top_width, None, ShapeSet);
2170 XFlush(dpy);
2173 void
2174 wWindowSetShape(WWindow *wwin)
2176 XRectangle rect[2];
2177 int count;
2178 #ifdef OPTIMIZE_SHAPE
2179 XRectangle *rects;
2180 XRectangle *urec;
2181 int ordering;
2183 /* only shape is the client's */
2184 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2185 goto alt_code;
2188 /* Get array of rectangles describing the shape mask */
2189 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2190 &count, &ordering);
2191 if (!rects) {
2192 goto alt_code;
2195 urec = malloc(sizeof(XRectangle)*(count+2));
2196 if (!urec) {
2197 XFree(rects);
2198 goto alt_code;
2201 /* insert our decoration rectangles in the rect list */
2202 memcpy(urec, rects, sizeof(XRectangle)*count);
2203 XFree(rects);
2205 if (!WFLAGP(wwin, no_titlebar)) {
2206 urec[count].x = -1;
2207 urec[count].y = -1 - wwin->frame->top_width;
2208 urec[count].width = wwin->frame->core->width + 2;
2209 urec[count].height = wwin->frame->top_width + 1;
2210 count++;
2212 if (!WFLAGP(wwin, no_resizebar)) {
2213 urec[count].x = -1;
2214 urec[count].y = wwin->frame->core->height
2215 - wwin->frame->bottom_width - wwin->frame->top_width;
2216 urec[count].width = wwin->frame->core->width + 2;
2217 urec[count].height = wwin->frame->bottom_width + 1;
2218 count++;
2221 /* shape our frame window */
2222 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2223 0, wwin->frame->top_width, urec, count,
2224 ShapeSet, Unsorted);
2225 XFlush(dpy);
2226 free(urec);
2227 return;
2229 alt_code:
2230 #endif /* OPTIMIZE_SHAPE */
2231 count = 0;
2232 if (!WFLAGP(wwin, no_titlebar)) {
2233 rect[count].x = -1;
2234 rect[count].y = -1;
2235 rect[count].width = wwin->frame->core->width + 2;
2236 rect[count].height = wwin->frame->top_width + 1;
2237 count++;
2239 if (!WFLAGP(wwin, no_resizebar)) {
2240 rect[count].x = -1;
2241 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2242 rect[count].width = wwin->frame->core->width + 2;
2243 rect[count].height = wwin->frame->bottom_width + 1;
2244 count++;
2246 if (count > 0) {
2247 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2248 0, 0, rect, count, ShapeSet, Unsorted);
2250 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2251 0, wwin->frame->top_width, wwin->client_win,
2252 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2253 XFlush(dpy);
2255 #endif /* SHAPE */
2257 /* ====================================================================== */
2259 static FocusMode
2260 getFocusMode(WWindow *wwin)
2262 FocusMode mode;
2264 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2265 if (wwin->wm_hints->input == True) {
2266 if (wwin->protocols.TAKE_FOCUS)
2267 mode = WFM_LOCALLY_ACTIVE;
2268 else
2269 mode = WFM_PASSIVE;
2270 } else {
2271 if (wwin->protocols.TAKE_FOCUS)
2272 mode = WFM_GLOBALLY_ACTIVE;
2273 else
2274 mode = WFM_NO_INPUT;
2276 } else {
2277 mode = WFM_PASSIVE;
2279 return mode;
2283 void
2284 wWindowSetKeyGrabs(WWindow *wwin)
2286 int i;
2287 WShortKey *key;
2289 for (i=0; i<WKBD_LAST; i++) {
2290 key = &wKeyBindings[i];
2292 if (key->keycode==0)
2293 continue;
2294 if (key->modifier!=AnyModifier) {
2295 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2296 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2297 #ifdef NUMLOCK_HACK
2298 /* Also grab all modifier combinations possible that include,
2299 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2300 * work even if the NumLock/ScrollLock key is on.
2302 wHackedGrabKey(key->keycode, key->modifier,
2303 wwin->frame->core->window, True, GrabModeAsync,
2304 GrabModeAsync);
2305 #endif
2307 XGrabKey(dpy, key->keycode, key->modifier,
2308 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2311 #ifndef LITE
2312 wRootMenuBindShortcuts(wwin->frame->core->window);
2313 #endif
2318 void
2319 wWindowResetMouseGrabs(WWindow *wwin)
2321 /* Mouse grabs can't be done on the client window because of
2322 * ICCCM and because clients that try to do the same will crash.
2324 * But there is a problem wich makes tbar buttons of unfocused
2325 * windows not usable as the click goes to the frame window instead
2326 * of the button itself. Must figure a way to fix that.
2329 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2331 if (!WFLAGP(wwin, no_bind_mouse)) {
2332 /* grabs for Meta+drag */
2333 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2334 True, ButtonPressMask, GrabModeSync,
2335 GrabModeAsync, None, None);
2338 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2339 /* the passive grabs to focus the window */
2340 if (wPreferences.focus_mode == WKF_CLICK)
2341 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2342 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2343 None, None);
2345 XFlush(dpy);
2349 void
2350 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2353 if (attr->flags & GSExtraFlagsAttr) {
2354 if (WFLAGP(wwin, broken_close) !=
2355 (attr->extra_flags & GSDocumentEditedFlag)) {
2357 wwin->client_flags.broken_close = !WFLAGP(wwin, broken_close);
2359 wWindowUpdateButtonImages(wwin);
2366 WMagicNumber
2367 wWindowAddSavedState(char *instance, char *class, char *command,
2368 pid_t pid, WSavedState *state)
2370 WWindowState *wstate;
2372 wstate = malloc(sizeof(WWindowState));
2373 if (!wstate)
2374 return 0;
2376 memset(wstate, 0, sizeof(WWindowState));
2377 wstate->pid = pid;
2378 if (instance)
2379 wstate->instance = wstrdup(instance);
2380 if (class)
2381 wstate->class = wstrdup(class);
2382 if (command)
2383 wstate->command = wstrdup(command);
2384 wstate->state = state;
2386 wstate->next = windowState;
2387 windowState = wstate;
2389 #ifdef DEBUG
2390 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2391 class, command);
2392 #endif
2394 return wstate;
2398 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2401 WMagicNumber
2402 wWindowGetSavedState(Window win)
2404 char *instance, *class, *command=NULL;
2405 WWindowState *wstate = windowState;
2406 char **argv;
2407 int argc;
2409 if (!wstate)
2410 return NULL;
2412 if (XGetCommand(dpy, win, &argv, &argc)) {
2413 if (argc > 0)
2414 command = FlattenStringList(argv, argc);
2415 XFreeStringList(argv);
2417 if (!command)
2418 return NULL;
2420 if (PropGetWMClass(win, &class, &instance)) {
2421 while (wstate) {
2422 if (SAME(instance, wstate->instance) &&
2423 SAME(class, wstate->class) &&
2424 SAME(command, wstate->command)) {
2425 break;
2427 wstate = wstate->next;
2429 } else {
2430 wstate = NULL;
2433 #ifdef DEBUG
2434 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2435 class, command);
2436 #endif
2438 if (command) free(command);
2439 if (instance) XFree(instance);
2440 if (class) XFree(class);
2442 return wstate;
2446 void
2447 wWindowDeleteSavedState(WMagicNumber id)
2449 WWindowState *tmp, *wstate=(WWindowState*)id;
2451 if (!wstate || !windowState)
2452 return;
2454 tmp = windowState;
2455 if (tmp==wstate) {
2456 windowState = wstate->next;
2457 #ifdef DEBUG
2458 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2459 wstate, wstate->instance, wstate->class, wstate->command);
2460 #endif
2461 if (wstate->instance) free(wstate->instance);
2462 if (wstate->class) free(wstate->class);
2463 if (wstate->command) free(wstate->command);
2464 free(wstate->state);
2465 free(wstate);
2466 } else {
2467 while (tmp->next) {
2468 if (tmp->next==wstate) {
2469 tmp->next=wstate->next;
2470 #ifdef DEBUG
2471 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2472 wstate, wstate->instance, wstate->class, wstate->command);
2473 #endif
2474 if (wstate->instance) free(wstate->instance);
2475 if (wstate->class) free(wstate->class);
2476 if (wstate->command) free(wstate->command);
2477 free(wstate->state);
2478 free(wstate);
2479 break;
2481 tmp = tmp->next;
2487 void
2488 wWindowDeleteSavedStatesForPID(pid_t pid)
2490 WWindowState *tmp, *wstate;
2492 if (!windowState)
2493 return;
2495 tmp = windowState;
2496 if (tmp->pid == pid) {
2497 wstate = windowState;
2498 windowState = tmp->next;
2499 #ifdef DEBUG
2500 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2501 wstate, wstate->instance, wstate->class, wstate->command);
2502 #endif
2503 if (wstate->instance) free(wstate->instance);
2504 if (wstate->class) free(wstate->class);
2505 if (wstate->command) free(wstate->command);
2506 free(wstate->state);
2507 free(wstate);
2508 } else {
2509 while (tmp->next) {
2510 if (tmp->next->pid==pid) {
2511 wstate = tmp->next;
2512 tmp->next = wstate->next;
2513 #ifdef DEBUG
2514 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2515 wstate, wstate->instance, wstate->class, wstate->command);
2516 #endif
2517 if (wstate->instance) free(wstate->instance);
2518 if (wstate->class) free(wstate->class);
2519 if (wstate->command) free(wstate->command);
2520 free(wstate->state);
2521 free(wstate);
2522 break;
2524 tmp = tmp->next;
2530 /* ====================================================================== */
2532 static void
2533 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2535 WWindow *wwin = data;
2537 #ifndef NUMLOCK_HACK
2538 if ((event->xbutton.state & ValidModMask)
2539 != (event->xbutton.state & ~LockMask)) {
2540 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2541 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2543 #endif
2545 event->xbutton.state &= ValidModMask;
2547 CloseWindowMenu(wwin->screen_ptr);
2549 if (wPreferences.focus_mode==WKF_CLICK
2550 && !(event->xbutton.state&ControlMask)
2551 && !WFLAGP(wwin, no_focusable)) {
2552 wSetFocusTo(wwin->screen_ptr, wwin);
2555 if (event->xbutton.button == Button1)
2556 wRaiseFrame(wwin->frame->core);
2558 if (event->xbutton.window != wwin->frame->resizebar->window) {
2559 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2560 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2561 GrabModeAsync, GrabModeAsync, None,
2562 None, CurrentTime)!=GrabSuccess) {
2563 #ifdef DEBUG0
2564 wwarning("pointer grab failed for window move");
2565 #endif
2566 return;
2570 if (event->xbutton.state & MOD_MASK) {
2571 /* move the window */
2572 wMouseMoveWindow(wwin, event);
2573 XUngrabPointer(dpy, CurrentTime);
2574 } else {
2575 wMouseResizeWindow(wwin, event);
2576 XUngrabPointer(dpy, CurrentTime);
2582 static void
2583 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2585 WWindow *wwin = data;
2587 event->xbutton.state &= ValidModMask;
2589 if (event->xbutton.button==Button1) {
2590 if (event->xbutton.state == 0) {
2591 if (!WFLAGP(wwin, no_shadeable)) {
2592 /* shade window */
2593 if (wwin->flags.shaded)
2594 wUnshadeWindow(wwin);
2595 else
2596 wShadeWindow(wwin);
2598 } else {
2599 int dir = 0;
2601 if (event->xbutton.state & ControlMask)
2602 dir |= MAX_VERTICAL;
2604 if (event->xbutton.state & ShiftMask) {
2605 dir |= MAX_HORIZONTAL;
2606 if (!(event->xbutton.state & ControlMask))
2607 wSelectWindow(wwin, !wwin->flags.selected);
2610 /* maximize window */
2611 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2612 if (wwin->flags.maximized)
2613 wUnmaximizeWindow(wwin);
2614 else
2615 wMaximizeWindow(wwin, dir);
2618 } else if (event->xbutton.button==Button3) {
2619 if (event->xbutton.state & MOD_MASK) {
2620 wHideOtherApplications(wwin);
2622 } else if (event->xbutton.button==Button2) {
2623 wSelectWindow(wwin, !wwin->flags.selected);
2628 static void
2629 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2631 WWindow *wwin = desc->parent;
2633 event->xbutton.state &= ValidModMask;
2635 CloseWindowMenu(wwin->screen_ptr);
2637 if (wPreferences.focus_mode==WKF_CLICK
2638 && !(event->xbutton.state&ControlMask)
2639 && !WFLAGP(wwin, no_focusable)) {
2640 wSetFocusTo(wwin->screen_ptr, wwin);
2642 if (event->xbutton.button == Button1) {
2643 wRaiseFrame(wwin->frame->core);
2646 if (event->xbutton.state & MOD_MASK) {
2647 /* move the window */
2648 if (XGrabPointer(dpy, wwin->client_win, False,
2649 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2650 GrabModeAsync, GrabModeAsync, None,
2651 None, CurrentTime)!=GrabSuccess) {
2652 #ifdef DEBUG0
2653 wwarning("pointer grab failed for window move");
2654 #endif
2655 return;
2657 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2658 wMouseResizeWindow(wwin, event);
2659 else
2660 wMouseMoveWindow(wwin, event);
2661 XUngrabPointer(dpy, CurrentTime);
2666 static void
2667 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2669 WWindow *wwin = (WWindow*)data;
2671 #ifndef NUMLOCK_HACK
2672 if ((event->xbutton.state & ValidModMask)
2673 != (event->xbutton.state & ~LockMask)) {
2674 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2675 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2677 #endif
2678 event->xbutton.state &= ValidModMask;
2680 CloseWindowMenu(wwin->screen_ptr);
2682 if (wPreferences.focus_mode==WKF_CLICK
2683 && !(event->xbutton.state&ControlMask)
2684 && !WFLAGP(wwin, no_focusable)) {
2685 wSetFocusTo(wwin->screen_ptr, wwin);
2688 if (event->xbutton.button == Button1
2689 || event->xbutton.button == Button2) {
2691 if (event->xbutton.button == Button1) {
2692 if (event->xbutton.state & MOD_MASK) {
2693 wLowerFrame(wwin->frame->core);
2694 } else {
2695 wRaiseFrame(wwin->frame->core);
2698 if ((event->xbutton.state & ShiftMask)
2699 && !(event->xbutton.state & ControlMask)) {
2700 wSelectWindow(wwin, !wwin->flags.selected);
2701 return;
2703 if (event->xbutton.window != wwin->frame->titlebar->window
2704 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2705 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2706 GrabModeAsync, GrabModeAsync, None,
2707 None, CurrentTime)!=GrabSuccess) {
2708 #ifdef DEBUG0
2709 wwarning("pointer grab failed for window move");
2710 #endif
2711 return;
2714 /* move the window */
2715 wMouseMoveWindow(wwin, event);
2717 XUngrabPointer(dpy, CurrentTime);
2718 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2719 && !wwin->flags.internal_window
2720 && !WCHECK_STATE(WSTATE_MODAL)) {
2721 WObjDescriptor *desc;
2723 if (event->xbutton.window != wwin->frame->titlebar->window
2724 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2725 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2726 GrabModeAsync, GrabModeAsync, None,
2727 None, CurrentTime)!=GrabSuccess) {
2728 #ifdef DEBUG0
2729 wwarning("pointer grab failed for window move");
2730 #endif
2731 return;
2734 OpenWindowMenu(wwin, event->xbutton.x_root,
2735 wwin->frame_y+wwin->frame->top_width, False);
2737 /* allow drag select */
2738 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2739 event->xany.send_event = True;
2740 (*desc->handle_mousedown)(desc, event);
2742 XUngrabPointer(dpy, CurrentTime);
2748 static void
2749 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2751 WWindow *wwin = data;
2753 event->xbutton.state &= ValidModMask;
2755 CloseWindowMenu(wwin->screen_ptr);
2757 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2758 return;
2760 /* if control-click, kill the client */
2761 if (event->xbutton.state & ControlMask) {
2762 wClientKill(wwin);
2763 } else {
2764 #ifdef OLWM_HINTS
2765 if (wwin->flags.olwm_push_pin_out) {
2767 wwin->flags.olwm_push_pin_out = 0;
2769 wOLWMChangePushpinState(wwin, True);
2771 wFrameWindowUpdatePushButton(wwin->frame, False);
2773 return;
2775 #endif
2776 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2777 /* send delete message */
2778 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2784 static void
2785 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2787 WWindow *wwin = data;
2789 CloseWindowMenu(wwin->screen_ptr);
2791 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2792 return;
2794 /* send delete message */
2795 if (wwin->protocols.DELETE_WINDOW) {
2796 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2797 } else {
2798 wClientKill(wwin);
2803 static void
2804 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2806 WWindow *wwin = data;
2808 event->xbutton.state &= ValidModMask;
2810 CloseWindowMenu(wwin->screen_ptr);
2812 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2813 return;
2815 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2816 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2817 LastTimestamp);
2818 } else {
2819 WApplication *wapp;
2820 if ((event->xbutton.state & ControlMask) ||
2821 (event->xbutton.button == Button3)) {
2823 wapp = wApplicationOf(wwin->main_window);
2824 if (wapp && !WFLAGP(wwin, no_appicon))
2825 wHideApplication(wapp);
2826 } else if (event->xbutton.state==0) {
2827 wIconifyWindow(wwin);