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"
38 /*** Global Variables ***/
39 extern XContext wStackContext
;
41 extern WPreferences wPreferences
;
43 static void notifyStackChange(WCoreWindow
* frame
, char *detail
)
45 WWindow
*wwin
= wWindowFor(frame
->window
);
47 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
51 *----------------------------------------------------------------------
53 * Remakes the stacking_list for the screen, getting the real
54 * stacking order from the server and reordering windows that are not
55 * in the correct stacking.
58 * The stacking order list and the actual window stacking
59 * may be changed (corrected)
61 *----------------------------------------------------------------------
63 void RemakeStackList(WScreen
* scr
)
66 unsigned int nwindows
;
73 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
74 wwarning(_("could not get window list!!"));
77 WMEmptyBag(scr
->stacking_list
);
79 /* verify list integrity */
81 for (i
= 0; i
< nwindows
; i
++) {
82 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*) & frame
)
89 level
= frame
->stacking
->window_level
;
90 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
92 tmp
->stacking
->above
= frame
;
93 frame
->stacking
->under
= tmp
;
94 frame
->stacking
->above
= NULL
;
95 WMSetInBag(scr
->stacking_list
, level
, frame
);
98 scr
->window_count
= c
;
105 *----------------------------------------------------------------------
107 * Reorders the actual window stacking, so that it has the stacking
108 * order in the internal window stacking lists. It does the opposite
109 * of RemakeStackList().
112 * Windows may be restacked.
113 *----------------------------------------------------------------------
115 void CommitStacking(WScreen
* scr
)
122 nwindows
= scr
->window_count
;
123 windows
= wmalloc(sizeof(Window
) * nwindows
);
126 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
128 windows
[i
++] = tmp
->window
;
129 tmp
= tmp
->stacking
->under
;
132 XRestackWindows(dpy
, windows
, i
);
134 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
138 *----------------------------------------------------------------------
140 * Reestacks windows so that "frame" is under "under".
146 * Changes the stacking order of frame.
147 *----------------------------------------------------------------------
149 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
153 wins
[0] = under
->window
;
154 wins
[1] = frame
->window
;
155 XRestackWindows(dpy
, wins
, 2);
159 *----------------------------------------------------------------------
160 * CommitStackingForWindow--
161 * Reorders the stacking for the specified window, so that it has the
162 * stacking order in the internal window stacking lists.
165 * Windows may be restacked.
166 *----------------------------------------------------------------------
168 void CommitStackingForWindow(WCoreWindow
* frame
)
170 int level
= frame
->stacking
->window_level
;
171 WScreen
*scr
= frame
->screen_ptr
;
173 if (frame
->stacking
->above
== NULL
) {
175 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
176 int i
, last
= above
->stacking
->window_level
;
178 /* find the 1st level above us which has windows in it */
179 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
180 above
= WMGetFromBag(scr
->stacking_list
, i
);
185 if (above
!= frame
&& above
!= NULL
) {
186 while (above
->stacking
->under
)
187 above
= above
->stacking
->under
;
188 moveFrameToUnder(above
, frame
);
190 /* no window above us */
191 XRaiseWindow(dpy
, frame
->window
);
194 moveFrameToUnder(frame
->stacking
->above
, frame
);
199 *----------------------------------------------------------------------
201 * Raises a frame taking the window level into account.
207 * Window stacking order and stacking list are changed.
209 *----------------------------------------------------------------------
211 void wRaiseFrame(WCoreWindow
* frame
)
213 WCoreWindow
*wlist
= frame
;
214 int level
= frame
->stacking
->window_level
;
215 WScreen
*scr
= frame
->screen_ptr
;
218 if (frame
->stacking
->above
== NULL
) {
222 /* insert it on top of other windows on the same level */
223 if (frame
->stacking
->under
)
224 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
225 if (frame
->stacking
->above
)
226 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
228 frame
->stacking
->above
= NULL
;
229 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
230 if (frame
->stacking
->under
) {
231 frame
->stacking
->under
->stacking
->above
= frame
;
233 WMSetInBag(scr
->stacking_list
, level
, frame
);
235 /* raise transients under us from bottom to top
236 * so that the order is kept */
238 wlist
= frame
->stacking
->under
;
239 while (wlist
&& wlist
->stacking
->under
)
240 wlist
= wlist
->stacking
->under
;
241 while (wlist
&& wlist
!= frame
) {
242 if (wlist
->stacking
->child_of
== frame
) {
246 wlist
= wlist
->stacking
->above
;
249 /* try to optimize things a little */
250 if (frame
->stacking
->above
== NULL
) {
252 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
253 int i
, last
= above
->stacking
->window_level
;
255 /* find the 1st level above us which has windows in it */
256 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
257 above
= WMGetFromBag(scr
->stacking_list
, i
);
262 if (above
!= frame
&& above
!= NULL
) {
263 while (above
->stacking
->under
)
264 above
= above
->stacking
->under
;
265 moveFrameToUnder(above
, frame
);
267 /* no window above us */
269 XRaiseWindow(dpy
, frame
->window
);
272 moveFrameToUnder(frame
->stacking
->above
, frame
);
275 notifyStackChange(frame
, "raise");
278 void wRaiseLowerFrame(WCoreWindow
* frame
)
280 if (!frame
->stacking
->above
281 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
285 WCoreWindow
*scan
= frame
->stacking
->above
;
286 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
290 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
291 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
293 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
294 && scan_wwin
->flags
.mapped
) {
298 scan
= scan
->stacking
->above
;
309 void wLowerFrame(WCoreWindow
* frame
)
311 WScreen
*scr
= frame
->screen_ptr
;
312 WCoreWindow
*wlist
= frame
;
313 int level
= frame
->stacking
->window_level
;
315 /* already in bottom */
316 if (wlist
->stacking
->under
== NULL
) {
319 /* cant lower transient below below its owner */
320 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
323 /* remove from the list */
324 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
325 /* it was the top window */
326 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
327 frame
->stacking
->under
->stacking
->above
= NULL
;
329 if (frame
->stacking
->under
)
330 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
331 if (frame
->stacking
->above
)
332 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
334 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
336 /* look for place to put this window */
338 WCoreWindow
*owner
= frame
->stacking
->child_of
;
340 if (owner
!= wlist
) {
341 while (wlist
->stacking
->under
) {
342 /* if this is a transient, it should not be placed under
344 if (owner
== wlist
->stacking
->under
)
346 wlist
= wlist
->stacking
->under
;
350 /* insert under the place found */
351 frame
->stacking
->above
= wlist
;
353 frame
->stacking
->under
= wlist
->stacking
->under
;
354 if (wlist
->stacking
->under
)
355 wlist
->stacking
->under
->stacking
->above
= frame
;
356 wlist
->stacking
->under
= frame
;
358 frame
->stacking
->under
= NULL
;
361 if (frame
->stacking
->above
== NULL
) {
363 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
364 int i
, last
= above
->stacking
->window_level
;
366 /* find the 1st level above us which has windows in it */
367 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
368 above
= WMGetFromBag(scr
->stacking_list
, i
);
373 if (above
!= frame
&& above
!= NULL
) {
374 while (above
->stacking
->under
)
375 above
= above
->stacking
->under
;
376 moveFrameToUnder(above
, frame
);
378 /* no window below us */
379 XLowerWindow(dpy
, frame
->window
);
382 moveFrameToUnder(frame
->stacking
->above
, frame
);
385 notifyStackChange(frame
, "lower");
389 *----------------------------------------------------------------------
391 * Inserts the frame in the top of the stacking list. The
392 * stacking precedence is obeyed.
398 * The frame is added to it's screen's window list.
399 *----------------------------------------------------------------------
401 void AddToStackList(WCoreWindow
* frame
)
403 WCoreWindow
*curtop
, *wlist
;
404 int index
= frame
->stacking
->window_level
;
405 WScreen
*scr
= frame
->screen_ptr
;
406 WCoreWindow
*trans
= NULL
;
408 frame
->screen_ptr
->window_count
++;
409 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
) frame
);
410 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
412 /* first window in this level */
413 if (curtop
== NULL
) {
414 WMSetInBag(scr
->stacking_list
, index
, frame
);
415 frame
->stacking
->above
= NULL
;
416 frame
->stacking
->under
= NULL
;
421 /* check if this is a transient owner */
424 if (wlist
->stacking
->child_of
== frame
)
426 wlist
= wlist
->stacking
->under
;
428 /* trans will hold the transient in the lowest position
429 * in stacking list */
431 frame
->stacking
->above
= trans
;
433 /* window is owner of a transient.. put it below
434 * the lowest transient */
435 frame
->stacking
->under
= trans
->stacking
->under
;
436 if (trans
->stacking
->under
) {
437 trans
->stacking
->under
->stacking
->above
= frame
;
439 trans
->stacking
->under
= frame
;
441 /* window is not owner of transients.. just put it in the
442 * top of other windows */
443 frame
->stacking
->under
= curtop
;
444 curtop
->stacking
->above
= frame
;
445 WMSetInBag(scr
->stacking_list
, index
, frame
);
451 *----------------------------------------------------------------------
452 * MoveInStackListAbove--
453 * Moves the frame above "next".
459 * Stacking order may be changed.
460 * Window level for frame may be changed.
461 *----------------------------------------------------------------------
463 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
466 WScreen
*scr
= frame
->screen_ptr
;
469 if (!next
|| frame
->stacking
->under
== next
)
472 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
473 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
475 index
= frame
->stacking
->window_level
;
477 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
479 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
480 if (frame
->stacking
->under
)
481 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
482 if (frame
->stacking
->above
)
483 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
484 if (next
->stacking
->above
)
485 next
->stacking
->above
->stacking
->under
= frame
;
486 frame
->stacking
->under
= next
;
487 frame
->stacking
->above
= next
->stacking
->above
;
488 next
->stacking
->above
= frame
;
490 WMSetInBag(scr
->stacking_list
, index
, frame
);
492 /* try to optimize things a little */
493 if (frame
->stacking
->above
== NULL
) {
494 WCoreWindow
*above
= NULL
;
497 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
498 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
501 while (above
->stacking
->under
)
502 above
= above
->stacking
->under
;
506 XRaiseWindow(dpy
, frame
->window
);
508 moveFrameToUnder(above
, frame
);
511 moveFrameToUnder(frame
->stacking
->above
, frame
);
514 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
518 *----------------------------------------------------------------------
519 * MoveInStackListUnder--
520 * Moves the frame to under "prev".
526 * Stacking order may be changed.
527 * Window level for frame may be changed.
528 *----------------------------------------------------------------------
530 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
534 WScreen
*scr
= frame
->screen_ptr
;
536 if (!prev
|| frame
->stacking
->above
== prev
)
539 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
540 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
542 index
= frame
->stacking
->window_level
;
544 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
546 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
547 if (frame
->stacking
->under
)
548 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
549 if (frame
->stacking
->above
)
550 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
551 if (prev
->stacking
->under
)
552 prev
->stacking
->under
->stacking
->above
= frame
;
553 frame
->stacking
->above
= prev
;
554 frame
->stacking
->under
= prev
->stacking
->under
;
555 prev
->stacking
->under
= frame
;
556 moveFrameToUnder(prev
, frame
);
558 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
561 void RemoveFromStackList(WCoreWindow
* frame
)
563 int index
= frame
->stacking
->window_level
;
565 if (XDeleteContext(dpy
, frame
->window
, wStackContext
) == XCNOENT
) {
566 wwarning("RemoveFromStackingList(): window not in list ");
569 /* remove from the window stack list */
570 if (frame
->stacking
->under
)
571 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
572 if (frame
->stacking
->above
)
573 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
574 else /* this was the first window on the list */
575 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
, frame
->stacking
->under
);
577 frame
->screen_ptr
->window_count
--;
579 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
582 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
586 if (frame
->stacking
->window_level
== new_level
)
588 old_level
= frame
->stacking
->window_level
;
590 RemoveFromStackList(frame
);
591 frame
->stacking
->window_level
= new_level
;
592 AddToStackList(frame
);
593 if (old_level
> new_level
) {