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"
37 #include "properties.h"
47 /****** Global Variables ******/
50 extern XContext wWinContext
;
52 extern Atom _XA_WM_STATE
;
53 extern Atom _XA_WM_PROTOCOLS
;
54 extern Atom _XA_WM_COLORMAP_WINDOWS
;
56 extern Atom _XA_WINDOWMAKER_MENU
;
58 extern Atom _XA_GNUSTEP_WM_ATTR
;
59 extern Atom _XA_GNUSTEP_WM_RESIZEBAR
;
62 extern Bool wShapeSupported
;
66 *--------------------------------------------------------------------
68 * Reparent the window back to the root window.
70 *--------------------------------------------------------------------
72 void wClientRestore(WWindow
* wwin
)
77 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
78 /* set the positio of the frame on screen */
79 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
80 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
81 /* if gravity is to the south, account for the border sizes */
83 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
85 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
86 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
, wwin
->frame_x
, wwin
->frame_y
);
88 /* don't let the window get iconified after restart */
90 if (wwin->flags.shaded)
91 wClientSetState(wwin, NormalState, None);
96 *----------------------------------------------------------------------
98 * Set the state of the client window to one of the window
99 * states defined in ICCCM (Iconic, Withdrawn, Normal)
102 * The WM_STATE property of the window is updated as well as the
103 * WWindow.state variable.
104 *----------------------------------------------------------------------
106 void wClientSetState(WWindow
* wwin
, int state
, Window icon_win
)
112 data
[0] = (unsigned long)state
;
113 data
[1] = (unsigned long)icon_win
;
115 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
116 PropModeReplace
, (unsigned char *)data
, 2);
119 void wClientGetGravityOffsets(WWindow
* wwin
, int *ofs_x
, int *ofs_y
)
121 switch (wwin
->normal_hints
->win_gravity
) {
128 case NorthWestGravity
:
136 case NorthEastGravity
:
148 case SouthWestGravity
:
156 case SouthEastGravity
:
163 void wClientConfigure(WWindow
* wwin
, XConfigureRequestEvent
* xcre
)
166 int nx
, ny
, nwidth
, nheight
;
169 /* printf("configure event: %d %d %d %d\n", xcre->x, xcre->y, xcre->width, xcre->height); */
173 * configure a window that was not mapped by us
177 xwc
.width
= xcre
->width
;
178 xwc
.height
= xcre
->height
;
179 xwc
.border_width
= xcre
->border_width
;
180 xwc
.stack_mode
= xcre
->detail
;
181 xwc
.sibling
= xcre
->above
;
182 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
186 if (wShapeSupported
) {
191 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
192 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
193 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
, &ujunk
);
194 wwin
->flags
.shaped
= b_shaped
;
197 if (xcre
->value_mask
& CWStackMode
) {
198 WObjDescriptor
*desc
;
201 if ((xcre
->value_mask
& CWSibling
) &&
202 (XFindContext(dpy
, xcre
->above
, wWinContext
, (XPointer
*) & desc
) == XCSUCCESS
)
203 && (desc
->parent_type
== WCLASS_WINDOW
)) {
204 sibling
= desc
->parent
;
205 xwc
.sibling
= sibling
->frame
->core
->window
;
207 xwc
.sibling
= xcre
->above
;
209 xwc
.stack_mode
= xcre
->detail
;
210 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
211 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
212 /* fix stacking order */
213 RemakeStackList(wwin
->screen_ptr
);
216 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
218 if (xcre
->value_mask
& CWBorderWidth
) {
219 wwin
->old_border_width
= xcre
->border_width
;
222 if (!wwin
->flags
.shaded
) {
223 /* If the window is shaded, wrong height will be set for the window */
224 if (xcre
->value_mask
& CWX
) {
226 /* Subtracting the border makes the window shift by 1 pixel -Dan */
227 /*if (HAS_BORDER(wwin)) {
228 nx -= FRAME_BORDER_WIDTH;
234 if (xcre
->value_mask
& CWY
) {
235 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
236 /* Subtracting the border makes the window shift by 1 pixel -Dan */
237 /*if (HAS_BORDER(wwin)) {
238 ny -= FRAME_BORDER_WIDTH;
244 if (xcre
->value_mask
& CWWidth
)
245 nwidth
= xcre
->width
;
247 nwidth
= wwin
->frame
->core
->width
;
249 if (xcre
->value_mask
& CWHeight
)
250 nheight
= xcre
->height
;
252 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
254 if (nwidth
!= wwin
->old_geometry
.width
)
255 wwin
->flags
.maximized
&= ~(MAX_HORIZONTAL
| MAX_MAXIMUS
);
256 if (nheight
!= wwin
->old_geometry
.height
)
257 wwin
->flags
.maximized
&= ~(MAX_VERTICAL
| MAX_LEFTHALF
| MAX_RIGHTHALF
| MAX_MAXIMUS
);
259 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
260 wwin
->old_geometry
.x
= nx
;
261 wwin
->old_geometry
.y
= ny
;
262 wwin
->old_geometry
.width
= nwidth
;
263 wwin
->old_geometry
.height
= nheight
;
267 void wClientSendProtocol(WWindow
* wwin
, Atom protocol
, Time time
)
271 event
.xclient
.type
= ClientMessage
;
272 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
273 event
.xclient
.format
= 32;
274 event
.xclient
.display
= dpy
;
275 event
.xclient
.window
= wwin
->client_win
;
276 event
.xclient
.data
.l
[0] = protocol
;
277 event
.xclient
.data
.l
[1] = time
;
278 event
.xclient
.data
.l
[2] = 0;
279 event
.xclient
.data
.l
[3] = 0;
280 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
284 void wClientKill(WWindow
* wwin
)
286 XKillClient(dpy
, wwin
->client_win
);
292 *----------------------------------------------------------------------
293 * wClientCheckProperty--
294 * Handles PropertyNotify'es, verifying which property was
295 * changed and updating internal state according to that, like redrawing
296 * the icon title when it is changed.
299 * Depends on the changed property.
301 * TODO: _GNUSTEP_WM_ATTR
302 *----------------------------------------------------------------------
304 void wClientCheckProperty(WWindow
* wwin
, XPropertyEvent
* event
)
306 XWindowAttributes attribs
;
311 switch (event
->atom
) {
313 if (!wwin
->flags
.net_has_title
) {
314 /* window title was changed */
315 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
316 wWindowUpdateName(wwin
, NULL
);
318 wWindowUpdateName(wwin
, tmp
);
325 case XA_WM_ICON_NAME
:
326 if (!wwin
->flags
.net_has_icon_title
) {
332 /* icon title was changed */
333 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
334 wIconChangeTitle(wwin
->icon
, new_title
);
340 if (wwin
->main_window
!= None
) {
341 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
344 if (!wapp
|| !wapp
->app_icon
|| wapp
->app_icon
->docked
)
347 command
= GetCommandForWindow(wwin
->main_window
);
349 if (wapp
->app_icon
->command
)
350 wfree(wapp
->app_icon
->command
);
351 wapp
->app_icon
->command
= command
;
359 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
361 /* group leader update
363 * This means that the window is setting the leader after
364 * it was mapped, changing leaders or removing the leader.
366 * Valid state transitions are:
378 * Where G is the window_group hint, C is CLIENT_LEADER property
379 * and ' indicates the hint is unset.
381 * 1,2 - change group leader to new value of window_group
382 * 3 - change leader to value of CLIENT_LEADER
383 * 4 - change leader to value of window_group
384 * 5 - destroy application
385 * 6 - create application
387 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
388 && new_hints
->window_group
!= None
) {
393 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
394 && wwin
->wm_hints
->window_group
!= None
) {
400 if (wwin
->client_leader
) {
401 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
403 } else if (g1
&& !g2
) {
405 } else if (!g1
&& g2
) {
411 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
413 } else if (g1
&& !g2
) {
415 } else if (!g1
&& g2
) {
422 /* Handling this may require more work. -Dan */
423 if (wwin
->fake_group
!= NULL
) {
428 XFree(wwin
->wm_hints
);
430 wwin
->wm_hints
= new_hints
;
432 /* do action according to state transition */
434 /* 3 - change leader to value of CLIENT_LEADER */
436 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
437 wwin
->main_window
= wwin
->client_leader
;
438 wwin
->group_id
= None
;
439 wApplicationCreate(wwin
);
442 /* 1,2,4 - change leader to new value of window_group */
446 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
447 wwin
->main_window
= new_hints
->window_group
;
448 wwin
->group_id
= wwin
->main_window
;
449 wApplicationCreate(wwin
);
452 /* 5 - destroy application */
454 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
455 wwin
->main_window
= None
;
456 wwin
->group_id
= None
;
459 /* 6 - create application */
461 wwin
->main_window
= new_hints
->window_group
;
462 wwin
->group_id
= wwin
->main_window
;
463 wApplicationCreate(wwin
);
465 /* 7 - we have a fake window group id, so just ignore anything else */
470 if (wwin
->wm_hints
) {
472 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
473 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
476 if (wwin
->flags
.miniaturized
&& wwin
->icon
) {
477 wIconUpdate(wwin
->icon
);
479 wapp
= wApplicationOf(wwin
->main_window
);
480 if (wapp
&& wapp
->app_icon
) {
481 wIconUpdate(wapp
->app_icon
->icon
);
485 if (wwin
->wm_hints
->flags
& UrgencyHint
)
486 wwin
->flags
.urgent
= 1;
488 wwin
->flags
.urgent
= 0;
489 /*} else if (wwin->fake_group!=NULL) {
490 wwin->group_id = wwin->fake_group->leader; */
492 wwin
->group_id
= None
;
496 case XA_WM_NORMAL_HINTS
:
497 /* normal (geometry) hints */
502 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
503 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
, &bar
, &bar
);
504 /* TODO: should we check for consistency of the current
505 * size against the new geometry hints? */
509 case XA_WM_TRANSIENT_FOR
:
514 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
517 if (new_owner
== 0 || new_owner
== wwin
->client_win
) {
518 new_owner
= wwin
->screen_ptr
->root_win
;
521 if (new_owner
!= wwin
->transient_for
) {
522 owner
= wWindowFor(wwin
->transient_for
);
524 if (owner
->flags
.semi_focused
) {
525 owner
->flags
.semi_focused
= 0;
526 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
528 wFrameWindowPaint(owner
->frame
);
531 owner
= wWindowFor(new_owner
);
533 if (!owner
->flags
.semi_focused
) {
534 owner
->flags
.semi_focused
= 1;
535 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
537 wFrameWindowPaint(owner
->frame
);
540 wwin
->transient_for
= new_owner
;
541 if (new_owner
== None
) {
542 if (WFLAGP(wwin
, no_miniaturizable
)) {
543 WSETUFLAG(wwin
, no_miniaturizable
, 0);
544 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
546 wWindowConfigureBorders(wwin
);
548 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
549 WSETUFLAG(wwin
, no_miniaturizable
, 1);
550 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
552 wWindowConfigureBorders(wwin
);
559 if (event
->atom
== _XA_WM_PROTOCOLS
) {
561 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
563 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
566 wWindowUpdateButtonImages(wwin
);
568 } else if (event
->atom
== _XA_WM_COLORMAP_WINDOWS
) {
570 GetColormapWindows(wwin
);
571 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
573 } else if (event
->atom
== _XA_WINDOWMAKER_MENU
) {
576 wapp
= wApplicationOf(wwin
->main_window
);
580 /* TODO: remake appmenu update */
581 wAppMenuDestroy(wapp
->menu
);
583 if (wwin
->fake_group
) {
584 extern WPreferences wPreferences
;
585 WScreen
*scr
= wwin
->screen_ptr
;
586 WWindow
*foo
= scr
->focused_window
;
587 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
589 wApplicationDestroy(wapp
);
591 if (foo
->fake_group
&& foo
->fake_group
== fPtr
) {
592 WSETUFLAG(foo
, shared_appicon
, 0);
593 foo
->fake_group
= NULL
;
594 if (foo
->group_id
!= None
)
595 foo
->main_window
= foo
->group_id
;
596 else if (foo
->client_leader
!= None
)
597 foo
->main_window
= foo
->client_leader
;
598 else if (WFLAGP(foo
, emulate_appicon
))
599 foo
->main_window
= foo
->client_win
;
601 foo
->main_window
= None
;
602 if (foo
->main_window
) {
603 wapp
= wApplicationCreate(foo
);
609 if (fPtr
->leader
!= None
)
610 XDestroyWindow(dpy
, fPtr
->leader
);
611 fPtr
->retainCount
= 0;
613 fPtr
->origLeader
= None
;
615 wapp
= wApplicationOf(wwin
->main_window
);
617 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
619 if (wPreferences
.auto_arrange_icons
) {
620 wArrangeIcons(wwin
->screen_ptr
, True
);
623 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
625 /* make the appmenu be mapped */
626 wSetFocusTo(wwin
->screen_ptr
, NULL
);
627 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
629 } else if (event
->atom
== _XA_GNUSTEP_WM_ATTR
) {
630 GNUstepWMAttributes
*attr
;
632 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
634 wWindowUpdateGNUstepAttr(wwin
, attr
);
638 wNETWMCheckClientHintChange(wwin
, event
);
644 *----------------------------------------------------------------------
645 * wClientGetNormalHints--
646 * Get size (normal) hints and a default geometry for the client
647 * window. The hints are also checked for inconsistency. If geometry is
648 * True, the returned data will account for client specified initial
652 * normal_hints is filled with valid data.
653 *----------------------------------------------------------------------
656 wClientGetNormalHints(WWindow
* wwin
, XWindowAttributes
* wattribs
, Bool geometry
,
657 int *x
, int *y
, unsigned *width
, unsigned *height
)
659 int pre_icccm
= 0; /* not used */
661 /* find a position for the window */
662 if (!wwin
->normal_hints
)
663 wwin
->normal_hints
= XAllocSizeHints();
665 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
666 wwin
->normal_hints
->flags
= 0;
671 *width
= wattribs
->width
;
672 *height
= wattribs
->height
;
674 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
675 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
677 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
678 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
679 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
681 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
682 wwin
->normal_hints
->base_width
= 0;
683 wwin
->normal_hints
->base_height
= 0;
685 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
686 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
* 2;
687 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
* 2;
690 /* some buggy apps set weird hints.. */
691 if (wwin
->normal_hints
->min_width
<= 0)
692 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
694 if (wwin
->normal_hints
->min_height
<= 0)
695 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
697 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
698 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
700 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
701 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
703 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
704 wwin
->normal_hints
->width_inc
= 1;
705 wwin
->normal_hints
->height_inc
= 1;
707 if (wwin
->normal_hints
->width_inc
<= 0)
708 wwin
->normal_hints
->width_inc
= 1;
709 if (wwin
->normal_hints
->height_inc
<= 0)
710 wwin
->normal_hints
->height_inc
= 1;
713 if (wwin
->normal_hints
->flags
& PAspect
) {
714 if (wwin
->normal_hints
->min_aspect
.x
< 1)
715 wwin
->normal_hints
->min_aspect
.x
= 1;
716 if (wwin
->normal_hints
->min_aspect
.y
< 1)
717 wwin
->normal_hints
->min_aspect
.y
= 1;
719 if (wwin
->normal_hints
->max_aspect
.x
< 1)
720 wwin
->normal_hints
->max_aspect
.x
= 1;
721 if (wwin
->normal_hints
->max_aspect
.y
< 1)
722 wwin
->normal_hints
->max_aspect
.y
= 1;
725 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
726 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
728 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
729 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
731 #ifdef IGNORE_PPOSITION
732 wwin
->normal_hints
->flags
&= ~PPosition
;
735 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
736 if (wwin
->normal_hints
->flags
& (USPosition
| PPosition
)) {
737 *x
= wwin
->normal_hints
->x
;
738 *y
= wwin
->normal_hints
->y
;
740 if (wwin
->normal_hints
->flags
& (USSize
| PSize
)) {
741 *width
= wwin
->normal_hints
->width
;
742 *height
= wwin
->normal_hints
->height
;
747 void GetColormapWindows(WWindow
* wwin
)
750 if (wwin
->cmap_windows
) {
751 XFree(wwin
->cmap_windows
);
754 wwin
->cmap_windows
= NULL
;
755 wwin
->cmap_window_no
= 0;
757 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
), &(wwin
->cmap_window_no
))
758 || !wwin
->cmap_windows
) {
759 wwin
->cmap_window_no
= 0;
760 wwin
->cmap_windows
= NULL
;