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"
46 /****** Global Variables ******/
49 extern XContext wWinContext
;
51 extern Atom _XA_WM_STATE
;
52 extern Atom _XA_WM_PROTOCOLS
;
53 extern Atom _XA_WM_COLORMAP_WINDOWS
;
55 extern Atom _XA_WINDOWMAKER_MENU
;
57 extern Atom _XA_GNUSTEP_WM_ATTR
;
58 extern Atom _XA_GNUSTEP_WM_RESIZEBAR
;
61 extern Bool wShapeSupported
;
65 *--------------------------------------------------------------------
67 * Reparent the window back to the root window.
69 *--------------------------------------------------------------------
71 void wClientRestore(WWindow
* wwin
)
76 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
77 /* set the position of the frame on screen */
78 wwin
->frame_x
-= gx
* FRAME_BORDER_WIDTH
;
79 wwin
->frame_y
-= gy
* FRAME_BORDER_WIDTH
;
80 /* if gravity is to the south, account for the border sizes */
82 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
84 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
85 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
, wwin
->frame_x
, wwin
->frame_y
);
87 /* don't let the window get iconified after restart */
89 if (wwin->flags.shaded)
90 wClientSetState(wwin, NormalState, None);
95 *----------------------------------------------------------------------
97 * Set the state of the client window to one of the window
98 * states defined in ICCCM (Iconic, Withdrawn, Normal)
101 * The WM_STATE property of the window is updated as well as the
102 * WWindow.state variable.
103 *----------------------------------------------------------------------
105 void wClientSetState(WWindow
* wwin
, int state
, Window icon_win
)
111 data
[0] = (unsigned long)state
;
112 data
[1] = (unsigned long)icon_win
;
114 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
115 PropModeReplace
, (unsigned char *)data
, 2);
118 void wClientGetGravityOffsets(WWindow
* wwin
, int *ofs_x
, int *ofs_y
)
120 switch (wwin
->normal_hints
->win_gravity
) {
127 case NorthWestGravity
:
135 case NorthEastGravity
:
147 case SouthWestGravity
:
155 case SouthEastGravity
:
162 void wClientConfigure(WWindow
* wwin
, XConfigureRequestEvent
* xcre
)
165 int nx
, ny
, nwidth
, nheight
;
168 /* printf("configure event: %d %d %d %d\n", xcre->x, xcre->y, xcre->width, xcre->height); */
172 * configure a window that was not mapped by us
176 xwc
.width
= xcre
->width
;
177 xwc
.height
= xcre
->height
;
178 xwc
.border_width
= xcre
->border_width
;
179 xwc
.stack_mode
= xcre
->detail
;
180 xwc
.sibling
= xcre
->above
;
181 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
185 if (wShapeSupported
) {
190 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
191 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
192 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
, &ujunk
);
193 wwin
->flags
.shaped
= b_shaped
;
196 if (xcre
->value_mask
& CWStackMode
) {
197 WObjDescriptor
*desc
;
200 if ((xcre
->value_mask
& CWSibling
) &&
201 (XFindContext(dpy
, xcre
->above
, wWinContext
, (XPointer
*) & desc
) == XCSUCCESS
)
202 && (desc
->parent_type
== WCLASS_WINDOW
)) {
203 sibling
= desc
->parent
;
204 xwc
.sibling
= sibling
->frame
->core
->window
;
206 xwc
.sibling
= xcre
->above
;
208 xwc
.stack_mode
= xcre
->detail
;
209 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
210 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
211 /* fix stacking order */
212 RemakeStackList(wwin
->screen_ptr
);
215 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
217 if (xcre
->value_mask
& CWBorderWidth
) {
218 wwin
->old_border_width
= xcre
->border_width
;
221 if (!wwin
->flags
.shaded
) {
222 /* If the window is shaded, wrong height will be set for the window */
223 if (xcre
->value_mask
& CWX
) {
225 /* Subtracting the border makes the window shift by 1 pixel -Dan */
226 /*if (HAS_BORDER(wwin)) {
227 nx -= FRAME_BORDER_WIDTH;
233 if (xcre
->value_mask
& CWY
) {
234 ny
= xcre
->y
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
235 /* Subtracting the border makes the window shift by 1 pixel -Dan */
236 /*if (HAS_BORDER(wwin)) {
237 ny -= FRAME_BORDER_WIDTH;
243 if (xcre
->value_mask
& CWWidth
)
244 nwidth
= xcre
->width
;
246 nwidth
= wwin
->frame
->core
->width
;
248 if (xcre
->value_mask
& CWHeight
)
249 nheight
= xcre
->height
;
251 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
253 if (nwidth
!= wwin
->old_geometry
.width
)
254 wwin
->flags
.maximized
&= ~(MAX_HORIZONTAL
| MAX_MAXIMUS
);
255 if (nheight
!= wwin
->old_geometry
.height
)
256 wwin
->flags
.maximized
&= ~(MAX_VERTICAL
| MAX_LEFTHALF
| MAX_RIGHTHALF
| MAX_MAXIMUS
);
258 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
259 wwin
->old_geometry
.x
= nx
;
260 wwin
->old_geometry
.y
= ny
;
261 wwin
->old_geometry
.width
= nwidth
;
262 wwin
->old_geometry
.height
= nheight
;
266 void 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
);
283 void wClientKill(WWindow
* wwin
)
285 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: _GNUSTEP_WM_ATTR
301 *----------------------------------------------------------------------
303 void wClientCheckProperty(WWindow
* wwin
, XPropertyEvent
* event
)
305 XWindowAttributes attribs
;
310 switch (event
->atom
) {
312 if (!wwin
->flags
.net_has_title
) {
313 /* window title was changed */
314 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
315 wWindowUpdateName(wwin
, NULL
);
317 wWindowUpdateName(wwin
, tmp
);
324 case XA_WM_ICON_NAME
:
325 if (!wwin
->flags
.net_has_icon_title
) {
331 /* icon title was changed */
332 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
333 wIconChangeTitle(wwin
->icon
, new_title
);
339 if (wwin
->main_window
!= None
) {
340 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
343 if (!wapp
|| !wapp
->app_icon
|| wapp
->app_icon
->docked
)
346 command
= GetCommandForWindow(wwin
->main_window
);
348 if (wapp
->app_icon
->command
)
349 wfree(wapp
->app_icon
->command
);
350 wapp
->app_icon
->command
= command
;
358 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
360 /* group leader update
362 * This means that the window is setting the leader after
363 * it was mapped, changing leaders or removing the leader.
365 * Valid state transitions are:
377 * Where G is the window_group hint, C is CLIENT_LEADER property
378 * and ' indicates the hint is unset.
380 * 1,2 - change group leader to new value of window_group
381 * 3 - change leader to value of CLIENT_LEADER
382 * 4 - change leader to value of window_group
383 * 5 - destroy application
384 * 6 - create application
386 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
387 && new_hints
->window_group
!= None
) {
392 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
393 && wwin
->wm_hints
->window_group
!= None
) {
399 if (wwin
->client_leader
) {
400 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
402 } else if (g1
&& !g2
) {
404 } else if (!g1
&& g2
) {
410 if (g1
&& g2
&& wwin
->wm_hints
->window_group
!= new_hints
->window_group
) {
412 } else if (g1
&& !g2
) {
414 } else if (!g1
&& g2
) {
421 /* Handling this may require more work. -Dan */
422 if (wwin
->fake_group
!= NULL
) {
427 XFree(wwin
->wm_hints
);
429 wwin
->wm_hints
= new_hints
;
431 /* do action according to state transition */
433 /* 3 - change leader to value of CLIENT_LEADER */
435 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
436 wwin
->main_window
= wwin
->client_leader
;
437 wwin
->group_id
= None
;
438 wApplicationCreate(wwin
);
441 /* 1,2,4 - change leader to new value of window_group */
445 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
446 wwin
->main_window
= new_hints
->window_group
;
447 wwin
->group_id
= wwin
->main_window
;
448 wApplicationCreate(wwin
);
451 /* 5 - destroy application */
453 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
454 wwin
->main_window
= None
;
455 wwin
->group_id
= None
;
458 /* 6 - create application */
460 wwin
->main_window
= new_hints
->window_group
;
461 wwin
->group_id
= wwin
->main_window
;
462 wApplicationCreate(wwin
);
464 /* 7 - we have a fake window group id, so just ignore anything else */
469 if (wwin
->wm_hints
) {
471 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
472 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
475 if (wwin
->flags
.miniaturized
&& wwin
->icon
) {
476 wIconUpdate(wwin
->icon
);
478 wapp
= wApplicationOf(wwin
->main_window
);
479 if (wapp
&& wapp
->app_icon
) {
480 wIconUpdate(wapp
->app_icon
->icon
);
481 /* The icon was updated in wIconUpdate, so we don't
482 * need update it again here */
483 wAppIconPaint(wapp
->app_icon
, False
);
487 if (wwin
->wm_hints
->flags
& UrgencyHint
)
488 wwin
->flags
.urgent
= 1;
490 wwin
->flags
.urgent
= 0;
491 wAppBounceWhileUrgent(wApplicationOf(wwin
->main_window
));
492 /*} else if (wwin->fake_group!=NULL) {
493 wwin->group_id = wwin->fake_group->leader; */
495 wwin
->group_id
= None
;
499 case XA_WM_NORMAL_HINTS
:
500 /* normal (geometry) hints */
505 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
506 wClientGetNormalHints(wwin
, &attribs
, False
, &foo
, &foo
, &bar
, &bar
);
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 (WFLAGP(wwin
, no_miniaturizable
)) {
546 WSETUFLAG(wwin
, no_miniaturizable
, 0);
547 WSETUFLAG(wwin
, no_miniaturize_button
, 0);
549 wWindowConfigureBorders(wwin
);
551 } else if (!WFLAGP(wwin
, no_miniaturizable
)) {
552 WSETUFLAG(wwin
, no_miniaturizable
, 1);
553 WSETUFLAG(wwin
, no_miniaturize_button
, 1);
555 wWindowConfigureBorders(wwin
);
562 if (event
->atom
== _XA_WM_PROTOCOLS
) {
564 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
566 WSETUFLAG(wwin
, kill_close
, !wwin
->protocols
.DELETE_WINDOW
);
569 wWindowUpdateButtonImages(wwin
);
571 } else if (event
->atom
== _XA_WM_COLORMAP_WINDOWS
) {
573 GetColormapWindows(wwin
);
574 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
576 } else if (event
->atom
== _XA_WINDOWMAKER_MENU
) {
579 wapp
= wApplicationOf(wwin
->main_window
);
583 /* TODO: remake appmenu update */
584 wAppMenuDestroy(wapp
->menu
);
586 if (wwin
->fake_group
) {
587 extern WPreferences wPreferences
;
588 WScreen
*scr
= wwin
->screen_ptr
;
589 WWindow
*foo
= scr
->focused_window
;
590 WFakeGroupLeader
*fPtr
= wwin
->fake_group
;
592 wApplicationDestroy(wapp
);
594 if (foo
->fake_group
&& foo
->fake_group
== fPtr
) {
595 WSETUFLAG(foo
, shared_appicon
, 0);
596 foo
->fake_group
= NULL
;
597 if (foo
->group_id
!= None
)
598 foo
->main_window
= foo
->group_id
;
599 else if (foo
->client_leader
!= None
)
600 foo
->main_window
= foo
->client_leader
;
601 else if (WFLAGP(foo
, emulate_appicon
))
602 foo
->main_window
= foo
->client_win
;
604 foo
->main_window
= None
;
605 if (foo
->main_window
) {
606 wapp
= wApplicationCreate(foo
);
612 if (fPtr
->leader
!= None
)
613 XDestroyWindow(dpy
, fPtr
->leader
);
614 fPtr
->retainCount
= 0;
616 fPtr
->origLeader
= None
;
618 wapp
= wApplicationOf(wwin
->main_window
);
620 wapp
->menu
= wAppMenuGet(scr
, wwin
->main_window
);
622 if (wPreferences
.auto_arrange_icons
) {
623 wArrangeIcons(wwin
->screen_ptr
, True
);
626 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
628 /* make the appmenu be mapped */
629 wSetFocusTo(wwin
->screen_ptr
, NULL
);
630 wSetFocusTo(wwin
->screen_ptr
, wwin
->screen_ptr
->focused_window
);
632 } else if (event
->atom
== _XA_GNUSTEP_WM_ATTR
) {
633 GNUstepWMAttributes
*attr
;
635 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
637 wWindowUpdateGNUstepAttr(wwin
, attr
);
641 wNETWMCheckClientHintChange(wwin
, event
);
647 *----------------------------------------------------------------------
648 * wClientGetNormalHints--
649 * Get size (normal) hints and a default geometry for the client
650 * window. The hints are also checked for inconsistency. If geometry is
651 * True, the returned data will account for client specified initial
655 * normal_hints is filled with valid data.
656 *----------------------------------------------------------------------
659 wClientGetNormalHints(WWindow
* wwin
, XWindowAttributes
* wattribs
, Bool geometry
,
660 int *x
, int *y
, unsigned *width
, unsigned *height
)
662 int pre_icccm
= 0; /* not used */
664 /* find a position for the window */
665 if (!wwin
->normal_hints
)
666 wwin
->normal_hints
= XAllocSizeHints();
668 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
669 wwin
->normal_hints
->flags
= 0;
674 *width
= wattribs
->width
;
675 *height
= wattribs
->height
;
677 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
678 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
680 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
681 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
682 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
684 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
685 wwin
->normal_hints
->base_width
= 0;
686 wwin
->normal_hints
->base_height
= 0;
688 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
689 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
* 2;
690 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
* 2;
693 /* some buggy apps set weird hints.. */
694 if (wwin
->normal_hints
->min_width
<= 0)
695 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
697 if (wwin
->normal_hints
->min_height
<= 0)
698 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
700 if (wwin
->normal_hints
->max_width
< wwin
->normal_hints
->min_width
)
701 wwin
->normal_hints
->max_width
= wwin
->normal_hints
->min_width
;
703 if (wwin
->normal_hints
->max_height
< wwin
->normal_hints
->min_height
)
704 wwin
->normal_hints
->max_height
= wwin
->normal_hints
->min_height
;
706 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
707 wwin
->normal_hints
->width_inc
= 1;
708 wwin
->normal_hints
->height_inc
= 1;
710 if (wwin
->normal_hints
->width_inc
<= 0)
711 wwin
->normal_hints
->width_inc
= 1;
712 if (wwin
->normal_hints
->height_inc
<= 0)
713 wwin
->normal_hints
->height_inc
= 1;
716 if (wwin
->normal_hints
->flags
& PAspect
) {
717 if (wwin
->normal_hints
->min_aspect
.x
< 1)
718 wwin
->normal_hints
->min_aspect
.x
= 1;
719 if (wwin
->normal_hints
->min_aspect
.y
< 1)
720 wwin
->normal_hints
->min_aspect
.y
= 1;
722 if (wwin
->normal_hints
->max_aspect
.x
< 1)
723 wwin
->normal_hints
->max_aspect
.x
= 1;
724 if (wwin
->normal_hints
->max_aspect
.y
< 1)
725 wwin
->normal_hints
->max_aspect
.y
= 1;
728 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
729 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
731 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
732 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
734 #ifdef IGNORE_PPOSITION
735 wwin
->normal_hints
->flags
&= ~PPosition
;
738 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
739 if (wwin
->normal_hints
->flags
& (USPosition
| PPosition
)) {
740 *x
= wwin
->normal_hints
->x
;
741 *y
= wwin
->normal_hints
->y
;
743 if (wwin
->normal_hints
->flags
& (USSize
| PSize
)) {
744 *width
= wwin
->normal_hints
->width
;
745 *height
= wwin
->normal_hints
->height
;
750 void GetColormapWindows(WWindow
* wwin
)
753 if (wwin
->cmap_windows
) {
754 XFree(wwin
->cmap_windows
);
757 wwin
->cmap_windows
= NULL
;
758 wwin
->cmap_window_no
= 0;
760 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
), &(wwin
->cmap_window_no
))
761 || !wwin
->cmap_windows
) {
762 wwin
->cmap_window_no
= 0;
763 wwin
->cmap_windows
= NULL
;