contrib/toolbar.lisp (define-toolbar-hooks): Add auto-hide clickable toolbar.
[clfswm.git] / src / xlib-util.lisp
blobb77ad4c90a5519a6fed4d8fda50c01cc8c15e13e
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility 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)
28 ;; Window states
29 (defconstant +withdrawn-state+ 0)
30 (defconstant +normal-state+ 1)
31 (defconstant +iconic-state+ 3)
34 (defparameter *window-events* '(:structure-notify
35 :property-change
36 :colormap-change
37 :focus-change
38 :enter-window
39 :exposure)
40 "The events to listen for on managed windows.")
43 (defparameter +netwm-supported+
44 '(:_NET_SUPPORTING_WM_CHECK
45 :_NET_NUMBER_OF_DESKTOPS
46 :_NET_DESKTOP_GEOMETRY
47 :_NET_DESKTOP_VIEWPORT
48 :_NET_CURRENT_DESKTOP
49 :_NET_WM_WINDOW_TYPE
50 :_NET_CLIENT_LIST)
51 "Supported NETWM properties.
52 Window types are in +WINDOW-TYPES+.")
54 (defparameter +netwm-window-types+
55 '((:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
56 (:_NET_WM_WINDOW_TYPE_DOCK . :dock)
57 (:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
58 (:_NET_WM_WINDOW_TYPE_MENU . :menu)
59 (:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
60 (:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
61 (:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
62 (:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
63 "Alist mapping NETWM window types to keywords.")
66 (defmacro with-xlib-protect (() &body body)
67 "Prevent Xlib errors"
68 `(handler-case
69 (with-simple-restart (top-level "Return to clfswm's top level")
70 ,@body)
71 ((or xlib:match-error xlib:window-error xlib:drawable-error xlib:lookup-error) (c)
72 (progn
73 (format t "Ignoring XLib error: ~S~%" c)
74 (unassoc-keyword-handle-event)
75 (assoc-keyword-handle-event 'main-mode)
76 (setf *in-second-mode* nil)))))
79 (defmacro with-x-pointer (&body body)
80 "Bind (x y) to mouse pointer positions"
81 `(multiple-value-bind (x y)
82 (xlib:query-pointer *root*)
83 ,@body))
87 (declaim (inline window-x2 window-y2))
88 (defun window-x2 (window)
89 (+ (x-drawable-x window) (x-drawable-width window)))
91 (defun window-y2 (window)
92 (+ (x-drawable-y window) (x-drawable-height window)))
96 ;;;
97 ;;; Events management functions.
98 ;;;
99 (defparameter *unhandled-events* nil)
100 (defparameter *current-event-mode* nil)
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defun keyword->handle-event (mode keyword)
104 (create-symbol 'handle-event-fun "-" mode "-" keyword)))
106 (defun handle-event->keyword (symbol)
107 (let* ((name (string-downcase (symbol-name symbol)))
108 (pos (search "handle-event-fun-" name)))
109 (when (and pos (zerop pos))
110 (let ((pos-mod (search "mode" name)))
111 (when pos-mod
112 (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
113 (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
115 (defparameter *handle-event-fun-symbols* nil)
117 (defun fill-handle-event-fun-symbols ()
118 (with-all-internal-symbols (symbol :clfswm)
119 (let ((pos (symbol-search "handle-event-fun-" symbol)))
120 (when (and pos (zerop pos))
121 (pushnew symbol *handle-event-fun-symbols*)))))
124 (defmacro with-handle-event-symbol ((mode) &body body)
125 "Bind symbol to all handle event functions available in mode"
126 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
127 (dolist (symbol *handle-event-fun-symbols*)
128 (let ((pos (symbol-search pattern symbol)))
129 (when (and pos (zerop pos))
130 ,@body)))))
133 (defun find-handle-event-function (&optional (mode ""))
134 "Print all handle event functions available in mode"
135 (with-handle-event-symbol (mode)
136 (print symbol)))
138 (defun assoc-keyword-handle-event (mode)
139 "Associate all keywords in mode to their corresponding handle event functions.
140 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
141 (setf *current-event-mode* mode)
142 (with-handle-event-symbol (mode)
143 (let ((keyword (handle-event->keyword symbol)))
144 (when (fboundp symbol)
145 #+:event-debug
146 (progn
147 (format t "~&Associating: ~S with ~S~%" symbol keyword)
148 (force-output))
149 (setf (symbol-function keyword) (symbol-function symbol))))))
151 (defun unassoc-keyword-handle-event (&optional (mode ""))
152 "Unbound all keywords from their corresponding handle event functions."
153 (setf *current-event-mode* nil)
154 (with-handle-event-symbol (mode)
155 (let ((keyword (handle-event->keyword symbol)))
156 (when (fboundp keyword)
157 #+:event-debug
158 (progn
159 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
160 (force-output))
161 (fmakunbound keyword)))))
163 (defmacro define-handler (mode keyword args &body body)
164 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
165 For example (define-handler main-mode :key-press (args) ...)
166 Expand in handle-event-fun-main-mode-key-press"
167 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
168 (declare (ignorable event-slots))
169 #+:event-debug (print (list *current-event-mode* event-key))
170 ,@body))
173 (defun event-hook-name (event-keyword)
174 (create-symbol '*event- event-keyword '-hook*))
176 (let ((event-hook-list nil))
177 (defmacro use-event-hook (event-keyword)
178 (let ((symb (event-hook-name event-keyword)))
179 (pushnew symb event-hook-list)
180 `(defvar ,symb nil)))
182 (defmacro add-event-hook (name &rest value)
183 (let ((symb (event-hook-name name)))
184 `(add-hook ,symb ,@value)))
186 (defun clear-event-hooks ()
187 (dolist (symb event-hook-list)
188 (makunbound symb))))
191 (defmacro define-event-hook (event-keyword args &body body)
192 `(add-event-hook ,event-keyword
193 (lambda (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
194 (declare (ignorable event-slots))
195 #+:event-debug (print (list ,event-keyword event-key))
196 ,@body)))
199 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
200 (labels ((make-xlib-window (xobject)
201 "For some reason the clx xid cache screws up returns pixmaps when
202 they should be windows. So use this function to make a window out of them."
203 ;; Workaround for pixmap error taken from STUMPWM - thanks:
204 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
205 ;; should be a window will be a pixmap instead. In this case, we
206 ;; need to manually translate it to a window to avoid breakage
207 ;; in stumpwm. So far the only slot that seems to be affected is
208 ;; the :window slot for configure-request and reparent-notify
209 ;; events. It appears as though the hash table of XIDs and clx
210 ;; structures gets out of sync with X or perhaps X assigns a
211 ;; duplicate ID for a pixmap and a window.
212 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
213 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
214 #-(or sbcl clisp ecl openmcl)
215 (error 'not-implemented)))
216 (with-xlib-protect ()
217 (catch 'exit-handle-event
218 (let ((win (getf event-slots :window)))
219 (when (and win (not (xlib:window-p win)))
220 (dbg "Pixmap Workaround! Should be a window: " win)
221 (setf (getf event-slots :window) (make-xlib-window win))))
222 (let ((hook-symbol (event-hook-name event-key)))
223 (when (boundp hook-symbol)
224 (call-hook (symbol-value hook-symbol) event-slots)))
225 (if (fboundp event-key)
226 (apply event-key event-slots)
227 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal)))
228 (xlib:display-finish-output *display*))
235 (defun parse-display-string (display)
236 "Parse an X11 DISPLAY string and return the host and display from it."
237 (let* ((colon (position #\: display))
238 (host (subseq display 0 colon))
239 (rest (subseq display (1+ colon)))
240 (dot (position #\. rest))
241 (num (parse-integer (subseq rest 0 dot))))
242 (values host num)))
245 ;;; Transparency support
246 (let ((opaque #xFFFFFFFF))
247 (defun window-transparency (window)
248 "Return the window transparency"
249 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
251 (defun set-window-transparency (window value)
252 "Set the window transparency"
253 (when (numberp value)
254 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
255 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
256 opaque)
258 :cardinal 32)))
260 (defsetf window-transparency set-window-transparency))
264 (defun window-state (win)
265 "Get the state (iconic, normal, withdrawn) of a window."
266 (first (xlib:get-property win :WM_STATE)))
269 (defun set-window-state (win state)
270 "Set the state (iconic, normal, withdrawn) of a window."
271 (xlib:change-property win
272 :WM_STATE
273 (list state)
274 :WM_STATE
275 32))
277 (defsetf window-state set-window-state)
281 (defun window-hidden-p (window)
282 (eql (window-state window) +iconic-state+))
285 (defun null-size-window-p (window)
286 (let ((hints (xlib:wm-normal-hints window)))
287 (and hints
288 (not (or (xlib:wm-size-hints-width hints)
289 (xlib:wm-size-hints-height hints)
290 (xlib:wm-size-hints-win-gravity hints)))
291 (xlib:wm-size-hints-user-specified-position-p hints))))
298 (defun unhide-window (window)
299 (when window
300 (when (window-hidden-p window)
301 (xlib:map-window window)
302 (setf (window-state window) +normal-state+
303 (xlib:window-event-mask window) *window-events*))))
306 (defun map-window (window)
307 (when window
308 (xlib:map-window window)))
311 (defun delete-window (window)
312 (send-client-message window :WM_PROTOCOLS
313 (xlib:intern-atom *display* "WM_DELETE_WINDOW")))
315 (defun destroy-window (window)
316 (xlib:kill-client *display* (xlib:window-id window)))
319 ;;(defconstant +exwm-atoms+
320 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
321 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
322 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
323 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
324 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
325 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
326 ;; "_NET_DESKTOP_LAYOUT"
328 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
329 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
330 ;; "_NET_WM_MOVERESIZE"
332 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
334 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
335 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
336 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
337 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
338 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
339 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
340 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
341 ;; ;; "_NET_WM_MOVE_ACTIONS"
343 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
344 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
345 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
346 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
347 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
348 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
349 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
350 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
351 ;; "_NET_WM_STATE_FULLSCREEN"
352 ;; "_NET_WM_STATE_ABOVE"
353 ;; "_NET_WM_STATE_BELOW"
354 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
356 ;; "_NET_WM_ALLOWED_ACTIONS"
357 ;; "_NET_WM_ACTION_MOVE"
358 ;; "_NET_WM_ACTION_RESIZE"
359 ;; "_NET_WM_ACTION_SHADE"
360 ;; "_NET_WM_ACTION_STICK"
361 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
362 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
363 ;; "_NET_WM_ACTION_FULLSCREEN"
364 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
365 ;; "_NET_WM_ACTION_CLOSE"
367 ;; ))
370 ;;(defun intern-atoms (display)
371 ;; (declare (type xlib:display display))
372 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
373 ;; +exwm-atoms+)
374 ;; (values))
378 ;;(defun get-atoms-property (window property-atom atom-list-p)
379 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
380 ;; a list of atom-id."
381 ;; (xlib:get-property window property-atom
382 ;; :transform (when atom-list-p
383 ;; (lambda (id)
384 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
386 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
387 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
388 ;; or a list of keyword atom-names."
389 ;; (xlib:change-property window property-atom atoms :ATOM 32
390 ;; :mode mode
391 ;; :transform (unless (integerp (car atoms))
392 ;; (lambda (atom-key)
393 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
398 ;;(defun net-wm-state (window)
399 ;; (get-atoms-property window :_NET_WM_STATE t))
401 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
402 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
405 (defun hide-window (window)
406 (when window
407 (setf (window-state window) +iconic-state+
408 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
409 (xlib:unmap-window window)
410 (setf (xlib:window-event-mask window) *window-events*)))
414 (defun window-type (window)
415 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
416 :dialog, :transient, :maxsize and :normal."
417 (or (and (let ((hints (xlib:wm-normal-hints window)))
418 (and hints (or (xlib:wm-size-hints-max-width hints)
419 (xlib:wm-size-hints-max-height hints)
420 (xlib:wm-size-hints-min-aspect hints)
421 (xlib:wm-size-hints-max-aspect hints))))
422 :maxsize)
423 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
424 (when net-wm-window-type
425 (dolist (type-atom net-wm-window-type)
426 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
427 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
428 (and (xlib:get-property window :WM_TRANSIENT_FOR)
429 :transient)
430 :normal))
435 ;;; Stolen from Eclipse
436 (defun send-configuration-notify (window x y w h bw)
437 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
438 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
439 :event-window window
440 :window window
441 :x x :y y
442 :width w
443 :height h
444 :border-width bw
445 :propagate-p nil))
449 (defun send-client-message (window type &rest data)
450 "Send a client message to a client's window."
451 (xlib:send-event window
452 :client-message nil
453 :window window
454 :type type
455 :format 32
456 :data data))
462 (defun raise-window (window)
463 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
464 (when (xlib:window-p window)
465 (when (window-hidden-p window)
466 (unhide-window window))
467 (setf (xlib:window-priority window) :above)))
469 (defun focus-window (window)
470 "Give the window focus."
471 (when (xlib:window-p window)
472 (xlib:set-input-focus *display* window :parent)))
474 (defun raise-and-focus-window (window)
475 "Raise and focus."
476 (raise-window window)
477 (focus-window window))
479 (defun no-focus ()
480 "don't focus any window but still read keyboard events."
481 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
484 (defun lower-window (window sibling)
485 "Map the window if needed and bring it just above sibling. Does not affect focus."
486 (when (xlib:window-p window)
487 (when (window-hidden-p window)
488 (unhide-window window))
489 (setf (xlib:window-priority window sibling) :below)))
494 (let ((cursor-font nil)
495 (cursor nil)
496 (pointer-grabbed nil))
497 (defun free-grab-pointer ()
498 (when cursor
499 (xlib:free-cursor cursor)
500 (setf cursor nil))
501 (when cursor-font
502 (xlib:close-font cursor-font)
503 (setf cursor-font nil)))
505 (defun xgrab-init-pointer ()
506 (setf pointer-grabbed nil))
508 (defun xgrab-pointer-p ()
509 pointer-grabbed)
511 (defun xgrab-pointer (root cursor-char cursor-mask-char
512 &optional (pointer-mask '(:enter-window :pointer-motion
513 :button-press :button-release)) owner-p)
514 "Grab the pointer and set the pointer shape."
515 (when pointer-grabbed
516 (xungrab-pointer))
517 (setf pointer-grabbed t)
518 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
519 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
520 (cond (cursor-char
521 (setf cursor-font (xlib:open-font *display* "cursor")
522 cursor (xlib:create-glyph-cursor :source-font cursor-font
523 :source-char (or cursor-char 68)
524 :mask-font cursor-font
525 :mask-char (or cursor-mask-char 69)
526 :foreground black
527 :background white))
528 (xlib:grab-pointer root pointer-mask
529 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
531 (xlib:grab-pointer root pointer-mask
532 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
534 (defun xungrab-pointer ()
535 "Remove the grab on the cursor and restore the cursor shape."
536 (setf pointer-grabbed nil)
537 (xlib:ungrab-pointer *display*)
538 (xlib:display-finish-output *display*)
539 (free-grab-pointer)))
542 (let ((keyboard-grabbed nil))
543 (defun xgrab-init-keyboard ()
544 (setf keyboard-grabbed nil))
546 (defun xgrab-keyboard-p ()
547 keyboard-grabbed)
549 (defun xgrab-keyboard (root)
550 (setf keyboard-grabbed t)
551 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
554 (defun xungrab-keyboard ()
555 (setf keyboard-grabbed nil)
556 (xlib:ungrab-keyboard *display*)))
563 (defun ungrab-all-buttons (window)
564 (xlib:ungrab-button window :any :modifiers :any))
566 (defun grab-all-buttons (window)
567 (ungrab-all-buttons window)
568 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
569 :modifiers :any
570 :owner-p nil
571 :sync-pointer-p t
572 :sync-keyboard-p nil))
574 (defun ungrab-all-keys (window)
575 (xlib:ungrab-key window :any :modifiers :any))
578 (defun stop-button-event ()
579 (xlib:allow-events *display* :sync-pointer))
581 (defun replay-button-event ()
582 (xlib:allow-events *display* :replay-pointer))
592 ;;; Mouse action on window
593 (let (add-fn add-arg dx dy window)
594 (define-handler move-window-mode :motion-notify (root-x root-y)
595 (unless (compress-motion-notify)
596 (if add-fn
597 (multiple-value-bind (move-x move-y)
598 (apply add-fn add-arg)
599 (when move-x
600 (setf (x-drawable-x window) (+ root-x dx)))
601 (when move-y
602 (setf (x-drawable-y window) (+ root-y dy))))
603 (setf (x-drawable-x window) (+ root-x dx)
604 (x-drawable-y window) (+ root-y dy)))))
606 (define-handler move-window-mode :key-release ()
607 (throw 'exit-move-window-mode nil))
609 (define-handler move-window-mode :button-release ()
610 (throw 'exit-move-window-mode nil))
612 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
613 (setf window orig-window
614 add-fn additional-fn
615 add-arg additional-arg
616 dx (- (x-drawable-x window) orig-x)
617 dy (- (x-drawable-y window) orig-y)
618 (xlib:window-border window) (get-color *color-move-window*))
619 (raise-window window)
620 (let ((pointer-grabbed-p (xgrab-pointer-p)))
621 (unless pointer-grabbed-p
622 (xgrab-pointer *root* nil nil))
623 (when additional-fn
624 (apply additional-fn additional-arg))
625 (generic-mode 'move-window-mode 'exit-move-window-mode
626 :original-mode '(main-mode))
627 (unless pointer-grabbed-p
628 (xungrab-pointer)))))
631 (let (add-fn add-arg window
632 o-x o-y
633 orig-width orig-height
634 min-width max-width
635 min-height max-height)
636 (define-handler resize-window-mode :motion-notify (root-x root-y)
637 (unless (compress-motion-notify)
638 (if add-fn
639 (multiple-value-bind (resize-w resize-h)
640 (apply add-fn add-arg)
641 (when resize-w
642 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
643 (when resize-h
644 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
645 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
646 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
648 (define-handler resize-window-mode :key-release ()
649 (throw 'exit-resize-window-mode nil))
651 (define-handler resize-window-mode :button-release ()
652 (throw 'exit-resize-window-mode nil))
654 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
655 (let* ((pointer-grabbed-p (xgrab-pointer-p))
656 (hints (xlib:wm-normal-hints orig-window)))
657 (setf window orig-window
658 add-fn additional-fn
659 add-arg additional-arg
660 o-x orig-x
661 o-y orig-y
662 orig-width (x-drawable-width window)
663 orig-height (x-drawable-height window)
664 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
665 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
666 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
667 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
668 (xlib:window-border window) (get-color *color-move-window*))
669 (raise-window window)
670 (unless pointer-grabbed-p
671 (xgrab-pointer *root* nil nil))
672 (when additional-fn
673 (apply additional-fn additional-arg))
674 (generic-mode 'resize-window-mode 'exit-resize-window-mode
675 :original-mode '(main-mode))
676 (unless pointer-grabbed-p
677 (xungrab-pointer)))))
680 (define-handler wait-mouse-button-release-mode :button-release ()
681 (throw 'exit-wait-mouse-button-release-mode nil))
683 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
684 (let ((pointer-grabbed-p (xgrab-pointer-p)))
685 (unless pointer-grabbed-p
686 (xgrab-pointer *root* cursor-char cursor-mask-char))
687 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)
688 (unless pointer-grabbed-p
689 (xungrab-pointer))))
694 (let ((color-hash (make-hash-table :test 'equal)))
695 (defun get-color (color)
696 (multiple-value-bind (val foundp)
697 (gethash color color-hash)
698 (if foundp
700 (setf (gethash color color-hash)
701 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
705 (defgeneric ->color (color))
707 (defmethod ->color ((color-name string))
708 color-name)
710 (defmethod ->color ((color integer))
711 (labels ((hex->float (color)
712 (/ (logand color #xFF) 256.0)))
713 (xlib:make-color :blue (hex->float color)
714 :green (hex->float (ash color -8))
715 :red (hex->float (ash color -16)))))
717 (defmethod ->color ((color list))
718 (destructuring-bind (red green blue) color
719 (xlib:make-color :blue red :green green :red blue)))
721 (defmethod ->color ((color xlib:color))
722 color)
724 (defmethod ->color (color)
725 (format t "Wrong color type: ~A~%" color)
726 "White")
729 (defun color->rgb (color)
730 (multiple-value-bind (r g b)
731 (xlib:color-rgb color)
732 (+ (ash (round (* 256 r)) +16)
733 (ash (round (* 256 g)) +8)
734 (round (* 256 b)))))
740 (defmacro my-character->keysyms (ch)
741 "Convert a char to a keysym"
742 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
743 ;; some day. Or just copied from MIT-CLX or some other CLX
744 ;; implementation (see translate.lisp and keysyms.lisp). For now,
745 ;; we do like this. It suffices for modifiers and ASCII symbols.
746 (if (fboundp 'xlib:character->keysyms)
747 `(xlib:character->keysyms ,ch)
748 `(list
749 (case ,ch
750 (:character-set-switch #xFF7E)
751 (:left-shift #xFFE1)
752 (:right-shift #xFFE2)
753 (:left-control #xFFE3)
754 (:right-control #xFFE4)
755 (:caps-lock #xFFE5)
756 (:shift-lock #xFFE6)
757 (:left-meta #xFFE7)
758 (:right-meta #xFFE8)
759 (:left-alt #xFFE9)
760 (:right-alt #xFFEA)
761 (:left-super #xFFEB)
762 (:right-super #xFFEC)
763 (:left-hyper #xFFED)
764 (:right-hyper #xFFEE)
766 (etypecase ,ch
767 (character
768 ;; Latin-1 characters have their own value as keysym
769 (if (< 31 (char-code ,ch) 256)
770 (char-code ,ch)
771 (error "Don't know how to get keysym from ~A" ,ch)))))))))
776 (defun char->keycode (char)
777 "Convert a character to a keycode"
778 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
781 (defun keycode->char (code state)
782 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
784 (defun modifiers->state (modifier-list)
785 (apply #'xlib:make-state-mask modifier-list))
787 (defun state->modifiers (state)
788 (xlib:make-state-keys state))
790 (defun keycode->keysym (code modifiers)
791 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
792 ((member :mod-5 modifiers) 4)
793 (t 0))))
796 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask) &body body)
797 `(let ((pointer-grabbed (xgrab-pointer-p))
798 (keyboard-grabbed (xgrab-keyboard-p)))
799 (xgrab-pointer *root* ,cursor ,mask)
800 (unless keyboard-grabbed
801 (xgrab-keyboard *root*))
802 (unwind-protect
803 (progn
804 ,@body)
805 (if pointer-grabbed
806 (xgrab-pointer *root* ,old-cursor ,old-mask)
807 (xungrab-pointer))
808 (unless keyboard-grabbed
809 (xungrab-keyboard)))))
816 (let ((modifier-list nil))
817 (defun init-modifier-list ()
818 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
819 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
820 "Mode_switch" "script_switch" "ISO_Level3_Shift"
821 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
822 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
823 (push it modifier-list))))
825 (defun modifier-p (code)
826 (member code modifier-list)))
828 (defun wait-no-key-or-button-press ()
829 (with-grab-keyboard-and-pointer (66 67 66 67)
830 (loop
831 (let ((key (loop for k across (xlib:query-keymap *display*)
832 for code from 0
833 when (and (plusp k) (not (modifier-p code)))
834 return t))
835 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
836 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
837 return t)))
838 (when (and (not key) (not button))
839 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
840 (:motion-notify () t)
841 (:key-press () t)
842 (:key-release () t)
843 (:button-press () t)
844 (:button-release () t)
845 (t nil)))
846 (return))))))
849 (defun wait-a-key-or-button-press ()
850 (with-grab-keyboard-and-pointer (24 25 66 67)
851 (loop
852 (let ((key (loop for k across (xlib:query-keymap *display*)
853 unless (zerop k) return t))
854 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
855 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
856 return t)))
857 (when (or key button)
858 (return))))))
862 (defun compress-motion-notify ()
863 (when *have-to-compress-notify*
864 (loop while (xlib:event-cond (*display* :timeout 0)
865 (:motion-notify () t)))))
868 (defun display-all-cursors (&optional (display-time 1))
869 "Display all X11 cursors for display-time seconds"
870 (loop for i from 0 to 152 by 2
871 do (xgrab-pointer *root* i (1+ i))
872 (dbg i)
873 (sleep display-time)
874 (xungrab-pointer)))
878 ;;; Double buffering tools
879 (defun clear-pixmap-buffer (window gc)
880 (if (equal *transparent-background* :pseudo)
881 (xlib:copy-area *background-image* *background-gc*
882 (x-drawable-x window) (x-drawable-y window)
883 (x-drawable-width window) (x-drawable-height window)
884 *pixmap-buffer* 0 0)
885 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
886 :background (xlib:gcontext-foreground gc))
887 (xlib:draw-rectangle *pixmap-buffer* gc
888 0 0 (x-drawable-width window) (x-drawable-height window)
889 t))))
892 (defun copy-pixmap-buffer (window gc)
893 (xlib:copy-area *pixmap-buffer* gc
894 0 0 (x-drawable-width window) (x-drawable-height window)
895 window 0 0))
899 (defun is-a-key-pressed-p ()
900 (loop for k across (xlib:query-keymap *display*)
901 when (plusp k)
902 return t))
904 ;;; Windows wm class and name tests
905 (defmacro defun-equal-wm-class (symbol class)
906 `(defun ,symbol (window)
907 (when (xlib:window-p window)
908 (string-equal (xlib:get-wm-class window) ,class))))
910 (defmacro defun-equal-wm-name (symbol name)
911 `(defun ,symbol (window)
912 (when (xlib:window-p window)
913 (string-equal (xlib:wm-name window) ,name))))