// File: fulhax[P]ncpamixer/src/ui/tab.cpp
// Function: Tab::handleMouse(int, int, int)
void Tab::handleMouse(int x, int y, int button)
{
    if (object == nullptr) {
        return;
    }

    int baseY = 0;
    int current_block = 0;
    int BLOCK_SIZE = getBlockSize();

    total_blocks = (ui.height - 2) / BLOCK_SIZE;
    int blocks_drawn = 0;

    bool more_up = false;
    bool more_down = false;

    for (auto &i : *object) {
        auto item = i.second;
        if (current_block <= selected_block - total_blocks) {
            current_block++;
            more_up = true;
            continue;
        }

        if (blocks_drawn >= total_blocks) {
            more_down = true;
            break;
        }

        blocks_drawn++;

        if (has_volume) {
            if (handleMouseVolumeBar(item, x, y, ui.width, ui.height, 0, baseY + 3, button)) {
                return;
            }
        } else { // Configuration
            if (item->active_attribute != nullptr) {
                if (handleMouseDropDown(item, x, y, ui.width - 2, 2, 1, baseY + 2)) {
                    return;
                }
            }
        }

        unsigned int len = 0;
        unsigned int sink_pos = 0;
        PaObject* toggle_item = nullptr;

        if (toggle != nullptr) {
            auto rel = toggle->find(i.second->getRelation());

            if (rel != toggle->end()) {
                char *name = rel->second->name;

                if (name != nullptr) {
                    len = strlen(name);
                    sink_pos = ui.width - 1 - len;
                    toggle_item = rel->second;
                }
            }
        } else {
            if (i.second->active_attribute != nullptr && has_volume) {
                len = strlen(i.second->active_attribute->description);
                sink_pos = ui.width - 1 - len;
                toggle_item = i.second;
            }
        }

        if (toggle_item != nullptr) {
            auto save_selected_index = selected_index;
            auto save_selected_block = selected_block;

            selected_index = i.first;
            selected_block = current_block;

            bool done = handleMouseDropDown(toggle_item, x, y, len, 1, sink_pos, baseY + 1);

            selected_index = save_selected_index;
            selected_block = save_selected_block;

            if (done) {
                return;
            }
        }

        baseY += BLOCK_SIZE;
        current_block++;
    }

    if (more_up) {
        if (handleMouseMoreUp(x, y, 4, 1, (ui.width / 2) - 4, 0)) {
            return;
        }
    }

    if (more_down) {
        if (handleMouseMoreDown(x, y, 4, 1, (ui.width / 2) - 4, ui.height - 2)) {
            return;
        }
    }
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movl %esi, 0xbc(%rsp)
movl %edx, 0xb8(%rsp)
movl %ecx, 0xb4(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, 0x10(%rax)
jne 0x1945d
jmp 0x1995d
movq 0x30(%rsp), %rdi
movl $0x0, 0xb0(%rsp)
movl $0x0, 0xac(%rsp)
callq 0x172e0
movl %eax, 0xa8(%rsp)
leaq 0x1246d(%rip), %rax     # 0x2b8f8
movl 0xc(%rax), %eax
subl $0x2, %eax
cltd
idivl 0xa8(%rsp)
movl %eax, %ecx
movq 0x30(%rsp), %rax
movl %ecx, 0x24(%rax)
movl $0x0, 0xa4(%rsp)
movb $0x0, 0xa3(%rsp)
movb $0x0, 0xa2(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0xd1b0
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0xd200
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0xd1e0
testb $0x1, %al
jne 0x19512
jmp 0x19894
leaq 0x90(%rsp), %rdi
callq 0xd360
movq 0x30(%rsp), %rdx
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x78(%rsp)
movl 0xac(%rsp), %eax
movl 0x20(%rdx), %ecx
subl 0x24(%rdx), %ecx
cmpl %ecx, %eax
jg 0x1956c
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
movb $0x1, 0xa3(%rsp)
jmp 0x19882
movq 0x30(%rsp), %rcx
movl 0xa4(%rsp), %eax
cmpl 0x24(%rcx), %eax
jl 0x1958a
movb $0x1, 0xa2(%rsp)
jmp 0x19894
movq 0x30(%rsp), %rax
movl 0xa4(%rsp), %ecx
addl $0x1, %ecx
movl %ecx, 0xa4(%rsp)
testb $0x1, 0x8(%rax)
je 0x1960c
movq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
leaq 0x12333(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %r8d
leaq 0x12328(%rip), %rax     # 0x2b8f8
movl 0xc(%rax), %r9d
movl 0xb0(%rsp), %r10d
addl $0x3, %r10d
movl 0xb4(%rsp), %eax
xorl %r11d, %r11d
movl $0x0, (%rsp)
movl %r10d, 0x8(%rsp)
movl %eax, 0x10(%rsp)
callq 0x19970
testb $0x1, %al
jne 0x19605
jmp 0x1960a
jmp 0x1995d
jmp 0x19671
movq 0x78(%rsp), %rax
cmpq $0x0, 0x230(%rax)
je 0x1966f
movq 0x30(%rsp), %rdi
movq 0x78(%rsp), %rsi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
leaq 0x122be(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %r8d
subl $0x2, %r8d
movl 0xb0(%rsp), %eax
addl $0x2, %eax
movl $0x2, %r9d
movl $0x1, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x19a60
testb $0x1, %al
jne 0x19668
jmp 0x1966d
jmp 0x1995d
jmp 0x1966f
jmp 0x19671
movq 0x30(%rsp), %rax
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
cmpq $0x0, 0x18(%rax)
je 0x19756
movq 0x30(%rsp), %rax
movq 0x18(%rax), %rax
movq %rax, 0x28(%rsp)
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x28(%rsp), %rdi
movl %eax, 0x5c(%rsp)
leaq 0x5c(%rsp), %rsi
callq 0xd330
movq %rax, %rcx
movq 0x30(%rsp), %rax
movq %rcx, 0x60(%rsp)
movq 0x18(%rax), %rdi
callq 0xd200
movq %rax, 0x50(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0xd1e0
testb $0x1, %al
jne 0x196fd
jmp 0x19754
leaq 0x60(%rsp), %rdi
callq 0xd230
movq 0x8(%rax), %rax
addq $0x10, %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0x19752
movq 0x48(%rsp), %rdi
callq 0x7310
movl %eax, 0x74(%rsp)
leaq 0x121c7(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %eax
subl $0x1, %eax
subl 0x74(%rsp), %eax
movl %eax, 0x70(%rsp)
leaq 0x60(%rsp), %rdi
callq 0xd230
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
jmp 0x19754
jmp 0x197c2
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x230(%rax)
je 0x197c0
movq 0x30(%rsp), %rax
testb $0x1, 0x8(%rax)
je 0x197c0
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x230(%rax), %rdi
addq $0x107, %rdi            # imm = 0x107
callq 0x7310
movl %eax, 0x74(%rsp)
leaq 0x12157(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %eax
subl $0x1, %eax
subl 0x74(%rsp), %eax
movl %eax, 0x70(%rsp)
movq 0x80(%rsp), %rax
movq 0x8(%rax), %rax
movq %rax, 0x68(%rsp)
jmp 0x197c2
cmpq $0x0, 0x68(%rsp)
je 0x1985c
movq 0x30(%rsp), %rdi
movl 0xc(%rdi), %eax
movl %eax, 0x44(%rsp)
movl 0x20(%rdi), %eax
movl %eax, 0x40(%rsp)
movq 0x80(%rsp), %rax
movl (%rax), %eax
movl %eax, 0xc(%rdi)
movl 0xac(%rsp), %eax
movl %eax, 0x20(%rdi)
movq 0x68(%rsp), %rsi
movl 0xbc(%rsp), %edx
movl 0xb8(%rsp), %ecx
movl 0x74(%rsp), %r8d
movl 0x70(%rsp), %r10d
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl $0x1, %r9d
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x19a60
movb %al, %cl
movq 0x30(%rsp), %rax
andb $0x1, %cl
movb %cl, 0x3f(%rsp)
movl 0x44(%rsp), %ecx
movl %ecx, 0xc(%rax)
movl 0x40(%rsp), %ecx
movl %ecx, 0x20(%rax)
testb $0x1, 0x3f(%rsp)
je 0x1985a
jmp 0x1995d
jmp 0x1985c
movl 0xa8(%rsp), %eax
addl 0xb0(%rsp), %eax
movl %eax, 0xb0(%rsp)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
leaq 0x90(%rsp), %rdi
callq 0xd380
jmp 0x194f4
testb $0x1, 0xa3(%rsp)
je 0x198f5
movq 0x30(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl 0xb8(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0x1203c(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl 0x24(%rsp), %edx
movl %eax, %r9d
subl $0x4, %r9d
movl $0x4, %ecx
movl $0x1, %r8d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x19af0
testb $0x1, %al
jne 0x198f1
jmp 0x198f3
jmp 0x1995d
jmp 0x198f5
testb $0x1, 0xa2(%rsp)
je 0x1995d
movq 0x30(%rsp), %rdi
movl 0xbc(%rsp), %esi
movl 0xb8(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x11fdb(%rip), %rax     # 0x2b8f8
movl 0x8(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl 0x20(%rsp), %edx
movl %eax, %r9d
subl $0x4, %r9d
leaq 0x11fbe(%rip), %rax     # 0x2b8f8
movl 0xc(%rax), %eax
subl $0x2, %eax
movl $0x4, %ecx
movl $0x1, %r8d
movl %eax, (%rsp)
callq 0x19b80
testb $0x1, %al
jne 0x19959
jmp 0x1995b
jmp 0x1995d
jmp 0x1995d
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)