callback.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  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()
  568. {
  569. // Create the directory for saving worlds
  570. char directory_path[128];
  571. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  572. // Create the directory
  573. Storage *storage = furi_record_open(RECORD_STORAGE);
  574. storage_common_mkdir(storage, directory_path);
  575. // free storage
  576. furi_record_close(RECORD_STORAGE);
  577. snprintf(
  578. fhttp.file_path,
  579. sizeof(fhttp.file_path),
  580. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  581. fhttp.save_received_data = true;
  582. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  583. }
  584. // combine register, login, and world list fetch into one function to switch to the loader view
  585. static bool flip_world_fetch_game(DataLoaderModel *model)
  586. {
  587. if (model->request_index == 0)
  588. {
  589. // login
  590. char username[64];
  591. char password[64];
  592. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  593. {
  594. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  595. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  596. easy_flipper_dialog("Error", "Failed to load saved username\nGo to user settings to update.");
  597. flipper_http_deinit();
  598. return false;
  599. }
  600. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  601. {
  602. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  603. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  604. easy_flipper_dialog("Error", "Failed to load saved password\nGo to settings to update.");
  605. flipper_http_deinit();
  606. return false;
  607. }
  608. char payload[256];
  609. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  610. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  611. }
  612. else if (model->request_index == 1)
  613. {
  614. // check if login was successful
  615. char is_logged_in[8];
  616. if (!load_char("is_logged_in", is_logged_in, sizeof(is_logged_in)))
  617. {
  618. FURI_LOG_E(TAG, "Failed to load is_logged_in");
  619. easy_flipper_dialog("Error", "Failed to load is_logged_in\nGo to user settings to update.");
  620. flipper_http_deinit();
  621. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  622. return false;
  623. }
  624. if (strcmp(is_logged_in, "false") == 0 && strcmp(model->title, "Registering...") == 0)
  625. {
  626. // register
  627. char username[64];
  628. char password[64];
  629. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  630. {
  631. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  632. easy_flipper_dialog("Error", "Failed to load saved username. Go to settings to update.");
  633. flipper_http_deinit();
  634. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  635. return false;
  636. }
  637. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  638. {
  639. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  640. easy_flipper_dialog("Error", "Failed to load saved password. Go to settings to update.");
  641. flipper_http_deinit();
  642. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  643. return false;
  644. }
  645. char payload[172];
  646. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  647. model->title = "Registering...";
  648. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  649. }
  650. else
  651. {
  652. // Create the directory for saving worlds
  653. char directory_path[128];
  654. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  655. // Create the directory
  656. Storage *storage = furi_record_open(RECORD_STORAGE);
  657. storage_common_mkdir(storage, directory_path);
  658. // free storage
  659. furi_record_close(RECORD_STORAGE);
  660. snprintf(
  661. fhttp.file_path,
  662. sizeof(fhttp.file_path),
  663. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  664. model->title = "Fetching World List..";
  665. fhttp.save_received_data = true;
  666. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  667. }
  668. }
  669. else if (model->request_index == 2)
  670. {
  671. // Create the directory for saving worlds
  672. char directory_path[128];
  673. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  674. // Create the directory
  675. Storage *storage = furi_record_open(RECORD_STORAGE);
  676. storage_common_mkdir(storage, directory_path);
  677. // free storage
  678. furi_record_close(RECORD_STORAGE);
  679. snprintf(
  680. fhttp.file_path,
  681. sizeof(fhttp.file_path),
  682. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  683. model->title = "Fetching World List..";
  684. fhttp.save_received_data = true;
  685. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/list/10/", "{\"Content-Type\":\"application/json\"}");
  686. }
  687. else if (model->request_index == 3)
  688. {
  689. snprintf(
  690. fhttp.file_path,
  691. sizeof(fhttp.file_path),
  692. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  693. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  694. if (!world_list)
  695. {
  696. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  697. FURI_LOG_E(TAG, "Failed to load world list");
  698. easy_flipper_dialog("Error", "Failed to load world list. Go to game settings to download packs.");
  699. return false;
  700. }
  701. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  702. if (!first_world)
  703. {
  704. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  705. FURI_LOG_E(TAG, "Failed to get first world");
  706. easy_flipper_dialog("Error", "Failed to get first world. Go to game settings to download packs.");
  707. furi_string_free(world_list);
  708. return false;
  709. }
  710. if (world_exists(furi_string_get_cstr(first_world)))
  711. {
  712. furi_string_free(world_list);
  713. furi_string_free(first_world);
  714. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  715. flipper_http_deinit();
  716. // free game thread
  717. if (game_thread_running)
  718. {
  719. game_thread_running = false;
  720. furi_thread_flags_set(thread_id, WorkerEvtStop);
  721. furi_thread_free(thread_id);
  722. }
  723. if (!app_instance)
  724. {
  725. FURI_LOG_E(TAG, "app_instance is NULL");
  726. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  727. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  728. return "app_instance is NULL";
  729. }
  730. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  731. if (!thread)
  732. {
  733. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  734. FURI_LOG_E(TAG, "Failed to allocate game thread");
  735. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  736. return false;
  737. }
  738. furi_thread_start(thread);
  739. thread_id = furi_thread_get_id(thread);
  740. game_thread_running = true;
  741. return true;
  742. }
  743. snprintf(
  744. fhttp.file_path,
  745. sizeof(fhttp.file_path),
  746. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  747. fhttp.save_received_data = true;
  748. char url[128];
  749. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/v2/get/world/%s/", furi_string_get_cstr(first_world));
  750. furi_string_free(world_list);
  751. furi_string_free(first_world);
  752. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  753. }
  754. FURI_LOG_E(TAG, "Unknown request index");
  755. return false;
  756. }
  757. static char *flip_world_parse_game(DataLoaderModel *model)
  758. {
  759. if (model->request_index == 0)
  760. {
  761. if (!fhttp.last_response)
  762. {
  763. save_char("is_logged_in", "false");
  764. flipper_http_deinit();
  765. // Go back to the main menu
  766. easy_flipper_dialog("Error", "Response is empty. Press BACK to return.");
  767. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  768. return "Response is empty...";
  769. }
  770. // Check for successful conditions
  771. if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  772. {
  773. save_char("is_logged_in", "true");
  774. model->title = "Login successful!";
  775. model->title = "Fetching World List..";
  776. return "Login successful!";
  777. }
  778. // Check if user not found
  779. if (strstr(fhttp.last_response, "User not found") != NULL)
  780. {
  781. save_char("is_logged_in", "false");
  782. model->title = "Registering...";
  783. return "Account not found...\nRegistering now.."; // if they see this an issue happened switching to register
  784. }
  785. // If not success, not found, check length conditions
  786. size_t resp_len = strlen(fhttp.last_response);
  787. if (resp_len == 0 || resp_len > 127)
  788. {
  789. // Empty or too long means failed login
  790. flipper_http_deinit();
  791. save_char("is_logged_in", "false");
  792. // Go back to the main menu
  793. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  794. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  795. return "Failed to login...";
  796. }
  797. // Handle any other unknown response as a failure
  798. flipper_http_deinit();
  799. save_char("is_logged_in", "false");
  800. // Go back to the main menu
  801. easy_flipper_dialog("Error", "Failed to login. Press BACK to return.");
  802. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  803. return "Failed to login...";
  804. }
  805. else if (model->request_index == 1)
  806. {
  807. if (strcmp(model->title, "Registering...") == 0)
  808. {
  809. // check registration response
  810. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  811. {
  812. save_char("is_logged_in", "true");
  813. char username[64];
  814. char password[64];
  815. // load the username and password, then save them
  816. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  817. {
  818. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  819. easy_flipper_dialog("Error", "Failed to load Flip-Social-Username");
  820. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  821. flipper_http_deinit();
  822. return "Failed to load Flip-Social-Username";
  823. }
  824. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  825. {
  826. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  827. easy_flipper_dialog("Error", "Failed to load Flip-Social-Password");
  828. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  829. flipper_http_deinit();
  830. return "Failed to load Flip-Social-Password";
  831. }
  832. // load wifi ssid,pass then save
  833. char ssid[64];
  834. char pass[64];
  835. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  836. {
  837. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  838. easy_flipper_dialog("Error", "Failed to load WiFi-SSID");
  839. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  840. flipper_http_deinit();
  841. return "Failed to load WiFi-SSID";
  842. }
  843. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  844. {
  845. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  846. easy_flipper_dialog("Error", "Failed to load WiFi-Password");
  847. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  848. flipper_http_deinit();
  849. return "Failed to load WiFi-Password";
  850. }
  851. save_settings(ssid, pass, username, password);
  852. model->title = "Fetching World List..";
  853. return "Account created!";
  854. }
  855. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  856. {
  857. flipper_http_deinit();
  858. easy_flipper_dialog("Error", "Please enter your credentials.\nPress BACK to return.");
  859. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  860. return "Please enter your credentials.";
  861. }
  862. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  863. {
  864. flipper_http_deinit();
  865. easy_flipper_dialog("Error", "Registration failed...\nUsername already exists.\nPress BACK to return.");
  866. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  867. return "Username already exists.";
  868. }
  869. else
  870. {
  871. flipper_http_deinit();
  872. easy_flipper_dialog("Error", "Registration failed...\nUpdate your credentials.\nPress BACK to return.");
  873. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  874. return "Registration failed...";
  875. }
  876. }
  877. else
  878. {
  879. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  880. flipper_http_deinit();
  881. // free game thread
  882. if (game_thread_running)
  883. {
  884. game_thread_running = false;
  885. furi_thread_flags_set(thread_id, WorkerEvtStop);
  886. furi_thread_free(thread_id);
  887. }
  888. if (!app_instance)
  889. {
  890. FURI_LOG_E(TAG, "app_instance is NULL");
  891. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  892. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  893. return "app_instance is NULL";
  894. }
  895. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  896. if (!thread)
  897. {
  898. FURI_LOG_E(TAG, "Failed to allocate game thread");
  899. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  900. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  901. return "Failed to allocate game thread";
  902. }
  903. furi_thread_start(thread);
  904. thread_id = furi_thread_get_id(thread);
  905. game_thread_running = true;
  906. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  907. }
  908. }
  909. else if (model->request_index == 2)
  910. {
  911. return "Welcome to FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  912. }
  913. else if (model->request_index == 3)
  914. {
  915. // view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu);
  916. flipper_http_deinit();
  917. // free game thread
  918. if (game_thread_running)
  919. {
  920. game_thread_running = false;
  921. furi_thread_flags_set(thread_id, WorkerEvtStop);
  922. furi_thread_free(thread_id);
  923. }
  924. if (!app_instance)
  925. {
  926. FURI_LOG_E(TAG, "app_instance is NULL");
  927. easy_flipper_dialog("Error", "app_instance is NULL. Press BACK to return.");
  928. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  929. return "app_instance is NULL";
  930. }
  931. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app_instance);
  932. if (!thread)
  933. {
  934. FURI_LOG_E(TAG, "Failed to allocate game thread");
  935. easy_flipper_dialog("Error", "Failed to allocate game thread. Restart your Flipper.");
  936. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  937. return "Failed to allocate game thread";
  938. }
  939. furi_thread_start(thread);
  940. thread_id = furi_thread_get_id(thread);
  941. game_thread_running = true;
  942. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  943. }
  944. easy_flipper_dialog("Error", "Unknown error. Press BACK to return.");
  945. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  946. return "Unknown error";
  947. }
  948. void flip_world_switch_to_view_get_game(FlipWorldApp *app)
  949. {
  950. flip_world_generic_switch_to_view(app, "Starting Game..", flip_world_fetch_game, flip_world_parse_game, 5, callback_to_submenu, FlipWorldViewLoader);
  951. }
  952. void callback_submenu_choices(void *context, uint32_t index)
  953. {
  954. FlipWorldApp *app = (FlipWorldApp *)context;
  955. if (!app)
  956. {
  957. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  958. return;
  959. }
  960. switch (index)
  961. {
  962. case FlipWorldSubmenuIndexRun:
  963. free_all_views(app, true, true);
  964. if (!flipper_http_init(flipper_http_rx_callback, app))
  965. {
  966. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  967. return;
  968. }
  969. app_instance = malloc(sizeof(FlipWorldApp));
  970. if (!app_instance)
  971. {
  972. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  973. return;
  974. }
  975. memcpy(app_instance, app, sizeof(FlipWorldApp));
  976. // check if logged in
  977. if (is_logged_in() || is_logged_in_to_flip_social())
  978. {
  979. bool parse_world_list()
  980. {
  981. flipper_http_deinit();
  982. return true;
  983. }
  984. flipper_http_loading_task(
  985. flip_world_fetch_world_list,
  986. parse_world_list,
  987. FlipWorldViewSubmenu,
  988. FlipWorldViewSubmenu,
  989. &app->view_dispatcher);
  990. // Free game thread properly
  991. if (game_thread_running)
  992. {
  993. game_thread_running = false;
  994. furi_thread_flags_set(thread_id, WorkerEvtStop);
  995. // Wait for the thread to terminate
  996. furi_thread_join(thread_id);
  997. furi_thread_free(thread_id);
  998. }
  999. // Update heap sizes after freeing
  1000. size_t heap_size = memmgr_get_free_heap();
  1001. size_t total_heap_size = memmgr_get_total_heap();
  1002. FURI_LOG_I(TAG, "Heap size: %d", heap_size);
  1003. FURI_LOG_I(TAG, "Total heap size: %d", total_heap_size);
  1004. FuriThread *thread = furi_thread_alloc_ex("flip_world_game", 4096, game_app, app);
  1005. if (!thread)
  1006. {
  1007. FURI_LOG_E(TAG, "Failed to allocate game thread");
  1008. return;
  1009. }
  1010. furi_thread_start(thread);
  1011. thread_id = furi_thread_get_id(thread);
  1012. game_thread_running = true;
  1013. }
  1014. else
  1015. {
  1016. flip_world_switch_to_view_get_game(app);
  1017. }
  1018. break;
  1019. case FlipWorldSubmenuIndexAbout:
  1020. free_all_views(app, true, true);
  1021. if (!alloc_about_view(app))
  1022. {
  1023. FURI_LOG_E(TAG, "Failed to allocate about view");
  1024. return;
  1025. }
  1026. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  1027. break;
  1028. case FlipWorldSubmenuIndexSettings:
  1029. free_all_views(app, true, true);
  1030. if (!alloc_submenu_settings(app))
  1031. {
  1032. FURI_LOG_E(TAG, "Failed to allocate settings view");
  1033. return;
  1034. }
  1035. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  1036. break;
  1037. case FlipWorldSubmenuIndexWiFiSettings:
  1038. free_all_views(app, true, false);
  1039. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  1040. {
  1041. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1042. return;
  1043. }
  1044. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1045. break;
  1046. case FlipWorldSubmenuIndexGameSettings:
  1047. free_all_views(app, true, false);
  1048. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  1049. {
  1050. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1051. return;
  1052. }
  1053. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1054. break;
  1055. case FlipWorldSubmenuIndexUserSettings:
  1056. free_all_views(app, true, false);
  1057. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  1058. {
  1059. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  1060. return;
  1061. }
  1062. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1063. break;
  1064. default:
  1065. break;
  1066. }
  1067. }
  1068. static void text_updated_wifi_ssid(void *context)
  1069. {
  1070. FlipWorldApp *app = (FlipWorldApp *)context;
  1071. if (!app)
  1072. {
  1073. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1074. return;
  1075. }
  1076. // store the entered text
  1077. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1078. // Ensure null-termination
  1079. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1080. // save the setting
  1081. save_char("WiFi-SSID", app->text_input_buffer);
  1082. // update the variable item text
  1083. if (app->variable_item_wifi_ssid)
  1084. {
  1085. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  1086. // get value of password
  1087. char pass[64];
  1088. char username[64];
  1089. char password[64];
  1090. if (load_char("WiFi-Password", pass, sizeof(pass)))
  1091. {
  1092. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  1093. {
  1094. // save the settings
  1095. load_char("Flip-Social-Username", username, sizeof(username));
  1096. load_char("Flip-Social-Password", password, sizeof(password));
  1097. save_settings(app->text_input_buffer, pass, username, password);
  1098. // initialize the http
  1099. if (flipper_http_init(flipper_http_rx_callback, app))
  1100. {
  1101. // save the wifi if the device is connected
  1102. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  1103. {
  1104. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1105. }
  1106. // free the resources
  1107. flipper_http_deinit();
  1108. }
  1109. else
  1110. {
  1111. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1112. }
  1113. }
  1114. }
  1115. }
  1116. // switch to the settings view
  1117. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1118. }
  1119. static void text_updated_wifi_pass(void *context)
  1120. {
  1121. FlipWorldApp *app = (FlipWorldApp *)context;
  1122. if (!app)
  1123. {
  1124. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1125. return;
  1126. }
  1127. // store the entered text
  1128. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1129. // Ensure null-termination
  1130. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1131. // save the setting
  1132. save_char("WiFi-Password", app->text_input_buffer);
  1133. // update the variable item text
  1134. if (app->variable_item_wifi_pass)
  1135. {
  1136. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  1137. }
  1138. // get value of ssid
  1139. char ssid[64];
  1140. char username[64];
  1141. char password[64];
  1142. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  1143. {
  1144. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  1145. {
  1146. // save the settings
  1147. load_char("Flip-Social-Username", username, sizeof(username));
  1148. load_char("Flip-Social-Password", password, sizeof(password));
  1149. save_settings(ssid, app->text_input_buffer, username, password);
  1150. // initialize the http
  1151. if (flipper_http_init(flipper_http_rx_callback, app))
  1152. {
  1153. // save the wifi if the device is connected
  1154. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  1155. {
  1156. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  1157. }
  1158. // free the resources
  1159. flipper_http_deinit();
  1160. }
  1161. else
  1162. {
  1163. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  1164. }
  1165. }
  1166. }
  1167. // switch to the settings view
  1168. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  1169. }
  1170. static void text_updated_username(void *context)
  1171. {
  1172. FlipWorldApp *app = (FlipWorldApp *)context;
  1173. if (!app)
  1174. {
  1175. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1176. return;
  1177. }
  1178. // store the entered text
  1179. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1180. // Ensure null-termination
  1181. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1182. // save the setting
  1183. save_char("Flip-Social-Username", app->text_input_buffer);
  1184. // update the variable item text
  1185. if (app->variable_item_user_username)
  1186. {
  1187. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1188. }
  1189. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1190. }
  1191. static void text_updated_password(void *context)
  1192. {
  1193. FlipWorldApp *app = (FlipWorldApp *)context;
  1194. if (!app)
  1195. {
  1196. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1197. return;
  1198. }
  1199. // store the entered text
  1200. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1201. // Ensure null-termination
  1202. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1203. // save the setting
  1204. save_char("Flip-Social-Password", app->text_input_buffer);
  1205. // update the variable item text
  1206. if (app->variable_item_user_password)
  1207. {
  1208. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1209. }
  1210. // get value of username
  1211. char username[64];
  1212. char ssid[64];
  1213. char pass[64];
  1214. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1215. {
  1216. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1217. {
  1218. // save the settings
  1219. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1220. load_char("WiFi-Password", pass, sizeof(pass));
  1221. save_settings(ssid, pass, username, app->text_input_buffer);
  1222. }
  1223. }
  1224. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1225. }
  1226. static void wifi_settings_item_selected(void *context, uint32_t index)
  1227. {
  1228. FlipWorldApp *app = (FlipWorldApp *)context;
  1229. if (!app)
  1230. {
  1231. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1232. return;
  1233. }
  1234. char ssid[64];
  1235. char pass[64];
  1236. char username[64];
  1237. char password[64];
  1238. switch (index)
  1239. {
  1240. case 0: // Input SSID
  1241. free_all_views(app, false, false);
  1242. if (!alloc_text_input_view(app, "SSID"))
  1243. {
  1244. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1245. return;
  1246. }
  1247. // load SSID
  1248. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1249. {
  1250. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1251. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1252. }
  1253. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1254. break;
  1255. case 1: // Input Password
  1256. free_all_views(app, false, false);
  1257. if (!alloc_text_input_view(app, "Password"))
  1258. {
  1259. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1260. return;
  1261. }
  1262. // load password
  1263. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1264. {
  1265. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1266. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1267. }
  1268. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1269. break;
  1270. default:
  1271. FURI_LOG_E(TAG, "Unknown configuration item index");
  1272. break;
  1273. }
  1274. }
  1275. static void flip_world_game_fps_change(VariableItem *item)
  1276. {
  1277. uint8_t index = variable_item_get_current_value_index(item);
  1278. game_fps_index = index;
  1279. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1280. // save the fps
  1281. save_char("Game-FPS", game_fps_choices[index]);
  1282. }
  1283. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1284. {
  1285. uint8_t index = variable_item_get_current_value_index(item);
  1286. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1287. // save the screen always on
  1288. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1289. }
  1290. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1291. {
  1292. UNUSED(model);
  1293. // Create the directory for saving settings
  1294. char directory_path[256];
  1295. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  1296. // Create the directory
  1297. Storage *storage = furi_record_open(RECORD_STORAGE);
  1298. storage_common_mkdir(storage, directory_path);
  1299. furi_record_close(RECORD_STORAGE);
  1300. snprintf(
  1301. fhttp.file_path,
  1302. sizeof(fhttp.file_path),
  1303. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list_full.json");
  1304. fhttp.save_received_data = true;
  1305. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/v2/get/10/", "{\"Content-Type\":\"application/json\"}");
  1306. }
  1307. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1308. {
  1309. UNUSED(model);
  1310. flipper_http_deinit();
  1311. return "World Pack Installed";
  1312. }
  1313. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1314. {
  1315. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1316. }
  1317. static void game_settings_item_selected(void *context, uint32_t index)
  1318. {
  1319. FlipWorldApp *app = (FlipWorldApp *)context;
  1320. if (!app)
  1321. {
  1322. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1323. return;
  1324. }
  1325. switch (index)
  1326. {
  1327. case 0: // Download all world data s one huge json
  1328. if (!flipper_http_init(flipper_http_rx_callback, app))
  1329. {
  1330. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1331. return;
  1332. }
  1333. flip_world_switch_to_view_get_worlds(app);
  1334. case 1: // Change FPS
  1335. break;
  1336. case 2: // Screen Always On
  1337. break;
  1338. }
  1339. }
  1340. static void user_settings_item_selected(void *context, uint32_t index)
  1341. {
  1342. FlipWorldApp *app = (FlipWorldApp *)context;
  1343. if (!app)
  1344. {
  1345. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1346. return;
  1347. }
  1348. switch (index)
  1349. {
  1350. case 0: // Username
  1351. free_all_views(app, false, false);
  1352. if (!alloc_text_input_view(app, "Username-Login"))
  1353. {
  1354. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1355. return;
  1356. }
  1357. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1358. break;
  1359. case 1: // Password
  1360. free_all_views(app, false, false);
  1361. if (!alloc_text_input_view(app, "Password-Login"))
  1362. {
  1363. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1364. return;
  1365. }
  1366. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1367. break;
  1368. }
  1369. }
  1370. static void flip_world_widget_set_text(char *message, Widget **widget)
  1371. {
  1372. if (widget == NULL)
  1373. {
  1374. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1375. DEV_CRASH();
  1376. return;
  1377. }
  1378. if (message == NULL)
  1379. {
  1380. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1381. DEV_CRASH();
  1382. return;
  1383. }
  1384. widget_reset(*widget);
  1385. uint32_t message_length = strlen(message); // Length of the message
  1386. uint32_t i = 0; // Index tracker
  1387. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1388. char *formatted_message; // Buffer to hold the final formatted message
  1389. // Allocate buffer with double the message length plus one for safety
  1390. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1391. {
  1392. return;
  1393. }
  1394. while (i < message_length)
  1395. {
  1396. uint32_t max_line_length = 31; // Maximum characters per line
  1397. uint32_t remaining_length = message_length - i; // Remaining characters
  1398. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1399. // Check for newline character within the current segment
  1400. uint32_t newline_pos = i;
  1401. bool found_newline = false;
  1402. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1403. {
  1404. if (message[newline_pos] == '\n')
  1405. {
  1406. found_newline = true;
  1407. break;
  1408. }
  1409. }
  1410. if (found_newline)
  1411. {
  1412. // If newline found, set line_length up to the newline
  1413. line_length = newline_pos - i;
  1414. }
  1415. // Temporary buffer to hold the current line
  1416. char line[32];
  1417. strncpy(line, message + i, line_length);
  1418. line[line_length] = '\0';
  1419. // If newline was found, skip it for the next iteration
  1420. if (found_newline)
  1421. {
  1422. i += line_length + 1; // +1 to skip the '\n' character
  1423. }
  1424. else
  1425. {
  1426. // Check if the line ends in the middle of a word and adjust accordingly
  1427. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1428. {
  1429. // Find the last space within the current line to avoid breaking a word
  1430. char *last_space = strrchr(line, ' ');
  1431. if (last_space != NULL)
  1432. {
  1433. // Adjust the line_length to avoid cutting the word
  1434. line_length = last_space - line;
  1435. line[line_length] = '\0'; // Null-terminate at the space
  1436. }
  1437. }
  1438. // Move the index forward by the determined line_length
  1439. i += line_length;
  1440. // Skip any spaces at the beginning of the next line
  1441. while (i < message_length && message[i] == ' ')
  1442. {
  1443. i++;
  1444. }
  1445. }
  1446. // Manually copy the fixed line into the formatted_message buffer
  1447. for (uint32_t j = 0; j < line_length; j++)
  1448. {
  1449. formatted_message[formatted_index++] = line[j];
  1450. }
  1451. // Add a newline character for line spacing
  1452. formatted_message[formatted_index++] = '\n';
  1453. }
  1454. // Null-terminate the formatted_message
  1455. formatted_message[formatted_index] = '\0';
  1456. // Add the formatted message to the widget
  1457. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1458. }
  1459. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1460. {
  1461. if (!canvas || !model)
  1462. {
  1463. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1464. return;
  1465. }
  1466. SerialState http_state = fhttp.state;
  1467. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1468. DataState data_state = data_loader_model->data_state;
  1469. char *title = data_loader_model->title;
  1470. canvas_set_font(canvas, FontSecondary);
  1471. if (http_state == INACTIVE)
  1472. {
  1473. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1474. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1475. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1476. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1477. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1478. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1479. return;
  1480. }
  1481. if (data_state == DataStateError || data_state == DataStateParseError)
  1482. {
  1483. flip_world_request_error_draw(canvas);
  1484. return;
  1485. }
  1486. canvas_draw_str(canvas, 0, 7, title);
  1487. canvas_draw_str(canvas, 0, 17, "Loading...");
  1488. if (data_state == DataStateInitial)
  1489. {
  1490. return;
  1491. }
  1492. if (http_state == SENDING)
  1493. {
  1494. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1495. return;
  1496. }
  1497. if (http_state == RECEIVING || data_state == DataStateRequested)
  1498. {
  1499. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1500. return;
  1501. }
  1502. if (http_state == IDLE && data_state == DataStateReceived)
  1503. {
  1504. canvas_draw_str(canvas, 0, 27, "Processing...");
  1505. return;
  1506. }
  1507. if (http_state == IDLE && data_state == DataStateParsed)
  1508. {
  1509. canvas_draw_str(canvas, 0, 27, "Processed...");
  1510. return;
  1511. }
  1512. }
  1513. static void flip_world_loader_process_callback(void *context)
  1514. {
  1515. if (context == NULL)
  1516. {
  1517. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1518. DEV_CRASH();
  1519. return;
  1520. }
  1521. FlipWorldApp *app = (FlipWorldApp *)context;
  1522. View *view = app->view_loader;
  1523. DataState current_data_state;
  1524. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1525. if (current_data_state == DataStateInitial)
  1526. {
  1527. with_view_model(
  1528. view,
  1529. DataLoaderModel * model,
  1530. {
  1531. model->data_state = DataStateRequested;
  1532. DataLoaderFetch fetch = model->fetcher;
  1533. if (fetch == NULL)
  1534. {
  1535. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1536. model->data_state = DataStateError;
  1537. return;
  1538. }
  1539. // Clear any previous responses
  1540. strncpy(fhttp.last_response, "", 1);
  1541. bool request_status = fetch(model);
  1542. if (!request_status)
  1543. {
  1544. model->data_state = DataStateError;
  1545. }
  1546. },
  1547. true);
  1548. }
  1549. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1550. {
  1551. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1552. {
  1553. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1554. {
  1555. FURI_LOG_DEV(TAG, "PONG received.");
  1556. }
  1557. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1558. {
  1559. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1560. }
  1561. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1562. {
  1563. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1564. }
  1565. else if (strlen(fhttp.last_response) == 0)
  1566. {
  1567. // Still waiting on response
  1568. }
  1569. else
  1570. {
  1571. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1572. }
  1573. }
  1574. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1575. {
  1576. // continue waiting
  1577. }
  1578. else if (fhttp.state == INACTIVE)
  1579. {
  1580. // inactive. try again
  1581. }
  1582. else if (fhttp.state == ISSUE)
  1583. {
  1584. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1585. }
  1586. else
  1587. {
  1588. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1589. DEV_CRASH();
  1590. }
  1591. }
  1592. else if (current_data_state == DataStateReceived)
  1593. {
  1594. with_view_model(
  1595. view,
  1596. DataLoaderModel * model,
  1597. {
  1598. char *data_text;
  1599. if (model->parser == NULL)
  1600. {
  1601. data_text = NULL;
  1602. FURI_LOG_DEV(TAG, "Parser is NULL");
  1603. DEV_CRASH();
  1604. }
  1605. else
  1606. {
  1607. data_text = model->parser(model);
  1608. }
  1609. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1610. model->data_text = data_text;
  1611. if (data_text == NULL)
  1612. {
  1613. model->data_state = DataStateParseError;
  1614. }
  1615. else
  1616. {
  1617. model->data_state = DataStateParsed;
  1618. }
  1619. },
  1620. true);
  1621. }
  1622. else if (current_data_state == DataStateParsed)
  1623. {
  1624. with_view_model(
  1625. view,
  1626. DataLoaderModel * model,
  1627. {
  1628. if (++model->request_index < model->request_count)
  1629. {
  1630. model->data_state = DataStateInitial;
  1631. }
  1632. else
  1633. {
  1634. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1635. if (model->data_text != NULL)
  1636. {
  1637. free(model->data_text);
  1638. model->data_text = NULL;
  1639. }
  1640. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1641. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1642. }
  1643. },
  1644. true);
  1645. }
  1646. }
  1647. static void flip_world_loader_timer_callback(void *context)
  1648. {
  1649. if (context == NULL)
  1650. {
  1651. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1652. DEV_CRASH();
  1653. return;
  1654. }
  1655. FlipWorldApp *app = (FlipWorldApp *)context;
  1656. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1657. }
  1658. static void flip_world_loader_on_enter(void *context)
  1659. {
  1660. if (context == NULL)
  1661. {
  1662. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1663. DEV_CRASH();
  1664. return;
  1665. }
  1666. FlipWorldApp *app = (FlipWorldApp *)context;
  1667. View *view = app->view_loader;
  1668. with_view_model(
  1669. view,
  1670. DataLoaderModel * model,
  1671. {
  1672. view_set_previous_callback(view, model->back_callback);
  1673. if (model->timer == NULL)
  1674. {
  1675. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1676. }
  1677. furi_timer_start(model->timer, 250);
  1678. },
  1679. true);
  1680. }
  1681. static void flip_world_loader_on_exit(void *context)
  1682. {
  1683. if (context == NULL)
  1684. {
  1685. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1686. DEV_CRASH();
  1687. return;
  1688. }
  1689. FlipWorldApp *app = (FlipWorldApp *)context;
  1690. View *view = app->view_loader;
  1691. with_view_model(
  1692. view,
  1693. DataLoaderModel * model,
  1694. {
  1695. if (model->timer)
  1696. {
  1697. furi_timer_stop(model->timer);
  1698. }
  1699. },
  1700. false);
  1701. }
  1702. void flip_world_loader_init(View *view)
  1703. {
  1704. if (view == NULL)
  1705. {
  1706. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1707. DEV_CRASH();
  1708. return;
  1709. }
  1710. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1711. view_set_enter_callback(view, flip_world_loader_on_enter);
  1712. view_set_exit_callback(view, flip_world_loader_on_exit);
  1713. }
  1714. void flip_world_loader_free_model(View *view)
  1715. {
  1716. if (view == NULL)
  1717. {
  1718. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1719. DEV_CRASH();
  1720. return;
  1721. }
  1722. with_view_model(
  1723. view,
  1724. DataLoaderModel * model,
  1725. {
  1726. if (model->timer)
  1727. {
  1728. furi_timer_free(model->timer);
  1729. model->timer = NULL;
  1730. }
  1731. if (model->parser_context)
  1732. {
  1733. free(model->parser_context);
  1734. model->parser_context = NULL;
  1735. }
  1736. },
  1737. false);
  1738. }
  1739. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1740. {
  1741. if (context == NULL)
  1742. {
  1743. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1744. DEV_CRASH();
  1745. return false;
  1746. }
  1747. switch (index)
  1748. {
  1749. case FlipWorldCustomEventProcess:
  1750. flip_world_loader_process_callback(context);
  1751. return true;
  1752. default:
  1753. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1754. return false;
  1755. }
  1756. }
  1757. 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)
  1758. {
  1759. if (app == NULL)
  1760. {
  1761. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1762. DEV_CRASH();
  1763. return;
  1764. }
  1765. View *view = app->view_loader;
  1766. if (view == NULL)
  1767. {
  1768. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1769. DEV_CRASH();
  1770. return;
  1771. }
  1772. with_view_model(
  1773. view,
  1774. DataLoaderModel * model,
  1775. {
  1776. model->title = title;
  1777. model->fetcher = fetcher;
  1778. model->parser = parser;
  1779. model->request_index = 0;
  1780. model->request_count = request_count;
  1781. model->back_callback = back;
  1782. model->data_state = DataStateInitial;
  1783. model->data_text = NULL;
  1784. },
  1785. true);
  1786. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1787. }