CCL meets Xlib and Cairo

After I struggled a bit to get CCL's ffigen working, everything is working like a charm. Here is my initial take at doing cairo graphics. [Not that McCLIM already has a Cairo backend, written by --- You guess.]

(defun foo ()
  (let ((dpy (#_XOpenDisplay +null-ptr+)))
    (unwind-protect
         (progn
           (let* ((w 400) (h 400)
                  (screen (#_XDefaultScreen dpy))
                  (win (#_XCreateSimpleWindow dpy
                                              (#_XDefaultRootWindow dpy)
                                              0 0 w h 0
                                              (#_XBlackPixel dpy screen)
                                              (#_XWhitePixel dpy screen)))
                  (visual (#_XDefaultVisual dpy screen))
                  (surface (#_cairo_xlib_surface_create dpy win visual w h)))
             ;;
             (#_cairo_xlib_surface_set_size surface w h)
             ;;
             (ccl:with-cstrs ((x "CCL meets Xlib and Cairo"))
               (#_XStoreName dpy win x))
             (#_XMapWindow dpy win)
             ;;
             (#_XSelectInput dpy win #$ExposureMask)
             ;;
             (rlet ((event :<XE>vent))
               (loop
                 (process-wait "Waiting for X11 event"
                               (lambda ()
                                 (not (zerop (#_XPending dpy)))))
                 (#_XNextEvent dpy event)
                 (case (pref event :<XE>vent.type)
                   (#.#$Expose
                    (let ((cr (#_cairo_create surface)))
                      (unwind-protect
                           (progn
                             (#_cairo_set_source_rgb cr 0d0 0d0 0d0)
                             (#_cairo_set_font_size cr 16d0)
                             (#_cairo_translate cr
                                                (coerce (/ w 2) 'double-float)
                                                (coerce (/ h 2) 'double-float))
                             (loop for phi below (* 2 pi) by (/ pi 10) do
                                   (#_cairo_save cr)
                                   (#_cairo_rotate cr (coerce phi 'double-float))
                                   (#_cairo_move_to cr 50d0 0d0)
                                   (with-cstrs ((s "CCL greets Cairo!"))
                                     (#_cairo_show_text cr s))
                                   (#_cairo_restore cr)))
                        (#_cairo_destroy cr))))
                   ;;
                   (otherwise
                    (format t "~&Unrecognized event type ~D."
                            (pref event :<XE>vent.type))))))))
      (#_XCloseDisplay dpy))))

Which gets you this. Looks familiar?


-- Gilbert Baumann, 2021-02-19