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*
(apply 'format t control-string args)))
(defun event-has-live-stream-p (event)
"Return non-nil if EVENT has a live stream."
(defun event-stream-states (event)
"Return a alist mapping ids to stream states."
(loop for obj across event
for info = (gethash "info" obj)
when (and (hash-table-p info)
(equal (gethash "type" obj) "PipeWire:Interface:Node")
(equal (gethash "state" info) "running")
(or (not (equal (gethash "n-input-ports" info) 0))
(not (equal (gethash "n-output-ports" info) 0))))
do (return t)))
for id = (gethash "id" obj)
for running = (and (hash-table-p info)
(equal (gethash "type" obj) "PipeWire:Interface:Node")
(equal (gethash "state" info) "running")
(or (not (equal (gethash "n-input-ports" info) 0))
(not (equal (gethash "n-output-ports" info) 0))))
collect (cons id running)))
(defvar *inhibitor-process* nil
"The systemd-inhibit process object.")
@ -54,14 +55,33 @@
(setq *inhibitor-process* nil)
(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)
"Process one event from pw-dump."
(let ((has-live-stream (event-has-live-stream-p event)))
(cond
((and has-live-stream (not (inhibitor-running-p)))
(start-inhibitor))
((and (not has-live-stream) (inhibitor-running-p))
(stop-inhibitor)))))
(let ((live-streams (event-stream-states event)))
(dolist (entry live-streams)
(destructuring-bind (id . state) entry
(if state
(progn
(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 ()
"Print a help message and then exit."