Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_objx/lv_kb.c
1476 views
1
/*
2
* Copyright (c) 2019-2020 CTCaer
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms and conditions of the GNU General Public License,
6
* version 2, as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11
* more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
17
/**
18
* @file lv_kb.c
19
*
20
*/
21
22
/*********************
23
* INCLUDES
24
*********************/
25
#include "lv_kb.h"
26
#if USE_LV_KB != 0
27
28
#include "lv_ta.h"
29
#include "../lv_themes/lv_theme.h"
30
31
/*********************
32
* DEFINES
33
*********************/
34
35
/**********************
36
* TYPEDEFS
37
**********************/
38
39
/**********************
40
* STATIC PROTOTYPES
41
**********************/
42
static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param);
43
static lv_res_t lv_kb_def_action(lv_obj_t * kb, const char * txt);
44
45
/**********************
46
* STATIC VARIABLES
47
**********************/
48
static lv_signal_func_t ancestor_signal;
49
50
static const char * kb_map_lc[] = {
51
"\2051#", "\204q", "\204w", "\204e", "\204r", "\204t", "\204y", "\204u", "\204i", "\204o", "\204p", "\207Bksp", "\n",
52
"\226ABC", "\203a", "\203s", "\203d", "\203f", "\203g", "\203h", "\203j", "\203k", "\203l", "\207Enter", "\n",
53
"_", "-", "z", "x", "c", "v", "b", "n", "m", ".", ",", ":", "\n",
54
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
55
};
56
57
static const char * kb_map_uc[] = {
58
"\2051#", "\204Q", "\204W", "\204E", "\204R", "\204T", "\204Y", "\204U", "\204I", "\204O", "\204P", "\207Bksp", "\n",
59
"\226abc", "\203A", "\203S", "\203D", "\203F", "\203G", "\203H", "\203J", "\203K", "\203L", "\207Enter", "\n",
60
"_", "-", "Z", "X", "C", "V", "B", "N", "M", ".", ",", ":", "\n",
61
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
62
};
63
64
static const char * kb_map_spec[] = {
65
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\202Bksp", "\n",
66
"\222abc", "+", "-", "/", "*", "=", "%", "!", "?", "#", "<", ">", "\n",
67
"\\", "@", "$", "(", ")", "{", "}", "[", "]", ";", "\"", "'", "\n",
68
"\202"SYMBOL_CLOSE, "\202"SYMBOL_LEFT, "\206 ", "\202"SYMBOL_RIGHT, "\202"SYMBOL_OK, ""
69
};
70
71
static const char * kb_map_num[] = {
72
"1", "2", "3", "\202"SYMBOL_CLOSE, "\n",
73
"4", "5", "6", "\202"SYMBOL_OK, "\n",
74
"7", "8", "9", "\202Bksp", "\n",
75
"+/-", "0", ".", SYMBOL_LEFT, SYMBOL_RIGHT, ""
76
};
77
78
static const char * kb_map_hex[] = {
79
"1", "2", "3", "A", "D", "\212", "\n",
80
"4", "5", "6", "B", "E", "\202Bksp", "\n",
81
"7", "8", "9", "C", "F", "\202"SYMBOL_OK, "\n",
82
"\211", "0", "\213", SYMBOL_LEFT, SYMBOL_RIGHT, ""
83
};
84
/**********************
85
* MACROS
86
**********************/
87
88
/**********************
89
* GLOBAL FUNCTIONS
90
**********************/
91
92
/**
93
* Create a keyboard objects
94
* @param par pointer to an object, it will be the parent of the new keyboard
95
* @param copy pointer to a keyboard object, if not NULL then the new object will be copied from it
96
* @return pointer to the created keyboard
97
*/
98
lv_obj_t * lv_kb_create(lv_obj_t * par, const lv_obj_t * copy)
99
{
100
LV_LOG_TRACE("keyboard create started");
101
102
/*Create the ancestor of keyboard*/
103
lv_obj_t * new_kb = lv_btnm_create(par, copy);
104
lv_mem_assert(new_kb);
105
if(new_kb == NULL) return NULL;
106
107
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_kb);
108
109
/*Allocate the keyboard type specific extended data*/
110
lv_kb_ext_t * ext = lv_obj_allocate_ext_attr(new_kb, sizeof(lv_kb_ext_t));
111
lv_mem_assert(ext);
112
if(ext == NULL) return NULL;
113
114
/*Initialize the allocated 'ext' */
115
116
ext->ta = NULL;
117
ext->mode = LV_KB_MODE_TEXT;
118
ext->cursor_mng = 0;
119
ext->hide_action = NULL;
120
ext->ok_action = NULL;
121
122
/*The signal and design functions are not copied so set them here*/
123
lv_obj_set_signal_func(new_kb, lv_kb_signal);
124
125
/*Init the new keyboard keyboard*/
126
if(copy == NULL) {
127
lv_obj_set_size(new_kb, LV_HOR_RES, LV_VER_RES / 2);
128
lv_obj_align(new_kb, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
129
lv_btnm_set_action(new_kb, lv_kb_def_action);
130
lv_btnm_set_map(new_kb, kb_map_lc);
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->kb.bg);
136
lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_REL, th->kb.btn.rel);
137
lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_PR, th->kb.btn.pr);
138
lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_TGL_REL, th->kb.btn.tgl_rel);
139
lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_TGL_PR, th->kb.btn.tgl_pr);
140
lv_kb_set_style(new_kb, LV_KB_STYLE_BTN_INA, th->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
ext->hide_action = copy_ext->hide_action;
153
ext->ok_action = copy_ext->ok_action;
154
155
/*Refresh the style with new signal function*/
156
lv_obj_refresh_style(new_kb);
157
}
158
159
160
LV_LOG_INFO("keyboard created");
161
162
163
return new_kb;
164
}
165
166
/*=====================
167
* Setter functions
168
*====================*/
169
170
/**
171
* Assign a Text Area to the Keyboard. The pressed characters will be put there.
172
* @param kb pointer to a Keyboard object
173
* @param ta pointer to a Text Area object to write there
174
*/
175
void lv_kb_set_ta(lv_obj_t * kb, lv_obj_t * ta)
176
{
177
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
178
lv_cursor_type_t cur_type;
179
180
/*Hide the cursor of the old Text area if cursor management is enabled*/
181
if(ext->ta && ext->cursor_mng) {
182
cur_type = lv_ta_get_cursor_type(ext->ta);
183
lv_ta_set_cursor_type(ext->ta, cur_type | LV_CURSOR_HIDDEN);
184
}
185
186
ext->ta = ta;
187
188
/*Show the cursor of the new Text area if cursor management is enabled*/
189
if(ext->ta && ext->cursor_mng) {
190
cur_type = lv_ta_get_cursor_type(ext->ta);
191
lv_ta_set_cursor_type(ext->ta, cur_type & (~LV_CURSOR_HIDDEN));
192
}
193
}
194
195
/**
196
* Set a new a mode (text or number map)
197
* @param kb pointer to a Keyboard object
198
* @param mode the mode from 'lv_kb_mode_t'
199
*/
200
void lv_kb_set_mode(lv_obj_t * kb, lv_kb_mode_t mode)
201
{
202
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
203
if(ext->mode == mode) return;
204
205
ext->mode = mode;
206
if(mode == LV_KB_MODE_TEXT) lv_btnm_set_map(kb, kb_map_lc);
207
else if(mode == LV_KB_MODE_NUM) lv_btnm_set_map(kb, kb_map_num);
208
else if (mode == LV_KB_MODE_HEX) lv_btnm_set_map(kb, kb_map_hex);
209
}
210
211
212
/**
213
* Automatically hide or show the cursor of Text Area
214
* @param kb pointer to a Keyboard object
215
* @param en true: show cursor on the current text area, false: hide cursor
216
*/
217
void lv_kb_set_cursor_manage(lv_obj_t * kb, bool en)
218
{
219
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
220
if(ext->cursor_mng == en) return;
221
222
ext->cursor_mng = en == false ? 0 : 1;
223
224
if(ext->ta) {
225
lv_cursor_type_t cur_type;
226
cur_type = lv_ta_get_cursor_type(ext->ta);
227
228
if(ext->cursor_mng) {
229
lv_ta_set_cursor_type(ext->ta, cur_type & (~LV_CURSOR_HIDDEN));
230
} else {
231
lv_ta_set_cursor_type(ext->ta, cur_type | LV_CURSOR_HIDDEN);
232
}
233
}
234
}
235
236
/**
237
* Set call back to call when the "Ok" button is pressed
238
* @param kb pointer to Keyboard object
239
* @param action a callback with 'lv_action_t' type
240
*/
241
void lv_kb_set_ok_action(lv_obj_t * kb, lv_action_t action)
242
{
243
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
244
ext->ok_action = action;
245
}
246
247
/**
248
* Set call back to call when the "Hide" button is pressed
249
* @param kb pointer to Keyboard object
250
* @param action a callback with 'lv_action_t' type
251
*/
252
void lv_kb_set_hide_action(lv_obj_t * kb, lv_action_t action)
253
{
254
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
255
ext->hide_action = action;
256
}
257
258
/**
259
* Set a style of a keyboard
260
* @param kb pointer to a keyboard object
261
* @param type which style should be set
262
* @param style pointer to a style
263
*/
264
void lv_kb_set_style(lv_obj_t * kb, lv_kb_style_t type, lv_style_t * style)
265
{
266
switch(type) {
267
case LV_KB_STYLE_BG:
268
lv_btnm_set_style(kb, LV_BTNM_STYLE_BG, style);
269
break;
270
case LV_KB_STYLE_BTN_REL:
271
lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_REL, style);
272
break;
273
case LV_KB_STYLE_BTN_PR:
274
lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_PR, style);
275
break;
276
case LV_KB_STYLE_BTN_TGL_REL:
277
lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_TGL_REL, style);
278
break;
279
case LV_KB_STYLE_BTN_TGL_PR:
280
lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_TGL_PR, style);
281
break;
282
case LV_KB_STYLE_BTN_INA:
283
lv_btnm_set_style(kb, LV_BTNM_STYLE_BTN_INA, style);
284
break;
285
}
286
}
287
288
/*=====================
289
* Getter functions
290
*====================*/
291
292
/**
293
* Assign a Text Area to the Keyboard. The pressed characters will be put there.
294
* @param kb pointer to a Keyboard object
295
* @return pointer to the assigned Text Area object
296
*/
297
lv_obj_t * lv_kb_get_ta(const lv_obj_t * kb)
298
{
299
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
300
return ext->ta;
301
}
302
303
/**
304
* Set a new a mode (text or number map)
305
* @param kb pointer to a Keyboard object
306
* @return the current mode from 'lv_kb_mode_t'
307
*/
308
lv_kb_mode_t lv_kb_get_mode(const lv_obj_t * kb)
309
{
310
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
311
return ext->mode;
312
}
313
314
315
/**
316
* Get the current cursor manage mode.
317
* @param kb pointer to a Keyboard object
318
* @return true: show cursor on the current text area, false: hide cursor
319
*/
320
bool lv_kb_get_cursor_manage(const lv_obj_t * kb)
321
{
322
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
323
return ext->cursor_mng == 0 ? false : true;
324
}
325
326
/**
327
* Get the callback to call when the "Ok" button is pressed
328
* @param kb pointer to Keyboard object
329
* @return the ok callback
330
*/
331
lv_action_t lv_kb_get_ok_action(const lv_obj_t * kb)
332
{
333
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
334
return ext->ok_action;
335
}
336
337
/**
338
* Get the callback to call when the "Hide" button is pressed
339
* @param kb pointer to Keyboard object
340
* @return the close callback
341
*/
342
lv_action_t lv_kb_get_hide_action(const lv_obj_t * kb)
343
{
344
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
345
return ext->hide_action;
346
}
347
348
/**
349
* Get a style of a keyboard
350
* @param kb pointer to a keyboard object
351
* @param type which style should be get
352
* @return style pointer to a style
353
*/
354
lv_style_t * lv_kb_get_style(const lv_obj_t * kb, lv_kb_style_t type)
355
{
356
lv_style_t * style = NULL;
357
358
switch(type) {
359
case LV_KB_STYLE_BG:
360
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BG);
361
break;
362
case LV_KB_STYLE_BTN_REL:
363
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_REL);
364
break;
365
case LV_KB_STYLE_BTN_PR:
366
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_PR);
367
break;
368
case LV_KB_STYLE_BTN_TGL_REL:
369
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_TGL_REL);
370
break;
371
case LV_KB_STYLE_BTN_TGL_PR:
372
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_TGL_PR);
373
break;
374
case LV_KB_STYLE_BTN_INA:
375
style = lv_btnm_get_style(kb, LV_BTNM_STYLE_BTN_INA);
376
break;
377
default:
378
style = NULL;
379
break;
380
}
381
382
return style;
383
}
384
385
/**********************
386
* STATIC FUNCTIONS
387
**********************/
388
389
/**
390
* Signal function of the keyboard
391
* @param kb pointer to a keyboard object
392
* @param sign a signal type from lv_signal_t enum
393
* @param param pointer to a signal specific variable
394
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
395
*/
396
static lv_res_t lv_kb_signal(lv_obj_t * kb, lv_signal_t sign, void * param)
397
{
398
lv_res_t res;
399
400
/* Include the ancient signal function */
401
res = ancestor_signal(kb, sign, param);
402
if(res != LV_RES_OK) return res;
403
404
if(sign == LV_SIGNAL_CLEANUP) {
405
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
406
} else if(sign == LV_SIGNAL_GET_TYPE) {
407
lv_obj_type_t * buf = param;
408
uint8_t i;
409
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
410
if(buf->type[i] == NULL) break;
411
}
412
buf->type[i] = "lv_kb";
413
}
414
415
return res;
416
}
417
418
/**
419
* Called when a button of 'kb_btnm' is released
420
* @param btnm pointer to 'kb_btnm'
421
* @param i the index of the released button from the current btnm map
422
* @return LV_ACTION_RES_INV if the btnm is deleted else LV_ACTION_RES_OK
423
*/
424
static lv_res_t lv_kb_def_action(lv_obj_t * kb, const char * txt)
425
{
426
lv_kb_ext_t * ext = lv_obj_get_ext_attr(kb);
427
lv_res_t res = LV_RES_OK;
428
429
/*Do the corresponding action according to the text of the button*/
430
if(strcmp(txt, "abc") == 0) {
431
lv_btnm_set_map(kb, kb_map_lc);
432
return LV_RES_OK;
433
} else if(strcmp(txt, "ABC") == 0) {
434
lv_btnm_set_map(kb, kb_map_uc);
435
return LV_RES_OK;
436
} else if(strcmp(txt, "1#") == 0) {
437
lv_btnm_set_map(kb, kb_map_spec);
438
return LV_RES_OK;
439
} else if(strcmp(txt, SYMBOL_CLOSE) == 0) {
440
if(ext->hide_action) res = ext->hide_action(kb);
441
else {
442
lv_kb_set_ta(kb, NULL); /*De-assign the text area to hide it cursor if needed*/
443
lv_obj_del(kb);
444
}
445
return res;
446
} else if(strcmp(txt, SYMBOL_OK) == 0) {
447
if(ext->ok_action) res = ext->ok_action(kb);
448
else {
449
lv_kb_set_ta(kb, NULL); /*De-assign the text area to hide it cursor if needed*/
450
res = lv_obj_del(kb);
451
}
452
return res;
453
}
454
455
if(res != LV_RES_OK) return res; /*The keyboard might be deleted in the actions*/
456
457
/*Add the characters to the text area if set*/
458
if(ext->ta == NULL) return res;
459
460
if(strcmp(txt, "Enter") == 0)lv_ta_add_char(ext->ta, '\n');
461
else if(strcmp(txt, SYMBOL_LEFT) == 0) lv_ta_cursor_left(ext->ta);
462
else if(strcmp(txt, SYMBOL_RIGHT) == 0) lv_ta_cursor_right(ext->ta);
463
else if(strcmp(txt, "Bksp") == 0) lv_ta_del_char(ext->ta);
464
else if(strcmp(txt, "+/-") == 0) {
465
uint16_t cur = lv_ta_get_cursor_pos(ext->ta);
466
const char * ta_txt = lv_ta_get_text(ext->ta);
467
if(ta_txt[0] == '-') {
468
lv_ta_set_cursor_pos(ext->ta, 1);
469
lv_ta_del_char(ext->ta);
470
lv_ta_add_char(ext->ta, '+');
471
lv_ta_set_cursor_pos(ext->ta, cur);
472
} else if(ta_txt[0] == '+') {
473
lv_ta_set_cursor_pos(ext->ta, 1);
474
lv_ta_del_char(ext->ta);
475
lv_ta_add_char(ext->ta, '-');
476
lv_ta_set_cursor_pos(ext->ta, cur);
477
} else {
478
lv_ta_set_cursor_pos(ext->ta, 0);
479
lv_ta_add_char(ext->ta, '-');
480
lv_ta_set_cursor_pos(ext->ta, cur + 1);
481
}
482
} else {
483
lv_ta_add_text(ext->ta, txt);
484
}
485
return LV_RES_OK;
486
}
487
488
#endif
489
490