2 * WindowMaker 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 *--------------------------------------------------------------------
64 * Reparent the window back to the root window.
66 *--------------------------------------------------------------------
69 wClientRestore(WWindow
*wwin
)
73 wClientGetGravityOffsets(wwin
, &gx
, &gy
);
74 /* set the positio of the frame on screen */
75 wwin
->frame_x
-= gx
* (wwin
->old_border_width
- FRAME_BORDER_WIDTH
);
76 wwin
->frame_y
-= gy
* (wwin
->old_border_width
- FRAME_BORDER_WIDTH
);
77 /* if gravity is to the south, account for the border sizes */
79 wwin
->frame_y
+= (wwin
->frame
->top_width
+ wwin
->frame
->bottom_width
);
81 XUnmapWindow(dpy
, wwin
->client_win
);
82 XSetWindowBorderWidth(dpy
, wwin
->client_win
, wwin
->old_border_width
);
83 XReparentWindow(dpy
, wwin
->client_win
, wwin
->screen_ptr
->root_win
,
84 wwin
->frame_x
, wwin
->frame_y
);
85 if (wwin
->flags
.mapped
)
86 XMapWindow(dpy
, wwin
->client_win
);
88 /* 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 *----------------------------------------------------------------------
106 wClientSetState(WWindow
*wwin
, int state
, Window icon_win
)
108 unsigned long data
[2];
112 data
[0] = (unsigned long) state
;
113 data
[1] = (unsigned long) icon_win
;
115 XChangeProperty(dpy
, wwin
->client_win
, _XA_WM_STATE
, _XA_WM_STATE
, 32,
116 PropModeReplace
, (unsigned char *) data
, 2);
121 wClientGetGravityOffsets(WWindow
*wwin
, int *ofs_x
, int *ofs_y
)
123 switch (wwin
->normal_hints
->win_gravity
) {
130 case NorthWestGravity
:
138 case NorthEastGravity
:
150 case SouthWestGravity
:
158 case SouthEastGravity
:
168 wClientConfigure(WWindow
*wwin
, XConfigureRequestEvent
*xcre
)
171 int nx
, ny
, nwidth
, nheight
;
176 * configure a window that was not mapped by us
180 xwc
.width
= xcre
->width
;
181 xwc
.height
= xcre
->height
;
182 xwc
.border_width
= xcre
->border_width
;
183 xwc
.stack_mode
= xcre
->detail
;
184 xwc
.sibling
= xcre
->above
;
185 XConfigureWindow(dpy
, xcre
->window
, xcre
->value_mask
, &xwc
);
194 XShapeSelectInput(dpy
, wwin
->client_win
, ShapeNotifyMask
);
195 XShapeQueryExtents(dpy
, wwin
->client_win
, &b_shaped
, &junk
, &junk
,
196 &ujunk
, &ujunk
, &junk
, &junk
, &junk
, &ujunk
,
198 wwin
->flags
.shaped
= b_shaped
;
201 if (xcre
->value_mask
& CWStackMode
) {
202 WObjDescriptor
*desc
;
205 if ((xcre
->value_mask
& CWSibling
) &&
206 (XFindContext(dpy
, xcre
->above
, wWinContext
,
207 (XPointer
*)&desc
) == XCSUCCESS
)
208 && (desc
->parent_type
==WCLASS_WINDOW
)) {
209 sibling
=desc
->parent
;
210 xwc
.sibling
= sibling
->frame
->core
->window
;
212 xwc
.sibling
= xcre
->above
;
214 xwc
.stack_mode
= xcre
->detail
;
215 XConfigureWindow(dpy
, wwin
->frame
->core
->window
,
216 xcre
->value_mask
& (CWSibling
| CWStackMode
), &xwc
);
217 /* fix stacking order */
218 RemakeStackList(wwin
->screen_ptr
);
221 wClientGetGravityOffsets(wwin
, &ofs_x
, &ofs_y
);
223 if (xcre
->value_mask
& CWBorderWidth
) {
224 wwin
->old_border_width
= xcre
->border_width
;
227 if (!wwin
->flags
.shaded
) {
228 /* If the window is shaded, wrong height will be set for the window */
229 if (xcre
->value_mask
& CWX
)
230 nx
= xcre
->x
- FRAME_BORDER_WIDTH
;
234 if (xcre
->value_mask
& CWY
)
235 ny
= xcre
->y
- FRAME_BORDER_WIDTH
- ((ofs_y
< 0) ? 0 : wwin
->frame
->top_width
);
239 if (xcre
->value_mask
& CWWidth
)
240 nwidth
= xcre
->width
;
242 nwidth
= wwin
->frame
->core
->width
;
244 if (xcre
->value_mask
& CWHeight
)
245 nheight
= xcre
->height
;
247 nheight
= wwin
->frame
->core
->height
- wwin
->frame
->top_width
- wwin
->frame
->bottom_width
;
249 wWindowConfigure(wwin
, nx
, ny
, nwidth
, nheight
);
250 wwin
->old_geometry
.x
= nx
;
251 wwin
->old_geometry
.y
= ny
;
252 wwin
->old_geometry
.width
= nwidth
;
253 wwin
->old_geometry
.height
= nheight
;
259 wClientSendProtocol(WWindow
*wwin
, Atom protocol
, Time time
)
263 event
.xclient
.type
= ClientMessage
;
264 event
.xclient
.message_type
= _XA_WM_PROTOCOLS
;
265 event
.xclient
.format
= 32;
266 event
.xclient
.display
= dpy
;
267 event
.xclient
.window
= wwin
->client_win
;
268 event
.xclient
.data
.l
[0] = protocol
;
269 event
.xclient
.data
.l
[1] = time
;
270 event
.xclient
.data
.l
[2] = 0;
271 event
.xclient
.data
.l
[3] = 0;
272 XSendEvent(dpy
, wwin
->client_win
, False
, NoEventMask
, &event
);
279 wClientKill(WWindow
*wwin
)
281 XKillClient(dpy
, wwin
->client_win
);
286 *----------------------------------------------------------------------
287 * wClientCheckProperty--
288 * Handles PropertyNotify'es, verifying which property was
289 * changed and updating internal state according to that, like redrawing
290 * the icon title when it is changed.
293 * Depends on the changed property.
295 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
296 *----------------------------------------------------------------------
299 wClientCheckProperty(WWindow
*wwin
, XPropertyEvent
*event
)
301 XWindowAttributes attribs
;
306 switch (event
->atom
) {
308 /* window title was changed */
310 if (!wFetchName(dpy
, wwin
->client_win
, &tmp
)) {
311 /* the hint was removed */
312 tmp
= wstrdup(DEF_WINDOW_TITLE
);
314 if (wFrameWindowChangeTitle(wwin
->frame
, tmp
)) {
315 /* only update the menu if the title has actually changed */
316 UpdateSwitchMenu(wwin
->screen_ptr
, wwin
, ACTION_CHANGE
);
323 case XA_WM_ICON_NAME
:
329 /* icon title was changed */
330 wGetIconName(dpy
, wwin
->client_win
, &new_title
);
331 wIconChangeTitle(wwin
->icon
, new_title
);
336 if (wwin
->main_window
!=None
) {
337 WApplication
*wapp
= wApplicationOf(wwin
->main_window
);
341 if (wapp
&& wapp
->app_icon
) {
342 if (wapp
->app_icon
->command
!=NULL
)
343 free(wapp
->app_icon
->command
);
345 if (XGetCommand(dpy
, wwin
->main_window
, &argv
, &argc
)) {
346 if (argc
> 0 && argv
!= NULL
)
347 wapp
->app_icon
->command
= FlattenStringList(argv
,argc
);
349 XFreeStringList(argv
);
359 new_hints
= XGetWMHints(dpy
, wwin
->client_win
);
361 /* group leader update
363 * This means that the window is setting the leader after
364 * it was mapped, changing leaders or removing the leader.
366 * Valid state transitions are:
378 * Where G is the window_group hint, C is CLIENT_LEADER property
379 * and ' indicates the hint is unset.
381 * 1,2 - change group leader to new value of window_group
382 * 3 - change leader to value of CLIENT_LEADER
383 * 4 - change leader to value of window_group
384 * 5 - destroy application
385 * 6 - create application
387 if (new_hints
&& (new_hints
->flags
& WindowGroupHint
)
388 && new_hints
->window_group
!=None
) {
393 if (wwin
->wm_hints
&& (wwin
->wm_hints
->flags
& WindowGroupHint
)
394 && wwin
->wm_hints
->window_group
!=None
) {
400 if (wwin
->client_leader
) {
402 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
404 } else if (g1
&& !g2
) {
406 } else if (!g1
&& g2
) {
413 && wwin
->wm_hints
->window_group
!=new_hints
->window_group
) {
415 } else if (g1
&& !g2
) {
417 } else if (!g1
&& g2
) {
425 XFree(wwin
->wm_hints
);
427 wwin
->wm_hints
= new_hints
;
429 /* do action according to state transition */
431 /* 3 - change leader to value of CLIENT_LEADER */
433 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
434 wwin
->main_window
= wwin
->client_leader
;
435 wwin
->group_id
= None
;
436 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
439 /* 1,2,4 - change leader to new value of window_group */
443 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
444 wwin
->main_window
= new_hints
->window_group
;
445 wwin
->group_id
= wwin
->main_window
;
446 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
449 /* 5 - destroy application */
451 wApplicationDestroy(wApplicationOf(wwin
->main_window
));
452 wwin
->main_window
= None
;
453 wwin
->group_id
= None
;
456 /* 6 - create application */
458 wwin
->main_window
= new_hints
->window_group
;
459 wwin
->group_id
= wwin
->main_window
;
460 wApplicationCreate(wwin
->screen_ptr
, wwin
->main_window
);
465 printf("window leader update caused state transition %i\n",i
);
469 if (wwin
->wm_hints
) {
471 if ((wwin
->wm_hints
->flags
& IconPixmapHint
)
472 || (wwin
->wm_hints
->flags
& IconWindowHint
)) {
475 if (wwin
->flags
.miniaturized
) {
476 wIconUpdate(wwin
->icon
);
478 wapp
= wApplicationOf(wwin
->main_window
);
479 if (wapp
&& wapp
->app_icon
) {
480 wIconUpdate(wapp
->app_icon
->icon
);
484 if (wwin
->wm_hints
->flags
& UrgencyHint
)
485 wwin
->flags
.urgent
= 1;
487 wwin
->flags
.urgent
= 0;
489 wwin
->group_id
= None
;
493 case XA_WM_NORMAL_HINTS
:
494 /* normal (geometry) hints */
496 XGetWindowAttributes(dpy
, wwin
->client_win
, &attribs
);
497 GetNormalHints(wwin
, &attribs
, False
);
500 case XA_WM_TRANSIENT_FOR
:
505 if (!XGetTransientForHint(dpy
, wwin
->client_win
, &new_owner
)) {
508 if (new_owner
==0 || new_owner
== wwin
->client_win
) {
509 new_owner
= wwin
->screen_ptr
->root_win
;
512 if (new_owner
!=wwin
->transient_for
) {
513 owner
= wWindowFor(wwin
->transient_for
);
515 if (owner
->flags
.semi_focused
) {
516 owner
->flags
.semi_focused
= 0;
517 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
519 wFrameWindowPaint(owner
->frame
);
522 owner
= wWindowFor(new_owner
);
524 if (!owner
->flags
.semi_focused
) {
525 owner
->flags
.semi_focused
= 1;
526 if ((owner
->flags
.mapped
|| owner
->flags
.shaded
)
528 wFrameWindowPaint(owner
->frame
);
531 wwin
->transient_for
= new_owner
;
532 if (new_owner
==None
) {
533 if (wwin
->window_flags
.no_miniaturizable
) {
534 wwin
->window_flags
.no_miniaturizable
= 0;
535 wwin
->window_flags
.no_miniaturize_button
= 0;
537 wWindowConfigureBorders(wwin
);
539 } else if (!wwin
->window_flags
.no_miniaturizable
) {
540 wwin
->window_flags
.no_miniaturizable
= 1;
541 wwin
->window_flags
.no_miniaturize_button
= 1;
543 wWindowConfigureBorders(wwin
);
550 if (event
->atom
==_XA_WM_PROTOCOLS
) {
551 PropGetProtocols(wwin
->client_win
, &wwin
->protocols
);
552 if (wwin
->protocols
.DELETE_WINDOW
) {
553 wwin
->window_flags
.kill_close
= 0;
555 wwin
->window_flags
.kill_close
= 1;
558 wWindowUpdateButtonImages(wwin
);
560 } else if (event
->atom
==_XA_WM_COLORMAP_WINDOWS
) {
562 GetColormapWindows(wwin
);
563 wColormapInstallForWindow(wwin
->screen_ptr
, wwin
);
565 } else if (event
->atom
==_XA_WINDOWMAKER_MENU
) {
568 wapp
= wApplicationOf(wwin
->main_window
);
572 /* TODO: remake appmenu update */
573 wAppMenuDestroy(wapp
->menu
);
575 wapp
->menu
= wAppMenuGet(wwin
->screen_ptr
, wwin
->main_window
);
576 /* make the appmenu be mapped */
577 wSetFocusTo(wwin
->screen_ptr
, NULL
);
578 wSetFocusTo(wwin
->screen_ptr
,
579 wwin
->screen_ptr
->focused_window
);
581 } else if (event
->atom
==_XA_GNUSTEP_WM_ATTR
) {
582 GNUstepWMAttributes
*attr
;
584 PropGetGNUstepWMAttr(wwin
->client_win
, &attr
);
586 wWindowUpdateGNUstepAttr(wwin
, attr
);
595 *----------------------------------------------------------------------
597 * Get size (normal) hints and a default geometry for the client
598 * window. The hints are also checked for inconsistency.
601 * normal_hints is filled with valid data and geometry information
602 * about the client is also changed.
603 *----------------------------------------------------------------------
606 GetNormalHints(WWindow
*wwin
, XWindowAttributes
*wattribs
, int geometry
)
610 /* find a position for the window */
611 if (!wwin
->normal_hints
)
612 wwin
->normal_hints
= XAllocSizeHints();
614 if (!PropGetNormalHints(wwin
->client_win
, wwin
->normal_hints
, &pre_icccm
)) {
615 wwin
->normal_hints
->flags
= 0;
617 wwin
->client
.x
= wattribs
->x
;
618 wwin
->client
.y
= wattribs
->y
;
620 wwin
->client
.width
= wattribs
->width
;
621 wwin
->client
.height
= wattribs
->height
;
623 if (!(wwin
->normal_hints
->flags
& PWinGravity
)) {
624 wwin
->normal_hints
->win_gravity
= NorthWestGravity
;
626 if (!(wwin
->normal_hints
->flags
& PMinSize
)) {
627 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
628 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
630 if (!(wwin
->normal_hints
->flags
& PBaseSize
)) {
631 wwin
->normal_hints
->base_width
= 0;
632 wwin
->normal_hints
->base_height
= 0;
634 if (!(wwin
->normal_hints
->flags
& PMaxSize
)) {
635 wwin
->normal_hints
->max_width
= wwin
->screen_ptr
->scr_width
*2;
636 wwin
->normal_hints
->max_height
= wwin
->screen_ptr
->scr_height
*2;
639 if (!(wwin
->normal_hints
->flags
& PResizeInc
)) {
640 wwin
->normal_hints
->width_inc
= 1;
641 wwin
->normal_hints
->height_inc
= 1;
643 if (wwin
->normal_hints
->width_inc
<= 0)
644 wwin
->normal_hints
->width_inc
= 1;
645 if (wwin
->normal_hints
->height_inc
<= 0)
646 wwin
->normal_hints
->height_inc
= 1;
649 if (wwin
->normal_hints
->flags
& PAspect
) {
650 if (wwin
->normal_hints
->min_aspect
.x
< 1)
651 wwin
->normal_hints
->min_aspect
.x
= 1;
652 if (wwin
->normal_hints
->min_aspect
.y
< 1)
653 wwin
->normal_hints
->min_aspect
.y
= 1;
655 if (wwin
->normal_hints
->max_aspect
.x
< 1)
656 wwin
->normal_hints
->max_aspect
.x
= 1;
657 if (wwin
->normal_hints
->max_aspect
.y
< 1)
658 wwin
->normal_hints
->max_aspect
.y
= 1;
661 if (wwin
->normal_hints
->min_width
<= 0)
662 wwin
->normal_hints
->min_width
= MIN_WINDOW_SIZE
;
664 if (wwin
->normal_hints
->min_height
<= 0)
665 wwin
->normal_hints
->min_height
= MIN_WINDOW_SIZE
;
667 if (wwin
->normal_hints
->min_height
> wwin
->normal_hints
->max_height
) {
668 wwin
->normal_hints
->min_height
= wwin
->normal_hints
->max_height
;
670 if (wwin
->normal_hints
->min_width
> wwin
->normal_hints
->max_width
) {
671 wwin
->normal_hints
->min_width
= wwin
->normal_hints
->max_width
;
674 /* pre ICCCM (old) client */
675 if (pre_icccm
&& !wwin
->screen_ptr
->flags
.startup
&& geometry
) {
677 printf("PRE ICCCM\n");
679 if (wwin
->normal_hints
->flags
& (USPosition
|PPosition
)) {
680 wwin
->client
.x
= wwin
->normal_hints
->x
;
681 wwin
->client
.y
= wwin
->normal_hints
->y
;
683 if (wwin
->normal_hints
->flags
& (USSize
|PSize
)) {
684 wwin
->client
.width
= wwin
->normal_hints
->width
;
685 wwin
->client
.height
= wwin
->normal_hints
->height
;
692 GetColormapWindows(WWindow
*wwin
)
694 if (wwin
->cmap_windows
) {
695 XFree(wwin
->cmap_windows
);
697 if (!XGetWMColormapWindows(dpy
, wwin
->client_win
, &(wwin
->cmap_windows
),
698 &(wwin
->cmap_window_no
))) {
699 wwin
->cmap_window_no
= 0;
700 wwin
->cmap_windows
= NULL
;