-
Notifications
You must be signed in to change notification settings - Fork 6
/
fdump.lisp
375 lines (341 loc) · 12.3 KB
/
fdump.lisp
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
;; Copyright (C) 2007-2008 Jonathan Liles, Shawn Betts
;; Copyright (C) 2010-2011 Alexander aka CosmonauT Vynnyk
;;
;; This file is part of dswm.
;;
;; dswm 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.
;; dswm 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 this software; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;; Boston, MA 02111-1307 USA
;; Commentary:
;; Code:
(in-package #:dswm)
(export '(ddump
ddump-current
ddump-screens
dump-desktop-to-file
dump-group-to-file
dump-screen-to-file
fdump
fdump-current
fdump-height
fdump-number
fdump-width
fdump-windows
fdump-x
fdump-y
fgdump
fgdump-name
fgdump-number
gdump
gdump-current
gdump-name
gdump-number
gdump-tree
place-existing-windows
restore
restore-all-rules
restore-desktop-snapshot
save-all-rules
save-frame+group-rules
sdump
sdump-current
sdump-groups
sdump-number
snapshot-desktop))
(defstruct fdump
number x y width height windows current)
;; group dump
(defstruct gdump
number name tree current)
(defstruct fgdump
number name)
;; screen dump
(defstruct sdump
number groups current)
;; desktop dump
(defstruct ddump
screens current)
(defun dump-structure (structure to-fs file &optional backup-p)
"Dump some code, values etc to file or just to output"
(if to-fs
(progn
(when (and backup-p (file-exists-p file))
(copy-file
file
(merge-pathnames
(make-pathname :type (concat (pathname-type file) "~")) file)
:overwrite t))
(with-open-file (fp file :direction :output :if-exists :supersede)
(with-standard-io-syntax
(let ((*package* (find-package :dswm))
(*print-pretty* t))
(prin1 structure fp))))
structure)
structure))
(defun dump-group (group &key (window-dump-fn 'window-id)
(to-fs nil)
(file
(data-dir-file
(concat "group-" (prin1-to-string (group-name group))) "rules"))
(backup-p nil))
(labels ((dump (f)
(make-fdump
:windows (mapcar window-dump-fn (frame-windows group f))
:current (and (frame-window f)
(funcall window-dump-fn (frame-window f)))
:number (frame-number f)
:x (frame-x f)
:y (frame-y f)
:width (frame-width f)
:height (frame-height f)))
(copy (tree)
(cond ((null tree) tree)
((typep tree 'frame)
(dump tree))
(t
(mapcar #'copy tree)))))
(let ((group-dump
(cond ((eq (type-of group) 'tile-group)
(make-gdump
;; we only use the name and number for screen and desktop restores
:number (group-number group)
:name (group-name group)
:tree (copy (tile-group-frame-tree group))
:current (frame-number (tile-group-current-frame group))))
((eq (type-of group) 'float-group)
(make-fgdump
:number (group-number group)
:name (group-name group))))))
(dump-structure group-dump to-fs file backup-p))))
(defun dump-screen (screen &key
(to-fs nil)
(file (data-dir-file
(concat "screen-" (prin1-to-string
(screen-id screen))) "rules"))
(backup-p nil))
"Makes dump of given screen"
(let
((screen-dump
(make-sdump :number (screen-id screen)
:current (group-number (screen-current-group screen))
:groups (mapcar 'dump-group (sort-groups screen)))))
(dump-structure screen-dump to-fs file backup-p)))
(defun dump-desktop (&key
(to-fs nil)
(file (data-dir-file "desktop" "rules"))
(backup-p nil))
"Makes full dump of desktop"
(let ((desktop-dump
(make-ddump :screens (mapcar 'dump-screen *screen-list*)
:current (screen-id (current-screen)))))
(dump-structure desktop-dump to-fs file backup-p)))
;;;
(defun read-dump-from-file (file)
(with-open-file (fp file :direction :input)
(with-standard-io-syntax
(let ((*package* (find-package :dswm)))
(read fp)))))
(defun restore-group (group gdump &optional auto-populate (window-dump-fn 'window-id))
;; TODO: make from-fs option, like in dump-group
(let ((windows (group-windows group)))
(labels ((give-frame-a-window (f)
(unless (frame-window f)
(setf (frame-window f) (find f windows :key 'window-frame))))
(restore (fd)
(let ((f (make-frame
:number (fdump-number fd)
:x (fdump-x fd)
:y (fdump-y fd)
:width (fdump-width fd)
:height (fdump-height fd))))
;; import matching windows
(if auto-populate
(choose-new-frame-window f group)
(progn
(dolist (w windows)
(when (equal (fdump-current fd) (funcall window-dump-fn w))
(setf (frame-window f) w))
(when (find (funcall window-dump-fn w) (fdump-windows fd) :test 'equal)
(setf (window-frame w) f)))))
(when (fdump-current fd)
(give-frame-a-window f))
f))
(copy (tree)
(cond ((null tree) tree)
((typep tree 'fdump)
(restore tree))
(t
(mapcar #'copy tree))))
(migrate (group gdump)
(progn
(setf (group-number (current-group)) (find-free-hidden-group-number (current-screen)))
(let ((old-group (current-group))
(new-group
(cond ((eq (type-of gdump) 'fgdump)
(add-group (current-screen)
(concat (group-name group "~"))
:type 'float-group
:background t))
((eq (type-of gdump) 'gdump)
(add-group (current-screen)
(concat (group-name group "~"))
:type 'tile-group
:background t))
(t
(error "Incorrect group dump type")))))
(progn
(kill-group (current-group) new-group)
(switch-to-group new-group)
(setf (group-name (current-group)) (group-name old-group)))
(restore-group (current-group) gdump)
))))
(cond
((and (eq (type-of group) 'tile-group)
(eq (type-of gdump) 'gdump))
;; clear references to old frames
(dolist (w windows)
(setf (window-frame w) nil))
(setf (tile-group-frame-tree group) (copy (gdump-tree gdump))
(tile-group-current-frame group) (find (gdump-current gdump) (group-frames group) :key 'frame-number))
;; give any windows still not in a frame a frame
(dolist (w windows)
(unless (window-frame w)
(setf (window-frame w) (tile-group-current-frame group))))
;; FIXME: if the current window was blank in the dump, this does not honour that.
(give-frame-a-window (tile-group-current-frame group))
;; raise the curtains
(dolist (w windows)
(if (eq (frame-window (window-frame w)) w)
(unhide-window w)
(hide-window w)))
(sync-all-frame-windows group)
(focus-frame group (tile-group-current-frame group)))
;; If group is float and dump is float
((and (eq (type-of group) 'float-group)
(eq (type-of gdump) 'fgdump))
(setf (group-name group) (fgdump-name gdump)))
;; If group is float and dump is tile or group is tile and dump is float
(t
(migrate group gdump))
))))
(defun restore-screen (screen sdump)
"Restore all frames in all groups of given screen. Create groups if
they don't already exist."
(dolist (gdump (sdump-groups sdump))
(cond ((eq (type-of gdump) 'gdump)
(format t "~a~%" (gdump-name gdump))
(restore-group
(or
(find-group screen (gdump-name gdump))
;; FIXME: if the group doesn't exist then
;; windows won't be migrated from existing
;; groups
(add-group screen (gdump-name gdump)))
gdump))
((eq (type-of gdump) 'fgdump)
(format t "~a~%" (fgdump-name gdump))
(format t "~a"
(restore-group
(or
(find-group screen (fgdump-name gdump))
(add-group screen (fgdump-name gdump) :type 'float-group))
gdump))))))
(defun restore-desktop (ddump)
"Restore all frames, all groups, and all screens."
(dolist (sdump (ddump-screens ddump))
(let ((screen (find (sdump-number sdump) *screen-list*
:key 'screen-id :test '=)))
(when screen
(restore-screen screen sdump)))))
(defun restore-all ()
"Restore all rules. Useful at startup"
(progn
(when (file-exists-p (data-dir-file "desktop" "rules"))
(restore-from-file
(data-dir-file "desktop" "rules")))
(when (file-exists-p (data-dir-file "window-placement" "rules"))
(setf *window-placement-rules*
(read-dump-from-file
(data-dir-file "window-placement" "rules"))))
;; Add function for restore all programs, running in last session
))
(defcommand restore-from-file (file) ((:rest "Restore From File: "))
"Restores screen, groups, or frames from named file, depending on file's contents."
(let ((dump (read-dump-from-file file)))
(typecase dump
((or gdump fgdump)
(restore-group (current-group) dump)
(message "Group restored."))
(sdump
(restore-screen (current-screen) dump)
(message "Screen restored."))
(ddump
(restore-desktop dump)
(message "Desktop restored."))
(t
(message "Don't know how to restore ~a" dump)))))
;; (defcommand-alias restore restore-from-file)
(defcommand place-existing-windows () ()
"Re-arrange existing windows according to placement rules."
(sync-window-placement))
(defcommand dump-group-to-file (file) ((:rest "Dump To File: "))
"Dumps the frames of the current group of the current screen to the
named file."
(eval-with-message :body
(dump-group (current-group) :to-fs t :file file)
:message-if-done "Group saved"
:message-if-false "Cannot save group"))
(defcommand remember-group () ()
"Dumps the frames of the current group of the current screen to the
default dump file."
(dump-group-to-file *desktop-dump-file*))
(defcommand dump-screen-to-file (file) ((:rest "Dump To File: "))
"Dumps the frames of all groups of the current screen to the named
file"
(eval-with-message :body
(dump-screen (current-screen) :to-fs t :file file)
:message-if-done "Screen saved"
:message-if-false "Can't save screen"))
(defcommand remember-screen () ()
"Dumps the frames of all groups of the current screen to the default
dump file"
(dump-screen-to-file *desktop-dump-file*))
(defcommand dump-desktop-to-file (file) ((:rest "Dump To File: "))
"Dumps the frames of all groups of all screens to the named file"
(eval-with-message :body
(dump-desktop :to-fs t :file file)
:message-if-done "Desktop saved"
:message-if-false "Can't save desktop"))
(defcommand remember-desktop () ()
"Dumps the frames of all groups of all screens to the default dump file"
(dump-desktop-to-file *desktop-dump-file*))
;; TODO: defcommand forgot-desktop () ()
(defcommand snapshot-desktop () ()
"Make rules of all existing windows, bind it to groups and frames,
where they located now and dump all groups frames and window placement
rules to frame-froup-placement.rules and window-placement.rules in
data dir"
(eval-with-message :body
(progn
(remember-all-windows '(t) '(nil))
(dump-desktop :to-fs t))
:message-if-done "Snapshot created"
:message-if-false "Can't create snapshot"))
(defcommand restore-desktop-snapshot () ()
"Restores frame and group placement rules (like numbers, names,
splitting rules etc) and window placement rules of all groups and
frames of the current desktop from frame-froup-placement.rules and
window-placement.rules file in data dir"
(eval-with-message :body
(restore-all)
:message-if-done "Snapshot restored"
:message-if-false "Can't restore snapshot"))