// File: fulhax[P]ncpamixer/src/ui/ui.cpp
// Function: Ui::handleInput()
void Ui::handleInput()
{
    set_escdelay(0);

#ifdef KEY_MOUSE
    mouseinterval(0);
#if NCURSES_MOUSE_VERSION > 1
    mousemask(BUTTON1_PRESSED | BUTTON4_PRESSED | BUTTON5_PRESSED, NULL);
#else
    mousemask(BUTTON1_PRESSED, NULL);
#endif
#endif

    int input = wgetch(window);
    std::string event{};

    if (input == ERR) {
        return;
    }

    switch (input) {
#ifdef KEY_MOUSE
        case KEY_MOUSE: {
            MEVENT mevent;
            int ok;

            ok = getmouse(&mevent);
            if (ok != OK) {
                return;
            }
            if (mevent.y == height - 1) {
                if (mevent.bstate & BUTTON1_PRESSED) {
                    int x = 0;
                    for (int i = 0; i < NUM_TABS; i++) {
                        int len = strlen(tabs[i]);
                        if (mevent.x >= x && mevent.x < x + len) {
                            switchTab(i);
                            return;
                        }
                        x += len + 1;
                    }
#if NCURSES_MOUSE_VERSION > 1
                } else if (mevent.bstate & BUTTON4_PRESSED) {
                    switchTab(++tab_index);
                    return;
                } else if (mevent.bstate & BUTTON5_PRESSED) {
                    switchTab(--tab_index);
                    return;
#endif
                }
            } else {
                int button = 0;
                if (mevent.bstate & BUTTON1_PRESSED) {
                    button = 1;
                }
#if NCURSES_MOUSE_VERSION > 1
                else if (mevent.bstate & BUTTON4_PRESSED) {
                    button = 4;
                } else if (mevent.bstate & BUTTON5_PRESSED) {
                    button = 5;
                }
#endif

                current_tab->handleMouse(mevent.x, mevent.y, button);
            }

            return;
        }
#endif
        case KEY_RESIZE:
            getmaxyx(stdscr, height, width);
            wresize(window, height - 1, width);

            mvwin(statusbar, height - 1, 0);
            wresize(statusbar, 1, width);

            return;

        case 27: { // Fix for alt/escape (also f-keys on some terminals)
            input = wgetch(window);

            if (input != -1 && input != 79) {
                std::string key = std::to_string(input);

                event = config.getString(
                            ("keycode.alt." +  key).c_str(),
                            "unbound"
                        );

                break;
            }

            if (input == 79) {
                input = wgetch(window);

                if (input != -1) {
                    std::string key = std::to_string(input);

                    event = config.getString(
                            ("keycode.f." +  key).c_str(),
                            "unbound"
                            );

                    break;
                }
            }

            input = 27;
        }

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

            break;
    }

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

    if (!strcmp("quit", event.c_str())) {
        kill();
    } else if (!strcmp("tab_next", event.c_str())) {
        switchTab(++tab_index);
    } else if (!strcmp("tab_prev", event.c_str())) {
        switchTab(--tab_index);
    } else if (!strcmp("tab_playback", event.c_str())) {
        switchTab(TAB_PLAYBACK);
    } else if (!strcmp("tab_recording", event.c_str())) {
        switchTab(TAB_RECORDING);
    } else if (!strcmp("tab_output", event.c_str())) {
        switchTab(TAB_OUTPUT);
    } else if (!strcmp("tab_input", event.c_str())) {
        switchTab(TAB_INPUT);
    } else if (!strcmp("tab_config", event.c_str())) {
        switchTab(TAB_CONFIGURATION);
    } else if (!strcmp("help", event.c_str())) {
        show_help();
    } else {
        current_tab->handleEvents(event.c_str());
    }
}

// Assembly:
subq $0x278, %rsp            # imm = 0x278
movq %rdi, 0x270(%rsp)
movq 0x270(%rsp), %rax
movq %rax, 0x60(%rsp)
xorl %edi, %edi
callq 0x7750
xorl %edi, %edi
callq 0x7ad0
movl $0x210002, %edi         # imm = 0x210002
xorl %eax, %eax
movl %eax, %esi
callq 0x74a0
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x7260
movl %eax, 0x26c(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x7950
cmpl $-0x1, 0x26c(%rsp)
jne 0x15de4
movl $0x1, 0x244(%rsp)
jmp 0x16888
movl 0x26c(%rsp), %eax
movl %eax, 0x5c(%rsp)
subl $0x1b, %eax
je 0x1614e
jmp 0x15dfa
movl 0x5c(%rsp), %eax
subl $0x199, %eax            # imm = 0x199
je 0x15e1b
jmp 0x15e07
movl 0x5c(%rsp), %eax
subl $0x19a, %eax            # imm = 0x19A
je 0x16084
jmp 0x164bb
leaq 0x230(%rsp), %rdi
callq 0x7b50
movl %eax, 0x58(%rsp)
jmp 0x15e2e
movl 0x58(%rsp), %eax
movl %eax, 0x22c(%rsp)
cmpl $0x0, 0x22c(%rsp)
je 0x15e6c
movl $0x1, 0x244(%rsp)
jmp 0x16888
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1689d
movq 0x60(%rsp), %rcx
movl 0x238(%rsp), %eax
movl 0xc(%rcx), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jne 0x15fdf
movl 0x240(%rsp), %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0x15f5c
movl $0x0, 0x218(%rsp)
movl $0x0, 0x214(%rsp)
cmpl $0x5, 0x214(%rsp)
jge 0x15f5a
movq 0x60(%rsp), %rax
movslq 0x214(%rsp), %rcx
movq 0x108(%rax,%rcx,8), %rdi
callq 0x7310
movl %eax, 0x210(%rsp)
movl 0x234(%rsp), %eax
cmpl 0x218(%rsp), %eax
jl 0x15f2c
movl 0x234(%rsp), %eax
movl 0x218(%rsp), %ecx
addl 0x210(%rsp), %ecx
cmpl %ecx, %eax
jge 0x15f2c
movq 0x60(%rsp), %rdi
movl 0x214(%rsp), %esi
callq 0x15a10
jmp 0x15f1c
movl $0x1, 0x244(%rsp)
jmp 0x16888
movl 0x210(%rsp), %eax
addl $0x1, %eax
addl 0x218(%rsp), %eax
movl %eax, 0x218(%rsp)
movl 0x214(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x214(%rsp)
jmp 0x15eb1
jmp 0x15fda
movl 0x240(%rsp), %eax
andq $0x10000, %rax          # imm = 0x10000
cmpq $0x0, %rax
je 0x15f99
movq 0x60(%rsp), %rdi
movl 0x130(%rdi), %esi
incl %esi
movl %esi, 0x130(%rdi)
callq 0x15a10
jmp 0x15f89
movl $0x1, 0x244(%rsp)
jmp 0x16888
movl 0x240(%rsp), %eax
andq $0x200000, %rax         # imm = 0x200000
cmpq $0x0, %rax
je 0x15fd6
movq 0x60(%rsp), %rdi
movl 0x130(%rdi), %esi
decl %esi
movl %esi, 0x130(%rdi)
callq 0x15a10
jmp 0x15fc6
movl $0x1, 0x244(%rsp)
jmp 0x16888
jmp 0x15fd8
jmp 0x15fda
jmp 0x16074
movl $0x0, 0x20c(%rsp)
movl 0x240(%rsp), %eax
andq $0x2, %rax
cmpq $0x0, %rax
je 0x16008
movl $0x1, 0x20c(%rsp)
jmp 0x1604a
movl 0x240(%rsp), %eax
andq $0x10000, %rax          # imm = 0x10000
cmpq $0x0, %rax
je 0x16028
movl $0x4, 0x20c(%rsp)
jmp 0x16048
movl 0x240(%rsp), %eax
andq $0x200000, %rax         # imm = 0x200000
cmpq $0x0, %rax
je 0x16046
movl $0x5, 0x20c(%rsp)
jmp 0x16048
jmp 0x1604a
movq 0x60(%rsp), %rax
movq 0x138(%rax), %rdi
movl 0x234(%rsp), %esi
movl 0x238(%rsp), %edx
movl 0x20c(%rsp), %ecx
callq 0x19420
jmp 0x16072
jmp 0x16074
movl $0x1, 0x244(%rsp)
jmp 0x16888
xorl %eax, %eax
movq 0x14f0b(%rip), %rcx     # 0x2af98
cmpq (%rcx), %rax
je 0x160a9
movq 0x14eff(%rip), %rax     # 0x2af98
movq (%rax), %rax
movswl 0x4(%rax), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x160b4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x54(%rsp)
jmp 0x160b4
movq 0x60(%rsp), %rax
movl 0x54(%rsp), %ecx
movl %ecx, 0xc(%rax)
xorl %eax, %eax
movq 0x14ecf(%rip), %rcx     # 0x2af98
cmpq (%rcx), %rax
je 0x160e5
movq 0x14ec3(%rip), %rax     # 0x2af98
movq (%rax), %rax
movswl 0x6(%rax), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x160f0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x50(%rsp)
jmp 0x160f0
movq 0x60(%rsp), %rax
movl 0x50(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq 0x10(%rax), %rdi
movl 0x8(%rax), %edx
movl 0xc(%rax), %esi
decl %esi
callq 0x7440
jmp 0x1610f
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0xc(%rax), %esi
decl %esi
xorl %edx, %edx
callq 0x72f0
jmp 0x16126
movq 0x60(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x8(%rax), %edx
movl $0x1, %esi
callq 0x7440
jmp 0x1613e
movl $0x1, 0x244(%rsp)
jmp 0x16888
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x7260
movl %eax, 0x4c(%rsp)
jmp 0x16162
movl 0x4c(%rsp), %eax
movl %eax, 0x26c(%rsp)
cmpl $-0x1, 0x26c(%rsp)
je 0x162fe
cmpl $0x4f, 0x26c(%rsp)
je 0x162fe
movl 0x26c(%rsp), %esi
leaq 0x1e8(%rsp), %rdi
callq 0x10a50
jmp 0x1619f
leaq 0xb73d(%rip), %rsi      # 0x218e3
leaq 0x1a8(%rsp), %rdi
leaq 0x1e8(%rsp), %rdx
callq 0x170b0
jmp 0x161bd
leaq 0x1a8(%rsp), %rdi
callq 0x72e0
movq %rax, 0x38(%rsp)
leaq 0x187(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x7c50
movq 0x40(%rsp), %rdx
leaq 0xb703(%rip), %rsi      # 0x218f0
leaq 0x188(%rsp), %rdi
callq 0x10940
jmp 0x161fc
movq 0x38(%rsp), %rdx
leaq 0x15698(%rip), %rsi     # 0x2b8a0
leaq 0x1c8(%rsp), %rdi
leaq 0x188(%rsp), %rcx
callq 0x10150
jmp 0x1621f
leaq 0x248(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x7860
leaq 0x1c8(%rsp), %rdi
callq 0x7480
leaq 0x188(%rsp), %rdi
callq 0x7480
leaq 0x187(%rsp), %rdi
callq 0x7760
leaq 0x1a8(%rsp), %rdi
callq 0x7480
movl $0x2, 0x244(%rsp)
leaq 0x1e8(%rsp), %rdi
callq 0x7480
jmp 0x16618
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x162ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x162d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x7480
leaq 0x187(%rsp), %rdi
callq 0x7760
leaq 0x1a8(%rsp), %rdi
callq 0x7480
leaq 0x1e8(%rsp), %rdi
callq 0x7480
jmp 0x1689d
cmpl $0x4f, 0x26c(%rsp)
jne 0x164b0
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
callq 0x7260
movl %eax, 0x34(%rsp)
jmp 0x16320
movl 0x34(%rsp), %eax
movl %eax, 0x26c(%rsp)
cmpl $-0x1, 0x26c(%rsp)
je 0x164ae
movl 0x26c(%rsp), %esi
leaq 0x160(%rsp), %rdi
callq 0x10a50
jmp 0x1634f
leaq 0xb5a2(%rip), %rsi      # 0x218f8
leaq 0x120(%rsp), %rdi
leaq 0x160(%rsp), %rdx
callq 0x170b0
jmp 0x1636d
leaq 0x120(%rsp), %rdi
callq 0x72e0
movq %rax, 0x20(%rsp)
leaq 0xff(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x7c50
movq 0x28(%rsp), %rdx
leaq 0xb553(%rip), %rsi      # 0x218f0
leaq 0x100(%rsp), %rdi
callq 0x10940
jmp 0x163ac
movq 0x20(%rsp), %rdx
leaq 0x154e8(%rip), %rsi     # 0x2b8a0
leaq 0x140(%rsp), %rdi
leaq 0x100(%rsp), %rcx
callq 0x10150
jmp 0x163cf
leaq 0x248(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x7860
leaq 0x140(%rsp), %rdi
callq 0x7480
leaq 0x100(%rsp), %rdi
callq 0x7480
leaq 0xff(%rsp), %rdi
callq 0x7760
leaq 0x120(%rsp), %rdi
callq 0x7480
movl $0x2, 0x244(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x7480
jmp 0x16618
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x1649c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x16482
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x7480
leaq 0xff(%rsp), %rdi
callq 0x7760
leaq 0x120(%rsp), %rdi
callq 0x7480
leaq 0x160(%rsp), %rdi
callq 0x7480
jmp 0x1689d
jmp 0x164b0
movl $0x1b, 0x26c(%rsp)
movl 0x26c(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0x10a50
jmp 0x164d1
leaq 0xb42b(%rip), %rsi      # 0x21903
leaq 0x98(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
callq 0x170b0
jmp 0x164ef
leaq 0x98(%rsp), %rdi
callq 0x72e0
movq %rax, 0x10(%rsp)
leaq 0x77(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x7c50
movq 0x18(%rsp), %rdx
leaq 0xb3d4(%rip), %rsi      # 0x218f0
leaq 0x78(%rsp), %rdi
callq 0x10940
jmp 0x16528
movq 0x10(%rsp), %rdx
leaq 0x1536c(%rip), %rsi     # 0x2b8a0
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rcx
callq 0x10150
jmp 0x16548
leaq 0x248(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x7860
leaq 0xb8(%rsp), %rdi
callq 0x7480
leaq 0x78(%rsp), %rdi
callq 0x7480
leaq 0x77(%rsp), %rdi
callq 0x7760
leaq 0x98(%rsp), %rdi
callq 0x7480
movl $0x2, 0x244(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x7480
jmp 0x16618
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x16606
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x165ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x7480
leaq 0x77(%rsp), %rdi
callq 0x7760
leaq 0x98(%rsp), %rdi
callq 0x7480
leaq 0xd8(%rsp), %rdi
callq 0x7480
jmp 0x1689d
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb2c1(%rip), %rdi      # 0x218f0
callq 0x7980
cmpl $0x0, %eax
jne 0x16649
movl $0x1, 0x244(%rsp)
jmp 0x16888
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb2ac(%rip), %rdi      # 0x2190c
callq 0x7980
cmpl $0x0, %eax
jne 0x1667b
movq 0x60(%rsp), %rdi
callq 0x168c0
jmp 0x16676
jmp 0x1687d
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb27f(%rip), %rdi      # 0x21911
callq 0x7980
cmpl $0x0, %eax
jne 0x166bb
movq 0x60(%rsp), %rdi
movl 0x130(%rdi), %esi
incl %esi
movl %esi, 0x130(%rdi)
callq 0x15a10
jmp 0x166b6
jmp 0x1687b
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb248(%rip), %rdi      # 0x2191a
callq 0x7980
cmpl $0x0, %eax
jne 0x166fb
movq 0x60(%rsp), %rdi
movl 0x130(%rdi), %esi
decl %esi
movl %esi, 0x130(%rdi)
callq 0x15a10
jmp 0x166f6
jmp 0x16879
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb211(%rip), %rdi      # 0x21923
callq 0x7980
cmpl $0x0, %eax
jne 0x1672f
movq 0x60(%rsp), %rdi
xorl %esi, %esi
callq 0x15a10
jmp 0x1672a
jmp 0x16877
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb1ea(%rip), %rdi      # 0x21930
callq 0x7980
cmpl $0x0, %eax
jne 0x16766
movq 0x60(%rsp), %rdi
movl $0x1, %esi
callq 0x15a10
jmp 0x16761
jmp 0x16875
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb1c1(%rip), %rdi      # 0x2193e
callq 0x7980
cmpl $0x0, %eax
jne 0x1679d
movq 0x60(%rsp), %rdi
movl $0x2, %esi
callq 0x15a10
jmp 0x16798
jmp 0x16873
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb195(%rip), %rdi      # 0x21949
callq 0x7980
cmpl $0x0, %eax
jne 0x167d4
movq 0x60(%rsp), %rdi
movl $0x3, %esi
callq 0x15a10
jmp 0x167cf
jmp 0x16871
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0xb168(%rip), %rdi      # 0x21953
callq 0x7980
cmpl $0x0, %eax
jne 0x16808
movq 0x60(%rsp), %rdi
movl $0x4, %esi
callq 0x15a10
jmp 0x16806
jmp 0x1686f
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq %rax, %rsi
leaq 0x9992(%rip), %rdi      # 0x201b1
callq 0x7980
cmpl $0x0, %eax
jne 0x16837
movq 0x60(%rsp), %rdi
callq 0x168f0
jmp 0x16835
jmp 0x1686d
movq 0x60(%rsp), %rax
movq 0x138(%rax), %rax
movq %rax, 0x8(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x72e0
movq 0x8(%rsp), %rdi
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rsi
callq 0x18720
jmp 0x1686b
jmp 0x1686d
jmp 0x1686f
jmp 0x16871
jmp 0x16873
jmp 0x16875
jmp 0x16877
jmp 0x16879
jmp 0x1687b
jmp 0x1687d
movl $0x0, 0x244(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x7480
addq $0x278, %rsp            # imm = 0x278
retq
leaq 0x248(%rsp), %rdi
callq 0x7480
movq 0x220(%rsp), %rdi
callq 0x7c40
nopw (%rax,%rax)