xremote.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #define TAG "XRemote"
  17. void xremote_get_version(char* version, size_t length) {
  18. snprintf(version, length, "%.1f.%d", (double)FAP_VERSION, XREMOTE_BUILD_NUMBER);
  19. }
  20. static uint32_t xremote_view_exit_callback(void* context) {
  21. UNUSED(context);
  22. return XRemoteViewSubmenu;
  23. }
  24. static uint32_t xremote_exit_callback(void* context) {
  25. UNUSED(context);
  26. return VIEW_NONE;
  27. }
  28. static void xremote_child_clear_callback(void* context) {
  29. xremote_app_assert_void(context);
  30. xremote_app_free((XRemoteApp*)context);
  31. }
  32. static XRemoteApp* xremote_about_alloc(XRemoteAppContext* app_ctx) {
  33. XRemoteApp* app = xremote_app_alloc(app_ctx);
  34. xremote_app_view_alloc(app, XRemoteViewAbout, xremote_about_view_alloc);
  35. xremote_app_view_set_previous_callback(app, xremote_view_exit_callback);
  36. return app;
  37. }
  38. void xremote_submenu_callback(void* context, uint32_t index) {
  39. furi_assert(context);
  40. XRemoteApp* app = (XRemoteApp*)context;
  41. /* Cleanup previous app first */
  42. xremote_app_user_context_free(app);
  43. XRemoteApp* child = NULL;
  44. /* Allocate child app and view based on submenu selection */
  45. if(index == XRemoteViewLearn)
  46. child = xremote_learn_alloc(app->app_ctx);
  47. else if(index == XRemoteViewIRSubmenu)
  48. child = xremote_control_alloc(app->app_ctx);
  49. else if(index == XRemoteViewAnalyzer)
  50. child = xremote_analyzer_alloc(app->app_ctx);
  51. else if(index == XRemoteViewSettings)
  52. child = xremote_settings_alloc(app->app_ctx);
  53. else if(index == XRemoteViewAbout)
  54. child = xremote_about_alloc(app->app_ctx);
  55. if(child != NULL) {
  56. /* Switch to the view of newely allocated app */
  57. xremote_app_set_user_context(app, child, xremote_child_clear_callback);
  58. xremote_app_switch_to_view(child, index);
  59. }
  60. }
  61. int32_t xremote_main(void* p) {
  62. /* Allocate context and main application */
  63. XRemoteAppContext* context = xremote_app_context_alloc(p);
  64. XRemoteApp* app = xremote_app_alloc(context);
  65. /* Allocate and build the menu */
  66. xremote_app_submenu_alloc(app, XRemoteViewSubmenu, xremote_exit_callback);
  67. xremote_app_submenu_add(app, "Learn", XRemoteViewLearn, xremote_submenu_callback);
  68. xremote_app_submenu_add(app, "Saved", XRemoteViewIRSubmenu, xremote_submenu_callback);
  69. xremote_app_submenu_add(app, "Analyzer", XRemoteViewAnalyzer, xremote_submenu_callback);
  70. xremote_app_submenu_add(app, "Settings", XRemoteViewSettings, xremote_submenu_callback);
  71. xremote_app_submenu_add(app, "About", XRemoteViewAbout, xremote_submenu_callback);
  72. /* Switch to main menu by default and run disparcher*/
  73. xremote_app_switch_to_view(app, XRemoteViewSubmenu);
  74. view_dispatcher_run(app->app_ctx->view_dispatcher);
  75. /* Cleanup and exit */
  76. xremote_app_free(app);
  77. xremote_app_context_free(context);
  78. return 0;
  79. }