1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include "aos_fs.h"
6 #include "aos_system.h"
7 #include "amp_config.h"
8 #include "amp_defines.h"
9 #include "amp_task.h"
10 #include "board_mgr.h"
11 #include "be_inl.h"
12 #include "cJSON.h"
13 #include "startup/page_entry.h"
14 #include "render.h"
15 
16 
17 #define MOD_STR "APPENTRY"
18 
19 //static dlist_t g_pages_list;
20 
21 
22 #ifdef JSE_ADVANCED_ADDON_UI
23 extern char g_app_path[128];
24 
25 char app_file_path[128];
26 
search_js_page_entry(amp_app_desc_t * app)27 int search_js_page_entry(amp_app_desc_t *app)
28 {
29     int i;
30     int size;
31     cJSON *root     = NULL;
32     cJSON *item     = NULL;
33     cJSON *temp     = NULL;
34     void *json_data = NULL;
35 
36     void * js_app_fd = NULL;
37     int file_len  = 0;
38     int json_fd   = -1;
39 
40     page_desc_t *page;
41 
42     if (app == NULL) {
43         return -1;
44     }
45 
46     snprintf(app_file_path, 128, AMP_APP_MAIN_JSON);
47     /* cannot find the index.js int current dir */
48     if ((json_fd = aos_open(app_file_path, O_RDONLY)) < 0) {
49         amp_error(MOD_STR, "cannot find the file :%s", app_file_path);
50         return -1;
51     }
52 
53     /* read package config file to json_data buffer */
54     file_len = aos_lseek(json_fd, 0, SEEK_END);
55     printf("%s %d\r\n", __func__, file_len);
56 
57     json_data = aos_calloc(1, sizeof(char) * (file_len + 1));
58     if (NULL == json_data) {
59         aos_close(json_fd);
60         json_fd = -1;
61         return -1;
62     }
63     aos_lseek(json_fd, 0, SEEK_SET);
64     aos_read(json_fd, json_data, file_len);
65     aos_close(json_fd);
66 
67     /* parser the package json data */
68     root = cJSON_Parse(json_data);
69     if (NULL == root) {
70         aos_free(json_data);
71         amp_error(MOD_STR, "cJSON_Parse failed");
72         return -1;
73     }
74 
75     item = cJSON_GetObjectItem(root, "pages");
76     if (item == NULL)
77     {
78         return -1;
79     }
80 
81     size = cJSON_GetArraySize(item);
82 
83     app->page = aos_malloc(sizeof(page_desc_t) * size);
84     if (NULL == app->page)
85     {
86         return -1;
87     }
88 
89     memset(app->page, 0, sizeof(page_desc_t) * size);
90     app->num = size;
91     app->cur_page = 0;
92 
93 
94     for (i = 0; i < size; i++) {
95         page = &app->page[i];
96         page->index = i;
97         temp = cJSON_GetArrayItem(item, i);
98         if(NULL == temp )
99         {
100             continue;
101         }
102 
103         //strncpy(page->path, temp->valuestring, 64);
104         //printf("temp->valuestring == %s\n\r",temp->valuestring);
105         snprintf(page->path, 64, "%s", temp->valuestring);
106         snprintf(page->css_file, 128, "%s%s", temp->valuestring, ".css");
107         snprintf(page->xml_file, 128, "%s%s", temp->valuestring, ".xml");
108         snprintf(page->js_file, 128, "%s%s", temp->valuestring, ".js");
109 
110     }
111 
112     aos_free(json_data);
113     cJSON_Delete(root);
114 
115     return 0;
116 }
117 
page_config_parse()118 void page_config_parse()
119 {
120     memset(&g_app, 0, sizeof(g_app));
121 
122     search_js_page_entry(&g_app);
123 }
124 
125 #endif
126 
set_active_page(void)127 void set_active_page(void)
128 {
129 
130 
131 }
132 
133 
page_get_cur_options(void)134 page_options_t* page_get_cur_options(void)
135 {
136     int index;
137 
138     if (g_app.cur_page >=  g_app.num) {
139         return NULL;
140     }
141 
142     index = g_app.cur_page;
143 
144     return &(g_app.page[index].options);
145 }
146 
page_add_options(page_options_t * options)147 static int page_add_options(page_options_t *options)
148 {
149     int index;
150 
151     if (options ==  NULL) {
152         return -1;
153     }
154     if (g_app.cur_page >=  g_app.num) {
155         return -1;
156     }
157 
158     index = g_app.cur_page;
159 
160     g_app.page[index].options.object    = options->object;
161     g_app.page[index].options.data      = options->data;
162     g_app.page[index].options.on_show   = options->on_show;
163     g_app.page[index].options.on_update = options->on_update;
164     g_app.page[index].options.on_exit   = options->on_exit;
165 
166     return 0;
167 }
168 
169 /* App(Object options) entry */
native_page_entry(duk_context * ctx)170 static duk_ret_t native_page_entry(duk_context *ctx)
171 {
172     int i;
173     int ret;
174     page_options_t page_options;
175 
176     /* check paramters */
177     if (!duk_is_object(ctx, 0))
178     {
179         // amp_warn("parameter must be object");
180         duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "parameter must be object");
181         return duk_throw(ctx);
182     }
183 
184     memset(&page_options, 0, sizeof(page_options_t));
185 
186     /* get options object */
187     duk_dup(ctx, -1);
188     page_options.object = be_ref(ctx);
189 
190     /* find data */
191     if (duk_get_prop_string(ctx, 0, "data"))
192     {
193         if (!duk_is_object(ctx, -1))
194         {
195             duk_pop(ctx);
196             duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "data must be function");
197             return duk_throw(ctx);
198         }
199         amp_debug(MOD_STR, "find Page#data");
200         duk_dup(ctx, -1);
201         page_options.data = be_ref(ctx);
202         duk_pop(ctx);
203     }
204 
205     /* find onShow() */
206     if (duk_get_prop_string(ctx, 0, "onShow"))
207     {
208         if (!duk_is_function(ctx, -1))
209         {
210             duk_pop(ctx);
211             duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onShow must be function");
212             return duk_throw(ctx);
213         }
214         amp_debug(MOD_STR, "find Page#onShow()");
215         duk_dup(ctx, -1);
216         page_options.on_show = be_ref(ctx);
217         duk_pop(ctx);
218     }
219 
220     /* find onUpdate() */
221     if (duk_get_prop_string(ctx, 0, "onUpdate"))
222     {
223         if (!duk_is_function(ctx, -1))
224         {
225             duk_pop(ctx);
226             duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onUpdate must be function");
227             return duk_throw(ctx);
228         }
229         amp_debug(MOD_STR, "find Page#onUpdate()");
230         duk_dup(ctx, -1);
231         page_options.on_update = be_ref(ctx);
232         duk_pop(ctx);
233     }
234 
235     /* find onExit() */
236     if (duk_get_prop_string(ctx, 0, "onExit"))
237     {
238         if (!duk_is_function(ctx, -1))
239         {
240             duk_pop(ctx);
241             duk_push_error_object(ctx, DUK_ERR_TYPE_ERROR, "onExit must be function");
242             return duk_throw(ctx);
243         }
244         amp_debug(MOD_STR, "find Page#onExit()");
245         duk_dup(ctx, -1);
246         page_options.on_exit = be_ref(ctx);
247         duk_pop(ctx);
248     }
249 
250     /* one-by-one insert into page list */
251     ret = page_add_options(&page_options);
252 
253     // amp_task_schedule_call(page_entry, NULL);
254     return 1;
255 }
256 
page_entry(void * para)257 void page_entry(void *para)
258 {
259     int i = 0;
260     page_options_t *options;
261     duk_context *ctx = be_get_context();
262 
263     options = page_get_cur_options();
264     if (options == NULL) {
265         return;
266     }
267 
268     /* onShow hook */
269     be_push_ref(ctx, options->on_show);
270 
271     duk_push_object(ctx);
272 
273     if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) {
274         amp_debug("page", "%s : %d---------------------------------", __func__, __LINE__);
275         //amp_console("%s", duk_safe_to_stacktrace(ctx, -1));
276     }
277 
278     duk_pop(ctx);
279     duk_gc(ctx, 0);
280 }
281 
282 
page_exit(void * para)283 void page_exit(void *para)
284 {
285     int i = 0;
286     page_options_t *options;
287     duk_context *ctx = be_get_context();
288 
289 
290     options = page_get_cur_options();
291     if (options == NULL) {
292         amp_debug("page", "%s : %d---------------------------------", __func__, __LINE__);
293         return;
294     }
295 
296     /* onShow hook */
297     be_push_ref(ctx, options->on_exit);
298 
299     duk_push_object(ctx);
300 
301     if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) {
302         amp_debug("page", "%s : %d---------------------------------", __func__, __LINE__);
303         //amp_console("%s", duk_safe_to_stacktrace(ctx, -1));
304     }
305     duk_pop(ctx);
306     duk_gc(ctx, 0);
307 
308     be_unref(ctx, options->object);
309 
310 }
311 
312 
page_update(void * para)313 void page_update(void *para)
314 {
315     int i = 0;
316     page_options_t *options;
317     duk_context *ctx = be_get_context();
318 
319     options = page_get_cur_options();
320     if (options == NULL) {
321         return;
322     }
323 
324     /* onShow hook */
325     be_push_ref(ctx, options->on_update);
326 
327     duk_push_object(ctx);
328 
329     if (duk_pcall(ctx, 1) != DUK_EXEC_SUCCESS) {
330         amp_console("%s", duk_safe_to_stacktrace(ctx, -1));
331     }
332 
333     duk_pop(ctx);
334     duk_gc(ctx, 0);
335     /* onExit hook */
336     // be_push_ref(ctx, app_options.on_exit);
337 
338     // duk_push_object(ctx);
339 
340     // duk_pcall(ctx, 1);
341     // duk_pop(ctx);
342 }
343 
page_entry_register(void)344 void page_entry_register(void)
345 {
346     duk_context *ctx = be_get_context();
347 
348     duk_push_object(ctx);
349 
350     /* Page({}) */
351     duk_push_c_function(ctx, native_page_entry, 1);
352     duk_put_global_string(ctx, "Page");
353 }
354 
355