1 /* dbusbind.h -- Definitions used in and for dbusbind.c */
4 * Copyright (C) 2012 Steve Youngs
8 * Time-stamp: <Sunday Jan 29, 2012 17:45:16 steve>
9 * Created: <2012-01-22>
10 * Author: Steve Youngs <steve@sxemacs.org>
11 * Maintainer: Steve Youngs <steve@sxemacs.org>
12 * Homepage: http://www.sxemacs.org/
16 * This file is part of SXEmacs.
18 * SXEmacs is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * SXEmacs is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * Herein lives 2 categories of stuff...
37 * 1) Stuff to keep dbusbind.c happy.
38 * 2) Stuff lifted from GNU/Emacs to allow for #1
40 * Some of the things from #2 may turn out to be useful in other
41 * areas of SXEmacs, and if/when that happens feel free to move it
42 * from here to a more appropiate place like lisp.h.
50 /* The category 2 stuff (things that might one day live elsewhere) */
52 /* Take the car or cdr of something whose type is not known. */
54 (CONSP ((c)) ? XCAR ((c)) \
56 : wrong_type_argument (Qlistp, (c)))
59 (CONSP ((c)) ? XCDR ((c)) \
61 : wrong_type_argument (Qlistp, (c)))
63 /* Take the car or cdr of something whose type is not known. */
65 (CONSP ((c)) ? XCAR ((c)) : Qnil)
68 (CONSP ((c)) ? XCDR ((c)) : Qnil)
70 /* Convenience macros for dealing with Lisp strings. */
72 #define SDATA(string) (XSTRING (string)->data + 0)
73 #define SREF(string, index) (SDATA (string)[index] + 0)
74 #define SSET(string, index, new) (SDATA (string)[index] = (new))
75 #define SCHARS(string) (XSTRING (string)->size + 0)
76 #define SBYTES(string) (XSTRING_LENGTH (string + 0))
78 /* Avoid "differ in sign" warnings. */
79 #define SSDATA(x) ((char *) SDATA (x))
81 #define STRING_SET_CHARS(string, newsize) \
82 (XSTRING (string)->size = (newsize))
84 #define STRING_COPYIN(string, index, new, count) \
85 memcpy (SDATA (string) + index, new, count)
87 /* For integers known to be positive, XFASTINT provides fast retrieval
88 and XSETFASTINT provides fast storage. This takes advantage of the
89 fact that Lisp_Int is 0. */
90 #define XFASTINT(a) ((a) + 0)
91 #define XSETFASTINT(a, b) ((a) = (b))
95 /* Return a fixnum or float, depending on whether VAL fits in a Lisp
97 #define make_fixnum_or_float(val) \
98 (NUMBER_FITS_IN_AN_EMACS_INT(val) ? make_float(val) : make_int(val))
100 #define make_number(N) make_int(N)
103 extern void xsignal (Lisp_Object, Lisp_Object) NO_RETURN;
104 extern void xsignal0 (Lisp_Object) NO_RETURN;
105 extern void xsignal1 (Lisp_Object, Lisp_Object) NO_RETURN;
106 extern void xsignal2 (Lisp_Object, Lisp_Object, Lisp_Object) NO_RETURN;
107 extern void xsignal3 (Lisp_Object, Lisp_Object, Lisp_Object, Lisp_Object) NO_RETURN;
109 extern Lisp_Object format2 (const char *, Lisp_Object, Lisp_Object);
111 /* End From GNU/Emacs */
114 * Fmake_hash_table shouldn't be used within C. I'm only
115 * doing this so dbusbind.c will compile.
117 EXFUN(Fmake_hash_table, MANY);
119 /* The category 1 stuff */
120 static Lisp_Object Qdbus_init_bus;
121 static Lisp_Object Qdbus_close_bus;
122 static Lisp_Object Qdbus_get_unique_name;
123 static Lisp_Object Qdbus_call_method;
124 static Lisp_Object Qdbus_call_method_asynchronously;
125 static Lisp_Object Qdbus_method_return_internal;
126 static Lisp_Object Qdbus_method_error_internal;
127 static Lisp_Object Qdbus_send_signal;
128 static Lisp_Object Qdbus_register_service;
129 static Lisp_Object Qdbus_register_signal;
130 static Lisp_Object Qdbus_register_method;
132 /* D-Bus error symbol. */
133 static Lisp_Object Qdbus_error;
135 /* Lisp symbols of the system and session buses. */
136 static Lisp_Object QCdbus_system_bus, QCdbus_session_bus;
138 /* Lisp symbol for method call timeout. */
139 static Lisp_Object QCdbus_timeout;
141 /* Lisp symbols for name request flags. */
142 static Lisp_Object QCdbus_request_name_allow_replacement;
143 static Lisp_Object QCdbus_request_name_replace_existing;
144 static Lisp_Object QCdbus_request_name_do_not_queue;
146 /* Lisp symbols for name request replies. */
147 static Lisp_Object QCdbus_request_name_reply_primary_owner;
148 static Lisp_Object QCdbus_request_name_reply_in_queue;
149 static Lisp_Object QCdbus_request_name_reply_exists;
150 static Lisp_Object QCdbus_request_name_reply_already_owner;
152 /* Lisp symbols of D-Bus types. */
153 static Lisp_Object QCdbus_type_byte, QCdbus_type_boolean;
154 static Lisp_Object QCdbus_type_int16, QCdbus_type_uint16;
155 static Lisp_Object QCdbus_type_int32, QCdbus_type_uint32;
156 static Lisp_Object QCdbus_type_int64, QCdbus_type_uint64;
157 static Lisp_Object QCdbus_type_double, QCdbus_type_string;
158 static Lisp_Object QCdbus_type_object_path, QCdbus_type_signature;
159 #ifdef DBUS_TYPE_UNIX_FD
160 static Lisp_Object QCdbus_type_unix_fd;
162 static Lisp_Object QCdbus_type_array, QCdbus_type_variant;
163 static Lisp_Object QCdbus_type_struct, QCdbus_type_dict_entry;
164 static Lisp_Object Vdbus_debug, Vdbus_registered_buses;
165 static Lisp_Object Vdbus_registered_objects_table;
168 #define string_overflow(args...) error("Maximum string size exceeded")
170 /* We use "xd_" and "XD_" as prefix for all internal symbols, because
171 we don't want to poison other namespaces with "dbus_". */
173 /* Raise a signal. If we are reading events, we cannot signal; we
174 throw to xd_read_queued_messages then. */
175 #define XD_SIGNAL1(arg) \
177 if (xd_in_read_queued_messages) \
178 Fthrow (Qdbus_error, Qnil); \
180 xsignal1 (Qdbus_error, arg); \
183 #define XD_SIGNAL2(arg1, arg2) \
185 if (xd_in_read_queued_messages) \
186 Fthrow (Qdbus_error, Qnil); \
188 xsignal2 (Qdbus_error, arg1, arg2); \
191 #define XD_SIGNAL3(arg1, arg2, arg3) \
193 if (xd_in_read_queued_messages) \
194 Fthrow (Qdbus_error, Qnil); \
196 xsignal3 (Qdbus_error, arg1, arg2, arg3); \
199 /* Raise a Lisp error from a D-Bus ERROR. */
200 #define XD_ERROR(error) \
202 /* Remove the trailing newline. */ \
203 const char *mess = error.message; \
204 const char *nl = strchr (mess, '\n'); \
205 Lisp_Object err = make_string \
206 ((const Bufbyte *)mess, \
207 nl ? nl - mess : strlen (mess)); \
208 dbus_error_free (&error); \
212 /* Macros for debugging. In order to enable them, build with
213 "MYCPPFLAGS='-DDBUS_DEBUG -Wall' make". */
215 #define XD_DEBUG_MESSAGE(...) \
218 snprintf (s, sizeof s, __VA_ARGS__); \
219 printf ("%s: %s\n", __func__, s); \
220 message ("%s: %s", __func__, s); \
222 #define XD_DEBUG_VALID_LISP_OBJECT_P(object) \
224 if (!valid_lisp_object_p (object)) \
226 XD_DEBUG_MESSAGE ("%d Assertion failure", __LINE__); \
227 XD_SIGNAL1 (build_string ("Assertion failure")); \
231 #else /* !DBUS_DEBUG */
232 #define XD_DEBUG_MESSAGE(...) \
234 if (!NILP (Vdbus_debug)) \
237 snprintf (s, 1023, __VA_ARGS__); \
238 message ("%s: %s", __func__, s); \
241 #define XD_DEBUG_VALID_LISP_OBJECT_P(object)
244 /* Check whether TYPE is a basic DBusType. */
245 #ifdef DBUS_TYPE_UNIX_FD
246 #define XD_BASIC_DBUS_TYPE(type) \
247 ((type == DBUS_TYPE_BYTE) \
248 || (type == DBUS_TYPE_BOOLEAN) \
249 || (type == DBUS_TYPE_INT16) \
250 || (type == DBUS_TYPE_UINT16) \
251 || (type == DBUS_TYPE_INT32) \
252 || (type == DBUS_TYPE_UINT32) \
253 || (type == DBUS_TYPE_INT64) \
254 || (type == DBUS_TYPE_UINT64) \
255 || (type == DBUS_TYPE_DOUBLE) \
256 || (type == DBUS_TYPE_STRING) \
257 || (type == DBUS_TYPE_OBJECT_PATH) \
258 || (type == DBUS_TYPE_SIGNATURE) \
259 || (type == DBUS_TYPE_UNIX_FD))
261 #define XD_BASIC_DBUS_TYPE(type) \
262 ((type == DBUS_TYPE_BYTE) \
263 || (type == DBUS_TYPE_BOOLEAN) \
264 || (type == DBUS_TYPE_INT16) \
265 || (type == DBUS_TYPE_UINT16) \
266 || (type == DBUS_TYPE_INT32) \
267 || (type == DBUS_TYPE_UINT32) \
268 || (type == DBUS_TYPE_INT64) \
269 || (type == DBUS_TYPE_UINT64) \
270 || (type == DBUS_TYPE_DOUBLE) \
271 || (type == DBUS_TYPE_STRING) \
272 || (type == DBUS_TYPE_OBJECT_PATH) \
273 || (type == DBUS_TYPE_SIGNATURE))
276 /* Check whether a Lisp symbol is a predefined D-Bus type symbol. */
277 #define XD_DBUS_TYPE_P(object) \
278 (SYMBOLP (object) && ((xd_symbol_to_dbus_type (object) != DBUS_TYPE_INVALID)))
280 /* Determine the DBusType of a given Lisp OBJECT. It is used to
281 convert Lisp objects, being arguments of `dbus-call-method' or
282 `dbus-send-signal', into corresponding C values appended as
283 arguments to a D-Bus message. */
284 #define XD_OBJECT_TO_DBUS_TYPE(object) \
285 ((EQ (object, Qt) || EQ (object, Qnil)) ? DBUS_TYPE_BOOLEAN \
286 : (NATNUMP (object)) ? DBUS_TYPE_UINT32 \
287 : (INTEGERP (object)) ? DBUS_TYPE_INT32 \
288 : (FLOATP (object)) ? DBUS_TYPE_DOUBLE \
289 : (STRINGP (object)) ? DBUS_TYPE_STRING \
290 : (XD_DBUS_TYPE_P (object)) ? xd_symbol_to_dbus_type (object) \
292 ? ((XD_DBUS_TYPE_P (CAR_SAFE (object))) \
293 ? ((XD_BASIC_DBUS_TYPE (xd_symbol_to_dbus_type (CAR_SAFE (object)))) \
295 : xd_symbol_to_dbus_type (CAR_SAFE (object))) \
299 /* Return a list pointer which does not have a Lisp symbol as car. */
300 #define XD_NEXT_VALUE(object) \
301 ((XD_DBUS_TYPE_P (CAR_SAFE (object))) ? CDR_SAFE (object) : object)
303 /* Check whether X is a valid dbus serial number. If valid, set
304 SERIAL to its value. Otherwise, signal an error. */
305 #define CHECK_DBUS_SERIAL_GET_SERIAL(x, serial) \
308 dbus_uint32_t DBUS_SERIAL_MAX = -1; \
309 if (NATNUMP (x) && XINT (x) <= DBUS_SERIAL_MAX) \
311 else if (EMACS_INT_MAX < DBUS_SERIAL_MAX \
313 && 0 <= XFLOAT_DATA (x) \
314 && XFLOAT_DATA (x) <= DBUS_SERIAL_MAX) \
315 serial = XFLOAT_DATA (x); \
317 XD_SIGNAL2 (build_string ("Invalid dbus serial"), x); \
322 void syms_of_dbusbind(void);
324 #endif /* _DBUSBIND_H */
328 /* dbusbind.h ends here */