totp_input_text.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "totp_input_text.h"
  2. #include <gui/view_i.h>
  3. void view_draw(View* view, Canvas* canvas) {
  4. furi_assert(view);
  5. if(view->draw_callback) {
  6. void* data = view_get_model(view);
  7. view->draw_callback(canvas, data);
  8. view_unlock_model(view);
  9. }
  10. }
  11. bool view_input(View* view, InputEvent* event) {
  12. furi_assert(view);
  13. if(view->input_callback) {
  14. return view->input_callback(event, view->context);
  15. } else {
  16. return false;
  17. }
  18. }
  19. void view_unlock_model(View* view) {
  20. furi_assert(view);
  21. if(view->model_type == ViewModelTypeLocking) {
  22. ViewModelLocking* model = (ViewModelLocking*)(view->model);
  23. furi_check(furi_mutex_release(model->mutex) == FuriStatusOk);
  24. }
  25. }
  26. static void commit_text_input_callback(void* context) {
  27. InputTextSceneState* text_input_state = (InputTextSceneState*)context;
  28. if(text_input_state->callback != NULL) {
  29. InputTextSceneCallbackResult* result = malloc(sizeof(InputTextSceneCallbackResult));
  30. furi_check(result != NULL);
  31. result->user_input_length =
  32. strnlen(text_input_state->text_input_buffer, INPUT_BUFFER_SIZE);
  33. result->user_input = malloc(result->user_input_length + 1);
  34. furi_check(result->user_input != NULL);
  35. result->callback_data = text_input_state->callback_data;
  36. strlcpy(
  37. result->user_input,
  38. text_input_state->text_input_buffer,
  39. result->user_input_length + 1);
  40. text_input_state->callback(result);
  41. }
  42. }
  43. InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context) {
  44. InputTextSceneState* text_input_state = malloc(sizeof(InputTextSceneState));
  45. furi_check(text_input_state != NULL);
  46. text_input_state->text_input = text_input_alloc();
  47. text_input_state->text_input_view = text_input_get_view(text_input_state->text_input);
  48. text_input_state->callback = context->callback;
  49. text_input_state->callback_data = context->callback_data;
  50. text_input_set_header_text(text_input_state->text_input, context->header_text);
  51. text_input_set_result_callback(
  52. text_input_state->text_input,
  53. commit_text_input_callback,
  54. text_input_state,
  55. &text_input_state->text_input_buffer[0],
  56. INPUT_BUFFER_SIZE,
  57. true);
  58. return text_input_state;
  59. }
  60. void totp_input_text_render(Canvas* const canvas, InputTextSceneState* text_input_state) {
  61. view_draw(text_input_state->text_input_view, canvas);
  62. }
  63. bool totp_input_text_handle_event(PluginEvent* const event, InputTextSceneState* text_input_state) {
  64. if(event->type == EventTypeKey) {
  65. view_input(text_input_state->text_input_view, &event->input);
  66. }
  67. return true;
  68. }
  69. void totp_input_text_free(InputTextSceneState* state) {
  70. text_input_free(state->text_input);
  71. free(state);
  72. }