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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <X11/Xutil.h>
24 #include <X11/Xatom.h>
26 #include <X11/extensions/shape.h>
33 #include "WindowMaker.h"
36 #include "properties.h"
49 *--------------------------------------------------------------------
51 * Reparent the window back to the root window.
53 *--------------------------------------------------------------------
55 void wClientRestore(WWindow
* wwin
)
60 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
61 /* set the position of the frame on screen */
62 wwin
->frame_x
-= gx
* wwin
->screen_ptr
->frame_border_width
;
63 wwin
->frame_y
-= gy
* wwin
->screen_ptr
->frame_border_width
;
64 /* if gravity is to the south, account for the border sizes */
66 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
68 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
69 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
, wwin
->frame_x
, wwin
->frame_y
);
71 /* don't let the window get iconified after restart */
73 if (wwin->flags.shaded)
74 wClientSetState(wwin, NormalState, None);
79 *----------------------------------------------------------------------
81 * Set the state of the client window to one of the window
82 * states defined in ICCCM (Iconic, Withdrawn, Normal)
85 * The WM_STATE property of the window is updated as well as the
86 * WWindow.state variable.
87 *----------------------------------------------------------------------
89 void wClientSetState(WWindow
* wwin
, int state
, Window icon_win
)
95 data
[0] = (unsigned long)state
;
96 data
[1] = (unsigned long)icon_win
;
98 XChangeProperty(dpy
, wwin
->client_win
, w_global
.atom
.wm
.state
,
99 w_global
.atom
.wm
.state
, 32, PropModeReplace
,
100 (unsigned char *)data
, 2);
103 void wClientGetGravityOffsets(WWindow
* wwin
, int *ofs_x
, int *ofs_y
)
105 switch (wwin
->normal_hints
->win_gravity
) {
112 case NorthWestGravity
:
120 case NorthEastGravity
:
132 case SouthWestGravity
:
140 case SouthEastGravity
:
147 void wClientConfigure(WWindow
* wwin
, XConfigureRequestEvent
* xcre
)
150 int nx
, ny
, nwidth
, nheight
;
153 /* printf("configure event: %d %d %d %d\n", xcre->x, xcre->y, xcre->width, xcre->height); */
157 * configure a window that was not mapped by us
161 xwc
.width
= xcre
->width
;
162 xwc
.height
= xcre
->height
;
163 xwc
.border_width
= xcre
->border_width
;
164 xwc
.stack_mode
= xcre
->detail
;
165 xwc
.sibling
= xcre
->above
;
166 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
170 if (w_global
.xext
.shape
.supported
) {
175 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
176 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
177 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
, &ujunk
);
178 wwin
->flags
.shaped
= b_shaped
;
181 if (xcre
->value_mask
& CWStackMode
) {
182 WObjDescriptor
*desc
;
185 if ((xcre
->value_mask
& CWSibling
) &&
186 (XFindContext(dpy
, xcre
->above
, w_global
.context
.client_win
, (XPointer
*) & desc
) == XCSUCCESS
)
187 && (desc
->parent_type
== WCLASS_WINDOW
)) {
188 sibling
= desc
->parent
;
189 xwc
.sibling
= sibling
->frame
->core
->window
;
191 xwc
.sibling
= xcre
->above
;
193 xwc
.stack_mode
= xcre
->detail
;
194 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
195 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
196 /* fix stacking order */
197 RemakeStackList(wwin
->screen_ptr
);
200 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
202 if (xcre
->value_mask
& CWBorderWidth
) {
203 wwin
->old_border_width
= xcre
->border_width
;
206 if (!wwin
->flags
.shaded
) {
207 /* If the window is shaded, wrong height will be set for the window */
208 if (xcre
->value_mask
& CWX
) {
210 /* Subtracting the border makes the window shift by 1 pixel -Dan */
211 /*if (HAS_BORDER(wwin)) {
212 nx -= wwin->screen_ptr->frame_border_width;
218 if (xcre
->value_mask
& CWY
) {
219 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
220 /* Subtracting the border makes the window shift by 1 pixel -Dan */
221 /*if (HAS_BORDER(wwin)) {
222 ny -= wwin->screen_ptr->frame_border_width;
228 if (xcre
->value_mask
& CWWidth
)
229 nwidth
= xcre
->width
;
231 nwidth
= wwin
->frame
->core
->width
;
233 if (xcre
->value_mask
& CWHeight
)
234 nheight
= xcre
->height
;
236 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
238 /* Don't overwrite the saved geometry unnecessarily. */
239 if (!(xcre
->value_mask
& (CWX
| CWY
| CWWidth
| CWHeight
)))
242 if (nwidth
!= wwin
->old_geometry
.width
)
243 wwin
->flags
.maximized
&= ~(MAX_HORIZONTAL
| MAX_TOPHALF
| MAX_BOTTOMHALF
| MAX_MAXIMUS
);
244 if (nheight
!= wwin
->old_geometry
.height
)
245 wwin
->flags
.maximized
&= ~(MAX_VERTICAL
| MAX_LEFTHALF
| MAX_RIGHTHALF
| MAX_MAXIMUS
);
247 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
248 wwin
->old_geometry
.x
= nx
;
249 wwin
->old_geometry
.y
= ny
;
250 wwin
->old_geometry
.width
= nwidth
;
251 wwin
->old_geometry
.height
= nheight
;
255 void wClientSendProtocol(WWindow
* wwin
, Atom protocol
, Time time
)
259 event
.xclient
.type
= ClientMessage
;
260 event
.xclient
.message_type
= w_global
.atom
.wm
.protocols
;
261 event
.xclient
.format
= 32;
262 event
.xclient
.display
= dpy
;
263 event
.xclient
.window
= wwin
->client_win
;
264 event
.xclient
.data
.l
[0] = protocol
;
265 event
.xclient
.data
.l
[1] = time
;
266 event
.xclient
.data
.l
[2] = 0;
267 event
.xclient
.data
.l
[3] = 0;
268 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
272 void wClientKill(WWindow
* wwin
)
274 XKillClient(dpy
, wwin
->client_win
);
280 *----------------------------------------------------------------------
281 * wClientCheckProperty--
282 * Handles PropertyNotify'es, verifying which property was
283 * changed and updating internal state according to that, like redrawing
284 * the icon title when it is changed.
287 * Depends on the changed property.
289 * TODO: _GNUSTEP_WM_ATTR
290 *----------------------------------------------------------------------
292 void wClientCheckProperty(WWindow
* wwin
, XPropertyEvent
* event
)
294 XWindowAttributes attribs
;
299 switch (event
->atom
) {
301 if (!wwin
->flags
.net_has_title
) {
302 /* window title was changed */
303 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
304 wWindowUpdateName(wwin
, NULL
);
306 wWindowUpdateName(wwin
, tmp
);
313 case XA_WM_ICON_NAME
:
314 /* Title has changed, update the icon title */
316 wIconChangeTitle(wwin
->icon
, wwin
);
317 wIconPaint(wwin
->icon
);
322 if (wwin
->main_window
!= None
) {
323 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
326 if (!wapp
|| !wapp
->app_icon
|| wapp
->app_icon
->docked
)
329 command
= GetCommandForWindow(wwin
->main_window
);
331 if (wapp
->app_icon
->command
)
332 wfree(wapp
->app_icon
->command
);
333 wapp
->app_icon
->command
= command
;
341 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
343 /* group leader update
345 * This means that the window is setting the leader after
346 * it was mapped, changing leaders or removing the leader.
348 * Valid state transitions are:
360 * Where G is the window_group hint, C is CLIENT_LEADER property
361 * and ' indicates the hint is unset.
363 * 1,2 - change group leader to new value of window_group
364 * 3 - change leader to value of CLIENT_LEADER
365 * 4 - change leader to value of window_group
366 * 5 - destroy application
367 * 6 - create application
369 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
370 && new_hints
->window_group
!= None
) {
375 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
376 && wwin
->wm_hints
->window_group
!= None
) {
382 if (wwin
->client_leader
) {
383 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
385 } else if (g1
&& !g2
) {
387 } else if (!g1
&& g2
) {
393 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
395 } else if (g1
&& !g2
) {
397 } else if (!g1
&& g2
) {
404 /* Handling this may require more work. -Dan */
405 if (wwin
->fake_group
!= NULL
) {
410 XFree(wwin
->wm_hints
);
412 wwin
->wm_hints
= new_hints
;
414 /* do action according to state transition */
416 /* 3 - change leader to value of CLIENT_LEADER */
418 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
419 wwin
->main_window
= wwin
->client_leader
;
420 wwin
->group_id
= None
;
421 wApplicationCreate(wwin
);
424 /* 1,2,4 - change leader to new value of window_group */
428 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
429 wwin
->main_window
= new_hints
->window_group
;
430 wwin
->group_id
= wwin
->main_window
;
431 wApplicationCreate(wwin
);
434 /* 5 - destroy application */
436 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
437 wwin
->main_window
= None
;
438 wwin
->group_id
= None
;
441 /* 6 - create application */
443 wwin
->main_window
= new_hints
->window_group
;
444 wwin
->group_id
= wwin
->main_window
;
445 wApplicationCreate(wwin
);
447 /* 7 - we have a fake window group id, so just ignore anything else */
452 if (wwin
->wm_hints
) {
454 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
455 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
458 if (wwin
->flags
.miniaturized
&& wwin
->icon
)
459 wIconUpdate(wwin
->icon
);
461 wapp
= wApplicationOf(wwin
->main_window
);
462 if (wapp
&& wapp
->app_icon
) {
463 wIconUpdate(wapp
->app_icon
->icon
);
464 wAppIconPaint(wapp
->app_icon
);
468 if (wwin
->wm_hints
->flags
& UrgencyHint
)
469 wwin
->flags
.urgent
= 1;
471 wwin
->flags
.urgent
= 0;
472 wAppBounceWhileUrgent(wApplicationOf(wwin
->main_window
));
473 /*} else if (wwin->fake_group!=NULL) {
474 wwin->group_id = wwin->fake_group->leader; */
476 wwin
->group_id
= None
;
480 case XA_WM_NORMAL_HINTS
:
481 /* normal (geometry) hints */
486 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
487 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
, &bar
, &bar
);
488 /* TODO: should we check for consistency of the current
489 * size against the new geometry hints? */
493 case XA_WM_TRANSIENT_FOR
:
498 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
501 if (new_owner
== 0 || new_owner
== wwin
->client_win
) {
502 new_owner
= wwin
->screen_ptr
->root_win
;
505 if (new_owner
!= wwin
->transient_for
) {
506 owner
= wWindowFor(wwin
->transient_for
);
508 if (owner
->flags
.semi_focused
) {
509 owner
->flags
.semi_focused
= 0;
510 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
512 wFrameWindowPaint(owner
->frame
);
515 owner
= wWindowFor(new_owner
);
517 if (!owner
->flags
.semi_focused
) {
518 owner
->flags
.semi_focused
= 1;
519 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
521 wFrameWindowPaint(owner
->frame
);
524 wwin
->transient_for
= new_owner
;
525 if (new_owner
== None
) {
526 if (WFLAGP(wwin
, no_miniaturizable
)) {
527 WSETUFLAG(wwin
, no_miniaturizable
, 0);
528 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
530 wWindowConfigureBorders(wwin
);
532 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
533 WSETUFLAG(wwin
, no_miniaturizable
, 1);
534 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
536 wWindowConfigureBorders(wwin
);
543 if (event
->atom
== w_global
.atom
.wm
.protocols
) {
545 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
547 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
550 wWindowUpdateButtonImages(wwin
);
552 } else if (event
->atom
== w_global
.atom
.wm
.colormap_windows
) {
554 GetColormapWindows(wwin
);
555 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
557 } else if (event
->atom
== w_global
.atom
.wmaker
.menu
) {
560 wapp
= wApplicationOf(wwin
->main_window
);
564 /* TODO: remake appmenu update */
565 wAppMenuDestroy(wapp
->menu
);
567 if (wwin
->fake_group
) {
568 WScreen
*scr
= wwin
->screen_ptr
;
569 WWindow
*foo
= scr
->focused_window
;
570 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
572 wApplicationDestroy(wapp
);
574 if (foo
->fake_group
&& foo
->fake_group
== fPtr
) {
575 WSETUFLAG(foo
, shared_appicon
, 0);
576 foo
->fake_group
= NULL
;
577 if (foo
->group_id
!= None
)
578 foo
->main_window
= foo
->group_id
;
579 else if (foo
->client_leader
!= None
)
580 foo
->main_window
= foo
->client_leader
;
581 else if (WFLAGP(foo
, emulate_appicon
))
582 foo
->main_window
= foo
->client_win
;
584 foo
->main_window
= None
;
585 if (foo
->main_window
) {
586 wapp
= wApplicationCreate(foo
);
592 if (fPtr
->leader
!= None
)
593 XDestroyWindow(dpy
, fPtr
->leader
);
594 fPtr
->retainCount
= 0;
596 fPtr
->origLeader
= None
;
598 wapp
= wApplicationOf(wwin
->main_window
);
600 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
602 if (wPreferences
.auto_arrange_icons
) {
603 wArrangeIcons(wwin
->screen_ptr
, True
);
606 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
608 /* make the appmenu be mapped */
609 wSetFocusTo(wwin
->screen_ptr
, NULL
);
610 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
612 } else if (event
->atom
== w_global
.atom
.gnustep
.wm_attr
) {
613 GNUstepWMAttributes
*attr
;
615 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
617 wWindowUpdateGNUstepAttr(wwin
, attr
);
621 wNETWMCheckClientHintChange(wwin
, event
);
627 *----------------------------------------------------------------------
628 * wClientGetNormalHints--
629 * Get size (normal) hints and a default geometry for the client
630 * window. The hints are also checked for inconsistency. If geometry is
631 * True, the returned data will account for client specified initial
635 * normal_hints is filled with valid data.
636 *----------------------------------------------------------------------
639 wClientGetNormalHints(WWindow
* wwin
, XWindowAttributes
* wattribs
, Bool geometry
,
640 int *x
, int *y
, unsigned *width
, unsigned *height
)
642 int pre_icccm
= 0; /* not used */
644 /* find a position for the window */
645 if (!wwin
->normal_hints
)
646 wwin
->normal_hints
= XAllocSizeHints();
648 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
649 wwin
->normal_hints
->flags
= 0;
654 *width
= wattribs
->width
;
655 *height
= wattribs
->height
;
657 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
658 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
660 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
661 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
662 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
664 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
665 wwin
->normal_hints
->base_width
= 0;
666 wwin
->normal_hints
->base_height
= 0;
668 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
669 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
* 2;
670 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
* 2;
673 /* some buggy apps set weird hints.. */
674 if (wwin
->normal_hints
->min_width
<= 0)
675 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
677 if (wwin
->normal_hints
->min_height
<= 0)
678 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
680 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
681 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
683 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
684 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
686 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
687 wwin
->normal_hints
->width_inc
= 1;
688 wwin
->normal_hints
->height_inc
= 1;
690 if (wwin
->normal_hints
->width_inc
<= 0)
691 wwin
->normal_hints
->width_inc
= 1;
692 if (wwin
->normal_hints
->height_inc
<= 0)
693 wwin
->normal_hints
->height_inc
= 1;
696 if (wwin
->normal_hints
->flags
& PAspect
) {
697 if (wwin
->normal_hints
->min_aspect
.x
< 1)
698 wwin
->normal_hints
->min_aspect
.x
= 1;
699 if (wwin
->normal_hints
->min_aspect
.y
< 1)
700 wwin
->normal_hints
->min_aspect
.y
= 1;
702 if (wwin
->normal_hints
->max_aspect
.x
< 1)
703 wwin
->normal_hints
->max_aspect
.x
= 1;
704 if (wwin
->normal_hints
->max_aspect
.y
< 1)
705 wwin
->normal_hints
->max_aspect
.y
= 1;
708 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
709 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
711 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
712 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
714 #ifdef IGNORE_PPOSITION
715 wwin
->normal_hints
->flags
&= ~PPosition
;
718 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
719 if (wwin
->normal_hints
->flags
& (USPosition
| PPosition
)) {
720 *x
= wwin
->normal_hints
->x
;
721 *y
= wwin
->normal_hints
->y
;
723 if (wwin
->normal_hints
->flags
& (USSize
| PSize
)) {
724 *width
= wwin
->normal_hints
->width
;
725 *height
= wwin
->normal_hints
->height
;
730 void GetColormapWindows(WWindow
* wwin
)
733 if (wwin
->cmap_windows
) {
734 XFree(wwin
->cmap_windows
);
737 wwin
->cmap_windows
= NULL
;
738 wwin
->cmap_window_no
= 0;
740 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
), &(wwin
->cmap_window_no
))
741 || !wwin
->cmap_windows
) {
742 wwin
->cmap_window_no
= 0;
743 wwin
->cmap_windows
= NULL
;