xremote_app.c 9.2 KB

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