callback.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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. // Setup game engine settings...
  29. GameEngineSettings settings = game_engine_settings_init();
  30. settings.target_fps = game_fps_choices_2[game_fps_index];
  31. settings.show_fps = game.show_fps;
  32. settings.always_backlight = strstr(game_screen_always_on_choices[game_screen_always_on_index], "Yes") != NULL;
  33. settings.frame_callback = frame_cb;
  34. settings.context = game_manager;
  35. GameEngine *engine = game_engine_alloc(settings);
  36. if (!engine)
  37. {
  38. FURI_LOG_E("Game", "Failed to allocate game engine");
  39. game_manager_free(game_manager);
  40. return -1;
  41. }
  42. game_manager_engine_set(game_manager, engine);
  43. // Allocate custom game context if needed
  44. void *game_context = NULL;
  45. if (game.context_size > 0)
  46. {
  47. game_context = malloc(game.context_size);
  48. game_manager_game_context_set(game_manager, game_context);
  49. }
  50. // Start the game
  51. game.start(game_manager, game_context);
  52. // 1) Run the engine
  53. game_engine_run(engine);
  54. // 2) Stop the game FIRST, so it can do any internal cleanup
  55. game.stop(game_context);
  56. // 3) Now free the engine
  57. game_engine_free(engine);
  58. // 4) Now free the manager
  59. game_manager_free(game_manager);
  60. // 5) Finally, free your custom context if it was allocated
  61. if (game_context)
  62. {
  63. free(game_context);
  64. }
  65. // 6) Check for leftover entities
  66. int32_t entities = entities_get_count();
  67. if (entities != 0)
  68. {
  69. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static void flip_world_request_error_draw(Canvas *canvas)
  75. {
  76. if (canvas == NULL)
  77. {
  78. FURI_LOG_E(TAG, "flip_world_request_error_draw - canvas is NULL");
  79. DEV_CRASH();
  80. return;
  81. }
  82. if (fhttp.last_response != NULL)
  83. {
  84. if (strstr(fhttp.last_response, "[ERROR] Not connected to Wifi. Failed to reconnect.") != NULL)
  85. {
  86. canvas_clear(canvas);
  87. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  88. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  89. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  90. }
  91. else if (strstr(fhttp.last_response, "[ERROR] Failed to connect to Wifi.") != NULL)
  92. {
  93. canvas_clear(canvas);
  94. canvas_draw_str(canvas, 0, 10, "[ERROR] Not connected to Wifi.");
  95. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  96. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  97. }
  98. else if (strstr(fhttp.last_response, "[ERROR] GET request failed or returned empty data.") != NULL)
  99. {
  100. canvas_clear(canvas);
  101. canvas_draw_str(canvas, 0, 10, "[ERROR] WiFi error.");
  102. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  103. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  104. }
  105. else if (strstr(fhttp.last_response, "[PONG]") != NULL)
  106. {
  107. canvas_clear(canvas);
  108. canvas_draw_str(canvas, 0, 10, "[STATUS]Connecting to AP...");
  109. }
  110. else
  111. {
  112. canvas_clear(canvas);
  113. FURI_LOG_E(TAG, "Received an error: %s", fhttp.last_response);
  114. canvas_draw_str(canvas, 0, 10, "[ERROR] Unusual error...");
  115. canvas_draw_str(canvas, 0, 60, "Press BACK and retry.");
  116. }
  117. }
  118. else
  119. {
  120. canvas_clear(canvas);
  121. canvas_draw_str(canvas, 0, 10, "[ERROR] Unknown error.");
  122. canvas_draw_str(canvas, 0, 50, "Update your WiFi settings.");
  123. canvas_draw_str(canvas, 0, 60, "Press BACK to return.");
  124. }
  125. }
  126. static bool alloc_about_view(void *context);
  127. static bool alloc_text_input_view(void *context, char *title);
  128. static bool alloc_variable_item_list(void *context, uint32_t view_id);
  129. //
  130. static void wifi_settings_item_selected(void *context, uint32_t index);
  131. static void text_updated_wifi_ssid(void *context);
  132. static void text_updated_wifi_pass(void *context);
  133. static void text_updated_username(void *context);
  134. static void text_updated_password(void *context);
  135. //
  136. static void flip_world_game_fps_change(VariableItem *item);
  137. static void game_settings_item_selected(void *context, uint32_t index);
  138. static void user_settings_item_selected(void *context, uint32_t index);
  139. static void flip_world_game_screen_always_on_change(VariableItem *item);
  140. uint32_t callback_to_submenu(void *context)
  141. {
  142. UNUSED(context);
  143. return FlipWorldViewSubmenu;
  144. }
  145. static uint32_t callback_to_wifi_settings(void *context)
  146. {
  147. UNUSED(context);
  148. return FlipWorldViewVariableItemList;
  149. }
  150. static uint32_t callback_to_settings(void *context)
  151. {
  152. UNUSED(context);
  153. return FlipWorldViewSettings;
  154. }
  155. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  156. {
  157. UNUSED(model);
  158. canvas_clear(canvas);
  159. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  160. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  161. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  162. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  163. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  164. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  165. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  166. }
  167. // alloc
  168. static bool alloc_about_view(void *context)
  169. {
  170. FlipWorldApp *app = (FlipWorldApp *)context;
  171. if (!app)
  172. {
  173. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  174. return false;
  175. }
  176. if (!app->view_about)
  177. {
  178. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  179. {
  180. return false;
  181. }
  182. if (!app->view_about)
  183. {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. static bool alloc_text_input_view(void *context, char *title)
  190. {
  191. FlipWorldApp *app = (FlipWorldApp *)context;
  192. if (!app)
  193. {
  194. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  195. return false;
  196. }
  197. if (!title)
  198. {
  199. FURI_LOG_E(TAG, "Title is NULL");
  200. return false;
  201. }
  202. app->text_input_buffer_size = 64;
  203. if (!app->text_input_buffer)
  204. {
  205. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  206. {
  207. return false;
  208. }
  209. }
  210. if (!app->text_input_temp_buffer)
  211. {
  212. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  213. {
  214. return false;
  215. }
  216. }
  217. if (!app->text_input)
  218. {
  219. if (!easy_flipper_set_uart_text_input(
  220. &app->text_input,
  221. FlipWorldViewTextInput,
  222. title,
  223. app->text_input_temp_buffer,
  224. app->text_input_buffer_size,
  225. strcmp(title, "SSID") == 0 ? text_updated_wifi_ssid : strcmp(title, "Password") == 0 ? text_updated_wifi_pass
  226. : strcmp(title, "Username-Login") == 0 ? text_updated_username
  227. : text_updated_password,
  228. callback_to_wifi_settings,
  229. &app->view_dispatcher,
  230. app))
  231. {
  232. return false;
  233. }
  234. if (!app->text_input)
  235. {
  236. return false;
  237. }
  238. char ssid[64];
  239. char pass[64];
  240. char username[64];
  241. char password[64];
  242. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  243. {
  244. if (strcmp(title, "SSID") == 0)
  245. {
  246. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  247. }
  248. else if (strcmp(title, "Password") == 0)
  249. {
  250. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  251. }
  252. else if (strcmp(title, "Username-Login") == 0)
  253. {
  254. strncpy(app->text_input_temp_buffer, username, app->text_input_buffer_size);
  255. }
  256. else if (strcmp(title, "Password-Login") == 0)
  257. {
  258. strncpy(app->text_input_temp_buffer, password, app->text_input_buffer_size);
  259. }
  260. }
  261. }
  262. return true;
  263. }
  264. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  265. {
  266. FlipWorldApp *app = (FlipWorldApp *)context;
  267. if (!app)
  268. {
  269. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  270. return false;
  271. }
  272. char ssid[64];
  273. char pass[64];
  274. char username[64];
  275. char password[64];
  276. if (!app->variable_item_list)
  277. {
  278. switch (view_id)
  279. {
  280. case FlipWorldSubmenuIndexWiFiSettings:
  281. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  282. {
  283. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  284. return false;
  285. }
  286. if (!app->variable_item_list)
  287. {
  288. FURI_LOG_E(TAG, "Variable item list is NULL");
  289. return false;
  290. }
  291. if (!app->variable_item_wifi_ssid)
  292. {
  293. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  294. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  295. }
  296. if (!app->variable_item_wifi_pass)
  297. {
  298. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  299. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  300. }
  301. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  302. {
  303. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  304. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  305. save_char("WiFi-SSID", ssid);
  306. save_char("WiFi-Password", pass);
  307. save_char("Flip-Social-Username", username);
  308. save_char("Flip-Social-Password", password);
  309. }
  310. break;
  311. case FlipWorldSubmenuIndexGameSettings:
  312. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  313. {
  314. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  315. return false;
  316. }
  317. if (!app->variable_item_list)
  318. {
  319. FURI_LOG_E(TAG, "Variable item list is NULL");
  320. return false;
  321. }
  322. if (!app->variable_item_game_download_world)
  323. {
  324. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  325. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  326. }
  327. if (!app->variable_item_game_fps)
  328. {
  329. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  330. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  331. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  332. }
  333. if (!app->variable_item_game_screen_always_on)
  334. {
  335. 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);
  336. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  337. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[1]);
  338. }
  339. char _game_fps[8];
  340. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  341. {
  342. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  343. : strcmp(_game_fps, "120") == 0 ? 2
  344. : strcmp(_game_fps, "240") == 0 ? 3
  345. : 0;
  346. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  347. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  348. }
  349. char _game_screen_always_on[8];
  350. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  351. {
  352. int index = strcmp(_game_screen_always_on, "No") == 0 ? 0 : strcmp(_game_screen_always_on, "Yes") == 0 ? 1
  353. : 0;
  354. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[index]);
  355. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  356. }
  357. break;
  358. case FlipWorldSubmenuIndexUserSettings:
  359. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  360. {
  361. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  362. return false;
  363. }
  364. if (!app->variable_item_list)
  365. {
  366. FURI_LOG_E(TAG, "Variable item list is NULL");
  367. return false;
  368. }
  369. // if logged in, show profile info, otherwise show login/register
  370. if (is_logged_in() || is_logged_in_to_flip_social())
  371. {
  372. if (!app->variable_item_user_username)
  373. {
  374. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  375. variable_item_set_current_value_text(app->variable_item_user_username, "");
  376. }
  377. if (!app->variable_item_user_password)
  378. {
  379. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  380. variable_item_set_current_value_text(app->variable_item_user_password, "");
  381. }
  382. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  383. {
  384. variable_item_set_current_value_text(app->variable_item_user_username, username);
  385. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  386. }
  387. }
  388. else
  389. {
  390. if (!app->variable_item_user_username)
  391. {
  392. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  393. variable_item_set_current_value_text(app->variable_item_user_username, "");
  394. }
  395. if (!app->variable_item_user_password)
  396. {
  397. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  398. variable_item_set_current_value_text(app->variable_item_user_password, "");
  399. }
  400. }
  401. break;
  402. }
  403. }
  404. return true;
  405. }
  406. static bool alloc_submenu_settings(void *context)
  407. {
  408. FlipWorldApp *app = (FlipWorldApp *)context;
  409. if (!app)
  410. {
  411. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  412. return false;
  413. }
  414. if (!app->submenu_settings)
  415. {
  416. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  417. {
  418. return NULL;
  419. }
  420. if (!app->submenu_settings)
  421. {
  422. return false;
  423. }
  424. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  425. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  426. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  427. }
  428. return true;
  429. }
  430. // free
  431. static void free_about_view(void *context)
  432. {
  433. FlipWorldApp *app = (FlipWorldApp *)context;
  434. if (!app)
  435. {
  436. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  437. return;
  438. }
  439. if (app->view_about)
  440. {
  441. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  442. view_free(app->view_about);
  443. app->view_about = NULL;
  444. }
  445. }
  446. static void free_text_input_view(void *context)
  447. {
  448. FlipWorldApp *app = (FlipWorldApp *)context;
  449. if (!app)
  450. {
  451. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  452. return;
  453. }
  454. if (app->text_input)
  455. {
  456. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  457. uart_text_input_free(app->text_input);
  458. app->text_input = NULL;
  459. }
  460. if (app->text_input_buffer)
  461. {
  462. free(app->text_input_buffer);
  463. app->text_input_buffer = NULL;
  464. }
  465. if (app->text_input_temp_buffer)
  466. {
  467. free(app->text_input_temp_buffer);
  468. app->text_input_temp_buffer = NULL;
  469. }
  470. }
  471. static void free_variable_item_list(void *context)
  472. {
  473. FlipWorldApp *app = (FlipWorldApp *)context;
  474. if (!app)
  475. {
  476. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  477. return;
  478. }
  479. if (app->variable_item_list)
  480. {
  481. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  482. variable_item_list_free(app->variable_item_list);
  483. app->variable_item_list = NULL;
  484. }
  485. if (app->variable_item_wifi_ssid)
  486. {
  487. free(app->variable_item_wifi_ssid);
  488. app->variable_item_wifi_ssid = NULL;
  489. }
  490. if (app->variable_item_wifi_pass)
  491. {
  492. free(app->variable_item_wifi_pass);
  493. app->variable_item_wifi_pass = NULL;
  494. }
  495. if (app->variable_item_game_fps)
  496. {
  497. free(app->variable_item_game_fps);
  498. app->variable_item_game_fps = NULL;
  499. }
  500. if (app->variable_item_game_screen_always_on)
  501. {
  502. free(app->variable_item_game_screen_always_on);
  503. app->variable_item_game_screen_always_on = NULL;
  504. }
  505. if (app->variable_item_game_download_world)
  506. {
  507. free(app->variable_item_game_download_world);
  508. app->variable_item_game_download_world = NULL;
  509. }
  510. if (app->variable_item_user_username)
  511. {
  512. free(app->variable_item_user_username);
  513. app->variable_item_user_username = NULL;
  514. }
  515. if (app->variable_item_user_password)
  516. {
  517. free(app->variable_item_user_password);
  518. app->variable_item_user_password = NULL;
  519. }
  520. }
  521. static void free_submenu_settings(void *context)
  522. {
  523. FlipWorldApp *app = (FlipWorldApp *)context;
  524. if (!app)
  525. {
  526. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  527. return;
  528. }
  529. if (app->submenu_settings)
  530. {
  531. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  532. submenu_free(app->submenu_settings);
  533. app->submenu_settings = NULL;
  534. }
  535. }
  536. static FuriThreadId thread_id;
  537. static bool game_thread_running = false;
  538. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  539. {
  540. FlipWorldApp *app = (FlipWorldApp *)context;
  541. if (!app)
  542. {
  543. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  544. return;
  545. }
  546. if (should_free_variable_item_list)
  547. {
  548. free_variable_item_list(app);
  549. }
  550. free_about_view(app);
  551. free_text_input_view(app);
  552. // free game thread
  553. if (game_thread_running)
  554. {
  555. game_thread_running = false;
  556. furi_thread_flags_set(thread_id, WorkerEvtStop);
  557. furi_thread_free(thread_id);
  558. }
  559. if (should_free_submenu_settings)
  560. free_submenu_settings(app);
  561. if (app_instance)
  562. {
  563. free(app_instance);
  564. app_instance = NULL;
  565. }
  566. }
  567. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  568. {
  569. if (model->request_index == 0)
  570. {
  571. // Create the directory for saving worlds
  572. char directory_path[128];
  573. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  574. // Create the directory
  575. Storage *storage = furi_record_open(RECORD_STORAGE);
  576. storage_common_mkdir(storage, directory_path);
  577. // free storage
  578. furi_record_close(RECORD_STORAGE);
  579. snprintf(
  580. fhttp.file_path,
  581. sizeof(fhttp.file_path),
  582. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  583. fhttp.save_received_data = true;
  584. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  585. }
  586. else if (model->request_index == 1)
  587. {
  588. snprintf(
  589. fhttp.file_path,
  590. sizeof(fhttp.file_path),
  591. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  592. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  593. if (!world_list)
  594. {
  595. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  596. FURI_LOG_E(TAG, "Failed to load world list");
  597. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  598. return false;
  599. }
  600. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  601. if (!first_world)
  602. {
  603. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  604. FURI_LOG_E(TAG, "Failed to get first world");
  605. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  606. return false;
  607. }
  608. if (world_exists(furi_string_get_cstr(first_world)))
  609. {
  610. furi_string_free(world_list);
  611. furi_string_free(first_world);
  612. FURI_LOG_I(TAG, "World already exists");
  613. flipper_http_deinit();
  614. // free game thread
  615. if (game_thread_running)
  616. {
  617. game_thread_running = false;
  618. furi_thread_flags_set(thread_id, WorkerEvtStop);
  619. furi_thread_free(thread_id);
  620. }
  621. if (!app_instance)
  622. {
  623. FURI_LOG_E(TAG, "app_instance is NULL");
  624. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  625. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  626. return "app_instance is NULL";
  627. }
  628. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  629. if (!thread)
  630. {
  631. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  632. FURI_LOG_E(TAG, "Failed to allocate game thread");
  633. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  634. return false;
  635. }
  636. furi_thread_start(thread);
  637. thread_id = furi_thread_get_id(thread);
  638. game_thread_running = true;
  639. return true;
  640. }
  641. snprintf(
  642. fhttp.file_path,
  643. sizeof(fhttp.file_path),
  644. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  645. fhttp.save_received_data = true;
  646. char url[128];
  647. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  648. furi_string_free(world_list);
  649. furi_string_free(first_world);
  650. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  651. }
  652. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  653. FURI_LOG_E(TAG, "Unknown request index");
  654. easy_flipper_dialog("Error", "Unknown request index.");
  655. return false;
  656. }
  657. static char *flip_world_parse_world_list(DataLoaderModel *model)
  658. {
  659. if (model->request_index == 0)
  660. {
  661. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  662. }
  663. else if (model->request_index == 1)
  664. {
  665. flipper_http_deinit();
  666. // free game thread
  667. if (game_thread_running)
  668. {
  669. game_thread_running = false;
  670. furi_thread_flags_set(thread_id, WorkerEvtStop);
  671. furi_thread_free(thread_id);
  672. }
  673. if (!app_instance)
  674. {
  675. FURI_LOG_E(TAG, "app_instance is NULL");
  676. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  677. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  678. return "app_instance is NULL";
  679. }
  680. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  681. if (!thread)
  682. {
  683. FURI_LOG_E(TAG, "Failed to allocate game thread");
  684. return "Failed to allocate game thread";
  685. }
  686. furi_thread_start(thread);
  687. thread_id = furi_thread_get_id(thread);
  688. game_thread_running = true;
  689. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  690. }
  691. return "Unknown error";
  692. }
  693. void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  694. {
  695. 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);
  696. }
  697. // combine register, login, and world list fetch into one function to switch to the loader view
  698. static bool flip_world_fetch_game(DataLoaderModel *model)
  699. {
  700. if (model->request_index == 0)
  701. {
  702. // login
  703. char username[64];
  704. char password[64];
  705. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  706. {
  707. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  708. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  709. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  710. flipper_http_deinit();
  711. return false;
  712. }
  713. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  714. {
  715. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  716. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  717. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  718. flipper_http_deinit();
  719. return false;
  720. }
  721. char payload[256];
  722. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  723. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  724. }
  725. else if (model->request_index == 1)
  726. {
  727. // check if login was successful
  728. char is_logged_in[8];
  729. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  730. {
  731. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  732. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  733. flipper_http_deinit();
  734. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  735. return false;
  736. }
  737. if (strcmp(is_logged_in, "false") == 0 && strcmp(model->title, "Registering...") == 0)
  738. {
  739. // register
  740. char username[64];
  741. char password[64];
  742. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  743. {
  744. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  745. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  746. flipper_http_deinit();
  747. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  748. return false;
  749. }
  750. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  751. {
  752. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  753. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  754. flipper_http_deinit();
  755. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  756. return false;
  757. }
  758. char payload[172];
  759. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  760. model->title = "Registering...";
  761. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  762. }
  763. else
  764. {
  765. // Create the directory for saving worlds
  766. char directory_path[128];
  767. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  768. // Create the directory
  769. Storage *storage = furi_record_open(RECORD_STORAGE);
  770. storage_common_mkdir(storage, directory_path);
  771. // free storage
  772. furi_record_close(RECORD_STORAGE);
  773. snprintf(
  774. fhttp.file_path,
  775. sizeof(fhttp.file_path),
  776. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  777. model->title = "Fetching World List..";
  778. fhttp.save_received_data = true;
  779. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  780. }
  781. }
  782. else if (model->request_index == 2)
  783. {
  784. // Create the directory for saving worlds
  785. char directory_path[128];
  786. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  787. // Create the directory
  788. Storage *storage = furi_record_open(RECORD_STORAGE);
  789. storage_common_mkdir(storage, directory_path);
  790. // free storage
  791. furi_record_close(RECORD_STORAGE);
  792. snprintf(
  793. fhttp.file_path,
  794. sizeof(fhttp.file_path),
  795. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  796. model->title = "Fetching World List..";
  797. fhttp.save_received_data = true;
  798. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  799. }
  800. else if (model->request_index == 3)
  801. {
  802. snprintf(
  803. fhttp.file_path,
  804. sizeof(fhttp.file_path),
  805. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  806. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  807. if (!world_list)
  808. {
  809. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  810. FURI_LOG_E(TAG, "Failed to load world list");
  811. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  812. return false;
  813. }
  814. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  815. if (!first_world)
  816. {
  817. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  818. FURI_LOG_E(TAG, "Failed to get first world");
  819. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  820. furi_string_free(world_list);
  821. return false;
  822. }
  823. if (world_exists(furi_string_get_cstr(first_world)))
  824. {
  825. furi_string_free(world_list);
  826. furi_string_free(first_world);
  827. FURI_LOG_I(TAG, "World already exists");
  828. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  829. flipper_http_deinit();
  830. // free game thread
  831. if (game_thread_running)
  832. {
  833. game_thread_running = false;
  834. furi_thread_flags_set(thread_id, WorkerEvtStop);
  835. furi_thread_free(thread_id);
  836. }
  837. if (!app_instance)
  838. {
  839. FURI_LOG_E(TAG, "app_instance is NULL");
  840. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  841. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  842. return "app_instance is NULL";
  843. }
  844. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  845. if (!thread)
  846. {
  847. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  848. FURI_LOG_E(TAG, "Failed to allocate game thread");
  849. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  850. return false;
  851. }
  852. furi_thread_start(thread);
  853. thread_id = furi_thread_get_id(thread);
  854. game_thread_running = true;
  855. return true;
  856. }
  857. snprintf(
  858. fhttp.file_path,
  859. sizeof(fhttp.file_path),
  860. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  861. fhttp.save_received_data = true;
  862. char url[128];
  863. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  864. furi_string_free(world_list);
  865. furi_string_free(first_world);
  866. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  867. }
  868. FURI_LOG_E(TAG, "Unknown request index");
  869. return false;
  870. }
  871. static char *flip_world_parse_game(DataLoaderModel *model)
  872. {
  873. if (model->request_index == 0)
  874. {
  875. if (!fhttp.last_response)
  876. {
  877. save_char("is_logged_in", "false");
  878. flipper_http_deinit();
  879. // Go back to the main menu
  880. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  881. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  882. return "Response is empty...";
  883. }
  884. // Check for successful conditions
  885. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  886. {
  887. save_char("is_logged_in", "true");
  888. model->title = "Login successful!";
  889. model->title = "Fetching World List..";
  890. return "Login successful!";
  891. }
  892. // Check if user not found
  893. if (strstr(fhttp.last_response, "User not found") != NULL)
  894. {
  895. save_char("is_logged_in", "false");
  896. model->title = "Registering...";
  897. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  898. }
  899. // If not success, not found, check length conditions
  900. size_t resp_len = strlen(fhttp.last_response);
  901. if (resp_len == 0 || resp_len > 127)
  902. {
  903. // Empty or too long means failed login
  904. flipper_http_deinit();
  905. save_char("is_logged_in", "false");
  906. // Go back to the main menu
  907. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  908. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  909. return "Failed to login...";
  910. }
  911. // Handle any other unknown response as a failure
  912. flipper_http_deinit();
  913. save_char("is_logged_in", "false");
  914. // Go back to the main menu
  915. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  916. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  917. return "Failed to login...";
  918. }
  919. else if (model->request_index == 1)
  920. {
  921. if (strcmp(model->title, "Registering...") == 0)
  922. {
  923. // check registration response
  924. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  925. {
  926. save_char("is_logged_in", "true");
  927. char username[64];
  928. char password[64];
  929. // load the username and password, then save them
  930. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  931. {
  932. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  933. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  934. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  935. flipper_http_deinit();
  936. return "Failed to load Flip-Social-Username";
  937. }
  938. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  939. {
  940. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  941. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  942. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  943. flipper_http_deinit();
  944. return "Failed to load Flip-Social-Password";
  945. }
  946. // load wifi ssid,pass then save
  947. char ssid[64];
  948. char pass[64];
  949. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  950. {
  951. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  952. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  953. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  954. flipper_http_deinit();
  955. return "Failed to load WiFi-SSID";
  956. }
  957. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  958. {
  959. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  960. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  961. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  962. flipper_http_deinit();
  963. return "Failed to load WiFi-Password";
  964. }
  965. save_settings(ssid, pass, username, password);
  966. model->title = "Fetching World List..";
  967. return "Account created!";
  968. }
  969. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  970. {
  971. flipper_http_deinit();
  972. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  973. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  974. return "Please enter your credentials.";
  975. }
  976. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  977. {
  978. flipper_http_deinit();
  979. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  980. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  981. return "Username already exists.";
  982. }
  983. else
  984. {
  985. flipper_http_deinit();
  986. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  987. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  988. return "Registration failed...";
  989. }
  990. }
  991. else
  992. {
  993. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  994. flipper_http_deinit();
  995. // free game thread
  996. if (game_thread_running)
  997. {
  998. game_thread_running = false;
  999. furi_thread_flags_set(thread_id, WorkerEvtStop);
  1000. furi_thread_free(thread_id);
  1001. }
  1002. if (!app_instance)
  1003. {
  1004. FURI_LOG_E(TAG, "app_instance is NULL");
  1005. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  1006. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1007. return "app_instance is NULL";
  1008. }
  1009. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  1010. if (!thread)
  1011. {
  1012. FURI_LOG_E(TAG, "Failed to allocate game thread");
  1013. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  1014. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1015. return "Failed to allocate game thread";
  1016. }
  1017. furi_thread_start(thread);
  1018. thread_id = furi_thread_get_id(thread);
  1019. game_thread_running = true;
  1020. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1021. }
  1022. }
  1023. else if (model->request_index == 2)
  1024. {
  1025. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1026. }
  1027. else if (model->request_index == 3)
  1028. {
  1029. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  1030. flipper_http_deinit();
  1031. // free game thread
  1032. if (game_thread_running)
  1033. {
  1034. game_thread_running = false;
  1035. furi_thread_flags_set(thread_id, WorkerEvtStop);
  1036. furi_thread_free(thread_id);
  1037. }
  1038. if (!app_instance)
  1039. {
  1040. FURI_LOG_E(TAG, "app_instance is NULL");
  1041. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  1042. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1043. return "app_instance is NULL";
  1044. }
  1045. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  1046. if (!thread)
  1047. {
  1048. FURI_LOG_E(TAG, "Failed to allocate game thread");
  1049. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  1050. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1051. return "Failed to allocate game thread";
  1052. }
  1053. furi_thread_start(thread);
  1054. thread_id = furi_thread_get_id(thread);
  1055. game_thread_running = true;
  1056. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  1057. }
  1058. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  1059. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  1060. return "Unknown error";
  1061. }
  1062. void flip_world_switch_to_view_get_game(FlipWorldApp *app)
  1063. {
  1064. flip_world_generic_switch_to_view(app, "Starting Game..", flip_world_fetch_game, flip_world_parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  1065. }
  1066. void callback_submenu_choices(void *context, uint32_t index)
  1067. {
  1068. FlipWorldApp *app = (FlipWorldApp *)context;
  1069. if (!app)
  1070. {
  1071. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1072. return;
  1073. }
  1074. switch (index)
  1075. {
  1076. case FlipWorldSubmenuIndexRun:
  1077. free_all_views(app, true, true);
  1078. if (!flipper_http_init(flipper_http_rx_callback, app))
  1079. {
  1080. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1081. return;
  1082. }
  1083. app_instance = malloc(sizeof(FlipWorldApp));
  1084. if (!app_instance)
  1085. {
  1086. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  1087. return;
  1088. }
  1089. memcpy(app_instance, app, sizeof(FlipWorldApp));
  1090. // check if logged in
  1091. if (is_logged_in() || is_logged_in_to_flip_social())
  1092. {
  1093. flip_world_switch_to_view_get_world_list(app_instance);
  1094. }
  1095. else
  1096. {
  1097. flip_world_switch_to_view_get_game(app_instance);
  1098. }
  1099. break;
  1100. case FlipWorldSubmenuIndexAbout:
  1101. free_all_views(app, true, true);
  1102. if (!alloc_about_view(app))
  1103. {
  1104. FURI_LOG_E(TAG, "Failed to allocate about view");
  1105. return;
  1106. }
  1107. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  1108. break;
  1109. case FlipWorldSubmenuIndexSettings:
  1110. free_all_views(app, true, true);
  1111. if (!alloc_submenu_settings(app))
  1112. {
  1113. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1114. return;
  1115. }
  1116. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1117. break;
  1118. case FlipWorldSubmenuIndexWiFiSettings:
  1119. free_all_views(app, true, false);
  1120. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1121. {
  1122. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1123. return;
  1124. }
  1125. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1126. break;
  1127. case FlipWorldSubmenuIndexGameSettings:
  1128. free_all_views(app, true, false);
  1129. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1130. {
  1131. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1132. return;
  1133. }
  1134. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1135. break;
  1136. case FlipWorldSubmenuIndexUserSettings:
  1137. free_all_views(app, true, false);
  1138. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1139. {
  1140. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1141. return;
  1142. }
  1143. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1144. break;
  1145. default:
  1146. break;
  1147. }
  1148. }
  1149. static void text_updated_wifi_ssid(void *context)
  1150. {
  1151. FlipWorldApp *app = (FlipWorldApp *)context;
  1152. if (!app)
  1153. {
  1154. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1155. return;
  1156. }
  1157. // store the entered text
  1158. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1159. // Ensure null-termination
  1160. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1161. // save the setting
  1162. save_char("WiFi-SSID", app->text_input_buffer);
  1163. // update the variable item text
  1164. if (app->variable_item_wifi_ssid)
  1165. {
  1166. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1167. // get value of password
  1168. char pass[64];
  1169. char username[64];
  1170. char password[64];
  1171. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1172. {
  1173. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1174. {
  1175. // save the settings
  1176. load_char("Flip-Social-Username", username, sizeof(username));
  1177. load_char("Flip-Social-Password", password, sizeof(password));
  1178. save_settings(app->text_input_buffer, pass, username, password);
  1179. // initialize the http
  1180. if (flipper_http_init(flipper_http_rx_callback, app))
  1181. {
  1182. // save the wifi if the device is connected
  1183. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  1184. {
  1185. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1186. }
  1187. // free the resources
  1188. flipper_http_deinit();
  1189. }
  1190. else
  1191. {
  1192. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1193. }
  1194. }
  1195. }
  1196. }
  1197. // switch to the settings view
  1198. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1199. }
  1200. static void text_updated_wifi_pass(void *context)
  1201. {
  1202. FlipWorldApp *app = (FlipWorldApp *)context;
  1203. if (!app)
  1204. {
  1205. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1206. return;
  1207. }
  1208. // store the entered text
  1209. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1210. // Ensure null-termination
  1211. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1212. // save the setting
  1213. save_char("WiFi-Password", app->text_input_buffer);
  1214. // update the variable item text
  1215. if (app->variable_item_wifi_pass)
  1216. {
  1217. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1218. }
  1219. // get value of ssid
  1220. char ssid[64];
  1221. char username[64];
  1222. char password[64];
  1223. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1224. {
  1225. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1226. {
  1227. // save the settings
  1228. load_char("Flip-Social-Username", username, sizeof(username));
  1229. load_char("Flip-Social-Password", password, sizeof(password));
  1230. save_settings(ssid, app->text_input_buffer, username, password);
  1231. // initialize the http
  1232. if (flipper_http_init(flipper_http_rx_callback, app))
  1233. {
  1234. // save the wifi if the device is connected
  1235. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  1236. {
  1237. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1238. }
  1239. // free the resources
  1240. flipper_http_deinit();
  1241. }
  1242. else
  1243. {
  1244. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1245. }
  1246. }
  1247. }
  1248. // switch to the settings view
  1249. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1250. }
  1251. static void text_updated_username(void *context)
  1252. {
  1253. FlipWorldApp *app = (FlipWorldApp *)context;
  1254. if (!app)
  1255. {
  1256. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1257. return;
  1258. }
  1259. // store the entered text
  1260. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1261. // Ensure null-termination
  1262. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1263. // save the setting
  1264. save_char("Flip-Social-Username", app->text_input_buffer);
  1265. // update the variable item text
  1266. if (app->variable_item_user_username)
  1267. {
  1268. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1269. }
  1270. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1271. }
  1272. static void text_updated_password(void *context)
  1273. {
  1274. FlipWorldApp *app = (FlipWorldApp *)context;
  1275. if (!app)
  1276. {
  1277. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1278. return;
  1279. }
  1280. // store the entered text
  1281. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1282. // Ensure null-termination
  1283. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1284. // save the setting
  1285. save_char("Flip-Social-Password", app->text_input_buffer);
  1286. // update the variable item text
  1287. if (app->variable_item_user_password)
  1288. {
  1289. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1290. }
  1291. // get value of username
  1292. char username[64];
  1293. char ssid[64];
  1294. char pass[64];
  1295. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1296. {
  1297. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1298. {
  1299. // save the settings
  1300. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1301. load_char("WiFi-Password", pass, sizeof(pass));
  1302. save_settings(ssid, pass, username, app->text_input_buffer);
  1303. }
  1304. }
  1305. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1306. }
  1307. static void wifi_settings_item_selected(void *context, uint32_t index)
  1308. {
  1309. FlipWorldApp *app = (FlipWorldApp *)context;
  1310. if (!app)
  1311. {
  1312. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1313. return;
  1314. }
  1315. char ssid[64];
  1316. char pass[64];
  1317. char username[64];
  1318. char password[64];
  1319. switch (index)
  1320. {
  1321. case 0: // Input SSID
  1322. free_all_views(app, false, false);
  1323. if (!alloc_text_input_view(app, "SSID"))
  1324. {
  1325. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1326. return;
  1327. }
  1328. // load SSID
  1329. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1330. {
  1331. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1332. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1333. }
  1334. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1335. break;
  1336. case 1: // Input Password
  1337. free_all_views(app, false, false);
  1338. if (!alloc_text_input_view(app, "Password"))
  1339. {
  1340. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1341. return;
  1342. }
  1343. // load password
  1344. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1345. {
  1346. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1347. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1348. }
  1349. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1350. break;
  1351. default:
  1352. FURI_LOG_E(TAG, "Unknown configuration item index");
  1353. break;
  1354. }
  1355. }
  1356. static void flip_world_game_fps_change(VariableItem *item)
  1357. {
  1358. uint8_t index = variable_item_get_current_value_index(item);
  1359. game_fps_index = index;
  1360. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1361. // save the fps
  1362. save_char("Game-FPS", game_fps_choices[index]);
  1363. }
  1364. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1365. {
  1366. uint8_t index = variable_item_get_current_value_index(item);
  1367. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1368. // save the screen always on
  1369. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1370. }
  1371. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1372. {
  1373. UNUSED(model);
  1374. snprintf(
  1375. fhttp.file_path,
  1376. sizeof(fhttp.file_path),
  1377. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  1378. fhttp.save_received_data = true;
  1379. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  1380. }
  1381. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1382. {
  1383. UNUSED(model);
  1384. flipper_http_deinit();
  1385. return "World Pack Installed";
  1386. }
  1387. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1388. {
  1389. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1390. }
  1391. static void game_settings_item_selected(void *context, uint32_t index)
  1392. {
  1393. FlipWorldApp *app = (FlipWorldApp *)context;
  1394. if (!app)
  1395. {
  1396. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1397. return;
  1398. }
  1399. switch (index)
  1400. {
  1401. case 0: // Download Worlds
  1402. if (!flipper_http_init(flipper_http_rx_callback, app))
  1403. {
  1404. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1405. return;
  1406. }
  1407. flip_world_switch_to_view_get_worlds(app);
  1408. case 1: // Change FPS
  1409. break;
  1410. case 2: // Screen Always On
  1411. break;
  1412. }
  1413. }
  1414. static void user_settings_item_selected(void *context, uint32_t index)
  1415. {
  1416. FlipWorldApp *app = (FlipWorldApp *)context;
  1417. if (!app)
  1418. {
  1419. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1420. return;
  1421. }
  1422. switch (index)
  1423. {
  1424. case 0: // Username
  1425. free_all_views(app, false, false);
  1426. if (!alloc_text_input_view(app, "Username-Login"))
  1427. {
  1428. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1429. return;
  1430. }
  1431. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1432. break;
  1433. case 1: // Password
  1434. free_all_views(app, false, false);
  1435. if (!alloc_text_input_view(app, "Password-Login"))
  1436. {
  1437. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1438. return;
  1439. }
  1440. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1441. break;
  1442. }
  1443. }
  1444. static void flip_world_widget_set_text(char *message, Widget **widget)
  1445. {
  1446. if (widget == NULL)
  1447. {
  1448. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1449. DEV_CRASH();
  1450. return;
  1451. }
  1452. if (message == NULL)
  1453. {
  1454. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1455. DEV_CRASH();
  1456. return;
  1457. }
  1458. widget_reset(*widget);
  1459. uint32_t message_length = strlen(message); // Length of the message
  1460. uint32_t i = 0; // Index tracker
  1461. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1462. char *formatted_message; // Buffer to hold the final formatted message
  1463. // Allocate buffer with double the message length plus one for safety
  1464. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1465. {
  1466. return;
  1467. }
  1468. while (i < message_length)
  1469. {
  1470. uint32_t max_line_length = 31; // Maximum characters per line
  1471. uint32_t remaining_length = message_length - i; // Remaining characters
  1472. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1473. // Check for newline character within the current segment
  1474. uint32_t newline_pos = i;
  1475. bool found_newline = false;
  1476. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1477. {
  1478. if (message[newline_pos] == '\n')
  1479. {
  1480. found_newline = true;
  1481. break;
  1482. }
  1483. }
  1484. if (found_newline)
  1485. {
  1486. // If newline found, set line_length up to the newline
  1487. line_length = newline_pos - i;
  1488. }
  1489. // Temporary buffer to hold the current line
  1490. char line[32];
  1491. strncpy(line, message + i, line_length);
  1492. line[line_length] = '\0';
  1493. // If newline was found, skip it for the next iteration
  1494. if (found_newline)
  1495. {
  1496. i += line_length + 1; // +1 to skip the '\n' character
  1497. }
  1498. else
  1499. {
  1500. // Check if the line ends in the middle of a word and adjust accordingly
  1501. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1502. {
  1503. // Find the last space within the current line to avoid breaking a word
  1504. char *last_space = strrchr(line, ' ');
  1505. if (last_space != NULL)
  1506. {
  1507. // Adjust the line_length to avoid cutting the word
  1508. line_length = last_space - line;
  1509. line[line_length] = '\0'; // Null-terminate at the space
  1510. }
  1511. }
  1512. // Move the index forward by the determined line_length
  1513. i += line_length;
  1514. // Skip any spaces at the beginning of the next line
  1515. while (i < message_length && message[i] == ' ')
  1516. {
  1517. i++;
  1518. }
  1519. }
  1520. // Manually copy the fixed line into the formatted_message buffer
  1521. for (uint32_t j = 0; j < line_length; j++)
  1522. {
  1523. formatted_message[formatted_index++] = line[j];
  1524. }
  1525. // Add a newline character for line spacing
  1526. formatted_message[formatted_index++] = '\n';
  1527. }
  1528. // Null-terminate the formatted_message
  1529. formatted_message[formatted_index] = '\0';
  1530. // Add the formatted message to the widget
  1531. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1532. }
  1533. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1534. {
  1535. if (!canvas || !model)
  1536. {
  1537. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1538. return;
  1539. }
  1540. SerialState http_state = fhttp.state;
  1541. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1542. DataState data_state = data_loader_model->data_state;
  1543. char *title = data_loader_model->title;
  1544. canvas_set_font(canvas, FontSecondary);
  1545. if (http_state == INACTIVE)
  1546. {
  1547. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1548. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1549. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1550. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1551. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1552. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1553. return;
  1554. }
  1555. if (data_state == DataStateError || data_state == DataStateParseError)
  1556. {
  1557. flip_world_request_error_draw(canvas);
  1558. return;
  1559. }
  1560. canvas_draw_str(canvas, 0, 7, title);
  1561. canvas_draw_str(canvas, 0, 17, "Loading...");
  1562. if (data_state == DataStateInitial)
  1563. {
  1564. return;
  1565. }
  1566. if (http_state == SENDING)
  1567. {
  1568. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1569. return;
  1570. }
  1571. if (http_state == RECEIVING || data_state == DataStateRequested)
  1572. {
  1573. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1574. return;
  1575. }
  1576. if (http_state == IDLE && data_state == DataStateReceived)
  1577. {
  1578. canvas_draw_str(canvas, 0, 27, "Processing...");
  1579. return;
  1580. }
  1581. if (http_state == IDLE && data_state == DataStateParsed)
  1582. {
  1583. canvas_draw_str(canvas, 0, 27, "Processed...");
  1584. return;
  1585. }
  1586. }
  1587. static void flip_world_loader_process_callback(void *context)
  1588. {
  1589. if (context == NULL)
  1590. {
  1591. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1592. DEV_CRASH();
  1593. return;
  1594. }
  1595. FlipWorldApp *app = (FlipWorldApp *)context;
  1596. View *view = app->view_loader;
  1597. DataState current_data_state;
  1598. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1599. if (current_data_state == DataStateInitial)
  1600. {
  1601. with_view_model(
  1602. view,
  1603. DataLoaderModel * model,
  1604. {
  1605. model->data_state = DataStateRequested;
  1606. DataLoaderFetch fetch = model->fetcher;
  1607. if (fetch == NULL)
  1608. {
  1609. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1610. model->data_state = DataStateError;
  1611. return;
  1612. }
  1613. // Clear any previous responses
  1614. strncpy(fhttp.last_response, "", 1);
  1615. bool request_status = fetch(model);
  1616. if (!request_status)
  1617. {
  1618. model->data_state = DataStateError;
  1619. }
  1620. },
  1621. true);
  1622. }
  1623. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1624. {
  1625. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1626. {
  1627. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1628. {
  1629. FURI_LOG_DEV(TAG, "PONG received.");
  1630. }
  1631. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1632. {
  1633. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1634. }
  1635. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1636. {
  1637. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1638. }
  1639. else if (strlen(fhttp.last_response) == 0)
  1640. {
  1641. // Still waiting on response
  1642. }
  1643. else
  1644. {
  1645. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1646. }
  1647. }
  1648. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1649. {
  1650. // continue waiting
  1651. }
  1652. else if (fhttp.state == INACTIVE)
  1653. {
  1654. // inactive. try again
  1655. }
  1656. else if (fhttp.state == ISSUE)
  1657. {
  1658. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1659. }
  1660. else
  1661. {
  1662. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1663. DEV_CRASH();
  1664. }
  1665. }
  1666. else if (current_data_state == DataStateReceived)
  1667. {
  1668. with_view_model(
  1669. view,
  1670. DataLoaderModel * model,
  1671. {
  1672. char *data_text;
  1673. if (model->parser == NULL)
  1674. {
  1675. data_text = NULL;
  1676. FURI_LOG_DEV(TAG, "Parser is NULL");
  1677. DEV_CRASH();
  1678. }
  1679. else
  1680. {
  1681. data_text = model->parser(model);
  1682. }
  1683. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1684. model->data_text = data_text;
  1685. if (data_text == NULL)
  1686. {
  1687. model->data_state = DataStateParseError;
  1688. }
  1689. else
  1690. {
  1691. model->data_state = DataStateParsed;
  1692. }
  1693. },
  1694. true);
  1695. }
  1696. else if (current_data_state == DataStateParsed)
  1697. {
  1698. with_view_model(
  1699. view,
  1700. DataLoaderModel * model,
  1701. {
  1702. if (++model->request_index < model->request_count)
  1703. {
  1704. model->data_state = DataStateInitial;
  1705. }
  1706. else
  1707. {
  1708. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1709. if (model->data_text != NULL)
  1710. {
  1711. free(model->data_text);
  1712. model->data_text = NULL;
  1713. }
  1714. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1715. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1716. }
  1717. },
  1718. true);
  1719. }
  1720. }
  1721. static void flip_world_loader_timer_callback(void *context)
  1722. {
  1723. if (context == NULL)
  1724. {
  1725. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1726. DEV_CRASH();
  1727. return;
  1728. }
  1729. FlipWorldApp *app = (FlipWorldApp *)context;
  1730. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1731. }
  1732. static void flip_world_loader_on_enter(void *context)
  1733. {
  1734. if (context == NULL)
  1735. {
  1736. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1737. DEV_CRASH();
  1738. return;
  1739. }
  1740. FlipWorldApp *app = (FlipWorldApp *)context;
  1741. View *view = app->view_loader;
  1742. with_view_model(
  1743. view,
  1744. DataLoaderModel * model,
  1745. {
  1746. view_set_previous_callback(view, model->back_callback);
  1747. if (model->timer == NULL)
  1748. {
  1749. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1750. }
  1751. furi_timer_start(model->timer, 250);
  1752. },
  1753. true);
  1754. }
  1755. static void flip_world_loader_on_exit(void *context)
  1756. {
  1757. if (context == NULL)
  1758. {
  1759. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1760. DEV_CRASH();
  1761. return;
  1762. }
  1763. FlipWorldApp *app = (FlipWorldApp *)context;
  1764. View *view = app->view_loader;
  1765. with_view_model(
  1766. view,
  1767. DataLoaderModel * model,
  1768. {
  1769. if (model->timer)
  1770. {
  1771. furi_timer_stop(model->timer);
  1772. }
  1773. },
  1774. false);
  1775. }
  1776. void flip_world_loader_init(View *view)
  1777. {
  1778. if (view == NULL)
  1779. {
  1780. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1781. DEV_CRASH();
  1782. return;
  1783. }
  1784. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1785. view_set_enter_callback(view, flip_world_loader_on_enter);
  1786. view_set_exit_callback(view, flip_world_loader_on_exit);
  1787. }
  1788. void flip_world_loader_free_model(View *view)
  1789. {
  1790. if (view == NULL)
  1791. {
  1792. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1793. DEV_CRASH();
  1794. return;
  1795. }
  1796. with_view_model(
  1797. view,
  1798. DataLoaderModel * model,
  1799. {
  1800. if (model->timer)
  1801. {
  1802. furi_timer_free(model->timer);
  1803. model->timer = NULL;
  1804. }
  1805. if (model->parser_context)
  1806. {
  1807. free(model->parser_context);
  1808. model->parser_context = NULL;
  1809. }
  1810. },
  1811. false);
  1812. }
  1813. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1814. {
  1815. if (context == NULL)
  1816. {
  1817. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1818. DEV_CRASH();
  1819. return false;
  1820. }
  1821. switch (index)
  1822. {
  1823. case FlipWorldCustomEventProcess:
  1824. flip_world_loader_process_callback(context);
  1825. return true;
  1826. default:
  1827. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1828. return false;
  1829. }
  1830. }
  1831. 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)
  1832. {
  1833. if (app == NULL)
  1834. {
  1835. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1836. DEV_CRASH();
  1837. return;
  1838. }
  1839. View *view = app->view_loader;
  1840. if (view == NULL)
  1841. {
  1842. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1843. DEV_CRASH();
  1844. return;
  1845. }
  1846. with_view_model(
  1847. view,
  1848. DataLoaderModel * model,
  1849. {
  1850. model->title = title;
  1851. model->fetcher = fetcher;
  1852. model->parser = parser;
  1853. model->request_index = 0;
  1854. model->request_count = request_count;
  1855. model->back_callback = back;
  1856. model->data_state = DataStateInitial;
  1857. model->data_text = NULL;
  1858. },
  1859. true);
  1860. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1861. }