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"
40 /*** Global Variables ***/
41 extern XContext wStackContext
;
43 extern WPreferences wPreferences
;
45 static void notifyStackChange(WCoreWindow
* frame
, char *detail
)
47 WWindow
*wwin
= wWindowFor(frame
->window
);
49 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
53 *----------------------------------------------------------------------
55 * Remakes the stacking_list for the screen, getting the real
56 * stacking order from the server and reordering windows that are not
57 * in the correct stacking.
60 * The stacking order list and the actual window stacking
61 * may be changed (corrected)
63 *----------------------------------------------------------------------
65 void RemakeStackList(WScreen
* scr
)
68 unsigned int nwindows
;
75 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
76 wwarning(_("could not get window list!!"));
79 WMEmptyBag(scr
->stacking_list
);
81 /* verify list integrity */
83 for (i
= 0; i
< nwindows
; i
++) {
84 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*) & frame
)
91 level
= frame
->stacking
->window_level
;
92 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
94 tmp
->stacking
->above
= frame
;
95 frame
->stacking
->under
= tmp
;
96 frame
->stacking
->above
= NULL
;
97 WMSetInBag(scr
->stacking_list
, level
, frame
);
100 scr
->window_count
= c
;
107 *----------------------------------------------------------------------
109 * Reorders the actual window stacking, so that it has the stacking
110 * order in the internal window stacking lists. It does the opposite
111 * of RemakeStackList().
114 * Windows may be restacked.
115 *----------------------------------------------------------------------
117 void CommitStacking(WScreen
* scr
)
124 nwindows
= scr
->window_count
;
125 windows
= wmalloc(sizeof(Window
) * nwindows
);
128 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
130 windows
[i
++] = tmp
->window
;
131 tmp
= tmp
->stacking
->under
;
134 XRestackWindows(dpy
, windows
, i
);
136 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
140 *----------------------------------------------------------------------
142 * Reestacks windows so that "frame" is under "under".
148 * Changes the stacking order of frame.
149 *----------------------------------------------------------------------
151 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
155 wins
[0] = under
->window
;
156 wins
[1] = frame
->window
;
157 XRestackWindows(dpy
, wins
, 2);
161 *----------------------------------------------------------------------
163 * Raises a frame taking the window level into account.
169 * Window stacking order and stacking list are changed.
171 *----------------------------------------------------------------------
173 void wRaiseFrame(WCoreWindow
* frame
)
175 WCoreWindow
*wlist
= frame
;
176 int level
= frame
->stacking
->window_level
;
177 WScreen
*scr
= frame
->screen_ptr
;
180 if (frame
->stacking
->above
== NULL
) {
184 /* insert it on top of other windows on the same level */
185 if (frame
->stacking
->under
)
186 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
187 if (frame
->stacking
->above
)
188 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
190 frame
->stacking
->above
= NULL
;
191 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
192 if (frame
->stacking
->under
) {
193 frame
->stacking
->under
->stacking
->above
= frame
;
195 WMSetInBag(scr
->stacking_list
, level
, frame
);
197 /* raise transients under us from bottom to top
198 * so that the order is kept */
200 wlist
= frame
->stacking
->under
;
201 while (wlist
&& wlist
->stacking
->under
)
202 wlist
= wlist
->stacking
->under
;
203 while (wlist
&& wlist
!= frame
) {
204 if (wlist
->stacking
->child_of
== frame
) {
208 wlist
= wlist
->stacking
->above
;
211 /* try to optimize things a little */
212 if (frame
->stacking
->above
== NULL
) {
214 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
215 int i
, last
= above
->stacking
->window_level
;
217 /* find the 1st level above us which has windows in it */
218 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
219 above
= WMGetFromBag(scr
->stacking_list
, i
);
224 if (above
!= frame
&& above
!= NULL
) {
225 while (above
->stacking
->under
)
226 above
= above
->stacking
->under
;
227 moveFrameToUnder(above
, frame
);
229 /* no window above us */
231 XRaiseWindow(dpy
, frame
->window
);
234 moveFrameToUnder(frame
->stacking
->above
, frame
);
237 notifyStackChange(frame
, "raise");
240 void wRaiseLowerFrame(WCoreWindow
* frame
)
242 if (!frame
->stacking
->above
243 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
247 WCoreWindow
*scan
= frame
->stacking
->above
;
248 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
252 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
253 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
255 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
256 && scan_wwin
->flags
.mapped
) {
260 scan
= scan
->stacking
->above
;
271 void wLowerFrame(WCoreWindow
* frame
)
273 WScreen
*scr
= frame
->screen_ptr
;
274 WCoreWindow
*wlist
= frame
;
275 int level
= frame
->stacking
->window_level
;
277 /* already in bottom */
278 if (wlist
->stacking
->under
== NULL
) {
281 /* cant lower transient below below its owner */
282 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
285 /* remove from the list */
286 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
287 /* it was the top window */
288 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
289 frame
->stacking
->under
->stacking
->above
= NULL
;
291 if (frame
->stacking
->under
)
292 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
293 if (frame
->stacking
->above
)
294 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
296 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
298 /* look for place to put this window */
300 WCoreWindow
*owner
= frame
->stacking
->child_of
;
302 if (owner
!= wlist
) {
303 while (wlist
->stacking
->under
) {
304 /* if this is a transient, it should not be placed under
306 if (owner
== wlist
->stacking
->under
)
308 wlist
= wlist
->stacking
->under
;
312 /* insert under the place found */
313 frame
->stacking
->above
= wlist
;
315 frame
->stacking
->under
= wlist
->stacking
->under
;
316 if (wlist
->stacking
->under
)
317 wlist
->stacking
->under
->stacking
->above
= frame
;
318 wlist
->stacking
->under
= frame
;
320 frame
->stacking
->under
= NULL
;
323 if (frame
->stacking
->above
== NULL
) {
325 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
326 int i
, last
= above
->stacking
->window_level
;
328 /* find the 1st level above us which has windows in it */
329 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
330 above
= WMGetFromBag(scr
->stacking_list
, i
);
335 if (above
!= frame
&& above
!= NULL
) {
336 while (above
->stacking
->under
)
337 above
= above
->stacking
->under
;
338 moveFrameToUnder(above
, frame
);
340 /* no window below us */
341 XLowerWindow(dpy
, frame
->window
);
344 moveFrameToUnder(frame
->stacking
->above
, frame
);
347 notifyStackChange(frame
, "lower");
351 *----------------------------------------------------------------------
353 * Inserts the frame in the top of the stacking list. The
354 * stacking precedence is obeyed.
360 * The frame is added to it's screen's window list.
361 *----------------------------------------------------------------------
363 void AddToStackList(WCoreWindow
* frame
)
365 WCoreWindow
*curtop
, *wlist
;
366 int index
= frame
->stacking
->window_level
;
367 WScreen
*scr
= frame
->screen_ptr
;
368 WCoreWindow
*trans
= NULL
;
370 frame
->screen_ptr
->window_count
++;
371 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
) frame
);
372 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
374 /* first window in this level */
375 if (curtop
== NULL
) {
376 WMSetInBag(scr
->stacking_list
, index
, frame
);
377 frame
->stacking
->above
= NULL
;
378 frame
->stacking
->under
= NULL
;
383 /* check if this is a transient owner */
386 if (wlist
->stacking
->child_of
== frame
)
388 wlist
= wlist
->stacking
->under
;
390 /* trans will hold the transient in the lowest position
391 * in stacking list */
393 frame
->stacking
->above
= trans
;
395 /* window is owner of a transient.. put it below
396 * the lowest transient */
397 frame
->stacking
->under
= trans
->stacking
->under
;
398 if (trans
->stacking
->under
) {
399 trans
->stacking
->under
->stacking
->above
= frame
;
401 trans
->stacking
->under
= frame
;
403 /* window is not owner of transients.. just put it in the
404 * top of other windows */
405 frame
->stacking
->under
= curtop
;
406 curtop
->stacking
->above
= frame
;
407 WMSetInBag(scr
->stacking_list
, index
, frame
);
413 *----------------------------------------------------------------------
414 * MoveInStackListAbove--
415 * Moves the frame above "next".
421 * Stacking order may be changed.
422 * Window level for frame may be changed.
423 *----------------------------------------------------------------------
425 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
428 WScreen
*scr
= frame
->screen_ptr
;
431 if (!next
|| frame
->stacking
->under
== next
)
434 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
435 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
437 index
= frame
->stacking
->window_level
;
439 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
441 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
442 if (frame
->stacking
->under
)
443 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
444 if (frame
->stacking
->above
)
445 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
446 if (next
->stacking
->above
)
447 next
->stacking
->above
->stacking
->under
= frame
;
448 frame
->stacking
->under
= next
;
449 frame
->stacking
->above
= next
->stacking
->above
;
450 next
->stacking
->above
= frame
;
452 WMSetInBag(scr
->stacking_list
, index
, frame
);
454 /* try to optimize things a little */
455 if (frame
->stacking
->above
== NULL
) {
456 WCoreWindow
*above
= NULL
;
459 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
460 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
463 while (above
->stacking
->under
)
464 above
= above
->stacking
->under
;
468 XRaiseWindow(dpy
, frame
->window
);
470 moveFrameToUnder(above
, frame
);
473 moveFrameToUnder(frame
->stacking
->above
, frame
);
476 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
480 *----------------------------------------------------------------------
481 * MoveInStackListUnder--
482 * Moves the frame to under "prev".
488 * Stacking order may be changed.
489 * Window level for frame may be changed.
490 *----------------------------------------------------------------------
492 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
496 WScreen
*scr
= frame
->screen_ptr
;
498 if (!prev
|| frame
->stacking
->above
== prev
)
501 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
502 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
504 index
= frame
->stacking
->window_level
;
506 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
508 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
509 if (frame
->stacking
->under
)
510 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
511 if (frame
->stacking
->above
)
512 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
513 if (prev
->stacking
->under
)
514 prev
->stacking
->under
->stacking
->above
= frame
;
515 frame
->stacking
->above
= prev
;
516 frame
->stacking
->under
= prev
->stacking
->under
;
517 prev
->stacking
->under
= frame
;
518 moveFrameToUnder(prev
, frame
);
520 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
523 void RemoveFromStackList(WCoreWindow
* frame
)
525 int index
= frame
->stacking
->window_level
;
527 if (XDeleteContext(dpy
, frame
->window
, wStackContext
) == XCNOENT
) {
528 wwarning("RemoveFromStackingList(): window not in list ");
531 /* remove from the window stack list */
532 if (frame
->stacking
->under
)
533 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
534 if (frame
->stacking
->above
)
535 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
536 else /* this was the first window on the list */
537 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
, frame
->stacking
->under
);
539 frame
->screen_ptr
->window_count
--;
541 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
544 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
548 if (frame
->stacking
->window_level
== new_level
)
550 old_level
= frame
->stacking
->window_level
;
552 RemoveFromStackList(frame
);
553 frame
->stacking
->window_level
= new_level
;
554 AddToStackList(frame
);
555 if (old_level
> new_level
) {