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"
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
;
67 *--------------------------------------------------------------------
69 * Reparent the window back to the root window.
71 *--------------------------------------------------------------------
74 wClientRestore(WWindow
*wwin
)
79 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
80 /* set the positio of the frame on screen */
81 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
82 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
83 /* if gravity is to the south, account for the border sizes */
85 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
87 XUnmapWindow(dpy
, wwin
->client_win
);
88 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
89 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
90 wwin
->frame_x
, wwin
->frame_y
);
91 if (wwin
->flags
.mapped
)
92 XMapWindow(dpy
, wwin
->client_win
);
94 /* don't let the window get iconified after restart */
95 if (wwin
->flags
.shaded
)
96 wClientSetState(wwin
, NormalState
, None
);
101 *----------------------------------------------------------------------
103 * Set the state of the client window to one of the window
104 * states defined in ICCCM (Iconic, Withdrawn, Normal)
107 * The WM_STATE property of the window is updated as well as the
108 * WWindow.state variable.
109 *----------------------------------------------------------------------
112 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
118 data
[0] = (unsigned long) state
;
119 data
[1] = (unsigned long) icon_win
;
121 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
122 PropModeReplace
, (unsigned char *) data
, 2);
127 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
129 switch (wwin
->normal_hints
->win_gravity
) {
136 case NorthWestGravity
:
144 case NorthEastGravity
:
156 case SouthWestGravity
:
164 case SouthEastGravity
:
174 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
177 int nx
, ny
, nwidth
, nheight
;
182 * configure a window that was not mapped by us
186 xwc
.width
= xcre
->width
;
187 xwc
.height
= xcre
->height
;
188 xwc
.border_width
= xcre
->border_width
;
189 xwc
.stack_mode
= xcre
->detail
;
190 xwc
.sibling
= xcre
->above
;
191 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
195 if (wShapeSupported
) {
200 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
201 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
202 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
204 wwin
->flags
.shaped
= b_shaped
;
207 if (xcre
->value_mask
& CWStackMode
) {
208 WObjDescriptor
*desc
;
211 if ((xcre
->value_mask
& CWSibling
) &&
212 (XFindContext(dpy
, xcre
->above
, wWinContext
,
213 (XPointer
*)&desc
) == XCSUCCESS
)
214 && (desc
->parent_type
==WCLASS_WINDOW
)) {
215 sibling
=desc
->parent
;
216 xwc
.sibling
= sibling
->frame
->core
->window
;
218 xwc
.sibling
= xcre
->above
;
220 xwc
.stack_mode
= xcre
->detail
;
221 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
222 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
223 /* fix stacking order */
224 RemakeStackList(wwin
->screen_ptr
);
227 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
229 if (xcre
->value_mask
& CWBorderWidth
) {
230 wwin
->old_border_width
= xcre
->border_width
;
233 if (!wwin
->flags
.shaded
) {
234 /* If the window is shaded, wrong height will be set for the window */
235 if (xcre
->value_mask
& CWX
)
236 nx
= xcre
->x
- FRAME_BORDER_WIDTH
;
240 if (xcre
->value_mask
& CWY
)
241 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
)
242 - FRAME_BORDER_WIDTH
;
246 if (xcre
->value_mask
& CWWidth
)
247 nwidth
= xcre
->width
;
249 nwidth
= wwin
->frame
->core
->width
;
251 if (xcre
->value_mask
& CWHeight
)
252 nheight
= xcre
->height
;
254 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
256 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
257 wwin
->old_geometry
.x
= nx
;
258 wwin
->old_geometry
.y
= ny
;
259 wwin
->old_geometry
.width
= nwidth
;
260 wwin
->old_geometry
.height
= nheight
;
266 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
270 event
.xclient
.type
= ClientMessage
;
271 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
272 event
.xclient
.format
= 32;
273 event
.xclient
.display
= dpy
;
274 event
.xclient
.window
= wwin
->client_win
;
275 event
.xclient
.data
.l
[0] = protocol
;
276 event
.xclient
.data
.l
[1] = time
;
277 event
.xclient
.data
.l
[2] = 0;
278 event
.xclient
.data
.l
[3] = 0;
279 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
286 wClientKill(WWindow
*wwin
)
288 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: CLIENT_LEADER, _GNUSTEP_WM_ATTR
303 *----------------------------------------------------------------------
306 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
308 XWindowAttributes attribs
;
313 switch (event
->atom
) {
315 /* window title was changed */
317 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
318 /* the hint was removed */
319 tmp
= wstrdup(DEF_WINDOW_TITLE
);
321 if (wFrameWindowChangeTitle(wwin
->frame
, tmp
)) {
322 /* only update the menu if the title has actually changed */
323 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
330 case XA_WM_ICON_NAME
:
336 /* icon title was changed */
337 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
338 wIconChangeTitle(wwin
->icon
, new_title
);
343 if (wwin
->main_window
!=None
) {
344 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
348 if (wapp
&& wapp
->app_icon
) {
349 if (wapp
->app_icon
->command
!=NULL
)
350 free(wapp
->app_icon
->command
);
352 if (XGetCommand(dpy
, wwin
->main_window
, &argv
, &argc
)) {
353 if (argc
> 0 && argv
!= NULL
)
354 wapp
->app_icon
->command
= FlattenStringList(argv
,argc
);
356 XFreeStringList(argv
);
366 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
368 /* group leader update
370 * This means that the window is setting the leader after
371 * it was mapped, changing leaders or removing the leader.
373 * Valid state transitions are:
385 * Where G is the window_group hint, C is CLIENT_LEADER property
386 * and ' indicates the hint is unset.
388 * 1,2 - change group leader to new value of window_group
389 * 3 - change leader to value of CLIENT_LEADER
390 * 4 - change leader to value of window_group
391 * 5 - destroy application
392 * 6 - create application
394 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
395 && new_hints
->window_group
!=None
) {
400 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
401 && wwin
->wm_hints
->window_group
!=None
) {
407 if (wwin
->client_leader
) {
409 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
411 } else if (g1
&& !g2
) {
413 } else if (!g1
&& g2
) {
420 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
422 } else if (g1
&& !g2
) {
424 } else if (!g1
&& g2
) {
432 XFree(wwin
->wm_hints
);
434 wwin
->wm_hints
= new_hints
;
436 /* do action according to state transition */
438 /* 3 - change leader to value of CLIENT_LEADER */
440 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
441 wwin
->main_window
= wwin
->client_leader
;
442 wwin
->group_id
= None
;
443 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
446 /* 1,2,4 - change leader to new value of window_group */
450 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
451 wwin
->main_window
= new_hints
->window_group
;
452 wwin
->group_id
= wwin
->main_window
;
453 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
456 /* 5 - destroy application */
458 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
459 wwin
->main_window
= None
;
460 wwin
->group_id
= None
;
463 /* 6 - create application */
465 wwin
->main_window
= new_hints
->window_group
;
466 wwin
->group_id
= wwin
->main_window
;
467 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
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
) {
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;
496 wwin
->group_id
= None
;
500 case XA_WM_NORMAL_HINTS
:
501 /* normal (geometry) hints */
506 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
507 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
,
509 /* TODO: should we check for consistency of the current
510 * size against the new geometry hints? */
514 case XA_WM_TRANSIENT_FOR
:
519 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
522 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
523 new_owner
= wwin
->screen_ptr
->root_win
;
526 if (new_owner
!=wwin
->transient_for
) {
527 owner
= wWindowFor(wwin
->transient_for
);
529 if (owner
->flags
.semi_focused
) {
530 owner
->flags
.semi_focused
= 0;
531 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
533 wFrameWindowPaint(owner
->frame
);
536 owner
= wWindowFor(new_owner
);
538 if (!owner
->flags
.semi_focused
) {
539 owner
->flags
.semi_focused
= 1;
540 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
542 wFrameWindowPaint(owner
->frame
);
545 wwin
->transient_for
= new_owner
;
546 if (new_owner
==None
) {
547 if (wwin
->window_flags
.no_miniaturizable
) {
548 wwin
->window_flags
.no_miniaturizable
= 0;
549 wwin
->window_flags
.no_miniaturize_button
= 0;
551 wWindowConfigureBorders(wwin
);
553 } else if (!wwin
->window_flags
.no_miniaturizable
) {
554 wwin
->window_flags
.no_miniaturizable
= 1;
555 wwin
->window_flags
.no_miniaturize_button
= 1;
557 wWindowConfigureBorders(wwin
);
564 if (event
->atom
==_XA_WM_PROTOCOLS
) {
565 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
566 if (wwin
->protocols
.DELETE_WINDOW
) {
567 wwin
->window_flags
.kill_close
= 0;
569 wwin
->window_flags
.kill_close
= 1;
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 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
590 /* make the appmenu be mapped */
591 wSetFocusTo(wwin
->screen_ptr
, NULL
);
592 wSetFocusTo(wwin
->screen_ptr
,
593 wwin
->screen_ptr
->focused_window
);
595 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
596 GNUstepWMAttributes
*attr
;
598 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
600 wWindowUpdateGNUstepAttr(wwin
, attr
);
609 *----------------------------------------------------------------------
610 * wClientGetNormalHints--
611 * Get size (normal) hints and a default geometry for the client
612 * window. The hints are also checked for inconsistency. If geometry is
613 * True, the returned data will account for client specified initial
617 * normal_hints is filled with valid data.
618 *----------------------------------------------------------------------
621 wClientGetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, Bool geometry
,
622 int *x
, int *y
, unsigned *width
, unsigned *height
)
626 /* find a position for the window */
627 if (!wwin
->normal_hints
)
628 wwin
->normal_hints
= XAllocSizeHints();
630 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
631 wwin
->normal_hints
->flags
= 0;
636 *width
= wattribs
->width
;
637 *height
= wattribs
->height
;
639 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
640 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
642 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
643 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
644 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
646 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
647 wwin
->normal_hints
->base_width
= 0;
648 wwin
->normal_hints
->base_height
= 0;
650 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
651 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
652 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
655 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
656 wwin
->normal_hints
->width_inc
= 1;
657 wwin
->normal_hints
->height_inc
= 1;
659 if (wwin
->normal_hints
->width_inc
<= 0)
660 wwin
->normal_hints
->width_inc
= 1;
661 if (wwin
->normal_hints
->height_inc
<= 0)
662 wwin
->normal_hints
->height_inc
= 1;
665 if (wwin
->normal_hints
->flags
& PAspect
) {
666 if (wwin
->normal_hints
->min_aspect
.x
< 1)
667 wwin
->normal_hints
->min_aspect
.x
= 1;
668 if (wwin
->normal_hints
->min_aspect
.y
< 1)
669 wwin
->normal_hints
->min_aspect
.y
= 1;
671 if (wwin
->normal_hints
->max_aspect
.x
< 1)
672 wwin
->normal_hints
->max_aspect
.x
= 1;
673 if (wwin
->normal_hints
->max_aspect
.y
< 1)
674 wwin
->normal_hints
->max_aspect
.y
= 1;
677 if (wwin
->normal_hints
->min_width
<= 0)
678 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
680 if (wwin
->normal_hints
->min_height
<= 0)
681 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
683 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
684 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
686 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
687 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
690 /* pre ICCCM (old) client */
691 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
693 printf("PRE ICCCM\n");
695 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
696 *x
= wwin
->normal_hints
->x
;
697 *y
= wwin
->normal_hints
->y
;
699 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
700 *width
= wwin
->normal_hints
->width
;
701 *height
= wwin
->normal_hints
->height
;
708 GetColormapWindows(WWindow
*wwin
)
710 if (wwin
->cmap_windows
) {
711 XFree(wwin
->cmap_windows
);
714 wwin
->cmap_windows
= NULL
;
715 wwin
->cmap_window_no
= 0;
717 if (XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
718 &(wwin
->cmap_window_no
))!=Success
) {
719 wwin
->cmap_window_no
= 0;
720 wwin
->cmap_windows
= NULL
;