summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eo_mono/iwrapper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bindings/mono/eo_mono/iwrapper.cs')
-rw-r--r--src/bindings/mono/eo_mono/iwrapper.cs254
1 files changed, 204 insertions, 50 deletions
diff --git a/src/bindings/mono/eo_mono/iwrapper.cs b/src/bindings/mono/eo_mono/iwrapper.cs
index 16acf6a..7444215 100644
--- a/src/bindings/mono/eo_mono/iwrapper.cs
+++ b/src/bindings/mono/eo_mono/iwrapper.cs
@@ -12,19 +12,42 @@ using EoG = Efl.Eo.Globals;
12namespace Efl { namespace Eo { 12namespace Efl { namespace Eo {
13 13
14public class Globals { 14public class Globals {
15 [DllImport(efl.Libs.Eo)] public static extern void efl_object_init(); 15 [return: MarshalAs(UnmanagedType.U1)]
16 [DllImport(efl.Libs.Eo)] public static extern void efl_object_shutdown(); 16 public delegate bool efl_object_init_delegate();
17 public static FunctionWrapper<efl_object_init_delegate> efl_object_init_ptr =
18 new FunctionWrapper<efl_object_init_delegate>(efl.Libs.EoModule, "efl_object_init");
19 public static bool efl_object_init() => efl_object_init_ptr.Value.Delegate();
20
21 public delegate void efl_object_shutdown_delegate();
22 public static FunctionWrapper<efl_object_shutdown_delegate> efl_object_shutdown_ptr = new FunctionWrapper<efl_object_shutdown_delegate>(efl.Libs.EoModule, "efl_object_shutdown");
23 public static void efl_object_shutdown() => efl_object_shutdown_ptr.Value.Delegate();
24 // [DllImport(efl.Libs.Eo)] public static extern void efl_object_shutdown();
25 public static FunctionWrapper<_efl_add_internal_start_delegate> _efl_add_internal_start_ptr = new FunctionWrapper<_efl_add_internal_start_delegate>(efl.Libs.EoModule, "_efl_add_internal_start");
26 public delegate IntPtr
27 _efl_add_internal_start_delegate([MarshalAs(UnmanagedType.LPStr)] String file, int line,
28 IntPtr klass, IntPtr parent, byte is_ref, byte is_fallback);
17 [DllImport(efl.Libs.Eo)] public static extern IntPtr 29 [DllImport(efl.Libs.Eo)] public static extern IntPtr
18 _efl_add_internal_start([MarshalAs(UnmanagedType.LPStr)] String file, int line, 30 _efl_add_internal_start([MarshalAs(UnmanagedType.LPStr)] String file, int line,
19 IntPtr klass, IntPtr parent, byte is_ref, byte is_fallback); 31 IntPtr klass, IntPtr parent, byte is_ref, byte is_fallback);
32 public delegate IntPtr
33 _efl_add_end_delegate(IntPtr eo, byte is_ref, byte is_fallback);
20 [DllImport(efl.Libs.Eo)] public static extern IntPtr 34 [DllImport(efl.Libs.Eo)] public static extern IntPtr
21 _efl_add_end(IntPtr eo, byte is_ref, byte is_fallback); 35 _efl_add_end(IntPtr eo, byte is_ref, byte is_fallback);
36 public delegate IntPtr
37 efl_ref_delegate(IntPtr eo);
22 [DllImport(efl.Libs.Eo)] public static extern IntPtr 38 [DllImport(efl.Libs.Eo)] public static extern IntPtr
23 efl_ref(IntPtr eo); 39 efl_ref(IntPtr eo);
40 public delegate void
41 efl_unref_delegate(IntPtr eo);
24 [DllImport(efl.Libs.CustomExports)] public static extern void 42 [DllImport(efl.Libs.CustomExports)] public static extern void
25 efl_unref(IntPtr eo); 43 efl_unref(IntPtr eo);
44 public delegate int
45 efl_ref_count_delegate(IntPtr eo);
26 [DllImport(efl.Libs.Eo)] public static extern int 46 [DllImport(efl.Libs.Eo)] public static extern int
27 efl_ref_count(IntPtr eo); 47 efl_ref_count(IntPtr eo);
48
49 [DllImport(efl.Libs.Eo)] public static extern IntPtr
50 efl_class_name_get(IntPtr eo);
28 [DllImport(efl.Libs.Eo)] public static extern IntPtr 51 [DllImport(efl.Libs.Eo)] public static extern IntPtr
29 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr term); 52 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr term);
30 [DllImport(efl.Libs.Eo)] public static extern IntPtr 53 [DllImport(efl.Libs.Eo)] public static extern IntPtr
@@ -123,36 +146,45 @@ public class Globals {
123 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr extn1, IntPtr extn2, IntPtr extn3, IntPtr extn4, IntPtr extn5, IntPtr extn6, IntPtr extn7, IntPtr extn8, IntPtr extn9, IntPtr extn10, IntPtr extn11, IntPtr extn12, IntPtr extn13, IntPtr extn14, IntPtr extn15, IntPtr extn16, IntPtr extn17, IntPtr extn18, IntPtr extn19, IntPtr extn20, IntPtr extn21, IntPtr extn22, IntPtr extn23, IntPtr extn24, IntPtr extn25, IntPtr extn26, IntPtr extn27, IntPtr extn28, IntPtr extn29, IntPtr extn30, IntPtr extn31, IntPtr extn32, IntPtr extn33, IntPtr extn34, IntPtr extn35, IntPtr extn36, IntPtr extn37, IntPtr extn38, IntPtr extn39, IntPtr extn40, IntPtr extn41, IntPtr extn42, IntPtr extn43, IntPtr extn44, IntPtr extn45, IntPtr extn46, IntPtr extn47, IntPtr term); 146 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr extn1, IntPtr extn2, IntPtr extn3, IntPtr extn4, IntPtr extn5, IntPtr extn6, IntPtr extn7, IntPtr extn8, IntPtr extn9, IntPtr extn10, IntPtr extn11, IntPtr extn12, IntPtr extn13, IntPtr extn14, IntPtr extn15, IntPtr extn16, IntPtr extn17, IntPtr extn18, IntPtr extn19, IntPtr extn20, IntPtr extn21, IntPtr extn22, IntPtr extn23, IntPtr extn24, IntPtr extn25, IntPtr extn26, IntPtr extn27, IntPtr extn28, IntPtr extn29, IntPtr extn30, IntPtr extn31, IntPtr extn32, IntPtr extn33, IntPtr extn34, IntPtr extn35, IntPtr extn36, IntPtr extn37, IntPtr extn38, IntPtr extn39, IntPtr extn40, IntPtr extn41, IntPtr extn42, IntPtr extn43, IntPtr extn44, IntPtr extn45, IntPtr extn46, IntPtr extn47, IntPtr term);
124 [DllImport(efl.Libs.Eo)] public static extern IntPtr 147 [DllImport(efl.Libs.Eo)] public static extern IntPtr
125 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr extn1, IntPtr extn2, IntPtr extn3, IntPtr extn4, IntPtr extn5, IntPtr extn6, IntPtr extn7, IntPtr extn8, IntPtr extn9, IntPtr extn10, IntPtr extn11, IntPtr extn12, IntPtr extn13, IntPtr extn14, IntPtr extn15, IntPtr extn16, IntPtr extn17, IntPtr extn18, IntPtr extn19, IntPtr extn20, IntPtr extn21, IntPtr extn22, IntPtr extn23, IntPtr extn24, IntPtr extn25, IntPtr extn26, IntPtr extn27, IntPtr extn28, IntPtr extn29, IntPtr extn30, IntPtr extn31, IntPtr extn32, IntPtr extn33, IntPtr extn34, IntPtr extn35, IntPtr extn36, IntPtr extn37, IntPtr extn38, IntPtr extn39, IntPtr extn40, IntPtr extn41, IntPtr extn42, IntPtr extn43, IntPtr extn44, IntPtr extn45, IntPtr extn46, IntPtr extn47, IntPtr extn48, IntPtr term); 148 efl_class_new(IntPtr class_description, IntPtr parent, IntPtr extn1, IntPtr extn2, IntPtr extn3, IntPtr extn4, IntPtr extn5, IntPtr extn6, IntPtr extn7, IntPtr extn8, IntPtr extn9, IntPtr extn10, IntPtr extn11, IntPtr extn12, IntPtr extn13, IntPtr extn14, IntPtr extn15, IntPtr extn16, IntPtr extn17, IntPtr extn18, IntPtr extn19, IntPtr extn20, IntPtr extn21, IntPtr extn22, IntPtr extn23, IntPtr extn24, IntPtr extn25, IntPtr extn26, IntPtr extn27, IntPtr extn28, IntPtr extn29, IntPtr extn30, IntPtr extn31, IntPtr extn32, IntPtr extn33, IntPtr extn34, IntPtr extn35, IntPtr extn36, IntPtr extn37, IntPtr extn38, IntPtr extn39, IntPtr extn40, IntPtr extn41, IntPtr extn42, IntPtr extn43, IntPtr extn44, IntPtr extn45, IntPtr extn46, IntPtr extn47, IntPtr extn48, IntPtr term);
126 [DllImport(efl.Libs.Eo)] public static extern byte efl_class_functions_set(IntPtr klass_id, IntPtr object_ops, IntPtr class_ops, IntPtr reflection_ops); 149
150 public delegate byte efl_class_functions_set_delegate(IntPtr klass_id, IntPtr object_ops, IntPtr class_ops);
151 [DllImport(efl.Libs.Eo)] public static extern byte efl_class_functions_set(IntPtr klass_id, IntPtr object_ops, IntPtr class_ops);
152 public delegate IntPtr efl_data_scope_get_delegate(IntPtr obj, IntPtr klass);
127 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_data_scope_get(IntPtr obj, IntPtr klass); 153 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_data_scope_get(IntPtr obj, IntPtr klass);
154 public delegate IntPtr efl_super_delegate(IntPtr obj, IntPtr klass);
128 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_super(IntPtr obj, IntPtr klass); 155 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_super(IntPtr obj, IntPtr klass);
156 public delegate IntPtr efl_class_get_delegate(IntPtr obj);
129 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_class_get(IntPtr obj); 157 [DllImport(efl.Libs.Eo)] public static extern IntPtr efl_class_get(IntPtr obj);
130#if WIN32 158 public delegate IntPtr dlerror_delegate();
131 public static IntPtr RTLD_DEFAULT = new IntPtr(1);
132#else
133 public static IntPtr RTLD_DEFAULT = new IntPtr(0);
134#endif
135 [DllImport(efl.Libs.Evil)] public static extern IntPtr dlerror(); 159 [DllImport(efl.Libs.Evil)] public static extern IntPtr dlerror();
136 [DllImport(efl.Libs.Evil)] public static extern IntPtr dlsym
137 (IntPtr handle, [MarshalAs(UnmanagedType.LPStr)] String name);
138 160
139 [DllImport(efl.Libs.Eo)] public static extern bool efl_event_callback_priority_add( 161 public delegate bool efl_event_callback_priority_add_delegate(
162 System.IntPtr obj,
163 IntPtr desc,
164 short priority,
165 Efl.EventCb cb,
166 System.IntPtr data);
167 [DllImport(efl.Libs.Eo)] public static extern bool efl_event_callback_priority_add(
140 System.IntPtr obj, 168 System.IntPtr obj,
141 IntPtr desc, 169 IntPtr desc,
142 short priority, 170 short priority,
143 Efl.EventCb cb, 171 Efl.EventCb cb,
144 System.IntPtr data); 172 System.IntPtr data);
145 [DllImport(efl.Libs.Eo)] public static extern bool efl_event_callback_del( 173 public delegate bool efl_event_callback_del_delegate(
146 System.IntPtr obj, 174 System.IntPtr obj,
147 IntPtr desc, 175 IntPtr desc,
148 Efl.EventCb cb, 176 Efl.EventCb cb,
149 System.IntPtr data); 177 System.IntPtr data);
178 [DllImport(efl.Libs.Eo)] public static extern bool efl_event_callback_del(
179 System.IntPtr obj,
180 IntPtr desc,
181 Efl.EventCb cb,
182 System.IntPtr data);
183 public delegate IntPtr
184 efl_object_legacy_only_event_description_get_delegate([MarshalAs(UnmanagedType.LPStr)] String name);
150 [DllImport(efl.Libs.Eo)] public static extern IntPtr 185 [DllImport(efl.Libs.Eo)] public static extern IntPtr
151 efl_object_legacy_only_event_description_get([MarshalAs(UnmanagedType.LPStr)] String name); 186 efl_object_legacy_only_event_description_get([MarshalAs(UnmanagedType.LPStr)] String name);
152 187
153 public static System.Collections.Concurrent.ConcurrentDictionary<System.Type, System.IntPtr> klasses
154 = new System.Collections.Concurrent.ConcurrentDictionary<System.Type, System.IntPtr>();
155
156 public const int RTLD_NOW = 2; 188 public const int RTLD_NOW = 2;
157 189
158 public delegate byte class_initializer(IntPtr klass); 190 public delegate byte class_initializer(IntPtr klass);
@@ -241,7 +273,7 @@ public class Globals {
241 } 273 }
242 public static byte class_initializer_call(IntPtr klass, System.Type type) 274 public static byte class_initializer_call(IntPtr klass, System.Type type)
243 { 275 {
244 Eina.Log.Debug($"called with 0x{klass.ToInt64()} {type}"); 276 Eina.Log.Debug($"called with 0x{klass.ToInt64():x} {type}");
245 Efl.Eo.NativeClass nativeClass = get_native_class(type.BaseType); 277 Efl.Eo.NativeClass nativeClass = get_native_class(type.BaseType);
246 278
247 if (nativeClass != null) 279 if (nativeClass != null)
@@ -260,7 +292,7 @@ public class Globals {
260 if(nc != null) 292 if(nc != null)
261 { 293 {
262 var moredescs = nc.GetEoOps(type); 294 var moredescs = nc.GetEoOps(type);
263 Eina.Log.Debug("adding {moredescs.Count} more descs to registration"); 295 Eina.Log.Debug($"adding {moredescs.Count} more descs to registration");
264 descs.AddRange(moredescs); 296 descs.AddRange(moredescs);
265 count = descs.Count; 297 count = descs.Count;
266 } 298 }
@@ -279,7 +311,7 @@ public class Globals {
279 ops.count = (UIntPtr)count; 311 ops.count = (UIntPtr)count;
280 IntPtr ops_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ops)); 312 IntPtr ops_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ops));
281 Marshal.StructureToPtr(ops, ops_ptr, false); 313 Marshal.StructureToPtr(ops, ops_ptr, false);
282 Efl.Eo.Globals.efl_class_functions_set(klass, ops_ptr, IntPtr.Zero, IntPtr.Zero); 314 Efl.Eo.Globals.efl_class_functions_set(klass, ops_ptr, IntPtr.Zero);
283 //EoKlass = klass; 315 //EoKlass = klass;
284 } 316 }
285 else 317 else
@@ -392,38 +424,6 @@ public class Globals {
392 return null; 424 return null;
393 } 425 }
394 426
395 public static IntPtr cached_string_to_intptr(Dictionary<String, IntPtr> dict, String str)
396 {
397 IntPtr ptr = IntPtr.Zero;
398
399 if (str == null)
400 return ptr;
401
402 if (!dict.TryGetValue(str, out ptr))
403 {
404 ptr = Eina.StringConversion.ManagedStringToNativeUtf8Alloc(str);
405 dict[str] = ptr;
406 }
407
408 return ptr;
409 }
410
411 public static IntPtr cached_stringshare_to_intptr(Dictionary<String, IntPtr> dict, String str)
412 {
413 IntPtr ptr = IntPtr.Zero;
414
415 if (str == null)
416 return ptr;
417
418 if (!dict.TryGetValue(str, out ptr))
419 {
420 ptr = Eina.Stringshare.eina_stringshare_add(str);
421 dict[str] = ptr;
422 }
423
424 return ptr;
425 }
426
427 public static void free_dict_values(Dictionary<String, IntPtr> dict) 427 public static void free_dict_values(Dictionary<String, IntPtr> dict)
428 { 428 {
429 foreach(IntPtr ptr in dict.Values) 429 foreach(IntPtr ptr in dict.Values)
@@ -529,6 +529,120 @@ public interface IWrapper
529 } 529 }
530} 530}
531 531
532public static class ClassRegister
533{
534 public static System.Type GetManagedType(IntPtr klass)
535 {
536 System.Type t;
537 if (Efl.Eo.ClassRegister.typeFromKlass.TryGetValue(klass, out t))
538 return t;
539
540 // If it isn't on the dictionary then it is a Native binding class
541 IntPtr namePtr = Efl.Eo.Globals.efl_class_name_get(klass);
542 if (namePtr == IntPtr.Zero) {
543 throw new System.InvalidOperationException($"Could not get Native class name. Handle: {klass}");
544 }
545
546 string name = Eina.StringConversion.NativeUtf8ToManagedString(namePtr)
547 .Replace("_", ""); // Convert Efl C name to C# name
548
549 var curr_asm = typeof(IWrapper).Assembly;
550 t = curr_asm.GetType(name);
551 if (t == null)
552 {
553 foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
554 {
555 if (assembly == curr_asm)
556 continue;
557
558 t = assembly.GetType(name);
559 if (t != null)
560 break;
561 }
562 if (t == null) {
563 throw new System.InvalidOperationException($"Could not find the C# binding class for the EFL class: {name}");
564 }
565 }
566 AddToKlassTypeBiDictionary(klass, t); // Cache it in the dictionary
567 return t;
568 }
569
570 public static IntPtr GetKlass(System.Type objectType)
571 {
572 IntPtr klass;
573 if (klassFromType.TryGetValue(objectType, out klass))
574 return klass;
575
576 // Check if it is a Native binding class
577 klass = GetNativeKlassPtr(objectType);
578 if (klass != IntPtr.Zero) {
579 // Add to the dictionary cache
580 AddToKlassTypeBiDictionary(klass, objectType);
581 return klass;
582 }
583
584 // Unregistered Inherited class, let's register it
585 IntPtr baseKlass = GetNativeBaseKlassPtr(objectType);
586 if (baseKlass == IntPtr.Zero)
587 throw new System.InvalidOperationException($"Could not get base C# binding class for Inherited type: {objectType.FullName}");
588 return RegisterKlass(baseKlass, objectType);
589 }
590
591 public static IntPtr GetInheritKlassOrRegister(IntPtr baseKlass, System.Type objectType)
592 {
593 IntPtr klass;
594 if (klassFromType.TryGetValue(objectType, out klass))
595 return klass;
596
597 return RegisterKlass(baseKlass, objectType);
598 }
599
600 private static IntPtr RegisterKlass(IntPtr baseKlass, System.Type objectType)
601 {
602 lock (klassAllocLock) {
603 IntPtr newKlass = Efl.Eo.Globals.register_class(objectType.FullName, baseKlass, objectType);
604 if (newKlass == IntPtr.Zero) {
605 throw new System.InvalidOperationException($"Failed to register class '{objectType.FullName}'");
606 }
607 AddToKlassTypeBiDictionary(newKlass, objectType);
608 return newKlass;
609 }
610 }
611
612 private static IntPtr GetNativeBaseKlassPtr(System.Type objectType)
613 {
614 for (System.Type t = objectType.BaseType; t != null; t = t.BaseType)
615 {
616 var method = t.GetMethod("GetEflClassStatic",
617 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
618 if (method != null)
619 return (IntPtr) method.Invoke(null, null);
620 }
621 throw new System.InvalidOperationException($"Class '{objectType.FullName}' is not an Efl object");
622 }
623
624 private static IntPtr GetNativeKlassPtr(System.Type objectType)
625 {
626 var method = objectType.GetMethod("GetEflClassStatic",
627 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
628 return (IntPtr) method?.Invoke(null, null);
629 }
630
631 public static void AddToKlassTypeBiDictionary(IntPtr klassPtr, System.Type objectType)
632 {
633 klassFromType[objectType] = klassPtr;
634 typeFromKlass[klassPtr] = objectType;
635 }
636
637 public static System.Collections.Concurrent.ConcurrentDictionary<System.Type, System.IntPtr> klassFromType
638 = new System.Collections.Concurrent.ConcurrentDictionary<System.Type, System.IntPtr>();
639
640 public static System.Collections.Concurrent.ConcurrentDictionary<System.IntPtr, System.Type> typeFromKlass
641 = new System.Collections.Concurrent.ConcurrentDictionary<System.IntPtr, System.Type>();
642
643 private static readonly object klassAllocLock = new object();
644}
645
532public interface IOwnershipTag 646public interface IOwnershipTag
533{ 647{
534} 648}
@@ -587,6 +701,46 @@ public class MarshalTest<T, U> : ICustomMarshaler
587 } 701 }
588} 702}
589 703
704///<summary>Marshals between System.Type instances and Eo classes (IntPtrs).</summary>
705public class MarshalEflClass : ICustomMarshaler
706{
707 public static ICustomMarshaler GetInstance(string cookie)
708 {
709 Eina.Log.Debug("MarshalTest.GetInstance cookie " + cookie);
710 return new MarshalEflClass();
711 }
712 public void CleanUpManagedData(object ManagedObj)
713 {
714 }
715
716 public void CleanUpNativeData(IntPtr pNativeData)
717 {
718 }
719
720 public int GetNativeDataSize()
721 {
722 Eina.Log.Debug("MarshalTest.GetNativeDataSize");
723 return 0;
724 }
725
726 public IntPtr MarshalManagedToNative(object ManagedObj)
727 {
728 Eina.Log.Debug("MarshalTest.MarshallManagedToNative");
729 if (ManagedObj == null)
730 return IntPtr.Zero;
731 var t = (System.Type) ManagedObj;
732 return Efl.Eo.ClassRegister.GetKlass(t);
733 }
734
735 public object MarshalNativeToManaged(IntPtr pNativeData)
736 {
737 Eina.Log.Debug("MarshalTest.MarshalNativeToManaged");
738 if (pNativeData == IntPtr.Zero)
739 return null;
740 return Efl.Eo.ClassRegister.GetManagedType(pNativeData);
741 }
742}
743
590public class StringPassOwnershipMarshaler : ICustomMarshaler { 744public class StringPassOwnershipMarshaler : ICustomMarshaler {
591 public object MarshalNativeToManaged(IntPtr pNativeData) { 745 public object MarshalNativeToManaged(IntPtr pNativeData) {
592 var ret = Eina.StringConversion.NativeUtf8ToManagedString(pNativeData); 746 var ret = Eina.StringConversion.NativeUtf8ToManagedString(pNativeData);