]> code.delx.au - gnu-emacs/blobdiff - src/dbusbind.c
Add entries for the EDE and EIEIO manuals.
[gnu-emacs] / src / dbusbind.c
index 76b0da542053a7b3fd6b98549207f2c02af09fc6..d0233fdab435cfa9ff521172ee2119bc30c75aee 100644 (file)
@@ -29,6 +29,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 \f
 /* Subroutines.  */
+Lisp_Object Qdbus_init_bus;
 Lisp_Object Qdbus_get_unique_name;
 Lisp_Object Qdbus_call_method;
 Lisp_Object Qdbus_call_method_asynchronously;
@@ -698,13 +699,17 @@ xd_initialize (bus)
 {
   DBusConnection *connection;
   DBusError derror;
-  int fd;
 
   /* Parameter check.  */
   CHECK_SYMBOL (bus);
-  if (!((EQ (bus, QCdbus_system_bus)) || (EQ (bus, QCdbus_session_bus))))
+  if (!(EQ (bus, QCdbus_system_bus) || EQ (bus, QCdbus_session_bus)))
     XD_SIGNAL2 (build_string ("Wrong bus name"), bus);
 
+  /* We do not want to have an autolaunch for the session bus.  */
+  if (EQ (bus, QCdbus_session_bus)
+      && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL)
+    XD_SIGNAL2 (build_string ("No connection to bus"), bus);
+
   /* Open a connection to the bus.  */
   dbus_error_init (&derror);
 
@@ -717,12 +722,7 @@ xd_initialize (bus)
     XD_ERROR (derror);
 
   if (connection == NULL)
-    XD_SIGNAL2 (build_string ("No connection"), bus);
-
-  /* Add connection file descriptor to input_wait_mask, in order to
-     let select() detect, whether a new message has been arrived.  */
-  if (dbus_connection_get_unix_fd (connection, &fd))
-    add_keyboard_wait_descriptor (fd);
+    XD_SIGNAL2 (build_string ("No connection to bus"), bus);
 
   /* Cleanup.  */
   dbus_error_free (&derror);
@@ -731,6 +731,93 @@ xd_initialize (bus)
   return connection;
 }
 
+
+/* Add connection file descriptor to input_wait_mask, in order to
+   let select() detect, whether a new message has been arrived.  */
+dbus_bool_t
+xd_add_watch (watch, data)
+     DBusWatch *watch;
+     void *data;
+{
+  /* We check only for incoming data.  */
+  if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE)
+    {
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+      /* TODO: Reverse these on Win32, which prefers the opposite. */
+      int fd = dbus_watch_get_unix_fd(watch);
+      if (fd == -1)
+       fd = dbus_watch_get_socket(watch);
+#else
+      int fd = dbus_watch_get_fd(watch);
+#endif
+      XD_DEBUG_MESSAGE ("%d", fd);
+
+      if (fd == -1)
+       return FALSE;
+
+      /* Add the file descriptor to input_wait_mask.  */
+      add_keyboard_wait_descriptor (fd);
+    }
+
+  /* Return.  */
+  return TRUE;
+}
+
+/* Remove connection file descriptor from input_wait_mask.  */
+void
+xd_remove_watch (watch, data)
+     DBusWatch *watch;
+     void *data;
+{
+  /* We check only for incoming data.  */
+  if (dbus_watch_get_flags (watch) & DBUS_WATCH_READABLE)
+    {
+#if HAVE_DBUS_WATCH_GET_UNIX_FD
+      /* TODO: Reverse these on Win32, which prefers the opposite. */
+      int fd = dbus_watch_get_unix_fd(watch);
+      if (fd == -1)
+       fd = dbus_watch_get_socket(watch);
+#else
+      int fd = dbus_watch_get_fd(watch);
+#endif
+      XD_DEBUG_MESSAGE ("%d", fd);
+
+      if (fd == -1)
+       return;
+
+      /* Remove the file descriptor from input_wait_mask.  */
+      delete_keyboard_wait_descriptor (fd);
+    }
+
+  /* Return.  */
+  return;
+}
+
+DEFUN ("dbus-init-bus", Fdbus_init_bus, Sdbus_init_bus, 1, 1, 0,
+       doc: /* Initialize connection to D-Bus BUS.
+This is an internal function, it shall not be used outside dbus.el.  */)
+     (bus)
+     Lisp_Object bus;
+{
+  DBusConnection *connection;
+
+  /* Check parameters.  */
+  CHECK_SYMBOL (bus);
+
+  /* Open a connection to the bus.  */
+  connection = xd_initialize (bus);
+
+  /* Add the watch functions.  */
+  if (!dbus_connection_set_watch_functions (connection,
+                                           xd_add_watch,
+                                           xd_remove_watch,
+                                           NULL, NULL, NULL))
+    XD_SIGNAL1 (build_string ("Cannot add watch functions"));
+
+  /* Return.  */
+  return Qnil;
+}
+
 DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name, Sdbus_get_unique_name,
        1, 1, 0,
        doc: /* Return the unique name of Emacs registered at D-Bus BUS.  */)
@@ -766,7 +853,7 @@ offered by SERVICE.  It must provide METHOD.
 
 If the parameter `:timeout' is given, the following integer TIMEOUT
 specifies the maximun number of milliseconds the method call must
-return.  The default value is 25.000.  If the method call doesn't
+return.  The default value is 25,000.  If the method call doesn't
 return in time, a D-Bus error is raised.
 
 All other arguments ARGS are passed to METHOD as arguments.  They are
@@ -974,7 +1061,7 @@ be expected.
 
 If the parameter `:timeout' is given, the following integer TIMEOUT
 specifies the maximun number of milliseconds the method call must
-return.  The default value is 25.000.  If the method call doesn't
+return.  The default value is 25,000.  If the method call doesn't
 return in time, a D-Bus error is raised.
 
 All other arguments ARGS are passed to METHOD as arguments.  They are
@@ -1469,8 +1556,10 @@ xd_pending_messages ()
      table in dbus.el.  When this package isn't loaded yet, it doesn't
      make sense to handle D-Bus messages.  */
   return (HASH_TABLE_P (Vdbus_registered_functions_table)
-         ? ((xd_get_dispatch_status (QCdbus_system_bus)
-             || (xd_get_dispatch_status (QCdbus_session_bus))))
+         ? (xd_get_dispatch_status (QCdbus_system_bus)
+            || ((getenv ("DBUS_SESSION_BUS_ADDRESS") != NULL)
+                ? xd_get_dispatch_status (QCdbus_session_bus)
+                : FALSE))
          : FALSE);
 }
 
@@ -1865,6 +1954,10 @@ void
 syms_of_dbusbind ()
 {
 
+  Qdbus_init_bus = intern ("dbus-init-bus");
+  staticpro (&Qdbus_init_bus);
+  defsubr (&Sdbus_init_bus);
+
   Qdbus_get_unique_name = intern ("dbus-get-unique-name");
   staticpro (&Qdbus_get_unique_name);
   defsubr (&Sdbus_get_unique_name);