aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2020-10-04 18:56:45 +0200
committerMichael Albinus2020-10-04 18:56:45 +0200
commit96a8e846061f255b1a394a5854197aa742dfff84 (patch)
treed61810b678df0c95bd4cfb28ea97c889ac02f1b9
parentd8665e6d3473403c90a0831e83439a013d0012d3 (diff)
downloademacs-96a8e846061f255b1a394a5854197aa742dfff84.tar.gz
emacs-96a8e846061f255b1a394a5854197aa742dfff84.zip
Make dbus-unregister-object work for monitors
* doc/misc/dbus.texi (Monitoring Messages): Rename from "Monitoring Events". (Register Objects, Monitoring Messages): Mention returned object. * lisp/net/dbus.el (dbus-unregister-object): Adapt docstring. (dbus-unregister-object): Delete monitor if needed. (dbus-register-monitor): Return proper object. * src/dbusbind.c (dbus-registered-objects-table): Adapt docstring. * test/lisp/net/dbus-tests.el (dbus--test-signal-handler): Adapt docstring. (dbus-test08-register-monitor): New test.
-rw-r--r--doc/misc/dbus.texi35
-rw-r--r--lisp/net/dbus.el12
-rw-r--r--src/dbusbind.c18
-rw-r--r--test/lisp/net/dbus-tests.el42
4 files changed, 79 insertions, 28 deletions
diff --git a/doc/misc/dbus.texi b/doc/misc/dbus.texi
index 4b2eab4eb76..5a1dd55248d 100644
--- a/doc/misc/dbus.texi
+++ b/doc/misc/dbus.texi
@@ -63,7 +63,7 @@ another. An overview of D-Bus can be found at
63* Signals:: Sending and receiving signals. 63* Signals:: Sending and receiving signals.
64* Alternative Buses:: Alternative buses and environments. 64* Alternative Buses:: Alternative buses and environments.
65* Errors and Events:: Errors and events. 65* Errors and Events:: Errors and events.
66* Monitoring Events:: Monitoring events. 66* Monitoring Messages:: Monitoring messages.
67* Index:: Index including concepts, functions, variables. 67* Index:: Index including concepts, functions, variables.
68 68
69* GNU Free Documentation License:: The license for this documentation. 69* GNU Free Documentation License:: The license for this documentation.
@@ -1622,7 +1622,9 @@ are constructed incrementally by adding single methods or properties
1622at a time, @var{dont-register-service} can be used to prevent other 1622at a time, @var{dont-register-service} can be used to prevent other
1623clients from discovering the still incomplete interface. 1623clients from discovering the still incomplete interface.
1624 1624
1625@noindent Example: 1625@code{dbus-register-property} returns a Lisp object, which can be used
1626as argument in @code{dbus-unregister-object} for removing the
1627registration for @var{property}. Example:
1626 1628
1627@lisp 1629@lisp
1628(dbus-register-property 1630(dbus-register-property
@@ -1687,9 +1689,9 @@ It is also possible to apply the @code{dbus-get-property},
1687@defun dbus-unregister-object object 1689@defun dbus-unregister-object object
1688This function unregisters @var{object} from the D-Bus. @var{object} 1690This function unregisters @var{object} from the D-Bus. @var{object}
1689must be the result of a preceding @code{dbus-register-method}, 1691must be the result of a preceding @code{dbus-register-method},
1690@code{dbus-register-property} or @code{dbus-register-signal} call 1692@code{dbus-register-property}, @code{dbus-register-signal}
1691(@pxref{Signals}). It returns @code{t} if @var{object} has been 1693(@pxref{Signals}) or @code{dbus-register-monitor} call. It returns
1692unregistered, @code{nil} otherwise. 1694@code{t} if @var{object} has been unregistered, @code{nil} otherwise.
1693 1695
1694When @var{object} identifies the last method or property, which is 1696When @var{object} identifies the last method or property, which is
1695registered for the respective service, Emacs releases its association 1697registered for the respective service, Emacs releases its association
@@ -2099,24 +2101,24 @@ D-Bus applications running. They should therefore check carefully,
2099whether a given D-Bus error is related to them. 2101whether a given D-Bus error is related to them.
2100 2102
2101 2103
2102@node Monitoring Events 2104@node Monitoring Messages
2103@chapter Monitoring events. 2105@chapter Monitoring messages.
2104@cindex monitoring 2106@cindex monitoring
2105 2107
2106@defun dbus-register-monitor bus &optional handler &key type sender destination path interface member 2108@defun dbus-register-monitor bus &optional handler &key type sender destination path interface member
2107This function registers @var{handler} for monitor events on the D-Bus 2109This function registers @var{handler} for monitoring messages on the
2108@var{bus}. 2110D-Bus @var{bus}.
2109 2111
2110@var{bus} is either a Lisp keyword, @code{:system} or @code{:session}, 2112@var{bus} is either a Lisp keyword, @code{:system} or @code{:session},
2111or a string denoting the bus address. 2113or a string denoting the bus address.
2112 2114
2113@findex dbus-monitor-handler 2115@findex dbus-monitor-handler
2114@var{handler} is the function to be called when a monitor event 2116@var{handler} is the function to be called when a D-Bus event to be
2115arrives. It is called with the `args' slot of the monitor event, 2117monitored arrives. It is called with the @var{args} slot of the D-Bus
2116which are stripped off the type keywords. If @var{handler} is 2118event (@pxref{Errors and Events}), which are stripped off the type
2117@code{nil}, the default handler @code{dbus-monitor-handler} is 2119keywords. If @var{handler} is @code{nil}, the default handler
2118applied. This default handler behaves similar to the 2120@code{dbus-monitor-handler} is applied. This default handler behaves
2119@command{dbus-monitor} program. 2121similar to the @command{dbus-monitor} program.
2120 2122
2121The other arguments are keyword-value pairs. @code{:type @var{type}} 2123The other arguments are keyword-value pairs. @code{:type @var{type}}
2122defines the message type to be monitored. If given, it must be equal 2124defines the message type to be monitored. If given, it must be equal
@@ -2132,6 +2134,9 @@ names.
2132@code{:member @var{member}} is either a method name, a signal name, or 2134@code{:member @var{member}} is either a method name, a signal name, or
2133an error name. 2135an error name.
2134 2136
2137@code{dbus-register-monitor} returns a Lisp object, which can be used
2138as argument in @code{dbus-unregister-object} for removing the monitor.
2139
2135The following form shows all D-Bus events on the session bus in buffer 2140The following form shows all D-Bus events on the session bus in buffer
2136@samp{*D-Bus Monitor*}: 2141@samp{*D-Bus Monitor*}:
2137 2142
diff --git a/lisp/net/dbus.el b/lisp/net/dbus.el
index 48712a9c3d8..77ba5266dcb 100644
--- a/lisp/net/dbus.el
+++ b/lisp/net/dbus.el
@@ -912,8 +912,9 @@ discovering the still incomplete interface."
912(defun dbus-unregister-object (object) 912(defun dbus-unregister-object (object)
913 "Unregister OBJECT from D-Bus. 913 "Unregister OBJECT from D-Bus.
914OBJECT must be the result of a preceding `dbus-register-method', 914OBJECT must be the result of a preceding `dbus-register-method',
915`dbus-register-property' or `dbus-register-signal' call. It 915`dbus-register-signal', `dbus-register-property' or
916returns t if OBJECT has been unregistered, nil otherwise. 916`dbus-register-monitor' call. The function returns t if OBJECT
917has been unregistered, nil otherwise.
917 918
918When OBJECT identifies the last method or property, which is 919When OBJECT identifies the last method or property, which is
919registered for the respective service, Emacs releases its 920registered for the respective service, Emacs releases its
@@ -951,7 +952,10 @@ association to the service from D-Bus."
951 (when (eq type :signal) 952 (when (eq type :signal)
952 (dbus-call-method 953 (dbus-call-method
953 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus 954 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
954 "RemoveMatch" (nth 4 elt))))) 955 "RemoveMatch" (nth 4 elt)))
956 ;; Delete monitor connection by reestablishing private bus.
957 (when (eq type :monitor)
958 (dbus-init-bus bus 'private))))
955 959
956 ;; Check, whether there is still a registered function or property 960 ;; Check, whether there is still a registered function or property
957 ;; for the given service. If not, unregister the service from the 961 ;; for the given service. If not, unregister the service from the
@@ -2037,7 +2041,7 @@ either a method name, a signal name, or an error name."
2037 (when dbus-debug (message "%s" dbus-registered-objects-table)) 2041 (when dbus-debug (message "%s" dbus-registered-objects-table))
2038 2042
2039 ;; Return the object. 2043 ;; Return the object.
2040 (list key key1))) 2044 (list key (list nil nil handler))))
2041 2045
2042(defconst dbus-monitor-method-call 2046(defconst dbus-monitor-method-call
2043 (propertize "method-call" 'face 'font-lock-function-name-face) 2047 (propertize "method-call" 'face 'font-lock-function-name-face)
diff --git a/src/dbusbind.c b/src/dbusbind.c
index cca5f13907d..1ac77313a35 100644
--- a/src/dbusbind.c
+++ b/src/dbusbind.c
@@ -1937,11 +1937,12 @@ syms_of_dbusbind (void)
1937 doc: /* Hash table of registered functions for D-Bus. 1937 doc: /* Hash table of registered functions for D-Bus.
1938 1938
1939There are two different uses of the hash table: for accessing 1939There are two different uses of the hash table: for accessing
1940registered interfaces properties, targeted by signals or method calls, 1940registered interfaces properties, targeted by signals, method calls or
1941and for calling handlers in case of non-blocking method call returns. 1941monitors, and for calling handlers in case of non-blocking method call
1942returns.
1942 1943
1943In the first case, the key in the hash table is the list (TYPE BUS 1944In the first case, the key in the hash table is the list (TYPE BUS
1944INTERFACE MEMBER). TYPE is one of the Lisp symbols `:method', 1945[INTERFACE MEMBER]). TYPE is one of the Lisp symbols `:method',
1945`:signal', `:property' or `:monitor'. BUS is either a Lisp symbol, 1946`:signal', `:property' or `:monitor'. BUS is either a Lisp symbol,
1946`:system', `:session', `:system-private' or `:session-private', or a 1947`:system', `:session', `:system-private' or `:session-private', or a
1947string denoting the bus address. INTERFACE is a string which denotes 1948string denoting the bus address. INTERFACE is a string which denotes
@@ -1951,17 +1952,18 @@ signal or a property INTERFACE is offering. All arguments can be nil.
1951The value in the hash table is a list of quadruple lists ((UNAME 1952The value in the hash table is a list of quadruple lists ((UNAME
1952SERVICE PATH OBJECT [RULE]) ...). SERVICE is the service name as 1953SERVICE PATH OBJECT [RULE]) ...). SERVICE is the service name as
1953registered, UNAME is the corresponding unique name. In case of 1954registered, UNAME is the corresponding unique name. In case of
1954registered methods and properties, UNAME is nil. PATH is the object 1955registered methods, properties and monitors, UNAME is nil. PATH is
1955path of the sending object. All of them can be nil, which means a 1956the object path of the sending object. All of them can be nil, which
1956wildcard then. 1957means a wildcard then.
1957 1958
1958OBJECT is either the handler to be called when a D-Bus message, which 1959OBJECT is either the handler to be called when a D-Bus message, which
1959matches the key criteria, arrives (TYPE `:method', `:signal' and 1960matches the key criteria, arrives (TYPE `:method', `:signal' and
1960`:monitor'), or a list (ACCESS EMITS-SIGNAL VALUE) for TYPE 1961`:monitor'), or a list (ACCESS EMITS-SIGNAL VALUE) for TYPE
1961`:property'. 1962`:property'.
1962 1963
1963For entries of type `:signal', there is also a fifth element RULE, 1964For entries of type `:signal' or `:monitor', there is also a fifth
1964which keeps the match string the signal is registered with. 1965element RULE, which keeps the match string the signal or monitor is
1966registered with.
1965 1967
1966In the second case, the key in the hash table is the list (:serial BUS 1968In the second case, the key in the hash table is the list (:serial BUS
1967SERIAL). BUS is either a Lisp symbol, `:system' or `:session', or a 1969SERIAL). BUS is either a Lisp symbol, `:system' or `:session', or a
diff --git a/test/lisp/net/dbus-tests.el b/test/lisp/net/dbus-tests.el
index 7ebef5d2609..94816bb4929 100644
--- a/test/lisp/net/dbus-tests.el
+++ b/test/lisp/net/dbus-tests.el
@@ -697,7 +697,7 @@ is in progress."
697 "Received signal value in `dbus--test-signal-handler'.") 697 "Received signal value in `dbus--test-signal-handler'.")
698 698
699(defun dbus--test-signal-handler (&rest args) 699(defun dbus--test-signal-handler (&rest args)
700 "Signal handler for `dbus-test*-signal'." 700 "Signal handler for `dbus-test*-signal' and `dbus-test08-register-monitor'."
701 (setq dbus--test-signal-received args)) 701 (setq dbus--test-signal-received args))
702 702
703(defun dbus--test-timeout-handler (&rest _ignore) 703(defun dbus--test-timeout-handler (&rest _ignore)
@@ -1833,6 +1833,46 @@ The argument EXPECTED-ARGS is a list of expected arguments for the method."
1833 ;; Cleanup. 1833 ;; Cleanup.
1834 (dbus-unregister-service :session dbus--test-service))) 1834 (dbus-unregister-service :session dbus--test-service)))
1835 1835
1836(ert-deftest dbus-test08-register-monitor ()
1837 "Check monitor registration."
1838 :tags '(:expensive-test)
1839 (skip-unless dbus--test-enabled-session-bus)
1840
1841 (unwind-protect
1842 (let (registered)
1843 (should
1844 (equal
1845 (setq registered
1846 (dbus-register-monitor :session #'dbus--test-signal-handler))
1847 '((:monitor :session-private)
1848 (nil nil dbus--test-signal-handler))))
1849
1850 ;; Send a signal, shall be traced.
1851 (setq dbus--test-signal-received nil)
1852 (dbus-send-signal
1853 :session dbus--test-service dbus--test-path
1854 dbus--test-interface "Foo" "foo")
1855 (with-timeout (1 (dbus--test-timeout-handler))
1856 (while (null dbus--test-signal-received)
1857 (read-event nil nil 0.1)))
1858
1859 ;; Unregister monitor.
1860 (should (dbus-unregister-object registered))
1861 (should-not (dbus-unregister-object registered))
1862
1863 ;; Send a signal, shall not be traced.
1864 (setq dbus--test-signal-received nil)
1865 (dbus-send-signal
1866 :session dbus--test-service dbus--test-path
1867 dbus--test-interface "Foo" "foo")
1868 (with-timeout (1 (ignore))
1869 (while (null dbus--test-signal-received)
1870 (read-event nil nil 0.1)))
1871 (should-not dbus--test-signal-received))
1872
1873 ;; Cleanup.
1874 (dbus-unregister-service :session dbus--test-service)))
1875
1836(defun dbus-test-all (&optional interactive) 1876(defun dbus-test-all (&optional interactive)
1837 "Run all tests for \\[dbus]." 1877 "Run all tests for \\[dbus]."
1838 (interactive "p") 1878 (interactive "p")