remove fake test
[clfswm.git] / src / xlib-util.lisp
blob7e42730782e9dcf38b57f5962d27207edeb5d185
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))
174 ;;; Workaround for pixmap error taken from STUMPWM - thanks:
175 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
176 ;; should be a window will be a pixmap instead. In this case, we
177 ;; need to manually translate it to a window to avoid breakage
178 ;; in stumpwm. So far the only slot that seems to be affected is
179 ;; the :window slot for configure-request and reparent-notify
180 ;; events. It appears as though the hash table of XIDs and clx
181 ;; structures gets out of sync with X or perhaps X assigns a
182 ;; duplicate ID for a pixmap and a window.
183 (defun make-xlib-window (xobject)
184 "For some reason the clx xid cache screws up returns pixmaps when
185 they should be windows. So use this function to make a window out of them."
186 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
187 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
188 #-(or sbcl clisp ecl openmcl)
189 (error 'not-implemented))
192 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
193 (with-xlib-protect ()
194 (let ((win (getf event-slots :window)))
195 (when (and win (not (xlib:window-p win)))
196 (dbg "Pixmap Workaround! Should be a window: " win)
197 (setf (getf event-slots :window) (make-xlib-window win))))
198 (if (fboundp event-key)
199 (apply event-key event-slots)
200 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal))
201 (xlib:display-finish-output *display*))
208 (defun parse-display-string (display)
209 "Parse an X11 DISPLAY string and return the host and display from it."
210 (let* ((colon (position #\: display))
211 (host (subseq display 0 colon))
212 (rest (subseq display (1+ colon)))
213 (dot (position #\. rest))
214 (num (parse-integer (subseq rest 0 dot))))
215 (values host num)))
218 ;;; Transparency support
219 (let ((opaque #xFFFFFFFF))
220 (defun window-transparency (window)
221 "Return the window transparency"
222 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
224 (defun set-window-transparency (window value)
225 "Set the window transparency"
226 (when (numberp value)
227 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
228 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
229 opaque)
231 :cardinal 32)))
233 (defsetf window-transparency set-window-transparency))
237 (defun window-state (win)
238 "Get the state (iconic, normal, withdrawn) of a window."
239 (first (xlib:get-property win :WM_STATE)))
242 (defun set-window-state (win state)
243 "Set the state (iconic, normal, withdrawn) of a window."
244 (xlib:change-property win
245 :WM_STATE
246 (list state)
247 :WM_STATE
248 32))
250 (defsetf window-state set-window-state)
254 (defun window-hidden-p (window)
255 (eql (window-state window) +iconic-state+))
258 (defun null-size-window-p (window)
259 (let ((hints (xlib:wm-normal-hints window)))
260 (and hints
261 (not (or (xlib:wm-size-hints-width hints)
262 (xlib:wm-size-hints-height hints)
263 (xlib:wm-size-hints-win-gravity hints)))
264 (xlib:wm-size-hints-user-specified-position-p hints))))
271 (defun unhide-window (window)
272 (when window
273 (when (window-hidden-p window)
274 (xlib:map-window window)
275 (setf (window-state window) +normal-state+
276 (xlib:window-event-mask window) *window-events*))))
279 (defun map-window (window)
280 (when window
281 (xlib:map-window window)))
284 (defun delete-window (window)
285 (send-client-message window :WM_PROTOCOLS
286 (xlib:intern-atom *display* "WM_DELETE_WINDOW")))
288 (defun destroy-window (window)
289 (xlib:kill-client *display* (xlib:window-id window)))
292 ;;(defconstant +exwm-atoms+
293 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
294 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
295 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
296 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
297 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
298 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
299 ;; "_NET_DESKTOP_LAYOUT"
301 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
302 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
303 ;; "_NET_WM_MOVERESIZE"
305 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
307 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
308 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
309 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
310 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
311 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
312 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
313 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
314 ;; ;; "_NET_WM_MOVE_ACTIONS"
316 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
317 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
318 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
319 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
320 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
321 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
322 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
323 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
324 ;; "_NET_WM_STATE_FULLSCREEN"
325 ;; "_NET_WM_STATE_ABOVE"
326 ;; "_NET_WM_STATE_BELOW"
327 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
329 ;; "_NET_WM_ALLOWED_ACTIONS"
330 ;; "_NET_WM_ACTION_MOVE"
331 ;; "_NET_WM_ACTION_RESIZE"
332 ;; "_NET_WM_ACTION_SHADE"
333 ;; "_NET_WM_ACTION_STICK"
334 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
335 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
336 ;; "_NET_WM_ACTION_FULLSCREEN"
337 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
338 ;; "_NET_WM_ACTION_CLOSE"
340 ;; ))
343 ;;(defun intern-atoms (display)
344 ;; (declare (type xlib:display display))
345 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
346 ;; +exwm-atoms+)
347 ;; (values))
351 ;;(defun get-atoms-property (window property-atom atom-list-p)
352 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
353 ;; a list of atom-id."
354 ;; (xlib:get-property window property-atom
355 ;; :transform (when atom-list-p
356 ;; (lambda (id)
357 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
359 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
360 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
361 ;; or a list of keyword atom-names."
362 ;; (xlib:change-property window property-atom atoms :ATOM 32
363 ;; :mode mode
364 ;; :transform (unless (integerp (car atoms))
365 ;; (lambda (atom-key)
366 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
371 ;;(defun net-wm-state (window)
372 ;; (get-atoms-property window :_NET_WM_STATE t))
374 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
375 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
378 (defun hide-window (window)
379 (when window
380 (setf (window-state window) +iconic-state+
381 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
382 (xlib:unmap-window window)
383 (setf (xlib:window-event-mask window) *window-events*)))
387 (defun window-type (window)
388 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
389 :dialog, :transient, :maxsize and :normal."
390 (or (and (let ((hints (xlib:wm-normal-hints window)))
391 (and hints (or (xlib:wm-size-hints-max-width hints)
392 (xlib:wm-size-hints-max-height hints)
393 (xlib:wm-size-hints-min-aspect hints)
394 (xlib:wm-size-hints-max-aspect hints))))
395 :maxsize)
396 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
397 (when net-wm-window-type
398 (dolist (type-atom net-wm-window-type)
399 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
400 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
401 (and (xlib:get-property window :WM_TRANSIENT_FOR)
402 :transient)
403 :normal))
408 ;;; Stolen from Eclipse
409 (defun send-configuration-notify (window x y w h bw)
410 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
411 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
412 :event-window window
413 :window window
414 :x x :y y
415 :width w
416 :height h
417 :border-width bw
418 :propagate-p nil))
422 (defun send-client-message (window type &rest data)
423 "Send a client message to a client's window."
424 (xlib:send-event window
425 :client-message nil
426 :window window
427 :type type
428 :format 32
429 :data data))
435 (defun raise-window (window)
436 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
437 (when (xlib:window-p window)
438 (when (window-hidden-p window)
439 (unhide-window window))
440 (setf (xlib:window-priority window) :above)))
442 (defun focus-window (window)
443 "Give the window focus."
444 (when (xlib:window-p window)
445 (xlib:set-input-focus *display* window :parent)))
447 (defun raise-and-focus-window (window)
448 "Raise and focus."
449 (raise-window window)
450 (focus-window window))
452 (defun no-focus ()
453 "don't focus any window but still read keyboard events."
454 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
457 (defun lower-window (window sibling)
458 "Map the window if needed and bring it just above sibling. Does not affect focus."
459 (when (xlib:window-p window)
460 (when (window-hidden-p window)
461 (unhide-window window))
462 (setf (xlib:window-priority window sibling) :below)))
467 (let ((cursor-font nil)
468 (cursor nil)
469 (pointer-grabbed nil))
470 (defun free-grab-pointer ()
471 (when cursor
472 (xlib:free-cursor cursor)
473 (setf cursor nil))
474 (when cursor-font
475 (xlib:close-font cursor-font)
476 (setf cursor-font nil)))
478 (defun xgrab-init-pointer ()
479 (setf pointer-grabbed nil))
481 (defun xgrab-pointer-p ()
482 pointer-grabbed)
484 (defun xgrab-pointer (root cursor-char cursor-mask-char
485 &optional (pointer-mask '(:enter-window :pointer-motion
486 :button-press :button-release)) owner-p)
487 "Grab the pointer and set the pointer shape."
488 (when pointer-grabbed
489 (xungrab-pointer))
490 (setf pointer-grabbed t)
491 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
492 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
493 (cond (cursor-char
494 (setf cursor-font (xlib:open-font *display* "cursor")
495 cursor (xlib:create-glyph-cursor :source-font cursor-font
496 :source-char (or cursor-char 68)
497 :mask-font cursor-font
498 :mask-char (or cursor-mask-char 69)
499 :foreground black
500 :background white))
501 (xlib:grab-pointer root pointer-mask
502 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
504 (xlib:grab-pointer root pointer-mask
505 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
507 (defun xungrab-pointer ()
508 "Remove the grab on the cursor and restore the cursor shape."
509 (setf pointer-grabbed nil)
510 (xlib:ungrab-pointer *display*)
511 (xlib:display-finish-output *display*)
512 (free-grab-pointer)))
515 (let ((keyboard-grabbed nil))
516 (defun xgrab-init-keyboard ()
517 (setf keyboard-grabbed nil))
519 (defun xgrab-keyboard-p ()
520 keyboard-grabbed)
522 (defun xgrab-keyboard (root)
523 (setf keyboard-grabbed t)
524 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
527 (defun xungrab-keyboard ()
528 (setf keyboard-grabbed nil)
529 (xlib:ungrab-keyboard *display*)))
536 (defun ungrab-all-buttons (window)
537 (xlib:ungrab-button window :any :modifiers :any))
539 (defun grab-all-buttons (window)
540 (ungrab-all-buttons window)
541 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
542 :modifiers :any
543 :owner-p nil
544 :sync-pointer-p t
545 :sync-keyboard-p nil))
547 (defun ungrab-all-keys (window)
548 (xlib:ungrab-key window :any :modifiers :any))
551 (defun stop-button-event ()
552 (xlib:allow-events *display* :sync-pointer))
554 (defun replay-button-event ()
555 (xlib:allow-events *display* :replay-pointer))
565 ;;; Mouse action on window
566 (let (add-fn add-arg dx dy window)
567 (define-handler move-window-mode :motion-notify (root-x root-y)
568 (unless (compress-motion-notify)
569 (if add-fn
570 (multiple-value-bind (move-x move-y)
571 (apply add-fn add-arg)
572 (when move-x
573 (setf (x-drawable-x window) (+ root-x dx)))
574 (when move-y
575 (setf (x-drawable-y window) (+ root-y dy))))
576 (setf (x-drawable-x window) (+ root-x dx)
577 (x-drawable-y window) (+ root-y dy)))))
579 (define-handler move-window-mode :key-release ()
580 (throw 'exit-move-window-mode nil))
582 (define-handler move-window-mode :button-release ()
583 (throw 'exit-move-window-mode nil))
585 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
586 (setf window orig-window
587 add-fn additional-fn
588 add-arg additional-arg
589 dx (- (x-drawable-x window) orig-x)
590 dy (- (x-drawable-y window) orig-y)
591 (xlib:window-border window) (get-color *color-move-window*))
592 (raise-window window)
593 (let ((pointer-grabbed-p (xgrab-pointer-p)))
594 (unless pointer-grabbed-p
595 (xgrab-pointer *root* nil nil))
596 (when additional-fn
597 (apply additional-fn additional-arg))
598 (generic-mode 'move-window-mode 'exit-move-window-mode
599 :original-mode '(main-mode))
600 (unless pointer-grabbed-p
601 (xungrab-pointer)))))
604 (let (add-fn add-arg window
605 o-x o-y
606 orig-width orig-height
607 min-width max-width
608 min-height max-height)
609 (define-handler resize-window-mode :motion-notify (root-x root-y)
610 (unless (compress-motion-notify)
611 (if add-fn
612 (multiple-value-bind (resize-w resize-h)
613 (apply add-fn add-arg)
614 (when resize-w
615 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
616 (when resize-h
617 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
618 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
619 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
621 (define-handler resize-window-mode :key-release ()
622 (throw 'exit-resize-window-mode nil))
624 (define-handler resize-window-mode :button-release ()
625 (throw 'exit-resize-window-mode nil))
627 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
628 (let* ((pointer-grabbed-p (xgrab-pointer-p))
629 (hints (xlib:wm-normal-hints orig-window)))
630 (setf window orig-window
631 add-fn additional-fn
632 add-arg additional-arg
633 o-x orig-x
634 o-y orig-y
635 orig-width (x-drawable-width window)
636 orig-height (x-drawable-height window)
637 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
638 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
639 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
640 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
641 (xlib:window-border window) (get-color *color-move-window*))
642 (raise-window window)
643 (unless pointer-grabbed-p
644 (xgrab-pointer *root* nil nil))
645 (when additional-fn
646 (apply additional-fn additional-arg))
647 (generic-mode 'resize-window-mode 'exit-resize-window-mode
648 :original-mode '(main-mode))
649 (unless pointer-grabbed-p
650 (xungrab-pointer)))))
653 (define-handler wait-mouse-button-release-mode :button-release ()
654 (throw 'exit-wait-mouse-button-release-mode nil))
656 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
657 (let ((pointer-grabbed-p (xgrab-pointer-p)))
658 (unless pointer-grabbed-p
659 (xgrab-pointer *root* cursor-char cursor-mask-char))
660 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)
661 (unless pointer-grabbed-p
662 (xungrab-pointer))))
667 (let ((color-hash (make-hash-table :test 'equal)))
668 (defun get-color (color)
669 (multiple-value-bind (val foundp)
670 (gethash color color-hash)
671 (if foundp
673 (setf (gethash color color-hash)
674 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
678 (defgeneric ->color (color))
680 (defmethod ->color ((color-name string))
681 color-name)
683 (defmethod ->color ((color integer))
684 (labels ((hex->float (color)
685 (/ (logand color #xFF) 256.0)))
686 (xlib:make-color :blue (hex->float color)
687 :green (hex->float (ash color -8))
688 :red (hex->float (ash color -16)))))
690 (defmethod ->color ((color list))
691 (destructuring-bind (red green blue) color
692 (xlib:make-color :blue red :green green :red blue)))
694 (defmethod ->color ((color xlib:color))
695 color)
697 (defmethod ->color (color)
698 (format t "Wrong color type: ~A~%" color)
699 "White")
702 (defun color->rgb (color)
703 (multiple-value-bind (r g b)
704 (xlib:color-rgb color)
705 (+ (ash (round (* 256 r)) +16)
706 (ash (round (* 256 g)) +8)
707 (round (* 256 b)))))
713 (defmacro my-character->keysyms (ch)
714 "Convert a char to a keysym"
715 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
716 ;; some day. Or just copied from MIT-CLX or some other CLX
717 ;; implementation (see translate.lisp and keysyms.lisp). For now,
718 ;; we do like this. It suffices for modifiers and ASCII symbols.
719 (if (fboundp 'xlib:character->keysyms)
720 `(xlib:character->keysyms ,ch)
721 `(list
722 (case ,ch
723 (:character-set-switch #xFF7E)
724 (:left-shift #xFFE1)
725 (:right-shift #xFFE2)
726 (:left-control #xFFE3)
727 (:right-control #xFFE4)
728 (:caps-lock #xFFE5)
729 (:shift-lock #xFFE6)
730 (:left-meta #xFFE7)
731 (:right-meta #xFFE8)
732 (:left-alt #xFFE9)
733 (:right-alt #xFFEA)
734 (:left-super #xFFEB)
735 (:right-super #xFFEC)
736 (:left-hyper #xFFED)
737 (:right-hyper #xFFEE)
739 (etypecase ,ch
740 (character
741 ;; Latin-1 characters have their own value as keysym
742 (if (< 31 (char-code ,ch) 256)
743 (char-code ,ch)
744 (error "Don't know how to get keysym from ~A" ,ch)))))))))
749 (defun char->keycode (char)
750 "Convert a character to a keycode"
751 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
754 (defun keycode->char (code state)
755 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
757 (defun modifiers->state (modifier-list)
758 (apply #'xlib:make-state-mask modifier-list))
760 (defun state->modifiers (state)
761 (xlib:make-state-keys state))
763 (defun keycode->keysym (code modifiers)
764 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
765 ((member :mod-5 modifiers) 4)
766 (t 0))))
769 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask) &body body)
770 `(let ((pointer-grabbed (xgrab-pointer-p))
771 (keyboard-grabbed (xgrab-keyboard-p)))
772 (xgrab-pointer *root* ,cursor ,mask)
773 (unless keyboard-grabbed
774 (xgrab-keyboard *root*))
775 (unwind-protect
776 (progn
777 ,@body)
778 (if pointer-grabbed
779 (xgrab-pointer *root* ,old-cursor ,old-mask)
780 (xungrab-pointer))
781 (unless keyboard-grabbed
782 (xungrab-keyboard)))))
789 (let ((modifier-list nil))
790 (defun init-modifier-list ()
791 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
792 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
793 "Mode_switch" "script_switch" "ISO_Level3_Shift"
794 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
795 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
796 (push it modifier-list))))
798 (defun modifier-p (code)
799 (member code modifier-list)))
801 (defun wait-no-key-or-button-press ()
802 (with-grab-keyboard-and-pointer (66 67 66 67)
803 (loop
804 (let ((key (loop for k across (xlib:query-keymap *display*)
805 for code from 0
806 when (and (plusp k) (not (modifier-p code)))
807 return t))
808 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
809 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
810 return t)))
811 (when (and (not key) (not button))
812 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
813 (:motion-notify () t)
814 (:key-press () t)
815 (:key-release () t)
816 (:button-press () t)
817 (:button-release () t)
818 (t nil)))
819 (return))))))
822 (defun wait-a-key-or-button-press ()
823 (with-grab-keyboard-and-pointer (24 25 66 67)
824 (loop
825 (let ((key (loop for k across (xlib:query-keymap *display*)
826 unless (zerop k) return t))
827 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
828 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
829 return t)))
830 (when (or key button)
831 (return))))))
835 (defun compress-motion-notify ()
836 (when *have-to-compress-notify*
837 (loop while (xlib:event-cond (*display* :timeout 0)
838 (:motion-notify () t)))))
841 (defun display-all-cursors (&optional (display-time 1))
842 "Display all X11 cursors for display-time seconds"
843 (loop for i from 0 to 152 by 2
844 do (xgrab-pointer *root* i (1+ i))
845 (dbg i)
846 (sleep display-time)
847 (xungrab-pointer)))
851 ;;; Double buffering tools
852 (defun clear-pixmap-buffer (window gc)
853 (if (equal *transparent-background* :pseudo)
854 (xlib:copy-area *background-image* *background-gc*
855 (x-drawable-x window) (x-drawable-y window)
856 (x-drawable-width window) (x-drawable-height window)
857 *pixmap-buffer* 0 0)
858 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
859 :background (xlib:gcontext-foreground gc))
860 (xlib:draw-rectangle *pixmap-buffer* gc
861 0 0 (x-drawable-width window) (x-drawable-height window)
862 t))))
865 (defun copy-pixmap-buffer (window gc)
866 (xlib:copy-area *pixmap-buffer* gc
867 0 0 (x-drawable-width window) (x-drawable-height window)
868 window 0 0))
872 (defun is-a-key-pressed-p ()
873 (loop for k across (xlib:query-keymap *display*)
874 when (plusp k)
875 return t))
877 ;;; Windows wm class and name tests
878 (defmacro defun-equal-wm-class (symbol class)
879 `(defun ,symbol (window)
880 (when (xlib:window-p window)
881 (string-equal (xlib:get-wm-class window) ,class))))
883 (defmacro defun-equal-wm-name (symbol name)
884 `(defun ,symbol (window)
885 (when (xlib:window-p window)
886 (string-equal (xlib:wm-name window) ,name))))