// File: /workspace/github_c_makefile_star2_O2/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 0x25914(%rip), %r12     # 0x4a2d8
andl $0x0, 0x44(%r12)
leaq -0x60(%rbp), %r14
leaq 0x6ab3(%rip), %r13      # 0x2b488
leaq 0x41964(%rip), %r15     # 0x66340
movq %r14, %rdi
callq 0x24d0
testl %eax, %eax
je 0x24e4a
movl -0x60(%rbp), %eax
xorl %edx, %edx
leal -0x300(%rax), %ecx
cmpl $0x3, %ecx
jbe 0x24a49
leal -0x401(%rax), %esi
cmpl $0x3, %esi
jb 0x24a4e
leal -0x650(%rax), %edx
cmpl $0x3, %edx
jb 0x24a45
cmpl $0x100, %eax            # imm = 0x100
je 0x24cc3
cmpl $0x200, %eax            # imm = 0x200
je 0x24c88
cmpl $0x400, %eax            # imm = 0x400
je 0x24ade
cmpl $0x653, %eax            # imm = 0x653
je 0x24bce
cmpl $0x654, %eax            # imm = 0x654
jne 0x249dc
movb $0x1, %dl
jmp 0x24a4e
cmpl $0x2, %ecx
je 0x249fc
movb %dl, 0x94(%r12)
leal -0x650(%rax), %edx
cmpl $0x4, %edx
jbe 0x24aa7
cmpl $0x2, %ecx
jb 0x24c08
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jb 0x24c52
cmpl $0x303, %eax            # imm = 0x303
je 0x24cf8
cmpl $0x400, %eax            # imm = 0x400
je 0x24ade
cmpl $0x403, %eax            # imm = 0x403
jne 0x249dc
movq -0x50(%rbp), %rax
movq %rax, 0x84(%r12)
jmp 0x249dc
movslq (%r13,%rdx,4), %rax
addq %r13, %rax
jmpq *%rax
movl -0x58(%rbp), %edi
callq 0x29111
movzbl -0x54(%rbp), %edi
movl %eax, %esi
callq 0x246ea
cmpb $0x1, -0x53(%rbp)
movss 0x6534(%rip), %xmm0     # 0x2b004
je 0x24d66
xorps %xmm0, %xmm0
jmp 0x24d66
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
movl %esi, 0x74(%r12)
movl %edx, 0x78(%r12)
movl -0x44(%rbp), %eax
addl %eax, 0x7c(%r12)
movl -0x40(%rbp), %eax
addl %eax, 0x80(%r12)
testb $0x1, 0x68(%r12)
je 0x249dc
cmpl $0x2, %esi
jg 0x24b3b
leaq 0x41717(%rip), %rax     # 0x66230
movq (%rax), %rdi
movss 0x41768(%rip), %xmm0    # 0x6628c
addss 0x6740(%rip), %xmm0     # 0x2b26c
cvttss2si %xmm0, %esi
callq 0x20f0
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
cmpl $0x2, %edx
jg 0x24b69
leaq 0x416e9(%rip), %rax     # 0x66230
movq (%rax), %rdi
movss 0x4173e(%rip), %xmm0    # 0x66290
addss 0x6712(%rip), %xmm0     # 0x2b26c
cvttss2si %xmm0, %edx
callq 0x20f0
movl -0x4c(%rbp), %esi
movl -0x48(%rbp), %edx
cvtsi2ss %esi, %xmm0
movss 0x41717(%rip), %xmm1    # 0x6628c
addss 0x669b(%rip), %xmm1     # 0x2b218
ucomiss %xmm1, %xmm0
jb 0x24b97
leaq 0x416a7(%rip), %rax     # 0x66230
movq (%rax), %rdi
pushq $0x4
popq %rsi
callq 0x20f0
movl -0x48(%rbp), %edx
cvtsi2ss %edx, %xmm0
movss 0x416ed(%rip), %xmm1    # 0x66290
addss 0x666d(%rip), %xmm1     # 0x2b218
ucomiss %xmm1, %xmm0
jb 0x249dc
leaq 0x41675(%rip), %rax     # 0x66230
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
pushq $0x4
popq %rdx
callq 0x20f0
jmp 0x249dc
movl -0x58(%rbp), %ebx
movl %ebx, %edi
callq 0x2030
movq %rax, %rdi
leaq 0x1bb6f(%rip), %rsi     # 0x40751
callq 0x98ab
testb %al, %al
je 0x24d79
cmpb $0x1, 0x41742(%rip)     # 0x66338
jne 0x24d72
movb $0x0, 0x41735(%rip)     # 0x66338
jmp 0x249dc
cmpb $0x0, 0x40(%r12)
je 0x24c22
cmpl $0x8, -0x4c(%rbp)
jne 0x24c22
leaq 0x1bb32(%rip), %rdi     # 0x4074f
callq 0x9de1
cmpb $0x0, -0x53(%rbp)
jne 0x249dc
cmpl $0x300, -0x60(%rbp)     # imm = 0x300
movss 0x63c9(%rip), %xmm0     # 0x2b004
je 0x24c40
xorps %xmm0, %xmm0
movl -0x4c(%rbp), %edi
shll $0x5, %edi
incl %edi
callq 0x9cf9
jmp 0x249dc
xorl %ecx, %ecx
cmpl $0x401, %eax            # imm = 0x401
sete %cl
orl $0x2, %ecx
movzbl -0x50(%rbp), %eax
cmpl $0x3, %eax
je 0x24e2b
cmpl $0x2, %eax
je 0x24e21
cmpl $0x1, %eax
jne 0x249dc
movl %ecx, 0x68(%r12)
jmp 0x249dc
movb -0x54(%rbp), %al
cmpb $0xe, %al
jne 0x24c96
movb $0x1, 0x4166a(%rip)     # 0x66300
cmpb $0x6, %al
sete %cl
leaq 0x26c6e(%rip), %rax     # 0x4b910
movq (%rax), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x249dc
movl -0x50(%rbp), %edi
movl -0x4c(%rbp), %esi
callq *%rax
jmp 0x249dc
movb $0x1, 0x41636(%rip)     # 0x66300
jmp 0x249dc
movl -0x58(%rbp), %eax
xorl %ebx, %ebx
cmpq $0x40, %rbx
je 0x24dec
movq 0x8(%rbx,%r15), %rdi
testq %rdi, %rdi
je 0x24cf2
cmpl %eax, (%rbx,%r15)
je 0x24e35
addq $0x10, %rbx
jmp 0x24cd4
leaq -0x54(%rbp), %rdi
callq 0x9de1
jmp 0x249dc
movl -0x58(%rbp), %edi
callq 0x29111
movzbl -0x54(%rbp), %ebx
movl %ebx, %edi
movl %eax, %esi
callq 0x246ac
movswl -0x50(%rbp), %ecx
cvtsi2sd %ecx, %xmm0
divsd 0x6d45(%rip), %xmm0     # 0x2ba70
movss 0x64f5(%rip), %xmm1     # 0x2b228
cmpw $0x8000, %cx            # imm = 0x8000
je 0x24d3e
cvtsd2ss %xmm0, %xmm1
movaps %xmm1, %xmm0
movaps 0x62f8(%rip), %xmm2     # 0x2b040
xorps %xmm2, %xmm0
maxss %xmm1, %xmm0
cmpltss 0x64d8(%rip), %xmm0     # 0x2b230
andnps %xmm1, %xmm0
andb $-0x3, %bl
cmpb $0x1, %bl
jne 0x24d66
xorps %xmm2, %xmm0
movl %eax, %edi
callq 0x9cf9
jmp 0x249dc
movb $0x1, 0x415bf(%rip)     # 0x66338
movq %r14, %r12
movq %r13, %r14
pushq $-0x10
popq %rax
leaq 0x10(%rax), %r13
cmpq $0x40, %r13
je 0x24daa
cmpq $0x0, 0x18(%rax,%r15)
movq %r13, %rax
jne 0x24d82
movl %ebx, %edi
callq 0x2370
movl %ebx, (%r13,%r15)
movq %rax, 0x8(%r13,%r15)
jmp 0x24dda
movq 0x2521f(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
leaq 0x6ebc(%rip), %rdi      # 0x2bc77
leaq 0x7f8c(%rip), %rdx      # 0x2cd4e
pushq $0x3f
popq %rcx
leaq 0x1b9c0(%rip), %r8      # 0x4078c
leaq 0x1b982(%rip), %r9      # 0x40755
xorl %eax, %eax
callq 0x683a
movq %r14, %r13
movq %r12, %r14
leaq 0x254f1(%rip), %r12     # 0x4a2d8
jmp 0x249dc
movq 0x251dd(%rip), %rax     # 0x49fd0
movq (%rax), %rsi
leaq 0x6e7a(%rip), %rdi      # 0x2bc77
leaq 0x7f4a(%rip), %rdx      # 0x2cd4e
pushq $0x53
popq %rcx
leaq 0x1b97c(%rip), %r8      # 0x4078a
leaq 0x1b98b(%rip), %r9      # 0x407a0
xorl %eax, %eax
callq 0x683a
jmp 0x249dc
movl %ecx, 0x6c(%r12)
jmp 0x249dc
movl %ecx, 0x70(%r12)
jmp 0x249dc
callq 0x2200
andl $0x0, (%rbx,%r15)
andq $0x0, 0x8(%rbx,%r15)
jmp 0x249dc
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq