Rename *root-size-change* hook to *root-size-change-hook*
[clfswm.git] / src / xlib-util.lisp
blobe72438da681b32cc08f422938f6ee796f1d63435
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility 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 ;;; --------------------------------------------------------------------------
27 (in-package :clfswm)
30 ;; Window states
31 (defconstant +withdrawn-state+ 0)
32 (defconstant +normal-state+ 1)
33 (defconstant +iconic-state+ 3)
36 (defparameter *window-events* '(:structure-notify
37 :property-change
38 :colormap-change
39 :focus-change
40 :enter-window
41 :leave-window
42 :pointer-motion
43 :exposure)
44 "The events to listen for on managed windows.")
47 (defparameter +netwm-supported+
48 '(:_NET_SUPPORTING_WM_CHECK
49 :_NET_NUMBER_OF_DESKTOPS
50 :_NET_DESKTOP_GEOMETRY
51 :_NET_DESKTOP_VIEWPORT
52 :_NET_CURRENT_DESKTOP
53 :_NET_WM_WINDOW_TYPE
54 :_NET_CLIENT_LIST)
55 "Supported NETWM properties.
56 Window types are in +WINDOW-TYPES+.")
58 (defparameter +netwm-window-types+
59 '((:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
60 (:_NET_WM_WINDOW_TYPE_DOCK . :dock)
61 (:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
62 (:_NET_WM_WINDOW_TYPE_MENU . :menu)
63 (:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
64 (:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
65 (:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
66 (:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
67 "Alist mapping NETWM window types to keywords.")
72 (defparameter *x-error-count* 0)
73 (defparameter *max-x-error-count* 10000)
74 (defparameter *clfswm-x-error-filename* "/tmp/clfswm-backtrace.error")
77 (defmacro with-xlib-protect ((&optional name tag) &body body)
78 "Ignore Xlib errors in body."
79 `(handler-case
80 (with-simple-restart (top-level "Return to clfswm's top level")
81 ,@body)
82 (xlib::x-error (c)
83 (incf *x-error-count*)
84 (when (> *x-error-count* *max-x-error-count*)
85 (format t "Xlib error: ~A ~A: ~A~%" ,name (if ,tag ,tag ',body) c)
86 (force-output)
87 (write-backtrace *clfswm-x-error-filename*
88 (format nil "~%------- Additional information ---------
89 Xlib error: ~A ~A: ~A
90 Body: ~A
92 Features: ~A"
93 ,name ,tag c ',body
94 *features*))
95 (error "Too many X errors: ~A (logged in ~A)" c *clfswm-x-error-filename*))
96 #+:xlib-debug
97 (progn
98 (format t "Xlib error: ~A ~A: ~A~%" ,name (if ,tag ,tag ',body) c)
99 (force-output)))))
102 ;;(defmacro with-xlib-protect ((&optional name tag) &body body)
103 ;; `(progn
104 ;; ,@body))
108 (defmacro with-x-pointer (&body body)
109 "Bind (x y) to mouse pointer positions"
110 `(multiple-value-bind (x y)
111 (xlib:query-pointer *root*)
112 ,@body))
116 (declaim (inline window-x2 window-y2))
117 (defun window-x2 (window)
118 (+ (x-drawable-x window) (x-drawable-width window)))
120 (defun window-y2 (window)
121 (+ (x-drawable-y window) (x-drawable-height window)))
126 ;;; Events management functions.
128 (defparameter *unhandled-events* nil)
129 (defparameter *current-event-mode* nil)
131 (eval-when (:compile-toplevel :load-toplevel :execute)
132 (defun keyword->handle-event (mode keyword)
133 (create-symbol 'handle-event-fun "-" mode "-" keyword)))
135 (defun handle-event->keyword (symbol)
136 (let* ((name (string-downcase (symbol-name symbol)))
137 (pos (search "handle-event-fun-" name)))
138 (when (and pos (zerop pos))
139 (let ((pos-mod (search "mode" name)))
140 (when pos-mod
141 (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword))))))
142 ;; (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
143 ;; (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
145 (defparameter *handle-event-fun-symbols* nil)
147 (defun fill-handle-event-fun-symbols ()
148 (with-all-internal-symbols (symbol :clfswm)
149 (let ((pos (symbol-search "handle-event-fun-" symbol)))
150 (when (and pos (zerop pos))
151 (pushnew symbol *handle-event-fun-symbols*)))))
154 (defmacro with-handle-event-symbol ((mode) &body body)
155 "Bind symbol to all handle event functions available in mode"
156 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
157 (dolist (symbol *handle-event-fun-symbols*)
158 (let ((pos (symbol-search pattern symbol)))
159 (when (and pos (zerop pos))
160 ,@body)))))
163 (defun find-handle-event-function (&optional (mode ""))
164 "Print all handle event functions available in mode"
165 (with-handle-event-symbol (mode)
166 (print symbol)))
168 (defun assoc-keyword-handle-event (mode)
169 "Associate all keywords in mode to their corresponding handle event functions.
170 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
171 (setf *current-event-mode* mode)
172 (with-handle-event-symbol (mode)
173 (let ((keyword (handle-event->keyword symbol)))
174 (when (fboundp symbol)
175 #+:event-debug
176 (progn
177 (format t "~&Associating: ~S with ~S~%" symbol keyword)
178 (force-output))
179 (setf (symbol-function keyword) (symbol-function symbol))))))
181 (defun unassoc-keyword-handle-event (&optional (mode ""))
182 "Unbound all keywords from their corresponding handle event functions."
183 (setf *current-event-mode* nil)
184 (with-handle-event-symbol (mode)
185 (let ((keyword (handle-event->keyword symbol)))
186 (when (fboundp keyword)
187 #+:event-debug
188 (progn
189 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
190 (force-output))
191 (fmakunbound keyword)))))
193 (defmacro define-handler (mode keyword args &body body)
194 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
195 For example (define-handler main-mode :key-press (args) ...)
196 Expand in handle-event-fun-main-mode-key-press"
197 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
198 (declare (ignorable event-slots))
199 #+:event-debug (print (list *current-event-mode* event-key))
200 ,@body))
203 (defun event-hook-name (event-keyword)
204 (create-symbol-in-package :clfswm '*event- event-keyword '-hook*))
206 (let ((event-hook-list nil))
207 (defun get-event-hook-list ()
208 event-hook-list)
210 (defmacro use-event-hook (event-keyword)
211 (let ((symb (event-hook-name event-keyword)))
212 (pushnew symb event-hook-list)
213 `(defvar ,symb nil)))
215 (defun unuse-event-hook (event-keyword)
216 (let ((symb (event-hook-name event-keyword)))
217 (setf event-hook-list (remove symb event-hook-list))
218 (makunbound symb)))
220 (defmacro add-event-hook (name &rest value)
221 (let ((symb (event-hook-name name)))
222 `(add-hook ,symb ,@value)))
224 (defmacro remove-event-hook (name &rest value)
225 (let ((symb (event-hook-name name)))
226 `(remove-hook ,symb ,@value)))
228 (defun clear-event-hooks ()
229 (dolist (symb event-hook-list)
230 (makunbound symb)))
233 (defun optimize-event-hook ()
234 "Remove unused event hooks"
235 (dolist (symb event-hook-list)
236 (when (and (boundp symb)
237 (null (symbol-value symb)))
238 (makunbound symb)
239 (setf event-hook-list (remove symb event-hook-list))))))
243 (defmacro define-event-hook (event-keyword args &body body)
244 (let ((event-fun (gensym)))
245 `(let ((,event-fun (lambda (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
246 (declare (ignorable event-slots))
247 #+:event-debug (print (list ,event-keyword event-key))
248 ,@body)))
249 (add-event-hook ,event-keyword ,event-fun)
250 ,event-fun)))
253 (defmacro event-defun (name args &body body)
254 `(defun ,name (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
255 (declare (ignorable event-slots))
256 #+:event-debug (print (list ,event-keyword event-key))
257 ,@body))
259 (defun exit-handle-event ()
260 (throw 'exit-handle-event nil))
263 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
264 (labels ((make-xlib-window (xobject)
265 "For some reason the clx xid cache screws up returns pixmaps when
266 they should be windows. So use this function to make a window out of them."
267 ;; Workaround for pixmap error taken from STUMPWM - thanks:
268 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
269 ;; should be a window will be a pixmap instead. In this case, we
270 ;; need to manually translate it to a window to avoid breakage
271 ;; in stumpwm. So far the only slot that seems to be affected is
272 ;; the :window slot for configure-request and reparent-notify
273 ;; events. It appears as though the hash table of XIDs and clx
274 ;; structures gets out of sync with X or perhaps X assigns a
275 ;; duplicate ID for a pixmap and a window.
276 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
277 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
278 #-(or sbcl clisp ecl openmcl)
279 (error 'not-implemented)))
280 (handler-case
281 (catch 'exit-handle-event
282 (let ((win (getf event-slots :window)))
283 (when (and win (not (xlib:window-p win)))
284 (dbg "Pixmap Workaround! Should be a window: " win)
285 (setf (getf event-slots :window) (make-xlib-window win))))
286 (let ((hook-symbol (event-hook-name event-key)))
287 (when (boundp hook-symbol)
288 (apply #'call-hook (symbol-value hook-symbol) event-slots)))
289 (if (fboundp event-key)
290 (apply event-key event-slots)
291 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal))
292 (xlib:display-finish-output *display*))
293 ((or xlib:window-error xlib:drawable-error) (c)
294 #-xlib-debug (declare (ignore c))
295 #+xlib-debug (format t "Ignore Xlib synchronous error: ~a~%" c)))
302 (defun parse-display-string (display)
303 "Parse an X11 DISPLAY string and return the host and display from it."
304 (let* ((colon (position #\: display))
305 (host (subseq display 0 colon))
306 (rest (subseq display (1+ colon)))
307 (dot (position #\. rest))
308 (num (parse-integer (subseq rest 0 dot))))
309 (values host num)))
312 ;;; Transparency support
313 (let ((opaque #xFFFFFFFF))
314 (defun window-transparency (window)
315 "Return the window transparency"
316 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
318 (defun set-window-transparency (window value)
319 "Set the window transparency"
320 (when (numberp value)
321 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
322 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
323 opaque)
325 :cardinal 32)))
327 (defsetf window-transparency set-window-transparency))
331 (defun window-state (win)
332 "Get the state (iconic, normal, withdrawn) of a window."
333 (first (xlib:get-property win :WM_STATE)))
336 (defun set-window-state (win state)
337 "Set the state (iconic, normal, withdrawn) of a window."
338 (xlib:change-property win
339 :WM_STATE
340 (list state)
341 :WM_STATE
342 32))
344 (defsetf window-state set-window-state)
348 (defun window-hidden-p (window)
349 (eql (window-state window) +iconic-state+))
352 (defun null-size-window-p (window)
353 (let ((hints (xlib:wm-normal-hints window)))
354 (and hints
355 (not (or (xlib:wm-size-hints-width hints)
356 (xlib:wm-size-hints-height hints)
357 (xlib:wm-size-hints-win-gravity hints)))
358 (xlib:wm-size-hints-user-specified-position-p hints))))
365 (defun unhide-window (window)
366 (when window
367 (when (window-hidden-p window)
368 (xlib:map-window window)
369 (setf (window-state window) +normal-state+
370 (xlib:window-event-mask window) *window-events*))))
373 (defun map-window (window)
374 (when window
375 (xlib:map-window window)))
378 (defun delete-window (window)
379 (send-client-message window :WM_PROTOCOLS
380 (xlib:intern-atom *display* :WM_DELETE_WINDOW)))
382 (defun destroy-window (window)
383 (xlib:kill-client *display* (xlib:window-id window)))
386 ;;(defconstant +exwm-atoms+
387 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
388 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
389 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
390 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
391 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
392 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
393 ;; "_NET_DESKTOP_LAYOUT"
395 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
396 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
397 ;; "_NET_WM_MOVERESIZE"
399 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
401 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
402 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
403 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
404 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
405 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
406 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
407 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
408 ;; ;; "_NET_WM_MOVE_ACTIONS"
410 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
411 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
412 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
413 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
414 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
415 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
416 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
417 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
418 ;; "_NET_WM_STATE_FULLSCREEN"
419 ;; "_NET_WM_STATE_ABOVE"
420 ;; "_NET_WM_STATE_BELOW"
421 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
423 ;; "_NET_WM_ALLOWED_ACTIONS"
424 ;; "_NET_WM_ACTION_MOVE"
425 ;; "_NET_WM_ACTION_RESIZE"
426 ;; "_NET_WM_ACTION_SHADE"
427 ;; "_NET_WM_ACTION_STICK"
428 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
429 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
430 ;; "_NET_WM_ACTION_FULLSCREEN"
431 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
432 ;; "_NET_WM_ACTION_CLOSE"
434 ;; ))
437 ;;(defun intern-atoms (display)
438 ;; (declare (type xlib:display display))
439 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
440 ;; +exwm-atoms+)
441 ;; (values))
445 ;;(defun get-atoms-property (window property-atom atom-list-p)
446 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
447 ;; a list of atom-id."
448 ;; (xlib:get-property window property-atom
449 ;; :transform (when atom-list-p
450 ;; (lambda (id)
451 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
453 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
454 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
455 ;; or a list of keyword atom-names."
456 ;; (xlib:change-property window property-atom atoms :ATOM 32
457 ;; :mode mode
458 ;; :transform (unless (integerp (car atoms))
459 ;; (lambda (atom-key)
460 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
465 ;;(defun net-wm-state (window)
466 ;; (get-atoms-property window :_NET_WM_STATE t))
468 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
469 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
472 (defun hide-window (window)
473 (when window
474 (setf (window-state window) +iconic-state+
475 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
476 (xlib:unmap-window window)
477 (setf (xlib:window-event-mask window) *window-events*)))
481 (defun window-type (window)
482 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
483 :dialog, :transient, :maxsize and :normal."
484 (or (and (let ((hints (xlib:wm-normal-hints window)))
485 (and hints (or (xlib:wm-size-hints-max-width hints)
486 (xlib:wm-size-hints-max-height hints)
487 (xlib:wm-size-hints-min-aspect hints)
488 (xlib:wm-size-hints-max-aspect hints))))
489 :maxsize)
490 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
491 (when net-wm-window-type
492 (dolist (type-atom net-wm-window-type)
493 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
494 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
495 (and (xlib:get-property window :WM_TRANSIENT_FOR)
496 :transient)
497 :normal))
502 ;;; Stolen from Eclipse
503 (defun send-configuration-notify (window x y w h bw)
504 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
505 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
506 :event-window window
507 :window window
508 :x x :y y
509 :width w
510 :height h
511 :border-width bw
512 :propagate-p nil))
516 (defun send-client-message (window type &rest data)
517 "Send a client message to a client's window."
518 (xlib:send-event window
519 :client-message nil
520 :window window
521 :type type
522 :format 32
523 :data data))
529 (defun raise-window (window)
530 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
531 (when (xlib:window-p window)
532 (when (window-hidden-p window)
533 (unhide-window window))
534 (setf (xlib:window-priority window) :above)))
536 (defun focus-window (window)
537 "Give the window focus."
538 (when (xlib:window-p window)
539 (xlib:set-input-focus *display* window :parent)))
541 (defun raise-and-focus-window (window)
542 "Raise and focus."
543 (raise-window window)
544 (focus-window window))
546 (defun no-focus ()
547 "don't focus any window but still read keyboard events."
548 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
551 (defun lower-window (window sibling)
552 "Map the window if needed and bring it just above sibling. Does not affect focus."
553 (when (xlib:window-p window)
554 (when (window-hidden-p window)
555 (unhide-window window))
556 (setf (xlib:window-priority window sibling) :below)))
561 (let ((cursor-font nil)
562 (cursor nil)
563 (pointer-grabbed nil))
564 (defun free-grab-pointer ()
565 (when cursor
566 (xlib:free-cursor cursor)
567 (setf cursor nil))
568 (when cursor-font
569 (xlib:close-font cursor-font)
570 (setf cursor-font nil)))
572 (defun xgrab-init-pointer ()
573 (setf pointer-grabbed nil))
575 (defun xgrab-pointer-p ()
576 pointer-grabbed)
578 (defun xgrab-pointer (root cursor-char cursor-mask-char
579 &optional (pointer-mask '(:enter-window :pointer-motion
580 :button-press :button-release)) owner-p)
581 "Grab the pointer and set the pointer shape."
582 (when pointer-grabbed
583 (xungrab-pointer))
584 (setf pointer-grabbed t)
585 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
586 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
587 (cond (cursor-char
588 (setf cursor-font (xlib:open-font *display* "cursor")
589 cursor (xlib:create-glyph-cursor :source-font cursor-font
590 :source-char (or cursor-char 68)
591 :mask-font cursor-font
592 :mask-char (or cursor-mask-char 69)
593 :foreground black
594 :background white))
595 (xlib:grab-pointer root pointer-mask
596 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
598 (xlib:grab-pointer root pointer-mask
599 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
601 (defun xungrab-pointer ()
602 "Remove the grab on the cursor and restore the cursor shape."
603 (setf pointer-grabbed nil)
604 (xlib:ungrab-pointer *display*)
605 (xlib:display-finish-output *display*)
606 (free-grab-pointer)))
609 (let ((keyboard-grabbed nil))
610 (defun xgrab-init-keyboard ()
611 (setf keyboard-grabbed nil))
613 (defun xgrab-keyboard-p ()
614 keyboard-grabbed)
616 (defun xgrab-keyboard (root)
617 (setf keyboard-grabbed t)
618 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
621 (defun xungrab-keyboard ()
622 (setf keyboard-grabbed nil)
623 (xlib:ungrab-keyboard *display*)))
630 (defun ungrab-all-buttons (window)
631 (xlib:ungrab-button window :any :modifiers :any))
633 (defun grab-all-buttons (window)
634 (ungrab-all-buttons window)
635 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
636 :modifiers :any
637 :owner-p nil
638 :sync-pointer-p t
639 :sync-keyboard-p nil))
641 (defun ungrab-all-keys (window)
642 (xlib:ungrab-key window :any :modifiers :any))
646 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask &optional ungrab-main) &body body)
647 `(let ((pointer-grabbed (xgrab-pointer-p))
648 (keyboard-grabbed (xgrab-keyboard-p)))
649 (xgrab-pointer *root* ,cursor ,mask)
650 (unless keyboard-grabbed
651 (when ,ungrab-main
652 (ungrab-main-keys))
653 (xgrab-keyboard *root*))
654 (unwind-protect
655 (progn
656 ,@body)
657 (progn
658 (if pointer-grabbed
659 (xgrab-pointer *root* ,old-cursor ,old-mask)
660 (xungrab-pointer))
661 (unless keyboard-grabbed
662 (when ,ungrab-main
663 (grab-main-keys))
664 (xungrab-keyboard))))))
667 (defmacro with-grab-pointer ((&optional cursor-char cursor-mask-char) &body body)
668 `(let ((pointer-grabbed-p (xgrab-pointer-p)))
669 (unless pointer-grabbed-p
670 (xgrab-pointer *root* ,cursor-char ,cursor-mask-char))
671 (unwind-protect
672 (progn
673 ,@body)
674 (unless pointer-grabbed-p
675 (xungrab-pointer)))))
681 (defun stop-button-event ()
682 (xlib:allow-events *display* :sync-pointer))
684 (defun replay-button-event ()
685 (xlib:allow-events *display* :replay-pointer))
695 ;;; Mouse action on window
696 (let (add-fn add-arg dx dy window)
697 (define-handler move-window-mode :motion-notify (root-x root-y)
698 (unless (compress-motion-notify)
699 (if add-fn
700 (multiple-value-bind (move-x move-y)
701 (apply add-fn add-arg)
702 (when move-x
703 (setf (x-drawable-x window) (+ root-x dx)))
704 (when move-y
705 (setf (x-drawable-y window) (+ root-y dy))))
706 (setf (x-drawable-x window) (+ root-x dx)
707 (x-drawable-y window) (+ root-y dy)))))
709 (define-handler move-window-mode :key-release ()
710 (throw 'exit-move-window-mode nil))
712 (define-handler move-window-mode :button-release ()
713 (throw 'exit-move-window-mode nil))
715 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
716 (setf window orig-window
717 add-fn additional-fn
718 add-arg additional-arg
719 dx (- (x-drawable-x window) orig-x)
720 dy (- (x-drawable-y window) orig-y)
721 (xlib:window-border window) (get-color *color-move-window*))
722 (raise-window window)
723 (with-grab-pointer ()
724 (when additional-fn
725 (apply additional-fn additional-arg))
726 (generic-mode 'move-window-mode 'exit-move-window-mode
727 :original-mode '(main-mode)))))
730 (let (add-fn add-arg window
731 o-x o-y
732 orig-width orig-height
733 min-width max-width
734 min-height max-height)
735 (define-handler resize-window-mode :motion-notify (root-x root-y)
736 (unless (compress-motion-notify)
737 (if add-fn
738 (multiple-value-bind (resize-w resize-h)
739 (apply add-fn add-arg)
740 (when resize-w
741 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
742 (when resize-h
743 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
744 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
745 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
747 (define-handler resize-window-mode :key-release ()
748 (throw 'exit-resize-window-mode nil))
750 (define-handler resize-window-mode :button-release ()
751 (throw 'exit-resize-window-mode nil))
753 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
754 (let* ((hints (xlib:wm-normal-hints orig-window)))
755 (setf window orig-window
756 add-fn additional-fn
757 add-arg additional-arg
758 o-x orig-x
759 o-y orig-y
760 orig-width (x-drawable-width window)
761 orig-height (x-drawable-height window)
762 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
763 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
764 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
765 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
766 (xlib:window-border window) (get-color *color-move-window*))
767 (raise-window window)
768 (with-grab-pointer ()
769 (when additional-fn
770 (apply additional-fn additional-arg))
771 (generic-mode 'resize-window-mode 'exit-resize-window-mode
772 :original-mode '(main-mode))))))
776 (define-handler wait-mouse-button-release-mode :button-release ()
777 (throw 'exit-wait-mouse-button-release-mode nil))
779 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
780 (with-grab-pointer (cursor-char cursor-mask-char)
781 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)))
787 (let ((color-hash (make-hash-table :test 'equal)))
788 (defun get-color (color)
789 (multiple-value-bind (val foundp)
790 (gethash color color-hash)
791 (if foundp
793 (setf (gethash color color-hash)
794 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
798 (defgeneric ->color (color))
800 (defmethod ->color ((color-name string))
801 color-name)
803 (defmethod ->color ((color integer))
804 (labels ((hex->float (color)
805 (/ (logand color #xFF) 256.0)))
806 (xlib:make-color :blue (hex->float color)
807 :green (hex->float (ash color -8))
808 :red (hex->float (ash color -16)))))
810 (defmethod ->color ((color list))
811 (destructuring-bind (red green blue) color
812 (xlib:make-color :blue red :green green :red blue)))
814 (defmethod ->color ((color xlib:color))
815 color)
817 (defmethod ->color (color)
818 (format t "Wrong color type: ~A~%" color)
819 "White")
822 (defun color->rgb (color)
823 (multiple-value-bind (r g b)
824 (xlib:color-rgb color)
825 (+ (ash (round (* 256 r)) +16)
826 (ash (round (* 256 g)) +8)
827 (round (* 256 b)))))
833 (defmacro my-character->keysyms (ch)
834 "Convert a char to a keysym"
835 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
836 ;; some day. Or just copied from MIT-CLX or some other CLX
837 ;; implementation (see translate.lisp and keysyms.lisp). For now,
838 ;; we do like this. It suffices for modifiers and ASCII symbols.
839 (if (fboundp 'xlib:character->keysyms)
840 `(xlib:character->keysyms ,ch)
841 `(list
842 (case ,ch
843 (:character-set-switch #xFF7E)
844 (:left-shift #xFFE1)
845 (:right-shift #xFFE2)
846 (:left-control #xFFE3)
847 (:right-control #xFFE4)
848 (:caps-lock #xFFE5)
849 (:shift-lock #xFFE6)
850 (:left-meta #xFFE7)
851 (:right-meta #xFFE8)
852 (:left-alt #xFFE9)
853 (:right-alt #xFFEA)
854 (:left-super #xFFEB)
855 (:right-super #xFFEC)
856 (:left-hyper #xFFED)
857 (:right-hyper #xFFEE)
859 (etypecase ,ch
860 (character
861 ;; Latin-1 characters have their own value as keysym
862 (if (< 31 (char-code ,ch) 256)
863 (char-code ,ch)
864 (error "Don't know how to get keysym from ~A" ,ch)))))))))
869 (defun char->keycode (char)
870 "Convert a character to a keycode"
871 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
874 (defun keycode->char (code state)
875 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
877 (defun modifiers->state (modifier-list)
878 (apply #'xlib:make-state-mask modifier-list))
880 (defun state->modifiers (state)
881 (xlib:make-state-keys state))
883 (defun keycode->keysym (code modifiers)
884 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
885 ((member :mod-5 modifiers) 4)
886 (t 0))))
892 (let ((modifier-list nil))
893 (defun init-modifier-list ()
894 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
895 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
896 "Mode_switch" "script_switch" "ISO_Level3_Shift"
897 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
898 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
899 (push it modifier-list))))
901 (defun modifier-p (code)
902 (member code modifier-list)))
904 (defun wait-no-key-or-button-press ()
905 (with-grab-keyboard-and-pointer (66 67 66 67)
906 (loop
907 (let ((key (loop for k across (xlib:query-keymap *display*)
908 for code from 0
909 when (and (plusp k) (not (modifier-p code)))
910 return t))
911 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
912 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
913 return t)))
914 (when (and (not key) (not button))
915 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
916 (:motion-notify () t)
917 (:key-press () t)
918 (:key-release () t)
919 (:button-press () t)
920 (:button-release () t)
921 (t nil)))
922 (return))))))
925 (defun wait-a-key-or-button-press ()
926 (with-grab-keyboard-and-pointer (24 25 66 67)
927 (loop
928 (let ((key (loop for k across (xlib:query-keymap *display*)
929 unless (zerop k) return t))
930 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
931 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
932 return t)))
933 (when (or key button)
934 (return))))))
938 (defun compress-motion-notify ()
939 (when *have-to-compress-notify*
940 (loop while (xlib:event-cond (*display* :timeout 0)
941 (:motion-notify () t)))))
944 (defun display-all-cursors (&optional (display-time 1))
945 "Display all X11 cursors for display-time seconds"
946 (loop for i from 0 to 152 by 2
947 do (xgrab-pointer *root* i (1+ i))
948 (dbg i)
949 (sleep display-time)
950 (xungrab-pointer)))
954 ;;; Double buffering tools
955 (defun clear-pixmap-buffer (window gc)
956 (if (equal *transparent-background* :pseudo)
957 (xlib:copy-area *background-image* *background-gc*
958 (x-drawable-x window) (x-drawable-y window)
959 (x-drawable-width window) (x-drawable-height window)
960 *pixmap-buffer* 0 0)
961 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
962 :background (xlib:gcontext-foreground gc))
963 (xlib:draw-rectangle *pixmap-buffer* gc
964 0 0 (x-drawable-width window) (x-drawable-height window)
965 t))))
968 (defun copy-pixmap-buffer (window gc)
969 (xlib:copy-area *pixmap-buffer* gc
970 0 0 (x-drawable-width window) (x-drawable-height window)
971 window 0 0))
975 (defun is-a-key-pressed-p ()
976 (loop for k across (xlib:query-keymap *display*)
977 when (plusp k)
978 return t))
980 ;;; Windows wm class and name tests
981 (defmacro defun-equal-wm-class (symbol class)
982 `(defun ,symbol (window)
983 (when (xlib:window-p window)
984 (string-equal (xlib:get-wm-class window) ,class))))
986 (defmacro defun-equal-wm-name (symbol name)
987 `(defun ,symbol (window)
988 (when (xlib:window-p window)
989 (string-equal (xlib:wm-name window) ,name))))