text_input.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. return (letter - 0x20);
  115. }
  116. static void text_input_backspace_cb(TextInputModel* model) {
  117. uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer);
  118. if(text_length > 0) {
  119. model->text_buffer[text_length - 1] = 0;
  120. }
  121. }
  122. static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
  123. TextInputModel* model = _model;
  124. uint8_t text_length = strlen(model->text_buffer);
  125. uint8_t needed_string_width = canvas_width(canvas) - 8;
  126. uint8_t start_pos = 4;
  127. const char* text = model->text_buffer;
  128. canvas_clear(canvas);
  129. canvas_set_color(canvas, ColorBlack);
  130. canvas_draw_str(canvas, 2, 8, model->header);
  131. elements_slightly_rounded_frame(canvas, 1, 12, 126, 15);
  132. if(canvas_string_width(canvas, text) > needed_string_width) {
  133. canvas_draw_str(canvas, start_pos, 22, "...");
  134. start_pos += 6;
  135. needed_string_width -= 8;
  136. }
  137. while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
  138. text++;
  139. }
  140. if(model->clear_default_text) {
  141. elements_slightly_rounded_box(
  142. canvas, start_pos - 1, 14, canvas_string_width(canvas, text) + 2, 10);
  143. canvas_set_color(canvas, ColorWhite);
  144. } else {
  145. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 22, "|");
  146. canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 22, "|");
  147. }
  148. canvas_draw_str(canvas, start_pos, 22, text);
  149. canvas_set_font(canvas, FontKeyboard);
  150. for(uint8_t row = 0; row <= keyboard_row_count; row++) {
  151. const uint8_t column_count = get_row_size(row);
  152. const TextInputKey* keys = get_row(row);
  153. for(size_t column = 0; column < column_count; column++) {
  154. if(keys[column].text == ENTER_KEY) {
  155. canvas_set_color(canvas, ColorBlack);
  156. if(model->selected_row == row && model->selected_column == column) {
  157. canvas_draw_icon(
  158. canvas,
  159. keyboard_origin_x + keys[column].x,
  160. keyboard_origin_y + keys[column].y,
  161. &I_KeySaveSelected_24x11);
  162. } else {
  163. canvas_draw_icon(
  164. canvas,
  165. keyboard_origin_x + keys[column].x,
  166. keyboard_origin_y + keys[column].y,
  167. &I_KeySave_24x11);
  168. }
  169. } else if(keys[column].text == BACKSPACE_KEY) {
  170. canvas_set_color(canvas, ColorBlack);
  171. if(model->selected_row == row && model->selected_column == column) {
  172. canvas_draw_icon(
  173. canvas,
  174. keyboard_origin_x + keys[column].x,
  175. keyboard_origin_y + keys[column].y,
  176. &I_KeyBackspaceSelected_16x9);
  177. } else {
  178. canvas_draw_icon(
  179. canvas,
  180. keyboard_origin_x + keys[column].x,
  181. keyboard_origin_y + keys[column].y,
  182. &I_KeyBackspace_16x9);
  183. }
  184. } else {
  185. if(model->selected_row == row && model->selected_column == column) {
  186. canvas_set_color(canvas, ColorBlack);
  187. canvas_draw_box(
  188. canvas,
  189. keyboard_origin_x + keys[column].x - 1,
  190. keyboard_origin_y + keys[column].y - 8,
  191. 7,
  192. 10);
  193. canvas_set_color(canvas, ColorWhite);
  194. } else {
  195. canvas_set_color(canvas, ColorBlack);
  196. }
  197. if(text_length == 0 && char_is_lowercase(keys[column].text)) {
  198. canvas_draw_glyph(
  199. canvas,
  200. keyboard_origin_x + keys[column].x,
  201. keyboard_origin_y + keys[column].y,
  202. char_to_uppercase(keys[column].text));
  203. } else {
  204. canvas_draw_glyph(
  205. canvas,
  206. keyboard_origin_x + keys[column].x,
  207. keyboard_origin_y + keys[column].y,
  208. keys[column].text);
  209. }
  210. }
  211. }
  212. }
  213. if(model->valadator_message_visible) {
  214. canvas_set_font(canvas, FontSecondary);
  215. canvas_set_color(canvas, ColorWhite);
  216. canvas_draw_box(canvas, 8, 10, 110, 48);
  217. canvas_set_color(canvas, ColorBlack);
  218. canvas_draw_icon(canvas, 10, 14, &I_WarningDolphin_45x42);
  219. canvas_draw_rframe(canvas, 8, 8, 112, 50, 3);
  220. canvas_draw_rframe(canvas, 9, 9, 110, 48, 2);
  221. elements_multiline_text(canvas, 62, 20, string_get_cstr(model->validator_text));
  222. canvas_set_font(canvas, FontKeyboard);
  223. }
  224. }
  225. static void text_input_handle_up(TextInput* text_input) {
  226. with_view_model(
  227. text_input->view, (TextInputModel * model) {
  228. if(model->selected_row > 0) {
  229. model->selected_row--;
  230. if(model->selected_column > get_row_size(model->selected_row) - 6) {
  231. model->selected_column = model->selected_column + 1;
  232. }
  233. }
  234. return true;
  235. });
  236. }
  237. static void text_input_handle_down(TextInput* text_input) {
  238. with_view_model(
  239. text_input->view, (TextInputModel * model) {
  240. if(model->selected_row < keyboard_row_count - 1) {
  241. model->selected_row++;
  242. if(model->selected_column > get_row_size(model->selected_row) - 4) {
  243. model->selected_column = model->selected_column - 1;
  244. }
  245. }
  246. return true;
  247. });
  248. }
  249. static void text_input_handle_left(TextInput* text_input) {
  250. with_view_model(
  251. text_input->view, (TextInputModel * model) {
  252. if(model->selected_column > 0) {
  253. model->selected_column--;
  254. } else {
  255. model->selected_column = get_row_size(model->selected_row) - 1;
  256. }
  257. return true;
  258. });
  259. }
  260. static void text_input_handle_right(TextInput* text_input) {
  261. with_view_model(
  262. text_input->view, (TextInputModel * model) {
  263. if(model->selected_column < get_row_size(model->selected_row) - 1) {
  264. model->selected_column++;
  265. } else {
  266. model->selected_column = 0;
  267. }
  268. return true;
  269. });
  270. }
  271. static void text_input_handle_ok(TextInput* text_input) {
  272. with_view_model(
  273. text_input->view, (TextInputModel * model) {
  274. char selected = get_selected_char(model);
  275. uint8_t text_length = strlen(model->text_buffer);
  276. if(selected == ENTER_KEY) {
  277. if(model->validator_callback && (!model->validator_callback(
  278. model->text_buffer,
  279. model->validator_text,
  280. model->validator_callback_context))) {
  281. model->valadator_message_visible = true;
  282. osTimerStart(text_input->timer, osKernelGetTickFreq() * 4);
  283. } else if(model->callback != 0 && text_length > 0) {
  284. model->callback(model->callback_context);
  285. }
  286. } else if(selected == BACKSPACE_KEY) {
  287. text_input_backspace_cb(model);
  288. } else if(text_length < (model->text_buffer_size - 1)) {
  289. if(model->clear_default_text) {
  290. text_length = 0;
  291. }
  292. if(text_length == 0 && char_is_lowercase(selected)) {
  293. selected = char_to_uppercase(selected);
  294. }
  295. model->text_buffer[text_length] = selected;
  296. model->text_buffer[text_length + 1] = 0;
  297. }
  298. model->clear_default_text = false;
  299. return true;
  300. });
  301. }
  302. static bool text_input_view_input_callback(InputEvent* event, void* context) {
  303. TextInput* text_input = context;
  304. furi_assert(text_input);
  305. bool consumed = false;
  306. if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
  307. with_view_model(
  308. text_input->view, (TextInputModel * model) {
  309. if(model->valadator_message_visible) {
  310. if(event->key == InputKeyBack) {
  311. consumed = true;
  312. }
  313. }
  314. model->valadator_message_visible = false;
  315. return false;
  316. });
  317. switch(event->key) {
  318. case InputKeyUp:
  319. text_input_handle_up(text_input);
  320. consumed = true;
  321. break;
  322. case InputKeyDown:
  323. text_input_handle_down(text_input);
  324. consumed = true;
  325. break;
  326. case InputKeyLeft:
  327. text_input_handle_left(text_input);
  328. consumed = true;
  329. break;
  330. case InputKeyRight:
  331. text_input_handle_right(text_input);
  332. consumed = true;
  333. break;
  334. case InputKeyOk:
  335. text_input_handle_ok(text_input);
  336. consumed = true;
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. if((event->type == InputTypeLong || event->type == InputTypeRepeat) &&
  343. event->key == InputKeyBack) {
  344. with_view_model(
  345. text_input->view, (TextInputModel * model) {
  346. if(model->valadator_message_visible) {
  347. model->valadator_message_visible = false;
  348. } else {
  349. text_input_backspace_cb(model);
  350. }
  351. return true;
  352. });
  353. consumed = true;
  354. }
  355. return consumed;
  356. }
  357. void text_input_timer_callback(void* context) {
  358. furi_assert(context);
  359. TextInput* text_input = context;
  360. with_view_model(
  361. text_input->view, (TextInputModel * model) {
  362. model->valadator_message_visible = false;
  363. return true;
  364. });
  365. }
  366. TextInput* text_input_alloc() {
  367. TextInput* text_input = malloc(sizeof(TextInput));
  368. text_input->view = view_alloc();
  369. view_set_context(text_input->view, text_input);
  370. view_allocate_model(text_input->view, ViewModelTypeLocking, sizeof(TextInputModel));
  371. view_set_draw_callback(text_input->view, text_input_view_draw_callback);
  372. view_set_input_callback(text_input->view, text_input_view_input_callback);
  373. text_input->timer = osTimerNew(text_input_timer_callback, osTimerOnce, text_input, NULL);
  374. with_view_model(
  375. text_input->view, (TextInputModel * model) {
  376. string_init(model->validator_text);
  377. return false;
  378. });
  379. text_input_reset(text_input);
  380. return text_input;
  381. }
  382. void text_input_free(TextInput* text_input) {
  383. furi_assert(text_input);
  384. with_view_model(
  385. text_input->view, (TextInputModel * model) {
  386. string_clear(model->validator_text);
  387. return false;
  388. });
  389. // Send stop command
  390. osTimerStop(text_input->timer);
  391. // Wait till timer stop
  392. while(osTimerIsRunning(text_input->timer)) osDelay(1);
  393. // Release allocated memory
  394. osTimerDelete(text_input->timer);
  395. view_free(text_input->view);
  396. free(text_input);
  397. }
  398. void text_input_reset(TextInput* text_input) {
  399. furi_assert(text_input);
  400. with_view_model(
  401. text_input->view, (TextInputModel * model) {
  402. model->text_buffer_size = 0;
  403. model->header = "";
  404. model->selected_row = 0;
  405. model->selected_column = 0;
  406. model->clear_default_text = false;
  407. model->text_buffer = NULL;
  408. model->text_buffer_size = 0;
  409. model->callback = NULL;
  410. model->callback_context = NULL;
  411. model->validator_callback = NULL;
  412. model->validator_callback_context = NULL;
  413. string_reset(model->validator_text);
  414. model->valadator_message_visible = false;
  415. return true;
  416. });
  417. }
  418. View* text_input_get_view(TextInput* text_input) {
  419. furi_assert(text_input);
  420. return text_input->view;
  421. }
  422. void text_input_set_result_callback(
  423. TextInput* text_input,
  424. TextInputCallback callback,
  425. void* callback_context,
  426. char* text_buffer,
  427. size_t text_buffer_size,
  428. bool clear_default_text) {
  429. with_view_model(
  430. text_input->view, (TextInputModel * model) {
  431. model->callback = callback;
  432. model->callback_context = callback_context;
  433. model->text_buffer = text_buffer;
  434. model->text_buffer_size = text_buffer_size;
  435. model->clear_default_text = clear_default_text;
  436. if(text_buffer && text_buffer[0] != '\0') {
  437. // Set focus on Save
  438. model->selected_row = 2;
  439. model->selected_column = 8;
  440. }
  441. return true;
  442. });
  443. }
  444. void text_input_set_validator(
  445. TextInput* text_input,
  446. TextInputValidatorCallback callback,
  447. void* callback_context) {
  448. with_view_model(
  449. text_input->view, (TextInputModel * model) {
  450. model->validator_callback = callback;
  451. model->validator_callback_context = callback_context;
  452. return true;
  453. });
  454. }
  455. TextInputValidatorCallback text_input_get_validator_callback(TextInput* text_input) {
  456. TextInputValidatorCallback validator_callback = NULL;
  457. with_view_model(
  458. text_input->view, (TextInputModel * model) {
  459. validator_callback = model->validator_callback;
  460. return false;
  461. });
  462. return validator_callback;
  463. }
  464. void* text_input_get_validator_callback_context(TextInput* text_input) {
  465. void* validator_callback_context = NULL;
  466. with_view_model(
  467. text_input->view, (TextInputModel * model) {
  468. validator_callback_context = model->validator_callback_context;
  469. return false;
  470. });
  471. return validator_callback_context;
  472. }
  473. void text_input_set_header_text(TextInput* text_input, const char* text) {
  474. with_view_model(
  475. text_input->view, (TextInputModel * model) {
  476. model->header = text;
  477. return true;
  478. });
  479. }