totp_input_text.c 2.6 KB

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