// File: /workspace/github_c_makefile_star2_O3/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
movq %rsp, %rdi
callq 0x6bc0
xorl %ebx, %ebx
testl %eax, %eax
je 0x6f70c
leaq 0x34cdb(%rip), %r13     # 0xa402c
movq %rsp, %r14
xorl %r12d, %r12d
movl (%rsp), %eax
cmpl $0x400, %eax            # imm = 0x400
jg 0x6f38e
cmpl $0x300, %eax            # imm = 0x300
jg 0x6f3e3
cmpl $0x100, %eax            # imm = 0x100
je 0x6f4cf
cmpl $0x200, %eax            # imm = 0x200
je 0x6f5da
cmpl $0x300, %eax            # imm = 0x300
je 0x6f508
jmp 0x6f6e8
cmpl $0x64f, %eax            # imm = 0x64F
jg 0x6f43b
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jae 0x6f470
movb 0x10(%rsp), %al
decb %al
cmpb $0x2, %al
ja 0x6f6e8
movzbl %al, %eax
leaq 0xf898(%rip), %rcx      # 0x7ec58
movb (%rcx,%rax,4), %cl
movl $0x1, %eax
shll %cl, %eax
cmpb $0x1, 0x11(%rsp)
je 0x6f492
notl %eax
leaq 0x10ecfe(%rip), %rcx    # 0x17e0dc
jmp 0x6f6e6
cmpl $0x301, %eax            # imm = 0x301
je 0x6f4e3
cmpl $0x303, %eax            # imm = 0x303
je 0x6f624
cmpl $0x400, %eax            # imm = 0x400
jne 0x6f6e8
cmpb $0x1, 0x65fc65(%rip)    # 0x6cf070
jne 0x6f6e8
cmpb $0x0, (%r13)
je 0x6f6e8
movl 0x1c(%rsp), %eax
leaq 0x10ecad(%rip), %rcx    # 0x17e0d4
addl %eax, (%rcx)
movl 0x20(%rsp), %eax
leaq 0x10eca4(%rip), %rcx    # 0x17e0d8
addl %eax, (%rcx)
jmp 0x6f6e8
leal -0x651(%rax), %ecx
cmpl $0x2, %ecx
jae 0x6f4a0
cmpb $0x0, (%r13)
je 0x6f6e8
movb 0xc(%rsp), %cl
movl $0x1, %eax
shll %cl, %eax
cmpb $0x1, 0xd(%rsp)
jne 0x6f6dd
leaq 0x10ec72(%rip), %rcx    # 0x17e0e0
jmp 0x6f499
cmpl $0x403, %eax            # imm = 0x403
jne 0x6f6e8
movl $0x5, %ecx
cmpl $0x0, 0x14(%rsp)
jle 0x6f6fa
movl $0x1, %eax
shll %cl, %eax
leaq 0x10ec43(%rip), %rcx    # 0x17e0dc
orl %eax, (%rcx)
jmp 0x6f6e8
cmpl $0x650, %eax            # imm = 0x650
jne 0x6f6e8
cmpb $0x0, (%r13)
je 0x6f6e8
movswl 0x10(%rsp), %eax
movzbl 0xc(%rsp), %ecx
leaq 0x10eec9(%rip), %rdx    # 0x17e390
movl %eax, (%rdx,%rcx,4)
jmp 0x6f6e8
leaq 0x10ebca(%rip), %rax    # 0x17e0a0
movb $0x1, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x6f6e8
cmpl $0x67, 0x14(%rsp)
jne 0x6f508
testb $-0x40, 0x18(%rsp)
je 0x6f508
xorl %edi, %edi
cmpb $0x0, 0x65ddce(%rip)    # 0x6cd2c8
sete %dil
callq 0x6e2a9
jmp 0x6f6e8
movl 0x10(%rsp), %ecx
leaq 0x65f6cd(%rip), %rdx    # 0x6cebe0
movzbl (%rdx,%rcx,2), %ebp
cmpl $0x300, %eax            # imm = 0x300
jne 0x6f5a4
leaq 0x65f6b7(%rip), %rax    # 0x6cebe0
movb 0x1(%rax,%rcx,2), %al
testb %al, %al
je 0x6f5a4
movzwl 0x18(%rsp), %ecx
jns 0x6f54c
movl %ecx, %edx
andl $0xcfff, %edx           # imm = 0xCFFF
je 0x6f54c
movl %ecx, %edx
andl $0xcf3f, %edx           # imm = 0xCF3F
je 0x6f561
testb %al, %al
setns %dl
testl $0xcfff, %ecx           # imm = 0xCFFF
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x6f5a4
movzbl %al, %r15d
andl $0x7f, %r15d
movl %r15d, %edi
callq 0x4bb18
testl %eax, %eax
je 0x6f5a4
leaq 0x10eb74(%rip), %rax    # 0x17e0f0
movslq (%rax), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x10eb60(%rip), %rdx    # 0x17e0ec
cmpl (%rdx), %ecx
je 0x6f5a4
leaq 0x10eb69(%rip), %rdx    # 0x17e100
movb %r15b, (%rdx,%rax)
leaq 0x10eb4e(%rip), %rax    # 0x17e0f0
movl %ecx, (%rax)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4bb07
movl (%rsp), %ecx
cmpl %ebp, %eax
jne 0x6f67f
cmpl $0x300, %ecx            # imm = 0x300
jne 0x6f6e8
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4c79f
movl $0x1, %r12d
jmp 0x6f6e8
movb 0xc(%rsp), %al
movl %eax, %ecx
andb $-0x2, %cl
cmpb $0xc, %cl
jne 0x6f6e8
cmpb $0xc, %al
sete (%r13)
cmpb $0x0, 0x65dcce(%rip)    # 0x6cd2c8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x6f6e8
testb $0x1, 0x65dcbd(%rip)    # 0x6cd2c9
je 0x6f6e8
xorl %edi, %edi
cmpb $0xc, %al
sete %dil
callq 0x6540
jmp 0x6f6e8
testl %r12d, %r12d
jne 0x6f67a
xorl %r15d, %r15d
leaq 0x10eabd(%rip), %r12    # 0x17e0f0
movsbl 0xc(%rsp,%r15), %ebp
testl %ebp, %ebp
je 0x6f67a
testb %bpl, %bpl
js 0x6f671
movl %ebp, %edi
callq 0x4bb18
testl %eax, %eax
je 0x6f671
movslq (%r12), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x10ea8e(%rip), %rdx    # 0x17e0ec
cmpl (%rdx), %ecx
je 0x6f671
leaq 0x10ea97(%rip), %rdx    # 0x17e100
movb %bpl, (%rdx,%rax)
movl %ecx, (%r12)
incq %r15
cmpq $0x20, %r15
jne 0x6f633
xorl %r12d, %r12d
jmp 0x6f6e8
xorl %esi, %esi
cmpl $0x300, %ecx            # imm = 0x300
sete %sil
movl %ebp, %edi
callq 0x4c4c3
testl %eax, %eax
leaq 0x10ebf5(%rip), %rcx    # 0x17e290
je 0x6f6e8
cmpl $0x300, (%rsp)          # imm = 0x300
movl %ebp, %eax
jne 0x6f706
cmpb $0x0, (%rcx,%rax)
jne 0x6f6e8
movb $0x1, (%rcx,%rax)
leaq 0x10ea8b(%rip), %rdx    # 0x17e144
movslq (%rdx), %rax
leaq 0x10ea8d(%rip), %rsi    # 0x17e150
movl %ebp, (%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 0x6f6e8
notl %eax
leaq 0x10e9fa(%rip), %rcx    # 0x17e0e0
andl %eax, (%rcx)
movq %r14, %rdi
callq 0x6bc0
testl %eax, %eax
jne 0x6f357
jmp 0x6f70c
jns 0x6f6e8
movl $0x4, %ecx
jmp 0x6f48b
movb $0x0, (%rcx,%rax)
jmp 0x6f6e8
callq 0x6e3ce
xorl %edi, %edi
callq 0x1e75d
xorl %edi, %edi
callq 0x6fa2d
movb $0x1, 0x65f94a(%rip)    # 0x6cf070
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop