1 /**
2  * @file lv_indev.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  ********************/
9 #include "lv_indev.h"
10 #include "lv_disp.h"
11 #include "lv_obj.h"
12 #include "lv_indev_scroll.h"
13 #include "lv_group.h"
14 #include "lv_refr.h"
15 
16 #include "../hal/lv_hal_tick.h"
17 #include "../misc/lv_timer.h"
18 #include "../misc/lv_math.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 #if LV_INDEV_DEF_SCROLL_THROW <= 0
24     #warning "LV_INDEV_DRAG_THROW must be greater than 0"
25 #endif
26 
27 /**********************
28  *      TYPEDEFS
29  **********************/
30 
31 /**********************
32  *  STATIC PROTOTYPES
33  **********************/
34 static void indev_pointer_proc(lv_indev_t * i, lv_indev_data_t * data);
35 static void indev_keypad_proc(lv_indev_t * i, lv_indev_data_t * data);
36 static void indev_encoder_proc(lv_indev_t * i, lv_indev_data_t * data);
37 static void indev_button_proc(lv_indev_t * i, lv_indev_data_t * data);
38 static void indev_proc_press(_lv_indev_proc_t * proc);
39 static void indev_proc_release(_lv_indev_proc_t * proc);
40 static void indev_proc_reset_query_handler(lv_indev_t * indev);
41 static void indev_click_focus(_lv_indev_proc_t * proc);
42 static void indev_gesture(_lv_indev_proc_t * proc);
43 static bool indev_reset_check(_lv_indev_proc_t * proc);
44 
45 /**********************
46  *  STATIC VARIABLES
47  **********************/
48 static lv_indev_t * indev_act;
49 static lv_obj_t * indev_obj_act = NULL;
50 
51 /**********************
52  *      MACROS
53  **********************/
54 #if LV_LOG_TRACE_INDEV
55     #define INDEV_TRACE(...) LV_LOG_TRACE(__VA_ARGS__)
56 #else
57     #define INDEV_TRACE(...)
58 #endif
59 
60 /**********************
61  *   GLOBAL FUNCTIONS
62  **********************/
63 
lv_indev_read_timer_cb(lv_timer_t * timer)64 void lv_indev_read_timer_cb(lv_timer_t * timer)
65 {
66     INDEV_TRACE("begin");
67 
68     lv_indev_data_t data;
69 
70     indev_act = timer->user_data;
71 
72     /*Read and process all indevs*/
73     if(indev_act->driver->disp == NULL) return; /*Not assigned to any displays*/
74 
75     /*Handle reset query before processing the point*/
76     indev_proc_reset_query_handler(indev_act);
77 
78     if(indev_act->proc.disabled ||
79        indev_act->driver->disp->prev_scr != NULL) return; /*Input disabled or screen animation active*/
80     bool continue_reading;
81     do {
82         /*Read the data*/
83         _lv_indev_read(indev_act, &data);
84         continue_reading = data.continue_reading;
85 
86         /*The active object might be deleted even in the read function*/
87         indev_proc_reset_query_handler(indev_act);
88         indev_obj_act = NULL;
89 
90         indev_act->proc.state = data.state;
91 
92         /*Save the last activity time*/
93         if(indev_act->proc.state == LV_INDEV_STATE_PRESSED) {
94             indev_act->driver->disp->last_activity_time = lv_tick_get();
95         }
96         else if(indev_act->driver->type == LV_INDEV_TYPE_ENCODER && data.enc_diff) {
97             indev_act->driver->disp->last_activity_time = lv_tick_get();
98         }
99 
100         if(indev_act->driver->type == LV_INDEV_TYPE_POINTER) {
101             indev_pointer_proc(indev_act, &data);
102         }
103         else if(indev_act->driver->type == LV_INDEV_TYPE_KEYPAD) {
104             indev_keypad_proc(indev_act, &data);
105         }
106         else if(indev_act->driver->type == LV_INDEV_TYPE_ENCODER) {
107             indev_encoder_proc(indev_act, &data);
108         }
109         else if(indev_act->driver->type == LV_INDEV_TYPE_BUTTON) {
110             indev_button_proc(indev_act, &data);
111         }
112         /*Handle reset query if it happened in during processing*/
113         indev_proc_reset_query_handler(indev_act);
114     } while(continue_reading);
115 
116     /*End of indev processing, so no act indev*/
117     indev_act     = NULL;
118     indev_obj_act = NULL;
119 
120     INDEV_TRACE("finished");
121 }
122 
lv_indev_enable(lv_indev_t * indev,bool en)123 void lv_indev_enable(lv_indev_t * indev, bool en)
124 {
125     uint8_t enable = en ? 0 : 1;
126 
127     if(indev) {
128         indev->proc.disabled = enable;
129     }
130     else {
131         lv_indev_t * i = lv_indev_get_next(NULL);
132         while(i) {
133             i->proc.disabled = enable;
134             i = lv_indev_get_next(i);
135         }
136     }
137 }
138 
lv_indev_get_act(void)139 lv_indev_t * lv_indev_get_act(void)
140 {
141     return indev_act;
142 }
143 
lv_indev_get_type(const lv_indev_t * indev)144 lv_indev_type_t lv_indev_get_type(const lv_indev_t * indev)
145 {
146     if(indev == NULL) return LV_INDEV_TYPE_NONE;
147 
148     return indev->driver->type;
149 }
150 
lv_indev_reset(lv_indev_t * indev,lv_obj_t * obj)151 void lv_indev_reset(lv_indev_t * indev, lv_obj_t * obj)
152 {
153     if(indev) {
154         indev->proc.reset_query = 1;
155         if(indev_act == indev) indev_obj_act = NULL;
156         if(indev->driver->type == LV_INDEV_TYPE_POINTER || indev->driver->type == LV_INDEV_TYPE_KEYPAD) {
157             if(obj == NULL || indev->proc.types.pointer.last_pressed == obj) {
158                 indev->proc.types.pointer.last_pressed = NULL;
159             }
160             if(obj == NULL || indev->proc.types.pointer.act_obj == obj) {
161                 indev->proc.types.pointer.act_obj = NULL;
162             }
163             if(obj == NULL || indev->proc.types.pointer.last_obj == obj) {
164                 indev->proc.types.pointer.last_obj = NULL;
165             }
166         }
167     }
168     else {
169         lv_indev_t * i = lv_indev_get_next(NULL);
170         while(i) {
171             i->proc.reset_query = 1;
172             if(i->driver->type == LV_INDEV_TYPE_POINTER || i->driver->type == LV_INDEV_TYPE_KEYPAD) {
173                 if(obj == NULL || i->proc.types.pointer.last_pressed == obj) {
174                     i->proc.types.pointer.last_pressed = NULL;
175                 }
176                 if(obj == NULL || i->proc.types.pointer.act_obj == obj) {
177                     i->proc.types.pointer.act_obj = NULL;
178                 }
179                 if(obj == NULL || i->proc.types.pointer.last_obj == obj) {
180                     i->proc.types.pointer.last_obj = NULL;
181                 }
182             }
183             i = lv_indev_get_next(i);
184         }
185         indev_obj_act = NULL;
186     }
187 }
188 
lv_indev_reset_long_press(lv_indev_t * indev)189 void lv_indev_reset_long_press(lv_indev_t * indev)
190 {
191     indev->proc.long_pr_sent         = 0;
192     indev->proc.longpr_rep_timestamp = lv_tick_get();
193     indev->proc.pr_timestamp         = lv_tick_get();
194 }
195 
lv_indev_set_cursor(lv_indev_t * indev,lv_obj_t * cur_obj)196 void lv_indev_set_cursor(lv_indev_t * indev, lv_obj_t * cur_obj)
197 {
198     if(indev->driver->type != LV_INDEV_TYPE_POINTER) return;
199 
200     indev->cursor = cur_obj;
201     lv_obj_set_parent(indev->cursor, lv_disp_get_layer_sys(indev->driver->disp));
202     lv_obj_set_pos(indev->cursor, indev->proc.types.pointer.act_point.x, indev->proc.types.pointer.act_point.y);
203     lv_obj_clear_flag(indev->cursor, LV_OBJ_FLAG_CLICKABLE);
204     lv_obj_add_flag(indev->cursor, LV_OBJ_FLAG_IGNORE_LAYOUT | LV_OBJ_FLAG_FLOATING);
205 }
206 
lv_indev_set_group(lv_indev_t * indev,lv_group_t * group)207 void lv_indev_set_group(lv_indev_t * indev, lv_group_t * group)
208 {
209     if(indev->driver->type == LV_INDEV_TYPE_KEYPAD || indev->driver->type == LV_INDEV_TYPE_ENCODER) {
210         indev->group = group;
211     }
212 }
213 
lv_indev_set_button_points(lv_indev_t * indev,const lv_point_t points[])214 void lv_indev_set_button_points(lv_indev_t * indev, const lv_point_t points[])
215 {
216     if(indev->driver->type == LV_INDEV_TYPE_BUTTON) {
217         indev->btn_points = points;
218     }
219 }
220 
lv_indev_get_point(const lv_indev_t * indev,lv_point_t * point)221 void lv_indev_get_point(const lv_indev_t * indev, lv_point_t * point)
222 {
223     if(indev == NULL) {
224         point->x = 0;
225         point->y = 0;
226         return;
227     }
228     if(indev->driver->type != LV_INDEV_TYPE_POINTER && indev->driver->type != LV_INDEV_TYPE_BUTTON) {
229         point->x = -1;
230         point->y = -1;
231     }
232     else {
233         point->x = indev->proc.types.pointer.act_point.x;
234         point->y = indev->proc.types.pointer.act_point.y;
235     }
236 }
237 
lv_indev_get_gesture_dir(const lv_indev_t * indev)238 lv_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev)
239 {
240     return indev->proc.types.pointer.gesture_dir;
241 }
242 
lv_indev_get_key(const lv_indev_t * indev)243 uint32_t lv_indev_get_key(const lv_indev_t * indev)
244 {
245     if(indev->driver->type != LV_INDEV_TYPE_KEYPAD)
246         return 0;
247     else
248         return indev->proc.types.keypad.last_key;
249 }
250 
lv_indev_get_scroll_dir(const lv_indev_t * indev)251 lv_dir_t lv_indev_get_scroll_dir(const lv_indev_t * indev)
252 {
253     if(indev == NULL) return false;
254     if(indev->driver->type != LV_INDEV_TYPE_POINTER && indev->driver->type != LV_INDEV_TYPE_BUTTON) return false;
255     return indev->proc.types.pointer.scroll_dir;
256 }
257 
lv_indev_get_scroll_obj(const lv_indev_t * indev)258 lv_obj_t * lv_indev_get_scroll_obj(const lv_indev_t * indev)
259 {
260     if(indev == NULL) return NULL;
261     if(indev->driver->type != LV_INDEV_TYPE_POINTER && indev->driver->type != LV_INDEV_TYPE_BUTTON) return NULL;
262     return indev->proc.types.pointer.scroll_obj;
263 }
264 
lv_indev_get_vect(const lv_indev_t * indev,lv_point_t * point)265 void lv_indev_get_vect(const lv_indev_t * indev, lv_point_t * point)
266 {
267     point->x = 0;
268     point->y = 0;
269 
270     if(indev == NULL) return;
271 
272     if(indev->driver->type == LV_INDEV_TYPE_POINTER || indev->driver->type == LV_INDEV_TYPE_BUTTON) {
273         point->x = indev->proc.types.pointer.vect.x;
274         point->y = indev->proc.types.pointer.vect.y;
275     }
276 }
277 
lv_indev_wait_release(lv_indev_t * indev)278 void lv_indev_wait_release(lv_indev_t * indev)
279 {
280     if(indev == NULL)return;
281     indev->proc.wait_until_release = 1;
282 }
283 
lv_indev_get_obj_act(void)284 lv_obj_t * lv_indev_get_obj_act(void)
285 {
286     return indev_obj_act;
287 }
288 
lv_indev_get_read_timer(lv_disp_t * indev)289 lv_timer_t * lv_indev_get_read_timer(lv_disp_t * indev)
290 {
291     if(!indev) {
292         LV_LOG_WARN("lv_indev_get_read_timer: indev was NULL");
293         return NULL;
294     }
295 
296     return indev->refr_timer;
297 }
298 
299 
lv_indev_search_obj(lv_obj_t * obj,lv_point_t * point)300 lv_obj_t * lv_indev_search_obj(lv_obj_t * obj, lv_point_t * point)
301 {
302     lv_obj_t * found_p = NULL;
303 
304     /*If this obj is hidden the children are hidden too so return immediately*/
305     if(lv_obj_has_flag(obj, LV_OBJ_FLAG_HIDDEN)) return NULL;
306 
307     lv_point_t p_trans = *point;
308     lv_obj_transform_point(obj, &p_trans, false, true);
309 
310     bool hit_test_ok = lv_obj_hit_test(obj, &p_trans);
311 
312     /*If the point is on this object or has overflow visible check its children too*/
313     if(_lv_area_is_point_on(&obj->coords, &p_trans, 0) || lv_obj_has_flag(obj, LV_OBJ_FLAG_OVERFLOW_VISIBLE)) {
314         int32_t i;
315         uint32_t child_cnt = lv_obj_get_child_cnt(obj);
316 
317         /*If a child matches use it*/
318         for(i = child_cnt - 1; i >= 0; i--) {
319             lv_obj_t * child = obj->spec_attr->children[i];
320             found_p = lv_indev_search_obj(child, &p_trans);
321             if(found_p) return found_p;
322         }
323     }
324 
325     /*If not return earlier for a clicked child and this obj's hittest was ok use it
326      *else return NULL*/
327     if(hit_test_ok) return obj;
328     else return NULL;
329 }
330 
331 /**********************
332  *   STATIC FUNCTIONS
333  **********************/
334 
335 /**
336  * Process a new point from LV_INDEV_TYPE_POINTER input device
337  * @param i pointer to an input device
338  * @param data pointer to the data read from the input device
339  */
indev_pointer_proc(lv_indev_t * i,lv_indev_data_t * data)340 static void indev_pointer_proc(lv_indev_t * i, lv_indev_data_t * data)
341 {
342     lv_disp_t * disp = i->driver->disp;
343     /*Save the raw points so they can be used again in _lv_indev_read*/
344     i->proc.types.pointer.last_raw_point.x = data->point.x;
345     i->proc.types.pointer.last_raw_point.y = data->point.y;
346 
347     if(disp->driver->rotated == LV_DISP_ROT_180 || disp->driver->rotated == LV_DISP_ROT_270) {
348         data->point.x = disp->driver->hor_res - data->point.x - 1;
349         data->point.y = disp->driver->ver_res - data->point.y - 1;
350     }
351     if(disp->driver->rotated == LV_DISP_ROT_90 || disp->driver->rotated == LV_DISP_ROT_270) {
352         lv_coord_t tmp = data->point.y;
353         data->point.y = data->point.x;
354         data->point.x = disp->driver->ver_res - tmp - 1;
355     }
356 
357     /*Simple sanity check*/
358     if(data->point.x < 0) {
359         LV_LOG_WARN("X is %d which is smaller than zero", data->point.x);
360     }
361     if(data->point.x >= lv_disp_get_hor_res(i->driver->disp)) {
362         LV_LOG_WARN("X is %d which is greater than hor. res", data->point.x);
363     }
364     if(data->point.y < 0) {
365         LV_LOG_WARN("Y is %d which is smaller than zero", data->point.y);
366     }
367     if(data->point.y >= lv_disp_get_ver_res(i->driver->disp)) {
368         LV_LOG_WARN("Y is %d which is greater than ver. res", data->point.y);
369     }
370 
371     /*Move the cursor if set and moved*/
372     if(i->cursor != NULL &&
373        (i->proc.types.pointer.last_point.x != data->point.x || i->proc.types.pointer.last_point.y != data->point.y)) {
374         lv_obj_set_pos(i->cursor, data->point.x, data->point.y);
375     }
376 
377     i->proc.types.pointer.act_point.x = data->point.x;
378     i->proc.types.pointer.act_point.y = data->point.y;
379 
380     if(i->proc.state == LV_INDEV_STATE_PRESSED) {
381         indev_proc_press(&i->proc);
382     }
383     else {
384         indev_proc_release(&i->proc);
385     }
386 
387     i->proc.types.pointer.last_point.x = i->proc.types.pointer.act_point.x;
388     i->proc.types.pointer.last_point.y = i->proc.types.pointer.act_point.y;
389 }
390 
391 /**
392  * Process a new point from LV_INDEV_TYPE_KEYPAD input device
393  * @param i pointer to an input device
394  * @param data pointer to the data read from the input device
395  */
indev_keypad_proc(lv_indev_t * i,lv_indev_data_t * data)396 static void indev_keypad_proc(lv_indev_t * i, lv_indev_data_t * data)
397 {
398     if(data->state == LV_INDEV_STATE_PRESSED && i->proc.wait_until_release) return;
399 
400     if(i->proc.wait_until_release) {
401         i->proc.wait_until_release      = 0;
402         i->proc.pr_timestamp            = 0;
403         i->proc.long_pr_sent            = 0;
404         i->proc.types.keypad.last_state = LV_INDEV_STATE_RELEASED; /*To skip the processing of release*/
405     }
406 
407     lv_group_t * g = i->group;
408     if(g == NULL) return;
409 
410     indev_obj_act = lv_group_get_focused(g);
411     if(indev_obj_act == NULL) return;
412 
413     bool dis = lv_obj_has_state(indev_obj_act, LV_STATE_DISABLED);
414 
415     /*Save the last key to compare it with the current latter on RELEASE*/
416     uint32_t prev_key = i->proc.types.keypad.last_key;
417 
418     /*Save the last key.
419      *It must be done here else `lv_indev_get_key` will return the last key in events*/
420     i->proc.types.keypad.last_key = data->key;
421 
422     /*Save the previous state so we can detect state changes below and also set the last state now
423      *so if any event handler on the way returns `LV_RES_INV` the last state is remembered
424      *for the next time*/
425     uint32_t prev_state             = i->proc.types.keypad.last_state;
426     i->proc.types.keypad.last_state = data->state;
427 
428     /*Key press happened*/
429     if(data->state == LV_INDEV_STATE_PRESSED && prev_state == LV_INDEV_STATE_RELEASED) {
430         LV_LOG_INFO("%" LV_PRIu32 " key is pressed", data->key);
431         i->proc.pr_timestamp = lv_tick_get();
432 
433         /*Move the focus on NEXT*/
434         if(data->key == LV_KEY_NEXT) {
435             lv_group_set_editing(g, false); /*Editing is not used by KEYPAD is be sure it is disabled*/
436             lv_group_focus_next(g);
437             if(indev_reset_check(&i->proc)) return;
438         }
439         /*Move the focus on PREV*/
440         else if(data->key == LV_KEY_PREV) {
441             lv_group_set_editing(g, false); /*Editing is not used by KEYPAD is be sure it is disabled*/
442             lv_group_focus_prev(g);
443             if(indev_reset_check(&i->proc)) return;
444         }
445         else if(!dis) {
446             /*Simulate a press on the object if ENTER was pressed*/
447             if(data->key == LV_KEY_ENTER) {
448                 /*Send the ENTER as a normal KEY*/
449                 lv_group_send_data(g, LV_KEY_ENTER);
450                 if(indev_reset_check(&i->proc)) return;
451 
452                 if(!dis) lv_event_send(indev_obj_act, LV_EVENT_PRESSED, indev_act);
453                 if(indev_reset_check(&i->proc)) return;
454             }
455             else if(data->key == LV_KEY_ESC) {
456                 /*Send the ESC as a normal KEY*/
457                 lv_group_send_data(g, LV_KEY_ESC);
458                 if(indev_reset_check(&i->proc)) return;
459 
460                 lv_event_send(indev_obj_act, LV_EVENT_CANCEL, indev_act);
461                 if(indev_reset_check(&i->proc)) return;
462             }
463             /*Just send other keys to the object (e.g. 'A' or `LV_GROUP_KEY_RIGHT`)*/
464             else {
465                 lv_group_send_data(g, data->key);
466                 if(indev_reset_check(&i->proc)) return;
467             }
468         }
469     }
470     /*Pressing*/
471     else if(!dis && data->state == LV_INDEV_STATE_PRESSED && prev_state == LV_INDEV_STATE_PRESSED) {
472 
473         if(data->key == LV_KEY_ENTER) {
474             lv_event_send(indev_obj_act, LV_EVENT_PRESSING, indev_act);
475             if(indev_reset_check(&i->proc)) return;
476         }
477 
478         /*Long press time has elapsed?*/
479         if(i->proc.long_pr_sent == 0 && lv_tick_elaps(i->proc.pr_timestamp) > i->driver->long_press_time) {
480             i->proc.long_pr_sent = 1;
481             if(data->key == LV_KEY_ENTER) {
482                 i->proc.longpr_rep_timestamp = lv_tick_get();
483                 lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED, indev_act);
484                 if(indev_reset_check(&i->proc)) return;
485             }
486         }
487         /*Long press repeated time has elapsed?*/
488         else if(i->proc.long_pr_sent != 0 &&
489                 lv_tick_elaps(i->proc.longpr_rep_timestamp) > i->driver->long_press_repeat_time) {
490 
491             i->proc.longpr_rep_timestamp = lv_tick_get();
492 
493             /*Send LONG_PRESS_REP on ENTER*/
494             if(data->key == LV_KEY_ENTER) {
495                 lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED_REPEAT, indev_act);
496                 if(indev_reset_check(&i->proc)) return;
497             }
498             /*Move the focus on NEXT again*/
499             else if(data->key == LV_KEY_NEXT) {
500                 lv_group_set_editing(g, false); /*Editing is not used by KEYPAD is be sure it is disabled*/
501                 lv_group_focus_next(g);
502                 if(indev_reset_check(&i->proc)) return;
503             }
504             /*Move the focus on PREV again*/
505             else if(data->key == LV_KEY_PREV) {
506                 lv_group_set_editing(g, false); /*Editing is not used by KEYPAD is be sure it is disabled*/
507                 lv_group_focus_prev(g);
508                 if(indev_reset_check(&i->proc)) return;
509             }
510             /*Just send other keys again to the object (e.g. 'A' or `LV_GROUP_KEY_RIGHT)*/
511             else {
512                 lv_group_send_data(g, data->key);
513                 if(indev_reset_check(&i->proc)) return;
514             }
515         }
516     }
517     /*Release happened*/
518     else if(!dis && data->state == LV_INDEV_STATE_RELEASED && prev_state == LV_INDEV_STATE_PRESSED) {
519         LV_LOG_INFO("%" LV_PRIu32 " key is released", data->key);
520         /*The user might clear the key when it was released. Always release the pressed key*/
521         data->key = prev_key;
522         if(data->key == LV_KEY_ENTER) {
523 
524             lv_event_send(indev_obj_act, LV_EVENT_RELEASED, indev_act);
525             if(indev_reset_check(&i->proc)) return;
526 
527             if(i->proc.long_pr_sent == 0) {
528                 lv_event_send(indev_obj_act, LV_EVENT_SHORT_CLICKED, indev_act);
529                 if(indev_reset_check(&i->proc)) return;
530             }
531 
532             lv_event_send(indev_obj_act, LV_EVENT_CLICKED, indev_act);
533             if(indev_reset_check(&i->proc)) return;
534 
535         }
536         i->proc.pr_timestamp = 0;
537         i->proc.long_pr_sent = 0;
538     }
539     indev_obj_act = NULL;
540 }
541 
542 /**
543  * Process a new point from LV_INDEV_TYPE_ENCODER input device
544  * @param i pointer to an input device
545  * @param data pointer to the data read from the input device
546  */
indev_encoder_proc(lv_indev_t * i,lv_indev_data_t * data)547 static void indev_encoder_proc(lv_indev_t * i, lv_indev_data_t * data)
548 {
549     if(data->state == LV_INDEV_STATE_PRESSED && i->proc.wait_until_release) return;
550 
551     if(i->proc.wait_until_release) {
552         i->proc.wait_until_release      = 0;
553         i->proc.pr_timestamp            = 0;
554         i->proc.long_pr_sent            = 0;
555         i->proc.types.keypad.last_state = LV_INDEV_STATE_RELEASED; /*To skip the processing of release*/
556     }
557 
558     /*Save the last keys before anything else.
559      *They need to be already saved if the function returns for any reason*/
560     lv_indev_state_t last_state     = i->proc.types.keypad.last_state;
561     i->proc.types.keypad.last_state = data->state;
562     i->proc.types.keypad.last_key   = data->key;
563 
564     lv_group_t * g = i->group;
565     if(g == NULL) return;
566 
567     indev_obj_act = lv_group_get_focused(g);
568     if(indev_obj_act == NULL) return;
569 
570     /*Process the steps they are valid only with released button*/
571     if(data->state != LV_INDEV_STATE_RELEASED) {
572         data->enc_diff = 0;
573     }
574 
575     /*Refresh the focused object. It might change due to lv_group_focus_prev/next*/
576     indev_obj_act = lv_group_get_focused(g);
577     if(indev_obj_act == NULL) return;
578 
579     /*Button press happened*/
580     if(data->state == LV_INDEV_STATE_PRESSED && last_state == LV_INDEV_STATE_RELEASED) {
581         LV_LOG_INFO("pressed");
582 
583         i->proc.pr_timestamp = lv_tick_get();
584 
585         if(data->key == LV_KEY_ENTER) {
586             bool editable_or_scrollable = lv_obj_is_editable(indev_obj_act) ||
587                                           lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_SCROLLABLE);
588             if(lv_group_get_editing(g) == true || editable_or_scrollable == false) {
589                 lv_event_send(indev_obj_act, LV_EVENT_PRESSED, indev_act);
590                 if(indev_reset_check(&i->proc)) return;
591             }
592         }
593         else if(data->key == LV_KEY_LEFT) {
594             /*emulate encoder left*/
595             data->enc_diff--;
596         }
597         else if(data->key == LV_KEY_RIGHT) {
598             /*emulate encoder right*/
599             data->enc_diff++;
600         }
601         else if(data->key == LV_KEY_ESC) {
602             /*Send the ESC as a normal KEY*/
603             lv_group_send_data(g, LV_KEY_ESC);
604             if(indev_reset_check(&i->proc)) return;
605 
606             lv_event_send(indev_obj_act, LV_EVENT_CANCEL, indev_act);
607             if(indev_reset_check(&i->proc)) return;
608         }
609         /*Just send other keys to the object (e.g. 'A' or `LV_GROUP_KEY_RIGHT`)*/
610         else {
611             lv_group_send_data(g, data->key);
612             if(indev_reset_check(&i->proc)) return;
613         }
614     }
615     /*Pressing*/
616     else if(data->state == LV_INDEV_STATE_PRESSED && last_state == LV_INDEV_STATE_PRESSED) {
617         /*Long press*/
618         if(i->proc.long_pr_sent == 0 && lv_tick_elaps(i->proc.pr_timestamp) > i->driver->long_press_time) {
619 
620             i->proc.long_pr_sent = 1;
621             i->proc.longpr_rep_timestamp = lv_tick_get();
622 
623             if(data->key == LV_KEY_ENTER) {
624                 bool editable_or_scrollable = lv_obj_is_editable(indev_obj_act) ||
625                                               lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_SCROLLABLE);
626 
627                 /*On enter long press toggle edit mode.*/
628                 if(editable_or_scrollable) {
629                     /*Don't leave edit mode if there is only one object (nowhere to navigate)*/
630                     if(lv_group_get_obj_count(g) > 1) {
631                         LV_LOG_INFO("toggling edit mode");
632                         lv_group_set_editing(g, lv_group_get_editing(g) ? false : true); /*Toggle edit mode on long press*/
633                         lv_obj_clear_state(indev_obj_act, LV_STATE_PRESSED);    /*Remove the pressed state manually*/
634                     }
635                 }
636                 /*If not editable then just send a long press Call the ancestor's event handler*/
637                 else {
638                     lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED, indev_act);
639                     if(indev_reset_check(&i->proc)) return;
640                 }
641             }
642 
643             i->proc.long_pr_sent = 1;
644         }
645         /*Long press repeated time has elapsed?*/
646         else if(i->proc.long_pr_sent != 0 && lv_tick_elaps(i->proc.longpr_rep_timestamp) > i->driver->long_press_repeat_time) {
647 
648             i->proc.longpr_rep_timestamp = lv_tick_get();
649 
650             if(data->key == LV_KEY_ENTER) {
651                 lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED_REPEAT, indev_act);
652                 if(indev_reset_check(&i->proc)) return;
653             }
654             else if(data->key == LV_KEY_LEFT) {
655                 /*emulate encoder left*/
656                 data->enc_diff--;
657             }
658             else if(data->key == LV_KEY_RIGHT) {
659                 /*emulate encoder right*/
660                 data->enc_diff++;
661             }
662             else {
663                 lv_group_send_data(g, data->key);
664                 if(indev_reset_check(&i->proc)) return;
665             }
666 
667         }
668 
669     }
670     /*Release happened*/
671     else if(data->state == LV_INDEV_STATE_RELEASED && last_state == LV_INDEV_STATE_PRESSED) {
672         LV_LOG_INFO("released");
673 
674         if(data->key == LV_KEY_ENTER) {
675             bool editable_or_scrollable = lv_obj_is_editable(indev_obj_act) ||
676                                           lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_SCROLLABLE);
677 
678             /*The button was released on a non-editable object. Just send enter*/
679             if(editable_or_scrollable == false) {
680                 lv_event_send(indev_obj_act, LV_EVENT_RELEASED, indev_act);
681                 if(indev_reset_check(&i->proc)) return;
682 
683                 if(i->proc.long_pr_sent == 0) lv_event_send(indev_obj_act, LV_EVENT_SHORT_CLICKED, indev_act);
684                 if(indev_reset_check(&i->proc)) return;
685 
686                 lv_event_send(indev_obj_act, LV_EVENT_CLICKED, indev_act);
687                 if(indev_reset_check(&i->proc)) return;
688 
689             }
690             /*An object is being edited and the button is released.*/
691             else if(lv_group_get_editing(g)) {
692                 /*Ignore long pressed enter release because it comes from mode switch*/
693                 if(!i->proc.long_pr_sent || lv_group_get_obj_count(g) <= 1) {
694                     lv_event_send(indev_obj_act, LV_EVENT_RELEASED, indev_act);
695                     if(indev_reset_check(&i->proc)) return;
696 
697                     lv_event_send(indev_obj_act, LV_EVENT_SHORT_CLICKED, indev_act);
698                     if(indev_reset_check(&i->proc)) return;
699 
700                     lv_event_send(indev_obj_act, LV_EVENT_CLICKED, indev_act);
701                     if(indev_reset_check(&i->proc)) return;
702 
703                     lv_group_send_data(g, LV_KEY_ENTER);
704                     if(indev_reset_check(&i->proc)) return;
705                 }
706                 else {
707                     lv_obj_clear_state(indev_obj_act, LV_STATE_PRESSED);    /*Remove the pressed state manually*/
708                 }
709             }
710             /*If the focused object is editable and now in navigate mode then on enter switch edit
711                mode*/
712             else if(!i->proc.long_pr_sent) {
713                 LV_LOG_INFO("entering edit mode");
714                 lv_group_set_editing(g, true); /*Set edit mode*/
715             }
716         }
717 
718         i->proc.pr_timestamp = 0;
719         i->proc.long_pr_sent = 0;
720     }
721     indev_obj_act = NULL;
722 
723     /*if encoder steps or simulated steps via left/right keys*/
724     if(data->enc_diff != 0) {
725         /*In edit mode send LEFT/RIGHT keys*/
726         if(lv_group_get_editing(g)) {
727             LV_LOG_INFO("rotated by %+d (edit)", data->enc_diff);
728             int32_t s;
729             if(data->enc_diff < 0) {
730                 for(s = 0; s < -data->enc_diff; s++) {
731                     lv_group_send_data(g, LV_KEY_LEFT);
732                     if(indev_reset_check(&i->proc)) return;
733                 }
734             }
735             else if(data->enc_diff > 0) {
736                 for(s = 0; s < data->enc_diff; s++) {
737                     lv_group_send_data(g, LV_KEY_RIGHT);
738                     if(indev_reset_check(&i->proc)) return;
739                 }
740             }
741         }
742         /*In navigate mode focus on the next/prev objects*/
743         else {
744             LV_LOG_INFO("rotated by %+d (nav)", data->enc_diff);
745             int32_t s;
746             if(data->enc_diff < 0) {
747                 for(s = 0; s < -data->enc_diff; s++) {
748                     lv_group_focus_prev(g);
749                     if(indev_reset_check(&i->proc)) return;
750                 }
751             }
752             else if(data->enc_diff > 0) {
753                 for(s = 0; s < data->enc_diff; s++) {
754                     lv_group_focus_next(g);
755                     if(indev_reset_check(&i->proc)) return;
756                 }
757             }
758         }
759     }
760 }
761 
762 /**
763  * Process new points from an input device. indev->state.pressed has to be set
764  * @param indev pointer to an input device state
765  * @param x x coordinate of the next point
766  * @param y y coordinate of the next point
767  */
indev_button_proc(lv_indev_t * i,lv_indev_data_t * data)768 static void indev_button_proc(lv_indev_t * i, lv_indev_data_t * data)
769 {
770     /*Die gracefully if i->btn_points is NULL*/
771     if(i->btn_points == NULL) {
772         LV_LOG_WARN("btn_points is NULL");
773         return;
774     }
775 
776     lv_coord_t x = i->btn_points[data->btn_id].x;
777     lv_coord_t y = i->btn_points[data->btn_id].y;
778 
779     static lv_indev_state_t prev_state = LV_INDEV_STATE_RELEASED;
780     if(prev_state != data->state) {
781         if(data->state == LV_INDEV_STATE_PRESSED) {
782             LV_LOG_INFO("button %" LV_PRIu32 " is pressed (x:%d y:%d)", data->btn_id, x, y);
783         }
784         else {
785             LV_LOG_INFO("button %" LV_PRIu32 " is released (x:%d y:%d)", data->btn_id, x, y);
786         }
787     }
788 
789     /*If a new point comes always make a release*/
790     if(data->state == LV_INDEV_STATE_PRESSED) {
791         if(i->proc.types.pointer.last_point.x != x ||
792            i->proc.types.pointer.last_point.y != y) {
793             indev_proc_release(&i->proc);
794         }
795     }
796 
797     if(indev_reset_check(&i->proc)) return;
798 
799     /*Save the new points*/
800     i->proc.types.pointer.act_point.x = x;
801     i->proc.types.pointer.act_point.y = y;
802 
803     if(data->state == LV_INDEV_STATE_PRESSED) indev_proc_press(&i->proc);
804     else indev_proc_release(&i->proc);
805 
806     if(indev_reset_check(&i->proc)) return;
807 
808     i->proc.types.pointer.last_point.x = i->proc.types.pointer.act_point.x;
809     i->proc.types.pointer.last_point.y = i->proc.types.pointer.act_point.y;
810 }
811 
812 /**
813  * Process the pressed state of LV_INDEV_TYPE_POINTER input devices
814  * @param indev pointer to an input device 'proc'
815  * @return LV_RES_OK: no indev reset required; LV_RES_INV: indev reset is required
816  */
indev_proc_press(_lv_indev_proc_t * proc)817 static void indev_proc_press(_lv_indev_proc_t * proc)
818 {
819     LV_LOG_INFO("pressed at x:%d y:%d", proc->types.pointer.act_point.x, proc->types.pointer.act_point.y);
820     indev_obj_act = proc->types.pointer.act_obj;
821 
822     if(proc->wait_until_release != 0) return;
823 
824     lv_disp_t * disp = indev_act->driver->disp;
825     bool new_obj_searched = false;
826 
827     /*If there is no last object then search*/
828     if(indev_obj_act == NULL) {
829         indev_obj_act = lv_indev_search_obj(lv_disp_get_layer_sys(disp), &proc->types.pointer.act_point);
830         if(indev_obj_act == NULL) indev_obj_act = lv_indev_search_obj(lv_disp_get_layer_top(disp),
831                                                                           &proc->types.pointer.act_point);
832         if(indev_obj_act == NULL) indev_obj_act = lv_indev_search_obj(lv_disp_get_scr_act(disp),
833                                                                           &proc->types.pointer.act_point);
834         new_obj_searched = true;
835     }
836     /*If there is last object but it is not scrolled and not protected also search*/
837     else if(proc->types.pointer.scroll_obj == NULL &&
838             lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_PRESS_LOCK) == false) {
839         indev_obj_act = lv_indev_search_obj(lv_disp_get_layer_sys(disp), &proc->types.pointer.act_point);
840         if(indev_obj_act == NULL) indev_obj_act = lv_indev_search_obj(lv_disp_get_layer_top(disp),
841                                                                           &proc->types.pointer.act_point);
842         if(indev_obj_act == NULL) indev_obj_act = lv_indev_search_obj(lv_disp_get_scr_act(disp),
843                                                                           &proc->types.pointer.act_point);
844         new_obj_searched = true;
845     }
846 
847     /*The last object might have scroll throw. Stop it manually*/
848     if(new_obj_searched && proc->types.pointer.last_obj) {
849         proc->types.pointer.scroll_throw_vect.x = 0;
850         proc->types.pointer.scroll_throw_vect.y = 0;
851         _lv_indev_scroll_throw_handler(proc);
852         if(indev_reset_check(proc)) return;
853     }
854 
855     /*If a new object was found reset some variables and send a pressed event handler*/
856     if(indev_obj_act != proc->types.pointer.act_obj) {
857         proc->types.pointer.last_point.x = proc->types.pointer.act_point.x;
858         proc->types.pointer.last_point.y = proc->types.pointer.act_point.y;
859 
860         /*If a new object found the previous was lost, so send a Call the ancestor's event handler*/
861         if(proc->types.pointer.act_obj != NULL) {
862             /*Save the obj because in special cases `act_obj` can change in the Call the ancestor's event handler function*/
863             lv_obj_t * last_obj = proc->types.pointer.act_obj;
864 
865             lv_event_send(last_obj, LV_EVENT_PRESS_LOST, indev_act);
866             if(indev_reset_check(proc)) return;
867         }
868 
869         proc->types.pointer.act_obj  = indev_obj_act; /*Save the pressed object*/
870         proc->types.pointer.last_obj = indev_obj_act;
871 
872         if(indev_obj_act != NULL) {
873             /*Save the time when the obj pressed to count long press time.*/
874             proc->pr_timestamp                 = lv_tick_get();
875             proc->long_pr_sent                 = 0;
876             proc->types.pointer.scroll_sum.x     = 0;
877             proc->types.pointer.scroll_sum.y     = 0;
878             proc->types.pointer.scroll_dir = LV_DIR_NONE;
879             proc->types.pointer.gesture_dir = LV_DIR_NONE;
880             proc->types.pointer.gesture_sent   = 0;
881             proc->types.pointer.gesture_sum.x  = 0;
882             proc->types.pointer.gesture_sum.y  = 0;
883             proc->types.pointer.vect.x         = 0;
884             proc->types.pointer.vect.y         = 0;
885 
886             /*Call the ancestor's event handler about the press*/
887             lv_event_send(indev_obj_act, LV_EVENT_PRESSED, indev_act);
888             if(indev_reset_check(proc)) return;
889 
890             if(indev_act->proc.wait_until_release) return;
891 
892             /*Handle focus*/
893             indev_click_focus(&indev_act->proc);
894             if(indev_reset_check(proc)) return;
895 
896         }
897     }
898 
899     /*Calculate the vector and apply a low pass filter: new value = 0.5 * old_value + 0.5 * new_value*/
900     proc->types.pointer.vect.x = proc->types.pointer.act_point.x - proc->types.pointer.last_point.x;
901     proc->types.pointer.vect.y = proc->types.pointer.act_point.y - proc->types.pointer.last_point.y;
902 
903     proc->types.pointer.scroll_throw_vect.x = (proc->types.pointer.scroll_throw_vect.x + proc->types.pointer.vect.x) / 2;
904     proc->types.pointer.scroll_throw_vect.y = (proc->types.pointer.scroll_throw_vect.y + proc->types.pointer.vect.y) / 2;
905 
906     proc->types.pointer.scroll_throw_vect_ori = proc->types.pointer.scroll_throw_vect;
907 
908     if(indev_obj_act) {
909         lv_event_send(indev_obj_act, LV_EVENT_PRESSING, indev_act);
910         if(indev_reset_check(proc)) return;
911 
912         if(indev_act->proc.wait_until_release) return;
913 
914         _lv_indev_scroll_handler(proc);
915         if(indev_reset_check(proc)) return;
916         indev_gesture(proc);
917         if(indev_reset_check(proc)) return;
918 
919         /*If there is no scrolling then check for long press time*/
920         if(proc->types.pointer.scroll_obj == NULL && proc->long_pr_sent == 0) {
921             /*Call the ancestor's event handler about the long press if enough time elapsed*/
922             if(lv_tick_elaps(proc->pr_timestamp) > indev_act->driver->long_press_time) {
923                 lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED, indev_act);
924                 if(indev_reset_check(proc)) return;
925 
926                 /*Mark the Call the ancestor's event handler sending to do not send it again*/
927                 proc->long_pr_sent = 1;
928 
929                 /*Save the long press time stamp for the long press repeat handler*/
930                 proc->longpr_rep_timestamp = lv_tick_get();
931             }
932         }
933 
934         /*Send long press repeated Call the ancestor's event handler*/
935         if(proc->types.pointer.scroll_obj == NULL && proc->long_pr_sent == 1) {
936             /*Call the ancestor's event handler about the long press repeat if enough time elapsed*/
937             if(lv_tick_elaps(proc->longpr_rep_timestamp) > indev_act->driver->long_press_repeat_time) {
938                 lv_event_send(indev_obj_act, LV_EVENT_LONG_PRESSED_REPEAT, indev_act);
939                 if(indev_reset_check(proc)) return;
940                 proc->longpr_rep_timestamp = lv_tick_get();
941             }
942         }
943     }
944 }
945 
946 /**
947  * Process the released state of LV_INDEV_TYPE_POINTER input devices
948  * @param proc pointer to an input device 'proc'
949  */
indev_proc_release(_lv_indev_proc_t * proc)950 static void indev_proc_release(_lv_indev_proc_t * proc)
951 {
952     if(proc->wait_until_release != 0) {
953         lv_event_send(proc->types.pointer.act_obj, LV_EVENT_PRESS_LOST, indev_act);
954         if(indev_reset_check(proc)) return;
955 
956         proc->types.pointer.act_obj  = NULL;
957         proc->types.pointer.last_obj = NULL;
958         proc->pr_timestamp           = 0;
959         proc->longpr_rep_timestamp   = 0;
960         proc->wait_until_release     = 0;
961     }
962     indev_obj_act = proc->types.pointer.act_obj;
963     lv_obj_t * scroll_obj = proc->types.pointer.scroll_obj;
964 
965     /*Forget the act obj and send a released Call the ancestor's event handler*/
966     if(indev_obj_act) {
967         LV_LOG_INFO("released");
968 
969         /*Send RELEASE Call the ancestor's event handler and event*/
970         lv_event_send(indev_obj_act, LV_EVENT_RELEASED, indev_act);
971         if(indev_reset_check(proc)) return;
972 
973         /*Send CLICK if no scrolling*/
974         if(scroll_obj == NULL) {
975             if(proc->long_pr_sent == 0) {
976                 lv_event_send(indev_obj_act, LV_EVENT_SHORT_CLICKED, indev_act);
977                 if(indev_reset_check(proc)) return;
978             }
979 
980             lv_event_send(indev_obj_act, LV_EVENT_CLICKED, indev_act);
981             if(indev_reset_check(proc)) return;
982         }
983 
984         proc->types.pointer.act_obj = NULL;
985         proc->pr_timestamp          = 0;
986         proc->longpr_rep_timestamp  = 0;
987 
988 
989         /*Get the transformed vector with this object*/
990         if(scroll_obj) {
991             int16_t angle = 0;
992             int16_t zoom = 256;
993             lv_point_t pivot = { 0, 0 };
994             lv_obj_t * parent = scroll_obj;
995             while(parent) {
996                 angle += lv_obj_get_style_transform_angle(parent, 0);
997                 zoom *= (lv_obj_get_style_transform_zoom(parent, 0) / 256);
998                 parent = lv_obj_get_parent(parent);
999             }
1000 
1001             if(angle != 0 || zoom != LV_IMG_ZOOM_NONE) {
1002                 angle = -angle;
1003                 zoom = (256 * 256) / zoom;
1004                 lv_point_transform(&proc->types.pointer.scroll_throw_vect, angle, zoom, &pivot);
1005                 lv_point_transform(&proc->types.pointer.scroll_throw_vect_ori, angle, zoom, &pivot);
1006             }
1007         }
1008 
1009     }
1010 
1011     /*The reset can be set in the Call the ancestor's event handler function.
1012      * In case of reset query ignore the remaining parts.*/
1013     if(scroll_obj) {
1014         _lv_indev_scroll_throw_handler(proc);
1015         if(indev_reset_check(proc)) return;
1016     }
1017 }
1018 
1019 /**
1020  * Process a new point from LV_INDEV_TYPE_BUTTON input device
1021  * @param i pointer to an input device
1022  * @param data pointer to the data read from the input device
1023  * Reset input device if a reset query has been sent to it
1024  * @param indev pointer to an input device
1025  */
indev_proc_reset_query_handler(lv_indev_t * indev)1026 static void indev_proc_reset_query_handler(lv_indev_t * indev)
1027 {
1028     if(indev->proc.reset_query) {
1029         indev->proc.types.pointer.act_obj           = NULL;
1030         indev->proc.types.pointer.last_obj          = NULL;
1031         indev->proc.types.pointer.scroll_obj          = NULL;
1032         indev->proc.long_pr_sent                    = 0;
1033         indev->proc.pr_timestamp                    = 0;
1034         indev->proc.longpr_rep_timestamp            = 0;
1035         indev->proc.types.pointer.scroll_sum.x        = 0;
1036         indev->proc.types.pointer.scroll_sum.y        = 0;
1037         indev->proc.types.pointer.scroll_dir = LV_DIR_NONE;
1038         indev->proc.types.pointer.scroll_throw_vect.x = 0;
1039         indev->proc.types.pointer.scroll_throw_vect.y = 0;
1040         indev->proc.types.pointer.gesture_sum.x     = 0;
1041         indev->proc.types.pointer.gesture_sum.y     = 0;
1042         indev->proc.reset_query                     = 0;
1043         indev_obj_act                               = NULL;
1044     }
1045 }
1046 
1047 /**
1048  * Handle focus/defocus on click for POINTER input devices
1049  * @param proc pointer to the state of the indev
1050  */
indev_click_focus(_lv_indev_proc_t * proc)1051 static void indev_click_focus(_lv_indev_proc_t * proc)
1052 {
1053     /*Handle click focus*/
1054     if(lv_obj_has_flag(indev_obj_act, LV_OBJ_FLAG_CLICK_FOCUSABLE) == false ||
1055        proc->types.pointer.last_pressed == indev_obj_act) {
1056         return;
1057     }
1058 
1059     lv_group_t * g_act = lv_obj_get_group(indev_obj_act);
1060     lv_group_t * g_prev = proc->types.pointer.last_pressed ? lv_obj_get_group(proc->types.pointer.last_pressed) : NULL;
1061 
1062     /*If both the last and act. obj. are in the same group (or have no group)*/
1063     if(g_act == g_prev) {
1064         /*The objects are in a group*/
1065         if(g_act) {
1066             lv_group_focus_obj(indev_obj_act);
1067             if(indev_reset_check(proc)) return;
1068         }
1069         /*The object are not in group*/
1070         else {
1071             if(proc->types.pointer.last_pressed) {
1072                 lv_event_send(proc->types.pointer.last_pressed, LV_EVENT_DEFOCUSED, indev_act);
1073                 if(indev_reset_check(proc)) return;
1074             }
1075 
1076             lv_event_send(indev_obj_act, LV_EVENT_FOCUSED, indev_act);
1077             if(indev_reset_check(proc)) return;
1078         }
1079     }
1080     /*The object are not in the same group (in different groups or one has no group)*/
1081     else {
1082         /*If the prev. obj. is not in a group then defocus it.*/
1083         if(g_prev == NULL && proc->types.pointer.last_pressed) {
1084             lv_event_send(proc->types.pointer.last_pressed, LV_EVENT_DEFOCUSED, indev_act);
1085             if(indev_reset_check(proc)) return;
1086         }
1087         /*Focus on a non-group object*/
1088         else {
1089             if(proc->types.pointer.last_pressed) {
1090                 /*If the prev. object also wasn't in a group defocus it*/
1091                 if(g_prev == NULL) {
1092                     lv_event_send(proc->types.pointer.last_pressed, LV_EVENT_DEFOCUSED, indev_act);
1093                     if(indev_reset_check(proc)) return;
1094                 }
1095                 /*If the prev. object also was in a group at least "LEAVE" it instead of defocus*/
1096                 else {
1097                     lv_event_send(proc->types.pointer.last_pressed, LV_EVENT_LEAVE, indev_act);
1098                     if(indev_reset_check(proc)) return;
1099                 }
1100             }
1101         }
1102 
1103         /*Focus to the act. in its group*/
1104         if(g_act) {
1105             lv_group_focus_obj(indev_obj_act);
1106             if(indev_reset_check(proc)) return;
1107         }
1108         else {
1109             lv_event_send(indev_obj_act, LV_EVENT_FOCUSED, indev_act);
1110             if(indev_reset_check(proc)) return;
1111         }
1112     }
1113     proc->types.pointer.last_pressed = indev_obj_act;
1114 }
1115 
1116 /**
1117 * Handle the gesture of indev_proc_p->types.pointer.act_obj
1118 * @param indev pointer to an input device state
1119 */
indev_gesture(_lv_indev_proc_t * proc)1120 void indev_gesture(_lv_indev_proc_t * proc)
1121 {
1122 
1123     if(proc->types.pointer.scroll_obj) return;
1124     if(proc->types.pointer.gesture_sent) return;
1125 
1126     lv_obj_t * gesture_obj = proc->types.pointer.act_obj;
1127 
1128     /*If gesture parent is active check recursively the gesture attribute*/
1129     while(gesture_obj && lv_obj_has_flag(gesture_obj, LV_OBJ_FLAG_GESTURE_BUBBLE)) {
1130         gesture_obj = lv_obj_get_parent(gesture_obj);
1131     }
1132 
1133     if(gesture_obj == NULL) return;
1134 
1135     if((LV_ABS(proc->types.pointer.vect.x) < indev_act->driver->gesture_min_velocity) &&
1136        (LV_ABS(proc->types.pointer.vect.y) < indev_act->driver->gesture_min_velocity)) {
1137         proc->types.pointer.gesture_sum.x = 0;
1138         proc->types.pointer.gesture_sum.y = 0;
1139     }
1140 
1141     /*Count the movement by gesture*/
1142     proc->types.pointer.gesture_sum.x += proc->types.pointer.vect.x;
1143     proc->types.pointer.gesture_sum.y += proc->types.pointer.vect.y;
1144 
1145     if((LV_ABS(proc->types.pointer.gesture_sum.x) > indev_act->driver->gesture_limit) ||
1146        (LV_ABS(proc->types.pointer.gesture_sum.y) > indev_act->driver->gesture_limit)) {
1147 
1148         proc->types.pointer.gesture_sent = 1;
1149 
1150         if(LV_ABS(proc->types.pointer.gesture_sum.x) > LV_ABS(proc->types.pointer.gesture_sum.y)) {
1151             if(proc->types.pointer.gesture_sum.x > 0)
1152                 proc->types.pointer.gesture_dir = LV_DIR_RIGHT;
1153             else
1154                 proc->types.pointer.gesture_dir = LV_DIR_LEFT;
1155         }
1156         else {
1157             if(proc->types.pointer.gesture_sum.y > 0)
1158                 proc->types.pointer.gesture_dir = LV_DIR_BOTTOM;
1159             else
1160                 proc->types.pointer.gesture_dir = LV_DIR_TOP;
1161         }
1162 
1163         lv_event_send(gesture_obj, LV_EVENT_GESTURE, indev_act);
1164         if(indev_reset_check(proc)) return;
1165     }
1166 }
1167 
1168 /**
1169  * Checks if the reset_query flag has been set. If so, perform necessary global indev cleanup actions
1170  * @param proc pointer to an input device 'proc'
1171  * @return true if indev query should be immediately truncated.
1172  */
indev_reset_check(_lv_indev_proc_t * proc)1173 static bool indev_reset_check(_lv_indev_proc_t * proc)
1174 {
1175     if(proc->reset_query) {
1176         indev_obj_act = NULL;
1177     }
1178 
1179     return proc->reset_query ? true : false;
1180 }
1181