callback.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. }
  155. return true;
  156. }
  157. static bool alloc_variable_item_list(void *context)
  158. {
  159. FlipWorldApp *app = (FlipWorldApp *)context;
  160. if (!app)
  161. {
  162. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  163. return false;
  164. }
  165. if (!app->variable_item_list)
  166. {
  167. if (!easy_flipper_set_variable_item_list(&app->variable_item_list, FlipWorldViewSettings, settings_item_selected, callback_to_submenu, &app->view_dispatcher, app))
  168. return false;
  169. if (!app->variable_item_list)
  170. return false;
  171. if (!app->variable_item_ssid)
  172. {
  173. app->variable_item_ssid = variable_item_list_add(app->variable_item_list, "SSID", 0, NULL, NULL);
  174. variable_item_set_current_value_text(app->variable_item_ssid, "");
  175. }
  176. if (!app->variable_item_pass)
  177. {
  178. app->variable_item_pass = variable_item_list_add(app->variable_item_list, "Password", 0, NULL, NULL);
  179. variable_item_set_current_value_text(app->variable_item_pass, "");
  180. }
  181. char ssid[64];
  182. char pass[64];
  183. if (load_settings(ssid, sizeof(ssid), pass, sizeof(pass)))
  184. {
  185. variable_item_set_current_value_text(app->variable_item_ssid, ssid);
  186. // variable_item_set_current_value_text(app->variable_item_pass, pass);
  187. }
  188. }
  189. return true;
  190. }
  191. // free
  192. static void free_about_view(void *context)
  193. {
  194. FlipWorldApp *app = (FlipWorldApp *)context;
  195. if (!app)
  196. {
  197. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  198. return;
  199. }
  200. if (app->view_about)
  201. {
  202. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewAbout);
  203. view_free(app->view_about);
  204. app->view_about = NULL;
  205. }
  206. }
  207. static void free_main_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_main)
  216. {
  217. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  218. view_free(app->view_main);
  219. app->view_main = NULL;
  220. }
  221. }
  222. static void free_text_input_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->text_input)
  231. {
  232. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewTextInput);
  233. uart_text_input_free(app->text_input);
  234. app->text_input = NULL;
  235. }
  236. if (app->text_input_buffer)
  237. {
  238. free(app->text_input_buffer);
  239. app->text_input_buffer = NULL;
  240. }
  241. if (app->text_input_temp_buffer)
  242. {
  243. free(app->text_input_temp_buffer);
  244. app->text_input_temp_buffer = NULL;
  245. }
  246. }
  247. static void free_variable_item_list(void *context)
  248. {
  249. FlipWorldApp *app = (FlipWorldApp *)context;
  250. if (!app)
  251. {
  252. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  253. return;
  254. }
  255. if (app->variable_item_list)
  256. {
  257. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewSettings);
  258. variable_item_list_free(app->variable_item_list);
  259. app->variable_item_list = NULL;
  260. }
  261. if (app->variable_item_ssid)
  262. {
  263. free(app->variable_item_ssid);
  264. app->variable_item_ssid = NULL;
  265. }
  266. if (app->variable_item_pass)
  267. {
  268. free(app->variable_item_pass);
  269. app->variable_item_pass = NULL;
  270. }
  271. }
  272. static FuriThreadId thread_id;
  273. static bool game_thread_running = false;
  274. void free_all_views(void *context, bool should_free_variable_item_list)
  275. {
  276. FlipWorldApp *app = (FlipWorldApp *)context;
  277. if (!app)
  278. {
  279. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  280. return;
  281. }
  282. if (should_free_variable_item_list)
  283. {
  284. free_variable_item_list(app);
  285. }
  286. free_about_view(app);
  287. free_main_view(app);
  288. free_text_input_view(app);
  289. if (app->view_main)
  290. {
  291. view_free(app->view_main);
  292. view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
  293. app->view_main = NULL;
  294. }
  295. // free game thread
  296. if (game_thread_running)
  297. {
  298. game_thread_running = false;
  299. furi_thread_flags_set(thread_id, WorkerEvtStop);
  300. furi_thread_free(thread_id);
  301. }
  302. }
  303. void callback_submenu_choices(void *context, uint32_t index)
  304. {
  305. FlipWorldApp *app = (FlipWorldApp *)context;
  306. if (!app)
  307. {
  308. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  309. return;
  310. }
  311. switch (index)
  312. {
  313. case FlipWorldSubmenuIndexRun:
  314. // free game thread
  315. if (game_thread_running)
  316. {
  317. game_thread_running = false;
  318. furi_thread_flags_set(thread_id, WorkerEvtStop);
  319. furi_thread_free(thread_id);
  320. }
  321. free_all_views(app, true);
  322. if (!app->view_main)
  323. {
  324. if (!easy_flipper_set_view(&app->view_main, FlipWorldViewMain, NULL, NULL, callback_to_submenu, &app->view_dispatcher, app))
  325. {
  326. return;
  327. }
  328. if (!app->view_main)
  329. {
  330. return;
  331. }
  332. }
  333. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMain);
  334. FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app);
  335. if (!thread)
  336. {
  337. FURI_LOG_E(TAG, "Failed to allocate game thread");
  338. return;
  339. }
  340. furi_thread_start(thread);
  341. thread_id = furi_thread_get_id(thread);
  342. game_thread_running = true;
  343. break;
  344. case FlipWorldSubmenuIndexAbout:
  345. free_all_views(app, true);
  346. if (!alloc_about_view(app))
  347. {
  348. FURI_LOG_E(TAG, "Failed to allocate about view");
  349. return;
  350. }
  351. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewAbout);
  352. break;
  353. case FlipWorldSubmenuIndexSettings:
  354. free_all_views(app, true);
  355. if (!alloc_variable_item_list(app))
  356. {
  357. FURI_LOG_E(TAG, "Failed to allocate variable item list");
  358. return;
  359. }
  360. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  361. break;
  362. default:
  363. break;
  364. }
  365. }
  366. static void text_updated_ssid(void *context)
  367. {
  368. FlipWorldApp *app = (FlipWorldApp *)context;
  369. if (!app)
  370. {
  371. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  372. return;
  373. }
  374. // store the entered text
  375. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  376. // Ensure null-termination
  377. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  378. // save the setting
  379. save_char("WiFi-SSID", app->text_input_buffer);
  380. // update the variable item text
  381. if (app->variable_item_ssid)
  382. {
  383. variable_item_set_current_value_text(app->variable_item_ssid, app->text_input_buffer);
  384. // get value of password
  385. char pass[64];
  386. if (load_char("WiFi-Password", pass, sizeof(pass)))
  387. {
  388. if (strlen(pass) > 0 && strlen(app->text_input_buffer) > 0)
  389. {
  390. // save the settings
  391. save_settings(app->text_input_buffer, pass);
  392. // initialize the http
  393. if (flipper_http_init(flipper_http_rx_callback, app))
  394. {
  395. // save the wifi if the device is connected
  396. if (!flipper_http_save_wifi(app->text_input_buffer, pass))
  397. {
  398. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  399. }
  400. // free the resources
  401. flipper_http_deinit();
  402. }
  403. else
  404. {
  405. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  406. }
  407. }
  408. }
  409. }
  410. // switch to the settings view
  411. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  412. }
  413. static void text_updated_pass(void *context)
  414. {
  415. FlipWorldApp *app = (FlipWorldApp *)context;
  416. if (!app)
  417. {
  418. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  419. return;
  420. }
  421. // store the entered text
  422. strncpy(app->text_input_buffer, app->text_input_temp_buffer, app->text_input_buffer_size);
  423. // Ensure null-termination
  424. app->text_input_buffer[app->text_input_buffer_size - 1] = '\0';
  425. // save the setting
  426. save_char("WiFi-Password", app->text_input_buffer);
  427. // update the variable item text
  428. if (app->variable_item_pass)
  429. {
  430. // variable_item_set_current_value_text(app->variable_item_pass, app->text_input_buffer);
  431. }
  432. // get value of ssid
  433. char ssid[64];
  434. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  435. {
  436. if (strlen(ssid) > 0 && strlen(app->text_input_buffer) > 0)
  437. {
  438. // save the settings
  439. save_settings(ssid, app->text_input_buffer);
  440. // initialize the http
  441. if (flipper_http_init(flipper_http_rx_callback, app))
  442. {
  443. // save the wifi if the device is connected
  444. if (!flipper_http_save_wifi(ssid, app->text_input_buffer))
  445. {
  446. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  447. }
  448. // free the resources
  449. flipper_http_deinit();
  450. }
  451. else
  452. {
  453. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  454. }
  455. }
  456. }
  457. // switch to the settings view
  458. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewSettings);
  459. }
  460. static void settings_item_selected(void *context, uint32_t index)
  461. {
  462. FlipWorldApp *app = (FlipWorldApp *)context;
  463. if (!app)
  464. {
  465. FURI_LOG_E(TAG, "FlipWorldApp is NULL");
  466. return;
  467. }
  468. switch (index)
  469. {
  470. case 0: // Input SSID
  471. free_all_views(app, false);
  472. if (!alloc_text_input_view(app, "SSID"))
  473. {
  474. FURI_LOG_E(TAG, "Failed to allocate text input view");
  475. return;
  476. }
  477. // load SSID
  478. char ssid[64];
  479. if (load_char("WiFi-SSID", ssid, sizeof(ssid)))
  480. {
  481. strncpy(app->text_input_temp_buffer, ssid, app->text_input_buffer_size - 1);
  482. app->text_input_temp_buffer[app->text_input_buffer_size - 1] = '\0';
  483. }
  484. view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewTextInput);
  485. break;
  486. case 1: // Input Password
  487. free_all_views(app, false);
  488. if (!alloc_text_input_view(app, "Password"))
  489. {
  490. FURI_LOG_E(TAG, "Failed to allocate text input view");
  491. return;
  492. }
  493. // load password
  494. char pass[64];
  495. if (load_char("WiFi-Password", pass, sizeof(pass)))
  496. {
  497. strncpy(app->text_input_temp_buffer, pass, 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. default:
  503. FURI_LOG_E(TAG, "Unknown configuration item index");
  504. break;
  505. }
  506. }