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"
38 #include "workspace.h"
41 /*** Global Variables ***/
42 extern XContext wStackContext
;
44 extern WPreferences wPreferences
;
48 notifyStackChange(WCoreWindow
*frame
, char *detail
)
50 WWindow
*wwin
= wWindowFor(frame
->window
);
52 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
57 *----------------------------------------------------------------------
59 * Remakes the stacking_list for the screen, getting the real
60 * stacking order from the server and reordering windows that are not
61 * in the correct stacking.
64 * The stacking order list and the actual window stacking
65 * may be changed (corrected)
67 *----------------------------------------------------------------------
70 RemakeStackList(WScreen
*scr
)
73 unsigned int nwindows
;
80 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
81 wwarning(_("could not get window list!!"));
84 WMEmptyBag(scr
->stacking_list
);
86 /* verify list integrity */
88 for (i
=0; i
<nwindows
; i
++) {
89 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*)&frame
)
95 level
= frame
->stacking
->window_level
;
96 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
98 tmp
->stacking
->above
= frame
;
99 frame
->stacking
->under
= tmp
;
100 frame
->stacking
->above
= NULL
;
101 WMSetInBag(scr
->stacking_list
, level
, frame
);
105 if (c
!=scr
->window_count
) {
106 puts("Found different number of windows than in window lists!!!");
109 scr
->window_count
= c
;
118 *----------------------------------------------------------------------
120 * Reorders the actual window stacking, so that it has the stacking
121 * order in the internal window stacking lists. It does the opposite
122 * of RemakeStackList().
125 * Windows may be restacked.
126 *----------------------------------------------------------------------
129 CommitStacking(WScreen
*scr
)
136 nwindows
= scr
->window_count
;
137 windows
= wmalloc(sizeof(Window
)*nwindows
);
140 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
144 puts("Internal inconsistency! window_count is incorrect!!!");
145 printf("window_count says %i windows\n", nwindows
);
150 windows
[i
++] = tmp
->window
;
151 tmp
= tmp
->stacking
->under
;
154 XRestackWindows(dpy
, windows
, i
);
157 #ifdef VIRTUAL_DESKTOP
158 wWorkspaceRaiseEdge(scr
);
161 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
165 *----------------------------------------------------------------------
167 * Reestacks windows so that "frame" is under "under".
173 * Changes the stacking order of frame.
174 *----------------------------------------------------------------------
177 moveFrameToUnder(WCoreWindow
*under
, WCoreWindow
*frame
)
181 wins
[0] = under
->window
;
182 wins
[1] = frame
->window
;
183 XRestackWindows(dpy
, wins
, 2);
187 *----------------------------------------------------------------------
189 * Raises a frame taking the window level into account.
195 * Window stacking order and stacking list are changed.
197 *----------------------------------------------------------------------
200 wRaiseFrame(WCoreWindow
*frame
)
202 WCoreWindow
*wlist
= frame
;
203 int level
= frame
->stacking
->window_level
;
204 WScreen
*scr
= frame
->screen_ptr
;
207 if (frame
->stacking
->above
== NULL
) {
211 /* insert it on top of other windows on the same level */
212 if (frame
->stacking
->under
)
213 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
214 if (frame
->stacking
->above
)
215 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
217 frame
->stacking
->above
= NULL
;
218 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
219 if (frame
->stacking
->under
) {
220 frame
->stacking
->under
->stacking
->above
= frame
;
222 WMSetInBag(scr
->stacking_list
, level
, frame
);
224 /* raise transients under us from bottom to top
225 * so that the order is kept */
227 wlist
= frame
->stacking
->under
;
228 while (wlist
&& wlist
->stacking
->under
)
229 wlist
= wlist
->stacking
->under
;
230 while (wlist
&& wlist
!=frame
) {
231 if (wlist
->stacking
->child_of
== frame
) {
235 wlist
= wlist
->stacking
->above
;
238 /* try to optimize things a little */
239 if (frame
->stacking
->above
== NULL
) {
241 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
242 int i
, last
= above
->stacking
->window_level
;
244 /* find the 1st level above us which has windows in it */
245 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
246 above
= WMGetFromBag(scr
->stacking_list
, i
);
251 if (above
!= frame
&& above
!= NULL
) {
252 while (above
->stacking
->under
)
253 above
= above
->stacking
->under
;
254 moveFrameToUnder(above
, frame
);
256 /* no window above us */
258 XRaiseWindow(dpy
, frame
->window
);
261 moveFrameToUnder(frame
->stacking
->above
, frame
);
264 notifyStackChange(frame
, "raise");
266 #ifdef VIRTUAL_DESKTOP
267 wWorkspaceRaiseEdge(scr
);
273 wRaiseLowerFrame(WCoreWindow
*frame
)
275 if (!frame
->stacking
->above
276 ||(frame
->stacking
->window_level
277 !=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
;
307 wLowerFrame(WCoreWindow
*frame
)
309 WScreen
*scr
= frame
->screen_ptr
;
310 WCoreWindow
*wlist
=frame
;
311 int level
= frame
->stacking
->window_level
;
313 /* already in bottom */
314 if (wlist
->stacking
->under
== NULL
) {
317 /* cant lower transient below below its owner */
318 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
321 /* remove from the list */
322 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
323 /* it was the top window */
324 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
325 frame
->stacking
->under
->stacking
->above
= NULL
;
327 if (frame
->stacking
->under
)
328 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
329 if (frame
->stacking
->above
)
330 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
332 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
334 /* look for place to put this window */
336 WCoreWindow
*owner
= frame
->stacking
->child_of
;
338 if (owner
!= wlist
) {
339 while (wlist
->stacking
->under
) {
340 /* if this is a transient, it should not be placed under
342 if (owner
== wlist
->stacking
->under
)
344 wlist
= wlist
->stacking
->under
;
348 /* insert under the place found */
349 frame
->stacking
->above
= wlist
;
351 frame
->stacking
->under
= wlist
->stacking
->under
;
352 if (wlist
->stacking
->under
)
353 wlist
->stacking
->under
->stacking
->above
= frame
;
354 wlist
->stacking
->under
= frame
;
356 frame
->stacking
->under
= NULL
;
359 if (frame
->stacking
->above
== NULL
) {
361 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
362 int i
, last
= above
->stacking
->window_level
;
364 /* find the 1st level above us which has windows in it */
365 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
366 above
= WMGetFromBag(scr
->stacking_list
, i
);
371 if (above
!= frame
&& above
!= NULL
) {
372 while (above
->stacking
->under
)
373 above
= above
->stacking
->under
;
374 moveFrameToUnder(above
, frame
);
376 /* no window below us */
377 XLowerWindow(dpy
, frame
->window
);
380 moveFrameToUnder(frame
->stacking
->above
, frame
);
383 notifyStackChange(frame
, "lower");
385 #ifdef VIRTUAL_DESKTOP
386 wWorkspaceRaiseEdge(scr
);
392 *----------------------------------------------------------------------
394 * Inserts the frame in the top of the stacking list. The
395 * stacking precedence is obeyed.
401 * The frame is added to it's screen's window list.
402 *----------------------------------------------------------------------
405 AddToStackList(WCoreWindow
*frame
)
407 WCoreWindow
*curtop
, *wlist
;
408 int index
= frame
->stacking
->window_level
;
409 WScreen
*scr
= frame
->screen_ptr
;
410 WCoreWindow
*trans
= NULL
;
412 frame
->screen_ptr
->window_count
++;
413 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
)frame
);
414 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
416 /* first window in this level */
417 if (curtop
== NULL
) {
418 WMSetInBag(scr
->stacking_list
, index
, frame
);
419 frame
->stacking
->above
= NULL
;
420 frame
->stacking
->under
= NULL
;
425 /* check if this is a transient owner */
428 if (wlist
->stacking
->child_of
== frame
)
430 wlist
= wlist
->stacking
->under
;
432 /* trans will hold the transient in the lowest position
433 * in stacking list */
435 frame
->stacking
->above
= trans
;
437 /* window is owner of a transient.. put it below
438 * the lowest transient */
439 frame
->stacking
->under
= trans
->stacking
->under
;
440 if (trans
->stacking
->under
) {
441 trans
->stacking
->under
->stacking
->above
= frame
;
443 trans
->stacking
->under
= frame
;
445 /* window is not owner of transients.. just put it in the
446 * top of other windows */
447 frame
->stacking
->under
= curtop
;
448 curtop
->stacking
->above
= frame
;
449 WMSetInBag(scr
->stacking_list
, index
, frame
);
456 *----------------------------------------------------------------------
457 * MoveInStackListAbove--
458 * Moves the frame above "next".
464 * Stacking order may be changed.
465 * Window level for frame may be changed.
466 *----------------------------------------------------------------------
469 MoveInStackListAbove(WCoreWindow
*next
, WCoreWindow
*frame
)
472 WScreen
*scr
= frame
->screen_ptr
;
475 if (!next
|| frame
->stacking
->under
== next
)
478 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
479 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
481 index
= frame
->stacking
->window_level
;
483 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
485 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
486 if (frame
->stacking
->under
)
487 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
488 if (frame
->stacking
->above
)
489 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
490 if (next
->stacking
->above
)
491 next
->stacking
->above
->stacking
->under
= frame
;
492 frame
->stacking
->under
= next
;
493 frame
->stacking
->above
= next
->stacking
->above
;
494 next
->stacking
->above
= frame
;
496 WMSetInBag(scr
->stacking_list
, index
, frame
);
498 /* try to optimize things a little */
499 if (frame
->stacking
->above
== NULL
) {
500 WCoreWindow
*above
= NULL
;
503 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+1, &iter
);
505 above
= WMBagNext(scr
->stacking_list
, &iter
)) {
508 while (above
->stacking
->under
)
509 above
= above
->stacking
->under
;
513 XRaiseWindow(dpy
, frame
->window
);
515 moveFrameToUnder(above
, frame
);
518 moveFrameToUnder(frame
->stacking
->above
, frame
);
521 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
523 #ifdef VIRTUAL_DESKTOP
524 wWorkspaceRaiseEdge(scr
);
530 *----------------------------------------------------------------------
531 * MoveInStackListUnder--
532 * Moves the frame to under "prev".
538 * Stacking order may be changed.
539 * Window level for frame may be changed.
540 *----------------------------------------------------------------------
543 MoveInStackListUnder(WCoreWindow
*prev
, WCoreWindow
*frame
)
547 WScreen
*scr
= frame
->screen_ptr
;
549 if (!prev
|| frame
->stacking
->above
== prev
)
552 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
553 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
555 index
= frame
->stacking
->window_level
;
557 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
559 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
560 if (frame
->stacking
->under
)
561 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
562 if (frame
->stacking
->above
)
563 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
564 if (prev
->stacking
->under
)
565 prev
->stacking
->under
->stacking
->above
= frame
;
566 frame
->stacking
->above
= prev
;
567 frame
->stacking
->under
= prev
->stacking
->under
;
568 prev
->stacking
->under
= frame
;
569 moveFrameToUnder(prev
, frame
);
571 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
576 RemoveFromStackList(WCoreWindow
*frame
)
578 int index
= frame
->stacking
->window_level
;
580 if (XDeleteContext(dpy
, frame
->window
, wStackContext
)==XCNOENT
) {
581 wwarning("RemoveFromStackingList(): window not in list ");
584 /* remove from the window stack list */
585 if (frame
->stacking
->under
)
586 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
587 if (frame
->stacking
->above
)
588 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
589 else /* this was the first window on the list */
590 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
,
591 frame
->stacking
->under
);
593 frame
->screen_ptr
->window_count
--;
595 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
600 ChangeStackingLevel(WCoreWindow
*frame
, int new_level
)
604 if (frame
->stacking
->window_level
== new_level
)
606 old_level
= frame
->stacking
->window_level
;
608 RemoveFromStackList(frame
);
609 frame
->stacking
->window_level
= new_level
;
610 AddToStackList(frame
);
611 if (old_level
> new_level
) {