1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility functions
6 ;;; --------------------------------------------------------------------------
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
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.
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.
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.
24 ;;; --------------------------------------------------------------------------
31 (defconstant +withdrawn-state
+ 0)
32 (defconstant +normal-state
+ 1)
33 (defconstant +iconic-state
+ 3)
36 (defparameter *window-events
* '(:structure-notify
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
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."
80 (with-simple-restart (top-level "Return to clfswm's top level")
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
)
87 (write-backtrace *clfswm-x-error-filename
*
88 (format nil
"~%------- Additional information ---------
95 (error "Too many X errors: ~A (logged in ~A)" c
*clfswm-x-error-filename
*))
98 (format t
"Xlib error: ~A ~A: ~A~%" ,name
(if ,tag
,tag
',body
) c
)
102 ;;(defmacro with-xlib-protect ((&optional name tag) &body 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
*)
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
)))
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
))
163 (defun find-handle-event-function (&optional
(mode ""))
164 "Print all handle event functions available in mode"
165 (with-handle-event-symbol (mode)
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
)
177 (format t
"~&Associating: ~S with ~S~%" symbol keyword
)
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
)
189 (format t
"~&Unassociating: ~S ~S~%" symbol keyword
)
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
))
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 ()
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
))
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
)
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
)))
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
))
249 (add-event-hook ,event-keyword
,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
))
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
)))
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
))))
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)))
327 (defsetf window-transparency set-window-transparency
))
331 (defun window-state (win)
332 "Get the state (iconic, normal, withdrawn) of a window."
333 (first (xlib:get-property win
:WM_STATE
)))
336 (defun set-window-state (win state
)
337 "Set the state (iconic, normal, withdrawn) of a window."
338 (xlib:change-property win
344 (defsetf window-state set-window-state
)
348 (defun window-hidden-p (window)
349 (eql (window-state window
) +iconic-state
+))
354 (defun unhide-window (window)
356 (when (window-hidden-p window
)
357 (xlib:map-window window
)
358 (setf (window-state window
) +normal-state
+
359 (xlib:window-event-mask window
) *window-events
*))))
362 (defun map-window (window)
364 (xlib:map-window window
)))
367 (defun delete-window (window)
368 (send-client-message window
:WM_PROTOCOLS
369 (xlib:intern-atom
*display
* :WM_DELETE_WINDOW
)))
371 (defun destroy-window (window)
372 (xlib:kill-client
*display
* (xlib:window-id window
)))
375 ;;(defconstant +exwm-atoms+
376 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
377 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
378 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
379 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
380 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
381 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
382 ;; "_NET_DESKTOP_LAYOUT"
384 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
385 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
386 ;; "_NET_WM_MOVERESIZE"
388 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
390 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
391 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
392 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
393 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
394 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
395 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
396 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
397 ;; ;; "_NET_WM_MOVE_ACTIONS"
399 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
400 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
401 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
402 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
403 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
404 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
405 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
406 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
407 ;; "_NET_WM_STATE_FULLSCREEN"
408 ;; "_NET_WM_STATE_ABOVE"
409 ;; "_NET_WM_STATE_BELOW"
410 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
412 ;; "_NET_WM_ALLOWED_ACTIONS"
413 ;; "_NET_WM_ACTION_MOVE"
414 ;; "_NET_WM_ACTION_RESIZE"
415 ;; "_NET_WM_ACTION_SHADE"
416 ;; "_NET_WM_ACTION_STICK"
417 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
418 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
419 ;; "_NET_WM_ACTION_FULLSCREEN"
420 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
421 ;; "_NET_WM_ACTION_CLOSE"
426 ;;(defun intern-atoms (display)
427 ;; (declare (type xlib:display display))
428 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
434 ;;(defun get-atoms-property (window property-atom atom-list-p)
435 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
436 ;; a list of atom-id."
437 ;; (xlib:get-property window property-atom
438 ;; :transform (when atom-list-p
440 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
442 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
443 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
444 ;; or a list of keyword atom-names."
445 ;; (xlib:change-property window property-atom atoms :ATOM 32
447 ;; :transform (unless (integerp (car atoms))
448 ;; (lambda (atom-key)
449 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
454 ;;(defun net-wm-state (window)
455 ;; (get-atoms-property window :_NET_WM_STATE t))
457 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
458 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
461 (defun hide-window (window)
463 (setf (window-state window
) +iconic-state
+
464 (xlib:window-event-mask window
) (remove :structure-notify
*window-events
*))
465 (xlib:unmap-window window
)
466 (setf (xlib:window-event-mask window
) *window-events
*)))
470 (defun window-type (window)
471 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
472 :dialog, :transient, :maxsize and :normal."
473 (or (and (let ((hints (xlib:wm-normal-hints window
)))
474 (and hints
(or (xlib:wm-size-hints-max-width hints
)
475 (xlib:wm-size-hints-max-height hints
)
476 (xlib:wm-size-hints-min-aspect hints
)
477 (xlib:wm-size-hints-max-aspect hints
))))
479 (let ((net-wm-window-type (xlib:get-property window
:_NET_WM_WINDOW_TYPE
)))
480 (when net-wm-window-type
481 (dolist (type-atom net-wm-window-type
)
482 (when (assoc (xlib:atom-name
*display
* type-atom
) +netwm-window-types
+)
483 (return (cdr (assoc (xlib:atom-name
*display
* type-atom
) +netwm-window-types
+)))))))
484 (and (xlib:get-property window
:WM_TRANSIENT_FOR
)
491 ;;; Stolen from Eclipse
492 (defun send-configuration-notify (window x y w h bw
)
493 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
494 (xlib:send-event window
:configure-notify
(xlib:make-event-mask
:structure-notify
)
505 (defun send-client-message (window type
&rest data
)
506 "Send a client message to a client's window."
507 (xlib:send-event window
518 (defun raise-window (window)
519 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
520 (when (xlib:window-p window
)
521 (when (window-hidden-p window
)
522 (unhide-window window
))
523 (setf (xlib:window-priority window
) :above
)))
527 "don't focus any window but still read keyboard events."
528 (xlib:set-input-focus
*display
* *no-focus-window
* :pointer-root
))
530 (defun focus-window (window)
531 "Give the window focus."
533 (when (xlib:window-p window
)
534 (xlib:set-input-focus
*display
* window
:parent
)))
536 (defun raise-and-focus-window (window)
538 (raise-window window
)
539 (focus-window window
))
542 (defun lower-window (window sibling
)
543 "Map the window if needed and bring it just above sibling. Does not affect focus."
544 (when (xlib:window-p window
)
545 (when (window-hidden-p window
)
546 (unhide-window window
))
547 (setf (xlib:window-priority window sibling
) :below
)))
552 (let ((cursor-font nil
)
554 (pointer-grabbed nil
))
555 (defun free-grab-pointer ()
557 (xlib:free-cursor cursor
)
560 (xlib:close-font cursor-font
)
561 (setf cursor-font nil
)))
563 (defun xgrab-init-pointer ()
564 (setf pointer-grabbed nil
))
566 (defun xgrab-pointer-p ()
569 (defun xgrab-pointer (root cursor-char cursor-mask-char
570 &optional
(pointer-mask '(:enter-window
:pointer-motion
571 :button-press
:button-release
)) owner-p
)
572 "Grab the pointer and set the pointer shape."
573 (when pointer-grabbed
575 (setf pointer-grabbed t
)
576 (let* ((white (xlib:make-color
:red
1.0 :green
1.0 :blue
1.0))
577 (black (xlib:make-color
:red
0.0 :green
0.0 :blue
0.0)))
579 (setf cursor-font
(xlib:open-font
*display
* "cursor")
580 cursor
(xlib:create-glyph-cursor
:source-font cursor-font
581 :source-char
(or cursor-char
68)
582 :mask-font cursor-font
583 :mask-char
(or cursor-mask-char
69)
586 (xlib:grab-pointer root pointer-mask
587 :owner-p owner-p
:sync-keyboard-p nil
:sync-pointer-p nil
:cursor cursor
))
589 (xlib:grab-pointer root pointer-mask
590 :owner-p owner-p
:sync-keyboard-p nil
:sync-pointer-p nil
)))))
592 (defun xungrab-pointer ()
593 "Remove the grab on the cursor and restore the cursor shape."
594 (setf pointer-grabbed nil
)
595 (xlib:ungrab-pointer
*display
*)
596 (xlib:display-finish-output
*display
*)
597 (free-grab-pointer)))
600 (let ((keyboard-grabbed nil
))
601 (defun xgrab-init-keyboard ()
602 (setf keyboard-grabbed nil
))
604 (defun xgrab-keyboard-p ()
607 (defun xgrab-keyboard (root)
608 (setf keyboard-grabbed t
)
609 (xlib:grab-keyboard root
:owner-p nil
:sync-keyboard-p nil
:sync-pointer-p nil
))
612 (defun xungrab-keyboard ()
613 (setf keyboard-grabbed nil
)
614 (xlib:ungrab-keyboard
*display
*)))
621 (defun ungrab-all-buttons (window)
622 (xlib:ungrab-button window
:any
:modifiers
:any
))
624 (defun grab-all-buttons (window)
625 (ungrab-all-buttons window
)
626 (xlib:grab-button window
:any
'(:button-press
:button-release
:pointer-motion
)
630 :sync-keyboard-p nil
))
632 (defun ungrab-all-keys (window)
633 (xlib:ungrab-key window
:any
:modifiers
:any
))
637 (defmacro with-grab-keyboard-and-pointer
((cursor mask old-cursor old-mask
&optional ungrab-main
) &body body
)
638 `(let ((pointer-grabbed (xgrab-pointer-p))
639 (keyboard-grabbed (xgrab-keyboard-p)))
640 (xgrab-pointer *root
* ,cursor
,mask
)
641 (unless keyboard-grabbed
644 (xgrab-keyboard *root
*))
650 (xgrab-pointer *root
* ,old-cursor
,old-mask
)
652 (unless keyboard-grabbed
655 (xungrab-keyboard))))))
658 (defmacro with-grab-pointer
((&optional cursor-char cursor-mask-char
) &body body
)
659 `(let ((pointer-grabbed-p (xgrab-pointer-p)))
660 (unless pointer-grabbed-p
661 (xgrab-pointer *root
* ,cursor-char
,cursor-mask-char
))
665 (unless pointer-grabbed-p
666 (xungrab-pointer)))))
672 (defun stop-button-event ()
673 (xlib:allow-events
*display
* :sync-pointer
))
675 (defun replay-button-event ()
676 (xlib:allow-events
*display
* :replay-pointer
))
686 ;;; Mouse action on window
687 (let (add-fn add-arg dx dy window
)
688 (define-handler move-window-mode
:motion-notify
(root-x root-y
)
689 (unless (compress-motion-notify)
691 (multiple-value-bind (move-x move-y
)
692 (apply add-fn add-arg
)
694 (setf (x-drawable-x window
) (+ root-x dx
)))
696 (setf (x-drawable-y window
) (+ root-y dy
))))
697 (setf (x-drawable-x window
) (+ root-x dx
)
698 (x-drawable-y window
) (+ root-y dy
)))))
700 (define-handler move-window-mode
:key-release
()
701 (throw 'exit-move-window-mode nil
))
703 (define-handler move-window-mode
:button-release
()
704 (throw 'exit-move-window-mode nil
))
706 (defun move-window (orig-window orig-x orig-y
&optional additional-fn additional-arg
)
707 (setf window orig-window
709 add-arg additional-arg
710 dx
(- (x-drawable-x window
) orig-x
)
711 dy
(- (x-drawable-y window
) orig-y
)
712 (xlib:window-border window
) (get-color *color-move-window
*))
713 (raise-window window
)
714 (with-grab-pointer ()
716 (apply additional-fn additional-arg
))
717 (generic-mode 'move-window-mode
'exit-move-window-mode
718 :original-mode
'(main-mode)))))
721 (let (add-fn add-arg window
723 orig-width orig-height
725 min-height max-height
)
726 (define-handler resize-window-mode
:motion-notify
(root-x root-y
)
727 (unless (compress-motion-notify)
729 (multiple-value-bind (resize-w resize-h
)
730 (apply add-fn add-arg
)
732 (setf (x-drawable-width window
) (min (max (+ orig-width
(- root-x o-x
)) 10 min-width
) max-width
)))
734 (setf (x-drawable-height window
) (min (max (+ orig-height
(- root-y o-y
)) 10 min-height
) max-height
))))
735 (setf (x-drawable-width window
) (min (max (+ orig-width
(- root-x o-x
)) 10 min-width
) max-width
)
736 (x-drawable-height window
) (min (max (+ orig-height
(- root-y o-y
)) 10 min-height
) max-height
)))))
738 (define-handler resize-window-mode
:key-release
()
739 (throw 'exit-resize-window-mode nil
))
741 (define-handler resize-window-mode
:button-release
()
742 (throw 'exit-resize-window-mode nil
))
744 (defun resize-window (orig-window orig-x orig-y
&optional additional-fn additional-arg
)
745 (let* ((hints (xlib:wm-normal-hints orig-window
)))
746 (setf window orig-window
748 add-arg additional-arg
751 orig-width
(x-drawable-width window
)
752 orig-height
(x-drawable-height window
)
753 min-width
(or (and hints
(xlib:wm-size-hints-min-width hints
)) 0)
754 min-height
(or (and hints
(xlib:wm-size-hints-min-height hints
)) 0)
755 max-width
(or (and hints
(xlib:wm-size-hints-max-width hints
)) most-positive-fixnum
)
756 max-height
(or (and hints
(xlib:wm-size-hints-max-height hints
)) most-positive-fixnum
)
757 (xlib:window-border window
) (get-color *color-move-window
*))
758 (raise-window window
)
759 (with-grab-pointer ()
761 (apply additional-fn additional-arg
))
762 (generic-mode 'resize-window-mode
'exit-resize-window-mode
763 :original-mode
'(main-mode))))))
767 (define-handler wait-mouse-button-release-mode
:button-release
()
768 (throw 'exit-wait-mouse-button-release-mode nil
))
770 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char
)
771 (with-grab-pointer (cursor-char cursor-mask-char
)
772 (generic-mode 'wait-mouse-button-release
'exit-wait-mouse-button-release-mode
)))
778 (let ((color-hash (make-hash-table :test
'equal
)))
779 (defun get-color (color)
780 (multiple-value-bind (val foundp
)
781 (gethash color color-hash
)
784 (setf (gethash color color-hash
)
785 (xlib:alloc-color
(xlib:screen-default-colormap
*screen
*) color
))))))
789 (defgeneric -
>color
(color))
791 (defmethod ->color
((color-name string
))
794 (defmethod ->color
((color integer
))
795 (labels ((hex->float
(color)
796 (/ (logand color
#xFF
) 256.0)))
797 (xlib:make-color
:blue
(hex->float color
)
798 :green
(hex->float
(ash color -
8))
799 :red
(hex->float
(ash color -
16)))))
801 (defmethod ->color
((color list
))
802 (destructuring-bind (red green blue
) color
803 (xlib:make-color
:blue red
:green green
:red blue
)))
805 (defmethod ->color
((color xlib
:color
))
808 (defmethod ->color
(color)
809 (format t
"Wrong color type: ~A~%" color
)
813 (defun color->rgb
(color)
814 (multiple-value-bind (r g b
)
815 (xlib:color-rgb color
)
816 (+ (ash (round (* 256 r
)) +16)
817 (ash (round (* 256 g
)) +8)
824 (defmacro my-character-
>keysyms
(ch)
825 "Convert a char to a keysym"
826 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
827 ;; some day. Or just copied from MIT-CLX or some other CLX
828 ;; implementation (see translate.lisp and keysyms.lisp). For now,
829 ;; we do like this. It suffices for modifiers and ASCII symbols.
830 (if (fboundp 'xlib
:character-
>keysyms
)
831 `(xlib:character-
>keysyms
,ch
)
834 (:character-set-switch
#xFF7E
)
836 (:right-shift
#xFFE2
)
837 (:left-control
#xFFE3
)
838 (:right-control
#xFFE4
)
846 (:right-super
#xFFEC
)
848 (:right-hyper
#xFFEE
)
852 ;; Latin-1 characters have their own value as keysym
853 (if (< 31 (char-code ,ch
) 256)
855 (error "Don't know how to get keysym from ~A" ,ch
)))))))))
860 (defun char->keycode
(char)
861 "Convert a character to a keycode"
862 (xlib:keysym-
>keycodes
*display
* (first (my-character->keysyms char
))))
865 (defun keycode->char
(code state
)
866 (xlib:keysym-
>character
*display
* (xlib:keycode-
>keysym
*display
* code
0) state
))
868 (defun modifiers->state
(modifier-list)
869 (apply #'xlib
:make-state-mask modifier-list
))
871 (defun state->modifiers
(state)
872 (xlib:make-state-keys state
))
874 (defun keycode->keysym
(code modifiers
)
875 (xlib:keycode-
>keysym
*display
* code
(cond ((member :shift modifiers
) 1)
876 ((member :mod-5 modifiers
) 4)
883 (let ((modifier-list nil
))
884 (defun init-modifier-list ()
885 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
886 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
887 "Mode_switch" "script_switch" "ISO_Level3_Shift"
888 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
889 (awhen (xlib:keysym-
>keycodes
*display
* (keysym-name->keysym name
))
890 (push it modifier-list
))))
892 (defun modifier-p (code)
893 (member code modifier-list
)))
895 (defun wait-no-key-or-button-press ()
896 (with-grab-keyboard-and-pointer (66 67 66 67)
898 (let ((key (loop for k across
(xlib:query-keymap
*display
*)
900 when
(and (plusp k
) (not (modifier-p code
)))
902 (button (loop for b in
(xlib:make-state-keys
(nth-value 4 (xlib:query-pointer
*root
*)))
903 when
(member b
'(:button-1
:button-2
:button-3
:button-4
:button-5
))
905 (when (and (not key
) (not button
))
906 (loop while
(xlib:event-case
(*display
* :discard-p t
:peek-p nil
:timeout
0)
907 (:motion-notify
() t
)
911 (:button-release
() t
)
916 (defun wait-a-key-or-button-press ()
917 (with-grab-keyboard-and-pointer (24 25 66 67)
919 (let ((key (loop for k across
(xlib:query-keymap
*display
*)
920 unless
(zerop k
) return t
))
921 (button (loop for b in
(xlib:make-state-keys
(nth-value 4 (xlib:query-pointer
*root
*)))
922 when
(member b
'(:button-1
:button-2
:button-3
:button-4
:button-5
))
924 (when (or key button
)
929 (defun compress-motion-notify ()
930 (when *have-to-compress-notify
*
931 (loop while
(xlib:event-cond
(*display
* :timeout
0)
932 (:motion-notify
() t
)))))
935 (defun display-all-cursors (&optional
(display-time 1))
936 "Display all X11 cursors for display-time seconds"
937 (loop for i from
0 to
152 by
2
938 do
(xgrab-pointer *root
* i
(1+ i
))
945 ;;; Double buffering tools
946 (defun clear-pixmap-buffer (window gc
)
947 (if (equal *transparent-background
* :pseudo
)
948 (xlib:copy-area
*background-image
* *background-gc
*
949 (x-drawable-x window
) (x-drawable-y window
)
950 (x-drawable-width window
) (x-drawable-height window
)
952 (xlib:with-gcontext
(gc :foreground
(xlib:gcontext-background gc
)
953 :background
(xlib:gcontext-foreground gc
))
954 (xlib:draw-rectangle
*pixmap-buffer
* gc
955 0 0 (x-drawable-width window
) (x-drawable-height window
)
959 (defun copy-pixmap-buffer (window gc
)
960 (xlib:copy-area
*pixmap-buffer
* gc
961 0 0 (x-drawable-width window
) (x-drawable-height window
)
966 (defun is-a-key-pressed-p ()
967 (loop for k across
(xlib:query-keymap
*display
*)
971 ;;; Windows wm class and name tests
972 (defmacro defun-equal-wm-class
(symbol class
)
973 `(defun ,symbol
(window)
974 (when (xlib:window-p window
)
975 (string-equal (xlib:get-wm-class window
) ,class
))))
977 (defmacro defun-equal-wm-name
(symbol name
)
978 `(defun ,symbol
(window)
979 (when (xlib:window-p window
)
980 (string-equal (xlib:wm-name window
) ,name
))))