// File: /workspace/github_c_makefile_star2_O1/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 *0x3f52b(%rip)          # 0x60420
movl %eax, %ebx
cmpb $0x1, %al
jne 0x21359
cmpb $0x7, %bpl
jne 0x20f22
leaq 0x1ea65(%rip), %rsi     # 0x3f971
movq %r12, %rdi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x6bf5
movq %r14, %rdi
callq 0x6ad8
cmpb $0x13, %bpl
ja 0x21359
movq %rax, %r15
movzbl %bpl, %eax
leaq 0x1e986(%rip), %rcx     # 0x3f8c8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq (%r15), %rsi
movq %r14, %rdi
callq 0x21710
jmp 0x21359
movl $0xffffffff, 0x42(%r15) # imm = 0xFFFFFFFF
jmp 0x21351
movzbl (%r12), %eax
addl $-0x11, %eax
cmpl $0x3, %eax
ja 0x21359
leaq 0x1e998(%rip), %rcx     # 0x3f918
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x21ccf
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x21342
testw %cx, %cx
js 0x21347
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, %r8d
cmpw %di, 0x2(%rdx,%rcx,8)
jge 0x20ff5
movswl (%rdx,%rcx,8), %r9d
movswl 0x1e(%rax), %r10d
subl %r10d, %r9d
cmpl %r8d, %r9d
jg 0x20ff5
cmpw 0x4(%rdx,%rcx,8), %si
jle 0x21340
leaq -0x1(%rcx), %r9
testq %rcx, %rcx
movq %r9, %rcx
jg 0x20fd1
jmp 0x21347
movb $0x1, (%r12)
jmp 0x21359
movq 0x8(%r15), %rdi
callq 0x29984
movq 0x10(%r15), %rdi
callq 0x29984
jmp 0x21359
movzwl 0x42(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21359
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %edx
testb $0x40, %dl
je 0x21063
xorl $0x80, %edx
movw %dx, (%rcx,%rax,2)
testb $0x2, 0x46(%r15)
je 0x21063
movzwl 0x42(%r15), %esi
movq %r14, %rdi
callq 0x220c3
movzwl 0x42(%r15), %esi
movq %r14, %rdi
callq 0x21e82
movq %r14, %rdi
callq 0x4ea3
movq %rax, %rdi
callq 0x2a39
cmpq %r14, %rax
je 0x2108c
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 0x2122c
jmp 0x21359
callq 0x3784
movq %rax, %r13
movq %rax, %rdi
callq 0x378c
cmpb $0x1, %al
je 0x210d0
movq %r13, %rdi
callq 0x378c
cmpb $0x3, %al
jne 0x2110d
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3832
movq %r14, %rdi
movq %r13, %rsi
callq 0x222f9
movl %eax, %ebp
movzwl 0x42(%r15), %esi
movq %r14, %rdi
callq 0x21e82
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
movzwl %bp, %esi
movq %r14, %rdi
callq 0x21e82
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21359
movq 0x10(%r15), %rcx
movzwl (%rcx,%rax,2), %ecx
testl $0x128, %ecx            # imm = 0x128
jne 0x21359
leaq 0x4(%rsp), %rdx
jmp 0x2122f
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3832
movq %r14, %rdi
movq %r13, %rsi
callq 0x222f9
movl %eax, %ebp
cmpw 0x42(%r15), %ax
je 0x211ac
movq %r12, %rdi
callq 0x3797
movzwl 0x42(%r15), %eax
cmpw $-0x1, %ax
je 0x2117c
movzwl %ax, %esi
movq %r14, %rdi
callq 0x21e82
cmpw $-0x1, %bp
je 0x211ac
movzwl 0x44(%r15), %eax
leaq 0x4(%rsp), %rdx
movl %eax, (%rdx)
movq %r14, %rdi
movl $0xe, %esi
callq 0x4fea
movl %eax, %ebx
cmpb $0x1, %al
jne 0x211ac
movzwl %bp, %esi
movq %r14, %rdi
callq 0x21e82
movw %bp, 0x42(%r15)
movw %bp, 0x44(%r15)
jmp 0x21359
callq 0x3784
movq %rax, %r12
movq %rax, %rdi
callq 0x378c
testb %al, %al
jne 0x211de
xorl %edi, %edi
callq 0x87d5
movq %rax, %rdi
callq 0x378c
cmpb $0x4, %al
je 0x21245
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x2125d
movq %rsp, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3832
movq %r14, %rdi
movq %r13, %rsi
callq 0x222f9
movw %ax, 0x42(%r15)
jmp 0x21347
movzwl 0x44(%r15), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x21359
movq 0x10(%r15), %rcx
testb $0x38, (%rcx,%rax,2)
jne 0x21359
movq %rsp, %rdx
movl %eax, (%rdx)
movq %r14, %rdi
movl $0xe, %esi
callq 0x4fea
movl %eax, %ebx
jmp 0x21359
movq %r14, %rdi
callq 0x4ea3
movq %rax, %rdi
callq 0x2b2b
testb %al, %al
je 0x21331
movw $0x0, 0x42(%r15)
jmp 0x21347
movzwl 0x42(%r15), %eax
incl %eax
movw %ax, 0x42(%r15)
movzwl %ax, %ecx
movzwl 0x40(%r15), %eax
decl %eax
cmpl %ecx, %eax
jle 0x212a7
jmp 0x21347
cmpw $-0x1, 0x42(%r15)
jne 0x21297
movw $0x0, 0x42(%r15)
movzwl 0x42(%r15), %eax
testw %ax, %ax
je 0x21347
decl %eax
movw %ax, 0x42(%r15)
jmp 0x21347
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
callq 0x21ccf
movzwl 0x42(%r15), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x21342
movzwl 0x40(%r15), %edx
cmpw %dx, %cx
setb %sil
jae 0x21347
movq 0x8(%r15), %rdi
movzwl (%rdi,%rcx,8), %r9d
movzwl 0x4(%rdi,%rcx,8), %r8d
subl %r9d, %r8d
incl %r8d
movswl %r8w, %r8d
sarl %r8d
addl %r9d, %r8d
movzwl 0x2(%rdi,%rcx,8), %r9d
movswl %r8w, %r10d
cmpw %r9w, 0x2(%rdi,%rcx,8)
jle 0x21323
cmpw (%rdi,%rcx,8), %r8w
jl 0x21323
movswl 0x4(%rdi,%rcx,8), %r11d
movswl 0x1e(%rax), %ebp
addl %r11d, %ebp
cmpl %r10d, %ebp
jge 0x2133a
incq %rcx
cmpq %rdx, %rcx
setb %sil
jne 0x21302
jmp 0x21347
movw $0xffff, 0x42(%r15)     # imm = 0xFFFF
jmp 0x21347
testb $0x1, %sil
je 0x21347
movl %ecx, %ebp
movw %bp, 0x42(%r15)
movzwl 0x42(%r15), %eax
movw %ax, 0x44(%r15)
movq %r14, %rdi
callq 0x4c36
movl %ebx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq