xremote.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*!
  2. * @file flipper-xremote/xremote.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 Entrypoint and factory of the XRemote main app.
  7. */
  8. #include "xremote.h"
  9. #include "xremote_learn.h"
  10. #include "xremote_control.h"
  11. #include "xremote_settings.h"
  12. #include "xremote_analyzer.h"
  13. #include "views/xremote_about_view.h"
  14. #include "views/xremote_learn_view.h"
  15. #include "views/xremote_signal_view.h"
  16. #include <infrared/infrared_last_settings.h>
  17. #define TAG "XRemote"
  18. void xremote_get_version(char* version, size_t length) {
  19. snprintf(
  20. version,
  21. length,
  22. "%d.%d.%d",
  23. XREMOTE_VERSION_MAJ,
  24. XREMOTE_VERSION_MIN,
  25. XREMOTE_BUILD_NUMBER);
  26. }
  27. static uint32_t xremote_view_exit_callback(void* context) {
  28. UNUSED(context);
  29. return XRemoteViewSubmenu;
  30. }
  31. static uint32_t xremote_exit_callback(void* context) {
  32. UNUSED(context);
  33. return VIEW_NONE;
  34. }
  35. static void xremote_child_clear_callback(void* context) {
  36. xremote_app_assert_void(context);
  37. xremote_app_free((XRemoteApp*)context);
  38. }
  39. static XRemoteApp* xremote_about_alloc(XRemoteAppContext* app_ctx) {
  40. XRemoteApp* app = xremote_app_alloc(app_ctx);
  41. xremote_app_view_alloc(app, XRemoteViewAbout, xremote_about_view_alloc);
  42. xremote_app_view_set_previous_callback(app, xremote_view_exit_callback);
  43. return app;
  44. }
  45. void xremote_submenu_callback(void* context, uint32_t index) {
  46. furi_assert(context);
  47. XRemoteApp* app = (XRemoteApp*)context;
  48. /* Cleanup previous app first */
  49. xremote_app_user_context_free(app);
  50. XRemoteApp* child = NULL;
  51. /* Allocate child app and view based on submenu selection */
  52. if(index == XRemoteViewLearn)
  53. child = xremote_learn_alloc(app->app_ctx);
  54. else if(index == XRemoteViewIRSubmenu)
  55. child = xremote_control_alloc(app->app_ctx);
  56. else if(index == XRemoteViewAnalyzer)
  57. child = xremote_analyzer_alloc(app->app_ctx);
  58. else if(index == XRemoteViewSettings)
  59. child = xremote_settings_alloc(app->app_ctx);
  60. else if(index == XRemoteViewAbout)
  61. child = xremote_about_alloc(app->app_ctx);
  62. if(child != NULL) {
  63. /* Switch to the view of newely allocated app */
  64. xremote_app_set_user_context(app, child, xremote_child_clear_callback);
  65. xremote_app_switch_to_view(child, index);
  66. }
  67. }
  68. int32_t xremote_main(void* p) {
  69. /* Allocate context and main application */
  70. XRemoteAppContext* context = xremote_app_context_alloc(p);
  71. XRemoteApp* app = xremote_app_alloc(context);
  72. /* Allocate and build the menu */
  73. xremote_app_submenu_alloc(app, XRemoteViewSubmenu, xremote_exit_callback);
  74. xremote_app_submenu_add(app, "Learn", XRemoteViewLearn, xremote_submenu_callback);
  75. xremote_app_submenu_add(app, "Saved", XRemoteViewIRSubmenu, xremote_submenu_callback);
  76. xremote_app_submenu_add(app, "Analyzer", XRemoteViewAnalyzer, xremote_submenu_callback);
  77. xremote_app_submenu_add(app, "Settings", XRemoteViewSettings, xremote_submenu_callback);
  78. xremote_app_submenu_add(app, "About", XRemoteViewAbout, xremote_submenu_callback);
  79. InfraredLastSettings* last_settings = infrared_last_settings_alloc();
  80. infrared_last_settings_load(last_settings);
  81. infrared_last_settings_apply(last_settings);
  82. /* Switch to main menu by default and run disparcher*/
  83. xremote_app_switch_to_view(app, XRemoteViewSubmenu);
  84. view_dispatcher_run(app->app_ctx->view_dispatcher);
  85. infrared_last_settings_reset(last_settings);
  86. infrared_last_settings_free(last_settings);
  87. /* Cleanup and exit */
  88. xremote_app_free(app);
  89. xremote_app_context_free(context);
  90. return 0;
  91. }