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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <X11/Xutil.h>
30 #include "WindowMaker.h"
34 #include "properties.h"
36 #include "workspace.h"
39 static void notifyStackChange(WCoreWindow
* frame
, char *detail
)
41 WWindow
*wwin
= wWindowFor(frame
->window
);
43 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
47 *----------------------------------------------------------------------
49 * Remakes the stacking_list for the screen, getting the real
50 * stacking order from the server and reordering windows that are not
51 * in the correct stacking.
54 * The stacking order list and the actual window stacking
55 * may be changed (corrected)
57 *----------------------------------------------------------------------
59 void RemakeStackList(WScreen
* scr
)
62 unsigned int nwindows
;
69 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
70 wwarning(_("could not get window list!!"));
73 WMEmptyBag(scr
->stacking_list
);
75 /* verify list integrity */
77 for (i
= 0; i
< nwindows
; i
++) {
78 if (XFindContext(dpy
, windows
[i
], w_global
.context
.stack
, (XPointer
*) & frame
)
85 level
= frame
->stacking
->window_level
;
86 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
88 tmp
->stacking
->above
= frame
;
89 frame
->stacking
->under
= tmp
;
90 frame
->stacking
->above
= NULL
;
91 WMSetInBag(scr
->stacking_list
, level
, frame
);
94 scr
->window_count
= c
;
101 *----------------------------------------------------------------------
103 * Reorders the actual window stacking, so that it has the stacking
104 * order in the internal window stacking lists. It does the opposite
105 * of RemakeStackList().
108 * Windows may be restacked.
109 *----------------------------------------------------------------------
111 void CommitStacking(WScreen
* scr
)
118 nwindows
= scr
->window_count
;
119 windows
= wmalloc(sizeof(Window
) * nwindows
);
122 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
124 windows
[i
++] = tmp
->window
;
125 tmp
= tmp
->stacking
->under
;
128 XRestackWindows(dpy
, windows
, i
);
130 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
134 *----------------------------------------------------------------------
136 * Reestacks windows so that "frame" is under "under".
142 * Changes the stacking order of frame.
143 *----------------------------------------------------------------------
145 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
149 wins
[0] = under
->window
;
150 wins
[1] = frame
->window
;
151 XRestackWindows(dpy
, wins
, 2);
155 *----------------------------------------------------------------------
156 * CommitStackingForWindow--
157 * Reorders the stacking for the specified window, so that it has the
158 * stacking order in the internal window stacking lists.
161 * Windows may be restacked.
162 *----------------------------------------------------------------------
164 void CommitStackingForWindow(WCoreWindow
* frame
)
166 int level
= frame
->stacking
->window_level
;
167 WScreen
*scr
= frame
->screen_ptr
;
169 if (frame
->stacking
->above
== NULL
) {
171 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
172 int i
, last
= above
->stacking
->window_level
;
174 /* find the 1st level above us which has windows in it */
175 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
176 above
= WMGetFromBag(scr
->stacking_list
, i
);
181 if (above
!= frame
&& above
!= NULL
) {
182 while (above
->stacking
->under
)
183 above
= above
->stacking
->under
;
184 moveFrameToUnder(above
, frame
);
186 /* no window above us */
187 XRaiseWindow(dpy
, frame
->window
);
190 moveFrameToUnder(frame
->stacking
->above
, frame
);
195 *----------------------------------------------------------------------
197 * Raises a frame taking the window level into account.
203 * Window stacking order and stacking list are changed.
205 *----------------------------------------------------------------------
207 void wRaiseFrame(WCoreWindow
* frame
)
209 WCoreWindow
*wlist
= frame
;
210 int level
= frame
->stacking
->window_level
;
211 WScreen
*scr
= frame
->screen_ptr
;
214 if (frame
->stacking
->above
== NULL
) {
218 /* insert it on top of other windows on the same level */
219 if (frame
->stacking
->under
)
220 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
221 if (frame
->stacking
->above
)
222 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
224 frame
->stacking
->above
= NULL
;
225 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
226 if (frame
->stacking
->under
) {
227 frame
->stacking
->under
->stacking
->above
= frame
;
229 WMSetInBag(scr
->stacking_list
, level
, frame
);
231 /* raise transients under us from bottom to top
232 * so that the order is kept */
234 wlist
= frame
->stacking
->under
;
235 while (wlist
&& wlist
->stacking
->under
)
236 wlist
= wlist
->stacking
->under
;
237 while (wlist
&& wlist
!= frame
) {
238 if (wlist
->stacking
->child_of
== frame
) {
242 wlist
= wlist
->stacking
->above
;
245 /* try to optimize things a little */
246 if (frame
->stacking
->above
== NULL
) {
248 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
249 int i
, last
= above
->stacking
->window_level
;
251 /* find the 1st level above us which has windows in it */
252 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
253 above
= WMGetFromBag(scr
->stacking_list
, i
);
258 if (above
!= frame
&& above
!= NULL
) {
259 while (above
->stacking
->under
)
260 above
= above
->stacking
->under
;
261 moveFrameToUnder(above
, frame
);
263 /* no window above us */
265 XRaiseWindow(dpy
, frame
->window
);
268 moveFrameToUnder(frame
->stacking
->above
, frame
);
271 notifyStackChange(frame
, "raise");
274 void wRaiseLowerFrame(WCoreWindow
* frame
)
276 if (!frame
->stacking
->above
277 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
281 WCoreWindow
*scan
= frame
->stacking
->above
;
282 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
286 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
287 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
289 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
290 && scan_wwin
->flags
.mapped
) {
294 scan
= scan
->stacking
->above
;
305 void wLowerFrame(WCoreWindow
* frame
)
307 WScreen
*scr
= frame
->screen_ptr
;
308 WCoreWindow
*wlist
= frame
;
309 int level
= frame
->stacking
->window_level
;
311 /* already in bottom */
312 if (wlist
->stacking
->under
== NULL
) {
315 /* can't lower transient below below its owner */
316 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
319 /* remove from the list */
320 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
321 /* it was the top window */
322 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
323 frame
->stacking
->under
->stacking
->above
= NULL
;
325 if (frame
->stacking
->under
)
326 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
327 if (frame
->stacking
->above
)
328 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
330 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
332 /* look for place to put this window */
334 WCoreWindow
*owner
= frame
->stacking
->child_of
;
336 if (owner
!= wlist
) {
337 while (wlist
->stacking
->under
) {
338 /* if this is a transient, it should not be placed under
340 if (owner
== wlist
->stacking
->under
)
342 wlist
= wlist
->stacking
->under
;
346 /* insert under the place found */
347 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 frame
->stacking
->under
= NULL
;
357 if (frame
->stacking
->above
== NULL
) {
359 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
360 int i
, last
= above
->stacking
->window_level
;
362 /* find the 1st level above us which has windows in it */
363 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
364 above
= WMGetFromBag(scr
->stacking_list
, i
);
369 if (above
!= frame
&& above
!= NULL
) {
370 while (above
->stacking
->under
)
371 above
= above
->stacking
->under
;
372 moveFrameToUnder(above
, frame
);
374 /* no window below us */
375 XLowerWindow(dpy
, frame
->window
);
378 moveFrameToUnder(frame
->stacking
->above
, frame
);
381 notifyStackChange(frame
, "lower");
385 *----------------------------------------------------------------------
387 * Inserts the frame in the top of the stacking list. The
388 * stacking precedence is obeyed.
394 * The frame is added to it's screen's window list.
395 *----------------------------------------------------------------------
397 void AddToStackList(WCoreWindow
* frame
)
399 WCoreWindow
*curtop
, *wlist
;
400 int index
= frame
->stacking
->window_level
;
401 WScreen
*scr
= frame
->screen_ptr
;
402 WCoreWindow
*trans
= NULL
;
404 frame
->screen_ptr
->window_count
++;
405 XSaveContext(dpy
, frame
->window
, w_global
.context
.stack
, (XPointer
) frame
);
406 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
408 /* first window in this level */
409 if (curtop
== NULL
) {
410 WMSetInBag(scr
->stacking_list
, index
, frame
);
411 frame
->stacking
->above
= NULL
;
412 frame
->stacking
->under
= NULL
;
417 /* check if this is a transient owner */
420 if (wlist
->stacking
->child_of
== frame
)
422 wlist
= wlist
->stacking
->under
;
424 /* trans will hold the transient in the lowest position
425 * in stacking list */
427 frame
->stacking
->above
= trans
;
429 /* window is owner of a transient.. put it below
430 * the lowest transient */
431 frame
->stacking
->under
= trans
->stacking
->under
;
432 if (trans
->stacking
->under
) {
433 trans
->stacking
->under
->stacking
->above
= frame
;
435 trans
->stacking
->under
= frame
;
437 /* window is not owner of transients.. just put it in the
438 * top of other windows */
439 frame
->stacking
->under
= curtop
;
440 curtop
->stacking
->above
= frame
;
441 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 *----------------------------------------------------------------------
459 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
462 WScreen
*scr
= frame
->screen_ptr
;
465 if (!next
|| frame
->stacking
->under
== next
)
468 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
469 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
471 index
= frame
->stacking
->window_level
;
473 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
475 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
476 if (frame
->stacking
->under
)
477 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
478 if (frame
->stacking
->above
)
479 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
480 if (next
->stacking
->above
)
481 next
->stacking
->above
->stacking
->under
= frame
;
482 frame
->stacking
->under
= next
;
483 frame
->stacking
->above
= next
->stacking
->above
;
484 next
->stacking
->above
= frame
;
486 WMSetInBag(scr
->stacking_list
, index
, frame
);
488 /* try to optimize things a little */
489 if (frame
->stacking
->above
== NULL
) {
490 WCoreWindow
*above
= NULL
;
493 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
494 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
497 while (above
->stacking
->under
)
498 above
= above
->stacking
->under
;
502 XRaiseWindow(dpy
, frame
->window
);
504 moveFrameToUnder(above
, frame
);
507 moveFrameToUnder(frame
->stacking
->above
, frame
);
510 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
514 *----------------------------------------------------------------------
515 * MoveInStackListUnder--
516 * Moves the frame to under "prev".
522 * Stacking order may be changed.
523 * Window level for frame may be changed.
524 *----------------------------------------------------------------------
526 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
530 WScreen
*scr
= frame
->screen_ptr
;
532 if (!prev
|| frame
->stacking
->above
== prev
)
535 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
536 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
538 index
= frame
->stacking
->window_level
;
540 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
542 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
543 if (frame
->stacking
->under
)
544 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
545 if (frame
->stacking
->above
)
546 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
547 if (prev
->stacking
->under
)
548 prev
->stacking
->under
->stacking
->above
= frame
;
549 frame
->stacking
->above
= prev
;
550 frame
->stacking
->under
= prev
->stacking
->under
;
551 prev
->stacking
->under
= frame
;
552 moveFrameToUnder(prev
, frame
);
554 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
557 void RemoveFromStackList(WCoreWindow
* frame
)
559 int index
= frame
->stacking
->window_level
;
561 if (XDeleteContext(dpy
, frame
->window
, w_global
.context
.stack
) == 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
, frame
->stacking
->under
);
573 frame
->screen_ptr
->window_count
--;
575 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
578 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
582 if (frame
->stacking
->window_level
== new_level
)
584 old_level
= frame
->stacking
->window_level
;
586 RemoveFromStackList(frame
);
587 frame
->stacking
->window_level
= new_level
;
588 AddToStackList(frame
);
589 if (old_level
> new_level
) {