// File: /workspace/github_c_makefile_star2_O1/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 0x6d97f
leaq 0x35aa7(%rip), %r13     # 0xa302c
movq %rsp, %r14
xorl %r12d, %r12d
movl (%rsp), %eax
cmpl $0x400, %eax            # imm = 0x400
jg 0x6d5c2
cmpl $0x300, %eax            # imm = 0x300
jg 0x6d606
cmpl $0x100, %eax            # imm = 0x100
je 0x6d6ec
cmpl $0x200, %eax            # imm = 0x200
je 0x6d7f7
cmpl $0x300, %eax            # imm = 0x300
je 0x6d725
jmp 0x6d95b
cmpl $0x64f, %eax            # imm = 0x64F
jg 0x6d65e
leal -0x401(%rax), %ecx
cmpl $0x2, %ecx
jae 0x6d696
movzbl 0x10(%rsp), %eax
cmpl $0x1, %eax
je 0x6d926
cmpl $0x2, %eax
je 0x6d91d
cmpl $0x3, %eax
jne 0x6d92c
movl $0x1, %ecx
jmp 0x6d922
cmpl $0x301, %eax            # imm = 0x301
je 0x6d700
cmpl $0x303, %eax            # imm = 0x303
je 0x6d841
cmpl $0x400, %eax            # imm = 0x400
jne 0x6d95b
cmpb $0x1, 0x660a52(%rip)    # 0x6ce080
jne 0x6d95b
cmpb $0x0, (%r13)
je 0x6d95b
movl 0x1c(%rsp), %eax
leaq 0x10fa8a(%rip), %rcx    # 0x17d0d4
addl %eax, (%rcx)
movl 0x20(%rsp), %eax
leaq 0x10fa81(%rip), %rcx    # 0x17d0d8
addl %eax, (%rcx)
jmp 0x6d95b
leal -0x651(%rax), %ecx
cmpl $0x2, %ecx
jae 0x6d6bd
cmpb $0x0, (%r13)
je 0x6d95b
cmpb $0x1, 0xd(%rsp)
jne 0x6d909
movb 0xc(%rsp), %cl
movl $0x1, %eax
shll %cl, %eax
leaq 0x10fa4f(%rip), %rcx    # 0x17d0e0
jmp 0x6d94c
cmpl $0x403, %eax            # imm = 0x403
jne 0x6d95b
movl $0x5, %ecx
cmpl $0x0, 0x14(%rsp)
jle 0x6d96d
movl $0x1, %eax
shll %cl, %eax
jmp 0x6d945
cmpl $0x650, %eax            # imm = 0x650
jne 0x6d95b
cmpb $0x0, (%r13)
je 0x6d95b
movswl 0x10(%rsp), %eax
movzbl 0xc(%rsp), %ecx
leaq 0x10fcac(%rip), %rdx    # 0x17d390
movl %eax, (%rdx,%rcx,4)
jmp 0x6d95b
leaq 0x10f9ad(%rip), %rax    # 0x17d0a0
movb $0x1, (%rax)
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x6d95b
cmpl $0x67, 0x14(%rsp)
jne 0x6d725
testb $-0x40, 0x18(%rsp)
je 0x6d725
xorl %edi, %edi
cmpb $0x0, 0x65ebc1(%rip)    # 0x6cc2d8
sete %dil
callq 0x6c577
jmp 0x6d95b
movl 0x10(%rsp), %ecx
leaq 0x6604c0(%rip), %rdx    # 0x6cdbf0
movzbl (%rdx,%rcx,2), %ebp
cmpl $0x300, %eax            # imm = 0x300
jne 0x6d7c1
leaq 0x6604aa(%rip), %rax    # 0x6cdbf0
movb 0x1(%rax,%rcx,2), %al
testb %al, %al
je 0x6d7c1
movzwl 0x18(%rsp), %ecx
jns 0x6d769
movl %ecx, %edx
andl $0xcfff, %edx           # imm = 0xCFFF
je 0x6d769
movl %ecx, %edx
andl $0xcf3f, %edx           # imm = 0xCF3F
je 0x6d77e
testb %al, %al
setns %dl
testl $0xcfff, %ecx           # imm = 0xCFFF
sete %cl
andb %dl, %cl
cmpb $0x1, %cl
jne 0x6d7c1
movzbl %al, %r15d
andl $0x7f, %r15d
movl %r15d, %edi
callq 0x49658
testl %eax, %eax
je 0x6d7c1
leaq 0x10f957(%rip), %rax    # 0x17d0f0
movslq (%rax), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x10f943(%rip), %rdx    # 0x17d0ec
cmpl (%rdx), %ecx
je 0x6d7c1
leaq 0x10f94c(%rip), %rdx    # 0x17d100
movb %r15b, (%rdx,%rax)
leaq 0x10f931(%rip), %rax    # 0x17d0f0
movl %ecx, (%rax)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x49647
movl (%rsp), %ecx
cmpl %ebp, %eax
jne 0x6d89f
cmpl $0x300, %ecx            # imm = 0x300
jne 0x6d95b
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x4a2db
movl $0x1, %r12d
jmp 0x6d95b
movb 0xc(%rsp), %al
movl %eax, %ecx
andb $-0x2, %cl
cmpb $0xc, %cl
jne 0x6d95b
cmpb $0xc, %al
sete (%r13)
cmpb $0x0, 0x65eac1(%rip)    # 0x6cc2d8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
je 0x6d95b
testb $0x1, 0x65eab0(%rip)    # 0x6cc2d9
je 0x6d95b
xorl %edi, %edi
cmpb $0xc, %al
sete %dil
callq 0x6540
jmp 0x6d95b
testl %r12d, %r12d
jne 0x6d897
xorl %r15d, %r15d
leaq 0x10f8a0(%rip), %r12    # 0x17d0f0
movsbl 0xc(%rsp,%r15), %ebp
testl %ebp, %ebp
je 0x6d897
testb %bpl, %bpl
js 0x6d88e
movl %ebp, %edi
callq 0x49658
testl %eax, %eax
je 0x6d88e
movslq (%r12), %rax
leal 0x1(%rax), %ecx
andl $0x3f, %ecx
leaq 0x10f871(%rip), %rdx    # 0x17d0ec
cmpl (%rdx), %ecx
je 0x6d88e
leaq 0x10f87a(%rip), %rdx    # 0x17d100
movb %bpl, (%rdx,%rax)
movl %ecx, (%r12)
incq %r15
cmpq $0x20, %r15
jne 0x6d850
xorl %r12d, %r12d
jmp 0x6d95b
xorl %esi, %esi
cmpl $0x300, %ecx            # imm = 0x300
sete %sil
movl %ebp, %edi
callq 0x49fec
testl %eax, %eax
leaq 0x10f9d5(%rip), %rcx    # 0x17d290
je 0x6d95b
cmpl $0x300, (%rsp)          # imm = 0x300
movl %ebp, %eax
jne 0x6d979
cmpb $0x0, (%rcx,%rax)
jne 0x6d95b
movb $0x1, (%rcx,%rax)
leaq 0x10f85f(%rip), %rdx    # 0x17d144
movslq (%rdx), %rax
leaq 0x10f861(%rip), %rsi    # 0x17d150
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 0x6d95b
movb 0xc(%rsp), %cl
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
roll %cl, %eax
leaq 0x10f7c5(%rip), %rcx    # 0x17d0e0
jmp 0x6d959
movl $0x2, %ecx
xorl %eax, %eax
jmp 0x6d933
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x6d933
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movb $0x1, %al
testb %al, %al
jne 0x6d95b
movl $0x1, %eax
shll %cl, %eax
cmpb $0x1, 0x11(%rsp)
jne 0x6d950
leaq 0x10f790(%rip), %rcx    # 0x17d0dc
orl %eax, (%rcx)
jmp 0x6d95b
notl %eax
leaq 0x10f783(%rip), %rcx    # 0x17d0dc
andl %eax, (%rcx)
movq %r14, %rdi
callq 0x6bc0
testl %eax, %eax
jne 0x6d58b
jmp 0x6d97f
jns 0x6d95b
movl $0x4, %ecx
jmp 0x6d6b1
movb $0x0, (%rcx,%rax)
jmp 0x6d95b
callq 0x6c69c
xorl %edi, %edi
callq 0x1e2e8
xorl %edi, %edi
callq 0x6dca1
movb $0x1, 0x6606e7(%rip)    # 0x6ce080
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop