makefile.am for icons
[wmaker-crm.git] / src / stacking.c
blobe7c90cad32538ca010c56f46f24dfb9fb59ec36f
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1998 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 #ifdef KWM_HINTS
39 #include "kwm.h"
40 #endif
42 /*** Global Variables ***/
43 extern XContext wStackContext;
45 extern WPreferences wPreferences;
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
62 RemakeStackList(WScreen *scr)
64 Window *windows;
65 unsigned int nwindows;
66 Window junkr, junkp;
67 WCoreWindow *frame;
68 WCoreWindow *onbotw[MAX_WINDOW_LEVELS];
69 int level;
70 int i, c;
72 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
73 wwarning(_("could not get window list!!"));
74 return;
75 } else {
76 for (i=0; i<MAX_WINDOW_LEVELS; i++) {
77 scr->stacking_list[i] = NULL;
78 onbotw[i] = NULL;
79 /* scr->window_level_count[i] = 0;*/
81 /* verify list integrity */
82 c=0;
83 for (i=0; i<nwindows; i++) {
84 if (XFindContext(dpy, windows[i], wStackContext, (XPointer*)&frame)
85 ==XCNOENT) {
86 continue;
88 if (!frame) continue;
89 c++;
90 level = frame->stacking->window_level;
91 if (onbotw[level])
92 onbotw[level]->stacking->above = frame;
93 frame->stacking->under = onbotw[level];
94 frame->stacking->above = NULL;
95 onbotw[level] = frame;
96 /* scr->window_level_count[level]++;*/
98 XFree(windows);
99 #ifdef DEBUG
100 if (c!=scr->window_count) {
101 puts("Found different number of windows than in window lists!!!");
103 #endif
104 scr->window_count = c;
106 /* now, just concatenate the lists */
107 for (i=0; i<MAX_WINDOW_LEVELS; i++) {
108 scr->stacking_list[i] = onbotw[i];
109 if (onbotw[i])
110 onbotw[i]->stacking->above = NULL;
113 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;
132 Window *windows;
133 int i, level;
135 nwindows = scr->window_count;
136 windows = wmalloc(sizeof(Window)*nwindows);
137 i=0;
138 for (level=MAX_WINDOW_LEVELS-1; level>=0; level--) {
139 tmp = scr->stacking_list[level];
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 free(windows);
146 return;
148 #endif
149 windows[i++] = tmp->window;
150 tmp = tmp->stacking->under;
153 XRestackWindows(dpy, windows, i);
154 free(windows);
156 #ifdef KWM_HINTS
157 wKWMBroadcastStacking(scr);
158 #endif
162 *----------------------------------------------------------------------
163 * moveFrameToUnder--
164 * Reestacks windows so that "frame" is under "under".
166 * Returns:
167 * None
169 * Side effects:
170 * Changes the stacking order of frame.
171 *----------------------------------------------------------------------
173 static void
174 moveFrameToUnder(WCoreWindow *under, WCoreWindow *frame)
176 Window wins[2];
178 wins[0] = under->window;
179 wins[1] = frame->window;
180 XRestackWindows(dpy, wins, 2);
182 #ifdef KWM_HINTS
183 wKWMBroadcastStacking(under->screen_ptr);
184 #endif
188 *----------------------------------------------------------------------
189 * wRaiseFrame--
190 * Raises a frame taking the window level and the on_top flag
191 * into account.
193 * Returns:
194 * None
196 * Side effects:
197 * Window stacking order and window list is changed.
199 *----------------------------------------------------------------------
201 void
202 wRaiseFrame(WCoreWindow *frame)
204 WCoreWindow *wlist = frame, *wlist_above;
205 int level = frame->stacking->window_level;
206 int i;
208 /* already on top */
209 if (frame->stacking->above == NULL) {
210 return;
213 /* insert on top of other windows */
214 #if 1
215 while (wlist) {
216 if (wlist == (wlist_above = wlist->stacking->above)) {
217 wwarning("You just found a bug in wmaker. Please try to figure what type of raising/lowering operations you did with which applications and report. Please give complete information about how to reproduce it.");
218 break;
219 } else {
220 wlist=wlist_above;
223 #else
224 while (wlist)
225 wlist = wlist->stacking->above;
226 #endif
227 /* window is inserted before the point found */
228 if (wlist==NULL) {
229 /* top most window (last on the list) */
230 if (frame->stacking->under)
231 frame->stacking->under->stacking->above = frame->stacking->above;
232 if (frame->stacking->above)
233 frame->stacking->above->stacking->under = frame->stacking->under;
235 frame->stacking->above = NULL;
236 frame->stacking->under = frame->screen_ptr->stacking_list[level];
237 frame->screen_ptr->stacking_list[level]->stacking->above=frame;
238 frame->screen_ptr->stacking_list[level] = frame;
239 } else if (frame!=wlist) {
240 if (frame->stacking->under)
241 frame->stacking->under->stacking->above = frame->stacking->above;
242 if (frame->stacking->above)
243 frame->stacking->above->stacking->under = frame->stacking->under;
245 frame->stacking->above = wlist;
246 frame->stacking->under = wlist->stacking->under;
247 if (wlist->stacking->under)
248 wlist->stacking->under->stacking->above = frame;
249 wlist->stacking->under = frame;
251 #ifdef removed
252 if (wPreferences.on_top_transients)
253 #endif
255 /* raise transients under us from bottom to top
256 * so that the order is kept */
257 again:
258 wlist = frame->stacking->under;
259 while (wlist && wlist->stacking->under)
260 wlist = wlist->stacking->under;
261 while (wlist && wlist!=frame) {
262 if (wlist->stacking->child_of == frame) {
263 wRaiseFrame(wlist);
264 goto again;
266 wlist = wlist->stacking->above;
268 # if 0
269 again:
270 wlist = frame->stacking->under;
271 while (wlist) {
272 if (wlist->stacking->child_of == frame) {
273 /* transient for us */
274 wRaiseFrame(wlist);
275 goto again; /* need this or we'll get in a loop */
277 wlist = wlist->stacking->under;
279 #endif
281 /* try to optimize things a little */
282 if (frame->stacking->above == NULL) {
283 WCoreWindow *above=NULL;
285 for (i=level+1; i<MAX_WINDOW_LEVELS; i++) {
286 if (frame->screen_ptr->stacking_list[i]!=NULL) {
287 /* can't optimize */
288 above = frame->screen_ptr->stacking_list[i];
289 while (above->stacking->under)
290 above = above->stacking->under;
291 break;
294 if (!above) {
295 XRaiseWindow(dpy, frame->window);
296 } else {
297 moveFrameToUnder(above, frame);
299 } else {
300 moveFrameToUnder(frame->stacking->above, frame);
302 #ifdef KWM_HINTS
304 WWindow *wwin = wWindowFor(frame->window);
306 if (wwin)
307 wKWMSendEventMessage(wwin, WKWMRaiseWindow);
309 #endif
310 #ifdef VIRTUAL_DESKTOP
311 wWorkspaceRaiseEdge(frame->screen_ptr);
312 #endif
316 void
317 wRaiseLowerFrame(WCoreWindow *frame)
319 if (!frame->stacking->above
320 ||(frame->stacking->window_level
321 !=frame->stacking->above->stacking->window_level)) {
323 wLowerFrame(frame);
324 } else {
325 WCoreWindow *scan = frame->stacking->above;
326 WWindow *frame_wwin = (WWindow*) frame->descriptor.parent;
328 while (scan) {
330 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
331 WWindow *scan_wwin = (WWindow*) scan->descriptor.parent;
333 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
334 && scan_wwin->flags.mapped) {
335 break;
338 scan = scan->stacking->above;
341 if (scan) {
342 wRaiseFrame(frame);
343 } else {
344 wLowerFrame(frame);
350 void
351 wLowerFrame(WCoreWindow *frame)
353 WScreen *scr=frame->screen_ptr;
354 WCoreWindow *prev, *wlist=frame;
355 int level = frame->stacking->window_level;
356 int i;
358 /* already in bottom */
359 if (wlist->stacking->under==NULL) {
360 return;
362 #ifdef removed
363 if (wPreferences.on_top_transients &&
364 wlist->stacking->under==wlist->stacking->child_of) {
365 return;
367 #else
368 if (wlist->stacking->under==wlist->stacking->child_of) {
369 return;
371 #endif
372 prev = wlist;
373 /* remove from the list */
374 if (scr->stacking_list[level] == frame) {
375 /* it was the top window */
376 scr->stacking_list[level] = frame->stacking->under;
377 scr->stacking_list[level]->stacking->above = NULL;
378 } else {
379 if (frame->stacking->under)
380 frame->stacking->under->stacking->above = frame->stacking->above;
381 if (frame->stacking->above)
382 frame->stacking->above->stacking->under = frame->stacking->under;
384 wlist = scr->stacking_list[level];
385 /* look for place to put this window */
386 #ifdef removed
387 if (wPreferences.on_top_transients)
388 #endif
390 WCoreWindow *owner = frame->stacking->child_of;
392 if (owner != wlist) {
393 while (wlist->stacking->under) {
394 /* if this is a transient, it should not be placed under
395 * it's owner */
396 if (owner == wlist->stacking->under)
397 break;
398 wlist = wlist->stacking->under;
402 #ifdef removed
403 else {
404 while (wlist->stacking->under) {
405 wlist = wlist->stacking->under;
408 #endif
409 /* insert under the place found */
410 frame->stacking->above = wlist;
411 frame->stacking->under = wlist->stacking->under;
412 if (wlist->stacking->under)
413 wlist->stacking->under->stacking->above = frame;
414 wlist->stacking->under = frame;
416 /* try to optimize things a little */
417 if (frame->stacking->above == NULL) {
418 WCoreWindow *above = NULL;
419 for (i=level-1; i>=0; i--) {
420 if (scr->stacking_list[i]!=NULL) {
421 /* can't optimize */
422 above = scr->stacking_list[i];
423 while (above->stacking->under)
424 above = above->stacking->under;
425 break;
428 if (!above) {
429 XLowerWindow(dpy, frame->window);
430 } else {
431 moveFrameToUnder(above, frame);
433 } else {
434 moveFrameToUnder(frame->stacking->above, frame);
436 #ifdef KWM_HINTS
438 WWindow *wwin = wWindowFor(frame->window);
440 if (wwin)
441 wKWMSendEventMessage(wwin, WKWMLowerWindow);
443 #endif
448 *----------------------------------------------------------------------
449 * AddToStackList--
450 * Inserts the frame in the top of the stacking list. The
451 * stacking precedence is obeyed.
453 * Returns:
454 * None
456 * Side effects:
457 * The frame is added to it's screen's window list.
458 *----------------------------------------------------------------------
460 void
461 AddToStackList(WCoreWindow *frame)
463 WCoreWindow *prev, *tmpw, *wlist;
464 int index = frame->stacking->window_level;
466 frame->screen_ptr->window_count++;
467 /* frame->screen_ptr->window_level_count[index]++;*/
468 XSaveContext(dpy, frame->window, wStackContext, (XPointer)frame);
469 tmpw = frame->screen_ptr->stacking_list[index];
470 if (!tmpw) {
471 frame->screen_ptr->stacking_list[index] = frame;
472 frame->stacking->above = NULL;
473 frame->stacking->under = NULL;
474 CommitStacking(frame->screen_ptr);
475 return;
477 prev = tmpw;
478 /* check if this is a transient owner */
479 #ifdef removed
480 if (wPreferences.on_top_transients)
481 #endif
483 WCoreWindow *trans = NULL;
485 wlist = frame->screen_ptr->stacking_list[index];
486 while (wlist) {
487 if (wlist->stacking->child_of == frame)
488 trans = wlist;
489 wlist = wlist->stacking->under;
492 frame->stacking->above = trans;
493 if (trans) {
494 frame->stacking->under = trans->stacking->under;
495 if (trans->stacking->under) {
496 trans->stacking->under->stacking->above = frame;
498 trans->stacking->under = frame;
499 } else {
500 frame->stacking->under = tmpw;
501 tmpw->stacking->above = frame;
502 frame->screen_ptr->stacking_list[index] = frame;
505 #ifdef removed
506 else {
507 /* put on top of the stacking list */
508 frame->stacking->above = NULL;
509 frame->stacking->under = tmpw;
510 tmpw->stacking->above = frame;
511 frame->screen_ptr->stacking_list[index] = frame;
513 #endif
514 CommitStacking(frame->screen_ptr);
519 *----------------------------------------------------------------------
520 * MoveInStackListAbove--
521 * Moves the frame above "next".
523 * Returns:
524 * None
526 * Side effects:
527 * Stacking order may be changed.
528 * Window level for frame may be changed.
529 *----------------------------------------------------------------------
531 void
532 MoveInStackListAbove(WCoreWindow *next, WCoreWindow *frame)
534 WCoreWindow *tmpw;
535 int index;
537 if (!next || frame->stacking->under == next)
538 return;
540 if (frame->stacking->window_level != next->stacking->window_level)
541 ChangeStackingLevel(frame, next->stacking->window_level);
543 index = frame->stacking->window_level;
545 tmpw = frame->screen_ptr->stacking_list[index];
546 if (tmpw == frame)
547 frame->screen_ptr->stacking_list[index] = frame->stacking->under;
548 if (frame->stacking->under)
549 frame->stacking->under->stacking->above = frame->stacking->above;
550 if (frame->stacking->above)
551 frame->stacking->above->stacking->under = frame->stacking->under;
552 if (next->stacking->above)
553 next->stacking->above->stacking->under = frame;
554 frame->stacking->under = next;
555 frame->stacking->above = next->stacking->above;
556 next->stacking->above = frame;
557 if (tmpw == next)
558 frame->screen_ptr->stacking_list[index] = frame;
560 /* try to optimize things a little */
561 if (frame->stacking->above == NULL) {
562 WCoreWindow *above=NULL;
563 int i;
565 for (i=index+1; i<MAX_WINDOW_LEVELS; i++) {
566 if (frame->screen_ptr->stacking_list[i]!=NULL) {
567 /* can't optimize */
568 above = frame->screen_ptr->stacking_list[i];
569 while (above->stacking->under)
570 above = above->stacking->under;
571 break;
574 if (!above) {
575 XRaiseWindow(dpy, frame->window);
576 } else {
577 moveFrameToUnder(above, frame);
579 } else {
580 moveFrameToUnder(frame->stacking->above, frame);
586 *----------------------------------------------------------------------
587 * MoveInStackListUnder--
588 * Moves the frame to under "prev".
590 * Returns:
591 * None
593 * Side effects:
594 * Stacking order may be changed.
595 * Window level for frame may be changed.
596 *----------------------------------------------------------------------
598 void
599 MoveInStackListUnder(WCoreWindow *prev, WCoreWindow *frame)
601 WCoreWindow *tmpw;
602 int index;
604 if (!prev || frame->stacking->above == prev)
605 return;
607 if (frame->stacking->window_level != prev->stacking->window_level)
608 ChangeStackingLevel(frame, prev->stacking->window_level);
610 index = frame->stacking->window_level;
612 tmpw = frame->screen_ptr->stacking_list[index];
613 if (tmpw == frame)
614 frame->screen_ptr->stacking_list[index] = frame->stacking->under;
615 if (frame->stacking->under)
616 frame->stacking->under->stacking->above = frame->stacking->above;
617 if (frame->stacking->above)
618 frame->stacking->above->stacking->under = frame->stacking->under;
619 if (prev->stacking->under)
620 prev->stacking->under->stacking->above = frame;
621 frame->stacking->above = prev;
622 frame->stacking->under = prev->stacking->under;
623 prev->stacking->under = frame;
624 moveFrameToUnder(prev, frame);
628 void
629 RemoveFromStackList(WCoreWindow *frame)
631 int index = frame->stacking->window_level;
633 if (XDeleteContext(dpy, frame->window, wStackContext)==XCNOENT) {
634 #ifdef DEBUG0
635 wwarning("RemoveFromStackingList(): window not in list ");
636 #endif
637 return;
639 /* remove from the window stack list */
640 if (frame->stacking->under)
641 frame->stacking->under->stacking->above = frame->stacking->above;
642 if (frame->stacking->above)
643 frame->stacking->above->stacking->under = frame->stacking->under;
644 else /* this was the first window on the list */
645 frame->screen_ptr->stacking_list[index] = frame->stacking->under;
647 frame->screen_ptr->window_count--;
648 /* frame->screen_ptr->window_level_count[index]--;*/
652 void
653 ChangeStackingLevel(WCoreWindow *frame, int new_level)
655 int old_level;
656 if (frame->stacking->window_level == new_level)
657 return;
658 old_level = frame->stacking->window_level;
660 RemoveFromStackList(frame);
661 frame->stacking->window_level = new_level;
662 AddToStackList(frame);
663 if (old_level > new_level) {
664 wRaiseFrame(frame);
665 } else {
666 wLowerFrame(frame);