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
;
47 static void notifyStackChange(WCoreWindow
*frame
, char *detail
)
49 WWindow
*wwin
= wWindowFor(frame
->window
);
51 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
56 *----------------------------------------------------------------------
58 * Remakes the stacking_list for the screen, getting the real
59 * stacking order from the server and reordering windows that are not
60 * in the correct stacking.
63 * The stacking order list and the actual window stacking
64 * may be changed (corrected)
66 *----------------------------------------------------------------------
69 RemakeStackList(WScreen
*scr
)
72 unsigned int nwindows
;
79 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
80 wwarning(_("could not get window list!!"));
83 WMEmptyBag(scr
->stacking_list
);
85 /* verify list integrity */
87 for (i
=0; i
<nwindows
; i
++) {
88 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*)&frame
)
94 level
= frame
->stacking
->window_level
;
95 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
97 tmp
->stacking
->above
= frame
;
98 frame
->stacking
->under
= tmp
;
99 frame
->stacking
->above
= NULL
;
100 WMSetInBag(scr
->stacking_list
, level
, frame
);
104 if (c
!=scr
->window_count
) {
105 puts("Found different number of windows than in window lists!!!");
108 scr
->window_count
= c
;
117 *----------------------------------------------------------------------
119 * Reorders the actual window stacking, so that it has the stacking
120 * order in the internal window stacking lists. It does the opposite
121 * of RemakeStackList().
124 * Windows may be restacked.
125 *----------------------------------------------------------------------
128 CommitStacking(WScreen
*scr
)
135 nwindows
= scr
->window_count
;
136 windows
= wmalloc(sizeof(Window
)*nwindows
);
139 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
143 puts("Internal inconsistency! window_count is incorrect!!!");
144 printf("window_count says %i windows\n", nwindows
);
149 windows
[i
++] = tmp
->window
;
150 tmp
= tmp
->stacking
->under
;
153 XRestackWindows(dpy
, windows
, i
);
156 #ifdef VIRTUAL_DESKTOP
157 wWorkspaceRaiseEdge(scr
);
160 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
164 *----------------------------------------------------------------------
166 * Reestacks windows so that "frame" is under "under".
172 * Changes the stacking order of frame.
173 *----------------------------------------------------------------------
176 moveFrameToUnder(WCoreWindow
*under
, WCoreWindow
*frame
)
180 wins
[0] = under
->window
;
181 wins
[1] = frame
->window
;
182 XRestackWindows(dpy
, wins
, 2);
186 *----------------------------------------------------------------------
188 * Raises a frame taking the window level into account.
194 * Window stacking order and stacking list are changed.
196 *----------------------------------------------------------------------
199 wRaiseFrame(WCoreWindow
*frame
)
201 WCoreWindow
*wlist
= frame
;
202 int level
= frame
->stacking
->window_level
;
203 WScreen
*scr
= frame
->screen_ptr
;
206 if (frame
->stacking
->above
== NULL
) {
210 /* insert it on top of other windows on the same level */
211 if (frame
->stacking
->under
)
212 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
213 if (frame
->stacking
->above
)
214 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
216 frame
->stacking
->above
= NULL
;
217 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
218 if (frame
->stacking
->under
) {
219 frame
->stacking
->under
->stacking
->above
= frame
;
221 WMSetInBag(scr
->stacking_list
, level
, frame
);
223 /* raise transients under us from bottom to top
224 * so that the order is kept */
226 wlist
= frame
->stacking
->under
;
227 while (wlist
&& wlist
->stacking
->under
)
228 wlist
= wlist
->stacking
->under
;
229 while (wlist
&& wlist
!=frame
) {
230 if (wlist
->stacking
->child_of
== frame
) {
234 wlist
= wlist
->stacking
->above
;
237 /* try to optimize things a little */
238 if (frame
->stacking
->above
== NULL
) {
240 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
241 int i
, last
= above
->stacking
->window_level
;
243 /* find the 1st level above us which has windows in it */
244 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
245 above
= WMGetFromBag(scr
->stacking_list
, i
);
250 if (above
!= frame
&& above
!= NULL
) {
251 while (above
->stacking
->under
)
252 above
= above
->stacking
->under
;
253 moveFrameToUnder(above
, frame
);
255 /* no window above us */
257 XRaiseWindow(dpy
, frame
->window
);
260 moveFrameToUnder(frame
->stacking
->above
, frame
);
263 notifyStackChange(frame
, "raise");
265 #ifdef VIRTUAL_DESKTOP
266 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
;
350 frame
->stacking
->under
= wlist
->stacking
->under
;
351 if (wlist
->stacking
->under
)
352 wlist
->stacking
->under
->stacking
->above
= frame
;
353 wlist
->stacking
->under
= frame
;
355 frame
->stacking
->under
= NULL
;
358 if (frame
->stacking
->above
== NULL
) {
360 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
361 int i
, last
= above
->stacking
->window_level
;
363 /* find the 1st level above us which has windows in it */
364 for (i
= level
+1, above
= NULL
; i
<= last
; i
++) {
365 above
= WMGetFromBag(scr
->stacking_list
, i
);
370 if (above
!= frame
&& above
!= NULL
) {
371 while (above
->stacking
->under
)
372 above
= above
->stacking
->under
;
373 moveFrameToUnder(above
, frame
);
375 /* no window below us */
376 XLowerWindow(dpy
, frame
->window
);
379 moveFrameToUnder(frame
->stacking
->above
, frame
);
382 notifyStackChange(frame
, "lower");
384 #ifdef VIRTUAL_DESKTOP
385 wWorkspaceRaiseEdge(scr
);
391 *----------------------------------------------------------------------
393 * Inserts the frame in the top of the stacking list. The
394 * stacking precedence is obeyed.
400 * The frame is added to it's screen's window list.
401 *----------------------------------------------------------------------
404 AddToStackList(WCoreWindow
*frame
)
406 WCoreWindow
*curtop
, *wlist
;
407 int index
= frame
->stacking
->window_level
;
408 WScreen
*scr
= frame
->screen_ptr
;
409 WCoreWindow
*trans
= NULL
;
411 frame
->screen_ptr
->window_count
++;
412 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
)frame
);
413 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
415 /* first window in this level */
416 if (curtop
== NULL
) {
417 WMSetInBag(scr
->stacking_list
, index
, frame
);
418 frame
->stacking
->above
= NULL
;
419 frame
->stacking
->under
= NULL
;
424 /* check if this is a transient owner */
427 if (wlist
->stacking
->child_of
== frame
)
429 wlist
= wlist
->stacking
->under
;
431 /* trans will hold the transient in the lowest position
432 * in stacking list */
434 frame
->stacking
->above
= trans
;
436 /* window is owner of a transient.. put it below
437 * the lowest transient */
438 frame
->stacking
->under
= trans
->stacking
->under
;
439 if (trans
->stacking
->under
) {
440 trans
->stacking
->under
->stacking
->above
= frame
;
442 trans
->stacking
->under
= frame
;
444 /* window is not owner of transients.. just put it in the
445 * top of other windows */
446 frame
->stacking
->under
= curtop
;
447 curtop
->stacking
->above
= frame
;
448 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
);
520 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
522 #ifdef VIRTUAL_DESKTOP
523 wWorkspaceRaiseEdge(scr
);
529 *----------------------------------------------------------------------
530 * MoveInStackListUnder--
531 * Moves the frame to under "prev".
537 * Stacking order may be changed.
538 * Window level for frame may be changed.
539 *----------------------------------------------------------------------
542 MoveInStackListUnder(WCoreWindow
*prev
, WCoreWindow
*frame
)
546 WScreen
*scr
= frame
->screen_ptr
;
548 if (!prev
|| frame
->stacking
->above
== prev
)
551 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
552 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
554 index
= frame
->stacking
->window_level
;
556 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
558 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
559 if (frame
->stacking
->under
)
560 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
561 if (frame
->stacking
->above
)
562 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
563 if (prev
->stacking
->under
)
564 prev
->stacking
->under
->stacking
->above
= frame
;
565 frame
->stacking
->above
= prev
;
566 frame
->stacking
->under
= prev
->stacking
->under
;
567 prev
->stacking
->under
= frame
;
568 moveFrameToUnder(prev
, frame
);
570 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
575 RemoveFromStackList(WCoreWindow
*frame
)
577 int index
= frame
->stacking
->window_level
;
579 if (XDeleteContext(dpy
, frame
->window
, wStackContext
)==XCNOENT
) {
580 wwarning("RemoveFromStackingList(): window not in list ");
583 /* remove from the window stack list */
584 if (frame
->stacking
->under
)
585 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
586 if (frame
->stacking
->above
)
587 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
588 else /* this was the first window on the list */
589 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
,
590 frame
->stacking
->under
);
592 frame
->screen_ptr
->window_count
--;
594 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
599 ChangeStackingLevel(WCoreWindow
*frame
, int new_level
)
603 if (frame
->stacking
->window_level
== new_level
)
605 old_level
= frame
->stacking
->window_level
;
607 RemoveFromStackList(frame
);
608 frame
->stacking
->window_level
= new_level
;
609 AddToStackList(frame
);
610 if (old_level
> new_level
) {