+;;; Cairo interaction
+
+#?(pkg-exists-p "gtk+-2.0" :atleast-version "2.8.0")
+(progn
+ (defbinding cairo-create () cairo:context
+ (drawable drawable))
+
+ (defmacro with-cairo-context ((cr drawable) &body body)
+ `(let ((,cr (cairo-create ,drawable)))
+ (unwind-protect
+ (progn ,@body)
+ (invalidate-instance ,cr t))))
+
+ (defbinding cairo-set-source-color () nil
+ (cr cairo:context)
+ (color color))
+
+ (defbinding cairo-set-source-pixbuf () nil
+ (cr cairo:context)
+ (pixbuf pixbuf)
+ (x double-float)
+ (y double-float))
+
+ (defbinding cairo-rectangle () nil
+ (cr cairo:context)
+ (rectangle rectangle))
+
+;; (defbinding cairo-region () nil
+;; (cr cairo:context)
+;; (region region))
+)
+
+
+
+;;; Multi-threading support
+
+#+sbcl
+(progn
+ (defvar *global-lock* (sb-thread:make-mutex :name "global GDK lock"))
+ (let ((recursive-level 0))
+ (defun threads-enter ()
+ (if (eq (sb-thread:mutex-value *global-lock*) sb-thread:*current-thread*)
+ (incf recursive-level)
+ (sb-thread:get-mutex *global-lock*)))
+
+ (defun threads-leave (&optional flush-p)
+ (cond
+ ((zerop recursive-level)
+ (when flush-p
+ (display-flush))
+ (sb-thread:release-mutex *global-lock*))
+ (t (decf recursive-level)))))
+
+ (define-callback %enter-fn nil ()
+ (threads-enter))
+
+ (define-callback %leave-fn nil ()
+ (threads-leave))
+
+ (defbinding threads-set-lock-functions (&optional) nil
+ (%enter-fn callback)
+ (%leave-fn callback))
+
+ (defmacro with-global-lock (&body body)
+ `(progn
+ (threads-enter)
+ (unwind-protect
+ ,@body
+ (threads-leave t)))))