-
-
Save orivej/5522461 to your computer and use it in GitHub Desktop.
Take, save, and display a screenshot in Common Lisp (using CLX and ZPNG).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro with-display (host (display screen root-window) &body body) | |
`(let* ((,display (xlib:open-display ,host)) | |
(,screen (first (xlib:display-roots ,display))) | |
(,root-window (xlib:screen-root ,screen))) | |
(unwind-protect (progn ,@body) | |
(xlib:close-display ,display)))) | |
(defun take-screenshot (&optional (host "")) | |
(with-display host (display screen root-window) | |
(xlib:get-image root-window :x 0 :y 0 :width (xlib:screen-width screen) :height (xlib:screen-height screen)))) | |
(defun save-screenshot-as-png (image path) | |
(let* ((width (xlib:image-width image)) (height (xlib:image-height image)) | |
(png (make-instance 'zpng:png :width width :height height | |
:color-type :truecolor-alpha | |
:image-data (xlib:image-z-pixarray image))) | |
(data (zpng:data-array png))) | |
(dotimes (y height (zpng:write-png png path)) | |
(dotimes (x width) | |
(rotatef (aref data y x 0) (aref data y x 2)) ; BGR -> RGB | |
(setf (aref data y x 3) 255))))) | |
(defun display-screenshot (image &optional (host "")) | |
(with-display host (display screen root-window) | |
(let* ((window (xlib:create-window :parent root-window | |
:x 0 :y 0 :width (xlib:image-width image) :height (xlib:image-height image) | |
:event-mask (xlib:make-event-mask :exposure :key-press :button-press))) | |
(gcontext (xlib:create-gcontext :drawable window))) | |
(xlib:map-window window) | |
(unwind-protect | |
(handler-case | |
(xlib:event-case (display :force-output-p t :discard-p t) | |
(:exposure () (xlib:put-image window gcontext image :x 0 :y 0)) | |
(t () t)) | |
(end-of-file ())) | |
(xlib:destroy-window window))))) |
Thank you for your work, but I do desire more. Is there any other tutorials like this that you recommend? Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, learned a lot from here.
I've made something about GUI automation on X11
https://gist.github.com/VitoVan/abeeb79da01298855692153f1830360e