summaryrefslogtreecommitdiffstats
path: root/glib
diff options
context:
space:
mode:
authorAnders Carlsson <andersca@codefactory.se>2002-12-16 00:26:05 +0000
committerAnders Carlsson <andersca@codefactory.se>2002-12-16 00:26:05 +0000
commitc531a709f3b0f05c0a95aa932045d6e2209ae7b9 (patch)
tree22e198a761c88fb8ed13a382e943fb25b3bcb38c /glib
parent3327ecfdcf6d3cd36b8b739282e9c0d2b768567a (diff)
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.
Diffstat (limited to 'glib')
-rw-r--r--glib/Makefile.am26
-rw-r--r--glib/dbus-glib.h40
-rw-r--r--glib/dbus-gmain.c194
-rw-r--r--glib/dbus-gthread.c84
-rw-r--r--glib/test-dbus-glib.c60
5 files changed, 404 insertions, 0 deletions
diff --git a/glib/Makefile.am b/glib/Makefile.am
new file mode 100644
index 00000000..6f3906ce
--- /dev/null
+++ b/glib/Makefile.am
@@ -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
index 00000000..a6633daf
--- /dev/null
+++ b/glib/dbus-glib.h
@@ -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
index 00000000..0b9ac9f1
--- /dev/null
+++ b/glib/dbus-gmain.c
@@ -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
index 00000000..75c11fea
--- /dev/null
+++ b/glib/dbus-gthread.c
@@ -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
index 00000000..3637678d
--- /dev/null
+++ b/glib/test-dbus-glib.c
@@ -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;
+}