Cache object bindings

This commit is contained in:
Milan Zamazal 2022-06-13 20:01:21 +02:00
parent 94bbd26205
commit 2d76e595a9
2 changed files with 19 additions and 15 deletions

View File

@ -30,10 +30,12 @@
(defvar pw-lib--accessor (pw-cli-accessor)) (defvar pw-lib--accessor (pw-cli-accessor))
(defvar pw-lib--objects '()) (defvar pw-lib--objects '())
(defvar pw-lib--bindings nil)
(defun pw-lib-refresh () (defun pw-lib-refresh ()
"Clear cache of objects retrieved from PipeWire." "Clear cache of objects retrieved from PipeWire."
(setq pw-lib--objects (pw-access-objects pw-lib--accessor))) (setq pw-lib--objects (pw-access-objects pw-lib--accessor)
pw-lib--bindings nil))
(defun pw-lib-objects (&optional type) (defun pw-lib-objects (&optional type)
"Return a list of PipeWire objects. "Return a list of PipeWire objects.
@ -119,23 +121,26 @@ An association lists with elements of the form (PARENT . CHILD) is
returned where PARENT and CHILD are numeric ids of PipeWire objects. returned where PARENT and CHILD are numeric ids of PipeWire objects.
Note that PipeWire data is cached, if you need its up-to-date Note that PipeWire data is cached, if you need its up-to-date
version, call `pw-lib-refresh' first." version, call `pw-lib-refresh' first."
(apply #'nconc (mapcar #'(lambda (o) (or pw-lib--bindings
(let ((o-id (pw-lib-object-id o))) (setq pw-lib--bindings
(mapcar #'(lambda (p) (apply #'nconc
(cons o-id (cdr p))) (mapcar #'(lambda (o)
(cl-remove-if-not #'numberp (pw-lib--object-info o) (let ((o-id (pw-lib-object-id o)))
:key #'cdr)))) (mapcar #'(lambda (p)
(pw-lib-objects)))) (cons o-id (cdr p)))
(cl-remove-if-not #'numberp (pw-lib--object-info o)
:key #'cdr))))
(pw-lib-objects))))))
(defun pw-lib-children (id bindings &optional type) (defun pw-lib-children (id &optional type)
"Return child objects of the object identified by numeric PipeWire ID. "Return child objects of the object identified by numeric PipeWire ID.
BINDINGS are object bindings as returned from `pw-lib-bindings'.
If a string TYPE is specified then only children of the given PipeWire If a string TYPE is specified then only children of the given PipeWire
type are returned. type are returned.
Note that PipeWire data is cached, if you need its up-to-date Note that PipeWire data is cached, if you need its up-to-date
version, call `pw-lib-refresh' first." version, call `pw-lib-refresh' first."
(let ((children (mapcar #'pw-lib-get-object (let ((children (mapcar #'pw-lib-get-object
(mapcar #'car (cl-remove-if #'(lambda (b) (/= (cdr b) id)) bindings))))) (mapcar #'car (cl-remove-if #'(lambda (b) (/= (cdr b) id))
(pw-lib-bindings))))))
(when type (when type
(setq children (cl-remove-if-not #'(lambda (o) (equal (pw-lib-object-type o) type)) (setq children (cl-remove-if-not #'(lambda (o) (equal (pw-lib-object-type o) type))
children))) children)))
@ -233,7 +238,7 @@ If STORED-P is non-nil, set the stored default sink or source,
otherwise set the current default sink or source." otherwise set the current default sink or source."
(pcase (pw-lib-object-type object) (pcase (pw-lib-object-type object)
("Device" ("Device"
(dolist (node (pw-lib-children (pw-lib-object-id object) (pw-lib-bindings) "Node")) (dolist (node (pw-lib-children (pw-lib-object-id object) "Node"))
(pw-lib--set-default-node node stored-p))) (pw-lib--set-default-node node stored-p)))
("Node" ("Node"
(pw-lib--set-default-node object stored-p)) (pw-lib--set-default-node object stored-p))

View File

@ -137,16 +137,15 @@ The indicator is displayed only on graphical terminals."
(error "Not in a PipeWire buffer")) (error "Not in a PipeWire buffer"))
(pw-lib-refresh) (pw-lib-refresh)
(let ((inhibit-read-only t) (let ((inhibit-read-only t)
(bindings (pw-lib-bindings))
(default-ids (mapcar #'cdr (pw-lib-default-nodes))) (default-ids (mapcar #'cdr (pw-lib-default-nodes)))
(current-line (count-lines (point-min) (min (1+ (point)) (point-max))))) (current-line (count-lines (point-min) (min (1+ (point)) (point-max)))))
(erase-buffer) (erase-buffer)
(insert (pw-ui--label "Devices") "\n") (insert (pw-ui--label "Devices") "\n")
(dolist (device (pw-lib-objects "Device")) (dolist (device (pw-lib-objects "Device"))
(pw-ui--insert-line (pw-ui--object-label device default-ids) device) (pw-ui--insert-line (pw-ui--object-label device default-ids) device)
(dolist (node (pw-lib-children (pw-lib-object-id device) bindings "Node")) (dolist (node (pw-lib-children (pw-lib-object-id device) "Node"))
(pw-ui--insert-line (concat " " (pw-ui--object-label node default-ids)) node) (pw-ui--insert-line (concat " " (pw-ui--object-label node default-ids)) node)
(dolist (port (pw-lib-children (pw-lib-object-id node) bindings "Port")) (dolist (port (pw-lib-children (pw-lib-object-id node) "Port"))
(pw-ui--insert-line (concat " " (pw-ui--object-label port default-ids)) port)))) (pw-ui--insert-line (concat " " (pw-ui--object-label port default-ids)) port))))
(insert (pw-ui--label "Clients") "\n") (insert (pw-ui--label "Clients") "\n")
(dolist (client (pw-lib-objects "Client")) (dolist (client (pw-lib-objects "Client"))