Small fix for wmaker nightly build script 2
[wmaker-crm.git] / src / stacking.c
blobaceb95073923aa1aa0da47af5259d36ab5149e33
1 /*
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,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
31 #include "WindowMaker.h"
32 #include "screen.h"
33 #include "window.h"
34 #include "funcs.h"
35 #include "actions.h"
36 #include "properties.h"
37 #include "stacking.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 *----------------------------------------------------------------------
54 * RemakeStackList--
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.
59 * Side effects:
60 * The stacking order list and the actual window stacking
61 * may be changed (corrected)
63 *----------------------------------------------------------------------
65 void RemakeStackList(WScreen * scr)
67 Window *windows;
68 unsigned int nwindows;
69 Window junkr, junkp;
70 WCoreWindow *frame;
71 WCoreWindow *tmp;
72 int level;
73 int i, c;
75 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
76 wwarning(_("could not get window list!!"));
77 return;
78 } else {
79 WMEmptyBag(scr->stacking_list);
81 /* verify list integrity */
82 c = 0;
83 for (i = 0; i < nwindows; i++) {
84 if (XFindContext(dpy, windows[i], wStackContext, (XPointer *) & frame)
85 == XCNOENT) {
86 continue;
88 if (!frame)
89 continue;
90 c++;
91 level = frame->stacking->window_level;
92 tmp = WMGetFromBag(scr->stacking_list, level);
93 if (tmp)
94 tmp->stacking->above = frame;
95 frame->stacking->under = tmp;
96 frame->stacking->above = NULL;
97 WMSetInBag(scr->stacking_list, level, frame);
99 XFree(windows);
100 #ifdef DEBUG
101 if (c != scr->window_count) {
102 puts("Found different number of windows than in window lists!!!");
104 #endif
105 scr->window_count = c;
108 CommitStacking(scr);
112 *----------------------------------------------------------------------
113 * CommitStacking--
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().
118 * Side effects:
119 * Windows may be restacked.
120 *----------------------------------------------------------------------
122 void CommitStacking(WScreen * scr)
124 WCoreWindow *tmp;
125 int nwindows, i;
126 Window *windows;
127 WMBagIterator iter;
129 nwindows = scr->window_count;
130 windows = wmalloc(sizeof(Window) * nwindows);
132 i = 0;
133 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
134 while (tmp) {
135 #ifdef DEBUG
136 if (i >= nwindows) {
137 puts("Internal inconsistency! window_count is incorrect!!!");
138 printf("window_count says %i windows\n", nwindows);
139 wfree(windows);
140 return;
142 #endif
143 windows[i++] = tmp->window;
144 tmp = tmp->stacking->under;
147 XRestackWindows(dpy, windows, i);
148 wfree(windows);
149 WMPostNotificationName(WMNResetStacking, scr, NULL);
153 *----------------------------------------------------------------------
154 * moveFrameToUnder--
155 * Reestacks windows so that "frame" is under "under".
157 * Returns:
158 * None
160 * Side effects:
161 * Changes the stacking order of frame.
162 *----------------------------------------------------------------------
164 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
166 Window wins[2];
168 wins[0] = under->window;
169 wins[1] = frame->window;
170 XRestackWindows(dpy, wins, 2);
174 *----------------------------------------------------------------------
175 * wRaiseFrame--
176 * Raises a frame taking the window level into account.
178 * Returns:
179 * None
181 * Side effects:
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;
192 /* already on top */
193 if (frame->stacking->above == NULL) {
194 return;
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 */
212 again:
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) {
218 wRaiseFrame(wlist);
219 goto again;
221 wlist = wlist->stacking->above;
224 /* try to optimize things a little */
225 if (frame->stacking->above == NULL) {
226 WMBagIterator iter;
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);
233 if (above != NULL)
234 break;
237 if (above != frame && above != NULL) {
238 while (above->stacking->under)
239 above = above->stacking->under;
240 moveFrameToUnder(above, frame);
241 } else {
242 /* no window above us */
243 above = NULL;
244 XRaiseWindow(dpy, frame->window);
246 } else {
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)) {
258 wLowerFrame(frame);
259 } else {
260 WCoreWindow *scan = frame->stacking->above;
261 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
263 while (scan) {
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) {
270 break;
273 scan = scan->stacking->above;
276 if (scan) {
277 wRaiseFrame(frame);
278 } else {
279 wLowerFrame(frame);
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) {
292 return;
294 /* cant lower transient below below its owner */
295 if (wlist->stacking->under == wlist->stacking->child_of) {
296 return;
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;
303 } else {
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 */
312 if (wlist) {
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
318 * it's owner */
319 if (owner == wlist->stacking->under)
320 break;
321 wlist = wlist->stacking->under;
325 /* insert under the place found */
326 frame->stacking->above = wlist;
327 if (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;
332 } else {
333 frame->stacking->under = NULL;
336 if (frame->stacking->above == NULL) {
337 WMBagIterator iter;
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);
344 if (above != NULL)
345 break;
348 if (above != frame && above != NULL) {
349 while (above->stacking->under)
350 above = above->stacking->under;
351 moveFrameToUnder(above, frame);
352 } else {
353 /* no window below us */
354 XLowerWindow(dpy, frame->window);
356 } else {
357 moveFrameToUnder(frame->stacking->above, frame);
360 notifyStackChange(frame, "lower");
364 *----------------------------------------------------------------------
365 * AddToStackList--
366 * Inserts the frame in the top of the stacking list. The
367 * stacking precedence is obeyed.
369 * Returns:
370 * None
372 * Side effects:
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;
392 CommitStacking(scr);
393 return;
396 /* check if this is a transient owner */
397 wlist = curtop;
398 while (wlist) {
399 if (wlist->stacking->child_of == frame)
400 trans = wlist;
401 wlist = wlist->stacking->under;
403 /* trans will hold the transient in the lowest position
404 * in stacking list */
406 frame->stacking->above = trans;
407 if (trans != NULL) {
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;
415 } else {
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);
422 CommitStacking(scr);
426 *----------------------------------------------------------------------
427 * MoveInStackListAbove--
428 * Moves the frame above "next".
430 * Returns:
431 * None
433 * Side effects:
434 * Stacking order may be changed.
435 * Window level for frame may be changed.
436 *----------------------------------------------------------------------
438 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
440 WCoreWindow *tmpw;
441 WScreen *scr = frame->screen_ptr;
442 int index;
444 if (!next || frame->stacking->under == next)
445 return;
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);
453 if (tmpw == frame)
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;
464 if (tmpw == next)
465 WMSetInBag(scr->stacking_list, index, frame);
467 /* try to optimize things a little */
468 if (frame->stacking->above == NULL) {
469 WCoreWindow *above = NULL;
470 WMBagIterator iter;
472 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
473 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
475 /* can't optimize */
476 while (above->stacking->under)
477 above = above->stacking->under;
478 break;
480 if (above == NULL) {
481 XRaiseWindow(dpy, frame->window);
482 } else {
483 moveFrameToUnder(above, frame);
485 } else {
486 moveFrameToUnder(frame->stacking->above, frame);
489 WMPostNotificationName(WMNResetStacking, scr, NULL);
493 *----------------------------------------------------------------------
494 * MoveInStackListUnder--
495 * Moves the frame to under "prev".
497 * Returns:
498 * None
500 * Side effects:
501 * Stacking order may be changed.
502 * Window level for frame may be changed.
503 *----------------------------------------------------------------------
505 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
507 WCoreWindow *tmpw;
508 int index;
509 WScreen *scr = frame->screen_ptr;
511 if (!prev || frame->stacking->above == prev)
512 return;
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);
520 if (tmpw == frame)
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 ");
542 return;
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)
559 int old_level;
561 if (frame->stacking->window_level == new_level)
562 return;
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) {
569 wRaiseFrame(frame);
570 } else {
571 wLowerFrame(frame);