1 #pragma warning disable CS1591
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
6 using System.ComponentModel;
7 namespace Efl { namespace Io {
8 /// <summary>Generic interface for objects that can change or report position.
10 [PositionerNativeInherit]
11 public interface Positioner :
12 Efl.Eo.IWrapper, IDisposable
14 /// <summary>Position property
16 /// <returns>Position in file or buffer
19 /// <summary>Try to set position object, relative to start of file. See <see cref="Efl.Io.Positioner.Seek"/>
21 /// <param name="position">Position in file or buffer
23 /// <returns><c>true</c> if could reposition, <c>false</c> if errors.
25 bool SetPosition( ulong position);
26 /// <summary>Seek in data
28 /// <param name="offset">Offset in byte relative to whence
30 /// <param name="whence">Whence
32 /// <returns>0 on succeed, a mapping of errno otherwise
34 Eina.Error Seek( long offset, Efl.Io.PositionerWhence whence);
35 /// <summary>Notifies position changed
37 event EventHandler PositionChangedEvt;
38 /// <summary>Position property
40 /// <value>Position in file or buffer
47 /// <summary>Generic interface for objects that can change or report position.
49 sealed public class PositionerConcrete :
54 ///<summary>Pointer to the native class description.</summary>
55 public System.IntPtr NativeClass {
57 if (((object)this).GetType() == typeof (PositionerConcrete))
58 return Efl.Io.PositionerNativeInherit.GetEflClassStatic();
60 return Efl.Eo.ClassRegister.klassFromType[((object)this).GetType()];
63 private EventHandlerList eventHandlers = new EventHandlerList();
64 private System.IntPtr handle;
65 ///<summary>Pointer to the native instance.</summary>
66 public System.IntPtr NativeHandle {
67 get { return handle; }
69 [System.Runtime.InteropServices.DllImport(efl.Libs.Efl)] internal static extern System.IntPtr
70 efl_io_positioner_mixin_get();
71 ///<summary>Internal usage: Constructs an instance from a native pointer. This is used when interacting with C code and should not be used directly.</summary>
72 public PositionerConcrete(System.IntPtr raw)
75 register_event_proxies();
77 ///<summary>Destructor.</summary>
82 ///<summary>Releases the underlying native instance.</summary>
83 void Dispose(bool disposing)
85 if (handle != System.IntPtr.Zero) {
86 Efl.Eo.Globals.efl_unref(handle);
87 handle = System.IntPtr.Zero;
90 ///<summary>Releases the underlying native instance.</summary>
94 GC.SuppressFinalize(this);
96 ///<summary>Casts obj into an instance of this type.</summary>
97 public static PositionerConcrete static_cast(Efl.Object obj)
100 throw new System.ArgumentNullException("obj");
101 return new PositionerConcrete(obj.NativeHandle);
103 ///<summary>Verifies if the given object is equal to this one.</summary>
104 public override bool Equals(object obj)
106 var other = obj as Efl.Object;
109 return this.NativeHandle == other.NativeHandle;
111 ///<summary>Gets the hash code for this object based on the native pointer it points to.</summary>
112 public override int GetHashCode()
114 return this.NativeHandle.ToInt32();
116 ///<summary>Turns the native pointer into a string representation.</summary>
117 public override String ToString()
119 return $"{this.GetType().Name}@[{this.NativeHandle.ToInt32():x}]";
121 private readonly object eventLock = new object();
122 private Dictionary<string, int> event_cb_count = new Dictionary<string, int>();
123 private bool add_cpp_event_handler(string lib, string key, Efl.EventCb evt_delegate) {
125 if (!event_cb_count.TryGetValue(key, out event_count))
126 event_cb_count[key] = event_count;
127 if (event_count == 0) {
128 IntPtr desc = Efl.EventDescription.GetNative(lib, key);
129 if (desc == IntPtr.Zero) {
130 Eina.Log.Error($"Failed to get native event {key}");
133 bool result = Efl.Eo.Globals.efl_event_callback_priority_add(handle, desc, 0, evt_delegate, System.IntPtr.Zero);
135 Eina.Log.Error($"Failed to add event proxy for event {key}");
138 Eina.Error.RaiseIfUnhandledException();
140 event_cb_count[key]++;
143 private bool remove_cpp_event_handler(string key, Efl.EventCb evt_delegate) {
145 if (!event_cb_count.TryGetValue(key, out event_count))
146 event_cb_count[key] = event_count;
147 if (event_count == 1) {
148 IntPtr desc = Efl.EventDescription.GetNative(efl.Libs.Efl, key);
149 if (desc == IntPtr.Zero) {
150 Eina.Log.Error($"Failed to get native event {key}");
153 bool result = Efl.Eo.Globals.efl_event_callback_del(handle, desc, evt_delegate, System.IntPtr.Zero);
155 Eina.Log.Error($"Failed to remove event proxy for event {key}");
158 Eina.Error.RaiseIfUnhandledException();
159 } else if (event_count == 0) {
160 Eina.Log.Error($"Trying to remove proxy for event {key} when there is nothing registered.");
163 event_cb_count[key]--;
166 private static object PositionChangedEvtKey = new object();
167 /// <summary>Notifies position changed
169 public event EventHandler PositionChangedEvt
173 string key = "_EFL_IO_POSITIONER_EVENT_POSITION_CHANGED";
174 if (add_cpp_event_handler(efl.Libs.Efl, key, this.evt_PositionChangedEvt_delegate)) {
175 eventHandlers.AddHandler(PositionChangedEvtKey , value);
177 Eina.Log.Error($"Error adding proxy for event {key}");
182 string key = "_EFL_IO_POSITIONER_EVENT_POSITION_CHANGED";
183 if (remove_cpp_event_handler(key, this.evt_PositionChangedEvt_delegate)) {
184 eventHandlers.RemoveHandler(PositionChangedEvtKey , value);
186 Eina.Log.Error($"Error removing proxy for event {key}");
190 ///<summary>Method to raise event PositionChangedEvt.</summary>
191 public void On_PositionChangedEvt(EventArgs e)
195 evt = (EventHandler)eventHandlers[PositionChangedEvtKey];
197 evt?.Invoke(this, e);
199 Efl.EventCb evt_PositionChangedEvt_delegate;
200 private void on_PositionChangedEvt_NativeCallback(System.IntPtr data, ref Efl.Event_StructInternal evt)
202 EventArgs args = EventArgs.Empty;
204 On_PositionChangedEvt(args);
205 } catch (Exception e) {
206 Eina.Log.Error(e.ToString());
207 Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
211 void register_event_proxies()
213 evt_PositionChangedEvt_delegate = new Efl.EventCb(on_PositionChangedEvt_NativeCallback);
215 /// <summary>Position property
217 /// <returns>Position in file or buffer
219 public ulong GetPosition() {
220 var _ret_var = Efl.Io.PositionerNativeInherit.efl_io_positioner_position_get_ptr.Value.Delegate(this.NativeHandle);
221 Eina.Error.RaiseIfUnhandledException();
224 /// <summary>Try to set position object, relative to start of file. See <see cref="Efl.Io.Positioner.Seek"/>
226 /// <param name="position">Position in file or buffer
228 /// <returns><c>true</c> if could reposition, <c>false</c> if errors.
230 public bool SetPosition( ulong position) {
231 var _ret_var = Efl.Io.PositionerNativeInherit.efl_io_positioner_position_set_ptr.Value.Delegate(this.NativeHandle, position);
232 Eina.Error.RaiseIfUnhandledException();
235 /// <summary>Seek in data
237 /// <param name="offset">Offset in byte relative to whence
239 /// <param name="whence">Whence
241 /// <returns>0 on succeed, a mapping of errno otherwise
243 public Eina.Error Seek( long offset, Efl.Io.PositionerWhence whence) {
244 var _ret_var = Efl.Io.PositionerNativeInherit.efl_io_positioner_seek_ptr.Value.Delegate(this.NativeHandle, offset, whence);
245 Eina.Error.RaiseIfUnhandledException();
248 /// <summary>Position property
250 /// <value>Position in file or buffer
252 public ulong Position {
253 get { return GetPosition(); }
254 set { SetPosition( value); }
257 public class PositionerNativeInherit : Efl.Eo.NativeClass{
258 public static Efl.Eo.NativeModule _Module = new Efl.Eo.NativeModule(efl.Libs.Efl);
259 public override System.Collections.Generic.List<Efl_Op_Description> GetEoOps(System.Type type)
261 var descs = new System.Collections.Generic.List<Efl_Op_Description>();
262 if (efl_io_positioner_position_get_static_delegate == null)
263 efl_io_positioner_position_get_static_delegate = new efl_io_positioner_position_get_delegate(position_get);
264 descs.Add(new Efl_Op_Description() {api_func = Efl.Eo.FunctionInterop.LoadFunctionPointer(_Module.Module, "efl_io_positioner_position_get"), func = Marshal.GetFunctionPointerForDelegate(efl_io_positioner_position_get_static_delegate)});
265 if (efl_io_positioner_position_set_static_delegate == null)
266 efl_io_positioner_position_set_static_delegate = new efl_io_positioner_position_set_delegate(position_set);
267 descs.Add(new Efl_Op_Description() {api_func = Efl.Eo.FunctionInterop.LoadFunctionPointer(_Module.Module, "efl_io_positioner_position_set"), func = Marshal.GetFunctionPointerForDelegate(efl_io_positioner_position_set_static_delegate)});
268 if (efl_io_positioner_seek_static_delegate == null)
269 efl_io_positioner_seek_static_delegate = new efl_io_positioner_seek_delegate(seek);
270 descs.Add(new Efl_Op_Description() {api_func = Efl.Eo.FunctionInterop.LoadFunctionPointer(_Module.Module, "efl_io_positioner_seek"), func = Marshal.GetFunctionPointerForDelegate(efl_io_positioner_seek_static_delegate)});
273 public override IntPtr GetEflClass()
275 return Efl.Io.PositionerConcrete.efl_io_positioner_mixin_get();
277 public static IntPtr GetEflClassStatic()
279 return Efl.Io.PositionerConcrete.efl_io_positioner_mixin_get();
283 private delegate ulong efl_io_positioner_position_get_delegate(System.IntPtr obj, System.IntPtr pd);
286 public delegate ulong efl_io_positioner_position_get_api_delegate(System.IntPtr obj);
287 public static Efl.Eo.FunctionWrapper<efl_io_positioner_position_get_api_delegate> efl_io_positioner_position_get_ptr = new Efl.Eo.FunctionWrapper<efl_io_positioner_position_get_api_delegate>(_Module, "efl_io_positioner_position_get");
288 private static ulong position_get(System.IntPtr obj, System.IntPtr pd)
290 Eina.Log.Debug("function efl_io_positioner_position_get was called");
291 Efl.Eo.IWrapper wrapper = Efl.Eo.Globals.data_get(pd);
292 if(wrapper != null) {
293 ulong _ret_var = default( ulong);
295 _ret_var = ((PositionerConcrete)wrapper).GetPosition();
296 } catch (Exception e) {
297 Eina.Log.Warning($"Callback error: {e.ToString()}");
298 Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
302 return efl_io_positioner_position_get_ptr.Value.Delegate(Efl.Eo.Globals.efl_super(obj, Efl.Eo.Globals.efl_class_get(obj)));
305 private static efl_io_positioner_position_get_delegate efl_io_positioner_position_get_static_delegate;
308 [return: MarshalAs(UnmanagedType.U1)] private delegate bool efl_io_positioner_position_set_delegate(System.IntPtr obj, System.IntPtr pd, ulong position);
311 [return: MarshalAs(UnmanagedType.U1)] public delegate bool efl_io_positioner_position_set_api_delegate(System.IntPtr obj, ulong position);
312 public static Efl.Eo.FunctionWrapper<efl_io_positioner_position_set_api_delegate> efl_io_positioner_position_set_ptr = new Efl.Eo.FunctionWrapper<efl_io_positioner_position_set_api_delegate>(_Module, "efl_io_positioner_position_set");
313 private static bool position_set(System.IntPtr obj, System.IntPtr pd, ulong position)
315 Eina.Log.Debug("function efl_io_positioner_position_set was called");
316 Efl.Eo.IWrapper wrapper = Efl.Eo.Globals.data_get(pd);
317 if(wrapper != null) {
318 bool _ret_var = default(bool);
320 _ret_var = ((PositionerConcrete)wrapper).SetPosition( position);
321 } catch (Exception e) {
322 Eina.Log.Warning($"Callback error: {e.ToString()}");
323 Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
327 return efl_io_positioner_position_set_ptr.Value.Delegate(Efl.Eo.Globals.efl_super(obj, Efl.Eo.Globals.efl_class_get(obj)), position);
330 private static efl_io_positioner_position_set_delegate efl_io_positioner_position_set_static_delegate;
333 private delegate Eina.Error efl_io_positioner_seek_delegate(System.IntPtr obj, System.IntPtr pd, long offset, Efl.Io.PositionerWhence whence);
336 public delegate Eina.Error efl_io_positioner_seek_api_delegate(System.IntPtr obj, long offset, Efl.Io.PositionerWhence whence);
337 public static Efl.Eo.FunctionWrapper<efl_io_positioner_seek_api_delegate> efl_io_positioner_seek_ptr = new Efl.Eo.FunctionWrapper<efl_io_positioner_seek_api_delegate>(_Module, "efl_io_positioner_seek");
338 private static Eina.Error seek(System.IntPtr obj, System.IntPtr pd, long offset, Efl.Io.PositionerWhence whence)
340 Eina.Log.Debug("function efl_io_positioner_seek was called");
341 Efl.Eo.IWrapper wrapper = Efl.Eo.Globals.data_get(pd);
342 if(wrapper != null) {
343 Eina.Error _ret_var = default( Eina.Error);
345 _ret_var = ((PositionerConcrete)wrapper).Seek( offset, whence);
346 } catch (Exception e) {
347 Eina.Log.Warning($"Callback error: {e.ToString()}");
348 Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
352 return efl_io_positioner_seek_ptr.Value.Delegate(Efl.Eo.Globals.efl_super(obj, Efl.Eo.Globals.efl_class_get(obj)), offset, whence);
355 private static efl_io_positioner_seek_delegate efl_io_positioner_seek_static_delegate;
358 namespace Efl { namespace Io {
359 /// <summary>Seek position modes</summary>
360 public enum PositionerWhence
362 /// <summary>Seek from start of the stream/file</summary>
364 /// <summary>Seek from current position</summary>
366 /// <summary>Seek from the end of stream/file</summary>