clfswm-util.lisp 65.9 KB
Newer Older
Philippe Brochard's avatar
Philippe Brochard committed
1
2
3
4
5
6
7
;;; --------------------------------------------------------------------------
;;; CLFSWM - FullScreen Window Manager
;;;
;;; --------------------------------------------------------------------------
;;; Documentation: Utility
;;; --------------------------------------------------------------------------
;;;
Philippe Brochard's avatar
Philippe Brochard committed
8
;;; (C) 2005-2013 Philippe Brochard <pbrochard@common-lisp.net>
Philippe Brochard's avatar
Philippe Brochard committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
;;;
;;; This program 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 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program 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 program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;;
;;; --------------------------------------------------------------------------

(in-package :clfswm)

28
29
30

;;; Configuration file
(defun xdg-config-home ()
31
32
33
34
  (aif (getenv "XDG_CONFIG_HOME")
       (pathname-directory (concatenate 'string it "/"))
       (append (pathname-directory (user-homedir-pathname)) '(".config"))))

35

36
37
38
39
40
41
42
(let ((saved-conf-name nil))
  (defun conf-file-name (&optional alternate-name)
    (unless (and saved-conf-name (not alternate-name))
      (let* ((user-conf (probe-file (merge-pathnames (user-homedir-pathname) #p".clfswmrc")))
	     (etc-conf (probe-file #p"/etc/clfswmrc"))
	     (config-user-conf (probe-file (make-pathname :directory (append (xdg-config-home) '("clfswm"))
							  :name "clfswmrc")))
43
	     (alternate-conf (and alternate-name (probe-file alternate-name))))
44
45
46
	(setf saved-conf-name (or alternate-conf config-user-conf user-conf etc-conf))))
    (print saved-conf-name)
    saved-conf-name))
47
48
49
50




51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(let ((already-warn nil))
  (defun load-contrib (file)
    "Load a file in the contrib directory"
    (let ((truename (merge-pathnames file *contrib-dir*)))
      (format t "Loading contribution file: ~A~%" truename)
      (if (probe-file truename)
          (load truename :verbose nil)
          (progn
            (format t "  File not found!~%")
            (unless already-warn
              (setf already-warn t)
              (format t "  ~&Please, adjust the *contrib-dir* variable to the place where CLFSWM can
find its contrib module files. For example: /usr/local/lib/clfswm/.
Write (defparameter *contrib-dir* \"/usr/local/lib/clfswm/\") in ~A.~%"
                      (conf-file-name))))))))
Philippe Brochard's avatar
Philippe Brochard committed
66
67


68
69
70
71
(defun reload-clfswm ()
  "Reload clfswm"
  (format t "~&-*- Reloading CLFSWM -*-~%")
  (asdf:oos 'asdf:load-op :clfswm)
Philippe Brochard's avatar
Philippe Brochard committed
72
  (reset-clfswm))
73
74
75



76
77
78
;;;----------------------------
;;; Lisp image part
;;;----------------------------
79
80
#+:ECL (require :cmp)

81
(defun build-lisp-image (dump-name)
82
83
84
85
86
87
  #+:CLISP (ext:saveinitmem dump-name :init-function (lambda () (clfswm:main) (ext:quit)) :executable t)
  #+:SBCL (sb-ext:save-lisp-and-die dump-name :toplevel 'clfswm:main :executable t)
  #+:CMU (ext:save-lisp dump-name :init-function (lambda () (clfswm:main) (ext:quit)) :executable t)
  #+:CCL (ccl:save-application dump-name :toplevel-function (lambda () (clfswm:main) (ccl:quit)) :prepend-kernel t)
  #+:ECL (c:build-program dump-name :epilogue-code '(clfswm:main)))

88
89
90



91
(defun query-yes-or-no (formatter &rest args)
92
  (let ((rep (query-string (apply #'format nil formatter args) "" '("Yes" "No"))))
93
94
95
96
97
    (or (string= rep "")
	(char= (char rep 0) #\y)
	(char= (char rep 0) #\Y))))


Philippe Brochard's avatar
Philippe Brochard committed
98

99
100
101
102
103
104
(defun banish-pointer ()
  "Move the pointer to the lower right corner of the screen"
  (with-placement (*banish-pointer-placement* x y)
    (xlib:warp-pointer *root* x y)))


105
106
107


;;; Root functions utility
108
109
110
111
112
(defun show-current-root ()
  (when *have-to-show-current-root*
    (let ((*notify-window-placement* *show-current-root-placement*))
      (notify-message *show-current-root-delay* *show-current-root-message*))))

113
114
115
116
117
118
119
120
121
122
123
(defun select-generic-root (fun restart-menu)
  (no-focus)
  (let* ((current-root (find-root (current-child)))
         (parent (find-parent-frame (root-original current-root))))
    (when parent
      (setf (frame-child parent) (funcall fun (frame-child parent)))
      (let ((new-root (find-root (frame-selected-child parent))))
        (setf (current-child) (aif (root-current-child new-root)
                                   it
                                   (frame-selected-child parent))))))
  (show-all-children t)
124
  (show-current-root)
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
  (if restart-menu
      (open-menu (find-menu 'root-menu))
      (leave-second-mode)))

(defun select-next-root ()
  "Select the next root"
  (select-generic-root #'rotate-list nil))

(defun select-previous-root ()
  "Select the previous root"
  (select-generic-root #'anti-rotate-list nil))


(defun select-next-root-restart-menu ()
  "Select the next root"
  (select-generic-root #'rotate-list t))

(defun select-previous-root-restart-menu ()
  "Select the previous root"
  (select-generic-root #'anti-rotate-list t))


(defun rotate-root-geometry-generic (fun restart-menu)
  (no-focus)
  (funcall fun)
  (show-all-children t)
151
  (show-current-root)
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
  (if restart-menu
      (open-menu (find-menu 'root-menu))
      (leave-second-mode)))


(defun rotate-root-geometry-next ()
  "Rotate root geometry to next root"
  (rotate-root-geometry-generic #'rotate-root-geometry nil))

(defun rotate-root-geometry-previous ()
  "Rotate root geometry to previous root"
  (rotate-root-geometry-generic #'anti-rotate-root-geometry nil))

(defun rotate-root-geometry-next-restart-menu ()
  "Rotate root geometry to next root"
  (rotate-root-geometry-generic #'rotate-root-geometry t))

(defun rotate-root-geometry-previous-restart-menu ()
  "Rotate root geometry to previous root"
  (rotate-root-geometry-generic #'anti-rotate-root-geometry t))



(defun exchange-root-geometry-with-mouse ()
  "Exchange two root geometry pointed with the mouse"
  (open-notify-window '("Select the first root to exchange"))
  (wait-no-key-or-button-press)
  (wait-mouse-button-release)
  (close-notify-window)
  (multiple-value-bind (x1 y1) (xlib:query-pointer *root*)
    (open-notify-window '("Select the second root to exchange"))
    (wait-no-key-or-button-press)
    (wait-mouse-button-release)
    (close-notify-window)
    (multiple-value-bind (x2 y2) (xlib:query-pointer *root*)
      (exchange-root-geometry (find-root-by-coordinates x1 y1)
                              (find-root-by-coordinates x2 y2))))
189
  (show-all-children)
190
  (show-current-root)
191
192
193
194
195
196
197
198
199
200
201
  (leave-second-mode))

(defun change-current-root-geometry ()
  "Change the current root geometry"
  (let* ((root (find-root (current-child)))
         (x (query-number "New root X position" (root-x root)))
         (y (query-number "New root Y position" (root-y root)))
         (w (query-number "New root width" (root-w root)))
         (h (query-number "New root height" (root-h root))))
    (setf (root-x root) x  (root-y root) y
          (root-w root) w  (root-h root) h)
202
    (show-all-children)
203
    (show-current-root)
204
    (leave-second-mode)))
205
206
207



208
209
210
211
212
213
214
215
216
217
(defun display-all-frame-info ()
  (with-all-frames (*root-frame* frame)
    (display-frame-info frame)))

(defun display-all-root-frame-info ()
  (with-all-root-child (root)
    (display-frame-info root)))



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(defun place-window-from-hints (window)
  "Place a window from its hints"
  (let* ((hints (xlib:wm-normal-hints window))
	 (min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0))
	 (min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0))
	 (max-width (or (and hints (xlib:wm-size-hints-max-width hints)) (x-drawable-width *root*)))
	 (max-height (or (and hints (xlib:wm-size-hints-max-height hints)) (x-drawable-height *root*)))
	 (rwidth (or (and hints (or (xlib:wm-size-hints-width hints) (xlib:wm-size-hints-base-width hints)))
		     (x-drawable-width window)))
	 (rheight (or (and hints (or (xlib:wm-size-hints-height hints) (xlib:wm-size-hints-base-height hints)))
		      (x-drawable-height window))))
    (setf (x-drawable-width window) (min (max min-width rwidth *default-window-width*) max-width)
	  (x-drawable-height window) (min (max min-height rheight *default-window-height*) max-height))
    (with-placement (*unmanaged-window-placement* x y (x-drawable-width window) (x-drawable-height window))
      (setf (x-drawable-x window) x
            (x-drawable-y window) y))
    (xlib:display-finish-output *display*)))


Philippe Brochard's avatar
Philippe Brochard committed
237
238
(defun rename-current-child ()
  "Rename the current child"
239
240
241
  (let ((name (query-string (format nil "New child name: (last: ~A)" (child-name (current-child)))
			    (child-name (current-child)))))
    (rename-child (current-child) name)
Philippe Brochard's avatar
Philippe Brochard committed
242
243
244
    (leave-second-mode)))


245
246
247
248
249
250
251
252
253
254
(defun ask-child-transparency (msg child)
  (let ((trans (query-number (format nil "New ~A transparency: (last: ~A)"
                                     msg
                                     (* 100 (child-transparency child)))
                             (* 100 (child-transparency child)))))
    (when (numberp trans)
      (setf (child-transparency child) (float (/ trans 100))))))

(defun set-current-child-transparency ()
  "Set the current child transparency"
255
  (ask-child-transparency "child" (current-child))
256
257
258
  (leave-second-mode))


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
(defun ask-child-border-size (msg child)
  (let ((size (query-number (format nil "New ~A border size: (last: ~A)"
                                    msg
                                    (child-border-size child))
                            (child-border-size child))))
    (when (numberp size)
      (setf (child-border-size child) size))))


(defun set-current-child-border-size ()
  "Set the current child border size"
  (ask-child-border-size "child" (current-child))
  (leave-second-mode))


Philippe Brochard's avatar
Philippe Brochard committed
274
275
(defun renumber-current-frame ()
  "Renumber the current frame"
276
277
278
279
  (when (frame-p (current-child))
    (let ((number (query-number (format nil "New child number: (last: ~A)" (frame-number (current-child)))
				(frame-number (current-child)))))
      (setf (frame-number (current-child)) number)
Philippe Brochard's avatar
Philippe Brochard committed
280
281
      (leave-second-mode))))

Philippe Brochard's avatar
Philippe Brochard committed
282

Philippe Brochard's avatar
Philippe Brochard committed
283
284
285


(defun add-default-frame ()
286
  "Add a default frame in the current frame"
287
  (when (frame-p (current-child))
Philippe Brochard's avatar
Philippe Brochard committed
288
    (let ((name (query-string "Frame name")))
289
      (push (create-frame :name name) (frame-child (current-child)))))
Philippe Brochard's avatar
Philippe Brochard committed
290
  (leave-second-mode))
Philippe Brochard's avatar
Philippe Brochard committed
291

292
293
(defun add-frame-in-parent-frame ()
  "Add a frame in the parent frame (and reorganize parent frame)"
294
295
  (let ((parent (find-parent-frame (current-child))))
    (when (and parent (not (child-original-root-p (current-child))))
296
297
      (let ((new-frame (create-frame)))
        (pushnew new-frame (frame-child parent))
298
        (awhen (child-root-p (current-child))
299
          (change-root it parent))
300
        (setf (current-child) parent)
301
        (set-layout-once #'tile-space-layout)
302
        (setf (current-child) new-frame)
303
        (leave-second-mode)))))
304
305
306



Philippe Brochard's avatar
Philippe Brochard committed
307
308

(defun add-placed-frame ()
309
  "Add a placed frame in the current frame"
310
  (when (frame-p (current-child))
Philippe Brochard's avatar
Philippe Brochard committed
311
312
313
    (let ((name (query-string "Frame name"))
	  (x (/ (query-number "Frame x in percent (%)") 100))
	  (y (/ (query-number "Frame y in percent (%)") 100))
314
315
	  (w (/ (query-number "Frame width in percent (%)" 100) 100))
	  (h (/ (query-number "Frame height in percent (%)" 100) 100)))
Philippe Brochard's avatar
Philippe Brochard committed
316
      (push (create-frame :name name :x x :y y :w w :h h)
317
	    (frame-child (current-child)))))
Philippe Brochard's avatar
Philippe Brochard committed
318
319
320
321
  (leave-second-mode))



322
(defun delete-focus-window-generic (close-fun)
323
  (with-focus-window (window)
324
325
    (when (child-equal-p window (current-child))
      (setf (current-child) (find-current-root)))
326
    (delete-child-and-children-in-all-frames window close-fun)))
Philippe Brochard's avatar
Philippe Brochard committed
327

328

329
330
331
332
(defun delete-focus-window ()
  "Close focus window: Delete the focus window in all frames and workspaces"
  (delete-focus-window-generic 'delete-window))

Philippe Brochard's avatar
Philippe Brochard committed
333
(defun destroy-focus-window ()
334
  "Kill focus window: Destroy the focus window in all frames and workspaces"
335
  (delete-focus-window-generic 'destroy-window))
Philippe Brochard's avatar
Philippe Brochard committed
336
337

(defun remove-focus-window ()
338
  "Remove the focus window from the current frame"
339
  (with-focus-window (window)
340
    (setf (current-child) (find-current-root))
341
342
343
    (hide-child window)
    (remove-child-in-frame window (find-parent-frame window))
    (show-all-children)))
Philippe Brochard's avatar
Philippe Brochard committed
344
345
346
347


(defun unhide-all-windows-in-current-child ()
  "Unhide all hidden windows into the current child"
348
349
350
351
  (dolist (window (get-hidden-windows))
    (unhide-window window)
    (process-new-window window)
    (map-window window))
Philippe Brochard's avatar
Philippe Brochard committed
352
353
354
355
356
  (show-all-children))




357
358
(defun find-child-under-mouse-in-never-managed-windows (x y)
  "Return the child under mouse from never managed windows"
359
360
361
362
363
364
365
366
  (let ((ret nil))
    (dolist (win (xlib:query-tree *root*))
      (unless (window-hidden-p win)
	(multiple-value-bind (never-managed raise)
	    (never-managed-window-p win)
	  (when (and never-managed raise (in-window win x y))
	    (setf ret win)))))
    ret))
367

368
369
(defun find-child-under-mouse-in-child-tree (x y)
  (dolist (child-rect (get-displayed-child))
370
371
    (when (in-child (child-rect-child child-rect) x y)
      (return-from find-child-under-mouse-in-child-tree (child-rect-child child-rect)))))
372

Philippe Brochard's avatar
Philippe Brochard committed
373
374


375
(defun find-child-under-mouse (x y &optional also-never-managed)
376
377
  "Return the child under the mouse"
  (or (and also-never-managed
378
           (find-child-under-mouse-in-never-managed-windows x y))
379
380
      (find-child-under-mouse-in-child-tree x y)))

381

Philippe Brochard's avatar
Philippe Brochard committed
382
383
384
385
386
387


;;; Selection functions
(defun clear-selection ()
  "Clear the current selection"
  (setf *child-selection* nil)
388
  (display-all-root-frame-info))
Philippe Brochard's avatar
Philippe Brochard committed
389
390
391

(defun copy-current-child ()
  "Copy the current child to the selection"
392
  (pushnew (current-child) *child-selection*)
393
  (display-all-root-frame-info))
Philippe Brochard's avatar
Philippe Brochard committed
394
395


396
(defun cut-current-child (&optional (show-now t))
Philippe Brochard's avatar
Philippe Brochard committed
397
  "Cut the current child to the selection"
398
399
400
  (unless (child-root-p (current-child))
    (let ((parent (find-parent-frame (current-child))))
      (hide-all (current-child))
401
      (copy-current-child)
402
      (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
403
      (when parent
404
        (setf (current-child) parent))
405
406
      (when show-now
        (show-all-children t))
407
      (current-child))))
Philippe Brochard's avatar
Philippe Brochard committed
408
409
410

(defun remove-current-child ()
  "Remove the current child from its parent frame"
411
412
413
414
  (unless (child-root-p (current-child))
    (let ((parent (find-parent-frame (current-child))))
      (hide-all (current-child))
      (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
415
      (when parent
416
        (setf (current-child) parent))
417
418
      (show-all-children t)
      (leave-second-mode))))
419

420
(defun delete-current-child ()
421
  "Delete the current child and its children in all frames"
422
423
424
  (unless (child-root-p (current-child))
    (hide-all (current-child))
    (delete-child-and-children-in-all-frames (current-child))
425
426
    (show-all-children t)
    (leave-second-mode)))
427
428


Philippe Brochard's avatar
Philippe Brochard committed
429
430
(defun paste-selection-no-clear ()
  "Paste the selection in the current frame - Do not clear the selection after paste"
431
  (when (frame-p (current-child))
432
    (dolist (child *child-selection*)
433
434
      (unless (find-child-in-parent child (current-child))
        (pushnew child (frame-child (current-child)) :test #'child-equal-p)))
435
    (show-all-children)))
Philippe Brochard's avatar
Philippe Brochard committed
436
437
438

(defun paste-selection ()
  "Paste the selection in the current frame"
439
  (when (frame-p (current-child))
440
441
    (paste-selection-no-clear)
    (setf *child-selection* nil)
442
    (display-all-root-frame-info)))
443
444
445
446
447


(defun copy-focus-window ()
  "Copy the focus window to the selection"
  (with-focus-window (window)
448
    (with-current-child (window)
449
450
451
452
453
454
      (copy-current-child))))


(defun cut-focus-window ()
  "Cut the focus window to the selection"
  (with-focus-window (window)
455
    (setf (current-child) (with-current-child (window)
Philippe Brochard's avatar
Philippe Brochard committed
456
457
                            (cut-current-child nil)))
    (show-all-children t)))
458
459


Philippe Brochard's avatar
Philippe Brochard committed
460
461
462



Philippe Brochard's avatar
Philippe Brochard committed
463

464
465
466
;;; Maximize function
(defun frame-toggle-maximize ()
  "Maximize/Unmaximize the current frame in its parent frame"
467
468
  (when (frame-p (current-child))
    (let ((unmaximized-coords (frame-data-slot (current-child) :unmaximized-coords)))
469
      (if unmaximized-coords
470
	  (with-slots (x y w h) (current-child)
471
	    (destructuring-bind (nx ny nw nh) unmaximized-coords
472
	      (setf (frame-data-slot (current-child) :unmaximized-coords) nil
473
		    x nx y ny w nw h nh)))
474
475
	  (with-slots (x y w h) (current-child)
	    (setf (frame-data-slot (current-child) :unmaximized-coords)
476
477
		  (list x y w h)
		  x 0 y 0 w 1 h 1))))
478
    (show-all-children)
479
    (leave-second-mode)))
480
481
482
483
484
485






Philippe Brochard's avatar
Philippe Brochard committed
486
487
488
489
490
491
492
493
494
495



;;; CONFIG - Identify mode
(defun identify-key ()
  "Identify a key"
  (let* ((done nil)
	 (font (xlib:open-font *display* *identify-font-string*))
	 (window (xlib:create-window :parent *root*
				     :x 0 :y 0
496
				     :width (- (screen-width) (* *border-size* 2))
Philippe Brochard's avatar
Philippe Brochard committed
497
498
				     :height (* 5 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font)))
				     :background (get-color *identify-background*)
499
				     :border-width *border-size*
Philippe Brochard's avatar
Philippe Brochard committed
500
501
502
503
504
505
506
507
				     :border (get-color *identify-border*)
				     :colormap (xlib:screen-default-colormap *screen*)
				     :event-mask '(:exposure)))
	 (gc (xlib:create-gcontext :drawable window
				   :foreground (get-color *identify-foreground*)
				   :background (get-color *identify-background*)
				   :font font
				   :line-style :solid)))
508
    (setf (window-transparency window) *identify-transparency*)
Philippe Brochard's avatar
Philippe Brochard committed
509
510
511
    (labels ((print-doc (msg hash-table-key pos code state)
	       (let ((function (find-key-from-code hash-table-key code state)))
		 (when (and function (fboundp (first function)))
512
513
		   (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* pos (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
				     (format nil "~A ~A" msg (documentation (first function) 'function))))))
Philippe Brochard's avatar
Philippe Brochard committed
514
	     (print-key (code state keysym key modifiers)
515
	       (clear-pixmap-buffer window gc)
Philippe Brochard's avatar
Philippe Brochard committed
516
	       (setf (xlib:gcontext-foreground gc) (get-color *identify-foreground*))
517
518
	       (xlib:draw-glyphs *pixmap-buffer* gc 5 (+ (xlib:max-char-ascent font) 5)
				 (format nil "Press a key to identify. Press 'q' to stop the identify loop."))
Philippe Brochard's avatar
Philippe Brochard committed
519
	       (when code
520
521
522
		 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* 2 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
				   (format nil "Code=~A  KeySym=~S  Key=~S  Modifiers=~A"
					   code keysym key modifiers))
Philippe Brochard's avatar
Philippe Brochard committed
523
		 (print-doc "Main mode  : " *main-keys* 3 code state)
524
525
		 (print-doc "Second mode: " *second-keys* 4 code state))
	       (copy-pixmap-buffer window gc))
Philippe Brochard's avatar
Philippe Brochard committed
526
527
528
529
	     (handle-identify-key (&rest event-slots &key root code state &allow-other-keys)
	       (declare (ignore event-slots root))
	       (let* ((modifiers (state->modifiers state))
		      (key (keycode->char code state))
Philippe Brochard's avatar
Philippe Brochard committed
530
		      (keysym (keysym->keysym-name (keycode->keysym code modifiers))))
531
		 (setf done (and (equal key #\q) (equal modifiers *default-modifiers*)))
Philippe Brochard's avatar
Philippe Brochard committed
532
533
534
535
536
537
538
539
540
541
		 (dbg code keysym key modifiers)
		 (print-key code state keysym key modifiers)
		 (force-output)))
	     (handle-identify (&rest event-slots &key display event-key &allow-other-keys)
	       (declare (ignore display))
	       (case event-key
		 (:key-press (apply #'handle-identify-key event-slots) t)
		 (:exposure (print-key nil nil nil nil nil)))
	       t))
      (xgrab-pointer *root* 92 93)
542
      (map-window window)
Philippe Brochard's avatar
Philippe Brochard committed
543
544
545
546
547
      (format t "~&Press 'q' to stop the identify loop~%")
      (print-key nil nil nil nil nil)
      (force-output)
      (unwind-protect
	   (loop until done do
548
549
550
551
552
553
554
555
                (with-xlib-protect (:Identify-Loop nil)
                  (when (xlib:event-listen *display* *loop-timeout*)
                    (xlib:process-event *display* :handler #'handle-identify))
                  (xlib:display-finish-output *display*)))
        (progn
          (xlib:destroy-window window)
          (xlib:close-font font)
          (xgrab-pointer *root* 66 67))))))
Philippe Brochard's avatar
Philippe Brochard committed
556
557
558
559
560
561






562
563
564
565
566
(let ((all-symbols (collect-all-symbols)))
  (defun eval-from-query-string ()
    "Eval a lisp form from the query input"
    (let ((form (query-string (format nil "Eval Lisp <~A> " (package-name *package*))
                              "" all-symbols))
567
568
569
570
571
572
573
574
575
576
577
578
579
          (result nil))
      (when (and form (not (equal form "")))
        (let ((printed-result
               (with-output-to-string (*standard-output*)
                 (setf result (handler-case
                                  (loop for i in (multiple-value-list
                                                  (eval (read-from-string form)))
                                     collect (format nil "~S" i))
                                (error (condition)
                                  (format nil "~A" condition)))))))
          (let ((ret (info-mode (expand-newline (append (ensure-list (format nil "> ~A" form))
                                                        (ensure-list printed-result)
                                                        (ensure-list result)))
580
                                :width (- (screen-width) 2))))
581
582
583
584
585
586
587
588
589
590
591
592
            (when (or (search "defparameter" form :test #'string-equal)
                      (search "defvar" form :test #'string-equal))
              (let ((elem (split-string form)))
                (pushnew (string-downcase (if (string= (first elem) "(") (third elem) (second elem)))
                         all-symbols :test #'string=)))
            (when (search "in-package" form :test #'string-equal)
              (let ((*notify-window-placement* 'middle-middle-root-placement))
                (open-notify-window '("Collecting all symbols for Lisp REPL completion."))
                (setf all-symbols (collect-all-symbols))
                (close-notify-window)))
            (when ret
              (eval-from-query-string))))))))
Philippe Brochard's avatar
Philippe Brochard committed
593
594
595
596




597

598
(let ((commands (command-in-path)))
599
600
  (defun run-program-from-query-string ()
    "Run a program from the query input"
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    (labels ((run-program-from-query-string-fun ()
               (multiple-value-bind (program return)
                   (query-string "Run:" "" commands)
                 (when (and (equal return :return) program (not (equal program "")))
                   (let ((cmd (concatenate 'string "cd $HOME && exec " program)))
                     (lambda ()
                       (do-shell cmd)))))))
      (let ((fun (run-program-from-query-string-fun)))
        (when fun
          (if *in-second-mode*
              (progn
                (setf *second-mode-leave-function* fun)
                (leave-second-mode))
              (funcall fun)))))))

Philippe Brochard's avatar
Philippe Brochard committed
616
617
618
619
620
621
622




;;; Frame name actions
(defun ask-frame-name (msg)
  "Ask a frame name"
623
  (let ((all-frame-name nil))
Philippe Brochard's avatar
Philippe Brochard committed
624
625
    (with-all-frames (*root-frame* frame)
      (awhen (frame-name frame) (push it all-frame-name)))
626
    (query-string msg "" all-frame-name)))
Philippe Brochard's avatar
Philippe Brochard committed
627
628
629
630
631


;;; Focus by functions
(defun focus-frame-by (frame)
  (when (frame-p frame)
632
    (focus-all-children frame (or (find-parent-frame frame (find-current-root))
Philippe Brochard's avatar
Philippe Brochard committed
633
634
				  (find-parent-frame frame)
				  *root-frame*))
635
    (show-all-children t)))
Philippe Brochard's avatar
Philippe Brochard committed
636
637
638
639


(defun focus-frame-by-name ()
  "Focus a frame by name"
640
  (focus-frame-by (find-frame-by-name (ask-frame-name "Focus frame:")))
Philippe Brochard's avatar
Philippe Brochard committed
641
642
643
644
645
646
647
648
649
650
651
652
  (leave-second-mode))

(defun focus-frame-by-number ()
  "Focus a frame by number"
  (focus-frame-by (find-frame-by-number (query-number "Focus frame by number:")))
  (leave-second-mode))


;;; Open by functions
(defun open-frame-by (frame)
  (when (frame-p frame)
    (push (create-frame :name (query-string "Frame name")) (frame-child frame))
653
    (show-all-children)))
Philippe Brochard's avatar
Philippe Brochard committed
654
655
656
657
658



(defun open-frame-by-name ()
  "Open a new frame in a named frame"
659
  (open-frame-by (find-frame-by-name (ask-frame-name "Open a new frame in: ")))
Philippe Brochard's avatar
Philippe Brochard committed
660
661
662
663
664
665
666
667
668
669
  (leave-second-mode))

(defun open-frame-by-number ()
  "Open a new frame in a numbered frame"
  (open-frame-by (find-frame-by-number (query-number "Open a new frame in the group numbered:")))
  (leave-second-mode))


;;; Delete by functions
(defun delete-frame-by (frame)
670
  (unless (or (child-equal-p frame *root-frame*)
671
              (child-root-p frame))
672
673
    (when (child-equal-p frame (current-child))
      (setf (current-child) (find-current-root)))
674
    (delete-child-and-children-in-all-frames frame))
675
  (show-all-children t))
Philippe Brochard's avatar
Philippe Brochard committed
676
677
678
679


(defun delete-frame-by-name ()
  "Delete a frame by name"
680
  (delete-frame-by (find-frame-by-name (ask-frame-name "Delete frame: ")))
Philippe Brochard's avatar
Philippe Brochard committed
681
682
683
684
685
686
687
688
689
690
691
  (leave-second-mode))

(defun delete-frame-by-number ()
  "Delete a frame by number"
  (delete-frame-by (find-frame-by-number (query-number "Delete frame by number:")))
  (leave-second-mode))


;;; Move by function
(defun move-current-child-by-name ()
  "Move current child in a named frame"
692
  (move-child-to (current-child)
693
		 (find-frame-by-name
694
		  (ask-frame-name (format nil "Move '~A' to frame: " (child-name (current-child))))))
Philippe Brochard's avatar
Philippe Brochard committed
695
696
697
698
  (leave-second-mode))

(defun move-current-child-by-number ()
  "Move current child in a numbered frame"
699
  (move-child-to (current-child)
700
		 (find-frame-by-number
701
		  (query-number (format nil "Move '~A' to frame numbered:" (child-name (current-child))))))
Philippe Brochard's avatar
Philippe Brochard committed
702
703
704
705
  (leave-second-mode))


;;; Copy by function
706
(defun copy-child-to (child frame-dest)
Philippe Brochard's avatar
Philippe Brochard committed
707
708
709
  (when (and child (frame-p frame-dest))
    (pushnew child (frame-child frame-dest))
    (focus-all-children child frame-dest)
710
    (show-all-children t)))
Philippe Brochard's avatar
Philippe Brochard committed
711
712
713

(defun copy-current-child-by-name ()
  "Copy current child in a named frame"
714
  (copy-child-to (current-child)
715
		 (find-frame-by-name
716
		  (ask-frame-name (format nil "Copy '~A' to frame: " (child-name (current-child))))))
Philippe Brochard's avatar
Philippe Brochard committed
717
718
719
720
  (leave-second-mode))

(defun copy-current-child-by-number ()
  "Copy current child in a numbered frame"
721
  (copy-child-to (current-child)
722
		 (find-frame-by-number
723
		  (query-number (format nil "Copy '~A' to frame numbered:" (child-name (current-child))))))
Philippe Brochard's avatar
Philippe Brochard committed
724
725
726
727
728
729
730
731
732
733
  (leave-second-mode))




;;; Show frame info
(defun show-all-frames-info ()
  "Show all frames info windows"
  (let ((*show-root-frame-p* t))
    (show-all-children)
734
    (with-all-root-child (root)
735
736
737
      (with-all-frames (root frame)
        (raise-window (frame-window frame))
        (display-frame-info frame)))))
Philippe Brochard's avatar
Philippe Brochard committed
738
739
740
741
742
743
744
745
746
747
748
749
750

(defun hide-all-frames-info ()
  "Hide all frames info windows"
  (show-all-children))

(defun show-all-frames-info-key ()
  "Show all frames info windows until a key is release"
  (show-all-frames-info)
  (wait-no-key-or-button-press)
  (hide-all-frames-info))


(defun move-frame (frame parent orig-x orig-y)
751
  (when (and frame parent (not (child-root-p frame)))
752
753
754
    (hide-all-children frame)
    (with-slots (window) frame
      (move-window window orig-x orig-y #'display-frame-info (list frame))
755
756
      (setf (frame-x frame) (x-px->fl (x-drawable-x window) parent)
	    (frame-y frame) (y-px->fl (x-drawable-y window) parent)))
757
    (show-all-children)))
Philippe Brochard's avatar
Philippe Brochard committed
758
759

(defun resize-frame (frame parent orig-x orig-y)
760
  (when (and frame parent (not (child-root-p frame)))
761
762
763
    (hide-all-children frame)
    (with-slots (window) frame
      (resize-window window orig-x orig-y #'display-frame-info (list frame))
764
765
      (setf (frame-w frame) (w-px->fl (anti-adj-border-wh (x-drawable-width window) frame) parent)
	    (frame-h frame) (h-px->fl (anti-adj-border-wh (x-drawable-height window) frame) parent)))
766
    (show-all-children)))
Philippe Brochard's avatar
Philippe Brochard committed
767

Philippe Brochard's avatar
Philippe Brochard committed
768

Philippe Brochard's avatar
Philippe Brochard committed
769

770
(defun mouse-click-to-focus-generic (window root-x root-y mouse-fn)
Philippe Brochard's avatar
Philippe Brochard committed
771
772
  "Focus the current frame or focus the current window parent
mouse-fun is #'move-frame or #'resize-frame"
773
  (let* ((to-replay t)
774
775
	 (child (find-child-under-mouse root-x root-y))
	 (parent (find-parent-frame child))
776
         (root-p (child-root-p child)))
777
    (labels ((add-new-frame ()
778
               (when (frame-p child)
779
780
                 (setf parent child
                       child (create-frame)
781
                       mouse-fn #'resize-frame
782
                       (current-child) child)
783
784
                 (place-frame child parent root-x root-y 10 10)
                 (map-window (frame-window child))
785
786
787
                 (pushnew child (frame-child parent)))))
      (when (and root-p  *create-frame-on-root*)
        (add-new-frame))
788
      (when (and (frame-p child) (not (child-root-p child))
789
                 (not (never-managed-window-and-handled-p window)))
790
791
        (funcall mouse-fn child parent root-x root-y))
      (when (and child parent
792
                 (not (never-managed-window-and-handled-p window))
793
                 (focus-all-children child parent (not (child-root-p child))))
794
795
        (when (show-all-children)
          (setf to-replay nil)))
796
797
798
799
      (if to-replay
	  (replay-button-event)
	  (stop-button-event)))))

Philippe Brochard's avatar
Philippe Brochard committed
800
801

(defun mouse-click-to-focus-and-move (window root-x root-y)
802
  "Move and focus the current frame or focus the current window parent.
803
804
Or do actions on corners"
  (or (do-corner-action root-x root-y *corner-main-mode-left-button*)
805
      (mouse-click-to-focus-generic window root-x root-y #'move-frame)))
Philippe Brochard's avatar
Philippe Brochard committed
806
807

(defun mouse-click-to-focus-and-resize (window root-x root-y)
808
  "Resize and focus the current frame or focus the current window parent.
809
Or do actions on corners"
810
  ;;(declare (ignore window))
811
  (or (do-corner-action root-x root-y *corner-main-mode-right-button*)
812
      (mouse-click-to-focus-generic window root-x root-y #'resize-frame)))
Philippe Brochard's avatar
Philippe Brochard committed
813

814
815
816
817
818
819
(defun mouse-middle-click (window root-x root-y)
  "Do actions on corners"
  (declare (ignore window))
  (or (do-corner-action root-x root-y *corner-main-mode-middle-button*)
      (replay-button-event)))

Philippe Brochard's avatar
Philippe Brochard committed
820
821
822
823
824
825
826
827



(defun mouse-focus-move/resize-generic (root-x root-y mouse-fn window-parent)
  "Focus the current frame or focus the current window parent
mouse-fun is #'move-frame or #'resize-frame.
Focus child and its parents -
For window: set current child to window or its parent according to window-parent"
828
829
  (labels ((move/resize-managed (child)
	     (let ((parent (find-parent-frame child)))
830
	       (when (and child
831
                          (frame-p child)
832
                          (child-root-p child))
833
834
		 (setf parent child
                       child (create-frame)
835
836
837
		       mouse-fn #'resize-frame)
		 (place-frame child parent root-x root-y 10 10)
		 (map-window (frame-window child))
838
839
		 (push child (frame-child parent)))
               (focus-all-children child parent window-parent)
840
	       (show-all-children)
841
842
843
844
	       (typecase child
		 (xlib:window
		  (if (managed-window-p child parent)
		      (funcall mouse-fn parent (find-parent-frame parent) root-x root-y)
845
846
847
848
849
850
		      (funcall (cond ((or (eql mouse-fn #'move-frame)
                                          (eql mouse-fn #'move-frame-constrained))
                                      #'move-window)
				     ((or (eql mouse-fn #'resize-frame)
                                          (eql mouse-fn #'resize-frame-constrained))
                                      #'resize-window))
851
852
			       child root-x root-y)))
		 (frame (funcall mouse-fn child parent root-x root-y)))
853
	       (show-all-children)))
854
855
	   (move/resize-never-managed (child raise-fun)
	     (funcall raise-fun child)
856
857
	     (funcall (cond ((eql mouse-fn #'move-frame) #'move-window)
			    ((eql mouse-fn #'resize-frame) #'resize-window))
858
		      child root-x root-y)))
859
    (let ((child (find-child-under-mouse root-x root-y t)))
860
      (multiple-value-bind (never-managed raise-fun)
861
	  (never-managed-window-p child)
862
863
	(if (and (xlib:window-p child) never-managed raise-fun)
	    (move/resize-never-managed child raise-fun)
864
865
	    (move/resize-managed child))))))

Philippe Brochard's avatar
Philippe Brochard committed
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928

(defun test-mouse-binding (window root-x root-y)
  (dbg window root-x root-y)
  (replay-button-event))



(defun mouse-select-next-level (window root-x root-y)
  "Select the next level in frame"
  (declare (ignore root-x root-y))
  (let ((frame (find-frame-window window)))
    (when (or frame (xlib:window-equal window *root*))
      (select-next-level))
    (replay-button-event)))



(defun mouse-select-previous-level (window root-x root-y)
  "Select the previous level in frame"
  (declare (ignore root-x root-y))
  (let ((frame (find-frame-window window)))
    (when (or frame (xlib:window-equal window *root*))
      (select-previous-level))
    (replay-button-event)))



(defun mouse-enter-frame (window root-x root-y)
  "Enter in the selected frame - ie make it the root frame"
  (declare (ignore root-x root-y))
  (let ((frame (find-frame-window window)))
    (when (or frame (xlib:window-equal window *root*))
      (enter-frame))
    (replay-button-event)))



(defun mouse-leave-frame (window root-x root-y)
  "Leave the selected frame - ie make its parent the root frame"
  (declare (ignore root-x root-y))
  (let ((frame (find-frame-window window)))
    (when (or frame (xlib:window-equal window *root*))
      (leave-frame))
    (replay-button-event)))



;;;;;,-----
;;;;;| Various definitions
;;;;;`-----

(defun show-help (&optional (browser "dillo") (tempfile "/tmp/clfswm.html"))
  "Show current keys and buttons bindings"
  (ignore-errors
    (produce-doc-html-in-file tempfile))
  (sleep 1)
  (do-shell (format nil "~A ~A" browser tempfile)))



;;;  Bind or jump functions
(let ((key-slots (make-array 10 :initial-element nil))
      (current-slot 1))
929
930
931
932
  (defun reset-bind-or-jump-slots ()
    (dotimes (i 10)
      (setf (aref key-slots i) nil)))

933
  (defun bind-on-slot (&optional (slot current-slot) child)
Philippe Brochard's avatar
Philippe Brochard committed
934
    "Bind current child to slot"
935
    (setf (aref key-slots slot) (if child child (current-child))))
Philippe Brochard's avatar
Philippe Brochard committed
936
937
938
939
940
941
942

  (defun remove-binding-on-slot ()
    "Remove binding on slot"
    (setf (aref key-slots current-slot) nil))

  (defun jump-to-slot ()
    "Jump to slot"
943
    (let ((jump-child (aref key-slots current-slot)))
944
      (when (and jump-child (find-child jump-child *root-frame*))
945
        (unless (find-child-in-all-root jump-child)
946
          (change-root (find-root jump-child) jump-child))
947
948
949
        (setf (current-child) jump-child)
        (focus-all-children jump-child jump-child)
        (show-all-children t))))
Philippe Brochard's avatar
Philippe Brochard committed
950

Philippe Brochard's avatar
Philippe Brochard committed
951
  (defun bind-or-jump (n)
Philippe Brochard's avatar
Philippe Brochard committed
952
    "Bind or jump to a slot (a frame or a window)"
953
954
    (setf current-slot (- n 1))
    (let ((default-bind `("b" bind-on-slot
955
			      ,(format nil "Bind slot ~A on child: ~A" n (child-fullname (current-child))))))
Philippe Brochard's avatar
Philippe Brochard committed
956
957
958
      (info-mode-menu (aif (aref key-slots current-slot)
			   `(,default-bind
				("BackSpace" remove-binding-on-slot
959
					     ,(format nil "Remove slot ~A binding on child: ~A" n (child-fullname (current-child))))
Philippe Brochard's avatar
Philippe Brochard committed
960
961
962
963
				("   -  " nil " -")
			      ("Tab" jump-to-slot
				     ,(format nil "Jump to child: ~A" (aif (aref key-slots current-slot)
									   (child-fullname it)
964
965
966
									   "Not set - Please, bind it with 'b'")))
			      ("Return" jump-to-slot "Same thing")
			      ("space" jump-to-slot "Same thing"))
Philippe Brochard's avatar
Philippe Brochard committed
967
968
969
970
971
972
973
974
			   (list default-bind))))))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Useful function for the second mode ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro with-movement (&body body)
975
  `(when (frame-p (current-child))
976
977
978
979
980
981
982
     (unwind-protect
          (progn
            ,@body)
       (show-all-children)
       (display-all-frame-info)
       (draw-second-mode-window)
       (open-menu (find-menu 'frame-movement-menu)))))
Philippe Brochard's avatar
Philippe Brochard committed
983
984
985
986
987


;;; Pack
(defun current-frame-pack-up ()
  "Pack the current frame up"
988
  (with-movement (pack-frame-up (current-child) (find-parent-frame (current-child) (find-current-root)))))
Philippe Brochard's avatar
Philippe Brochard committed
989
990
991

(defun current-frame-pack-down ()
  "Pack the current frame down"
992
  (with-movement (pack-frame-down (current-child) (find-parent-frame (current-child) (find-current-root)))))
Philippe Brochard's avatar
Philippe Brochard committed
993
994
995

(defun current-frame-pack-left ()
  "Pack the current frame left"
996
  (with-movement (pack-frame-left (current-child) (find-parent-frame (current-child) (find-current-root)))))
Philippe Brochard's avatar
Philippe Brochard committed
997
998
999

(defun current-frame-pack-right ()
  "Pack the current frame right"
1000
  (with-movement (pack-frame-right (current-child) (find-parent-frame (current-child) (find-current-root)))))
For faster browsing, not all history is shown. View entire blame