-
Notifications
You must be signed in to change notification settings - Fork 4
/
pager.jl
690 lines (616 loc) · 23 KB
/
pager.jl
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#| sawfish.wm.ext.pager -- Code for communicating with C pager
Copyright (C) 2009 Christopher Bratusek <[email protected]>
Copyright (C) 2007 Janek Kozicki <[email protected]>
Copyright (C) 2002 Daniel Pfeiffer <[email protected]>
Copyright (C) 2000 Satyaki Das <[email protected]>
Ryan Lovett <[email protected]>
Andreas Buesching <[email protected]>
Hakon Alstadheim
This file is part of sawfish.
sawfish 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.
sawfish 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 sawfish; see the file COPYING. If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
|#
(define-structure sawfish.wm.ext.pager
(export pager
send-background-file
pager-change-depth
pager-goto
pager-move-window
pager-tooltip
pager-select
pager-hide
pager-unhide)
(open rep
rep.io.files
rep.io.timers
rep.io.processes
rep.structures
rep.system
rep.regexp
sawfish.wm.colors
sawfish.wm.misc
sawfish.wm.custom
sawfish.wm.ext.tooltips
sawfish.wm.fonts
sawfish.wm.stacking
sawfish.wm.util.window-order
sawfish.wm.viewport
sawfish.wm.commands.viewport-extras
sawfish.wm.windows
sawfish.wm.windows.subrs
sawfish.wm.workspace)
;;; Customization code contributed by Ryan Lovett <[email protected]>
(defgroup pager "Pager")
;; closures for out of scope call
(let ((state (lambda () (send-windows)))
(resize (lambda () (send-size t)))
(color (lambda () (send-colors))))
(defcustom pager-show-ignored-windows nil
"Show ignored windows"
:type boolean
:group pager
:after-set state)
(defcustom pager-show-all-workspaces t
"Show all workspaces"
:type boolean
:group pager
:after-set resize)
(defcustom pager-workspaces-per-column 1
"The number of workspaces per column"
:type number
:range (1)
:depends pager-show-all-workspaces
:group pager
:after-set resize)
(defcustom pager-stickies-on-all-workspaces t
"Show (workspace) sticky windows on all their workspaces"
:type boolean
:depends pager-show-all-workspaces
:group pager
:after-set state)
(defcustom pager-stickies-on-all-viewports t
"Show (viewport) sticky windows on all their viewports"
:type boolean
:group pager
:after-set state)
(defcustom pager-shrink-factor 32
"Each length in the pager is this many times shorter than the original"
:type number
:group pager
:after-set resize)
(defcustom pager-focus t
"Button1 focuses the clicked window"
:type boolean
:group pager)
(defcustom pager-warp-cursor nil
"Button1 warps the cursor to the clicked point"
:type boolean
:group pager)
(defcustom pager-color-window (get-color "#8080d0")
"Windows"
:type color
:group pager
:after-set color)
(defcustom pager-color-focus-window (get-color "#58a8ff")
"Window with input focus"
:type color
:group pager
:after-set color)
(defcustom pager-color-window-border (get-color "#606060")
"Window borders"
:type color
:group pager
:after-set color)
(defcustom pager-color-viewport (get-color "#f0f0f0")
"Current viewport color"
:type color
:group pager
:after-set color)
(defcustom pager-background ""
"Pager background, an XPM file"
:type file-name
:tooltip "Create this from a pager screenshot in a paint program."
:group pager
:after-set (lambda () (send-background-file)))
(defcustom pager-color-background (get-color "#d8d8d8")
"Pager background"
:type color
:group pager
:after-set color)
(defcustom pager-color-viewport-divider (get-color "#e8e8e8")
"Lines separating viewports"
:type color
:group pager
:after-set color)
(defcustom pager-color-workspace-divider (get-color "#202020")
"Lines separating workspaces"
:type color
:group pager
:after-set color))
(defcustom pager-hatch-windows nil
"Draw windows using hatching"
:type boolean
:group pager
:after-set (lambda () (send-hatching)))
(defcustom pager-X-current-viewport nil
"Use X to mark current viewport"
:type boolean
:group pager
:after-set (lambda () (send-xmark)))
(defcustom pager-tooltips-enabled t
"When focused, show window name and pager usage."
:type boolean
:group pager)
(defcustom pager-autohide-enable nil
"Whether to autohide the pager and only show it, when
entering a new workspace."
:type boolean
:group pager
:after-set (lambda () (pager-autounhide/workspace)))
(defcustom pager-unhide-when-flip nil
"Also unhide the pager when fliping edges or hitting the screen-border."
:type boolean
:group pager
:after-set (lambda () (pager-autounhide/edge-flip)))
(defcustom pager-unhide-time 5
"How long (in seconds) to show the pager then autohiding it."
:type number
:range ( 3 . 60 )
:group pager)
(defcustom pager-select-type 'workspace
"When scrolling with mouse on pager, select either next
workspace, viewport or none."
:type (choice workspace viewport none)
:group pager)
(defvar pager-executable
(if (file-exists-p "~/.sawfish/sawfishpager")
(concat (user-home-directory) "/.sawfish/sawfishpager")
(concat sawfish-exec-directory "/sawfishpager")))
(defvar pager-output-stream nil
"Pager's output stream.")
;; Remembers the number of workspaces...
(define ws-limits)
;; Remembers the viewport dimensions...
(define vp-rows)
(define vp-columns)
(define vp-width)
(define vp-height)
(define ws-width)
(define ws-height)
(define ws-list)
(define pager-width)
(define pager-height)
(define skip-sending nil)
(define process nil)
(define hooks
'((after-move-hook . send-window)
(after-resize-hook . send-window)
(after-restacking-hook . send-windows)
(enter-workspace-hook . send-viewport)
(destroy-notify-hook . send-windows)
(focus-in-hook . send-focus)
(focus-out-hook . send-focus)
(map-notify-hook . send-windows)
(unmap-notify-hook . send-windows)
(viewport-moved-hook . send-viewport)
(viewport-resized-hook . send-size)
(window-moved-hook . send-window)
(window-resized-hook . send-window)
(window-state-change-hook . send-window)
(workspace-state-change-hook . send-size)))
(define cache)
;;; Internal utilities
;; This is just to scale the window co-ords and dimensions.
(define-macro (scale val #!optional x up)
(if x
(if up
`(inexact->exact (round (/ (* ,val (screen-width)) vp-width)))
`(inexact->exact (round (/ (* ,val vp-width) (screen-width)))))
(if up
`(inexact->exact (round (/ (* ,val (screen-height)) vp-height)))
`(inexact->exact (round (/ (* ,val vp-height) (screen-height)))))))
(define (get-window-info w)
(if (or (not (window-id w))
(not (window-mapped-p w))
(window-get w 'iconified)
(window-get w 'desktop)
(get-x-property w '_NET_WM_STATE_SKIP_PAGER)
(window-get w 'window-list-skip)
(unless pager-show-ignored-windows (window-get w 'ignored)))
0
(let* ((x (car (window-position w)))
(y (cdr (window-position w)))
(dim (window-frame-dimensions w))
;; l1 is a list of elements for each workspace the window
;; is present in. These elements describe the location of
;; that workspace in the pager display. Each one is a
;; list of six values; the first four values are the
;; left, top, right and bottom edges of the workspace.
;; The last two are the viewport-[x,y]-offset coordinates
;; for the currently active viewport in that workspace.
;; The first four values are scaled to the pager display,
;; the offset values are not (to minimize rounding
;; errors).
(l1 (if pager-show-all-workspaces
(or (mapcar
(lambda (ws)
(append
(nth (- ws (car ws-limits)) ws-list)
(if (eql ws current-workspace)
`(,viewport-x-offset ,viewport-y-offset)
(let ((vp-data
(assoc ws workspace-viewport-data)))
(if vp-data
`(,(nth 1 vp-data) ,(nth 2 vp-data))
;; No vp data yet for ws:
'(0 0))))))
(if (and pager-stickies-on-all-workspaces
(window-get w 'sticky))
;; List every workspace:
(let ((limits (workspace-limits))
(l))
(do ((i (cdr limits) (1- i)))
((< i (car limits)) l)
(setq l (cons i l))))
(or (window-workspaces w)
(list (- current-workspace
(car ws-limits)))))))
(list (append (car ws-list)
`(,viewport-x-offset ,viewport-y-offset))))))
(setq dim `(,(max 3 (scale (car dim) 'x)) ,(max 3 (scale (cdr dim)))))
(if (and pager-stickies-on-all-viewports
(or (> vp-rows 1) (> vp-columns 1))
(window-get w 'sticky-viewport))
(let* ((vxo (mod viewport-x-offset (screen-width)))
(vyo (mod viewport-y-offset (screen-height)))
(j1 (- vp-rows (if (> vyo 0) 2 1)))
(wh `(,vp-width ,vp-height)))
(setq x (scale (+ x vxo) 'x)
y (scale (+ y vyo)))
(let loop ((l l1)
(i (- vp-columns (if (> vxo 0) 2 1)))
(j j1)
r)
(cond ((< i 0)
r)
((< j 0)
(loop l (1- i) j1 r))
(l
(loop (cdr l) i j
`((,(window-id w)
,(+ x (* i vp-width) (caar l))
,(+ y (* j vp-height) (cadar l))
,dim
,(+ (scale vxo 'x) (* i vp-width) (caar l))
,(+ (scale vyo) (* j vp-height) (cadar l))
,@wh)
,@r)))
((loop l1 i (1- j) r)))))
(mapcar (lambda (ws)
(let ((vp-x (nth 4 ws))
(vp-y (nth 5 ws)))
`(,(window-id w)
,(+ (scale (+ x vp-x) 'x) (car ws))
,(+ (scale (+ y vp-y)) (cadr ws))
,dim
,(car ws)
,(nth 1 ws)
,(nth 2 ws)
,(nth 3 ws))))
l1)))))
;;; Functions that talk to the C program
;; do a bit of caching to save redundant commands
(define-macro (send f #!rest args)
`(when process
(let ((s (format nil ,(concat f (if (stringp f) "\n" "%s\n")) ,@args))
(c (assq ,(char-downcase (if (stringp f) (aref f 0) f))
cache)))
(unless (equal (cdr c) s)
(write process (setcdr c s))))))
;; Tells the C program to change the colors
(define (send-colors)
(send ?c
(mapcar (lambda (color)
(let ((rgb (color-rgb color)))
(list (elt rgb 0)
(elt rgb 1)
(elt rgb 2))))
(list pager-color-background
pager-color-viewport
pager-color-window
pager-color-focus-window
pager-color-window-border
pager-color-viewport-divider
pager-color-workspace-divider))))
(define (send-hatching)
(send ?h (if pager-hatch-windows 1 0)))
(define (send-xmark)
(send ?x (if pager-X-current-viewport 1 0)))
(define (send-background-file #!optional file)
"Tells the C program to change the pager-background from FILE."
(if file (setq pager-background file))
(send ?b pager-background))
;; Sends window-id of focussed window (or 0 if none is focussed)
(define (send-focus #!rest args)
(declare (unused args))
(send "f%d"
(if (input-focus)
(window-id (input-focus))
0)))
;; calculates all kinds of sizes and tells the pager
(define (send-size #!optional force init)
(let* ((wsl (workspace-limits))
(n (- (cdr wsl) (car wsl)))
(vp-dims
(if pager-show-all-workspaces
;; Maximum viewport dimensions, accross all workspaces:
(let ((dims (cons viewport-dimensions
(mapcar (lambda (e)
(unless (eq (car e)
current-workspace)
(nth 3 e)))
workspace-viewport-data))))
(cons (apply max (mapcar car dims))
(apply max (mapcar cdr dims))))
viewport-dimensions)))
(unless (and (not force)
(equal wsl ws-limits)
(eql (car vp-dims) vp-columns)
(eql (cdr vp-dims) vp-rows))
(setq ws-limits wsl
vp-columns (car vp-dims)
vp-rows (cdr vp-dims)
vp-width (quotient (screen-width) pager-shrink-factor)
vp-height (quotient (screen-height) pager-shrink-factor)
ws-width (1+ (* vp-columns vp-width))
ws-height (1+ (* vp-rows vp-height))
pager-width (if pager-show-all-workspaces
(* ws-width
(ceiling (/ (1+ n) pager-workspaces-per-column)))
ws-width)
pager-height (if pager-show-all-workspaces
(* ws-height
(min (1+ n) pager-workspaces-per-column))
ws-height))
(let ((ws-w `(,(1- ws-width) ,(1- ws-height))))
(let loop ((i n)
r)
(if (< i 0)
(setq ws-list r)
(loop (1- i)
(if pager-show-all-workspaces
`((,(+ 1 (quotient i pager-workspaces-per-column)
(* vp-columns vp-width
(quotient i pager-workspaces-per-column)))
,(+ 1 (mod i pager-workspaces-per-column)
(* vp-rows vp-height
(mod i pager-workspaces-per-column)))
,@ws-w)
,@r)
`((1 1 ,@ws-w) ,@r))))))
(send "s%d %d %d %d %d %d %d"
(if pager-show-all-workspaces 1 0)
vp-width vp-height
ws-width ws-height
pager-width pager-height))
(or init (send-windows))))
;; Send the viewport that is in focus.
(define (send-viewport #!rest args)
(setq args (nth (- current-workspace (car ws-limits)) ws-list))
(send "v%d %d %d %d"
(car args) (cadr args)
(scale viewport-x-offset 'x)
(scale viewport-y-offset))
;; should send only stickies instead, and only depending on options
(send-windows))
;; When only the size or shading of a window changes send only the data
;; pertaining to that window.
(define (send-window w #!rest args)
(if (or (memq (caar args) '(sticky iconified))
(if pager-show-all-workspaces
(cdr (window-workspaces w)))
(if pager-stickies-on-all-viewports
(window-get w 'sticky-viewport))
(and pager-show-all-workspaces
pager-stickies-on-all-workspaces
(window-get w 'sticky)))
(send-windows)
(if skip-sending ;; don't send positions when viewport is changed. Or when window is repositioned.
()
(send ?w (get-window-info w))
)
))
;; Tell the C program what to display. For each window we send five
;; integers: window id, position and dimensions.
(define (send-windows #!rest args)
(declare (unused args))
(send ?W
(mapcar get-window-info
(if pager-show-all-workspaces
(stacking-order)
(filter (lambda (w)
(let ((ws (window-workspaces w)))
(or (null ws)
(member current-workspace ws))))
(stacking-order))))))
(define (pager #!optional plug-to stop)
"This function (re)starts the pager.
Optional PLUG-TO, if set, must be the numerical X id of the window to try
to plug in to.
Optional STOP, if non-nil, stops the pager instead."
(when process
(kill-process process)
(setq process nil))
(unless stop
(setq process
(make-process pager-output-stream
(lambda ()
(and process
(not (process-in-use-p process))
(setq process nil))))
cache
(mapcar list '(?w ?f ?v ?s ?c ?b ?h ?x)))
(if plug-to
(set-process-args process (list (number->string plug-to))))
(start-process process pager-executable)
(send-colors)
(send-hatching)
(send-xmark)
(send-background-file)
(send-size t t)
(send-viewport)
(send-focus)
(condition-case err-info
(mapc (lambda (hook)
(unless (in-hook-p (car hook) (symbol-value (cdr hook)))
(add-hook (car hook) (symbol-value (cdr hook)) t)))
hooks)
(error
(format standard-error "pager: error adding hooks %s\n" err-info)))))
;;; Functions called from C program for 3 buttons and tooltips
(define (pager-goto w x y)
"Change to viewport and/or workspace where the user clicked on the pager."
(setq skip-sending 1)
(let ((ws (if pager-show-all-workspaces
(+ (* pager-workspaces-per-column (quotient x ws-width))
(quotient y ws-height))
current-workspace))
(x1 (scale (1- (mod x ws-width)) 'x 'up))
(y1 (scale (1- (mod y ws-height)) () 'up)))
(setq x (quotient x1 (screen-width))
y (quotient y1 (screen-height)))
(if (eql ws current-workspace)
(set-screen-viewport x y)
(select-workspace-and-viewport ws x y))
(if pager-warp-cursor
(warp-cursor (% x1 (screen-width))
(% y1 (screen-height))))
(and pager-focus
(setq w (get-window-by-id w))
(set-input-focus w)))
(setq skip-sending nil)
)
(define (pager-change-depth w)
"Raise or lower the window clicked on in the pager."
(if (setq w (get-window-by-id w))
(raise-lower-window w)))
(define-macro (bound lower var upper)
`(setq ,var (if (< ,var ,lower)
,lower
(if (> ,var ,upper)
,upper
,var))))
(define (pager-move-window w x y width height mouse-x mouse-y #!optional skip)
"Moves window with id ID to co-ordinates (X, Y) where (X, Y) is what the
pager thinks the position of the window is."
(setq skip-sending skip)
(when (setq w (get-window-by-id w))
(bound (- 4 width) x (- pager-width 4))
(bound (- 4 height) y (- pager-height 4))
(bound 1 mouse-x (1- pager-width))
(bound 1 mouse-y (1- pager-height))
(setq x (- (scale (- (% mouse-x ws-width) (- mouse-x x) 1) 'x 'up)
viewport-x-offset)
y (- (scale (- (% mouse-y ws-height) (- mouse-y y) 1) () 'up)
viewport-y-offset))
(when (window-get w 'sticky-viewport)
(setq x (% x (screen-width))
y (% y (screen-height))))
(move-window-to w x y)
(when pager-show-all-workspaces
(let* ((ws (+ (* pager-workspaces-per-column (quotient mouse-x ws-width))
(quotient mouse-y ws-height)))
(cws (window-workspaces w))
(was-focused (eq (input-focus) w))
(orig-space (if (window-in-workspace-p w current-workspace)
current-workspace
(car cws)))
(new-space (workspace-id-from-logical ws)))
(and cws (null (cdr cws))
(not (eql ws (car cws)))
orig-space
(progn
(copy-window-to-workspace w orig-space new-space nil)
(if (eql orig-space current-workspace)
(delete-window-instance w))
(move-window-to-workspace w orig-space new-space was-focused))))))
(setq skip-sending nil)
)
(define (pager-tooltip #!optional id)
"Show a tooltip for window ID, or remove it if no ID given."
(when pager-tooltips-enabled
(if (and id
(setq id (get-window-by-id id))
(setq id (window-name id)))
(let ((te tooltips-enabled)
(tooltips-enabled t))
(display-tooltip-after-delay
(if te
(concat id "\n\n"
(_ "Button1-Click select viewport (and optionally window)
Button2-Click raise/lower window
Button3-Move drag window"))
id)))
(remove-tooltip))))
(define (pager-select direction)
(if (eq pager-select-type 'workspace)
(progn
(if (eq direction 'previous)
(previous-workspace 1)
(next-workspace 1)))
(if (eq direction 'previous)
(move-viewport-previous)
(move-viewport-next))))
(define (pager-autohide w)
(if (string-match (window-class-name w) "Sawfishpager")
(if pager-autohide-enable
(progn (make-timer (lambda () (pager-hide)) 5)
(if pager-unhide-when-flip
(unless (in-hook-p 'enter-flipper-hook pager-unhide)
(add-hook 'enter-flipper-hook pager-unhide)))
(unless (in-hook-p 'enter-workspace-hook pager-unhide)
(add-hook 'enter-workspace-hook pager-unhide))
(when (in-hook-p 'map-notify-hook pager-autohide)
(remove-hook 'map-notify-hook pager-autohide)))
(make-timer (lambda () (pager-unhide #:permanent t)) 5)
(if pager-unhide-when-flip
(remove-hook 'enter-flipper-hook pager-unhide))
(remove-hook 'enter-workspace-hook pager-unhide))))
(unless batch-mode
(add-hook 'map-notify-hook pager-autohide))
(define (pager-autounhide/workspace)
(if pager-autohide-enable
(progn (pager-hide)
(unless (in-hook-p 'enter-workspace-hook pager-unhide)
(add-hook 'enter-workspace-hook pager-unhide)))
(pager-unhide #:permanent t)
(if (in-hook-p 'enter-workspace-hook pager-unhide)
(remove-hook 'enter-workspace-hook pager-unhide))))
(define (pager-autounhide/edge-flip)
(if pager-unhide-when-flip
(progn (pager-hide)
(unless (in-hook-p 'enter-flipper-hook pager-unhide)
(add-hook 'enter-flipper-hook pager-unhide)))
(pager-unhide)
(remove-hook 'enter-flipper-hook pager-unhide)))
(define (pager-hide)
(hide-window (get-window-by-class "Sawfishpager" #:regex t)))
(define (pager-unhide #!key permanent)
(if permanent
(show-window (get-window-by-class "Sawfishpager" #:regex t))
(show-window (get-window-by-class "Sawfishpager" #:regex t))
(make-timer (lambda () (hide-window (get-window-by-class "Sawfishpager" #:regex t))) pager-unhide-time)))
;; Push this module into the module 'user'.
;; pager.c invokes functions in this module via client-eval which
;; lives in 'user'.
(user-require (structure-name (current-structure))))