]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
2003-06-22 Havoc Pennington <hp@pobox.com>
authorHavoc Pennington <hp@redhat.com>
Mon, 23 Jun 2003 02:12:19 +0000 (02:12 +0000)
committerHavoc Pennington <hp@redhat.com>
Mon, 23 Jun 2003 02:12:19 +0000 (02:12 +0000)
* mono/Connection.cs: add more bindings

* dbus/dbus-threads.c (dbus_threads_init): allow calling this
more than once.

ChangeLog
dbus/dbus-threads.c
mono/Connection.cs
mono/DBus.cs
mono/Error.cs
mono/Message.cs
mono/Test.cs

index 06deccd2e828e5a1f1a1e6106bfe9792468b6729..9e3983541374ed43bf232cf7efe201acf8180c61 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2003-06-22  Havoc Pennington  <hp@pobox.com>
+
+       * mono/Connection.cs: add more bindings
+
+       * dbus/dbus-threads.c (dbus_threads_init): allow calling this
+       more than once.
+
 2003-06-22  Havoc Pennington  <hp@pobox.com>
 
        * mono/Connection.cs, mono/DBus.cs, mono/Error.cs: 
index 5f953d82fd95f6ae728709c068fa29309cb73be7..b604a397dac70ba8fbe3186b64c0d8aa1f641de0 100644 (file)
@@ -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;
index f0d34eec24cc06b43890561cc86031b42d05d07b..ff983d5aa78944f45d4458cb2407ae777bf68c48 100644 (file)
@@ -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);
+    
   }
 }
index 1032792aa0760c6a245f36d0b045457bf6674ad4..e2751841663c7b314f63d593df748a8dbea03301 100644 (file)
@@ -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); 
   }
 }
index 95c0193b3e8a0642743e7385e766ce47cba2046f..dab4df1ff963266ed7ab2f1bc849680940308d77 100644 (file)
@@ -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);
   }
 }
index a6d3c09246e7308a7ccc42722481d463d71dcfff..edd1aff9fceeadaea931abe38b47f23bbfc9c251 100644 (file)
@@ -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);
   }
index e07504d74a17f0dafaa7fd8e14b3e0d8e9fa2966..b64ed9c3858d4b5d5b30b051cb9e4263c7c6b1fc 100644 (file)
@@ -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 ();    
   }
 }