xremote.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. app->scene_manager = scene_manager_alloc(&xremote_scene_handlers, app);
  27. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  28. view_dispatcher_set_navigation_event_callback(
  29. app->view_dispatcher, xremote_navigation_event_callback);
  30. view_dispatcher_set_tick_event_callback(
  31. app->view_dispatcher, xremote_tick_event_callback, 100);
  32. view_dispatcher_set_custom_event_callback(app->view_dispatcher, xremote_custom_event_callback);
  33. app->submenu = submenu_alloc();
  34. app->editmenu = submenu_alloc();
  35. // Set defaults, in case no config loaded
  36. app->haptic = 1;
  37. app->speaker = 1;
  38. app->led = 1;
  39. app->save_settings = 1;
  40. app->transmitting = 0;
  41. app->ir_timing = 1000;
  42. app->ir_timing_char = "1000";
  43. app->sg_timing = 500;
  44. app->sg_timing_char = "500";
  45. app->stop_transmit = false;
  46. app->loop_transmit = 0;
  47. app->transmit_item = 0;
  48. // Load configs
  49. xremote_read_settings(app);
  50. app->dialogs = furi_record_open(RECORD_DIALOGS);
  51. app->file_path = furi_string_alloc();
  52. app->ir_remote_buffer = xremote_ir_remote_alloc();
  53. app->ir_worker = infrared_worker_alloc();
  54. app->cross_remote = xremote_cross_remote_alloc();
  55. app->sg_remote_buffer = xremote_sg_remote_alloc();
  56. app->loading = loading_alloc();
  57. app->subghz = subghz_alloc();
  58. app->text_input = text_input_alloc();
  59. app->number_input = number_input_alloc();
  60. view_dispatcher_add_view(
  61. app->view_dispatcher, XRemoteViewIdNumberInput, number_input_get_view(app->number_input));
  62. view_dispatcher_add_view(
  63. app->view_dispatcher, XRemoteViewIdTextInput, text_input_get_view(app->text_input));
  64. view_dispatcher_add_view(
  65. app->view_dispatcher, XRemoteViewIdMenu, submenu_get_view(app->submenu));
  66. view_dispatcher_add_view(
  67. app->view_dispatcher, XRemoteViewIdEditItem, submenu_get_view(app->editmenu));
  68. app->xremote_infoscreen = xremote_infoscreen_alloc();
  69. view_dispatcher_add_view(
  70. app->view_dispatcher,
  71. XRemoteViewIdInfoscreen,
  72. xremote_infoscreen_get_view(app->xremote_infoscreen));
  73. app->xremote_transmit = xremote_transmit_alloc();
  74. view_dispatcher_add_view(
  75. app->view_dispatcher,
  76. XRemoteViewIdTransmit,
  77. xremote_transmit_get_view(app->xremote_transmit));
  78. app->xremote_pause_set = xremote_pause_set_alloc();
  79. view_dispatcher_add_view(
  80. app->view_dispatcher,
  81. XRemoteViewIdPauseSet,
  82. xremote_pause_set_get_view(app->xremote_pause_set));
  83. app->button_menu_create = button_menu_alloc();
  84. view_dispatcher_add_view(
  85. app->view_dispatcher, XRemoteViewIdCreate, button_menu_get_view(app->button_menu_create));
  86. app->button_menu_create_add = button_menu_alloc();
  87. view_dispatcher_add_view(
  88. app->view_dispatcher,
  89. XRemoteViewIdCreateAdd,
  90. button_menu_get_view(app->button_menu_create_add));
  91. app->button_menu_ir = button_menu_alloc();
  92. view_dispatcher_add_view(
  93. app->view_dispatcher, XRemoteViewIdIrRemote, button_menu_get_view(app->button_menu_ir));
  94. app->variable_item_list = variable_item_list_alloc();
  95. view_dispatcher_add_view(
  96. app->view_dispatcher,
  97. XRemoteViewIdSettings,
  98. variable_item_list_get_view(app->variable_item_list));
  99. app->popup = popup_alloc();
  100. view_dispatcher_add_view(app->view_dispatcher, XRemoteViewIdWip, popup_get_view(app->popup));
  101. app->view_stack = view_stack_alloc();
  102. view_dispatcher_add_view(
  103. app->view_dispatcher, XRemoteViewIdStack, view_stack_get_view(app->view_stack));
  104. //End Scene Additions
  105. return app;
  106. }
  107. void xremote_app_free(XRemote* app) {
  108. furi_assert(app);
  109. // Scene manager
  110. scene_manager_free(app->scene_manager);
  111. infrared_worker_free(app->ir_worker);
  112. xremote_cross_remote_free(app->cross_remote);
  113. subghz_free(app->subghz);
  114. // View Dispatcher
  115. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdMenu);
  116. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdEditItem);
  117. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdInfoscreen);
  118. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreate);
  119. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdCreateAdd);
  120. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdSettings);
  121. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdWip);
  122. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdStack);
  123. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTextInput);
  124. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdNumberInput);
  125. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdTransmit);
  126. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdPauseSet);
  127. view_dispatcher_remove_view(app->view_dispatcher, XRemoteViewIdIrRemote);
  128. text_input_free(app->text_input);
  129. number_input_free(app->number_input);
  130. button_menu_free(app->button_menu_create);
  131. button_menu_free(app->button_menu_create_add);
  132. button_menu_free(app->button_menu_ir);
  133. view_stack_free(app->view_stack);
  134. popup_free(app->popup);
  135. submenu_free(app->submenu);
  136. xremote_infoscreen_free(app->xremote_infoscreen);
  137. xremote_pause_set_free(app->xremote_pause_set);
  138. xremote_transmit_free(app->xremote_transmit);
  139. view_dispatcher_free(app->view_dispatcher);
  140. furi_record_close(RECORD_GUI);
  141. furi_record_close(RECORD_DIALOGS);
  142. furi_string_free(app->file_path);
  143. loading_free(app->loading);
  144. app->gui = NULL;
  145. app->notification = NULL;
  146. //Remove whatever is left
  147. free(app);
  148. }
  149. void xremote_popup_closed_callback(void* context) {
  150. furi_assert(context);
  151. XRemote* app = context;
  152. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTypePopupClosed);
  153. }
  154. void xremote_text_input_callback(void* context) {
  155. furi_assert(context);
  156. XRemote* app = context;
  157. view_dispatcher_send_custom_event(app->view_dispatcher, XRemoteCustomEventTextInput);
  158. }
  159. int32_t xremote_app(void* p) {
  160. UNUSED(p);
  161. XRemote* app = xremote_app_alloc();
  162. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  163. //scene_manager_next_scene(app->scene_manager, XRemoteSceneInfoscreen); //Start with start screen
  164. scene_manager_next_scene(
  165. app->scene_manager, XRemoteSceneMenu); //if you want to directly start with Menu
  166. furi_hal_power_suppress_charge_enter();
  167. view_dispatcher_run(app->view_dispatcher);
  168. xremote_save_settings(app);
  169. furi_hal_power_suppress_charge_exit();
  170. xremote_app_free(app);
  171. return 0;
  172. }