aboutsummaryrefslogtreecommitdiffstats
path: root/src/dbusbind.c
diff options
context:
space:
mode:
authorJoakim Verona2010-08-27 10:58:44 +0200
committerJoakim Verona2010-08-27 10:58:44 +0200
commit362120833bcbbaea94976b6701633e2ed75f6051 (patch)
tree632690a24a934bb51a32303add5172d63b6b9e00 /src/dbusbind.c
parent1800c4865b15a9e1154bf1f03d87d1aaf750a527 (diff)
parent1a868076f51b5d6f1cf78117463e6f9c614551ec (diff)
downloademacs-362120833bcbbaea94976b6701633e2ed75f6051.tar.gz
emacs-362120833bcbbaea94976b6701633e2ed75f6051.zip
merge from trunk, fix conflicts
Diffstat (limited to 'src/dbusbind.c')
-rw-r--r--src/dbusbind.c321
1 files changed, 186 insertions, 135 deletions
diff --git a/src/dbusbind.c b/src/dbusbind.c
index e813d0b5ad8..3b6f0e543bb 100644
--- a/src/dbusbind.c
+++ b/src/dbusbind.c
@@ -31,6 +31,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
31 31
32/* Subroutines. */ 32/* Subroutines. */
33Lisp_Object Qdbus_init_bus; 33Lisp_Object Qdbus_init_bus;
34Lisp_Object Qdbus_close_bus;
34Lisp_Object Qdbus_get_unique_name; 35Lisp_Object Qdbus_get_unique_name;
35Lisp_Object Qdbus_call_method; 36Lisp_Object Qdbus_call_method;
36Lisp_Object Qdbus_call_method_asynchronously; 37Lisp_Object Qdbus_call_method_asynchronously;
@@ -59,6 +60,9 @@ Lisp_Object QCdbus_type_object_path, QCdbus_type_signature;
59Lisp_Object QCdbus_type_array, QCdbus_type_variant; 60Lisp_Object QCdbus_type_array, QCdbus_type_variant;
60Lisp_Object QCdbus_type_struct, QCdbus_type_dict_entry; 61Lisp_Object QCdbus_type_struct, QCdbus_type_dict_entry;
61 62
63/* Registered buses. */
64Lisp_Object Vdbus_registered_buses;
65
62/* Hash table which keeps function definitions. */ 66/* Hash table which keeps function definitions. */
63Lisp_Object Vdbus_registered_objects_table; 67Lisp_Object Vdbus_registered_objects_table;
64 68
@@ -111,7 +115,7 @@ int xd_in_read_queued_messages = 0;
111 } while (0) 115 } while (0)
112 116
113/* Macros for debugging. In order to enable them, build with 117/* Macros for debugging. In order to enable them, build with
114 "make MYCPPFLAGS='-DDBUS_DEBUG -Wall'". */ 118 "MYCPPFLAGS='-DDBUS_DEBUG -Wall' make". */
115#ifdef DBUS_DEBUG 119#ifdef DBUS_DEBUG
116#define XD_DEBUG_MESSAGE(...) \ 120#define XD_DEBUG_MESSAGE(...) \
117 do { \ 121 do { \
@@ -163,8 +167,7 @@ int xd_in_read_queued_messages = 0;
163/* Determine the DBusType of a given Lisp symbol. OBJECT must be one 167/* Determine the DBusType of a given Lisp symbol. OBJECT must be one
164 of the predefined D-Bus type symbols. */ 168 of the predefined D-Bus type symbols. */
165static int 169static int
166xd_symbol_to_dbus_type (object) 170xd_symbol_to_dbus_type (Lisp_Object object)
167 Lisp_Object object;
168{ 171{
169 return 172 return
170 ((EQ (object, QCdbus_type_byte)) ? DBUS_TYPE_BYTE 173 ((EQ (object, QCdbus_type_byte)) ? DBUS_TYPE_BYTE
@@ -221,10 +224,7 @@ xd_symbol_to_dbus_type (object)
221 signature is embedded, or DBUS_TYPE_INVALID. It is needed for the 224 signature is embedded, or DBUS_TYPE_INVALID. It is needed for the
222 check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */ 225 check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */
223static void 226static void
224xd_signature (signature, dtype, parent_type, object) 227xd_signature (char *signature, unsigned int dtype, unsigned int parent_type, Lisp_Object object)
225 char *signature;
226 unsigned int dtype, parent_type;
227 Lisp_Object object;
228{ 228{
229 unsigned int subtype; 229 unsigned int subtype;
230 Lisp_Object elt; 230 Lisp_Object elt;
@@ -393,10 +393,7 @@ xd_signature (signature, dtype, parent_type, object)
393 `dbus-send-signal', into corresponding C values appended as 393 `dbus-send-signal', into corresponding C values appended as
394 arguments to a D-Bus message. */ 394 arguments to a D-Bus message. */
395static void 395static void
396xd_append_arg (dtype, object, iter) 396xd_append_arg (unsigned int dtype, Lisp_Object object, DBusMessageIter *iter)
397 unsigned int dtype;
398 Lisp_Object object;
399 DBusMessageIter *iter;
400{ 397{
401 char signature[DBUS_MAXIMUM_SIGNATURE_LENGTH]; 398 char signature[DBUS_MAXIMUM_SIGNATURE_LENGTH];
402 DBusMessageIter subiter; 399 DBusMessageIter subiter;
@@ -604,9 +601,7 @@ xd_append_arg (dtype, object, iter)
604 D-Bus message must be a valid DBusType. Compound D-Bus types 601 D-Bus message must be a valid DBusType. Compound D-Bus types
605 result always in a Lisp list. */ 602 result always in a Lisp list. */
606static Lisp_Object 603static Lisp_Object
607xd_retrieve_arg (dtype, iter) 604xd_retrieve_arg (unsigned int dtype, DBusMessageIter *iter)
608 unsigned int dtype;
609 DBusMessageIter *iter;
610{ 605{
611 606
612 switch (dtype) 607 switch (dtype)
@@ -722,37 +717,79 @@ xd_retrieve_arg (dtype, iter)
722 } 717 }
723} 718}
724 719
725/* Initialize D-Bus connection. BUS is a Lisp symbol, either :system 720/* Initialize D-Bus connection. BUS is either a Lisp symbol, :system
726 or :session. It tells which D-Bus to be initialized. */ 721 or :session, or a string denoting the bus address. It tells which
722 D-Bus to initialize. If RAISE_ERROR is non-zero, signal an error
723 when the connection cannot be initialized. */
727static DBusConnection * 724static DBusConnection *
728xd_initialize (bus) 725xd_initialize (Lisp_Object bus, int raise_error)
729 Lisp_Object bus;
730{ 726{
731 DBusConnection *connection; 727 DBusConnection *connection;
732 DBusError derror; 728 DBusError derror;
733 729
734 /* Parameter check. */ 730 /* Parameter check. */
735 CHECK_SYMBOL (bus); 731 if (!STRINGP (bus))
736 if (!(EQ (bus, QCdbus_system_bus) || EQ (bus, QCdbus_session_bus))) 732 {
737 XD_SIGNAL2 (build_string ("Wrong bus name"), bus); 733 CHECK_SYMBOL (bus);
734 if (!(EQ (bus, QCdbus_system_bus) || EQ (bus, QCdbus_session_bus)))
735 {
736 if (raise_error)
737 XD_SIGNAL2 (build_string ("Wrong bus name"), bus);
738 else
739 return NULL;
740 }
738 741
739 /* We do not want to have an autolaunch for the session bus. */ 742 /* We do not want to have an autolaunch for the session bus. */
740 if (EQ (bus, QCdbus_session_bus) 743 if (EQ (bus, QCdbus_session_bus)
741 && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL) 744 && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL)
742 XD_SIGNAL2 (build_string ("No connection to bus"), bus); 745 {
746 if (raise_error)
747 XD_SIGNAL2 (build_string ("No connection to bus"), bus);
748 else
749 return NULL;
750 }
751 }
743 752
744 /* Open a connection to the bus. */ 753 /* Open a connection to the bus. */
745 dbus_error_init (&derror); 754 dbus_error_init (&derror);
746 755
747 if (EQ (bus, QCdbus_system_bus)) 756 if (STRINGP (bus))
748 connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror); 757 connection = dbus_connection_open (SDATA (bus), &derror);
749 else 758 else
750 connection = dbus_bus_get (DBUS_BUS_SESSION, &derror); 759 if (EQ (bus, QCdbus_system_bus))
760 connection = dbus_bus_get (DBUS_BUS_SYSTEM, &derror);
761 else
762 connection = dbus_bus_get (DBUS_BUS_SESSION, &derror);
751 763
752 if (dbus_error_is_set (&derror)) 764 if (dbus_error_is_set (&derror))
753 XD_ERROR (derror); 765 {
766 if (raise_error)
767 XD_ERROR (derror);
768 else
769 connection = NULL;
770 }
771
772 /* If it is not the system or session bus, we must register
773 ourselves. Otherwise, we have called dbus_bus_get, which has
774 configured us to exit if the connection closes - we undo this
775 setting. */
776 if (connection != NULL)
777 {
778 if (STRINGP (bus))
779 dbus_bus_register (connection, &derror);
780 else
781 dbus_connection_set_exit_on_disconnect (connection, FALSE);
782 }
754 783
755 if (connection == NULL) 784 if (dbus_error_is_set (&derror))
785 {
786 if (raise_error)
787 XD_ERROR (derror);
788 else
789 connection = NULL;
790 }
791
792 if (connection == NULL && raise_error)
756 XD_SIGNAL2 (build_string ("No connection to bus"), bus); 793 XD_SIGNAL2 (build_string ("No connection to bus"), bus);
757 794
758 /* Cleanup. */ 795 /* Cleanup. */
@@ -766,9 +803,7 @@ xd_initialize (bus)
766/* Add connection file descriptor to input_wait_mask, in order to 803/* Add connection file descriptor to input_wait_mask, in order to
767 let select() detect, whether a new message has been arrived. */ 804 let select() detect, whether a new message has been arrived. */
768dbus_bool_t 805dbus_bool_t
769xd_add_watch (watch, data) 806xd_add_watch (DBusWatch *watch, void *data)
770 DBusWatch *watch;
771 void *data;
772{ 807{
773 /* We check only for incoming data. */ 808 /* We check only for incoming data. */
774 if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE) 809 if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE)
@@ -795,11 +830,10 @@ xd_add_watch (watch, data)
795} 830}
796 831
797/* Remove connection file descriptor from input_wait_mask. DATA is 832/* Remove connection file descriptor from input_wait_mask. DATA is
798 the used bus, either QCdbus_system_bus or QCdbus_session_bus. */ 833 the used bus, either a string or QCdbus_system_bus or
834 QCdbus_session_bus. */
799void 835void
800xd_remove_watch (watch, data) 836xd_remove_watch (DBusWatch *watch, void *data)
801 DBusWatch *watch;
802 void *data;
803{ 837{
804 /* We check only for incoming data. */ 838 /* We check only for incoming data. */
805 if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE) 839 if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE)
@@ -833,18 +867,13 @@ xd_remove_watch (watch, data)
833} 867}
834 868
835DEFUN ("dbus-init-bus", Fdbus_init_bus, Sdbus_init_bus, 1, 1, 0, 869DEFUN ("dbus-init-bus", Fdbus_init_bus, Sdbus_init_bus, 1, 1, 0,
836 doc: /* Initialize connection to D-Bus BUS. 870 doc: /* Initialize connection to D-Bus BUS. */)
837This is an internal function, it shall not be used outside dbus.el. */) 871 (Lisp_Object bus)
838 (bus)
839 Lisp_Object bus;
840{ 872{
841 DBusConnection *connection; 873 DBusConnection *connection;
842 874
843 /* Check parameters. */
844 CHECK_SYMBOL (bus);
845
846 /* Open a connection to the bus. */ 875 /* Open a connection to the bus. */
847 connection = xd_initialize (bus); 876 connection = xd_initialize (bus, TRUE);
848 877
849 /* Add the watch functions. We pass also the bus as data, in order 878 /* Add the watch functions. We pass also the bus as data, in order
850 to distinguish between the busses in xd_remove_watch. */ 879 to distinguish between the busses in xd_remove_watch. */
@@ -854,6 +883,28 @@ This is an internal function, it shall not be used outside dbus.el. */)
854 NULL, (void*) XHASH (bus), NULL)) 883 NULL, (void*) XHASH (bus), NULL))
855 XD_SIGNAL1 (build_string ("Cannot add watch functions")); 884 XD_SIGNAL1 (build_string ("Cannot add watch functions"));
856 885
886 /* Add bus to list of registered buses. */
887 Vdbus_registered_buses = Fcons (bus, Vdbus_registered_buses);
888
889 /* Return. */
890 return Qnil;
891}
892
893DEFUN ("dbus-close-bus", Fdbus_close_bus, Sdbus_close_bus, 1, 1, 0,
894 doc: /* Close connection to D-Bus BUS. */)
895 (Lisp_Object bus)
896{
897 DBusConnection *connection;
898
899 /* Open a connection to the bus. */
900 connection = xd_initialize (bus, TRUE);
901
902 /* Decrement reference count to the bus. */
903 dbus_connection_unref (connection);
904
905 /* Remove bus from list of registered buses. */
906 Vdbus_registered_buses = Fdelete (bus, Vdbus_registered_buses);
907
857 /* Return. */ 908 /* Return. */
858 return Qnil; 909 return Qnil;
859} 910}
@@ -861,17 +912,13 @@ This is an internal function, it shall not be used outside dbus.el. */)
861DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name, Sdbus_get_unique_name, 912DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name, Sdbus_get_unique_name,
862 1, 1, 0, 913 1, 1, 0,
863 doc: /* Return the unique name of Emacs registered at D-Bus BUS. */) 914 doc: /* Return the unique name of Emacs registered at D-Bus BUS. */)
864 (bus) 915 (Lisp_Object bus)
865 Lisp_Object bus;
866{ 916{
867 DBusConnection *connection; 917 DBusConnection *connection;
868 const char *name; 918 const char *name;
869 919
870 /* Check parameters. */
871 CHECK_SYMBOL (bus);
872
873 /* Open a connection to the bus. */ 920 /* Open a connection to the bus. */
874 connection = xd_initialize (bus); 921 connection = xd_initialize (bus, TRUE);
875 922
876 /* Request the name. */ 923 /* Request the name. */
877 name = dbus_bus_get_unique_name (connection); 924 name = dbus_bus_get_unique_name (connection);
@@ -885,7 +932,8 @@ DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name, Sdbus_get_unique_name,
885DEFUN ("dbus-call-method", Fdbus_call_method, Sdbus_call_method, 5, MANY, 0, 932DEFUN ("dbus-call-method", Fdbus_call_method, Sdbus_call_method, 5, MANY, 0,
886 doc: /* Call METHOD on the D-Bus BUS. 933 doc: /* Call METHOD on the D-Bus BUS.
887 934
888BUS is either the symbol `:system' or the symbol `:session'. 935BUS is either a Lisp symbol, `:system' or `:session', or a string
936denoting the bus address.
889 937
890SERVICE is the D-Bus service name to be used. PATH is the D-Bus 938SERVICE is the D-Bus service name to be used. PATH is the D-Bus
891object path SERVICE is registered at. INTERFACE is an interface 939object path SERVICE is registered at. INTERFACE is an interface
@@ -950,9 +998,7 @@ object is returned instead of a list containing this single Lisp object.
950 => "i686" 998 => "i686"
951 999
952usage: (dbus-call-method BUS SERVICE PATH INTERFACE METHOD &optional :timeout TIMEOUT &rest ARGS) */) 1000usage: (dbus-call-method BUS SERVICE PATH INTERFACE METHOD &optional :timeout TIMEOUT &rest ARGS) */)
953 (nargs, args) 1001 (int nargs, register Lisp_Object *args)
954 int nargs;
955 register Lisp_Object *args;
956{ 1002{
957 Lisp_Object bus, service, path, interface, method; 1003 Lisp_Object bus, service, path, interface, method;
958 Lisp_Object result; 1004 Lisp_Object result;
@@ -974,7 +1020,6 @@ usage: (dbus-call-method BUS SERVICE PATH INTERFACE METHOD &optional :timeout TI
974 interface = args[3]; 1020 interface = args[3];
975 method = args[4]; 1021 method = args[4];
976 1022
977 CHECK_SYMBOL (bus);
978 CHECK_STRING (service); 1023 CHECK_STRING (service);
979 CHECK_STRING (path); 1024 CHECK_STRING (path);
980 CHECK_STRING (interface); 1025 CHECK_STRING (interface);
@@ -988,7 +1033,7 @@ usage: (dbus-call-method BUS SERVICE PATH INTERFACE METHOD &optional :timeout TI
988 SDATA (method)); 1033 SDATA (method));
989 1034
990 /* Open a connection to the bus. */ 1035 /* Open a connection to the bus. */
991 connection = xd_initialize (bus); 1036 connection = xd_initialize (bus, TRUE);
992 1037
993 /* Create the message. */ 1038 /* Create the message. */
994 dmessage = dbus_message_new_method_call (SDATA (service), 1039 dmessage = dbus_message_new_method_call (SDATA (service),
@@ -1089,7 +1134,8 @@ DEFUN ("dbus-call-method-asynchronously", Fdbus_call_method_asynchronously,
1089 Sdbus_call_method_asynchronously, 6, MANY, 0, 1134 Sdbus_call_method_asynchronously, 6, MANY, 0,
1090 doc: /* Call METHOD on the D-Bus BUS asynchronously. 1135 doc: /* Call METHOD on the D-Bus BUS asynchronously.
1091 1136
1092BUS is either the symbol `:system' or the symbol `:session'. 1137BUS is either a Lisp symbol, `:system' or `:session', or a string
1138denoting the bus address.
1093 1139
1094SERVICE is the D-Bus service name to be used. PATH is the D-Bus 1140SERVICE is the D-Bus service name to be used. PATH is the D-Bus
1095object path SERVICE is registered at. INTERFACE is an interface 1141object path SERVICE is registered at. INTERFACE is an interface
@@ -1134,9 +1180,7 @@ Example:
1134 -| i686 1180 -| i686
1135 1181
1136usage: (dbus-call-method-asynchronously BUS SERVICE PATH INTERFACE METHOD HANDLER &optional :timeout TIMEOUT &rest ARGS) */) 1182usage: (dbus-call-method-asynchronously BUS SERVICE PATH INTERFACE METHOD HANDLER &optional :timeout TIMEOUT &rest ARGS) */)
1137 (nargs, args) 1183 (int nargs, register Lisp_Object *args)
1138 int nargs;
1139 register Lisp_Object *args;
1140{ 1184{
1141 Lisp_Object bus, service, path, interface, method, handler; 1185 Lisp_Object bus, service, path, interface, method, handler;
1142 Lisp_Object result; 1186 Lisp_Object result;
@@ -1157,7 +1201,6 @@ usage: (dbus-call-method-asynchronously BUS SERVICE PATH INTERFACE METHOD HANDLE
1157 method = args[4]; 1201 method = args[4];
1158 handler = args[5]; 1202 handler = args[5];
1159 1203
1160 CHECK_SYMBOL (bus);
1161 CHECK_STRING (service); 1204 CHECK_STRING (service);
1162 CHECK_STRING (path); 1205 CHECK_STRING (path);
1163 CHECK_STRING (interface); 1206 CHECK_STRING (interface);
@@ -1173,7 +1216,7 @@ usage: (dbus-call-method-asynchronously BUS SERVICE PATH INTERFACE METHOD HANDLE
1173 SDATA (method)); 1216 SDATA (method));
1174 1217
1175 /* Open a connection to the bus. */ 1218 /* Open a connection to the bus. */
1176 connection = xd_initialize (bus); 1219 connection = xd_initialize (bus, TRUE);
1177 1220
1178 /* Create the message. */ 1221 /* Create the message. */
1179 dmessage = dbus_message_new_method_call (SDATA (service), 1222 dmessage = dbus_message_new_method_call (SDATA (service),
@@ -1264,9 +1307,7 @@ DEFUN ("dbus-method-return-internal", Fdbus_method_return_internal,
1264This is an internal function, it shall not be used outside dbus.el. 1307This is an internal function, it shall not be used outside dbus.el.
1265 1308
1266usage: (dbus-method-return-internal BUS SERIAL SERVICE &rest ARGS) */) 1309usage: (dbus-method-return-internal BUS SERIAL SERVICE &rest ARGS) */)
1267 (nargs, args) 1310 (int nargs, register Lisp_Object *args)
1268 int nargs;
1269 register Lisp_Object *args;
1270{ 1311{
1271 Lisp_Object bus, serial, service; 1312 Lisp_Object bus, serial, service;
1272 struct gcpro gcpro1, gcpro2, gcpro3; 1313 struct gcpro gcpro1, gcpro2, gcpro3;
@@ -1282,7 +1323,6 @@ usage: (dbus-method-return-internal BUS SERIAL SERVICE &rest ARGS) */)
1282 serial = args[1]; 1323 serial = args[1];
1283 service = args[2]; 1324 service = args[2];
1284 1325
1285 CHECK_SYMBOL (bus);
1286 CHECK_NUMBER (serial); 1326 CHECK_NUMBER (serial);
1287 CHECK_STRING (service); 1327 CHECK_STRING (service);
1288 GCPRO3 (bus, serial, service); 1328 GCPRO3 (bus, serial, service);
@@ -1290,7 +1330,7 @@ usage: (dbus-method-return-internal BUS SERIAL SERVICE &rest ARGS) */)
1290 XD_DEBUG_MESSAGE ("%lu %s ", (unsigned long) XUINT (serial), SDATA (service)); 1330 XD_DEBUG_MESSAGE ("%lu %s ", (unsigned long) XUINT (serial), SDATA (service));
1291 1331
1292 /* Open a connection to the bus. */ 1332 /* Open a connection to the bus. */
1293 connection = xd_initialize (bus); 1333 connection = xd_initialize (bus, TRUE);
1294 1334
1295 /* Create the message. */ 1335 /* Create the message. */
1296 dmessage = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_RETURN); 1336 dmessage = dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_RETURN);
@@ -1358,9 +1398,7 @@ DEFUN ("dbus-method-error-internal", Fdbus_method_error_internal,
1358This is an internal function, it shall not be used outside dbus.el. 1398This is an internal function, it shall not be used outside dbus.el.
1359 1399
1360usage: (dbus-method-error-internal BUS SERIAL SERVICE &rest ARGS) */) 1400usage: (dbus-method-error-internal BUS SERIAL SERVICE &rest ARGS) */)
1361 (nargs, args) 1401 (int nargs, register Lisp_Object *args)
1362 int nargs;
1363 register Lisp_Object *args;
1364{ 1402{
1365 Lisp_Object bus, serial, service; 1403 Lisp_Object bus, serial, service;
1366 struct gcpro gcpro1, gcpro2, gcpro3; 1404 struct gcpro gcpro1, gcpro2, gcpro3;
@@ -1376,7 +1414,6 @@ usage: (dbus-method-error-internal BUS SERIAL SERVICE &rest ARGS) */)
1376 serial = args[1]; 1414 serial = args[1];
1377 service = args[2]; 1415 service = args[2];
1378 1416
1379 CHECK_SYMBOL (bus);
1380 CHECK_NUMBER (serial); 1417 CHECK_NUMBER (serial);
1381 CHECK_STRING (service); 1418 CHECK_STRING (service);
1382 GCPRO3 (bus, serial, service); 1419 GCPRO3 (bus, serial, service);
@@ -1384,7 +1421,7 @@ usage: (dbus-method-error-internal BUS SERIAL SERVICE &rest ARGS) */)
1384 XD_DEBUG_MESSAGE ("%lu %s ", (unsigned long) XUINT (serial), SDATA (service)); 1421 XD_DEBUG_MESSAGE ("%lu %s ", (unsigned long) XUINT (serial), SDATA (service));
1385 1422
1386 /* Open a connection to the bus. */ 1423 /* Open a connection to the bus. */
1387 connection = xd_initialize (bus); 1424 connection = xd_initialize (bus, TRUE);
1388 1425
1389 /* Create the message. */ 1426 /* Create the message. */
1390 dmessage = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR); 1427 dmessage = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
@@ -1449,7 +1486,8 @@ usage: (dbus-method-error-internal BUS SERIAL SERVICE &rest ARGS) */)
1449DEFUN ("dbus-send-signal", Fdbus_send_signal, Sdbus_send_signal, 5, MANY, 0, 1486DEFUN ("dbus-send-signal", Fdbus_send_signal, Sdbus_send_signal, 5, MANY, 0,
1450 doc: /* Send signal SIGNAL on the D-Bus BUS. 1487 doc: /* Send signal SIGNAL on the D-Bus BUS.
1451 1488
1452BUS is either the symbol `:system' or the symbol `:session'. 1489BUS is either a Lisp symbol, `:system' or `:session', or a string
1490denoting the bus address.
1453 1491
1454SERVICE is the D-Bus service name SIGNAL is sent from. PATH is the 1492SERVICE is the D-Bus service name SIGNAL is sent from. PATH is the
1455D-Bus object path SERVICE is registered at. INTERFACE is an interface 1493D-Bus object path SERVICE is registered at. INTERFACE is an interface
@@ -1475,9 +1513,7 @@ Example:
1475 "org.gnu.Emacs.FileManager" "FileModified" "/home/albinus/.emacs") 1513 "org.gnu.Emacs.FileManager" "FileModified" "/home/albinus/.emacs")
1476 1514
1477usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */) 1515usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */)
1478 (nargs, args) 1516 (int nargs, register Lisp_Object *args)
1479 int nargs;
1480 register Lisp_Object *args;
1481{ 1517{
1482 Lisp_Object bus, service, path, interface, signal; 1518 Lisp_Object bus, service, path, interface, signal;
1483 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5; 1519 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
@@ -1495,7 +1531,6 @@ usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */)
1495 interface = args[3]; 1531 interface = args[3];
1496 signal = args[4]; 1532 signal = args[4];
1497 1533
1498 CHECK_SYMBOL (bus);
1499 CHECK_STRING (service); 1534 CHECK_STRING (service);
1500 CHECK_STRING (path); 1535 CHECK_STRING (path);
1501 CHECK_STRING (interface); 1536 CHECK_STRING (interface);
@@ -1509,7 +1544,7 @@ usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */)
1509 SDATA (signal)); 1544 SDATA (signal));
1510 1545
1511 /* Open a connection to the bus. */ 1546 /* Open a connection to the bus. */
1512 connection = xd_initialize (bus); 1547 connection = xd_initialize (bus, TRUE);
1513 1548
1514 /* Create the message. */ 1549 /* Create the message. */
1515 dmessage = dbus_message_new_signal (SDATA (path), 1550 dmessage = dbus_message_new_signal (SDATA (path),
@@ -1567,15 +1602,16 @@ usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */)
1567} 1602}
1568 1603
1569/* Check, whether there is pending input in the message queue of the 1604/* Check, whether there is pending input in the message queue of the
1570 D-Bus BUS. BUS is a Lisp symbol, either :system or :session. */ 1605 D-Bus BUS. BUS is either a Lisp symbol, :system or :session, or a
1606 string denoting the bus address. */
1571int 1607int
1572xd_get_dispatch_status (bus) 1608xd_get_dispatch_status (Lisp_Object bus)
1573 Lisp_Object bus;
1574{ 1609{
1575 DBusConnection *connection; 1610 DBusConnection *connection;
1576 1611
1577 /* Open a connection to the bus. */ 1612 /* Open a connection to the bus. */
1578 connection = xd_initialize (bus); 1613 connection = xd_initialize (bus, FALSE);
1614 if (connection == NULL) return FALSE;
1579 1615
1580 /* Non blocking read of the next available message. */ 1616 /* Non blocking read of the next available message. */
1581 dbus_connection_read_write (connection, 0); 1617 dbus_connection_read_write (connection, 0);
@@ -1587,27 +1623,33 @@ xd_get_dispatch_status (bus)
1587 ? TRUE : FALSE; 1623 ? TRUE : FALSE;
1588} 1624}
1589 1625
1590/* Check for queued incoming messages from the system and session buses. */ 1626/* Check for queued incoming messages from the buses. */
1591int 1627int
1592xd_pending_messages () 1628xd_pending_messages (void)
1593{ 1629{
1630 Lisp_Object busp = Vdbus_registered_buses;
1631
1632 while (!NILP (busp))
1633 {
1634 /* We do not want to have an autolaunch for the session bus. */
1635 if (EQ ((CAR_SAFE (busp)), QCdbus_session_bus)
1636 && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL)
1637 continue;
1638
1639 if (xd_get_dispatch_status (CAR_SAFE (busp)))
1640 return TRUE;
1594 1641
1595 /* Vdbus_registered_objects_table will be initialized as hash table 1642 busp = CDR_SAFE (busp);
1596 in dbus.el. When this package isn't loaded yet, it doesn't make 1643 }
1597 sense to handle D-Bus messages. */ 1644
1598 return (HASH_TABLE_P (Vdbus_registered_objects_table) 1645 return FALSE;
1599 ? (xd_get_dispatch_status (QCdbus_system_bus)
1600 || ((getenv ("DBUS_SESSION_BUS_ADDRESS") != NULL)
1601 ? xd_get_dispatch_status (QCdbus_session_bus)
1602 : FALSE))
1603 : FALSE);
1604} 1646}
1605 1647
1606/* Read queued incoming message of the D-Bus BUS. BUS is a Lisp 1648/* Read queued incoming message of the D-Bus BUS. BUS is either a
1607 symbol, either :system or :session. */ 1649 Lisp symbol, :system or :session, or a string denoting the bus
1650 address. */
1608static Lisp_Object 1651static Lisp_Object
1609xd_read_message (bus) 1652xd_read_message (Lisp_Object bus)
1610 Lisp_Object bus;
1611{ 1653{
1612 Lisp_Object args, key, value; 1654 Lisp_Object args, key, value;
1613 struct gcpro gcpro1; 1655 struct gcpro gcpro1;
@@ -1620,7 +1662,7 @@ xd_read_message (bus)
1620 const char *uname, *path, *interface, *member; 1662 const char *uname, *path, *interface, *member;
1621 1663
1622 /* Open a connection to the bus. */ 1664 /* Open a connection to the bus. */
1623 connection = xd_initialize (bus); 1665 connection = xd_initialize (bus, TRUE);
1624 1666
1625 /* Non blocking read of the next available message. */ 1667 /* Non blocking read of the next available message. */
1626 dbus_connection_read_write (connection, 0); 1668 dbus_connection_read_write (connection, 0);
@@ -1762,29 +1804,28 @@ xd_read_message (bus)
1762 RETURN_UNGCPRO (Qnil); 1804 RETURN_UNGCPRO (Qnil);
1763} 1805}
1764 1806
1765/* Read queued incoming messages from the system and session buses. */ 1807/* Read queued incoming messages from all buses. */
1766void 1808void
1767xd_read_queued_messages () 1809xd_read_queued_messages (void)
1768{ 1810{
1811 Lisp_Object busp = Vdbus_registered_buses;
1769 1812
1770 /* Vdbus_registered_objects_table will be initialized as hash table 1813 xd_in_read_queued_messages = 1;
1771 in dbus.el. When this package isn't loaded yet, it doesn't make 1814 while (!NILP (busp))
1772 sense to handle D-Bus messages. Furthermore, we ignore all Lisp
1773 errors during the call. */
1774 if (HASH_TABLE_P (Vdbus_registered_objects_table))
1775 { 1815 {
1776 xd_in_read_queued_messages = 1; 1816 /* We ignore all Lisp errors during the call. */
1777 internal_catch (Qdbus_error, xd_read_message, QCdbus_system_bus); 1817 internal_catch (Qdbus_error, xd_read_message, CAR_SAFE (busp));
1778 internal_catch (Qdbus_error, xd_read_message, QCdbus_session_bus); 1818 busp = CDR_SAFE (busp);
1779 xd_in_read_queued_messages = 0;
1780 } 1819 }
1820 xd_in_read_queued_messages = 0;
1781} 1821}
1782 1822
1783DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal, 1823DEFUN ("dbus-register-signal", Fdbus_register_signal, Sdbus_register_signal,
1784 6, MANY, 0, 1824 6, MANY, 0,
1785 doc: /* Register for signal SIGNAL on the D-Bus BUS. 1825 doc: /* Register for signal SIGNAL on the D-Bus BUS.
1786 1826
1787BUS is either the symbol `:system' or the symbol `:session'. 1827BUS is either a Lisp symbol, `:system' or `:session', or a string
1828denoting the bus address.
1788 1829
1789SERVICE is the D-Bus service name used by the sending D-Bus object. 1830SERVICE is the D-Bus service name used by the sending D-Bus object.
1790It can be either a known name or the unique name of the D-Bus object 1831It can be either a known name or the unique name of the D-Bus object
@@ -1819,9 +1860,7 @@ INTERFACE, SIGNAL and HANDLER must not be nil. Example:
1819`dbus-unregister-object' for removing the registration. 1860`dbus-unregister-object' for removing the registration.
1820 1861
1821usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARGS) */) 1862usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARGS) */)
1822 (nargs, args) 1863 (int nargs, register Lisp_Object *args)
1823 int nargs;
1824 register Lisp_Object *args;
1825{ 1864{
1826 Lisp_Object bus, service, path, interface, signal, handler; 1865 Lisp_Object bus, service, path, interface, signal, handler;
1827 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6; 1866 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
@@ -1840,7 +1879,6 @@ usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARG
1840 signal = args[4]; 1879 signal = args[4];
1841 handler = args[5]; 1880 handler = args[5];
1842 1881
1843 CHECK_SYMBOL (bus);
1844 if (!NILP (service)) CHECK_STRING (service); 1882 if (!NILP (service)) CHECK_STRING (service);
1845 if (!NILP (path)) CHECK_STRING (path); 1883 if (!NILP (path)) CHECK_STRING (path);
1846 CHECK_STRING (interface); 1884 CHECK_STRING (interface);
@@ -1872,7 +1910,7 @@ usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARG
1872 if (NILP (uname) || (SBYTES (uname) > 0)) 1910 if (NILP (uname) || (SBYTES (uname) > 0))
1873 { 1911 {
1874 /* Open a connection to the bus. */ 1912 /* Open a connection to the bus. */
1875 connection = xd_initialize (bus); 1913 connection = xd_initialize (bus, TRUE);
1876 1914
1877 /* Create a rule to receive related signals. */ 1915 /* Create a rule to receive related signals. */
1878 sprintf (rule, 1916 sprintf (rule,
@@ -1933,7 +1971,8 @@ DEFUN ("dbus-register-method", Fdbus_register_method, Sdbus_register_method,
1933 6, 6, 0, 1971 6, 6, 0,
1934 doc: /* Register for method METHOD on the D-Bus BUS. 1972 doc: /* Register for method METHOD on the D-Bus BUS.
1935 1973
1936BUS is either the symbol `:system' or the symbol `:session'. 1974BUS is either a Lisp symbol, `:system' or `:session', or a string
1975denoting the bus address.
1937 1976
1938SERVICE is the D-Bus service name of the D-Bus object METHOD is 1977SERVICE is the D-Bus service name of the D-Bus object METHOD is
1939registered for. It must be a known name. 1978registered for. It must be a known name.
@@ -1943,8 +1982,7 @@ interface offered by SERVICE. It must provide METHOD. HANDLER is a
1943Lisp function to be called when a method call is received. It must 1982Lisp function to be called when a method call is received. It must
1944accept the input arguments of METHOD. The return value of HANDLER is 1983accept the input arguments of METHOD. The return value of HANDLER is
1945used for composing the returning D-Bus message. */) 1984used for composing the returning D-Bus message. */)
1946 (bus, service, path, interface, method, handler) 1985 (Lisp_Object bus, Lisp_Object service, Lisp_Object path, Lisp_Object interface, Lisp_Object method, Lisp_Object handler)
1947 Lisp_Object bus, service, path, interface, method, handler;
1948{ 1986{
1949 Lisp_Object key, key1, value; 1987 Lisp_Object key, key1, value;
1950 DBusConnection *connection; 1988 DBusConnection *connection;
@@ -1952,7 +1990,6 @@ used for composing the returning D-Bus message. */)
1952 DBusError derror; 1990 DBusError derror;
1953 1991
1954 /* Check parameters. */ 1992 /* Check parameters. */
1955 CHECK_SYMBOL (bus);
1956 CHECK_STRING (service); 1993 CHECK_STRING (service);
1957 CHECK_STRING (path); 1994 CHECK_STRING (path);
1958 CHECK_STRING (interface); 1995 CHECK_STRING (interface);
@@ -1963,7 +2000,7 @@ used for composing the returning D-Bus message. */)
1963 a segmentation fault. */ 2000 a segmentation fault. */
1964 2001
1965 /* Open a connection to the bus. */ 2002 /* Open a connection to the bus. */
1966 connection = xd_initialize (bus); 2003 connection = xd_initialize (bus, TRUE);
1967 2004
1968 /* Request the known name from the bus. We can ignore the result, 2005 /* Request the known name from the bus. We can ignore the result,
1969 it is set to -1 if there is an error - kind of redundancy. */ 2006 it is set to -1 if there is an error - kind of redundancy. */
@@ -1990,13 +2027,17 @@ used for composing the returning D-Bus message. */)
1990 2027
1991 2028
1992void 2029void
1993syms_of_dbusbind () 2030syms_of_dbusbind (void)
1994{ 2031{
1995 2032
1996 Qdbus_init_bus = intern_c_string ("dbus-init-bus"); 2033 Qdbus_init_bus = intern_c_string ("dbus-init-bus");
1997 staticpro (&Qdbus_init_bus); 2034 staticpro (&Qdbus_init_bus);
1998 defsubr (&Sdbus_init_bus); 2035 defsubr (&Sdbus_init_bus);
1999 2036
2037 Qdbus_close_bus = intern_c_string ("dbus-close-bus");
2038 staticpro (&Qdbus_close_bus);
2039 defsubr (&Sdbus_close_bus);
2040
2000 Qdbus_get_unique_name = intern_c_string ("dbus-get-unique-name"); 2041 Qdbus_get_unique_name = intern_c_string ("dbus-get-unique-name");
2001 staticpro (&Qdbus_get_unique_name); 2042 staticpro (&Qdbus_get_unique_name);
2002 defsubr (&Sdbus_get_unique_name); 2043 defsubr (&Sdbus_get_unique_name);
@@ -2093,18 +2134,25 @@ syms_of_dbusbind ()
2093 QCdbus_type_dict_entry = intern_c_string (":dict-entry"); 2134 QCdbus_type_dict_entry = intern_c_string (":dict-entry");
2094 staticpro (&QCdbus_type_dict_entry); 2135 staticpro (&QCdbus_type_dict_entry);
2095 2136
2137 DEFVAR_LISP ("dbus-registered-buses",
2138 &Vdbus_registered_buses,
2139 doc: /* List of D-Bus buses we are polling for messages. */);
2140 Vdbus_registered_buses = Qnil;
2141
2096 DEFVAR_LISP ("dbus-registered-objects-table", 2142 DEFVAR_LISP ("dbus-registered-objects-table",
2097 &Vdbus_registered_objects_table, 2143 &Vdbus_registered_objects_table,
2098 doc: /* Hash table of registered functions for D-Bus. 2144 doc: /* Hash table of registered functions for D-Bus.
2145
2099There are two different uses of the hash table: for accessing 2146There are two different uses of the hash table: for accessing
2100registered interfaces properties, targeted by signals or method calls, 2147registered interfaces properties, targeted by signals or method calls,
2101and for calling handlers in case of non-blocking method call returns. 2148and for calling handlers in case of non-blocking method call returns.
2102 2149
2103In the first case, the key in the hash table is the list (BUS 2150In the first case, the key in the hash table is the list (BUS
2104INTERFACE MEMBER). BUS is either the symbol `:system' or the symbol 2151INTERFACE MEMBER). BUS is either a Lisp symbol, `:system' or
2105`:session'. INTERFACE is a string which denotes a D-Bus interface, 2152`:session', or a string denoting the bus address. INTERFACE is a
2106and MEMBER, also a string, is either a method, a signal or a property 2153string which denotes a D-Bus interface, and MEMBER, also a string, is
2107INTERFACE is offering. All arguments but BUS must not be nil. 2154either a method, a signal or a property INTERFACE is offering. All
2155arguments but BUS must not be nil.
2108 2156
2109The value in the hash table is a list of quadruple lists 2157The value in the hash table is a list of quadruple lists
2110\((UNAME SERVICE PATH OBJECT) (UNAME SERVICE PATH OBJECT) ...). 2158\((UNAME SERVICE PATH OBJECT) (UNAME SERVICE PATH OBJECT) ...).
@@ -2116,15 +2164,18 @@ be called when a D-Bus message, which matches the key criteria,
2116arrives (methods and signals), or a cons cell containing the value of 2164arrives (methods and signals), or a cons cell containing the value of
2117the property. 2165the property.
2118 2166
2119In the second case, the key in the hash table is the list (BUS SERIAL). 2167In the second case, the key in the hash table is the list (BUS
2120BUS is either the symbol `:system' or the symbol `:session'. SERIAL 2168SERIAL). BUS is either a Lisp symbol, `:system' or `:session', or a
2121is the serial number of the non-blocking method call, a reply is 2169string denoting the bus address. SERIAL is the serial number of the
2122expected. Both arguments must not be nil. The value in the hash 2170non-blocking method call, a reply is expected. Both arguments must
2123table is HANDLER, the function to be called when the D-Bus reply 2171not be nil. The value in the hash table is HANDLER, the function to
2124message arrives. */); 2172be called when the D-Bus reply message arrives. */);
2125 /* We initialize Vdbus_registered_objects_table in dbus.el, because 2173 {
2126 we need to define a hash table function first. */ 2174 Lisp_Object args[2];
2127 Vdbus_registered_objects_table = Qnil; 2175 args[0] = QCtest;
2176 args[1] = Qequal;
2177 Vdbus_registered_objects_table = Fmake_hash_table (2, args);
2178 }
2128 2179
2129 DEFVAR_LISP ("dbus-debug", &Vdbus_debug, 2180 DEFVAR_LISP ("dbus-debug", &Vdbus_debug,
2130 doc: /* If non-nil, debug messages of D-Bus bindings are raised. */); 2181 doc: /* If non-nil, debug messages of D-Bus bindings are raised. */);