callback.c 61 KB

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