From cef11442f69e9a649731f3b2a12b655996da265b Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 23 Jun 2003 02:12:19 +0000 Subject: 2003-06-22 Havoc Pennington * mono/Connection.cs: add more bindings * dbus/dbus-threads.c (dbus_threads_init): allow calling this more than once. --- ChangeLog | 7 +++++ dbus/dbus-threads.c | 22 +++++++++++--- mono/Connection.cs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++----- mono/DBus.cs | 24 +++++++++++++-- mono/Error.cs | 4 +-- mono/Message.cs | 20 +++++++------ mono/Test.cs | 10 +++++-- 7 files changed, 143 insertions(+), 28 deletions(-) diff --git a/ChangeLog b/ChangeLog index 06deccd2..9e398354 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2003-06-22 Havoc Pennington + + * mono/Connection.cs: add more bindings + + * dbus/dbus-threads.c (dbus_threads_init): allow calling this + more than once. + 2003-06-22 Havoc Pennington * mono/Connection.cs, mono/DBus.cs, mono/Error.cs: diff --git a/dbus/dbus-threads.c b/dbus/dbus-threads.c index 5f953d82..b604a397 100644 --- a/dbus/dbus-threads.c +++ b/dbus/dbus-threads.c @@ -279,8 +279,10 @@ init_global_locks (void) * in efficiency. Note that this function must be called * BEFORE using any other D-BUS functions. * - * @todo right now this function can only be called once, - * maybe we should instead silently ignore multiple calls. + * This function may be called more than once, as long + * as you pass in the same functions each time. If it's + * called multiple times with different functions, then + * a warning is printed, because someone is confused. * * @param functions functions for using threads * @returns #TRUE on success, #FALSE if no memory @@ -325,8 +327,20 @@ dbus_threads_init (const DBusThreadFunctions *functions) if (thread_functions.mask != 0) { - _dbus_warn ("dbus_threads_init() may only be called one time\n"); - return FALSE; + /* Silently allow multiple init if the functions are the same ones. + * Well, we only bother checking two of them, just out of laziness. + */ + if (thread_functions.mask == functions->mask && + thread_functions.mutex_new == functions->mutex_new && + thread_functions.condvar_new == functions->condvar_new) + { + return TRUE; + } + else + { + _dbus_warn ("dbus_threads_init() called twice with two different sets of functions\n"); + return FALSE; + } } thread_functions.mutex_new = functions->mutex_new; diff --git a/mono/Connection.cs b/mono/Connection.cs index f0d34eec..ff983d5a 100644 --- a/mono/Connection.cs +++ b/mono/Connection.cs @@ -20,6 +20,49 @@ namespace DBus { } } + // Keep in sync with C + public enum BusType { + Session = 0, + System = 1, + Activation = 2 + } + + public static Connection GetBus (BusType bus) { + Error error = new Error (); + + error.Init (); + + IntPtr ptr = dbus_bus_get ((int) bus, ref error); + if (ptr != (IntPtr) 0) { + Connection c = Wrap (ptr); + dbus_connection_unref (ptr); + return c; + } else { + Exception e = new Exception (ref error); + error.Free (); + throw e; + } + } + + public void Send (Message m, + ref int serial) { + if (!dbus_connection_send (raw, m.raw, ref serial)) + throw new OutOfMemoryException (); + } + + public void Send (Message m) { + int ignored = 0; + Send (m, ref ignored); + } + + public void Flush () { + dbus_connection_flush (raw); + } + + public void Disconnect () { + dbus_connection_disconnect (raw); + } + public static Connection Wrap (IntPtr ptr) { IntPtr gch_ptr; @@ -34,7 +77,7 @@ namespace DBus { // surely there's a convention for this pattern with the property // and the real member IntPtr raw_; - IntPtr raw { + internal IntPtr raw { get { return raw_; } @@ -74,6 +117,9 @@ namespace DBus { } ~Connection () { + if (raw != (IntPtr) 0) { + Disconnect (); + } raw = (IntPtr) 0; // free the native object } @@ -83,6 +129,8 @@ namespace DBus { // static constructor runs before any methods static Connection () { + DBus.Internals.Init (); + Debug.Assert (wrapper_slot == -1); if (!dbus_connection_allocate_data_slot (ref wrapper_slot)) @@ -94,30 +142,50 @@ namespace DBus { // slot used to store the C# object on the C object static int wrapper_slot = -1; - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_open")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_open")] private extern static IntPtr dbus_connection_open (string address, ref Error error); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_unref")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_unref")] private extern static void dbus_connection_unref (IntPtr ptr); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_ref")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_ref")] private extern static void dbus_connection_ref (IntPtr ptr); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_allocate_data_slot")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_allocate_data_slot")] private extern static bool dbus_connection_allocate_data_slot (ref int slot); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_free_data_slot")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_free_data_slot")] private extern static void dbus_connection_free_data_slot (ref int slot); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_set_data")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_set_data")] private extern static bool dbus_connection_set_data (IntPtr ptr, int slot, IntPtr data, IntPtr free_data_func); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_connection_get_data")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_send")] + private extern static bool dbus_connection_send (IntPtr ptr, + IntPtr message, + ref int client_serial); + + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_flush")] + private extern static void dbus_connection_flush (IntPtr ptr); + + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_bus_get")] + private extern static IntPtr dbus_bus_get (int which, + ref Error error); + + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_get_data")] private extern static IntPtr dbus_connection_get_data (IntPtr ptr, int slot); + + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_connection_disconnect")] + private extern static void dbus_connection_disconnect (IntPtr ptr); + + [DllImport (DBus.Internals.DBusGLibname, EntryPoint="dbus_connection_setup_with_g_main")] + private extern static void dbus_connection_setup_with_g_main (IntPtr ptr, + IntPtr context); + } } diff --git a/mono/DBus.cs b/mono/DBus.cs index 1032792a..e2751841 100644 --- a/mono/DBus.cs +++ b/mono/DBus.cs @@ -1,13 +1,33 @@ namespace DBus { using System; + using System.Runtime.InteropServices; public class Exception : ApplicationException { internal Exception (ref Error error) : base (Runtime.InteropServices.Marshal.PtrToStringAnsi (error.message)) { } } - public class Internals { - public const string Libname = "libdbus-1.so.0"; + internal class Internals { + internal const string DBusLibname = "libdbus-1.so.0"; + internal const string DBusGLibname = "libdbus-glib-1.so.0"; + internal const string GLibname = "libglib-2.0.so.0"; + internal const string GThreadname = "libgthread-2.0.so.0"; + + internal static void Init () { + if (!initialized) { + initialized = true; + g_thread_init ((IntPtr) 0); + dbus_gthread_init (); + } + } + + private static bool initialized = false; + + [DllImport (DBus.Internals.DBusGLibname, EntryPoint="dbus_gthread_init")] + private extern static void dbus_gthread_init (); + + [DllImport (DBus.Internals.GThreadname, EntryPoint="g_thread_init")] + private extern static void g_thread_init (IntPtr vtable); } } diff --git a/mono/Error.cs b/mono/Error.cs index 95c0193b..dab4df1f 100644 --- a/mono/Error.cs +++ b/mono/Error.cs @@ -21,9 +21,9 @@ namespace DBus { dbus_error_free (ref this); } - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_error_init")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_error_init")] private extern static void dbus_error_init (ref Error error); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_error_free")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_error_free")] private extern static void dbus_error_free (ref Error error); } } diff --git a/mono/Message.cs b/mono/Message.cs index a6d3c092..edd1aff9 100644 --- a/mono/Message.cs +++ b/mono/Message.cs @@ -35,7 +35,7 @@ namespace DBus { // surely there's a convention for this pattern with the property // and the real member IntPtr raw_; - IntPtr raw { + internal IntPtr raw { get { return raw_; } @@ -84,6 +84,8 @@ namespace DBus { // static constructor runs before any methods static Message () { + DBus.Internals.Init (); + Debug.Assert (wrapper_slot == -1); if (!dbus_message_allocate_data_slot (ref wrapper_slot)) @@ -95,32 +97,32 @@ namespace DBus { // slot used to store the C# object on the C object static int wrapper_slot = -1; - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_new")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_new")] private extern static IntPtr dbus_message_new (string name, string dest_service); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_unref")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_unref")] private extern static void dbus_message_unref (IntPtr ptr); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_ref")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_ref")] private extern static void dbus_message_ref (IntPtr ptr); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_get_name")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_get_name")] private extern static string dbus_message_get_name (IntPtr ptr); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_allocate_data_slot")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_allocate_data_slot")] private extern static bool dbus_message_allocate_data_slot (ref int slot); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_free_data_slot")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_free_data_slot")] private extern static void dbus_message_free_data_slot (ref int slot); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_set_data")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_set_data")] private extern static bool dbus_message_set_data (IntPtr ptr, int slot, IntPtr data, IntPtr free_data_func); - [DllImport (DBus.Internals.Libname, EntryPoint="dbus_message_get_data")] + [DllImport (DBus.Internals.DBusLibname, EntryPoint="dbus_message_get_data")] private extern static IntPtr dbus_message_get_data (IntPtr ptr, int slot); } diff --git a/mono/Test.cs b/mono/Test.cs index e07504d7..b64ed9c3 100644 --- a/mono/Test.cs +++ b/mono/Test.cs @@ -6,10 +6,14 @@ class Test { DBus.Message m; DBus.Connection c; - c = new DBus.Connection ("unix:path=/tmp/foobar"); + // c = new DBus.Connection ("unix:path=/tmp/foobar"); - m = new DBus.Message ("org.freedesktop.Foo", null); + c = DBus.Connection.GetBus (DBus.Connection.BusType.Session); + + m = new DBus.Message ("org.freedesktop.Foo", + "org.freedesktop.DBus.Broadcast"); - Console.WriteLine ("Message name is {0}\n", m.Name); + c.Send (m); + c.Flush (); } } -- cgit