// File: /workspace/github_c_makefile_star2_O3/FredTheDino[P]Fog/src/engine/platform/input_sdl.cpp
// Function: (anonymous namespace)::poll_events()
void poll_events() {
    SDL_Event event;

    // Reset the text input
    Input::global_mapping.text_length = 0;

    while (SDL_PollEvent(&event)) {
        switch (event.type) {
            case (SDL_KEYDOWN):
            case (SDL_KEYUP):
            case (SDL_TEXTINPUT):
            case (SDL_MOUSEWHEEL):
            case (SDL_MOUSEBUTTONDOWN):
            case (SDL_MOUSEBUTTONUP):
                Input::global_mapping.using_controller = false;
                break;
            case (SDL_CONTROLLERAXISMOTION):
            case (SDL_CONTROLLERBUTTONDOWN):
            case (SDL_CONTROLLERBUTTONUP):
            case (SDL_CONTROLLERDEVICEREMOVED):
                Input::global_mapping.using_controller = true;
                break;
            default:
                break;
        }

        switch (event.type) {
            case (SDL_WINDOWEVENT):
                if (event.window.event == SDL_WINDOWEVENT_CLOSE)
                    running = false;
                if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
                    if (window_callback) {
                        window_callback(event.window.data1,
                                        event.window.data2);
                    }
                }
                break;
            case (SDL_QUIT):
                running = false;
                break;
            case (SDL_KEYDOWN):
            case (SDL_KEYUP): {
                if (Input::global_mapping.text_input) {
                    // Special control sequences for text manipulatin.
                    if (event.key.keysym.sym == SDLK_BACKSPACE) {
                        Input::type_text("\b");
                    }
                }
                if (event.key.repeat) break;
                f32 value = event.key.type == SDL_KEYDOWN ? 1.0 : 0.0;
                Input::InputCode code = key_to_input_code(event.key.keysym.sym);
                Input::activate(code, value);
            } break;
            case (SDL_TEXTINPUT): {
                Input::type_text(event.text.text);
            } break;
            case (SDL_MOUSEWHEEL):
                Input::global_mapping.mouse.wheel_x = event.wheel.x;
                Input::global_mapping.mouse.wheel_y = event.wheel.y;
                break;
            case (SDL_MOUSEBUTTONDOWN):
            case (SDL_MOUSEBUTTONUP): {
                Input::ButtonState state =
                    event.type == SDL_MOUSEBUTTONDOWN ?
                    Input::ButtonState::PRESSED : Input::ButtonState::RELEASED;
                if (event.button.button == SDL_BUTTON_LEFT)
                    Input::global_mapping.mouse.state[0] = state;
                else if (event.button.button == SDL_BUTTON_MIDDLE)
                    Input::global_mapping.mouse.state[1] = state;
                else if (event.button.button == SDL_BUTTON_RIGHT)
                    Input::global_mapping.mouse.state[2] = state;
            } break;
            case (SDL_CONTROLLERAXISMOTION): {
                s32 which = event.caxis.which;
                u32 id = get_contoller_id(which);
                u8 axis = event.caxis.axis;
                Input::InputCode code = axis_to_input_code(axis, id);
                f32 value = s16_to_float(event.caxis.value);
                // TODO(ed): If flipping the sticks should be added,
                // it's here it's done.
                if (axis == SDL_CONTROLLER_AXIS_LEFTY ||
                    axis == SDL_CONTROLLER_AXIS_RIGHTY) {
                    value = -value;
                }
                Input::activate(code, value);
            } break;
            case (SDL_CONTROLLERBUTTONDOWN):
            case (SDL_CONTROLLERBUTTONUP): {
                s32 which = event.cbutton.which;
                u32 id = get_contoller_id(which);
                u8 button = event.cbutton.button;
                Input::InputCode code = button_to_input_code(button, id);
                f32 value = event.cbutton.state == SDL_PRESSED ? 1.0 : 0.0;
                Input::activate(code, value);
            } break;
            case (SDL_CONTROLLERDEVICEADDED): {
                s32 which = event.cdevice.which;
                const char *name = SDL_GameControllerNameForIndex(which);
                if (Util::contains_substr(name, "PS4")) {
                    // NOTE(ed): I know this looks wierd... But
                    // for some reason PS4 controllers are 2 devices
                    // on Linux, this filters out the second one
                    // which looks to be useless (I think it's the
                    // motion controlls).
                    static b8 skipp = false;
                    if (skipp) {
                        skipp = false;
                        break;
                    }
                    skipp = true;
                }
                register_controller(which);
            } break;
            case (SDL_CONTROLLERDEVICEREMOVED): {
                s32 which = event.cdevice.which;
                unregister_controller(which);
            } break;
            case (SDL_MOUSEMOTION):
                Input::global_mapping.mouse.x = event.motion.x;
                Input::global_mapping.mouse.y = event.motion.y;
                Input::global_mapping.mouse.move_x += event.motion.xrel;
                Input::global_mapping.mouse.move_y += event.motion.yrel;
#if MOUSE_WARP_IF_CLICKED
                if (Input::mouse_down(0)) {
                    const int SAFE_BORDER = 2;
                    const int WARP_BORDER = 4;
                    if (event.motion.x <= SAFE_BORDER)
                        SDL_WarpMouseInWindow(Renderer::Impl::window,
                                              Renderer::get_window_width() - WARP_BORDER,
                                              event.motion.y);
                    if (event.motion.y <= SAFE_BORDER)
                        SDL_WarpMouseInWindow(Renderer::Impl::window,
                                              event.motion.x,
                                              Renderer::get_window_height() - WARP_BORDER);
                    if (event.motion.x >= Renderer::get_window_width() - SAFE_BORDER)
                        SDL_WarpMouseInWindow(Renderer::Impl::window,
                                              WARP_BORDER,
                                              event.motion.y);
                    if (event.motion.y >= Renderer::get_window_height() - SAFE_BORDER)
                        SDL_WarpMouseInWindow(Renderer::Impl::window,
                                              event.motion.x,
                                              WARP_BORDER);
                }
#endif
                break;
            default:
                break;
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x27a9a(%rip), %r15     # 0x4f2d8
movl $0x0, 0x44(%r15)
leaq -0x60(%rbp), %rdi
callq 0x24d0
testl %eax, %eax
je 0x27d7b
leaq -0x60(%rbp), %r14
leaq 0x43ade(%rip), %r12     # 0x6b340
leaq 0x1df10(%rip), %r13     # 0x45779
movl -0x60(%rbp), %eax
xorl %ecx, %ecx
cmpl $0x400, %eax            # imm = 0x400
jle 0x278a1
cmpl $0x64f, %eax            # imm = 0x64F
jle 0x278be
leal -0x650(%rax), %ecx
cmpl $0x3, %ecx
jb 0x2789d
cmpl $0x653, %eax            # imm = 0x653
je 0x27b34
cmpl $0x654, %eax            # imm = 0x654
jne 0x27d45
movb $0x1, %cl
jmp 0x278cd
cmpl $0x2ff, %eax            # imm = 0x2FF
jle 0x27a7e
leal -0x300(%rax), %edx
cmpl $0x3, %edx
ja 0x278f9
cmpl $0x2, %edx
jne 0x278cd
jmp 0x278f9
leal -0x401(%rax), %edx
cmpl $0x3, %edx
jae 0x27d45
movb %cl, 0x94(%r15)
cmpl $0x400, %eax            # imm = 0x400
jg 0x279f9
leal -0x300(%rax), %ecx
cmpl $0x2, %ecx
jb 0x27aa7
cmpl $0x303, %eax            # imm = 0x303
je 0x27b6e
cmpl $0x400, %eax            # imm = 0x400
jne 0x27d45
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
movl %esi, 0x74(%r15)
movl %edx, 0x78(%r15)
movl -0x44(%rbp), %eax
addl %eax, 0x7c(%r15)
movl -0x40(%rbp), %eax
addl %eax, 0x80(%r15)
testb $0x1, 0x68(%r15)
je 0x27d45
cmpl $0x2, %esi
jg 0x2795c
leaq 0x438e6(%rip), %rax     # 0x6b220
movq (%rax), %rdi
movss 0x4393b(%rip), %xmm0    # 0x6b280
addss 0x8927(%rip), %xmm0     # 0x30274
cvttss2si %xmm0, %esi
callq 0x20f0
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
cmpl $0x2, %edx
jg 0x2798a
leaq 0x438b8(%rip), %rax     # 0x6b220
movq (%rax), %rdi
movss 0x43911(%rip), %xmm0    # 0x6b284
addss 0x88f9(%rip), %xmm0     # 0x30274
cvttss2si %xmm0, %edx
callq 0x20f0
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
movss 0x438e7(%rip), %xmm1    # 0x6b280
addss 0x8877(%rip), %xmm1     # 0x30218
ucomiss %xmm1, %xmm0
jb 0x279bd
leaq 0x43873(%rip), %rax     # 0x6b220
movq (%rax), %rdi
movl $0x4, %esi
callq 0x20f0
movl -0x48(%rbp), %edx
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
movss 0x438b8(%rip), %xmm1    # 0x6b284
addss 0x8844(%rip), %xmm1     # 0x30218
ucomiss %xmm1, %xmm0
jb 0x27d45
leaq 0x4383c(%rip), %rax     # 0x6b220
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
movl $0x4, %edx
callq 0x20f0
jmp 0x27d45
leal -0x650(%rax), %ecx
cmpl $0x4, %ecx
ja 0x27af0
leaq 0x8a99(%rip), %rdx      # 0x304a8
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl -0x58(%rbp), %eax
movq %r12, %rcx
xorl %ebx, %ebx
cmpl %eax, (%rcx)
je 0x27a36
incq %rbx
addq $0x10, %rcx
cmpq $0x4, %rbx
jne 0x27a20
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movzbl -0x54(%rbp), %r14d
xorl %ecx, %ecx
cmpl $0x4, %ebx
setl %cl
leaq 0xa32c(%rip), %rdi      # 0x31d76
movl $0x26, %esi
leaq 0xa342(%rip), %rdx      # 0x31d98
callq 0x729c
cmpb $0x1, -0x53(%rbp)
movss 0x859d(%rip), %xmm0     # 0x30004
je 0x27a6c
xorps %xmm0, %xmm0
shll $0x5, %r14d
shll $0x3, %ebx
orl %r14d, %ebx
orl $0x3, %ebx
jmp 0x27caf
cmpl $0x100, %eax            # imm = 0x100
je 0x27a9b
cmpl $0x200, %eax            # imm = 0x200
jne 0x27d45
movb -0x54(%rbp), %al
cmpb $0xe, %al
jne 0x27cbb
movb $0x1, 0x43856(%rip)     # 0x6b2f8
jmp 0x27d45
cmpb $0x0, 0x40(%r15)
je 0x27ac0
cmpl $0x8, -0x4c(%rbp)
jne 0x27ac0
leaq 0x1dcbc(%rip), %rdi     # 0x45777
callq 0xb191
cmpb $0x0, -0x53(%rbp)
jne 0x27d45
cmpl $0x300, -0x60(%rbp)     # imm = 0x300
movss 0x852b(%rip), %xmm0     # 0x30004
je 0x27ade
xorps %xmm0, %xmm0
movl -0x4c(%rbp), %edi
shll $0x5, %edi
incl %edi
callq 0xb04f
jmp 0x27d45
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jae 0x27b95
xorl %ecx, %ecx
cmpl $0x401, %eax            # imm = 0x401
sete %cl
orl $0x2, %ecx
movzbl -0x50(%rbp), %eax
cmpl $0x3, %eax
je 0x27d5d
cmpl $0x2, %eax
je 0x27d57
cmpl $0x1, %eax
jne 0x27d45
movl %ecx, 0x68(%r15)
jmp 0x27d45
movl -0x58(%rbp), %ebx
movl %ebx, %edi
callq 0x2030
cmpb $0x0, (%rax)
je 0x27ce8
movb $0x50, %dl
xorl %ecx, %ecx
cmpb %dl, (%rax,%rcx)
jne 0x27b5f
movb 0x1(%rcx,%r13), %dl
incq %rcx
cmpl $0x3, %ecx
jne 0x27b4b
jmp 0x27b7c
cmpb $0x0, 0x1(%rax)
leaq 0x1(%rax), %rax
jne 0x27b47
jmp 0x27ce8
leaq -0x54(%rbp), %rdi
callq 0xb191
jmp 0x27d45
cmpb $0x1, 0x437ad(%rip)     # 0x6b330
jne 0x27ce1
movb $0x0, 0x437a0(%rip)     # 0x6b330
jmp 0x27d45
cmpl $0x403, %eax            # imm = 0x403
jne 0x27d45
movq -0x50(%rbp), %rax
movq %rax, 0x84(%r15)
jmp 0x27d45
movl -0x58(%rbp), %eax
xorl %ebx, %ebx
movq 0x8(%rbx,%r12), %rdi
testq %rdi, %rdi
je 0x27bc9
cmpl %eax, (%rbx,%r12)
je 0x27d63
addq $0x10, %rbx
cmpq $0x40, %rbx
jne 0x27bb5
movq 0x273f6(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x90bb(%rip), %rdi      # 0x30c9f
leaq 0xa18b(%rip), %rdx      # 0x31d76
movl $0x53, %ecx
leaq 0x1dbbb(%rip), %r8      # 0x457b2
leaq 0x1dbca(%rip), %r9      # 0x457c8
xorl %eax, %eax
callq 0x718c
jmp 0x27d45
movl -0x58(%rbp), %eax
movq %r12, %rcx
xorl %ebx, %ebx
cmpl %eax, (%rcx)
je 0x27c28
incq %rbx
addq $0x10, %rcx
cmpq $0x4, %rbx
jne 0x27c12
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movzbl -0x54(%rbp), %r14d
xorl %ecx, %ecx
cmpl $0x4, %ebx
setl %cl
leaq 0xa13a(%rip), %rdi      # 0x31d76
movl $0x20, %esi
leaq 0xa150(%rip), %rdx      # 0x31d98
callq 0x729c
movswl -0x50(%rbp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movzwl %ax, %eax
divsd 0x8e35(%rip), %xmm0     # 0x30a98
movss 0x85bd(%rip), %xmm1     # 0x30228
cmpl $0x8000, %eax           # imm = 0x8000
je 0x27c79
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps %xmm1, %xmm0
movaps 0x83bd(%rip), %xmm2     # 0x30040
xorps %xmm2, %xmm0
maxss %xmm1, %xmm0
cmpltss 0x8599(%rip), %xmm0     # 0x3022c
andnps %xmm1, %xmm0
movl %r14d, %eax
andb $-0x3, %al
cmpb $0x1, %al
jne 0x27ca2
xorps %xmm2, %xmm0
shll $0x5, %r14d
shll $0x3, %ebx
orl %r14d, %ebx
orl $0x2, %ebx
movl %ebx, %edi
callq 0xb04f
jmp 0x27d41
cmpb $0x6, %al
sete %cl
leaq 0x28c39(%rip), %rax     # 0x50900
movq (%rax), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x27d45
movl -0x50(%rbp), %edi
movl -0x4c(%rbp), %esi
callq *%rax
jmp 0x27d45
movb $0x1, 0x43648(%rip)     # 0x6b330
xorl %r14d, %r14d
cmpq $0x0, 0x8(%r14,%r12)
je 0x27d31
addq $0x10, %r14
cmpq $0x40, %r14
jne 0x27ceb
movq 0x272cc(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x8f91(%rip), %rdi      # 0x30c9f
leaq 0xa061(%rip), %rdx      # 0x31d76
movl $0x3f, %ecx
leaq 0x1da93(%rip), %r8      # 0x457b4
leaq 0x1da55(%rip), %r9      # 0x4577d
xorl %eax, %eax
callq 0x718c
jmp 0x27d41
movl %ebx, %edi
callq 0x2370
movl %ebx, (%r14,%r12)
movq %rax, 0x8(%r14,%r12)
leaq -0x60(%rbp), %r14
movq %r14, %rdi
callq 0x24d0
testl %eax, %eax
jne 0x27869
jmp 0x27d7b
movl %ecx, 0x6c(%r15)
jmp 0x27d45
movl %ecx, 0x70(%r15)
jmp 0x27d45
callq 0x2200
movl $0x0, (%rbx,%r12)
movq $0x0, 0x8(%rbx,%r12)
jmp 0x27d45
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq