src/clfswm.lisp (:unmap-notify, :destroy-notify): Show all children just after the...
[clfswm.git] / src / clfswm.lisp
blob4e42f2e3c10ef9c56f958ae399ceba61bbe1e712
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Main functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2011 Philippe Brochard <hocwp@free.fr>
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*)))
46 (define-handler main-mode :configure-request (stack-mode window x y width height border-width value-mask)
47 (labels ((has-x (mask) (= 1 (logand mask 1)))
48 (has-y (mask) (= 2 (logand mask 2)))
49 (has-w (mask) (= 4 (logand mask 4)))
50 (has-h (mask) (= 8 (logand mask 8)))
51 (has-bw (mask) (= 16 (logand mask 16)))
52 (has-stackmode (mask) (= 64 (logand mask 64)))
53 (adjust-from-request ()
54 (when (has-x value-mask) (setf (xlib:drawable-x window) x))
55 (when (has-y value-mask) (setf (xlib:drawable-y window) y))
56 (when (has-h value-mask) (setf (xlib:drawable-height window) height))
57 (when (has-w value-mask) (setf (xlib:drawable-width window) width))))
58 (xlib:with-state (window)
59 (when (has-bw value-mask)
60 (setf (xlib:drawable-border-width window) border-width))
61 (if (find-child window *current-root*)
62 (let ((parent (find-parent-frame window *current-root*)))
63 (if (and parent (managed-window-p window parent))
64 (adapt-child-to-parent window parent)
65 (adjust-from-request)))
66 (adjust-from-request))
67 (send-configuration-notify window (xlib:drawable-x window) (xlib:drawable-y window)
68 (xlib:drawable-width window) (xlib:drawable-height window)
69 (xlib:drawable-border-width window))
70 (when (has-stackmode value-mask)
71 (case stack-mode
72 (:above
73 (unless (null-size-window-p window)
74 (when (or (child-equal-p window *current-child*)
75 (is-in-current-child-p window))
76 (raise-window window)
77 (focus-window window)
78 (focus-all-children window (find-parent-frame window *current-root*))))))))))
81 (define-handler main-mode :map-request (window send-event-p)
82 (unless send-event-p
83 (unhide-window window)
84 (process-new-window window)
85 (map-window window)
86 (unless (null-size-window-p window)
87 (multiple-value-bind (never-managed raise)
88 (never-managed-window-p window)
89 (unless (and never-managed raise)
90 (show-all-children))))))
93 (define-handler main-mode :unmap-notify (send-event-p event-window window)
94 (unless (and (not send-event-p)
95 (not (xlib:window-equal window event-window)))
96 (when (find-child window *root-frame*)
97 (clean-windows-in-all-frames)
98 (show-all-children)
99 (delete-child-in-all-frames window)
100 (show-all-children))))
103 (define-handler main-mode :destroy-notify (send-event-p event-window window)
104 (unless (or send-event-p
105 (xlib:window-equal window event-window))
106 (when (find-child window *root-frame*)
107 (clean-windows-in-all-frames)
108 (show-all-children)
109 (delete-child-in-all-frames window)
110 (show-all-children))))
112 (define-handler main-mode :enter-notify (window root-x root-y)
113 (unless (and (> root-x (- (xlib:screen-width *screen*) 3))
114 (> root-y (- (xlib:screen-height *screen*) 3)))
115 (case (if (frame-p *current-child*)
116 (frame-focus-policy *current-child*)
117 *default-focus-policy*)
118 (:sloppy (focus-window window))
119 (:sloppy-strict (when (and (frame-p *current-child*)
120 (child-member window (frame-child *current-child*)))
121 (focus-window window)))
122 (:sloppy-select (let* ((child (find-child-under-mouse root-x root-y))
123 (parent (find-parent-frame child)))
124 (unless (or (child-equal-p child *current-root*)
125 (equal (typecase child
126 (xlib:window parent)
127 (t child))
128 *current-child*))
129 (focus-all-children child parent)
130 (show-all-children)))))))
132 (define-handler main-mode :exposure (window)
133 (awhen (find-frame-window window *current-root*)
134 (display-frame-info it)))
137 (defun error-handler (display error-key &rest key-vals &key asynchronous &allow-other-keys)
138 "Handle X errors"
139 (cond
140 ;; ignore asynchronous window errors
141 ((and asynchronous
142 (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error)))
143 (format t "Ignoring XLib asynchronous error: ~s~%" error-key))
144 ((eq error-key 'xlib:access-error)
145 (write-line "Another window manager is running.")
146 (throw 'exit-clfswm nil))
147 ;; all other asynchronous errors are printed.
148 (asynchronous
149 (format t "Caught Asynchronous X Error: ~s ~s" error-key key-vals))
151 (apply 'error error-key :display display :error-key error-key key-vals))))
154 (defun main-loop ()
155 (loop
156 (call-hook *loop-hook*)
157 (process-timers)
158 (with-xlib-protect
159 (when (xlib:event-listen *display* *loop-timeout*)
160 (xlib:process-event *display* :handler #'handle-event))
161 (xlib:display-finish-output *display*))))
165 (defun open-display (display-str protocol)
166 (multiple-value-bind (host display-num) (parse-display-string display-str)
167 (setf *display* (xlib:open-display host :display display-num :protocol protocol)
168 (xlib:display-error-handler *display*) 'error-handler
169 (getenv "DISPLAY") display-str)))
173 (defun default-init-hook ()
174 (let ((frame (add-frame (create-frame :name "Default"
175 :layout nil :x 0.05 :y 0.05
176 :w 0.9 :h 0.9)
177 *root-frame*)))
178 (setf *current-child* frame)))
181 (defun init-display ()
182 (fill-handle-event-fun-symbols)
183 (assoc-keyword-handle-event 'main-mode)
184 (setf *screen* (first (xlib:display-roots *display*))
185 *root* (xlib:screen-root *screen*)
186 *no-focus-window* (xlib:create-window :parent *root* :x 0 :y 0 :width 1 :height 1)
187 *default-font* (xlib:open-font *display* *default-font-string*)
188 *pixmap-buffer* (xlib:create-pixmap :width (xlib:screen-width *screen*)
189 :height (xlib:screen-height *screen*)
190 :depth (xlib:screen-root-depth *screen*)
191 :drawable *root*)
192 *in-second-mode* nil)
193 (init-modifier-list)
194 (xgrab-init-pointer)
195 (xgrab-init-keyboard)
196 (init-last-child)
197 (call-hook *binding-hook*)
198 (clear-timers)
199 (map-window *no-focus-window*)
200 (dbg *display*)
201 (setf (xlib:window-event-mask *root*) (xlib:make-event-mask :substructure-redirect
202 :substructure-notify
203 :property-change
204 :exposure
205 :button-press
206 :button-release
207 :pointer-motion))
208 ;;(intern-atoms *display*)
209 (netwm-set-properties)
210 (xlib:display-force-output *display*)
211 (setf *child-selection* nil)
212 (setf *root-frame* (create-frame :name "Root" :number 0)
213 *current-root* *root-frame*
214 *current-child* *current-root*)
215 (call-hook *init-hook*)
216 (process-existing-windows *screen*)
217 (show-all-children)
218 (grab-main-keys)
219 (xlib:display-finish-output *display*))
224 (defun read-conf-file ()
225 (let* ((conf (conf-file-name)))
226 (if conf
227 (handler-case (load conf)
228 (error (c)
229 (format t "~2%*** Error loading configuration file: ~A ***~&~A~%" conf c)
230 (values nil (format nil "~s" c) conf))
231 (:no-error (&rest args)
232 (declare (ignore args))
233 (values t nil conf)))
234 (values t nil nil))))
241 (defun exit-clfswm ()
242 "Exit clfswm"
243 (throw 'exit-clfswm nil))
245 (defun reset-clfswm ()
246 "Reset clfswm"
247 (throw 'exit-main-loop nil))
252 (defun main-unprotected (&key (display (or (getenv "DISPLAY") ":0")) protocol
253 (base-dir (asdf:system-source-directory :clfswm))
254 (read-conf-file-p t) (alternate-conf nil)
255 error-msg)
256 (setf *contrib-dir* (merge-pathnames "contrib/" base-dir))
257 (conf-file-name alternate-conf)
258 (when read-conf-file-p
259 (read-conf-file))
260 (create-configuration-menu :clear t)
261 (call-hook *main-entrance-hook*)
262 (handler-case
263 (open-display display protocol)
264 (xlib:access-error (c)
265 (format t "~&~A~&Maybe another window manager is running. [1]~%" c)
266 (force-output)
267 (exit-clfswm)))
268 (handler-case
269 (init-display)
270 (xlib:access-error (c)
271 (ungrab-main-keys)
272 (xlib:destroy-window *no-focus-window*)
273 (xlib:close-display *display*)
274 (format t "~&~A~&Maybe another window manager is running. [2]~%" c)
275 (force-output)
276 (exit-clfswm)))
277 (when error-msg
278 (info-mode error-msg))
279 (catch 'exit-main-loop
280 (unwind-protect
281 (main-loop)
282 (ungrab-main-keys)
283 (xlib:destroy-window *no-focus-window*)
284 (xlib:free-pixmap *pixmap-buffer*)
285 (destroy-all-frames-window)
286 (call-hook *close-hook*)
287 (xlib:close-display *display*)
288 #+:event-debug
289 (format t "~2&Unhandled events: ~A~%" *unhandled-events*))))
293 (defun main (&key (display (or (getenv "DISPLAY") ":0")) protocol
294 (base-dir (asdf:system-source-directory :clfswm))
295 (read-conf-file-p t)
296 (alternate-conf nil))
297 (let (error-msg)
298 (catch 'exit-clfswm
299 (loop
300 (handler-case
301 (if *other-window-manager*
302 (run-other-window-manager)
303 (main-unprotected :display display :protocol protocol :base-dir base-dir
304 :read-conf-file-p read-conf-file-p
305 :alternate-conf alternate-conf
306 :error-msg error-msg))
307 (error (c)
308 (let ((msg (format nil "CLFSWM Error: ~A." c)))
309 (format t "~&~A~%Reinitializing...~%" msg)
310 (setf error-msg (list (list msg *info-color-title*)
311 "Reinitializing...")))))))))