totp_input_text.c 3.0 KB

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