summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/obj.c
blob: a316f59c7177534d0eee7a05d1bd1e435d85a431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#include "luavgl.h"
#include "private.h"

/* extended feature for object */
#include "event.c"
#include "style.c"
#include "widgets/widgets.c"

static int luavgl_anim_create(lua_State *L);
static int luavgl_obj_delete(lua_State *L);

static void _lv_obj_set_align(void *obj, lua_State *L)
{
  if (lua_isinteger(L, -1)) {
    lv_obj_align(obj, lua_tointeger(L, -1), 0, 0);
    return;
  }

  if (!lua_istable(L, -1)) {
    luaL_argerror(L, -1, "should be table.");
    debug("para should be table.");
    return;
  }

  lua_getfield(L, -1, "type");
  lv_align_t align = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, -1, "x_ofs");
  lv_coord_t x_ofs = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, -1, "y_ofs");
  lv_coord_t y_ofs = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lv_obj_align(obj, align, x_ofs, y_ofs);
}

/**
 * @brief Set obj properties based on property table on stack top
 *
 * Internally used.
 */
static inline void luavgl_setup_obj(lua_State *L, lv_obj_t *obj)
{
  luavgl_iterate(L, -1, luavgl_obj_set_property_kv, obj);
}

static void obj_delete_cb(lv_event_t *e)
{
  lua_State *L = e->user_data;
  lua_pushlightuserdata(L, e->current_target);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnoneornil(L, -1)) {
    goto pop_exit;
  }

  luavgl_obj_t *lobj = luavgl_to_lobj(L, -1);
  if (lobj->lua_created)
    goto pop_exit;

  luavgl_obj_delete(L);
  return;

pop_exit:
  lua_pop(L, 1);
  return;
}

/**
 * get the obj userdata and uservalue, if uservalue is not a table, then add
 * one. result stack: table(from uservalue)
 * return uservalue type: LUA_TTABLE
 */
LUALIB_API int luavgl_obj_getuserdatauv(lua_State *L, int idx)
{
  int type = lua_getuservalue(L, idx);
  if (type == LUA_TTABLE)
    return type;

  lua_pop(L, 1);
  /* initial element: 1 */
  lua_createtable(L, 0, 1);

#if 0 /* reserved slot, not used for now */
  lua_pushinteger(L, 1);
  lua_pushnil(L);
  lua_rawset(L, -3);
#endif
  lua_pushvalue(L, -1); /* leave one on stack */
  lua_setuservalue(L, idx > 0 ? idx : idx - 3);
  return LUA_TTABLE;
}

static int luavgl_obj_create(lua_State *L)
{
  return luavgl_obj_create_helper(L, lv_obj_create);
}

static int luavgl_obj_delete(lua_State *L)
{
  luavgl_obj_t *lobj;

  /**
   * Some widget may create child obj that doesn't belong to lua. Ignore them
   * and report no error.
   */
  if (!(lobj = lua_touserdata(L, -1))) {
    return 0;
  }

  if (lobj->obj == NULL) {
    /* could be already deleted, but not gc'ed */
    return 0;
  }

  uint32_t cnt = lv_obj_get_child_cnt(lobj->obj);

  for (int i = cnt - 1; i >= 0; i--) {
    lv_obj_t *child = lv_obj_get_child(lobj->obj, i);
    lua_checkstack(L, 2);
    lua_pushlightuserdata(L, child);
    lua_rawget(L, LUA_REGISTRYINDEX);
    if (lua_isnoneornil(L, -1)) {
      continue;
    }

    luavgl_obj_delete(L);
  }

  luavgl_obj_remove_event_all(L, lobj);

  /* delete obj firstly, then cleanup memory */
  if (lobj->lua_created) {
    lv_obj_del(lobj->obj);
  }

  lua_checkstack(L, 2);
  /* remove userdata from registry. */
  lua_pushlightuserdata(L, lobj->obj);
  lua_pushnil(L);
  lua_rawset(L, LUA_REGISTRYINDEX);

  debug("delete obj: %p\n", lobj->obj);
  lobj->obj = NULL;

  lua_pop(L, 1); /* remove the userdata para */
  return 0;
}

static int luavgl_obj_clean(lua_State *L)
{
  luavgl_obj_t *lobj = luavgl_to_lobj(L, -1);
  if (lobj == NULL || lobj->obj == NULL)
    return 0;

  lv_obj_t *obj = lobj->obj;
  uint32_t cnt = lv_obj_get_child_cnt(obj);

  for (int i = cnt - 1; i >= 0; i--) {
    lv_obj_t *child = lv_obj_get_child(obj, i);

    lua_checkstack(L, 2);
    lua_pushlightuserdata(L, child);
    lua_rawget(L, LUA_REGISTRYINDEX);
    luavgl_obj_delete(L);
  }

  lua_pop(L, 1); /* remove the userdata para */
  return 0;
}

static int luavgl_obj_set(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);

  if (!lua_istable(L, -1)) {
    luaL_error(L, "expect a table on 2nd para.");
    return 0;
  }

  luavgl_setup_obj(L, obj);
  return 0;
}

/**
 * obj:align_to({base=base, type=type, x_ofs=0, y_ofs=0})
 */
static int luavgl_obj_align_to(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);

  if (!lua_istable(L, 2)) {
    debug("para should be table.");
    return luaL_argerror(L, 2, "should be table.");
  }

  lua_getfield(L, 2, "type");
  lv_align_t align = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, 2, "base");
  lv_obj_t *base = luavgl_to_lobj(L, -1)->obj;
  lua_pop(L, 1);
  if (base == NULL) {
    debug("base is not lua obj");
    return luaL_argerror(L, -1, "base is not lua obj");
  }

  lua_getfield(L, 2, "x_ofs");
  lv_coord_t x_ofs = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lua_getfield(L, 2, "y_ofs");
  lv_coord_t y_ofs = lua_tointeger(L, -1);
  lua_pop(L, 1);

  lv_obj_align_to(obj, base, align, x_ofs, y_ofs);
  return 0;
}

static int luavgl_obj_set_parent(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_t *parent = luavgl_to_obj(L, 2);
  lv_obj_set_parent(obj, parent);
  return 0;
}

static int luavgl_obj_get_screen(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_t *screen = lv_obj_get_screen(obj);

  /* check if userdata is added to this obj, so lua can access it. */
  lua_pushlightuserdata(L, screen);
  lua_rawget(L, LUA_REGISTRYINDEX);

  if (lua_isnil(L, -1)) {
    /* create luavgl object and attach screen on it. */
    luavgl_obj_t *lobj = luavgl_add_lobj(L, screen);
    /* mark it's non-lua created, thus cannot be deleted by lua */
    lobj->lua_created = false;
  }

  return 1;
}

static int luavgl_obj_get_parent(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_t *parent = lv_obj_get_parent(obj);

  /* check if userdata is added to this obj, so lua can access it. */
  lua_pushlightuserdata(L, parent);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnil(L, -1)) {
    /* create luavgl object and attach screen on it. */
    luavgl_obj_t *lobj = luavgl_add_lobj(L, parent);
    /* mark it's non-lua created, thus cannot be deleted by lua */
    lobj->lua_created = false;
  }

  return 1;
}

static int luavgl_obj_set_get_parent(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  if (!lua_isnoneornil(L, 2)) {
    lv_obj_t *parent = luavgl_to_obj(L, 2);
    lv_obj_set_parent(obj, parent);
  }

  return luavgl_obj_get_parent(L);
}

static int luavgl_obj_get_child(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);

  int id = luavgl_tointeger(L, 2);
  lv_obj_t *child = lv_obj_get_child(obj, id);
  if (child == NULL) {
    lua_pushnil(L);
    return 1;
  }

  /* check if userdata is added to this obj, so lua can access it. */
  lua_pushlightuserdata(L, child);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (lua_isnil(L, -1)) {
    luavgl_add_lobj(L, child)->lua_created = false;
  }

  return 1;
}

static int luavgl_obj_get_child_cnt(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushinteger(L, lv_obj_get_child_cnt(obj));
  return 1;
}

static int luavgl_obj_get_state(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_state_t state = lv_obj_get_state(obj);
  lua_pushinteger(L, state);

  return 1;
}

/**
 * obj:scroll_to({x=10})
 * obj:scroll_to({x=10, anim=true})
 * obj:scroll_to({x=10, y=100, anim=false})
 */
static int luavgl_obj_scroll_to(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);

  if (!lua_istable(L, -1)) {
    luaL_argerror(L, -1, "should be table {x=0,y=0,anim=true}");
  }

  lua_getfield(L, -1, "anim");
  bool anim = luavgl_tointeger(L, -1);
  lua_pop(L, 1);

  lv_coord_t v;
  lua_getfield(L, -1, "x");
  if (!lua_isnil(L, -1)) {
    v = lua_tointeger(L, -1);
    lv_obj_scroll_to_x(obj, v, anim ? LV_ANIM_ON : LV_ANIM_OFF);
  }
  lua_pop(L, 1);

  lua_getfield(L, -1, "y");
  if (!lua_isnil(L, -1)) {
    v = lua_tointeger(L, -1);
    lv_obj_scroll_to_x(obj, v, anim ? LV_ANIM_ON : LV_ANIM_OFF);
  }
  lua_pop(L, 1);

  return 0;
}

static int luavgl_obj_is_visible(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_is_visible(obj));

  return 1;
}

static int luavgl_obj_add_flag(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_flag_t flag = lua_tointeger(L, 2);
  lv_obj_add_flag(obj, flag);

  return 0;
}

static int luavgl_obj_clear_flag(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_flag_t flag = lua_tointeger(L, 2);
  lv_obj_clear_flag(obj, flag);

  return 0;
}

static int luavgl_obj_add_state(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_state_t state = lua_tointeger(L, 2);
  lv_obj_add_state(obj, state);
  return 0;
}

static int luavgl_obj_clear_state(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_state_t state = lua_tointeger(L, 2);
  lv_obj_clear_state(obj, state);
  return 0;
}

/**
 * obj:scroll_by(x, y, anim_en)
 */
static int luavgl_obj_scroll_by(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int x = luavgl_tointeger(L, 2);
  int y = luavgl_tointeger(L, 3);
  int anim_en = luavgl_tointeger(L, 4);

  lv_obj_scroll_by(obj, x, y, anim_en);
  return 0;
}

static int luavgl_obj_scroll_by_bounded(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int dx = luavgl_tointeger(L, 2);
  int dy = luavgl_tointeger(L, 3);
  int anim_en = luavgl_tointeger(L, 4);

  lv_obj_scroll_by_bounded(obj, dx, dy, anim_en);
  return 0;
}

static int luavgl_obj_scroll_to_view(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int anim_en = luavgl_tointeger(L, 2);

  lv_obj_scroll_to_view(obj, anim_en);
  return 0;
}

static int luavgl_obj_scroll_to_view_recursive(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int anim_en = luavgl_tointeger(L, 2);

  lv_obj_scroll_to_view_recursive(obj, anim_en);
  return 0;
}

static int luavgl_obj_scroll_by_raw(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int x = luavgl_tointeger(L, 2);
  int y = luavgl_tointeger(L, 3);

  _lv_obj_scroll_by_raw(obj, x, y);
  return 0;
}

static int luavgl_obj_is_scrolling(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_is_scrolling(obj));
  return 1;
}

static int luavgl_obj_scrollbar_invalidate(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_scrollbar_invalidate(obj);
  return 0;
}

static int luavgl_obj_readjust_scroll(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  int anim_en = luavgl_tointeger(L, 2);
  lv_obj_readjust_scroll(obj, anim_en);
  return 0;
}

static int luavgl_obj_is_editable(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_is_editable(obj));
  return 1;
}

static int luavgl_obj_is_group_def(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_is_group_def(obj));
  return 1;
}

static int luavgl_obj_is_layout_positioned(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_pushboolean(L, lv_obj_is_layout_positioned(obj));
  return 1;
}

static int luavgl_obj_mark_layout_as_dirty(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_mark_layout_as_dirty(obj);
  return 0;
}

static int luavgl_obj_center(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_center(obj);
  return 0;
}

static int luavgl_obj_invalidate(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_invalidate(obj);
  return 0;
}

static int luavgl_obj_set_flex_flow(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_flex_flow_t flow = luavgl_tointeger(L, 2);

  lv_obj_set_flex_flow(obj, flow);
  return 0;
}

static int luavgl_obj_set_flex_align(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_flex_align_t m = luavgl_tointeger(L, 2);
  lv_flex_align_t c = luavgl_tointeger(L, 3);
  lv_flex_align_t t = luavgl_tointeger(L, 4);

  lv_obj_set_flex_align(obj, m, c, t);
  return 0;
}

static int luavgl_obj_set_flex_grow(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  uint8_t grow = luavgl_tointeger(L, 2);

  lv_obj_set_flex_grow(obj, grow);
  return 0;
}

static int luavgl_obj_indev_search(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_point_t point;
  if (lua_istable(L, 2)) {
    lua_geti(L, 2, 1);
    point.x = lua_tointeger(L, -1);
    lua_pop(L, 1);

    lua_geti(L, 2, 2);
    point.y = lua_tointeger(L, -1);
    lua_pop(L, 1);
  } else {
    point.x = lua_tointeger(L, 2);
    point.y = lua_tointeger(L, 3);
  }

  obj = lv_indev_search_obj(obj, &point);
  if (obj == NULL) {
    lua_pushnil(L);
  } else {
    lua_pushlightuserdata(L, obj);
    lua_rawget(L, LUA_REGISTRYINDEX);
    if (lua_isnoneornil(L, -1)) {
      luavgl_add_lobj(L, obj)->lua_created = false;
    }
  }

  return 1;
}

static int luavgl_obj_get_coords(lua_State *L)
{
  lv_area_t area;
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_obj_get_coords(obj, &area);
  lua_newtable(L);

  lua_pushinteger(L, area.x1);
  lua_setfield(L, -2, "x1");

  lua_pushinteger(L, area.x2);
  lua_setfield(L, -2, "x2");

  lua_pushinteger(L, area.y1);
  lua_setfield(L, -2, "y1");

  lua_pushinteger(L, area.y2);
  lua_setfield(L, -2, "y2");

  return 1;
}

/**
 * get object real position using lv_obj_get_x/x2/y/y2
 */
static int luavgl_obj_get_pos(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lua_newtable(L);

  lua_pushinteger(L, lv_obj_get_x(obj));
  lua_setfield(L, -2, "x1");

  lua_pushinteger(L, lv_obj_get_x2(obj));
  lua_setfield(L, -2, "x2");

  lua_pushinteger(L, lv_obj_get_y(obj));
  lua_setfield(L, -2, "y1");

  lua_pushinteger(L, lv_obj_get_y2(obj));
  lua_setfield(L, -2, "y2");

  return 1;
}

/**
 * Remove all animations associates to this object
 */
static int luavgl_obj_remove_anim_all(lua_State *L)
{
  lv_obj_t *obj = luavgl_to_obj(L, 1);
  lv_anim_del(obj, NULL);
  return 1;
}

static int luavgl_obj_gc(lua_State *L)
{
  if (lua_type(L, 1) != LUA_TUSERDATA) {
    /* If t = setmetatable({}, obj_meta_table), this will happen when t is
     * gc;ed. Currently all metatables for classes based on obj, that has no own
     * __gc will goes here.
     */
    return 0;
  }

  debug("\n");

  luavgl_obj_t *lobj = lua_touserdata(L, 1);
  if (lobj == NULL || lobj->obj == NULL) {
    /* obj is already deleted. It's ok. */
    return 0;
  }

  debug("GC for obj: %p\n", lobj->obj);
  luavgl_obj_delete(L);

  return 0;
}

static const luaL_Reg luavgl_obj_methods[] = {
    {"set",                      luavgl_obj_set                     },
    {"set_style",                luavgl_obj_set_style               },
    {"align_to",                 luavgl_obj_align_to                },
    {"delete",                   luavgl_obj_delete                  },
    {"clean",                    luavgl_obj_clean                   },

 /* misc. functions */
    {"parent",                   luavgl_obj_set_get_parent          },
    {"set_parent",               luavgl_obj_set_parent              },
    {"get_parent",               luavgl_obj_get_parent              },
    {"get_child",                luavgl_obj_get_child               },
    {"get_child_cnt",            luavgl_obj_get_child_cnt           },
    {"get_screen",               luavgl_obj_get_screen              },
    {"get_state",                luavgl_obj_get_state               },
    {"scroll_to",                luavgl_obj_scroll_to               },
    {"is_scrolling",             luavgl_obj_is_scrolling            },
    {"is_visible",               luavgl_obj_is_visible              },
    {"add_flag",                 luavgl_obj_add_flag                },
    {"clear_flag",               luavgl_obj_clear_flag              },
    {"add_state",                luavgl_obj_add_state               },
    {"clear_state",              luavgl_obj_clear_state             },
    {"add_style",                luavgl_obj_add_style               },
    {"remove_style",             luavgl_obj_remove_style            },
    {"remove_style_all",         luavgl_obj_remove_style_all        },
    {"scroll_by",                luavgl_obj_scroll_by               },
    {"scroll_by_bounded",        luavgl_obj_scroll_by_bounded       },
    {"scroll_to_view",           luavgl_obj_scroll_to_view          },
    {"scroll_to_view_recursive", luavgl_obj_scroll_to_view_recursive},
    {"scroll_by_raw",            luavgl_obj_scroll_by_raw           },
    {"scrollbar_invalidate",     luavgl_obj_scrollbar_invalidate    },
    {"readjust_scroll",          luavgl_obj_readjust_scroll         },
    {"is_editable",              luavgl_obj_is_editable             },
    {"is_group_def",             luavgl_obj_is_group_def            },
    {"is_layout_positioned",     luavgl_obj_is_layout_positioned    },
    {"mark_layout_as_dirty",     luavgl_obj_mark_layout_as_dirty    },
    {"center",                   luavgl_obj_center                  },
    {"invalidate",               luavgl_obj_invalidate              },
    {"set_flex_flow",            luavgl_obj_set_flex_flow           },
    {"set_flex_align",           luavgl_obj_set_flex_align          },
    {"set_flex_grow",            luavgl_obj_set_flex_grow           },
    {"indev_search",             luavgl_obj_indev_search            },
    {"get_coords",               luavgl_obj_get_coords              },
    {"get_pos",                  luavgl_obj_get_pos                 },

    {"onevent",                  luavgl_obj_on_event                },
    {"onPressed",                luavgl_obj_on_pressed              },
    {"onClicked",                luavgl_obj_on_clicked              },
    {"onShortClicked",           luavgl_obj_on_short_clicked        },
    {"anim",                     luavgl_anim_create                 },
    {"Anim",                     luavgl_anim_create                 },
    {"remove_all_anim",          luavgl_obj_remove_anim_all         }, /* remove all */
    {NULL,                       NULL                               },
};

static void luavgl_obj_init(lua_State *L)
{
  /* base lv_obj */
  luavgl_obj_newmetatable(L, &lv_obj_class, "lv_obj", luavgl_obj_methods);
  lua_pushcfunction(L, luavgl_obj_gc);
  lua_setfield(L, -2, "__gc");

  /**
   * Widget creation functions is a metatable, so we can add extended widget to
   * it.
   *
   * widgets = {}
   * lib = {widget_create_methods}
   * widgets.__index = lib
   * obj.__metatable = widgets
   */
  lua_getfield(L, -1, "__index");
  luaL_newmetatable(L, "widgets");
  luaL_newlib(L, widget_create_methods);
  lua_setfield(L, -2, "__index");
  lua_setmetatable(L, -2);
  lua_pop(L, 1); /* remove obj.__index table */

  lua_pop(L, 1); /* remove obj metatable */
}

static const luavgl_value_setter_t obj_property_table[] = {
    {"x",              0,                 {.setter = (setter_int_t)lv_obj_set_x}             },
    {"y",              0,                 {.setter = (setter_int_t)lv_obj_set_y}             },
    {"w",              0,                 {.setter = (setter_int_t)lv_obj_set_width}         },
    {"h",              0,                 {.setter = (setter_int_t)lv_obj_set_height}        },
    {"align",          SETTER_TYPE_STACK, {.setter_stack = _lv_obj_set_align}                },

    {"scrollbar_mode", 0,                 {.setter = (setter_int_t)lv_obj_set_scrollbar_mode}},
    {"scroll_dir",     0,                 {.setter = (setter_int_t)lv_obj_set_scroll_dir}    },
    {"scroll_snap_x",  0,                 {.setter = (setter_int_t)lv_obj_set_scroll_snap_x} },
    {"scroll_snap_y",  0,                 {.setter = (setter_int_t)lv_obj_set_scroll_snap_y} },
};

/**
 * Set object property.
 * Differ from set object style, this one is usually used to set widget
 * property like lv_WIDGETNAME_set_src()
 *
 * Used internally.
 *
 * Expected stack:
 * stack[-2]: key(property name)
 * stack[-1]: value(could be any lua data)
 */
LUALIB_API int luavgl_obj_set_property_kv(lua_State *L, void *data)
{
  lv_obj_t *obj = data;
  int ret = luavgl_set_property(L, obj, obj_property_table);

  if (ret == 0)
    return 0;

  /* fallback to set obj local style, with default state. */
  return luavgl_obj_set_style_kv(L, obj, 0);
}

LUALIB_API int luavgl_obj_create_helper(lua_State *L,
                                        lv_obj_t *(*create)(lv_obj_t *parent))
{
  luavgl_ctx_t *ctx = luavgl_context(L);
  lv_obj_t *parent;

  if (lua_isnoneornil(L, 1)) {
    parent = ctx->root;
  } else {
    parent = luavgl_to_obj(L, 1);
    /* remove parent, in order to keep clean stack to call obj.set */
    lua_remove(L, 1);
  }

  debug("create obj on: %p\n", parent);

  lv_obj_t *obj = create(parent);
  luavgl_add_lobj(L, obj)->lua_created = true;

  if (!lua_istable(L, -2)) {
    /* no need to call setup */
    return 1;
  }

  lua_insert(L, -2); /* obj, prop */

  /* now call obj.set to setup property */
  lua_getfield(L, -2, "set"); /* obj, prop, set */
  if (!luavgl_is_callable(L, -1)) {
    /* set method not found, call basic obj set method. */
    lua_pop(L, 2); /* remove prop table, and set method */
    luavgl_setup_obj(L, obj);
  } else {
    lua_insert(L, -3); /* set, obj, prop */
    lua_call(L, 2, 0); /* now stack is clean */
    lua_pushlightuserdata(L, obj);
    lua_rawget(L, LUA_REGISTRYINDEX);
  }

  debug("create obj: %p\n", obj);
  return 1;
}

/**
 * Add existing lvgl obj to lua, return lobj(luavgl obj).
 * If no metatable not found for this obj class, then lv_obj_class metatable is
 * used
 */
LUALIB_API luavgl_obj_t *luavgl_add_lobj(lua_State *L, lv_obj_t *obj)
{
  luavgl_obj_t *lobj;

  /* In rare case, obj may be deleted but not gc'ed in lua, and lvgl quickly
   * creates another obj but happen to malloced same address, thus using obj
   * light userdata as key may have some potential issues. */
  lua_pushlightuserdata(L, obj);
  lua_rawget(L, LUA_REGISTRYINDEX);
  if (!lua_isnoneornil(L, -1)) {
    /* already exists */
    return luavgl_to_lobj(L, -1);
  }
  lua_pop(L, 1); /* pop nil value */

  lobj = lua_newuserdata(L, sizeof(*lobj));

  if (luavgl_obj_getmetatable(L, obj->class_p) == LUA_TNIL) {
    lua_pop(L, 1);
    debug("cannot find metatable for class: %p\n", obj->class_p);
    /* use base obj metatable instead */
    luavgl_obj_getmetatable(L, &lv_obj_class);
  }

  lua_setmetatable(L, -2);

  memset(lobj, 0, sizeof(*lobj));
  luavgl_obj_event_init(lobj);
  lobj->obj = obj;
  lv_obj_add_event_cb(obj, obj_delete_cb, LV_EVENT_DELETE, L);

  /* registry[obj] = lobj */
  lua_pushlightuserdata(L, obj);
  lua_pushvalue(L, -2);
  lua_rawset(L, LUA_REGISTRYINDEX);
  return lobj;
}