callback.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include <callback/callback.h>
  2. static void frame_cb(GameEngine *engine, Canvas *canvas, InputState input, void *context)
  3. {
  4. UNUSED(engine);
  5. GameManager *game_manager = context;
  6. game_manager_input_set(game_manager, input);
  7. game_manager_update(game_manager);
  8. game_manager_render(game_manager, canvas);
  9. }
  10. int32_t game_app(void *p)
  11. {
  12. UNUSED(p);
  13. GameManager *game_manager = game_manager_alloc();
  14. if (!game_manager)
  15. {
  16. FURI_LOG_E("Game", "Failed to allocate game manager");
  17. return -1;
  18. }
  19. GameEngineSettings settings = game_engine_settings_init();
  20. settings.target_fps = game.target_fps;
  21. settings.show_fps = game.show_fps;
  22. settings.always_backlight = game.always_backlight;
  23. settings.frame_callback = frame_cb;
  24. settings.context = game_manager;
  25. GameEngine *engine = game_engine_alloc(settings);
  26. if (!engine)
  27. {
  28. FURI_LOG_E("Game", "Failed to allocate game engine");
  29. game_manager_free(game_manager);
  30. return -1;
  31. }
  32. game_manager_engine_set(game_manager, engine);
  33. void *game_context = NULL;
  34. if (game.context_size > 0)
  35. {
  36. game_context = malloc(game.context_size);
  37. game_manager_game_context_set(game_manager, game_context);
  38. }
  39. game.start(game_manager, game_context);
  40. game_engine_run(engine);
  41. game_engine_free(engine);
  42. game_manager_free(game_manager);
  43. game.stop(game_context);
  44. if (game_context)
  45. {
  46. free(game_context);
  47. }
  48. int32_t entities = entities_get_count();
  49. if (entities != 0)
  50. {
  51. FURI_LOG_E("Game", "Memory leak detected: %ld entities still allocated", entities);
  52. return -1;
  53. }
  54. return 0;
  55. }
  56. static bool alloc_about_view(void *context);
  57. static bool alloc_text_input_view(void *context, char *title);
  58. static bool alloc_variable_item_list(void *context);
  59. //
  60. static void settings_item_selected(void *context, uint32_t index);
  61. static void text_updated_ssid(void *context);
  62. static void text_updated_pass(void *context);
  63. static uint32_t callback_to_submenu(void *context)
  64. {
  65. UNUSED(context);
  66. return FlipWorldViewSubmenu;
  67. }
  68. static uint32_t callback_to_wifi_settings(void *context)
  69. {
  70. UNUSED(context);
  71. return FlipWorldViewSettings;
  72. }
  73. static void flip_world_view_about_draw_callback(Canvas *canvas, void *model)
  74. {
  75. UNUSED(model);
  76. canvas_clear(canvas);
  77. canvas_set_font_custom(canvas, FONT_SIZE_XLARGE);
  78. canvas_draw_str(canvas, 0, 10, VERSION_TAG);
  79. canvas_set_font_custom(canvas, FONT_SIZE_MEDIUM);
  80. canvas_draw_str(canvas, 0, 20, "- @JBlanked @codeallnight");
  81. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  82. canvas_draw_str(canvas, 0, 30, "- github.com/JBlanked/FlipWorld");
  83. canvas_draw_str_multi(canvas, 0, 55, "The first open world multiplayer\ngame on the Flipper Zero.");
  84. }
  85. // alloc
  86. static bool alloc_about_view(void *context)
  87. {
  88. FlipWorldApp *app = (FlipWorldApp *)context;
  89. if (!app)
  90. {
  91. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  92. return false;
  93. }
  94. if (!app->view_about)
  95. {
  96. if (!easy_flipper_set_view(&app->view_about, FlipWorldViewAbout, flip_world_view_about_draw_callback, NULL, callback_to_submenu, &app->view_dispatcher, app))
  97. {
  98. return false;
  99. }
  100. if (!app->view_about)
  101. {
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. static bool alloc_text_input_view(void *context, char *title)
  108. {
  109. FlipWorldApp *app = (FlipWorldApp *)context;
  110. if (!app)
  111. {
  112. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  113. return false;
  114. }
  115. if (!title)
  116. {
  117. FURI_LOG_E(TAG, "Title is NULL");
  118. return false;
  119. }
  120. app->text_input_buffer_size = 64;
  121. if (!app->text_input_buffer)
  122. {
  123. if (!easy_flipper_set_buffer(&app->text_input_buffer, app->text_input_buffer_size))
  124. {
  125. return false;
  126. }
  127. }
  128. if (!app->text_input_temp_buffer)
  129. {
  130. if (!easy_flipper_set_buffer(&app->text_input_temp_buffer, app->text_input_buffer_size))
  131. {
  132. return false;
  133. }
  134. }
  135. if (!app->text_input)
  136. {
  137. if (!easy_flipper_set_uart_text_input(
  138. &app->text_input,
  139. FlipWorldViewTextInput,
  140. title,
  141. app->text_input_temp_buffer,
  142. app->text_input_buffer_size,
  143. strcmp(title, "SSID") == 0 ? text_updated_ssid : text_updated_pass,
  144. callback_to_wifi_settings,
  145. &app->view_dispatcher,
  146. app))
  147. {
  148. return false;
  149. }
  150. if (!app->text_input)
  151. {
  152. return false;
  153. }
  154. char ssid[64];
  155. char pass[64];
  156. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  157. {
  158. if (strcmp(title, "SSID") == 0)
  159. {
  160. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size);
  161. }
  162. else
  163. {
  164. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size);
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. static bool alloc_variable_item_list(void *context)
  171. {
  172. FlipWorldApp *app = (FlipWorldApp *)context;
  173. if (!app)
  174. {
  175. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  176. return false;
  177. }
  178. if (!app->variable_item_list)
  179. {
  180. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  181. return false;
  182. if (!app->variable_item_list)
  183. return false;
  184. if (!app->variable_item_ssid)
  185. {
  186. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  187. variable_item_set_current_value_text(app->variable_item_ssid, "");
  188. }
  189. if (!app->variable_item_pass)
  190. {
  191. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  192. variable_item_set_current_value_text(app->variable_item_pass, "");
  193. }
  194. char ssid[64];
  195. char pass[64];
  196. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  197. {
  198. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  199. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  200. save_char("WiFi-SSID", ssid);
  201. save_char("WiFi-Password", pass);
  202. }
  203. }
  204. return true;
  205. }
  206. // free
  207. static void free_about_view(void *context)
  208. {
  209. FlipWorldApp *app = (FlipWorldApp *)context;
  210. if (!app)
  211. {
  212. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  213. return;
  214. }
  215. if (app->view_about)
  216. {
  217. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  218. view_free(app->view_about);
  219. app->view_about = NULL;
  220. }
  221. }
  222. static void free_main_view(void *context)
  223. {
  224. FlipWorldApp *app = (FlipWorldApp *)context;
  225. if (!app)
  226. {
  227. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  228. return;
  229. }
  230. if (app->view_main)
  231. {
  232. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  233. view_free(app->view_main);
  234. app->view_main = NULL;
  235. }
  236. }
  237. static void free_text_input_view(void *context)
  238. {
  239. FlipWorldApp *app = (FlipWorldApp *)context;
  240. if (!app)
  241. {
  242. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  243. return;
  244. }
  245. if (app->text_input)
  246. {
  247. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  248. uart_text_input_free(app->text_input);
  249. app->text_input = NULL;
  250. }
  251. if (app->text_input_buffer)
  252. {
  253. free(app->text_input_buffer);
  254. app->text_input_buffer = NULL;
  255. }
  256. if (app->text_input_temp_buffer)
  257. {
  258. free(app->text_input_temp_buffer);
  259. app->text_input_temp_buffer = NULL;
  260. }
  261. }
  262. static void free_variable_item_list(void *context)
  263. {
  264. FlipWorldApp *app = (FlipWorldApp *)context;
  265. if (!app)
  266. {
  267. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  268. return;
  269. }
  270. if (app->variable_item_list)
  271. {
  272. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  273. variable_item_list_free(app->variable_item_list);
  274. app->variable_item_list = NULL;
  275. }
  276. if (app->variable_item_ssid)
  277. {
  278. free(app->variable_item_ssid);
  279. app->variable_item_ssid = NULL;
  280. }
  281. if (app->variable_item_pass)
  282. {
  283. free(app->variable_item_pass);
  284. app->variable_item_pass = NULL;
  285. }
  286. }
  287. static FuriThreadId thread_id;
  288. static bool game_thread_running = false;
  289. void free_all_views(void *context, bool should_free_variable_item_list)
  290. {
  291. FlipWorldApp *app = (FlipWorldApp *)context;
  292. if (!app)
  293. {
  294. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  295. return;
  296. }
  297. if (should_free_variable_item_list)
  298. {
  299. free_variable_item_list(app);
  300. }
  301. free_about_view(app);
  302. free_main_view(app);
  303. free_text_input_view(app);
  304. if (app->view_main)
  305. {
  306. view_free(app->view_main);
  307. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  308. app->view_main = NULL;
  309. }
  310. // free game thread
  311. if (game_thread_running)
  312. {
  313. game_thread_running = false;
  314. furi_thread_flags_set(thread_id, WorkerEvtStop);
  315. furi_thread_free(thread_id);
  316. }
  317. }
  318. void callback_submenu_choices(void *context, uint32_t index)
  319. {
  320. FlipWorldApp *app = (FlipWorldApp *)context;
  321. if (!app)
  322. {
  323. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  324. return;
  325. }
  326. switch (index)
  327. {
  328. case FlipWorldSubmenuIndexRun:
  329. // free game thread
  330. if (game_thread_running)
  331. {
  332. game_thread_running = false;
  333. furi_thread_flags_set(thread_id, WorkerEvtStop);
  334. furi_thread_free(thread_id);
  335. }
  336. free_all_views(app, true);
  337. if (!app->view_main)
  338. {
  339. if (!easy_flipper_set_view(&app->view_main, FlipWorldViewMain, NULL, NULL, callback_to_submenu, &app->view_dispatcher, app))
  340. {
  341. return;
  342. }
  343. if (!app->view_main)
  344. {
  345. return;
  346. }
  347. }
  348. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMain);
  349. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app);
  350. if (!thread)
  351. {
  352. FURI_LOG_E(TAG, "Failed to allocate game thread");
  353. return;
  354. }
  355. furi_thread_start(thread);
  356. thread_id = furi_thread_get_id(thread);
  357. game_thread_running = true;
  358. break;
  359. case FlipWorldSubmenuIndexAbout:
  360. free_all_views(app, true);
  361. if (!alloc_about_view(app))
  362. {
  363. FURI_LOG_E(TAG, "Failed to allocate about view");
  364. return;
  365. }
  366. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  367. break;
  368. case FlipWorldSubmenuIndexSettings:
  369. free_all_views(app, true);
  370. if (!alloc_variable_item_list(app))
  371. {
  372. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  373. return;
  374. }
  375. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. static void text_updated_ssid(void *context)
  382. {
  383. FlipWorldApp *app = (FlipWorldApp *)context;
  384. if (!app)
  385. {
  386. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  387. return;
  388. }
  389. // store the entered text
  390. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  391. // Ensure null-termination
  392. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  393. // save the setting
  394. save_char("WiFi-SSID", app->text_input_buffer);
  395. // update the variable item text
  396. if (app->variable_item_ssid)
  397. {
  398. variable_item_set_current_value_text(app->variable_item_ssid, app->text_input_buffer);
  399. // get value of password
  400. char pass[64];
  401. if (load_char("WiFi-Password", pass, sizeof(pass)))
  402. {
  403. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  404. {
  405. // save the settings
  406. save_settings(app->text_input_buffer, pass);
  407. // initialize the http
  408. if (flipper_http_init(flipper_http_rx_callback, app))
  409. {
  410. // save the wifi if the device is connected
  411. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  412. {
  413. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  414. }
  415. // free the resources
  416. flipper_http_deinit();
  417. }
  418. else
  419. {
  420. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  421. }
  422. }
  423. }
  424. }
  425. // switch to the settings view
  426. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  427. }
  428. static void text_updated_pass(void *context)
  429. {
  430. FlipWorldApp *app = (FlipWorldApp *)context;
  431. if (!app)
  432. {
  433. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  434. return;
  435. }
  436. // store the entered text
  437. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  438. // Ensure null-termination
  439. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  440. // save the setting
  441. save_char("WiFi-Password", app->text_input_buffer);
  442. // update the variable item text
  443. if (app->variable_item_pass)
  444. {
  445. // variable_item_set_current_value_text(app->variable_item_pass, app->text_input_buffer);
  446. }
  447. // get value of ssid
  448. char ssid[64];
  449. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  450. {
  451. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  452. {
  453. // save the settings
  454. save_settings(ssid, app->text_input_buffer);
  455. // initialize the http
  456. if (flipper_http_init(flipper_http_rx_callback, app))
  457. {
  458. // save the wifi if the device is connected
  459. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  460. {
  461. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  462. }
  463. // free the resources
  464. flipper_http_deinit();
  465. }
  466. else
  467. {
  468. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  469. }
  470. }
  471. }
  472. // switch to the settings view
  473. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  474. }
  475. static void settings_item_selected(void *context, uint32_t index)
  476. {
  477. FlipWorldApp *app = (FlipWorldApp *)context;
  478. if (!app)
  479. {
  480. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  481. return;
  482. }
  483. char ssid[64];
  484. char pass[64];
  485. switch (index)
  486. {
  487. case 0: // Input SSID
  488. free_all_views(app, false);
  489. if (!alloc_text_input_view(app, "SSID"))
  490. {
  491. FURI_LOG_E(TAG, "Failed to allocate text input view");
  492. return;
  493. }
  494. // load SSID
  495. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  496. {
  497. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  498. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  499. }
  500. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  501. break;
  502. case 1: // Input Password
  503. free_all_views(app, false);
  504. if (!alloc_text_input_view(app, "Password"))
  505. {
  506. FURI_LOG_E(TAG, "Failed to allocate text input view");
  507. return;
  508. }
  509. // load password
  510. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  511. {
  512. strncpy(app->text_input_temp_buffer, pass, app->text_input_buffer_size - 1);
  513. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  514. }
  515. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  516. break;
  517. default:
  518. FURI_LOG_E(TAG, "Unknown configuration item index");
  519. break;
  520. }
  521. }