Destroy window is needed in some cases
[clfswm.git] / src / clfswm.lisp
blobec70a9a6db68a4cefd0aeb38171b7ca276fdadd0
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 (when (or (child-equal-p window (current-child))
80 (is-in-current-child-p window))
81 (setf change (or change :moved))
82 (focus-window window)
83 (focus-all-children window (find-parent-frame window (find-current-root)))
84 (show-all-children))))))
85 (unless (eq change :resized)
86 ;; To be ICCCM compliant, send a fake configuration notify event only when
87 ;; the window has moved and not when it has been resized or the border width has changed.
88 (send-configuration-notify window (x-drawable-x window) (x-drawable-y window)
89 (x-drawable-width window) (x-drawable-height window)
90 (x-drawable-border-width window)))))))
93 (define-handler main-mode :map-request (window send-event-p)
94 (unless send-event-p
95 (unless (find-child window *root-frame*)
96 (unhide-window window)
97 (process-new-window window)
98 (map-window window)
99 (multiple-value-bind (never-managed raise)
100 (never-managed-window-p window)
101 (unless (and never-managed raise)
102 (show-all-children))))))
106 (define-handler main-mode :unmap-notify (send-event-p event-window window)
107 (unless (and (not send-event-p)
108 (not (xlib:window-equal window event-window)))
109 (when (find-child window *root-frame*)
110 (setf (window-state window) +withdrawn-state+)
111 (remove-child-in-all-frames window)
112 (show-all-children))))
115 (define-handler main-mode :destroy-notify (send-event-p event-window window)
116 (unless (or send-event-p
117 (xlib:window-equal window event-window))
118 (when (find-child window *root-frame*)
119 (delete-child-in-all-frames window)
120 (xlib:destroy-window window)
121 (show-all-children))))
124 (define-handler main-mode :enter-notify (window root-x root-y)
125 (unless (and (> root-x (- (xlib:screen-width *screen*) 3))
126 (> root-y (- (xlib:screen-height *screen*) 3)))
127 (manage-focus window root-x root-y)))
130 (define-handler main-mode :exposure (window)
131 (awhen (find-frame-window window)
132 (display-frame-info it)))
135 (define-handler main-mode :configure-notify (window)
136 (when (child-equal-p window *root*)
137 (unless (eql (place-frames-from-xinerama-infos) :update)
138 (finish-configuring-root))
139 (show-all-children)
140 (call-hook *root-size-change-hook*)))
143 (defun error-handler (display error-key &rest key-vals &key asynchronous &allow-other-keys)
144 "Handle X errors"
145 (cond
146 ;; ignore asynchronous window errors
147 ((and asynchronous
148 (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error)))
149 #+:xlib-debug (format t "~&Ignoring XLib asynchronous error: ~s~%" error-key))
150 ((eq error-key 'xlib:access-error)
151 (write-line "~&Another window manager is running.")
152 (throw 'exit-clfswm nil))
153 ;; all other asynchronous errors are printed.
154 (asynchronous
155 #+:xlib-debug (format t "~&Caught Asynchronous X Error: ~s ~s" error-key key-vals))
156 ;;((find error-key '(xlib:window-error xlib:drawable-error xlib:match-error))
157 ;; (format t "~&Ignoring Xlib error: ~S ~S~%" error-key key-vals))
159 (apply 'error error-key :display display :error-key error-key key-vals))))
162 (defun main-loop ()
163 (loop
164 (with-xlib-protect (:main-loop nil)
165 (call-hook *loop-hook*)
166 (process-timers)
167 (when (xlib:event-listen *display* *loop-timeout*)
168 (xlib:process-event *display* :handler #'handle-event))
169 (xlib:display-finish-output *display*)
170 (setf *x-error-count* 0))))
175 (defun open-display (display-str protocol)
176 (multiple-value-bind (host display-num) (parse-display-string display-str)
177 (setf *display* (xlib:open-display host :display display-num :protocol protocol)
178 (xlib:display-error-handler *display*) 'error-handler
179 (getenv "DISPLAY") display-str)))
183 (defun default-init-hook ()
184 (place-frames-from-xinerama-infos)
185 (finish-configuring-root))
188 (defun init-display ()
189 (reset-root-list)
190 (reset-last-head-size)
191 (reset-bind-or-jump-slots)
192 (reset-open-menu)
193 (fill-handle-event-fun-symbols)
194 (assoc-keyword-handle-event 'main-mode)
195 (setf *screen* (first (xlib:display-roots *display*))
196 *root* (xlib:screen-root *screen*)
197 *no-focus-window* (xlib:create-window :parent *root* :x 0 :y 0 :width 1 :height 1)
198 *default-font* (xlib:open-font *display* *default-font-string*)
199 *pixmap-buffer* (xlib:create-pixmap :width (xlib:screen-width *screen*)
200 :height (xlib:screen-height *screen*)
201 :depth (xlib:screen-root-depth *screen*)
202 :drawable *root*)
203 *in-second-mode* nil
204 *x-error-count* 0)
205 (store-root-background)
206 (init-modifier-list)
207 (xgrab-init-pointer)
208 (xgrab-init-keyboard)
209 (init-last-child)
210 (call-hook *binding-hook*)
211 (clear-timers)
212 (map-window *no-focus-window*)
213 (dbg *display*)
214 (setf (xlib:window-event-mask *root*) (xlib:make-event-mask :substructure-redirect
215 :substructure-notify
216 :structure-notify
217 :property-change
218 ;;:resize-redirect
219 :exposure
220 :button-press
221 :button-release
222 :pointer-motion))
223 (xlib:display-finish-output *display*)
224 ;;(intern-atoms *display*)
225 (netwm-set-properties)
226 (xlib:display-force-output *display*)
227 (setf *child-selection* nil)
228 (setf *root-frame* (create-frame :name "Root" :number 0)
229 (current-child) *root-frame*)
230 (call-hook *init-hook*)
231 (process-existing-windows *screen*)
232 (show-all-children)
233 (grab-main-keys)
234 (xlib:display-finish-output *display*)
235 (optimize-event-hook))
240 (defun read-conf-file ()
241 (let* ((conf (conf-file-name)))
242 (if conf
243 (handler-case (load conf)
244 (error (c)
245 (format t "~2%*** Error loading configuration file: ~A ***~&~A~%" conf c)
246 (values nil (format nil "~s" c) conf))
247 (:no-error (&rest args)
248 (declare (ignore args))
249 (values t nil conf)))
250 (values t nil nil))))
257 (defun exit-clfswm ()
258 "Exit clfswm"
259 (throw 'exit-clfswm nil))
261 (defun reset-clfswm ()
262 "Reset clfswm"
263 (throw 'exit-main-loop nil))
268 (defun main-unprotected (&key (display (or (getenv "DISPLAY") ":0")) protocol
269 (read-conf-file-p t) (alternate-conf nil)
270 error-msg)
271 (conf-file-name alternate-conf)
272 (when read-conf-file-p
273 (read-conf-file))
274 (create-configuration-menu :clear t)
275 (call-hook *main-entrance-hook*)
276 (handler-case
277 (open-display display protocol)
278 (xlib:access-error (c)
279 (format t "~&~A~&Maybe another window manager is running. [1]~%" c)
280 (force-output)
281 (exit-clfswm)))
282 (handler-case
283 (init-display)
284 (xlib:access-error (c)
285 (ungrab-main-keys)
286 (xlib:destroy-window *no-focus-window*)
287 (xlib:close-display *display*)
288 (format t "~&~A~&Maybe another window manager is running. [2]~%" c)
289 (force-output)
290 (exit-clfswm)))
291 (when error-msg
292 (info-mode error-msg))
293 (catch 'exit-main-loop
294 (unwind-protect
295 (main-loop)
296 (progn
297 (ungrab-main-keys)
298 (xlib:destroy-window *no-focus-window*)
299 (xlib:free-pixmap *pixmap-buffer*)
300 (destroy-all-frames-window)
301 (call-hook *close-hook*)
302 (clear-event-hooks)
303 (xlib:close-display *display*)
304 #+:event-debug
305 (format t "~2&Unhandled events: ~A~%" *unhandled-events*)))))
309 (defun main (&key (display (or (getenv "DISPLAY") ":0")) protocol
310 (read-conf-file-p t)
311 (alternate-conf nil))
312 (let (error-msg)
313 (catch 'exit-clfswm
314 (loop
315 (handler-case
316 (if *other-window-manager*
317 (run-other-window-manager)
318 (main-unprotected :display display :protocol protocol
319 :read-conf-file-p read-conf-file-p
320 :alternate-conf alternate-conf
321 :error-msg error-msg))
322 (error (c)
323 (let ((msg (format nil "CLFSWM Error: ~A." c)))
324 (format t "~&~A~%Reinitializing...~%" msg)
325 (setf error-msg (list (list msg *info-color-title*)
326 "Reinitializing...")))))))))