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"
48 /****** Global Variables ******/
51 extern XContext wWinContext
;
53 extern Atom _XA_WM_STATE
;
54 extern Atom _XA_WM_PROTOCOLS
;
55 extern Atom _XA_WM_COLORMAP_WINDOWS
;
57 extern Atom _XA_WINDOWMAKER_MENU
;
59 extern Atom _XA_GNUSTEP_WM_ATTR
;
60 extern Atom _XA_GNUSTEP_WM_RESIZEBAR
;
63 extern Bool wShapeSupported
;
67 *--------------------------------------------------------------------
69 * Reparent the window back to the root window.
71 *--------------------------------------------------------------------
73 void wClientRestore(WWindow
* wwin
)
78 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
79 /* set the positio of the frame on screen */
80 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
81 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
82 /* if gravity is to the south, account for the border sizes */
84 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
86 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
87 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
, wwin
->frame_x
, wwin
->frame_y
);
89 /* don't let the window get iconified after restart */
91 if (wwin->flags.shaded)
92 wClientSetState(wwin, NormalState, None);
97 *----------------------------------------------------------------------
99 * Set the state of the client window to one of the window
100 * states defined in ICCCM (Iconic, Withdrawn, Normal)
103 * The WM_STATE property of the window is updated as well as the
104 * WWindow.state variable.
105 *----------------------------------------------------------------------
107 void wClientSetState(WWindow
* wwin
, int state
, Window icon_win
)
113 data
[0] = (unsigned long)state
;
114 data
[1] = (unsigned long)icon_win
;
116 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
117 PropModeReplace
, (unsigned char *)data
, 2);
120 void wClientGetGravityOffsets(WWindow
* wwin
, int *ofs_x
, int *ofs_y
)
122 switch (wwin
->normal_hints
->win_gravity
) {
129 case NorthWestGravity
:
137 case NorthEastGravity
:
149 case SouthWestGravity
:
157 case SouthEastGravity
:
164 void wClientConfigure(WWindow
* wwin
, XConfigureRequestEvent
* xcre
)
167 int nx
, ny
, nwidth
, nheight
;
170 /* printf("configure event: %d %d %d %d\n", xcre->x, xcre->y, xcre->width, xcre->height); */
174 * configure a window that was not mapped by us
178 xwc
.width
= xcre
->width
;
179 xwc
.height
= xcre
->height
;
180 xwc
.border_width
= xcre
->border_width
;
181 xwc
.stack_mode
= xcre
->detail
;
182 xwc
.sibling
= xcre
->above
;
183 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
187 if (wShapeSupported
) {
192 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
193 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
194 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
, &ujunk
);
195 wwin
->flags
.shaped
= b_shaped
;
198 if (xcre
->value_mask
& CWStackMode
) {
199 WObjDescriptor
*desc
;
202 if ((xcre
->value_mask
& CWSibling
) &&
203 (XFindContext(dpy
, xcre
->above
, wWinContext
, (XPointer
*) & desc
) == XCSUCCESS
)
204 && (desc
->parent_type
== WCLASS_WINDOW
)) {
205 sibling
= desc
->parent
;
206 xwc
.sibling
= sibling
->frame
->core
->window
;
208 xwc
.sibling
= xcre
->above
;
210 xwc
.stack_mode
= xcre
->detail
;
211 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
212 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
213 /* fix stacking order */
214 RemakeStackList(wwin
->screen_ptr
);
217 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
219 if (xcre
->value_mask
& CWBorderWidth
) {
220 wwin
->old_border_width
= xcre
->border_width
;
223 if (!wwin
->flags
.shaded
) {
224 /* If the window is shaded, wrong height will be set for the window */
225 if (xcre
->value_mask
& CWX
) {
227 /* Subtracting the border makes the window shift by 1 pixel -Dan */
228 /*if (HAS_BORDER(wwin)) {
229 nx -= FRAME_BORDER_WIDTH;
235 if (xcre
->value_mask
& CWY
) {
236 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
237 /* Subtracting the border makes the window shift by 1 pixel -Dan */
238 /*if (HAS_BORDER(wwin)) {
239 ny -= FRAME_BORDER_WIDTH;
245 if (xcre
->value_mask
& CWWidth
)
246 nwidth
= xcre
->width
;
248 nwidth
= wwin
->frame
->core
->width
;
250 if (xcre
->value_mask
& CWHeight
)
251 nheight
= xcre
->height
;
253 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
255 if (nwidth
!= wwin
->old_geometry
.width
)
256 wwin
->flags
.maximized
&= ~(MAX_HORIZONTAL
| MAX_MAXIMUS
);
257 if (nheight
!= wwin
->old_geometry
.height
)
258 wwin
->flags
.maximized
&= ~(MAX_VERTICAL
| MAX_LEFTHALF
| MAX_RIGHTHALF
| MAX_MAXIMUS
);
260 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
261 wwin
->old_geometry
.x
= nx
;
262 wwin
->old_geometry
.y
= ny
;
263 wwin
->old_geometry
.width
= nwidth
;
264 wwin
->old_geometry
.height
= nheight
;
268 void wClientSendProtocol(WWindow
* wwin
, Atom protocol
, Time time
)
272 event
.xclient
.type
= ClientMessage
;
273 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
274 event
.xclient
.format
= 32;
275 event
.xclient
.display
= dpy
;
276 event
.xclient
.window
= wwin
->client_win
;
277 event
.xclient
.data
.l
[0] = protocol
;
278 event
.xclient
.data
.l
[1] = time
;
279 event
.xclient
.data
.l
[2] = 0;
280 event
.xclient
.data
.l
[3] = 0;
281 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
285 void wClientKill(WWindow
* wwin
)
287 XKillClient(dpy
, wwin
->client_win
);
293 *----------------------------------------------------------------------
294 * wClientCheckProperty--
295 * Handles PropertyNotify'es, verifying which property was
296 * changed and updating internal state according to that, like redrawing
297 * the icon title when it is changed.
300 * Depends on the changed property.
302 * TODO: _GNUSTEP_WM_ATTR
303 *----------------------------------------------------------------------
305 void wClientCheckProperty(WWindow
* wwin
, XPropertyEvent
* event
)
307 XWindowAttributes attribs
;
312 switch (event
->atom
) {
314 if (!wwin
->flags
.net_has_title
) {
315 /* window title was changed */
316 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
317 wWindowUpdateName(wwin
, NULL
);
319 wWindowUpdateName(wwin
, tmp
);
326 case XA_WM_ICON_NAME
:
327 if (!wwin
->flags
.net_has_icon_title
) {
333 /* icon title was changed */
334 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
335 wIconChangeTitle(wwin
->icon
, new_title
);
341 if (wwin
->main_window
!= None
) {
342 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
345 if (!wapp
|| !wapp
->app_icon
|| wapp
->app_icon
->docked
)
348 command
= GetCommandForWindow(wwin
->main_window
);
350 if (wapp
->app_icon
->command
)
351 wfree(wapp
->app_icon
->command
);
352 wapp
->app_icon
->command
= command
;
360 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
362 /* group leader update
364 * This means that the window is setting the leader after
365 * it was mapped, changing leaders or removing the leader.
367 * Valid state transitions are:
379 * Where G is the window_group hint, C is CLIENT_LEADER property
380 * and ' indicates the hint is unset.
382 * 1,2 - change group leader to new value of window_group
383 * 3 - change leader to value of CLIENT_LEADER
384 * 4 - change leader to value of window_group
385 * 5 - destroy application
386 * 6 - create application
388 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
389 && new_hints
->window_group
!= None
) {
394 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
395 && wwin
->wm_hints
->window_group
!= None
) {
401 if (wwin
->client_leader
) {
402 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
404 } else if (g1
&& !g2
) {
406 } else if (!g1
&& g2
) {
412 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
414 } else if (g1
&& !g2
) {
416 } else if (!g1
&& g2
) {
423 /* Handling this may require more work. -Dan */
424 if (wwin
->fake_group
!= NULL
) {
429 XFree(wwin
->wm_hints
);
431 wwin
->wm_hints
= new_hints
;
433 /* do action according to state transition */
435 /* 3 - change leader to value of CLIENT_LEADER */
437 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
438 wwin
->main_window
= wwin
->client_leader
;
439 wwin
->group_id
= None
;
440 wApplicationCreate(wwin
);
443 /* 1,2,4 - change leader to new value of window_group */
447 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
448 wwin
->main_window
= new_hints
->window_group
;
449 wwin
->group_id
= wwin
->main_window
;
450 wApplicationCreate(wwin
);
453 /* 5 - destroy application */
455 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
456 wwin
->main_window
= None
;
457 wwin
->group_id
= None
;
460 /* 6 - create application */
462 wwin
->main_window
= new_hints
->window_group
;
463 wwin
->group_id
= wwin
->main_window
;
464 wApplicationCreate(wwin
);
466 /* 7 - we have a fake window group id, so just ignore anything else */
472 printf("window leader update caused state transition %i\n", i
);
476 if (wwin
->wm_hints
) {
478 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
479 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
482 if (wwin
->flags
.miniaturized
&& wwin
->icon
) {
483 wIconUpdate(wwin
->icon
);
485 wapp
= wApplicationOf(wwin
->main_window
);
486 if (wapp
&& wapp
->app_icon
) {
487 wIconUpdate(wapp
->app_icon
->icon
);
491 if (wwin
->wm_hints
->flags
& UrgencyHint
)
492 wwin
->flags
.urgent
= 1;
494 wwin
->flags
.urgent
= 0;
495 /*} else if (wwin->fake_group!=NULL) {
496 wwin->group_id = wwin->fake_group->leader; */
498 wwin
->group_id
= None
;
502 case XA_WM_NORMAL_HINTS
:
503 /* normal (geometry) hints */
508 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
509 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
, &bar
, &bar
);
510 /* TODO: should we check for consistency of the current
511 * size against the new geometry hints? */
515 case XA_WM_TRANSIENT_FOR
:
520 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
523 if (new_owner
== 0 || new_owner
== wwin
->client_win
) {
524 new_owner
= wwin
->screen_ptr
->root_win
;
527 if (new_owner
!= wwin
->transient_for
) {
528 owner
= wWindowFor(wwin
->transient_for
);
530 if (owner
->flags
.semi_focused
) {
531 owner
->flags
.semi_focused
= 0;
532 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
534 wFrameWindowPaint(owner
->frame
);
537 owner
= wWindowFor(new_owner
);
539 if (!owner
->flags
.semi_focused
) {
540 owner
->flags
.semi_focused
= 1;
541 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
543 wFrameWindowPaint(owner
->frame
);
546 wwin
->transient_for
= new_owner
;
547 if (new_owner
== None
) {
548 if (WFLAGP(wwin
, no_miniaturizable
)) {
549 WSETUFLAG(wwin
, no_miniaturizable
, 0);
550 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
552 wWindowConfigureBorders(wwin
);
554 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
555 WSETUFLAG(wwin
, no_miniaturizable
, 1);
556 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
558 wWindowConfigureBorders(wwin
);
565 if (event
->atom
== _XA_WM_PROTOCOLS
) {
567 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
569 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
572 wWindowUpdateButtonImages(wwin
);
574 } else if (event
->atom
== _XA_WM_COLORMAP_WINDOWS
) {
576 GetColormapWindows(wwin
);
577 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
579 } else if (event
->atom
== _XA_WINDOWMAKER_MENU
) {
582 wapp
= wApplicationOf(wwin
->main_window
);
586 /* TODO: remake appmenu update */
587 wAppMenuDestroy(wapp
->menu
);
589 if (wwin
->fake_group
) {
590 extern WPreferences wPreferences
;
591 WScreen
*scr
= wwin
->screen_ptr
;
592 WWindow
*foo
= scr
->focused_window
;
593 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
595 wApplicationDestroy(wapp
);
597 if (foo
->fake_group
&& foo
->fake_group
== fPtr
) {
598 WSETUFLAG(foo
, shared_appicon
, 0);
599 foo
->fake_group
= NULL
;
600 if (foo
->group_id
!= None
)
601 foo
->main_window
= foo
->group_id
;
602 else if (foo
->client_leader
!= None
)
603 foo
->main_window
= foo
->client_leader
;
604 else if (WFLAGP(foo
, emulate_appicon
))
605 foo
->main_window
= foo
->client_win
;
607 foo
->main_window
= None
;
608 if (foo
->main_window
) {
609 wapp
= wApplicationCreate(foo
);
615 if (fPtr
->leader
!= None
)
616 XDestroyWindow(dpy
, fPtr
->leader
);
617 fPtr
->retainCount
= 0;
619 fPtr
->origLeader
= None
;
621 wapp
= wApplicationOf(wwin
->main_window
);
623 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
625 if (wPreferences
.auto_arrange_icons
) {
626 wArrangeIcons(wwin
->screen_ptr
, True
);
629 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
631 /* make the appmenu be mapped */
632 wSetFocusTo(wwin
->screen_ptr
, NULL
);
633 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
635 } else if (event
->atom
== _XA_GNUSTEP_WM_ATTR
) {
636 GNUstepWMAttributes
*attr
;
638 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
640 wWindowUpdateGNUstepAttr(wwin
, attr
);
644 wNETWMCheckClientHintChange(wwin
, event
);
650 *----------------------------------------------------------------------
651 * wClientGetNormalHints--
652 * Get size (normal) hints and a default geometry for the client
653 * window. The hints are also checked for inconsistency. If geometry is
654 * True, the returned data will account for client specified initial
658 * normal_hints is filled with valid data.
659 *----------------------------------------------------------------------
662 wClientGetNormalHints(WWindow
* wwin
, XWindowAttributes
* wattribs
, Bool geometry
,
663 int *x
, int *y
, unsigned *width
, unsigned *height
)
665 int pre_icccm
= 0; /* not used */
667 /* find a position for the window */
668 if (!wwin
->normal_hints
)
669 wwin
->normal_hints
= XAllocSizeHints();
671 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
672 wwin
->normal_hints
->flags
= 0;
677 *width
= wattribs
->width
;
678 *height
= wattribs
->height
;
680 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
681 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
683 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
684 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
685 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
687 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
688 wwin
->normal_hints
->base_width
= 0;
689 wwin
->normal_hints
->base_height
= 0;
691 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
692 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
* 2;
693 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
* 2;
696 /* some buggy apps set weird hints.. */
697 if (wwin
->normal_hints
->min_width
<= 0)
698 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
700 if (wwin
->normal_hints
->min_height
<= 0)
701 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
703 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
704 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
706 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
707 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
709 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
710 wwin
->normal_hints
->width_inc
= 1;
711 wwin
->normal_hints
->height_inc
= 1;
713 if (wwin
->normal_hints
->width_inc
<= 0)
714 wwin
->normal_hints
->width_inc
= 1;
715 if (wwin
->normal_hints
->height_inc
<= 0)
716 wwin
->normal_hints
->height_inc
= 1;
719 if (wwin
->normal_hints
->flags
& PAspect
) {
720 if (wwin
->normal_hints
->min_aspect
.x
< 1)
721 wwin
->normal_hints
->min_aspect
.x
= 1;
722 if (wwin
->normal_hints
->min_aspect
.y
< 1)
723 wwin
->normal_hints
->min_aspect
.y
= 1;
725 if (wwin
->normal_hints
->max_aspect
.x
< 1)
726 wwin
->normal_hints
->max_aspect
.x
= 1;
727 if (wwin
->normal_hints
->max_aspect
.y
< 1)
728 wwin
->normal_hints
->max_aspect
.y
= 1;
731 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
732 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
734 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
735 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
737 #ifdef IGNORE_PPOSITION
738 wwin
->normal_hints
->flags
&= ~PPosition
;
741 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
742 if (wwin
->normal_hints
->flags
& (USPosition
| PPosition
)) {
743 *x
= wwin
->normal_hints
->x
;
744 *y
= wwin
->normal_hints
->y
;
746 if (wwin
->normal_hints
->flags
& (USSize
| PSize
)) {
747 *width
= wwin
->normal_hints
->width
;
748 *height
= wwin
->normal_hints
->height
;
753 void GetColormapWindows(WWindow
* wwin
)
756 if (wwin
->cmap_windows
) {
757 XFree(wwin
->cmap_windows
);
760 wwin
->cmap_windows
= NULL
;
761 wwin
->cmap_window_no
= 0;
763 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
), &(wwin
->cmap_window_no
))
764 || !wwin
->cmap_windows
) {
765 wwin
->cmap_window_no
= 0;
766 wwin
->cmap_windows
= NULL
;