f28e5b3e63b3056f9396c8da746d38de13488bc4
[wmaker-crm.git] / src / stacking.c
blobf28e5b3e63b3056f9396c8da746d38de13488bc4
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <X11/Xlib.h>
28 #include <X11/Xutil.h>
30 #include "WindowMaker.h"
31 #include "screen.h"
32 #include "window.h"
33 #include "actions.h"
34 #include "properties.h"
35 #include "stacking.h"
36 #include "workspace.h"
38 /*** Global Variables ***/
39 extern XContext wStackContext;
41 extern WPreferences wPreferences;
43 static void notifyStackChange(WCoreWindow * frame, char *detail)
45 WWindow *wwin = wWindowFor(frame->window);
47 WMPostNotificationName(WMNChangedStacking, wwin, detail);
51 *----------------------------------------------------------------------
52 * RemakeStackList--
53 * Remakes the stacking_list for the screen, getting the real
54 * stacking order from the server and reordering windows that are not
55 * in the correct stacking.
57 * Side effects:
58 * The stacking order list and the actual window stacking
59 * may be changed (corrected)
61 *----------------------------------------------------------------------
63 void RemakeStackList(WScreen * scr)
65 Window *windows;
66 unsigned int nwindows;
67 Window junkr, junkp;
68 WCoreWindow *frame;
69 WCoreWindow *tmp;
70 int level;
71 int i, c;
73 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
74 wwarning(_("could not get window list!!"));
75 return;
76 } else {
77 WMEmptyBag(scr->stacking_list);
79 /* verify list integrity */
80 c = 0;
81 for (i = 0; i < nwindows; i++) {
82 if (XFindContext(dpy, windows[i], wStackContext, (XPointer *) & frame)
83 == XCNOENT) {
84 continue;
86 if (!frame)
87 continue;
88 c++;
89 level = frame->stacking->window_level;
90 tmp = WMGetFromBag(scr->stacking_list, level);
91 if (tmp)
92 tmp->stacking->above = frame;
93 frame->stacking->under = tmp;
94 frame->stacking->above = NULL;
95 WMSetInBag(scr->stacking_list, level, frame);
97 XFree(windows);
98 scr->window_count = c;
101 CommitStacking(scr);
105 *----------------------------------------------------------------------
106 * CommitStacking--
107 * Reorders the actual window stacking, so that it has the stacking
108 * order in the internal window stacking lists. It does the opposite
109 * of RemakeStackList().
111 * Side effects:
112 * Windows may be restacked.
113 *----------------------------------------------------------------------
115 void CommitStacking(WScreen * scr)
117 WCoreWindow *tmp;
118 int nwindows, i;
119 Window *windows;
120 WMBagIterator iter;
122 nwindows = scr->window_count;
123 windows = wmalloc(sizeof(Window) * nwindows);
125 i = 0;
126 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
127 while (tmp) {
128 windows[i++] = tmp->window;
129 tmp = tmp->stacking->under;
132 XRestackWindows(dpy, windows, i);
133 wfree(windows);
134 WMPostNotificationName(WMNResetStacking, scr, NULL);
138 *----------------------------------------------------------------------
139 * moveFrameToUnder--
140 * Reestacks windows so that "frame" is under "under".
142 * Returns:
143 * None
145 * Side effects:
146 * Changes the stacking order of frame.
147 *----------------------------------------------------------------------
149 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
151 Window wins[2];
153 wins[0] = under->window;
154 wins[1] = frame->window;
155 XRestackWindows(dpy, wins, 2);
159 *----------------------------------------------------------------------
160 * CommitStackingForWindow--
161 * Reorders the stacking for the specified window, so that it has the
162 * stacking order in the internal window stacking lists.
164 * Side effects:
165 * Windows may be restacked.
166 *----------------------------------------------------------------------
168 void CommitStackingForWindow(WCoreWindow * frame)
170 int level = frame->stacking->window_level;
171 WScreen *scr = frame->screen_ptr;
173 if (frame->stacking->above == NULL) {
174 WMBagIterator iter;
175 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
176 int i, last = above->stacking->window_level;
178 /* find the 1st level above us which has windows in it */
179 for (i = level + 1, above = NULL; i <= last; i++) {
180 above = WMGetFromBag(scr->stacking_list, i);
181 if (above != NULL)
182 break;
185 if (above != frame && above != NULL) {
186 while (above->stacking->under)
187 above = above->stacking->under;
188 moveFrameToUnder(above, frame);
189 } else {
190 /* no window above us */
191 XRaiseWindow(dpy, frame->window);
193 } else {
194 moveFrameToUnder(frame->stacking->above, frame);
199 *----------------------------------------------------------------------
200 * wRaiseFrame--
201 * Raises a frame taking the window level into account.
203 * Returns:
204 * None
206 * Side effects:
207 * Window stacking order and stacking list are changed.
209 *----------------------------------------------------------------------
211 void wRaiseFrame(WCoreWindow * frame)
213 WCoreWindow *wlist = frame;
214 int level = frame->stacking->window_level;
215 WScreen *scr = frame->screen_ptr;
217 /* already on top */
218 if (frame->stacking->above == NULL) {
219 return;
222 /* insert it on top of other windows on the same level */
223 if (frame->stacking->under)
224 frame->stacking->under->stacking->above = frame->stacking->above;
225 if (frame->stacking->above)
226 frame->stacking->above->stacking->under = frame->stacking->under;
228 frame->stacking->above = NULL;
229 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
230 if (frame->stacking->under) {
231 frame->stacking->under->stacking->above = frame;
233 WMSetInBag(scr->stacking_list, level, frame);
235 /* raise transients under us from bottom to top
236 * so that the order is kept */
237 again:
238 wlist = frame->stacking->under;
239 while (wlist && wlist->stacking->under)
240 wlist = wlist->stacking->under;
241 while (wlist && wlist != frame) {
242 if (wlist->stacking->child_of == frame) {
243 wRaiseFrame(wlist);
244 goto again;
246 wlist = wlist->stacking->above;
249 /* try to optimize things a little */
250 if (frame->stacking->above == NULL) {
251 WMBagIterator iter;
252 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
253 int i, last = above->stacking->window_level;
255 /* find the 1st level above us which has windows in it */
256 for (i = level + 1, above = NULL; i <= last; i++) {
257 above = WMGetFromBag(scr->stacking_list, i);
258 if (above != NULL)
259 break;
262 if (above != frame && above != NULL) {
263 while (above->stacking->under)
264 above = above->stacking->under;
265 moveFrameToUnder(above, frame);
266 } else {
267 /* no window above us */
268 above = NULL;
269 XRaiseWindow(dpy, frame->window);
271 } else {
272 moveFrameToUnder(frame->stacking->above, frame);
275 notifyStackChange(frame, "raise");
278 void wRaiseLowerFrame(WCoreWindow * frame)
280 if (!frame->stacking->above
281 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
283 wLowerFrame(frame);
284 } else {
285 WCoreWindow *scan = frame->stacking->above;
286 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
288 while (scan) {
290 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
291 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
293 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
294 && scan_wwin->flags.mapped) {
295 break;
298 scan = scan->stacking->above;
301 if (scan) {
302 wRaiseFrame(frame);
303 } else {
304 wLowerFrame(frame);
309 void wLowerFrame(WCoreWindow * frame)
311 WScreen *scr = frame->screen_ptr;
312 WCoreWindow *wlist = frame;
313 int level = frame->stacking->window_level;
315 /* already in bottom */
316 if (wlist->stacking->under == NULL) {
317 return;
319 /* cant lower transient below below its owner */
320 if (wlist->stacking->under == wlist->stacking->child_of) {
321 return;
323 /* remove from the list */
324 if (WMGetFromBag(scr->stacking_list, level) == frame) {
325 /* it was the top window */
326 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
327 frame->stacking->under->stacking->above = NULL;
328 } else {
329 if (frame->stacking->under)
330 frame->stacking->under->stacking->above = frame->stacking->above;
331 if (frame->stacking->above)
332 frame->stacking->above->stacking->under = frame->stacking->under;
334 wlist = WMGetFromBag(scr->stacking_list, level);
336 /* look for place to put this window */
337 if (wlist) {
338 WCoreWindow *owner = frame->stacking->child_of;
340 if (owner != wlist) {
341 while (wlist->stacking->under) {
342 /* if this is a transient, it should not be placed under
343 * it's owner */
344 if (owner == wlist->stacking->under)
345 break;
346 wlist = wlist->stacking->under;
350 /* insert under the place found */
351 frame->stacking->above = wlist;
352 if (wlist) {
353 frame->stacking->under = wlist->stacking->under;
354 if (wlist->stacking->under)
355 wlist->stacking->under->stacking->above = frame;
356 wlist->stacking->under = frame;
357 } else {
358 frame->stacking->under = NULL;
361 if (frame->stacking->above == NULL) {
362 WMBagIterator iter;
363 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
364 int i, last = above->stacking->window_level;
366 /* find the 1st level above us which has windows in it */
367 for (i = level + 1, above = NULL; i <= last; i++) {
368 above = WMGetFromBag(scr->stacking_list, i);
369 if (above != NULL)
370 break;
373 if (above != frame && above != NULL) {
374 while (above->stacking->under)
375 above = above->stacking->under;
376 moveFrameToUnder(above, frame);
377 } else {
378 /* no window below us */
379 XLowerWindow(dpy, frame->window);
381 } else {
382 moveFrameToUnder(frame->stacking->above, frame);
385 notifyStackChange(frame, "lower");
389 *----------------------------------------------------------------------
390 * AddToStackList--
391 * Inserts the frame in the top of the stacking list. The
392 * stacking precedence is obeyed.
394 * Returns:
395 * None
397 * Side effects:
398 * The frame is added to it's screen's window list.
399 *----------------------------------------------------------------------
401 void AddToStackList(WCoreWindow * frame)
403 WCoreWindow *curtop, *wlist;
404 int index = frame->stacking->window_level;
405 WScreen *scr = frame->screen_ptr;
406 WCoreWindow *trans = NULL;
408 frame->screen_ptr->window_count++;
409 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
410 curtop = WMGetFromBag(scr->stacking_list, index);
412 /* first window in this level */
413 if (curtop == NULL) {
414 WMSetInBag(scr->stacking_list, index, frame);
415 frame->stacking->above = NULL;
416 frame->stacking->under = NULL;
417 CommitStacking(scr);
418 return;
421 /* check if this is a transient owner */
422 wlist = curtop;
423 while (wlist) {
424 if (wlist->stacking->child_of == frame)
425 trans = wlist;
426 wlist = wlist->stacking->under;
428 /* trans will hold the transient in the lowest position
429 * in stacking list */
431 frame->stacking->above = trans;
432 if (trans != NULL) {
433 /* window is owner of a transient.. put it below
434 * the lowest transient */
435 frame->stacking->under = trans->stacking->under;
436 if (trans->stacking->under) {
437 trans->stacking->under->stacking->above = frame;
439 trans->stacking->under = frame;
440 } else {
441 /* window is not owner of transients.. just put it in the
442 * top of other windows */
443 frame->stacking->under = curtop;
444 curtop->stacking->above = frame;
445 WMSetInBag(scr->stacking_list, index, frame);
447 CommitStacking(scr);
451 *----------------------------------------------------------------------
452 * MoveInStackListAbove--
453 * Moves the frame above "next".
455 * Returns:
456 * None
458 * Side effects:
459 * Stacking order may be changed.
460 * Window level for frame may be changed.
461 *----------------------------------------------------------------------
463 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
465 WCoreWindow *tmpw;
466 WScreen *scr = frame->screen_ptr;
467 int index;
469 if (!next || frame->stacking->under == next)
470 return;
472 if (frame->stacking->window_level != next->stacking->window_level)
473 ChangeStackingLevel(frame, next->stacking->window_level);
475 index = frame->stacking->window_level;
477 tmpw = WMGetFromBag(scr->stacking_list, index);
478 if (tmpw == frame)
479 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
480 if (frame->stacking->under)
481 frame->stacking->under->stacking->above = frame->stacking->above;
482 if (frame->stacking->above)
483 frame->stacking->above->stacking->under = frame->stacking->under;
484 if (next->stacking->above)
485 next->stacking->above->stacking->under = frame;
486 frame->stacking->under = next;
487 frame->stacking->above = next->stacking->above;
488 next->stacking->above = frame;
489 if (tmpw == next)
490 WMSetInBag(scr->stacking_list, index, frame);
492 /* try to optimize things a little */
493 if (frame->stacking->above == NULL) {
494 WCoreWindow *above = NULL;
495 WMBagIterator iter;
497 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
498 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
500 /* can't optimize */
501 while (above->stacking->under)
502 above = above->stacking->under;
503 break;
505 if (above == NULL) {
506 XRaiseWindow(dpy, frame->window);
507 } else {
508 moveFrameToUnder(above, frame);
510 } else {
511 moveFrameToUnder(frame->stacking->above, frame);
514 WMPostNotificationName(WMNResetStacking, scr, NULL);
518 *----------------------------------------------------------------------
519 * MoveInStackListUnder--
520 * Moves the frame to under "prev".
522 * Returns:
523 * None
525 * Side effects:
526 * Stacking order may be changed.
527 * Window level for frame may be changed.
528 *----------------------------------------------------------------------
530 void 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);
561 void RemoveFromStackList(WCoreWindow * frame)
563 int index = frame->stacking->window_level;
565 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
566 wwarning("RemoveFromStackingList(): window not in list ");
567 return;
569 /* remove from the window stack list */
570 if (frame->stacking->under)
571 frame->stacking->under->stacking->above = frame->stacking->above;
572 if (frame->stacking->above)
573 frame->stacking->above->stacking->under = frame->stacking->under;
574 else /* this was the first window on the list */
575 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
577 frame->screen_ptr->window_count--;
579 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
582 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
584 int old_level;
586 if (frame->stacking->window_level == new_level)
587 return;
588 old_level = frame->stacking->window_level;
590 RemoveFromStackList(frame);
591 frame->stacking->window_level = new_level;
592 AddToStackList(frame);
593 if (old_level > new_level) {
594 wRaiseFrame(frame);
595 } else {
596 wLowerFrame(frame);