totp_app_settings.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "totp_app_settings.h"
  2. #include "../../services/ui/ui_controls.h"
  3. #include "../scene_director.h"
  4. #include "../token_menu/totp_scene_token_menu.h"
  5. #include "../../services/ui/constants.h"
  6. #include "../../services/config/config.h"
  7. #define DIGIT_TO_CHAR(digit) ((digit) + '0')
  8. typedef enum {
  9. HoursInput,
  10. MinutesInput,
  11. ConfirmButton
  12. } Control;
  13. typedef struct {
  14. int8_t tz_offset_hours;
  15. uint8_t tz_offset_minutes;
  16. int16_t current_token_index;
  17. Control selected_control;
  18. } SceneState;
  19. void totp_scene_app_settings_init(PluginState* plugin_state) {
  20. UNUSED(plugin_state);
  21. }
  22. void totp_scene_app_settings_activate(PluginState* plugin_state, const AppSettingsSceneContext* context) {
  23. SceneState* scene_state = malloc(sizeof(SceneState));
  24. plugin_state->current_scene_state = scene_state;
  25. if (context != NULL) {
  26. scene_state->current_token_index = context->current_token_index;
  27. } else {
  28. scene_state->current_token_index = -1;
  29. }
  30. float off_int;
  31. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  32. scene_state->tz_offset_hours = off_int;
  33. scene_state->tz_offset_minutes = 60.0f * off_dec;
  34. }
  35. static void two_digit_to_str(int8_t num, char* str) {
  36. uint8_t index = 0;
  37. if (num < 0) {
  38. str[0] = '-';
  39. index++;
  40. num = -num;
  41. }
  42. uint8_t d1 = (num / 10) % 10;
  43. uint8_t d2 = num % 10;
  44. str[index] = DIGIT_TO_CHAR(d1);
  45. str[index + 1] = DIGIT_TO_CHAR(d2);
  46. str[index + 2] = '\0';
  47. }
  48. void totp_scene_app_settings_render(Canvas* const canvas, PluginState* plugin_state) {
  49. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  50. canvas_set_font(canvas, FontPrimary);
  51. canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "Timezone offset");
  52. canvas_set_font(canvas, FontSecondary);
  53. char tmp_str[4];
  54. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  55. canvas_draw_str_aligned(canvas, 0, 16, AlignLeft, AlignTop, "Hours:");
  56. ui_control_select_render(canvas, 36, 10, SCREEN_WIDTH - 36, &tmp_str[0], scene_state->selected_control == HoursInput);
  57. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  58. canvas_draw_str_aligned(canvas, 0, 34, AlignLeft, AlignTop, "Minutes:");
  59. ui_control_select_render(canvas, 36, 28, SCREEN_WIDTH - 36, &tmp_str[0], scene_state->selected_control == MinutesInput);
  60. ui_control_button_render(canvas, SCREEN_WIDTH_CENTER - 24, 50, 48, 13, "Confirm", scene_state->selected_control == ConfirmButton);
  61. }
  62. bool totp_scene_app_settings_handle_event(PluginEvent* const event, PluginState* plugin_state) {
  63. if (event->type == EventTypeKey) {
  64. SceneState* scene_state = (SceneState *)plugin_state->current_scene_state;
  65. if(event->input.type == InputTypePress) {
  66. switch(event->input.key) {
  67. case InputKeyUp:
  68. if (scene_state->selected_control > HoursInput) {
  69. scene_state->selected_control--;
  70. }
  71. break;
  72. case InputKeyDown:
  73. if (scene_state->selected_control < ConfirmButton) {
  74. scene_state->selected_control++;
  75. }
  76. break;
  77. case InputKeyRight:
  78. if (scene_state->selected_control == HoursInput) {
  79. if (scene_state->tz_offset_hours < 12) {
  80. scene_state->tz_offset_hours++;
  81. }
  82. } else if (scene_state->selected_control == MinutesInput) {
  83. if (scene_state->tz_offset_minutes < 45) {
  84. scene_state->tz_offset_minutes += 15;
  85. } else {
  86. scene_state->tz_offset_minutes = 0;
  87. }
  88. }
  89. break;
  90. case InputKeyLeft:
  91. if (scene_state->selected_control == HoursInput) {
  92. if (scene_state->tz_offset_hours > -12) {
  93. scene_state->tz_offset_hours--;
  94. }
  95. } else if (scene_state->selected_control == MinutesInput) {
  96. if (scene_state->tz_offset_minutes >= 15) {
  97. scene_state->tz_offset_minutes -= 15;
  98. } else {
  99. scene_state->tz_offset_minutes = 45;
  100. }
  101. }
  102. break;
  103. case InputKeyOk:
  104. if (scene_state->selected_control == ConfirmButton) {
  105. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours + (float)scene_state->tz_offset_minutes / 60.0f;
  106. totp_config_file_update_timezone_offset(plugin_state->timezone_offset);
  107. if (scene_state->current_token_index >= 0) {
  108. TokenMenuSceneContext generate_scene_context = { .current_token_index = scene_state->current_token_index };
  109. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  110. } else {
  111. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  112. }
  113. }
  114. break;
  115. case InputKeyBack: {
  116. if (scene_state->current_token_index >= 0) {
  117. TokenMenuSceneContext generate_scene_context = { .current_token_index = scene_state->current_token_index };
  118. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, &generate_scene_context);
  119. } else {
  120. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu, NULL);
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. return true;
  128. }
  129. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  130. if (plugin_state->current_scene_state == NULL) return;
  131. free(plugin_state->current_scene_state);
  132. plugin_state->current_scene_state = NULL;
  133. }
  134. void totp_scene_app_settings_free(PluginState* plugin_state) {
  135. UNUSED(plugin_state);
  136. }