// File: /workspace/github_c_makefile_star2_O2/jonof[P]jftekwar/jfbuild/src/sdlayer2.c
// Function: handleevents
int handleevents(void)
{
	int code, rv=0, j, control;
	SDL_Event ev;
	static int firstcall = 1;
	int eattextinput = 0;

#define SetKey(key,state) { \
	keystatus[key] = state; \
		if (state) { \
	keyfifo[keyfifoend] = key; \
	keyfifo[(keyfifoend+1)&(KEYFIFOSIZ-1)] = state; \
	keyfifoend = ((keyfifoend+2)&(KEYFIFOSIZ-1)); \
		} \
}

	while (SDL_PollEvent(&ev)) {
		switch (ev.type) {
			case SDL_TEXTINPUT:
				if (eattextinput) {
					eattextinput = 0;
					break;
				}
				for (j = 0; j < SDL_TEXTINPUTEVENT_TEXT_SIZE && ev.text.text[j]; j++) {
					if (ev.text.text[j] & 0x80) {
						continue;   // UTF8 character byte
					}
					code = ev.text.text[j];
					if (OSD_HandleChar(code)) {
						if (((keyasciififoend+1)&(KEYFIFOSIZ-1)) != keyasciififoplc) {
							keyasciififo[keyasciififoend] = code;
							keyasciififoend = ((keyasciififoend+1)&(KEYFIFOSIZ-1));
						}
					}
				}
				break;

			case SDL_KEYUP:
				// (un)grab mouse with ctrl-g
				if (ev.key.keysym.sym == SDLK_g
					&& (ev.key.keysym.mod & KMOD_CTRL)) {
					grabmouse(!mouseacquired);
					break;
				}
				// else, fallthrough
			case SDL_KEYDOWN:
#ifdef __APPLE__
				// For Mac keyboards that removed the Help key, which shared a scancode
				// with PC Insert, pretend Cmd+Delete is insert when in the editor.
				if (ev.key.keysym.sym == SDLK_DELETE && ev.key.keysym.mod == KMOD_RGUI) {
					extern short editstatus;
					if (editstatus) {
						ev.key.keysym.sym = SDLK_INSERT;
						ev.key.keysym.scancode = SDL_SCANCODE_INSERT;
						ev.key.keysym.mod &= ~KMOD_RGUI;
					}
				}
#endif
				code = keytranslation[ev.key.keysym.scancode].normal;
				control = keytranslation[ev.key.keysym.scancode].controlchar;

				if (control && ev.key.type == SDL_KEYDOWN) {
					int needcontrol = (control & WITH_CONTROL_KEY) == WITH_CONTROL_KEY;
					int mod = ev.key.keysym.mod & ~(KMOD_CAPS|KMOD_NUM);
					control &= ~WITH_CONTROL_KEY;

					// May need to insert a control character into the ascii input
					// FIFO depending on what the state of the control keys are.
					if ((needcontrol  && mod && (mod & KMOD_CTRL) == mod) ||
						(!needcontrol && (mod == KMOD_NONE))) {
						if (OSD_HandleChar(control)) {
							if (((keyasciififoend+1)&(KEYFIFOSIZ-1)) != keyasciififoplc) {
								keyasciififo[keyasciififoend] = control;
								keyasciififoend = ((keyasciififoend+1)&(KEYFIFOSIZ-1));
							}
						}
					}
				}

				// hook in the osd
				if (code == OSD_CaptureKey(-1)) {
					if (ev.key.type == SDL_KEYDOWN) {
						// The character produced by the OSD toggle key needs to be ignored.
						eattextinput = 1;

						OSD_ShowDisplay(-1);
					}
					break;
				} else if (OSD_HandleKey(code, (ev.key.type == SDL_KEYDOWN)) == 0)
					break;

				if (ev.key.type == SDL_KEYDOWN) {
					if (!keystatus[code]) {
						SetKey(code, 1);
					}
				} else {
					SetKey(code, 0);
				}
				break;

			case SDL_WINDOWEVENT:
				if (ev.window.event == SDL_WINDOWEVENT_FOCUS_GAINED ||
						ev.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
					appactive = ev.window.event == SDL_WINDOWEVENT_FOCUS_GAINED;
					if (mouseacquired && moustat) {
						SDL_SetRelativeMouseMode(appactive ? SDL_TRUE : SDL_FALSE);
					}
					rv=-1;
				}
				break;

			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				switch (ev.button.button) {
					case SDL_BUTTON_LEFT: j = 0; break;
					case SDL_BUTTON_RIGHT: j = 1; break;
					case SDL_BUTTON_MIDDLE: j = 2; break;
					default: j = -1; break;
				}
				if (j<0) break;

				if (ev.button.state == SDL_PRESSED)
					mouseb |= (1<<j);
				else
					mouseb &= ~(1<<j);
				break;

			case SDL_MOUSEWHEEL:
				if (ev.wheel.y > 0) {   // Up
					j = 5;
				} else if (ev.wheel.y < 0) {    // Down
					j = 4;
				} else {
					break;
				}
				mouseb |= 1<<j;
				// 'release' is done in readmousebstatus()
				break;

			case SDL_MOUSEMOTION:
				if (!firstcall) {
					if (appactive) {
						mousex += ev.motion.xrel;
						mousey += ev.motion.yrel;
					}
				}
				break;

			case SDL_CONTROLLERAXISMOTION:
				if (appactive) {
					joyaxis[ ev.caxis.axis ] = ev.caxis.value;
				}
				break;

			case SDL_CONTROLLERBUTTONDOWN:
			case SDL_CONTROLLERBUTTONUP:
				if (appactive) {
					if (ev.cbutton.state == SDL_PRESSED)
						joyb |= 1 << ev.cbutton.button;
					else
						joyb &= ~(1 << ev.cbutton.button);
				}
				break;

			case SDL_QUIT:
				quitevent = 1;
				rv=-1;
				break;

			default:
				//buildprintf("Got event (%d)\n", ev.type);
				break;
		}
	}

	sampletimer();
	startwin_idle(NULL);
	wm_idle(NULL);
#undef SetKey

	firstcall = 0;

	return rv;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
xorl %r13d, %r13d
movq %rsp, %rbx
leaq 0x395a0(%rip), %r12     # 0x9d02c
pushq $-0x1
popq %rbp
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x6bc0
testl %eax, %eax
je 0x63e70
movl (%rsp), %eax
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jb 0x63bb4
leal -0x651(%rax), %ecx
cmpl $0x2, %ecx
jb 0x63b87
cmpl $0x100, %eax            # imm = 0x100
je 0x63bef
cmpl $0x200, %eax            # imm = 0x200
je 0x63d72
cmpl $0x300, %eax            # imm = 0x300
je 0x63ca2
cmpl $0x301, %eax            # imm = 0x301
je 0x63c7d
cmpl $0x650, %eax            # imm = 0x650
je 0x63c01
cmpl $0x400, %eax            # imm = 0x400
je 0x63c25
cmpl $0x403, %eax            # imm = 0x403
je 0x63c5c
cmpl $0x303, %eax            # imm = 0x303
jne 0x63a92
testl %r13d, %r13d
movl $0x0, %r13d
jne 0x63a92
xorl %r12d, %r12d
leaq 0x1135bc(%rip), %r13    # 0x1770f0
cmpq $0x20, %r12
je 0x63e58
movsbl 0xc(%rsp,%r12), %r15d
testl %r15d, %r15d
je 0x63e58
testb %r15b, %r15b
js 0x63b82
movl %r15d, %edi
callq 0x432b6
testl %eax, %eax
je 0x63b82
movslq (%r13), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x11357d(%rip), %rdx    # 0x1770ec
cmpl (%rdx), %ecx
je 0x63b82
leaq 0x113586(%rip), %rdx    # 0x177100
movb %r15b, (%rdx,%rax)
movl %ecx, (%r13)
incq %r12
jmp 0x63b34
cmpb $0x0, (%r12)
je 0x63a92
movb 0xc(%rsp), %cl
pushq $0x1
popq %rax
shll %cl, %eax
cmpb $0x1, 0xd(%rsp)
jne 0x63e3a
leaq 0x113533(%rip), %rcx    # 0x1770e0
orl %eax, (%rcx)
jmp 0x63a92
movb 0x10(%rsp), %al
decb %al
cmpb $0x2, %al
ja 0x63a92
movzbl %al, %eax
leaq 0xfeec(%rip), %rcx      # 0x73ab8
movb (%rcx,%rax,4), %cl
pushq $0x1
popq %rax
shll %cl, %eax
cmpb $0x1, 0x11(%rsp)
je 0x63c6f
notl %eax
leaq 0x1134f4(%rip), %rcx    # 0x1770dc
andl %eax, (%rcx)
jmp 0x63a92
leaq 0x1134aa(%rip), %rax    # 0x1770a0
movb $0x1, (%rax)
movl %ebp, %r14d
jmp 0x63a92
cmpb $0x0, (%r12)
je 0x63a92
movswl 0x10(%rsp), %eax
movzbl 0xc(%rsp), %ecx
leaq 0x113773(%rip), %rdx    # 0x177390
movl %eax, (%rdx,%rcx,4)
jmp 0x63a92
cmpb $0x1, 0x664444(%rip)    # 0x6c8070
jne 0x63a92
cmpb $0x0, (%r12)
je 0x63a92
movl 0x1c(%rsp), %eax
leaq 0x11348c(%rip), %rcx    # 0x1770d4
addl %eax, (%rcx)
movl 0x20(%rsp), %eax
leaq 0x113483(%rip), %rcx    # 0x1770d8
addl %eax, (%rcx)
jmp 0x63a92
cmpl $0x0, 0x14(%rsp)
pushq $0x5
popq %rcx
jle 0x63e4a
pushq $0x1
popq %rax
shll %cl, %eax
leaq 0x113466(%rip), %rcx    # 0x1770dc
orl %eax, (%rcx)
jmp 0x63a92
cmpl $0x67, 0x14(%rsp)
jne 0x63ca2
testb $-0x40, 0x18(%rsp)
je 0x63ca2
xorl %edi, %edi
cmpb $0x0, 0x662634(%rip)    # 0x6c62c8
sete %dil
callq 0x62b55
jmp 0x63a92
movl 0x10(%rsp), %ecx
leaq 0x663f33(%rip), %rdx    # 0x6c7be0
movzbl (%rdx,%rcx,2), %r15d
cmpl $0x300, %eax            # imm = 0x300
jne 0x63d3e
leaq 0x663f1c(%rip), %rax    # 0x6c7be0
movb 0x1(%rax,%rcx,2), %al
testb %al, %al
je 0x63d3e
movzwl 0x18(%rsp), %ecx
jns 0x63ce7
movl %ecx, %edx
andl $0xcfff, %edx           # imm = 0xCFFF
je 0x63ce7
movl %ecx, %edx
andl $0xcf3f, %edx           # imm = 0xCF3F
je 0x63cfb
testb %al, %al
setns %dl
testw $0xcfff, %cx            # imm = 0xCFFF
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x63d3e
movzbl %al, %r12d
andl $0x7f, %r12d
movl %r12d, %edi
callq 0x432b6
testl %eax, %eax
je 0x63d3e
leaq 0x1133da(%rip), %rax    # 0x1770f0
movslq (%rax), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x1133c6(%rip), %rdx    # 0x1770ec
cmpl (%rdx), %ecx
je 0x63d3e
leaq 0x1133cf(%rip), %rdx    # 0x177100
movb %r12b, (%rdx,%rax)
leaq 0x1133b4(%rip), %rax    # 0x1770f0
movl %ecx, (%rax)
movl %ebp, %edi
callq 0x432a5
movl (%rsp), %ecx
cmpl %r15d, %eax
jne 0x63dc0
cmpl $0x300, %ecx            # imm = 0x300
leaq 0x392d2(%rip), %r12     # 0x9d02c
jne 0x63a92
movl %ebp, %edi
callq 0x43ca3
pushq $0x1
popq %rax
movl %eax, %r13d
jmp 0x63a92
movb 0xc(%rsp), %al
movl %eax, %ecx
andb $-0x2, %cl
cmpb $0xc, %cl
jne 0x63a92
cmpb $0xc, %al
sete (%r12)
cmpb $0x0, 0x662536(%rip)    # 0x6c62c8
movl %ebp, %r14d
je 0x63a92
testb $0x1, 0x662527(%rip)    # 0x6c62c9
movl %ebp, %r14d
je 0x63a92
xorl %edi, %edi
cmpb $0xc, %al
sete %dil
callq 0x6540
movl %ebp, %r14d
jmp 0x63a92
xorl %esi, %esi
cmpl $0x300, %ecx            # imm = 0x300
sete %sil
movl %r15d, %edi
callq 0x43b19
testl %eax, %eax
leaq 0x3924f(%rip), %r12     # 0x9d02c
je 0x63a92
cmpl $0x300, (%rsp)          # imm = 0x300
movl %r15d, %eax
leaq 0x11349c(%rip), %rcx    # 0x177290
jne 0x63e67
cmpb $0x0, (%rcx,%rax)
jne 0x63a92
leaq 0x113489(%rip), %rcx    # 0x177290
movb $0x1, (%rcx,%rax)
leaq 0x113332(%rip), %rdx    # 0x177144
movslq (%rdx), %rax
leaq 0x113334(%rip), %rsi    # 0x177150
movl %r15d, (%rsi,%rax,4)
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
movl $0x1, (%rsi,%rcx,4)
addl $0x2, %eax
andl $0x3f, %eax
movl %eax, (%rdx)
jmp 0x63a92
notl %eax
leaq 0x11329d(%rip), %rcx    # 0x1770e0
andl %eax, (%rcx)
jmp 0x63a92
pushq $0x4
popq %rcx
jns 0x63a92
jmp 0x63c6a
xorl %r13d, %r13d
leaq 0x391ca(%rip), %r12     # 0x9d02c
jmp 0x63a92
movb $0x0, (%rcx,%rax)
jmp 0x63a92
callq 0x62c7a
xorl %edi, %edi
callq 0x1c1c5
xorl %edi, %edi
callq 0x64180
movb $0x1, 0x6641e6(%rip)    # 0x6c8070
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq