XuNeo / luavgl

lua + lvgl = luavgl An optimized lvgl Lua binding
MIT License
57 stars 13 forks source link

event: simplify event porting by leveraging lvgl's event dsc handler #15

Closed XuNeo closed 4 months ago

XuNeo commented 4 months ago

Only lua created obj can add event callback, and it makes logic much simpler.

Fix #10

Depends on the dsc returned from add_event, need to wait lvgl merge this change firstly.

XuNeo commented 4 months ago

For event added from lua, the e->user_data is expected to be struct event_callback_s*. Same object can add event both from lua and native C code. Thus we need a way to distinguish between this two scenarios.

For object created from lua, we have struct luavgl_obj_t, similar to what lvgl stores event list, we also add an lv_array_t to luavgl_obj_t to store events from lua.

Following operation(add/remove) to event called from lua will rely on this array.

XuNeo commented 4 months ago

It's common in luavgl to monitor is an obj is deleted by

  lv_obj_add_event_cb(obj, obj_delete_cb, LV_EVENT_DELETE, L);

In the callback, cleanup resources added from lua, including the events. Be noted that the callback context is from lvgl's event send function which iterates the event list of this obj. Modify the event array by lv_obj_event_remove_dsc will corrupt the lvgl's loop leading to heap-use-after-free memory issue.

Normally it's not necessary to manually remove the event dsc, obj is going to be deleted anyway.