- Fixed crashing bug in menu.c
[wmaker-crm.git] / src / stacking.c
blobe246983fc0e4a022210251429e1ae1d8a075d1cd
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 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 if (frame->stacking->under) {
215 frame->stacking->under->stacking->above = frame;
217 WMSetInBag(scr->stacking_list, level, frame);
219 /* raise transients under us from bottom to top
220 * so that the order is kept */
221 again:
222 wlist = frame->stacking->under;
223 while (wlist && wlist->stacking->under)
224 wlist = wlist->stacking->under;
225 while (wlist && wlist!=frame) {
226 if (wlist->stacking->child_of == frame) {
227 wRaiseFrame(wlist);
228 goto again;
230 wlist = wlist->stacking->above;
233 /* try to optimize things a little */
234 if (frame->stacking->above == NULL) {
235 WMBagIterator iter;
236 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
237 int i, last = above->stacking->window_level;
239 /* find the 1st level above us which has windows in it */
240 for (i = level+1, above = NULL; i <= last; i++) {
241 above = WMGetFromBag(scr->stacking_list, i);
242 if (above != NULL)
243 break;
246 if (above != frame && above != NULL) {
247 while (above->stacking->under)
248 above = above->stacking->under;
249 moveFrameToUnder(above, frame);
250 } else {
251 /* no window above us */
252 above = NULL;
253 XRaiseWindow(dpy, frame->window);
255 } else {
256 moveFrameToUnder(frame->stacking->above, frame);
259 notifyStackChange(frame, "raise");
261 #ifdef VIRTUAL_DESKTOP
262 wWorkspaceRaiseEdge(scr);
263 #endif
267 void
268 wRaiseLowerFrame(WCoreWindow *frame)
270 if (!frame->stacking->above
271 ||(frame->stacking->window_level
272 !=frame->stacking->above->stacking->window_level)) {
274 wLowerFrame(frame);
275 } else {
276 WCoreWindow *scan = frame->stacking->above;
277 WWindow *frame_wwin = (WWindow*) frame->descriptor.parent;
279 while (scan) {
281 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
282 WWindow *scan_wwin = (WWindow*)scan->descriptor.parent;
284 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
285 && scan_wwin->flags.mapped) {
286 break;
289 scan = scan->stacking->above;
292 if (scan) {
293 wRaiseFrame(frame);
294 } else {
295 wLowerFrame(frame);
301 void
302 wLowerFrame(WCoreWindow *frame)
304 WScreen *scr = frame->screen_ptr;
305 WCoreWindow *wlist=frame;
306 int level = frame->stacking->window_level;
308 /* already in bottom */
309 if (wlist->stacking->under == NULL) {
310 return;
312 /* cant lower transient below below its owner */
313 if (wlist->stacking->under == wlist->stacking->child_of) {
314 return;
316 /* remove from the list */
317 if (WMGetFromBag(scr->stacking_list, level) == frame) {
318 /* it was the top window */
319 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
320 frame->stacking->under->stacking->above = NULL;
321 } else {
322 if (frame->stacking->under)
323 frame->stacking->under->stacking->above = frame->stacking->above;
324 if (frame->stacking->above)
325 frame->stacking->above->stacking->under = frame->stacking->under;
327 wlist = WMGetFromBag(scr->stacking_list, level);
329 /* look for place to put this window */
330 if (wlist) {
331 WCoreWindow *owner = frame->stacking->child_of;
333 if (owner != wlist) {
334 while (wlist->stacking->under) {
335 /* if this is a transient, it should not be placed under
336 * it's owner */
337 if (owner == wlist->stacking->under)
338 break;
339 wlist = wlist->stacking->under;
343 /* insert under the place found */
344 frame->stacking->above = wlist;
345 if (wlist) {
346 frame->stacking->under = wlist->stacking->under;
347 if (wlist->stacking->under)
348 wlist->stacking->under->stacking->above = frame;
349 wlist->stacking->under = frame;
350 } else {
351 frame->stacking->under = NULL;
354 if (frame->stacking->above == NULL) {
355 WMBagIterator iter;
356 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
357 int i, last = above->stacking->window_level;
359 /* find the 1st level above us which has windows in it */
360 for (i = level+1, above = NULL; i <= last; i++) {
361 above = WMGetFromBag(scr->stacking_list, i);
362 if (above != NULL)
363 break;
366 if (above != frame && above != NULL) {
367 while (above->stacking->under)
368 above = above->stacking->under;
369 moveFrameToUnder(above, frame);
370 } else {
371 /* no window below us */
372 XLowerWindow(dpy, frame->window);
374 } else {
375 moveFrameToUnder(frame->stacking->above, frame);
378 notifyStackChange(frame, "lower");
383 *----------------------------------------------------------------------
384 * AddToStackList--
385 * Inserts the frame in the top of the stacking list. The
386 * stacking precedence is obeyed.
388 * Returns:
389 * None
391 * Side effects:
392 * The frame is added to it's screen's window list.
393 *----------------------------------------------------------------------
395 void
396 AddToStackList(WCoreWindow *frame)
398 WCoreWindow *curtop, *wlist;
399 int index = frame->stacking->window_level;
400 WScreen *scr = frame->screen_ptr;
401 WCoreWindow *trans = NULL;
403 frame->screen_ptr->window_count++;
404 XSaveContext(dpy, frame->window, wStackContext, (XPointer)frame);
405 curtop = WMGetFromBag(scr->stacking_list, index);
407 /* first window in this level */
408 if (curtop == NULL) {
409 WMSetInBag(scr->stacking_list, index, frame);
410 frame->stacking->above = NULL;
411 frame->stacking->under = NULL;
412 CommitStacking(scr);
413 return;
416 /* check if this is a transient owner */
417 wlist = curtop;
418 while (wlist) {
419 if (wlist->stacking->child_of == frame)
420 trans = wlist;
421 wlist = wlist->stacking->under;
423 /* trans will hold the transient in the lowest position
424 * in stacking list */
426 frame->stacking->above = trans;
427 if (trans != NULL) {
428 /* window is owner of a transient.. put it below
429 * the lowest transient */
430 frame->stacking->under = trans->stacking->under;
431 if (trans->stacking->under) {
432 trans->stacking->under->stacking->above = frame;
434 trans->stacking->under = frame;
435 } else {
436 /* window is not owner of transients.. just put it in the
437 * top of other windows */
438 frame->stacking->under = curtop;
439 curtop->stacking->above = frame;
440 WMSetInBag(scr->stacking_list, index, frame);
442 CommitStacking(scr);
447 *----------------------------------------------------------------------
448 * MoveInStackListAbove--
449 * Moves the frame above "next".
451 * Returns:
452 * None
454 * Side effects:
455 * Stacking order may be changed.
456 * Window level for frame may be changed.
457 *----------------------------------------------------------------------
459 void
460 MoveInStackListAbove(WCoreWindow *next, WCoreWindow *frame)
462 WCoreWindow *tmpw;
463 WScreen *scr = frame->screen_ptr;
464 int index;
466 if (!next || frame->stacking->under == next)
467 return;
469 if (frame->stacking->window_level != next->stacking->window_level)
470 ChangeStackingLevel(frame, next->stacking->window_level);
472 index = frame->stacking->window_level;
474 tmpw = WMGetFromBag(scr->stacking_list, index);
475 if (tmpw == frame)
476 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
477 if (frame->stacking->under)
478 frame->stacking->under->stacking->above = frame->stacking->above;
479 if (frame->stacking->above)
480 frame->stacking->above->stacking->under = frame->stacking->under;
481 if (next->stacking->above)
482 next->stacking->above->stacking->under = frame;
483 frame->stacking->under = next;
484 frame->stacking->above = next->stacking->above;
485 next->stacking->above = frame;
486 if (tmpw == next)
487 WMSetInBag(scr->stacking_list, index, frame);
489 /* try to optimize things a little */
490 if (frame->stacking->above == NULL) {
491 WCoreWindow *above = NULL;
492 WMBagIterator iter;
494 for (above = WMBagIteratorAtIndex(scr->stacking_list, index+1, &iter);
495 above != NULL;
496 above = WMBagNext(scr->stacking_list, &iter)) {
498 /* can't optimize */
499 while (above->stacking->under)
500 above = above->stacking->under;
501 break;
503 if (above == NULL) {
504 XRaiseWindow(dpy, frame->window);
505 } else {
506 moveFrameToUnder(above, frame);
508 } else {
509 moveFrameToUnder(frame->stacking->above, frame);
512 WMPostNotificationName(WMNResetStacking, scr, NULL);
517 *----------------------------------------------------------------------
518 * MoveInStackListUnder--
519 * Moves the frame to under "prev".
521 * Returns:
522 * None
524 * Side effects:
525 * Stacking order may be changed.
526 * Window level for frame may be changed.
527 *----------------------------------------------------------------------
529 void
530 MoveInStackListUnder(WCoreWindow *prev, WCoreWindow *frame)
532 WCoreWindow *tmpw;
533 int index;
534 WScreen *scr = frame->screen_ptr;
536 if (!prev || frame->stacking->above == prev)
537 return;
539 if (frame->stacking->window_level != prev->stacking->window_level)
540 ChangeStackingLevel(frame, prev->stacking->window_level);
542 index = frame->stacking->window_level;
544 tmpw = WMGetFromBag(scr->stacking_list, index);
545 if (tmpw == frame)
546 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
547 if (frame->stacking->under)
548 frame->stacking->under->stacking->above = frame->stacking->above;
549 if (frame->stacking->above)
550 frame->stacking->above->stacking->under = frame->stacking->under;
551 if (prev->stacking->under)
552 prev->stacking->under->stacking->above = frame;
553 frame->stacking->above = prev;
554 frame->stacking->under = prev->stacking->under;
555 prev->stacking->under = frame;
556 moveFrameToUnder(prev, frame);
558 WMPostNotificationName(WMNResetStacking, scr, NULL);
562 void
563 RemoveFromStackList(WCoreWindow *frame)
565 int index = frame->stacking->window_level;
567 if (XDeleteContext(dpy, frame->window, wStackContext)==XCNOENT) {
568 wwarning("RemoveFromStackingList(): window not in list ");
569 return;
571 /* remove from the window stack list */
572 if (frame->stacking->under)
573 frame->stacking->under->stacking->above = frame->stacking->above;
574 if (frame->stacking->above)
575 frame->stacking->above->stacking->under = frame->stacking->under;
576 else /* this was the first window on the list */
577 WMSetInBag(frame->screen_ptr->stacking_list, index,
578 frame->stacking->under);
580 frame->screen_ptr->window_count--;
582 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
586 void
587 ChangeStackingLevel(WCoreWindow *frame, int new_level)
589 int old_level;
591 if (frame->stacking->window_level == new_level)
592 return;
593 old_level = frame->stacking->window_level;
595 RemoveFromStackList(frame);
596 frame->stacking->window_level = new_level;
597 AddToStackList(frame);
598 if (old_level > new_level) {
599 wRaiseFrame(frame);
600 } else {
601 wLowerFrame(frame);