luat_airui_lvgl_screen.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "luat_base.h"
  2. // #include "luat_lvgl.h"
  3. #include "luat_airui.h"
  4. #include "luat_mem.h"
  5. #include "math.h"
  6. #include <stdlib.h>
  7. #define LUAT_LOG_TAG "airui"
  8. #include "luat_log.h"
  9. int top_screen_layout(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos);
  10. int top_screen_layout_blocks(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos);
  11. int top_screen_layout_block(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos, lv_obj_t *scr);
  12. int top_screens_one(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos) {
  13. LLOGD("parse screen");
  14. if (tok[pos].type != JSMN_OBJECT) {
  15. LLOGD("screen must be object");
  16. return -1;
  17. }
  18. int name_pos = jsmn_find_by_key(ctx->data, "name", tok, pos);
  19. if (name_pos < 1) {
  20. LLOGD("screen without name, skip");
  21. return 0;
  22. }
  23. c_str_t name = {.len = 0};
  24. jsmn_get_string(ctx->data, tok, name_pos + 1, &name);
  25. if (name.len < 1) {
  26. LLOGD("screen name is emtry, skip");
  27. return 0;
  28. }
  29. if (ctx->screen_name != NULL) {
  30. if (name.len != strlen(ctx->screen_name)) {
  31. LLOGD("skip screen, name not match");
  32. return 0;
  33. }
  34. if (memcmp(ctx->screen_name, name.ptr, name.len)) {
  35. LLOGD("skip screen, name not match");
  36. return 0;
  37. }
  38. }
  39. //LLOGD("load screen name %.*s", name.len, name);
  40. int layout_pos = jsmn_find_by_key(ctx->data, "layout", tok, pos);
  41. if (layout_pos < 1) {
  42. LLOGD("screen without layout, skip");
  43. return 0;
  44. }
  45. top_screen_layout(ctx, tok, layout_pos);
  46. return 0;
  47. }
  48. int top_screen_layout(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos) {
  49. LLOGD("screen layout");
  50. pos ++; // 跳到值
  51. if (tok[pos].type != JSMN_OBJECT) {
  52. LLOGD("layout must be map, skip");
  53. return 0;
  54. }
  55. int blocks_pos = jsmn_find_by_key(ctx->data, "blocks", tok, pos);
  56. if (blocks_pos < 1) {
  57. LLOGD("layout without blocks, skip");
  58. return 0;
  59. }
  60. top_screen_layout_blocks(ctx, tok, blocks_pos);
  61. return 0;
  62. }
  63. int top_screen_layout_blocks(luat_airui_ctx_t* ctx, jsmntok_t *tok, int pos) {
  64. LLOGD("screen.layer.blocks");
  65. // 移动到值
  66. pos ++;
  67. if (tok[pos].type != JSMN_ARRAY) {
  68. LLOGD("layout.blocks must be array, skip");
  69. return 0;
  70. }
  71. if (tok[pos].size < 1) {
  72. LLOGD("layout.blocks is emtry, skip");
  73. return 0;
  74. }
  75. int block_count = tok[pos].size;
  76. LLOGD("layout.blocks count %d", block_count);
  77. // 移动到blocks[0]
  78. pos ++;
  79. lv_obj_t *scr = lv_obj_create(NULL, NULL);
  80. for (size_t i = 0; i < block_count; i++)
  81. {
  82. top_screen_layout_block(ctx, tok, pos, scr);
  83. jsmn_skip_object(tok, &pos);
  84. }
  85. ctx->scr = scr;
  86. return 0;
  87. }