callback.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  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, "Password") == 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
  240. {
  241. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  242. }
  243. }
  244. }
  245. return true;
  246. }
  247. static bool alloc_variable_item_list(void *context, uint32_t view_id)
  248. {
  249. FlipWorldApp *app = (FlipWorldApp *)context;
  250. if (!app)
  251. {
  252. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  253. return false;
  254. }
  255. char ssid[64];
  256. char pass[64];
  257. char username[64];
  258. char password[64];
  259. if (!app->variable_item_list)
  260. {
  261. switch (view_id)
  262. {
  263. case FlipWorldSubmenuIndexWiFiSettings:
  264. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, wifi_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  265. {
  266. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  267. return false;
  268. }
  269. if (!app->variable_item_list)
  270. {
  271. FURI_LOG_E(TAG, "Variable item list is NULL");
  272. return false;
  273. }
  274. if (!app->variable_item_wifi_ssid)
  275. {
  276. app->variable_item_wifi_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  277. variable_item_set_current_value_text(app->variable_item_wifi_ssid, "");
  278. }
  279. if (!app->variable_item_wifi_pass)
  280. {
  281. app->variable_item_wifi_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  282. variable_item_set_current_value_text(app->variable_item_wifi_pass, "");
  283. }
  284. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  285. {
  286. variable_item_set_current_value_text(app->variable_item_wifi_ssid, ssid);
  287. // variable_item_set_current_value_text(app->variable_item_wifi_pass, pass);
  288. save_char("WiFi-SSID", ssid);
  289. save_char("WiFi-Password", pass);
  290. save_char("Flip-Social-Username", username);
  291. save_char("Flip-Social-Password", password);
  292. }
  293. break;
  294. case FlipWorldSubmenuIndexGameSettings:
  295. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, game_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  296. {
  297. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  298. return false;
  299. }
  300. if (!app->variable_item_list)
  301. {
  302. FURI_LOG_E(TAG, "Variable item list is NULL");
  303. return false;
  304. }
  305. if (!app->variable_item_game_download_world)
  306. {
  307. app->variable_item_game_download_world = variable_item_list_add(app->variable_item_list, "Install Official World Pack", 0, NULL, NULL);
  308. variable_item_set_current_value_text(app->variable_item_game_download_world, "");
  309. }
  310. if (!app->variable_item_game_fps)
  311. {
  312. app->variable_item_game_fps = variable_item_list_add(app->variable_item_list, "FPS", 4, flip_world_game_fps_change, NULL);
  313. variable_item_set_current_value_index(app->variable_item_game_fps, 0);
  314. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[0]);
  315. }
  316. if (!app->variable_item_game_screen_always_on)
  317. {
  318. 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);
  319. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, 1);
  320. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[1]);
  321. }
  322. char _game_fps[8];
  323. if (load_char("Game-FPS", _game_fps, sizeof(_game_fps)))
  324. {
  325. int index = strcmp(_game_fps, "30") == 0 ? 0 : strcmp(_game_fps, "60") == 0 ? 1
  326. : strcmp(_game_fps, "120") == 0 ? 2
  327. : strcmp(_game_fps, "240") == 0 ? 3
  328. : 0;
  329. variable_item_set_current_value_text(app->variable_item_game_fps, game_fps_choices[index]);
  330. variable_item_set_current_value_index(app->variable_item_game_fps, index);
  331. }
  332. char _game_screen_always_on[8];
  333. if (load_char("Game-Screen-Always-On", _game_screen_always_on, sizeof(_game_screen_always_on)))
  334. {
  335. int index = strcmp(_game_screen_always_on, "No") == 0 ? 0 : strcmp(_game_screen_always_on, "Yes") == 0 ? 1
  336. : 0;
  337. variable_item_set_current_value_text(app->variable_item_game_screen_always_on, game_screen_always_on_choices[index]);
  338. variable_item_set_current_value_index(app->variable_item_game_screen_always_on, index);
  339. }
  340. break;
  341. case FlipWorldSubmenuIndexUserSettings:
  342. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewVariableItemList, user_settings_item_selected, callback_to_settings, &app->view_dispatcher, app))
  343. {
  344. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  345. return false;
  346. }
  347. if (!app->variable_item_list)
  348. {
  349. FURI_LOG_E(TAG, "Variable item list is NULL");
  350. return false;
  351. }
  352. // if logged in, show profile info, otherwise show login/register
  353. if (is_logged_in() || is_logged_in_to_flip_social())
  354. {
  355. if (!app->variable_item_user_username)
  356. {
  357. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  358. variable_item_set_current_value_text(app->variable_item_user_username, "");
  359. }
  360. if (!app->variable_item_user_password)
  361. {
  362. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  363. variable_item_set_current_value_text(app->variable_item_user_password, "");
  364. }
  365. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  366. {
  367. variable_item_set_current_value_text(app->variable_item_user_username, username);
  368. variable_item_set_current_value_text(app->variable_item_user_password, "*****");
  369. }
  370. }
  371. else
  372. {
  373. if (!app->variable_item_user_username)
  374. {
  375. app->variable_item_user_username = variable_item_list_add(app->variable_item_list, "Username", 0, NULL, NULL);
  376. variable_item_set_current_value_text(app->variable_item_user_username, "");
  377. }
  378. if (!app->variable_item_user_password)
  379. {
  380. app->variable_item_user_password = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  381. variable_item_set_current_value_text(app->variable_item_user_password, "");
  382. }
  383. }
  384. break;
  385. }
  386. }
  387. return true;
  388. }
  389. static bool alloc_submenu_settings(void *context)
  390. {
  391. FlipWorldApp *app = (FlipWorldApp *)context;
  392. if (!app)
  393. {
  394. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  395. return false;
  396. }
  397. if (!app->submenu_settings)
  398. {
  399. if (!easy_flipper_set_submenu(&app->submenu_settings, FlipWorldViewSettings, "Settings", callback_to_submenu, &app->view_dispatcher))
  400. {
  401. return NULL;
  402. }
  403. if (!app->submenu_settings)
  404. {
  405. return false;
  406. }
  407. submenu_add_item(app->submenu_settings, "WiFi", FlipWorldSubmenuIndexWiFiSettings, callback_submenu_choices, app);
  408. submenu_add_item(app->submenu_settings, "Game", FlipWorldSubmenuIndexGameSettings, callback_submenu_choices, app);
  409. submenu_add_item(app->submenu_settings, "User", FlipWorldSubmenuIndexUserSettings, callback_submenu_choices, app);
  410. }
  411. return true;
  412. }
  413. // free
  414. static void free_about_view(void *context)
  415. {
  416. FlipWorldApp *app = (FlipWorldApp *)context;
  417. if (!app)
  418. {
  419. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  420. return;
  421. }
  422. if (app->view_about)
  423. {
  424. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  425. view_free(app->view_about);
  426. app->view_about = NULL;
  427. }
  428. }
  429. static void free_main_view(void *context)
  430. {
  431. FlipWorldApp *app = (FlipWorldApp *)context;
  432. if (!app)
  433. {
  434. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  435. return;
  436. }
  437. }
  438. static void free_text_input_view(void *context)
  439. {
  440. FlipWorldApp *app = (FlipWorldApp *)context;
  441. if (!app)
  442. {
  443. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  444. return;
  445. }
  446. if (app->text_input)
  447. {
  448. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  449. uart_text_input_free(app->text_input);
  450. app->text_input = NULL;
  451. }
  452. if (app->text_input_buffer)
  453. {
  454. free(app->text_input_buffer);
  455. app->text_input_buffer = NULL;
  456. }
  457. if (app->text_input_temp_buffer)
  458. {
  459. free(app->text_input_temp_buffer);
  460. app->text_input_temp_buffer = NULL;
  461. }
  462. }
  463. static void free_variable_item_list(void *context)
  464. {
  465. FlipWorldApp *app = (FlipWorldApp *)context;
  466. if (!app)
  467. {
  468. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  469. return;
  470. }
  471. if (app->variable_item_list)
  472. {
  473. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  474. variable_item_list_free(app->variable_item_list);
  475. app->variable_item_list = NULL;
  476. }
  477. if (app->variable_item_wifi_ssid)
  478. {
  479. free(app->variable_item_wifi_ssid);
  480. app->variable_item_wifi_ssid = NULL;
  481. }
  482. if (app->variable_item_wifi_pass)
  483. {
  484. free(app->variable_item_wifi_pass);
  485. app->variable_item_wifi_pass = NULL;
  486. }
  487. if (app->variable_item_game_fps)
  488. {
  489. free(app->variable_item_game_fps);
  490. app->variable_item_game_fps = NULL;
  491. }
  492. if (app->variable_item_game_screen_always_on)
  493. {
  494. free(app->variable_item_game_screen_always_on);
  495. app->variable_item_game_screen_always_on = NULL;
  496. }
  497. if (app->variable_item_game_download_world)
  498. {
  499. free(app->variable_item_game_download_world);
  500. app->variable_item_game_download_world = NULL;
  501. }
  502. if (app->variable_item_user_username)
  503. {
  504. free(app->variable_item_user_username);
  505. app->variable_item_user_username = NULL;
  506. }
  507. if (app->variable_item_user_password)
  508. {
  509. free(app->variable_item_user_password);
  510. app->variable_item_user_password = NULL;
  511. }
  512. }
  513. static void free_submenu_settings(void *context)
  514. {
  515. FlipWorldApp *app = (FlipWorldApp *)context;
  516. if (!app)
  517. {
  518. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  519. return;
  520. }
  521. if (app->submenu_settings)
  522. {
  523. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  524. submenu_free(app->submenu_settings);
  525. app->submenu_settings = NULL;
  526. }
  527. }
  528. static FlipWorldApp *app_instance = NULL;
  529. static FuriThreadId thread_id;
  530. static bool game_thread_running = false;
  531. void free_all_views(void *context, bool should_free_variable_item_list, bool should_free_submenu_settings)
  532. {
  533. FlipWorldApp *app = (FlipWorldApp *)context;
  534. if (!app)
  535. {
  536. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  537. return;
  538. }
  539. if (should_free_variable_item_list)
  540. {
  541. free_variable_item_list(app);
  542. }
  543. free_about_view(app);
  544. free_main_view(app);
  545. free_text_input_view(app);
  546. // free game thread
  547. if (game_thread_running)
  548. {
  549. game_thread_running = false;
  550. furi_thread_flags_set(thread_id, WorkerEvtStop);
  551. furi_thread_free(thread_id);
  552. }
  553. if (should_free_submenu_settings)
  554. free_submenu_settings(app);
  555. if (app_instance)
  556. {
  557. free(app_instance);
  558. app_instance = NULL;
  559. }
  560. }
  561. static bool flip_world_fetch_world_list(DataLoaderModel *model)
  562. {
  563. if (model->request_index == 0)
  564. {
  565. // Create the directory for saving worlds
  566. char directory_path[128];
  567. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds");
  568. // Create the directory
  569. Storage *storage = furi_record_open(RECORD_STORAGE);
  570. storage_common_mkdir(storage, directory_path);
  571. // free storage
  572. furi_record_close(RECORD_STORAGE);
  573. snprintf(
  574. fhttp.file_path,
  575. sizeof(fhttp.file_path),
  576. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  577. fhttp.save_received_data = true;
  578. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/list/10/", "{\"Content-Type\":\"application/json\"}");
  579. }
  580. else if (model->request_index == 1)
  581. {
  582. FuriString *world_list = flipper_http_load_from_file(fhttp.file_path);
  583. if (!world_list)
  584. {
  585. FURI_LOG_E(TAG, "Failed to load world list");
  586. return "Failed to load world list";
  587. }
  588. FuriString *first_world = get_json_array_value_furi("worlds", 0, world_list);
  589. if (!first_world)
  590. {
  591. FURI_LOG_E(TAG, "Failed to get first world");
  592. return "Failed to get first world";
  593. }
  594. if (world_exists(furi_string_get_cstr(first_world)))
  595. {
  596. furi_string_free(world_list);
  597. furi_string_free(first_world);
  598. FURI_LOG_I(TAG, "World already exists");
  599. fhttp.state = IDLE;
  600. model->data_state = DataStateParsed;
  601. flipper_http_deinit();
  602. // free game thread
  603. if (game_thread_running)
  604. {
  605. game_thread_running = false;
  606. furi_thread_flags_set(thread_id, WorkerEvtStop);
  607. furi_thread_free(thread_id);
  608. }
  609. // free_all_views(app_instance, true, true);
  610. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  611. if (!thread)
  612. {
  613. FURI_LOG_E(TAG, "Failed to allocate game thread");
  614. return "Failed to allocate game thread";
  615. }
  616. furi_thread_start(thread);
  617. thread_id = furi_thread_get_id(thread);
  618. game_thread_running = true;
  619. return true;
  620. }
  621. snprintf(
  622. fhttp.file_path,
  623. sizeof(fhttp.file_path),
  624. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/%s.json", furi_string_get_cstr(first_world));
  625. fhttp.save_received_data = true;
  626. char url[128];
  627. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/world/get/world/%s/", furi_string_get_cstr(first_world));
  628. return flipper_http_get_request_with_headers(url, "{\"Content-Type\":\"application/json\"}");
  629. }
  630. return false;
  631. }
  632. static char *flip_world_parse_world_list(DataLoaderModel *model)
  633. {
  634. if (model->request_index == 0)
  635. {
  636. return "World List Fetched";
  637. }
  638. else if (model->request_index == 1)
  639. {
  640. flipper_http_deinit();
  641. // free game thread
  642. if (game_thread_running)
  643. {
  644. game_thread_running = false;
  645. furi_thread_flags_set(thread_id, WorkerEvtStop);
  646. furi_thread_free(thread_id);
  647. }
  648. // free_all_views(app_instance, true, true);
  649. FuriThread *thread = furi_thread_alloc_ex("game", 1024, game_app, app_instance);
  650. if (!thread)
  651. {
  652. FURI_LOG_E(TAG, "Failed to allocate game thread");
  653. return "Failed to allocate game thread";
  654. }
  655. furi_thread_start(thread);
  656. thread_id = furi_thread_get_id(thread);
  657. game_thread_running = true;
  658. return "Thanks for playing FlipWorld!\n\n\n\nPress BACK to return if this\ndoesn't automatically close.";
  659. }
  660. return "Unknown error";
  661. }
  662. static void flip_world_switch_to_view_get_world_list(FlipWorldApp *app)
  663. {
  664. 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);
  665. }
  666. static bool flip_social_register_fetch(DataLoaderModel *model)
  667. {
  668. UNUSED(model);
  669. char username[64];
  670. char password[64];
  671. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  672. {
  673. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  674. return false;
  675. }
  676. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  677. {
  678. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  679. return false;
  680. }
  681. char payload[172];
  682. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  683. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/register/", "{\"Content-Type\":\"application/json\"}", payload);
  684. }
  685. static char *flip_social_register_parse(DataLoaderModel *model)
  686. {
  687. UNUSED(model);
  688. if (fhttp.last_response != NULL && (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User created") != NULL))
  689. {
  690. flipper_http_deinit();
  691. save_char("is_logged_in", "true");
  692. char username[64];
  693. char password[64];
  694. // load the username and password, then save them
  695. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  696. {
  697. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  698. return "Failed to load Flip-Social-Username";
  699. }
  700. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  701. {
  702. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  703. return "Failed to load Flip-Social-Password";
  704. }
  705. // load wifi ssid,pass then save
  706. char ssid[64];
  707. char pass[64];
  708. if (!load_char("WiFi-SSID", ssid, sizeof(ssid)))
  709. {
  710. FURI_LOG_E(TAG, "Failed to load WiFi-SSID");
  711. return "Failed to load WiFi-SSID";
  712. }
  713. if (!load_char("WiFi-Password", pass, sizeof(pass)))
  714. {
  715. FURI_LOG_E(TAG, "Failed to load WiFi-Password");
  716. return "Failed to load WiFi-Password";
  717. }
  718. save_settings(ssid, pass, username, password);
  719. flip_world_switch_to_view_get_world_list(app_instance);
  720. return "Account created!";
  721. }
  722. else if (strstr(fhttp.last_response, "Username or password not provided") != NULL)
  723. {
  724. flipper_http_deinit();
  725. return "Please enter your credentials.\nPress BACK to return.";
  726. }
  727. else if (strstr(fhttp.last_response, "User already exists") != NULL || strstr(fhttp.last_response, "Multiple users found") != NULL)
  728. {
  729. flipper_http_deinit();
  730. return "Registration failed...\nUsername already exists.\nPress BACK to return.";
  731. }
  732. else
  733. {
  734. flipper_http_deinit();
  735. return "Registration failed...\nUpdate your credentials.\nPress BACK to return.";
  736. }
  737. }
  738. static void flip_social_register_switch_to_view(FlipWorldApp *app)
  739. {
  740. flip_world_generic_switch_to_view(app, "Registering...", flip_social_register_fetch, flip_social_register_parse, 1, callback_to_settings, FlipWorldViewTextInput);
  741. }
  742. static bool flip_social_login_fetch(DataLoaderModel *model)
  743. {
  744. UNUSED(model);
  745. char username[64];
  746. char password[64];
  747. if (!load_char("Flip-Social-Username", username, sizeof(username)))
  748. {
  749. FURI_LOG_E(TAG, "Failed to load Flip-Social-Username");
  750. return false;
  751. }
  752. if (!load_char("Flip-Social-Password", password, sizeof(password)))
  753. {
  754. FURI_LOG_E(TAG, "Failed to load Flip-Social-Password");
  755. return false;
  756. }
  757. char payload[172];
  758. snprintf(payload, sizeof(payload), "{\"username\":\"%s\",\"password\":\"%s\"}", username, password);
  759. return flipper_http_post_request_with_headers("https://www.flipsocial.net/api/user/login/", "{\"Content-Type\":\"application/json\"}", payload);
  760. }
  761. static char *flip_social_login_parse(DataLoaderModel *model)
  762. {
  763. UNUSED(model);
  764. char returned_message[128];
  765. if (!fhttp.last_response)
  766. {
  767. snprintf(returned_message, sizeof(returned_message), "Failed to login...");
  768. }
  769. else if (strstr(fhttp.last_response, "[SUCCESS]") != NULL || strstr(fhttp.last_response, "User found") != NULL)
  770. {
  771. snprintf(returned_message, sizeof(returned_message), "Login successful!");
  772. flip_world_switch_to_view_get_world_list(app_instance);
  773. }
  774. else if (strstr(fhttp.last_response, "User not found") != NULL)
  775. {
  776. snprintf(returned_message, sizeof(returned_message), "Account not found...");
  777. // only if the warning is User not found, should we allow the user to register
  778. flip_social_register_switch_to_view(app_instance);
  779. }
  780. else
  781. {
  782. snprintf(
  783. returned_message,
  784. sizeof(returned_message),
  785. strlen(fhttp.last_response) > 127 || strlen(fhttp.last_response) == 0 ? "Failed to login..." : "%s",
  786. fhttp.last_response);
  787. }
  788. flipper_http_deinit();
  789. save_char("is_logged_in", strstr(fhttp.last_response, "[SUCCESS]") != NULL ? "true" : "false");
  790. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipWorldViewSubmenu); // just go back to the main menu for now
  791. return strstr(returned_message, "successful") != NULL ? "Login successful!" : strstr(returned_message, "found") != NULL ? "Account not found..."
  792. : "Failed to login...";
  793. }
  794. static void flip_social_login_switch_to_view(FlipWorldApp *app)
  795. {
  796. flip_world_generic_switch_to_view(app, "Logging in...", flip_social_login_fetch, flip_social_login_parse, 1, callback_to_settings, FlipWorldViewTextInput);
  797. }
  798. void callback_submenu_choices(void *context, uint32_t index)
  799. {
  800. FlipWorldApp *app = (FlipWorldApp *)context;
  801. if (!app)
  802. {
  803. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  804. return;
  805. }
  806. switch (index)
  807. {
  808. case FlipWorldSubmenuIndexRun:
  809. if (!flipper_http_init(flipper_http_rx_callback, app))
  810. {
  811. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  812. return;
  813. }
  814. app_instance = malloc(sizeof(FlipWorldApp));
  815. if (!app_instance)
  816. {
  817. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  818. return;
  819. }
  820. memcpy(app_instance, app, sizeof(FlipWorldApp));
  821. // check if logged in
  822. if (is_logged_in() || is_logged_in_to_flip_social())
  823. {
  824. flip_world_switch_to_view_get_world_list(app_instance);
  825. }
  826. else
  827. {
  828. flip_social_login_switch_to_view(app_instance);
  829. }
  830. break;
  831. case FlipWorldSubmenuIndexAbout:
  832. free_all_views(app, true, true);
  833. if (!alloc_about_view(app))
  834. {
  835. FURI_LOG_E(TAG, "Failed to allocate about view");
  836. return;
  837. }
  838. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  839. break;
  840. case FlipWorldSubmenuIndexSettings:
  841. free_all_views(app, true, true);
  842. if (!alloc_submenu_settings(app))
  843. {
  844. FURI_LOG_E(TAG, "Failed to allocate settings view");
  845. return;
  846. }
  847. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  848. break;
  849. case FlipWorldSubmenuIndexWiFiSettings:
  850. free_all_views(app, true, false);
  851. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexWiFiSettings))
  852. {
  853. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  854. return;
  855. }
  856. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  857. break;
  858. case FlipWorldSubmenuIndexGameSettings:
  859. free_all_views(app, true, false);
  860. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexGameSettings))
  861. {
  862. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  863. return;
  864. }
  865. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  866. break;
  867. case FlipWorldSubmenuIndexUserSettings:
  868. free_all_views(app, true, false);
  869. if (!alloc_variable_item_list(app, FlipWorldSubmenuIndexUserSettings))
  870. {
  871. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  872. return;
  873. }
  874. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  875. break;
  876. default:
  877. break;
  878. }
  879. }
  880. static void text_updated_wifi_ssid(void *context)
  881. {
  882. FlipWorldApp *app = (FlipWorldApp *)context;
  883. if (!app)
  884. {
  885. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  886. return;
  887. }
  888. // store the entered text
  889. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  890. // Ensure null-termination
  891. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  892. // save the setting
  893. save_char("WiFi-SSID", app->text_input_buffer);
  894. // update the variable item text
  895. if (app->variable_item_wifi_ssid)
  896. {
  897. variable_item_set_current_value_text(app->variable_item_wifi_ssid, app->text_input_buffer);
  898. // get value of password
  899. char pass[64];
  900. char username[64];
  901. char password[64];
  902. if (load_char("WiFi-Password", pass, sizeof(pass)))
  903. {
  904. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  905. {
  906. // save the settings
  907. load_char("Flip-Social-Username", username, sizeof(username));
  908. load_char("Flip-Social-Password", password, sizeof(password));
  909. save_settings(app->text_input_buffer, pass, username, password);
  910. // initialize the http
  911. if (flipper_http_init(flipper_http_rx_callback, app))
  912. {
  913. // save the wifi if the device is connected
  914. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  915. {
  916. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  917. }
  918. // free the resources
  919. flipper_http_deinit();
  920. }
  921. else
  922. {
  923. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  924. }
  925. }
  926. }
  927. }
  928. // switch to the settings view
  929. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  930. }
  931. static void text_updated_wifi_pass(void *context)
  932. {
  933. FlipWorldApp *app = (FlipWorldApp *)context;
  934. if (!app)
  935. {
  936. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  937. return;
  938. }
  939. // store the entered text
  940. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  941. // Ensure null-termination
  942. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  943. // save the setting
  944. save_char("WiFi-Password", app->text_input_buffer);
  945. // update the variable item text
  946. if (app->variable_item_wifi_pass)
  947. {
  948. // variable_item_set_current_value_text(app->variable_item_wifi_pass, app->text_input_buffer);
  949. }
  950. // get value of ssid
  951. char ssid[64];
  952. char username[64];
  953. char password[64];
  954. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  955. {
  956. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  957. {
  958. // save the settings
  959. load_char("Flip-Social-Username", username, sizeof(username));
  960. load_char("Flip-Social-Password", password, sizeof(password));
  961. save_settings(ssid, app->text_input_buffer, username, password);
  962. // initialize the http
  963. if (flipper_http_init(flipper_http_rx_callback, app))
  964. {
  965. // save the wifi if the device is connected
  966. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  967. {
  968. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  969. }
  970. // free the resources
  971. flipper_http_deinit();
  972. }
  973. else
  974. {
  975. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  976. }
  977. }
  978. }
  979. // switch to the settings view
  980. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList);
  981. }
  982. static void text_updated_username(void *context)
  983. {
  984. FlipWorldApp *app = (FlipWorldApp *)context;
  985. if (!app)
  986. {
  987. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  988. return;
  989. }
  990. // store the entered text
  991. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  992. // Ensure null-termination
  993. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  994. // save the setting
  995. save_char("Flip-Social-Username", app->text_input_buffer);
  996. // update the variable item text
  997. if (app->variable_item_user_username)
  998. {
  999. variable_item_set_current_value_text(app->variable_item_user_username, app->text_input_buffer);
  1000. }
  1001. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1002. }
  1003. static void text_updated_password(void *context)
  1004. {
  1005. FlipWorldApp *app = (FlipWorldApp *)context;
  1006. if (!app)
  1007. {
  1008. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1009. return;
  1010. }
  1011. // store the entered text
  1012. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  1013. // Ensure null-termination
  1014. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  1015. // save the setting
  1016. save_char("Flip-Social-Password", app->text_input_buffer);
  1017. // update the variable item text
  1018. if (app->variable_item_user_password)
  1019. {
  1020. variable_item_set_current_value_text(app->variable_item_user_password, app->text_input_buffer);
  1021. }
  1022. // get value of username
  1023. char username[64];
  1024. char ssid[64];
  1025. char pass[64];
  1026. if (load_char("Flip-Social-Username", username, sizeof(username)))
  1027. {
  1028. if (strlen(username) > 0 && strlen(app->text_input_buffer) > 0)
  1029. {
  1030. // save the settings
  1031. load_char("WiFi-SSID", ssid, sizeof(ssid));
  1032. load_char("WiFi-Password", pass, sizeof(pass));
  1033. save_settings(ssid, pass, username, app->text_input_buffer);
  1034. }
  1035. }
  1036. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewVariableItemList); // back to user settings
  1037. }
  1038. static void wifi_settings_item_selected(void *context, uint32_t index)
  1039. {
  1040. FlipWorldApp *app = (FlipWorldApp *)context;
  1041. if (!app)
  1042. {
  1043. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1044. return;
  1045. }
  1046. char ssid[64];
  1047. char pass[64];
  1048. char username[64];
  1049. char password[64];
  1050. switch (index)
  1051. {
  1052. case 0: // Input SSID
  1053. free_all_views(app, false, false);
  1054. if (!alloc_text_input_view(app, "SSID"))
  1055. {
  1056. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1057. return;
  1058. }
  1059. // load SSID
  1060. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1061. {
  1062. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  1063. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1064. }
  1065. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1066. break;
  1067. case 1: // Input Password
  1068. free_all_views(app, false, false);
  1069. if (!alloc_text_input_view(app, "Password"))
  1070. {
  1071. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1072. return;
  1073. }
  1074. // load password
  1075. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass), username, sizeof(username), password, sizeof(password)))
  1076. {
  1077. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  1078. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  1079. }
  1080. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1081. break;
  1082. default:
  1083. FURI_LOG_E(TAG, "Unknown configuration item index");
  1084. break;
  1085. }
  1086. }
  1087. static void flip_world_game_fps_change(VariableItem *item)
  1088. {
  1089. uint8_t index = variable_item_get_current_value_index(item);
  1090. game_fps_index = index;
  1091. variable_item_set_current_value_text(item, game_fps_choices[index]);
  1092. // save the fps
  1093. save_char("Game-FPS", game_fps_choices[index]);
  1094. }
  1095. static void flip_world_game_screen_always_on_change(VariableItem *item)
  1096. {
  1097. uint8_t index = variable_item_get_current_value_index(item);
  1098. variable_item_set_current_value_text(item, game_screen_always_on_choices[index]);
  1099. // save the screen always on
  1100. save_char("Game-Screen-Always-On", game_screen_always_on_choices[index]);
  1101. }
  1102. static bool flip_world_fetch_worlds(DataLoaderModel *model)
  1103. {
  1104. UNUSED(model);
  1105. snprintf(
  1106. fhttp.file_path,
  1107. sizeof(fhttp.file_path),
  1108. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds.json");
  1109. fhttp.save_received_data = true;
  1110. return flipper_http_get_request_with_headers("https://www.flipsocial.net/api/world/get/10/", "{\"Content-Type\":\"application/json\"}");
  1111. }
  1112. static char *flip_world_parse_worlds(DataLoaderModel *model)
  1113. {
  1114. UNUSED(model);
  1115. flipper_http_deinit();
  1116. return "World Pack Installed";
  1117. }
  1118. static void flip_world_switch_to_view_get_worlds(FlipWorldApp *app)
  1119. {
  1120. flip_world_generic_switch_to_view(app, "Fetching World Pack..", flip_world_fetch_worlds, flip_world_parse_worlds, 1, callback_to_submenu, FlipWorldViewLoader);
  1121. }
  1122. static void game_settings_item_selected(void *context, uint32_t index)
  1123. {
  1124. FlipWorldApp *app = (FlipWorldApp *)context;
  1125. if (!app)
  1126. {
  1127. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1128. return;
  1129. }
  1130. switch (index)
  1131. {
  1132. case 0: // Download Worlds
  1133. if (!flipper_http_init(flipper_http_rx_callback, app))
  1134. {
  1135. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  1136. return;
  1137. }
  1138. flip_world_switch_to_view_get_worlds(app);
  1139. case 1: // Change FPS
  1140. break;
  1141. case 2: // Screen Always On
  1142. break;
  1143. }
  1144. }
  1145. static void user_settings_item_selected(void *context, uint32_t index)
  1146. {
  1147. FlipWorldApp *app = (FlipWorldApp *)context;
  1148. if (!app)
  1149. {
  1150. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  1151. return;
  1152. }
  1153. switch (index)
  1154. {
  1155. case 0: // Username
  1156. free_all_views(app, false, false);
  1157. if (!alloc_text_input_view(app, "Username"))
  1158. {
  1159. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1160. return;
  1161. }
  1162. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1163. break;
  1164. case 1: // Password
  1165. free_all_views(app, false, false);
  1166. if (!alloc_text_input_view(app, "Password"))
  1167. {
  1168. FURI_LOG_E(TAG, "Failed to allocate text input view");
  1169. return;
  1170. }
  1171. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  1172. break;
  1173. }
  1174. }
  1175. static void flip_world_widget_set_text(char *message, Widget **widget)
  1176. {
  1177. if (widget == NULL)
  1178. {
  1179. FURI_LOG_E(TAG, "flip_world_set_widget_text - widget is NULL");
  1180. DEV_CRASH();
  1181. return;
  1182. }
  1183. if (message == NULL)
  1184. {
  1185. FURI_LOG_E(TAG, "flip_world_set_widget_text - message is NULL");
  1186. DEV_CRASH();
  1187. return;
  1188. }
  1189. widget_reset(*widget);
  1190. uint32_t message_length = strlen(message); // Length of the message
  1191. uint32_t i = 0; // Index tracker
  1192. uint32_t formatted_index = 0; // Tracker for where we are in the formatted message
  1193. char *formatted_message; // Buffer to hold the final formatted message
  1194. // Allocate buffer with double the message length plus one for safety
  1195. if (!easy_flipper_set_buffer(&formatted_message, message_length * 2 + 1))
  1196. {
  1197. return;
  1198. }
  1199. while (i < message_length)
  1200. {
  1201. uint32_t max_line_length = 31; // Maximum characters per line
  1202. uint32_t remaining_length = message_length - i; // Remaining characters
  1203. uint32_t line_length = (remaining_length < max_line_length) ? remaining_length : max_line_length;
  1204. // Check for newline character within the current segment
  1205. uint32_t newline_pos = i;
  1206. bool found_newline = false;
  1207. for (; newline_pos < i + line_length && newline_pos < message_length; newline_pos++)
  1208. {
  1209. if (message[newline_pos] == '\n')
  1210. {
  1211. found_newline = true;
  1212. break;
  1213. }
  1214. }
  1215. if (found_newline)
  1216. {
  1217. // If newline found, set line_length up to the newline
  1218. line_length = newline_pos - i;
  1219. }
  1220. // Temporary buffer to hold the current line
  1221. char line[32];
  1222. strncpy(line, message + i, line_length);
  1223. line[line_length] = '\0';
  1224. // If newline was found, skip it for the next iteration
  1225. if (found_newline)
  1226. {
  1227. i += line_length + 1; // +1 to skip the '\n' character
  1228. }
  1229. else
  1230. {
  1231. // Check if the line ends in the middle of a word and adjust accordingly
  1232. if (line_length == max_line_length && message[i + line_length] != '\0' && message[i + line_length] != ' ')
  1233. {
  1234. // Find the last space within the current line to avoid breaking a word
  1235. char *last_space = strrchr(line, ' ');
  1236. if (last_space != NULL)
  1237. {
  1238. // Adjust the line_length to avoid cutting the word
  1239. line_length = last_space - line;
  1240. line[line_length] = '\0'; // Null-terminate at the space
  1241. }
  1242. }
  1243. // Move the index forward by the determined line_length
  1244. i += line_length;
  1245. // Skip any spaces at the beginning of the next line
  1246. while (i < message_length && message[i] == ' ')
  1247. {
  1248. i++;
  1249. }
  1250. }
  1251. // Manually copy the fixed line into the formatted_message buffer
  1252. for (uint32_t j = 0; j < line_length; j++)
  1253. {
  1254. formatted_message[formatted_index++] = line[j];
  1255. }
  1256. // Add a newline character for line spacing
  1257. formatted_message[formatted_index++] = '\n';
  1258. }
  1259. // Null-terminate the formatted_message
  1260. formatted_message[formatted_index] = '\0';
  1261. // Add the formatted message to the widget
  1262. widget_add_text_scroll_element(*widget, 0, 0, 128, 64, formatted_message);
  1263. }
  1264. void flip_world_loader_draw_callback(Canvas *canvas, void *model)
  1265. {
  1266. if (!canvas || !model)
  1267. {
  1268. FURI_LOG_E(TAG, "flip_world_loader_draw_callback - canvas or model is NULL");
  1269. return;
  1270. }
  1271. SerialState http_state = fhttp.state;
  1272. DataLoaderModel *data_loader_model = (DataLoaderModel *)model;
  1273. DataState data_state = data_loader_model->data_state;
  1274. char *title = data_loader_model->title;
  1275. canvas_set_font(canvas, FontSecondary);
  1276. if (http_state == INACTIVE)
  1277. {
  1278. canvas_draw_str(canvas, 0, 7, "Wifi Dev Board disconnected.");
  1279. canvas_draw_str(canvas, 0, 17, "Please connect to the board.");
  1280. canvas_draw_str(canvas, 0, 32, "If your board is connected,");
  1281. canvas_draw_str(canvas, 0, 42, "make sure you have flashed");
  1282. canvas_draw_str(canvas, 0, 52, "your WiFi Devboard with the");
  1283. canvas_draw_str(canvas, 0, 62, "latest FlipperHTTP flash.");
  1284. return;
  1285. }
  1286. if (data_state == DataStateError || data_state == DataStateParseError)
  1287. {
  1288. flip_world_request_error_draw(canvas);
  1289. return;
  1290. }
  1291. canvas_draw_str(canvas, 0, 7, title);
  1292. canvas_draw_str(canvas, 0, 17, "Loading...");
  1293. if (data_state == DataStateInitial)
  1294. {
  1295. return;
  1296. }
  1297. if (http_state == SENDING)
  1298. {
  1299. canvas_draw_str(canvas, 0, 27, "Fetching...");
  1300. return;
  1301. }
  1302. if (http_state == RECEIVING || data_state == DataStateRequested)
  1303. {
  1304. canvas_draw_str(canvas, 0, 27, "Receiving...");
  1305. return;
  1306. }
  1307. if (http_state == IDLE && data_state == DataStateReceived)
  1308. {
  1309. canvas_draw_str(canvas, 0, 27, "Processing...");
  1310. return;
  1311. }
  1312. if (http_state == IDLE && data_state == DataStateParsed)
  1313. {
  1314. canvas_draw_str(canvas, 0, 27, "Processed...");
  1315. return;
  1316. }
  1317. }
  1318. static void flip_world_loader_process_callback(void *context)
  1319. {
  1320. if (context == NULL)
  1321. {
  1322. FURI_LOG_E(TAG, "flip_world_loader_process_callback - context is NULL");
  1323. DEV_CRASH();
  1324. return;
  1325. }
  1326. FlipWorldApp *app = (FlipWorldApp *)context;
  1327. View *view = app->view_loader;
  1328. DataState current_data_state;
  1329. with_view_model(view, DataLoaderModel * model, { current_data_state = model->data_state; }, false);
  1330. if (current_data_state == DataStateInitial)
  1331. {
  1332. with_view_model(
  1333. view,
  1334. DataLoaderModel * model,
  1335. {
  1336. model->data_state = DataStateRequested;
  1337. DataLoaderFetch fetch = model->fetcher;
  1338. if (fetch == NULL)
  1339. {
  1340. FURI_LOG_E(TAG, "Model doesn't have Fetch function assigned.");
  1341. model->data_state = DataStateError;
  1342. return;
  1343. }
  1344. // Clear any previous responses
  1345. strncpy(fhttp.last_response, "", 1);
  1346. bool request_status = fetch(model);
  1347. if (!request_status)
  1348. {
  1349. model->data_state = DataStateError;
  1350. }
  1351. },
  1352. true);
  1353. }
  1354. else if (current_data_state == DataStateRequested || current_data_state == DataStateError)
  1355. {
  1356. if (fhttp.state == IDLE && fhttp.last_response != NULL)
  1357. {
  1358. if (strstr(fhttp.last_response, "[PONG]") != NULL)
  1359. {
  1360. FURI_LOG_DEV(TAG, "PONG received.");
  1361. }
  1362. else if (strncmp(fhttp.last_response, "[SUCCESS]", 9) == 0)
  1363. {
  1364. FURI_LOG_DEV(TAG, "SUCCESS received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1365. }
  1366. else if (strncmp(fhttp.last_response, "[ERROR]", 9) == 0)
  1367. {
  1368. FURI_LOG_DEV(TAG, "ERROR received. %s", fhttp.last_response ? fhttp.last_response : "NULL");
  1369. }
  1370. else if (strlen(fhttp.last_response) == 0)
  1371. {
  1372. // Still waiting on response
  1373. }
  1374. else
  1375. {
  1376. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateReceived; }, true);
  1377. }
  1378. }
  1379. else if (fhttp.state == SENDING || fhttp.state == RECEIVING)
  1380. {
  1381. // continue waiting
  1382. }
  1383. else if (fhttp.state == INACTIVE)
  1384. {
  1385. // inactive. try again
  1386. }
  1387. else if (fhttp.state == ISSUE)
  1388. {
  1389. with_view_model(view, DataLoaderModel * model, { model->data_state = DataStateError; }, true);
  1390. }
  1391. else
  1392. {
  1393. FURI_LOG_DEV(TAG, "Unexpected state: %d lastresp: %s", fhttp.state, fhttp.last_response ? fhttp.last_response : "NULL");
  1394. DEV_CRASH();
  1395. }
  1396. }
  1397. else if (current_data_state == DataStateReceived)
  1398. {
  1399. with_view_model(
  1400. view,
  1401. DataLoaderModel * model,
  1402. {
  1403. char *data_text;
  1404. if (model->parser == NULL)
  1405. {
  1406. data_text = NULL;
  1407. FURI_LOG_DEV(TAG, "Parser is NULL");
  1408. DEV_CRASH();
  1409. }
  1410. else
  1411. {
  1412. data_text = model->parser(model);
  1413. }
  1414. FURI_LOG_DEV(TAG, "Parsed data: %s\r\ntext: %s", fhttp.last_response ? fhttp.last_response : "NULL", data_text ? data_text : "NULL");
  1415. model->data_text = data_text;
  1416. if (data_text == NULL)
  1417. {
  1418. model->data_state = DataStateParseError;
  1419. }
  1420. else
  1421. {
  1422. model->data_state = DataStateParsed;
  1423. }
  1424. },
  1425. true);
  1426. }
  1427. else if (current_data_state == DataStateParsed)
  1428. {
  1429. with_view_model(
  1430. view,
  1431. DataLoaderModel * model,
  1432. {
  1433. if (++model->request_index < model->request_count)
  1434. {
  1435. model->data_state = DataStateInitial;
  1436. }
  1437. else
  1438. {
  1439. flip_world_widget_set_text(model->data_text != NULL ? model->data_text : "", &app->widget_result);
  1440. if (model->data_text != NULL)
  1441. {
  1442. free(model->data_text);
  1443. model->data_text = NULL;
  1444. }
  1445. view_set_previous_callback(widget_get_view(app->widget_result), model->back_callback);
  1446. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewWidgetResult);
  1447. }
  1448. },
  1449. true);
  1450. }
  1451. }
  1452. static void flip_world_loader_timer_callback(void *context)
  1453. {
  1454. if (context == NULL)
  1455. {
  1456. FURI_LOG_E(TAG, "flip_world_loader_timer_callback - context is NULL");
  1457. DEV_CRASH();
  1458. return;
  1459. }
  1460. FlipWorldApp *app = (FlipWorldApp *)context;
  1461. view_dispatcher_send_custom_event(app->view_dispatcher, FlipWorldCustomEventProcess);
  1462. }
  1463. static void flip_world_loader_on_enter(void *context)
  1464. {
  1465. if (context == NULL)
  1466. {
  1467. FURI_LOG_E(TAG, "flip_world_loader_on_enter - context is NULL");
  1468. DEV_CRASH();
  1469. return;
  1470. }
  1471. FlipWorldApp *app = (FlipWorldApp *)context;
  1472. View *view = app->view_loader;
  1473. with_view_model(
  1474. view,
  1475. DataLoaderModel * model,
  1476. {
  1477. view_set_previous_callback(view, model->back_callback);
  1478. if (model->timer == NULL)
  1479. {
  1480. model->timer = furi_timer_alloc(flip_world_loader_timer_callback, FuriTimerTypePeriodic, app);
  1481. }
  1482. furi_timer_start(model->timer, 250);
  1483. },
  1484. true);
  1485. }
  1486. static void flip_world_loader_on_exit(void *context)
  1487. {
  1488. if (context == NULL)
  1489. {
  1490. FURI_LOG_E(TAG, "flip_world_loader_on_exit - context is NULL");
  1491. DEV_CRASH();
  1492. return;
  1493. }
  1494. FlipWorldApp *app = (FlipWorldApp *)context;
  1495. View *view = app->view_loader;
  1496. with_view_model(
  1497. view,
  1498. DataLoaderModel * model,
  1499. {
  1500. if (model->timer)
  1501. {
  1502. furi_timer_stop(model->timer);
  1503. }
  1504. },
  1505. false);
  1506. }
  1507. void flip_world_loader_init(View *view)
  1508. {
  1509. if (view == NULL)
  1510. {
  1511. FURI_LOG_E(TAG, "flip_world_loader_init - view is NULL");
  1512. DEV_CRASH();
  1513. return;
  1514. }
  1515. view_allocate_model(view, ViewModelTypeLocking, sizeof(DataLoaderModel));
  1516. view_set_enter_callback(view, flip_world_loader_on_enter);
  1517. view_set_exit_callback(view, flip_world_loader_on_exit);
  1518. }
  1519. void flip_world_loader_free_model(View *view)
  1520. {
  1521. if (view == NULL)
  1522. {
  1523. FURI_LOG_E(TAG, "flip_world_loader_free_model - view is NULL");
  1524. DEV_CRASH();
  1525. return;
  1526. }
  1527. with_view_model(
  1528. view,
  1529. DataLoaderModel * model,
  1530. {
  1531. if (model->timer)
  1532. {
  1533. furi_timer_free(model->timer);
  1534. model->timer = NULL;
  1535. }
  1536. if (model->parser_context)
  1537. {
  1538. free(model->parser_context);
  1539. model->parser_context = NULL;
  1540. }
  1541. },
  1542. false);
  1543. }
  1544. bool flip_world_custom_event_callback(void *context, uint32_t index)
  1545. {
  1546. if (context == NULL)
  1547. {
  1548. FURI_LOG_E(TAG, "flip_world_custom_event_callback - context is NULL");
  1549. DEV_CRASH();
  1550. return false;
  1551. }
  1552. switch (index)
  1553. {
  1554. case FlipWorldCustomEventProcess:
  1555. flip_world_loader_process_callback(context);
  1556. return true;
  1557. default:
  1558. FURI_LOG_DEV(TAG, "flip_world_custom_event_callback. Unknown index: %ld", index);
  1559. return false;
  1560. }
  1561. }
  1562. 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)
  1563. {
  1564. if (app == NULL)
  1565. {
  1566. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - app is NULL");
  1567. DEV_CRASH();
  1568. return;
  1569. }
  1570. View *view = app->view_loader;
  1571. if (view == NULL)
  1572. {
  1573. FURI_LOG_E(TAG, "flip_world_generic_switch_to_view - view is NULL");
  1574. DEV_CRASH();
  1575. return;
  1576. }
  1577. with_view_model(
  1578. view,
  1579. DataLoaderModel * model,
  1580. {
  1581. model->title = title;
  1582. model->fetcher = fetcher;
  1583. model->parser = parser;
  1584. model->request_index = 0;
  1585. model->request_count = request_count;
  1586. model->back_callback = back;
  1587. model->data_state = DataStateInitial;
  1588. model->data_text = NULL;
  1589. },
  1590. true);
  1591. view_dispatcher_switch_to_view(app->view_dispatcher, view_id);
  1592. }