totp_app_settings.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include "totp_app_settings.h"
  2. #include <math.h>
  3. #include <totp_icons.h>
  4. #include "../../../services/fonts/font_provider.h"
  5. #include "../../canvas_extensions.h"
  6. #include "../../ui_controls.h"
  7. #include "../../common_dialogs.h"
  8. #include "../../scene_director.h"
  9. #include "../../constants.h"
  10. #include "../../../services/config/config.h"
  11. #include "../../../services/convert/convert.h"
  12. #include "../../../services/kb_layouts/kb_layout_provider.h"
  13. #include <roll_value.h>
  14. #include "../../../config/app/config.h"
  15. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  16. #include "../../../workers/bt_type_code/bt_type_code.h"
  17. #endif
  18. #define FONT_TEST_STR_LENGTH (7)
  19. static const char* YES_NO_LIST[] = {"NO", "YES"};
  20. static const char* AUTOMATION_LIST[] = {
  21. "None",
  22. "USB"
  23. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  24. ,
  25. "Bluetooth",
  26. "BT and USB"
  27. #endif
  28. };
  29. static const char* FONT_TEST_STR = "0123BCD";
  30. typedef enum {
  31. HoursInput,
  32. MinutesInput,
  33. FontSelect,
  34. SoundSwitch,
  35. VibroSwitch,
  36. AutomationSwitch,
  37. BadKeyboardLayoutSelect,
  38. AutomationDelaySelect,
  39. BadBtProfileSelect,
  40. ConfirmButton
  41. } Control;
  42. typedef struct {
  43. int8_t tz_offset_hours;
  44. uint8_t tz_offset_minutes;
  45. bool notification_sound;
  46. bool notification_vibro;
  47. AutomationMethod automation_method;
  48. uint16_t y_offset;
  49. AutomationKeyboardLayout automation_kb_layout;
  50. uint8_t automation_kb_layout_count;
  51. char automation_kb_layout_name[TOTP_KB_LAYOUT_NAME_MAX_LENGTH + 1];
  52. Control selected_control;
  53. uint8_t active_font_index;
  54. FontInfo* active_font;
  55. uint8_t total_fonts_count;
  56. uint16_t automation_initial_delay;
  57. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  58. uint8_t badbt_profile_index;
  59. #endif
  60. char automation_initial_delay_formatted[10];
  61. } SceneState;
  62. static void two_digit_to_str(int8_t num, char* str) {
  63. char* s = str;
  64. if(num < 0) {
  65. *(s++) = '-';
  66. num = -num;
  67. }
  68. uint8_t d1 = (num / 10) % 10;
  69. uint8_t d2 = num % 10;
  70. *(s++) = CONVERT_DIGIT_TO_CHAR(d1);
  71. *(s++) = CONVERT_DIGIT_TO_CHAR(d2);
  72. *(s++) = '\0';
  73. }
  74. static void update_formatted_automation_initial_delay(SceneState* scene_state) {
  75. snprintf(
  76. &scene_state->automation_initial_delay_formatted[0],
  77. sizeof(scene_state->automation_initial_delay_formatted),
  78. "%.1f sec.",
  79. (double)(scene_state->automation_initial_delay / 1000.0f));
  80. }
  81. static void update_formatted_automation_kb_layout_name(SceneState* scene_state) {
  82. totp_kb_layout_provider_get_layout_name(
  83. scene_state->automation_kb_layout,
  84. &scene_state->automation_kb_layout_name[0],
  85. sizeof(scene_state->automation_kb_layout_name));
  86. }
  87. void totp_scene_app_settings_activate(PluginState* plugin_state) {
  88. SceneState* scene_state = malloc(sizeof(SceneState));
  89. furi_check(scene_state != NULL);
  90. plugin_state->current_scene_state = scene_state;
  91. float off_int;
  92. float off_dec = modff(plugin_state->timezone_offset, &off_int);
  93. scene_state->tz_offset_hours = off_int;
  94. scene_state->tz_offset_minutes = 60.0f * off_dec;
  95. scene_state->notification_sound = plugin_state->notification_method & NotificationMethodSound;
  96. scene_state->notification_vibro = plugin_state->notification_method & NotificationMethodVibro;
  97. scene_state->automation_method =
  98. MIN(plugin_state->automation_method, COUNT_OF(AUTOMATION_LIST) - 1);
  99. scene_state->automation_kb_layout_count = totp_kb_layout_provider_get_layouts_count();
  100. scene_state->automation_kb_layout =
  101. MIN(plugin_state->automation_kb_layout, scene_state->automation_kb_layout_count - 1);
  102. scene_state->automation_initial_delay = plugin_state->automation_initial_delay;
  103. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  104. scene_state->badbt_profile_index = plugin_state->bt_type_code_worker_profile_index;
  105. #endif
  106. scene_state->total_fonts_count = totp_font_provider_get_fonts_count();
  107. scene_state->active_font_index = plugin_state->active_font_index;
  108. scene_state->active_font = totp_font_info_alloc();
  109. if(!totp_font_provider_get_font(scene_state->active_font_index, scene_state->active_font)) {
  110. scene_state->active_font_index = 0;
  111. totp_font_provider_get_font(scene_state->active_font_index, scene_state->active_font);
  112. }
  113. update_formatted_automation_initial_delay(scene_state);
  114. update_formatted_automation_kb_layout_name(scene_state);
  115. }
  116. void totp_scene_app_settings_render(Canvas* const canvas, const PluginState* plugin_state) {
  117. const SceneState* scene_state = plugin_state->current_scene_state;
  118. if(scene_state->selected_control < FontSelect) {
  119. canvas_set_font(canvas, FontPrimary);
  120. canvas_draw_str_aligned(
  121. canvas, 0, 0 - scene_state->y_offset, AlignLeft, AlignTop, "Timezone offset");
  122. canvas_set_font(canvas, FontSecondary);
  123. char tmp_str[4];
  124. two_digit_to_str(scene_state->tz_offset_hours, &tmp_str[0]);
  125. canvas_draw_str_aligned(
  126. canvas, 0, 17 - scene_state->y_offset, AlignLeft, AlignTop, "Hours:");
  127. ui_control_select_render(
  128. canvas,
  129. 36,
  130. 10 - scene_state->y_offset,
  131. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  132. &tmp_str[0],
  133. scene_state->selected_control == HoursInput);
  134. two_digit_to_str(scene_state->tz_offset_minutes, &tmp_str[0]);
  135. canvas_draw_str_aligned(
  136. canvas, 0, 35 - scene_state->y_offset, AlignLeft, AlignTop, "Minutes:");
  137. ui_control_select_render(
  138. canvas,
  139. 36,
  140. 28 - scene_state->y_offset,
  141. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  142. &tmp_str[0],
  143. scene_state->selected_control == MinutesInput);
  144. } else if(scene_state->selected_control < SoundSwitch) {
  145. canvas_set_font(canvas, FontPrimary);
  146. canvas_draw_str_aligned(
  147. canvas, 0, 64 - scene_state->y_offset, AlignLeft, AlignTop, "Font");
  148. canvas_set_font(canvas, FontSecondary);
  149. const FontInfo* const font = scene_state->active_font;
  150. ui_control_select_render(
  151. canvas,
  152. 0,
  153. 74 - scene_state->y_offset,
  154. SCREEN_WIDTH - UI_CONTROL_VSCROLL_WIDTH,
  155. font->name,
  156. scene_state->selected_control == FontSelect);
  157. uint8_t font_x_offset =
  158. SCREEN_WIDTH_CENTER -
  159. (((font->char_info[0].width + font->space_width) * FONT_TEST_STR_LENGTH) >> 1);
  160. uint8_t font_y_offset = 108 - scene_state->y_offset - (font->height >> 1);
  161. canvas_draw_str_ex(
  162. canvas, font_x_offset, font_y_offset, FONT_TEST_STR, FONT_TEST_STR_LENGTH, font);
  163. } else if(scene_state->selected_control < AutomationSwitch) {
  164. canvas_set_font(canvas, FontPrimary);
  165. canvas_draw_str_aligned(
  166. canvas, 0, 128 - scene_state->y_offset, AlignLeft, AlignTop, "Notifications");
  167. canvas_set_font(canvas, FontSecondary);
  168. canvas_draw_str_aligned(
  169. canvas, 0, 145 - scene_state->y_offset, AlignLeft, AlignTop, "Sound:");
  170. ui_control_select_render(
  171. canvas,
  172. 36,
  173. 138 - scene_state->y_offset,
  174. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  175. YES_NO_LIST[scene_state->notification_sound],
  176. scene_state->selected_control == SoundSwitch);
  177. canvas_draw_str_aligned(
  178. canvas, 0, 163 - scene_state->y_offset, AlignLeft, AlignTop, "Vibro:");
  179. ui_control_select_render(
  180. canvas,
  181. 36,
  182. 156 - scene_state->y_offset,
  183. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  184. YES_NO_LIST[scene_state->notification_vibro],
  185. scene_state->selected_control == VibroSwitch);
  186. } else {
  187. canvas_set_font(canvas, FontPrimary);
  188. canvas_draw_str_aligned(
  189. canvas, 0, 192 - scene_state->y_offset, AlignLeft, AlignTop, "Automation");
  190. canvas_set_font(canvas, FontSecondary);
  191. int group_offset = 0;
  192. if(scene_state->selected_control <= BadKeyboardLayoutSelect) {
  193. canvas_draw_str_aligned(
  194. canvas,
  195. 0,
  196. 209 - scene_state->y_offset - group_offset,
  197. AlignLeft,
  198. AlignTop,
  199. "Method:");
  200. ui_control_select_render(
  201. canvas,
  202. 36,
  203. 202 - scene_state->y_offset - group_offset,
  204. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  205. AUTOMATION_LIST[scene_state->automation_method],
  206. scene_state->selected_control == AutomationSwitch);
  207. } else {
  208. group_offset += 18;
  209. }
  210. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  211. if(scene_state->selected_control <= AutomationDelaySelect ||
  212. (scene_state->automation_method & AutomationMethodBadBt) == 0) {
  213. #endif
  214. canvas_draw_str_aligned(
  215. canvas,
  216. 0,
  217. 227 - scene_state->y_offset - group_offset,
  218. AlignLeft,
  219. AlignTop,
  220. "Layout:");
  221. ui_control_select_render(
  222. canvas,
  223. 36,
  224. 220 - scene_state->y_offset - group_offset,
  225. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  226. scene_state->automation_kb_layout_name,
  227. scene_state->selected_control == BadKeyboardLayoutSelect);
  228. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  229. } else if((scene_state->automation_method & AutomationMethodBadBt) != 0) {
  230. group_offset += 18;
  231. }
  232. #endif
  233. canvas_draw_str_aligned(
  234. canvas, 0, 245 - scene_state->y_offset - group_offset, AlignLeft, AlignTop, "Delay:");
  235. ui_control_select_render(
  236. canvas,
  237. 36,
  238. 238 - scene_state->y_offset - group_offset,
  239. SCREEN_WIDTH - 36 - UI_CONTROL_VSCROLL_WIDTH,
  240. &scene_state->automation_initial_delay_formatted[0],
  241. scene_state->selected_control == AutomationDelaySelect);
  242. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  243. if((scene_state->automation_method & AutomationMethodBadBt) != 0) {
  244. canvas_draw_str_aligned(
  245. canvas,
  246. 0,
  247. 263 - scene_state->y_offset - group_offset,
  248. AlignLeft,
  249. AlignTop,
  250. "BT Profile:");
  251. char profile_index_formatted[4];
  252. snprintf(profile_index_formatted, 4, "%d", scene_state->badbt_profile_index);
  253. ui_control_select_render(
  254. canvas,
  255. 42,
  256. 256 - scene_state->y_offset - group_offset,
  257. SCREEN_WIDTH - 42 - UI_CONTROL_VSCROLL_WIDTH,
  258. &profile_index_formatted[0],
  259. scene_state->selected_control == BadBtProfileSelect);
  260. }
  261. #endif
  262. ui_control_button_render(
  263. canvas,
  264. SCREEN_WIDTH_CENTER - 24,
  265. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  266. ((scene_state->automation_method & AutomationMethodBadBt) == 0 ? 260 : 278)
  267. #else
  268. 260
  269. #endif
  270. - scene_state->y_offset - group_offset,
  271. 48,
  272. 13,
  273. "Confirm",
  274. scene_state->selected_control == ConfirmButton);
  275. }
  276. ui_control_vscroll_render(
  277. canvas, SCREEN_WIDTH - 3, 0, SCREEN_HEIGHT, scene_state->selected_control, ConfirmButton);
  278. }
  279. bool totp_scene_app_settings_handle_event(
  280. const PluginEvent* const event,
  281. PluginState* plugin_state) {
  282. if(event->type != EventTypeKey) {
  283. return true;
  284. }
  285. SceneState* scene_state = (SceneState*)plugin_state->current_scene_state;
  286. if(event->input.type == InputTypePress || event->input.type == InputTypeRepeat) {
  287. switch(event->input.key) {
  288. case InputKeyUp:
  289. totp_roll_value_uint8_t(
  290. &scene_state->selected_control,
  291. -1,
  292. HoursInput,
  293. ConfirmButton,
  294. RollOverflowBehaviorStop);
  295. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  296. if((scene_state->automation_method & AutomationMethodBadBt) == 0 &&
  297. scene_state->selected_control == BadBtProfileSelect) {
  298. scene_state->selected_control--;
  299. }
  300. #endif
  301. if(scene_state->selected_control > VibroSwitch) {
  302. scene_state->y_offset = SCREEN_HEIGHT * 3;
  303. } else if(scene_state->selected_control > FontSelect) {
  304. scene_state->y_offset = SCREEN_HEIGHT * 2;
  305. } else if(scene_state->selected_control > MinutesInput) {
  306. scene_state->y_offset = SCREEN_HEIGHT;
  307. } else {
  308. scene_state->y_offset = 0;
  309. }
  310. break;
  311. case InputKeyDown:
  312. totp_roll_value_uint8_t(
  313. &scene_state->selected_control,
  314. 1,
  315. HoursInput,
  316. ConfirmButton,
  317. RollOverflowBehaviorStop);
  318. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  319. if((scene_state->automation_method & AutomationMethodBadBt) == 0 &&
  320. scene_state->selected_control == BadBtProfileSelect) {
  321. scene_state->selected_control++;
  322. }
  323. #endif
  324. if(scene_state->selected_control > VibroSwitch) {
  325. scene_state->y_offset = SCREEN_HEIGHT * 3;
  326. } else if(scene_state->selected_control > FontSelect) {
  327. scene_state->y_offset = SCREEN_HEIGHT * 2;
  328. } else if(scene_state->selected_control > MinutesInput) {
  329. scene_state->y_offset = SCREEN_HEIGHT;
  330. } else {
  331. scene_state->y_offset = 0;
  332. }
  333. break;
  334. case InputKeyRight:
  335. if(scene_state->selected_control == HoursInput) {
  336. totp_roll_value_int8_t(
  337. &scene_state->tz_offset_hours, 1, -12, 12, RollOverflowBehaviorStop);
  338. } else if(scene_state->selected_control == MinutesInput) {
  339. totp_roll_value_uint8_t(
  340. &scene_state->tz_offset_minutes, 15, 0, 45, RollOverflowBehaviorRoll);
  341. } else if(scene_state->selected_control == FontSelect) {
  342. totp_roll_value_uint8_t(
  343. &scene_state->active_font_index,
  344. 1,
  345. 0,
  346. scene_state->total_fonts_count - 1,
  347. RollOverflowBehaviorRoll);
  348. totp_font_provider_get_font(
  349. scene_state->active_font_index, scene_state->active_font);
  350. } else if(scene_state->selected_control == SoundSwitch) {
  351. scene_state->notification_sound = !scene_state->notification_sound;
  352. } else if(scene_state->selected_control == VibroSwitch) {
  353. scene_state->notification_vibro = !scene_state->notification_vibro;
  354. } else if(scene_state->selected_control == AutomationSwitch) {
  355. totp_roll_value_uint8_t(
  356. &scene_state->automation_method,
  357. 1,
  358. 0,
  359. COUNT_OF(AUTOMATION_LIST) - 1,
  360. RollOverflowBehaviorRoll);
  361. } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
  362. totp_roll_value_uint8_t(
  363. &scene_state->automation_kb_layout,
  364. 1,
  365. 0,
  366. scene_state->automation_kb_layout_count - 1,
  367. RollOverflowBehaviorRoll);
  368. update_formatted_automation_kb_layout_name(scene_state);
  369. } else if(scene_state->selected_control == AutomationDelaySelect) {
  370. totp_roll_value_uint16_t(
  371. &scene_state->automation_initial_delay,
  372. 500,
  373. 0,
  374. 60000,
  375. RollOverflowBehaviorStop);
  376. update_formatted_automation_initial_delay(scene_state);
  377. }
  378. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  379. else if(scene_state->selected_control == BadBtProfileSelect) {
  380. totp_roll_value_uint8_t(
  381. &scene_state->badbt_profile_index, 1, 0, UINT8_MAX, RollOverflowBehaviorRoll);
  382. }
  383. #endif
  384. break;
  385. case InputKeyLeft:
  386. if(scene_state->selected_control == HoursInput) {
  387. totp_roll_value_int8_t(
  388. &scene_state->tz_offset_hours, -1, -12, 12, RollOverflowBehaviorStop);
  389. } else if(scene_state->selected_control == MinutesInput) {
  390. totp_roll_value_uint8_t(
  391. &scene_state->tz_offset_minutes, -15, 0, 45, RollOverflowBehaviorRoll);
  392. } else if(scene_state->selected_control == FontSelect) {
  393. totp_roll_value_uint8_t(
  394. &scene_state->active_font_index,
  395. -1,
  396. 0,
  397. scene_state->total_fonts_count - 1,
  398. RollOverflowBehaviorRoll);
  399. totp_font_provider_get_font(
  400. scene_state->active_font_index, scene_state->active_font);
  401. } else if(scene_state->selected_control == SoundSwitch) {
  402. scene_state->notification_sound = !scene_state->notification_sound;
  403. } else if(scene_state->selected_control == VibroSwitch) {
  404. scene_state->notification_vibro = !scene_state->notification_vibro;
  405. } else if(scene_state->selected_control == AutomationSwitch) {
  406. totp_roll_value_uint8_t(
  407. &scene_state->automation_method,
  408. -1,
  409. 0,
  410. COUNT_OF(AUTOMATION_LIST) - 1,
  411. RollOverflowBehaviorRoll);
  412. } else if(scene_state->selected_control == BadKeyboardLayoutSelect) {
  413. totp_roll_value_uint8_t(
  414. &scene_state->automation_kb_layout,
  415. -1,
  416. 0,
  417. scene_state->automation_kb_layout_count - 1,
  418. RollOverflowBehaviorRoll);
  419. update_formatted_automation_kb_layout_name(scene_state);
  420. } else if(scene_state->selected_control == AutomationDelaySelect) {
  421. totp_roll_value_uint16_t(
  422. &scene_state->automation_initial_delay,
  423. -500,
  424. 0,
  425. 60000,
  426. RollOverflowBehaviorStop);
  427. update_formatted_automation_initial_delay(scene_state);
  428. }
  429. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  430. else if(scene_state->selected_control == BadBtProfileSelect) {
  431. totp_roll_value_uint8_t(
  432. &scene_state->badbt_profile_index, -1, 0, UINT8_MAX, RollOverflowBehaviorRoll);
  433. }
  434. #endif
  435. break;
  436. case InputKeyOk:
  437. if(scene_state->selected_control == ConfirmButton) {
  438. plugin_state->timezone_offset = (float)scene_state->tz_offset_hours +
  439. (float)scene_state->tz_offset_minutes / 60.0f;
  440. plugin_state->notification_method =
  441. (scene_state->notification_sound ? NotificationMethodSound :
  442. NotificationMethodNone) |
  443. (scene_state->notification_vibro ? NotificationMethodVibro :
  444. NotificationMethodNone);
  445. plugin_state->automation_method = scene_state->automation_method;
  446. plugin_state->active_font_index = scene_state->active_font_index;
  447. plugin_state->automation_kb_layout = scene_state->automation_kb_layout;
  448. plugin_state->automation_initial_delay = scene_state->automation_initial_delay;
  449. #ifdef TOTP_BADBT_AUTOMATION_ENABLED
  450. if(((scene_state->automation_method & AutomationMethodBadBt) == 0 ||
  451. plugin_state->bt_type_code_worker_profile_index !=
  452. scene_state->badbt_profile_index) &&
  453. plugin_state->bt_type_code_worker_context != NULL) {
  454. totp_bt_type_code_worker_free(plugin_state->bt_type_code_worker_context);
  455. plugin_state->bt_type_code_worker_context = NULL;
  456. }
  457. plugin_state->bt_type_code_worker_profile_index = scene_state->badbt_profile_index;
  458. #endif
  459. if(!totp_config_file_update_user_settings(plugin_state)) {
  460. totp_dialogs_config_updating_error(plugin_state);
  461. return false;
  462. }
  463. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  464. }
  465. break;
  466. case InputKeyBack: {
  467. totp_scene_director_activate_scene(plugin_state, TotpSceneTokenMenu);
  468. break;
  469. }
  470. default:
  471. break;
  472. }
  473. }
  474. return true;
  475. }
  476. void totp_scene_app_settings_deactivate(PluginState* plugin_state) {
  477. if(plugin_state->current_scene_state == NULL) return;
  478. SceneState* scene_state = plugin_state->current_scene_state;
  479. totp_font_info_free(scene_state->active_font);
  480. free(scene_state);
  481. plugin_state->current_scene_state = NULL;
  482. }