callback.c 73 KB

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