// File: /workspace/github_c_makefile_star2_O0/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
subq $0xd0, %rsp
leaq 0x32fce(%rip), %rax     # 0x692e0
movl $0x0, 0x44(%rax)
leaq -0x38(%rbp), %rdi
callq 0x24d0
cmpl $0x0, %eax
je 0x368d0
movl -0x38(%rbp), %eax
movl %eax, -0x88(%rbp)
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
subl $0x2, %eax
jb 0x36382
jmp 0x36340
movl -0x88(%rbp), %eax
subl $0x303, %eax            # imm = 0x303
je 0x36382
jmp 0x3634f
movl -0x88(%rbp), %eax
addl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
subl $0x3, %eax
jb 0x36382
jmp 0x36361
movl -0x88(%rbp), %eax
addl $0xfffff9b0, %eax       # imm = 0xFFFFF9B0
subl $0x3, %eax
jb 0x36392
jmp 0x36373
movl -0x88(%rbp), %eax
subl $0x654, %eax            # imm = 0x654
je 0x36392
jmp 0x363a2
leaq 0x32f57(%rip), %rax     # 0x692e0
movb $0x0, 0x94(%rax)
jmp 0x363a4
leaq 0x32f47(%rip), %rax     # 0x692e0
movb $0x1, 0x94(%rax)
jmp 0x363a4
jmp 0x363a4
movl -0x38(%rbp), %eax
movl %eax, -0x8c(%rbp)
subl $0x100, %eax            # imm = 0x100
je 0x364c3
jmp 0x363ba
movl -0x8c(%rbp), %eax
subl $0x200, %eax            # imm = 0x200
je 0x36484
jmp 0x363cd
movl -0x8c(%rbp), %eax
addl $0xfffffd00, %eax       # imm = 0xFFFFFD00
subl $0x2, %eax
jb 0x364cf
jmp 0x363e3
movl -0x8c(%rbp), %eax
subl $0x303, %eax            # imm = 0x303
je 0x3655e
jmp 0x363f6
movl -0x8c(%rbp), %eax
subl $0x400, %eax            # imm = 0x400
je 0x3674b
jmp 0x36409
movl -0x8c(%rbp), %eax
addl $0xfffffbff, %eax       # imm = 0xFFFFFBFF
subl $0x2, %eax
jb 0x36595
jmp 0x3641f
movl -0x8c(%rbp), %eax
subl $0x403, %eax            # imm = 0x403
je 0x36570
jmp 0x36432
movl -0x8c(%rbp), %eax
subl $0x650, %eax            # imm = 0x650
je 0x365fd
jmp 0x36445
movl -0x8c(%rbp), %eax
addl $0xfffff9af, %eax       # imm = 0xFFFFF9AF
subl $0x2, %eax
jb 0x3666c
jmp 0x3645b
movl -0x8c(%rbp), %eax
subl $0x653, %eax            # imm = 0x653
je 0x366e9
jmp 0x3646e
movl -0x8c(%rbp), %eax
subl $0x654, %eax            # imm = 0x654
je 0x36738
jmp 0x368c9
movzbl -0x2c(%rbp), %eax
cmpl $0xe, %eax
jne 0x36494
movb $0x0, 0x338bc(%rip)     # 0x69d50
movzbl -0x2c(%rbp), %eax
cmpl $0x6, %eax
jne 0x364be
leaq 0x3449c(%rip), %rax     # 0x6a940
cmpq $0x0, (%rax)
je 0x364bc
leaq 0x3448f(%rip), %rax     # 0x6a940
movq (%rax), %rax
movl -0x28(%rbp), %edi
movl -0x24(%rbp), %esi
callq *%rax
jmp 0x364be
jmp 0x368cb
movb $0x0, 0x33886(%rip)     # 0x69d50
jmp 0x368cb
leaq 0x32e0a(%rip), %rax     # 0x692e0
cmpb $0x0, 0x40(%rax)
je 0x364f0
cmpl $0x8, -0x24(%rbp)
jne 0x364ee
leaq 0x24383(%rip), %rdi     # 0x5a86c
callq 0x11020
jmp 0x364f0
cmpb $0x0, -0x2b(%rbp)
je 0x364fb
jmp 0x368cb
movl -0x38(%rbp), %eax
movsd 0xeb5a(%rip), %xmm0     # 0x45060
xorps %xmm1, %xmm1
movsd %xmm1, -0xa0(%rbp)
cmpl $0x300, %eax            # imm = 0x300
movsd %xmm0, -0x98(%rbp)
je 0x36530
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, -0x98(%rbp)
movsd -0x98(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x3c(%rbp)
movl -0x24(%rbp), %edi
callq 0x35ee0
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %edi
movss -0x3c(%rbp), %xmm0
callq 0x10ef0
jmp 0x368cb
leaq -0x38(%rbp), %rdi
addq $0xc, %rdi
callq 0x11020
jmp 0x368cb
movl -0x28(%rbp), %ecx
leaq 0x32d66(%rip), %rax     # 0x692e0
movl %ecx, 0x84(%rax)
movl -0x24(%rbp), %ecx
leaq 0x32d56(%rip), %rax     # 0x692e0
movl %ecx, 0x88(%rax)
jmp 0x368cb
movl -0x38(%rbp), %edx
movl $0x2, %eax
movl $0x3, %ecx
cmpl $0x401, %edx            # imm = 0x401
cmovel %ecx, %eax
movl %eax, -0x44(%rbp)
movzbl -0x28(%rbp), %eax
cmpl $0x1, %eax
jne 0x365c6
movl -0x44(%rbp), %ecx
leaq 0x32d1f(%rip), %rax     # 0x692e0
movl %ecx, 0x68(%rax)
jmp 0x365f8
movzbl -0x28(%rbp), %eax
cmpl $0x2, %eax
jne 0x365de
movl -0x44(%rbp), %ecx
leaq 0x32d07(%rip), %rax     # 0x692e0
movl %ecx, 0x6c(%rax)
jmp 0x365f6
movzbl -0x28(%rbp), %eax
cmpl $0x3, %eax
jne 0x365f4
movl -0x44(%rbp), %ecx
leaq 0x32cef(%rip), %rax     # 0x692e0
movl %ecx, 0x70(%rax)
jmp 0x365f6
jmp 0x365f8
jmp 0x368cb
movl -0x30(%rbp), %eax
movl %eax, -0x48(%rbp)
movl -0x48(%rbp), %edi
callq 0x40960
movl %eax, -0x4c(%rbp)
movb -0x2c(%rbp), %al
movb %al, -0x4d(%rbp)
movzbl -0x4d(%rbp), %edi
movl -0x4c(%rbp), %esi
callq 0x35f00
movl %eax, -0x54(%rbp)
movswl -0x28(%rbp), %edi
callq 0x409b0
movss %xmm0, -0x58(%rbp)
movzbl -0x4d(%rbp), %eax
cmpl $0x1, %eax
je 0x36643
movzbl -0x4d(%rbp), %eax
cmpl $0x3, %eax
jne 0x3665a
movss -0x58(%rbp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movss %xmm0, -0x58(%rbp)
movl -0x54(%rbp), %edi
movss -0x58(%rbp), %xmm0
callq 0x10ef0
jmp 0x368cb
movl -0x30(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %edi
callq 0x40960
movl %eax, -0x60(%rbp)
movb -0x2c(%rbp), %al
movb %al, -0x61(%rbp)
movzbl -0x61(%rbp), %edi
movl -0x60(%rbp), %esi
callq 0x35f60
movl %eax, -0x68(%rbp)
movzbl -0x2b(%rbp), %eax
movsd 0xe9c2(%rip), %xmm0     # 0x45060
xorps %xmm1, %xmm1
movsd %xmm1, -0xb0(%rbp)
cmpl $0x1, %eax
movsd %xmm0, -0xa8(%rbp)
je 0x366c6
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movsd -0xa8(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x6c(%rbp)
movl -0x68(%rbp), %edi
movss -0x6c(%rbp), %xmm0
callq 0x10ef0
jmp 0x368cb
movl -0x30(%rbp), %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %edi
callq 0x2030
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
leaq 0x24168(%rip), %rsi     # 0x5a86e
callq 0x10630
cmpb $0x0, %al
je 0x3672b
cmpb $0x0, 0x4ec52(%rip)     # 0x85368
je 0x36724
movb $0x0, 0x4ec49(%rip)     # 0x85368
jmp 0x368cb
movb $0x1, 0x4ec3d(%rip)     # 0x85368
movl -0x70(%rbp), %edi
callq 0x40a50
jmp 0x368cb
movl -0x30(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %edi
callq 0x40b00
jmp 0x368cb
movl -0x24(%rbp), %ecx
leaq 0x32b8b(%rip), %rax     # 0x692e0
movl %ecx, 0x74(%rax)
movl -0x20(%rbp), %ecx
leaq 0x32b7e(%rip), %rax     # 0x692e0
movl %ecx, 0x78(%rax)
movl -0x1c(%rbp), %ecx
leaq 0x32b71(%rip), %rax     # 0x692e0
addl 0x7c(%rax), %ecx
leaq 0x32b67(%rip), %rax     # 0x692e0
movl %ecx, 0x7c(%rax)
movl -0x18(%rbp), %ecx
leaq 0x32b5a(%rip), %rax     # 0x692e0
addl 0x80(%rax), %ecx
leaq 0x32b4d(%rip), %rax     # 0x692e0
movl %ecx, 0x80(%rax)
xorl %edi, %edi
callq 0x11cf0
cmpb $0x0, %al
je 0x368c7
movl $0x2, -0x80(%rbp)
movl $0x4, -0x84(%rbp)
cmpl $0x2, -0x24(%rbp)
jg 0x367f4
leaq 0x4ea9a(%rip), %rax     # 0x85260
movq (%rax), %rax
movq %rax, -0xb8(%rbp)
callq 0xf7a0
movq -0xb8(%rbp), %rdi
movss 0xea7c(%rip), %xmm1     # 0x45260
subss %xmm1, %xmm0
cvttss2si %xmm0, %esi
movl -0x20(%rbp), %edx
callq 0x20f0
cmpl $0x2, -0x20(%rbp)
jg 0x3683b
leaq 0x4ea5f(%rip), %rax     # 0x85260
movq (%rax), %rax
movq %rax, -0xc8(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xbc(%rbp)
callq 0x30570
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %esi
movss 0xea32(%rip), %xmm1     # 0x45260
subss %xmm1, %xmm0
cvttss2si %xmm0, %edx
callq 0x20f0
cvtsi2ssl -0x24(%rbp), %xmm0
movss %xmm0, -0xcc(%rbp)
callq 0xf7a0
movaps %xmm0, %xmm1
movss -0xcc(%rbp), %xmm0
movss 0xe998(%rip), %xmm2     # 0x451f8
subss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
jb 0x36880
leaq 0x4e9f0(%rip), %rax     # 0x85260
movq (%rax), %rdi
movl -0x20(%rbp), %edx
movl $0x4, %esi
callq 0x20f0
cvtsi2ssl -0x20(%rbp), %xmm0
movss %xmm0, -0xd0(%rbp)
callq 0x30570
movaps %xmm0, %xmm1
movss -0xd0(%rbp), %xmm0
movss 0xe953(%rip), %xmm2     # 0x451f8
subss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
jb 0x368c5
leaq 0x4e9ab(%rip), %rax     # 0x85260
movq (%rax), %rdi
movl -0x24(%rbp), %esi
movl $0x4, %edx
callq 0x20f0
jmp 0x368c7
jmp 0x368cb
jmp 0x368cb
jmp 0x36319
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)