callback.c 61 KB

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