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"
42 /*** Global Variables ***/
43 extern XContext wStackContext
;
45 extern WPreferences wPreferences
;
49 *----------------------------------------------------------------------
51 * Remakes the stacking_list for the screen, getting the real
52 * stacking order from the server and reordering windows that are not
53 * in the correct stacking.
56 * The stacking order list and the actual window stacking
57 * may be changed (corrected)
59 *----------------------------------------------------------------------
62 RemakeStackList(WScreen
*scr
)
65 unsigned int nwindows
;
72 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
73 wwarning(_("could not get window list!!"));
76 WMEmptyBag(scr
->stacking_list
);
78 /* verify list integrity */
80 for (i
=0; i
<nwindows
; i
++) {
81 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*)&frame
)
87 level
= frame
->stacking
->window_level
;
88 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
90 tmp
->stacking
->above
= frame
;
91 frame
->stacking
->under
= tmp
;
92 frame
->stacking
->above
= NULL
;
93 WMSetInBag(scr
->stacking_list
, level
, frame
);
97 if (c
!=scr
->window_count
) {
98 puts("Found different number of windows than in window lists!!!");
101 scr
->window_count
= c
;
110 *----------------------------------------------------------------------
112 * Reorders the actual window stacking, so that it has the stacking
113 * order in the internal window stacking lists. It does the opposite
114 * of RemakeStackList().
117 * Windows may be restacked.
118 *----------------------------------------------------------------------
121 CommitStacking(WScreen
*scr
)
128 nwindows
= scr
->window_count
;
129 windows
= wmalloc(sizeof(Window
)*nwindows
);
132 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
136 puts("Internal inconsistency! window_count is incorrect!!!");
137 printf("window_count says %i windows\n", nwindows
);
142 windows
[i
++] = tmp
->window
;
143 tmp
= tmp
->stacking
->under
;
146 XRestackWindows(dpy
, windows
, i
);
150 wKWMBroadcastStacking(scr
);
156 *----------------------------------------------------------------------
158 * Reestacks windows so that "frame" is under "under".
164 * Changes the stacking order of frame.
165 *----------------------------------------------------------------------
168 moveFrameToUnder(WCoreWindow
*under
, WCoreWindow
*frame
)
172 wins
[0] = under
->window
;
173 wins
[1] = frame
->window
;
174 XRestackWindows(dpy
, wins
, 2);
177 wKWMBroadcastStacking(under
->screen_ptr
);
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
);
258 WWindow
*wwin
= wWindowFor(frame
->window
);
261 wKWMSendEventMessage(wwin
, WKWMRaiseWindow
);
264 #ifdef VIRTUAL_DESKTOP
265 wWorkspaceRaiseEdge(scr
);
272 wRaiseLowerFrame(WCoreWindow
*frame
)
274 if (!frame
->stacking
->above
275 ||(frame
->stacking
->window_level
276 !=frame
->stacking
->above
->stacking
->window_level
)) {
280 WCoreWindow
*scan
= frame
->stacking
->above
;
281 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
285 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
286 WWindow
*scan_wwin
= (WWindow
*)scan
->descriptor
.parent
;
288 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
289 && scan_wwin
->flags
.mapped
) {
293 scan
= scan
->stacking
->above
;
306 wLowerFrame(WCoreWindow
*frame
)
308 WScreen
*scr
= frame
->screen_ptr
;
309 WCoreWindow
*wlist
=frame
;
310 int level
= frame
->stacking
->window_level
;
312 /* already in bottom */
313 if (wlist
->stacking
->under
== NULL
) {
316 /* cant lower transient below below its owner */
317 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
320 /* remove from the list */
321 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
322 /* it was the top window */
323 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
324 frame
->stacking
->under
->stacking
->above
= NULL
;
326 if (frame
->stacking
->under
)
327 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
328 if (frame
->stacking
->above
)
329 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
331 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
333 /* look for place to put this window */
335 WCoreWindow
*owner
= frame
->stacking
->child_of
;
337 if (owner
!= wlist
) {
338 while (wlist
->stacking
->under
) {
339 /* if this is a transient, it should not be placed under
341 if (owner
== wlist
->stacking
->under
)
343 wlist
= wlist
->stacking
->under
;
347 /* insert under the place found */
348 frame
->stacking
->above
= wlist
;
349 frame
->stacking
->under
= wlist
->stacking
->under
;
350 if (wlist
->stacking
->under
)
351 wlist
->stacking
->under
->stacking
->above
= frame
;
352 wlist
->stacking
->under
= frame
;
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
);
379 WWindow
*wwin
= wWindowFor(frame
->window
);
382 wKWMSendEventMessage(wwin
, WKWMLowerWindow
);
390 *----------------------------------------------------------------------
392 * Inserts the frame in the top of the stacking list. The
393 * stacking precedence is obeyed.
399 * The frame is added to it's screen's window list.
400 *----------------------------------------------------------------------
403 AddToStackList(WCoreWindow
*frame
)
405 WCoreWindow
*curtop
, *wlist
;
406 int index
= frame
->stacking
->window_level
;
407 WScreen
*scr
= frame
->screen_ptr
;
408 WCoreWindow
*trans
= NULL
;
410 frame
->screen_ptr
->window_count
++;
411 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
)frame
);
412 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
414 /* first window in this level */
415 if (curtop
== NULL
) {
416 WMSetInBag(scr
->stacking_list
, index
, frame
);
417 frame
->stacking
->above
= NULL
;
418 frame
->stacking
->under
= NULL
;
423 /* check if this is a transient owner */
426 if (wlist
->stacking
->child_of
== frame
)
428 wlist
= wlist
->stacking
->under
;
430 /* trans will hold the transient in the lowest position
431 * in stacking list */
433 frame
->stacking
->above
= trans
;
435 /* window is owner of a transient.. put it below
436 * the lowest transient */
437 frame
->stacking
->under
= trans
->stacking
->under
;
438 if (trans
->stacking
->under
) {
439 trans
->stacking
->under
->stacking
->above
= frame
;
441 trans
->stacking
->under
= frame
;
443 /* window is not owner of transients.. just put it in the
444 * top of other windows */
445 frame
->stacking
->under
= curtop
;
446 curtop
->stacking
->above
= frame
;
447 WMSetInBag(scr
->stacking_list
, index
, frame
);
455 *----------------------------------------------------------------------
456 * MoveInStackListAbove--
457 * Moves the frame above "next".
463 * Stacking order may be changed.
464 * Window level for frame may be changed.
465 *----------------------------------------------------------------------
468 MoveInStackListAbove(WCoreWindow
*next
, WCoreWindow
*frame
)
471 WScreen
*scr
= frame
->screen_ptr
;
474 if (!next
|| frame
->stacking
->under
== next
)
477 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
478 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
480 index
= frame
->stacking
->window_level
;
482 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
484 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
485 if (frame
->stacking
->under
)
486 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
487 if (frame
->stacking
->above
)
488 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
489 if (next
->stacking
->above
)
490 next
->stacking
->above
->stacking
->under
= frame
;
491 frame
->stacking
->under
= next
;
492 frame
->stacking
->above
= next
->stacking
->above
;
493 next
->stacking
->above
= frame
;
495 WMSetInBag(scr
->stacking_list
, index
, frame
);
497 /* try to optimize things a little */
498 if (frame
->stacking
->above
== NULL
) {
499 WCoreWindow
*above
= NULL
;
502 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+1, &iter
);
504 above
= WMBagNext(scr
->stacking_list
, &iter
)) {
507 while (above
->stacking
->under
)
508 above
= above
->stacking
->under
;
512 XRaiseWindow(dpy
, frame
->window
);
514 moveFrameToUnder(above
, frame
);
517 moveFrameToUnder(frame
->stacking
->above
, frame
);
524 *----------------------------------------------------------------------
525 * MoveInStackListUnder--
526 * Moves the frame to under "prev".
532 * Stacking order may be changed.
533 * Window level for frame may be changed.
534 *----------------------------------------------------------------------
537 MoveInStackListUnder(WCoreWindow
*prev
, WCoreWindow
*frame
)
541 WScreen
*scr
= frame
->screen_ptr
;
543 if (!prev
|| frame
->stacking
->above
== prev
)
546 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
547 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
549 index
= frame
->stacking
->window_level
;
551 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
553 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
554 if (frame
->stacking
->under
)
555 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
556 if (frame
->stacking
->above
)
557 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
558 if (prev
->stacking
->under
)
559 prev
->stacking
->under
->stacking
->above
= frame
;
560 frame
->stacking
->above
= prev
;
561 frame
->stacking
->under
= prev
->stacking
->under
;
562 prev
->stacking
->under
= frame
;
563 moveFrameToUnder(prev
, frame
);
568 RemoveFromStackList(WCoreWindow
*frame
)
570 int index
= frame
->stacking
->window_level
;
572 if (XDeleteContext(dpy
, frame
->window
, wStackContext
)==XCNOENT
) {
573 wwarning("RemoveFromStackingList(): window not in list ");
576 /* remove from the window stack list */
577 if (frame
->stacking
->under
)
578 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
579 if (frame
->stacking
->above
)
580 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
581 else /* this was the first window on the list */
582 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
,
583 frame
->stacking
->under
);
585 frame
->screen_ptr
->window_count
--;
590 ChangeStackingLevel(WCoreWindow
*frame
, int new_level
)
594 if (frame
->stacking
->window_level
== new_level
)
596 old_level
= frame
->stacking
->window_level
;
598 RemoveFromStackList(frame
);
599 frame
->stacking
->window_level
= new_level
;
600 AddToStackList(frame
);
601 if (old_level
> new_level
) {