2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 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 XUnmapWindow(dpy
, wwin
->client_win
);
91 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
92 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
93 wwin
->frame_x
, wwin
->frame_y
);
94 if (wwin
->flags
.mapped
)
95 XMapWindow(dpy
, wwin
->client_win
);
97 /* don't let the window get iconified after restart */
98 if (wwin
->flags
.shaded
)
99 wClientSetState(wwin
, NormalState
, None
);
104 *----------------------------------------------------------------------
106 * Set the state of the client window to one of the window
107 * states defined in ICCCM (Iconic, Withdrawn, Normal)
110 * The WM_STATE property of the window is updated as well as the
111 * WWindow.state variable.
112 *----------------------------------------------------------------------
115 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
121 data
[0] = (unsigned long) state
;
122 data
[1] = (unsigned long) icon_win
;
124 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
125 PropModeReplace
, (unsigned char *) data
, 2);
130 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
132 switch (wwin
->normal_hints
->win_gravity
) {
139 case NorthWestGravity
:
147 case NorthEastGravity
:
159 case SouthWestGravity
:
167 case SouthEastGravity
:
177 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
180 int nx
, ny
, nwidth
, nheight
;
185 * configure a window that was not mapped by us
189 xwc
.width
= xcre
->width
;
190 xwc
.height
= xcre
->height
;
191 xwc
.border_width
= xcre
->border_width
;
192 xwc
.stack_mode
= xcre
->detail
;
193 xwc
.sibling
= xcre
->above
;
194 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
198 if (wShapeSupported
) {
203 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
204 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
205 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
207 wwin
->flags
.shaped
= b_shaped
;
210 if (xcre
->value_mask
& CWStackMode
) {
211 WObjDescriptor
*desc
;
214 if ((xcre
->value_mask
& CWSibling
) &&
215 (XFindContext(dpy
, xcre
->above
, wWinContext
,
216 (XPointer
*)&desc
) == XCSUCCESS
)
217 && (desc
->parent_type
==WCLASS_WINDOW
)) {
218 sibling
=desc
->parent
;
219 xwc
.sibling
= sibling
->frame
->core
->window
;
221 xwc
.sibling
= xcre
->above
;
223 xwc
.stack_mode
= xcre
->detail
;
224 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
225 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
226 /* fix stacking order */
227 RemakeStackList(wwin
->screen_ptr
);
230 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
232 if (xcre
->value_mask
& CWBorderWidth
) {
233 wwin
->old_border_width
= xcre
->border_width
;
236 if (!wwin
->flags
.shaded
) {
237 /* If the window is shaded, wrong height will be set for the window */
238 if (xcre
->value_mask
& CWX
)
239 nx
= xcre
->x
- FRAME_BORDER_WIDTH
;
243 if (xcre
->value_mask
& CWY
)
244 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
)
245 - FRAME_BORDER_WIDTH
;
249 if (xcre
->value_mask
& CWWidth
)
250 nwidth
= xcre
->width
;
252 nwidth
= wwin
->frame
->core
->width
;
254 if (xcre
->value_mask
& CWHeight
)
255 nheight
= xcre
->height
;
257 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
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
;
269 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
273 event
.xclient
.type
= ClientMessage
;
274 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
275 event
.xclient
.format
= 32;
276 event
.xclient
.display
= dpy
;
277 event
.xclient
.window
= wwin
->client_win
;
278 event
.xclient
.data
.l
[0] = protocol
;
279 event
.xclient
.data
.l
[1] = time
;
280 event
.xclient
.data
.l
[2] = 0;
281 event
.xclient
.data
.l
[3] = 0;
282 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
289 wClientKill(WWindow
*wwin
)
291 XKillClient(dpy
, wwin
->client_win
);
296 *----------------------------------------------------------------------
297 * wClientCheckProperty--
298 * Handles PropertyNotify'es, verifying which property was
299 * changed and updating internal state according to that, like redrawing
300 * the icon title when it is changed.
303 * Depends on the changed property.
305 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
306 *----------------------------------------------------------------------
309 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
311 XWindowAttributes attribs
;
316 switch (event
->atom
) {
318 /* window title was changed */
320 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
321 /* the hint was removed */
322 tmp
= wstrdup(DEF_WINDOW_TITLE
);
324 wKWMSendEventMessage(wwin
, WKWMChangedClient
);
327 if (wFrameWindowChangeTitle(wwin
->frame
, tmp
)) {
328 /* only update the menu if the title has actually changed */
329 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
331 wKWMSendEventMessage(wwin
, WKWMChangedClient
);
339 case XA_WM_ICON_NAME
:
341 wKWMSendEventMessage(wwin
, WKWMChangedClient
);
348 /* icon title was changed */
349 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
350 wIconChangeTitle(wwin
->icon
, new_title
);
355 if (wwin
->main_window
!=None
) {
356 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
360 if (wapp
&& wapp
->app_icon
) {
361 if (wapp
->app_icon
->command
!=NULL
)
362 free(wapp
->app_icon
->command
);
364 if (XGetCommand(dpy
, wwin
->main_window
, &argv
, &argc
)) {
365 if (argc
> 0 && argv
!= NULL
)
366 wapp
->app_icon
->command
= FlattenStringList(argv
,argc
);
368 XFreeStringList(argv
);
378 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
380 /* group leader update
382 * This means that the window is setting the leader after
383 * it was mapped, changing leaders or removing the leader.
385 * Valid state transitions are:
397 * Where G is the window_group hint, C is CLIENT_LEADER property
398 * and ' indicates the hint is unset.
400 * 1,2 - change group leader to new value of window_group
401 * 3 - change leader to value of CLIENT_LEADER
402 * 4 - change leader to value of window_group
403 * 5 - destroy application
404 * 6 - create application
406 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
407 && new_hints
->window_group
!=None
) {
412 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
413 && wwin
->wm_hints
->window_group
!=None
) {
419 if (wwin
->client_leader
) {
421 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
423 } else if (g1
&& !g2
) {
425 } else if (!g1
&& g2
) {
432 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
434 } else if (g1
&& !g2
) {
436 } else if (!g1
&& g2
) {
444 XFree(wwin
->wm_hints
);
446 wwin
->wm_hints
= new_hints
;
448 /* do action according to state transition */
450 /* 3 - change leader to value of CLIENT_LEADER */
452 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
453 wwin
->main_window
= wwin
->client_leader
;
454 wwin
->group_id
= None
;
455 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
458 /* 1,2,4 - change leader to new value of window_group */
462 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
463 wwin
->main_window
= new_hints
->window_group
;
464 wwin
->group_id
= wwin
->main_window
;
465 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
468 /* 5 - destroy application */
470 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
471 wwin
->main_window
= None
;
472 wwin
->group_id
= None
;
475 /* 6 - create application */
477 wwin
->main_window
= new_hints
->window_group
;
478 wwin
->group_id
= wwin
->main_window
;
479 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
484 printf("window leader update caused state transition %i\n",i
);
488 if (wwin
->wm_hints
) {
490 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
491 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
494 if (wwin
->flags
.miniaturized
) {
495 wIconUpdate(wwin
->icon
);
497 wapp
= wApplicationOf(wwin
->main_window
);
498 if (wapp
&& wapp
->app_icon
) {
499 wIconUpdate(wapp
->app_icon
->icon
);
502 wKWMSendEventMessage(wwin
, WKWMIconChange
);
506 if (wwin
->wm_hints
->flags
& UrgencyHint
)
507 wwin
->flags
.urgent
= 1;
509 wwin
->flags
.urgent
= 0;
511 wwin
->group_id
= None
;
515 case XA_WM_NORMAL_HINTS
:
516 /* normal (geometry) hints */
521 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
522 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
,
524 /* TODO: should we check for consistency of the current
525 * size against the new geometry hints? */
529 case XA_WM_TRANSIENT_FOR
:
534 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
537 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
538 new_owner
= wwin
->screen_ptr
->root_win
;
541 if (new_owner
!=wwin
->transient_for
) {
542 owner
= wWindowFor(wwin
->transient_for
);
544 if (owner
->flags
.semi_focused
) {
545 owner
->flags
.semi_focused
= 0;
546 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
548 wFrameWindowPaint(owner
->frame
);
551 owner
= wWindowFor(new_owner
);
553 if (!owner
->flags
.semi_focused
) {
554 owner
->flags
.semi_focused
= 1;
555 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
557 wFrameWindowPaint(owner
->frame
);
560 wwin
->transient_for
= new_owner
;
561 if (new_owner
==None
) {
562 if (WFLAGP(wwin
, no_miniaturizable
)) {
563 WSETUFLAG(wwin
, no_miniaturizable
, 0);
564 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
566 wWindowConfigureBorders(wwin
);
568 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
569 WSETUFLAG(wwin
, no_miniaturizable
, 1);
570 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
572 wWindowConfigureBorders(wwin
);
579 if (event
->atom
==_XA_WM_PROTOCOLS
) {
581 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
583 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
586 wWindowUpdateButtonImages(wwin
);
588 } else if (event
->atom
==_XA_WM_COLORMAP_WINDOWS
) {
590 GetColormapWindows(wwin
);
591 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
593 } else if (event
->atom
==_XA_WINDOWMAKER_MENU
) {
596 wapp
= wApplicationOf(wwin
->main_window
);
600 /* TODO: remake appmenu update */
601 wAppMenuDestroy(wapp
->menu
);
603 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
604 /* make the appmenu be mapped */
605 wSetFocusTo(wwin
->screen_ptr
, NULL
);
606 wSetFocusTo(wwin
->screen_ptr
,
607 wwin
->screen_ptr
->focused_window
);
609 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
610 GNUstepWMAttributes
*attr
;
612 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
614 wWindowUpdateGNUstepAttr(wwin
, attr
);
621 done
= wKWMCheckClientHintChange(wwin
, event
);
622 #endif /* KWM_HINTS */
629 *----------------------------------------------------------------------
630 * wClientGetNormalHints--
631 * Get size (normal) hints and a default geometry for the client
632 * window. The hints are also checked for inconsistency. If geometry is
633 * True, the returned data will account for client specified initial
637 * normal_hints is filled with valid data.
638 *----------------------------------------------------------------------
641 wClientGetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, Bool geometry
,
642 int *x
, int *y
, unsigned *width
, unsigned *height
)
646 /* find a position for the window */
647 if (!wwin
->normal_hints
)
648 wwin
->normal_hints
= XAllocSizeHints();
650 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
651 wwin
->normal_hints
->flags
= 0;
656 *width
= wattribs
->width
;
657 *height
= wattribs
->height
;
659 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
660 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
662 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
663 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
664 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
666 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
667 wwin
->normal_hints
->base_width
= 0;
668 wwin
->normal_hints
->base_height
= 0;
670 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
671 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
672 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
675 /* some buggy apps set weird hints.. */
676 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
677 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
679 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
680 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
682 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
683 wwin
->normal_hints
->width_inc
= 1;
684 wwin
->normal_hints
->height_inc
= 1;
686 if (wwin
->normal_hints
->width_inc
<= 0)
687 wwin
->normal_hints
->width_inc
= 1;
688 if (wwin
->normal_hints
->height_inc
<= 0)
689 wwin
->normal_hints
->height_inc
= 1;
692 if (wwin
->normal_hints
->flags
& PAspect
) {
693 if (wwin
->normal_hints
->min_aspect
.x
< 1)
694 wwin
->normal_hints
->min_aspect
.x
= 1;
695 if (wwin
->normal_hints
->min_aspect
.y
< 1)
696 wwin
->normal_hints
->min_aspect
.y
= 1;
698 if (wwin
->normal_hints
->max_aspect
.x
< 1)
699 wwin
->normal_hints
->max_aspect
.x
= 1;
700 if (wwin
->normal_hints
->max_aspect
.y
< 1)
701 wwin
->normal_hints
->max_aspect
.y
= 1;
704 if (wwin
->normal_hints
->min_width
<= 0)
705 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
707 if (wwin
->normal_hints
->min_height
<= 0)
708 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
710 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
711 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
713 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
714 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
717 /* pre ICCCM (old) client */
718 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
720 printf("PRE ICCCM\n");
722 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
723 *x
= wwin
->normal_hints
->x
;
724 *y
= wwin
->normal_hints
->y
;
726 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
727 *width
= wwin
->normal_hints
->width
;
728 *height
= wwin
->normal_hints
->height
;
735 GetColormapWindows(WWindow
*wwin
)
737 if (wwin
->cmap_windows
) {
738 XFree(wwin
->cmap_windows
);
741 wwin
->cmap_windows
= NULL
;
742 wwin
->cmap_window_no
= 0;
744 if (XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
745 &(wwin
->cmap_window_no
))!=Success
746 || !wwin
->cmap_windows
) {
747 wwin
->cmap_window_no
= 0;
748 wwin
->cmap_windows
= NULL
;