pipewire-0/pipewire-lib.el

339 lines
14 KiB
EmacsLisp
Raw Permalink Normal View History

;;; pipewire-lib.el --- PipeWire library -*- lexical-binding: t -*-
2022-06-04 21:51:45 +02:00
;; Copyright (C) 2022 Milan Zamazal <pdm@zamazal.org>
;; Author: Milan Zamazal <pdm@zamazal.org>
;; Version: 1
;; Package-Requires: ((emacs "28.1"))
;; Keywords: multimedia
;; URL: https://git.zamazal.org/pdm/pipewire-0
2022-06-04 21:51:45 +02:00
;; COPYRIGHT NOTICE
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-06-28 21:25:20 +02:00
;;; Commentary:
;;
2022-06-04 21:51:45 +02:00
;; Backend-independent library to access PipeWire functionality.
;; It abstracts data returned from `pipewire-access' methods and provides
2022-06-04 21:51:45 +02:00
;; functions to work with them.
;;
;; pipewire-lib caches data retrieved from PipeWire and uses the cached
2022-07-16 20:29:24 +02:00
;; data. The cache can be invalidated by calling `pipewire-lib-refresh'.
2022-06-04 21:51:45 +02:00
;;; Code:
2022-06-04 21:51:45 +02:00
(require 'cl-lib)
(require 'pipewire-access)
(require 'pipewire-cli)
2022-06-04 21:51:45 +02:00
(defvar pipewire-lib--accessor (pipewire-cli-accessor))
2022-06-04 21:51:45 +02:00
(defvar pipewire-lib--objects '())
(defvar pipewire-lib--bindings nil)
(defvar pipewire-lib--defaults nil)
2022-06-04 21:51:45 +02:00
(defun pipewire-lib-refresh ()
2022-06-04 21:51:45 +02:00
"Clear cache of objects retrieved from PipeWire."
(setq pipewire-lib--objects (pipewire-access-objects pipewire-lib--accessor)
pipewire-lib--bindings nil
pipewire-lib--defaults nil))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib-objects (&optional type)
2022-06-04 21:51:45 +02:00
"Return a list of PipeWire objects.
TYPE is a string identifying PipeWire objects types (e.g. \"Device\",
\"Node\", \"Port\", \"Client\", ...). If specified, return only
objects of the given type.
The format of the list elements is unspecified, use pipewire-lib functions
2022-06-04 21:51:45 +02:00
to access their data.
Note that PipeWire data is cached, if you need its up-to-date
version, call `pipewire-lib-refresh' first."
(unless pipewire-lib--objects
(pipewire-lib-refresh))
(let ((objects pipewire-lib--objects))
2022-06-04 21:51:45 +02:00
(when type
(setq objects (cl-remove-if-not
2022-06-28 21:27:49 +02:00
(lambda (o) (string= (cdr (assq 'type (cdr o))) type))
2022-06-04 21:51:45 +02:00
objects)))
objects))
(defun pipewire-lib-get-object (id)
2022-06-04 21:51:45 +02:00
"Return PipeWire object identified by ID.
If such an object doesn't exist, return nil.
Note that PipeWire data is cached, if you need its up-to-date
version, call `pipewire-lib-refresh' first."
(assoc id pipewire-lib--objects))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib-object-id (object)
2022-06-04 21:51:45 +02:00
"Return id of the given PipeWire OBJECT."
(car object))
(defun pipewire-lib--object-info (object)
2022-06-04 21:51:45 +02:00
(cdr object))
2022-07-02 09:30:44 +02:00
(defun pipewire-lib-object-value (object key &optional default)
2022-06-04 21:51:45 +02:00
"Return PipeWire OBJECT value identified by KEY.
KEY is a string corresponding to a PipeWire value identifier.
If the given KEY doesn't exist in OBJECT, return DEFAULT."
(or (cdr (assoc key (pipewire-lib--object-info object)))
2022-06-04 21:51:45 +02:00
default))
(defun pipewire-lib-properties (object)
2022-07-02 09:43:54 +02:00
"Return names of PipeWire OBJECT properties.
The returned value is a list of strings.
The corresponding values can be retrieved using `pipewire-lib-object-value'
2022-07-02 09:43:54 +02:00
function."
(cl-remove-if-not #'stringp (mapcar #'car (pipewire-lib--object-info object))))
2022-07-02 09:43:54 +02:00
(defun pipewire-lib-object-type (object)
2022-06-04 21:51:45 +02:00
"Return PipeWire type of OBJECT as a string.
E.g. \"Device\", \"Node\", \"Port\", \"Client\", ..."
(pipewire-lib-object-value object 'type))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib--profile-name (profile)
(cdr (or (assoc "description" profile)
(assoc "name" profile))))
(defun pipewire-lib-current-profile (device-id)
"Return the current profile name of the given device.
DEVICE-ID is the numeric id of the device.
The returned profile name is a string, or nil if it cannot be found."
(pipewire-lib--profile-name (pipewire-access-current-profile pipewire-lib--accessor device-id)))
(defun pipewire-lib-profiles (device-id)
"Return list of available profiles of the given device.
DEVICE-ID is the numeric id of the device.
A list of strings (possibly empty) is returned."
(mapcar #'pipewire-lib--profile-name (pipewire-access-profiles pipewire-lib--accessor device-id)))
(defun pipewire-lib-set-profile (device-id profile)
"Set the profile of the given device.
DEVICE-ID is the numeric id of the device.
PROFILE is a string name of the profile, it must be one of the values
returned from `pipewire-lib-profiles'."
(let* ((all-profiles (pipewire-access-profiles pipewire-lib--accessor device-id))
(properties (cl-find profile all-profiles :key #'pipewire-lib--profile-name :test #'equal)))
(unless properties
(error "Profile %s of device %s not found" profile device-id))
(let ((index (cdr (assoc "index" properties))))
(unless index
(error "Index of %s profile of device %s not found" profile device-id))
(pipewire-access-set-profile pipewire-lib--accessor device-id index))))
(defun pipewire-lib-parent-node (object)
2022-07-04 19:16:12 +02:00
"Return parent node of OBJECT.
2022-06-28 20:55:10 +02:00
This is typically used for ports.
2022-07-04 19:16:12 +02:00
Behavior is undefined if OBJECT has no parent node."
(pipewire-lib-get-object (pipewire-lib-object-value object "node.id")))
2022-06-28 20:55:10 +02:00
(defun pipewire-lib--node (object)
(if (equal (pipewire-lib-object-type object) "Node")
2022-06-04 21:51:45 +02:00
object
(pipewire-lib-parent-node object)))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib--node-parameters (object-or-id &optional refresh)
2022-06-04 21:51:45 +02:00
(let* ((object (if (numberp object-or-id)
(pipewire-lib-get-object object-or-id)
2022-06-04 21:51:45 +02:00
object-or-id))
(node (pipewire-lib--node object))
(parameters (pipewire-lib-object-value node 'parameters)))
(when (or refresh (not parameters))
(setq parameters (pipewire-access-properties pipewire-lib--accessor (pipewire-lib-object-id node)))
2022-06-04 21:51:45 +02:00
(setcdr node (cons (cons 'parameters parameters)
(assq-delete-all 'parameters (cdr node)))))
2022-06-04 21:51:45 +02:00
parameters))
(defun pipewire-lib-default-nodes ()
2022-06-04 21:51:45 +02:00
"Return assignments of PipeWire Nodes to default sinks and sources.
An association lists with elements of the form (KEY . ID) is
returned, where KEY is a string identifying the given kind of
default sink or source as reported by PipeWire and ID is the
corresponding PipeWire node numeric id.
Note that PipeWire data is cached, if you need its up-to-date
version, call `pipewire-lib-refresh' first."
(unless pipewire-lib--defaults
(let ((defaults (pipewire-access-defaults pipewire-lib--accessor))
2022-06-28 21:27:49 +02:00
(nodes (mapcar (lambda (o)
(cons (pipewire-lib-object-value o "node.name") (pipewire-lib-object-id o)))
(pipewire-lib-objects "Node"))))
(setq pipewire-lib--defaults
(cl-remove-if-not #'cdr
2022-06-28 21:27:49 +02:00
(mapcar (lambda (d)
(cons (car d) (cdr (assoc (cdr d) nodes))))
defaults)))))
pipewire-lib--defaults)
2022-06-04 21:51:45 +02:00
(defun pipewire-lib--default-node (key)
(pipewire-lib-get-object (cdr (assoc key (pipewire-lib-default-nodes)))))
2022-06-14 20:15:08 +02:00
(defun pipewire-lib-bindings ()
2022-06-04 21:51:45 +02:00
"Return bindings between PipeWire objects.
An association lists with elements of the form (PARENT . CHILD) is
returned where PARENT and CHILD are numeric ids of PipeWire objects.
Note that PipeWire data is cached, if you need its up-to-date
version, call `pipewire-lib-refresh' first."
(or pipewire-lib--bindings
(setq pipewire-lib--bindings
2022-06-13 20:01:21 +02:00
(apply #'nconc
2022-06-28 21:27:49 +02:00
(mapcar (lambda (o)
(let ((o-id (pipewire-lib-object-id o)))
2022-06-28 21:27:49 +02:00
(mapcar (lambda (p)
(cons o-id (cdr p)))
(cl-remove-if-not #'numberp (pipewire-lib--object-info o)
2022-06-28 21:27:49 +02:00
:key #'cdr))))
(pipewire-lib-objects))))))
2022-06-13 20:01:21 +02:00
(defun pipewire-lib-children (id &optional type)
2022-06-04 21:51:45 +02:00
"Return child objects of the object identified by numeric PipeWire ID.
If a string TYPE is specified then only children of the given PipeWire
type are returned.
Note that PipeWire data is cached, if you need its up-to-date
version, call `pipewire-lib-refresh' first."
(let ((children (mapcar #'pipewire-lib-get-object
2022-06-28 21:27:49 +02:00
(mapcar #'car (cl-remove-if (lambda (b) (/= (cdr b) id))
(pipewire-lib-bindings))))))
2022-06-04 21:51:45 +02:00
(when type
(setq children (cl-remove-if-not (lambda (o) (equal (pipewire-lib-object-type o) type))
2022-06-04 21:51:45 +02:00
children)))
children))
(defun pipewire-lib--node-ports (node &optional regexp)
2022-06-14 20:15:08 +02:00
(when node
(let ((ports (pipewire-lib-children (pipewire-lib-object-id node) "Port")))
2022-06-14 20:15:08 +02:00
(if regexp
2022-06-28 21:27:49 +02:00
(cl-delete-if-not (lambda (o)
(if-let ((name (pipewire-lib-object-value o "port.name")))
2022-06-28 21:27:49 +02:00
(string-match regexp name)))
2022-06-14 20:15:08 +02:00
ports)
ports))))
(defun pipewire-lib-default-audio-sink ()
2022-06-04 21:51:45 +02:00
"Return a PipeWire object that is the current default audio sink."
(pipewire-lib--default-node "default.audio.sink"))
2022-06-14 20:15:08 +02:00
(defun pipewire-lib-default-audio-source ()
2022-06-14 20:15:08 +02:00
"Return a PipeWire object that is the current default audio source."
(pipewire-lib--default-node "default.audio.source"))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib-default-playback-ports ()
"Return list of PipeWire objects that are default playback ports."
(pipewire-lib--node-ports (pipewire-lib-default-audio-sink) "^playback"))
2022-06-14 20:15:08 +02:00
(defun pipewire-lib-default-capture-ports ()
2022-06-14 20:15:08 +02:00
"Return list of PipeWire objects that are default capture ports."
(pipewire-lib--node-ports (pipewire-lib-default-audio-source) "^capture"))
(defun pipewire-lib--volume-% (volume)
2022-06-04 21:51:45 +02:00
(when volume
(round (* 100 volume))))
(defun pipewire-lib--volume-float (volume)
2022-06-04 21:51:45 +02:00
(/ (float volume) 100))
(defun pipewire-lib--object-parameters (object &optional refresh)
(let* ((node-p (equal (pipewire-lib-object-type object) "Node"))
(parameters (pipewire-lib--node-parameters object refresh))
2022-06-04 21:51:45 +02:00
(monitor-p (unless node-p
(equal (pipewire-lib-object-value object "port.monitor") "true")))
(node-id (pipewire-lib-object-id (pipewire-lib--node object)))
2022-06-04 21:51:45 +02:00
(port-id (unless node-p
(pipewire-lib-object-value object "port.id"))))
2022-06-04 21:51:45 +02:00
(list node-p parameters monitor-p node-id port-id)))
(defun pipewire-lib-muted-p (object &optional refresh)
2022-07-04 19:16:12 +02:00
"Return whether the given PipeWire OBJECT is muted.
2022-06-04 21:51:45 +02:00
Applicable only to Nodes and Ports.
If REFRESH is non-nil then retrive fresh information from PipeWire
rather than using cached data to obtain the result."
(cl-destructuring-bind (_node-p parameters monitor-p _node-id _port-id)
(pipewire-lib--object-parameters object refresh)
2022-06-04 21:51:45 +02:00
(eq (cdr (assoc (if monitor-p "monitorMute" "mute") parameters)) 'true)))
(defun pipewire-lib-toggle-mute (object &optional refresh)
2022-06-04 21:51:45 +02:00
"Toggle mute status of the given PipeWire OBJECT.
Return the new boolean mute status of OBJECT.
Applicable only to Nodes and Ports.
If REFRESH is non-nil then retrive fresh information from PipeWire
rather than using cached data to obtain the result."
(cl-destructuring-bind (_node-p _parameters monitor-p node-id _port-id)
(pipewire-lib--object-parameters object refresh)
(let* ((mute (not (pipewire-lib-muted-p object)))
2022-06-04 21:51:45 +02:00
(property (if monitor-p "monitorMute" "mute"))
(value (if mute "true" "false")))
(pipewire-access-set-properties pipewire-lib--accessor node-id (list (cons property value)))
2022-06-04 21:51:45 +02:00
mute)))
(defun pipewire-lib-volume (object &optional refresh)
2022-07-04 19:16:12 +02:00
"Return volume of the given PipeWire OBJECT.
2022-06-04 21:51:45 +02:00
The returned value is an integer in the range 0-100.
Applicable only to Nodes and Ports.
If REFRESH is non-nil then retrive fresh information from PipeWire
rather than using cached data to obtain the result."
(cl-destructuring-bind (node-p parameters monitor-p _node-id port-id)
(pipewire-lib--object-parameters object refresh)
(pipewire-lib--volume-%
2022-06-04 21:51:45 +02:00
(if node-p
(cdr (assoc "volume" parameters))
(nth port-id (cdr (assoc (if monitor-p "monitorVolumes" "channelVolumes") parameters)))))))
2022-07-02 09:30:44 +02:00
(defun pipewire-lib-set-volume (volume object &optional single-p)
2022-06-04 21:51:45 +02:00
"Set the volume of PipeWire OBJECT to VOLUME.
VOLUME must be an integer in the range 0-100.
If SINGLE-P is non-nil, set the volume only for a single channel,
otherwise set the volume to the same value for all the related channels."
2022-06-04 21:51:45 +02:00
(cl-destructuring-bind (node-p parameters monitor-p node-id port-id)
(pipewire-lib--object-parameters object)
2022-06-04 21:51:45 +02:00
(let* ((property (cond
(node-p "volume")
(monitor-p "monitorVolumes")
(t "channelVolumes")))
(float-volume (pipewire-lib--volume-float volume))
2022-06-04 21:51:45 +02:00
(value (if node-p
float-volume
(let ((orig-value (cdr (assoc property parameters))))
(if single-p
(cl-substitute float-volume nil orig-value
:test #'always :start port-id :count 1)
(make-list (length orig-value) float-volume))))))
(pipewire-access-set-properties pipewire-lib--accessor node-id (list (cons property value))))))
2022-06-04 21:51:45 +02:00
(defun pipewire-lib--set-default-node (object stored-p)
2022-06-04 21:51:45 +02:00
(let ((suffix (mapconcat #'downcase
(split-string (pipewire-lib-object-value object "media.class") "/")
2022-06-04 21:51:45 +02:00
"."))
(prefix (if stored-p "default.configured." "default."))
(node-name (pipewire-lib-object-value object "node.name")))
(pipewire-access-set-default pipewire-lib--accessor (concat prefix suffix) node-name)))
(defun pipewire-lib-set-default (object stored-p)
"Set PipeWire OBJECT as the default sink or source.
If STORED-P is non-nil, set the stored default sink or source,
otherwise set the current default sink or source."
(pcase (pipewire-lib-object-type object)
("Device"
(dolist (node (pipewire-lib-children (pipewire-lib-object-id object) "Node"))
(pipewire-lib--set-default-node node stored-p)))
("Node"
(pipewire-lib--set-default-node object stored-p))
(_
(error "Cannot set this kind of object as default"))))
2022-07-02 09:30:44 +02:00
(provide 'pipewire-lib)
;; Local Variables:
;; checkdoc-force-docstrings-flag: nil
;; End:
;;; pipewire-lib.el ends here