xremote_app.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_notification_blink(NotificationApp* notifications) {
  111. xremote_app_assert_void(notifications);
  112. notification_message(notifications, &g_sequence_blink_purple_50);
  113. }
  114. void xremote_app_context_notify_led(XRemoteAppContext* app_ctx) {
  115. xremote_app_assert_void(app_ctx);
  116. xremote_app_notification_blink(app_ctx->notifications);
  117. }
  118. bool xremote_app_send_signal(XRemoteAppContext* app_ctx, InfraredSignal* signal) {
  119. xremote_app_assert(signal, false);
  120. XRemoteAppSettings* settings = app_ctx->app_settings;
  121. infrared_signal_transmit_times(signal, settings->repeat_count);
  122. xremote_app_context_notify_led(app_ctx);
  123. return true;
  124. }
  125. void xremote_app_view_alloc(XRemoteApp* app, uint32_t view_id, XRemoteViewAllocator allocator) {
  126. furi_assert(app);
  127. xremote_app_assert_void(app->app_ctx);
  128. if(app->view_id == view_id && app->view_ctx != NULL) return;
  129. xremote_app_view_free(app);
  130. app->view_id = view_id;
  131. app->view_ctx = allocator(app->app_ctx);
  132. View* app_view = xremote_view_get_view(app->view_ctx);
  133. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  134. view_dispatcher_add_view(view_disp, app->view_id, app_view);
  135. }
  136. void xremote_app_view_free(XRemoteApp* app) {
  137. xremote_app_assert_void(app);
  138. if(app->app_ctx != NULL && app->view_id != XRemoteViewNone) {
  139. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  140. view_dispatcher_remove_view(view_disp, app->view_id);
  141. app->view_id = XRemoteViewNone;
  142. }
  143. if(app->view_ctx != NULL) {
  144. xremote_view_free(app->view_ctx);
  145. app->view_ctx = NULL;
  146. }
  147. }
  148. bool xremote_app_has_view(XRemoteApp* app, uint32_t view_id) {
  149. xremote_app_assert(app, false);
  150. return (app->view_id == view_id || app->submenu_id == view_id);
  151. }
  152. void xremote_app_switch_to_view(XRemoteApp* app, uint32_t view_id) {
  153. furi_assert(app);
  154. xremote_app_assert_void(app->app_ctx);
  155. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  156. view_dispatcher_switch_to_view(view_disp, view_id);
  157. }
  158. void xremote_app_switch_to_submenu(XRemoteApp* app) {
  159. furi_assert(app);
  160. xremote_app_assert_void(app->app_ctx);
  161. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  162. view_dispatcher_switch_to_view(view_disp, app->submenu_id);
  163. }
  164. void xremote_app_submenu_add(
  165. XRemoteApp* app,
  166. const char* name,
  167. uint32_t index,
  168. SubmenuItemCallback callback) {
  169. furi_assert(app);
  170. xremote_app_assert_void(app->submenu);
  171. submenu_add_item(app->submenu, name, index, callback, app);
  172. }
  173. void xremote_app_submenu_alloc(XRemoteApp* app, uint32_t index, ViewNavigationCallback prev_cb) {
  174. furi_assert(app);
  175. app->submenu = submenu_alloc();
  176. app->submenu_id = index;
  177. XRemoteAppSettings* settings = app->app_ctx->app_settings;
  178. View* view = submenu_get_view(app->submenu);
  179. view_set_previous_callback(view, prev_cb);
  180. #ifdef FW_ORIGIN_Unleashed
  181. submenu_set_orientation(app->submenu, settings->orientation);
  182. #else
  183. view_set_orientation(view, settings->orientation);
  184. #endif
  185. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  186. view_dispatcher_add_view(view_disp, app->submenu_id, view);
  187. }
  188. void xremote_app_submenu_free(XRemoteApp* app) {
  189. xremote_app_assert_void(app);
  190. /* Remove submenu view from dispatcher */
  191. if(app->submenu_id != XRemoteViewNone && app->app_ctx != NULL) {
  192. ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
  193. view_dispatcher_remove_view(view_disp, app->submenu_id);
  194. app->submenu_id = XRemoteViewNone;
  195. }
  196. /* Free submenu */
  197. if(app->submenu != NULL) {
  198. submenu_free(app->submenu);
  199. app->submenu = NULL;
  200. }
  201. }
  202. void xremote_app_view_set_previous_callback(XRemoteApp* app, ViewNavigationCallback callback) {
  203. furi_assert(app);
  204. xremote_app_assert_void(app->view_ctx);
  205. View* view = xremote_view_get_view(app->view_ctx);
  206. view_set_previous_callback(view, callback);
  207. }
  208. void xremote_app_set_view_context(XRemoteApp* app, void* context, XRemoteClearCallback on_clear) {
  209. furi_assert(app);
  210. xremote_app_assert_void(app->view_ctx);
  211. xremote_view_set_context(app->view_ctx, context, on_clear);
  212. }
  213. void xremote_app_set_user_context(XRemoteApp* app, void* context, XRemoteClearCallback on_clear) {
  214. furi_assert(app);
  215. app->on_clear = on_clear;
  216. app->context = context;
  217. }
  218. void xremote_app_user_context_free(XRemoteApp* app) {
  219. furi_assert(app);
  220. xremote_app_assert_void(app->context);
  221. xremote_app_assert_void(app->on_clear);
  222. app->on_clear(app->context);
  223. app->on_clear = NULL;
  224. app->context = NULL;
  225. }
  226. XRemoteApp* xremote_app_alloc(XRemoteAppContext* ctx) {
  227. furi_assert(ctx);
  228. XRemoteApp* app = malloc(sizeof(XRemoteApp));
  229. app->submenu_id = XRemoteViewNone;
  230. app->view_id = XRemoteViewNone;
  231. app->app_ctx = ctx;
  232. app->submenu = NULL;
  233. app->view_ctx = NULL;
  234. app->on_clear = NULL;
  235. app->context = NULL;
  236. return app;
  237. }
  238. void xremote_app_free(XRemoteApp* app) {
  239. xremote_app_assert_void(app);
  240. xremote_app_submenu_free(app);
  241. xremote_app_view_free(app);
  242. if(app->on_clear != NULL) app->on_clear(app->context);
  243. free(app);
  244. }