Skip to content

Instantly share code, notes, and snippets.

@orivej
Forked from liftoff/xcb_screenshot.py
Last active June 5, 2018 15:20
Show Gist options
  • Save orivej/5522461 to your computer and use it in GitHub Desktop.
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).
(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)))))
@VitoVan
Copy link

VitoVan commented Aug 16, 2016

Thank you, learned a lot from here.
I've made something about GUI automation on X11
https://gist.github.com/VitoVan/abeeb79da01298855692153f1830360e

@sparkecho
Copy link

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