callback.c 72 KB

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