826f0fc1df51ecaa8022a2da53a72652533f671c
[clfswm.git] / src / clfswm.lisp
blob826f0fc1df51ecaa8022a2da53a72652533f671c
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Main functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; --------------------------------------------------------------------------
26 (in-package :clfswm)
29 (define-handler main-mode :key-press (code state)
30 (funcall-key-from-code *main-keys* code state))
32 (define-handler main-mode :button-press (code state window root-x root-y)
33 (unless (funcall-button-from-code *main-mouse* code state window root-x root-y *fun-press*)
34 (replay-button-event)))
36 (define-handler main-mode :button-release (code state window root-x root-y)
37 (unless (funcall-button-from-code *main-mouse* code state window root-x root-y *fun-release*)
38 (replay-button-event)))
40 (define-handler main-mode :motion-notify (window root-x root-y)
41 (unless (compress-motion-notify)
42 (funcall-button-from-code *main-mouse* 'motion
43 (modifiers->state *default-modifiers*)
44 window root-x root-y *fun-press*)))
47 (define-handler main-mode :configure-request (stack-mode window x y width height border-width value-mask)
48 (let ((change nil))
49 (labels ((has-x (mask) (= 1 (logand mask 1)))
50 (has-y (mask) (= 2 (logand mask 2)))
51 (has-w (mask) (= 4 (logand mask 4)))
52 (has-h (mask) (= 8 (logand mask 8)))
53 (has-bw (mask) (= 16 (logand mask 16)))
54 (has-stackmode (mask) (= 64 (logand mask 64)))
55 (adjust-from-request ()
56 (when (has-x value-mask) (setf (x-drawable-x window) x
57 change :moved))
58 (when (has-y value-mask) (setf (x-drawable-y window) y
59 change :moved))
60 (when (has-h value-mask) (setf (x-drawable-height window) height
61 change :resized))
62 (when (has-w value-mask) (setf (x-drawable-width window) width
63 change :resized))))
64 (when window
65 (xlib:with-state (window)
66 (let ((current-root (find-current-root)))
67 (if (find-child window current-root)
68 (let ((parent (find-parent-frame window current-root)))
69 (if (and parent (managed-window-p window parent))
70 (setf change (adapt-child-to-parent window parent))
71 (adjust-from-request)))
72 (adjust-from-request)))
73 (when (has-bw value-mask)
74 (setf (x-drawable-border-width window) border-width
75 change :resized))
76 (when (has-stackmode value-mask)
77 (case stack-mode
78 (:above
79 (unless (null-size-window-p window)
80 (when (or (child-equal-p window (current-child))
81 (is-in-current-child-p window))
82 (setf change (or change :moved))
83 (show-all-children)
84 (raise-window window)
85 (focus-window window)
86 (focus-all-children window (find-parent-frame window (find-current-root)))))))))
87 (unless (eq change :resized)
88 ;; To be ICCCM compliant, send a fake configuration notify event only when
89 ;; the window has moved and not when it has been resized or the border width has changed.
90 (send-configuration-notify window (x-drawable-x window) (x-drawable-y window)
91 (x-drawable-width window) (x-drawable-height window)
92 (x-drawable-border-width window)))))))
95 (define-handler main-mode :map-request (window send-event-p)
96 (unless send-event-p
97 (unless (find-child window *root-frame*)
98 (unhide-window window)
99 (process-new-window window)
100 (map-window window)
101 (unless (null-size-window-p window)
102 (multiple-value-bind (never-managed raise)
103 (never-managed-window-p window)
104 (unless (and never-managed raise)
105 (show-all-children)))))))
109 (define-handler main-mode :unmap-notify (send-event-p event-window window)
110 (unless (and (not send-event-p)
111 (not (xlib:window-equal window event-window)))
112 (when (find-child window *root-frame*)
113 (setf (window-state window) +withdrawn-state+)
114 (xlib:unmap-window window)
115 (remove-child-in-all-frames window)
116 (unless (null-size-window-in-frame *root-frame*)
117 (show-all-children)))))
123 (define-handler main-mode :destroy-notify (send-event-p event-window window)
124 (unless (or send-event-p
125 (xlib:window-equal window event-window))
126 (when (find-child window *root-frame*)
127 (delete-child-in-all-frames window)
128 (unless (null-size-window-in-frame *root-frame*)
129 (show-all-children))
130 (xlib:destroy-window window))))
132 (define-handler main-mode :enter-notify (window root-x root-y)
133 (unless (and (> root-x (- (xlib:screen-width *screen*) 3))
134 (> root-y (- (xlib:screen-height *screen*) 3)))
135 (case (if (frame-p (current-child))
136 (frame-focus-policy (current-child))
137 *default-focus-policy*)
138 (:sloppy (focus-window window))
139 (:sloppy-strict (when (and (frame-p (current-child))
140 (child-member window (frame-child (current-child))))
141 (focus-window window)))
142 (:sloppy-select (let* ((child (find-child-under-mouse root-x root-y))
143 (parent (find-parent-frame child)))
144 (unless (or (child-root-p child)
145 (equal (typecase child
146 (xlib:window parent)
147 (t child))
148 (current-child)))
149 (focus-all-children child parent)
150 (show-all-children)))))))
152 (define-handler main-mode :exposure (window)
153 (awhen (find-frame-window window)
154 (display-frame-info it)))
157 (define-handler main-mode :configure-notify (window)
158 (when (child-equal-p window *root*)
159 (unless (null-size-window-in-frame *root-frame*)
160 (unless (eql (place-frames-from-xinerama-infos) :update)
161 (finish-configuring-root))
162 (show-all-children)
163 (call-hook *root-size-change-hook*))))
166 (defun error-handler (display error-key &rest key-vals &key asynchronous &allow-other-keys)
167 "Handle X errors"
168 (cond
169 ;; ignore asynchronous window errors
170 ((and asynchronous
171 (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error)))
172 #+:xlib-debug (format t "~&Ignoring XLib asynchronous error: ~s~%" error-key))
173 ((eq error-key 'xlib:access-error)
174 (write-line "~&Another window manager is running.")
175 (throw 'exit-clfswm nil))
176 ;; all other asynchronous errors are printed.
177 (asynchronous
178 #+:xlib-debug (format t "~&Caught Asynchronous X Error: ~s ~s" error-key key-vals))
179 ;;((find error-key '(xlib:window-error xlib:drawable-error xlib:match-error))
180 ;; (format t "~&Ignoring Xlib error: ~S ~S~%" error-key key-vals))
182 (apply 'error error-key :display display :error-key error-key key-vals))))
185 (defun main-loop ()
186 (loop
187 (with-xlib-protect (:main-loop nil)
188 (call-hook *loop-hook*)
189 (process-timers)
190 (when (xlib:event-listen *display* *loop-timeout*)
191 (xlib:process-event *display* :handler #'handle-event))
192 (xlib:display-finish-output *display*)
193 (setf *x-error-count* 0))))
198 (defun open-display (display-str protocol)
199 (multiple-value-bind (host display-num) (parse-display-string display-str)
200 (setf *display* (xlib:open-display host :display display-num :protocol protocol)
201 (xlib:display-error-handler *display*) 'error-handler
202 (getenv "DISPLAY") display-str)))
206 (defun default-init-hook ()
207 (place-frames-from-xinerama-infos)
208 (finish-configuring-root))
211 (defun init-display ()
212 (reset-root-list)
213 (reset-last-head-size)
214 (reset-bind-or-jump-slots)
215 (reset-open-menu)
216 (fill-handle-event-fun-symbols)
217 (assoc-keyword-handle-event 'main-mode)
218 (setf *screen* (first (xlib:display-roots *display*))
219 *root* (xlib:screen-root *screen*)
220 *no-focus-window* (xlib:create-window :parent *root* :x 0 :y 0 :width 1 :height 1)
221 *default-font* (xlib:open-font *display* *default-font-string*)
222 *pixmap-buffer* (xlib:create-pixmap :width (xlib:screen-width *screen*)
223 :height (xlib:screen-height *screen*)
224 :depth (xlib:screen-root-depth *screen*)
225 :drawable *root*)
226 *in-second-mode* nil
227 *x-error-count* 0)
228 (store-root-background)
229 (init-modifier-list)
230 (xgrab-init-pointer)
231 (xgrab-init-keyboard)
232 (init-last-child)
233 (call-hook *binding-hook*)
234 (clear-timers)
235 (map-window *no-focus-window*)
236 (dbg *display*)
237 (setf (xlib:window-event-mask *root*) (xlib:make-event-mask :substructure-redirect
238 :substructure-notify
239 :structure-notify
240 :property-change
241 ;;:resize-redirect
242 :exposure
243 :button-press
244 :button-release
245 :pointer-motion))
246 (xlib:display-finish-output *display*)
247 ;;(intern-atoms *display*)
248 (netwm-set-properties)
249 (xlib:display-force-output *display*)
250 (setf *child-selection* nil)
251 (setf *root-frame* (create-frame :name "Root" :number 0)
252 (current-child) *root-frame*)
253 (call-hook *init-hook*)
254 (process-existing-windows *screen*)
255 (show-all-children)
256 (grab-main-keys)
257 (xlib:display-finish-output *display*)
258 (optimize-event-hook))
263 (defun read-conf-file ()
264 (let* ((conf (conf-file-name)))
265 (if conf
266 (handler-case (load conf)
267 (error (c)
268 (format t "~2%*** Error loading configuration file: ~A ***~&~A~%" conf c)
269 (values nil (format nil "~s" c) conf))
270 (:no-error (&rest args)
271 (declare (ignore args))
272 (values t nil conf)))
273 (values t nil nil))))
280 (defun exit-clfswm ()
281 "Exit clfswm"
282 (throw 'exit-clfswm nil))
284 (defun reset-clfswm ()
285 "Reset clfswm"
286 (throw 'exit-main-loop nil))
291 (defun main-unprotected (&key (display (or (getenv "DISPLAY") ":0")) protocol
292 (read-conf-file-p t) (alternate-conf nil)
293 error-msg)
294 (conf-file-name alternate-conf)
295 (when read-conf-file-p
296 (read-conf-file))
297 (create-configuration-menu :clear t)
298 (call-hook *main-entrance-hook*)
299 (handler-case
300 (open-display display protocol)
301 (xlib:access-error (c)
302 (format t "~&~A~&Maybe another window manager is running. [1]~%" c)
303 (force-output)
304 (exit-clfswm)))
305 (handler-case
306 (init-display)
307 (xlib:access-error (c)
308 (ungrab-main-keys)
309 (xlib:destroy-window *no-focus-window*)
310 (xlib:close-display *display*)
311 (format t "~&~A~&Maybe another window manager is running. [2]~%" c)
312 (force-output)
313 (exit-clfswm)))
314 (when error-msg
315 (info-mode error-msg))
316 (catch 'exit-main-loop
317 (unwind-protect
318 (main-loop)
319 (progn
320 (ungrab-main-keys)
321 (xlib:destroy-window *no-focus-window*)
322 (xlib:free-pixmap *pixmap-buffer*)
323 (destroy-all-frames-window)
324 (call-hook *close-hook*)
325 (clear-event-hooks)
326 (xlib:close-display *display*)
327 #+:event-debug
328 (format t "~2&Unhandled events: ~A~%" *unhandled-events*)))))
332 (defun main (&key (display (or (getenv "DISPLAY") ":0")) protocol
333 (read-conf-file-p t)
334 (alternate-conf nil))
335 (let (error-msg)
336 (catch 'exit-clfswm
337 (loop
338 (handler-case
339 (if *other-window-manager*
340 (run-other-window-manager)
341 (main-unprotected :display display :protocol protocol
342 :read-conf-file-p read-conf-file-p
343 :alternate-conf alternate-conf
344 :error-msg error-msg))
345 (error (c)
346 (let ((msg (format nil "CLFSWM Error: ~A." c)))
347 (format t "~&~A~%Reinitializing...~%" msg)
348 (setf error-msg (list (list msg *info-color-title*)
349 "Reinitializing...")))))))))