1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <pthread.h>
19 #include <sys/mount.h>
20 #include "hi_gv.h"
21 #include "higv_cextfile.h"
22 #include "hi_gv_parser.h"
23 #include "higv_mw_media.h"
24 #include "sample_utils.h"
25 #include "sdk.h"
26
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif
31 #endif /* __cplusplus */
32
33 #define PROJECT_ID LABEL_WIN // Window Id
34 #define HIGV_CONTROL_PAGENUM 10 // 10 the maximum number of characters to read at one time
35
36 static HIGV_HANDLE g_sysFontHandle = INVALID_HANDLE;
37 static HIGV_HANDLE g_layer = INVALID_HANDLE;
38 static HI_S32 g_appHandle = 0;
39
40 #ifdef __HuaweiLite__
41 static const HI_CHAR* MOUNT_POINT = "/home";
42 static const HI_CHAR* SDCARD_DEV = "/dev/mmcblk0p0";
43 #endif
44
WidgetAppCreateSysFont(HIGV_HANDLE * fontHandle)45 static HI_S32 WidgetAppCreateSysFont(HIGV_HANDLE* fontHandle)
46 {
47 HI_S32 ret;
48 HI_RESID sbFont, mbFont;
49 HIGV_HANDLE fontHandleTmp;
50 HIGV_FONT_S fontInfo = {0};
51 ret = HI_GV_Res_CreateID(SBFONT_FILE, HIGV_RESTYPE_FONT, &sbFont);
52 if (ret != HI_SUCCESS) {
53 return ret;
54 }
55
56 ret = HI_GV_Res_CreateID(MBFONT_FILE, HIGV_RESTYPE_FONT, &mbFont);
57 if (ret != HI_SUCCESS) {
58 HI_GV_Res_DestroyID(sbFont);
59 return ret;
60 }
61
62 fontInfo.MbFontID = mbFont;
63 fontInfo.SbFontID = sbFont;
64 fontInfo.Size = 20; // 20 is font size
65 fontInfo.bBold = HI_FALSE;
66 fontInfo.bItalic = HI_FALSE;
67 fontInfo.lineSpace = 0;
68 ret = HI_GV_Font_Create((const HIGV_FONT_S*)&fontInfo, &fontHandleTmp);
69 if (ret != HI_SUCCESS) {
70 HI_GV_Res_DestroyID(sbFont);
71 HI_GV_Res_DestroyID(mbFont);
72 return ret;
73 }
74
75 *fontHandle = fontHandleTmp;
76 return HI_SUCCESS;
77 }
78
InitDepend(HI_VOID)79 static HI_S32 InitDepend(HI_VOID)
80 {
81 HI_S32 ret;
82
83 ret = HI_MW_DISP_Open();
84 HIGV_CHECK("HI_MW_DISP_Open", ret);
85
86 ret = HI_GV_Init();
87 if (ret != HI_SUCCESS) {
88 printf("HI_GV_Init failed! Return: %d\n", ret);
89 return ret;
90 }
91
92 ret = HI_GV_PARSER_Init();
93 if (ret != HI_SUCCESS) {
94 HI_GV_Deinit();
95 printf("HI_GV_PARSER_Init failed! Return: %d\n", ret);
96 return ret;
97 }
98
99 HI_GV_PARSER_SetWidgetEventFunc(g_pfunHIGVAppEventFunc, sizeof(g_pfunHIGVAppEventFunc) / sizeof(HI_CHAR*));
100 #ifdef __HuaweiLite__
101 ret = mount(SDCARD_DEV, MOUNT_POINT, "vfat", 0, 0);
102 if (ret != HI_SUCCESS) {
103 printf("mount failed! ret:%d \n", ret);
104 }
105 #endif
106
107 ret = HI_GV_PARSER_LoadFile(RES_PATH"../higv.bin");
108 if (ret != HI_SUCCESS) {
109 printf("HI_GV_PARSER_LoadFile failed! Return: %x \n", ret);
110 HI_GV_PARSER_Deinit();
111 HI_GV_Deinit();
112 return ret;
113 }
114
115 return HI_SUCCESS;
116 }
117
CreateLayer(HI_VOID)118 static HI_S32 CreateLayer(HI_VOID)
119 {
120 HI_S32 ret;
121
122 HIGO_LAYER_INFO_S LayerInfo = {SCREEN_WIDTH, SCREEN_HEIGHT, 320, 240, SCREEN_WIDTH, SCREEN_HEIGHT,
123 (HIGO_LAYER_FLUSHTYPE_E)(HIGO_LAYER_FLUSH_OVER), HIGO_LAYER_DEFLICKER_AUTO, HIGO_PF_1555, HIGO_LAYER_HD_0};
124
125 ret = HI_GV_Layer_Create(&LayerInfo, &g_layer);
126 if (ret != HI_SUCCESS) {
127 printf("HI_GV_Layer_Create failed! Return: %x \n", ret);
128 return ret;
129 }
130
131 #ifndef __HuaweiLite__
132 ret = HI_GV_Layer_SetRotateMode(g_layer, HIGV_ROTATE_90);
133 if (ret != HI_SUCCESS) {
134 printf("HI_GV_Layer_SetRotateMode failed! Return: %x \n", ret);
135 return ret;
136 }
137 #endif
138
139 return HI_SUCCESS;
140 }
141
RegisterLanuage(HI_VOID)142 static HI_S32 RegisterLanuage(HI_VOID)
143 {
144 HI_S32 ret;
145
146 ret = WidgetAppCreateSysFont(&g_sysFontHandle);
147 if (ret != HI_SUCCESS) {
148 printf("WidgetAppCreateSysFont failed!: %x\n", ret);
149 }
150
151 HI_GV_Lan_Register(RES_PATH"lan/en.lang", g_sysFontHandle, LAN_EN);
152 HI_GV_Lan_Register(RES_PATH"lan/zh.lang", g_sysFontHandle, LAN_ZH);
153 HI_GV_Lan_Change(LAN_EN);
154
155 return HI_SUCCESS;
156 }
157
LoadAndShow(HI_VOID)158 static HI_S32 LoadAndShow(HI_VOID)
159 {
160 HI_S32 ret;
161
162 ret = HI_GV_PARSER_LoadViewById(PROJECT_ID);
163 if (ret != HI_SUCCESS) {
164 printf("HI_GV_PARSER_LoadViewById failed! Return: %x\n", ret);
165 return ret;
166 }
167
168 ret = HI_GV_App_Create("MainApp", (HIGV_HANDLE*)&g_appHandle);
169 if (ret != HI_SUCCESS) {
170 printf("HI_GV_App_Create failed! Return: %d\n", ret);
171 return ret;
172 }
173
174 ret = HI_GV_Widget_Show(PROJECT_ID);
175 if (ret != HI_SUCCESS) {
176 printf("HI_GV_Widget_Show() failed ....: %d \n", ret);
177 return ret;
178 }
179
180 ret = HI_GV_Widget_Active(PROJECT_ID);
181 if (ret != HI_SUCCESS) {
182 printf("HI_GV_Widget_Active() failed ....: %d \n", ret);
183 return ret;
184 }
185
186 return HI_SUCCESS;
187 }
188
START_HIGV_App(const void * arg)189 static void* START_HIGV_App(const void* arg)
190 {
191 HIGV_UNUSED(arg);
192 HI_S32 ret;
193
194 ret = InitDepend();
195 if (ret != HI_SUCCESS) {
196 return NULL;
197 }
198
199 ret = CreateLayer();
200 if (ret != HI_SUCCESS) {
201 return NULL;
202 }
203
204 ret = RegisterLanuage();
205 if (ret != HI_SUCCESS) {
206 goto HANDLE_ERR;
207 }
208
209 ret = LoadAndShow();
210 if (ret != HI_SUCCESS) {
211 goto HANDLE_ERR;
212 }
213
214 HI_MW_DISP_SetDisplayGraphicCSC();
215
216 (HI_VOID)HI_GV_App_Start(g_appHandle);
217
218 (HI_VOID)HI_GV_App_Destroy(g_appHandle);
219 HANDLE_ERR:
220 (HI_VOID)HI_GV_Layer_Destroy(g_layer);
221 (HI_VOID)HI_GV_PARSER_Deinit();
222 (HI_VOID)HI_GV_Deinit();
223 (HI_VOID)HI_MW_DISP_Close();
224
225 return NULL;
226 }
227
228 #ifdef __HuaweiLite__
app_main(HI_S32 argc,HI_CHAR * argv[])229 HI_S32 app_main(HI_S32 argc, HI_CHAR* argv[])
230 #else
231 HI_S32 main(HI_S32 argc, HI_CHAR* argv[])
232 #endif
233 {
234 sdk_init();
235 HIGV_UNUSED(argc);
236 HIGV_UNUSED(argv);
237 HI_CHAR cmd[64]; // 64 is the length of buffer
238 pthread_t threadId = 0;
239 HI_S32 ret = pthread_create(&threadId, NULL, START_HIGV_App, NULL);
240 if (ret != HI_SUCCESS) {
241 printf("pthread_create exec failed \n");
242 }
243 printf("\nInput CMD: 'quit' or 'q' for quit the program!\n");
244 while (fgets(cmd, HIGV_CONTROL_PAGENUM, stdin) != 0) {
245 cmd[10] = '\0'; // 10 is the max length of cmd buffer
246 if (strncmp(cmd, "quit", 4) == 0) { // 4 is the length of cmd string
247 HI_GV_App_Stop(g_appHandle);
248 break;
249 } else if (strncmp(cmd, "q", 1) == 0) {
250 HI_GV_App_Stop(g_appHandle);
251 break;
252 } else {
253 printf("Input CMD: 'quit' or 'q' for quit the program!\n");
254 }
255 }
256 ret = pthread_join(threadId, NULL);
257 if (ret != HI_SUCCESS) {
258 printf("pthread_create exec failed \n");
259 }
260
261 sdk_exit();
262 return HI_SUCCESS;
263 }
264
265 #ifdef __cplusplus
266 #if __cplusplus
267 }
268 #endif
269 #endif /* __cplusplus */
270