Mercurial > emacs
comparison lisp/net/dbus.el @ 87586:9554989ee375
* net/dbus.el (dbus-list-hash-table)
(dbus-name-owner-changed-handler): Replace "signal" by "member".
(dbus-check-event): Add serial number to event.
(dbus-handle-event): Apply return message if needed.
(dbus-event-serial-number): New defun.
(dbus-event-service-name, dbus-event-path-name)
(dbus-event-interface-name, dbus-event-member-name): Adapt
implementation to serial number.
author | Michael Albinus <michael.albinus@gmx.de> |
---|---|
date | Fri, 04 Jan 2008 21:52:51 +0000 |
parents | 84ece2f8e012 |
children | b560a1744534 |
comparison
equal
deleted
inserted
replaced
87585:072778cd2f17 | 87586:9554989ee375 |
---|---|
52 ;; We create it here. So we have a simple test in dbusbind.c, whether | 52 ;; We create it here. So we have a simple test in dbusbind.c, whether |
53 ;; the Lisp code has been loaded. | 53 ;; the Lisp code has been loaded. |
54 (setq dbus-registered-functions-table (make-hash-table :test 'equal)) | 54 (setq dbus-registered-functions-table (make-hash-table :test 'equal)) |
55 | 55 |
56 (defun dbus-list-hash-table () | 56 (defun dbus-list-hash-table () |
57 "Returns all registered signal registrations to D-Bus. | 57 "Returns all registered member registrations to D-Bus. |
58 The return value is a list, with elements of kind (KEY . VALUE). | 58 The return value is a list, with elements of kind (KEY . VALUE). |
59 See `dbus-registered-functions-table' for a description of the | 59 See `dbus-registered-functions-table' for a description of the |
60 hash table." | 60 hash table." |
61 (let (result) | 61 (let (result) |
62 (maphash | 62 (maphash |
63 '(lambda (key value) (add-to-list 'result (cons key value) 'append)) | 63 '(lambda (key value) (add-to-list 'result (cons key value) 'append)) |
64 dbus-registered-functions-table) | 64 dbus-registered-functions-table) |
65 result)) | 65 result)) |
66 | 66 |
67 (defun dbus-name-owner-changed-handler (&rest args) | 67 (defun dbus-name-owner-changed-handler (&rest args) |
68 "Reapplies all signal registrations to D-Bus. | 68 "Reapplies all member registrations to D-Bus. |
69 This handler is applied when a \"NameOwnerChanged\" signal has | 69 This handler is applied when a \"NameOwnerChanged\" signal has |
70 arrived. SERVICE is the object name for which the name owner has | 70 arrived. SERVICE is the object name for which the name owner has |
71 been changed. OLD-OWNER is the previous owner of SERVICE, or the | 71 been changed. OLD-OWNER is the previous owner of SERVICE, or the |
72 empty string if SERVICE was not owned yet. NEW-OWNER is the new | 72 empty string if SERVICE was not owned yet. NEW-OWNER is the new |
73 owner of SERVICE, or the empty string if SERVICE looses any name owner. | 73 owner of SERVICE, or the empty string if SERVICE looses any name owner. |
86 ;; Check whether SERVICE is a known name. | 86 ;; Check whether SERVICE is a known name. |
87 (when (not (string-match "^:" service)) | 87 (when (not (string-match "^:" service)) |
88 (maphash | 88 (maphash |
89 '(lambda (key value) | 89 '(lambda (key value) |
90 (dolist (elt value) | 90 (dolist (elt value) |
91 ;; key has the structure (BUS INTERFACE SIGNAL). | 91 ;; key has the structure (BUS INTERFACE MEMBER). |
92 ;; elt has the structure (UNAME SERVICE PATH HANDLER). | 92 ;; elt has the structure (UNAME SERVICE PATH HANDLER). |
93 (when (string-equal old-owner (car elt)) | 93 (when (string-equal old-owner (car elt)) |
94 ;; Remove old key, and add new entry with changed name. | 94 ;; Remove old key, and add new entry with changed name. |
95 (dbus-unregister-object (list key (cdr elt))) | 95 (dbus-unregister-object (list key (cdr elt))) |
96 ;; Maybe we could arrange the lists a little bit better | 96 ;; Maybe we could arrange the lists a little bit better |
97 ;; that we don't need to extract every single element? | 97 ;; that we don't need to extract every single element? |
98 (dbus-register-signal | 98 (dbus-register-signal |
99 ;; BUS SERVICE PATH | 99 ;; BUS SERVICE PATH |
100 (nth 0 key) (nth 1 elt) (nth 2 elt) | 100 (nth 0 key) (nth 1 elt) (nth 2 elt) |
101 ;; INTERFACE SIGNAL HANDLER | 101 ;; INTERFACE MEMBER HANDLER |
102 (nth 1 key) (nth 2 key) (nth 3 elt))))) | 102 (nth 1 key) (nth 2 key) (nth 3 elt))))) |
103 (copy-hash-table dbus-registered-functions-table)))) | 103 (copy-hash-table dbus-registered-functions-table)))) |
104 ;; The error is reported only in debug mode. | 104 ;; The error is reported only in debug mode. |
105 (when dbus-debug | 105 (when dbus-debug |
106 (signal | 106 (signal |
125 | 125 |
126 (defun dbus-check-event (event) | 126 (defun dbus-check-event (event) |
127 "Checks whether EVENT is a well formed D-Bus event. | 127 "Checks whether EVENT is a well formed D-Bus event. |
128 EVENT is a list which starts with symbol `dbus-event': | 128 EVENT is a list which starts with symbol `dbus-event': |
129 | 129 |
130 (dbus-event BUS SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS) | 130 (dbus-event BUS SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS) |
131 | 131 |
132 BUS identifies the D-Bus the signal is coming from. It is either | 132 BUS identifies the D-Bus the message is coming from. It is |
133 the symbol `:system' or the symbol `:session'. SERVICE and PATH | 133 either the symbol `:system' or the symbol `:session'. SERIAL is |
134 are the unique name and the object path of the D-Bus object | 134 the serial number of the received D-Bus message if it is a method |
135 emitting the signal. INTERFACE and MEMBER denote the signal | 135 call, or nil. SERVICE and PATH are the unique name and the |
136 which has been sent. HANDLER is the function which has been | 136 object path of the D-Bus object emitting the message. INTERFACE |
137 registered for this signal. ARGS are the arguments passed to | 137 and MEMBER denote the message which has been sent. HANDLER is |
138 HANDLER, when it is called during event handling in | 138 the function which has been registered for this message. ARGS |
139 `dbus-handle-event'. | 139 are the arguments passed to HANDLER, when it is called during |
140 event handling in `dbus-handle-event'. | |
140 | 141 |
141 This function raises a `dbus-error' signal in case the event is | 142 This function raises a `dbus-error' signal in case the event is |
142 not well formed." | 143 not well formed." |
143 (when dbus-debug (message "DBus-Event %s" event)) | 144 (when dbus-debug (message "DBus-Event %s" event)) |
144 (unless (and (listp event) | 145 (unless (and (listp event) |
145 (eq (car event) 'dbus-event) | 146 (eq (car event) 'dbus-event) |
146 ;; Bus symbol. | 147 ;; Bus symbol. |
147 (symbolp (nth 1 event)) | 148 (symbolp (nth 1 event)) |
149 ;; Serial. | |
150 (or (natnump (nth 2 event)) (null (nth 2 event))) | |
148 ;; Service. | 151 ;; Service. |
149 (stringp (nth 2 event)) | 152 (stringp (nth 3 event)) |
150 ;; Object path. | 153 ;; Object path. |
151 (stringp (nth 3 event)) | 154 (stringp (nth 4 event)) |
152 ;; Interface. | 155 ;; Interface. |
153 (stringp (nth 4 event)) | 156 (stringp (nth 5 event)) |
154 ;; Member. | 157 ;; Member. |
155 (stringp (nth 5 event)) | 158 (stringp (nth 6 event)) |
156 ;; Handler. | 159 ;; Handler. |
157 (functionp (nth 6 event))) | 160 (functionp (nth 7 event))) |
158 (signal 'dbus-error (list "Not a valid D-Bus event" event)))) | 161 (signal 'dbus-error (list "Not a valid D-Bus event" event)))) |
159 | 162 |
160 ;;;###autoload | 163 ;;;###autoload |
161 (defun dbus-handle-event (event) | 164 (defun dbus-handle-event (event) |
162 "Handle events from the D-Bus. | 165 "Handle events from the D-Bus. |
164 part of the event, is called with arguments ARGS." | 167 part of the event, is called with arguments ARGS." |
165 (interactive "e") | 168 (interactive "e") |
166 ;; We don't want to raise an error, because this function is called | 169 ;; We don't want to raise an error, because this function is called |
167 ;; in the event handling loop. | 170 ;; in the event handling loop. |
168 (condition-case err | 171 (condition-case err |
169 (progn | 172 (let (result) |
170 (dbus-check-event event) | 173 (dbus-check-event event) |
171 (apply (nth 6 event) (nthcdr 7 event))) | 174 (setq result (apply (nth 7 event) (nthcdr 8 event))) |
175 (unless (consp result) (setq result (cons result nil))) | |
176 ;; Return a message when serial is not nil. | |
177 (when (not (null (nth 2 event))) | |
178 (apply 'dbus-method-return | |
179 (nth 1 event) (nth 2 event) (nth 3 event) result))) | |
172 (dbus-error (when dbus-debug (signal (car err) (cdr err)))))) | 180 (dbus-error (when dbus-debug (signal (car err) (cdr err)))))) |
173 | 181 |
174 (defun dbus-event-bus-name (event) | 182 (defun dbus-event-bus-name (event) |
175 "Return the bus name the event is coming from. | 183 "Return the bus name the event is coming from. |
176 The result is either the symbol `:system' or the symbol `:session'. | 184 The result is either the symbol `:system' or the symbol `:session'. |
178 raises a `dbus-error' signal in case the event is not well | 186 raises a `dbus-error' signal in case the event is not well |
179 formed." | 187 formed." |
180 (dbus-check-event event) | 188 (dbus-check-event event) |
181 (nth 1 event)) | 189 (nth 1 event)) |
182 | 190 |
191 (defun dbus-event-serial-number (event) | |
192 "Return the serial number of the corresponding D-Bus message. | |
193 The result is a number in case the D-Bus message is a method | |
194 call, or nil for all other mesage types. The serial number is | |
195 needed for generating a reply message. EVENT is a D-Bus event, | |
196 see `dbus-check-event'. This function raises a `dbus-error' | |
197 signal in case the event is not well formed." | |
198 (dbus-check-event event) | |
199 (nth 2 event)) | |
200 | |
183 (defun dbus-event-service-name (event) | 201 (defun dbus-event-service-name (event) |
184 "Return the name of the D-Bus object the event is coming from. | 202 "Return the name of the D-Bus object the event is coming from. |
185 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. | 203 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. |
186 This function raises a `dbus-error' signal in case the event is | 204 This function raises a `dbus-error' signal in case the event is |
187 not well formed." | 205 not well formed." |
188 (dbus-check-event event) | 206 (dbus-check-event event) |
189 (nth 2 event)) | 207 (nth 3 event)) |
190 | 208 |
191 (defun dbus-event-path-name (event) | 209 (defun dbus-event-path-name (event) |
192 "Return the object path of the D-Bus object the event is coming from. | 210 "Return the object path of the D-Bus object the event is coming from. |
193 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. | 211 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. |
194 This function raises a `dbus-error' signal in case the event is | 212 This function raises a `dbus-error' signal in case the event is |
195 not well formed." | 213 not well formed." |
196 (dbus-check-event event) | 214 (dbus-check-event event) |
197 (nth 3 event)) | 215 (nth 4 event)) |
198 | 216 |
199 (defun dbus-event-interface-name (event) | 217 (defun dbus-event-interface-name (event) |
200 "Return the interface name of the D-Bus object the event is coming from. | 218 "Return the interface name of the D-Bus object the event is coming from. |
201 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. | 219 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'. |
202 This function raises a `dbus-error' signal in case the event is | 220 This function raises a `dbus-error' signal in case the event is |
203 not well formed." | 221 not well formed." |
204 (dbus-check-event event) | 222 (dbus-check-event event) |
205 (nth 4 event)) | 223 (nth 5 event)) |
206 | 224 |
207 (defun dbus-event-member-name (event) | 225 (defun dbus-event-member-name (event) |
208 "Return the member name the event is coming from. | 226 "Return the member name the event is coming from. |
209 It is either a signal name or a method name. The result is is a | 227 It is either a signal name or a method name. The result is is a |
210 string. EVENT is a D-Bus event, see `dbus-check-event'. This | 228 string. EVENT is a D-Bus event, see `dbus-check-event'. This |
211 function raises a `dbus-error' signal in case the event is not | 229 function raises a `dbus-error' signal in case the event is not |
212 well formed." | 230 well formed." |
213 (dbus-check-event event) | 231 (dbus-check-event event) |
214 (nth 5 event)) | 232 (nth 6 event)) |
215 | 233 |
216 | 234 |
217 ;;; D-Bus registered names. | 235 ;;; D-Bus registered names. |
218 | 236 |
219 (defun dbus-list-activatable-names () | 237 (defun dbus-list-activatable-names () |