contrib/toolbar.lisp: End of toolbar framework.
[clfswm.git] / src / xlib-util.lisp
blob5e45bfaca7d83d80439b60990108b7703feb1909
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 ;;; --------------------------------------------------------------------------
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 :leave-window
40 :pointer-motion
41 :exposure)
42 "The events to listen for on managed windows.")
45 (defparameter +netwm-supported+
46 '(:_NET_SUPPORTING_WM_CHECK
47 :_NET_NUMBER_OF_DESKTOPS
48 :_NET_DESKTOP_GEOMETRY
49 :_NET_DESKTOP_VIEWPORT
50 :_NET_CURRENT_DESKTOP
51 :_NET_WM_WINDOW_TYPE
52 :_NET_CLIENT_LIST)
53 "Supported NETWM properties.
54 Window types are in +WINDOW-TYPES+.")
56 (defparameter +netwm-window-types+
57 '((:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
58 (:_NET_WM_WINDOW_TYPE_DOCK . :dock)
59 (:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
60 (:_NET_WM_WINDOW_TYPE_MENU . :menu)
61 (:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
62 (:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
63 (:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
64 (:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
65 "Alist mapping NETWM window types to keywords.")
68 (defmacro with-xlib-protect (() &body body)
69 "Prevent Xlib errors"
70 `(handler-case
71 (with-simple-restart (top-level "Return to clfswm's top level")
72 ,@body)
73 ((or xlib:match-error xlib:window-error xlib:drawable-error xlib:lookup-error) (c)
74 (progn
75 (format t "Ignoring XLib error: ~S~%" c)
76 (unassoc-keyword-handle-event)
77 (assoc-keyword-handle-event 'main-mode)
78 (setf *in-second-mode* nil)))))
81 (defmacro with-x-pointer (&body body)
82 "Bind (x y) to mouse pointer positions"
83 `(multiple-value-bind (x y)
84 (xlib:query-pointer *root*)
85 ,@body))
89 (declaim (inline window-x2 window-y2))
90 (defun window-x2 (window)
91 (+ (x-drawable-x window) (x-drawable-width window)))
93 (defun window-y2 (window)
94 (+ (x-drawable-y window) (x-drawable-height window)))
98 ;;;
99 ;;; Events management functions.
101 (defparameter *unhandled-events* nil)
102 (defparameter *current-event-mode* nil)
104 (eval-when (:compile-toplevel :load-toplevel :execute)
105 (defun keyword->handle-event (mode keyword)
106 (create-symbol 'handle-event-fun "-" mode "-" keyword)))
108 (defun handle-event->keyword (symbol)
109 (let* ((name (string-downcase (symbol-name symbol)))
110 (pos (search "handle-event-fun-" name)))
111 (when (and pos (zerop pos))
112 (let ((pos-mod (search "mode" name)))
113 (when pos-mod
114 (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
115 (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
117 (defparameter *handle-event-fun-symbols* nil)
119 (defun fill-handle-event-fun-symbols ()
120 (with-all-internal-symbols (symbol :clfswm)
121 (let ((pos (symbol-search "handle-event-fun-" symbol)))
122 (when (and pos (zerop pos))
123 (pushnew symbol *handle-event-fun-symbols*)))))
126 (defmacro with-handle-event-symbol ((mode) &body body)
127 "Bind symbol to all handle event functions available in mode"
128 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
129 (dolist (symbol *handle-event-fun-symbols*)
130 (let ((pos (symbol-search pattern symbol)))
131 (when (and pos (zerop pos))
132 ,@body)))))
135 (defun find-handle-event-function (&optional (mode ""))
136 "Print all handle event functions available in mode"
137 (with-handle-event-symbol (mode)
138 (print symbol)))
140 (defun assoc-keyword-handle-event (mode)
141 "Associate all keywords in mode to their corresponding handle event functions.
142 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
143 (setf *current-event-mode* mode)
144 (with-handle-event-symbol (mode)
145 (let ((keyword (handle-event->keyword symbol)))
146 (when (fboundp symbol)
147 #+:event-debug
148 (progn
149 (format t "~&Associating: ~S with ~S~%" symbol keyword)
150 (force-output))
151 (setf (symbol-function keyword) (symbol-function symbol))))))
153 (defun unassoc-keyword-handle-event (&optional (mode ""))
154 "Unbound all keywords from their corresponding handle event functions."
155 (setf *current-event-mode* nil)
156 (with-handle-event-symbol (mode)
157 (let ((keyword (handle-event->keyword symbol)))
158 (when (fboundp keyword)
159 #+:event-debug
160 (progn
161 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
162 (force-output))
163 (fmakunbound keyword)))))
165 (defmacro define-handler (mode keyword args &body body)
166 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
167 For example (define-handler main-mode :key-press (args) ...)
168 Expand in handle-event-fun-main-mode-key-press"
169 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
170 (declare (ignorable event-slots))
171 #+:event-debug (print (list *current-event-mode* event-key))
172 ,@body))
175 (defun event-hook-name (event-keyword)
176 (create-symbol '*event- event-keyword '-hook*))
178 (let ((event-hook-list nil))
179 (defmacro use-event-hook (event-keyword)
180 (let ((symb (event-hook-name event-keyword)))
181 (pushnew symb event-hook-list)
182 `(defvar ,symb nil)))
184 (defun unuse-event-hook (event-keyword)
185 (let ((symb (event-hook-name event-keyword)))
186 (setf event-hook-list (remove symb event-hook-list))
187 (makunbound symb)))
189 (defmacro add-event-hook (name &rest value)
190 (let ((symb (event-hook-name name)))
191 `(add-hook ,symb ,@value)))
193 (defmacro remove-event-hook (name &rest value)
194 (let ((symb (event-hook-name name)))
195 `(remove-hook ,symb ,@value)))
197 (defun clear-event-hooks ()
198 (dolist (symb event-hook-list)
199 (makunbound symb))))
202 (defmacro define-event-hook (event-keyword args &body body)
203 `(add-event-hook ,event-keyword
204 (lambda (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
205 (declare (ignorable event-slots))
206 #+:event-debug (print (list ,event-keyword event-key))
207 ,@body)))
209 (defmacro event-defun (name args &body body)
210 `(defun ,name (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
211 (declare (ignorable event-slots))
212 #+:event-debug (print (list ,event-keyword event-key))
213 ,@body))
215 (defun exit-handle-event ()
216 (throw 'exit-handle-event nil))
219 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
220 (labels ((make-xlib-window (xobject)
221 "For some reason the clx xid cache screws up returns pixmaps when
222 they should be windows. So use this function to make a window out of them."
223 ;; Workaround for pixmap error taken from STUMPWM - thanks:
224 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
225 ;; should be a window will be a pixmap instead. In this case, we
226 ;; need to manually translate it to a window to avoid breakage
227 ;; in stumpwm. So far the only slot that seems to be affected is
228 ;; the :window slot for configure-request and reparent-notify
229 ;; events. It appears as though the hash table of XIDs and clx
230 ;; structures gets out of sync with X or perhaps X assigns a
231 ;; duplicate ID for a pixmap and a window.
232 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
233 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
234 #-(or sbcl clisp ecl openmcl)
235 (error 'not-implemented)))
236 (with-xlib-protect ()
237 (catch 'exit-handle-event
238 (let ((win (getf event-slots :window)))
239 (when (and win (not (xlib:window-p win)))
240 (dbg "Pixmap Workaround! Should be a window: " win)
241 (setf (getf event-slots :window) (make-xlib-window win))))
242 (let ((hook-symbol (event-hook-name event-key)))
243 (when (boundp hook-symbol)
244 (apply #'call-hook (symbol-value hook-symbol) event-slots)))
245 (if (fboundp event-key)
246 (apply event-key event-slots)
247 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal)))
248 (xlib:display-finish-output *display*))
255 (defun parse-display-string (display)
256 "Parse an X11 DISPLAY string and return the host and display from it."
257 (let* ((colon (position #\: display))
258 (host (subseq display 0 colon))
259 (rest (subseq display (1+ colon)))
260 (dot (position #\. rest))
261 (num (parse-integer (subseq rest 0 dot))))
262 (values host num)))
265 ;;; Transparency support
266 (let ((opaque #xFFFFFFFF))
267 (defun window-transparency (window)
268 "Return the window transparency"
269 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
271 (defun set-window-transparency (window value)
272 "Set the window transparency"
273 (when (numberp value)
274 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
275 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
276 opaque)
278 :cardinal 32)))
280 (defsetf window-transparency set-window-transparency))
284 (defun window-state (win)
285 "Get the state (iconic, normal, withdrawn) of a window."
286 (first (xlib:get-property win :WM_STATE)))
289 (defun set-window-state (win state)
290 "Set the state (iconic, normal, withdrawn) of a window."
291 (xlib:change-property win
292 :WM_STATE
293 (list state)
294 :WM_STATE
295 32))
297 (defsetf window-state set-window-state)
301 (defun window-hidden-p (window)
302 (eql (window-state window) +iconic-state+))
305 (defun null-size-window-p (window)
306 (let ((hints (xlib:wm-normal-hints window)))
307 (and hints
308 (not (or (xlib:wm-size-hints-width hints)
309 (xlib:wm-size-hints-height hints)
310 (xlib:wm-size-hints-win-gravity hints)))
311 (xlib:wm-size-hints-user-specified-position-p hints))))
318 (defun unhide-window (window)
319 (when window
320 (when (window-hidden-p window)
321 (xlib:map-window window)
322 (setf (window-state window) +normal-state+
323 (xlib:window-event-mask window) *window-events*))))
326 (defun map-window (window)
327 (when window
328 (xlib:map-window window)))
331 (defun delete-window (window)
332 (send-client-message window :WM_PROTOCOLS
333 (xlib:intern-atom *display* "WM_DELETE_WINDOW")))
335 (defun destroy-window (window)
336 (xlib:kill-client *display* (xlib:window-id window)))
339 ;;(defconstant +exwm-atoms+
340 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
341 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
342 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
343 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
344 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
345 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
346 ;; "_NET_DESKTOP_LAYOUT"
348 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
349 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
350 ;; "_NET_WM_MOVERESIZE"
352 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
354 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
355 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
356 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
357 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
358 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
359 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
360 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
361 ;; ;; "_NET_WM_MOVE_ACTIONS"
363 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
364 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
365 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
366 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
367 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
368 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
369 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
370 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
371 ;; "_NET_WM_STATE_FULLSCREEN"
372 ;; "_NET_WM_STATE_ABOVE"
373 ;; "_NET_WM_STATE_BELOW"
374 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
376 ;; "_NET_WM_ALLOWED_ACTIONS"
377 ;; "_NET_WM_ACTION_MOVE"
378 ;; "_NET_WM_ACTION_RESIZE"
379 ;; "_NET_WM_ACTION_SHADE"
380 ;; "_NET_WM_ACTION_STICK"
381 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
382 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
383 ;; "_NET_WM_ACTION_FULLSCREEN"
384 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
385 ;; "_NET_WM_ACTION_CLOSE"
387 ;; ))
390 ;;(defun intern-atoms (display)
391 ;; (declare (type xlib:display display))
392 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
393 ;; +exwm-atoms+)
394 ;; (values))
398 ;;(defun get-atoms-property (window property-atom atom-list-p)
399 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
400 ;; a list of atom-id."
401 ;; (xlib:get-property window property-atom
402 ;; :transform (when atom-list-p
403 ;; (lambda (id)
404 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
406 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
407 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
408 ;; or a list of keyword atom-names."
409 ;; (xlib:change-property window property-atom atoms :ATOM 32
410 ;; :mode mode
411 ;; :transform (unless (integerp (car atoms))
412 ;; (lambda (atom-key)
413 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
418 ;;(defun net-wm-state (window)
419 ;; (get-atoms-property window :_NET_WM_STATE t))
421 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
422 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
425 (defun hide-window (window)
426 (when window
427 (setf (window-state window) +iconic-state+
428 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
429 (xlib:unmap-window window)
430 (setf (xlib:window-event-mask window) *window-events*)))
434 (defun window-type (window)
435 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
436 :dialog, :transient, :maxsize and :normal."
437 (or (and (let ((hints (xlib:wm-normal-hints window)))
438 (and hints (or (xlib:wm-size-hints-max-width hints)
439 (xlib:wm-size-hints-max-height hints)
440 (xlib:wm-size-hints-min-aspect hints)
441 (xlib:wm-size-hints-max-aspect hints))))
442 :maxsize)
443 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
444 (when net-wm-window-type
445 (dolist (type-atom net-wm-window-type)
446 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
447 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
448 (and (xlib:get-property window :WM_TRANSIENT_FOR)
449 :transient)
450 :normal))
455 ;;; Stolen from Eclipse
456 (defun send-configuration-notify (window x y w h bw)
457 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
458 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
459 :event-window window
460 :window window
461 :x x :y y
462 :width w
463 :height h
464 :border-width bw
465 :propagate-p nil))
469 (defun send-client-message (window type &rest data)
470 "Send a client message to a client's window."
471 (xlib:send-event window
472 :client-message nil
473 :window window
474 :type type
475 :format 32
476 :data data))
482 (defun raise-window (window)
483 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
484 (when (xlib:window-p window)
485 (when (window-hidden-p window)
486 (unhide-window window))
487 (setf (xlib:window-priority window) :above)))
489 (defun focus-window (window)
490 "Give the window focus."
491 (when (xlib:window-p window)
492 (xlib:set-input-focus *display* window :parent)))
494 (defun raise-and-focus-window (window)
495 "Raise and focus."
496 (raise-window window)
497 (focus-window window))
499 (defun no-focus ()
500 "don't focus any window but still read keyboard events."
501 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
504 (defun lower-window (window sibling)
505 "Map the window if needed and bring it just above sibling. Does not affect focus."
506 (when (xlib:window-p window)
507 (when (window-hidden-p window)
508 (unhide-window window))
509 (setf (xlib:window-priority window sibling) :below)))
514 (let ((cursor-font nil)
515 (cursor nil)
516 (pointer-grabbed nil))
517 (defun free-grab-pointer ()
518 (when cursor
519 (xlib:free-cursor cursor)
520 (setf cursor nil))
521 (when cursor-font
522 (xlib:close-font cursor-font)
523 (setf cursor-font nil)))
525 (defun xgrab-init-pointer ()
526 (setf pointer-grabbed nil))
528 (defun xgrab-pointer-p ()
529 pointer-grabbed)
531 (defun xgrab-pointer (root cursor-char cursor-mask-char
532 &optional (pointer-mask '(:enter-window :pointer-motion
533 :button-press :button-release)) owner-p)
534 "Grab the pointer and set the pointer shape."
535 (when pointer-grabbed
536 (xungrab-pointer))
537 (setf pointer-grabbed t)
538 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
539 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
540 (cond (cursor-char
541 (setf cursor-font (xlib:open-font *display* "cursor")
542 cursor (xlib:create-glyph-cursor :source-font cursor-font
543 :source-char (or cursor-char 68)
544 :mask-font cursor-font
545 :mask-char (or cursor-mask-char 69)
546 :foreground black
547 :background white))
548 (xlib:grab-pointer root pointer-mask
549 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
551 (xlib:grab-pointer root pointer-mask
552 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
554 (defun xungrab-pointer ()
555 "Remove the grab on the cursor and restore the cursor shape."
556 (setf pointer-grabbed nil)
557 (xlib:ungrab-pointer *display*)
558 (xlib:display-finish-output *display*)
559 (free-grab-pointer)))
562 (let ((keyboard-grabbed nil))
563 (defun xgrab-init-keyboard ()
564 (setf keyboard-grabbed nil))
566 (defun xgrab-keyboard-p ()
567 keyboard-grabbed)
569 (defun xgrab-keyboard (root)
570 (setf keyboard-grabbed t)
571 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
574 (defun xungrab-keyboard ()
575 (setf keyboard-grabbed nil)
576 (xlib:ungrab-keyboard *display*)))
583 (defun ungrab-all-buttons (window)
584 (xlib:ungrab-button window :any :modifiers :any))
586 (defun grab-all-buttons (window)
587 (ungrab-all-buttons window)
588 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
589 :modifiers :any
590 :owner-p nil
591 :sync-pointer-p t
592 :sync-keyboard-p nil))
594 (defun ungrab-all-keys (window)
595 (xlib:ungrab-key window :any :modifiers :any))
598 (defun stop-button-event ()
599 (xlib:allow-events *display* :sync-pointer))
601 (defun replay-button-event ()
602 (xlib:allow-events *display* :replay-pointer))
612 ;;; Mouse action on window
613 (let (add-fn add-arg dx dy window)
614 (define-handler move-window-mode :motion-notify (root-x root-y)
615 (unless (compress-motion-notify)
616 (if add-fn
617 (multiple-value-bind (move-x move-y)
618 (apply add-fn add-arg)
619 (when move-x
620 (setf (x-drawable-x window) (+ root-x dx)))
621 (when move-y
622 (setf (x-drawable-y window) (+ root-y dy))))
623 (setf (x-drawable-x window) (+ root-x dx)
624 (x-drawable-y window) (+ root-y dy)))))
626 (define-handler move-window-mode :key-release ()
627 (throw 'exit-move-window-mode nil))
629 (define-handler move-window-mode :button-release ()
630 (throw 'exit-move-window-mode nil))
632 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
633 (setf window orig-window
634 add-fn additional-fn
635 add-arg additional-arg
636 dx (- (x-drawable-x window) orig-x)
637 dy (- (x-drawable-y window) orig-y)
638 (xlib:window-border window) (get-color *color-move-window*))
639 (raise-window window)
640 (let ((pointer-grabbed-p (xgrab-pointer-p)))
641 (unless pointer-grabbed-p
642 (xgrab-pointer *root* nil nil))
643 (when additional-fn
644 (apply additional-fn additional-arg))
645 (generic-mode 'move-window-mode 'exit-move-window-mode
646 :original-mode '(main-mode))
647 (unless pointer-grabbed-p
648 (xungrab-pointer)))))
651 (let (add-fn add-arg window
652 o-x o-y
653 orig-width orig-height
654 min-width max-width
655 min-height max-height)
656 (define-handler resize-window-mode :motion-notify (root-x root-y)
657 (unless (compress-motion-notify)
658 (if add-fn
659 (multiple-value-bind (resize-w resize-h)
660 (apply add-fn add-arg)
661 (when resize-w
662 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
663 (when resize-h
664 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
665 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
666 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
668 (define-handler resize-window-mode :key-release ()
669 (throw 'exit-resize-window-mode nil))
671 (define-handler resize-window-mode :button-release ()
672 (throw 'exit-resize-window-mode nil))
674 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
675 (let* ((pointer-grabbed-p (xgrab-pointer-p))
676 (hints (xlib:wm-normal-hints orig-window)))
677 (setf window orig-window
678 add-fn additional-fn
679 add-arg additional-arg
680 o-x orig-x
681 o-y orig-y
682 orig-width (x-drawable-width window)
683 orig-height (x-drawable-height window)
684 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
685 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
686 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
687 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
688 (xlib:window-border window) (get-color *color-move-window*))
689 (raise-window window)
690 (unless pointer-grabbed-p
691 (xgrab-pointer *root* nil nil))
692 (when additional-fn
693 (apply additional-fn additional-arg))
694 (generic-mode 'resize-window-mode 'exit-resize-window-mode
695 :original-mode '(main-mode))
696 (unless pointer-grabbed-p
697 (xungrab-pointer)))))
700 (define-handler wait-mouse-button-release-mode :button-release ()
701 (throw 'exit-wait-mouse-button-release-mode nil))
703 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
704 (let ((pointer-grabbed-p (xgrab-pointer-p)))
705 (unless pointer-grabbed-p
706 (xgrab-pointer *root* cursor-char cursor-mask-char))
707 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)
708 (unless pointer-grabbed-p
709 (xungrab-pointer))))
714 (let ((color-hash (make-hash-table :test 'equal)))
715 (defun get-color (color)
716 (multiple-value-bind (val foundp)
717 (gethash color color-hash)
718 (if foundp
720 (setf (gethash color color-hash)
721 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
725 (defgeneric ->color (color))
727 (defmethod ->color ((color-name string))
728 color-name)
730 (defmethod ->color ((color integer))
731 (labels ((hex->float (color)
732 (/ (logand color #xFF) 256.0)))
733 (xlib:make-color :blue (hex->float color)
734 :green (hex->float (ash color -8))
735 :red (hex->float (ash color -16)))))
737 (defmethod ->color ((color list))
738 (destructuring-bind (red green blue) color
739 (xlib:make-color :blue red :green green :red blue)))
741 (defmethod ->color ((color xlib:color))
742 color)
744 (defmethod ->color (color)
745 (format t "Wrong color type: ~A~%" color)
746 "White")
749 (defun color->rgb (color)
750 (multiple-value-bind (r g b)
751 (xlib:color-rgb color)
752 (+ (ash (round (* 256 r)) +16)
753 (ash (round (* 256 g)) +8)
754 (round (* 256 b)))))
760 (defmacro my-character->keysyms (ch)
761 "Convert a char to a keysym"
762 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
763 ;; some day. Or just copied from MIT-CLX or some other CLX
764 ;; implementation (see translate.lisp and keysyms.lisp). For now,
765 ;; we do like this. It suffices for modifiers and ASCII symbols.
766 (if (fboundp 'xlib:character->keysyms)
767 `(xlib:character->keysyms ,ch)
768 `(list
769 (case ,ch
770 (:character-set-switch #xFF7E)
771 (:left-shift #xFFE1)
772 (:right-shift #xFFE2)
773 (:left-control #xFFE3)
774 (:right-control #xFFE4)
775 (:caps-lock #xFFE5)
776 (:shift-lock #xFFE6)
777 (:left-meta #xFFE7)
778 (:right-meta #xFFE8)
779 (:left-alt #xFFE9)
780 (:right-alt #xFFEA)
781 (:left-super #xFFEB)
782 (:right-super #xFFEC)
783 (:left-hyper #xFFED)
784 (:right-hyper #xFFEE)
786 (etypecase ,ch
787 (character
788 ;; Latin-1 characters have their own value as keysym
789 (if (< 31 (char-code ,ch) 256)
790 (char-code ,ch)
791 (error "Don't know how to get keysym from ~A" ,ch)))))))))
796 (defun char->keycode (char)
797 "Convert a character to a keycode"
798 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
801 (defun keycode->char (code state)
802 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
804 (defun modifiers->state (modifier-list)
805 (apply #'xlib:make-state-mask modifier-list))
807 (defun state->modifiers (state)
808 (xlib:make-state-keys state))
810 (defun keycode->keysym (code modifiers)
811 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
812 ((member :mod-5 modifiers) 4)
813 (t 0))))
816 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask) &body body)
817 `(let ((pointer-grabbed (xgrab-pointer-p))
818 (keyboard-grabbed (xgrab-keyboard-p)))
819 (xgrab-pointer *root* ,cursor ,mask)
820 (unless keyboard-grabbed
821 (xgrab-keyboard *root*))
822 (unwind-protect
823 (progn
824 ,@body)
825 (if pointer-grabbed
826 (xgrab-pointer *root* ,old-cursor ,old-mask)
827 (xungrab-pointer))
828 (unless keyboard-grabbed
829 (xungrab-keyboard)))))
836 (let ((modifier-list nil))
837 (defun init-modifier-list ()
838 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
839 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
840 "Mode_switch" "script_switch" "ISO_Level3_Shift"
841 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
842 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
843 (push it modifier-list))))
845 (defun modifier-p (code)
846 (member code modifier-list)))
848 (defun wait-no-key-or-button-press ()
849 (with-grab-keyboard-and-pointer (66 67 66 67)
850 (loop
851 (let ((key (loop for k across (xlib:query-keymap *display*)
852 for code from 0
853 when (and (plusp k) (not (modifier-p code)))
854 return t))
855 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
856 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
857 return t)))
858 (when (and (not key) (not button))
859 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
860 (:motion-notify () t)
861 (:key-press () t)
862 (:key-release () t)
863 (:button-press () t)
864 (:button-release () t)
865 (t nil)))
866 (return))))))
869 (defun wait-a-key-or-button-press ()
870 (with-grab-keyboard-and-pointer (24 25 66 67)
871 (loop
872 (let ((key (loop for k across (xlib:query-keymap *display*)
873 unless (zerop k) return t))
874 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
875 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
876 return t)))
877 (when (or key button)
878 (return))))))
882 (defun compress-motion-notify ()
883 (when *have-to-compress-notify*
884 (loop while (xlib:event-cond (*display* :timeout 0)
885 (:motion-notify () t)))))
888 (defun display-all-cursors (&optional (display-time 1))
889 "Display all X11 cursors for display-time seconds"
890 (loop for i from 0 to 152 by 2
891 do (xgrab-pointer *root* i (1+ i))
892 (dbg i)
893 (sleep display-time)
894 (xungrab-pointer)))
898 ;;; Double buffering tools
899 (defun clear-pixmap-buffer (window gc)
900 (if (equal *transparent-background* :pseudo)
901 (xlib:copy-area *background-image* *background-gc*
902 (x-drawable-x window) (x-drawable-y window)
903 (x-drawable-width window) (x-drawable-height window)
904 *pixmap-buffer* 0 0)
905 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
906 :background (xlib:gcontext-foreground gc))
907 (xlib:draw-rectangle *pixmap-buffer* gc
908 0 0 (x-drawable-width window) (x-drawable-height window)
909 t))))
912 (defun copy-pixmap-buffer (window gc)
913 (xlib:copy-area *pixmap-buffer* gc
914 0 0 (x-drawable-width window) (x-drawable-height window)
915 window 0 0))
919 (defun is-a-key-pressed-p ()
920 (loop for k across (xlib:query-keymap *display*)
921 when (plusp k)
922 return t))
924 ;;; Windows wm class and name tests
925 (defmacro defun-equal-wm-class (symbol class)
926 `(defun ,symbol (window)
927 (when (xlib:window-p window)
928 (string-equal (xlib:get-wm-class window) ,class))))
930 (defmacro defun-equal-wm-name (symbol name)
931 `(defun ,symbol (window)
932 (when (xlib:window-p window)
933 (string-equal (xlib:wm-name window) ,name))))