ae1729933e8fec496610ae23f2efcebfce6f0e33
[clfswm.git] / src / clfswm.lisp
blobae1729933e8fec496610ae23f2efcebfce6f0e33
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 (raise-window window)
84 (focus-window window)
85 (focus-all-children window (find-parent-frame window (find-current-root)))))))))
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 (unless (null-size-window-p 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 (xlib:unmap-window window)
114 (remove-child-in-all-frames window)
115 (unless (null-size-window-in-frame *root-frame*)
116 (show-all-children)))))
122 (define-handler main-mode :destroy-notify (send-event-p event-window window)
123 (unless (or send-event-p
124 (xlib:window-equal window event-window))
125 (when (find-child window *root-frame*)
126 (delete-child-in-all-frames window)
127 (unless (null-size-window-in-frame *root-frame*)
128 (show-all-children))
129 (xlib:destroy-window window))))
131 (define-handler main-mode :enter-notify (window root-x root-y)
132 (unless (and (> root-x (- (xlib:screen-width *screen*) 3))
133 (> root-y (- (xlib:screen-height *screen*) 3)))
134 (case (if (frame-p (current-child))
135 (frame-focus-policy (current-child))
136 *default-focus-policy*)
137 (:sloppy (focus-window window))
138 (:sloppy-strict (when (and (frame-p (current-child))
139 (child-member window (frame-child (current-child))))
140 (focus-window window)))
141 (:sloppy-select (let* ((child (find-child-under-mouse root-x root-y))
142 (parent (find-parent-frame child)))
143 (unless (or (child-root-p child)
144 (equal (typecase child
145 (xlib:window parent)
146 (t child))
147 (current-child)))
148 (focus-all-children child parent)
149 (show-all-children)))))))
151 (define-handler main-mode :exposure (window)
152 (awhen (find-frame-window window)
153 (display-frame-info it)))
156 (define-handler main-mode :configure-notify (window)
157 (when (child-equal-p window *root*)
158 (unless (null-size-window-in-frame *root-frame*)
159 (unless (eql (place-frames-from-xinerama-infos) :update)
160 (finish-configuring-root))
161 (show-all-children)
162 (call-hook *root-size-change-hook*))))
165 (defun error-handler (display error-key &rest key-vals &key asynchronous &allow-other-keys)
166 "Handle X errors"
167 (cond
168 ;; ignore asynchronous window errors
169 ((and asynchronous
170 (find error-key '(xlib:window-error xlib:drawable-error xlib:match-error)))
171 #+:xlib-debug (format t "~&Ignoring XLib asynchronous error: ~s~%" error-key))
172 ((eq error-key 'xlib:access-error)
173 (write-line "~&Another window manager is running.")
174 (throw 'exit-clfswm nil))
175 ;; all other asynchronous errors are printed.
176 (asynchronous
177 #+:xlib-debug (format t "~&Caught Asynchronous X Error: ~s ~s" error-key key-vals))
178 ;;((find error-key '(xlib:window-error xlib:drawable-error xlib:match-error))
179 ;; (format t "~&Ignoring Xlib error: ~S ~S~%" error-key key-vals))
181 (apply 'error error-key :display display :error-key error-key key-vals))))
184 (defun main-loop ()
185 (loop
186 (with-xlib-protect (:main-loop nil)
187 (call-hook *loop-hook*)
188 (process-timers)
189 (when (xlib:event-listen *display* *loop-timeout*)
190 (xlib:process-event *display* :handler #'handle-event))
191 (xlib:display-finish-output *display*)
192 (setf *x-error-count* 0))))
197 (defun open-display (display-str protocol)
198 (multiple-value-bind (host display-num) (parse-display-string display-str)
199 (setf *display* (xlib:open-display host :display display-num :protocol protocol)
200 (xlib:display-error-handler *display*) 'error-handler
201 (getenv "DISPLAY") display-str)))
205 (defun default-init-hook ()
206 (place-frames-from-xinerama-infos)
207 (finish-configuring-root))
210 (defun init-display ()
211 (reset-root-list)
212 (reset-bind-or-jump-slots)
213 (reset-open-menu)
214 (fill-handle-event-fun-symbols)
215 (assoc-keyword-handle-event 'main-mode)
216 (setf *screen* (first (xlib:display-roots *display*))
217 *root* (xlib:screen-root *screen*)
218 *no-focus-window* (xlib:create-window :parent *root* :x 0 :y 0 :width 1 :height 1)
219 *default-font* (xlib:open-font *display* *default-font-string*)
220 *pixmap-buffer* (xlib:create-pixmap :width (xlib:screen-width *screen*)
221 :height (xlib:screen-height *screen*)
222 :depth (xlib:screen-root-depth *screen*)
223 :drawable *root*)
224 *in-second-mode* nil
225 *x-error-count* 0)
226 (store-root-background)
227 (init-modifier-list)
228 (xgrab-init-pointer)
229 (xgrab-init-keyboard)
230 (init-last-child)
231 (call-hook *binding-hook*)
232 (clear-timers)
233 (map-window *no-focus-window*)
234 (dbg *display*)
235 (setf (xlib:window-event-mask *root*) (xlib:make-event-mask :substructure-redirect
236 :substructure-notify
237 :structure-notify
238 :property-change
239 ;;:resize-redirect
240 :exposure
241 :button-press
242 :button-release
243 :pointer-motion))
244 (xlib:display-finish-output *display*)
245 ;;(intern-atoms *display*)
246 (netwm-set-properties)
247 (xlib:display-force-output *display*)
248 (setf *child-selection* nil)
249 (setf *root-frame* (create-frame :name "Root" :number 0)
250 (current-child) *root-frame*)
251 (call-hook *init-hook*)
252 (process-existing-windows *screen*)
253 (show-all-children)
254 (grab-main-keys)
255 (xlib:display-finish-output *display*)
256 (optimize-event-hook))
261 (defun read-conf-file ()
262 (let* ((conf (conf-file-name)))
263 (if conf
264 (handler-case (load conf)
265 (error (c)
266 (format t "~2%*** Error loading configuration file: ~A ***~&~A~%" conf c)
267 (values nil (format nil "~s" c) conf))
268 (:no-error (&rest args)
269 (declare (ignore args))
270 (values t nil conf)))
271 (values t nil nil))))
278 (defun exit-clfswm ()
279 "Exit clfswm"
280 (throw 'exit-clfswm nil))
282 (defun reset-clfswm ()
283 "Reset clfswm"
284 (throw 'exit-main-loop nil))
289 (defun main-unprotected (&key (display (or (getenv "DISPLAY") ":0")) protocol
290 (read-conf-file-p t) (alternate-conf nil)
291 error-msg)
292 (conf-file-name alternate-conf)
293 (when read-conf-file-p
294 (read-conf-file))
295 (create-configuration-menu :clear t)
296 (call-hook *main-entrance-hook*)
297 (handler-case
298 (open-display display protocol)
299 (xlib:access-error (c)
300 (format t "~&~A~&Maybe another window manager is running. [1]~%" c)
301 (force-output)
302 (exit-clfswm)))
303 (handler-case
304 (init-display)
305 (xlib:access-error (c)
306 (ungrab-main-keys)
307 (xlib:destroy-window *no-focus-window*)
308 (xlib:close-display *display*)
309 (format t "~&~A~&Maybe another window manager is running. [2]~%" c)
310 (force-output)
311 (exit-clfswm)))
312 (when error-msg
313 (info-mode error-msg))
314 (catch 'exit-main-loop
315 (unwind-protect
316 (main-loop)
317 (progn
318 (ungrab-main-keys)
319 (xlib:destroy-window *no-focus-window*)
320 (xlib:free-pixmap *pixmap-buffer*)
321 (destroy-all-frames-window)
322 (call-hook *close-hook*)
323 (clear-event-hooks)
324 (xlib:close-display *display*)
325 #+:event-debug
326 (format t "~2&Unhandled events: ~A~%" *unhandled-events*)))))
330 (defun main (&key (display (or (getenv "DISPLAY") ":0")) protocol
331 (read-conf-file-p t)
332 (alternate-conf nil))
333 (let (error-msg)
334 (catch 'exit-clfswm
335 (loop
336 (handler-case
337 (if *other-window-manager*
338 (run-other-window-manager)
339 (main-unprotected :display display :protocol protocol
340 :read-conf-file-p read-conf-file-p
341 :alternate-conf alternate-conf
342 :error-msg error-msg))
343 (error (c)
344 (let ((msg (format nil "CLFSWM Error: ~A." c)))
345 (format t "~&~A~%Reinitializing...~%" msg)
346 (setf error-msg (list (list msg *info-color-title*)
347 "Reinitializing...")))))))))