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