2022-07-16 20:25:08 +02:00
|
|
|
;;; pipewire-lib.el --- PipeWire library -*- lexical-binding: t -*-
|
2022-06-04 21:51:45 +02:00
|
|
|
|
|
|
|
;; Copyright (C) 2022 Milan Zamazal <pdm@zamazal.org>
|
|
|
|
|
2022-06-29 21:56:59 +02:00
|
|
|
;; Author: Milan Zamazal <pdm@zamazal.org>
|
2022-07-04 19:03:15 +02:00
|
|
|
;; Version: 1
|
2022-06-29 21:56:59 +02:00
|
|
|
;; 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.
|
2022-07-16 20:25:08 +02:00
|
|
|
;; It abstracts data returned from `pipewire-access' methods and provides
|
2022-06-04 21:51:45 +02:00
|
|
|
;; functions to work with them.
|
|
|
|
;;
|
2022-07-16 20:25:08 +02:00
|
|
|
;; 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
|
|
|
|
2022-07-05 13:30:30 +02:00
|
|
|
;;; Code:
|
|
|
|
|
2022-06-04 21:51:45 +02:00
|
|
|
(require 'cl-lib)
|
2022-07-16 20:25:08 +02:00
|
|
|
(require 'pipewire-access)
|
2022-07-16 20:59:33 +02:00
|
|
|
(require 'pipewire-cli)
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defvar pipewire-lib--accessor (pipewire-cli-accessor))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defvar pipewire-lib--objects '())
|
|
|
|
(defvar pipewire-lib--bindings nil)
|
|
|
|
(defvar pipewire-lib--defaults nil)
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-refresh ()
|
2022-06-04 21:51:45 +02:00
|
|
|
"Clear cache of objects retrieved from PipeWire."
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
|
|
|
2022-07-16 20:25:08 +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.
|
2022-07-16 20:25:08 +02:00
|
|
|
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
|
2022-07-16 20:25:08 +02:00
|
|
|
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))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
2022-07-16 20:25:08 +02:00
|
|
|
version, call `pipewire-lib-refresh' first."
|
|
|
|
(assoc id pipewire-lib--objects))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +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))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--object-info (object)
|
2022-06-04 21:51:45 +02:00
|
|
|
(cdr object))
|
2022-07-02 09:30:44 +02:00
|
|
|
|
2022-07-16 20:25:08 +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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(or (cdr (assoc key (pipewire-lib--object-info object)))
|
2022-06-04 21:51:45 +02:00
|
|
|
default))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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.
|
2022-07-16 20:25:08 +02:00
|
|
|
The corresponding values can be retrieved using `pipewire-lib-object-value'
|
2022-07-02 09:43:54 +02:00
|
|
|
function."
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-remove-if-not #'stringp (mapcar #'car (pipewire-lib--object-info object))))
|
2022-07-02 09:43:54 +02:00
|
|
|
|
2022-07-16 20:25:08 +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\", ..."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-object-value object 'type))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--profile-name (profile)
|
2022-06-15 21:31:24 +02:00
|
|
|
(cdr (or (assoc "description" profile)
|
|
|
|
(assoc "name" profile))))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-current-profile (device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
"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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--profile-name (pipewire-access-current-profile pipewire-lib--accessor device-id)))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-profiles (device-id)
|
2022-06-15 21:31:24 +02:00
|
|
|
"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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(mapcar #'pipewire-lib--profile-name (pipewire-access-profiles pipewire-lib--accessor device-id)))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-set-profile (device-id profile)
|
2022-06-15 21:31:24 +02:00
|
|
|
"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
|
2022-07-16 20:25:08 +02:00
|
|
|
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)))
|
2022-06-15 21:31:24 +02:00
|
|
|
(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))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-access-set-profile pipewire-lib--accessor device-id index))))
|
2022-06-15 21:31:24 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-get-object (pipewire-lib-object-value object "node.id")))
|
2022-06-28 20:55:10 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--node (object)
|
|
|
|
(if (equal (pipewire-lib-object-type object) "Node")
|
2022-06-04 21:51:45 +02:00
|
|
|
object
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-parent-node object)))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +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)
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-get-object object-or-id)
|
2022-06-04 21:51:45 +02:00
|
|
|
object-or-id))
|
2022-07-16 20:25:08 +02:00
|
|
|
(node (pipewire-lib--node object))
|
|
|
|
(parameters (pipewire-lib-object-value node 'parameters)))
|
2022-06-05 11:42:10 +02:00
|
|
|
(when (or refresh (not parameters))
|
2022-07-16 20:25:08 +02:00
|
|
|
(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)
|
2022-06-05 11:42:10 +02:00
|
|
|
(assq-delete-all 'parameters (cdr node)))))
|
2022-06-04 21:51:45 +02:00
|
|
|
parameters))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
2022-07-16 20:25:08 +02:00
|
|
|
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)
|
2022-07-16 20:25:08 +02:00
|
|
|
(cons (pipewire-lib-object-value o "node.name") (pipewire-lib-object-id o)))
|
|
|
|
(pipewire-lib-objects "Node"))))
|
|
|
|
(setq pipewire-lib--defaults
|
2022-06-25 07:11:20 +02:00
|
|
|
(cl-remove-if-not #'cdr
|
2022-06-28 21:27:49 +02:00
|
|
|
(mapcar (lambda (d)
|
|
|
|
(cons (car d) (cdr (assoc (cdr d) nodes))))
|
2022-06-25 07:11:20 +02:00
|
|
|
defaults)))))
|
2022-07-16 20:25:08 +02:00
|
|
|
pipewire-lib--defaults)
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +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
|
|
|
|
2022-07-16 20:25: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
|
2022-07-16 20:25:08 +02:00
|
|
|
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)
|
2022-07-16 20:25:08 +02:00
|
|
|
(let ((o-id (pipewire-lib-object-id o)))
|
2022-06-28 21:27:49 +02:00
|
|
|
(mapcar (lambda (p)
|
|
|
|
(cons o-id (cdr p)))
|
2022-07-16 20:25:08 +02:00
|
|
|
(cl-remove-if-not #'numberp (pipewire-lib--object-info o)
|
2022-06-28 21:27:49 +02:00
|
|
|
:key #'cdr))))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-objects))))))
|
2022-06-13 20:01:21 +02:00
|
|
|
|
2022-07-16 20:25:08 +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
|
2022-07-16 20:25:08 +02:00
|
|
|
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))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib-bindings))))))
|
2022-06-04 21:51:45 +02:00
|
|
|
(when type
|
2022-07-16 20:25:08 +02:00
|
|
|
(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))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--node-ports (node &optional regexp)
|
2022-06-14 20:15:08 +02:00
|
|
|
(when node
|
2022-07-16 20:25:08 +02:00
|
|
|
(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)
|
2022-07-16 20:25:08 +02:00
|
|
|
(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))))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--default-node "default.audio.sink"))
|
2022-06-14 20:15:08 +02:00
|
|
|
|
2022-07-16 20:25: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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--default-node "default.audio.source"))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-default-playback-ports ()
|
2022-06-13 20:04:20 +02:00
|
|
|
"Return list of PipeWire objects that are default playback ports."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--node-ports (pipewire-lib-default-audio-sink) "^playback"))
|
2022-06-14 20:15:08 +02:00
|
|
|
|
2022-07-16 20:25: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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--node-ports (pipewire-lib-default-audio-source) "^capture"))
|
2022-06-13 20:04:20 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--volume-% (volume)
|
2022-06-04 21:51:45 +02:00
|
|
|
(when volume
|
|
|
|
(round (* 100 volume))))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--volume-float (volume)
|
2022-06-04 21:51:45 +02:00
|
|
|
(/ (float volume) 100))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
2022-07-16 20:25:08 +02:00
|
|
|
(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)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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.
|
2022-06-05 11:42:10 +02:00
|
|
|
If REFRESH is non-nil then retrive fresh information from PipeWire
|
|
|
|
rather than using cached data to obtain the result."
|
2022-06-15 19:50:24 +02:00
|
|
|
(cl-destructuring-bind (_node-p parameters monitor-p _node-id _port-id)
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--object-parameters object refresh)
|
2022-06-04 21:51:45 +02:00
|
|
|
(eq (cdr (assoc (if monitor-p "monitorMute" "mute") parameters)) 'true)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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.
|
2022-06-05 11:42:10 +02:00
|
|
|
If REFRESH is non-nil then retrive fresh information from PipeWire
|
|
|
|
rather than using cached data to obtain the result."
|
2022-06-15 19:50:24 +02:00
|
|
|
(cl-destructuring-bind (_node-p _parameters monitor-p node-id _port-id)
|
2022-07-16 20:25:08 +02:00
|
|
|
(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")))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-access-set-properties pipewire-lib--accessor node-id (list (cons property value)))
|
2022-06-04 21:51:45 +02:00
|
|
|
mute)))
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(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.
|
2022-06-05 11:42:10 +02:00
|
|
|
If REFRESH is non-nil then retrive fresh information from PipeWire
|
|
|
|
rather than using cached data to obtain the result."
|
2022-06-15 19:50:24 +02:00
|
|
|
(cl-destructuring-bind (node-p parameters monitor-p _node-id port-id)
|
2022-07-16 20:25:08 +02:00
|
|
|
(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
|
|
|
|
2022-07-16 20:25:08 +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.
|
2022-06-06 20:16:39 +02:00
|
|
|
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)
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--object-parameters object)
|
2022-06-04 21:51:45 +02:00
|
|
|
(let* ((property (cond
|
|
|
|
(node-p "volume")
|
|
|
|
(monitor-p "monitorVolumes")
|
|
|
|
(t "channelVolumes")))
|
2022-07-16 20:25:08 +02:00
|
|
|
(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))))
|
2022-06-06 20:16:39 +02:00
|
|
|
(if single-p
|
|
|
|
(cl-substitute float-volume nil orig-value
|
|
|
|
:test #'always :start port-id :count 1)
|
|
|
|
(make-list (length orig-value) float-volume))))))
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-access-set-properties pipewire-lib--accessor node-id (list (cons property value))))))
|
2022-06-04 21:51:45 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib--set-default-node (object stored-p)
|
2022-06-04 21:51:45 +02:00
|
|
|
(let ((suffix (mapconcat #'downcase
|
2022-07-16 20:25:08 +02:00
|
|
|
(split-string (pipewire-lib-object-value object "media.class") "/")
|
2022-06-04 21:51:45 +02:00
|
|
|
"."))
|
|
|
|
(prefix (if stored-p "default.configured." "default."))
|
2022-07-16 20:25:08 +02:00
|
|
|
(node-name (pipewire-lib-object-value object "node.name")))
|
|
|
|
(pipewire-access-set-default pipewire-lib--accessor (concat prefix suffix) node-name)))
|
2022-06-11 07:56:38 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(defun pipewire-lib-set-default (object stored-p)
|
2022-06-11 07:56:38 +02:00
|
|
|
"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."
|
2022-07-16 20:25:08 +02:00
|
|
|
(pcase (pipewire-lib-object-type object)
|
2022-06-11 07:56:38 +02:00
|
|
|
("Device"
|
2022-07-16 20:25:08 +02:00
|
|
|
(dolist (node (pipewire-lib-children (pipewire-lib-object-id object) "Node"))
|
|
|
|
(pipewire-lib--set-default-node node stored-p)))
|
2022-06-11 07:56:38 +02:00
|
|
|
("Node"
|
2022-07-16 20:25:08 +02:00
|
|
|
(pipewire-lib--set-default-node object stored-p))
|
2022-06-11 07:56:38 +02:00
|
|
|
(_
|
2022-07-04 19:17:02 +02:00
|
|
|
(error "Cannot set this kind of object as default"))))
|
2022-07-02 09:30:44 +02:00
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
(provide 'pipewire-lib)
|
2022-06-29 21:55:53 +02:00
|
|
|
|
2022-07-06 10:20:27 +02:00
|
|
|
;; Local Variables:
|
|
|
|
;; checkdoc-force-docstrings-flag: nil
|
|
|
|
;; End:
|
|
|
|
|
2022-07-16 20:25:08 +02:00
|
|
|
;;; pipewire-lib.el ends here
|