1 
2 /**
3  * @file lv_kb.c
4  *
5  */
6 
7 /*********************
8  *      INCLUDES
9  *********************/
10 #include "lv_kb.h"
11 #if LV_USE_KB != 0
12 
13 #include "lv_ta.h"
14 #include "../lv_themes/lv_theme.h"
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 #define LV_KB_CTRL_BTN_FLAGS (LV_BTNM_CTRL_NO_REPEAT | LV_BTNM_CTRL_CLICK_TRIG)
20 
21 /**********************
22  *      TYPEDEFS
23  **********************/
24 
25 /**********************
26  *  STATIC PROTOTYPES
27  **********************/
28 static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param);
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 static lv_signal_cb_t ancestor_signal;
34 /* clang-format off */
35 static const char * kb_map_lc[] = {"1#", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "Bksp", "\n",
36                                    "ABC", "a", "s", "d", "f", "g", "h", "j", "k", "l", "Enter", "\n",
37                                    "_", "-", "z", "x", "c", "v", "b", "n", "m", ".", ",", ":", "\n",
38                                    LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
39 
40 static const lv_btnm_ctrl_t kb_ctrl_lc_map[] = {
41     LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
42     LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
43     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
44     LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
45 
46 static const char * kb_map_uc[] = {"1#", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "Bksp", "\n",
47                                    "abc", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Enter", "\n",
48                                    "_", "-", "Z", "X", "C", "V", "B", "N", "M", ".", ",", ":", "\n",
49                                    LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
50 
51 static const lv_btnm_ctrl_t kb_ctrl_uc_map[] = {
52     LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
53     LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
54     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
55     LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
56 
57 static const char * kb_map_spec[] = {"0", "1", "2", "3", "4" ,"5", "6", "7", "8", "9", "Bksp", "\n",
58                                      "abc", "+", "-", "/", "*", "=", "%", "!", "?", "#", "<", ">", "\n",
59                                      "\\",  "@", "$", "(", ")", "{", "}", "[", "]", ";", "\"", "'", "\n",
60                                      LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
61 
62 static const lv_btnm_ctrl_t kb_ctrl_spec_map[] = {
63     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
64     LV_KB_CTRL_BTN_FLAGS | 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66     LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
67 
68 static const char * kb_map_num[] = {"1", "2", "3", LV_SYMBOL_CLOSE, "\n",
69                                     "4", "5", "6", LV_SYMBOL_OK, "\n",
70                                     "7", "8", "9", "Bksp", "\n",
71                                     "+/-", "0", ".", LV_SYMBOL_LEFT, LV_SYMBOL_RIGHT, ""};
72 
73 static const lv_btnm_ctrl_t kb_ctrl_num_map[] = {
74         1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
75         1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
76         1, 1, 1, 2,
77         1, 1, 1, 1, 1};
78 /* clang-format on */
79 
80 /**********************
81  *      MACROS
82  **********************/
83 
84 /**********************
85  *   GLOBAL FUNCTIONS
86  **********************/
87 
88 /**
89  * Create a keyboard objects
90  * @param par pointer to an object, it will be the parent of the new keyboard
91  * @param copy pointer to a keyboard object, if not NULL then the new object will be copied from it
92  * @return pointer to the created keyboard
93  */
lv_kb_create(lv_obj_t * par,const lv_obj_t * copy)94 lv_obj_t * lv_kb_create(lv_obj_t * par, const lv_obj_t * copy)
95 {
96     LV_LOG_TRACE("keyboard create started");
97 
98     /*Create the ancestor of keyboard*/
99     lv_obj_t * new_kb = lv_btnm_create(par, copy);
100     lv_mem_assert(new_kb);
101     if(new_kb == NULL) return NULL;
102 
103     if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_kb);
104 
105     /*Allocate the keyboard type specific extended data*/
106     lv_kb_ext_t * ext = lv_obj_allocate_ext_attr(new_kb, sizeof(lv_kb_ext_t));
107     lv_mem_assert(ext);
108     if(ext == NULL) return NULL;
109 
110     /*Initialize the allocated 'ext' */
111 
112     ext->ta         = NULL;
113     ext->mode       = LV_KB_MODE_TEXT;
114     ext->cursor_mng = 0;
115 
116     /*The signal and design functions are not copied so set them here*/
117     lv_obj_set_signal_cb(new_kb, lv_kb_signal);
118 
119     /*Init the new keyboard keyboard*/
120     if(copy == NULL) {
121         /* Set a size which fits into the parent.
122          * Don't use `par` directly because if the window is created on a page it is moved to the
123          * scrollable so the parent has changed */
124         lv_obj_set_size(new_kb, lv_obj_get_width_fit(lv_obj_get_parent(new_kb)),
125                         lv_obj_get_height_fit(lv_obj_get_parent(new_kb)) / 2);
126 
127         lv_obj_align(new_kb, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
128         lv_obj_set_event_cb(new_kb, lv_kb_def_event_cb);
129         lv_btnm_set_map(new_kb, kb_map_lc);
130         lv_btnm_set_ctrl_map(new_kb, kb_ctrl_lc_map);
131 
132         /*Set the default styles*/
133         lv_theme_t * th = lv_theme_get_current();
134         if(th) {
135             lv_kb_set_style(new_kb, LV_KB_STYLE_BG, th->style.kb.bg);
136             lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_REL, th->style.kb.btn.rel);
137             lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_PR, th->style.kb.btn.pr);
138             lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_TGL_REL, th->style.kb.btn.tgl_rel);
139             lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_TGL_PR, th->style.kb.btn.tgl_pr);
140             lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_INA, th->style.kb.btn.ina);
141         } else {
142             /*Let the button matrix's styles*/
143         }
144     }
145     /*Copy an existing keyboard*/
146     else {
147         lv_kb_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
148         ext->ta                = NULL;
149         ext->ta                = copy_ext->ta;
150         ext->mode              = copy_ext->mode;
151         ext->cursor_mng        = copy_ext->cursor_mng;
152 
153         /*Refresh the style with new signal function*/
154         lv_obj_refresh_style(new_kb);
155     }
156 
157     LV_LOG_INFO("keyboard created");
158 
159     return new_kb;
160 }
161 
162 /*=====================
163  * Setter functions
164  *====================*/
165 
166 /**
167  * Assign a Text Area to the Keyboard. The pressed characters will be put there.
168  * @param kb pointer to a Keyboard object
169  * @param ta pointer to a Text Area object to write there
170  */
lv_kb_set_ta(lv_obj_t * kb,lv_obj_t * ta)171 void lv_kb_set_ta(lv_obj_t * kb, lv_obj_t * ta)
172 {
173     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
174     lv_cursor_type_t cur_type;
175 
176     /*Hide the cursor of the old Text area if cursor management is enabled*/
177     if(ext->ta && ext->cursor_mng) {
178         cur_type = lv_ta_get_cursor_type(ext->ta);
179         lv_ta_set_cursor_type(ext->ta, cur_type | LV_CURSOR_HIDDEN);
180     }
181 
182     ext->ta = ta;
183 
184     /*Show the cursor of the new Text area if cursor management is enabled*/
185     if(ext->ta && ext->cursor_mng) {
186         cur_type = lv_ta_get_cursor_type(ext->ta);
187         lv_ta_set_cursor_type(ext->ta, cur_type & (~LV_CURSOR_HIDDEN));
188     }
189 }
190 
191 /**
192  * Set a new a mode (text or number map)
193  * @param kb pointer to a Keyboard object
194  * @param mode the mode from 'lv_kb_mode_t'
195  */
lv_kb_set_mode(lv_obj_t * kb,lv_kb_mode_t mode)196 void lv_kb_set_mode(lv_obj_t * kb, lv_kb_mode_t mode)
197 {
198     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
199     if(ext->mode == mode) return;
200 
201     ext->mode = mode;
202     if(mode == LV_KB_MODE_TEXT) {
203         lv_btnm_set_map(kb, kb_map_lc);
204         lv_btnm_set_ctrl_map(kb, kb_ctrl_lc_map);
205     } else if(mode == LV_KB_MODE_NUM) {
206         lv_btnm_set_map(kb, kb_map_num);
207         lv_btnm_set_ctrl_map(kb, kb_ctrl_num_map);
208     }
209 }
210 
211 /**
212  * Automatically hide or show the cursor of Text Area
213  * @param kb pointer to a Keyboard object
214  * @param en true: show cursor on the current text area, false: hide cursor
215  */
lv_kb_set_cursor_manage(lv_obj_t * kb,bool en)216 void lv_kb_set_cursor_manage(lv_obj_t * kb, bool en)
217 {
218     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
219     if(ext->cursor_mng == en) return;
220 
221     ext->cursor_mng = en == false ? 0 : 1;
222 
223     if(ext->ta) {
224         lv_cursor_type_t cur_type;
225         cur_type = lv_ta_get_cursor_type(ext->ta);
226 
227         if(ext->cursor_mng) {
228             lv_ta_set_cursor_type(ext->ta, cur_type & (~LV_CURSOR_HIDDEN));
229         } else {
230             lv_ta_set_cursor_type(ext->ta, cur_type | LV_CURSOR_HIDDEN);
231         }
232     }
233 }
234 
235 /**
236  * Set a style of a keyboard
237  * @param kb pointer to a keyboard object
238  * @param type which style should be set
239  * @param style pointer to a style
240  */
lv_kb_set_style(lv_obj_t * kb,lv_kb_style_t type,const lv_style_t * style)241 void lv_kb_set_style(lv_obj_t * kb, lv_kb_style_t type, const lv_style_t * style)
242 {
243     switch(type) {
244         case LV_KB_STYLE_BG: lv_btnm_set_style(kb, LV_BTNM_STYLE_BG, style); break;
245         case LV_KB_STYLE_BTN_REL: lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_REL, style); break;
246         case LV_KB_STYLE_BTN_PR: lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_PR, style); break;
247         case LV_KB_STYLE_BTN_TGL_REL: lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_TGL_REL, style); break;
248         case LV_KB_STYLE_BTN_TGL_PR: lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_TGL_PR, style); break;
249         case LV_KB_STYLE_BTN_INA: lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_INA, style); break;
250     }
251 }
252 
253 /*=====================
254  * Getter functions
255  *====================*/
256 
257 /**
258  * Assign a Text Area to the Keyboard. The pressed characters will be put there.
259  * @param kb pointer to a Keyboard object
260  * @return pointer to the assigned Text Area object
261  */
lv_kb_get_ta(const lv_obj_t * kb)262 lv_obj_t * lv_kb_get_ta(const lv_obj_t * kb)
263 {
264     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
265     return ext->ta;
266 }
267 
268 /**
269  * Set a new a mode (text or number map)
270  * @param kb pointer to a Keyboard object
271  * @return the current mode from 'lv_kb_mode_t'
272  */
lv_kb_get_mode(const lv_obj_t * kb)273 lv_kb_mode_t lv_kb_get_mode(const lv_obj_t * kb)
274 {
275     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
276     return ext->mode;
277 }
278 
279 /**
280  * Get the current cursor manage mode.
281  * @param kb pointer to a Keyboard object
282  * @return true: show cursor on the current text area, false: hide cursor
283  */
lv_kb_get_cursor_manage(const lv_obj_t * kb)284 bool lv_kb_get_cursor_manage(const lv_obj_t * kb)
285 {
286     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
287     return ext->cursor_mng == 0 ? false : true;
288 }
289 
290 /**
291  * Get a style of a keyboard
292  * @param kb pointer to a keyboard object
293  * @param type which style should be get
294  * @return style pointer to a style
295  */
lv_kb_get_style(const lv_obj_t * kb,lv_kb_style_t type)296 const lv_style_t * lv_kb_get_style(const lv_obj_t * kb, lv_kb_style_t type)
297 {
298     const lv_style_t * style = NULL;
299 
300     switch(type) {
301         case LV_KB_STYLE_BG: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BG); break;
302         case LV_KB_STYLE_BTN_REL: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_REL); break;
303         case LV_KB_STYLE_BTN_PR: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_PR); break;
304         case LV_KB_STYLE_BTN_TGL_REL: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_TGL_REL); break;
305         case LV_KB_STYLE_BTN_TGL_PR: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_TGL_PR); break;
306         case LV_KB_STYLE_BTN_INA: style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_INA); break;
307         default: style = NULL; break;
308     }
309 
310     return style;
311 }
312 
313 /*=====================
314  * Other functions
315  *====================*/
316 
317 /**
318  * Default keyboard event to add characters to the Text area and change the map.
319  * If a custom `event_cb` is added to the keyboard this function be called from it to handle the
320  * button clicks
321  * @param kb pointer to a  keyboard
322  * @param event the triggering event
323  */
lv_kb_def_event_cb(lv_obj_t * kb,lv_event_t event)324 void lv_kb_def_event_cb(lv_obj_t * kb, lv_event_t event)
325 {
326     if(event != LV_EVENT_VALUE_CHANGED) return;
327 
328     lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
329     uint16_t btn_id   = lv_btnm_get_active_btn(kb);
330     if(btn_id == LV_BTNM_BTN_NONE) return;
331     if(lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_HIDDEN | LV_BTNM_CTRL_INACTIVE)) return;
332     if(lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_NO_REPEAT) && event == LV_EVENT_LONG_PRESSED_REPEAT) return;
333 
334     const char * txt = lv_btnm_get_active_btn_text(kb);
335     if(txt == NULL) return;
336 
337     /*Do the corresponding action according to the text of the button*/
338     if(strcmp(txt, "abc") == 0) {
339         lv_btnm_set_map(kb, kb_map_lc);
340         lv_btnm_set_ctrl_map(kb, kb_ctrl_lc_map);
341         return;
342     } else if(strcmp(txt, "ABC") == 0) {
343         lv_btnm_set_map(kb, kb_map_uc);
344         lv_btnm_set_ctrl_map(kb, kb_ctrl_uc_map);
345         return;
346     } else if(strcmp(txt, "1#") == 0) {
347         lv_btnm_set_map(kb, kb_map_spec);
348         lv_btnm_set_ctrl_map(kb, kb_ctrl_spec_map);
349         return;
350     } else if(strcmp(txt, LV_SYMBOL_CLOSE) == 0) {
351         if(kb->event_cb != lv_kb_def_event_cb) {
352             lv_res_t res = lv_event_send(kb, LV_EVENT_CANCEL, NULL);
353             if(res != LV_RES_OK) return;
354         } else {
355             lv_kb_set_ta(kb, NULL); /*De-assign the text area  to hide it cursor if needed*/
356             lv_obj_del(kb);
357             return;
358         }
359         return;
360     } else if(strcmp(txt, LV_SYMBOL_OK) == 0) {
361         if(kb->event_cb != lv_kb_def_event_cb) {
362             lv_res_t res = lv_event_send(kb, LV_EVENT_APPLY, NULL);
363             if(res != LV_RES_OK) return;
364         } else {
365             lv_kb_set_ta(kb, NULL); /*De-assign the text area to hide it cursor if needed*/
366         }
367         return;
368     }
369 
370     /*Add the characters to the text area if set*/
371     if(ext->ta == NULL) return;
372 
373     if(strcmp(txt, "Enter") == 0)
374         lv_ta_add_char(ext->ta, '\n');
375     else if(strcmp(txt, LV_SYMBOL_LEFT) == 0)
376         lv_ta_cursor_left(ext->ta);
377     else if(strcmp(txt, LV_SYMBOL_RIGHT) == 0)
378         lv_ta_cursor_right(ext->ta);
379     else if(strcmp(txt, "Bksp") == 0)
380         lv_ta_del_char(ext->ta);
381     else if(strcmp(txt, "+/-") == 0) {
382         uint16_t cur        = lv_ta_get_cursor_pos(ext->ta);
383         const char * ta_txt = lv_ta_get_text(ext->ta);
384         if(ta_txt[0] == '-') {
385             lv_ta_set_cursor_pos(ext->ta, 1);
386             lv_ta_del_char(ext->ta);
387             lv_ta_add_char(ext->ta, '+');
388             lv_ta_set_cursor_pos(ext->ta, cur);
389         } else if(ta_txt[0] == '+') {
390             lv_ta_set_cursor_pos(ext->ta, 1);
391             lv_ta_del_char(ext->ta);
392             lv_ta_add_char(ext->ta, '-');
393             lv_ta_set_cursor_pos(ext->ta, cur);
394         } else {
395             lv_ta_set_cursor_pos(ext->ta, 0);
396             lv_ta_add_char(ext->ta, '-');
397             lv_ta_set_cursor_pos(ext->ta, cur + 1);
398         }
399     } else {
400         lv_ta_add_text(ext->ta, txt);
401     }
402 }
403 
404 /**********************
405  *   STATIC FUNCTIONS
406  **********************/
407 
408 /**
409  * Signal function of the keyboard
410  * @param kb pointer to a keyboard object
411  * @param sign a signal type from lv_signal_t enum
412  * @param param pointer to a signal specific variable
413  * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
414  */
lv_kb_signal(lv_obj_t * kb,lv_signal_t sign,void * param)415 static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param)
416 {
417     lv_res_t res;
418 
419     /* Include the ancient signal function */
420     res = ancestor_signal(kb, sign, param);
421     if(res != LV_RES_OK) return res;
422 
423     if(sign == LV_SIGNAL_CLEANUP) {
424         /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
425     } else if(sign == LV_SIGNAL_FOCUS) {
426         lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
427         /*Show the cursor of the new Text area if cursor management is enabled*/
428         if(ext->ta && ext->cursor_mng) {
429             lv_cursor_type_t cur_type = lv_ta_get_cursor_type(ext->ta);
430             lv_ta_set_cursor_type(ext->ta, cur_type & (~LV_CURSOR_HIDDEN));
431         }
432     } else if(sign == LV_SIGNAL_DEFOCUS) {
433         lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
434         /*Show the cursor of the new Text area if cursor management is enabled*/
435         if(ext->ta && ext->cursor_mng) {
436             lv_cursor_type_t cur_type = lv_ta_get_cursor_type(ext->ta);
437             lv_ta_set_cursor_type(ext->ta, cur_type | LV_CURSOR_HIDDEN);
438         }
439     } else if(sign == LV_SIGNAL_GET_TYPE) {
440         lv_obj_type_t * buf = param;
441         uint8_t i;
442         for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
443             if(buf->type[i] == NULL) break;
444         }
445         buf->type[i] = "lv_kb";
446     }
447 
448     return res;
449 }
450 
451 #endif
452