efl-csharp: Fix event callback removal.

Summary:
Use the native event loaded from dlsym directly instead of wrapping it
in a Event_Description structure.

Fixes: T7355

Reviewers: felipealmeida, vitor.sousa, segfaultxavi

Reviewed By: vitor.sousa

Subscribers: cedric, #reviewers, #committers

Tags: #efl_language_bindings

Differential Revision: https://phab.enlightenment.org/D6981
This commit is contained in:
Lauro Moura 2018-09-05 14:25:11 -03:00 committed by Vitor Sousa
parent 5e107aa19d
commit 4c53151096
4 changed files with 45 additions and 8 deletions

View File

@ -484,7 +484,11 @@ struct klass
<< scope_tab << scope_tab << "if (!event_cb_count.TryGetValue(key, out event_count))\n"
<< scope_tab << scope_tab << scope_tab << "event_cb_count[key] = event_count;\n"
<< scope_tab << scope_tab << "if (event_count == 0) {\n"
<< scope_tab << scope_tab << scope_tab << "IntPtr desc = efl.eo.Globals.dlsym(efl.eo.Globals.RTLD_DEFAULT, key);\n"
<< scope_tab << scope_tab << scope_tab << "IntPtr desc = efl.Event_Description.GetNative(key);\n"
<< scope_tab << scope_tab << scope_tab << "if (desc == IntPtr.Zero) {\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "eina.Log.Error($\"Failed to get native event {key}\");\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "return false;\n"
<< scope_tab << scope_tab << scope_tab << "}\n"
<< scope_tab << scope_tab << scope_tab << "bool result = efl.eo.Globals.efl_event_callback_priority_add(handle, desc, 0, evt_delegate, System.IntPtr.Zero);\n"
<< scope_tab << scope_tab << scope_tab << "if (!result) {\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "eina.Log.Error($\"Failed to add event proxy for event {key}\");\n"
@ -500,7 +504,11 @@ struct klass
<< scope_tab << scope_tab << "if (!event_cb_count.TryGetValue(key, out event_count))\n"
<< scope_tab << scope_tab << scope_tab << "event_cb_count[key] = event_count;\n"
<< scope_tab << scope_tab << "if (event_count == 1) {\n"
<< scope_tab << scope_tab << scope_tab << "efl.Event_Description desc = new efl.Event_Description(key);\n"
<< scope_tab << scope_tab << scope_tab << "IntPtr desc = efl.Event_Description.GetNative(key);\n"
<< scope_tab << scope_tab << scope_tab << "if (desc == IntPtr.Zero) {\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "eina.Log.Error($\"Failed to get native event {key}\");\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "return false;\n"
<< scope_tab << scope_tab << scope_tab << "}\n"
<< scope_tab << scope_tab << scope_tab << "bool result = efl.eo.Globals.efl_event_callback_del(handle, desc, evt_delegate, System.IntPtr.Zero);\n"
<< scope_tab << scope_tab << scope_tab << "if (!result) {\n"
<< scope_tab << scope_tab << scope_tab << scope_tab << "eina.Log.Error($\"Failed to remove event proxy for event {key}\");\n"

View File

@ -59,7 +59,7 @@ public class Globals {
System.IntPtr data);
[DllImport(efl.Libs.Eo)] public static extern bool efl_event_callback_del(
System.IntPtr obj,
efl.Event_Description desc,
IntPtr desc,
efl.Event_Cb cb,
System.IntPtr data);
[DllImport(efl.Libs.Eo)] public static extern IntPtr

View File

@ -82,6 +82,14 @@ public struct Event_Description {
private static Dictionary<string, IntPtr> descriptions = new Dictionary<string, IntPtr>();
public Event_Description(string name)
{
this.Name = GetNative(name);
this.Unfreezable = false;
this.Legacy_is = false;
this.Restart = false;
}
public static IntPtr GetNative(string name)
{
if (!descriptions.ContainsKey(name))
{
@ -93,11 +101,7 @@ public struct Event_Description {
}
descriptions.Add(name, data);
}
this.Name = descriptions[name];
this.Unfreezable = false;
this.Legacy_is = false;
this.Restart = false;
return descriptions[name];
}
};

View File

@ -165,4 +165,29 @@ class TestEoEvents
Test.AssertEquals(sent, received);
}
}
class TestEventAddRemove
{
public static void test_add_remove_event()
{
test.ITesting obj = new test.Testing();
bool called = true;
EventHandler<test.Testing.EvtWithIntEvt_Args> evtCb = (object sender, EvtWithIntEvt_Args e) => {
called = true;
};
obj.EvtWithIntEvt += evtCb;
obj.EmitEventWithInt(42);
Test.Assert(called);
called = false;
obj.EvtWithIntEvt -= evtCb;
obj.EmitEventWithInt(42);
Test.Assert(!called);
}
}
}