2 * Window Maker window manager
4 * Copyright (c) 1997-2003 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"
54 /****** Global Variables ******/
57 extern XContext wWinContext
;
59 extern Atom _XA_WM_STATE
;
60 extern Atom _XA_WM_PROTOCOLS
;
61 extern Atom _XA_WM_COLORMAP_WINDOWS
;
63 extern Atom _XA_WINDOWMAKER_MENU
;
65 extern Atom _XA_GNUSTEP_WM_ATTR
;
66 extern Atom _XA_GNUSTEP_WM_RESIZEBAR
;
69 extern Bool wShapeSupported
;
74 *--------------------------------------------------------------------
76 * Reparent the window back to the root window.
78 *--------------------------------------------------------------------
81 wClientRestore(WWindow
*wwin
)
86 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
87 /* set the positio of the frame on screen */
88 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
89 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
90 /* if gravity is to the south, account for the border sizes */
92 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
94 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
95 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
96 wwin
->frame_x
, wwin
->frame_y
);
98 /* don't let the window get iconified after restart */
100 if (wwin->flags.shaded)
101 wClientSetState(wwin, NormalState, None);
107 *----------------------------------------------------------------------
109 * Set the state of the client window to one of the window
110 * states defined in ICCCM (Iconic, Withdrawn, Normal)
113 * The WM_STATE property of the window is updated as well as the
114 * WWindow.state variable.
115 *----------------------------------------------------------------------
118 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
124 data
[0] = (unsigned long) state
;
125 data
[1] = (unsigned long) icon_win
;
127 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
128 PropModeReplace
, (unsigned char *) data
, 2);
133 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
135 switch (wwin
->normal_hints
->win_gravity
) {
142 case NorthWestGravity
:
150 case NorthEastGravity
:
162 case SouthWestGravity
:
170 case SouthEastGravity
:
180 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
183 int nx
, ny
, nwidth
, nheight
;
186 /* printf("configure event: %d %d %d %d\n", xcre->x, xcre->y, xcre->width, xcre->height);*/
190 * configure a window that was not mapped by us
194 xwc
.width
= xcre
->width
;
195 xwc
.height
= xcre
->height
;
196 xwc
.border_width
= xcre
->border_width
;
197 xwc
.stack_mode
= xcre
->detail
;
198 xwc
.sibling
= xcre
->above
;
199 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
203 if (wShapeSupported
) {
208 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
209 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
210 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
212 wwin
->flags
.shaped
= b_shaped
;
215 if (xcre
->value_mask
& CWStackMode
) {
216 WObjDescriptor
*desc
;
219 if ((xcre
->value_mask
& CWSibling
) &&
220 (XFindContext(dpy
, xcre
->above
, wWinContext
,
221 (XPointer
*)&desc
) == XCSUCCESS
)
222 && (desc
->parent_type
==WCLASS_WINDOW
)) {
223 sibling
=desc
->parent
;
224 xwc
.sibling
= sibling
->frame
->core
->window
;
226 xwc
.sibling
= xcre
->above
;
228 xwc
.stack_mode
= xcre
->detail
;
229 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
230 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
231 /* fix stacking order */
232 RemakeStackList(wwin
->screen_ptr
);
235 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
237 if (xcre
->value_mask
& CWBorderWidth
) {
238 wwin
->old_border_width
= xcre
->border_width
;
241 if (!wwin
->flags
.shaded
) {
242 /* If the window is shaded, wrong height will be set for the window */
243 if (xcre
->value_mask
& CWX
) {
245 if (HAS_BORDER(wwin
))
246 nx
-= FRAME_BORDER_WIDTH
;
251 if (xcre
->value_mask
& CWY
) {
252 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
253 if (HAS_BORDER(wwin
))
254 ny
-= FRAME_BORDER_WIDTH
;
259 if (xcre
->value_mask
& CWWidth
)
260 nwidth
= xcre
->width
;
262 nwidth
= wwin
->frame
->core
->width
;
264 if (xcre
->value_mask
& CWHeight
)
265 nheight
= xcre
->height
;
267 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
269 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
270 wwin
->old_geometry
.x
= nx
;
271 wwin
->old_geometry
.y
= ny
;
272 wwin
->old_geometry
.width
= nwidth
;
273 wwin
->old_geometry
.height
= nheight
;
279 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
283 event
.xclient
.type
= ClientMessage
;
284 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
285 event
.xclient
.format
= 32;
286 event
.xclient
.display
= dpy
;
287 event
.xclient
.window
= wwin
->client_win
;
288 event
.xclient
.data
.l
[0] = protocol
;
289 event
.xclient
.data
.l
[1] = time
;
290 event
.xclient
.data
.l
[2] = 0;
291 event
.xclient
.data
.l
[3] = 0;
292 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
299 wClientKill(WWindow
*wwin
)
301 XKillClient(dpy
, wwin
->client_win
);
309 *----------------------------------------------------------------------
310 * wClientCheckProperty--
311 * Handles PropertyNotify'es, verifying which property was
312 * changed and updating internal state according to that, like redrawing
313 * the icon title when it is changed.
316 * Depends on the changed property.
318 * TODO: _GNUSTEP_WM_ATTR
319 *----------------------------------------------------------------------
322 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
324 XWindowAttributes attribs
;
329 switch (event
->atom
) {
331 /* window title was changed */
332 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
333 wWindowUpdateName(wwin
, NULL
);
335 wWindowUpdateName(wwin
, tmp
);
341 case XA_WM_ICON_NAME
:
343 wKWMSendEventMessage(wwin
, WKWMChangedClient
);
350 /* icon title was changed */
351 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
352 wIconChangeTitle(wwin
->icon
, new_title
);
357 if (wwin
->main_window
!=None
) {
358 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
361 if (!wapp
|| !wapp
->app_icon
|| wapp
->app_icon
->docked
)
364 command
= GetCommandForWindow(wwin
->main_window
);
366 if (wapp
->app_icon
->command
)
367 wfree(wapp
->app_icon
->command
);
368 wapp
->app_icon
->command
= command
;
376 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
378 /* group leader update
380 * This means that the window is setting the leader after
381 * it was mapped, changing leaders or removing the leader.
383 * Valid state transitions are:
395 * Where G is the window_group hint, C is CLIENT_LEADER property
396 * and ' indicates the hint is unset.
398 * 1,2 - change group leader to new value of window_group
399 * 3 - change leader to value of CLIENT_LEADER
400 * 4 - change leader to value of window_group
401 * 5 - destroy application
402 * 6 - create application
404 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
405 && new_hints
->window_group
!=None
) {
410 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
411 && wwin
->wm_hints
->window_group
!=None
) {
417 if (wwin
->client_leader
) {
419 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
421 } else if (g1
&& !g2
) {
423 } else if (!g1
&& g2
) {
430 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
432 } else if (g1
&& !g2
) {
434 } else if (!g1
&& g2
) {
441 /* Handling this may require more work. -Dan */
442 if (wwin
->fake_group
!=NULL
) {
447 XFree(wwin
->wm_hints
);
449 wwin
->wm_hints
= new_hints
;
451 /* do action according to state transition */
453 /* 3 - change leader to value of CLIENT_LEADER */
455 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
456 wwin
->main_window
= wwin
->client_leader
;
457 wwin
->group_id
= None
;
458 wApplicationCreate(wwin
);
461 /* 1,2,4 - change leader to new value of window_group */
465 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
466 wwin
->main_window
= new_hints
->window_group
;
467 wwin
->group_id
= wwin
->main_window
;
468 wApplicationCreate(wwin
);
471 /* 5 - destroy application */
473 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
474 wwin
->main_window
= None
;
475 wwin
->group_id
= None
;
478 /* 6 - create application */
480 wwin
->main_window
= new_hints
->window_group
;
481 wwin
->group_id
= wwin
->main_window
;
482 wApplicationCreate(wwin
);
484 /* 7 - we have a fake window group id, so just ignore anything else */
490 printf("window leader update caused state transition %i\n",i
);
494 if (wwin
->wm_hints
) {
496 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
497 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
500 if (wwin
->flags
.miniaturized
&& wwin
->icon
) {
501 wIconUpdate(wwin
->icon
);
503 wapp
= wApplicationOf(wwin
->main_window
);
504 if (wapp
&& wapp
->app_icon
) {
505 wIconUpdate(wapp
->app_icon
->icon
);
508 wKWMSendEventMessage(wwin
, WKWMIconChange
);
512 if (wwin
->wm_hints
->flags
& UrgencyHint
)
513 wwin
->flags
.urgent
= 1;
515 wwin
->flags
.urgent
= 0;
516 /*} else if (wwin->fake_group!=NULL) {
517 wwin->group_id = wwin->fake_group->leader;*/
519 wwin
->group_id
= None
;
523 case XA_WM_NORMAL_HINTS
:
524 /* normal (geometry) hints */
529 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
530 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
,
532 /* TODO: should we check for consistency of the current
533 * size against the new geometry hints? */
537 case XA_WM_TRANSIENT_FOR
:
542 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
545 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
546 new_owner
= wwin
->screen_ptr
->root_win
;
549 if (new_owner
!=wwin
->transient_for
) {
550 owner
= wWindowFor(wwin
->transient_for
);
552 if (owner
->flags
.semi_focused
) {
553 owner
->flags
.semi_focused
= 0;
554 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
556 wFrameWindowPaint(owner
->frame
);
559 owner
= wWindowFor(new_owner
);
561 if (!owner
->flags
.semi_focused
) {
562 owner
->flags
.semi_focused
= 1;
563 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
565 wFrameWindowPaint(owner
->frame
);
568 wwin
->transient_for
= new_owner
;
569 if (new_owner
==None
) {
570 if (WFLAGP(wwin
, no_miniaturizable
)) {
571 WSETUFLAG(wwin
, no_miniaturizable
, 0);
572 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
574 wWindowConfigureBorders(wwin
);
576 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
577 WSETUFLAG(wwin
, no_miniaturizable
, 1);
578 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
580 wWindowConfigureBorders(wwin
);
587 if (event
->atom
==_XA_WM_PROTOCOLS
) {
589 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
591 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
594 wWindowUpdateButtonImages(wwin
);
596 } else if (event
->atom
==_XA_WM_COLORMAP_WINDOWS
) {
598 GetColormapWindows(wwin
);
599 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
601 } else if (event
->atom
==_XA_WINDOWMAKER_MENU
) {
604 wapp
= wApplicationOf(wwin
->main_window
);
608 /* TODO: remake appmenu update */
609 wAppMenuDestroy(wapp
->menu
);
611 if (wwin
->fake_group
) {
612 extern WPreferences wPreferences
;
613 WScreen
*scr
= wwin
->screen_ptr
;
614 WWindow
*foo
= scr
->focused_window
;
615 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
617 wApplicationDestroy(wapp
);
619 if (foo
->fake_group
&& foo
->fake_group
==fPtr
) {
620 WSETUFLAG(foo
, shared_appicon
, 0);
621 foo
->fake_group
= NULL
;
622 if (foo
->group_id
!=None
)
623 foo
->main_window
= foo
->group_id
;
624 else if (foo
->client_leader
!=None
)
625 foo
->main_window
= foo
->client_leader
;
626 else if (WFLAGP(foo
, emulate_appicon
))
627 foo
->main_window
= foo
->client_win
;
629 foo
->main_window
= None
;
630 if (foo
->main_window
) {
631 wapp
= wApplicationCreate(foo
);
637 if (fPtr
->leader
!=None
)
638 XDestroyWindow(dpy
, fPtr
->leader
);
639 fPtr
->retainCount
= 0;
641 fPtr
->origLeader
= None
;
643 wapp
= wApplicationOf(wwin
->main_window
);
645 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
647 if (wPreferences
.auto_arrange_icons
) {
648 wArrangeIcons(wwin
->screen_ptr
, True
);
651 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
653 /* make the appmenu be mapped */
654 wSetFocusTo(wwin
->screen_ptr
, NULL
);
655 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
657 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
658 GNUstepWMAttributes
*attr
;
660 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
662 wWindowUpdateGNUstepAttr(wwin
, attr
);
666 #if defined(KWM_HINTS) || defined(NETWM_HINTS)
671 done
= wKWMCheckClientHintChange(wwin
, event
);
672 #endif /* KWM_HINTS */
675 done
= wNETWMCheckClientHintChange(wwin
, event
);
684 *----------------------------------------------------------------------
685 * wClientGetNormalHints--
686 * Get size (normal) hints and a default geometry for the client
687 * window. The hints are also checked for inconsistency. If geometry is
688 * True, the returned data will account for client specified initial
692 * normal_hints is filled with valid data.
693 *----------------------------------------------------------------------
696 wClientGetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, Bool geometry
,
697 int *x
, int *y
, unsigned *width
, unsigned *height
)
699 int pre_icccm
= 0; /* not used */
701 /* find a position for the window */
702 if (!wwin
->normal_hints
)
703 wwin
->normal_hints
= XAllocSizeHints();
705 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
706 wwin
->normal_hints
->flags
= 0;
711 *width
= wattribs
->width
;
712 *height
= wattribs
->height
;
714 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
715 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
717 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
718 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
719 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
721 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
722 wwin
->normal_hints
->base_width
= 0;
723 wwin
->normal_hints
->base_height
= 0;
725 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
726 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
727 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
730 /* some buggy apps set weird hints.. */
731 if (wwin
->normal_hints
->min_width
<= 0)
732 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
734 if (wwin
->normal_hints
->min_height
<= 0)
735 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
738 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
739 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
741 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
742 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
744 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
745 wwin
->normal_hints
->width_inc
= 1;
746 wwin
->normal_hints
->height_inc
= 1;
748 if (wwin
->normal_hints
->width_inc
<= 0)
749 wwin
->normal_hints
->width_inc
= 1;
750 if (wwin
->normal_hints
->height_inc
<= 0)
751 wwin
->normal_hints
->height_inc
= 1;
754 if (wwin
->normal_hints
->flags
& PAspect
) {
755 if (wwin
->normal_hints
->min_aspect
.x
< 1)
756 wwin
->normal_hints
->min_aspect
.x
= 1;
757 if (wwin
->normal_hints
->min_aspect
.y
< 1)
758 wwin
->normal_hints
->min_aspect
.y
= 1;
760 if (wwin
->normal_hints
->max_aspect
.x
< 1)
761 wwin
->normal_hints
->max_aspect
.x
= 1;
762 if (wwin
->normal_hints
->max_aspect
.y
< 1)
763 wwin
->normal_hints
->max_aspect
.y
= 1;
766 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
767 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
769 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
770 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
773 #ifdef IGNORE_PPOSITION
774 wwin
->normal_hints
->flags
&= ~PPosition
;
777 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
778 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
779 *x
= wwin
->normal_hints
->x
;
780 *y
= wwin
->normal_hints
->y
;
782 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
783 *width
= wwin
->normal_hints
->width
;
784 *height
= wwin
->normal_hints
->height
;
791 GetColormapWindows(WWindow
*wwin
)
794 if (wwin
->cmap_windows
) {
795 XFree(wwin
->cmap_windows
);
798 wwin
->cmap_windows
= NULL
;
799 wwin
->cmap_window_no
= 0;
801 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
802 &(wwin
->cmap_window_no
))
803 || !wwin
->cmap_windows
) {
804 wwin
->cmap_window_no
= 0;
805 wwin
->cmap_windows
= NULL
;