2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 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 if (frame
->stacking
->under
) {
215 frame
->stacking
->under
->stacking
->above
= frame
;
217 WMSetInBag(scr
->stacking_list
, level
, frame
);
219 /* raise transients under us from bottom to top
220 * so that the order is kept */
222 wlist
= frame
->stacking
->under
;
223 while (wlist
&& wlist
->stacking
->under
)
224 wlist
= wlist
->stacking
->under
;
225 while (wlist
&& wlist
!=frame
) {
226 if (wlist
->stacking
->child_of
== frame
) {
230 wlist
= wlist
->stacking
->above
;
233 /* try to optimize things a little */
234 if (frame
->stacking
->above
== NULL
) {
236 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
237 int i
, last
= above
->stacking
->window_level
;
239 /* find the 1st level above us which has windows in it */
240 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
241 above
= WMGetFromBag(scr
->stacking_list
, i
);
246 if (above
!= frame
&& above
!= NULL
) {
247 while (above
->stacking
->under
)
248 above
= above
->stacking
->under
;
249 moveFrameToUnder(above
, frame
);
251 /* no window above us */
253 XRaiseWindow(dpy
, frame
->window
);
256 moveFrameToUnder(frame
->stacking
->above
, frame
);
259 notifyStackChange(frame
, "raise");
261 #ifdef VIRTUAL_DESKTOP
262 wWorkspaceRaiseEdge(scr
);
268 wRaiseLowerFrame(WCoreWindow
*frame
)
270 if (!frame
->stacking
->above
271 ||(frame
->stacking
->window_level
272 !=frame
->stacking
->above
->stacking
->window_level
)) {
276 WCoreWindow
*scan
= frame
->stacking
->above
;
277 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
281 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
282 WWindow
*scan_wwin
= (WWindow
*)scan
->descriptor
.parent
;
284 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
285 && scan_wwin
->flags
.mapped
) {
289 scan
= scan
->stacking
->above
;
302 wLowerFrame(WCoreWindow
*frame
)
304 WScreen
*scr
= frame
->screen_ptr
;
305 WCoreWindow
*wlist
=frame
;
306 int level
= frame
->stacking
->window_level
;
308 /* already in bottom */
309 if (wlist
->stacking
->under
== NULL
) {
312 /* cant lower transient below below its owner */
313 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
316 /* remove from the list */
317 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
318 /* it was the top window */
319 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
320 frame
->stacking
->under
->stacking
->above
= NULL
;
322 if (frame
->stacking
->under
)
323 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
324 if (frame
->stacking
->above
)
325 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
327 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
329 /* look for place to put this window */
331 WCoreWindow
*owner
= frame
->stacking
->child_of
;
333 if (owner
!= wlist
) {
334 while (wlist
->stacking
->under
) {
335 /* if this is a transient, it should not be placed under
337 if (owner
== wlist
->stacking
->under
)
339 wlist
= wlist
->stacking
->under
;
343 /* insert under the place found */
344 frame
->stacking
->above
= wlist
;
346 frame
->stacking
->under
= wlist
->stacking
->under
;
347 if (wlist
->stacking
->under
)
348 wlist
->stacking
->under
->stacking
->above
= frame
;
349 wlist
->stacking
->under
= frame
;
351 frame
->stacking
->under
= NULL
;
354 if (frame
->stacking
->above
== NULL
) {
356 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
357 int i
, last
= above
->stacking
->window_level
;
359 /* find the 1st level above us which has windows in it */
360 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
361 above
= WMGetFromBag(scr
->stacking_list
, i
);
366 if (above
!= frame
&& above
!= NULL
) {
367 while (above
->stacking
->under
)
368 above
= above
->stacking
->under
;
369 moveFrameToUnder(above
, frame
);
371 /* no window below us */
372 XLowerWindow(dpy
, frame
->window
);
375 moveFrameToUnder(frame
->stacking
->above
, frame
);
378 notifyStackChange(frame
, "lower");
383 *----------------------------------------------------------------------
385 * Inserts the frame in the top of the stacking list. The
386 * stacking precedence is obeyed.
392 * The frame is added to it's screen's window list.
393 *----------------------------------------------------------------------
396 AddToStackList(WCoreWindow
*frame
)
398 WCoreWindow
*curtop
, *wlist
;
399 int index
= frame
->stacking
->window_level
;
400 WScreen
*scr
= frame
->screen_ptr
;
401 WCoreWindow
*trans
= NULL
;
403 frame
->screen_ptr
->window_count
++;
404 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
)frame
);
405 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
407 /* first window in this level */
408 if (curtop
== NULL
) {
409 WMSetInBag(scr
->stacking_list
, index
, frame
);
410 frame
->stacking
->above
= NULL
;
411 frame
->stacking
->under
= NULL
;
416 /* check if this is a transient owner */
419 if (wlist
->stacking
->child_of
== frame
)
421 wlist
= wlist
->stacking
->under
;
423 /* trans will hold the transient in the lowest position
424 * in stacking list */
426 frame
->stacking
->above
= trans
;
428 /* window is owner of a transient.. put it below
429 * the lowest transient */
430 frame
->stacking
->under
= trans
->stacking
->under
;
431 if (trans
->stacking
->under
) {
432 trans
->stacking
->under
->stacking
->above
= frame
;
434 trans
->stacking
->under
= frame
;
436 /* window is not owner of transients.. just put it in the
437 * top of other windows */
438 frame
->stacking
->under
= curtop
;
439 curtop
->stacking
->above
= frame
;
440 WMSetInBag(scr
->stacking_list
, index
, frame
);
447 *----------------------------------------------------------------------
448 * MoveInStackListAbove--
449 * Moves the frame above "next".
455 * Stacking order may be changed.
456 * Window level for frame may be changed.
457 *----------------------------------------------------------------------
460 MoveInStackListAbove(WCoreWindow
*next
, WCoreWindow
*frame
)
463 WScreen
*scr
= frame
->screen_ptr
;
466 if (!next
|| frame
->stacking
->under
== next
)
469 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
470 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
472 index
= frame
->stacking
->window_level
;
474 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
476 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
477 if (frame
->stacking
->under
)
478 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
479 if (frame
->stacking
->above
)
480 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
481 if (next
->stacking
->above
)
482 next
->stacking
->above
->stacking
->under
= frame
;
483 frame
->stacking
->under
= next
;
484 frame
->stacking
->above
= next
->stacking
->above
;
485 next
->stacking
->above
= frame
;
487 WMSetInBag(scr
->stacking_list
, index
, frame
);
489 /* try to optimize things a little */
490 if (frame
->stacking
->above
== NULL
) {
491 WCoreWindow
*above
= NULL
;
494 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+1, &iter
);
496 above
= WMBagNext(scr
->stacking_list
, &iter
)) {
499 while (above
->stacking
->under
)
500 above
= above
->stacking
->under
;
504 XRaiseWindow(dpy
, frame
->window
);
506 moveFrameToUnder(above
, frame
);
509 moveFrameToUnder(frame
->stacking
->above
, frame
);
512 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
517 *----------------------------------------------------------------------
518 * MoveInStackListUnder--
519 * Moves the frame to under "prev".
525 * Stacking order may be changed.
526 * Window level for frame may be changed.
527 *----------------------------------------------------------------------
530 MoveInStackListUnder(WCoreWindow
*prev
, WCoreWindow
*frame
)
534 WScreen
*scr
= frame
->screen_ptr
;
536 if (!prev
|| frame
->stacking
->above
== prev
)
539 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
540 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
542 index
= frame
->stacking
->window_level
;
544 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
546 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
547 if (frame
->stacking
->under
)
548 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
549 if (frame
->stacking
->above
)
550 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
551 if (prev
->stacking
->under
)
552 prev
->stacking
->under
->stacking
->above
= frame
;
553 frame
->stacking
->above
= prev
;
554 frame
->stacking
->under
= prev
->stacking
->under
;
555 prev
->stacking
->under
= frame
;
556 moveFrameToUnder(prev
, frame
);
558 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
563 RemoveFromStackList(WCoreWindow
*frame
)
565 int index
= frame
->stacking
->window_level
;
567 if (XDeleteContext(dpy
, frame
->window
, wStackContext
)==XCNOENT
) {
568 wwarning("RemoveFromStackingList(): window not in list ");
571 /* remove from the window stack list */
572 if (frame
->stacking
->under
)
573 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
574 if (frame
->stacking
->above
)
575 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
576 else /* this was the first window on the list */
577 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
,
578 frame
->stacking
->under
);
580 frame
->screen_ptr
->window_count
--;
582 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
587 ChangeStackingLevel(WCoreWindow
*frame
, int new_level
)
591 if (frame
->stacking
->window_level
== new_level
)
593 old_level
= frame
->stacking
->window_level
;
595 RemoveFromStackList(frame
);
596 frame
->stacking
->window_level
= new_level
;
597 AddToStackList(frame
);
598 if (old_level
> new_level
) {