2 * Window Maker window manager
4 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
27 #include <X11/extensions/shape.h>
34 #include "WindowMaker.h"
38 #include "properties.h"
50 /****** Global Variables ******/
53 extern XContext wWinContext
;
55 extern Atom _XA_WM_STATE
;
56 extern Atom _XA_WM_PROTOCOLS
;
57 extern Atom _XA_WM_COLORMAP_WINDOWS
;
59 extern Atom _XA_WINDOWMAKER_MENU
;
61 extern Atom _XA_GNUSTEP_WM_ATTR
;
62 extern Atom _XA_GNUSTEP_WM_RESIZEBAR
;
65 extern Bool wShapeSupported
;
70 *--------------------------------------------------------------------
72 * Reparent the window back to the root window.
74 *--------------------------------------------------------------------
77 wClientRestore(WWindow
*wwin
)
82 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
83 /* set the positio of the frame on screen */
84 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
85 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
86 /* if gravity is to the south, account for the border sizes */
88 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
90 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
91 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
92 wwin
->frame_x
, wwin
->frame_y
);
94 /* don't let the window get iconified after restart */
96 if (wwin->flags.shaded)
97 wClientSetState(wwin, NormalState, None);
103 *----------------------------------------------------------------------
105 * Set the state of the client window to one of the window
106 * states defined in ICCCM (Iconic, Withdrawn, Normal)
109 * The WM_STATE property of the window is updated as well as the
110 * WWindow.state variable.
111 *----------------------------------------------------------------------
114 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
120 data
[0] = (unsigned long) state
;
121 data
[1] = (unsigned long) icon_win
;
123 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
124 PropModeReplace
, (unsigned char *) data
, 2);
129 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
131 switch (wwin
->normal_hints
->win_gravity
) {
138 case NorthWestGravity
:
146 case NorthEastGravity
:
158 case SouthWestGravity
:
166 case SouthEastGravity
:
176 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
179 int nx
, ny
, nwidth
, nheight
;
184 * configure a window that was not mapped by us
188 xwc
.width
= xcre
->width
;
189 xwc
.height
= xcre
->height
;
190 xwc
.border_width
= xcre
->border_width
;
191 xwc
.stack_mode
= xcre
->detail
;
192 xwc
.sibling
= xcre
->above
;
193 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
197 if (wShapeSupported
) {
202 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
203 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
204 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
206 wwin
->flags
.shaped
= b_shaped
;
209 if (xcre
->value_mask
& CWStackMode
) {
210 WObjDescriptor
*desc
;
213 if ((xcre
->value_mask
& CWSibling
) &&
214 (XFindContext(dpy
, xcre
->above
, wWinContext
,
215 (XPointer
*)&desc
) == XCSUCCESS
)
216 && (desc
->parent_type
==WCLASS_WINDOW
)) {
217 sibling
=desc
->parent
;
218 xwc
.sibling
= sibling
->frame
->core
->window
;
220 xwc
.sibling
= xcre
->above
;
222 xwc
.stack_mode
= xcre
->detail
;
223 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
224 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
225 /* fix stacking order */
226 RemakeStackList(wwin
->screen_ptr
);
229 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
231 if (xcre
->value_mask
& CWBorderWidth
) {
232 wwin
->old_border_width
= xcre
->border_width
;
235 if (!wwin
->flags
.shaded
) {
236 /* If the window is shaded, wrong height will be set for the window */
237 if (xcre
->value_mask
& CWX
) {
239 if (!WFLAGP(wwin
, no_border
))
240 nx
-= FRAME_BORDER_WIDTH
;
245 if (xcre
->value_mask
& CWY
) {
246 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
247 if (!WFLAGP(wwin
, no_border
))
248 ny
-= FRAME_BORDER_WIDTH
;
253 if (xcre
->value_mask
& CWWidth
)
254 nwidth
= xcre
->width
;
256 nwidth
= wwin
->frame
->core
->width
;
258 if (xcre
->value_mask
& CWHeight
)
259 nheight
= xcre
->height
;
261 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
263 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
264 wwin
->old_geometry
.x
= nx
;
265 wwin
->old_geometry
.y
= ny
;
266 wwin
->old_geometry
.width
= nwidth
;
267 wwin
->old_geometry
.height
= nheight
;
273 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
277 event
.xclient
.type
= ClientMessage
;
278 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
279 event
.xclient
.format
= 32;
280 event
.xclient
.display
= dpy
;
281 event
.xclient
.window
= wwin
->client_win
;
282 event
.xclient
.data
.l
[0] = protocol
;
283 event
.xclient
.data
.l
[1] = time
;
284 event
.xclient
.data
.l
[2] = 0;
285 event
.xclient
.data
.l
[3] = 0;
286 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
293 wClientKill(WWindow
*wwin
)
295 XKillClient(dpy
, wwin
->client_win
);
303 *----------------------------------------------------------------------
304 * wClientCheckProperty--
305 * Handles PropertyNotify'es, verifying which property was
306 * changed and updating internal state according to that, like redrawing
307 * the icon title when it is changed.
310 * Depends on the changed property.
312 * TODO: _GNUSTEP_WM_ATTR
313 *----------------------------------------------------------------------
316 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
318 XWindowAttributes attribs
;
323 switch (event
->atom
) {
325 /* window title was changed */
326 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
327 wWindowUpdateName(wwin
, NULL
);
329 wWindowUpdateName(wwin
, tmp
);
335 case XA_WM_ICON_NAME
:
337 wKWMSendEventMessage(wwin
, WKWMChangedClient
);
344 /* icon title was changed */
345 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
346 wIconChangeTitle(wwin
->icon
, new_title
);
351 if (wwin
->main_window
!=None
) {
352 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
356 if (!wapp
|| !wapp
->app_icon
)
359 if (XGetCommand(dpy
, wwin
->main_window
, &argv
, &argc
)) {
360 if (argc
> 0 && argv
!= NULL
) {
361 if (wapp
->app_icon
->command
)
362 wfree(wapp
->app_icon
->command
);
363 wapp
->app_icon
->command
= wtokenjoin(argv
, argc
);
366 XFreeStringList(argv
);
375 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
377 /* group leader update
379 * This means that the window is setting the leader after
380 * it was mapped, changing leaders or removing the leader.
382 * Valid state transitions are:
394 * Where G is the window_group hint, C is CLIENT_LEADER property
395 * and ' indicates the hint is unset.
397 * 1,2 - change group leader to new value of window_group
398 * 3 - change leader to value of CLIENT_LEADER
399 * 4 - change leader to value of window_group
400 * 5 - destroy application
401 * 6 - create application
403 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
404 && new_hints
->window_group
!=None
) {
409 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
410 && wwin
->wm_hints
->window_group
!=None
) {
416 if (wwin
->client_leader
) {
418 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
420 } else if (g1
&& !g2
) {
422 } else if (!g1
&& g2
) {
429 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
431 } else if (g1
&& !g2
) {
433 } else if (!g1
&& g2
) {
440 /* Handling this may require more work. -Dan */
441 if (wwin
->fake_group
!=NULL
) {
446 XFree(wwin
->wm_hints
);
448 wwin
->wm_hints
= new_hints
;
450 /* do action according to state transition */
452 /* 3 - change leader to value of CLIENT_LEADER */
454 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
455 wwin
->main_window
= wwin
->client_leader
;
456 wwin
->group_id
= None
;
457 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
460 /* 1,2,4 - change leader to new value of window_group */
464 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
465 wwin
->main_window
= new_hints
->window_group
;
466 wwin
->group_id
= wwin
->main_window
;
467 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
470 /* 5 - destroy application */
472 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
473 wwin
->main_window
= None
;
474 wwin
->group_id
= None
;
477 /* 6 - create application */
479 wwin
->main_window
= new_hints
->window_group
;
480 wwin
->group_id
= wwin
->main_window
;
481 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
483 /* 7 - we have a fake window group id, so just ignore anything else */
489 printf("window leader update caused state transition %i\n",i
);
493 if (wwin
->wm_hints
) {
495 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
496 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
499 if (wwin
->flags
.miniaturized
&& wwin
->icon
) {
500 wIconUpdate(wwin
->icon
);
502 wapp
= wApplicationOf(wwin
->main_window
);
503 if (wapp
&& wapp
->app_icon
) {
504 wIconUpdate(wapp
->app_icon
->icon
);
507 wKWMSendEventMessage(wwin
, WKWMIconChange
);
511 if (wwin
->wm_hints
->flags
& UrgencyHint
)
512 wwin
->flags
.urgent
= 1;
514 wwin
->flags
.urgent
= 0;
515 /*} else if (wwin->fake_group!=NULL) {
516 wwin->group_id = wwin->fake_group->leader;*/
518 wwin
->group_id
= None
;
522 case XA_WM_NORMAL_HINTS
:
523 /* normal (geometry) hints */
528 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
529 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
,
531 /* TODO: should we check for consistency of the current
532 * size against the new geometry hints? */
536 case XA_WM_TRANSIENT_FOR
:
541 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
544 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
545 new_owner
= wwin
->screen_ptr
->root_win
;
548 if (new_owner
!=wwin
->transient_for
) {
549 owner
= wWindowFor(wwin
->transient_for
);
551 if (owner
->flags
.semi_focused
) {
552 owner
->flags
.semi_focused
= 0;
553 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
555 wFrameWindowPaint(owner
->frame
);
558 owner
= wWindowFor(new_owner
);
560 if (!owner
->flags
.semi_focused
) {
561 owner
->flags
.semi_focused
= 1;
562 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
564 wFrameWindowPaint(owner
->frame
);
567 wwin
->transient_for
= new_owner
;
568 if (new_owner
==None
) {
569 if (WFLAGP(wwin
, no_miniaturizable
)) {
570 WSETUFLAG(wwin
, no_miniaturizable
, 0);
571 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
573 wWindowConfigureBorders(wwin
);
575 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
576 WSETUFLAG(wwin
, no_miniaturizable
, 1);
577 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
579 wWindowConfigureBorders(wwin
);
586 if (event
->atom
==_XA_WM_PROTOCOLS
) {
588 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
590 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
593 wWindowUpdateButtonImages(wwin
);
595 } else if (event
->atom
==_XA_WM_COLORMAP_WINDOWS
) {
597 GetColormapWindows(wwin
);
598 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
600 } else if (event
->atom
==_XA_WINDOWMAKER_MENU
) {
603 wapp
= wApplicationOf(wwin
->main_window
);
607 /* TODO: remake appmenu update */
608 wAppMenuDestroy(wapp
->menu
);
610 if (wwin
->fake_group
) {
611 extern WPreferences wPreferences
;
612 WScreen
*scr
= wwin
->screen_ptr
;
613 WWindow
*foo
= scr
->focused_window
;
614 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
616 wApplicationDestroy(wapp
);
618 if (foo
->fake_group
&& foo
->fake_group
==fPtr
) {
619 WSETUFLAG(foo
, shared_appicon
, 0);
620 foo
->fake_group
= NULL
;
621 if (foo
->group_id
!=None
)
622 foo
->main_window
= foo
->group_id
;
623 else if (foo
->client_leader
!=None
)
624 foo
->main_window
= foo
->client_leader
;
625 else if (WFLAGP(foo
, emulate_appicon
))
626 foo
->main_window
= foo
->client_win
;
628 foo
->main_window
= None
;
629 if (foo
->main_window
) {
630 wapp
= wApplicationCreate(scr
, foo
->main_window
);
636 if (fPtr
->leader
!=None
)
637 XDestroyWindow(dpy
, fPtr
->leader
);
638 fPtr
->retainCount
= 0;
640 fPtr
->origLeader
= None
;
642 wapp
= wApplicationOf(wwin
->main_window
);
644 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
646 if (wPreferences
.auto_arrange_icons
) {
647 wArrangeIcons(wwin
->screen_ptr
, True
);
650 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
652 /* make the appmenu be mapped */
653 wSetFocusTo(wwin
->screen_ptr
, NULL
);
654 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
656 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
657 GNUstepWMAttributes
*attr
;
659 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
661 wWindowUpdateGNUstepAttr(wwin
, attr
);
668 done
= wKWMCheckClientHintChange(wwin
, event
);
669 #endif /* KWM_HINTS */
676 *----------------------------------------------------------------------
677 * wClientGetNormalHints--
678 * Get size (normal) hints and a default geometry for the client
679 * window. The hints are also checked for inconsistency. If geometry is
680 * True, the returned data will account for client specified initial
684 * normal_hints is filled with valid data.
685 *----------------------------------------------------------------------
688 wClientGetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, Bool geometry
,
689 int *x
, int *y
, unsigned *width
, unsigned *height
)
691 int pre_icccm
= 0; /* not used */
693 /* find a position for the window */
694 if (!wwin
->normal_hints
)
695 wwin
->normal_hints
= XAllocSizeHints();
697 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
698 wwin
->normal_hints
->flags
= 0;
703 *width
= wattribs
->width
;
704 *height
= wattribs
->height
;
706 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
707 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
709 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
710 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
711 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
713 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
714 wwin
->normal_hints
->base_width
= 0;
715 wwin
->normal_hints
->base_height
= 0;
717 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
718 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
719 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
722 /* some buggy apps set weird hints.. */
723 if (wwin
->normal_hints
->min_width
<= 0)
724 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
726 if (wwin
->normal_hints
->min_height
<= 0)
727 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
730 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
731 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
733 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
734 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
736 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
737 wwin
->normal_hints
->width_inc
= 1;
738 wwin
->normal_hints
->height_inc
= 1;
740 if (wwin
->normal_hints
->width_inc
<= 0)
741 wwin
->normal_hints
->width_inc
= 1;
742 if (wwin
->normal_hints
->height_inc
<= 0)
743 wwin
->normal_hints
->height_inc
= 1;
746 if (wwin
->normal_hints
->flags
& PAspect
) {
747 if (wwin
->normal_hints
->min_aspect
.x
< 1)
748 wwin
->normal_hints
->min_aspect
.x
= 1;
749 if (wwin
->normal_hints
->min_aspect
.y
< 1)
750 wwin
->normal_hints
->min_aspect
.y
= 1;
752 if (wwin
->normal_hints
->max_aspect
.x
< 1)
753 wwin
->normal_hints
->max_aspect
.x
= 1;
754 if (wwin
->normal_hints
->max_aspect
.y
< 1)
755 wwin
->normal_hints
->max_aspect
.y
= 1;
758 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
759 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
761 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
762 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
765 #ifdef IGNORE_PPOSITION
766 wwin
->normal_hints
->flags
&= ~PPosition
;
769 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
770 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
771 *x
= wwin
->normal_hints
->x
;
772 *y
= wwin
->normal_hints
->y
;
774 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
775 *width
= wwin
->normal_hints
->width
;
776 *height
= wwin
->normal_hints
->height
;
783 GetColormapWindows(WWindow
*wwin
)
786 if (wwin
->cmap_windows
) {
787 XFree(wwin
->cmap_windows
);
790 wwin
->cmap_windows
= NULL
;
791 wwin
->cmap_window_no
= 0;
793 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
794 &(wwin
->cmap_window_no
))
795 || !wwin
->cmap_windows
) {
796 wwin
->cmap_window_no
= 0;
797 wwin
->cmap_windows
= NULL
;