Move mouse pointer only when needed on :sloppy-select-window focus policy
[clfswm.git] / src / xlib-util.lisp
blob8748db3904924f70857e028591dc558cd06e4326
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 maxmin-size-equal-p (window)
332 (when (xlib:window-p window)
333 (let ((hints (xlib:wm-normal-hints window)))
334 (when hints
335 (let ((hint-x (xlib:wm-size-hints-x hints))
336 (hint-y (xlib:wm-size-hints-y hints))
337 (user-specified-position-p (xlib:wm-size-hints-user-specified-position-p hints))
338 (min-width (xlib:wm-size-hints-min-width hints))
339 (min-height (xlib:wm-size-hints-min-height hints))
340 (max-width (xlib:wm-size-hints-max-width hints))
341 (max-height (xlib:wm-size-hints-max-height hints)))
342 (and hint-x hint-y min-width max-width min-height max-height
343 user-specified-position-p
344 (= hint-x 0) (= hint-y 0)
345 (= min-width max-width)
346 (= min-height max-height)))))))
348 (defun maxmin-size-equal-window-in-tree ()
349 (dolist (win (xlib:query-tree *root*))
350 (when (maxmin-size-equal-p win)
351 (return win))))
356 (defun window-state (win)
357 "Get the state (iconic, normal, withdrawn) of a window."
358 (first (xlib:get-property win :WM_STATE)))
361 (defun set-window-state (win state)
362 "Set the state (iconic, normal, withdrawn) of a window."
363 (xlib:change-property win
364 :WM_STATE
365 (list state)
366 :WM_STATE
367 32))
369 (defsetf window-state set-window-state)
373 (defun window-hidden-p (window)
374 (eql (window-state window) +iconic-state+))
377 (defun window-transient-for (window)
378 (first (xlib:get-property window :WM_TRANSIENT_FOR)))
380 (defun window-leader (window)
381 (when (xlib:window-p window)
382 (or (first (xlib:get-property window :WM_CLIENT_LEADER))
383 (let ((id (window-transient-for window)))
384 (when id
385 (window-leader id))))))
389 (defun unhide-window (window)
390 (when window
391 (when (window-hidden-p window)
392 (xlib:map-window window)
393 (setf (window-state window) +normal-state+
394 (xlib:window-event-mask window) *window-events*))))
397 (defun map-window (window)
398 (when window
399 (xlib:map-window window)))
402 (defun delete-window (window)
403 (send-client-message window :WM_PROTOCOLS
404 (xlib:intern-atom *display* :WM_DELETE_WINDOW)))
406 (defun destroy-window (window)
407 (xlib:kill-client *display* (xlib:window-id window)))
410 ;;(defconstant +exwm-atoms+
411 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
412 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
413 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
414 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
415 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
416 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
417 ;; "_NET_DESKTOP_LAYOUT"
419 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
420 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
421 ;; "_NET_WM_MOVERESIZE"
423 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
425 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
426 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
427 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
428 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
429 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
430 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
431 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
432 ;; ;; "_NET_WM_MOVE_ACTIONS"
434 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
435 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
436 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
437 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
438 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
439 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
440 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
441 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
442 ;; "_NET_WM_STATE_FULLSCREEN"
443 ;; "_NET_WM_STATE_ABOVE"
444 ;; "_NET_WM_STATE_BELOW"
445 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
447 ;; "_NET_WM_ALLOWED_ACTIONS"
448 ;; "_NET_WM_ACTION_MOVE"
449 ;; "_NET_WM_ACTION_RESIZE"
450 ;; "_NET_WM_ACTION_SHADE"
451 ;; "_NET_WM_ACTION_STICK"
452 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
453 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
454 ;; "_NET_WM_ACTION_FULLSCREEN"
455 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
456 ;; "_NET_WM_ACTION_CLOSE"
458 ;; ))
461 ;;(defun intern-atoms (display)
462 ;; (declare (type xlib:display display))
463 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
464 ;; +exwm-atoms+)
465 ;; (values))
469 ;;(defun get-atoms-property (window property-atom atom-list-p)
470 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
471 ;; a list of atom-id."
472 ;; (xlib:get-property window property-atom
473 ;; :transform (when atom-list-p
474 ;; (lambda (id)
475 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
477 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
478 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
479 ;; or a list of keyword atom-names."
480 ;; (xlib:change-property window property-atom atoms :ATOM 32
481 ;; :mode mode
482 ;; :transform (unless (integerp (car atoms))
483 ;; (lambda (atom-key)
484 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
489 ;;(defun net-wm-state (window)
490 ;; (get-atoms-property window :_NET_WM_STATE t))
492 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
493 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
496 (defun hide-window (window)
497 (when window
498 (setf (window-state window) +iconic-state+
499 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
500 (xlib:unmap-window window)
501 (setf (xlib:window-event-mask window) *window-events*)))
505 (defun window-type (window)
506 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
507 :dialog, :transient, :maxsize and :normal."
508 (when (xlib:window-p window)
509 (or (and (let ((hints (xlib:wm-normal-hints window)))
510 (and hints (or (and (xlib:wm-size-hints-max-width hints)
511 (< (xlib:wm-size-hints-max-width hints) (x-drawable-width *root*)))
512 (and (xlib:wm-size-hints-max-height hints)
513 (< (xlib:wm-size-hints-max-height hints) (x-drawable-height *root*)))
514 (xlib:wm-size-hints-min-aspect hints)
515 (xlib:wm-size-hints-max-aspect hints))))
516 :maxsize)
517 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
518 (when net-wm-window-type
519 (dolist (type-atom net-wm-window-type)
520 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
521 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
522 (and (xlib:get-property window :WM_TRANSIENT_FOR)
523 :transient)
524 :normal)))
529 ;;; Stolen from Eclipse
530 (defun send-configuration-notify (window x y w h bw)
531 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
532 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
533 :event-window window
534 :window window
535 :x x :y y
536 :width w
537 :height h
538 :border-width bw
539 :propagate-p nil))
543 (defun send-client-message (window type &rest data)
544 "Send a client message to a client's window."
545 (xlib:send-event window
546 :client-message nil
547 :window window
548 :type type
549 :format 32
550 :data data))
556 (defun raise-window (window)
557 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
558 (when (xlib:window-p window)
559 (when (window-hidden-p window)
560 (unhide-window window))
561 (setf (xlib:window-priority window) :above)))
564 (defun no-focus ()
565 "don't focus any window but still read keyboard events."
566 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
568 (defun focus-window (window)
569 "Give the window focus."
570 (no-focus)
571 (when (xlib:window-p window)
572 (xlib:set-input-focus *display* window :parent)))
574 (defun raise-and-focus-window (window)
575 "Raise and focus."
576 (raise-window window)
577 (focus-window window))
580 (defun lower-window (window sibling)
581 "Map the window if needed and bring it just above sibling. Does not affect focus."
582 (when (xlib:window-p window)
583 (when (window-hidden-p window)
584 (unhide-window window))
585 (setf (xlib:window-priority window sibling) :below)))
590 (let ((cursor-font nil)
591 (cursor nil)
592 (pointer-grabbed nil))
593 (defun free-grab-pointer ()
594 (when cursor
595 (xlib:free-cursor cursor)
596 (setf cursor nil))
597 (when cursor-font
598 (xlib:close-font cursor-font)
599 (setf cursor-font nil)))
601 (defun xgrab-init-pointer ()
602 (setf pointer-grabbed nil))
604 (defun xgrab-pointer-p ()
605 pointer-grabbed)
607 (defun xgrab-pointer (root cursor-char cursor-mask-char
608 &optional (pointer-mask '(:enter-window :pointer-motion
609 :button-press :button-release)) owner-p)
610 "Grab the pointer and set the pointer shape."
611 (when pointer-grabbed
612 (xungrab-pointer))
613 (setf pointer-grabbed t)
614 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
615 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
616 (cond (cursor-char
617 (setf cursor-font (xlib:open-font *display* "cursor")
618 cursor (xlib:create-glyph-cursor :source-font cursor-font
619 :source-char (or cursor-char 68)
620 :mask-font cursor-font
621 :mask-char (or cursor-mask-char 69)
622 :foreground black
623 :background white))
624 (xlib:grab-pointer root pointer-mask
625 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
627 (xlib:grab-pointer root pointer-mask
628 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
630 (defun xungrab-pointer ()
631 "Remove the grab on the cursor and restore the cursor shape."
632 (setf pointer-grabbed nil)
633 (xlib:ungrab-pointer *display*)
634 (xlib:display-finish-output *display*)
635 (free-grab-pointer)))
638 (let ((keyboard-grabbed nil))
639 (defun xgrab-init-keyboard ()
640 (setf keyboard-grabbed nil))
642 (defun xgrab-keyboard-p ()
643 keyboard-grabbed)
645 (defun xgrab-keyboard (root)
646 (setf keyboard-grabbed t)
647 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
650 (defun xungrab-keyboard ()
651 (setf keyboard-grabbed nil)
652 (xlib:ungrab-keyboard *display*)))
659 (defun ungrab-all-buttons (window)
660 (xlib:ungrab-button window :any :modifiers :any))
662 (defun grab-all-buttons (window)
663 (ungrab-all-buttons window)
664 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
665 :modifiers :any
666 :owner-p nil
667 :sync-pointer-p t
668 :sync-keyboard-p nil))
670 (defun ungrab-all-keys (window)
671 (xlib:ungrab-key window :any :modifiers :any))
675 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask &optional ungrab-main) &body body)
676 `(let ((pointer-grabbed (xgrab-pointer-p))
677 (keyboard-grabbed (xgrab-keyboard-p)))
678 (xgrab-pointer *root* ,cursor ,mask)
679 (unless keyboard-grabbed
680 (when ,ungrab-main
681 (ungrab-main-keys))
682 (xgrab-keyboard *root*))
683 (unwind-protect
684 (progn
685 ,@body)
686 (progn
687 (if pointer-grabbed
688 (xgrab-pointer *root* ,old-cursor ,old-mask)
689 (xungrab-pointer))
690 (unless keyboard-grabbed
691 (when ,ungrab-main
692 (grab-main-keys))
693 (xungrab-keyboard))))))
696 (defmacro with-grab-pointer ((&optional cursor-char cursor-mask-char) &body body)
697 `(let ((pointer-grabbed-p (xgrab-pointer-p)))
698 (unless pointer-grabbed-p
699 (xgrab-pointer *root* ,cursor-char ,cursor-mask-char))
700 (unwind-protect
701 (progn
702 ,@body)
703 (unless pointer-grabbed-p
704 (xungrab-pointer)))))
710 (defun stop-button-event ()
711 (xlib:allow-events *display* :sync-pointer))
713 (defun replay-button-event ()
714 (xlib:allow-events *display* :replay-pointer))
724 ;;; Mouse action on window
725 (let (add-fn add-arg dx dy window)
726 (define-handler move-window-mode :motion-notify (root-x root-y)
727 (unless (compress-motion-notify)
728 (if add-fn
729 (multiple-value-bind (move-x move-y)
730 (apply add-fn add-arg)
731 (when move-x
732 (setf (x-drawable-x window) (+ root-x dx)))
733 (when move-y
734 (setf (x-drawable-y window) (+ root-y dy))))
735 (setf (x-drawable-x window) (+ root-x dx)
736 (x-drawable-y window) (+ root-y dy)))))
738 (define-handler move-window-mode :key-release ()
739 (throw 'exit-move-window-mode nil))
741 (define-handler move-window-mode :button-release ()
742 (throw 'exit-move-window-mode nil))
744 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
745 (setf window orig-window
746 add-fn additional-fn
747 add-arg additional-arg
748 dx (- (x-drawable-x window) orig-x)
749 dy (- (x-drawable-y window) orig-y)
750 (xlib:window-border window) (get-color *color-move-window*))
751 (raise-window window)
752 (with-grab-pointer ()
753 (when additional-fn
754 (apply additional-fn additional-arg))
755 (generic-mode 'move-window-mode 'exit-move-window-mode
756 :original-mode '(main-mode)))))
759 (let (add-fn add-arg window
760 o-x o-y
761 orig-width orig-height
762 min-width max-width
763 min-height max-height)
764 (define-handler resize-window-mode :motion-notify (root-x root-y)
765 (unless (compress-motion-notify)
766 (if add-fn
767 (multiple-value-bind (resize-w resize-h)
768 (apply add-fn add-arg)
769 (when resize-w
770 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
771 (when resize-h
772 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
773 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
774 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
776 (define-handler resize-window-mode :key-release ()
777 (throw 'exit-resize-window-mode nil))
779 (define-handler resize-window-mode :button-release ()
780 (throw 'exit-resize-window-mode nil))
782 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
783 (let* ((hints (xlib:wm-normal-hints orig-window)))
784 (setf window orig-window
785 add-fn additional-fn
786 add-arg additional-arg
787 o-x orig-x
788 o-y orig-y
789 orig-width (x-drawable-width window)
790 orig-height (x-drawable-height window)
791 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
792 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
793 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
794 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
795 (xlib:window-border window) (get-color *color-move-window*))
796 (raise-window window)
797 (with-grab-pointer ()
798 (when additional-fn
799 (apply additional-fn additional-arg))
800 (generic-mode 'resize-window-mode 'exit-resize-window-mode
801 :original-mode '(main-mode))))))
805 (define-handler wait-mouse-button-release-mode :button-release ()
806 (throw 'exit-wait-mouse-button-release-mode nil))
808 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
809 (with-grab-pointer (cursor-char cursor-mask-char)
810 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)))
816 (let ((color-hash (make-hash-table :test 'equal)))
817 (defun get-color (color)
818 (multiple-value-bind (val foundp)
819 (gethash color color-hash)
820 (if foundp
822 (setf (gethash color color-hash)
823 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
827 (defgeneric ->color (color))
829 (defmethod ->color ((color-name string))
830 color-name)
832 (defmethod ->color ((color integer))
833 (labels ((hex->float (color)
834 (/ (logand color #xFF) 256.0)))
835 (xlib:make-color :blue (hex->float color)
836 :green (hex->float (ash color -8))
837 :red (hex->float (ash color -16)))))
839 (defmethod ->color ((color list))
840 (destructuring-bind (red green blue) color
841 (xlib:make-color :blue red :green green :red blue)))
843 (defmethod ->color ((color xlib:color))
844 color)
846 (defmethod ->color (color)
847 (format t "Wrong color type: ~A~%" color)
848 "White")
851 (defun color->rgb (color)
852 (multiple-value-bind (r g b)
853 (xlib:color-rgb color)
854 (+ (ash (round (* 256 r)) +16)
855 (ash (round (* 256 g)) +8)
856 (round (* 256 b)))))
862 (defmacro my-character->keysyms (ch)
863 "Convert a char to a keysym"
864 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
865 ;; some day. Or just copied from MIT-CLX or some other CLX
866 ;; implementation (see translate.lisp and keysyms.lisp). For now,
867 ;; we do like this. It suffices for modifiers and ASCII symbols.
868 (if (fboundp 'xlib:character->keysyms)
869 `(xlib:character->keysyms ,ch)
870 `(list
871 (case ,ch
872 (:character-set-switch #xFF7E)
873 (:left-shift #xFFE1)
874 (:right-shift #xFFE2)
875 (:left-control #xFFE3)
876 (:right-control #xFFE4)
877 (:caps-lock #xFFE5)
878 (:shift-lock #xFFE6)
879 (:left-meta #xFFE7)
880 (:right-meta #xFFE8)
881 (:left-alt #xFFE9)
882 (:right-alt #xFFEA)
883 (:left-super #xFFEB)
884 (:right-super #xFFEC)
885 (:left-hyper #xFFED)
886 (:right-hyper #xFFEE)
888 (etypecase ,ch
889 (character
890 ;; Latin-1 characters have their own value as keysym
891 (if (< 31 (char-code ,ch) 256)
892 (char-code ,ch)
893 (error "Don't know how to get keysym from ~A" ,ch)))))))))
898 (defun char->keycode (char)
899 "Convert a character to a keycode"
900 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
903 (defun keycode->char (code state)
904 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
906 (defun modifiers->state (modifier-list)
907 (apply #'xlib:make-state-mask modifier-list))
909 (defun state->modifiers (state)
910 (xlib:make-state-keys state))
912 (defun keycode->keysym (code modifiers)
913 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
914 ((member :mod-5 modifiers) 4)
915 (t 0))))
921 (let ((modifier-list nil))
922 (defun init-modifier-list ()
923 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
924 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
925 "Mode_switch" "script_switch" "ISO_Level3_Shift"
926 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
927 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
928 (push it modifier-list))))
930 (defun modifier-p (code)
931 (member code modifier-list)))
933 (defun wait-no-key-or-button-press ()
934 (with-grab-keyboard-and-pointer (66 67 66 67)
935 (loop
936 (let ((key (loop for k across (xlib:query-keymap *display*)
937 for code from 0
938 when (and (plusp k) (not (modifier-p code)))
939 return t))
940 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
941 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
942 return t)))
943 (when (and (not key) (not button))
944 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
945 (:motion-notify () t)
946 (:key-press () t)
947 (:key-release () t)
948 (:button-press () t)
949 (:button-release () t)
950 (t nil)))
951 (return))))))
954 (defun wait-a-key-or-button-press ()
955 (with-grab-keyboard-and-pointer (24 25 66 67)
956 (loop
957 (let ((key (loop for k across (xlib:query-keymap *display*)
958 unless (zerop k) return t))
959 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
960 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
961 return t)))
962 (when (or key button)
963 (return))))))
967 (defun compress-motion-notify ()
968 (when *have-to-compress-notify*
969 (loop while (xlib:event-cond (*display* :timeout 0)
970 (:motion-notify () t)))))
973 (defun display-all-cursors (&optional (display-time 1))
974 "Display all X11 cursors for display-time seconds"
975 (loop for i from 0 to 152 by 2
976 do (xgrab-pointer *root* i (1+ i))
977 (dbg i)
978 (sleep display-time)
979 (xungrab-pointer)))
983 ;;; Double buffering tools
984 (defun clear-pixmap-buffer (window gc)
985 (if (equal *transparent-background* :pseudo)
986 (xlib:copy-area *background-image* *background-gc*
987 (x-drawable-x window) (x-drawable-y window)
988 (x-drawable-width window) (x-drawable-height window)
989 *pixmap-buffer* 0 0)
990 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
991 :background (xlib:gcontext-foreground gc))
992 (xlib:draw-rectangle *pixmap-buffer* gc
993 0 0 (x-drawable-width window) (x-drawable-height window)
994 t))))
997 (defun copy-pixmap-buffer (window gc)
998 (xlib:copy-area *pixmap-buffer* gc
999 0 0 (x-drawable-width window) (x-drawable-height window)
1000 window 0 0))
1004 (defun is-a-key-pressed-p ()
1005 (loop for k across (xlib:query-keymap *display*)
1006 when (plusp k)
1007 return t))
1009 ;;; Windows wm class and name tests
1010 (defmacro defun-equal-wm-class (symbol class)
1011 `(defun ,symbol (window)
1012 (when (xlib:window-p window)
1013 (string-equal (xlib:get-wm-class window) ,class))))
1015 (defmacro defun-equal-wm-name (symbol name)
1016 `(defun ,symbol (window)
1017 (when (xlib:window-p window)
1018 (string-equal (xlib:wm-name window) ,name))))