totp_input_text.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "totp_input_text.h"
  2. #include <gui/view_i.h>
  3. #include "../../../lib/polyfills/strnlen.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. furi_check(result != NULL);
  32. result->user_input_length =
  33. strnlen(text_input_state->text_input_buffer, INPUT_BUFFER_SIZE);
  34. result->user_input = malloc(result->user_input_length + 1);
  35. furi_check(result->user_input != NULL);
  36. result->callback_data = text_input_state->callback_data;
  37. strlcpy(
  38. result->user_input,
  39. text_input_state->text_input_buffer,
  40. result->user_input_length + 1);
  41. text_input_state->callback(result);
  42. }
  43. }
  44. InputTextSceneState* totp_input_text_activate(InputTextSceneContext* context) {
  45. InputTextSceneState* text_input_state = malloc(sizeof(InputTextSceneState));
  46. furi_check(text_input_state != NULL);
  47. text_input_state->text_input = text_input_alloc();
  48. text_input_state->text_input_view = text_input_get_view(text_input_state->text_input);
  49. text_input_state->callback = context->callback;
  50. text_input_state->callback_data = context->callback_data;
  51. text_input_set_header_text(text_input_state->text_input, context->header_text);
  52. text_input_set_result_callback(
  53. text_input_state->text_input,
  54. commit_text_input_callback,
  55. text_input_state,
  56. &text_input_state->text_input_buffer[0],
  57. INPUT_BUFFER_SIZE,
  58. true);
  59. return text_input_state;
  60. }
  61. void totp_input_text_render(Canvas* const canvas, InputTextSceneState* text_input_state) {
  62. view_draw(text_input_state->text_input_view, canvas);
  63. }
  64. bool totp_input_text_handle_event(PluginEvent* const event, InputTextSceneState* text_input_state) {
  65. if(event->type == EventTypeKey) {
  66. view_input(text_input_state->text_input_view, &event->input);
  67. }
  68. return true;
  69. }
  70. void totp_input_text_free(InputTextSceneState* state) {
  71. text_input_free(state->text_input);
  72. free(state);
  73. }