xremote_edit.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. * @file flipper-xremote/xremote_edit.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 Edit menu for XRemote custom layout buttons.
  7. */
  8. #include "xremote_edit.h"
  9. typedef struct {
  10. VariableItemList* item_list;
  11. XRemoteAppButtons* buttons;
  12. } XRemoteEditContext;
  13. static uint32_t xremote_edit_view_exit_callback(void* context) {
  14. UNUSED(context);
  15. return XRemoteViewIRSubmenu;
  16. }
  17. static void xremote_edit_buttons_store(XRemoteAppButtons* buttons) {
  18. FuriString* path = buttons->app_ctx->file_path;
  19. infrared_remote_store(buttons->remote);
  20. xremote_app_extension_store(buttons, path);
  21. }
  22. static void xremote_item_update_item(VariableItem* item, FuriString* button) {
  23. XRemoteEditContext* ctx = variable_item_get_context(item);
  24. XRemoteAppButtons* buttons = ctx->buttons;
  25. int button_index = variable_item_get_current_value_index(item);
  26. const char* button_name = xremote_button_get_name(button_index);
  27. variable_item_set_current_value_text(item, button_name);
  28. furi_string_set_str(button, button_name);
  29. xremote_edit_buttons_store(buttons);
  30. }
  31. static void xremote_edit_ok_press_changed(VariableItem* item) {
  32. XRemoteEditContext* ctx = variable_item_get_context(item);
  33. xremote_item_update_item(item, ctx->buttons->custom_ok);
  34. }
  35. static void xremote_edit_up_press_changed(VariableItem* item) {
  36. XRemoteEditContext* ctx = variable_item_get_context(item);
  37. xremote_item_update_item(item, ctx->buttons->custom_up);
  38. }
  39. static void xremote_edit_down_press_changed(VariableItem* item) {
  40. XRemoteEditContext* ctx = variable_item_get_context(item);
  41. xremote_item_update_item(item, ctx->buttons->custom_down);
  42. }
  43. static void xremote_edit_left_press_changed(VariableItem* item) {
  44. XRemoteEditContext* ctx = variable_item_get_context(item);
  45. xremote_item_update_item(item, ctx->buttons->custom_left);
  46. }
  47. static void xremote_edit_right_press_changed(VariableItem* item) {
  48. XRemoteEditContext* ctx = variable_item_get_context(item);
  49. xremote_item_update_item(item, ctx->buttons->custom_right);
  50. }
  51. static void xremote_edit_ok_hold_changed(VariableItem* item) {
  52. XRemoteEditContext* ctx = variable_item_get_context(item);
  53. xremote_item_update_item(item, ctx->buttons->custom_ok_hold);
  54. }
  55. static void xremote_edit_up_hold_changed(VariableItem* item) {
  56. XRemoteEditContext* ctx = variable_item_get_context(item);
  57. xremote_item_update_item(item, ctx->buttons->custom_up_hold);
  58. }
  59. static void xremote_edit_down_hold_changed(VariableItem* item) {
  60. XRemoteEditContext* ctx = variable_item_get_context(item);
  61. xremote_item_update_item(item, ctx->buttons->custom_down_hold);
  62. }
  63. static void xremote_edit_left_hold_changed(VariableItem* item) {
  64. XRemoteEditContext* ctx = variable_item_get_context(item);
  65. xremote_item_update_item(item, ctx->buttons->custom_left_hold);
  66. }
  67. static void xremote_edit_right_hold_changed(VariableItem* item) {
  68. XRemoteEditContext* ctx = variable_item_get_context(item);
  69. xremote_item_update_item(item, ctx->buttons->custom_right_hold);
  70. }
  71. static void xremote_edit_list_add_item(
  72. XRemoteEditContext* context,
  73. const char* item_name,
  74. FuriString* button,
  75. VariableItemChangeCallback change_callback) {
  76. VariableItemList* list = context->item_list;
  77. VariableItem* item;
  78. /* Add custom_up to variable item list */
  79. item = variable_item_list_add(list, item_name, XREMOTE_BUTTON_COUNT, change_callback, context);
  80. /* Get button name and index */
  81. const char* button_name = furi_string_get_cstr(button);
  82. uint32_t button_index = xremote_button_get_index(button_name);
  83. /* Set button name and index to the list item */
  84. variable_item_set_current_value_index(item, button_index);
  85. variable_item_set_current_value_text(item, button_name);
  86. }
  87. static XRemoteEditContext* xremote_edit_context_alloc(XRemoteAppButtons* buttons) {
  88. XRemoteEditContext* context = malloc(sizeof(XRemoteEditContext));
  89. context->item_list = variable_item_list_alloc();
  90. XRemoteAppContext* app_ctx = buttons->app_ctx;
  91. context->buttons = buttons;
  92. /* Configure variable item list view */
  93. View* view = variable_item_list_get_view(context->item_list);
  94. view_set_previous_callback(view, xremote_edit_view_exit_callback);
  95. view_dispatcher_add_view(app_ctx->view_dispatcher, XRemoteViewIRCustomEditPage, view);
  96. /* Add press items to the variable list */
  97. xremote_edit_list_add_item(
  98. context, "Ok press", buttons->custom_ok, xremote_edit_ok_press_changed);
  99. xremote_edit_list_add_item(
  100. context, "Up press", buttons->custom_up, xremote_edit_up_press_changed);
  101. xremote_edit_list_add_item(
  102. context, "Down press", buttons->custom_down, xremote_edit_down_press_changed);
  103. xremote_edit_list_add_item(
  104. context, "Left press", buttons->custom_left, xremote_edit_left_press_changed);
  105. xremote_edit_list_add_item(
  106. context, "Right press", buttons->custom_right, xremote_edit_right_press_changed);
  107. xremote_edit_list_add_item(
  108. context, "Ok hold", buttons->custom_ok_hold, xremote_edit_ok_hold_changed);
  109. xremote_edit_list_add_item(
  110. context, "Up hold", buttons->custom_up_hold, xremote_edit_up_hold_changed);
  111. xremote_edit_list_add_item(
  112. context, "Down hold", buttons->custom_down_hold, xremote_edit_down_hold_changed);
  113. xremote_edit_list_add_item(
  114. context, "Left hold", buttons->custom_left_hold, xremote_edit_left_hold_changed);
  115. xremote_edit_list_add_item(
  116. context, "Right hold", buttons->custom_right_hold, xremote_edit_right_hold_changed);
  117. return context;
  118. }
  119. void xremote_edit_context_clear_callback(void* context) {
  120. XRemoteEditContext* ctx = (XRemoteEditContext*)context;
  121. variable_item_list_free(ctx->item_list);
  122. }
  123. void xremote_edit_view_alloc(XRemoteApp* app, uint32_t view_id, XRemoteAppButtons* buttons) {
  124. xremote_app_view_free(app);
  125. XRemoteView* remote_view = xremote_view_alloc_empty();
  126. XRemoteEditContext* context = xremote_edit_context_alloc(buttons);
  127. xremote_view_set_app_context(remote_view, buttons->app_ctx);
  128. xremote_view_set_view(remote_view, variable_item_list_get_view(context->item_list));
  129. xremote_view_set_context(remote_view, context, xremote_edit_context_clear_callback);
  130. app->view_ctx = remote_view;
  131. app->view_id = view_id;
  132. }