xremote_control.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*!
  2. * @file flipper-xremote/xremote_control.c
  3. @license This project is released under the GNU GPLv3 License
  4. * @copyright (c) 2023 Sandro Kalatozishvili (s.kalatoz@gmail.com)
  5. *
  6. * @brief Remote controller application menu and view factory.
  7. */
  8. #include "xremote_control.h"
  9. #include "infrared/infrared_remote.h"
  10. #include "views/xremote_general_view.h"
  11. #include "views/xremote_control_view.h"
  12. #include "views/xremote_navigation_view.h"
  13. #include "views/xremote_player_view.h"
  14. #include "views/xremote_custom_view.h"
  15. static uint32_t xremote_control_submenu_exit_callback(void* context)
  16. {
  17. UNUSED(context);
  18. return XRemoteViewSubmenu;
  19. }
  20. static uint32_t xremote_navigation_view_exit_callback(void* context)
  21. {
  22. UNUSED(context);
  23. return XRemoteViewIRSubmenu;
  24. }
  25. static void xremote_ir_clear_callback(void *context)
  26. {
  27. xremote_app_assert_void(context);
  28. infrared_remote_free((InfraredRemote*)context);
  29. }
  30. static void xremote_control_submenu_callback(void* context, uint32_t index)
  31. {
  32. furi_assert(context);
  33. XRemoteApp* app = context;
  34. /* Allocate new view based on selection */
  35. if (index == XRemoteViewIRGeneral)
  36. xremote_app_view_alloc(app, index, xremote_general_view_alloc);
  37. else if (index == XRemoteViewIRControl)
  38. xremote_app_view_alloc(app, index, xremote_control_view_alloc);
  39. else if (index == XRemoteViewIRNavigation)
  40. xremote_app_view_alloc(app, index, xremote_navigation_view_alloc);
  41. else if (index == XRemoteViewIRPlayer)
  42. xremote_app_view_alloc(app, index, xremote_player_view_alloc);
  43. else if (index == XRemoteViewIRCustom)
  44. xremote_app_view_alloc(app, index, xremote_custom_view_alloc);
  45. if (app->view_ctx != NULL)
  46. {
  47. xremote_app_view_set_previous_callback(app, xremote_navigation_view_exit_callback);
  48. xremote_app_set_view_context(app, app->context, NULL);
  49. xremote_app_switch_to_view(app, index);
  50. }
  51. }
  52. static InfraredRemote* xremote_load_ir_buttons(XRemoteAppContext* app_ctx)
  53. {
  54. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  55. Storage* storage = furi_record_open(RECORD_STORAGE);
  56. storage_simply_mkdir(storage, XREMOTE_APP_FOLDER);
  57. /* Open file browser (view and dialogs are managed by the browser itself) */
  58. DialogsFileBrowserOptions browser;
  59. dialog_file_browser_set_basic_options(&browser, XREMOTE_APP_EXTENSION, &I_IR_Icon_10x10);
  60. browser.base_path = XREMOTE_APP_FOLDER;
  61. /* Show file selection dialog (returns selected file path with variable file_path) */
  62. if(!dialog_file_browser_show(dialogs, app_ctx->file_path, app_ctx->file_path, &browser))
  63. {
  64. furi_record_close(RECORD_STORAGE);
  65. furi_record_close(RECORD_DIALOGS);
  66. return NULL;
  67. }
  68. /* Load buttons from the selected path */
  69. InfraredRemote* remote = infrared_remote_alloc();
  70. bool success = infrared_remote_load(remote, app_ctx->file_path);
  71. /* Cleanup file loading context */
  72. furi_record_close(RECORD_STORAGE);
  73. furi_record_close(RECORD_DIALOGS);
  74. if (!success)
  75. {
  76. infrared_remote_free(remote);
  77. return NULL;
  78. }
  79. return remote;
  80. }
  81. XRemoteApp* xremote_control_alloc(XRemoteAppContext* app_ctx)
  82. {
  83. /* Open file browser and load buttons from selected file */
  84. InfraredRemote* remote = xremote_load_ir_buttons(app_ctx);
  85. xremote_app_assert(remote, NULL);
  86. /* Allocate remote controller app with submenu */
  87. XRemoteApp* app = xremote_app_alloc(app_ctx);
  88. xremote_app_submenu_alloc(app, XRemoteViewIRSubmenu, xremote_control_submenu_exit_callback);
  89. xremote_app_submenu_add(app, "General", XRemoteViewIRGeneral, xremote_control_submenu_callback);
  90. xremote_app_submenu_add(app, "Control", XRemoteViewIRControl, xremote_control_submenu_callback);
  91. xremote_app_submenu_add(app, "Navigation", XRemoteViewIRNavigation, xremote_control_submenu_callback);
  92. xremote_app_submenu_add(app, "Playback", XRemoteViewIRPlayer, xremote_control_submenu_callback);
  93. xremote_app_submenu_add(app, "Custom", XRemoteViewIRCustom, xremote_control_submenu_callback);
  94. xremote_app_set_user_context(app, remote, xremote_ir_clear_callback);
  95. return app;
  96. }