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 static void notifyStackChange(WCoreWindow
* frame
, char *detail
)
43 WWindow
*wwin
= wWindowFor(frame
->window
);
45 WMPostNotificationName(WMNChangedStacking
, wwin
, detail
);
49 *----------------------------------------------------------------------
51 * Remakes the stacking_list for the screen, getting the real
52 * stacking order from the server and reordering windows that are not
53 * in the correct stacking.
56 * The stacking order list and the actual window stacking
57 * may be changed (corrected)
59 *----------------------------------------------------------------------
61 void RemakeStackList(WScreen
* scr
)
64 unsigned int nwindows
;
71 if (!XQueryTree(dpy
, scr
->root_win
, &junkr
, &junkp
, &windows
, &nwindows
)) {
72 wwarning(_("could not get window list!!"));
75 WMEmptyBag(scr
->stacking_list
);
77 /* verify list integrity */
79 for (i
= 0; i
< nwindows
; i
++) {
80 if (XFindContext(dpy
, windows
[i
], wStackContext
, (XPointer
*) & frame
)
87 level
= frame
->stacking
->window_level
;
88 tmp
= WMGetFromBag(scr
->stacking_list
, level
);
90 tmp
->stacking
->above
= frame
;
91 frame
->stacking
->under
= tmp
;
92 frame
->stacking
->above
= NULL
;
93 WMSetInBag(scr
->stacking_list
, level
, frame
);
96 scr
->window_count
= c
;
103 *----------------------------------------------------------------------
105 * Reorders the actual window stacking, so that it has the stacking
106 * order in the internal window stacking lists. It does the opposite
107 * of RemakeStackList().
110 * Windows may be restacked.
111 *----------------------------------------------------------------------
113 void CommitStacking(WScreen
* scr
)
120 nwindows
= scr
->window_count
;
121 windows
= wmalloc(sizeof(Window
) * nwindows
);
124 WM_ETARETI_BAG(scr
->stacking_list
, tmp
, iter
) {
126 windows
[i
++] = tmp
->window
;
127 tmp
= tmp
->stacking
->under
;
130 XRestackWindows(dpy
, windows
, i
);
132 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
136 *----------------------------------------------------------------------
138 * Reestacks windows so that "frame" is under "under".
144 * Changes the stacking order of frame.
145 *----------------------------------------------------------------------
147 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
151 wins
[0] = under
->window
;
152 wins
[1] = frame
->window
;
153 XRestackWindows(dpy
, wins
, 2);
157 *----------------------------------------------------------------------
158 * CommitStackingForWindow--
159 * Reorders the stacking for the specified window, so that it has the
160 * stacking order in the internal window stacking lists.
163 * Windows may be restacked.
164 *----------------------------------------------------------------------
166 void CommitStackingForWindow(WCoreWindow
* frame
)
168 int level
= frame
->stacking
->window_level
;
169 WScreen
*scr
= frame
->screen_ptr
;
171 if (frame
->stacking
->above
== NULL
) {
173 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
174 int i
, last
= above
->stacking
->window_level
;
176 /* find the 1st level above us which has windows in it */
177 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
178 above
= WMGetFromBag(scr
->stacking_list
, i
);
183 if (above
!= frame
&& above
!= NULL
) {
184 while (above
->stacking
->under
)
185 above
= above
->stacking
->under
;
186 moveFrameToUnder(above
, frame
);
188 /* no window above us */
189 XRaiseWindow(dpy
, frame
->window
);
192 moveFrameToUnder(frame
->stacking
->above
, frame
);
197 *----------------------------------------------------------------------
199 * Raises a frame taking the window level into account.
205 * Window stacking order and stacking list are changed.
207 *----------------------------------------------------------------------
209 void wRaiseFrame(WCoreWindow
* frame
)
211 WCoreWindow
*wlist
= frame
;
212 int level
= frame
->stacking
->window_level
;
213 WScreen
*scr
= frame
->screen_ptr
;
216 if (frame
->stacking
->above
== NULL
) {
220 /* insert it on top of other windows on the same level */
221 if (frame
->stacking
->under
)
222 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
223 if (frame
->stacking
->above
)
224 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
226 frame
->stacking
->above
= NULL
;
227 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
228 if (frame
->stacking
->under
) {
229 frame
->stacking
->under
->stacking
->above
= frame
;
231 WMSetInBag(scr
->stacking_list
, level
, frame
);
233 /* raise transients under us from bottom to top
234 * so that the order is kept */
236 wlist
= frame
->stacking
->under
;
237 while (wlist
&& wlist
->stacking
->under
)
238 wlist
= wlist
->stacking
->under
;
239 while (wlist
&& wlist
!= frame
) {
240 if (wlist
->stacking
->child_of
== frame
) {
244 wlist
= wlist
->stacking
->above
;
247 /* try to optimize things a little */
248 if (frame
->stacking
->above
== NULL
) {
250 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
251 int i
, last
= above
->stacking
->window_level
;
253 /* find the 1st level above us which has windows in it */
254 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
255 above
= WMGetFromBag(scr
->stacking_list
, i
);
260 if (above
!= frame
&& above
!= NULL
) {
261 while (above
->stacking
->under
)
262 above
= above
->stacking
->under
;
263 moveFrameToUnder(above
, frame
);
265 /* no window above us */
267 XRaiseWindow(dpy
, frame
->window
);
270 moveFrameToUnder(frame
->stacking
->above
, frame
);
273 notifyStackChange(frame
, "raise");
276 void wRaiseLowerFrame(WCoreWindow
* frame
)
278 if (!frame
->stacking
->above
279 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
283 WCoreWindow
*scan
= frame
->stacking
->above
;
284 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
288 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
289 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
291 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
292 && scan_wwin
->flags
.mapped
) {
296 scan
= scan
->stacking
->above
;
307 void 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");
387 *----------------------------------------------------------------------
389 * Inserts the frame in the top of the stacking list. The
390 * stacking precedence is obeyed.
396 * The frame is added to it's screen's window list.
397 *----------------------------------------------------------------------
399 void AddToStackList(WCoreWindow
* frame
)
401 WCoreWindow
*curtop
, *wlist
;
402 int index
= frame
->stacking
->window_level
;
403 WScreen
*scr
= frame
->screen_ptr
;
404 WCoreWindow
*trans
= NULL
;
406 frame
->screen_ptr
->window_count
++;
407 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
) frame
);
408 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
410 /* first window in this level */
411 if (curtop
== NULL
) {
412 WMSetInBag(scr
->stacking_list
, index
, frame
);
413 frame
->stacking
->above
= NULL
;
414 frame
->stacking
->under
= NULL
;
419 /* check if this is a transient owner */
422 if (wlist
->stacking
->child_of
== frame
)
424 wlist
= wlist
->stacking
->under
;
426 /* trans will hold the transient in the lowest position
427 * in stacking list */
429 frame
->stacking
->above
= trans
;
431 /* window is owner of a transient.. put it below
432 * the lowest transient */
433 frame
->stacking
->under
= trans
->stacking
->under
;
434 if (trans
->stacking
->under
) {
435 trans
->stacking
->under
->stacking
->above
= frame
;
437 trans
->stacking
->under
= frame
;
439 /* window is not owner of transients.. just put it in the
440 * top of other windows */
441 frame
->stacking
->under
= curtop
;
442 curtop
->stacking
->above
= frame
;
443 WMSetInBag(scr
->stacking_list
, index
, frame
);
449 *----------------------------------------------------------------------
450 * MoveInStackListAbove--
451 * Moves the frame above "next".
457 * Stacking order may be changed.
458 * Window level for frame may be changed.
459 *----------------------------------------------------------------------
461 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
464 WScreen
*scr
= frame
->screen_ptr
;
467 if (!next
|| frame
->stacking
->under
== next
)
470 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
471 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
473 index
= frame
->stacking
->window_level
;
475 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
477 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
478 if (frame
->stacking
->under
)
479 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
480 if (frame
->stacking
->above
)
481 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
482 if (next
->stacking
->above
)
483 next
->stacking
->above
->stacking
->under
= frame
;
484 frame
->stacking
->under
= next
;
485 frame
->stacking
->above
= next
->stacking
->above
;
486 next
->stacking
->above
= frame
;
488 WMSetInBag(scr
->stacking_list
, index
, frame
);
490 /* try to optimize things a little */
491 if (frame
->stacking
->above
== NULL
) {
492 WCoreWindow
*above
= NULL
;
495 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
496 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
499 while (above
->stacking
->under
)
500 above
= above
->stacking
->under
;
504 XRaiseWindow(dpy
, frame
->window
);
506 moveFrameToUnder(above
, frame
);
509 moveFrameToUnder(frame
->stacking
->above
, frame
);
512 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
516 *----------------------------------------------------------------------
517 * MoveInStackListUnder--
518 * Moves the frame to under "prev".
524 * Stacking order may be changed.
525 * Window level for frame may be changed.
526 *----------------------------------------------------------------------
528 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
532 WScreen
*scr
= frame
->screen_ptr
;
534 if (!prev
|| frame
->stacking
->above
== prev
)
537 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
538 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
540 index
= frame
->stacking
->window_level
;
542 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
544 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
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 if (prev
->stacking
->under
)
550 prev
->stacking
->under
->stacking
->above
= frame
;
551 frame
->stacking
->above
= prev
;
552 frame
->stacking
->under
= prev
->stacking
->under
;
553 prev
->stacking
->under
= frame
;
554 moveFrameToUnder(prev
, frame
);
556 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
559 void RemoveFromStackList(WCoreWindow
* frame
)
561 int index
= frame
->stacking
->window_level
;
563 if (XDeleteContext(dpy
, frame
->window
, wStackContext
) == XCNOENT
) {
564 wwarning("RemoveFromStackingList(): window not in list ");
567 /* remove from the window stack list */
568 if (frame
->stacking
->under
)
569 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
570 if (frame
->stacking
->above
)
571 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
572 else /* this was the first window on the list */
573 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
, frame
->stacking
->under
);
575 frame
->screen_ptr
->window_count
--;
577 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
580 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
584 if (frame
->stacking
->window_level
== new_level
)
586 old_level
= frame
->stacking
->window_level
;
588 RemoveFromStackList(frame
);
589 frame
->stacking
->window_level
= new_level
;
590 AddToStackList(frame
);
591 if (old_level
> new_level
) {