Something went wrong. Try again.
Common Lisp bindings for egui
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213(in-package #:egui)
;;; Apps
(defvar *running-app* nil)
(defclass app () ((pending :initform '()) (pending-lock :initform (sb-thread:make-mutex :name "egui-cl app queue"))) (:documentation "Base class for applications. Define methods on APP-UI, andoptionally APP-LOGIC and APP-ON-EXIT. They are called by name every frame, soredefining them takes effect on the next frame."))
(defgeneric app-ui (app ui) (:documentation "Draw APP's UI into UI. Called every frame on the GUI thread."))
(defgeneric app-logic (app context) (:documentation "Called every frame before APP-UI, with the egui CONTEXT.") (:method ((app app) context) (declare (ignore context)) nil))
(defgeneric app-on-exit (app) (:documentation "Called once when APP's window closes.") (:method ((app app)) nil))
(defclass function-app (app) ((ui-function :initarg :ui-function :reader ui-function)))
(defmethod app-ui ((app function-app) ui) (funcall (ui-function app) ui))
(defun call-reporting-errors (name function) (block nil (handler-bind ((serious-condition (lambda (condition) (report-error condition name (capture-backtrace)) (return)))) (funcall function))))
(defun run-pending (app) (let ((functions (sb-thread:with-mutex ((slot-value app 'pending-lock)) (shiftf (slot-value app 'pending) '())))) (dolist (function (reverse functions)) (call-reporting-errors "call-on-gui-thread" function))))
(defun dispatch-app-ui (app ui) (run-pending app) (app-ui app ui))
(defun call-on-gui-thread (app function) "Run FUNCTION on the GUI thread before APP's next frame, and request thatframe. Use this for changes to state the UI reads that are not a single slotwrite. Never blocks." (sb-thread:with-mutex ((slot-value app 'pending-lock)) (push function (slot-value app 'pending))) (when (eq app *running-app*) (ignore-errors (ffi:%request-repaint))) app)
;;; The GUI thread. winit allows one event loop per process and eframe keeps;;; it in the local storage of the thread that created it, so every run must;;; happen on this one thread.
(defvar *gui-thread* nil)(defvar *gui-jobs* '())(defvar *gui-lock* (sb-thread:make-mutex :name "egui-cl gui thread"))(defvar *gui-wakeup* (sb-thread:make-waitqueue :name "egui-cl gui thread"))
(defun running-app () "The app whose window is open, or NIL." *running-app*)
(defun gui-thread-loop () (loop (let ((job (sb-thread:with-mutex (*gui-lock*) (loop until *gui-jobs* do (sb-thread:condition-wait *gui-wakeup* *gui-lock*)) (pop *gui-jobs*)))) (when (eq job :stop) (return)) (call-reporting-errors "GUI thread job" job))))
(defun ensure-gui-thread () (sb-thread:with-mutex (*gui-lock*) (unless (and *gui-thread* (sb-thread:thread-alive-p *gui-thread*)) (setf *gui-thread* (sb-thread:make-thread #'gui-thread-loop :name "egui-cl GUI")))))
(defun submit-gui-job (job) (sb-thread:with-mutex (*gui-lock*) (setf *gui-jobs* (append *gui-jobs* (list job))) (sb-thread:condition-broadcast *gui-wakeup*)))
(defstruct (completion (:constructor make-completion ())) (lock (sb-thread:make-mutex :name "egui-cl completion")) (wakeup (sb-thread:make-waitqueue)) (done nil) (condition nil))
(defun complete (completion condition) (sb-thread:with-mutex ((completion-lock completion)) (setf (completion-condition completion) condition (completion-done completion) t) (sb-thread:condition-broadcast (completion-wakeup completion))))
(defun wait-for (completion) (sb-thread:with-mutex ((completion-lock completion)) (loop until (completion-done completion) do (sb-thread:condition-wait (completion-wakeup completion) (completion-lock completion)))) (when (completion-condition completion) (error (completion-condition completion))))
(defun run-on-gui-thread (app app-name native-options) (let ((app-label (let ((*print-length* 3) (*print-level* 2)) (prin1-to-string app)))) (with-callback (ui-id (lambda (ui) (dispatch-app-ui app ui)) (format nil "(egui:app-ui ~A)" app-label) :ui) (with-callback (logic-id (lambda (context) (app-logic app context)) (format nil "(egui:app-logic ~A)" app-label) :context) (with-callback (exit-id (lambda (no-handle) (declare (ignore no-handle)) (app-on-exit app)) (format nil "(egui:app-on-exit ~A)" app-label) nil) (let* ((viewport (native-options-viewport native-options)) (inner-size (%viewport-builder-inner-size viewport))) (with-utf8 ((name-pointer name-length) app-name) (with-utf8 ((title-pointer title-length) (%viewport-builder-title viewport)) (check (ffi:%run name-pointer name-length title-pointer title-length (if inner-size (vec2-x inner-size) 0f0) (if inner-size (vec2-y inner-size) 0f0) ui-id logic-id exit-id))))))))))
(defun run-native (app-name native-options app &key wait) "Open a native window showing APP, an instance of APP, configured byNATIVE-OPTIONS. The window title defaults to APP-NAME, which also names thedirectory eframe persists state in.
Returns APP immediately and keeps the REPL free, unless WAIT is true, in whichcase it returns when the window closes and signals any error from the run." (check-type native-options native-options) (check-type app app) (load-library) (ensure-gui-thread) (sb-thread:with-mutex (*gui-lock*) (when *running-app* (error 'already-running-error :message "an app is already running; close it first")) (setf *running-app* app)) (let ((completion (make-completion))) (submit-gui-job (lambda () (let ((failure nil)) (block run (handler-bind ((serious-condition (lambda (condition) (setf failure condition) (report-error condition "egui:run-native" (capture-backtrace)) (return-from run)))) (unwind-protect (run-on-gui-thread app app-name native-options) (setf *running-app* nil)))) (complete completion failure)))) (when wait (wait-for completion)) app))
(defun run-ui-native (app-name native-options ui-function &key wait) "Like RUN-NATIVE for an app that is only a UI-FUNCTION of one argument, theUI. Pass a symbol to have redefinitions of the function take effect live." (run-native app-name native-options (make-instance 'function-app :ui-function ui-function) :wait wait))
(defun request-repaint (&optional target) "Request a new frame. TARGET is an app (the default is the running one) or aCONTEXT received in APP-LOGIC. Callable from any thread." (if (typep target 'context) (check (ffi:%context-request-repaint (context-handle target))) (check (ffi:%request-repaint))) (values))
(defun request-close () "Ask the running app's window to close. Callable from any thread." (check (ffi:%request-close)) (values))
;;; Shutdown and image saving. SBCL refuses to save while other threads run,;;; so the GUI thread is stopped and the library closed before saving.
(defun shutdown (&key (timeout 10)) "Close the running app, stop the GUI thread and unload the shim. Runs beforeSAVE-LISP-AND-DIE. winit allows only one event loop per process, so no app canrun again in this process; a saved image starts fresh." (loop with deadline = (+ (get-internal-real-time) (* timeout internal-time-units-per-second)) while (and *running-app* (< (get-internal-real-time) deadline)) ;; Retried because an app that is still starting cannot take the request yet. do (ignore-errors (request-close)) (sleep 0.01)) (let ((thread *gui-thread*)) (when (and thread (sb-thread:thread-alive-p thread)) (submit-gui-job :stop) (sb-thread:join-thread thread :default nil :timeout timeout))) (setf *gui-thread* nil *gui-jobs* '()) (close-library) (values))
(defun reset-after-image-start () (setf *library* nil *gui-thread* nil *gui-jobs* '() *running-app* nil))
(pushnew 'shutdown sb-ext:*save-hooks*)(pushnew 'reset-after-image-start sb-ext:*init-hooks*)