Fix inhibit-sleep-for-audio.lisp

This commit is contained in:
2025-10-13 14:50:30 -07:00
parent 99f991728b
commit e0dad7f7dd

View File

@ -17,16 +17,17 @@
(when *debug-output* (when *debug-output*
(apply 'format t control-string args))) (apply 'format t control-string args)))
(defun event-has-live-stream-p (event) (defun event-stream-states (event)
"Return non-nil if EVENT has a live stream." "Return a alist mapping ids to stream states."
(loop for obj across event (loop for obj across event
for info = (gethash "info" obj) for info = (gethash "info" obj)
when (and (hash-table-p info) for id = (gethash "id" obj)
(equal (gethash "type" obj) "PipeWire:Interface:Node") for running = (and (hash-table-p info)
(equal (gethash "state" info) "running") (equal (gethash "type" obj) "PipeWire:Interface:Node")
(or (not (equal (gethash "n-input-ports" info) 0)) (equal (gethash "state" info) "running")
(not (equal (gethash "n-output-ports" info) 0)))) (or (not (equal (gethash "n-input-ports" info) 0))
do (return t))) (not (equal (gethash "n-output-ports" info) 0))))
collect (cons id running)))
(defvar *inhibitor-process* nil (defvar *inhibitor-process* nil
"The systemd-inhibit process object.") "The systemd-inhibit process object.")
@ -54,14 +55,33 @@
(setq *inhibitor-process* nil) (setq *inhibitor-process* nil)
(debug-format "Stopped inhibitor process~%")) (debug-format "Stopped inhibitor process~%"))
(defvar *running-streams* (make-hash-table :test #'eql)
"Hash table (set) of running streams. This maps ids to the symbol t.")
(defun have-running-streams-p ()
"Return non-nil if there are running streams."
(not (zerop (hash-table-count *running-streams*))))
(defun process-event (event) (defun process-event (event)
"Process one event from pw-dump." "Process one event from pw-dump."
(let ((has-live-stream (event-has-live-stream-p event))) (let ((live-streams (event-stream-states event)))
(cond (dolist (entry live-streams)
((and has-live-stream (not (inhibitor-running-p))) (destructuring-bind (id . state) entry
(start-inhibitor)) (if state
((and (not has-live-stream) (inhibitor-running-p)) (progn
(stop-inhibitor))))) (when (not (gethash id *running-streams*))
(debug-format "Stream ~A started~%" id))
(setf (gethash id *running-streams*) t))
(progn
(when (gethash id *running-streams*)
(debug-format "Stream ~A stopped~%" id))
(remhash id *running-streams*)))))
(let ((has-live-stream (have-running-streams-p)))
(cond
((and has-live-stream (not (inhibitor-running-p)))
(start-inhibitor))
((and (not has-live-stream) (inhibitor-running-p))
(stop-inhibitor))))))
(defun print-help-and-exit () (defun print-help-and-exit ()
"Print a help message and then exit." "Print a help message and then exit."