text_input.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. #include "text_input.h"
  2. #include <gui/elements.h>
  3. #include <furi.h>
  4. struct TextInput {
  5. View* view;
  6. osTimerId_t timer;
  7. };
  8. typedef struct {
  9. const char text;
  10. const uint8_t x;
  11. const uint8_t y;
  12. } TextInputKey;
  13. typedef struct {
  14. const char* header;
  15. char* text_buffer;
  16. size_t text_buffer_size;
  17. bool clear_default_text;
  18. TextInputCallback callback;
  19. void* callback_context;
  20. uint8_t selected_row;
  21. uint8_t selected_column;
  22. TextInputValidatorCallback validator_callback;
  23. void* validator_callback_context;
  24. string_t validator_text;
  25. bool valadator_message_visible;
  26. } TextInputModel;
  27. static const uint8_t keyboard_origin_x = 1;
  28. static const uint8_t keyboard_origin_y = 29;
  29. static const uint8_t keyboard_row_count = 3;
  30. #define ENTER_KEY '\r'
  31. #define BACKSPACE_KEY '\b'
  32. static const TextInputKey keyboard_keys_row_1[] = {
  33. {'q', 1, 8},
  34. {'w', 10, 8},
  35. {'e', 19, 8},
  36. {'r', 28, 8},
  37. {'t', 37, 8},
  38. {'y', 46, 8},
  39. {'u', 55, 8},
  40. {'i', 64, 8},
  41. {'o', 73, 8},
  42. {'p', 82, 8},
  43. {'0', 91, 8},
  44. {'1', 100, 8},
  45. {'2', 110, 8},
  46. {'3', 120, 8},
  47. };
  48. static const TextInputKey keyboard_keys_row_2[] = {
  49. {'a', 1, 20},
  50. {'s', 10, 20},
  51. {'d', 19, 20},
  52. {'f', 28, 20},
  53. {'g', 37, 20},
  54. {'h', 46, 20},
  55. {'j', 55, 20},
  56. {'k', 64, 20},
  57. {'l', 73, 20},
  58. {BACKSPACE_KEY, 82, 12},
  59. {'4', 100, 20},
  60. {'5', 110, 20},
  61. {'6', 120, 20},
  62. };
  63. static const TextInputKey keyboard_keys_row_3[] = {
  64. {'z', 1, 32},
  65. {'x', 10, 32},
  66. {'c', 19, 32},
  67. {'v', 28, 32},
  68. {'b', 37, 32},
  69. {'n', 46, 32},
  70. {'m', 55, 32},
  71. {'_', 64, 32},
  72. {ENTER_KEY, 74, 23},
  73. {'7', 100, 32},
  74. {'8', 110, 32},
  75. {'9', 120, 32},
  76. };
  77. static uint8_t get_row_size(uint8_t row_index) {
  78. uint8_t row_size = 0;
  79. switch(row_index + 1) {
  80. case 1:
  81. row_size = sizeof(keyboard_keys_row_1) / sizeof(TextInputKey);
  82. break;
  83. case 2:
  84. row_size = sizeof(keyboard_keys_row_2) / sizeof(TextInputKey);
  85. break;
  86. case 3:
  87. row_size = sizeof(keyboard_keys_row_3) / sizeof(TextInputKey);
  88. break;
  89. }
  90. return row_size;
  91. }
  92. static const TextInputKey* get_row(uint8_t row_index) {
  93. const TextInputKey* row = NULL;
  94. switch(row_index + 1) {
  95. case 1:
  96. row = keyboard_keys_row_1;
  97. break;
  98. case 2:
  99. row = keyboard_keys_row_2;
  100. break;
  101. case 3:
  102. row = keyboard_keys_row_3;
  103. break;
  104. }
  105. return row;
  106. }
  107. static const char get_selected_char(TextInputModel* model) {
  108. return get_row(model->selected_row)[model->selected_column].text;
  109. }
  110. static const bool char_is_lowercase(char letter) {
  111. return (letter >= 0x61 && letter <= 0x7A);
  112. }
  113. static const char char_to_uppercase(const char letter) {
  114. if(isalpha(letter)) {
  115. return (letter - 0x20);
  116. } else {
  117. return letter;
  118. }
  119. }
  120. static void text_input_backspace_cb(TextInputModel* model) {
  121. uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer);
  122. if(text_length > 0) {
  123. model->text_buffer[text_length - 1] = 0;
  124. }
  125. }
  126. static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
  127. TextInputModel* model = _model;
  128. uint8_t text_length = strlen(model->text_buffer);
  129. uint8_t needed_string_width = canvas_width(canvas) - 8;
  130. uint8_t start_pos = 4;
  131. const char* text = model->text_buffer;
  132. canvas_clear(canvas);
  133. canvas_set_color(canvas, ColorBlack);
  134. canvas_draw_str(canvas, 2, 8, model->header);
  135. elements_slightly_rounded_frame(canvas, 1, 12, 126, 15);
  136. if(canvas_string_width(canvas, text) > needed_string_width) {
  137. canvas_draw_str(canvas, start_pos, 22, "...");
  138. start_pos += 6;
  139. needed_string_width -= 8;
  140. }
  141. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  142. text++;
  143. }
  144. if(model->clear_default_text) {
  145. elements_slightly_rounded_box(
  146. canvas, start_pos - 1, 14, canvas_string_width(canvas, text) + 2, 10);
  147. canvas_set_color(canvas, ColorWhite);
  148. } else {
  149. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 22, "|");
  150. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 22, "|");
  151. }
  152. canvas_draw_str(canvas, start_pos, 22, text);
  153. canvas_set_font(canvas, FontKeyboard);
  154. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  155. const uint8_t column_count = get_row_size(row);
  156. const TextInputKey* keys = get_row(row);
  157. for(size_t column = 0; column < column_count; column++) {
  158. if(keys[column].text == ENTER_KEY) {
  159. canvas_set_color(canvas, ColorBlack);
  160. if(model->selected_row == row && model->selected_column == column) {
  161. canvas_draw_icon(
  162. canvas,
  163. keyboard_origin_x + keys[column].x,
  164. keyboard_origin_y + keys[column].y,
  165. &I_KeySaveSelected_24x11);
  166. } else {
  167. canvas_draw_icon(
  168. canvas,
  169. keyboard_origin_x + keys[column].x,
  170. keyboard_origin_y + keys[column].y,
  171. &I_KeySave_24x11);
  172. }
  173. } else if(keys[column].text == BACKSPACE_KEY) {
  174. canvas_set_color(canvas, ColorBlack);
  175. if(model->selected_row == row && model->selected_column == column) {
  176. canvas_draw_icon(
  177. canvas,
  178. keyboard_origin_x + keys[column].x,
  179. keyboard_origin_y + keys[column].y,
  180. &I_KeyBackspaceSelected_16x9);
  181. } else {
  182. canvas_draw_icon(
  183. canvas,
  184. keyboard_origin_x + keys[column].x,
  185. keyboard_origin_y + keys[column].y,
  186. &I_KeyBackspace_16x9);
  187. }
  188. } else {
  189. if(model->selected_row == row && model->selected_column == column) {
  190. canvas_set_color(canvas, ColorBlack);
  191. canvas_draw_box(
  192. canvas,
  193. keyboard_origin_x + keys[column].x - 1,
  194. keyboard_origin_y + keys[column].y - 8,
  195. 7,
  196. 10);
  197. canvas_set_color(canvas, ColorWhite);
  198. } else {
  199. canvas_set_color(canvas, ColorBlack);
  200. }
  201. if(text_length == 0 && char_is_lowercase(keys[column].text)) {
  202. canvas_draw_glyph(
  203. canvas,
  204. keyboard_origin_x + keys[column].x,
  205. keyboard_origin_y + keys[column].y,
  206. char_to_uppercase(keys[column].text));
  207. } else {
  208. canvas_draw_glyph(
  209. canvas,
  210. keyboard_origin_x + keys[column].x,
  211. keyboard_origin_y + keys[column].y,
  212. keys[column].text);
  213. }
  214. }
  215. }
  216. }
  217. if(model->valadator_message_visible) {
  218. canvas_set_font(canvas, FontSecondary);
  219. canvas_set_color(canvas, ColorWhite);
  220. canvas_draw_box(canvas, 8, 10, 110, 48);
  221. canvas_set_color(canvas, ColorBlack);
  222. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  223. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  224. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  225. elements_multiline_text(canvas, 62, 20, string_get_cstr(model->validator_text));
  226. canvas_set_font(canvas, FontKeyboard);
  227. }
  228. }
  229. static void text_input_handle_up(TextInput* text_input) {
  230. with_view_model(
  231. text_input->view, (TextInputModel * model) {
  232. if(model->selected_row > 0) {
  233. model->selected_row--;
  234. if(model->selected_column > get_row_size(model->selected_row) - 6) {
  235. model->selected_column = model->selected_column + 1;
  236. }
  237. }
  238. return true;
  239. });
  240. }
  241. static void text_input_handle_down(TextInput* text_input) {
  242. with_view_model(
  243. text_input->view, (TextInputModel * model) {
  244. if(model->selected_row < keyboard_row_count - 1) {
  245. model->selected_row++;
  246. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  247. model->selected_column = model->selected_column - 1;
  248. }
  249. }
  250. return true;
  251. });
  252. }
  253. static void text_input_handle_left(TextInput* text_input) {
  254. with_view_model(
  255. text_input->view, (TextInputModel * model) {
  256. if(model->selected_column > 0) {
  257. model->selected_column--;
  258. } else {
  259. model->selected_column = get_row_size(model->selected_row) - 1;
  260. }
  261. return true;
  262. });
  263. }
  264. static void text_input_handle_right(TextInput* text_input) {
  265. with_view_model(
  266. text_input->view, (TextInputModel * model) {
  267. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  268. model->selected_column++;
  269. } else {
  270. model->selected_column = 0;
  271. }
  272. return true;
  273. });
  274. }
  275. static void text_input_handle_ok(TextInput* text_input, bool shift) {
  276. with_view_model(
  277. text_input->view, (TextInputModel * model) {
  278. char selected = get_selected_char(model);
  279. uint8_t text_length = strlen(model->text_buffer);
  280. if(shift) {
  281. selected = char_to_uppercase(selected);
  282. }
  283. if(selected == ENTER_KEY) {
  284. if(model->validator_callback && (!model->validator_callback(
  285. model->text_buffer,
  286. model->validator_text,
  287. model->validator_callback_context))) {
  288. model->valadator_message_visible = true;
  289. osTimerStart(text_input->timer, osKernelGetTickFreq() * 4);
  290. } else if(model->callback != 0 && text_length > 0) {
  291. model->callback(model->callback_context);
  292. }
  293. } else if(selected == BACKSPACE_KEY) {
  294. text_input_backspace_cb(model);
  295. } else if(text_length < (model->text_buffer_size - 1)) {
  296. if(model->clear_default_text) {
  297. text_length = 0;
  298. }
  299. if(text_length == 0 && char_is_lowercase(selected)) {
  300. selected = char_to_uppercase(selected);
  301. }
  302. model->text_buffer[text_length] = selected;
  303. model->text_buffer[text_length + 1] = 0;
  304. }
  305. model->clear_default_text = false;
  306. return true;
  307. });
  308. }
  309. static bool text_input_view_input_callback(InputEvent* event, void* context) {
  310. TextInput* text_input = context;
  311. furi_assert(text_input);
  312. bool consumed = false;
  313. if(event->type == InputTypeShort) {
  314. with_view_model(
  315. text_input->view, (TextInputModel * model) {
  316. if(model->valadator_message_visible) {
  317. if(event->key == InputKeyBack) {
  318. consumed = true;
  319. }
  320. }
  321. model->valadator_message_visible = false;
  322. return false;
  323. });
  324. switch(event->key) {
  325. case InputKeyUp:
  326. text_input_handle_up(text_input);
  327. consumed = true;
  328. break;
  329. case InputKeyDown:
  330. text_input_handle_down(text_input);
  331. consumed = true;
  332. break;
  333. case InputKeyLeft:
  334. text_input_handle_left(text_input);
  335. consumed = true;
  336. break;
  337. case InputKeyRight:
  338. text_input_handle_right(text_input);
  339. consumed = true;
  340. break;
  341. case InputKeyOk:
  342. text_input_handle_ok(text_input, false);
  343. consumed = true;
  344. break;
  345. default:
  346. break;
  347. }
  348. }
  349. if((event->type == InputTypeLong || event->type == InputTypeRepeat) &&
  350. event->key == InputKeyBack) {
  351. with_view_model(
  352. text_input->view, (TextInputModel * model) {
  353. if(model->valadator_message_visible) {
  354. model->valadator_message_visible = false;
  355. } else {
  356. text_input_backspace_cb(model);
  357. }
  358. return true;
  359. });
  360. consumed = true;
  361. }
  362. // Allow shift key on long press
  363. if(event->type == InputTypeLong && event->key == InputKeyOk) {
  364. with_view_model(
  365. text_input->view, (TextInputModel * model) {
  366. if(model->valadator_message_visible) {
  367. if(event->key == InputKeyBack) {
  368. consumed = true;
  369. }
  370. }
  371. model->valadator_message_visible = false;
  372. return false;
  373. });
  374. switch(event->key) {
  375. case InputKeyOk:
  376. text_input_handle_ok(text_input, true);
  377. consumed = true;
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. return consumed;
  384. }
  385. void text_input_timer_callback(void* context) {
  386. furi_assert(context);
  387. TextInput* text_input = context;
  388. with_view_model(
  389. text_input->view, (TextInputModel * model) {
  390. model->valadator_message_visible = false;
  391. return true;
  392. });
  393. }
  394. TextInput* text_input_alloc() {
  395. TextInput* text_input = malloc(sizeof(TextInput));
  396. text_input->view = view_alloc();
  397. view_set_context(text_input->view, text_input);
  398. view_allocate_model(text_input->view, ViewModelTypeLocking, sizeof(TextInputModel));
  399. view_set_draw_callback(text_input->view, text_input_view_draw_callback);
  400. view_set_input_callback(text_input->view, text_input_view_input_callback);
  401. text_input->timer = osTimerNew(text_input_timer_callback, osTimerOnce, text_input, NULL);
  402. with_view_model(
  403. text_input->view, (TextInputModel * model) {
  404. string_init(model->validator_text);
  405. return false;
  406. });
  407. text_input_reset(text_input);
  408. return text_input;
  409. }
  410. void text_input_free(TextInput* text_input) {
  411. furi_assert(text_input);
  412. with_view_model(
  413. text_input->view, (TextInputModel * model) {
  414. string_clear(model->validator_text);
  415. return false;
  416. });
  417. // Send stop command
  418. osTimerStop(text_input->timer);
  419. // Wait till timer stop
  420. while(osTimerIsRunning(text_input->timer)) osDelay(1);
  421. // Release allocated memory
  422. osTimerDelete(text_input->timer);
  423. view_free(text_input->view);
  424. free(text_input);
  425. }
  426. void text_input_reset(TextInput* text_input) {
  427. furi_assert(text_input);
  428. with_view_model(
  429. text_input->view, (TextInputModel * model) {
  430. model->text_buffer_size = 0;
  431. model->header = "";
  432. model->selected_row = 0;
  433. model->selected_column = 0;
  434. model->clear_default_text = false;
  435. model->text_buffer = NULL;
  436. model->text_buffer_size = 0;
  437. model->callback = NULL;
  438. model->callback_context = NULL;
  439. model->validator_callback = NULL;
  440. model->validator_callback_context = NULL;
  441. string_reset(model->validator_text);
  442. model->valadator_message_visible = false;
  443. return true;
  444. });
  445. }
  446. View* text_input_get_view(TextInput* text_input) {
  447. furi_assert(text_input);
  448. return text_input->view;
  449. }
  450. void text_input_set_result_callback(
  451. TextInput* text_input,
  452. TextInputCallback callback,
  453. void* callback_context,
  454. char* text_buffer,
  455. size_t text_buffer_size,
  456. bool clear_default_text) {
  457. with_view_model(
  458. text_input->view, (TextInputModel * model) {
  459. model->callback = callback;
  460. model->callback_context = callback_context;
  461. model->text_buffer = text_buffer;
  462. model->text_buffer_size = text_buffer_size;
  463. model->clear_default_text = clear_default_text;
  464. if(text_buffer && text_buffer[0] != '\0') {
  465. // Set focus on Save
  466. model->selected_row = 2;
  467. model->selected_column = 8;
  468. }
  469. return true;
  470. });
  471. }
  472. void text_input_set_validator(
  473. TextInput* text_input,
  474. TextInputValidatorCallback callback,
  475. void* callback_context) {
  476. with_view_model(
  477. text_input->view, (TextInputModel * model) {
  478. model->validator_callback = callback;
  479. model->validator_callback_context = callback_context;
  480. return true;
  481. });
  482. }
  483. TextInputValidatorCallback text_input_get_validator_callback(TextInput* text_input) {
  484. TextInputValidatorCallback validator_callback = NULL;
  485. with_view_model(
  486. text_input->view, (TextInputModel * model) {
  487. validator_callback = model->validator_callback;
  488. return false;
  489. });
  490. return validator_callback;
  491. }
  492. void* text_input_get_validator_callback_context(TextInput* text_input) {
  493. void* validator_callback_context = NULL;
  494. with_view_model(
  495. text_input->view, (TextInputModel * model) {
  496. validator_callback_context = model->validator_callback_context;
  497. return false;
  498. });
  499. return validator_callback_context;
  500. }
  501. void text_input_set_header_text(TextInput* text_input, const char* text) {
  502. with_view_model(
  503. text_input->view, (TextInputModel * model) {
  504. model->header = text;
  505. return true;
  506. });
  507. }