- Fixed compilation include path issue in wrlib/tests
[wmaker-crm.git] / src / stacking.c
blobe6d0f26b4d7cf17fe3587dd23138b6c00b8cc9de
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"
41 /*** Global Variables ***/
42 extern XContext wStackContext;
44 extern WPreferences wPreferences;
47 static void
48 notifyStackChange(WCoreWindow *frame, char *detail)
50 WWindow *wwin = wWindowFor(frame->window);
52 WMPostNotificationName(WMNChangedStacking, wwin, detail);
57 *----------------------------------------------------------------------
58 * RemakeStackList--
59 * Remakes the stacking_list for the screen, getting the real
60 * stacking order from the server and reordering windows that are not
61 * in the correct stacking.
63 * Side effects:
64 * The stacking order list and the actual window stacking
65 * may be changed (corrected)
67 *----------------------------------------------------------------------
69 void
70 RemakeStackList(WScreen *scr)
72 Window *windows;
73 unsigned int nwindows;
74 Window junkr, junkp;
75 WCoreWindow *frame;
76 WCoreWindow *tmp;
77 int level;
78 int i, c;
80 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
81 wwarning(_("could not get window list!!"));
82 return;
83 } else {
84 WMEmptyBag(scr->stacking_list);
86 /* verify list integrity */
87 c=0;
88 for (i=0; i<nwindows; i++) {
89 if (XFindContext(dpy, windows[i], wStackContext, (XPointer*)&frame)
90 ==XCNOENT) {
91 continue;
93 if (!frame) continue;
94 c++;
95 level = frame->stacking->window_level;
96 tmp = WMGetFromBag(scr->stacking_list, level);
97 if (tmp)
98 tmp->stacking->above = frame;
99 frame->stacking->under = tmp;
100 frame->stacking->above = NULL;
101 WMSetInBag(scr->stacking_list, level, frame);
103 XFree(windows);
104 #ifdef DEBUG
105 if (c!=scr->window_count) {
106 puts("Found different number of windows than in window lists!!!");
108 #endif
109 scr->window_count = c;
112 CommitStacking(scr);
118 *----------------------------------------------------------------------
119 * CommitStacking--
120 * Reorders the actual window stacking, so that it has the stacking
121 * order in the internal window stacking lists. It does the opposite
122 * of RemakeStackList().
124 * Side effects:
125 * Windows may be restacked.
126 *----------------------------------------------------------------------
128 void
129 CommitStacking(WScreen *scr)
131 WCoreWindow *tmp;
132 int nwindows, i;
133 Window *windows;
134 WMBagIterator iter;
136 nwindows = scr->window_count;
137 windows = wmalloc(sizeof(Window)*nwindows);
139 i = 0;
140 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
141 while (tmp) {
142 #ifdef DEBUG
143 if (i>=nwindows) {
144 puts("Internal inconsistency! window_count is incorrect!!!");
145 printf("window_count says %i windows\n", nwindows);
146 wfree(windows);
147 return;
149 #endif
150 windows[i++] = tmp->window;
151 tmp = tmp->stacking->under;
154 XRestackWindows(dpy, windows, i);
155 wfree(windows);
157 #ifdef VIRTUAL_DESKTOP
158 wWorkspaceRaiseEdge(scr);
159 #endif
161 WMPostNotificationName(WMNResetStacking, scr, NULL);
165 *----------------------------------------------------------------------
166 * moveFrameToUnder--
167 * Reestacks windows so that "frame" is under "under".
169 * Returns:
170 * None
172 * Side effects:
173 * Changes the stacking order of frame.
174 *----------------------------------------------------------------------
176 static void
177 moveFrameToUnder(WCoreWindow *under, WCoreWindow *frame)
179 Window wins[2];
181 wins[0] = under->window;
182 wins[1] = frame->window;
183 XRestackWindows(dpy, wins, 2);
187 *----------------------------------------------------------------------
188 * wRaiseFrame--
189 * Raises a frame taking the window level into account.
191 * Returns:
192 * None
194 * Side effects:
195 * Window stacking order and stacking list are changed.
197 *----------------------------------------------------------------------
199 void
200 wRaiseFrame(WCoreWindow *frame)
202 WCoreWindow *wlist = frame;
203 int level = frame->stacking->window_level;
204 WScreen *scr = frame->screen_ptr;
206 /* already on top */
207 if (frame->stacking->above == NULL) {
208 return;
211 /* insert it on top of other windows on the same level */
212 if (frame->stacking->under)
213 frame->stacking->under->stacking->above = frame->stacking->above;
214 if (frame->stacking->above)
215 frame->stacking->above->stacking->under = frame->stacking->under;
217 frame->stacking->above = NULL;
218 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
219 if (frame->stacking->under) {
220 frame->stacking->under->stacking->above = frame;
222 WMSetInBag(scr->stacking_list, level, frame);
224 /* raise transients under us from bottom to top
225 * so that the order is kept */
226 again:
227 wlist = frame->stacking->under;
228 while (wlist && wlist->stacking->under)
229 wlist = wlist->stacking->under;
230 while (wlist && wlist!=frame) {
231 if (wlist->stacking->child_of == frame) {
232 wRaiseFrame(wlist);
233 goto again;
235 wlist = wlist->stacking->above;
238 /* try to optimize things a little */
239 if (frame->stacking->above == NULL) {
240 WMBagIterator iter;
241 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
242 int i, last = above->stacking->window_level;
244 /* find the 1st level above us which has windows in it */
245 for (i = level+1, above = NULL; i <= last; i++) {
246 above = WMGetFromBag(scr->stacking_list, i);
247 if (above != NULL)
248 break;
251 if (above != frame && above != NULL) {
252 while (above->stacking->under)
253 above = above->stacking->under;
254 moveFrameToUnder(above, frame);
255 } else {
256 /* no window above us */
257 above = NULL;
258 XRaiseWindow(dpy, frame->window);
260 } else {
261 moveFrameToUnder(frame->stacking->above, frame);
264 notifyStackChange(frame, "raise");
266 #ifdef VIRTUAL_DESKTOP
267 wWorkspaceRaiseEdge(scr);
268 #endif
272 void
273 wRaiseLowerFrame(WCoreWindow *frame)
275 if (!frame->stacking->above
276 ||(frame->stacking->window_level
277 !=frame->stacking->above->stacking->window_level)) {
279 wLowerFrame(frame);
280 } else {
281 WCoreWindow *scan = frame->stacking->above;
282 WWindow *frame_wwin = (WWindow*) frame->descriptor.parent;
284 while (scan) {
286 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
287 WWindow *scan_wwin = (WWindow*)scan->descriptor.parent;
289 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
290 && scan_wwin->flags.mapped) {
291 break;
294 scan = scan->stacking->above;
297 if (scan) {
298 wRaiseFrame(frame);
299 } else {
300 wLowerFrame(frame);
306 void
307 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) {
315 return;
317 /* cant lower transient below below its owner */
318 if (wlist->stacking->under == wlist->stacking->child_of) {
319 return;
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;
326 } else {
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 */
335 if (wlist) {
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
341 * it's owner */
342 if (owner == wlist->stacking->under)
343 break;
344 wlist = wlist->stacking->under;
348 /* insert under the place found */
349 frame->stacking->above = wlist;
350 if (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;
355 } else {
356 frame->stacking->under = NULL;
359 if (frame->stacking->above == NULL) {
360 WMBagIterator iter;
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);
367 if (above != NULL)
368 break;
371 if (above != frame && above != NULL) {
372 while (above->stacking->under)
373 above = above->stacking->under;
374 moveFrameToUnder(above, frame);
375 } else {
376 /* no window below us */
377 XLowerWindow(dpy, frame->window);
379 } else {
380 moveFrameToUnder(frame->stacking->above, frame);
383 notifyStackChange(frame, "lower");
385 #ifdef VIRTUAL_DESKTOP
386 wWorkspaceRaiseEdge(scr);
387 #endif
392 *----------------------------------------------------------------------
393 * AddToStackList--
394 * Inserts the frame in the top of the stacking list. The
395 * stacking precedence is obeyed.
397 * Returns:
398 * None
400 * Side effects:
401 * The frame is added to it's screen's window list.
402 *----------------------------------------------------------------------
404 void
405 AddToStackList(WCoreWindow *frame)
407 WCoreWindow *curtop, *wlist;
408 int index = frame->stacking->window_level;
409 WScreen *scr = frame->screen_ptr;
410 WCoreWindow *trans = NULL;
412 frame->screen_ptr->window_count++;
413 XSaveContext(dpy, frame->window, wStackContext, (XPointer)frame);
414 curtop = WMGetFromBag(scr->stacking_list, index);
416 /* first window in this level */
417 if (curtop == NULL) {
418 WMSetInBag(scr->stacking_list, index, frame);
419 frame->stacking->above = NULL;
420 frame->stacking->under = NULL;
421 CommitStacking(scr);
422 return;
425 /* check if this is a transient owner */
426 wlist = curtop;
427 while (wlist) {
428 if (wlist->stacking->child_of == frame)
429 trans = wlist;
430 wlist = wlist->stacking->under;
432 /* trans will hold the transient in the lowest position
433 * in stacking list */
435 frame->stacking->above = trans;
436 if (trans != NULL) {
437 /* window is owner of a transient.. put it below
438 * the lowest transient */
439 frame->stacking->under = trans->stacking->under;
440 if (trans->stacking->under) {
441 trans->stacking->under->stacking->above = frame;
443 trans->stacking->under = frame;
444 } else {
445 /* window is not owner of transients.. just put it in the
446 * top of other windows */
447 frame->stacking->under = curtop;
448 curtop->stacking->above = frame;
449 WMSetInBag(scr->stacking_list, index, frame);
451 CommitStacking(scr);
456 *----------------------------------------------------------------------
457 * MoveInStackListAbove--
458 * Moves the frame above "next".
460 * Returns:
461 * None
463 * Side effects:
464 * Stacking order may be changed.
465 * Window level for frame may be changed.
466 *----------------------------------------------------------------------
468 void
469 MoveInStackListAbove(WCoreWindow *next, WCoreWindow *frame)
471 WCoreWindow *tmpw;
472 WScreen *scr = frame->screen_ptr;
473 int index;
475 if (!next || frame->stacking->under == next)
476 return;
478 if (frame->stacking->window_level != next->stacking->window_level)
479 ChangeStackingLevel(frame, next->stacking->window_level);
481 index = frame->stacking->window_level;
483 tmpw = WMGetFromBag(scr->stacking_list, index);
484 if (tmpw == frame)
485 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
486 if (frame->stacking->under)
487 frame->stacking->under->stacking->above = frame->stacking->above;
488 if (frame->stacking->above)
489 frame->stacking->above->stacking->under = frame->stacking->under;
490 if (next->stacking->above)
491 next->stacking->above->stacking->under = frame;
492 frame->stacking->under = next;
493 frame->stacking->above = next->stacking->above;
494 next->stacking->above = frame;
495 if (tmpw == next)
496 WMSetInBag(scr->stacking_list, index, frame);
498 /* try to optimize things a little */
499 if (frame->stacking->above == NULL) {
500 WCoreWindow *above = NULL;
501 WMBagIterator iter;
503 for (above = WMBagIteratorAtIndex(scr->stacking_list, index+1, &iter);
504 above != NULL;
505 above = WMBagNext(scr->stacking_list, &iter)) {
507 /* can't optimize */
508 while (above->stacking->under)
509 above = above->stacking->under;
510 break;
512 if (above == NULL) {
513 XRaiseWindow(dpy, frame->window);
514 } else {
515 moveFrameToUnder(above, frame);
517 } else {
518 moveFrameToUnder(frame->stacking->above, frame);
521 WMPostNotificationName(WMNResetStacking, scr, NULL);
523 #ifdef VIRTUAL_DESKTOP
524 wWorkspaceRaiseEdge(scr);
525 #endif
530 *----------------------------------------------------------------------
531 * MoveInStackListUnder--
532 * Moves the frame to under "prev".
534 * Returns:
535 * None
537 * Side effects:
538 * Stacking order may be changed.
539 * Window level for frame may be changed.
540 *----------------------------------------------------------------------
542 void
543 MoveInStackListUnder(WCoreWindow *prev, WCoreWindow *frame)
545 WCoreWindow *tmpw;
546 int index;
547 WScreen *scr = frame->screen_ptr;
549 if (!prev || frame->stacking->above == prev)
550 return;
552 if (frame->stacking->window_level != prev->stacking->window_level)
553 ChangeStackingLevel(frame, prev->stacking->window_level);
555 index = frame->stacking->window_level;
557 tmpw = WMGetFromBag(scr->stacking_list, index);
558 if (tmpw == frame)
559 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
560 if (frame->stacking->under)
561 frame->stacking->under->stacking->above = frame->stacking->above;
562 if (frame->stacking->above)
563 frame->stacking->above->stacking->under = frame->stacking->under;
564 if (prev->stacking->under)
565 prev->stacking->under->stacking->above = frame;
566 frame->stacking->above = prev;
567 frame->stacking->under = prev->stacking->under;
568 prev->stacking->under = frame;
569 moveFrameToUnder(prev, frame);
571 WMPostNotificationName(WMNResetStacking, scr, NULL);
575 void
576 RemoveFromStackList(WCoreWindow *frame)
578 int index = frame->stacking->window_level;
580 if (XDeleteContext(dpy, frame->window, wStackContext)==XCNOENT) {
581 wwarning("RemoveFromStackingList(): window not in list ");
582 return;
584 /* remove from the window stack list */
585 if (frame->stacking->under)
586 frame->stacking->under->stacking->above = frame->stacking->above;
587 if (frame->stacking->above)
588 frame->stacking->above->stacking->under = frame->stacking->under;
589 else /* this was the first window on the list */
590 WMSetInBag(frame->screen_ptr->stacking_list, index,
591 frame->stacking->under);
593 frame->screen_ptr->window_count--;
595 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
599 void
600 ChangeStackingLevel(WCoreWindow *frame, int new_level)
602 int old_level;
604 if (frame->stacking->window_level == new_level)
605 return;
606 old_level = frame->stacking->window_level;
608 RemoveFromStackList(frame);
609 frame->stacking->window_level = new_level;
610 AddToStackList(frame);
611 if (old_level > new_level) {
612 wRaiseFrame(frame);
613 } else {
614 wLowerFrame(frame);