// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_btnm.c
// Function: lv_btnm_signal
static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_signal(btnm, sign, param);
    if(res != LV_RES_OK) return res;
    if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);

    lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
    lv_point_t p;
    if(sign == LV_SIGNAL_CLEANUP) {
        lv_mem_free(ext->button_areas);
        lv_mem_free(ext->ctrl_bits);
    } else if(sign == LV_SIGNAL_STYLE_CHG || sign == LV_SIGNAL_CORD_CHG) {
        lv_btnm_set_map(btnm, ext->map_p);
    } else if(sign == LV_SIGNAL_PRESSED) {
        lv_indev_t * indev = lv_indev_get_act();
        if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
            uint16_t btn_pr;
            /*Search the pressed area*/
            lv_indev_get_point(param, &p);
            btn_pr = get_button_from_point(btnm, &p);

            invalidate_button_area(btnm, ext->btn_id_pr) /*Invalidate the old area*/;
            ext->btn_id_pr  = btn_pr;
            ext->btn_id_act = btn_pr;
            invalidate_button_area(btnm, ext->btn_id_pr); /*Invalidate the new area*/
        }
        if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
            if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == false &&
               button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
               button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
                uint32_t b = ext->btn_id_act;
                res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
            }
        }
    } else if(sign == LV_SIGNAL_PRESSING) {
        uint16_t btn_pr;
        /*Search the pressed area*/
        lv_indev_get_point(param, &p);
        btn_pr = get_button_from_point(btnm, &p);
        /*Invalidate to old and the new areas*/;
        if(btn_pr != ext->btn_id_pr) {
            lv_indev_reset_long_press(param); /*Start the log press time again on the new button*/
            if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
                invalidate_button_area(btnm, ext->btn_id_pr);
            }
            if(btn_pr != LV_BTNM_BTN_NONE) {
                uint32_t b = ext->btn_id_act;
                res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
                if(res == LV_RES_OK) {
                    invalidate_button_area(btnm, btn_pr);
                }
            }
        }

        ext->btn_id_pr  = btn_pr;
        ext->btn_id_act = btn_pr;
    } else if(sign == LV_SIGNAL_RELEASED) {
        if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
            /*Toggle the button if enabled*/
            if(button_is_tgl_enabled(ext->ctrl_bits[ext->btn_id_pr])) {
                if(button_get_tgl_state(ext->ctrl_bits[ext->btn_id_pr])) {
                    ext->ctrl_bits[ext->btn_id_pr] &= (~LV_BTNM_CTRL_TGL_STATE);
                } else {
                    ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNM_CTRL_TGL_STATE;
                }
                if(ext->one_toggle) make_one_button_toggled(btnm, ext->btn_id_pr);
            }

            /*Invalidate to old pressed area*/;
            invalidate_button_area(btnm, ext->btn_id_pr);

#if LV_USE_GROUP
            /*Leave the clicked button when releases if this not the focused object in a group*/
            lv_group_t * g = lv_obj_get_group(btnm);
            if(lv_group_get_focused(g) != btnm) {
                ext->btn_id_pr = LV_BTNM_BTN_NONE;
            }
#else
            ext->btn_id_pr = LV_BTNM_BTN_NONE;
#endif

            if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == true &&
               button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
               button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
                uint32_t b = ext->btn_id_act;
                res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
            }
        }
    } else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
        if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
            if(button_is_repeat_disabled(ext->ctrl_bits[ext->btn_id_act]) == false &&
               button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
               button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
                uint32_t b = ext->btn_id_act;
                res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
            }
        }
    } else if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_DEFOCUS) {
        ext->btn_id_pr  = LV_BTNM_BTN_NONE;
        ext->btn_id_act = LV_BTNM_BTN_NONE;
        lv_obj_invalidate(btnm);
    } else if(sign == LV_SIGNAL_FOCUS) {
#if LV_USE_GROUP
        lv_indev_t * indev         = lv_indev_get_act();
        lv_indev_type_t indev_type = lv_indev_get_type(indev);

        /*If not focused by an input device assume the last input device*/
       if(indev_type == LV_INDEV_TYPE_NONE) {
           indev_type = lv_indev_get_type(lv_indev_get_next(NULL));
       }

        if(indev_type == LV_INDEV_TYPE_POINTER) {
            /*Select the clicked button*/
            lv_point_t p1;
            lv_indev_get_point(indev, &p1);
            uint16_t btn_i = get_button_from_point(btnm, &p1);
            ext->btn_id_pr = btn_i;

        } else if(indev_type == LV_INDEV_TYPE_ENCODER) {
            /*In navigation mode don't select any button but in edit mode select the fist*/
            if(lv_group_get_editing(lv_obj_get_group(btnm)))
                ext->btn_id_pr = 0;
            else
                ext->btn_id_pr = LV_BTNM_BTN_NONE;
        } else {
            ext->btn_id_pr = 0;
        }
#else
        ext->btn_id_pr = 0;
#endif

        ext->btn_id_act = ext->btn_id_pr;
        lv_obj_invalidate(btnm);
    } else if(sign == LV_SIGNAL_CONTROL) {
        char c = *((char *)param);
        if(c == LV_KEY_RIGHT) {
            if(ext->btn_id_pr == LV_BTNM_BTN_NONE)
                ext->btn_id_pr = 0;
            else
                ext->btn_id_pr++;
            if(ext->btn_id_pr >= ext->btn_cnt - 1) ext->btn_id_pr = ext->btn_cnt - 1;
            ext->btn_id_act = ext->btn_id_pr;
            lv_obj_invalidate(btnm);
        } else if(c == LV_KEY_LEFT) {
            if(ext->btn_id_pr == LV_BTNM_BTN_NONE) ext->btn_id_pr = 0;
            if(ext->btn_id_pr > 0) ext->btn_id_pr--;
            ext->btn_id_act = ext->btn_id_pr;
            lv_obj_invalidate(btnm);
        } else if(c == LV_KEY_DOWN) {
            const lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
            /*Find the area below the the current*/
            if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
                ext->btn_id_pr = 0;
            } else {
                uint16_t area_below;
                lv_coord_t pr_center =
                    ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);

                for(area_below = ext->btn_id_pr; area_below < ext->btn_cnt; area_below++) {
                    if(ext->button_areas[area_below].y1 > ext->button_areas[ext->btn_id_pr].y1 &&
                       pr_center >= ext->button_areas[area_below].x1 &&
                       pr_center <= ext->button_areas[area_below].x2 + style->body.padding.inner) {
                        break;
                    }
                }

                if(area_below < ext->btn_cnt) ext->btn_id_pr = area_below;
            }
            ext->btn_id_act = ext->btn_id_pr;
            lv_obj_invalidate(btnm);
        } else if(c == LV_KEY_UP) {
            const lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
            /*Find the area below the the current*/
            if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
                ext->btn_id_pr = 0;
            } else {
                int16_t area_above;
                lv_coord_t pr_center =
                    ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);

                for(area_above = ext->btn_id_pr; area_above >= 0; area_above--) {
                    if(ext->button_areas[area_above].y1 < ext->button_areas[ext->btn_id_pr].y1 &&
                       pr_center >= ext->button_areas[area_above].x1 - style->body.padding.inner &&
                       pr_center <= ext->button_areas[area_above].x2) {
                        break;
                    }
                }
                if(area_above >= 0) ext->btn_id_pr = area_above;
            }
            ext->btn_id_act = ext->btn_id_pr;
            lv_obj_invalidate(btnm);
        }
    } else if(sign == LV_SIGNAL_GET_EDITABLE) {
        bool * editable = (bool *)param;
        *editable       = true;
    }
    return res;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r12
movl %esi, %ebp
movq %rdi, %r14
callq *0x3edd3(%rip)          # 0x5e420
movl %eax, %ebx
cmpb $0x1, %al
jne 0x1fab5
cmpb $0x7, %bpl
jne 0x1f67a
leaq 0x1c391(%rip), %rsi     # 0x3b9f5
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x684b
movq %r14, %rdi
callq 0x6747
movb $0x1, %bl
cmpb $0x13, %bpl
ja 0x1fab5
movq %rax, %r15
movzbl %bpl, %eax
leaq 0x1c2b0(%rip), %rcx     # 0x3b94c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rsi
movq %r14, %rdi
callq 0x1fe53
jmp 0x1fab5
orl $-0x1, 0x42(%r15)
jmp 0x1faad
movzbl (%r12), %eax
addl $-0x11, %eax
cmpl $0x3, %eax
ja 0x1fab5
leaq 0x1c2c5(%rip), %rcx     # 0x3b99c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x20368
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x1fa85
movq 0x8(%r15), %rdx
movzwl (%rdx,%rcx,8), %edi
movzwl 0x4(%rdx,%rcx,8), %esi
subl %edi, %esi
incl %esi
movswl %si, %esi
sarl %esi
addl %edi, %esi
movswl %si, %edi
movl %ecx, %ebp
testw %bp, %bp
js 0x1faa8
movzwl %bp, %r8d
movzwl 0x2(%rdx,%r8,8), %r9d
cmpw 0x2(%rdx,%rcx,8), %r9w
jge 0x1f754
movswl (%rdx,%r8,8), %r9d
movswl 0x1e(%rax), %r10d
subl %r10d, %r9d
cmpl %edi, %r9d
jg 0x1f754
cmpw 0x4(%rdx,%r8,8), %si
jle 0x1fa85
decl %ebp
jmp 0x1f71b
movb $0x1, (%r12)
jmp 0x1fab5
movq 0x8(%r15), %rdi
callq 0x27a7a
movq 0x10(%r15), %rdi
callq 0x27a7a
jmp 0x1fab5
movzwl 0x42(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x1fab5
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %edx
testb $0x40, %dl
je 0x1f7bd
xorl $0x80, %edx
movw %dx, (%rcx,%rax,2)
testb $0x2, 0x46(%r15)
movzwl 0x42(%r15), %eax
je 0x1f7bd
movzwl %ax, %esi
movq %r14, %rdi
callq 0x20742
movzwl 0x42(%r15), %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x2051b
movq %r14, %rdi
callq 0x4b8e
movq %rax, %rdi
callq 0x29b8
cmpq %r14, %rax
je 0x1f7e3
orw $-0x1, 0x42(%r15)
movq 0x10(%r15), %rcx
movzwl 0x44(%r15), %eax
movzwl (%rcx,%rax,2), %ecx
andl $0x128, %ecx            # imm = 0x128
cmpw $0x100, %cx             # imm = 0x100
je 0x1f981
jmp 0x1fab5
callq 0x35b3
movq %rax, %r13
movq %rax, %rdi
callq 0x35bb
cmpb $0x1, %al
je 0x1f826
movq %r13, %rdi
callq 0x35bb
cmpb $0x3, %al
jne 0x1f863
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3661
movq %r14, %rdi
movq %r13, %rsi
callq 0x2093e
movl %eax, %ebp
movzwl 0x42(%r15), %esi
movq %r14, %rdi
callq 0x2051b
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
movzwl %bp, %esi
movq %r14, %rdi
callq 0x2051b
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x1fab5
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %ecx
testw $0x128, %cx             # imm = 0x128
jne 0x1fab5
leaq 0x4(%rsp), %rdx
jmp 0x1f984
movq %rsp, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3661
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2093e
movl %eax, %ebp
movb $0x1, %bl
cmpw 0x42(%r15), %ax
je 0x1f901
movq %r12, %rdi
callq 0x35c6
movzwl 0x42(%r15), %eax
cmpw $-0x1, %ax
je 0x1f8d3
movzwl %ax, %esi
movq %r14, %rdi
callq 0x2051b
cmpw $-0x1, %bp
je 0x1f901
movzwl 0x44(%r15), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
pushq $0xe
popq %rsi
movq %r14, %rdi
callq 0x4caf
movl %eax, %ebx
cmpb $0x1, %al
jne 0x1f901
movzwl %bp, %esi
movq %r14, %rdi
callq 0x2051b
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
jmp 0x1fab5
callq 0x35b3
movq %rax, %r12
movq %rax, %rdi
callq 0x35bb
testb %al, %al
jne 0x1f933
xorl %edi, %edi
callq 0x7fb2
movq %rax, %rdi
callq 0x35bb
cmpb $0x4, %al
je 0x1f998
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x1f9b0
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3661
movq %r14, %rdi
movq %r13, %rsi
callq 0x2093e
movw %ax, 0x42(%r15)
jmp 0x1fa7e
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x1fab5
movq 0x10(%r15), %rcx
testb $0x38, (%rcx,%rax,2)
jne 0x1fab5
movq %rsp, %rdx
movl %eax, (%rdx)
pushq $0xe
popq %rsi
movq %r14, %rdi
callq 0x4caf
movl %eax, %ebx
jmp 0x1fab5
movq %r14, %rdi
callq 0x4b8e
movq %rax, %rdi
callq 0x2aaa
testb %al, %al
je 0x1fa74
andw $0x0, 0x42(%r15)
xorl %eax, %eax
jmp 0x1fa7e
movzwl 0x42(%r15), %eax
incl %eax
movw %ax, 0x42(%r15)
movzwl %ax, %edx
movzwl 0x40(%r15), %ecx
decl %ecx
cmpl %edx, %ecx
jg 0x1fa7e
movw %cx, 0x42(%r15)
movl %ecx, %eax
jmp 0x1fa7e
movzwl 0x42(%r15), %eax
testl %eax, %eax
je 0x1fa99
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x1fa92
xorl %eax, %eax
jmp 0x1fa94
movq %r14, %rdi
xorl %esi, %esi
callq 0x20368
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x1fa8e
movq 0x8(%r15), %rdx
movzwl (%rdx,%rcx,8), %edi
movzwl 0x4(%rdx,%rcx,8), %esi
subl %edi, %esi
incl %esi
movswl %si, %esi
sarl %esi
addl %edi, %esi
movzwl 0x40(%r15), %edi
movswl %si, %r9d
movq %rcx, %r8
cmpq %rdi, %r8
jae 0x1faa8
movzwl 0x2(%rdx,%r8,8), %r10d
cmpw 0x2(%rdx,%rcx,8), %r10w
jle 0x1fa6f
cmpw (%rdx,%r8,8), %si
jl 0x1fa6f
movswl 0x4(%rdx,%r8,8), %r10d
movswl 0x1e(%rax), %r11d
addl %r10d, %r11d
cmpl %r9d, %r11d
jge 0x1faa0
incq %r8
jmp 0x1fa42
orw $-0x1, 0x42(%r15)
movw $0xffff, %ax            # imm = 0xFFFF
movw %ax, 0x44(%r15)
jmp 0x1faad
movw %bp, 0x42(%r15)
movl %ebp, %ecx
jmp 0x1faa8
xorl %ecx, %ecx
jmp 0x1faa3
decl %eax
movw %ax, 0x42(%r15)
movw %ax, 0x44(%r15)
jmp 0x1faad
movl %r8d, %ecx
movw %cx, 0x42(%r15)
movw %cx, 0x44(%r15)
movq %r14, %rdi
callq 0x493a
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq