xremote_app.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*!
  2. * @file flipper-xremote/xremote_app.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 Shared functionality and data types between the apps.
  7. */
  8. #include "xremote_app.h"
  9. #define XREMOTE_APP_SETTINGS ANY_PATH("infrared/assets/xremote.cfg")
  10. #define TAG "XRemoteApp"
  11. XRemoteAppSettings* xremote_app_settings_alloc()
  12. {
  13. XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
  14. settings->orientation = ViewOrientationHorizontal;
  15. settings->exit_behavior = XRemoteAppExitPress;
  16. settings->repeat_count = 2;
  17. return settings;
  18. }
  19. void xremote_app_settings_free(XRemoteAppSettings* settings)
  20. {
  21. xremote_app_assert_void(settings);
  22. free(settings);
  23. }
  24. bool xremote_app_settings_store(XRemoteAppSettings* settings)
  25. {
  26. Storage* storage = furi_record_open(RECORD_STORAGE);
  27. FlipperFormat* ff = flipper_format_file_alloc(storage);
  28. FURI_LOG_I(TAG, "store config file: \'%s\'", XREMOTE_APP_SETTINGS);
  29. bool success = false;
  30. do {
  31. /* Write header in config file */
  32. if (!flipper_format_file_open_always(ff, XREMOTE_APP_SETTINGS)) break;
  33. if (!flipper_format_write_header_cstr(ff, "XRemote settings file", 1)) break;
  34. if (!flipper_format_write_comment_cstr(ff, "")) break;
  35. /* Write actual configuration to the settings file */
  36. if (!flipper_format_write_uint32(ff, "orientation", (uint32_t*)&settings->orientation, 1)) break;
  37. if (!flipper_format_write_uint32(ff, "repeat", (uint32_t*)&settings->repeat_count, 1)) break;
  38. if (!flipper_format_write_uint32(ff, "exit", (uint32_t*)&settings->exit_behavior, 1)) break;
  39. success = true;
  40. } while(false);
  41. furi_record_close(RECORD_STORAGE);
  42. flipper_format_free(ff);
  43. return success;
  44. }
  45. bool xremote_app_settings_load(XRemoteAppSettings* settings)
  46. {
  47. Storage* storage = furi_record_open(RECORD_STORAGE);
  48. FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
  49. FuriString* header = furi_string_alloc();
  50. FURI_LOG_I(TAG, "load config file: \'%s\'", XREMOTE_APP_SETTINGS);
  51. uint32_t version = 0;
  52. uint32_t value = 0;
  53. bool success = false;
  54. do {
  55. /* Open file and read the header */
  56. if (!flipper_format_buffered_file_open_existing(ff, XREMOTE_APP_SETTINGS)) break;
  57. if (!flipper_format_read_header(ff, header, &version)) break;
  58. if (!furi_string_equal(header, "XRemote settings file") || (version != 1)) break;
  59. /* Parse config data from the buffer */
  60. if (!flipper_format_read_uint32(ff, "orientation", &value, 1)) break;
  61. settings->orientation = value;
  62. if (!flipper_format_read_uint32(ff, "repeat", &value, 1)) break;
  63. settings->repeat_count = value;
  64. if (!flipper_format_read_uint32(ff, "exit", &value, 1)) break;
  65. settings->exit_behavior = value;
  66. success = true;
  67. } while(false);
  68. furi_record_close(RECORD_STORAGE);
  69. furi_string_free(header);
  70. flipper_format_free(ff);
  71. return success;
  72. }
  73. XRemoteAppContext* xremote_app_context_alloc(void *arg)
  74. {
  75. XRemoteAppContext* ctx = malloc(sizeof(XRemoteAppContext));
  76. ctx->app_argument = arg;
  77. /* Open GUI and norification records */
  78. ctx->gui = furi_record_open(RECORD_GUI);
  79. ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
  80. /* Allocate and load global app settings */
  81. ctx->app_settings = xremote_app_settings_alloc();
  82. xremote_app_settings_load(ctx->app_settings);
  83. /* Allocate and setup view dispatcher */
  84. ctx->view_dispatcher = view_dispatcher_alloc();
  85. view_dispatcher_enable_queue(ctx->view_dispatcher);
  86. view_dispatcher_attach_to_gui(ctx->view_dispatcher, ctx->gui, ViewDispatcherTypeFullscreen);
  87. return ctx;
  88. }
  89. void xremote_app_context_free(XRemoteAppContext* ctx)
  90. {
  91. xremote_app_assert_void(ctx);
  92. notification_internal_message(ctx->notifications, &sequence_reset_blue);
  93. xremote_app_settings_free(ctx->app_settings);
  94. view_dispatcher_free(ctx->view_dispatcher);
  95. furi_record_close(RECORD_NOTIFICATION);
  96. furi_record_close(RECORD_GUI);
  97. free(ctx);
  98. }
  99. const char* xremote_app_context_get_exit_str(XRemoteAppContext* ctx)
  100. {
  101. XRemoteAppExit exit_behavior = ctx->app_settings->exit_behavior;
  102. return exit_behavior == XRemoteAppExitHold ? "Hold to exit" : "Press to exit";
  103. }
  104. void xremote_app_view_alloc(XRemoteApp *app, uint32_t view_id, XRemoteViewAllocator allocator)
  105. {
  106. furi_assert(app);
  107. xremote_app_assert_void(app->app_ctx);
  108. if (app->view_id == view_id &&
  109. app->view_ctx != NULL) return;
  110. xremote_app_view_free(app);
  111. app->view_id = view_id;
  112. app->view_ctx = allocator(app->app_ctx);
  113. View* app_view = xremote_view_get_view(app->view_ctx);
  114. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  115. view_dispatcher_add_view(view_disp, app->view_id, app_view);
  116. }
  117. void xremote_app_view_free(XRemoteApp* app)
  118. {
  119. xremote_app_assert_void(app);
  120. if (app->app_ctx != NULL && app->view_id != XRemoteViewNone)
  121. {
  122. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  123. view_dispatcher_remove_view(view_disp, app->view_id);
  124. app->view_id = XRemoteViewNone;
  125. }
  126. if (app->view_ctx != NULL)
  127. {
  128. xremote_view_free(app->view_ctx);
  129. app->view_ctx = NULL;
  130. }
  131. }
  132. bool xremote_app_has_view(XRemoteApp *app, uint32_t view_id)
  133. {
  134. xremote_app_assert(app, false);
  135. return (app->view_id == view_id ||
  136. app->submenu_id == view_id);
  137. }
  138. void xremote_app_switch_to_view(XRemoteApp *app, uint32_t view_id)
  139. {
  140. furi_assert(app);
  141. xremote_app_assert_void(app->app_ctx);
  142. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  143. view_dispatcher_switch_to_view(view_disp, view_id);
  144. }
  145. void xremote_app_switch_to_submenu(XRemoteApp *app)
  146. {
  147. furi_assert(app);
  148. xremote_app_assert_void(app->app_ctx);
  149. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  150. view_dispatcher_switch_to_view(view_disp, app->submenu_id);
  151. }
  152. void xremote_app_submenu_add(XRemoteApp* app, const char *name, uint32_t index, SubmenuItemCallback callback)
  153. {
  154. furi_assert(app);
  155. xremote_app_assert_void(app->submenu);
  156. submenu_add_item(app->submenu, name, index, callback, app);
  157. }
  158. void xremote_app_submenu_alloc(XRemoteApp* app, uint32_t index, ViewNavigationCallback prev_cb)
  159. {
  160. furi_assert(app);
  161. app->submenu = submenu_alloc();
  162. app->submenu_id = index;
  163. XRemoteAppSettings *settings = app->app_ctx->app_settings;
  164. submenu_set_orientation(app->submenu, settings->orientation);
  165. view_set_previous_callback(submenu_get_view(app->submenu), prev_cb);
  166. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  167. view_dispatcher_add_view(view_disp, app->submenu_id, submenu_get_view(app->submenu));
  168. }
  169. void xremote_app_submenu_free(XRemoteApp *app)
  170. {
  171. xremote_app_assert_void(app);
  172. /* Remove submenu view from dispatcher */
  173. if (app->submenu_id != XRemoteViewNone && app->app_ctx != NULL)
  174. {
  175. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  176. view_dispatcher_remove_view(view_disp, app->submenu_id);
  177. app->submenu_id = XRemoteViewNone;
  178. }
  179. /* Free submenu */
  180. if (app->submenu != NULL)
  181. {
  182. submenu_free(app->submenu);
  183. app->submenu = NULL;
  184. }
  185. }
  186. void xremote_app_view_set_previous_callback(XRemoteApp* app, ViewNavigationCallback callback)
  187. {
  188. furi_assert(app);
  189. xremote_app_assert_void(app->view_ctx);
  190. View* view = xremote_view_get_view(app->view_ctx);
  191. view_set_previous_callback(view, callback);
  192. }
  193. void xremote_app_set_view_context(XRemoteApp* app, void *context, XRemoteViewClearCallback on_clear)
  194. {
  195. furi_assert(app);
  196. xremote_app_assert_void(app->view_ctx);
  197. xremote_view_set_context(app->view_ctx, context, on_clear);
  198. }
  199. void xremote_app_set_user_context(XRemoteApp* app, void *context, XRemoteAppClearCallback on_clear)
  200. {
  201. furi_assert(app);
  202. app->on_clear = on_clear;
  203. app->context = context;
  204. }
  205. void xremote_app_user_context_free(XRemoteApp* app)
  206. {
  207. furi_assert(app);
  208. xremote_app_assert_void(app->context);
  209. xremote_app_assert_void(app->on_clear);
  210. app->on_clear(app->context);
  211. app->on_clear = NULL;
  212. app->context = NULL;
  213. }
  214. XRemoteApp* xremote_app_alloc(XRemoteAppContext *ctx)
  215. {
  216. furi_assert(ctx);
  217. XRemoteApp* app = malloc(sizeof(XRemoteApp));
  218. app->submenu_id = XRemoteViewNone;
  219. app->view_id = XRemoteViewNone;
  220. app->app_ctx = ctx;
  221. app->submenu = NULL;
  222. app->view_ctx = NULL;
  223. app->on_clear = NULL;
  224. app->context = NULL;
  225. return app;
  226. }
  227. void xremote_app_free(XRemoteApp* app)
  228. {
  229. xremote_app_assert_void(app);
  230. xremote_app_submenu_free(app);
  231. xremote_app_view_free(app);
  232. if (app->on_clear != NULL)
  233. app->on_clear(app->context);
  234. free(app);
  235. }