/* * Copyright 2019 by its authors. See AUTHORS. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma warning disable 1591 using System.Runtime.InteropServices; using System.ComponentModel; using static eldbus.EldbusObjectNativeFunctions; using IntPtr = System.IntPtr; namespace eldbus { [EditorBrowsable(EditorBrowsableState.Never)] public static class EldbusObjectNativeFunctions { [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_get(IntPtr conn, string bus, string path); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_ref(IntPtr obj); [DllImport(efl.Libs.Eldbus)] public static extern void eldbus_object_unref(IntPtr obj); [DllImport(efl.Libs.Eldbus)] public static extern void eldbus_object_free_cb_add(IntPtr obj, IntPtr cb, IntPtr data); [DllImport(efl.Libs.Eldbus)] public static extern void eldbus_object_free_cb_del(IntPtr obj, IntPtr cb, IntPtr data); // typedef enum // { // ELDBUS_OBJECT_EVENT_IFACE_ADDED = 0, /**< a parent path must have a ObjectManager interface */ // ELDBUS_OBJECT_EVENT_IFACE_REMOVED, /**< a parent path must have a ObjectManager interface */ // ELDBUS_OBJECT_EVENT_PROPERTY_CHANGED, /**< a property has changes */ // ELDBUS_OBJECT_EVENT_PROPERTY_REMOVED, /**< a property was removed */ // ELDBUS_OBJECT_EVENT_DEL, // ELDBUS_OBJECT_EVENT_LAST /**< sentinel, not a real event type */ // } Eldbus_Object_Event_Type; // // [DllImport(efl.Libs.Eldbus)] public static extern void // eldbus_object_event_callback_add(IntPtr obj, Eldbus_Object_Event_Type type, IntPtr cb, IntPtr cb_data); // // [DllImport(efl.Libs.Eldbus)] public static extern void // eldbus_object_event_callback_del(IntPtr obj, Eldbus_Object_Event_Type type, IntPtr cb, IntPtr cb_data); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_connection_get(IntPtr obj); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_bus_name_get(IntPtr obj); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_path_get(IntPtr obj); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_send(IntPtr obj, IntPtr msg, IntPtr cb, IntPtr cb_data, double timeout); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_signal_handler_add(IntPtr obj, string _interface, string member, IntPtr cb, IntPtr cb_data); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_method_call_new(IntPtr obj, string _interface, string member); // FreeDesktop.Org Methods [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_peer_ping(IntPtr obj, IntPtr cb, IntPtr data); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_peer_machine_id_get(IntPtr obj, IntPtr cb, IntPtr data); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_introspect(IntPtr obj, IntPtr cb, IntPtr data); [DllImport(efl.Libs.Eldbus)] public static extern IntPtr eldbus_object_managed_objects_get(IntPtr obj, IntPtr cb, IntPtr data); // [DllImport(efl.Libs.Eldbus)] public static extern IntPtr // eldbus_object_manager_interfaces_added(IntPtr obj, Eldbus_Signal_Cb cb, IntPtr cb_data); // // [DllImport(efl.Libs.Eldbus)] public static extern IntPtr // eldbus_object_manager_interfaces_removed(IntPtr obj, Eldbus_Signal_Cb cb, IntPtr cb_data); } /// Represents a DBus object. /// Since EFL 1.23. /// public class Object : System.IDisposable { [EditorBrowsable(EditorBrowsableState.Never)] public IntPtr Handle {get;set;} = IntPtr.Zero; /// Whether this managed list owns the native one. /// Since EFL 1.23. /// public bool Own {get;set;} = true; [EditorBrowsable(EditorBrowsableState.Never)] private void InitNew(IntPtr handle, bool own) { Handle = handle; Own = own; CheckHandle(); } private void CheckHandle() { if (Handle == IntPtr.Zero) { eldbus.Common.RaiseNullHandle(); } } [EditorBrowsable(EditorBrowsableState.Never)] public Object(IntPtr handle, bool own) { InitNew(handle, own); } /// /// Constructor. /// Since EFL 1.23. /// /// where object /// belongs. /// The name of the bus or unique id who listens for calls /// of this object /// The object path of this object. public Object(eldbus.Connection conn, string bus, string path) { if (conn == null) { throw new System.ArgumentNullException("conn"); } if (bus == null) { throw new System.ArgumentNullException("bus"); } if (path == null) { throw new System.ArgumentNullException("path"); } var handle = eldbus_object_get(conn.Handle, bus, path); if (handle == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Object' object from eldbus_object_get"); } InitNew(handle, true); } /// Finalizes with garbage collector. /// Since EFL 1.23. /// ~Object() { Dispose(false); } /// Disposes of this list. /// Since EFL 1.23. /// /// Whether this was called from the finalizer (false) or from the /// method. protected virtual void Dispose(bool disposing) { IntPtr h = Handle; Handle = IntPtr.Zero; if (h == IntPtr.Zero) { return; } if (Own) { if (disposing) { eldbus_object_unref(h); } else { Efl.Eo.Globals.ThreadSafeFreeCbExec(eldbus_object_unref, h); } } } /// Disposes of this list. /// Since EFL 1.23. /// public void Dispose() { Dispose(true); System.GC.SuppressFinalize(this); } /// Releases the native resources held by this instance. /// Since EFL 1.23. /// public void Free() { Dispose(); } /// /// Releases the native handler. /// Since EFL 1.23. /// /// The native handler. public IntPtr Release() { IntPtr h = Handle; Handle = IntPtr.Zero; return h; } /// /// Get the object associated with a /// /// Since EFL 1.23. /// /// The object public eldbus.Connection GetConnection() { CheckHandle(); var conn = eldbus_object_connection_get(Handle); if (conn == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Connection' object from eldbus_object_connection_get"); } return new eldbus.Connection(conn, false); } /// /// Get the name associated with a /// Since EFL 1.23. /// /// A string corresponding to the /// name public string GetBusName() { CheckHandle(); var ptr = eldbus_object_bus_name_get(Handle); return Eina.StringConversion.NativeUtf8ToManagedString(ptr); } /// /// Get the path associated with a /// Since EFL 1.23. /// /// A string corresponding to the /// path. public string GetPath() { CheckHandle(); var ptr = eldbus_object_path_get(Handle); return Eina.StringConversion.NativeUtf8ToManagedString(ptr); } /// /// Increse object reference /// Since EFL 1.23. /// public void Ref() { CheckHandle(); eldbus_object_ref(Handle); } /// /// Decrease object reference. /// If reference == 0 object will be freed and all its children. /// /// Since EFL 1.23. /// /// public void Unref() { CheckHandle(); eldbus_object_unref(Handle); } /// /// Send a message /// Since EFL 1.23. /// /// The message will be sent in connection to this object. /// The function to call when receiving answer. /// Timeout. /// A public eldbus.Pending Send(eldbus.Message msg, eldbus.MessageDelegate dlgt = null, double timeout = -1) { CheckHandle(); if (msg == null) { throw new System.ArgumentNullException("msg"); } IntPtr cb_wrapper = dlgt == null ? IntPtr.Zero : eldbus.Common.GetMessageCbWrapperPtr(); IntPtr cb_data = dlgt == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(dlgt); var pending_hdl = eldbus_object_send(Handle, msg.Handle, cb_wrapper, cb_data, timeout); if (pending_hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Pending' object from eldbus_object_send"); } return new eldbus.Pending(pending_hdl, false); } /// /// Call a dbus method on the . /// Since EFL 1.23. /// /// The interface name. /// Name of the method to be called. /// A new public eldbus.Message NewMethodCall(string _interface, string member) { CheckHandle(); var hdl = eldbus_object_method_call_new(Handle, _interface, member); if (hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Message' object from eldbus_object_method_call_new"); } return new eldbus.Message(hdl, false); } /// /// Call the method Ping on the eldbus. /// Since EFL 1.23. /// /// The function to call when receiving answer. /// A public eldbus.Pending PeerPing(eldbus.MessageDelegate dlgt = null) { CheckHandle(); IntPtr cb_wrapper = dlgt == null ? IntPtr.Zero : eldbus.Common.GetMessageCbWrapperPtr(); IntPtr cb_data = dlgt == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(dlgt); var pending_hdl = eldbus_object_peer_ping(Handle, cb_wrapper, cb_data); if (pending_hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Pending' object from eldbus_object_peer_ping"); } return new eldbus.Pending(pending_hdl, false); } /// /// Call the method GetMachineId on the eldbus. /// Since EFL 1.23. /// /// The function to call when receiving answer. /// A public eldbus.Pending GetPeerMachineId(eldbus.MessageDelegate dlgt = null) { CheckHandle(); IntPtr cb_wrapper = dlgt == null ? IntPtr.Zero : eldbus.Common.GetMessageCbWrapperPtr(); IntPtr cb_data = dlgt == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(dlgt); var pending_hdl = eldbus_object_peer_machine_id_get(Handle, cb_wrapper, cb_data); if (pending_hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Pending' object from eldbus_object_peer_machine_id_get"); } return new eldbus.Pending(pending_hdl, false); } /// /// Call the method Introspect on the eldbus. /// Since EFL 1.23. /// /// The function to call when receiving answer. /// A public eldbus.Pending Introspect(eldbus.MessageDelegate dlgt = null) { CheckHandle(); IntPtr cb_wrapper = dlgt == null ? IntPtr.Zero : eldbus.Common.GetMessageCbWrapperPtr(); IntPtr cb_data = dlgt == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(dlgt); var pending_hdl = eldbus_object_introspect(Handle, cb_wrapper, cb_data); if (pending_hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Pending' object from eldbus_object_introspect"); } return new eldbus.Pending(pending_hdl, false); } /// /// Call the method GetmanagedObjects on eldbus. /// Since EFL 1.23. /// /// The function to call when receiving answer. /// A public eldbus.Pending GetManagedObjects(eldbus.MessageDelegate dlgt = null) { CheckHandle(); IntPtr cb_wrapper = dlgt == null ? IntPtr.Zero : eldbus.Common.GetMessageCbWrapperPtr(); IntPtr cb_data = dlgt == null ? IntPtr.Zero : Marshal.GetFunctionPointerForDelegate(dlgt); var pending_hdl = eldbus_object_managed_objects_get(Handle, cb_wrapper, cb_data); if (pending_hdl == IntPtr.Zero) { throw new SEHException("Eldbus: could not get `Pending' object from eldbus_object_managed_objects_get"); } return new eldbus.Pending(pending_hdl, false); } } }