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
)
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 XUnmapWindow(dpy
, wwin
->client_win
);
87 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
88 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
89 wwin
->frame_x
, wwin
->frame_y
);
90 if (wwin
->flags
.mapped
)
91 XMapWindow(dpy
, wwin
->client_win
);
93 /* don't let the window get iconified after restart */
94 if (wwin
->flags
.shaded
)
95 wClientSetState(wwin
, NormalState
, None
);
100 *----------------------------------------------------------------------
102 * Set the state of the client window to one of the window
103 * states defined in ICCCM (Iconic, Withdrawn, Normal)
106 * The WM_STATE property of the window is updated as well as the
107 * WWindow.state variable.
108 *----------------------------------------------------------------------
111 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
117 data
[0] = (unsigned long) state
;
118 data
[1] = (unsigned long) icon_win
;
120 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
121 PropModeReplace
, (unsigned char *) data
, 2);
126 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
128 switch (wwin
->normal_hints
->win_gravity
) {
135 case NorthWestGravity
:
143 case NorthEastGravity
:
155 case SouthWestGravity
:
163 case SouthEastGravity
:
173 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
176 int nx
, ny
, nwidth
, nheight
;
181 * configure a window that was not mapped by us
185 xwc
.width
= xcre
->width
;
186 xwc
.height
= xcre
->height
;
187 xwc
.border_width
= xcre
->border_width
;
188 xwc
.stack_mode
= xcre
->detail
;
189 xwc
.sibling
= xcre
->above
;
190 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
194 if (wShapeSupported
) {
199 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
200 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
201 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
203 wwin
->flags
.shaped
= b_shaped
;
206 if (xcre
->value_mask
& CWStackMode
) {
207 WObjDescriptor
*desc
;
210 if ((xcre
->value_mask
& CWSibling
) &&
211 (XFindContext(dpy
, xcre
->above
, wWinContext
,
212 (XPointer
*)&desc
) == XCSUCCESS
)
213 && (desc
->parent_type
==WCLASS_WINDOW
)) {
214 sibling
=desc
->parent
;
215 xwc
.sibling
= sibling
->frame
->core
->window
;
217 xwc
.sibling
= xcre
->above
;
219 xwc
.stack_mode
= xcre
->detail
;
220 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
221 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
222 /* fix stacking order */
223 RemakeStackList(wwin
->screen_ptr
);
226 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
228 if (xcre
->value_mask
& CWBorderWidth
) {
229 wwin
->old_border_width
= xcre
->border_width
;
232 if (!wwin
->flags
.shaded
) {
233 /* If the window is shaded, wrong height will be set for the window */
234 if (xcre
->value_mask
& CWX
)
235 nx
= xcre
->x
- FRAME_BORDER_WIDTH
;
239 if (xcre
->value_mask
& CWY
)
240 ny
= xcre
->y
- FRAME_BORDER_WIDTH
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_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 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
255 wwin
->old_geometry
.x
= nx
;
256 wwin
->old_geometry
.y
= ny
;
257 wwin
->old_geometry
.width
= nwidth
;
258 wwin
->old_geometry
.height
= nheight
;
264 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
268 event
.xclient
.type
= ClientMessage
;
269 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
270 event
.xclient
.format
= 32;
271 event
.xclient
.display
= dpy
;
272 event
.xclient
.window
= wwin
->client_win
;
273 event
.xclient
.data
.l
[0] = protocol
;
274 event
.xclient
.data
.l
[1] = time
;
275 event
.xclient
.data
.l
[2] = 0;
276 event
.xclient
.data
.l
[3] = 0;
277 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
284 wClientKill(WWindow
*wwin
)
286 XKillClient(dpy
, wwin
->client_win
);
291 *----------------------------------------------------------------------
292 * wClientCheckProperty--
293 * Handles PropertyNotify'es, verifying which property was
294 * changed and updating internal state according to that, like redrawing
295 * the icon title when it is changed.
298 * Depends on the changed property.
300 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
301 *----------------------------------------------------------------------
304 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
306 XWindowAttributes attribs
;
311 switch (event
->atom
) {
313 /* window title was changed */
315 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
316 /* the hint was removed */
317 tmp
= wstrdup(DEF_WINDOW_TITLE
);
319 if (wFrameWindowChangeTitle(wwin
->frame
, tmp
)) {
320 /* only update the menu if the title has actually changed */
321 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
328 case XA_WM_ICON_NAME
:
334 /* icon title was changed */
335 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
336 wIconChangeTitle(wwin
->icon
, new_title
);
341 if (wwin
->main_window
!=None
) {
342 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
346 if (wapp
&& wapp
->app_icon
) {
347 if (wapp
->app_icon
->command
!=NULL
)
348 free(wapp
->app_icon
->command
);
350 if (XGetCommand(dpy
, wwin
->main_window
, &argv
, &argc
)) {
351 if (argc
> 0 && argv
!= NULL
)
352 wapp
->app_icon
->command
= FlattenStringList(argv
,argc
);
354 XFreeStringList(argv
);
364 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
366 /* group leader update
368 * This means that the window is setting the leader after
369 * it was mapped, changing leaders or removing the leader.
371 * Valid state transitions are:
383 * Where G is the window_group hint, C is CLIENT_LEADER property
384 * and ' indicates the hint is unset.
386 * 1,2 - change group leader to new value of window_group
387 * 3 - change leader to value of CLIENT_LEADER
388 * 4 - change leader to value of window_group
389 * 5 - destroy application
390 * 6 - create application
392 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
393 && new_hints
->window_group
!=None
) {
398 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
399 && wwin
->wm_hints
->window_group
!=None
) {
405 if (wwin
->client_leader
) {
407 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
409 } else if (g1
&& !g2
) {
411 } else if (!g1
&& g2
) {
418 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
420 } else if (g1
&& !g2
) {
422 } else if (!g1
&& g2
) {
430 XFree(wwin
->wm_hints
);
432 wwin
->wm_hints
= new_hints
;
434 /* do action according to state transition */
436 /* 3 - change leader to value of CLIENT_LEADER */
438 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
439 wwin
->main_window
= wwin
->client_leader
;
440 wwin
->group_id
= None
;
441 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
444 /* 1,2,4 - change leader to new value of window_group */
448 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
449 wwin
->main_window
= new_hints
->window_group
;
450 wwin
->group_id
= wwin
->main_window
;
451 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
454 /* 5 - destroy application */
456 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
457 wwin
->main_window
= None
;
458 wwin
->group_id
= None
;
461 /* 6 - create application */
463 wwin
->main_window
= new_hints
->window_group
;
464 wwin
->group_id
= wwin
->main_window
;
465 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
470 printf("window leader update caused state transition %i\n",i
);
474 if (wwin
->wm_hints
) {
476 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
477 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
480 if (wwin
->flags
.miniaturized
) {
481 wIconUpdate(wwin
->icon
);
483 wapp
= wApplicationOf(wwin
->main_window
);
484 if (wapp
&& wapp
->app_icon
) {
485 wIconUpdate(wapp
->app_icon
->icon
);
489 if (wwin
->wm_hints
->flags
& UrgencyHint
)
490 wwin
->flags
.urgent
= 1;
492 wwin
->flags
.urgent
= 0;
494 wwin
->group_id
= None
;
498 case XA_WM_NORMAL_HINTS
:
499 /* normal (geometry) hints */
504 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
505 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
,
507 /* TODO: should we check for consistency of the current
508 * size against the new geometry hints? */
512 case XA_WM_TRANSIENT_FOR
:
517 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
520 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
521 new_owner
= wwin
->screen_ptr
->root_win
;
524 if (new_owner
!=wwin
->transient_for
) {
525 owner
= wWindowFor(wwin
->transient_for
);
527 if (owner
->flags
.semi_focused
) {
528 owner
->flags
.semi_focused
= 0;
529 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
531 wFrameWindowPaint(owner
->frame
);
534 owner
= wWindowFor(new_owner
);
536 if (!owner
->flags
.semi_focused
) {
537 owner
->flags
.semi_focused
= 1;
538 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
540 wFrameWindowPaint(owner
->frame
);
543 wwin
->transient_for
= new_owner
;
544 if (new_owner
==None
) {
545 if (wwin
->window_flags
.no_miniaturizable
) {
546 wwin
->window_flags
.no_miniaturizable
= 0;
547 wwin
->window_flags
.no_miniaturize_button
= 0;
549 wWindowConfigureBorders(wwin
);
551 } else if (!wwin
->window_flags
.no_miniaturizable
) {
552 wwin
->window_flags
.no_miniaturizable
= 1;
553 wwin
->window_flags
.no_miniaturize_button
= 1;
555 wWindowConfigureBorders(wwin
);
562 if (event
->atom
==_XA_WM_PROTOCOLS
) {
563 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
564 if (wwin
->protocols
.DELETE_WINDOW
) {
565 wwin
->window_flags
.kill_close
= 0;
567 wwin
->window_flags
.kill_close
= 1;
570 wWindowUpdateButtonImages(wwin
);
572 } else if (event
->atom
==_XA_WM_COLORMAP_WINDOWS
) {
574 GetColormapWindows(wwin
);
575 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
577 } else if (event
->atom
==_XA_WINDOWMAKER_MENU
) {
580 wapp
= wApplicationOf(wwin
->main_window
);
584 /* TODO: remake appmenu update */
585 wAppMenuDestroy(wapp
->menu
);
587 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
588 /* make the appmenu be mapped */
589 wSetFocusTo(wwin
->screen_ptr
, NULL
);
590 wSetFocusTo(wwin
->screen_ptr
,
591 wwin
->screen_ptr
->focused_window
);
593 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
594 GNUstepWMAttributes
*attr
;
596 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
598 wWindowUpdateGNUstepAttr(wwin
, attr
);
607 *----------------------------------------------------------------------
608 * wClientGetNormalHints--
609 * Get size (normal) hints and a default geometry for the client
610 * window. The hints are also checked for inconsistency. If geometry is
611 * True, the returned data will account for client specified initial
615 * normal_hints is filled with valid data.
616 *----------------------------------------------------------------------
619 wClientGetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, Bool geometry
,
620 int *x
, int *y
, unsigned *width
, unsigned *height
)
624 /* find a position for the window */
625 if (!wwin
->normal_hints
)
626 wwin
->normal_hints
= XAllocSizeHints();
628 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
629 wwin
->normal_hints
->flags
= 0;
634 *width
= wattribs
->width
;
635 *height
= wattribs
->height
;
637 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
638 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
640 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
641 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
642 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
644 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
645 wwin
->normal_hints
->base_width
= 0;
646 wwin
->normal_hints
->base_height
= 0;
648 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
649 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
650 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
653 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
654 wwin
->normal_hints
->width_inc
= 1;
655 wwin
->normal_hints
->height_inc
= 1;
657 if (wwin
->normal_hints
->width_inc
<= 0)
658 wwin
->normal_hints
->width_inc
= 1;
659 if (wwin
->normal_hints
->height_inc
<= 0)
660 wwin
->normal_hints
->height_inc
= 1;
663 if (wwin
->normal_hints
->flags
& PAspect
) {
664 if (wwin
->normal_hints
->min_aspect
.x
< 1)
665 wwin
->normal_hints
->min_aspect
.x
= 1;
666 if (wwin
->normal_hints
->min_aspect
.y
< 1)
667 wwin
->normal_hints
->min_aspect
.y
= 1;
669 if (wwin
->normal_hints
->max_aspect
.x
< 1)
670 wwin
->normal_hints
->max_aspect
.x
= 1;
671 if (wwin
->normal_hints
->max_aspect
.y
< 1)
672 wwin
->normal_hints
->max_aspect
.y
= 1;
675 if (wwin
->normal_hints
->min_width
<= 0)
676 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
678 if (wwin
->normal_hints
->min_height
<= 0)
679 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
681 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
682 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
684 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
685 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
688 /* pre ICCCM (old) client */
689 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
691 printf("PRE ICCCM\n");
693 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
694 *x
= wwin
->normal_hints
->x
;
695 *y
= wwin
->normal_hints
->y
;
697 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
698 *width
= wwin
->normal_hints
->width
;
699 *height
= wwin
->normal_hints
->height
;
706 GetColormapWindows(WWindow
*wwin
)
708 if (wwin
->cmap_windows
) {
709 XFree(wwin
->cmap_windows
);
711 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
712 &(wwin
->cmap_window_no
))) {
713 wwin
->cmap_window_no
= 0;
714 wwin
->cmap_windows
= NULL
;