Better handler for xlib:access-errors
[clfswm.git] / src / clfswm.lisp
blob3b6545a3e56bb132974cd171b77cc735fdb36e4d
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Main functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2005-2013 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)
28 (defparameter *clfswm-initializing* nil)
30 (define-handler main-mode :key-press (code state)
31 (funcall-key-from-code *main-keys* code state))
33 (define-handler main-mode :button-press (code state window root-x root-y)
34 (unless (funcall-button-from-code *main-mouse* code state window root-x root-y *fun-press*)
35 (replay-button-event)))
37 (define-handler main-mode :button-release (code state window root-x root-y)
38 (unless (funcall-button-from-code *main-mouse* code state window root-x root-y *fun-release*)
39 (replay-button-event)))
41 (define-handler main-mode :motion-notify (window root-x root-y)
42 (unless (compress-motion-notify)
43 (funcall-button-from-code *main-mouse* 'motion
44 (modifiers->state *default-modifiers*)
45 window root-x root-y *fun-press*)))
48 (define-handler main-mode :configure-request (stack-mode window x y width height border-width value-mask)
49 (let ((change nil))
50 (labels ((has-x (mask) (= 1 (logand mask 1)))
51 (has-y (mask) (= 2 (logand mask 2)))
52 (has-w (mask) (= 4 (logand mask 4)))
53 (has-h (mask) (= 8 (logand mask 8)))
54 (has-bw (mask) (= 16 (logand mask 16)))
55 (has-stackmode (mask) (= 64 (logand mask 64)))
56 (adjust-from-request ()
57 (when (has-x value-mask) (setf (x-drawable-x window) x
58 change :moved))
59 (when (has-y value-mask) (setf (x-drawable-y window) y
60 change :moved))
61 (when (has-h value-mask) (setf (x-drawable-height window) height
62 change :resized))
63 (when (has-w value-mask) (setf (x-drawable-width window) width
64 change :resized))))
65 (when window
66 (xlib:with-state (window)
67 (let ((current-root (find-current-root)))
68 (if (find-child window current-root)
69 (let ((parent (find-parent-frame window current-root)))
70 (if (and parent (managed-window-p window parent))
71 (setf change (adapt-child-to-parent window parent))
72 (adjust-from-request)))
73 (adjust-from-request)))
74 (when (has-bw value-mask)
75 (setf (x-drawable-border-width window) border-width
76 change :resized))
77 (when (has-stackmode value-mask)
78 (case stack-mode
79 (:above
80 (when (or (child-equal-p window (current-child))
81 (is-in-current-child-p window))
82 (setf change (or change :moved))
83 (when *steal-focus*
84 (focus-window window)
85 (when (focus-all-children window (find-parent-frame window (find-current-root)))
86 (show-all-children))))))))
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 (multiple-value-bind (never-managed raise)
102 (never-managed-window-p window)
103 (unless (and never-managed raise)
104 (show-all-children))))))
108 (define-handler main-mode :unmap-notify (send-event-p event-window window)
109 (unless (and (not send-event-p)
110 (not (xlib:window-equal window event-window)))
111 (when (find-child window *root-frame*)
112 (setf (window-state window) +withdrawn-state+)
113 (remove-child-in-all-frames window)
114 ;;(xlib:unmap-window window)
115 (show-all-children))))
118 (define-handler main-mode :destroy-notify (send-event-p event-window window)
119 (unless (or send-event-p
120 (xlib:window-equal window event-window))
121 (when (find-child window *root-frame*)
122 (delete-child-in-all-frames window)
123 (xlib:destroy-window window)
124 (show-all-children))))
127 (define-handler main-mode :enter-notify (window root-x root-y)
128 (unless (and (> root-x (- (screen-width) 3))
129 (> root-y (- (screen-height) 3)))
130 (manage-focus window root-x root-y)))
133 (define-handler main-mode :exposure (window)
134 (awhen (find-frame-window window)
135 (display-frame-info it)))
138 (define-handler main-mode :configure-notify (window)
139 (when (child-equal-p window *root*)
140 (unless (eql (place-frames-from-xinerama-infos) :update)
141 (finish-configuring-root))
142 (show-all-children)
143 (call-hook *root-size-change-hook*)))
146 (defun error-handler (display error-key &rest key-vals &key asynchronous &allow-other-keys)
147 "Handle X errors"
148 (cond
149 ;; ignore asynchronous window errors
150 ((and asynchronous
151 (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error)))
152 #+:xlib-debug (format t "~&Ignoring XLib asynchronous error: ~s~%" error-key))
153 ((eq error-key 'xlib:access-error)
154 (if *clfswm-initializing*
155 (progn
156 (format t "~3&Another window manager is running. Exiting...~%")
157 (throw 'exit-clfswm nil))
158 #+:xlib-debug
159 (format t "~&Ignoring XLib asynchronous access error: ~s~%" error-key)))
160 ;; all other asynchronous errors are printed.
161 (asynchronous
162 #+:xlib-debug (format t "~&Caught Asynchronous X Error: ~s ~s" error-key key-vals))
163 ;;((find error-key '(xlib:window-error xlib:drawable-error xlib:match-error))
164 ;; (format t "~&Ignoring Xlib error: ~S ~S~%" error-key key-vals))
166 (apply 'error error-key :display display :error-key error-key key-vals))))
171 (defun main-loop ()
172 (loop
173 (with-xlib-protect (:main-loop nil)
174 (call-hook *loop-hook*)
175 (process-timers)
176 (when (xlib:event-listen *display* *loop-timeout*)
177 (xlib:process-event *display* :handler #'handle-event))
178 (xlib:display-finish-output *display*)
179 (setf *x-error-count* 0))))
184 (defun open-display (display-str protocol)
185 (multiple-value-bind (host display-num) (parse-display-string display-str)
186 (setf *display* (xlib:open-display host :display display-num :protocol protocol)
187 (xlib:display-error-handler *display*) 'error-handler
188 (getenv "DISPLAY") display-str)))
192 (defun default-init-hook ()
193 (place-frames-from-xinerama-infos)
194 (finish-configuring-root))
197 (defun init-display ()
198 (reset-root-list)
199 (reset-last-head-size)
200 (reset-bind-or-jump-slots)
201 (reset-open-menu)
202 (fill-handle-event-fun-symbols)
203 (assoc-keyword-handle-event 'main-mode)
204 (setf *screen* (first (xlib:display-roots *display*))
205 *root* (xlib:screen-root *screen*)
206 *no-focus-window* (xlib:create-window :parent *root* :x 0 :y 0 :width 1 :height 1)
207 *default-font* (xlib:open-font *display* *default-font-string*)
208 *pixmap-buffer* (xlib:create-pixmap :width (screen-width)
209 :height (screen-height)
210 :depth (xlib:screen-root-depth *screen*)
211 :drawable *root*)
212 *in-second-mode* nil
213 *x-error-count* 0
214 *expose-current-number* 0
215 *expose-child-list* nil)
216 (store-root-background)
217 (init-modifier-list)
218 (xgrab-init-pointer)
219 (xgrab-init-keyboard)
220 (init-last-child)
221 (call-hook *binding-hook*)
222 (clear-timers)
223 (map-window *no-focus-window*)
224 (dbg *display*)
225 (setf (xlib:window-event-mask *root*) (xlib:make-event-mask :substructure-redirect
226 :substructure-notify
227 :structure-notify
228 :property-change
229 ;;:resize-redirect
230 :exposure
231 :button-press
232 :button-release
233 :pointer-motion))
234 (xlib:display-finish-output *display*)
235 ;;(intern-atoms *display*)
236 (netwm-set-properties)
237 (xlib:display-force-output *display*)
238 (setf *child-selection* nil)
239 (setf *root-frame* (create-frame :name "Root" :number 0)
240 (current-child) *root-frame*)
241 (call-hook *init-hook*)
242 (process-existing-windows *screen*)
243 (show-all-children)
244 (grab-main-keys)
245 (xlib:display-finish-output *display*)
246 (optimize-event-hook))
251 (defun read-conf-file ()
252 (let* ((conf (conf-file-name)))
253 (if conf
254 (handler-case (load conf)
255 (error (c)
256 (format t "~2%*** Error loading configuration file: ~A ***~&~A~%" conf c)
257 (values nil (format nil "~s" c) conf))
258 (:no-error (&rest args)
259 (declare (ignore args))
260 (values t nil conf)))
261 (values t nil nil))))
268 (defun exit-clfswm ()
269 "Exit clfswm"
270 (throw 'exit-clfswm nil))
272 (defun reset-clfswm ()
273 "Reset clfswm"
274 (throw 'exit-main-loop nil))
279 (defun main-unprotected (&key (display (or (getenv "DISPLAY") ":0")) protocol
280 (read-conf-file-p t) (alternate-conf nil)
281 error-msg)
282 (setf *clfswm-initializing* t)
283 (conf-file-name alternate-conf)
284 (when read-conf-file-p
285 (read-conf-file))
286 (create-configuration-menu :clear t)
287 (call-hook *main-entrance-hook*)
288 (handler-case
289 (open-display display protocol)
290 (xlib:access-error (c)
291 (format t "~&~A~&Maybe another window manager is running. [1]~%" c)
292 (force-output)
293 (exit-clfswm)))
294 (handler-case
295 (init-display)
296 (xlib:access-error (c)
297 (ungrab-main-keys)
298 (xlib:destroy-window *no-focus-window*)
299 (xlib:close-display *display*)
300 (format t "~&~A~&Maybe another window manager is running. [2]~%" c)
301 (force-output)
302 (exit-clfswm)))
303 (when error-msg
304 (info-mode error-msg))
305 (setf *clfswm-initializing* nil)
306 (catch 'exit-main-loop
307 (unwind-protect
308 (main-loop)
309 (progn
310 (ungrab-main-keys)
311 (xlib:destroy-window *no-focus-window*)
312 (xlib:free-pixmap *pixmap-buffer*)
313 (destroy-all-frames-window)
314 (call-hook *close-hook*)
315 (clear-event-hooks)
316 (xlib:close-display *display*)
317 #+:event-debug
318 (format t "~2&Unhandled events: ~A~%" *unhandled-events*)))))
322 (defun main (&key (display (or (getenv "DISPLAY") ":0")) protocol
323 (read-conf-file-p t)
324 (alternate-conf nil))
325 (let (error-msg)
326 (catch 'exit-clfswm
327 (loop
328 (handler-case
329 (if *other-window-manager*
330 (run-other-window-manager)
331 (main-unprotected :display display :protocol protocol
332 :read-conf-file-p read-conf-file-p
333 :alternate-conf alternate-conf
334 :error-msg error-msg))
335 (error (c)
336 (let ((msg (format nil "CLFSWM Error: ~A." c)))
337 (format t "~&~A~%Reinitializing...~%" msg)
338 (setf error-msg (list (list msg *info-color-title*)
339 "Reinitializing...")))))))))