]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Web/ventoy_http.h
Add muli-languages support for Ventoy menu.
[Ventoy.git] / Plugson / src / Web / ventoy_http.h
1 /******************************************************************************
2 * ventoy_http.h
3 *
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 #ifndef __VENTOY_HTTP_H__
21 #define __VENTOY_HTTP_H__
22
23 #include <civetweb.h>
24
25 #define MAX_LANGUAGE 256
26
27 #define L1 " "
28 #define L2 " "
29 #define L3 " "
30 #define L4 " "
31
32 typedef enum bios_mode
33 {
34 bios_common = 0,
35 bios_legacy,
36 bios_uefi,
37 bios_ia32,
38 bios_aa64,
39 bios_mips,
40
41 bios_max
42 }bios_mode;
43
44 typedef enum plugin_type
45 {
46 plugin_type_control = 0,
47 plugin_type_theme,
48 plugin_type_menu_alias,
49 plugin_type_menu_tip,
50 plugin_type_menu_class,
51 plugin_type_auto_install,
52 plugin_type_persistence,
53 plugin_type_injection,
54 plugin_type_conf_replace,
55 plugin_type_password,
56 plugin_type_image_list,
57 plugin_type_auto_memdisk,
58 plugin_type_dud,
59
60 plugin_type_max
61 }plugin_type;
62
63 typedef struct data_control
64 {
65 int default_menu_mode;
66 int treeview_style;
67 int filter_dot_underscore;
68 int sort_casesensitive;
69 int max_search_level;
70 int vhd_no_warning;
71 int filter_iso;
72 int filter_wim;
73 int filter_efi;
74 int filter_img;
75 int filter_vhd;
76 int filter_vtoy;
77 int win11_bypass_check;
78 int menu_timeout;
79 int secondary_menu_timeout;
80 int linux_remount;
81 int secondary_menu;
82 int password_asterisk;
83 char default_search_root[MAX_PATH];
84 char default_image[MAX_PATH];
85 char default_kbd_layout[32];
86 char help_text_language[32];
87 char menu_language[32];
88 }data_control;
89
90 #define display_mode_gui 0
91 #define display_mode_cli 1
92 #define display_mode_serial 2
93 #define display_mode_ser_console 3
94
95 typedef struct path_node
96 {
97 char path[MAX_PATH];
98 struct path_node *next;
99 }path_node;
100
101 typedef struct data_theme
102 {
103 int default_file;
104 path_node *filelist;
105 int display_mode;
106 char gfxmode[32];
107
108 char ventoy_left[32];
109 char ventoy_top[32];
110 char ventoy_color[32];
111 char serial_param[256];
112
113 path_node *fontslist;
114 }data_theme;
115
116 #define path_type_file 0
117 #define path_type_dir 1
118
119 typedef struct data_alias_node
120 {
121 int type;
122 char path[MAX_PATH];
123 char alias[256];
124 struct data_alias_node *next;
125 }data_alias_node;
126
127 typedef struct data_alias
128 {
129 data_alias_node *list;
130 }data_alias;
131
132
133 typedef struct data_tip_node
134 {
135 int type;
136 char path[MAX_PATH];
137 char tip[256];
138 struct data_tip_node *next;
139 }data_tip_node;
140
141 typedef struct data_tip
142 {
143 char left[32];
144 char top[32];
145 char color[32];
146 data_tip_node *list;
147 }data_tip;
148
149
150 #define class_type_key 0
151 #define class_type_dir 1
152 #define class_type_parent 2
153 typedef struct data_class_node
154 {
155 int type;
156 char path[MAX_PATH];
157 char class[256];
158 struct data_class_node *next;
159 }data_class_node;
160
161 typedef struct data_class
162 {
163 data_class_node *list;
164 }data_class;
165
166
167 typedef struct data_auto_memdisk
168 {
169 path_node *list;
170 }data_auto_memdisk;
171
172 typedef struct data_image_list
173 {
174 int type;
175 path_node *list;
176 }data_image_list;
177
178 typedef struct menu_password
179 {
180 int type;
181 char path[MAX_PATH];
182 char pwd[256];
183 struct menu_password *next;
184 }menu_password;
185
186 typedef struct data_password
187 {
188 char bootpwd[256];
189 char isopwd[256];
190 char wimpwd[256];
191 char vhdpwd[256];
192 char imgpwd[256];
193 char efipwd[256];
194 char vtoypwd[256];
195
196 menu_password *list;
197 }data_password;
198
199 typedef struct conf_replace_node
200 {
201 int image;
202 char path[MAX_PATH];
203 char org[256];
204 char new[MAX_PATH];
205 struct conf_replace_node *next;
206 }conf_replace_node;
207 typedef struct data_conf_replace
208 {
209 conf_replace_node *list;
210 }data_conf_replace;
211
212 typedef struct injection_node
213 {
214 int type;
215 char path[MAX_PATH];
216 char archive[MAX_PATH];
217 struct injection_node *next;
218 }injection_node;
219 typedef struct data_injection
220 {
221 injection_node *list;
222 }data_injection;
223
224
225
226 typedef struct dud_node
227 {
228 char path[MAX_PATH];
229 path_node *list;
230
231 struct dud_node *next;
232 }dud_node;
233
234 typedef struct data_dud
235 {
236 dud_node *list;
237 }data_dud;
238
239 typedef struct auto_install_node
240 {
241 int timeouten;
242 int timeout;
243 int autoselen;
244 int autosel;
245 int type;
246 char path[MAX_PATH];
247 path_node *list;
248
249 struct auto_install_node *next;
250 }auto_install_node;
251
252 typedef struct data_auto_install
253 {
254 auto_install_node *list;
255 }data_auto_install;
256
257 typedef struct persistence_node
258 {
259 int timeouten;
260 int timeout;
261 int autoselen;
262 int autosel;
263 int type;
264 char path[MAX_PATH];
265 path_node *list;
266
267 struct persistence_node *next;
268 }persistence_node;
269
270 typedef struct data_persistence
271 {
272 persistence_node *list;
273 }data_persistence;
274
275
276
277
278 #define ventoy_save_plug(plug) \
279 {\
280 for (i = 0; i < bios_max; i++) \
281 {\
282 scnprintf(title, sizeof(title), "%s%s", #plug, g_json_title_postfix[i]);\
283 g_json_exist[plugin_type_##plug][i] = 0;\
284 if (ventoy_data_cmp_##plug(g_data_##plug + i, g_data_##plug + bios_max))\
285 {\
286 g_json_exist[plugin_type_##plug][i] = 1;\
287 pos += ventoy_data_save_##plug(g_data_##plug + i, title, JSON_SAVE_BUFFER + pos, JSON_BUF_MAX - pos);\
288 }\
289 }\
290 }
291
292
293
294 #define api_get_func(conn, json, name) \
295 {\
296 int i = 0; \
297 int pos = 0; \
298 \
299 (void)json;\
300 \
301 VTOY_JSON_FMT_BEGIN(pos, JSON_BUFFER, JSON_BUF_MAX);\
302 VTOY_JSON_FMT_ARY_BEGIN();\
303 \
304 for (i = 0; i <= bios_max; i++)\
305 {\
306 __uiCurPos += ventoy_data_json_##name(g_data_##name + i, JSON_BUFFER + __uiCurPos, JSON_BUF_MAX - __uiCurPos);\
307 VTOY_JSON_FMT_COMA();\
308 }\
309 \
310 VTOY_JSON_FMT_ARY_END();\
311 VTOY_JSON_FMT_END(pos);\
312 \
313 ventoy_json_buffer(conn, JSON_BUFFER, pos);\
314 }
315
316
317 #define vtoy_list_free(type, list) \
318 {\
319 type *__next = NULL;\
320 type *__node = list;\
321 while (__node)\
322 {\
323 __next = __node->next;\
324 free(__node);\
325 __node = __next;\
326 }\
327 (list) = NULL;\
328 }
329
330 #define vtoy_list_del(last, node, LIST, field) \
331 for (last = node = LIST; node; node = node->next) \
332 {\
333 if (strcmp(node->field, field) == 0)\
334 {\
335 if (node == LIST)\
336 {\
337 LIST = LIST->next;\
338 }\
339 else\
340 {\
341 last->next = node->next;\
342 }\
343 free(node);\
344 break;\
345 }\
346 \
347 last = node;\
348 }
349
350
351 #define vtoy_list_del_ex(last, node, LIST, field, cb) \
352 for (last = node = LIST; node; node = node->next) \
353 {\
354 if (strcmp(node->field, field) == 0)\
355 {\
356 if (node == LIST)\
357 {\
358 LIST = LIST->next;\
359 }\
360 else\
361 {\
362 last->next = node->next;\
363 }\
364 cb(node->list);\
365 free(node);\
366 break;\
367 }\
368 \
369 last = node;\
370 }
371
372 #define vtoy_list_add(LIST, cur, node) \
373 if (LIST)\
374 {\
375 cur = LIST;\
376 while (cur && cur->next)\
377 {\
378 cur = cur->next;\
379 }\
380 cur->next = node;\
381 }\
382 else\
383 {\
384 LIST = node;\
385 }
386
387
388
389 #define ventoy_parse_json(name) \
390 {\
391 int __loop;\
392 int __len = strlen(#name);\
393 if (strncmp(#name, node->pcName, __len) == 0)\
394 {\
395 for (__loop = 0; __loop < bios_max; __loop++)\
396 {\
397 if (strcmp(g_json_title_postfix[__loop], node->pcName + __len) == 0)\
398 {\
399 vlog("json parse <%s>\n", node->pcName);\
400 ventoy_parse_##name(node, g_data_##name + __loop);\
401 break;\
402 }\
403 }\
404 } \
405 }
406
407 #define CONTROL_PARSE_INT_DEF_0(node, val) \
408 if (node->unData.pcStrVal[0] == '1') val = 1
409
410 #define CONTROL_PARSE_INT_DEF_1(node, val) \
411 if (node->unData.pcStrVal[0] == '0') val = 0
412
413 #define VTOY_JSON_INT(key, val) vtoy_json_get_int(json, key, &val)
414 #define VTOY_JSON_STR(key, buf) vtoy_json_get_string(json, key, sizeof(buf), buf)
415 #define VTOY_JSON_STR_EX(key) vtoy_json_get_string_ex(json, key)
416
417 typedef int (*ventoy_json_callback)(struct mg_connection *conn, VTOY_JSON *json);
418 typedef struct JSON_CB
419 {
420 const char *method;
421 ventoy_json_callback callback;
422 }JSON_CB;
423
424 int ventoy_http_init(void);
425 void ventoy_http_exit(void);
426 int ventoy_http_start(const char *ip, const char *port);
427 int ventoy_http_stop(void);
428 int ventoy_data_save_all(void);
429 int ventoy_data_real_save_all(int apilock);
430
431 #endif /* __VENTOY_HTTP_H__ */
432