2 * Window Maker window manager
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
29 #include <X11/Xutil.h>
31 #include "WindowMaker.h"
36 #include "properties.h"
40 /*** Global Variables ***/
41 extern XContext wStackContext
;
43 extern WPreferences wPreferences
;
46 static void notifyStackChange(WCoreWindow
*frame
, char *detail
)
48 WWindow
*wwin
= wWindowFor(frame
->window
);
50 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
55 *----------------------------------------------------------------------
57 * Remakes the stacking_list for the screen, getting the real
58 * stacking order from the server and reordering windows that are not
59 * in the correct stacking.
62 * The stacking order list and the actual window stacking
63 * may be changed (corrected)
65 *----------------------------------------------------------------------
68 RemakeStackList(WScreen
*scr
)
71 unsigned int nwindows
;
78 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
79 wwarning(_("could not get window list!!"));
82 WMEmptyBag(scr
->stacking_list
);
84 /* verify list integrity */
86 for (i
=0; i
<nwindows
; i
++) {
87 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*)&frame
)
93 level
= frame
->stacking
->window_level
;
94 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
96 tmp
->stacking
->above
= frame
;
97 frame
->stacking
->under
= tmp
;
98 frame
->stacking
->above
= NULL
;
99 WMSetInBag(scr
->stacking_list
, level
, frame
);
103 if (c
!=scr
->window_count
) {
104 puts("Found different number of windows than in window lists!!!");
107 scr
->window_count
= c
;
116 *----------------------------------------------------------------------
118 * Reorders the actual window stacking, so that it has the stacking
119 * order in the internal window stacking lists. It does the opposite
120 * of RemakeStackList().
123 * Windows may be restacked.
124 *----------------------------------------------------------------------
127 CommitStacking(WScreen
*scr
)
134 nwindows
= scr
->window_count
;
135 windows
= wmalloc(sizeof(Window
)*nwindows
);
138 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
142 puts("Internal inconsistency! window_count is incorrect!!!");
143 printf("window_count says %i windows\n", nwindows
);
148 windows
[i
++] = tmp
->window
;
149 tmp
= tmp
->stacking
->under
;
152 XRestackWindows(dpy
, windows
, i
);
156 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
160 *----------------------------------------------------------------------
162 * Reestacks windows so that "frame" is under "under".
168 * Changes the stacking order of frame.
169 *----------------------------------------------------------------------
172 moveFrameToUnder(WCoreWindow
*under
, WCoreWindow
*frame
)
176 wins
[0] = under
->window
;
177 wins
[1] = frame
->window
;
178 XRestackWindows(dpy
, wins
, 2);
182 *----------------------------------------------------------------------
184 * Raises a frame taking the window level into account.
190 * Window stacking order and stacking list are changed.
192 *----------------------------------------------------------------------
195 wRaiseFrame(WCoreWindow
*frame
)
197 WCoreWindow
*wlist
= frame
;
198 int level
= frame
->stacking
->window_level
;
199 WScreen
*scr
= frame
->screen_ptr
;
202 if (frame
->stacking
->above
== NULL
) {
206 /* insert it on top of other windows on the same level */
207 if (frame
->stacking
->under
)
208 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
209 if (frame
->stacking
->above
)
210 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
212 frame
->stacking
->above
= NULL
;
213 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
214 frame
->stacking
->under
->stacking
->above
= frame
;
215 WMSetInBag(scr
->stacking_list
, level
, frame
);
217 /* raise transients under us from bottom to top
218 * so that the order is kept */
220 wlist
= frame
->stacking
->under
;
221 while (wlist
&& wlist
->stacking
->under
)
222 wlist
= wlist
->stacking
->under
;
223 while (wlist
&& wlist
!=frame
) {
224 if (wlist
->stacking
->child_of
== frame
) {
228 wlist
= wlist
->stacking
->above
;
231 /* try to optimize things a little */
232 if (frame
->stacking
->above
== NULL
) {
234 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
235 int i
, last
= above
->stacking
->window_level
;
237 /* find the 1st level above us which has windows in it */
238 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
239 above
= WMGetFromBag(scr
->stacking_list
, i
);
244 if (above
!= frame
&& above
!= NULL
) {
245 while (above
->stacking
->under
)
246 above
= above
->stacking
->under
;
247 moveFrameToUnder(above
, frame
);
249 /* no window above us */
251 XRaiseWindow(dpy
, frame
->window
);
254 moveFrameToUnder(frame
->stacking
->above
, frame
);
257 notifyStackChange(frame
, "raise");
259 #ifdef VIRTUAL_DESKTOP
260 wWorkspaceRaiseEdge(scr
);
266 wRaiseLowerFrame(WCoreWindow
*frame
)
268 if (!frame
->stacking
->above
269 ||(frame
->stacking
->window_level
270 !=frame
->stacking
->above
->stacking
->window_level
)) {
274 WCoreWindow
*scan
= frame
->stacking
->above
;
275 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
279 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
280 WWindow
*scan_wwin
= (WWindow
*)scan
->descriptor
.parent
;
282 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
283 && scan_wwin
->flags
.mapped
) {
287 scan
= scan
->stacking
->above
;
300 wLowerFrame(WCoreWindow
*frame
)
302 WScreen
*scr
= frame
->screen_ptr
;
303 WCoreWindow
*wlist
=frame
;
304 int level
= frame
->stacking
->window_level
;
306 /* already in bottom */
307 if (wlist
->stacking
->under
== NULL
) {
310 /* cant lower transient below below its owner */
311 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
314 /* remove from the list */
315 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
316 /* it was the top window */
317 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
318 frame
->stacking
->under
->stacking
->above
= NULL
;
320 if (frame
->stacking
->under
)
321 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
322 if (frame
->stacking
->above
)
323 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
325 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
327 /* look for place to put this window */
329 WCoreWindow
*owner
= frame
->stacking
->child_of
;
331 if (owner
!= wlist
) {
332 while (wlist
->stacking
->under
) {
333 /* if this is a transient, it should not be placed under
335 if (owner
== wlist
->stacking
->under
)
337 wlist
= wlist
->stacking
->under
;
341 /* insert under the place found */
342 frame
->stacking
->above
= wlist
;
343 frame
->stacking
->under
= wlist
->stacking
->under
;
344 if (wlist
->stacking
->under
)
345 wlist
->stacking
->under
->stacking
->above
= frame
;
346 wlist
->stacking
->under
= frame
;
348 if (frame
->stacking
->above
== NULL
) {
350 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
351 int i
, last
= above
->stacking
->window_level
;
353 /* find the 1st level above us which has windows in it */
354 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
355 above
= WMGetFromBag(scr
->stacking_list
, i
);
360 if (above
!= frame
&& above
!= NULL
) {
361 while (above
->stacking
->under
)
362 above
= above
->stacking
->under
;
363 moveFrameToUnder(above
, frame
);
365 /* no window below us */
366 XLowerWindow(dpy
, frame
->window
);
369 moveFrameToUnder(frame
->stacking
->above
, frame
);
372 notifyStackChange(frame
, "lower");
377 *----------------------------------------------------------------------
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
386 * The frame is added to it's screen's window list.
387 *----------------------------------------------------------------------
390 AddToStackList(WCoreWindow
*frame
)
392 WCoreWindow
*curtop
, *wlist
;
393 int index
= frame
->stacking
->window_level
;
394 WScreen
*scr
= frame
->screen_ptr
;
395 WCoreWindow
*trans
= NULL
;
397 frame
->screen_ptr
->window_count
++;
398 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
)frame
);
399 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
401 /* first window in this level */
402 if (curtop
== NULL
) {
403 WMSetInBag(scr
->stacking_list
, index
, frame
);
404 frame
->stacking
->above
= NULL
;
405 frame
->stacking
->under
= NULL
;
410 /* check if this is a transient owner */
413 if (wlist
->stacking
->child_of
== frame
)
415 wlist
= wlist
->stacking
->under
;
417 /* trans will hold the transient in the lowest position
418 * in stacking list */
420 frame
->stacking
->above
= trans
;
422 /* window is owner of a transient.. put it below
423 * the lowest transient */
424 frame
->stacking
->under
= trans
->stacking
->under
;
425 if (trans
->stacking
->under
) {
426 trans
->stacking
->under
->stacking
->above
= frame
;
428 trans
->stacking
->under
= frame
;
430 /* window is not owner of transients.. just put it in the
431 * top of other windows */
432 frame
->stacking
->under
= curtop
;
433 curtop
->stacking
->above
= frame
;
434 WMSetInBag(scr
->stacking_list
, index
, frame
);
441 *----------------------------------------------------------------------
442 * MoveInStackListAbove--
443 * Moves the frame above "next".
449 * Stacking order may be changed.
450 * Window level for frame may be changed.
451 *----------------------------------------------------------------------
454 MoveInStackListAbove(WCoreWindow
*next
, WCoreWindow
*frame
)
457 WScreen
*scr
= frame
->screen_ptr
;
460 if (!next
|| frame
->stacking
->under
== next
)
463 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
464 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
466 index
= frame
->stacking
->window_level
;
468 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
470 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
471 if (frame
->stacking
->under
)
472 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
473 if (frame
->stacking
->above
)
474 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
475 if (next
->stacking
->above
)
476 next
->stacking
->above
->stacking
->under
= frame
;
477 frame
->stacking
->under
= next
;
478 frame
->stacking
->above
= next
->stacking
->above
;
479 next
->stacking
->above
= frame
;
481 WMSetInBag(scr
->stacking_list
, index
, frame
);
483 /* try to optimize things a little */
484 if (frame
->stacking
->above
== NULL
) {
485 WCoreWindow
*above
= NULL
;
488 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+1, &iter
);
490 above
= WMBagNext(scr
->stacking_list
, &iter
)) {
493 while (above
->stacking
->under
)
494 above
= above
->stacking
->under
;
498 XRaiseWindow(dpy
, frame
->window
);
500 moveFrameToUnder(above
, frame
);
503 moveFrameToUnder(frame
->stacking
->above
, frame
);
506 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
511 *----------------------------------------------------------------------
512 * MoveInStackListUnder--
513 * Moves the frame to under "prev".
519 * Stacking order may be changed.
520 * Window level for frame may be changed.
521 *----------------------------------------------------------------------
524 MoveInStackListUnder(WCoreWindow
*prev
, WCoreWindow
*frame
)
528 WScreen
*scr
= frame
->screen_ptr
;
530 if (!prev
|| frame
->stacking
->above
== prev
)
533 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
534 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
536 index
= frame
->stacking
->window_level
;
538 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
540 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
541 if (frame
->stacking
->under
)
542 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
543 if (frame
->stacking
->above
)
544 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
545 if (prev
->stacking
->under
)
546 prev
->stacking
->under
->stacking
->above
= frame
;
547 frame
->stacking
->above
= prev
;
548 frame
->stacking
->under
= prev
->stacking
->under
;
549 prev
->stacking
->under
= frame
;
550 moveFrameToUnder(prev
, frame
);
552 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
557 RemoveFromStackList(WCoreWindow
*frame
)
559 int index
= frame
->stacking
->window_level
;
561 if (XDeleteContext(dpy
, frame
->window
, wStackContext
)==XCNOENT
) {
562 wwarning("RemoveFromStackingList(): window not in list ");
565 /* remove from the window stack list */
566 if (frame
->stacking
->under
)
567 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
568 if (frame
->stacking
->above
)
569 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
570 else /* this was the first window on the list */
571 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
,
572 frame
->stacking
->under
);
574 frame
->screen_ptr
->window_count
--;
579 ChangeStackingLevel(WCoreWindow
*frame
, int new_level
)
583 if (frame
->stacking
->window_level
== new_level
)
585 old_level
= frame
->stacking
->window_level
;
587 RemoveFromStackList(frame
);
588 frame
->stacking
->window_level
= new_level
;
589 AddToStackList(frame
);
590 if (old_level
> new_level
) {