callback.c 73 KB

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