xremote_app.c 8.3 KB

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