callback.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. #include <callback/callback.h>
  2. // Below added by Derek Jamison
  3. // FURI_LOG_DEV will log only during app development. Be sure that Settings/System/Log Device is "LPUART"; so we dont use serial port.
  4. #ifdef DEVELOPMENT
  5. #define FURI_LOG_DEV(tag, format, ...) furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__)
  6. #define DEV_CRASH() furi_crash()
  7. #else
  8. #define FURI_LOG_DEV(tag, format, ...)
  9. #define DEV_CRASH()
  10. #endif
  11. static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  12. {
  13. UNUSED(engine);
  14. GameManager *game_manager = context;
  15. game_manager_input_set(game_manager, input);
  16. game_manager_update(game_manager);
  17. game_manager_render(game_manager, canvas);
  18. }
  19. static int32_t game_app(void *p)
  20. {
  21. UNUSED(p);
  22. GameManager *game_manager = game_manager_alloc();
  23. if (!game_manager)
  24. {
  25. FURI_LOG_E("Game", "Failed to allocate game manager");
  26. return -1;
  27. }
  28. GameEngineSettings settings = game_engine_settings_init();
  29. settings.target_fps = game.target_fps;
  30. settings.show_fps = game.show_fps;
  31. settings.always_backlight = game.always_backlight;
  32. settings.frame_callback = frame_cb;
  33. settings.context = game_manager;
  34. GameEngine *engine = game_engine_alloc(settings);
  35. if (!engine)
  36. {
  37. FURI_LOG_E("Game", "Failed to allocate game engine");
  38. game_manager_free(game_manager);
  39. return -1;
  40. }
  41. game_manager_engine_set(game_manager, engine);
  42. void *game_context = NULL;
  43. if (game.context_size > 0)
  44. {
  45. game_context = malloc(game.context_size);
  46. game_manager_game_context_set(game_manager, game_context);
  47. }
  48. game.start(game_manager, game_context);
  49. game_engine_run(engine);
  50. game_engine_free(engine);
  51. game_manager_free(game_manager);
  52. game.stop(game_context);
  53. if (game_context)
  54. {
  55. free(game_context);
  56. }
  57. int32_t entities = entities_get_count();
  58. if (entities != 0)
  59. {
  60. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. static void flip_world_request_error_draw(Canvas *canvas)
  66. {
  67. if (canvas == NULL)
  68. {
  69. FURI_LOG_E(TAG, "flip_world_request_error_draw - canvas is NULL");
  70. DEV_CRASH();
  71. return;
  72. }
  73. if (fhttp.last_response != NULL)
  74. {
  75. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  76. {
  77. canvas_clear(canvas);
  78. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  79. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  80. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  81. }
  82. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  83. {
  84. canvas_clear(canvas);
  85. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  86. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  87. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  88. }
  89. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  90. {
  91. canvas_clear(canvas);
  92. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  93. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  94. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  95. }
  96. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  97. {
  98. canvas_clear(canvas);
  99. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  100. }
  101. else
  102. {
  103. canvas_clear(canvas);
  104. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  105. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  106. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  107. }
  108. }
  109. else
  110. {
  111. canvas_clear(canvas);
  112. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  113. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  114. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  115. }
  116. }
  117. static bool alloc_about_view(void *context);
  118. static bool alloc_text_input_view(void *context, char *title);
  119. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  120. //
  121. static void wifi_settings_item_selected(void *context, uint32_t index);
  122. static void text_updated_ssid(void *context);
  123. static void text_updated_pass(void *context);
  124. //
  125. static void flip_world_game_fps_change(VariableItem *item);
  126. static void game_settings_item_selected(void *context, uint32_t index);
  127. uint32_t callback_to_submenu(void *context)
  128. {
  129. UNUSED(context);
  130. return FlipWorldViewSubmenu;
  131. }
  132. static uint32_t callback_to_wifi_settings(void *context)
  133. {
  134. UNUSED(context);
  135. return FlipWorldViewVariableItemList;
  136. }
  137. static uint32_t callback_to_settings(void *context)
  138. {
  139. UNUSED(context);
  140. return FlipWorldViewSettings;
  141. }
  142. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  143. {
  144. UNUSED(model);
  145. canvas_clear(canvas);
  146. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  147. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  148. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  149. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  150. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  151. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  152. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  153. }
  154. // alloc
  155. static bool alloc_about_view(void *context)
  156. {
  157. FlipWorldApp *app = (FlipWorldApp *)context;
  158. if (!app)
  159. {
  160. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  161. return false;
  162. }
  163. if (!app->view_about)
  164. {
  165. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  166. {
  167. return false;
  168. }
  169. if (!app->view_about)
  170. {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. static bool alloc_text_input_view(void *context, char *title)
  177. {
  178. FlipWorldApp *app = (FlipWorldApp *)context;
  179. if (!app)
  180. {
  181. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  182. return false;
  183. }
  184. if (!title)
  185. {
  186. FURI_LOG_E(TAG, "Title is NULL");
  187. return false;
  188. }
  189. app->text_input_buffer_size = 64;
  190. if (!app->text_input_buffer)
  191. {
  192. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  193. {
  194. return false;
  195. }
  196. }
  197. if (!app->text_input_temp_buffer)
  198. {
  199. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  200. {
  201. return false;
  202. }
  203. }
  204. if (!app->text_input)
  205. {
  206. if (!easy_flipper_set_uart_text_input(
  207. &app->text_input,
  208. FlipWorldViewTextInput,
  209. title,
  210. app->text_input_temp_buffer,
  211. app->text_input_buffer_size,
  212. strcmp(title, "SSID") == 0 ? text_updated_ssid : text_updated_pass,
  213. callback_to_wifi_settings,
  214. &app->view_dispatcher,
  215. app))
  216. {
  217. return false;
  218. }
  219. if (!app->text_input)
  220. {
  221. return false;
  222. }
  223. char ssid[64];
  224. char pass[64];
  225. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  226. {
  227. if (strcmp(title, "SSID") == 0)
  228. {
  229. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  230. }
  231. else
  232. {
  233. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  234. }
  235. }
  236. }
  237. return true;
  238. }
  239. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  240. {
  241. FlipWorldApp *app = (FlipWorldApp *)context;
  242. if (!app)
  243. {
  244. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  245. return false;
  246. }
  247. if (!app->variable_item_list)
  248. {
  249. switch (view_id)
  250. {
  251. case FlipWorldSubmenuIndexWiFiSettings:
  252. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  253. {
  254. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  255. return false;
  256. }
  257. if (!app->variable_item_list)
  258. {
  259. FURI_LOG_E(TAG, "Variable item list is NULL");
  260. return false;
  261. }
  262. if (!app->variable_item_wifi_ssid)
  263. {
  264. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  265. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  266. }
  267. if (!app->variable_item_wifi_pass)
  268. {
  269. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  270. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  271. }
  272. char ssid[64];
  273. char pass[64];
  274. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  275. {
  276. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  277. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  278. save_char("WiFi-SSID", ssid);
  279. save_char("WiFi-Password", pass);
  280. }
  281. break;
  282. case FlipWorldSubmenuIndexGameSettings:
  283. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  284. {
  285. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  286. return false;
  287. }
  288. if (!app->variable_item_list)
  289. {
  290. FURI_LOG_E(TAG, "Variable item list is NULL");
  291. return false;
  292. }
  293. if (!app->variable_item_game_fps)
  294. {
  295. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  296. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  297. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  298. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  299. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  300. }
  301. char _game_fps[8];
  302. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  303. {
  304. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  305. : strcmp(_game_fps, "120") == 0 ? 2
  306. : strcmp(_game_fps, "240") == 0 ? 3
  307. : 0;
  308. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  309. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  310. snprintf(game_fps, 8, "%s", _game_fps);
  311. }
  312. break;
  313. }
  314. }
  315. return true;
  316. }
  317. static bool alloc_submenu_settings(void *context)
  318. {
  319. FlipWorldApp *app = (FlipWorldApp *)context;
  320. if (!app)
  321. {
  322. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  323. return false;
  324. }
  325. if (!app->submenu_settings)
  326. {
  327. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  328. {
  329. return NULL;
  330. }
  331. if (!app->submenu_settings)
  332. {
  333. return false;
  334. }
  335. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  336. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  337. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  338. }
  339. return true;
  340. }
  341. // free
  342. static void free_about_view(void *context)
  343. {
  344. FlipWorldApp *app = (FlipWorldApp *)context;
  345. if (!app)
  346. {
  347. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  348. return;
  349. }
  350. if (app->view_about)
  351. {
  352. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  353. view_free(app->view_about);
  354. app->view_about = NULL;
  355. }
  356. }
  357. static void free_main_view(void *context)
  358. {
  359. FlipWorldApp *app = (FlipWorldApp *)context;
  360. if (!app)
  361. {
  362. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  363. return;
  364. }
  365. if (app->view_main)
  366. {
  367. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  368. view_free(app->view_main);
  369. app->view_main = NULL;
  370. }
  371. }
  372. static void free_text_input_view(void *context)
  373. {
  374. FlipWorldApp *app = (FlipWorldApp *)context;
  375. if (!app)
  376. {
  377. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  378. return;
  379. }
  380. if (app->text_input)
  381. {
  382. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  383. uart_text_input_free(app->text_input);
  384. app->text_input = NULL;
  385. }
  386. if (app->text_input_buffer)
  387. {
  388. free(app->text_input_buffer);
  389. app->text_input_buffer = NULL;
  390. }
  391. if (app->text_input_temp_buffer)
  392. {
  393. free(app->text_input_temp_buffer);
  394. app->text_input_temp_buffer = NULL;
  395. }
  396. }
  397. static void free_variable_item_list(void *context)
  398. {
  399. FlipWorldApp *app = (FlipWorldApp *)context;
  400. if (!app)
  401. {
  402. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  403. return;
  404. }
  405. if (app->variable_item_list)
  406. {
  407. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  408. variable_item_list_free(app->variable_item_list);
  409. app->variable_item_list = NULL;
  410. }
  411. if (app->variable_item_wifi_ssid)
  412. {
  413. free(app->variable_item_wifi_ssid);
  414. app->variable_item_wifi_ssid = NULL;
  415. }
  416. if (app->variable_item_wifi_pass)
  417. {
  418. free(app->variable_item_wifi_pass);
  419. app->variable_item_wifi_pass = NULL;
  420. }
  421. if (app->variable_item_game_fps)
  422. {
  423. free(app->variable_item_game_fps);
  424. app->variable_item_game_fps = NULL;
  425. }
  426. if (app->variable_item_user_username)
  427. {
  428. free(app->variable_item_user_username);
  429. app->variable_item_user_username = NULL;
  430. }
  431. if (app->variable_item_user_password)
  432. {
  433. free(app->variable_item_user_password);
  434. app->variable_item_user_password = NULL;
  435. }
  436. }
  437. static void free_submenu_settings(void *context)
  438. {
  439. FlipWorldApp *app = (FlipWorldApp *)context;
  440. if (!app)
  441. {
  442. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  443. return;
  444. }
  445. if (app->submenu_settings)
  446. {
  447. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  448. submenu_free(app->submenu_settings);
  449. app->submenu_settings = NULL;
  450. }
  451. }
  452. static FuriThreadId thread_id;
  453. static bool game_thread_running = false;
  454. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  455. {
  456. FlipWorldApp *app = (FlipWorldApp *)context;
  457. if (!app)
  458. {
  459. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  460. return;
  461. }
  462. if (should_free_variable_item_list)
  463. {
  464. free_variable_item_list(app);
  465. }
  466. free_about_view(app);
  467. free_main_view(app);
  468. free_text_input_view(app);
  469. if (app->view_main)
  470. {
  471. view_free(app->view_main);
  472. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  473. app->view_main = NULL;
  474. }
  475. // free game thread
  476. if (game_thread_running)
  477. {
  478. game_thread_running = false;
  479. furi_thread_flags_set(thread_id, WorkerEvtStop);
  480. furi_thread_free(thread_id);
  481. }
  482. if (should_free_submenu_settings)
  483. free_submenu_settings(app);
  484. }
  485. void callback_submenu_choices(void *context, uint32_t index)
  486. {
  487. FlipWorldApp *app = (FlipWorldApp *)context;
  488. if (!app)
  489. {
  490. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  491. return;
  492. }
  493. switch (index)
  494. {
  495. case FlipWorldSubmenuIndexRun:
  496. // free game thread
  497. if (game_thread_running)
  498. {
  499. game_thread_running = false;
  500. furi_thread_flags_set(thread_id, WorkerEvtStop);
  501. furi_thread_free(thread_id);
  502. }
  503. free_all_views(app, true, true);
  504. if (!app->view_main)
  505. {
  506. if (!easy_flipper_set_view(&app->view_main, FlipWorldViewMain, NULL, NULL, callback_to_submenu, &app->view_dispatcher, app))
  507. {
  508. return;
  509. }
  510. if (!app->view_main)
  511. {
  512. return;
  513. }
  514. }
  515. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMain);
  516. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app);
  517. if (!thread)
  518. {
  519. FURI_LOG_E(TAG, "Failed to allocate game thread");
  520. return;
  521. }
  522. furi_thread_start(thread);
  523. thread_id = furi_thread_get_id(thread);
  524. game_thread_running = true;
  525. break;
  526. case FlipWorldSubmenuIndexAbout:
  527. free_all_views(app, true, true);
  528. if (!alloc_about_view(app))
  529. {
  530. FURI_LOG_E(TAG, "Failed to allocate about view");
  531. return;
  532. }
  533. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  534. break;
  535. case FlipWorldSubmenuIndexSettings:
  536. free_all_views(app, true, true);
  537. if (!alloc_submenu_settings(app))
  538. {
  539. FURI_LOG_E(TAG, "Failed to allocate settings view");
  540. return;
  541. }
  542. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  543. break;
  544. case FlipWorldSubmenuIndexWiFiSettings:
  545. free_all_views(app, true, false);
  546. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  547. {
  548. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  549. return;
  550. }
  551. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  552. break;
  553. case FlipWorldSubmenuIndexGameSettings:
  554. free_all_views(app, true, false);
  555. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  556. {
  557. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  558. return;
  559. }
  560. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  561. break;
  562. case FlipWorldSubmenuIndexUserSettings:
  563. easy_flipper_dialog("User Settings", "Coming soon...");
  564. break;
  565. default:
  566. break;
  567. }
  568. }
  569. static void text_updated_ssid(void *context)
  570. {
  571. FlipWorldApp *app = (FlipWorldApp *)context;
  572. if (!app)
  573. {
  574. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  575. return;
  576. }
  577. // store the entered text
  578. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  579. // Ensure null-termination
  580. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  581. // save the setting
  582. save_char("WiFi-SSID", app->text_input_buffer);
  583. // update the variable item text
  584. if (app->variable_item_wifi_ssid)
  585. {
  586. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  587. // get value of password
  588. char pass[64];
  589. if (load_char("WiFi-Password", pass, sizeof(pass)))
  590. {
  591. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  592. {
  593. // save the settings
  594. save_settings(app->text_input_buffer, pass);
  595. // initialize the http
  596. if (flipper_http_init(flipper_http_rx_callback, app))
  597. {
  598. // save the wifi if the device is connected
  599. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  600. {
  601. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  602. }
  603. // free the resources
  604. flipper_http_deinit();
  605. }
  606. else
  607. {
  608. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  609. }
  610. }
  611. }
  612. }
  613. // switch to the settings view
  614. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  615. }
  616. static void text_updated_pass(void *context)
  617. {
  618. FlipWorldApp *app = (FlipWorldApp *)context;
  619. if (!app)
  620. {
  621. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  622. return;
  623. }
  624. // store the entered text
  625. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  626. // Ensure null-termination
  627. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  628. // save the setting
  629. save_char("WiFi-Password", app->text_input_buffer);
  630. // update the variable item text
  631. if (app->variable_item_wifi_pass)
  632. {
  633. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  634. }
  635. // get value of ssid
  636. char ssid[64];
  637. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  638. {
  639. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  640. {
  641. // save the settings
  642. save_settings(ssid, app->text_input_buffer);
  643. // initialize the http
  644. if (flipper_http_init(flipper_http_rx_callback, app))
  645. {
  646. // save the wifi if the device is connected
  647. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  648. {
  649. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  650. }
  651. // free the resources
  652. flipper_http_deinit();
  653. }
  654. else
  655. {
  656. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  657. }
  658. }
  659. }
  660. // switch to the settings view
  661. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  662. }
  663. static void wifi_settings_item_selected(void *context, uint32_t index)
  664. {
  665. FlipWorldApp *app = (FlipWorldApp *)context;
  666. if (!app)
  667. {
  668. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  669. return;
  670. }
  671. char ssid[64];
  672. char pass[64];
  673. switch (index)
  674. {
  675. case 0: // Input SSID
  676. free_all_views(app, false, false);
  677. if (!alloc_text_input_view(app, "SSID"))
  678. {
  679. FURI_LOG_E(TAG, "Failed to allocate text input view");
  680. return;
  681. }
  682. // load SSID
  683. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  684. {
  685. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  686. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  687. }
  688. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  689. break;
  690. case 1: // Input Password
  691. free_all_views(app, false, false);
  692. if (!alloc_text_input_view(app, "Password"))
  693. {
  694. FURI_LOG_E(TAG, "Failed to allocate text input view");
  695. return;
  696. }
  697. // load password
  698. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  699. {
  700. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  701. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  702. }
  703. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  704. break;
  705. default:
  706. FURI_LOG_E(TAG, "Unknown configuration item index");
  707. break;
  708. }
  709. }
  710. static void flip_world_game_fps_change(VariableItem *item)
  711. {
  712. uint8_t index = variable_item_get_current_value_index(item);
  713. variable_item_set_current_value_text(item, game_fps_choices[index]);
  714. // save the fps
  715. snprintf(game_fps, 8, "%s", game_fps_choices[index]);
  716. save_char("Game-FPS", game_fps);
  717. }
  718. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  719. {
  720. UNUSED(model);
  721. snprintf(
  722. fhttp.file_path,
  723. sizeof(fhttp.file_path),
  724. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  725. fhttp.save_received_data = true;
  726. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  727. }
  728. static char *flip_world_parse_worlds(DataLoaderModel *model)
  729. {
  730. UNUSED(model);
  731. // load the received data from the saved file
  732. FuriString *world_data = flipper_http_load_from_file(fhttp.file_path);
  733. flipper_http_deinit();
  734. if (!world_data)
  735. {
  736. FURI_LOG_E(TAG, "Failed to load world data");
  737. return "Failed to load world data";
  738. }
  739. // we used 10 since we passed 10 in the request
  740. for (int i = 0; i < 10; i++)
  741. {
  742. char *json = get_json_array_value("worlds", i, furi_string_get_cstr(world_data));
  743. if (!json)
  744. {
  745. FURI_LOG_E(TAG, "Failed to get worlds. Data likely empty");
  746. break;
  747. }
  748. char *world_name = get_json_value("name", json);
  749. if (!world_name)
  750. {
  751. FURI_LOG_E(TAG, "Failed to get world name");
  752. break;
  753. }
  754. if (!save_world(world_name, json))
  755. {
  756. FURI_LOG_E(TAG, "Failed to save world");
  757. }
  758. }
  759. return "World Pack Installed";
  760. }
  761. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  762. {
  763. flip_world_generic_switch_to_view(app, "Downlading Worlds..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  764. }
  765. static void game_settings_item_selected(void *context, uint32_t index)
  766. {
  767. FlipWorldApp *app = (FlipWorldApp *)context;
  768. if (!app)
  769. {
  770. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  771. return;
  772. }
  773. switch (index)
  774. {
  775. case 0: // Game FPS
  776. break; // handled by flip_world_game_fps_change
  777. case 1: // Download Worlds
  778. // TODO: Implement download worlds
  779. if (!flipper_http_init(flipper_http_rx_callback, app))
  780. {
  781. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  782. return;
  783. }
  784. flip_world_switch_to_view_get_worlds(app);
  785. break;
  786. }
  787. }
  788. static void flip_world_widget_set_text(char *message, Widget **widget)
  789. {
  790. if (widget == NULL)
  791. {
  792. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  793. DEV_CRASH();
  794. return;
  795. }
  796. if (message == NULL)
  797. {
  798. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  799. DEV_CRASH();
  800. return;
  801. }
  802. widget_reset(*widget);
  803. uint32_t message_length = strlen(message); // Length of the message
  804. uint32_t i = 0; // Index tracker
  805. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  806. char *formatted_message; // Buffer to hold the final formatted message
  807. // Allocate buffer with double the message length plus one for safety
  808. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  809. {
  810. return;
  811. }
  812. while (i < message_length)
  813. {
  814. uint32_t max_line_length = 31; // Maximum characters per line
  815. uint32_t remaining_length = message_length - i; // Remaining characters
  816. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  817. // Check for newline character within the current segment
  818. uint32_t newline_pos = i;
  819. bool found_newline = false;
  820. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  821. {
  822. if (message[newline_pos] == '\n')
  823. {
  824. found_newline = true;
  825. break;
  826. }
  827. }
  828. if (found_newline)
  829. {
  830. // If newline found, set line_length up to the newline
  831. line_length = newline_pos - i;
  832. }
  833. // Temporary buffer to hold the current line
  834. char line[32];
  835. strncpy(line, message + i, line_length);
  836. line[line_length] = '\0';
  837. // If newline was found, skip it for the next iteration
  838. if (found_newline)
  839. {
  840. i += line_length + 1; // +1 to skip the '\n' character
  841. }
  842. else
  843. {
  844. // Check if the line ends in the middle of a word and adjust accordingly
  845. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  846. {
  847. // Find the last space within the current line to avoid breaking a word
  848. char *last_space = strrchr(line, ' ');
  849. if (last_space != NULL)
  850. {
  851. // Adjust the line_length to avoid cutting the word
  852. line_length = last_space - line;
  853. line[line_length] = '\0'; // Null-terminate at the space
  854. }
  855. }
  856. // Move the index forward by the determined line_length
  857. i += line_length;
  858. // Skip any spaces at the beginning of the next line
  859. while (i < message_length && message[i] == ' ')
  860. {
  861. i++;
  862. }
  863. }
  864. // Manually copy the fixed line into the formatted_message buffer
  865. for (uint32_t j = 0; j < line_length; j++)
  866. {
  867. formatted_message[formatted_index++] = line[j];
  868. }
  869. // Add a newline character for line spacing
  870. formatted_message[formatted_index++] = '\n';
  871. }
  872. // Null-terminate the formatted_message
  873. formatted_message[formatted_index] = '\0';
  874. // Add the formatted message to the widget
  875. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  876. }
  877. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  878. {
  879. if (!canvas || !model)
  880. {
  881. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  882. return;
  883. }
  884. SerialState http_state = fhttp.state;
  885. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  886. DataState data_state = data_loader_model->data_state;
  887. char *title = data_loader_model->title;
  888. canvas_set_font(canvas, FontSecondary);
  889. if (http_state == INACTIVE)
  890. {
  891. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  892. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  893. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  894. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  895. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  896. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  897. return;
  898. }
  899. if (data_state == DataStateError || data_state == DataStateParseError)
  900. {
  901. flip_world_request_error_draw(canvas);
  902. return;
  903. }
  904. canvas_draw_str(canvas, 0, 7, title);
  905. canvas_draw_str(canvas, 0, 17, "Loading...");
  906. if (data_state == DataStateInitial)
  907. {
  908. return;
  909. }
  910. if (http_state == SENDING)
  911. {
  912. canvas_draw_str(canvas, 0, 27, "Fetching...");
  913. return;
  914. }
  915. if (http_state == RECEIVING || data_state == DataStateRequested)
  916. {
  917. canvas_draw_str(canvas, 0, 27, "Receiving...");
  918. return;
  919. }
  920. if (http_state == IDLE && data_state == DataStateReceived)
  921. {
  922. canvas_draw_str(canvas, 0, 27, "Processing...");
  923. return;
  924. }
  925. if (http_state == IDLE && data_state == DataStateParsed)
  926. {
  927. canvas_draw_str(canvas, 0, 27, "Processed...");
  928. return;
  929. }
  930. }
  931. static void flip_world_loader_process_callback(void *context)
  932. {
  933. if (context == NULL)
  934. {
  935. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  936. DEV_CRASH();
  937. return;
  938. }
  939. FlipWorldApp *app = (FlipWorldApp *)context;
  940. View *view = app->view_loader;
  941. DataState current_data_state;
  942. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  943. if (current_data_state == DataStateInitial)
  944. {
  945. with_view_model(
  946. view,
  947. DataLoaderModel * model,
  948. {
  949. model->data_state = DataStateRequested;
  950. DataLoaderFetch fetch = model->fetcher;
  951. if (fetch == NULL)
  952. {
  953. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  954. model->data_state = DataStateError;
  955. return;
  956. }
  957. // Clear any previous responses
  958. strncpy(fhttp.last_response, "", 1);
  959. bool request_status = fetch(model);
  960. if (!request_status)
  961. {
  962. model->data_state = DataStateError;
  963. }
  964. },
  965. true);
  966. }
  967. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  968. {
  969. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  970. {
  971. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  972. {
  973. FURI_LOG_DEV(TAG, "PONG received.");
  974. }
  975. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  976. {
  977. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  978. }
  979. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  980. {
  981. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  982. }
  983. else if (strlen(fhttp.last_response) == 0)
  984. {
  985. // Still waiting on response
  986. }
  987. else
  988. {
  989. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  990. }
  991. }
  992. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  993. {
  994. // continue waiting
  995. }
  996. else if (fhttp.state == INACTIVE)
  997. {
  998. // inactive. try again
  999. }
  1000. else if (fhttp.state == ISSUE)
  1001. {
  1002. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1003. }
  1004. else
  1005. {
  1006. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1007. DEV_CRASH();
  1008. }
  1009. }
  1010. else if (current_data_state == DataStateReceived)
  1011. {
  1012. with_view_model(
  1013. view,
  1014. DataLoaderModel * model,
  1015. {
  1016. char *data_text;
  1017. if (model->parser == NULL)
  1018. {
  1019. data_text = NULL;
  1020. FURI_LOG_DEV(TAG, "Parser is NULL");
  1021. DEV_CRASH();
  1022. }
  1023. else
  1024. {
  1025. data_text = model->parser(model);
  1026. }
  1027. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1028. model->data_text = data_text;
  1029. if (data_text == NULL)
  1030. {
  1031. model->data_state = DataStateParseError;
  1032. }
  1033. else
  1034. {
  1035. model->data_state = DataStateParsed;
  1036. }
  1037. },
  1038. true);
  1039. }
  1040. else if (current_data_state == DataStateParsed)
  1041. {
  1042. with_view_model(
  1043. view,
  1044. DataLoaderModel * model,
  1045. {
  1046. if (++model->request_index < model->request_count)
  1047. {
  1048. model->data_state = DataStateInitial;
  1049. }
  1050. else
  1051. {
  1052. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1053. if (model->data_text != NULL)
  1054. {
  1055. free(model->data_text);
  1056. model->data_text = NULL;
  1057. }
  1058. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1059. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1060. }
  1061. },
  1062. true);
  1063. }
  1064. }
  1065. static void flip_world_loader_timer_callback(void *context)
  1066. {
  1067. if (context == NULL)
  1068. {
  1069. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1070. DEV_CRASH();
  1071. return;
  1072. }
  1073. FlipWorldApp *app = (FlipWorldApp *)context;
  1074. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1075. }
  1076. static void flip_world_loader_on_enter(void *context)
  1077. {
  1078. if (context == NULL)
  1079. {
  1080. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1081. DEV_CRASH();
  1082. return;
  1083. }
  1084. FlipWorldApp *app = (FlipWorldApp *)context;
  1085. View *view = app->view_loader;
  1086. with_view_model(
  1087. view,
  1088. DataLoaderModel * model,
  1089. {
  1090. view_set_previous_callback(view, model->back_callback);
  1091. if (model->timer == NULL)
  1092. {
  1093. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1094. }
  1095. furi_timer_start(model->timer, 250);
  1096. },
  1097. true);
  1098. }
  1099. static void flip_world_loader_on_exit(void *context)
  1100. {
  1101. if (context == NULL)
  1102. {
  1103. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1104. DEV_CRASH();
  1105. return;
  1106. }
  1107. FlipWorldApp *app = (FlipWorldApp *)context;
  1108. View *view = app->view_loader;
  1109. with_view_model(
  1110. view,
  1111. DataLoaderModel * model,
  1112. {
  1113. if (model->timer)
  1114. {
  1115. furi_timer_stop(model->timer);
  1116. }
  1117. },
  1118. false);
  1119. }
  1120. void flip_world_loader_init(View *view)
  1121. {
  1122. if (view == NULL)
  1123. {
  1124. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1125. DEV_CRASH();
  1126. return;
  1127. }
  1128. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1129. view_set_enter_callback(view, flip_world_loader_on_enter);
  1130. view_set_exit_callback(view, flip_world_loader_on_exit);
  1131. }
  1132. void flip_world_loader_free_model(View *view)
  1133. {
  1134. if (view == NULL)
  1135. {
  1136. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1137. DEV_CRASH();
  1138. return;
  1139. }
  1140. with_view_model(
  1141. view,
  1142. DataLoaderModel * model,
  1143. {
  1144. if (model->timer)
  1145. {
  1146. furi_timer_free(model->timer);
  1147. model->timer = NULL;
  1148. }
  1149. if (model->parser_context)
  1150. {
  1151. free(model->parser_context);
  1152. model->parser_context = NULL;
  1153. }
  1154. },
  1155. false);
  1156. }
  1157. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1158. {
  1159. if (context == NULL)
  1160. {
  1161. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1162. DEV_CRASH();
  1163. return false;
  1164. }
  1165. switch (index)
  1166. {
  1167. case FlipWorldCustomEventProcess:
  1168. flip_world_loader_process_callback(context);
  1169. return true;
  1170. default:
  1171. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1172. return false;
  1173. }
  1174. }
  1175. void flip_world_generic_switch_to_view(FlipWorldApp *app, char *title, DataLoaderFetch fetcher, DataLoaderParser parser, size_t request_count, ViewNavigationCallback back, uint32_t view_id)
  1176. {
  1177. if (app == NULL)
  1178. {
  1179. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1180. DEV_CRASH();
  1181. return;
  1182. }
  1183. View *view = app->view_loader;
  1184. if (view == NULL)
  1185. {
  1186. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1187. DEV_CRASH();
  1188. return;
  1189. }
  1190. with_view_model(
  1191. view,
  1192. DataLoaderModel * model,
  1193. {
  1194. model->title = title;
  1195. model->fetcher = fetcher;
  1196. model->parser = parser;
  1197. model->request_index = 0;
  1198. model->request_count = request_count;
  1199. model->back_callback = back;
  1200. model->data_state = DataStateInitial;
  1201. model->data_text = NULL;
  1202. },
  1203. true);
  1204. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1205. }