// File: Alcaro[P]bridges/arlib/game-x11.cpp
// Function: gameview_x11::process_event(_XEvent*)
void process_event(XEvent* ev)
{
	got_event = true;
	switch (ev->type)
	{
		case Expose: break; // just set got_event, nothing else needed
		case FocusIn:
			focus = true;
			set_all_keys();
			break;
		case FocusOut:
			focus = false;
			break;
		case KeyPress:
		case KeyRelease:
			set_key(ev->xkey.keycode, (ev->type == KeyPress));
			break;
		case ButtonPress:
		case ButtonRelease:
			static_assert(Button1Mask == 256); // guaranteed by X11 spec
			static_assert(Button2Mask == 512);
			static_assert(Button3Mask == 1024);
			if (ev->xbutton.button >= 1 && ev->xbutton.button <= 3)
				send_mouse(ev->xbutton.x, ev->xbutton.y, ev->xbutton.state^(Button1Mask >> 1 << ev->xbutton.button));
			break;
		case MotionNotify:
			send_mouse(ev->xmotion.x, ev->xmotion.y, ev->xmotion.state);
			break;
		case LeaveNotify:
			if (!(ev->xcrossing.state & (Button1Mask | Button2Mask | Button3Mask))) // if a button is held, we'll get a MotionNotify soon
				send_mouse(-0x8000000, -0x8000000, ev->xcrossing.state);
			break;
		case ClientMessage:
			if (ev->xclient.message_type == atoms.WM_PROTOCOLS && ev->xclient.format == 32)
			{
				Atom subtype = ev->xclient.data.l[0];
				if (subtype == atoms.WM_DELETE_WINDOW)
				{
					cb_exit();
				}
				else if (subtype == atoms._NET_WM_PING)
				{
					ev->xclient.window = XRootWindow(display, window_x11.screen);
					XSendEvent(display, ev->xclient.window, false, SubstructureRedirectMask|SubstructureNotifyMask, ev);
				}
#ifndef ARLIB_OPT
				else puts("AAAAA unknown WM_PROTOCOLS");
#endif
			}
#ifndef ARLIB_OPT
			else puts("AAAAA unknown ClientMessage");
#endif
			break;
#ifndef ARLIB_OPT
		default:
			printf("AAAAA unknown event %d\n",ev->type);
			break;
#endif
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movb $0x1, 0x1a(%rax)
movq -0x10(%rbp), %rax
movl (%rax), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x20(%rbp)
subl $0x1f, %eax
ja 0x13a8c
movq -0x20(%rbp), %rax
leaq 0x4fba9(%rip), %rcx     # 0x63488
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x13aa0
movq -0x28(%rbp), %rdi
movb $0x1, 0x18(%rdi)
callq 0x13ab0
jmp 0x13aa0
movq -0x28(%rbp), %rax
movb $0x0, 0x18(%rax)
jmp 0x13aa0
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x54(%rax), %eax
movb %al, %cl
movq -0x10(%rbp), %rax
cmpl $0x2, (%rax)
sete %al
movzbl %cl, %esi
movzbl %al, %edx
andl $0x1, %edx
callq 0x13b70
jmp 0x13aa0
movq -0x10(%rbp), %rax
cmpl $0x1, 0x54(%rax)
jb 0x1397e
movq -0x10(%rbp), %rax
cmpl $0x3, 0x54(%rax)
ja 0x1397e
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x44(%rax), %edx
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movl 0x54(%rax), %ecx
movl $0x80, %eax
shll %cl, %eax
movl -0x2c(%rbp), %ecx
xorl %eax, %ecx
callq 0x13c60
jmp 0x13aa0
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x40(%rax), %esi
movq -0x10(%rbp), %rax
movl 0x44(%rax), %edx
movq -0x10(%rbp), %rax
movl 0x50(%rax), %ecx
callq 0x13c60
jmp 0x13aa0
movq -0x10(%rbp), %rax
movl 0x60(%rax), %eax
andl $0x700, %eax            # imm = 0x700
cmpl $0x0, %eax
jne 0x139ce
movq -0x28(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x60(%rax), %ecx
movl $0xf8000000, %edx       # imm = 0xF8000000
movl %edx, %esi
callq 0x13c60
jmp 0x13aa0
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
cmpq 0x20(%rcx), %rax
jne 0x13a7e
movq -0x10(%rbp), %rax
cmpl $0x20, 0x30(%rax)
jne 0x13a7e
movq -0x28(%rbp), %rcx
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
cmpq 0x28(%rcx), %rax
jne 0x13a20
movq -0x28(%rbp), %rdi
addq $0x60, %rdi
callq 0x13cb0
jmp 0x13a7c
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rax
cmpq 0x30(%rcx), %rax
jne 0x13a6e
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x6d934(%rip), %esi     # 0x81370
callq 0x45c0
movq %rax, %rdx
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
movq %rdx, 0x20(%rcx)
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %r8
xorl %edx, %edx
movl $0x180000, %ecx         # imm = 0x180000
callq 0x40c0
jmp 0x13a7a
leaq 0x4fad6(%rip), %rdi     # 0x6354b
callq 0x4580
jmp 0x13a7c
jmp 0x13a8a
leaq 0x4fae1(%rip), %rdi     # 0x63566
callq 0x4580
jmp 0x13aa0
movq -0x10(%rbp), %rax
movl (%rax), %esi
leaq 0x4fae9(%rip), %rdi     # 0x63582
movb $0x0, %al
callq 0x4050
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)