// File: /workspace/github_cpp_makefile_star2_O3/fulhax[P]ncpamixer/src/ui/tab.cpp
// Function: Tab::dropDown(int, int, std::map<unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>>, unsigned int, unsigned int, unsigned int)
uint32_t Tab::dropDown(
    int x,
    int y,
    std::map<uint32_t, std::string> objects,
    uint32_t current,
    uint32_t width,
    uint32_t height
)
{
    if (objects.empty()) {
        return -1;
    }

    bool autowidth = false;
    bool autoheight = false;

    if (width == 0) {
        autowidth = true;
    }

    if (height == 0) {
        autoheight = true;
    }

    uint32_t selected = 0;

    std::vector<ITEM *> items;
    MENU *menu = 0;
    WINDOW *menu_win = 0;

    for (auto &i : objects) {
        ITEM *item = new_item(i.second.c_str(), nullptr);
        set_item_opts(item, O_SELECTABLE);
        set_item_userptr(item, static_cast<void*>(&i));

        items.push_back(item);

        if (i.first == current) {
            selected = items.size() - 1;
        }

        if (autowidth) {
            width = (width < strlen(i.second.c_str()) + 3) ?
                    strlen(i.second.c_str()) :
                    width;
        }

        if (autoheight) {
            height = (height < 5) ? height + 1 : 5;
        }
    }

    items.push_back(new_item(nullptr, nullptr));
    menu = new_menu(&items[0]);

    if (x < 0) {
        x = ui.width - (width + 2);
    }

    menu_win = newwin(height + 2, width + 2, y, x);
    keypad(menu_win, true);

    set_menu_win(menu, menu_win);
    set_menu_sub(menu, derwin(menu_win, height + 1, width, 1, 1));
    set_menu_format(menu, height, 1);

    wbkgd(menu_win, COLOR_PAIR(COLOR_BORDER));
    //set_menu_back(menu, COLOR_PAIR(7));
    set_menu_fore(menu, COLOR_PAIR(COLOR_DROPDOWN_SELECTED));
    set_menu_grey(menu, COLOR_PAIR(COLOR_DROPDOWN_UNSELECTED));

    //set_menu_mark(menu, "* ");
    set_menu_mark(menu, "");
    menu_opts_on(menu, O_ONEVALUE);
    menu_opts_off(menu, O_SHOWDESC);
    menu_opts_off(menu, O_NONCYCLIC);

    box(menu_win, 0, 0);

    post_menu(menu);
    set_current_item(menu, items[selected]);

    wrefresh(menu_win);

    bool selecting = true;

    while (selecting) {
        int input = wgetch(menu_win);

        if (input == ERR) {
            continue;
        }

#ifdef KEY_MOUSE
        if (input == KEY_MOUSE) {
            MEVENT mevent;
            int ok;

            ok = getmouse(&mevent);
            if (ok != OK) {
                continue;
            }

            if (mevent.bstate & BUTTON1_PRESSED) {
                if (mevent.y < y || mevent.y > y + (int)height + 1 ||
                    mevent.x < x || mevent.x > x + (int)width + 1) {
                    clrtoeol();

                    ITEM *item = current_item(menu);
                    selected = static_cast<std::pair<uint32_t, std::string>*>(
                                   item_userptr(item)
                               )->first;

                    selecting = false;
                    continue;
                }
            }

            if (mevent.y == y || mevent.y == y + (int)height + 1 ||
                mevent.x == x || mevent.x == x + (int)width + 1) {
                continue;
            }

            if (mevent.bstate & BUTTON1_PRESSED) {
                int top = top_row(menu);
                int idx = mevent.y - y - 1 + top;
                ITEM** its = menu_items(menu);
                int itc = item_count(menu);
                if (idx < itc) {
                    ITEM* item = its[idx];
                    set_current_item(menu, item);
                    selected = static_cast<std::pair<uint32_t, std::string>*>(
                                   item_userptr(item)
                               )->first;
                }

                selecting = false;
                continue;
            }
#if NCURSES_MOUSE_VERSION > 1
            else if (mevent.bstate & BUTTON4_PRESSED) {
                menu_driver(menu, REQ_UP_ITEM);
            } else if (mevent.bstate & BUTTON5_PRESSED) {
                menu_driver(menu, REQ_DOWN_ITEM);
            }
#endif
        }
#endif

        if (input == KEY_RESIZE) {
            selecting = false;
            continue;
        }

        std::string key = std::to_string(input);
        
        std::string event = config.getString(("keycode." +  key).c_str(), "unbound");

        if (!strcmp("unbound", event.c_str())) {
            continue;
        }

        if (!strcmp("select", event.c_str())) {
            clrtoeol();

            ITEM *item = current_item(menu);
            selected = static_cast<std::pair<uint32_t, std::string>*>(
                            item_userptr(item)
                       )->first;

            selecting = false;
        } else if (!strcmp("move_down", event.c_str())) {
            menu_driver(menu, REQ_DOWN_ITEM);
        } else if (!strcmp("move_up", event.c_str())) {
            menu_driver(menu, REQ_UP_ITEM);
        } else if (!strcmp("page_up", event.c_str())) {
            menu_driver(menu, REQ_SCR_UPAGE);
        } else if (!strcmp("page_down", event.c_str())) {
            menu_driver(menu, REQ_SCR_DPAGE);
        } else if (!strcmp("quit", event.c_str())) {
            selecting = false;
        }

        wrefresh(menu_win);
    }

    unpost_menu(menu);
    free_menu(menu);

    for (auto i : items) {
        if (i != nullptr)
            free_item(i);
    }

    items.clear();
    refresh();

    return selected;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movl %r9d, 0x20(%rsp)
cmpq $0x0, 0x28(%rdx)
je 0xf0c2
movl %r8d, %r12d
movq %rdx, %r13
movl %edi, 0x2c(%rsp)
movq %rsi, 0x70(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
movq 0x18(%rdx), %rbx
addq $0x8, %r13
cmpq %r13, %rbx
je 0xf0cc
movl %ecx, %ebp
movq $0x0, 0x10(%rsp)
movl %r12d, %eax
movq %rax, 0x18(%rsp)
movl 0x20(%rsp), %eax
movl %eax, %r14d
movl %ecx, 0x8(%rsp)
movq 0x28(%rbx), %rdi
xorl %esi, %esi
callq 0x65d0
movq %rax, 0x50(%rsp)
movq %rax, %rdi
movl $0x1, %esi
callq 0x6b80
leaq 0x20(%rbx), %r15
movq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x6270
movq 0x38(%rsp), %rsi
cmpq 0x40(%rsp), %rsi
je 0xf036
movq 0x50(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, 0x38(%rsp)
jmp 0xf045
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x10020
cmpl %ebp, (%r15)
jne 0xf05f
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
shrq $0x3, %rax
decl %eax
movq %rax, 0x10(%rsp)
testl %r12d, %r12d
jne 0xf08f
movl %r12d, %ebp
movq 0x18(%rsp), %r12
movl %r12d, %r15d
movq 0x28(%rbx), %rdi
callq 0x62b0
leaq 0x3(%rax), %rcx
cmpq %r15, %rcx
cmovbel %r12d, %eax
movl %ebp, %r12d
movl 0x8(%rsp), %ebp
movq %rax, 0x18(%rsp)
leal 0x1(%r14), %r15d
cmpl $0x5, %r14d
movl $0x5, %eax
cmovael %eax, %r15d
cmpl $0x0, 0x20(%rsp)
cmovnel %r14d, %r15d
movq %rbx, %rdi
callq 0x6440
movq %rax, %rbx
movl %r15d, %r14d
cmpq %r13, %rax
jne 0xefec
jmp 0xf0e4
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0xf608
movq $0x0, 0x10(%rsp)
movl 0x20(%rsp), %eax
movl %eax, %r15d
movl %r12d, %eax
movq %rax, 0x18(%rsp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x65d0
leaq 0x50(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x30(%rsp), %rdi
callq 0x1015c
movq 0x30(%rsp), %rdi
callq 0x6380
movq 0x70(%rsp), %rbx
movq 0x18(%rsp), %rbp
movq %rax, %r14
leaq 0x96db(%rip), %rax      # 0x187f8
movl 0x8(%rax), %ecx
leal 0x2(%rbp), %esi
subl %esi, %ecx
movl 0x2c(%rsp), %eax
testl %eax, %eax
cmovnsl %eax, %ecx
leal 0x2(%r15), %edi
movl %ebx, %edx
movq %rcx, 0x20(%rsp)
callq 0x65e0
movq %rax, %r13
movq %rax, %rdi
movl $0x1, %esi
callq 0x6250
movq %r14, %rdi
movq %r13, %rsi
callq 0x63e0
leal 0x1(%r15), %esi
movq %r13, %rdi
movl %ebp, %edx
movl $0x1, %ecx
movl $0x1, %r8d
callq 0x6480
movq %r14, %rdi
movq %rax, %rsi
callq 0x6090
movq %r14, %rdi
movl %r15d, %esi
movl $0x1, %edx
callq 0x6720
movq %r13, %rdi
movl $0xd00, %esi            # imm = 0xD00
callq 0x69f0
movq %r14, %rdi
movl $0xb00, %esi            # imm = 0xB00
callq 0x61c0
movq %r14, %rdi
movl $0xc00, %esi            # imm = 0xC00
callq 0x60f0
leaq 0x2329(%rip), %rsi      # 0x114e4
movq %r14, %rdi
callq 0x6260
movq %r14, %rdi
movl $0x1, %esi
callq 0x6b20
movq %r14, %rdi
movl $0x2, %esi
callq 0x6a90
movq %r14, %rdi
movl $0x20, %esi
callq 0x6a90
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x6b10
movq %r14, %rdi
callq 0x6840
movl 0x10(%rsp), %eax
movq 0x30(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r14, %rdi
callq 0x6100
movq %r13, %rdi
callq 0x6670
leal (%rbx,%r15), %r12d
incl %r12d
movq 0x20(%rsp), %rax
leal 0x1(%rbp,%rax), %eax
movl %eax, 0x18(%rsp)
movl %ebx, %eax
notl %eax
movl %eax, 0x2c(%rsp)
leaq 0x50(%rsp), %rbp
movq %r14, 0x8(%rsp)
movq %r13, %rdi
callq 0x6240
movl %eax, %r15d
cmpl $-0x1, %eax
je 0xf241
cmpl $0x199, %r15d           # imm = 0x199
jne 0xf2e6
movq %rbp, %rdi
callq 0x69c0
testl %eax, %eax
jne 0xf241
movl 0x58(%rsp), %ecx
movl 0x60(%rsp), %eax
testb $0x2, %al
je 0xf29f
cmpl %ebx, %ecx
jl 0xf575
cmpl %r12d, %ecx
jg 0xf575
movl 0x54(%rsp), %edx
cmpl 0x20(%rsp), %edx
jl 0xf575
cmpl 0x18(%rsp), %edx
jg 0xf575
cmpl %ebx, %ecx
je 0xf241
cmpl %r12d, %ecx
je 0xf241
movl 0x54(%rsp), %ecx
cmpl 0x20(%rsp), %ecx
sete %dl
cmpl 0x18(%rsp), %ecx
sete %cl
orb %dl, %cl
jne 0xf241
testb $0x2, %al
jne 0xf61c
movl $0x202, %esi            # imm = 0x202
btl $0x10, %eax
jb 0xf2dc
movl $0x203, %esi            # imm = 0x203
btl $0x15, %eax
jae 0xf2f3
movq %r14, %rdi
callq 0x64d0
jmp 0xf2f3
cmpl $0x19a, %r15d           # imm = 0x19A
je 0xf5a1
movl %r15d, %ebx
negl %ebx
cmovsl %r15d, %ebx
movl $0x1, %r14d
cmpl $0xa, %ebx
jb 0xf34d
movl $0x4, %r14d
movl %ebx, %eax
cmpl $0x63, %eax
jbe 0xf344
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0xf34a
cmpl $0x2710, %eax           # imm = 0x2710
jb 0xf34d
movl %eax, %ecx
movl $0xd1b71759, %edx       # imm = 0xD1B71759
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r14d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0xf30f
addl $-0x3, %r14d
jmp 0xf34d
addl $-0x2, %r14d
jmp 0xf34d
decl %r14d
shrl $0x1f, %r15d
leal (%r14,%r15), %esi
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %rbp, %rdi
movl $0x2d, %edx
callq 0x66b0
movl %r15d, %edi
addq 0x50(%rsp), %rdi
movl %r14d, %esi
movl %ebx, %edx
callq 0xaba1
leaq 0x98(%rsp), %rdi
leaq 0x3506(%rip), %rsi      # 0x12893
movq %rbp, %rdx
callq 0xd392
movq 0x98(%rsp), %rbx
leaq 0x88(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x78(%rsp), %r14
movq %r14, %rdi
leaq 0x34c7(%rip), %rsi      # 0x12880
leaq 0x34c7(%rip), %rdx      # 0x12887
callq 0x73f0
leaq 0xb8(%rsp), %rdi
leaq 0x93cc(%rip), %rsi      # 0x187a0
movq %rbx, %rdx
movq %r14, %rcx
callq 0xa416
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xf401
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x65c0
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xf426
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x65c0
movq 0xb8(%rsp), %rbx
leaq 0x344b(%rip), %rdi      # 0x12880
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf4bb
leaq 0x3629(%rip), %rdi      # 0x12a71
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf4c0
leaq 0x3532(%rip), %rdi      # 0x1298d
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf4ed
leaq 0x3513(%rip), %rdi      # 0x12985
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf4f4
leaq 0x35f3(%rip), %rdi      # 0x12a78
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf4fb
leaq 0x35e8(%rip), %rdi      # 0x12a80
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
je 0xf502
leaq 0x33f1(%rip), %rdi      # 0x1289c
movq %rbx, %rsi
callq 0x67f0
testl %eax, %eax
setne %r14b
jmp 0xf514
movb $0x1, %r14b
jmp 0xf524
movq 0x8ad9(%rip), %rax      # 0x17fa0
movq (%rax), %rdi
callq 0x6610
movq 0x8(%rsp), %rdi
callq 0x68f0
movq %rax, %rdi
callq 0x6960
movl (%rax), %eax
movq %rax, 0x10(%rsp)
xorl %r14d, %r14d
jmp 0xf514
movl $0x203, %esi            # imm = 0x203
jmp 0xf507
movl $0x202, %esi            # imm = 0x202
jmp 0xf507
movl $0x207, %esi            # imm = 0x207
jmp 0xf507
movl $0x206, %esi            # imm = 0x206
movb $0x1, %r14b
movq 0x8(%rsp), %rdi
callq 0x64d0
movq %r13, %rdi
callq 0x6670
movq 0xb8(%rsp), %rbx
leaq 0xc8(%rsp), %rax
cmpq %rax, %rbx
je 0xf544
movq 0xc8(%rsp), %rsi
incq %rsi
movq %rbx, %rdi
callq 0x65c0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xf560
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x65c0
testb %r14b, %r14b
movq 0x70(%rsp), %rbx
movq 0x8(%rsp), %r14
jne 0xf241
jmp 0xf5a1
movq 0x8a24(%rip), %rax      # 0x17fa0
movq (%rax), %rdi
callq 0x6610
movq %r14, %rdi
callq 0x68f0
movq %rax, %rdi
callq 0x6960
movl (%rax), %r15d
movq %r15, 0x10(%rsp)
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x6460
movq %r14, %rdi
callq 0x6210
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r14
cmpq %r14, %rbx
je 0xf5e7
movq (%rbx), %rdi
testq %rdi, %rdi
je 0xf5cd
callq 0x66e0
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0xf5c0
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0xf5e7
movq %rax, 0x38(%rsp)
callq 0x6a10
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %rbx
je 0xf608
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x65c0
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x67a0
movl %eax, %ebx
addl 0x2c(%rsp), %ebx
addl 0x58(%rsp), %ebx
movq %r14, %rdi
callq 0x6160
movq 0x10(%rsp), %r15
movq %r14, %rdi
movq %rax, %r14
callq 0x62d0
cmpl %eax, %ebx
jge 0xf597
movslq %ebx, %rax
movq (%r14,%rax,8), %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x6100
movq %rbx, %rdi
callq 0x6960
jmp 0xf594
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf6e4
jmp 0xf736
movq %rax, %rbx
movq 0x78(%rsp), %rdi
leaq 0x88(%rsp), %rax
cmpq %rax, %rdi
je 0xf6be
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x65c0
jmp 0xf6be
movq %rax, %rbx
movq 0x98(%rsp), %rdi
leaq 0xa8(%rsp), %rax
cmpq %rax, %rdi
je 0xf70c
movq 0xa8(%rsp), %rsi
jmp 0xf704
movq %rax, %rbx
jmp 0xf70c
jmp 0xf736
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
leaq 0xc8(%rsp), %rax
cmpq %rax, %rdi
je 0xf70c
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x65c0
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0xf739
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x65c0
jmp 0xf739
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
jmp 0xf736
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0xf750
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x65c0
movq %rbx, %rdi
callq 0x6a80