*** empty log message ***
[wmaker-crm.git] / src / window.c
blob9f763440ab0480dc2a9b88f5ed4c4ea544dfe4e8
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 wWindowResetMouseGrabs(wwin);
1557 wwin->flags.focused = 1;
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;
1624 wWindowResetMouseGrabs(wwin);
1626 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1633 *----------------------------------------------------------------------
1635 * wWindowConstrainSize--
1636 * Constrains size for the client window, taking the maximal size,
1637 * window resize increments and other size hints into account.
1639 * Returns:
1640 * The closest size to what was given that the client window can
1641 * have.
1643 *----------------------------------------------------------------------
1645 void
1646 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1648 XSizeHints *sizeh = wwin->normal_hints;
1649 int width = *nwidth;
1650 int height = *nheight;
1651 int winc = sizeh->width_inc;
1652 int hinc = sizeh->height_inc;
1654 if (width < sizeh->min_width)
1655 width = sizeh->min_width;
1656 if (height < sizeh->min_height)
1657 height = sizeh->min_height;
1659 if (width > sizeh->max_width)
1660 width = sizeh->max_width;
1661 if (height > sizeh->max_height)
1662 height = sizeh->max_height;
1664 /* aspect ratio code borrowed from olwm */
1665 if (sizeh->flags & PAspect) {
1666 /* adjust max aspect ratio */
1667 if (!(sizeh->max_aspect.x==1 && sizeh->max_aspect.y==1)
1668 && width * sizeh->max_aspect.y > height * sizeh->max_aspect.x) {
1669 if (sizeh->max_aspect.x > sizeh->max_aspect.y) {
1670 height = (width * sizeh->max_aspect.y) / sizeh->max_aspect.x;
1671 if (height > sizeh->max_height) {
1672 height = sizeh->max_height;
1673 width = (height*sizeh->max_aspect.x) / sizeh->max_aspect.y;
1675 } else {
1676 width = (height * sizeh->max_aspect.x) / sizeh->max_aspect.y;
1677 if (width > sizeh->max_width) {
1678 width = sizeh->max_width;
1679 height = (width*sizeh->max_aspect.y) / sizeh->max_aspect.x;
1684 /* adjust min aspect ratio */
1685 if (!(sizeh->min_aspect.x==1 && sizeh->min_aspect.y==1)
1686 && width * sizeh->min_aspect.y < height * sizeh->min_aspect.x) {
1687 if (sizeh->min_aspect.x > sizeh->min_aspect.y) {
1688 height = (width * sizeh->min_aspect.y) / sizeh->min_aspect.x;
1689 if (height < sizeh->min_height) {
1690 height = sizeh->min_height;
1691 width = (height*sizeh->min_aspect.x) / sizeh->min_aspect.y;
1693 } else {
1694 width = (height * sizeh->min_aspect.x) / sizeh->min_aspect.y;
1695 if (width < sizeh->min_width) {
1696 width = sizeh->min_width;
1697 height = (width*sizeh->min_aspect.y) / sizeh->min_aspect.x;
1703 if (sizeh->base_width != 0) {
1704 width = (((width - sizeh->base_width) / winc) * winc)
1705 + sizeh->base_width;
1706 } else {
1707 width = (((width - sizeh->min_width) / winc) * winc)
1708 + sizeh->min_width;
1711 if (sizeh->base_width != 0) {
1712 height = (((height - sizeh->base_height) / hinc) * hinc)
1713 + sizeh->base_height;
1714 } else {
1715 height = (((height - sizeh->min_height) / hinc) * hinc)
1716 + sizeh->min_height;
1719 *nwidth = width;
1720 *nheight = height;
1724 void
1725 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1727 WScreen *scr = wwin->screen_ptr;
1728 WApplication *wapp;
1729 int unmap = 0;
1731 if (workspace >= scr->workspace_count || workspace < 0
1732 || workspace == wwin->frame->workspace)
1733 return;
1735 if (workspace != scr->current_workspace) {
1736 /* Sent to other workspace. Unmap window */
1737 if ((wwin->flags.mapped
1738 || wwin->flags.shaded
1739 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1740 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1742 wapp = wApplicationOf(wwin->main_window);
1743 if (wapp) {
1744 wapp->last_workspace = workspace;
1746 if (wwin->flags.miniaturized) {
1747 XUnmapWindow(dpy, wwin->icon->core->window);
1748 wwin->icon->mapped = 0;
1749 } else {
1750 unmap = 1;
1751 wSetFocusTo(scr, NULL);
1754 } else {
1755 /* brought to current workspace. Map window */
1756 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1757 XMapWindow(dpy, wwin->icon->core->window);
1758 wwin->icon->mapped = 1;
1759 } else if (!wwin->flags.mapped &&
1760 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1761 wWindowMap(wwin);
1764 if (!IS_OMNIPRESENT(wwin)) {
1765 wwin->frame->workspace = workspace;
1766 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1768 #ifdef GNOME_STUFF
1769 wGNOMEUpdateClientStateHint(wwin, True);
1770 #endif
1771 #ifdef KWM_HINTS
1772 wKWMUpdateClientWorkspace(wwin);
1773 wKWMSendEventMessage(wwin, WKWMChangedClient);
1774 #endif
1775 if (unmap) {
1776 wWindowUnmap(wwin);
1781 void
1782 wWindowSynthConfigureNotify(WWindow *wwin)
1784 XEvent sevent;
1786 sevent.type = ConfigureNotify;
1787 sevent.xconfigure.display = dpy;
1788 sevent.xconfigure.event = wwin->client_win;
1789 sevent.xconfigure.window = wwin->client_win;
1791 sevent.xconfigure.x = wwin->client.x;
1792 sevent.xconfigure.y = wwin->client.y;
1793 sevent.xconfigure.width = wwin->client.width;
1794 sevent.xconfigure.height = wwin->client.height;
1796 sevent.xconfigure.border_width = wwin->old_border_width;
1797 if (WFLAGP(wwin, no_titlebar))
1798 sevent.xconfigure.above = None;
1799 else
1800 sevent.xconfigure.above = wwin->frame->titlebar->window;
1802 sevent.xconfigure.override_redirect = False;
1803 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1804 #ifdef KWM_HINTS
1805 wKWMSendEventMessage(wwin, WKWMChangedClient);
1806 #endif
1807 XFlush(dpy);
1812 *----------------------------------------------------------------------
1813 * wWindowConfigure--
1814 * Configures the frame, decorations and client window to the
1815 * specified geometry. The geometry is not checked for validity,
1816 * wWindowConstrainSize() must be used for that.
1817 * The size parameters are for the client window, but the position is
1818 * for the frame.
1819 * The client window receives a ConfigureNotify event, according
1820 * to what ICCCM says.
1822 * Returns:
1823 * None
1825 * Side effects:
1826 * Window size and position are changed and client window receives
1827 * a ConfigureNotify event.
1828 *----------------------------------------------------------------------
1830 void
1831 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1832 WWindow *wwin;
1833 int req_x, req_y; /* new position of the frame */
1834 int req_width, req_height; /* new size of the client */
1836 int synth_notify = False;
1837 int resize;
1839 resize = (req_width!=wwin->client.width
1840 || req_height!=wwin->client.height);
1842 * if the window is being moved but not resized then
1843 * send a synthetic ConfigureNotify
1845 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1846 synth_notify = True;
1849 if (WFLAGP(wwin, dont_move_off))
1850 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1851 req_width, req_height);
1852 if (resize) {
1853 if (req_width < MIN_WINDOW_SIZE)
1854 req_width = MIN_WINDOW_SIZE;
1855 if (req_height < MIN_WINDOW_SIZE)
1856 req_height = MIN_WINDOW_SIZE;
1858 /* If growing, resize inner part before frame,
1859 * if shrinking, resize frame before.
1860 * This will prevent the frame (that can have a different color)
1861 * to be exposed, causing flicker */
1862 if (req_height > wwin->frame->core->height
1863 || req_width > wwin->frame->core->width)
1864 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1866 if (wwin->flags.shaded) {
1867 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1868 req_width, wwin->frame->core->height);
1869 wwin->old_geometry.height = req_height;
1870 } else {
1871 int h;
1873 h = req_height + wwin->frame->top_width
1874 + wwin->frame->bottom_width;
1876 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1879 if (!(req_height > wwin->frame->core->height
1880 || req_width > wwin->frame->core->width))
1881 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1883 wwin->client.x = req_x;
1884 wwin->client.y = req_y + wwin->frame->top_width;
1885 wwin->client.width = req_width;
1886 wwin->client.height = req_height;
1887 } else {
1888 wwin->client.x = req_x;
1889 wwin->client.y = req_y + wwin->frame->top_width;
1891 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1893 wwin->frame_x = req_x;
1894 wwin->frame_y = req_y;
1896 #ifdef SHAPE
1897 if (wShapeSupported && wwin->flags.shaped && resize) {
1898 wWindowSetShape(wwin);
1900 #endif
1902 if (synth_notify)
1903 wWindowSynthConfigureNotify(wwin);
1904 XFlush(dpy);
1908 void
1909 wWindowMove(wwin, req_x, req_y)
1910 WWindow *wwin;
1911 int req_x, req_y; /* new position of the frame */
1913 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1914 int synth_notify = False;
1916 /* Send a synthetic ConfigureNotify event for every window movement. */
1917 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1918 synth_notify = True;
1920 #else
1921 /* A single synthetic ConfigureNotify event is sent at the end of
1922 * a completed (opaque) movement in moveres.c */
1923 #endif
1925 if (WFLAGP(wwin, dont_move_off))
1926 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1927 wwin->frame->core->width, wwin->frame->core->height);
1929 wwin->client.x = req_x;
1930 wwin->client.y = req_y + wwin->frame->top_width;
1932 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1934 wwin->frame_x = req_x;
1935 wwin->frame_y = req_y;
1937 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1938 if (synth_notify)
1939 wWindowSynthConfigureNotify(wwin);
1940 #endif
1944 void
1945 wWindowUpdateButtonImages(WWindow *wwin)
1947 WScreen *scr = wwin->screen_ptr;
1948 Pixmap pixmap, mask;
1949 WFrameWindow *fwin = wwin->frame;
1951 if (WFLAGP(wwin, no_titlebar))
1952 return;
1954 /* miniaturize button */
1956 if (!WFLAGP(wwin, no_miniaturize_button)) {
1957 if (wwin->wm_gnustep_attr
1958 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
1959 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
1961 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
1962 mask = wwin->wm_gnustep_attr->miniaturize_mask;
1963 } else {
1964 mask = None;
1967 if (fwin->lbutton_image
1968 && (fwin->lbutton_image->image != pixmap
1969 || fwin->lbutton_image->mask != mask)) {
1970 wPixmapDestroy(fwin->lbutton_image);
1971 fwin->lbutton_image = NULL;
1974 if (!fwin->lbutton_image) {
1975 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
1976 fwin->lbutton_image->client_owned = 1;
1977 fwin->lbutton_image->client_owned_mask = 1;
1979 } else {
1980 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
1981 wPixmapDestroy(fwin->lbutton_image);
1983 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
1987 /* close button */
1989 if (!WFLAGP(wwin, no_close_button)) {
1990 if (wwin->wm_gnustep_attr
1991 && wwin->wm_gnustep_attr->flags&GSClosePixmapAttr) {
1992 pixmap = wwin->wm_gnustep_attr->close_pixmap;
1994 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr) {
1995 mask = wwin->wm_gnustep_attr->close_mask;
1996 } else {
1997 mask = None;
2000 if (fwin->rbutton_image
2001 && (fwin->rbutton_image->image != pixmap
2002 || fwin->rbutton_image->mask != mask)) {
2003 wPixmapDestroy(fwin->rbutton_image);
2004 fwin->rbutton_image = NULL;
2007 if (!fwin->rbutton_image) {
2008 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2009 fwin->rbutton_image->client_owned = 1;
2010 fwin->rbutton_image->client_owned_mask = 1;
2012 } else if (WFLAGP(wwin, kill_close)) {
2013 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2014 wPixmapDestroy(fwin->rbutton_image);
2016 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2017 } else if (WFLAGP(wwin, broken_close)) {
2018 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2019 wPixmapDestroy(fwin->rbutton_image);
2021 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2022 } else {
2023 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2024 wPixmapDestroy(fwin->rbutton_image);
2026 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2030 /* force buttons to be redrawn */
2031 fwin->flags.need_texture_change = 1;
2032 wFrameWindowPaint(fwin);
2037 *---------------------------------------------------------------------------
2038 * wWindowConfigureBorders--
2039 * Update window border configuration according to attribute flags.
2041 *---------------------------------------------------------------------------
2043 void
2044 wWindowConfigureBorders(WWindow *wwin)
2046 if (wwin->frame) {
2047 int flags;
2048 int newy, oldh;
2050 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2051 if (!WFLAGP(wwin, no_titlebar))
2052 flags |= WFF_TITLEBAR;
2053 if (!WFLAGP(wwin, no_resizebar))
2054 flags |= WFF_RESIZEBAR;
2055 if (wwin->flags.shaded)
2056 flags |= WFF_IS_SHADED;
2058 oldh = wwin->frame->top_width;
2059 wFrameWindowUpdateBorders(wwin->frame, flags);
2060 if (oldh != wwin->frame->top_width) {
2061 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2063 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2064 wWindowConfigure(wwin, wwin->frame_x, newy,
2065 wwin->client.width, wwin->client.height);
2068 flags = 0;
2069 if (!WFLAGP(wwin, no_miniaturize_button)
2070 && wwin->frame->flags.hide_left_button)
2071 flags |= WFF_LEFT_BUTTON;
2073 if (!WFLAGP(wwin, no_close_button)
2074 && wwin->frame->flags.hide_right_button)
2075 flags |= WFF_RIGHT_BUTTON;
2077 if (flags!=0) {
2078 wWindowUpdateButtonImages(wwin);
2079 wFrameWindowShowButton(wwin->frame, flags);
2082 flags = 0;
2083 if (WFLAGP(wwin, no_miniaturize_button)
2084 && !wwin->frame->flags.hide_left_button)
2085 flags |= WFF_LEFT_BUTTON;
2087 if (WFLAGP(wwin, no_close_button)
2088 && !wwin->frame->flags.hide_right_button)
2089 flags |= WFF_RIGHT_BUTTON;
2091 if (flags!=0)
2092 wFrameWindowHideButton(wwin->frame, flags);
2094 #ifdef SHAPE
2095 if (wShapeSupported && wwin->flags.shaped) {
2096 wWindowSetShape(wwin);
2098 #endif
2103 void
2104 wWindowSaveState(WWindow *wwin)
2106 CARD32 data[10];
2107 int i;
2109 memset(data, 0, sizeof(CARD32)*10);
2110 data[0] = wwin->frame->workspace;
2111 data[1] = wwin->flags.miniaturized;
2112 data[2] = wwin->flags.shaded;
2113 data[3] = wwin->flags.hidden;
2115 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2116 if (wwin->screen_ptr->shortcutWindow[i] == wwin)
2117 data[9] |= 1<<i;
2119 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2120 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2121 (unsigned char *)data, 10);
2125 static int
2126 getSavedState(Window window, WSavedState **state)
2128 Atom type_ret;
2129 int fmt_ret;
2130 unsigned long nitems_ret;
2131 unsigned long bytes_after_ret;
2132 CARD32 *data;
2134 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2135 True, _XA_WINDOWMAKER_STATE,
2136 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2137 (unsigned char **)&data)!=Success || !data)
2138 return 0;
2140 *state = malloc(sizeof(WSavedState));
2142 if (*state) {
2143 (*state)->workspace = data[0];
2144 (*state)->miniaturized = data[1];
2145 (*state)->shaded = data[2];
2146 (*state)->hidden = data[3];
2147 (*state)->use_geometry = data[4];
2148 (*state)->x = data[5];
2149 (*state)->y = data[6];
2150 (*state)->w = data[7];
2151 (*state)->h = data[8];
2152 (*state)->window_shortcuts = data[9];
2154 XFree(data);
2156 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2157 return 1;
2158 else
2159 return 0;
2163 #ifdef SHAPE
2164 void
2165 wWindowClearShape(WWindow *wwin)
2167 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2168 0, wwin->frame->top_width, None, ShapeSet);
2169 XFlush(dpy);
2172 void
2173 wWindowSetShape(WWindow *wwin)
2175 XRectangle rect[2];
2176 int count;
2177 #ifdef OPTIMIZE_SHAPE
2178 XRectangle *rects;
2179 XRectangle *urec;
2180 int ordering;
2182 /* only shape is the client's */
2183 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2184 goto alt_code;
2187 /* Get array of rectangles describing the shape mask */
2188 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2189 &count, &ordering);
2190 if (!rects) {
2191 goto alt_code;
2194 urec = malloc(sizeof(XRectangle)*(count+2));
2195 if (!urec) {
2196 XFree(rects);
2197 goto alt_code;
2200 /* insert our decoration rectangles in the rect list */
2201 memcpy(urec, rects, sizeof(XRectangle)*count);
2202 XFree(rects);
2204 if (!WFLAGP(wwin, no_titlebar)) {
2205 urec[count].x = -1;
2206 urec[count].y = -1 - wwin->frame->top_width;
2207 urec[count].width = wwin->frame->core->width + 2;
2208 urec[count].height = wwin->frame->top_width + 1;
2209 count++;
2211 if (!WFLAGP(wwin, no_resizebar)) {
2212 urec[count].x = -1;
2213 urec[count].y = wwin->frame->core->height
2214 - wwin->frame->bottom_width - wwin->frame->top_width;
2215 urec[count].width = wwin->frame->core->width + 2;
2216 urec[count].height = wwin->frame->bottom_width + 1;
2217 count++;
2220 /* shape our frame window */
2221 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2222 0, wwin->frame->top_width, urec, count,
2223 ShapeSet, Unsorted);
2224 XFlush(dpy);
2225 free(urec);
2226 return;
2228 alt_code:
2229 #endif /* OPTIMIZE_SHAPE */
2230 count = 0;
2231 if (!WFLAGP(wwin, no_titlebar)) {
2232 rect[count].x = -1;
2233 rect[count].y = -1;
2234 rect[count].width = wwin->frame->core->width + 2;
2235 rect[count].height = wwin->frame->top_width + 1;
2236 count++;
2238 if (!WFLAGP(wwin, no_resizebar)) {
2239 rect[count].x = -1;
2240 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2241 rect[count].width = wwin->frame->core->width + 2;
2242 rect[count].height = wwin->frame->bottom_width + 1;
2243 count++;
2245 if (count > 0) {
2246 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2247 0, 0, rect, count, ShapeSet, Unsorted);
2249 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2250 0, wwin->frame->top_width, wwin->client_win,
2251 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2252 XFlush(dpy);
2254 #endif /* SHAPE */
2256 /* ====================================================================== */
2258 static FocusMode
2259 getFocusMode(WWindow *wwin)
2261 FocusMode mode;
2263 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2264 if (wwin->wm_hints->input == True) {
2265 if (wwin->protocols.TAKE_FOCUS)
2266 mode = WFM_LOCALLY_ACTIVE;
2267 else
2268 mode = WFM_PASSIVE;
2269 } else {
2270 if (wwin->protocols.TAKE_FOCUS)
2271 mode = WFM_GLOBALLY_ACTIVE;
2272 else
2273 mode = WFM_NO_INPUT;
2275 } else {
2276 mode = WFM_PASSIVE;
2278 return mode;
2282 void
2283 wWindowSetKeyGrabs(WWindow *wwin)
2285 int i;
2286 WShortKey *key;
2288 for (i=0; i<WKBD_LAST; i++) {
2289 key = &wKeyBindings[i];
2291 if (key->keycode==0)
2292 continue;
2293 if (key->modifier!=AnyModifier) {
2294 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2295 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2296 #ifdef NUMLOCK_HACK
2297 /* Also grab all modifier combinations possible that include,
2298 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2299 * work even if the NumLock/ScrollLock key is on.
2301 wHackedGrabKey(key->keycode, key->modifier,
2302 wwin->frame->core->window, True, GrabModeAsync,
2303 GrabModeAsync);
2304 #endif
2306 XGrabKey(dpy, key->keycode, key->modifier,
2307 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2310 #ifndef LITE
2311 wRootMenuBindShortcuts(wwin->frame->core->window);
2312 #endif
2317 void
2318 wWindowResetMouseGrabs(WWindow *wwin)
2320 /* Mouse grabs can't be done on the client window because of
2321 * ICCCM and because clients that try to do the same will crash.
2323 * But there is a problem wich makes tbar buttons of unfocused
2324 * windows not usable as the click goes to the frame window instead
2325 * of the button itself. Must figure a way to fix that.
2328 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2330 if (!WFLAGP(wwin, no_bind_mouse)) {
2331 /* grabs for Meta+drag */
2332 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2333 True, ButtonPressMask, GrabModeSync,
2334 GrabModeAsync, None, None);
2337 if (!wwin->flags.focused) {
2338 /* the passive grabs to focus the window */
2339 if (wPreferences.focus_mode == WKF_CLICK)
2340 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2341 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2342 None, None);
2344 XFlush(dpy);
2348 void
2349 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2352 if (attr->flags & GSExtraFlagsAttr) {
2353 if (WFLAGP(wwin, broken_close) !=
2354 (attr->extra_flags & GSDocumentEditedFlag)) {
2356 wwin->client_flags.broken_close = !WFLAGP(wwin, broken_close);
2358 wWindowUpdateButtonImages(wwin);
2365 WMagicNumber
2366 wWindowAddSavedState(char *instance, char *class, char *command,
2367 pid_t pid, WSavedState *state)
2369 WWindowState *wstate;
2371 wstate = malloc(sizeof(WWindowState));
2372 if (!wstate)
2373 return 0;
2375 memset(wstate, 0, sizeof(WWindowState));
2376 wstate->pid = pid;
2377 if (instance)
2378 wstate->instance = wstrdup(instance);
2379 if (class)
2380 wstate->class = wstrdup(class);
2381 if (command)
2382 wstate->command = wstrdup(command);
2383 wstate->state = state;
2385 wstate->next = windowState;
2386 windowState = wstate;
2388 #ifdef DEBUG
2389 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2390 class, command);
2391 #endif
2393 return wstate;
2397 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2400 WMagicNumber
2401 wWindowGetSavedState(Window win)
2403 char *instance, *class, *command=NULL;
2404 WWindowState *wstate = windowState;
2405 char **argv;
2406 int argc;
2408 if (!wstate)
2409 return NULL;
2411 if (XGetCommand(dpy, win, &argv, &argc)) {
2412 if (argc > 0)
2413 command = FlattenStringList(argv, argc);
2414 XFreeStringList(argv);
2416 if (!command)
2417 return NULL;
2419 if (PropGetWMClass(win, &class, &instance)) {
2420 while (wstate) {
2421 if (SAME(instance, wstate->instance) &&
2422 SAME(class, wstate->class) &&
2423 SAME(command, wstate->command)) {
2424 break;
2426 wstate = wstate->next;
2428 } else {
2429 wstate = NULL;
2432 #ifdef DEBUG
2433 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2434 class, command);
2435 #endif
2437 if (command) free(command);
2438 if (instance) XFree(instance);
2439 if (class) XFree(class);
2441 return wstate;
2445 void
2446 wWindowDeleteSavedState(WMagicNumber id)
2448 WWindowState *tmp, *wstate=(WWindowState*)id;
2450 if (!wstate || !windowState)
2451 return;
2453 tmp = windowState;
2454 if (tmp==wstate) {
2455 windowState = wstate->next;
2456 #ifdef DEBUG
2457 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2458 wstate, wstate->instance, wstate->class, wstate->command);
2459 #endif
2460 if (wstate->instance) free(wstate->instance);
2461 if (wstate->class) free(wstate->class);
2462 if (wstate->command) free(wstate->command);
2463 free(wstate->state);
2464 free(wstate);
2465 } else {
2466 while (tmp->next) {
2467 if (tmp->next==wstate) {
2468 tmp->next=wstate->next;
2469 #ifdef DEBUG
2470 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2471 wstate, wstate->instance, wstate->class, wstate->command);
2472 #endif
2473 if (wstate->instance) free(wstate->instance);
2474 if (wstate->class) free(wstate->class);
2475 if (wstate->command) free(wstate->command);
2476 free(wstate->state);
2477 free(wstate);
2478 break;
2480 tmp = tmp->next;
2486 void
2487 wWindowDeleteSavedStatesForPID(pid_t pid)
2489 WWindowState *tmp, *wstate;
2491 if (!windowState)
2492 return;
2494 tmp = windowState;
2495 if (tmp->pid == pid) {
2496 wstate = windowState;
2497 windowState = tmp->next;
2498 #ifdef DEBUG
2499 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2500 wstate, wstate->instance, wstate->class, wstate->command);
2501 #endif
2502 if (wstate->instance) free(wstate->instance);
2503 if (wstate->class) free(wstate->class);
2504 if (wstate->command) free(wstate->command);
2505 free(wstate->state);
2506 free(wstate);
2507 } else {
2508 while (tmp->next) {
2509 if (tmp->next->pid==pid) {
2510 wstate = tmp->next;
2511 tmp->next = wstate->next;
2512 #ifdef DEBUG
2513 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2514 wstate, wstate->instance, wstate->class, wstate->command);
2515 #endif
2516 if (wstate->instance) free(wstate->instance);
2517 if (wstate->class) free(wstate->class);
2518 if (wstate->command) free(wstate->command);
2519 free(wstate->state);
2520 free(wstate);
2521 break;
2523 tmp = tmp->next;
2529 /* ====================================================================== */
2531 static void
2532 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2534 WWindow *wwin = data;
2536 #ifndef NUMLOCK_HACK
2537 if ((event->xbutton.state & ValidModMask)
2538 != (event->xbutton.state & ~LockMask)) {
2539 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2540 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2542 #endif
2544 event->xbutton.state &= ValidModMask;
2546 CloseWindowMenu(wwin->screen_ptr);
2548 if (wPreferences.focus_mode==WKF_CLICK
2549 && !(event->xbutton.state&ControlMask)
2550 && !WFLAGP(wwin, no_focusable)) {
2551 wSetFocusTo(wwin->screen_ptr, wwin);
2554 if (event->xbutton.button == Button1)
2555 wRaiseFrame(wwin->frame->core);
2557 if (event->xbutton.window != wwin->frame->resizebar->window) {
2558 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2559 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2560 GrabModeAsync, GrabModeAsync, None,
2561 None, CurrentTime)!=GrabSuccess) {
2562 #ifdef DEBUG0
2563 wwarning("pointer grab failed for window move");
2564 #endif
2565 return;
2569 if (event->xbutton.state & MOD_MASK) {
2570 /* move the window */
2571 wMouseMoveWindow(wwin, event);
2572 XUngrabPointer(dpy, CurrentTime);
2573 } else {
2574 wMouseResizeWindow(wwin, event);
2575 XUngrabPointer(dpy, CurrentTime);
2581 static void
2582 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2584 WWindow *wwin = data;
2586 event->xbutton.state &= ValidModMask;
2588 if (event->xbutton.button==Button1) {
2589 if (event->xbutton.state == 0) {
2590 if (!WFLAGP(wwin, no_shadeable)) {
2591 /* shade window */
2592 if (wwin->flags.shaded)
2593 wUnshadeWindow(wwin);
2594 else
2595 wShadeWindow(wwin);
2597 } else {
2598 int dir = 0;
2600 if (event->xbutton.state & ControlMask)
2601 dir |= MAX_VERTICAL;
2603 if (event->xbutton.state & ShiftMask) {
2604 dir |= MAX_HORIZONTAL;
2605 if (!(event->xbutton.state & ControlMask))
2606 wSelectWindow(wwin, !wwin->flags.selected);
2609 /* maximize window */
2610 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2611 if (wwin->flags.maximized)
2612 wUnmaximizeWindow(wwin);
2613 else
2614 wMaximizeWindow(wwin, dir);
2617 } else if (event->xbutton.button==Button3) {
2618 if (event->xbutton.state & MOD_MASK) {
2619 wHideOtherApplications(wwin);
2621 } else if (event->xbutton.button==Button2) {
2622 wSelectWindow(wwin, !wwin->flags.selected);
2627 static void
2628 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2630 WWindow *wwin = desc->parent;
2632 event->xbutton.state &= ValidModMask;
2634 CloseWindowMenu(wwin->screen_ptr);
2636 if (wPreferences.focus_mode==WKF_CLICK
2637 && !(event->xbutton.state&ControlMask)
2638 && !WFLAGP(wwin, no_focusable)) {
2639 wSetFocusTo(wwin->screen_ptr, wwin);
2641 if (event->xbutton.button == Button1) {
2642 wRaiseFrame(wwin->frame->core);
2645 if (event->xbutton.state & MOD_MASK) {
2646 /* move the window */
2647 if (XGrabPointer(dpy, wwin->client_win, False,
2648 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2649 GrabModeAsync, GrabModeAsync, None,
2650 None, CurrentTime)!=GrabSuccess) {
2651 #ifdef DEBUG0
2652 wwarning("pointer grab failed for window move");
2653 #endif
2654 return;
2656 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2657 wMouseResizeWindow(wwin, event);
2658 else
2659 wMouseMoveWindow(wwin, event);
2660 XUngrabPointer(dpy, CurrentTime);
2665 static void
2666 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2668 WWindow *wwin = (WWindow*)data;
2670 #ifndef NUMLOCK_HACK
2671 if ((event->xbutton.state & ValidModMask)
2672 != (event->xbutton.state & ~LockMask)) {
2673 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2674 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2676 #endif
2677 event->xbutton.state &= ValidModMask;
2679 CloseWindowMenu(wwin->screen_ptr);
2681 if (wPreferences.focus_mode==WKF_CLICK
2682 && !(event->xbutton.state&ControlMask)
2683 && !WFLAGP(wwin, no_focusable)) {
2684 wSetFocusTo(wwin->screen_ptr, wwin);
2687 if (event->xbutton.button == Button1
2688 || event->xbutton.button == Button2) {
2690 if (event->xbutton.button == Button1) {
2691 if (event->xbutton.state & MOD_MASK) {
2692 wLowerFrame(wwin->frame->core);
2693 } else {
2694 wRaiseFrame(wwin->frame->core);
2697 if ((event->xbutton.state & ShiftMask)
2698 && !(event->xbutton.state & ControlMask)) {
2699 wSelectWindow(wwin, !wwin->flags.selected);
2700 return;
2702 if (event->xbutton.window != wwin->frame->titlebar->window
2703 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2704 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2705 GrabModeAsync, GrabModeAsync, None,
2706 None, CurrentTime)!=GrabSuccess) {
2707 #ifdef DEBUG0
2708 wwarning("pointer grab failed for window move");
2709 #endif
2710 return;
2713 /* move the window */
2714 wMouseMoveWindow(wwin, event);
2716 XUngrabPointer(dpy, CurrentTime);
2717 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2718 && !wwin->flags.internal_window
2719 && !WCHECK_STATE(WSTATE_MODAL)) {
2720 WObjDescriptor *desc;
2722 if (event->xbutton.window != wwin->frame->titlebar->window
2723 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2724 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2725 GrabModeAsync, GrabModeAsync, None,
2726 None, CurrentTime)!=GrabSuccess) {
2727 #ifdef DEBUG0
2728 wwarning("pointer grab failed for window move");
2729 #endif
2730 return;
2733 OpenWindowMenu(wwin, event->xbutton.x_root,
2734 wwin->frame_y+wwin->frame->top_width, False);
2736 /* allow drag select */
2737 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2738 event->xany.send_event = True;
2739 (*desc->handle_mousedown)(desc, event);
2741 XUngrabPointer(dpy, CurrentTime);
2747 static void
2748 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2750 WWindow *wwin = data;
2752 event->xbutton.state &= ValidModMask;
2754 CloseWindowMenu(wwin->screen_ptr);
2756 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2757 return;
2759 /* if control-click, kill the client */
2760 if (event->xbutton.state & ControlMask) {
2761 wClientKill(wwin);
2762 } else {
2763 #ifdef OLWM_HINTS
2764 if (wwin->flags.olwm_push_pin_out) {
2766 wwin->flags.olwm_push_pin_out = 0;
2768 wOLWMChangePushpinState(wwin, True);
2770 wFrameWindowUpdatePushButton(wwin->frame, False);
2772 return;
2774 #endif
2775 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2776 /* send delete message */
2777 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2783 static void
2784 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2786 WWindow *wwin = data;
2788 CloseWindowMenu(wwin->screen_ptr);
2790 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2791 return;
2793 /* send delete message */
2794 if (wwin->protocols.DELETE_WINDOW) {
2795 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2796 } else {
2797 wClientKill(wwin);
2802 static void
2803 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2805 WWindow *wwin = data;
2807 event->xbutton.state &= ValidModMask;
2809 CloseWindowMenu(wwin->screen_ptr);
2811 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2812 return;
2814 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2815 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2816 LastTimestamp);
2817 } else {
2818 WApplication *wapp;
2819 if ((event->xbutton.state & ControlMask) ||
2820 (event->xbutton.button == Button3)) {
2822 wapp = wApplicationOf(wwin->main_window);
2823 if (wapp && !WFLAGP(wwin, no_appicon))
2824 wHideApplication(wapp);
2825 } else if (event->xbutton.state==0) {
2826 wIconifyWindow(wwin);