Compare commits

...

2 Commits

Author SHA1 Message Date
Milan Zamazal cd1a8beeab Change volume of playback ports by default
This seems to be more correct than changing volume of the whole sink.
The default volume of the sink is 1 (100) and alsamixer also changes
volume of the ports.
2022-06-13 21:27:29 +02:00
Milan Zamazal 2d76e595a9 Cache object bindings 2022-06-13 21:27:28 +02:00
2 changed files with 28 additions and 16 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)))
@ -145,6 +150,13 @@ version, call `pw-lib-refresh' first."
"Return a PipeWire object that is the current default audio sink." "Return a PipeWire object that is the current default audio sink."
(pw-lib-get-object (cdr (assoc "default.audio.sink" (pw-lib-default-nodes))))) (pw-lib-get-object (cdr (assoc "default.audio.sink" (pw-lib-default-nodes)))))
(defun pw-lib-default-playback-ports ()
"Return list of PipeWire objects that are default playback ports."
(cl-remove-if-not #'(lambda (o)
(if-let ((name (pw-lib-object-value o "port.name")))
(string-match "^playback" name)))
(pw-lib-children (pw-lib-object-id (pw-lib-default-audio-sink)) "Port")))
(defun pw-lib--volume-% (volume) (defun pw-lib--volume-% (volume)
(when volume (when volume
(round (* 100 volume)))) (round (* 100 volume))))
@ -233,7 +245,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"))
@ -165,7 +164,8 @@ The indicator is displayed only on graphical terminals."
(not (member (pw-lib-object-type object) allowed-types))) (not (member (pw-lib-object-type object) allowed-types)))
(setq object nil)) (setq object nil))
(when (and use-default-p (not object)) (when (and use-default-p (not object))
(setq object (pw-lib-default-audio-sink))) (setq object (or (car (pw-lib-default-playback-ports))
(pw-lib-default-audio-sink))))
object)) object))
(defvar pw-ui--osd-timer nil) (defvar pw-ui--osd-timer nil)