File: stacking.jl

package info (click to toggle)
sawfish 1%3A1.3.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 11,636 kB
  • ctags: 1,327
  • sloc: lisp: 22,765; ansic: 15,810; sh: 10,203; makefile: 675; perl: 19
file content (503 lines) | stat: -rw-r--r-- 17,822 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
;; stacking.jl -- window stacking
;; $Id: stacking.jl,v 1.59 2002/10/18 06:46:16 jsh Exp $

;; Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>

;; This file is part of sawmill.

;; sawmill is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; sawmill is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with sawmill; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

(define-structure sawfish.wm.stacking

    (export save-stacking-order
	    mapped-stacking-order
            window-obscured
            stacking-visibility
	    raise-window
	    lower-window
	    stack-window-above
	    stack-window-below
	    restack-window
	    stacking-order-by-depth
	    window-depth
	    set-window-depth
	    window-on-top-p
	    raise-lower-window
	    lower-window-depth
	    raise-window-depth
	    raise-windows
	    lower-windows
	    raise-lower-windows)

    (open rep
	  rep.system
	  sawfish.wm.windows
	  sawfish.wm.commands
	  sawfish.wm.custom
	  sawfish.wm.session.init
	  sawfish.wm.workspace
         sawfish.wm.state.transient
          sawfish.wm.util.rects)

  ;; Each window will have a `depth' property--an integer, zero
  ;; represents the level of normal windows, negative for windows below
  ;; this level, and positive for windows above the normal level

  (defgroup stacking "Stacking" :group misc)

  (defcustom transients-above 'parents
    "Keep transient windows stacked above: \\w"
    :group (misc stacking)
    :type (choice all parents none))

  (defmacro save-stacking-order (#!rest forms)
    "Execute FORMS, then reinstall the original stacking order."
    (let ((tem (gensym)))
      `(let ((,tem (stacking-order)))
	 (unwind-protect
	     (progn ,@forms)
	   (restack-windows ,tem)))))

  ;; this will always return an integer
  (define (window-depth w) (window-get w 'depth))

;;; constraint mechanics (predicates actually..)

  (define (stacking-constraint:layer w)
    (let ((depth (window-depth w)))
      (lambda (above below)
	;; constraints are met locally in ABOVE and BELOW, so just
	;; test against windows adjacent to W
	(and (or (null above)
		 (<= depth (window-depth (car above))))
	     (or (null below)
		 (>= depth (window-depth (car below))))))))

  (define (stacking-constraint:transients-above-parent w)
    (let ((parents (transient-parents w))
	  (children (transient-children w)))

      ;; ignore parents with depth > W, ignore children with depth < W
      (let ((w-depth (window-depth w)))
	(setq parents (delete-if (lambda (x)
				   (> (window-depth x) w-depth)) parents))
	(setq children (delete-if (lambda (x)
				    (< (window-depth x) w-depth)) children)))
				 
      (lambda (above below)
	(and (or (null parents)
		 ;; All parents must be below W
		 (let loop ((rest parents))
		   (cond ((null rest) t)
			 ((memq (car rest) above) nil)
			 (t (loop (cdr rest))))))
	     (or (null children)
		 ;; All children must be above W
		 (let loop ((rest children))
		   (cond ((null rest) t)
			 ((memq (car rest) below) nil)
			 (t (loop (cdr rest))))))))))

  (define (stacking-constraint:transients-above-all w)
    (let ((w-depth (window-depth w)))
      (if (window-transient-p w)
	  ;; ensure there are no normal windows above W, unless they
	  ;; have depth > W
	  (lambda (above below)
	    (declare (unused below))
	    (let loop ((rest above))
	      (cond ((null rest) t)
		    ((and (not (window-transient-p (car rest)))
			  (<= (window-depth (car rest)) w-depth)) nil)
		    (t (loop (cdr rest))))))
	;; ensure no transients below W, unless they're depth < W
	(lambda (above below)
	  (declare (unused above))
	  (let loop ((rest below))
	    (cond ((null rest) t)
		  ((and (window-transient-p (car rest))
			(>= (window-depth (car rest)) w-depth)) nil)
		  (t (loop (cdr rest)))))))))

  (define (combine-constraints constraints)
    "Combine the list of secondary constraint functions into a single
function (using logical `and' combinator)."
    (lambda (above below)
      (let loop ((rest constraints))
	(cond ((null rest) t)
	      ((not ((car rest) above below)) nil)
	      (t (loop (cdr rest)))))))

  (defvar basic-stacking-constraints (list stacking-constraint:layer)
    "List of stacking constraint functions to adhere to (excluding transient
stacking modes controlled by `transients-above' option).")

  (define (make-constraint w)
    "Return the constraint function to use when stacking window W, a function
of two arguments, the list of windows above W (from bottom to top) and the
list of windows below W (from top to bottom). If the constraint holds
for a particular stacking configuration, it will return non-`nil'."
    (combine-constraints
     (mapcar (lambda (c) (c w))
	     (case (or (window-get w 'transients-above) transients-above)
	       ((parents)
		(cons stacking-constraint:transients-above-parent
		      basic-stacking-constraints))
	       ((all)
		(cons stacking-constraint:transients-above-all
		      basic-stacking-constraints))
	       (t basic-stacking-constraints)))))

;;; utilities

  ;; returns (BEFORE . AFTER), modifies LST
  (define (break-window-list lst pivot)
    "Given a list of windows LST and a window PIVOT, return a cons cell
containing two lists, the windows that occur in LST before PIVOT does (in
reverse order), and the list of windows occurring in LST after PIVOT.

LST is destructively modified by this procedure."
    (let loop ((rest lst)
	       (before '()))
      (cond ((null rest)
	     (cons before rest))
	    ((eq (car rest) pivot)
	     (cons before (cdr rest)))
	    (t
	     (let ((next (cdr rest)))
	       (rplacd rest before)
	       (loop next rest))))))

  (define (stack-rotate-upwards cell)
    "Given a cons cell containing two lists of windows `(ABOVE . BELOW)',
rotate the lists so that the first element of BELOW becomes the first
element of ABOVE. The lists are modified. Returns `nil' if BELOW is
the empty list."
    (if (null (car cell))
	nil
      (let ((next (car cell)))
	(rplaca cell (cdar cell))
	(rplacd next (cdr cell))
	(rplacd cell next)
	cell)))

  (define (stack-rotate-downwards cell)
    "Given a cons cell containing two lists of windows `(ABOVE . BELOW)',
rotate the lists so that the first element of ABOVE becomes the first
element of BELOW. The lists are modified. Returns `nil' if ABOVE is
the empty list."
    (if (null (cdr cell))
	nil
      (let ((next (cdr cell)))
	(rplacd cell (cddr cell))
	(rplacd next (car cell))
	(rplaca cell next)
	cell)))

  (define (mapped-stacking-order)
    (delete-if-not window-mapped-p (stacking-order)))

  (define (window-obscured window)
    "Check whether WINDOW is obscured.  Return `t' if WINDOW is fully obscured
by some other window, otherwise a list of windows partially obscuring WINDOW.
In particular return `nil' if WINDOW is unobscured.  Note that if a list of
partially obscuring windows is returned, taken together they may or may not
fully obscure WINDOW."
    (define w window)
    (define ws (nearest-workspace-with-window w current-workspace))
    (let loop ((stack (stacking-order))
               (obs nil))
         (if (null stack)               ; Should not happen
             obs
           (let ((w2 (car stack)))
             (cond ((eq w2 w) obs)
                   ((and (window-visible-p w2)
                         (window-appears-in-workspace-p w2 ws))
                    (case (apply rect-obscured
                                 (rectangles-from-windows (list w w2)))
                      ((unobscured) (loop (cdr stack) obs))
                      ((fully-obscured) t)
                      (t (loop (cdr stack) (cons w2 obs))))) ; Partially
                   (t (loop (cdr stack) obs)))))))

  (define (stacking-visibility window)
    "Compute the visibility of WINDOW from the stacking order.  This should
work even with the Composite extension, which appears to disable
VisibilityNotify events.  Note that deciding between fully and partially
obscured may require quite a bit of computation.  If you do not need that
distinction, window-obscured should be faster."
    (define (rect-list-minus rs s tail)
      (if (null rs)
          tail
        (rect-list-minus (cdr rs) s
                         (rect-minus (car rs) s tail))))
    (let ((obs (window-obscured window)))
      (case obs
        ((t) 'fully-obscured)
        ((()) 'unobscured)
        (t
         (do ((unobs (rectangles-from-windows (list window))
                     (rect-list-minus unobs (car robs) nil))
              (robs (rectangles-from-windows obs) (cdr robs)))
             ((or (null unobs) (null robs))
              (if (null unobs)
                  'fully-obscured
                'partially-obscured)))))))


;;; stacking functions

  (define (raise-window w)
    "Raise the window to its highest allowed position in the stacking order."
    ;; work downwards from top
    (let ((constraint (make-constraint w))
	  (stack (cons '() (delq w (stacking-order)))))
      (let loop ()
	(cond ((constraint (car stack) (cdr stack))
	       (if (car stack)
		   (x-lower-window w (car (car stack)))
		 (x-raise-window w (car (cdr stack)))))
	      ((null (cdr stack))
	       ;; no position
	       nil)
	      (t
	       (stack-rotate-downwards stack)
	       (loop))))))

  (define (lower-window w)
    "Lower the window to its lowest allowed position in the stacking order."
    (let ((constraint (make-constraint w))
	  (stack (cons (nreverse (delq w (stacking-order))) '())))
      ;; work upwards from bottom
      (let loop ()
	(cond ((constraint (car stack) (cdr stack))
	       ;; found the lowest position
	       (if (cdr stack)
		   (x-raise-window w (car (cdr stack)))
		 (x-lower-window w (car (car stack)))))
	      ((null (car stack))
	       ;; no possible position..
	       nil)
	      (t
	       (stack-rotate-upwards stack)
	       (loop))))))

  (define (stack-window-above above below)
    "Change the stacking of window ABOVE so that it is as closely above window
BELOW as possible."
    (let ((constraint (make-constraint above))
	  (stack (break-window-list
		  (delq above (stacking-order)) below)))
      (rplacd stack (cons below (cdr stack)))
      (let loop ()
	(cond ((constraint (car stack) (cdr stack))
	       ;; found a suitable position
	       (if (car stack)
		   (x-lower-window above (car (car stack)))
		 (x-raise-window above (car (cdr stack)))))
	      ((null (car stack))
	       ;; reached the top
	       nil)
	      (t
	       (stack-rotate-upwards stack)
	       (loop))))))

  (define (stack-window-below below above)
    "Change the stacking of window BELOW so that it is as closely below window
ABOVE as possible."
    (let ((constraint (make-constraint below))
	  (stack (break-window-list
		  (delq below (stacking-order)) above)))
      (rplaca stack (cons above (car stack)))
      (let loop ()
	(cond ((constraint (car stack) (cdr stack))
	       (if (cdr stack)
		   (x-raise-window below (car (cdr stack)))
		 (x-lower-window below (car (car stack)))))
	      ((null (cdr stack))
	       ;; reached the bottom
	       nil)
	      (t
	       (stack-rotate-downwards stack)
	       (loop))))))

  (define (restack-window w)
    "Assuming that the current stacking order is in a consistent state
except, possibly, for the position of window W, restore the consistent
state including window W. This is achieved by raising or lowering
window W as appropriate."
    (let ((constraint (make-constraint w))
	  (stack (break-window-list (stacking-order) w)))
      (unless (constraint (car stack) (cdr stack))
	(if (< (length (car stack)) (length (cdr stack)))
	    (raise-window w)
	  (lower-window w)))))

  (define (stacking-order-by-depth depth)
    "Return a list of windows containing only those in depth DEPTH, in the
order they are stacked within the layer (top to bottom)."
    (let ((order (stacking-order)))
      (delete-if (lambda (x)
		   (/= (window-depth x) depth)) order)))

  (define (set-window-depth w depth)
    "Set the stacking depth of window W to DEPTH."
    (let ((old (window-depth w)))
      (window-put w 'depth depth)
      (cond ((> old depth)
	     ;; window's going downwards
	     (raise-window w))
	    ((< old depth)
	     ;; window's going upwards
	     (lower-window w)))
      (call-window-hook 'window-depth-change-hook w (list depth))
      (call-window-hook 'window-state-change-hook w (list '(stacking)))))

  (define (window-on-top-p w)
    "Return t if window W is as high as it can legally go in the stacking order."
    (let* ((constraint (make-constraint w))
	   (order (delete-if-not
		   ;; XXX doesn't handle viewports..
		   (let ((space (nearest-workspace-with-window
				 w current-workspace)))
		     (lambda (x)
		       (window-appears-in-workspace-p x space)))
		   (stacking-order)))
	   (old-posn (- (length order) (length (memq w order))))
	   (stack (cons '() (delq w order))))
      (let loop ()
	(if (or (constraint (car stack) (cdr stack))
		(null (cdr stack)))
	    ;; found highest position
	    (= (length (car stack)) old-posn)
	  (stack-rotate-downwards stack)
	  (loop)))))

  (define (raise-lower-window w)
    "If the window is at its highest possible position, then lower it to its
lowest possible position. Otherwise raise it as far as allowed."
    (if (or (not (window-obscured w))
	    (window-on-top-p w))
	(lower-window w)
      (raise-window w)))

  (define (lower-window-depth w)
    "Put the window in the stacking level beneath its current level."
    (set-window-depth w (1- (window-depth w))))

  (define (raise-window-depth w)
    "Put the window in the stacking level above its current level."
    (set-window-depth w (1+ (window-depth w))))

  (define-command 'raise-single-window raise-window
    #:spec "%W" #:class 'advanced)
  (define-command 'lower-single-window lower-window
    #:spec "%W" #:class 'advanced)
  (define-command 'raise-lower-single-window raise-lower-window
    #:spec "%W" #:class 'advanced)
  (define-command 'raise-window-depth raise-window-depth #:spec "%W")
  (define-command 'lower-window-depth lower-window-depth #:spec "%W")

;;; stacking groups of windows

  ;; Helper function for raise-windows and lower-windows. Calls (FIRST
  ;; X) on the first element X of ORDER, then (SUCCESSOR Y X) on each
  ;; pair of adjacent elements X and Y

  (define (apply-group-order order first successor)
    (when order
      (first (car order))
      (do ((rest order (cdr rest)))
	  ((null (cdr rest)))
	(successor (cadr rest) (car rest)))))

  (define (raise-windows w order)
    ;; STRATEGY: take every window in ORDER to its highest possible
    ;; position, giving W priority but otherwise obeying ORDER, which
    ;; must be a valid stacking configuration (highest-to-lowest).  Do
    ;; the restacking in such a way that no part of any window in
    ;; ORDER is exposed during the restacking that is not exposed
    ;; afterwards. 

    (define (highest-window order)
      ;; find the first window in ORDER that is allowed 
      ;; to be above all other windows in ORDER...
      (let loop ((rest order))
	(cond ((null rest) nil)
	      (((make-constraint (car rest)) '() (remq (car rest) order))
	       (car rest))
	      (t (loop (cdr rest))))))

    ;; Cons up the new order (in reverse), by picking the most-raisable
    ;; each time until no windows are left, then commit that ordering
    (let loop ((rest (cons w (remq w order)))
	       (out '()))
      (if (null rest)
	  (apply-group-order (nreverse out) raise-window stack-window-below)
	(let ((highest (or (highest-window rest)
			   (error "Stacking constraint failed"))))
	  (loop (delq highest rest) (cons highest out))))))

  (define (lower-windows w order)

    (define (lowest-window order)
      ;; find the last window in ORDER that may be below all other
      ;; windows in ORDER
      (let ((reversed (reverse order)))
	(let loop ((rest reversed))
	  (cond ((null rest) nil)
		(((make-constraint (car rest))
		  (remq (car rest) reversed) '()) (car rest))
		(t (loop (cdr rest)))))))

    (let loop ((rest (nconc (remq w order) (list w)))
	       (out '()))
      (if (null rest)
	  (apply-group-order (nreverse out) lower-window stack-window-above)
	(let ((lowest (or (lowest-window rest)
			  (error "Stacking constraint failed"))))
	  (loop (delq lowest rest) (cons lowest out))))))

  (define (raise-lower-windows w order)
    (if (or (not (window-obscured w))
	    (and (window-on-top-p (car order))
		 ;; look for the group as a block.. this is a heuristic
		 (let loop ((rest (memq (car order) (stacking-order))))
		   (cond ((null rest) nil)
			 ((eq (car rest) w) t)
			 ((memq (car rest) order) (loop (cdr rest)))
			 (t nil)))))
	(lower-windows w order)
      (raise-windows w order)))

;;; hooks

  ;; Called from the add-window-hook
  (define (stacking-add-window w)
    (unless (window-get w 'depth)
      (let ((parents (transient-parents w)))
	(if parents
	    ;; put dialogs in at least as high a layer as their
	    ;; highest parent, but never below the default depth
	    (window-put w 'depth (apply max 0 (mapcar window-depth parents)))
	  ;; default depth
	  (window-put w 'depth 0)))))

  (add-hook 'add-window-hook stacking-add-window t)
  (add-hook 'after-add-window-hook restack-window t)
  (add-hook 'map-notify-hook restack-window t)

  (sm-add-saved-properties 'depth)
  (add-swapped-properties 'depth))