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
);
101 if (c
!= scr
->window_count
) {
102 puts("Found different number of windows than in window lists!!!");
105 scr
->window_count
= c
;
112 *----------------------------------------------------------------------
114 * Reorders the actual window stacking, so that it has the stacking
115 * order in the internal window stacking lists. It does the opposite
116 * of RemakeStackList().
119 * Windows may be restacked.
120 *----------------------------------------------------------------------
122 void CommitStacking(WScreen
* scr
)
129 nwindows
= scr
->window_count
;
130 windows
= wmalloc(sizeof(Window
) * nwindows
);
133 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
137 puts("Internal inconsistency! window_count is incorrect!!!");
138 printf("window_count says %i windows\n", nwindows
);
143 windows
[i
++] = tmp
->window
;
144 tmp
= tmp
->stacking
->under
;
147 XRestackWindows(dpy
, windows
, i
);
149 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
153 *----------------------------------------------------------------------
155 * Reestacks windows so that "frame" is under "under".
161 * Changes the stacking order of frame.
162 *----------------------------------------------------------------------
164 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
168 wins
[0] = under
->window
;
169 wins
[1] = frame
->window
;
170 XRestackWindows(dpy
, wins
, 2);
174 *----------------------------------------------------------------------
176 * Raises a frame taking the window level into account.
182 * Window stacking order and stacking list are changed.
184 *----------------------------------------------------------------------
186 void wRaiseFrame(WCoreWindow
* frame
)
188 WCoreWindow
*wlist
= frame
;
189 int level
= frame
->stacking
->window_level
;
190 WScreen
*scr
= frame
->screen_ptr
;
193 if (frame
->stacking
->above
== NULL
) {
197 /* insert it on top of other windows on the same level */
198 if (frame
->stacking
->under
)
199 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
200 if (frame
->stacking
->above
)
201 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
203 frame
->stacking
->above
= NULL
;
204 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
205 if (frame
->stacking
->under
) {
206 frame
->stacking
->under
->stacking
->above
= frame
;
208 WMSetInBag(scr
->stacking_list
, level
, frame
);
210 /* raise transients under us from bottom to top
211 * so that the order is kept */
213 wlist
= frame
->stacking
->under
;
214 while (wlist
&& wlist
->stacking
->under
)
215 wlist
= wlist
->stacking
->under
;
216 while (wlist
&& wlist
!= frame
) {
217 if (wlist
->stacking
->child_of
== frame
) {
221 wlist
= wlist
->stacking
->above
;
224 /* try to optimize things a little */
225 if (frame
->stacking
->above
== NULL
) {
227 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
228 int i
, last
= above
->stacking
->window_level
;
230 /* find the 1st level above us which has windows in it */
231 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
232 above
= WMGetFromBag(scr
->stacking_list
, i
);
237 if (above
!= frame
&& above
!= NULL
) {
238 while (above
->stacking
->under
)
239 above
= above
->stacking
->under
;
240 moveFrameToUnder(above
, frame
);
242 /* no window above us */
244 XRaiseWindow(dpy
, frame
->window
);
247 moveFrameToUnder(frame
->stacking
->above
, frame
);
250 notifyStackChange(frame
, "raise");
253 void wRaiseLowerFrame(WCoreWindow
* frame
)
255 if (!frame
->stacking
->above
256 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
260 WCoreWindow
*scan
= frame
->stacking
->above
;
261 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
265 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
266 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
268 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
269 && scan_wwin
->flags
.mapped
) {
273 scan
= scan
->stacking
->above
;
284 void wLowerFrame(WCoreWindow
* frame
)
286 WScreen
*scr
= frame
->screen_ptr
;
287 WCoreWindow
*wlist
= frame
;
288 int level
= frame
->stacking
->window_level
;
290 /* already in bottom */
291 if (wlist
->stacking
->under
== NULL
) {
294 /* cant lower transient below below its owner */
295 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
298 /* remove from the list */
299 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
300 /* it was the top window */
301 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
302 frame
->stacking
->under
->stacking
->above
= NULL
;
304 if (frame
->stacking
->under
)
305 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
306 if (frame
->stacking
->above
)
307 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
309 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
311 /* look for place to put this window */
313 WCoreWindow
*owner
= frame
->stacking
->child_of
;
315 if (owner
!= wlist
) {
316 while (wlist
->stacking
->under
) {
317 /* if this is a transient, it should not be placed under
319 if (owner
== wlist
->stacking
->under
)
321 wlist
= wlist
->stacking
->under
;
325 /* insert under the place found */
326 frame
->stacking
->above
= wlist
;
328 frame
->stacking
->under
= wlist
->stacking
->under
;
329 if (wlist
->stacking
->under
)
330 wlist
->stacking
->under
->stacking
->above
= frame
;
331 wlist
->stacking
->under
= frame
;
333 frame
->stacking
->under
= NULL
;
336 if (frame
->stacking
->above
== NULL
) {
338 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
339 int i
, last
= above
->stacking
->window_level
;
341 /* find the 1st level above us which has windows in it */
342 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
343 above
= WMGetFromBag(scr
->stacking_list
, i
);
348 if (above
!= frame
&& above
!= NULL
) {
349 while (above
->stacking
->under
)
350 above
= above
->stacking
->under
;
351 moveFrameToUnder(above
, frame
);
353 /* no window below us */
354 XLowerWindow(dpy
, frame
->window
);
357 moveFrameToUnder(frame
->stacking
->above
, frame
);
360 notifyStackChange(frame
, "lower");
364 *----------------------------------------------------------------------
366 * Inserts the frame in the top of the stacking list. The
367 * stacking precedence is obeyed.
373 * The frame is added to it's screen's window list.
374 *----------------------------------------------------------------------
376 void AddToStackList(WCoreWindow
* frame
)
378 WCoreWindow
*curtop
, *wlist
;
379 int index
= frame
->stacking
->window_level
;
380 WScreen
*scr
= frame
->screen_ptr
;
381 WCoreWindow
*trans
= NULL
;
383 frame
->screen_ptr
->window_count
++;
384 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
) frame
);
385 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
387 /* first window in this level */
388 if (curtop
== NULL
) {
389 WMSetInBag(scr
->stacking_list
, index
, frame
);
390 frame
->stacking
->above
= NULL
;
391 frame
->stacking
->under
= NULL
;
396 /* check if this is a transient owner */
399 if (wlist
->stacking
->child_of
== frame
)
401 wlist
= wlist
->stacking
->under
;
403 /* trans will hold the transient in the lowest position
404 * in stacking list */
406 frame
->stacking
->above
= trans
;
408 /* window is owner of a transient.. put it below
409 * the lowest transient */
410 frame
->stacking
->under
= trans
->stacking
->under
;
411 if (trans
->stacking
->under
) {
412 trans
->stacking
->under
->stacking
->above
= frame
;
414 trans
->stacking
->under
= frame
;
416 /* window is not owner of transients.. just put it in the
417 * top of other windows */
418 frame
->stacking
->under
= curtop
;
419 curtop
->stacking
->above
= frame
;
420 WMSetInBag(scr
->stacking_list
, index
, frame
);
426 *----------------------------------------------------------------------
427 * MoveInStackListAbove--
428 * Moves the frame above "next".
434 * Stacking order may be changed.
435 * Window level for frame may be changed.
436 *----------------------------------------------------------------------
438 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
441 WScreen
*scr
= frame
->screen_ptr
;
444 if (!next
|| frame
->stacking
->under
== next
)
447 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
448 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
450 index
= frame
->stacking
->window_level
;
452 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
454 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
455 if (frame
->stacking
->under
)
456 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
457 if (frame
->stacking
->above
)
458 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
459 if (next
->stacking
->above
)
460 next
->stacking
->above
->stacking
->under
= frame
;
461 frame
->stacking
->under
= next
;
462 frame
->stacking
->above
= next
->stacking
->above
;
463 next
->stacking
->above
= frame
;
465 WMSetInBag(scr
->stacking_list
, index
, frame
);
467 /* try to optimize things a little */
468 if (frame
->stacking
->above
== NULL
) {
469 WCoreWindow
*above
= NULL
;
472 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
473 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
476 while (above
->stacking
->under
)
477 above
= above
->stacking
->under
;
481 XRaiseWindow(dpy
, frame
->window
);
483 moveFrameToUnder(above
, frame
);
486 moveFrameToUnder(frame
->stacking
->above
, frame
);
489 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
493 *----------------------------------------------------------------------
494 * MoveInStackListUnder--
495 * Moves the frame to under "prev".
501 * Stacking order may be changed.
502 * Window level for frame may be changed.
503 *----------------------------------------------------------------------
505 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
509 WScreen
*scr
= frame
->screen_ptr
;
511 if (!prev
|| frame
->stacking
->above
== prev
)
514 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
515 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
517 index
= frame
->stacking
->window_level
;
519 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
521 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
522 if (frame
->stacking
->under
)
523 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
524 if (frame
->stacking
->above
)
525 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
526 if (prev
->stacking
->under
)
527 prev
->stacking
->under
->stacking
->above
= frame
;
528 frame
->stacking
->above
= prev
;
529 frame
->stacking
->under
= prev
->stacking
->under
;
530 prev
->stacking
->under
= frame
;
531 moveFrameToUnder(prev
, frame
);
533 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
536 void RemoveFromStackList(WCoreWindow
* frame
)
538 int index
= frame
->stacking
->window_level
;
540 if (XDeleteContext(dpy
, frame
->window
, wStackContext
) == XCNOENT
) {
541 wwarning("RemoveFromStackingList(): window not in list ");
544 /* remove from the window stack list */
545 if (frame
->stacking
->under
)
546 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
547 if (frame
->stacking
->above
)
548 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
549 else /* this was the first window on the list */
550 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
, frame
->stacking
->under
);
552 frame
->screen_ptr
->window_count
--;
554 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
557 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
561 if (frame
->stacking
->window_level
== new_level
)
563 old_level
= frame
->stacking
->window_level
;
565 RemoveFromStackList(frame
);
566 frame
->stacking
->window_level
= new_level
;
567 AddToStackList(frame
);
568 if (old_level
> new_level
) {