Change to the linux kernel coding style
[wmaker-crm.git] / src / stacking.c
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.
11 *
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.
16 *
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.
21 */
22
23 #include "wconfig.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30
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"
39
40 /*** Global Variables ***/
41 extern XContext wStackContext;
42
43 extern WPreferences wPreferences;
44
45 static void notifyStackChange(WCoreWindow * frame, char *detail)
46 {
47 WWindow *wwin = wWindowFor(frame->window);
48
49 WMPostNotificationName(WMNChangedStacking, wwin, detail);
50 }
51
52 /*
53 *----------------------------------------------------------------------
54 * RemakeStackList--
55 * Remakes the stacking_list for the screen, getting the real
56 * stacking order from the server and reordering windows that are not
57 * in the correct stacking.
58 *
59 * Side effects:
60 * The stacking order list and the actual window stacking
61 * may be changed (corrected)
62 *
63 *----------------------------------------------------------------------
64 */
65 void RemakeStackList(WScreen * scr)
66 {
67 Window *windows;
68 unsigned int nwindows;
69 Window junkr, junkp;
70 WCoreWindow *frame;
71 WCoreWindow *tmp;
72 int level;
73 int i, c;
74
75 if (!XQueryTree(dpy, scr->root_win, &junkr, &junkp, &windows, &nwindows)) {
76 wwarning(_("could not get window list!!"));
77 return;
78 } else {
79 WMEmptyBag(scr->stacking_list);
80
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;
87 }
88 if (!frame)
89 continue;
90 c++;
91 level = frame->stacking->window_level;
92 tmp = WMGetFromBag(scr->stacking_list, level);
93 if (tmp)
94 tmp->stacking->above = frame;
95 frame->stacking->under = tmp;
96 frame->stacking->above = NULL;
97 WMSetInBag(scr->stacking_list, level, frame);
98 }
99 XFree(windows);
100 #ifdef DEBUG
101 if (c != scr->window_count) {
102 puts("Found different number of windows than in window lists!!!");
103 }
104 #endif
105 scr->window_count = c;
106 }
107
108 CommitStacking(scr);
109 }
110
111 /*
112 *----------------------------------------------------------------------
113 * CommitStacking--
114 * Reorders the actual window stacking, so that it has the stacking
115 * order in the internal window stacking lists. It does the opposite
116 * of RemakeStackList().
117 *
118 * Side effects:
119 * Windows may be restacked.
120 *----------------------------------------------------------------------
121 */
122 void CommitStacking(WScreen * scr)
123 {
124 WCoreWindow *tmp;
125 int nwindows, i;
126 Window *windows;
127 WMBagIterator iter;
128
129 nwindows = scr->window_count;
130 windows = wmalloc(sizeof(Window) * nwindows);
131
132 i = 0;
133 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
134 while (tmp) {
135 #ifdef DEBUG
136 if (i >= nwindows) {
137 puts("Internal inconsistency! window_count is incorrect!!!");
138 printf("window_count says %i windows\n", nwindows);
139 wfree(windows);
140 return;
141 }
142 #endif
143 windows[i++] = tmp->window;
144 tmp = tmp->stacking->under;
145 }
146 }
147 XRestackWindows(dpy, windows, i);
148 wfree(windows);
149
150 #ifdef VIRTUAL_DESKTOP
151 wWorkspaceRaiseEdge(scr);
152 #endif
153
154 WMPostNotificationName(WMNResetStacking, scr, NULL);
155 }
156
157 /*
158 *----------------------------------------------------------------------
159 * moveFrameToUnder--
160 * Reestacks windows so that "frame" is under "under".
161 *
162 * Returns:
163 * None
164 *
165 * Side effects:
166 * Changes the stacking order of frame.
167 *----------------------------------------------------------------------
168 */
169 static void moveFrameToUnder(WCoreWindow * under, WCoreWindow * frame)
170 {
171 Window wins[2];
172
173 wins[0] = under->window;
174 wins[1] = frame->window;
175 XRestackWindows(dpy, wins, 2);
176 }
177
178 /*
179 *----------------------------------------------------------------------
180 * wRaiseFrame--
181 * Raises a frame taking the window level into account.
182 *
183 * Returns:
184 * None
185 *
186 * Side effects:
187 * Window stacking order and stacking list are changed.
188 *
189 *----------------------------------------------------------------------
190 */
191 void wRaiseFrame(WCoreWindow * frame)
192 {
193 WCoreWindow *wlist = frame;
194 int level = frame->stacking->window_level;
195 WScreen *scr = frame->screen_ptr;
196
197 /* already on top */
198 if (frame->stacking->above == NULL) {
199 return;
200 }
201
202 /* insert it on top of other windows on the same level */
203 if (frame->stacking->under)
204 frame->stacking->under->stacking->above = frame->stacking->above;
205 if (frame->stacking->above)
206 frame->stacking->above->stacking->under = frame->stacking->under;
207
208 frame->stacking->above = NULL;
209 frame->stacking->under = WMGetFromBag(scr->stacking_list, level);
210 if (frame->stacking->under) {
211 frame->stacking->under->stacking->above = frame;
212 }
213 WMSetInBag(scr->stacking_list, level, frame);
214
215 /* raise transients under us from bottom to top
216 * so that the order is kept */
217 again:
218 wlist = frame->stacking->under;
219 while (wlist && wlist->stacking->under)
220 wlist = wlist->stacking->under;
221 while (wlist && wlist != frame) {
222 if (wlist->stacking->child_of == frame) {
223 wRaiseFrame(wlist);
224 goto again;
225 }
226 wlist = wlist->stacking->above;
227 }
228
229 /* try to optimize things a little */
230 if (frame->stacking->above == NULL) {
231 WMBagIterator iter;
232 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
233 int i, last = above->stacking->window_level;
234
235 /* find the 1st level above us which has windows in it */
236 for (i = level + 1, above = NULL; i <= last; i++) {
237 above = WMGetFromBag(scr->stacking_list, i);
238 if (above != NULL)
239 break;
240 }
241
242 if (above != frame && above != NULL) {
243 while (above->stacking->under)
244 above = above->stacking->under;
245 moveFrameToUnder(above, frame);
246 } else {
247 /* no window above us */
248 above = NULL;
249 XRaiseWindow(dpy, frame->window);
250 }
251 } else {
252 moveFrameToUnder(frame->stacking->above, frame);
253 }
254
255 notifyStackChange(frame, "raise");
256
257 #ifdef VIRTUAL_DESKTOP
258 wWorkspaceRaiseEdge(scr);
259 #endif
260 }
261
262 void wRaiseLowerFrame(WCoreWindow * frame)
263 {
264 if (!frame->stacking->above
265 || (frame->stacking->window_level != frame->stacking->above->stacking->window_level)) {
266
267 wLowerFrame(frame);
268 } else {
269 WCoreWindow *scan = frame->stacking->above;
270 WWindow *frame_wwin = (WWindow *) frame->descriptor.parent;
271
272 while (scan) {
273
274 if (scan->descriptor.parent_type == WCLASS_WINDOW) {
275 WWindow *scan_wwin = (WWindow *) scan->descriptor.parent;
276
277 if (wWindowObscuresWindow(scan_wwin, frame_wwin)
278 && scan_wwin->flags.mapped) {
279 break;
280 }
281 }
282 scan = scan->stacking->above;
283 }
284
285 if (scan) {
286 wRaiseFrame(frame);
287 } else {
288 wLowerFrame(frame);
289 }
290 }
291 }
292
293 void wLowerFrame(WCoreWindow * frame)
294 {
295 WScreen *scr = frame->screen_ptr;
296 WCoreWindow *wlist = frame;
297 int level = frame->stacking->window_level;
298
299 /* already in bottom */
300 if (wlist->stacking->under == NULL) {
301 return;
302 }
303 /* cant lower transient below below its owner */
304 if (wlist->stacking->under == wlist->stacking->child_of) {
305 return;
306 }
307 /* remove from the list */
308 if (WMGetFromBag(scr->stacking_list, level) == frame) {
309 /* it was the top window */
310 WMSetInBag(scr->stacking_list, level, frame->stacking->under);
311 frame->stacking->under->stacking->above = NULL;
312 } else {
313 if (frame->stacking->under)
314 frame->stacking->under->stacking->above = frame->stacking->above;
315 if (frame->stacking->above)
316 frame->stacking->above->stacking->under = frame->stacking->under;
317 }
318 wlist = WMGetFromBag(scr->stacking_list, level);
319
320 /* look for place to put this window */
321 if (wlist) {
322 WCoreWindow *owner = frame->stacking->child_of;
323
324 if (owner != wlist) {
325 while (wlist->stacking->under) {
326 /* if this is a transient, it should not be placed under
327 * it's owner */
328 if (owner == wlist->stacking->under)
329 break;
330 wlist = wlist->stacking->under;
331 }
332 }
333 }
334 /* insert under the place found */
335 frame->stacking->above = wlist;
336 if (wlist) {
337 frame->stacking->under = wlist->stacking->under;
338 if (wlist->stacking->under)
339 wlist->stacking->under->stacking->above = frame;
340 wlist->stacking->under = frame;
341 } else {
342 frame->stacking->under = NULL;
343 }
344
345 if (frame->stacking->above == NULL) {
346 WMBagIterator iter;
347 WCoreWindow *above = WMBagLast(scr->stacking_list, &iter);
348 int i, last = above->stacking->window_level;
349
350 /* find the 1st level above us which has windows in it */
351 for (i = level + 1, above = NULL; i <= last; i++) {
352 above = WMGetFromBag(scr->stacking_list, i);
353 if (above != NULL)
354 break;
355 }
356
357 if (above != frame && above != NULL) {
358 while (above->stacking->under)
359 above = above->stacking->under;
360 moveFrameToUnder(above, frame);
361 } else {
362 /* no window below us */
363 XLowerWindow(dpy, frame->window);
364 }
365 } else {
366 moveFrameToUnder(frame->stacking->above, frame);
367 }
368
369 notifyStackChange(frame, "lower");
370
371 #ifdef VIRTUAL_DESKTOP
372 wWorkspaceRaiseEdge(scr);
373 #endif
374 }
375
376 /*
377 *----------------------------------------------------------------------
378 * AddToStackList--
379 * Inserts the frame in the top of the stacking list. The
380 * stacking precedence is obeyed.
381 *
382 * Returns:
383 * None
384 *
385 * Side effects:
386 * The frame is added to it's screen's window list.
387 *----------------------------------------------------------------------
388 */
389 void AddToStackList(WCoreWindow * frame)
390 {
391 WCoreWindow *curtop, *wlist;
392 int index = frame->stacking->window_level;
393 WScreen *scr = frame->screen_ptr;
394 WCoreWindow *trans = NULL;
395
396 frame->screen_ptr->window_count++;
397 XSaveContext(dpy, frame->window, wStackContext, (XPointer) frame);
398 curtop = WMGetFromBag(scr->stacking_list, index);
399
400 /* first window in this level */
401 if (curtop == NULL) {
402 WMSetInBag(scr->stacking_list, index, frame);
403 frame->stacking->above = NULL;
404 frame->stacking->under = NULL;
405 CommitStacking(scr);
406 return;
407 }
408
409 /* check if this is a transient owner */
410 wlist = curtop;
411 while (wlist) {
412 if (wlist->stacking->child_of == frame)
413 trans = wlist;
414 wlist = wlist->stacking->under;
415 }
416 /* trans will hold the transient in the lowest position
417 * in stacking list */
418
419 frame->stacking->above = trans;
420 if (trans != NULL) {
421 /* window is owner of a transient.. put it below
422 * the lowest transient */
423 frame->stacking->under = trans->stacking->under;
424 if (trans->stacking->under) {
425 trans->stacking->under->stacking->above = frame;
426 }
427 trans->stacking->under = frame;
428 } else {
429 /* window is not owner of transients.. just put it in the
430 * top of other windows */
431 frame->stacking->under = curtop;
432 curtop->stacking->above = frame;
433 WMSetInBag(scr->stacking_list, index, frame);
434 }
435 CommitStacking(scr);
436 }
437
438 /*
439 *----------------------------------------------------------------------
440 * MoveInStackListAbove--
441 * Moves the frame above "next".
442 *
443 * Returns:
444 * None
445 *
446 * Side effects:
447 * Stacking order may be changed.
448 * Window level for frame may be changed.
449 *----------------------------------------------------------------------
450 */
451 void MoveInStackListAbove(WCoreWindow * next, WCoreWindow * frame)
452 {
453 WCoreWindow *tmpw;
454 WScreen *scr = frame->screen_ptr;
455 int index;
456
457 if (!next || frame->stacking->under == next)
458 return;
459
460 if (frame->stacking->window_level != next->stacking->window_level)
461 ChangeStackingLevel(frame, next->stacking->window_level);
462
463 index = frame->stacking->window_level;
464
465 tmpw = WMGetFromBag(scr->stacking_list, index);
466 if (tmpw == frame)
467 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
468 if (frame->stacking->under)
469 frame->stacking->under->stacking->above = frame->stacking->above;
470 if (frame->stacking->above)
471 frame->stacking->above->stacking->under = frame->stacking->under;
472 if (next->stacking->above)
473 next->stacking->above->stacking->under = frame;
474 frame->stacking->under = next;
475 frame->stacking->above = next->stacking->above;
476 next->stacking->above = frame;
477 if (tmpw == next)
478 WMSetInBag(scr->stacking_list, index, frame);
479
480 /* try to optimize things a little */
481 if (frame->stacking->above == NULL) {
482 WCoreWindow *above = NULL;
483 WMBagIterator iter;
484
485 for (above = WMBagIteratorAtIndex(scr->stacking_list, index + 1, &iter);
486 above != NULL; above = WMBagNext(scr->stacking_list, &iter)) {
487
488 /* can't optimize */
489 while (above->stacking->under)
490 above = above->stacking->under;
491 break;
492 }
493 if (above == NULL) {
494 XRaiseWindow(dpy, frame->window);
495 } else {
496 moveFrameToUnder(above, frame);
497 }
498 } else {
499 moveFrameToUnder(frame->stacking->above, frame);
500 }
501
502 WMPostNotificationName(WMNResetStacking, scr, NULL);
503
504 #ifdef VIRTUAL_DESKTOP
505 wWorkspaceRaiseEdge(scr);
506 #endif
507 }
508
509 /*
510 *----------------------------------------------------------------------
511 * MoveInStackListUnder--
512 * Moves the frame to under "prev".
513 *
514 * Returns:
515 * None
516 *
517 * Side effects:
518 * Stacking order may be changed.
519 * Window level for frame may be changed.
520 *----------------------------------------------------------------------
521 */
522 void MoveInStackListUnder(WCoreWindow * prev, WCoreWindow * frame)
523 {
524 WCoreWindow *tmpw;
525 int index;
526 WScreen *scr = frame->screen_ptr;
527
528 if (!prev || frame->stacking->above == prev)
529 return;
530
531 if (frame->stacking->window_level != prev->stacking->window_level)
532 ChangeStackingLevel(frame, prev->stacking->window_level);
533
534 index = frame->stacking->window_level;
535
536 tmpw = WMGetFromBag(scr->stacking_list, index);
537 if (tmpw == frame)
538 WMSetInBag(scr->stacking_list, index, frame->stacking->under);
539 if (frame->stacking->under)
540 frame->stacking->under->stacking->above = frame->stacking->above;
541 if (frame->stacking->above)
542 frame->stacking->above->stacking->under = frame->stacking->under;
543 if (prev->stacking->under)
544 prev->stacking->under->stacking->above = frame;
545 frame->stacking->above = prev;
546 frame->stacking->under = prev->stacking->under;
547 prev->stacking->under = frame;
548 moveFrameToUnder(prev, frame);
549
550 WMPostNotificationName(WMNResetStacking, scr, NULL);
551 }
552
553 void RemoveFromStackList(WCoreWindow * frame)
554 {
555 int index = frame->stacking->window_level;
556
557 if (XDeleteContext(dpy, frame->window, wStackContext) == XCNOENT) {
558 wwarning("RemoveFromStackingList(): window not in list ");
559 return;
560 }
561 /* remove from the window stack list */
562 if (frame->stacking->under)
563 frame->stacking->under->stacking->above = frame->stacking->above;
564 if (frame->stacking->above)
565 frame->stacking->above->stacking->under = frame->stacking->under;
566 else /* this was the first window on the list */
567 WMSetInBag(frame->screen_ptr->stacking_list, index, frame->stacking->under);
568
569 frame->screen_ptr->window_count--;
570
571 WMPostNotificationName(WMNResetStacking, frame->screen_ptr, NULL);
572 }
573
574 void ChangeStackingLevel(WCoreWindow * frame, int new_level)
575 {
576 int old_level;
577
578 if (frame->stacking->window_level == new_level)
579 return;
580 old_level = frame->stacking->window_level;
581
582 RemoveFromStackList(frame);
583 frame->stacking->window_level = new_level;
584 AddToStackList(frame);
585 if (old_level > new_level) {
586 wRaiseFrame(frame);
587 } else {
588 wLowerFrame(frame);
589 }
590 }