2002-12-16 Anders Carlsson <andersca@codefactory.se>
authorAnders Carlsson <andersca@codefactory.se>
Mon, 16 Dec 2002 00:26:05 +0000 (00:26 +0000)
committerAnders Carlsson <andersca@codefactory.se>
Mon, 16 Dec 2002 00:26:05 +0000 (00:26 +0000)
* Makefile.am:
* configure.in:
Add GLib checks and fixup .pc files

* glib/Makefile.am:
* glib/dbus-glib.h:
* glib/dbus-gmain.c: (gdbus_connection_prepare),
(gdbus_connection_check), (gdbus_connection_dispatch),
(gdbus_add_connection_watch), (gdbus_remove_connection_watch),
(dbus_connection_gsource_new):
* glib/dbus-gthread.c: (dbus_gmutex_new), (dbus_gmutex_free),
(dbus_gmutex_lock), (dbus_gmutex_unlock), (dbus_gthread_init):
* glib/test-dbus-glib.c: (message_handler), (main):
Add GLib support.

ChangeLog
Makefile.am
configure.in
glib/Makefile.am [new file with mode: 0644]
glib/dbus-glib.h [new file with mode: 0644]
glib/dbus-gmain.c [new file with mode: 0644]
glib/dbus-gthread.c [new file with mode: 0644]
glib/test-dbus-glib.c [new file with mode: 0644]

index 1b42540..7c4198f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2002-12-16  Anders Carlsson  <andersca@codefactory.se>
+
+       * Makefile.am:
+       * configure.in:
+       Add GLib checks and fixup .pc files
+       
+       * glib/Makefile.am:
+       * glib/dbus-glib.h:
+       * glib/dbus-gmain.c: (gdbus_connection_prepare),
+       (gdbus_connection_check), (gdbus_connection_dispatch),
+       (gdbus_add_connection_watch), (gdbus_remove_connection_watch),
+       (dbus_connection_gsource_new):
+       * glib/dbus-gthread.c: (dbus_gmutex_new), (dbus_gmutex_free),
+       (dbus_gmutex_lock), (dbus_gmutex_unlock), (dbus_gthread_init):
+       * glib/test-dbus-glib.c: (message_handler), (main):
+       Add GLib support.
+       
 2002-12-15  Harri Porten  <porten@kde.org>
 
        * autogen.sh: check for libtoolize before attempting to use it 
index e1450cd..dc120d3 100644 (file)
@@ -1,4 +1,21 @@
 
-SUBDIRS=dbus bus test doc
+if HAVE_GLIB
+    GLIB_SUBDIR=glib
+    GLIB_PC=dbus-glib-1.0.pc
+endif
+
+SUBDIRS=dbus $(GLIB_SUBDIR) bus test doc 
+
+pkgconfigdir = $(libdir)/pkgconfig
+pkgconfig_DATA = dbus-1.0.pc $(GLIB_PC)
+
+DISTCLEANFILES =               \
+       dbus-1.0.pc             \
+       $(GLIB_PC)
+
+EXTRA_DIST =                   \
+       dbus-1.0.pc.in          \
+       dbus-glib-1.0.pc.in
 
 all-local: Doxyfile
+
index 4440040..9806da9 100644 (file)
@@ -21,8 +21,8 @@ AC_ISC_POSIX
 AC_HEADER_STDC
 AM_PROG_LIBTOOL
 
-AC_ARG_ENABLE(qt,      [  --disable-qt      disable Qt-friendly client library],enable_qt=no,enable_qt=yes)
-AC_ARG_ENABLE(glib,    [  --disable-glib    disable GLib-friendly client library],enable_glib=no,enable_glib=yes)
+AC_ARG_ENABLE(qt,      [  --enable-qt      enable Qt-friendly client library],enable_qt=$enableval,enable_qt=no)
+AC_ARG_ENABLE(glib,    [  --enable-glib    enable GLib-friendly client library],enable_glib=$enableval,enable_glib=auto)
 AC_ARG_ENABLE(tests,   [  --enable-tests enable unit test code],enable_tests=yes,enable_tests=no)
 AC_ARG_ENABLE(ansi,    [  --enable-ansi enable -ansi -pedantic gcc flags],enable_ansi=yes,enable_ansi=no)
 
@@ -121,14 +121,39 @@ DBUS_TEST_LIBS=
 AC_SUBST(DBUS_TEST_CFLAGS)
 AC_SUBST(DBUS_TEST_LIBS)
 
+# Glib detection
+PKG_CHECK_MODULES(DBUS_GLIB, glib-2.0, have_glib=yes, have_glib=no)
+
+if test x$have_glib = xno ; then
+    AC_MSG_WARN([GLib development libraries not found])
+fi
+
+if test x$enable_glib = xyes; then
+    if test x$have_glib = xno; then
+       AC_MSG_ERROR([GLib explicitly required, and GLib development libraries not found])
+    fi
+fi
+
+if test x$enable_glib = xno; then
+   have_glib=no;
+fi
+
+AM_CONDITIONAL(HAVE_GLIB, test x$have_glib = xyes)
+
+dnl GLib flags
+AC_SUBST(DBUS_GLIB_CFLAGS)
+AC_SUBST(DBUS_GLIB_LIBS)
+
 AC_OUTPUT([
 Makefile
 Doxyfile
 dbus/Makefile
+glib/Makefile
 bus/Makefile
 test/Makefile
 doc/Makefile
 dbus-1.0.pc
+dbus-glib-1.0.pc
 ])
 
 dnl ==========================================================================
@@ -142,7 +167,7 @@ echo "
 
         Building unit tests:    ${enable_tests}
         Building Qt bindings:   ${enable_qt}
-        Building GLib bindings: ${enable_glib}
+        Building GLib bindings: ${have_glib}
 "
 
 if test x$enable_tests = xyes; then
@@ -151,7 +176,4 @@ fi
 if test x$enable_qt = xyes; then
         echo "NOTE: Qt bindings don't actually exist yet"
 fi
-if test x$enable_glib = xyes; then
-        echo "NOTE: GLib bindings don't actually exist yet"
-fi
 
diff --git a/glib/Makefile.am b/glib/Makefile.am
new file mode 100644 (file)
index 0000000..6f3906c
--- /dev/null
@@ -0,0 +1,26 @@
+INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS)
+
+dbusincludedir=$(includedir)/dbus-1.0/dbus
+
+lib_LTLIBRARIES=libdbus-glib-1.la
+
+dbusinclude_HEADERS=                           \
+       dbus-glib.h
+
+libdbus_glib_1_la_SOURCES =                    \
+       dbus-gmain.c                            \
+       dbus-gthread.c
+
+libdbus_glib_1_la_LIBADD= $(DBUS_GLIB_LIBS) $(top_builddir)/dbus/libdbus-1.la
+
+
+if DBUS_BUILD_TESTS
+
+noinst_PROGRAMS= test-dbus-glib
+
+test_dbus_glib_SOURCES=                                \
+       test-dbus-glib.c                                
+
+test_dbus_glib_LDADD= $(top_builddir)/glib/libdbus-glib-1.la
+
+endif
diff --git a/glib/dbus-glib.h b/glib/dbus-glib.h
new file mode 100644 (file)
index 0000000..a6633da
--- /dev/null
@@ -0,0 +1,40 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-glib.h GLib integration
+ *
+ * Copyright (C) 2002  CodeFactory AB
+ *
+ * Licensed under the Academic Free License version 1.2
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+#ifndef DBUS_GLIB_H
+#define DBUS_GLIB_H
+
+#include <dbus/dbus.h>
+#include <glib.h>
+
+typedef void (*DBusMessageHandler) (DBusConnection *connection,
+                                   DBusMessage    *message,
+                                   gpointer        data);
+
+void gdbus_threads_init (void);
+void dbus_glib_init     (void);
+
+
+GSource *dbus_connection_gsource_new (DBusConnection *connection);
+
+
+#endif /* DBUS_GLIB_H */
diff --git a/glib/dbus-gmain.c b/glib/dbus-gmain.c
new file mode 100644 (file)
index 0000000..0b9ac9f
--- /dev/null
@@ -0,0 +1,194 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-gmain.c GLib main loop integration
+ *
+ * Copyright (C) 2002  CodeFactory AB
+ *
+ * Licensed under the Academic Free License version 1.2
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include "dbus-glib.h"
+#include <glib.h>
+
+typedef struct _DBusGSource DBusGSource;
+
+struct _DBusGSource
+{
+  GSource source;
+
+  DBusConnection *connection;
+
+  GList *poll_fds;
+  GHashTable *watches;
+};
+
+static gboolean gdbus_connection_prepare  (GSource     *source,
+                                          gint        *timeout);
+static gboolean gdbus_connection_check    (GSource     *source);
+static gboolean gdbus_connection_dispatch (GSource     *source,
+                                          GSourceFunc  callback,
+                                          gpointer     user_data);
+
+static GSourceFuncs dbus_funcs = {
+  gdbus_connection_prepare,
+  gdbus_connection_check,
+  gdbus_connection_dispatch,
+  NULL
+};
+
+static gboolean
+gdbus_connection_prepare (GSource     *source,
+                         gint        *timeout)
+{
+  DBusConnection *connection = ((DBusGSource *)source)->connection;
+  
+  *timeout = -1;
+
+  return (dbus_connection_peek_message (connection) != NULL);
+}
+
+static gboolean
+gdbus_connection_check (GSource *source)
+{
+  DBusGSource *dbus_source = (DBusGSource *)source;
+  GList *list;
+
+  list = dbus_source->poll_fds;
+
+  while (list)
+    {
+      GPollFD *poll_fd = list->data;
+
+      if (poll_fd->revents != 0)
+       return TRUE;
+
+      list = list->next;
+    }
+
+  return FALSE;
+}
+
+static gboolean
+gdbus_connection_dispatch (GSource     *source,
+                          GSourceFunc  callback,
+                          gpointer     user_data)
+{
+   DBusGSource *dbus_source = (DBusGSource *)source;
+   DBusMessageHandler handler = (DBusMessageHandler)callback;
+   DBusMessage *message;
+   
+   GList *list;
+
+   list = dbus_source->poll_fds;
+
+   while (list)
+     {
+       GPollFD *poll_fd = list->data;
+
+       g_print ("poll_fd is: %p\n", poll_fd);
+       if (poll_fd->revents != 0)
+        {
+          DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
+          guint condition = 0;
+
+          if (poll_fd->revents & G_IO_IN)
+            condition |= DBUS_WATCH_READABLE;
+          if (poll_fd->revents & G_IO_OUT)
+            condition |= DBUS_WATCH_WRITABLE;
+          if (poll_fd->revents & G_IO_ERR)
+            condition |= DBUS_WATCH_ERROR;
+          if (poll_fd->revents & G_IO_HUP)
+            condition |= DBUS_WATCH_HANGUP;
+          
+          dbus_connection_handle_watch (dbus_source->connection, watch, condition);
+        }
+       
+       list = list->next;
+     }
+
+   while ((message = dbus_connection_pop_message (dbus_source->connection)))
+     {
+       handler (dbus_source->connection, message, user_data);
+
+       dbus_message_unref (message);
+     }
+   
+   return TRUE;
+}
+
+static void
+gdbus_add_connection_watch (DBusWatch      *watch,
+                           DBusGSource    *source)
+{
+  GPollFD *poll_fd;
+  guint flags;
+  
+  poll_fd = g_new (GPollFD, 1);
+  poll_fd->fd = dbus_watch_get_fd (watch);
+
+  poll_fd->events = 0;
+  flags = dbus_watch_get_flags (watch);
+  dbus_watch_set_data (watch, poll_fd, NULL);
+  
+  if (flags & DBUS_WATCH_READABLE)
+    poll_fd->events |= G_IO_IN;
+
+  if (flags & DBUS_WATCH_WRITABLE)
+    poll_fd->events |= G_IO_OUT;
+
+  g_source_add_poll ((GSource *)source, poll_fd);
+  
+  g_print ("Add connection watch: %p!\n", watch);
+
+  source->poll_fds = g_list_prepend (source->poll_fds, poll_fd);
+  g_hash_table_insert (source->watches, poll_fd, watch);
+}
+
+static void
+gdbus_remove_connection_watch (DBusWatch      *watch,
+                              DBusGSource    *source)
+{
+  GPollFD *poll_fd;
+
+  poll_fd = dbus_watch_get_data (watch);
+
+  source->poll_fds = g_list_remove (source->poll_fds, poll_fd);
+  g_hash_table_remove (source->watches, poll_fd);
+  g_source_remove_poll ((GSource *)source, poll_fd);
+  
+  g_free (poll_fd);
+}
+
+GSource *
+dbus_connection_gsource_new (DBusConnection *connection)
+{
+  GSource *source = g_source_new (&dbus_funcs, sizeof (DBusGSource));
+  DBusGSource *dbus_source = (DBusGSource *)source;
+
+  dbus_source->watches = g_hash_table_new (NULL, NULL);
+  dbus_source->connection = connection;
+  dbus_connection_ref (dbus_source->connection);
+  
+  dbus_connection_set_watch_functions (connection,
+                                       (DBusAddWatchFunction) gdbus_add_connection_watch,
+                                       (DBusRemoveWatchFunction) gdbus_remove_connection_watch,
+                                       dbus_source,
+                                       NULL);
+
+
+  return source;
+}
diff --git a/glib/dbus-gthread.c b/glib/dbus-gthread.c
new file mode 100644 (file)
index 0000000..75c11fe
--- /dev/null
@@ -0,0 +1,84 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-gthread.c GThread integration
+ *
+ * Copyright (C) 2002  CodeFactory AB
+ *
+ * Licensed under the Academic Free License version 1.2
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <glib.h>
+#include <dbus/dbus.h>
+#include "dbus-gthread.h"
+
+static DBusMutex * dbus_gmutex_new    (void);
+static void        dbus_gmutex_free   (DBusMutex *mutex);
+static dbus_bool_t dbus_gmutex_lock   (DBusMutex *mutex);
+static dbus_bool_t dbus_gmutex_unlock (DBusMutex *mutex);
+
+static const DBusThreadFunctions functions =
+{
+  DBUS_THREAD_FUNCTIONS_NEW_MASK |
+  DBUS_THREAD_FUNCTIONS_FREE_MASK |
+  DBUS_THREAD_FUNCTIONS_LOCK_MASK |
+  DBUS_THREAD_FUNCTIONS_UNLOCK_MASK,
+  dbus_gmutex_new,
+  dbus_gmutex_free,
+  dbus_gmutex_lock,
+  dbus_gmutex_unlock
+};
+
+static DBusMutex *
+dbus_gmutex_new (void)
+{
+  GMutex *mutex;
+
+  mutex = g_mutex_new ();
+
+  return (DBusMutex *)mutex;
+}
+
+static void
+dbus_gmutex_free (DBusMutex *mutex)
+{
+  g_mutex_free ((GMutex *)mutex);
+}
+
+static dbus_bool_t
+dbus_gmutex_lock (DBusMutex *mutex)
+{
+  g_mutex_lock ((GMutex *)mutex);
+
+  return TRUE;
+}
+
+static dbus_bool_t
+dbus_gmutex_unlock (DBusMutex *mutex)
+{
+  g_mutex_unlock ((GMutex *)mutex);
+
+  return TRUE;
+}
+
+void
+dbus_gthread_init (void)
+{
+  if (!g_thread_supported ())
+    g_error ("g_thread_init() must be called before gdbus_threads_init()");
+    
+  dbus_threads_init (&functions);
+}
diff --git a/glib/test-dbus-glib.c b/glib/test-dbus-glib.c
new file mode 100644 (file)
index 0000000..3637678
--- /dev/null
@@ -0,0 +1,60 @@
+#include "dbus-glib.h"
+#include <stdio.h>
+
+GMainLoop *loop;
+
+static void
+message_handler (DBusConnection *connection,
+                DBusMessage *message, gpointer user_data)
+{
+  static int count = 0;
+  DBusMessage *reply;
+
+  reply = dbus_message_new ();
+  dbus_connection_send_message (connection,
+                               reply,
+                               NULL);
+  dbus_message_unref (reply);
+  count += 1;
+  
+  if (count > 100)
+    {
+      printf ("Saw %d messages, exiting\n", count);
+      g_main_loop_quit (loop);
+    }
+}
+
+int
+main (int argc, char **argv)
+{
+  GSource *source;
+  
+  DBusConnection *connection;
+  DBusResultCode result;
+  DBusMessage *message;
+  
+  loop = g_main_loop_new (NULL, FALSE);
+
+  connection = dbus_connection_open (argv[1], &result);
+  if (connection == NULL)
+    {
+      fprintf (stderr, "Failed to open connection to %s: %s\n", argv[1],
+              dbus_result_to_string (result));
+      return 1;
+    }
+
+  source = dbus_connection_gsource_new (connection);
+  g_source_attach (source, NULL);
+  g_source_set_callback (source, (GSourceFunc)message_handler, NULL, NULL);
+  
+  message = dbus_message_new ();
+  dbus_connection_send_message (connection,
+                                message,
+                                NULL);
+  dbus_message_unref (message);
+  
+  
+  g_main_loop_run (loop);
+  
+  return 0;
+}