xremote.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "xremote.h"
  2. bool xremote_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. XRemote* app = context;
  5. return scene_manager_handle_custom_event(app->scene_manager, event);
  6. }
  7. void xremote_tick_event_callback(void* context) {
  8. furi_assert(context);
  9. XRemote* app = context;
  10. scene_manager_handle_tick_event(app->scene_manager);
  11. }
  12. //leave app if back button pressed
  13. bool xremote_navigation_event_callback(void* context) {
  14. furi_assert(context);
  15. XRemote* app = context;
  16. return scene_manager_handle_back_event(app->scene_manager);
  17. }
  18. XRemote* xremote_app_alloc() {
  19. XRemote* app = malloc(sizeof(XRemote));
  20. app->gui = furi_record_open(RECORD_GUI);
  21. app->notification = furi_record_open(RECORD_NOTIFICATION);
  22. //Turn backlight on, believe me this makes testing your app easier
  23. notification_message(app->notification, &sequence_display_backlight_on);
  24. //Scene additions
  25. app->view_dispatcher = view_dispatcher_alloc();
  26. view_dispatcher_enable_queue(app->view_dispatcher);
  27. app->scene_manager = scene_manager_alloc(&xremote_scene_handlers, app);
  28. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  29. view_dispatcher_set_navigation_event_callback(
  30. app->view_dispatcher, xremote_navigation_event_callback);
  31. view_dispatcher_set_tick_event_callback(
  32. app->view_dispatcher, xremote_tick_event_callback, 100);
  33. view_dispatcher_set_custom_event_callback(app->view_dispatcher, xremote_custom_event_callback);
  34. app->submenu = submenu_alloc();
  35. app->editmenu = submenu_alloc();
  36. // Set defaults, in case no config loaded
  37. app->haptic = 1;
  38. app->speaker = 1;
  39. app->led = 1;
  40. app->save_settings = 1;
  41. app->transmitting = 0;
  42. app->ir_timing = 1000;
  43. app->ir_timing_char = "1000";
  44. app->stop_transmit = false;
  45. // Load configs
  46. xremote_read_settings(app);
  47. app->dialogs = furi_record_open(RECORD_DIALOGS);
  48. app->file_path = furi_string_alloc();
  49. app->ir_remote_buffer = xremote_ir_remote_alloc();
  50. app->ir_worker = infrared_worker_alloc();
  51. app->cross_remote = xremote_cross_remote_alloc();
  52. app->sg_remote_buffer = xremote_sg_remote_alloc();
  53. app->loading = loading_alloc();
  54. app->subghz = subghz_alloc();
  55. app->text_input = text_input_alloc();
  56. // Custom made int keyboard
  57. app->int_input = int_input_alloc();
  58. view_dispatcher_add_view(
  59. app->view_dispatcher, XRemoteViewIdIntInput, int_input_get_view(app->int_input));
  60. view_dispatcher_add_view(
  61. app->view_dispatcher, XRemoteViewIdTextInput, text_input_get_view(app->text_input));
  62. view_dispatcher_add_view(
  63. app->view_dispatcher, XRemoteViewIdMenu, submenu_get_view(app->submenu));
  64. view_dispatcher_add_view(
  65. app->view_dispatcher, XRemoteViewIdEditItem, submenu_get_view(app->editmenu));
  66. app->xremote_infoscreen = xremote_infoscreen_alloc();
  67. view_dispatcher_add_view(
  68. app->view_dispatcher,
  69. XRemoteViewIdInfoscreen,
  70. xremote_infoscreen_get_view(app->xremote_infoscreen));
  71. app->xremote_transmit = xremote_transmit_alloc();
  72. view_dispatcher_add_view(
  73. app->view_dispatcher,
  74. XRemoteViewIdTransmit,
  75. xremote_transmit_get_view(app->xremote_transmit));
  76. app->xremote_pause_set = xremote_pause_set_alloc();
  77. view_dispatcher_add_view(
  78. app->view_dispatcher,
  79. XRemoteViewIdPauseSet,
  80. xremote_pause_set_get_view(app->xremote_pause_set));
  81. app->button_menu_create = button_menu_alloc();
  82. view_dispatcher_add_view(
  83. app->view_dispatcher, XRemoteViewIdCreate, button_menu_get_view(app->button_menu_create));
  84. app->button_menu_create_add = button_menu_alloc();
  85. view_dispatcher_add_view(
  86. app->view_dispatcher,
  87. XRemoteViewIdCreateAdd,
  88. button_menu_get_view(app->button_menu_create_add));
  89. app->button_menu_ir = button_menu_alloc();
  90. view_dispatcher_add_view(
  91. app->view_dispatcher, XRemoteViewIdIrRemote, button_menu_get_view(app->button_menu_ir));
  92. app->variable_item_list = variable_item_list_alloc();
  93. view_dispatcher_add_view(
  94. app->view_dispatcher,
  95. XRemoteViewIdSettings,
  96. variable_item_list_get_view(app->variable_item_list));
  97. app->popup = popup_alloc();
  98. view_dispatcher_add_view(app->view_dispatcher, XRemoteViewIdWip, popup_get_view(app->popup));
  99. app->view_stack = view_stack_alloc();
  100. view_dispatcher_add_view(
  101. app->view_dispatcher, XRemoteViewIdStack, view_stack_get_view(app->view_stack));
  102. //End Scene Additions
  103. return app;
  104. }
  105. void xremote_app_free(XRemote* app) {
  106. furi_assert(app);
  107. // Scene manager
  108. scene_manager_free(app->scene_manager);
  109. infrared_worker_free(app->ir_worker);
  110. xremote_cross_remote_free(app->cross_remote);
  111. subghz_free(app->subghz);
  112. // View Dispatcher
  113. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdMenu);
  114. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreate);
  115. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreateAdd);
  116. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdSettings);
  117. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdWip);
  118. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdStack);
  119. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTextInput);
  120. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTransmit);
  121. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdPauseSet);
  122. text_input_free(app->text_input);
  123. int_input_free(app->int_input);
  124. button_menu_free(app->button_menu_create);
  125. button_menu_free(app->button_menu_create_add);
  126. button_menu_free(app->button_menu_ir);
  127. view_stack_free(app->view_stack);
  128. popup_free(app->popup);
  129. submenu_free(app->submenu);
  130. view_dispatcher_free(app->view_dispatcher);
  131. furi_record_close(RECORD_GUI);
  132. furi_record_close(RECORD_DIALOGS);
  133. furi_string_free(app->file_path);
  134. loading_free(app->loading);
  135. app->gui = NULL;
  136. app->notification = NULL;
  137. //Remove whatever is left
  138. free(app);
  139. }
  140. void xremote_popup_closed_callback(void* context) {
  141. furi_assert(context);
  142. XRemote* app = context;
  143. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTypePopupClosed);
  144. }
  145. void xremote_text_input_callback(void* context) {
  146. furi_assert(context);
  147. XRemote* app = context;
  148. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTextInput);
  149. }
  150. int32_t xremote_app(void* p) {
  151. UNUSED(p);
  152. XRemote* app = xremote_app_alloc();
  153. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  154. //scene_manager_next_scene(app->scene_manager, XRemoteSceneInfoscreen); //Start with start screen
  155. scene_manager_next_scene(
  156. app->scene_manager, XRemoteSceneMenu); //if you want to directly start with Menu
  157. furi_hal_power_suppress_charge_enter();
  158. view_dispatcher_run(app->view_dispatcher);
  159. xremote_save_settings(app);
  160. furi_hal_power_suppress_charge_exit();
  161. xremote_app_free(app);
  162. return 0;
  163. }