begin wmspec stuff
[wmaker-crm.git] / src / stacking.c
blob38f8f501d8f08d30169770c9cff595b59ecc3e64
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 Dan Pascu
6 *
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"
40 /*** Global Variables ***/
41 extern XContext wStackContext;
43 extern WPreferences wPreferences;
46 static void notifyStackChange(WCoreWindow *frame, char *detail)
48 WWindow *wwin = wWindowFor(frame->window);
50 WMPostNotificationName(WMNChangedStacking, wwin, detail);
55 *----------------------------------------------------------------------
56 * RemakeStackList--
57 * Remakes the stacking_list for the screen, getting the real
58 * stacking order from the server and reordering windows that are not
59 * in the correct stacking.
61 * Side effects:
62 * The stacking order list and the actual window stacking
63 * may be changed (corrected)
65 *----------------------------------------------------------------------
67 void
68 RemakeStackList(WScreen *scr)
70 Window *windows;
71 unsigned int nwindows;
72 Window junkr, junkp;
73 WCoreWindow *frame;
74 WCoreWindow *tmp;
75 int level;
76 int i, c;
78 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
79 wwarning(_("could not get window list!!"));
80 return;
81 } else {
82 WMEmptyBag(scr->stacking_list);
84 /* verify list integrity */
85 c=0;
86 for (i=0; i<nwindows; i++) {
87 if (XFindContext(dpy, windows[i], wStackContext, (XPointer*)&frame)
88 ==XCNOENT) {
89 continue;
91 if (!frame) continue;
92 c++;
93 level = frame->stacking->window_level;
94 tmp = WMGetFromBag(scr->stacking_list, level);
95 if (tmp)
96 tmp->stacking->above = frame;
97 frame->stacking->under = tmp;
98 frame->stacking->above = NULL;
99 WMSetInBag(scr->stacking_list, level, frame);
101 XFree(windows);
102 #ifdef DEBUG
103 if (c!=scr->window_count) {
104 puts("Found different number of windows than in window lists!!!");
106 #endif
107 scr->window_count = c;
110 CommitStacking(scr);
116 *----------------------------------------------------------------------
117 * CommitStacking--
118 * Reorders the actual window stacking, so that it has the stacking
119 * order in the internal window stacking lists. It does the opposite
120 * of RemakeStackList().
122 * Side effects:
123 * Windows may be restacked.
124 *----------------------------------------------------------------------
126 void
127 CommitStacking(WScreen *scr)
129 WCoreWindow *tmp;
130 int nwindows, i;
131 Window *windows;
132 WMBagIterator iter;
134 nwindows = scr->window_count;
135 windows = wmalloc(sizeof(Window)*nwindows);
137 i = 0;
138 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
139 while (tmp) {
140 #ifdef DEBUG
141 if (i>=nwindows) {
142 puts("Internal inconsistency! window_count is incorrect!!!");
143 printf("window_count says %i windows\n", nwindows);
144 wfree(windows);
145 return;
147 #endif
148 windows[i++] = tmp->window;
149 tmp = tmp->stacking->under;
152 XRestackWindows(dpy, windows, i);
153 wfree(windows);
156 WMPostNotificationName(WMNResetStacking, scr, NULL);
160 *----------------------------------------------------------------------
161 * moveFrameToUnder--
162 * Reestacks windows so that "frame" is under "under".
164 * Returns:
165 * None
167 * Side effects:
168 * Changes the stacking order of frame.
169 *----------------------------------------------------------------------
171 static void
172 moveFrameToUnder(WCoreWindow *under, WCoreWindow *frame)
174 Window wins[2];
176 wins[0] = under->window;
177 wins[1] = frame->window;
178 XRestackWindows(dpy, wins, 2);
182 *----------------------------------------------------------------------
183 * wRaiseFrame--
184 * Raises a frame taking the window level into account.
186 * Returns:
187 * None
189 * Side effects:
190 * Window stacking order and stacking list are changed.
192 *----------------------------------------------------------------------
194 void
195 wRaiseFrame(WCoreWindow *frame)
197 WCoreWindow *wlist = frame;
198 int level = frame->stacking->window_level;
199 WScreen *scr = frame->screen_ptr;
201 /* already on top */
202 if (frame->stacking->above == NULL) {
203 return;
206 /* insert it on top of other windows on the same level */
207 if (frame->stacking->under)
208 frame->stacking->under->stacking->above = frame->stacking->above;
209 if (frame->stacking->above)
210 frame->stacking->above->stacking->under = frame->stacking->under;
212 frame->stacking->above = NULL;
213 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
214 frame->stacking->under->stacking->above = frame;
215 WMSetInBag(scr->stacking_list, level, frame);
217 /* raise transients under us from bottom to top
218 * so that the order is kept */
219 again:
220 wlist = frame->stacking->under;
221 while (wlist && wlist->stacking->under)
222 wlist = wlist->stacking->under;
223 while (wlist && wlist!=frame) {
224 if (wlist->stacking->child_of == frame) {
225 wRaiseFrame(wlist);
226 goto again;
228 wlist = wlist->stacking->above;
231 /* try to optimize things a little */
232 if (frame->stacking->above == NULL) {
233 WMBagIterator iter;
234 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
235 int i, last = above->stacking->window_level;
237 /* find the 1st level above us which has windows in it */
238 for (i = level+1, above = NULL; i <= last; i++) {
239 above = WMGetFromBag(scr->stacking_list, i);
240 if (above != NULL)
241 break;
244 if (above != frame && above != NULL) {
245 while (above->stacking->under)
246 above = above->stacking->under;
247 moveFrameToUnder(above, frame);
248 } else {
249 /* no window above us */
250 above = NULL;
251 XRaiseWindow(dpy, frame->window);
253 } else {
254 moveFrameToUnder(frame->stacking->above, frame);
257 notifyStackChange(frame, "raise");
259 #ifdef VIRTUAL_DESKTOP
260 wWorkspaceRaiseEdge(scr);
261 #endif
265 void
266 wRaiseLowerFrame(WCoreWindow *frame)
268 if (!frame->stacking->above
269 ||(frame->stacking->window_level
270 !=frame->stacking->above->stacking->window_level)) {
272 wLowerFrame(frame);
273 } else {
274 WCoreWindow *scan = frame->stacking->above;
275 WWindow *frame_wwin = (WWindow*) frame->descriptor.parent;
277 while (scan) {
279 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
280 WWindow *scan_wwin = (WWindow*)scan->descriptor.parent;
282 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
283 && scan_wwin->flags.mapped) {
284 break;
287 scan = scan->stacking->above;
290 if (scan) {
291 wRaiseFrame(frame);
292 } else {
293 wLowerFrame(frame);
299 void
300 wLowerFrame(WCoreWindow *frame)
302 WScreen *scr = frame->screen_ptr;
303 WCoreWindow *wlist=frame;
304 int level = frame->stacking->window_level;
306 /* already in bottom */
307 if (wlist->stacking->under == NULL) {
308 return;
310 /* cant lower transient below below its owner */
311 if (wlist->stacking->under == wlist->stacking->child_of) {
312 return;
314 /* remove from the list */
315 if (WMGetFromBag(scr->stacking_list, level) == frame) {
316 /* it was the top window */
317 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
318 frame->stacking->under->stacking->above = NULL;
319 } else {
320 if (frame->stacking->under)
321 frame->stacking->under->stacking->above = frame->stacking->above;
322 if (frame->stacking->above)
323 frame->stacking->above->stacking->under = frame->stacking->under;
325 wlist = WMGetFromBag(scr->stacking_list, level);
327 /* look for place to put this window */
329 WCoreWindow *owner = frame->stacking->child_of;
331 if (owner != wlist) {
332 while (wlist->stacking->under) {
333 /* if this is a transient, it should not be placed under
334 * it's owner */
335 if (owner == wlist->stacking->under)
336 break;
337 wlist = wlist->stacking->under;
341 /* insert under the place found */
342 frame->stacking->above = wlist;
343 frame->stacking->under = wlist->stacking->under;
344 if (wlist->stacking->under)
345 wlist->stacking->under->stacking->above = frame;
346 wlist->stacking->under = frame;
348 if (frame->stacking->above == NULL) {
349 WMBagIterator iter;
350 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
351 int i, last = above->stacking->window_level;
353 /* find the 1st level above us which has windows in it */
354 for (i = level+1, above = NULL; i <= last; i++) {
355 above = WMGetFromBag(scr->stacking_list, i);
356 if (above != NULL)
357 break;
360 if (above != frame && above != NULL) {
361 while (above->stacking->under)
362 above = above->stacking->under;
363 moveFrameToUnder(above, frame);
364 } else {
365 /* no window below us */
366 XLowerWindow(dpy, frame->window);
368 } else {
369 moveFrameToUnder(frame->stacking->above, frame);
372 notifyStackChange(frame, "lower");
377 *----------------------------------------------------------------------
378 * AddToStackList--
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
382 * Returns:
383 * None
385 * Side effects:
386 * The frame is added to it's screen's window list.
387 *----------------------------------------------------------------------
389 void
390 AddToStackList(WCoreWindow *frame)
392 WCoreWindow *curtop, *wlist;
393 int index = frame->stacking->window_level;
394 WScreen *scr = frame->screen_ptr;
395 WCoreWindow *trans = NULL;
397 frame->screen_ptr->window_count++;
398 XSaveContext(dpy, frame->window, wStackContext, (XPointer)frame);
399 curtop = WMGetFromBag(scr->stacking_list, index);
401 /* first window in this level */
402 if (curtop == NULL) {
403 WMSetInBag(scr->stacking_list, index, frame);
404 frame->stacking->above = NULL;
405 frame->stacking->under = NULL;
406 CommitStacking(scr);
407 return;
410 /* check if this is a transient owner */
411 wlist = curtop;
412 while (wlist) {
413 if (wlist->stacking->child_of == frame)
414 trans = wlist;
415 wlist = wlist->stacking->under;
417 /* trans will hold the transient in the lowest position
418 * in stacking list */
420 frame->stacking->above = trans;
421 if (trans != NULL) {
422 /* window is owner of a transient.. put it below
423 * the lowest transient */
424 frame->stacking->under = trans->stacking->under;
425 if (trans->stacking->under) {
426 trans->stacking->under->stacking->above = frame;
428 trans->stacking->under = frame;
429 } else {
430 /* window is not owner of transients.. just put it in the
431 * top of other windows */
432 frame->stacking->under = curtop;
433 curtop->stacking->above = frame;
434 WMSetInBag(scr->stacking_list, index, frame);
436 CommitStacking(scr);
441 *----------------------------------------------------------------------
442 * MoveInStackListAbove--
443 * Moves the frame above "next".
445 * Returns:
446 * None
448 * Side effects:
449 * Stacking order may be changed.
450 * Window level for frame may be changed.
451 *----------------------------------------------------------------------
453 void
454 MoveInStackListAbove(WCoreWindow *next, WCoreWindow *frame)
456 WCoreWindow *tmpw;
457 WScreen *scr = frame->screen_ptr;
458 int index;
460 if (!next || frame->stacking->under == next)
461 return;
463 if (frame->stacking->window_level != next->stacking->window_level)
464 ChangeStackingLevel(frame, next->stacking->window_level);
466 index = frame->stacking->window_level;
468 tmpw = WMGetFromBag(scr->stacking_list, index);
469 if (tmpw == frame)
470 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
471 if (frame->stacking->under)
472 frame->stacking->under->stacking->above = frame->stacking->above;
473 if (frame->stacking->above)
474 frame->stacking->above->stacking->under = frame->stacking->under;
475 if (next->stacking->above)
476 next->stacking->above->stacking->under = frame;
477 frame->stacking->under = next;
478 frame->stacking->above = next->stacking->above;
479 next->stacking->above = frame;
480 if (tmpw == next)
481 WMSetInBag(scr->stacking_list, index, frame);
483 /* try to optimize things a little */
484 if (frame->stacking->above == NULL) {
485 WCoreWindow *above = NULL;
486 WMBagIterator iter;
488 for (above = WMBagIteratorAtIndex(scr->stacking_list, index+1, &iter);
489 above != NULL;
490 above = WMBagNext(scr->stacking_list, &iter)) {
492 /* can't optimize */
493 while (above->stacking->under)
494 above = above->stacking->under;
495 break;
497 if (above == NULL) {
498 XRaiseWindow(dpy, frame->window);
499 } else {
500 moveFrameToUnder(above, frame);
502 } else {
503 moveFrameToUnder(frame->stacking->above, frame);
506 WMPostNotificationName(WMNResetStacking, scr, NULL);
511 *----------------------------------------------------------------------
512 * MoveInStackListUnder--
513 * Moves the frame to under "prev".
515 * Returns:
516 * None
518 * Side effects:
519 * Stacking order may be changed.
520 * Window level for frame may be changed.
521 *----------------------------------------------------------------------
523 void
524 MoveInStackListUnder(WCoreWindow *prev, WCoreWindow *frame)
526 WCoreWindow *tmpw;
527 int index;
528 WScreen *scr = frame->screen_ptr;
530 if (!prev || frame->stacking->above == prev)
531 return;
533 if (frame->stacking->window_level != prev->stacking->window_level)
534 ChangeStackingLevel(frame, prev->stacking->window_level);
536 index = frame->stacking->window_level;
538 tmpw = WMGetFromBag(scr->stacking_list, index);
539 if (tmpw == frame)
540 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
541 if (frame->stacking->under)
542 frame->stacking->under->stacking->above = frame->stacking->above;
543 if (frame->stacking->above)
544 frame->stacking->above->stacking->under = frame->stacking->under;
545 if (prev->stacking->under)
546 prev->stacking->under->stacking->above = frame;
547 frame->stacking->above = prev;
548 frame->stacking->under = prev->stacking->under;
549 prev->stacking->under = frame;
550 moveFrameToUnder(prev, frame);
552 WMPostNotificationName(WMNResetStacking, scr, NULL);
556 void
557 RemoveFromStackList(WCoreWindow *frame)
559 int index = frame->stacking->window_level;
561 if (XDeleteContext(dpy, frame->window, wStackContext)==XCNOENT) {
562 wwarning("RemoveFromStackingList(): window not in list ");
563 return;
565 /* remove from the window stack list */
566 if (frame->stacking->under)
567 frame->stacking->under->stacking->above = frame->stacking->above;
568 if (frame->stacking->above)
569 frame->stacking->above->stacking->under = frame->stacking->under;
570 else /* this was the first window on the list */
571 WMSetInBag(frame->screen_ptr->stacking_list, index,
572 frame->stacking->under);
574 frame->screen_ptr->window_count--;
576 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
580 void
581 ChangeStackingLevel(WCoreWindow *frame, int new_level)
583 int old_level;
585 if (frame->stacking->window_level == new_level)
586 return;
587 old_level = frame->stacking->window_level;
589 RemoveFromStackList(frame);
590 frame->stacking->window_level = new_level;
591 AddToStackList(frame);
592 if (old_level > new_level) {
593 wRaiseFrame(frame);
594 } else {
595 wLowerFrame(frame);