callback.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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_fps_choices_2[game_fps_index];
  30. settings.show_fps = game.show_fps;
  31. settings.always_backlight = strstr(game_screen_always_on_choices[game_screen_always_on_index], "Yes") != NULL;
  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_wifi_ssid(void *context);
  123. static void text_updated_wifi_pass(void *context);
  124. static void text_updated_username(void *context);
  125. static void text_updated_password(void *context);
  126. //
  127. static void flip_world_game_fps_change(VariableItem *item);
  128. static void game_settings_item_selected(void *context, uint32_t index);
  129. static void user_settings_item_selected(void *context, uint32_t index);
  130. static void flip_world_game_screen_always_on_change(VariableItem *item);
  131. uint32_t callback_to_submenu(void *context)
  132. {
  133. UNUSED(context);
  134. return FlipWorldViewSubmenu;
  135. }
  136. static uint32_t callback_to_wifi_settings(void *context)
  137. {
  138. UNUSED(context);
  139. return FlipWorldViewVariableItemList;
  140. }
  141. static uint32_t callback_to_settings(void *context)
  142. {
  143. UNUSED(context);
  144. return FlipWorldViewSettings;
  145. }
  146. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  147. {
  148. UNUSED(model);
  149. canvas_clear(canvas);
  150. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  151. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  152. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  153. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  154. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  155. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  156. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  157. }
  158. // alloc
  159. static bool alloc_about_view(void *context)
  160. {
  161. FlipWorldApp *app = (FlipWorldApp *)context;
  162. if (!app)
  163. {
  164. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  165. return false;
  166. }
  167. if (!app->view_about)
  168. {
  169. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  170. {
  171. return false;
  172. }
  173. if (!app->view_about)
  174. {
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. static bool alloc_text_input_view(void *context, char *title)
  181. {
  182. FlipWorldApp *app = (FlipWorldApp *)context;
  183. if (!app)
  184. {
  185. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  186. return false;
  187. }
  188. if (!title)
  189. {
  190. FURI_LOG_E(TAG, "Title is NULL");
  191. return false;
  192. }
  193. app->text_input_buffer_size = 64;
  194. if (!app->text_input_buffer)
  195. {
  196. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  197. {
  198. return false;
  199. }
  200. }
  201. if (!app->text_input_temp_buffer)
  202. {
  203. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  204. {
  205. return false;
  206. }
  207. }
  208. if (!app->text_input)
  209. {
  210. if (!easy_flipper_set_uart_text_input(
  211. &app->text_input,
  212. FlipWorldViewTextInput,
  213. title,
  214. app->text_input_temp_buffer,
  215. app->text_input_buffer_size,
  216. strcmp(title, "SSID") == 0 ? text_updated_wifi_ssid : strcmp(title, "Password") == 0 ? text_updated_wifi_pass
  217. : strcmp(title, "Password") == 0 ? text_updated_username
  218. : text_updated_password,
  219. callback_to_wifi_settings,
  220. &app->view_dispatcher,
  221. app))
  222. {
  223. return false;
  224. }
  225. if (!app->text_input)
  226. {
  227. return false;
  228. }
  229. char ssid[64];
  230. char pass[64];
  231. char username[64];
  232. char password[64];
  233. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  234. {
  235. if (strcmp(title, "SSID") == 0)
  236. {
  237. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  238. }
  239. else
  240. {
  241. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  242. }
  243. }
  244. }
  245. return true;
  246. }
  247. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  248. {
  249. FlipWorldApp *app = (FlipWorldApp *)context;
  250. if (!app)
  251. {
  252. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  253. return false;
  254. }
  255. char ssid[64];
  256. char pass[64];
  257. char username[64];
  258. char password[64];
  259. if (!app->variable_item_list)
  260. {
  261. switch (view_id)
  262. {
  263. case FlipWorldSubmenuIndexWiFiSettings:
  264. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  265. {
  266. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  267. return false;
  268. }
  269. if (!app->variable_item_list)
  270. {
  271. FURI_LOG_E(TAG, "Variable item list is NULL");
  272. return false;
  273. }
  274. if (!app->variable_item_wifi_ssid)
  275. {
  276. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  277. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  278. }
  279. if (!app->variable_item_wifi_pass)
  280. {
  281. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  282. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  283. }
  284. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  285. {
  286. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  287. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  288. save_char("WiFi-SSID", ssid);
  289. save_char("WiFi-Password", pass);
  290. save_char("Flip-Social-Username", username);
  291. save_char("Flip-Social-Password", password);
  292. }
  293. break;
  294. case FlipWorldSubmenuIndexGameSettings:
  295. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  296. {
  297. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  298. return false;
  299. }
  300. if (!app->variable_item_list)
  301. {
  302. FURI_LOG_E(TAG, "Variable item list is NULL");
  303. return false;
  304. }
  305. if (!app->variable_item_game_download_world)
  306. {
  307. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  308. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  309. }
  310. if (!app->variable_item_game_fps)
  311. {
  312. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  313. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  314. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  315. }
  316. if (!app->variable_item_game_screen_always_on)
  317. {
  318. app->variable_item_game_screen_always_on = variable_item_list_add(app->variable_item_list, "Keep Screen On?", 2, flip_world_game_screen_always_on_change, NULL);
  319. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  320. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[1]);
  321. }
  322. char _game_fps[8];
  323. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  324. {
  325. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  326. : strcmp(_game_fps, "120") == 0 ? 2
  327. : strcmp(_game_fps, "240") == 0 ? 3
  328. : 0;
  329. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  330. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  331. }
  332. char _game_screen_always_on[8];
  333. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  334. {
  335. int index = strcmp(_game_screen_always_on, "No") == 0 ? 0 : strcmp(_game_screen_always_on, "Yes") == 0 ? 1
  336. : 0;
  337. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[index]);
  338. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  339. }
  340. break;
  341. case FlipWorldSubmenuIndexUserSettings:
  342. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  343. {
  344. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  345. return false;
  346. }
  347. if (!app->variable_item_list)
  348. {
  349. FURI_LOG_E(TAG, "Variable item list is NULL");
  350. return false;
  351. }
  352. // if logged in, show profile info, otherwise show login/register
  353. if (is_logged_in() || is_logged_in_to_flip_social())
  354. {
  355. if (!app->variable_item_user_username)
  356. {
  357. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  358. variable_item_set_current_value_text(app->variable_item_user_username, "");
  359. }
  360. if (!app->variable_item_user_password)
  361. {
  362. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  363. variable_item_set_current_value_text(app->variable_item_user_password, "");
  364. }
  365. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  366. {
  367. variable_item_set_current_value_text(app->variable_item_user_username, username);
  368. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  369. }
  370. }
  371. else
  372. {
  373. if (!app->variable_item_user_username)
  374. {
  375. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  376. variable_item_set_current_value_text(app->variable_item_user_username, "");
  377. }
  378. if (!app->variable_item_user_password)
  379. {
  380. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  381. variable_item_set_current_value_text(app->variable_item_user_password, "");
  382. }
  383. }
  384. break;
  385. }
  386. }
  387. return true;
  388. }
  389. static bool alloc_submenu_settings(void *context)
  390. {
  391. FlipWorldApp *app = (FlipWorldApp *)context;
  392. if (!app)
  393. {
  394. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  395. return false;
  396. }
  397. if (!app->submenu_settings)
  398. {
  399. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  400. {
  401. return NULL;
  402. }
  403. if (!app->submenu_settings)
  404. {
  405. return false;
  406. }
  407. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  408. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  409. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  410. }
  411. return true;
  412. }
  413. // free
  414. static void free_about_view(void *context)
  415. {
  416. FlipWorldApp *app = (FlipWorldApp *)context;
  417. if (!app)
  418. {
  419. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  420. return;
  421. }
  422. if (app->view_about)
  423. {
  424. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  425. view_free(app->view_about);
  426. app->view_about = NULL;
  427. }
  428. }
  429. static void free_main_view(void *context)
  430. {
  431. FlipWorldApp *app = (FlipWorldApp *)context;
  432. if (!app)
  433. {
  434. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  435. return;
  436. }
  437. }
  438. static void free_text_input_view(void *context)
  439. {
  440. FlipWorldApp *app = (FlipWorldApp *)context;
  441. if (!app)
  442. {
  443. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  444. return;
  445. }
  446. if (app->text_input)
  447. {
  448. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  449. uart_text_input_free(app->text_input);
  450. app->text_input = NULL;
  451. }
  452. if (app->text_input_buffer)
  453. {
  454. free(app->text_input_buffer);
  455. app->text_input_buffer = NULL;
  456. }
  457. if (app->text_input_temp_buffer)
  458. {
  459. free(app->text_input_temp_buffer);
  460. app->text_input_temp_buffer = NULL;
  461. }
  462. }
  463. static void free_variable_item_list(void *context)
  464. {
  465. FlipWorldApp *app = (FlipWorldApp *)context;
  466. if (!app)
  467. {
  468. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  469. return;
  470. }
  471. if (app->variable_item_list)
  472. {
  473. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  474. variable_item_list_free(app->variable_item_list);
  475. app->variable_item_list = NULL;
  476. }
  477. if (app->variable_item_wifi_ssid)
  478. {
  479. free(app->variable_item_wifi_ssid);
  480. app->variable_item_wifi_ssid = NULL;
  481. }
  482. if (app->variable_item_wifi_pass)
  483. {
  484. free(app->variable_item_wifi_pass);
  485. app->variable_item_wifi_pass = NULL;
  486. }
  487. if (app->variable_item_game_fps)
  488. {
  489. free(app->variable_item_game_fps);
  490. app->variable_item_game_fps = NULL;
  491. }
  492. if (app->variable_item_game_screen_always_on)
  493. {
  494. free(app->variable_item_game_screen_always_on);
  495. app->variable_item_game_screen_always_on = NULL;
  496. }
  497. if (app->variable_item_game_download_world)
  498. {
  499. free(app->variable_item_game_download_world);
  500. app->variable_item_game_download_world = NULL;
  501. }
  502. if (app->variable_item_user_username)
  503. {
  504. free(app->variable_item_user_username);
  505. app->variable_item_user_username = NULL;
  506. }
  507. if (app->variable_item_user_password)
  508. {
  509. free(app->variable_item_user_password);
  510. app->variable_item_user_password = NULL;
  511. }
  512. }
  513. static void free_submenu_settings(void *context)
  514. {
  515. FlipWorldApp *app = (FlipWorldApp *)context;
  516. if (!app)
  517. {
  518. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  519. return;
  520. }
  521. if (app->submenu_settings)
  522. {
  523. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  524. submenu_free(app->submenu_settings);
  525. app->submenu_settings = NULL;
  526. }
  527. }
  528. static FlipWorldApp *app_instance = NULL;
  529. static FuriThreadId thread_id;
  530. static bool game_thread_running = false;
  531. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  532. {
  533. FlipWorldApp *app = (FlipWorldApp *)context;
  534. if (!app)
  535. {
  536. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  537. return;
  538. }
  539. if (should_free_variable_item_list)
  540. {
  541. free_variable_item_list(app);
  542. }
  543. free_about_view(app);
  544. free_main_view(app);
  545. free_text_input_view(app);
  546. // free game thread
  547. if (game_thread_running)
  548. {
  549. game_thread_running = false;
  550. furi_thread_flags_set(thread_id, WorkerEvtStop);
  551. furi_thread_free(thread_id);
  552. }
  553. if (should_free_submenu_settings)
  554. free_submenu_settings(app);
  555. if (app_instance)
  556. {
  557. free(app_instance);
  558. app_instance = NULL;
  559. }
  560. }
  561. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  562. {
  563. if (model->request_index == 0)
  564. {
  565. // Create the directory for saving worlds
  566. char directory_path[128];
  567. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  568. // Create the directory
  569. Storage *storage = furi_record_open(RECORD_STORAGE);
  570. storage_common_mkdir(storage, directory_path);
  571. // free storage
  572. furi_record_close(RECORD_STORAGE);
  573. snprintf(
  574. fhttp.file_path,
  575. sizeof(fhttp.file_path),
  576. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  577. fhttp.save_received_data = true;
  578. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  579. }
  580. else if (model->request_index == 1)
  581. {
  582. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  583. if (!world_list)
  584. {
  585. FURI_LOG_E(TAG, "Failed to load world list");
  586. return "Failed to load world list";
  587. }
  588. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  589. if (!first_world)
  590. {
  591. FURI_LOG_E(TAG, "Failed to get first world");
  592. return "Failed to get first world";
  593. }
  594. if (world_exists(furi_string_get_cstr(first_world)))
  595. {
  596. furi_string_free(world_list);
  597. furi_string_free(first_world);
  598. FURI_LOG_I(TAG, "World already exists");
  599. fhttp.state = IDLE;
  600. model->data_state = DataStateParsed;
  601. flipper_http_deinit();
  602. // free game thread
  603. if (game_thread_running)
  604. {
  605. game_thread_running = false;
  606. furi_thread_flags_set(thread_id, WorkerEvtStop);
  607. furi_thread_free(thread_id);
  608. }
  609. // free_all_views(app_instance, true, true);
  610. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  611. if (!thread)
  612. {
  613. FURI_LOG_E(TAG, "Failed to allocate game thread");
  614. return "Failed to allocate game thread";
  615. }
  616. furi_thread_start(thread);
  617. thread_id = furi_thread_get_id(thread);
  618. game_thread_running = true;
  619. return true;
  620. }
  621. snprintf(
  622. fhttp.file_path,
  623. sizeof(fhttp.file_path),
  624. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  625. fhttp.save_received_data = true;
  626. char url[128];
  627. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  628. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  629. }
  630. return false;
  631. }
  632. static char *flip_world_parse_world_list(DataLoaderModel *model)
  633. {
  634. if (model->request_index == 0)
  635. {
  636. return "World List Fetched";
  637. }
  638. else if (model->request_index == 1)
  639. {
  640. flipper_http_deinit();
  641. // free game thread
  642. if (game_thread_running)
  643. {
  644. game_thread_running = false;
  645. furi_thread_flags_set(thread_id, WorkerEvtStop);
  646. furi_thread_free(thread_id);
  647. }
  648. // free_all_views(app_instance, true, true);
  649. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  650. if (!thread)
  651. {
  652. FURI_LOG_E(TAG, "Failed to allocate game thread");
  653. return "Failed to allocate game thread";
  654. }
  655. furi_thread_start(thread);
  656. thread_id = furi_thread_get_id(thread);
  657. game_thread_running = true;
  658. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  659. }
  660. return "Unknown error";
  661. }
  662. static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  663. {
  664. flip_world_generic_switch_to_view(app, "Fetching World List..", flip_world_fetch_world_list, flip_world_parse_world_list, 2, callback_to_submenu, FlipWorldViewLoader);
  665. }
  666. static bool flip_social_login_fetch(DataLoaderModel *model)
  667. {
  668. UNUSED(model);
  669. char username[64];
  670. char password[64];
  671. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  672. {
  673. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  674. return false;
  675. }
  676. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  677. {
  678. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  679. return false;
  680. }
  681. char payload[172];
  682. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  683. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  684. }
  685. static char *flip_social_login_parse(DataLoaderModel *model)
  686. {
  687. UNUSED(model);
  688. char returned_message[128];
  689. if (!fhttp.last_response)
  690. {
  691. snprintf(returned_message, sizeof(returned_message), "Failed to login...");
  692. }
  693. else if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  694. {
  695. snprintf(returned_message, sizeof(returned_message), "Login successful!");
  696. flip_world_switch_to_view_get_world_list(app_instance);
  697. }
  698. else if (strstr(fhttp.last_response, "User not found") != NULL)
  699. {
  700. snprintf(returned_message, sizeof(returned_message), "Account not found...");
  701. }
  702. else
  703. {
  704. snprintf(returned_message, sizeof(returned_message), "Failed to login...");
  705. }
  706. flipper_http_deinit();
  707. save_char("is_logged_in", strstr(fhttp.last_response, "[SUCCESS]") != NULL ? "true" : "false");
  708. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  709. return strstr(returned_message, "successful") != NULL ? "Login successful!" : strstr(returned_message, "found") != NULL ? "Account not found..."
  710. : "Failed to login...";
  711. }
  712. static void flip_social_login_switch_to_view(FlipWorldApp *app)
  713. {
  714. flip_world_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, callback_to_settings, FlipWorldViewTextInput);
  715. }
  716. void callback_submenu_choices(void *context, uint32_t index)
  717. {
  718. FlipWorldApp *app = (FlipWorldApp *)context;
  719. if (!app)
  720. {
  721. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  722. return;
  723. }
  724. switch (index)
  725. {
  726. case FlipWorldSubmenuIndexRun:
  727. if (!flipper_http_init(flipper_http_rx_callback, app))
  728. {
  729. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  730. return;
  731. }
  732. app_instance = malloc(sizeof(FlipWorldApp));
  733. if (!app_instance)
  734. {
  735. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  736. return;
  737. }
  738. memcpy(app_instance, app, sizeof(FlipWorldApp));
  739. // check if logged in
  740. if (is_logged_in() || is_logged_in_to_flip_social())
  741. {
  742. flip_world_switch_to_view_get_world_list(app_instance);
  743. }
  744. else
  745. {
  746. flip_social_login_switch_to_view(app_instance);
  747. }
  748. break;
  749. case FlipWorldSubmenuIndexAbout:
  750. free_all_views(app, true, true);
  751. if (!alloc_about_view(app))
  752. {
  753. FURI_LOG_E(TAG, "Failed to allocate about view");
  754. return;
  755. }
  756. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  757. break;
  758. case FlipWorldSubmenuIndexSettings:
  759. free_all_views(app, true, true);
  760. if (!alloc_submenu_settings(app))
  761. {
  762. FURI_LOG_E(TAG, "Failed to allocate settings view");
  763. return;
  764. }
  765. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  766. break;
  767. case FlipWorldSubmenuIndexWiFiSettings:
  768. free_all_views(app, true, false);
  769. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  770. {
  771. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  772. return;
  773. }
  774. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  775. break;
  776. case FlipWorldSubmenuIndexGameSettings:
  777. free_all_views(app, true, false);
  778. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  779. {
  780. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  781. return;
  782. }
  783. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  784. break;
  785. case FlipWorldSubmenuIndexUserSettings:
  786. free_all_views(app, true, false);
  787. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  788. {
  789. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  790. return;
  791. }
  792. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  793. break;
  794. default:
  795. break;
  796. }
  797. }
  798. static void text_updated_wifi_ssid(void *context)
  799. {
  800. FlipWorldApp *app = (FlipWorldApp *)context;
  801. if (!app)
  802. {
  803. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  804. return;
  805. }
  806. // store the entered text
  807. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  808. // Ensure null-termination
  809. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  810. // save the setting
  811. save_char("WiFi-SSID", app->text_input_buffer);
  812. // update the variable item text
  813. if (app->variable_item_wifi_ssid)
  814. {
  815. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  816. // get value of password
  817. char pass[64];
  818. char username[64];
  819. char password[64];
  820. if (load_char("WiFi-Password", pass, sizeof(pass)))
  821. {
  822. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  823. {
  824. // save the settings
  825. load_char("Flip-Social-Username", username, sizeof(username));
  826. load_char("Flip-Social-Password", password, sizeof(password));
  827. save_settings(app->text_input_buffer, pass, username, password);
  828. // initialize the http
  829. if (flipper_http_init(flipper_http_rx_callback, app))
  830. {
  831. // save the wifi if the device is connected
  832. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  833. {
  834. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  835. }
  836. // free the resources
  837. flipper_http_deinit();
  838. }
  839. else
  840. {
  841. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  842. }
  843. }
  844. }
  845. }
  846. // switch to the settings view
  847. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  848. }
  849. static void text_updated_wifi_pass(void *context)
  850. {
  851. FlipWorldApp *app = (FlipWorldApp *)context;
  852. if (!app)
  853. {
  854. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  855. return;
  856. }
  857. // store the entered text
  858. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  859. // Ensure null-termination
  860. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  861. // save the setting
  862. save_char("WiFi-Password", app->text_input_buffer);
  863. // update the variable item text
  864. if (app->variable_item_wifi_pass)
  865. {
  866. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  867. }
  868. // get value of ssid
  869. char ssid[64];
  870. char username[64];
  871. char password[64];
  872. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  873. {
  874. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  875. {
  876. // save the settings
  877. load_char("Flip-Social-Username", username, sizeof(username));
  878. load_char("Flip-Social-Password", password, sizeof(password));
  879. save_settings(ssid, app->text_input_buffer, username, password);
  880. // initialize the http
  881. if (flipper_http_init(flipper_http_rx_callback, app))
  882. {
  883. // save the wifi if the device is connected
  884. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  885. {
  886. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  887. }
  888. // free the resources
  889. flipper_http_deinit();
  890. }
  891. else
  892. {
  893. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  894. }
  895. }
  896. }
  897. // switch to the settings view
  898. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  899. }
  900. static void text_updated_username(void *context)
  901. {
  902. FlipWorldApp *app = (FlipWorldApp *)context;
  903. if (!app)
  904. {
  905. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  906. return;
  907. }
  908. // store the entered text
  909. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  910. // Ensure null-termination
  911. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  912. // save the setting
  913. save_char("Flip-Social-Username", app->text_input_buffer);
  914. // update the variable item text
  915. if (app->variable_item_user_username)
  916. {
  917. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  918. }
  919. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  920. }
  921. static void text_updated_password(void *context)
  922. {
  923. FlipWorldApp *app = (FlipWorldApp *)context;
  924. if (!app)
  925. {
  926. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  927. return;
  928. }
  929. // store the entered text
  930. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  931. // Ensure null-termination
  932. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  933. // save the setting
  934. save_char("Flip-Social-Password", app->text_input_buffer);
  935. // update the variable item text
  936. if (app->variable_item_user_password)
  937. {
  938. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  939. }
  940. // get value of username
  941. char username[64];
  942. char ssid[64];
  943. char pass[64];
  944. if (load_char("Flip-Social-Username", username, sizeof(username)))
  945. {
  946. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  947. {
  948. // save the settings
  949. load_char("WiFi-SSID", ssid, sizeof(ssid));
  950. load_char("WiFi-Password", pass, sizeof(pass));
  951. save_settings(ssid, pass, username, app->text_input_buffer);
  952. }
  953. }
  954. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  955. }
  956. static void wifi_settings_item_selected(void *context, uint32_t index)
  957. {
  958. FlipWorldApp *app = (FlipWorldApp *)context;
  959. if (!app)
  960. {
  961. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  962. return;
  963. }
  964. char ssid[64];
  965. char pass[64];
  966. char username[64];
  967. char password[64];
  968. switch (index)
  969. {
  970. case 0: // Input SSID
  971. free_all_views(app, false, false);
  972. if (!alloc_text_input_view(app, "SSID"))
  973. {
  974. FURI_LOG_E(TAG, "Failed to allocate text input view");
  975. return;
  976. }
  977. // load SSID
  978. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  979. {
  980. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  981. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  982. }
  983. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  984. break;
  985. case 1: // Input Password
  986. free_all_views(app, false, false);
  987. if (!alloc_text_input_view(app, "Password"))
  988. {
  989. FURI_LOG_E(TAG, "Failed to allocate text input view");
  990. return;
  991. }
  992. // load password
  993. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  994. {
  995. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  996. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  997. }
  998. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  999. break;
  1000. default:
  1001. FURI_LOG_E(TAG, "Unknown configuration item index");
  1002. break;
  1003. }
  1004. }
  1005. static void flip_world_game_fps_change(VariableItem *item)
  1006. {
  1007. uint8_t index = variable_item_get_current_value_index(item);
  1008. game_fps_index = index;
  1009. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1010. // save the fps
  1011. save_char("Game-FPS", game_fps_choices[index]);
  1012. }
  1013. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1014. {
  1015. uint8_t index = variable_item_get_current_value_index(item);
  1016. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1017. // save the screen always on
  1018. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1019. }
  1020. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1021. {
  1022. UNUSED(model);
  1023. snprintf(
  1024. fhttp.file_path,
  1025. sizeof(fhttp.file_path),
  1026. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  1027. fhttp.save_received_data = true;
  1028. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  1029. }
  1030. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1031. {
  1032. UNUSED(model);
  1033. flipper_http_deinit();
  1034. return "World Pack Installed";
  1035. }
  1036. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1037. {
  1038. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1039. }
  1040. static void game_settings_item_selected(void *context, uint32_t index)
  1041. {
  1042. FlipWorldApp *app = (FlipWorldApp *)context;
  1043. if (!app)
  1044. {
  1045. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1046. return;
  1047. }
  1048. switch (index)
  1049. {
  1050. case 0: // Download Worlds
  1051. if (!flipper_http_init(flipper_http_rx_callback, app))
  1052. {
  1053. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1054. return;
  1055. }
  1056. flip_world_switch_to_view_get_worlds(app);
  1057. case 1: // Change FPS
  1058. break;
  1059. case 2: // Screen Always On
  1060. break;
  1061. }
  1062. }
  1063. static void user_settings_item_selected(void *context, uint32_t index)
  1064. {
  1065. FlipWorldApp *app = (FlipWorldApp *)context;
  1066. if (!app)
  1067. {
  1068. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1069. return;
  1070. }
  1071. switch (index)
  1072. {
  1073. case 0: // Username
  1074. free_all_views(app, false, false);
  1075. if (!alloc_text_input_view(app, "Username"))
  1076. {
  1077. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1078. return;
  1079. }
  1080. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1081. break;
  1082. case 1: // Password
  1083. free_all_views(app, false, false);
  1084. if (!alloc_text_input_view(app, "Password"))
  1085. {
  1086. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1087. return;
  1088. }
  1089. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1090. break;
  1091. }
  1092. }
  1093. static void flip_world_widget_set_text(char *message, Widget **widget)
  1094. {
  1095. if (widget == NULL)
  1096. {
  1097. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1098. DEV_CRASH();
  1099. return;
  1100. }
  1101. if (message == NULL)
  1102. {
  1103. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1104. DEV_CRASH();
  1105. return;
  1106. }
  1107. widget_reset(*widget);
  1108. uint32_t message_length = strlen(message); // Length of the message
  1109. uint32_t i = 0; // Index tracker
  1110. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1111. char *formatted_message; // Buffer to hold the final formatted message
  1112. // Allocate buffer with double the message length plus one for safety
  1113. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1114. {
  1115. return;
  1116. }
  1117. while (i < message_length)
  1118. {
  1119. uint32_t max_line_length = 31; // Maximum characters per line
  1120. uint32_t remaining_length = message_length - i; // Remaining characters
  1121. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1122. // Check for newline character within the current segment
  1123. uint32_t newline_pos = i;
  1124. bool found_newline = false;
  1125. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1126. {
  1127. if (message[newline_pos] == '\n')
  1128. {
  1129. found_newline = true;
  1130. break;
  1131. }
  1132. }
  1133. if (found_newline)
  1134. {
  1135. // If newline found, set line_length up to the newline
  1136. line_length = newline_pos - i;
  1137. }
  1138. // Temporary buffer to hold the current line
  1139. char line[32];
  1140. strncpy(line, message + i, line_length);
  1141. line[line_length] = '\0';
  1142. // If newline was found, skip it for the next iteration
  1143. if (found_newline)
  1144. {
  1145. i += line_length + 1; // +1 to skip the '\n' character
  1146. }
  1147. else
  1148. {
  1149. // Check if the line ends in the middle of a word and adjust accordingly
  1150. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1151. {
  1152. // Find the last space within the current line to avoid breaking a word
  1153. char *last_space = strrchr(line, ' ');
  1154. if (last_space != NULL)
  1155. {
  1156. // Adjust the line_length to avoid cutting the word
  1157. line_length = last_space - line;
  1158. line[line_length] = '\0'; // Null-terminate at the space
  1159. }
  1160. }
  1161. // Move the index forward by the determined line_length
  1162. i += line_length;
  1163. // Skip any spaces at the beginning of the next line
  1164. while (i < message_length && message[i] == ' ')
  1165. {
  1166. i++;
  1167. }
  1168. }
  1169. // Manually copy the fixed line into the formatted_message buffer
  1170. for (uint32_t j = 0; j < line_length; j++)
  1171. {
  1172. formatted_message[formatted_index++] = line[j];
  1173. }
  1174. // Add a newline character for line spacing
  1175. formatted_message[formatted_index++] = '\n';
  1176. }
  1177. // Null-terminate the formatted_message
  1178. formatted_message[formatted_index] = '\0';
  1179. // Add the formatted message to the widget
  1180. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1181. }
  1182. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1183. {
  1184. if (!canvas || !model)
  1185. {
  1186. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1187. return;
  1188. }
  1189. SerialState http_state = fhttp.state;
  1190. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1191. DataState data_state = data_loader_model->data_state;
  1192. char *title = data_loader_model->title;
  1193. canvas_set_font(canvas, FontSecondary);
  1194. if (http_state == INACTIVE)
  1195. {
  1196. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1197. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1198. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1199. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1200. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1201. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1202. return;
  1203. }
  1204. if (data_state == DataStateError || data_state == DataStateParseError)
  1205. {
  1206. flip_world_request_error_draw(canvas);
  1207. return;
  1208. }
  1209. canvas_draw_str(canvas, 0, 7, title);
  1210. canvas_draw_str(canvas, 0, 17, "Loading...");
  1211. if (data_state == DataStateInitial)
  1212. {
  1213. return;
  1214. }
  1215. if (http_state == SENDING)
  1216. {
  1217. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1218. return;
  1219. }
  1220. if (http_state == RECEIVING || data_state == DataStateRequested)
  1221. {
  1222. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1223. return;
  1224. }
  1225. if (http_state == IDLE && data_state == DataStateReceived)
  1226. {
  1227. canvas_draw_str(canvas, 0, 27, "Processing...");
  1228. return;
  1229. }
  1230. if (http_state == IDLE && data_state == DataStateParsed)
  1231. {
  1232. canvas_draw_str(canvas, 0, 27, "Processed...");
  1233. return;
  1234. }
  1235. }
  1236. static void flip_world_loader_process_callback(void *context)
  1237. {
  1238. if (context == NULL)
  1239. {
  1240. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1241. DEV_CRASH();
  1242. return;
  1243. }
  1244. FlipWorldApp *app = (FlipWorldApp *)context;
  1245. View *view = app->view_loader;
  1246. DataState current_data_state;
  1247. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1248. if (current_data_state == DataStateInitial)
  1249. {
  1250. with_view_model(
  1251. view,
  1252. DataLoaderModel * model,
  1253. {
  1254. model->data_state = DataStateRequested;
  1255. DataLoaderFetch fetch = model->fetcher;
  1256. if (fetch == NULL)
  1257. {
  1258. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1259. model->data_state = DataStateError;
  1260. return;
  1261. }
  1262. // Clear any previous responses
  1263. strncpy(fhttp.last_response, "", 1);
  1264. bool request_status = fetch(model);
  1265. if (!request_status)
  1266. {
  1267. model->data_state = DataStateError;
  1268. }
  1269. },
  1270. true);
  1271. }
  1272. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1273. {
  1274. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1275. {
  1276. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1277. {
  1278. FURI_LOG_DEV(TAG, "PONG received.");
  1279. }
  1280. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1281. {
  1282. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1283. }
  1284. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1285. {
  1286. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1287. }
  1288. else if (strlen(fhttp.last_response) == 0)
  1289. {
  1290. // Still waiting on response
  1291. }
  1292. else
  1293. {
  1294. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1295. }
  1296. }
  1297. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1298. {
  1299. // continue waiting
  1300. }
  1301. else if (fhttp.state == INACTIVE)
  1302. {
  1303. // inactive. try again
  1304. }
  1305. else if (fhttp.state == ISSUE)
  1306. {
  1307. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1308. }
  1309. else
  1310. {
  1311. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1312. DEV_CRASH();
  1313. }
  1314. }
  1315. else if (current_data_state == DataStateReceived)
  1316. {
  1317. with_view_model(
  1318. view,
  1319. DataLoaderModel * model,
  1320. {
  1321. char *data_text;
  1322. if (model->parser == NULL)
  1323. {
  1324. data_text = NULL;
  1325. FURI_LOG_DEV(TAG, "Parser is NULL");
  1326. DEV_CRASH();
  1327. }
  1328. else
  1329. {
  1330. data_text = model->parser(model);
  1331. }
  1332. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1333. model->data_text = data_text;
  1334. if (data_text == NULL)
  1335. {
  1336. model->data_state = DataStateParseError;
  1337. }
  1338. else
  1339. {
  1340. model->data_state = DataStateParsed;
  1341. }
  1342. },
  1343. true);
  1344. }
  1345. else if (current_data_state == DataStateParsed)
  1346. {
  1347. with_view_model(
  1348. view,
  1349. DataLoaderModel * model,
  1350. {
  1351. if (++model->request_index < model->request_count)
  1352. {
  1353. model->data_state = DataStateInitial;
  1354. }
  1355. else
  1356. {
  1357. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1358. if (model->data_text != NULL)
  1359. {
  1360. free(model->data_text);
  1361. model->data_text = NULL;
  1362. }
  1363. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1364. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1365. }
  1366. },
  1367. true);
  1368. }
  1369. }
  1370. static void flip_world_loader_timer_callback(void *context)
  1371. {
  1372. if (context == NULL)
  1373. {
  1374. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1375. DEV_CRASH();
  1376. return;
  1377. }
  1378. FlipWorldApp *app = (FlipWorldApp *)context;
  1379. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1380. }
  1381. static void flip_world_loader_on_enter(void *context)
  1382. {
  1383. if (context == NULL)
  1384. {
  1385. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1386. DEV_CRASH();
  1387. return;
  1388. }
  1389. FlipWorldApp *app = (FlipWorldApp *)context;
  1390. View *view = app->view_loader;
  1391. with_view_model(
  1392. view,
  1393. DataLoaderModel * model,
  1394. {
  1395. view_set_previous_callback(view, model->back_callback);
  1396. if (model->timer == NULL)
  1397. {
  1398. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1399. }
  1400. furi_timer_start(model->timer, 250);
  1401. },
  1402. true);
  1403. }
  1404. static void flip_world_loader_on_exit(void *context)
  1405. {
  1406. if (context == NULL)
  1407. {
  1408. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1409. DEV_CRASH();
  1410. return;
  1411. }
  1412. FlipWorldApp *app = (FlipWorldApp *)context;
  1413. View *view = app->view_loader;
  1414. with_view_model(
  1415. view,
  1416. DataLoaderModel * model,
  1417. {
  1418. if (model->timer)
  1419. {
  1420. furi_timer_stop(model->timer);
  1421. }
  1422. },
  1423. false);
  1424. }
  1425. void flip_world_loader_init(View *view)
  1426. {
  1427. if (view == NULL)
  1428. {
  1429. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1430. DEV_CRASH();
  1431. return;
  1432. }
  1433. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1434. view_set_enter_callback(view, flip_world_loader_on_enter);
  1435. view_set_exit_callback(view, flip_world_loader_on_exit);
  1436. }
  1437. void flip_world_loader_free_model(View *view)
  1438. {
  1439. if (view == NULL)
  1440. {
  1441. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1442. DEV_CRASH();
  1443. return;
  1444. }
  1445. with_view_model(
  1446. view,
  1447. DataLoaderModel * model,
  1448. {
  1449. if (model->timer)
  1450. {
  1451. furi_timer_free(model->timer);
  1452. model->timer = NULL;
  1453. }
  1454. if (model->parser_context)
  1455. {
  1456. free(model->parser_context);
  1457. model->parser_context = NULL;
  1458. }
  1459. },
  1460. false);
  1461. }
  1462. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1463. {
  1464. if (context == NULL)
  1465. {
  1466. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1467. DEV_CRASH();
  1468. return false;
  1469. }
  1470. switch (index)
  1471. {
  1472. case FlipWorldCustomEventProcess:
  1473. flip_world_loader_process_callback(context);
  1474. return true;
  1475. default:
  1476. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1477. return false;
  1478. }
  1479. }
  1480. 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)
  1481. {
  1482. if (app == NULL)
  1483. {
  1484. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1485. DEV_CRASH();
  1486. return;
  1487. }
  1488. View *view = app->view_loader;
  1489. if (view == NULL)
  1490. {
  1491. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1492. DEV_CRASH();
  1493. return;
  1494. }
  1495. with_view_model(
  1496. view,
  1497. DataLoaderModel * model,
  1498. {
  1499. model->title = title;
  1500. model->fetcher = fetcher;
  1501. model->parser = parser;
  1502. model->request_index = 0;
  1503. model->request_count = request_count;
  1504. model->back_callback = back;
  1505. model->data_state = DataStateInitial;
  1506. model->data_text = NULL;
  1507. },
  1508. true);
  1509. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1510. }