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
);
150 #ifdef VIRTUAL_DESKTOP
151 wWorkspaceRaiseEdge(scr
);
154 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
158 *----------------------------------------------------------------------
160 * Reestacks windows so that "frame" is under "under".
166 * Changes the stacking order of frame.
167 *----------------------------------------------------------------------
169 static void moveFrameToUnder(WCoreWindow
* under
, WCoreWindow
* frame
)
173 wins
[0] = under
->window
;
174 wins
[1] = frame
->window
;
175 XRestackWindows(dpy
, wins
, 2);
179 *----------------------------------------------------------------------
181 * Raises a frame taking the window level into account.
187 * Window stacking order and stacking list are changed.
189 *----------------------------------------------------------------------
191 void wRaiseFrame(WCoreWindow
* frame
)
193 WCoreWindow
*wlist
= frame
;
194 int level
= frame
->stacking
->window_level
;
195 WScreen
*scr
= frame
->screen_ptr
;
198 if (frame
->stacking
->above
== NULL
) {
202 /* insert it on top of other windows on the same level */
203 if (frame
->stacking
->under
)
204 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
205 if (frame
->stacking
->above
)
206 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
208 frame
->stacking
->above
= NULL
;
209 frame
->stacking
->under
= WMGetFromBag(scr
->stacking_list
, level
);
210 if (frame
->stacking
->under
) {
211 frame
->stacking
->under
->stacking
->above
= frame
;
213 WMSetInBag(scr
->stacking_list
, level
, frame
);
215 /* raise transients under us from bottom to top
216 * so that the order is kept */
218 wlist
= frame
->stacking
->under
;
219 while (wlist
&& wlist
->stacking
->under
)
220 wlist
= wlist
->stacking
->under
;
221 while (wlist
&& wlist
!= frame
) {
222 if (wlist
->stacking
->child_of
== frame
) {
226 wlist
= wlist
->stacking
->above
;
229 /* try to optimize things a little */
230 if (frame
->stacking
->above
== NULL
) {
232 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
233 int i
, last
= above
->stacking
->window_level
;
235 /* find the 1st level above us which has windows in it */
236 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
237 above
= WMGetFromBag(scr
->stacking_list
, i
);
242 if (above
!= frame
&& above
!= NULL
) {
243 while (above
->stacking
->under
)
244 above
= above
->stacking
->under
;
245 moveFrameToUnder(above
, frame
);
247 /* no window above us */
249 XRaiseWindow(dpy
, frame
->window
);
252 moveFrameToUnder(frame
->stacking
->above
, frame
);
255 notifyStackChange(frame
, "raise");
257 #ifdef VIRTUAL_DESKTOP
258 wWorkspaceRaiseEdge(scr
);
262 void wRaiseLowerFrame(WCoreWindow
* frame
)
264 if (!frame
->stacking
->above
265 || (frame
->stacking
->window_level
!= frame
->stacking
->above
->stacking
->window_level
)) {
269 WCoreWindow
*scan
= frame
->stacking
->above
;
270 WWindow
*frame_wwin
= (WWindow
*) frame
->descriptor
.parent
;
274 if (scan
->descriptor
.parent_type
== WCLASS_WINDOW
) {
275 WWindow
*scan_wwin
= (WWindow
*) scan
->descriptor
.parent
;
277 if (wWindowObscuresWindow(scan_wwin
, frame_wwin
)
278 && scan_wwin
->flags
.mapped
) {
282 scan
= scan
->stacking
->above
;
293 void wLowerFrame(WCoreWindow
* frame
)
295 WScreen
*scr
= frame
->screen_ptr
;
296 WCoreWindow
*wlist
= frame
;
297 int level
= frame
->stacking
->window_level
;
299 /* already in bottom */
300 if (wlist
->stacking
->under
== NULL
) {
303 /* cant lower transient below below its owner */
304 if (wlist
->stacking
->under
== wlist
->stacking
->child_of
) {
307 /* remove from the list */
308 if (WMGetFromBag(scr
->stacking_list
, level
) == frame
) {
309 /* it was the top window */
310 WMSetInBag(scr
->stacking_list
, level
, frame
->stacking
->under
);
311 frame
->stacking
->under
->stacking
->above
= NULL
;
313 if (frame
->stacking
->under
)
314 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
315 if (frame
->stacking
->above
)
316 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
318 wlist
= WMGetFromBag(scr
->stacking_list
, level
);
320 /* look for place to put this window */
322 WCoreWindow
*owner
= frame
->stacking
->child_of
;
324 if (owner
!= wlist
) {
325 while (wlist
->stacking
->under
) {
326 /* if this is a transient, it should not be placed under
328 if (owner
== wlist
->stacking
->under
)
330 wlist
= wlist
->stacking
->under
;
334 /* insert under the place found */
335 frame
->stacking
->above
= wlist
;
337 frame
->stacking
->under
= wlist
->stacking
->under
;
338 if (wlist
->stacking
->under
)
339 wlist
->stacking
->under
->stacking
->above
= frame
;
340 wlist
->stacking
->under
= frame
;
342 frame
->stacking
->under
= NULL
;
345 if (frame
->stacking
->above
== NULL
) {
347 WCoreWindow
*above
= WMBagLast(scr
->stacking_list
, &iter
);
348 int i
, last
= above
->stacking
->window_level
;
350 /* find the 1st level above us which has windows in it */
351 for (i
= level
+ 1, above
= NULL
; i
<= last
; i
++) {
352 above
= WMGetFromBag(scr
->stacking_list
, i
);
357 if (above
!= frame
&& above
!= NULL
) {
358 while (above
->stacking
->under
)
359 above
= above
->stacking
->under
;
360 moveFrameToUnder(above
, frame
);
362 /* no window below us */
363 XLowerWindow(dpy
, frame
->window
);
366 moveFrameToUnder(frame
->stacking
->above
, frame
);
369 notifyStackChange(frame
, "lower");
371 #ifdef VIRTUAL_DESKTOP
372 wWorkspaceRaiseEdge(scr
);
377 *----------------------------------------------------------------------
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
386 * The frame is added to it's screen's window list.
387 *----------------------------------------------------------------------
389 void AddToStackList(WCoreWindow
* frame
)
391 WCoreWindow
*curtop
, *wlist
;
392 int index
= frame
->stacking
->window_level
;
393 WScreen
*scr
= frame
->screen_ptr
;
394 WCoreWindow
*trans
= NULL
;
396 frame
->screen_ptr
->window_count
++;
397 XSaveContext(dpy
, frame
->window
, wStackContext
, (XPointer
) frame
);
398 curtop
= WMGetFromBag(scr
->stacking_list
, index
);
400 /* first window in this level */
401 if (curtop
== NULL
) {
402 WMSetInBag(scr
->stacking_list
, index
, frame
);
403 frame
->stacking
->above
= NULL
;
404 frame
->stacking
->under
= NULL
;
409 /* check if this is a transient owner */
412 if (wlist
->stacking
->child_of
== frame
)
414 wlist
= wlist
->stacking
->under
;
416 /* trans will hold the transient in the lowest position
417 * in stacking list */
419 frame
->stacking
->above
= trans
;
421 /* window is owner of a transient.. put it below
422 * the lowest transient */
423 frame
->stacking
->under
= trans
->stacking
->under
;
424 if (trans
->stacking
->under
) {
425 trans
->stacking
->under
->stacking
->above
= frame
;
427 trans
->stacking
->under
= frame
;
429 /* window is not owner of transients.. just put it in the
430 * top of other windows */
431 frame
->stacking
->under
= curtop
;
432 curtop
->stacking
->above
= frame
;
433 WMSetInBag(scr
->stacking_list
, index
, frame
);
439 *----------------------------------------------------------------------
440 * MoveInStackListAbove--
441 * Moves the frame above "next".
447 * Stacking order may be changed.
448 * Window level for frame may be changed.
449 *----------------------------------------------------------------------
451 void MoveInStackListAbove(WCoreWindow
* next
, WCoreWindow
* frame
)
454 WScreen
*scr
= frame
->screen_ptr
;
457 if (!next
|| frame
->stacking
->under
== next
)
460 if (frame
->stacking
->window_level
!= next
->stacking
->window_level
)
461 ChangeStackingLevel(frame
, next
->stacking
->window_level
);
463 index
= frame
->stacking
->window_level
;
465 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
467 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
468 if (frame
->stacking
->under
)
469 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
470 if (frame
->stacking
->above
)
471 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
472 if (next
->stacking
->above
)
473 next
->stacking
->above
->stacking
->under
= frame
;
474 frame
->stacking
->under
= next
;
475 frame
->stacking
->above
= next
->stacking
->above
;
476 next
->stacking
->above
= frame
;
478 WMSetInBag(scr
->stacking_list
, index
, frame
);
480 /* try to optimize things a little */
481 if (frame
->stacking
->above
== NULL
) {
482 WCoreWindow
*above
= NULL
;
485 for (above
= WMBagIteratorAtIndex(scr
->stacking_list
, index
+ 1, &iter
);
486 above
!= NULL
; above
= WMBagNext(scr
->stacking_list
, &iter
)) {
489 while (above
->stacking
->under
)
490 above
= above
->stacking
->under
;
494 XRaiseWindow(dpy
, frame
->window
);
496 moveFrameToUnder(above
, frame
);
499 moveFrameToUnder(frame
->stacking
->above
, frame
);
502 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
504 #ifdef VIRTUAL_DESKTOP
505 wWorkspaceRaiseEdge(scr
);
510 *----------------------------------------------------------------------
511 * MoveInStackListUnder--
512 * Moves the frame to under "prev".
518 * Stacking order may be changed.
519 * Window level for frame may be changed.
520 *----------------------------------------------------------------------
522 void MoveInStackListUnder(WCoreWindow
* prev
, WCoreWindow
* frame
)
526 WScreen
*scr
= frame
->screen_ptr
;
528 if (!prev
|| frame
->stacking
->above
== prev
)
531 if (frame
->stacking
->window_level
!= prev
->stacking
->window_level
)
532 ChangeStackingLevel(frame
, prev
->stacking
->window_level
);
534 index
= frame
->stacking
->window_level
;
536 tmpw
= WMGetFromBag(scr
->stacking_list
, index
);
538 WMSetInBag(scr
->stacking_list
, index
, frame
->stacking
->under
);
539 if (frame
->stacking
->under
)
540 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
541 if (frame
->stacking
->above
)
542 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
543 if (prev
->stacking
->under
)
544 prev
->stacking
->under
->stacking
->above
= frame
;
545 frame
->stacking
->above
= prev
;
546 frame
->stacking
->under
= prev
->stacking
->under
;
547 prev
->stacking
->under
= frame
;
548 moveFrameToUnder(prev
, frame
);
550 WMPostNotificationName(WMNResetStacking
, scr
, NULL
);
553 void RemoveFromStackList(WCoreWindow
* frame
)
555 int index
= frame
->stacking
->window_level
;
557 if (XDeleteContext(dpy
, frame
->window
, wStackContext
) == XCNOENT
) {
558 wwarning("RemoveFromStackingList(): window not in list ");
561 /* remove from the window stack list */
562 if (frame
->stacking
->under
)
563 frame
->stacking
->under
->stacking
->above
= frame
->stacking
->above
;
564 if (frame
->stacking
->above
)
565 frame
->stacking
->above
->stacking
->under
= frame
->stacking
->under
;
566 else /* this was the first window on the list */
567 WMSetInBag(frame
->screen_ptr
->stacking_list
, index
, frame
->stacking
->under
);
569 frame
->screen_ptr
->window_count
--;
571 WMPostNotificationName(WMNResetStacking
, frame
->screen_ptr
, NULL
);
574 void ChangeStackingLevel(WCoreWindow
* frame
, int new_level
)
578 if (frame
->stacking
->window_level
== new_level
)
580 old_level
= frame
->stacking
->window_level
;
582 RemoveFromStackList(frame
);
583 frame
->stacking
->window_level
= new_level
;
584 AddToStackList(frame
);
585 if (old_level
> new_level
) {