wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / stacking.c
blob4a773406c4c7070964cfee9c84e82639b732580a
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 static void notifyStackChange(WCoreWindow * frame, char *detail)
43 WWindow *wwin = wWindowFor(frame->window);
45 WMPostNotificationName(WMNChangedStacking, wwin, detail);
49 *----------------------------------------------------------------------
50 * RemakeStackList--
51 * Remakes the stacking_list for the screen, getting the real
52 * stacking order from the server and reordering windows that are not
53 * in the correct stacking.
55 * Side effects:
56 * The stacking order list and the actual window stacking
57 * may be changed (corrected)
59 *----------------------------------------------------------------------
61 void RemakeStackList(WScreen * scr)
63 Window *windows;
64 unsigned int nwindows;
65 Window junkr, junkp;
66 WCoreWindow *frame;
67 WCoreWindow *tmp;
68 int level;
69 int i, c;
71 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
72 wwarning(_("could not get window list!!"));
73 return;
74 } else {
75 WMEmptyBag(scr->stacking_list);
77 /* verify list integrity */
78 c = 0;
79 for (i = 0; i < nwindows; i++) {
80 if (XFindContext(dpy, windows[i], wStackContext, (XPointer *) & frame)
81 == XCNOENT) {
82 continue;
84 if (!frame)
85 continue;
86 c++;
87 level = frame->stacking->window_level;
88 tmp = WMGetFromBag(scr->stacking_list, level);
89 if (tmp)
90 tmp->stacking->above = frame;
91 frame->stacking->under = tmp;
92 frame->stacking->above = NULL;
93 WMSetInBag(scr->stacking_list, level, frame);
95 XFree(windows);
96 scr->window_count = c;
99 CommitStacking(scr);
103 *----------------------------------------------------------------------
104 * CommitStacking--
105 * Reorders the actual window stacking, so that it has the stacking
106 * order in the internal window stacking lists. It does the opposite
107 * of RemakeStackList().
109 * Side effects:
110 * Windows may be restacked.
111 *----------------------------------------------------------------------
113 void CommitStacking(WScreen * scr)
115 WCoreWindow *tmp;
116 int nwindows, i;
117 Window *windows;
118 WMBagIterator iter;
120 nwindows = scr->window_count;
121 windows = wmalloc(sizeof(Window) * nwindows);
123 i = 0;
124 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
125 while (tmp) {
126 windows[i++] = tmp->window;
127 tmp = tmp->stacking->under;
130 XRestackWindows(dpy, windows, i);
131 wfree(windows);
132 WMPostNotificationName(WMNResetStacking, scr, NULL);
136 *----------------------------------------------------------------------
137 * moveFrameToUnder--
138 * Reestacks windows so that "frame" is under "under".
140 * Returns:
141 * None
143 * Side effects:
144 * Changes the stacking order of frame.
145 *----------------------------------------------------------------------
147 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
149 Window wins[2];
151 wins[0] = under->window;
152 wins[1] = frame->window;
153 XRestackWindows(dpy, wins, 2);
157 *----------------------------------------------------------------------
158 * CommitStackingForWindow--
159 * Reorders the stacking for the specified window, so that it has the
160 * stacking order in the internal window stacking lists.
162 * Side effects:
163 * Windows may be restacked.
164 *----------------------------------------------------------------------
166 void CommitStackingForWindow(WCoreWindow * frame)
168 int level = frame->stacking->window_level;
169 WScreen *scr = frame->screen_ptr;
171 if (frame->stacking->above == NULL) {
172 WMBagIterator iter;
173 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
174 int i, last = above->stacking->window_level;
176 /* find the 1st level above us which has windows in it */
177 for (i = level + 1, above = NULL; i <= last; i++) {
178 above = WMGetFromBag(scr->stacking_list, i);
179 if (above != NULL)
180 break;
183 if (above != frame && above != NULL) {
184 while (above->stacking->under)
185 above = above->stacking->under;
186 moveFrameToUnder(above, frame);
187 } else {
188 /* no window above us */
189 XRaiseWindow(dpy, frame->window);
191 } else {
192 moveFrameToUnder(frame->stacking->above, frame);
197 *----------------------------------------------------------------------
198 * wRaiseFrame--
199 * Raises a frame taking the window level into account.
201 * Returns:
202 * None
204 * Side effects:
205 * Window stacking order and stacking list are changed.
207 *----------------------------------------------------------------------
209 void wRaiseFrame(WCoreWindow * frame)
211 WCoreWindow *wlist = frame;
212 int level = frame->stacking->window_level;
213 WScreen *scr = frame->screen_ptr;
215 /* already on top */
216 if (frame->stacking->above == NULL) {
217 return;
220 /* insert it on top of other windows on the same level */
221 if (frame->stacking->under)
222 frame->stacking->under->stacking->above = frame->stacking->above;
223 if (frame->stacking->above)
224 frame->stacking->above->stacking->under = frame->stacking->under;
226 frame->stacking->above = NULL;
227 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
228 if (frame->stacking->under) {
229 frame->stacking->under->stacking->above = frame;
231 WMSetInBag(scr->stacking_list, level, frame);
233 /* raise transients under us from bottom to top
234 * so that the order is kept */
235 again:
236 wlist = frame->stacking->under;
237 while (wlist && wlist->stacking->under)
238 wlist = wlist->stacking->under;
239 while (wlist && wlist != frame) {
240 if (wlist->stacking->child_of == frame) {
241 wRaiseFrame(wlist);
242 goto again;
244 wlist = wlist->stacking->above;
247 /* try to optimize things a little */
248 if (frame->stacking->above == NULL) {
249 WMBagIterator iter;
250 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
251 int i, last = above->stacking->window_level;
253 /* find the 1st level above us which has windows in it */
254 for (i = level + 1, above = NULL; i <= last; i++) {
255 above = WMGetFromBag(scr->stacking_list, i);
256 if (above != NULL)
257 break;
260 if (above != frame && above != NULL) {
261 while (above->stacking->under)
262 above = above->stacking->under;
263 moveFrameToUnder(above, frame);
264 } else {
265 /* no window above us */
266 above = NULL;
267 XRaiseWindow(dpy, frame->window);
269 } else {
270 moveFrameToUnder(frame->stacking->above, frame);
273 notifyStackChange(frame, "raise");
276 void wRaiseLowerFrame(WCoreWindow * frame)
278 if (!frame->stacking->above
279 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
281 wLowerFrame(frame);
282 } else {
283 WCoreWindow *scan = frame->stacking->above;
284 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
286 while (scan) {
288 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
289 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
291 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
292 && scan_wwin->flags.mapped) {
293 break;
296 scan = scan->stacking->above;
299 if (scan) {
300 wRaiseFrame(frame);
301 } else {
302 wLowerFrame(frame);
307 void 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");
387 *----------------------------------------------------------------------
388 * AddToStackList--
389 * Inserts the frame in the top of the stacking list. The
390 * stacking precedence is obeyed.
392 * Returns:
393 * None
395 * Side effects:
396 * The frame is added to it's screen's window list.
397 *----------------------------------------------------------------------
399 void AddToStackList(WCoreWindow * frame)
401 WCoreWindow *curtop, *wlist;
402 int index = frame->stacking->window_level;
403 WScreen *scr = frame->screen_ptr;
404 WCoreWindow *trans = NULL;
406 frame->screen_ptr->window_count++;
407 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
408 curtop = WMGetFromBag(scr->stacking_list, index);
410 /* first window in this level */
411 if (curtop == NULL) {
412 WMSetInBag(scr->stacking_list, index, frame);
413 frame->stacking->above = NULL;
414 frame->stacking->under = NULL;
415 CommitStacking(scr);
416 return;
419 /* check if this is a transient owner */
420 wlist = curtop;
421 while (wlist) {
422 if (wlist->stacking->child_of == frame)
423 trans = wlist;
424 wlist = wlist->stacking->under;
426 /* trans will hold the transient in the lowest position
427 * in stacking list */
429 frame->stacking->above = trans;
430 if (trans != NULL) {
431 /* window is owner of a transient.. put it below
432 * the lowest transient */
433 frame->stacking->under = trans->stacking->under;
434 if (trans->stacking->under) {
435 trans->stacking->under->stacking->above = frame;
437 trans->stacking->under = frame;
438 } else {
439 /* window is not owner of transients.. just put it in the
440 * top of other windows */
441 frame->stacking->under = curtop;
442 curtop->stacking->above = frame;
443 WMSetInBag(scr->stacking_list, index, frame);
445 CommitStacking(scr);
449 *----------------------------------------------------------------------
450 * MoveInStackListAbove--
451 * Moves the frame above "next".
453 * Returns:
454 * None
456 * Side effects:
457 * Stacking order may be changed.
458 * Window level for frame may be changed.
459 *----------------------------------------------------------------------
461 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
463 WCoreWindow *tmpw;
464 WScreen *scr = frame->screen_ptr;
465 int index;
467 if (!next || frame->stacking->under == next)
468 return;
470 if (frame->stacking->window_level != next->stacking->window_level)
471 ChangeStackingLevel(frame, next->stacking->window_level);
473 index = frame->stacking->window_level;
475 tmpw = WMGetFromBag(scr->stacking_list, index);
476 if (tmpw == frame)
477 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
478 if (frame->stacking->under)
479 frame->stacking->under->stacking->above = frame->stacking->above;
480 if (frame->stacking->above)
481 frame->stacking->above->stacking->under = frame->stacking->under;
482 if (next->stacking->above)
483 next->stacking->above->stacking->under = frame;
484 frame->stacking->under = next;
485 frame->stacking->above = next->stacking->above;
486 next->stacking->above = frame;
487 if (tmpw == next)
488 WMSetInBag(scr->stacking_list, index, frame);
490 /* try to optimize things a little */
491 if (frame->stacking->above == NULL) {
492 WCoreWindow *above = NULL;
493 WMBagIterator iter;
495 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
496 above != NULL; 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);
516 *----------------------------------------------------------------------
517 * MoveInStackListUnder--
518 * Moves the frame to under "prev".
520 * Returns:
521 * None
523 * Side effects:
524 * Stacking order may be changed.
525 * Window level for frame may be changed.
526 *----------------------------------------------------------------------
528 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
530 WCoreWindow *tmpw;
531 int index;
532 WScreen *scr = frame->screen_ptr;
534 if (!prev || frame->stacking->above == prev)
535 return;
537 if (frame->stacking->window_level != prev->stacking->window_level)
538 ChangeStackingLevel(frame, prev->stacking->window_level);
540 index = frame->stacking->window_level;
542 tmpw = WMGetFromBag(scr->stacking_list, index);
543 if (tmpw == frame)
544 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
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 if (prev->stacking->under)
550 prev->stacking->under->stacking->above = frame;
551 frame->stacking->above = prev;
552 frame->stacking->under = prev->stacking->under;
553 prev->stacking->under = frame;
554 moveFrameToUnder(prev, frame);
556 WMPostNotificationName(WMNResetStacking, scr, NULL);
559 void RemoveFromStackList(WCoreWindow * frame)
561 int index = frame->stacking->window_level;
563 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
564 wwarning("RemoveFromStackingList(): window not in list ");
565 return;
567 /* remove from the window stack list */
568 if (frame->stacking->under)
569 frame->stacking->under->stacking->above = frame->stacking->above;
570 if (frame->stacking->above)
571 frame->stacking->above->stacking->under = frame->stacking->under;
572 else /* this was the first window on the list */
573 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
575 frame->screen_ptr->window_count--;
577 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
580 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
582 int old_level;
584 if (frame->stacking->window_level == new_level)
585 return;
586 old_level = frame->stacking->window_level;
588 RemoveFromStackList(frame);
589 frame->stacking->window_level = new_level;
590 AddToStackList(frame);
591 if (old_level > new_level) {
592 wRaiseFrame(frame);
593 } else {
594 wLowerFrame(frame);