[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/bluetooth 1254ec44d2 24/32: add signal handlers for pai
From: |
Stefan Kangas |
Subject: |
[elpa] externals/bluetooth 1254ec44d2 24/32: add signal handlers for paired devices |
Date: |
Mon, 3 Jan 2022 15:11:08 -0500 (EST) |
branch: externals/bluetooth
commit 1254ec44d24b28c4f48341444e726c0357d9ad56
Author: Raffael Stocker <r.stocker@mnet-mail.de>
Commit: Raffael Stocker <r.stocker@mnet-mail.de>
add signal handlers for paired devices
* bluetooth.el (bluetooth-device-property-set): setter for device property
(bluetooth--make-signal-handler): per-device signal handler
---
bluetooth.el | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 55 insertions(+), 10 deletions(-)
diff --git a/bluetooth.el b/bluetooth.el
index 18d213affe..93162804bd 100644
--- a/bluetooth.el
+++ b/bluetooth.el
@@ -163,11 +163,23 @@ property and state.")
"A bluetooth device. This structure holds all the device
properties."
(id nil :read-only t)
+ signal-handler
properties)
-(defun bluetooth-device-property (device prop-name)
- "Return DEVICE's property PROP-NAME."
- (cl-rest (assoc prop-name (bluetooth-device-properties device))))
+(defun bluetooth-device-property (device property)
+ "Return DEVICE's property PROPERTY."
+ (alist-get property
+ (bluetooth-device-properties device)
+ nil nil #'equal))
+
+(defun bluetooth-device-property-set (device property value)
+ "Set DEVICE's PROPERTY to VALUE."
+ (setf (alist-get property (bluetooth-device-properties device)
+ nil nil #'equal)
+ value))
+
+(gv-define-simple-setter bluetooth-device-property
+ bluetooth-device-property-set)
(defun bluetooth--query-adapters ()
"Return a list of bluetooth adapters."
@@ -190,7 +202,26 @@ properties."
((null value) "no")
(t "yes"))))
-;; TODO install signal handler if the device is paired
+(defun bluetooth--make-signal-handler (device)
+ (let ((adapter (bluetooth-device-property device "Adapter"))
+ (dev-id (bluetooth-device-id device)))
+ (cl-flet ((handler
+ (_interface changed-props invalidated-props)
+ (let ((device (bluetooth--device dev-id)))
+ (mapc (lambda (prop)
+ (cl-destructuring-bind (key
(value)) prop
+ (setf
(bluetooth-device-property device key)
+ value)))
+ (append changed-props
invalidated-props))
+ (bluetooth--print-list))))
+ (dbus-register-signal bluetooth-bluez-bus bluetooth--service
+ (concat adapter "/"
dev-id)
+ (alist-get :properties
bluetooth--interfaces)
+ "PropertiesChanged"
+ #'handler
+ :arg-namespace
+ (alist-get :device
bluetooth--interfaces)))))
+
(defun bluetooth--device-create (adapter dev-id)
"Create a bluetooth device struct for DEV-ID on ADAPTER."
(let* ((path (mapconcat #'identity
@@ -201,21 +232,33 @@ properties."
path
(alist-get
:device bluetooth--interfaces))))
- (make-bluetooth-device :id dev-id :properties props)))
+ (make-bluetooth-device :id dev-id
+ :signal-handler nil
+ :properties props)))
-;; TODO remove signal handler
(defun bluetooth--device-remove (dev-id)
"Remove the device with id DEV-ID from the device info."
+ (let ((device (bluetooth--device dev-id)))
+ (when (bluetooth-device-signal-handler device)
+ (dbus-unregister-object (bluetooth-device-signal-handler device))
+ (setf (bluetooth-device-signal-handler device) nil)))
(remhash dev-id bluetooth--device-info))
(defun bluetooth--device-add (dev-id device)
"Add bluetooth DEVICE with id DEV-ID to device info."
+ (when (bluetooth-device-property device "Paired")
+ (setf (bluetooth-device-signal-handler device)
+ (bluetooth--make-signal-handler device)))
(puthash dev-id device bluetooth--device-info))
(defun bluetooth--device-update (dev-id device)
"Update device info for id DEV-ID with data in DEVICE."
(setf (bluetooth-device-properties (bluetooth--device dev-id))
- (bluetooth-device-properties device)))
+ (bluetooth-device-properties device))
+ (when (and (bluetooth-device-property device "Paired")
+ (null (bluetooth-device-signal-handler device)))
+ (setf (bluetooth-device-signal-handler device)
+ (bluetooth--make-signal-handler device))))
(defun bluetooth--adapter-properties (adapter)
"Return the properties of bluetooth ADAPTER.
@@ -351,7 +394,6 @@ This function only uses the first adapter reported by
Bluez."
(bluetooth--handle-prop-change (alist-get :adapter
bluetooth--interfaces)
info)))
-;; TODO de-register all the signal handlers
(defun bluetooth--cleanup ()
"Clean up when mode buffer is killed."
;; This function is registered as a kill-buffer-hook, so we don't
@@ -363,13 +405,16 @@ This function only uses the first adapter reported by
Bluez."
:object-path bluetooth--own-path)
(mapc #'dbus-unregister-object bluetooth--method-objects)
(dbus-unregister-object bluetooth--adapter-signal)
+ (mapc #'bluetooth--device-remove
+ (hash-table-keys bluetooth--device-info))
+ (setq bluetooth--device-info nil)
+ (remove-hook tabulated-list-revert-hook #'bluetooth--update-all)
(cancel-timer bluetooth--update-timer)))
-;; TODO de-register all the signal handlers
(defun bluetooth-unload-function ()
"Clean up when the bluetooth feature is unloaded."
(when (buffer-live-p (get-buffer bluetooth-buffer-name))
- (cancel-timer bluetooth--update-timer)
+ (bluetooth--cleanup)
(kill-buffer bluetooth-buffer-name))
nil)
- [elpa] externals/bluetooth 5a49391b00 07/32: clean up device property handling (breaks info view code), (continued)
- [elpa] externals/bluetooth 5a49391b00 07/32: clean up device property handling (breaks info view code), Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 0627cceab3 08/32: clean up the info view code and add an adapter info view, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth f75ac5e43d 11/32: clean up the method registration function, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth e69e4151ff 12/32: clean things up for readability, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth a411243fbe 17/32: add a function for bluetooth device info access, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 7a78351220 19/32: set tabulated-list-padding to 0, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 1f048b5949 20/32: clean up and drop multiple adapter plans, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth fa3761da3c 21/32: add timer to periodically update the device table in discovery mode, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth c75d2e3495 22/32: handle removal of devices in list view, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth ddad595699 23/32: move command definitions to bottom of file, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 1254ec44d2 24/32: add signal handlers for paired devices,
Stefan Kangas <=
- [elpa] externals/bluetooth 7a0ba3c7ea 29/32: update Readme.org, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 1fcdce66f8 31/32: require dash 2.18.1, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 7ed3db7260 32/32: Release 0.3 and bump copyright year, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth ab3a4c9cc5 18/32: move bluetooth--create-device to the other --device functions, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth a20d488f9a 28/32: delete invalid todo, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth b6c501ae6e 04/32: add bluetooth--adapters function, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth cc8f69992d 06/32: adjust initial hash table sizes, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 0ba4d39085 13/32: correct indentation, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 7198f6b1a5 26/32: correct bluetooth--with-alias in case of unknown devices, Stefan Kangas, 2022/01/03
- [elpa] externals/bluetooth 301c37f388 27/32: list device only when properties are available, Stefan Kangas, 2022/01/03