- Check whether libXft is at least version 2.1.2 else refuse to compile.
[wmaker-crm.git] / src / stacking.c
blob104b2a0ebe17921833402ccc5e73618ed765015f
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"
38 #include "workspace.h"
41 /*** Global Variables ***/
42 extern XContext wStackContext;
44 extern WPreferences wPreferences;
47 static void notifyStackChange(WCoreWindow *frame, char *detail)
49 WWindow *wwin = wWindowFor(frame->window);
51 WMPostNotificationName(WMNChangedStacking, wwin, detail);
56 *----------------------------------------------------------------------
57 * RemakeStackList--
58 * Remakes the stacking_list for the screen, getting the real
59 * stacking order from the server and reordering windows that are not
60 * in the correct stacking.
62 * Side effects:
63 * The stacking order list and the actual window stacking
64 * may be changed (corrected)
66 *----------------------------------------------------------------------
68 void
69 RemakeStackList(WScreen *scr)
71 Window *windows;
72 unsigned int nwindows;
73 Window junkr, junkp;
74 WCoreWindow *frame;
75 WCoreWindow *tmp;
76 int level;
77 int i, c;
79 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
80 wwarning(_("could not get window list!!"));
81 return;
82 } else {
83 WMEmptyBag(scr->stacking_list);
85 /* verify list integrity */
86 c=0;
87 for (i=0; i<nwindows; i++) {
88 if (XFindContext(dpy, windows[i], wStackContext, (XPointer*)&frame)
89 ==XCNOENT) {
90 continue;
92 if (!frame) continue;
93 c++;
94 level = frame->stacking->window_level;
95 tmp = WMGetFromBag(scr->stacking_list, level);
96 if (tmp)
97 tmp->stacking->above = frame;
98 frame->stacking->under = tmp;
99 frame->stacking->above = NULL;
100 WMSetInBag(scr->stacking_list, level, frame);
102 XFree(windows);
103 #ifdef DEBUG
104 if (c!=scr->window_count) {
105 puts("Found different number of windows than in window lists!!!");
107 #endif
108 scr->window_count = c;
111 CommitStacking(scr);
117 *----------------------------------------------------------------------
118 * CommitStacking--
119 * Reorders the actual window stacking, so that it has the stacking
120 * order in the internal window stacking lists. It does the opposite
121 * of RemakeStackList().
123 * Side effects:
124 * Windows may be restacked.
125 *----------------------------------------------------------------------
127 void
128 CommitStacking(WScreen *scr)
130 WCoreWindow *tmp;
131 int nwindows, i;
132 Window *windows;
133 WMBagIterator iter;
135 nwindows = scr->window_count;
136 windows = wmalloc(sizeof(Window)*nwindows);
138 i = 0;
139 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
140 while (tmp) {
141 #ifdef DEBUG
142 if (i>=nwindows) {
143 puts("Internal inconsistency! window_count is incorrect!!!");
144 printf("window_count says %i windows\n", nwindows);
145 wfree(windows);
146 return;
148 #endif
149 windows[i++] = tmp->window;
150 tmp = tmp->stacking->under;
153 XRestackWindows(dpy, windows, i);
154 wfree(windows);
156 #ifdef VIRTUAL_DESKTOP
157 wWorkspaceRaiseEdge(scr);
158 #endif
160 WMPostNotificationName(WMNResetStacking, scr, NULL);
164 *----------------------------------------------------------------------
165 * moveFrameToUnder--
166 * Reestacks windows so that "frame" is under "under".
168 * Returns:
169 * None
171 * Side effects:
172 * Changes the stacking order of frame.
173 *----------------------------------------------------------------------
175 static void
176 moveFrameToUnder(WCoreWindow *under, WCoreWindow *frame)
178 Window wins[2];
180 wins[0] = under->window;
181 wins[1] = frame->window;
182 XRestackWindows(dpy, wins, 2);
186 *----------------------------------------------------------------------
187 * wRaiseFrame--
188 * Raises a frame taking the window level into account.
190 * Returns:
191 * None
193 * Side effects:
194 * Window stacking order and stacking list are changed.
196 *----------------------------------------------------------------------
198 void
199 wRaiseFrame(WCoreWindow *frame)
201 WCoreWindow *wlist = frame;
202 int level = frame->stacking->window_level;
203 WScreen *scr = frame->screen_ptr;
205 /* already on top */
206 if (frame->stacking->above == NULL) {
207 return;
210 /* insert it on top of other windows on the same level */
211 if (frame->stacking->under)
212 frame->stacking->under->stacking->above = frame->stacking->above;
213 if (frame->stacking->above)
214 frame->stacking->above->stacking->under = frame->stacking->under;
216 frame->stacking->above = NULL;
217 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
218 if (frame->stacking->under) {
219 frame->stacking->under->stacking->above = frame;
221 WMSetInBag(scr->stacking_list, level, frame);
223 /* raise transients under us from bottom to top
224 * so that the order is kept */
225 again:
226 wlist = frame->stacking->under;
227 while (wlist && wlist->stacking->under)
228 wlist = wlist->stacking->under;
229 while (wlist && wlist!=frame) {
230 if (wlist->stacking->child_of == frame) {
231 wRaiseFrame(wlist);
232 goto again;
234 wlist = wlist->stacking->above;
237 /* try to optimize things a little */
238 if (frame->stacking->above == NULL) {
239 WMBagIterator iter;
240 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
241 int i, last = above->stacking->window_level;
243 /* find the 1st level above us which has windows in it */
244 for (i = level+1, above = NULL; i <= last; i++) {
245 above = WMGetFromBag(scr->stacking_list, i);
246 if (above != NULL)
247 break;
250 if (above != frame && above != NULL) {
251 while (above->stacking->under)
252 above = above->stacking->under;
253 moveFrameToUnder(above, frame);
254 } else {
255 /* no window above us */
256 above = NULL;
257 XRaiseWindow(dpy, frame->window);
259 } else {
260 moveFrameToUnder(frame->stacking->above, frame);
263 notifyStackChange(frame, "raise");
265 #ifdef VIRTUAL_DESKTOP
266 wWorkspaceRaiseEdge(scr);
267 #endif
271 void
272 wRaiseLowerFrame(WCoreWindow *frame)
274 if (!frame->stacking->above
275 ||(frame->stacking->window_level
276 !=frame->stacking->above->stacking->window_level)) {
278 wLowerFrame(frame);
279 } else {
280 WCoreWindow *scan = frame->stacking->above;
281 WWindow *frame_wwin = (WWindow*) frame->descriptor.parent;
283 while (scan) {
285 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
286 WWindow *scan_wwin = (WWindow*)scan->descriptor.parent;
288 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
289 && scan_wwin->flags.mapped) {
290 break;
293 scan = scan->stacking->above;
296 if (scan) {
297 wRaiseFrame(frame);
298 } else {
299 wLowerFrame(frame);
305 void
306 wLowerFrame(WCoreWindow *frame)
308 WScreen *scr = frame->screen_ptr;
309 WCoreWindow *wlist=frame;
310 int level = frame->stacking->window_level;
312 /* already in bottom */
313 if (wlist->stacking->under == NULL) {
314 return;
316 /* cant lower transient below below its owner */
317 if (wlist->stacking->under == wlist->stacking->child_of) {
318 return;
320 /* remove from the list */
321 if (WMGetFromBag(scr->stacking_list, level) == frame) {
322 /* it was the top window */
323 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
324 frame->stacking->under->stacking->above = NULL;
325 } else {
326 if (frame->stacking->under)
327 frame->stacking->under->stacking->above = frame->stacking->above;
328 if (frame->stacking->above)
329 frame->stacking->above->stacking->under = frame->stacking->under;
331 wlist = WMGetFromBag(scr->stacking_list, level);
333 /* look for place to put this window */
334 if (wlist) {
335 WCoreWindow *owner = frame->stacking->child_of;
337 if (owner != wlist) {
338 while (wlist->stacking->under) {
339 /* if this is a transient, it should not be placed under
340 * it's owner */
341 if (owner == wlist->stacking->under)
342 break;
343 wlist = wlist->stacking->under;
347 /* insert under the place found */
348 frame->stacking->above = wlist;
349 if (wlist) {
350 frame->stacking->under = wlist->stacking->under;
351 if (wlist->stacking->under)
352 wlist->stacking->under->stacking->above = frame;
353 wlist->stacking->under = frame;
354 } else {
355 frame->stacking->under = NULL;
358 if (frame->stacking->above == NULL) {
359 WMBagIterator iter;
360 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
361 int i, last = above->stacking->window_level;
363 /* find the 1st level above us which has windows in it */
364 for (i = level+1, above = NULL; i <= last; i++) {
365 above = WMGetFromBag(scr->stacking_list, i);
366 if (above != NULL)
367 break;
370 if (above != frame && above != NULL) {
371 while (above->stacking->under)
372 above = above->stacking->under;
373 moveFrameToUnder(above, frame);
374 } else {
375 /* no window below us */
376 XLowerWindow(dpy, frame->window);
378 } else {
379 moveFrameToUnder(frame->stacking->above, frame);
382 notifyStackChange(frame, "lower");
384 #ifdef VIRTUAL_DESKTOP
385 wWorkspaceRaiseEdge(scr);
386 #endif
391 *----------------------------------------------------------------------
392 * AddToStackList--
393 * Inserts the frame in the top of the stacking list. The
394 * stacking precedence is obeyed.
396 * Returns:
397 * None
399 * Side effects:
400 * The frame is added to it's screen's window list.
401 *----------------------------------------------------------------------
403 void
404 AddToStackList(WCoreWindow *frame)
406 WCoreWindow *curtop, *wlist;
407 int index = frame->stacking->window_level;
408 WScreen *scr = frame->screen_ptr;
409 WCoreWindow *trans = NULL;
411 frame->screen_ptr->window_count++;
412 XSaveContext(dpy, frame->window, wStackContext, (XPointer)frame);
413 curtop = WMGetFromBag(scr->stacking_list, index);
415 /* first window in this level */
416 if (curtop == NULL) {
417 WMSetInBag(scr->stacking_list, index, frame);
418 frame->stacking->above = NULL;
419 frame->stacking->under = NULL;
420 CommitStacking(scr);
421 return;
424 /* check if this is a transient owner */
425 wlist = curtop;
426 while (wlist) {
427 if (wlist->stacking->child_of == frame)
428 trans = wlist;
429 wlist = wlist->stacking->under;
431 /* trans will hold the transient in the lowest position
432 * in stacking list */
434 frame->stacking->above = trans;
435 if (trans != NULL) {
436 /* window is owner of a transient.. put it below
437 * the lowest transient */
438 frame->stacking->under = trans->stacking->under;
439 if (trans->stacking->under) {
440 trans->stacking->under->stacking->above = frame;
442 trans->stacking->under = frame;
443 } else {
444 /* window is not owner of transients.. just put it in the
445 * top of other windows */
446 frame->stacking->under = curtop;
447 curtop->stacking->above = frame;
448 WMSetInBag(scr->stacking_list, index, frame);
450 CommitStacking(scr);
455 *----------------------------------------------------------------------
456 * MoveInStackListAbove--
457 * Moves the frame above "next".
459 * Returns:
460 * None
462 * Side effects:
463 * Stacking order may be changed.
464 * Window level for frame may be changed.
465 *----------------------------------------------------------------------
467 void
468 MoveInStackListAbove(WCoreWindow *next, WCoreWindow *frame)
470 WCoreWindow *tmpw;
471 WScreen *scr = frame->screen_ptr;
472 int index;
474 if (!next || frame->stacking->under == next)
475 return;
477 if (frame->stacking->window_level != next->stacking->window_level)
478 ChangeStackingLevel(frame, next->stacking->window_level);
480 index = frame->stacking->window_level;
482 tmpw = WMGetFromBag(scr->stacking_list, index);
483 if (tmpw == frame)
484 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
485 if (frame->stacking->under)
486 frame->stacking->under->stacking->above = frame->stacking->above;
487 if (frame->stacking->above)
488 frame->stacking->above->stacking->under = frame->stacking->under;
489 if (next->stacking->above)
490 next->stacking->above->stacking->under = frame;
491 frame->stacking->under = next;
492 frame->stacking->above = next->stacking->above;
493 next->stacking->above = frame;
494 if (tmpw == next)
495 WMSetInBag(scr->stacking_list, index, frame);
497 /* try to optimize things a little */
498 if (frame->stacking->above == NULL) {
499 WCoreWindow *above = NULL;
500 WMBagIterator iter;
502 for (above = WMBagIteratorAtIndex(scr->stacking_list, index+1, &iter);
503 above != NULL;
504 above = WMBagNext(scr->stacking_list, &iter)) {
506 /* can't optimize */
507 while (above->stacking->under)
508 above = above->stacking->under;
509 break;
511 if (above == NULL) {
512 XRaiseWindow(dpy, frame->window);
513 } else {
514 moveFrameToUnder(above, frame);
516 } else {
517 moveFrameToUnder(frame->stacking->above, frame);
520 WMPostNotificationName(WMNResetStacking, scr, NULL);
522 #ifdef VIRTUAL_DESKTOP
523 wWorkspaceRaiseEdge(scr);
524 #endif
529 *----------------------------------------------------------------------
530 * MoveInStackListUnder--
531 * Moves the frame to under "prev".
533 * Returns:
534 * None
536 * Side effects:
537 * Stacking order may be changed.
538 * Window level for frame may be changed.
539 *----------------------------------------------------------------------
541 void
542 MoveInStackListUnder(WCoreWindow *prev, WCoreWindow *frame)
544 WCoreWindow *tmpw;
545 int index;
546 WScreen *scr = frame->screen_ptr;
548 if (!prev || frame->stacking->above == prev)
549 return;
551 if (frame->stacking->window_level != prev->stacking->window_level)
552 ChangeStackingLevel(frame, prev->stacking->window_level);
554 index = frame->stacking->window_level;
556 tmpw = WMGetFromBag(scr->stacking_list, index);
557 if (tmpw == frame)
558 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
559 if (frame->stacking->under)
560 frame->stacking->under->stacking->above = frame->stacking->above;
561 if (frame->stacking->above)
562 frame->stacking->above->stacking->under = frame->stacking->under;
563 if (prev->stacking->under)
564 prev->stacking->under->stacking->above = frame;
565 frame->stacking->above = prev;
566 frame->stacking->under = prev->stacking->under;
567 prev->stacking->under = frame;
568 moveFrameToUnder(prev, frame);
570 WMPostNotificationName(WMNResetStacking, scr, NULL);
574 void
575 RemoveFromStackList(WCoreWindow *frame)
577 int index = frame->stacking->window_level;
579 if (XDeleteContext(dpy, frame->window, wStackContext)==XCNOENT) {
580 wwarning("RemoveFromStackingList(): window not in list ");
581 return;
583 /* remove from the window stack list */
584 if (frame->stacking->under)
585 frame->stacking->under->stacking->above = frame->stacking->above;
586 if (frame->stacking->above)
587 frame->stacking->above->stacking->under = frame->stacking->under;
588 else /* this was the first window on the list */
589 WMSetInBag(frame->screen_ptr->stacking_list, index,
590 frame->stacking->under);
592 frame->screen_ptr->window_count--;
594 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
598 void
599 ChangeStackingLevel(WCoreWindow *frame, int new_level)
601 int old_level;
603 if (frame->stacking->window_level == new_level)
604 return;
605 old_level = frame->stacking->window_level;
607 RemoveFromStackList(frame);
608 frame->stacking->window_level = new_level;
609 AddToStackList(frame);
610 if (old_level > new_level) {
611 wRaiseFrame(frame);
612 } else {
613 wLowerFrame(frame);