]> code.delx.au - gnu-emacs/blobdiff - src/dbusbind.c
Update copyright year to 2015
[gnu-emacs] / src / dbusbind.c
index c1b1310454c7b0fd40a1950128b3c009e7fb08b3..9de694954d4362bed881047c9e19ce0ca2011b67 100644 (file)
@@ -1,5 +1,5 @@
 /* Elisp bindings for D-Bus.
-   Copyright (C) 2007-2012 Free Software Foundation, Inc.
+   Copyright (C) 2007-2015 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -21,7 +21,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #ifdef HAVE_DBUS
 #include <stdio.h>
 #include <dbus/dbus.h>
-#include <setjmp.h>
+
 #include "lisp.h"
 #include "frame.h"
 #include "termhooks.h"
@@ -32,9 +32,17 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #define DBUS_NUM_MESSAGE_TYPES 5
 #endif
 
+
+/* Some platforms define the symbol "interface", but we want to use it
+ * as a variable name below.  */
+
+#ifdef interface
+#undef interface
+#endif
+
 \f
 /* Subroutines.  */
-static Lisp_Object Qdbus_init_bus;
+static Lisp_Object Qdbus__init_bus;
 static Lisp_Object Qdbus_get_unique_name;
 static Lisp_Object Qdbus_message_internal;
 
@@ -70,7 +78,7 @@ static Lisp_Object QCdbus_registered_signal;
 static Lisp_Object xd_registered_buses;
 
 /* Whether we are reading a D-Bus event.  */
-static int xd_in_read_queued_messages = 0;
+static bool xd_in_read_queued_messages = 0;
 
 \f
 /* We use "xd_" and "XD_" as prefix for all internal symbols, because
@@ -134,7 +142,7 @@ static int xd_in_read_queued_messages = 0;
   } while (0)
 
 #else /* !DBUS_DEBUG */
-#define XD_DEBUG_MESSAGE(...)                                          \
+# define XD_DEBUG_MESSAGE(...)                                         \
   do {                                                                 \
     if (!NILP (Vdbus_debug))                                           \
       {                                                                        \
@@ -143,7 +151,7 @@ static int xd_in_read_queued_messages = 0;
        message ("%s: %s", __func__, s);                                \
       }                                                                        \
   } while (0)
-#define XD_DEBUG_VALID_LISP_OBJECT_P(object)
+# define XD_DEBUG_VALID_LISP_OBJECT_P(object)
 #endif
 
 /* Check whether TYPE is a basic DBusType.  */
@@ -291,8 +299,8 @@ xd_symbol_to_dbus_type (Lisp_Object object)
       }                                                                        \
   } while (0)
 
-#if (HAVE_DBUS_VALIDATE_BUS_NAME || HAVE_DBUS_VALIDATE_PATH \
-     || XD_DBUS_VALIDATE_OBJECT || HAVE_DBUS_VALIDATE_MEMBER)
+#if (HAVE_DBUS_VALIDATE_BUS_NAME || HAVE_DBUS_VALIDATE_PATH            \
+     || HAVE_DBUS_VALIDATE_INTERFACE || HAVE_DBUS_VALIDATE_MEMBER)
 #define XD_DBUS_VALIDATE_OBJECT(object, func)                          \
   do {                                                                 \
     if (!NILP (object))                                                        \
@@ -349,7 +357,7 @@ xd_signature_cat (char *signature, char const *x)
   ptrdiff_t xlen = strlen (x);
   if (DBUS_MAXIMUM_SIGNATURE_LENGTH - xlen <= siglen)
     string_overflow ();
-  strcat (signature, x);
+  strcpy (signature + siglen, x);
 }
 
 /* Compute SIGNATURE of OBJECT.  It must have a form that it can be
@@ -753,7 +761,7 @@ xd_append_arg (int dtype, Lisp_Object object, DBusMessageIter *iter)
                && STRINGP (CAR_SAFE (XD_NEXT_VALUE (object)))
                && NILP (CDR_SAFE (XD_NEXT_VALUE (object))))
              {
-               strcpy (signature, SSDATA (CAR_SAFE (XD_NEXT_VALUE (object))));
+               lispstpcpy (signature, CAR_SAFE (XD_NEXT_VALUE (object)));
                object = CDR_SAFE (XD_NEXT_VALUE (object));
              }
 
@@ -874,7 +882,7 @@ xd_retrieve_arg (int dtype, DBusMessageIter *iter)
 #endif
       {
        dbus_uint32_t val;
-       unsigned int pval = val;
+       unsigned int pval;
        dbus_message_iter_get_basic (iter, &val);
        pval = val;
        XD_DEBUG_MESSAGE ("%c %u", dtype, pval);
@@ -961,6 +969,13 @@ xd_get_connection_references (DBusConnection *connection)
   return *refcount;
 }
 
+/* Convert a Lisp D-Bus object to a pointer.  */
+static DBusConnection*
+xd_lisp_dbus_to_dbus (Lisp_Object bus)
+{
+  return (DBusConnection *) (intptr_t) XFASTINT (bus);
+}
+
 /* Return D-Bus connection address.  BUS is either a Lisp symbol,
    :system or :session, or a string denoting the bus address.  */
 static DBusConnection *
@@ -973,7 +988,7 @@ xd_get_connection_address (Lisp_Object bus)
   if (NILP (val))
     XD_SIGNAL2 (build_string ("No connection to bus"), bus);
   else
-    connection = (DBusConnection *) (intptr_t) XFASTINT (val);
+    connection = xd_lisp_dbus_to_dbus (val);
 
   if (!dbus_connection_get_is_connected (connection))
     XD_SIGNAL2 (build_string ("No connection to bus"), bus);
@@ -997,8 +1012,7 @@ xd_find_watch_fd (DBusWatch *watch)
 }
 
 /* Prototype.  */
-static void
-xd_read_queued_messages (int fd, void *data, int for_read);
+static void xd_read_queued_messages (int fd, void *data);
 
 /* Start monitoring WATCH for possible I/O.  */
 static dbus_bool_t
@@ -1039,11 +1053,14 @@ xd_remove_watch (DBusWatch *watch, void *data)
     return;
 
   /* Unset session environment.  */
+#if 0
+  /* This is buggy, since unsetenv is not thread-safe.  */
   if (XSYMBOL (QCdbus_session_bus) == data)
     {
-      //      XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS");
-      //      unsetenv ("DBUS_SESSION_BUS_ADDRESS");
+      XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS");
+      unsetenv ("DBUS_SESSION_BUS_ADDRESS");
     }
+#endif
 
   if (flags & DBUS_WATCH_WRITABLE)
     delete_write_fd (fd);
@@ -1067,14 +1084,21 @@ xd_close_bus (Lisp_Object bus)
 {
   DBusConnection *connection;
   Lisp_Object val;
+  Lisp_Object busobj;
 
   /* Check whether we are connected.  */
   val = Fassoc (bus, xd_registered_buses);
   if (NILP (val))
     return;
 
+  busobj = CDR_SAFE (val);
+  if (NILP (busobj)) {
+    xd_registered_buses = Fdelete (val, xd_registered_buses);
+    return;
+  }
+
   /* Retrieve bus address.  */
-  connection = xd_get_connection_address (bus);
+  connection = xd_lisp_dbus_to_dbus (busobj);
 
   if (xd_get_connection_references (connection) == 1)
     {
@@ -1094,9 +1118,12 @@ xd_close_bus (Lisp_Object bus)
   return;
 }
 
-DEFUN ("dbus-init-bus", Fdbus_init_bus, Sdbus_init_bus, 1, 2, 0,
+DEFUN ("dbus--init-bus", Fdbus__init_bus, Sdbus__init_bus, 1, 2, 0,
        doc: /* Establish the connection to D-Bus BUS.
 
+This function is dbus internal.  You almost certainly want to use
+`dbus-init-bus'.
+
 BUS can be either the symbol `:system' or the symbol `:session', or it
 can be a string denoting the address of the corresponding bus.  For
 the system and session buses, this function is called when loading
@@ -1193,9 +1220,6 @@ this connection to those buses.  */)
       XSETFASTINT (val, (intptr_t) connection);
       xd_registered_buses = Fcons (Fcons (bus, val), xd_registered_buses);
 
-      /* We do not want to abort.  */
-      putenv ((char *) "DBUS_FATAL_WARNINGS=0");
-
       /* Cleanup.  */
       dbus_error_free (&derror);
     }
@@ -1684,7 +1708,7 @@ xd_read_message (Lisp_Object bus)
 
 /* Callback called when something is ready to read or write.  */
 static void
-xd_read_queued_messages (int fd, void *data, int for_read)
+xd_read_queued_messages (int fd, void *data)
 {
   Lisp_Object busp = xd_registered_buses;
   Lisp_Object bus = Qnil;
@@ -1711,12 +1735,19 @@ xd_read_queued_messages (int fd, void *data, int for_read)
 }
 
 \f
+void
+init_dbusbind (void)
+{
+  /* We do not want to abort.  */
+  xputenv ("DBUS_FATAL_WARNINGS=0");
+}
+
 void
 syms_of_dbusbind (void)
 {
 
-  DEFSYM (Qdbus_init_bus, "dbus-init-bus");
-  defsubr (&Sdbus_init_bus);
+  DEFSYM (Qdbus__init_bus, "dbus--init-bus");
+  defsubr (&Sdbus__init_bus);
 
   DEFSYM (Qdbus_get_unique_name, "dbus-get-unique-name");
   defsubr (&Sdbus_get_unique_name);