// File: /workspace/github_c_makefile_star2_O1/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 0x276eb(%rip), %rbx     # 0x4f2d8
movl $0x0, 0x44(%rbx)
leaq -0x60(%rbp), %rdi
callq 0x24e0
testl %eax, %eax
je 0x28178
leaq 0x1db7d(%rip), %r12     # 0x45789
movl -0x60(%rbp), %eax
xorl %ecx, %ecx
cmpl $0x400, %eax            # imm = 0x400
jle 0x27c44
cmpl $0x64f, %eax            # imm = 0x64F
jle 0x27c61
leal -0x650(%rax), %ecx
cmpl $0x3, %ecx
jb 0x27c40
cmpl $0x653, %eax            # imm = 0x653
je 0x27f08
cmpl $0x654, %eax            # imm = 0x654
jne 0x28167
movb $0x1, %cl
jmp 0x27c70
cmpl $0x2ff, %eax            # imm = 0x2FF
jle 0x27e29
leal -0x300(%rax), %edx
cmpl $0x3, %edx
ja 0x27ca6
cmpl $0x2, %edx
jne 0x27c70
jmp 0x27ca6
leal -0x401(%rax), %edx
cmpl $0x3, %edx
jae 0x28167
movb %cl, 0x94(%rbx)
cmpl $0x400, %eax            # imm = 0x400
jg 0x27d9b
cmpl $0x2ff, %eax            # imm = 0x2FF
jle 0x27e29
leal -0x300(%rax), %ecx
cmpl $0x2, %ecx
jb 0x27ec5
cmpl $0x303, %eax            # imm = 0x303
je 0x27f5a
cmpl $0x400, %eax            # imm = 0x400
jne 0x28167
movl -0x4c(%rbp), %eax
movl -0x48(%rbp), %edx
movl %eax, 0x74(%rbx)
movl %edx, 0x78(%rbx)
movl -0x44(%rbp), %ecx
addl %ecx, 0x7c(%rbx)
movl -0x40(%rbp), %ecx
addl %ecx, 0x80(%rbx)
testb $0x1, 0x68(%rbx)
je 0x28167
cmpl $0x2, %eax
jg 0x27cfe
leaq 0x4354e(%rip), %rax     # 0x6b230
movq (%rax), %rdi
movss 0x435a3(%rip), %xmm0    # 0x6b290
addss 0x857f(%rip), %xmm0     # 0x30274
cvttss2si %xmm0, %esi
callq 0x20f0
cmpl $0x2, -0x48(%rbp)
jg 0x27d2a
leaq 0x43525(%rip), %rax     # 0x6b230
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
movss 0x4357b(%rip), %xmm0    # 0x6b294
addss 0x8553(%rip), %xmm0     # 0x30274
cvttss2si %xmm0, %edx
callq 0x20f0
xorps %xmm0, %xmm0
cvtsi2ssl -0x4c(%rbp), %xmm0
movss 0x43556(%rip), %xmm1    # 0x6b290
addss 0x84d6(%rip), %xmm1     # 0x30218
ucomiss %xmm1, %xmm0
jb 0x27d5e
leaq 0x434e2(%rip), %rax     # 0x6b230
movq (%rax), %rdi
movl -0x48(%rbp), %edx
movl $0x4, %esi
callq 0x20f0
xorps %xmm0, %xmm0
cvtsi2ssl -0x48(%rbp), %xmm0
movss 0x43526(%rip), %xmm1    # 0x6b294
addss 0x84a2(%rip), %xmm1     # 0x30218
ucomiss %xmm1, %xmm0
jb 0x28167
leaq 0x434aa(%rip), %rax     # 0x6b230
movq (%rax), %rdi
movl -0x4c(%rbp), %esi
movl $0x4, %edx
callq 0x20f0
jmp 0x28167
leal -0x650(%rax), %ecx
cmpl $0x4, %ecx
ja 0x27e82
leaq 0x8707(%rip), %rdx      # 0x304b8
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movl -0x58(%rbp), %eax
leaq 0x4358c(%rip), %rcx     # 0x6b350
xorl %r15d, %r15d
cmpl %eax, (%rcx)
je 0x27dde
incq %r15
addq $0x10, %rcx
cmpq $0x4, %r15
jne 0x27dc7
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movzbl -0x54(%rbp), %r13d
xorl %ecx, %ecx
cmpl $0x4, %r15d
setl %cl
leaq 0x9f93(%rip), %rdi      # 0x31d86
movl $0x26, %esi
leaq 0x9fa9(%rip), %rdx      # 0x31da8
callq 0x722f
cmpb $0x1, -0x53(%rbp)
movss 0x81f4(%rip), %xmm0     # 0x30004
je 0x27e15
xorps %xmm0, %xmm0
shll $0x5, %r13d
shll $0x3, %r15d
orl %r13d, %r15d
orl $0x3, %r15d
jmp 0x28070
cmpl $0x100, %eax            # imm = 0x100
je 0x27e76
cmpl $0x200, %eax            # imm = 0x200
jne 0x28167
movb -0x54(%rbp), %al
cmpb $0xe, %al
jne 0x27e49
movb $0x1, 0x434bf(%rip)     # 0x6b308
cmpb $0x6, %al
sete %cl
leaq 0x28abb(%rip), %rax     # 0x50910
movq (%rax), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x28167
movl -0x50(%rbp), %edi
movl -0x4c(%rbp), %esi
callq *%rax
jmp 0x28167
movb $0x1, 0x4348b(%rip)     # 0x6b308
jmp 0x28167
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jae 0x27f68
xorl %ecx, %ecx
cmpl $0x401, %eax            # imm = 0x401
sete %cl
orl $0x2, %ecx
movzbl -0x50(%rbp), %eax
cmpl $0x3, %eax
je 0x28116
cmpl $0x2, %eax
je 0x28111
cmpl $0x1, %eax
jne 0x28167
movl %ecx, 0x68(%rbx)
jmp 0x28167
cmpb $0x0, 0x40(%rbx)
je 0x27edd
cmpl $0x8, -0x4c(%rbp)
jne 0x27edd
leaq 0x1d8af(%rip), %rdi     # 0x45787
callq 0xb445
cmpb $0x0, -0x53(%rbp)
jne 0x28167
cmpl $0x300, -0x60(%rbp)     # imm = 0x300
movss 0x810e(%rip), %xmm0     # 0x30004
je 0x27efb
xorps %xmm0, %xmm0
movl -0x4c(%rbp), %edi
shll $0x5, %edi
incl %edi
jmp 0x28073
movl -0x58(%rbp), %r15d
movl %r15d, %edi
callq 0x2030
cmpb $0x0, (%rax)
je 0x28084
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
cmpb (%rcx,%r12), %dl
jne 0x27f32
incq %rcx
cmpl $0x3, %ecx
jne 0x27f1f
jmp 0x27f41
cmpb $0x0, 0x1(%rax)
leaq 0x1(%rax), %rax
jne 0x27f1d
jmp 0x28084
cmpb $0x1, 0x433f8(%rip)     # 0x6b340
jne 0x2807d
movb $0x0, 0x433eb(%rip)     # 0x6b340
jmp 0x28167
leaq -0x54(%rbp), %rdi
callq 0xb445
jmp 0x28167
cmpl $0x403, %eax            # imm = 0x403
jne 0x28167
movq -0x50(%rbp), %rax
movq %rax, 0x84(%rbx)
jmp 0x28167
movl -0x58(%rbp), %eax
xorl %ecx, %ecx
leaq 0x433c9(%rip), %r15     # 0x6b358
xorl %r13d, %r13d
movq (%r15), %rdi
testq %rdi, %rdi
je 0x27fa4
cmpl %eax, -0x8(%r15)
je 0x2811b
cmpq $0x3, %rcx
leaq 0x1(%rcx), %rdx
setae %r13b
addq $0x10, %r15
movq %rdx, %rcx
cmpq $0x4, %rdx
jne 0x27f92
jmp 0x28135
movl -0x58(%rbp), %eax
leaq 0x43384(%rip), %rcx     # 0x6b350
xorl %r15d, %r15d
cmpl %eax, (%rcx)
je 0x27fe6
incq %r15
addq $0x10, %rcx
cmpq $0x4, %r15
jne 0x27fcf
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movzbl -0x54(%rbp), %r13d
xorl %ecx, %ecx
cmpl $0x4, %r15d
setl %cl
leaq 0x9d8b(%rip), %rdi      # 0x31d86
movl $0x20, %esi
leaq 0x9da1(%rip), %rdx      # 0x31da8
callq 0x722f
movswl -0x50(%rbp), %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movzwl %ax, %eax
divsd 0x8a86(%rip), %xmm0     # 0x30aa8
movss 0x81fe(%rip), %xmm1     # 0x30228
cmpl $0x8000, %eax           # imm = 0x8000
je 0x28038
xorps %xmm1, %xmm1
cvtsd2ss %xmm0, %xmm1
movaps %xmm1, %xmm0
movaps 0x7ffe(%rip), %xmm2     # 0x30040
xorps %xmm2, %xmm0
maxss %xmm1, %xmm0
cmpltss 0x81da(%rip), %xmm0     # 0x3022c
andnps %xmm1, %xmm0
movl %r13d, %eax
andb $-0x3, %al
cmpb $0x1, %al
jne 0x28061
xorps %xmm2, %xmm0
shll $0x5, %r13d
shll $0x3, %r15d
orl %r13d, %r15d
orl $0x2, %r15d
movl %r15d, %edi
callq 0xb2fc
jmp 0x28167
movb $0x1, 0x432bc(%rip)     # 0x6b340
cmpq $0x0, 0x432cc(%rip)     # 0x6b358
je 0x280f9
movq $-0x1, %r13
leaq 0x432b4(%rip), %rax     # 0x6b350
cmpq $0x2, %r13
je 0x280cc
incq %r13
leaq 0x10(%rax), %r14
cmpq $0x0, 0x18(%rax)
movq %r14, %rax
jne 0x2809c
movl %r15d, %edi
callq 0x2370
movl %r15d, (%r14)
movq %rax, 0x8(%r14)
cmpq $0x3, %r13
jb 0x28167
movq 0x26efd(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x8bd2(%rip), %rdi      # 0x30caf
leaq 0x9ca2(%rip), %rdx      # 0x31d86
movl $0x3f, %ecx
leaq 0x1d6d4(%rip), %r8      # 0x457c4
leaq 0x1d696(%rip), %r9      # 0x4578d
jmp 0x28160
movl %r15d, %edi
callq 0x2370
movl %r15d, 0x43248(%rip)    # 0x6b350
movq %rax, 0x43249(%rip)     # 0x6b358
jmp 0x28167
movl %ecx, 0x6c(%rbx)
jmp 0x28167
movl %ecx, 0x70(%rbx)
jmp 0x28167
callq 0x2200
movl $0x0, -0x8(%r15)
movq $0x0, (%r15)
testb $0x1, %r13b
je 0x28167
movq 0x26e94(%rip), %rax     # 0x4efd0
movq (%rax), %rsi
leaq 0x8b69(%rip), %rdi      # 0x30caf
leaq 0x9c39(%rip), %rdx      # 0x31d86
movl $0x53, %ecx
leaq 0x1d669(%rip), %r8      # 0x457c2
leaq 0x1d678(%rip), %r9      # 0x457d8
xorl %eax, %eax
callq 0x711f
leaq -0x60(%rbp), %rdi
callq 0x24e0
testl %eax, %eax
jne 0x27c0c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq