// File: /workspace/github_c_makefile_star2_O3/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 *0x3ed57(%rip)          # 0x60420
movl %eax, %ebx
cmpb $0x1, %al
jne 0x21b42
cmpb $0x7, %bpl
jne 0x216f6
leaq 0x1e415(%rip), %rsi     # 0x3faf5
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6c02
movq %r14, %rdi
callq 0x6af7
movb $0x1, %bl
cmpb $0x13, %bpl
ja 0x21b42
movq %rax, %r15
movzbl %bpl, %eax
leaq 0x1e334(%rip), %rcx     # 0x3fa4c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rsi
movq %r14, %rdi
callq 0x21f33
jmp 0x21b42
movl $0xffffffff, 0x42(%r15) # imm = 0xFFFFFFFF
jmp 0x21b3a
movzbl (%r12), %eax
addl $-0x11, %eax
cmpl $0x3, %eax
ja 0x21b42
leaq 0x1e346(%rip), %rcx     # 0x3fa9c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq %r14, %rdi
xorl %esi, %esi
callq 0x2248e
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x21b53
testw %cx, %cx
js 0x21b5a
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 0x2(%rdx,%rcx,8), %edi
movswl %si, %r9d
movq %rcx, %r8
cmpw %di, 0x2(%rdx,%r8,8)
jge 0x217ce
movswl (%rdx,%r8,8), %r10d
movswl 0x1e(%rax), %r11d
subl %r11d, %r10d
cmpl %r9d, %r10d
jg 0x217ce
cmpw 0x4(%rdx,%r8,8), %si
jle 0x21b6f
leaq -0x1(%r8), %r10
testq %r8, %r8
movq %r10, %r8
jg 0x217a8
jmp 0x21b5a
movb $0x1, (%r12)
jmp 0x21b42
movq 0x8(%r15), %rdi
callq 0x2a006
movq 0x10(%r15), %rdi
callq 0x2a006
jmp 0x21b42
movzwl 0x42(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21b42
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %edx
testb $0x40, %dl
je 0x21844
xorl $0x80, %edx
movw %dx, (%rcx,%rax,2)
testb $0x2, 0x46(%r15)
movzwl 0x42(%r15), %eax
je 0x21844
movzwl %ax, %esi
movq %r14, %rdi
callq 0x22878
movzwl 0x42(%r15), %eax
movzwl %ax, %esi
movq %r14, %rdi
callq 0x22641
movq %r14, %rdi
callq 0x4ed1
movq %rax, %rdi
callq 0x2a19
cmpq %r14, %rax
je 0x2186b
movw $0xffff, 0x42(%r15)     # imm = 0xFFFF
movq 0x10(%r15), %rcx
movzwl 0x44(%r15), %eax
movzwl (%rcx,%rax,2), %ecx
andl $0x128, %ecx            # imm = 0x128
cmpl $0x100, %ecx            # imm = 0x100
je 0x21a0d
jmp 0x21b42
callq 0x378a
movq %rax, %r13
movq %rax, %rdi
callq 0x3792
cmpb $0x1, %al
je 0x218af
movq %r13, %rdi
callq 0x3792
cmpb $0x3, %al
jne 0x218ec
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3838
movq %r14, %rdi
movq %r13, %rsi
callq 0x22a81
movl %eax, %ebp
movzwl 0x42(%r15), %esi
movq %r14, %rdi
callq 0x22641
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
movzwl %bp, %esi
movq %r14, %rdi
callq 0x22641
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21b42
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %ecx
testl $0x128, %ecx            # imm = 0x128
jne 0x21b42
leaq 0x4(%rsp), %rdx
jmp 0x21a10
movq %rsp, %rbx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3838
movq %r14, %rdi
movq %rbx, %rsi
callq 0x22a81
movl %eax, %ebp
movb $0x1, %bl
cmpw 0x42(%r15), %ax
je 0x2198d
movq %r12, %rdi
callq 0x379d
movzwl 0x42(%r15), %eax
cmpw $-0x1, %ax
je 0x2195d
movzwl %ax, %esi
movq %r14, %rdi
callq 0x22641
cmpw $-0x1, %bp
je 0x2198d
movzwl 0x44(%r15), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
movq %r14, %rdi
movl $0xe, %esi
callq 0x5018
movl %eax, %ebx
cmpb $0x1, %al
jne 0x2198d
movzwl %bp, %esi
movq %r14, %rdi
callq 0x22641
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
jmp 0x21b42
callq 0x378a
movq %rax, %r12
movq %rax, %rdi
callq 0x3792
testb %al, %al
jne 0x219bf
xorl %edi, %edi
callq 0x874d
movq %rax, %rdi
callq 0x3792
cmpb $0x4, %al
je 0x21a26
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x21a3e
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3838
movq %r14, %rdi
movq %r13, %rsi
callq 0x22a81
movw %ax, 0x42(%r15)
jmp 0x21b35
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21b42
movq 0x10(%r15), %rcx
testb $0x38, (%rcx,%rax,2)
jne 0x21b42
movq %rsp, %rdx
movl %eax, (%rdx)
movq %r14, %rdi
movl $0xe, %esi
callq 0x5018
movl %eax, %ebx
jmp 0x21b42
movq %r14, %rdi
callq 0x4ed1
movq %rax, %rdi
callq 0x2b0b
testb %al, %al
je 0x21b2a
movw $0x0, 0x42(%r15)
xorl %eax, %eax
jmp 0x21b35
movzwl 0x42(%r15), %eax
incl %eax
movw %ax, 0x42(%r15)
movzwl %ax, %edx
movzwl 0x40(%r15), %ecx
decl %ecx
cmpl %edx, %ecx
jg 0x21b35
movw %cx, 0x42(%r15)
movl %ecx, %eax
jmp 0x21b35
movzwl 0x42(%r15), %eax
testl %eax, %eax
je 0x21b68
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x21b61
xorl %eax, %eax
jmp 0x21b63
xorl %r12d, %r12d
movq %r14, %rdi
xorl %esi, %esi
callq 0x2248e
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x21b77
movzwl 0x40(%r15), %r9d
cmpw %r9w, %cx
jae 0x21b25
movq 0x8(%r15), %r8
movzwl (%r8,%rcx,8), %esi
movzwl 0x4(%r8,%rcx,8), %edx
subl %esi, %edx
incl %edx
movswl %dx, %edx
sarl %edx
addl %esi, %edx
movzwl 0x2(%r8,%rcx,8), %esi
movswl %dx, %edi
leaq (%r8,%rcx,8), %r8
addq $0x4, %r8
negl %r9d
movzwl %r9w, %r9d
addq %rcx, %r9
xorl %r12d, %r12d
cmpw %si, -0x2(%r8,%r12,8)
jle 0x21b19
cmpw -0x4(%r8,%r12,8), %dx
jl 0x21b19
movswl (%r8,%r12,8), %r10d
movswl 0x1e(%rax), %r11d
addl %r10d, %r11d
cmpl %edi, %r11d
jge 0x21b74
incq %r12
movl %r9d, %r10d
addw %r12w, %r10w
jne 0x21af7
movl %ecx, %r12d
jmp 0x21b7c
movw $0xffff, 0x42(%r15)     # imm = 0xFFFF
movw $0xffff, %ax            # imm = 0xFFFF
movw %ax, 0x44(%r15)
movq %r14, %rdi
callq 0x4c4d
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
movw %cx, 0x42(%r15)
movw %cx, 0x44(%r15)
jmp 0x21b3a
decl %eax
movw %ax, 0x42(%r15)
movw %ax, 0x44(%r15)
jmp 0x21b3a
movl %r8d, %ecx
jmp 0x21b55
addl %ecx, %r12d
movw %r12w, 0x42(%r15)
movw %r12w, 0x44(%r15)
jmp 0x21b3a