// File: Alcaro[P]bridges/arlib/game-x11.cpp
// Function: gameview_x11::gameview_x11(unsigned long, unsigned int, unsigned int, cstring)
gameview_x11(Window window, uint32_t width, uint32_t height, cstring title) : window(window)
{
	signal(SIGPIPE, SIG_IGN);
	
	g_gameview = this;
	display = window_x11.display;
	
	XSelectInput(display, window, ExposureMask|FocusChangeMask|KeyPressMask|KeyReleaseMask|
	                              PointerMotionMask|ButtonPressMask|ButtonReleaseMask|LeaveWindowMask);
	
	XSizeHints hints;
	hints.flags = PSize|PMinSize|PMaxSize;
	hints.width = hints.min_width = hints.max_width = width;
	hints.height = hints.min_height = hints.max_height = height;
	XSetWMNormalHints(display, window, &hints);
	
	static const char * const atom_names[] = {
#define ATOM(name) #name,
		X11_ATOMS
#undef ATOM
	};
	XInternAtoms(display, (char**)atom_names, ARRAY_SIZE(atom_names), false, (Atom*)&atoms);
	
	Atom protocols[2] = { atoms.WM_DELETE_WINDOW, atoms._NET_WM_PING, };
	XSetWMProtocols(display, window, protocols, ARRAY_SIZE(protocols));
	
	long pid = getpid(); // gotta love how X11 insists that long is 32 bits. the C builtin types are ridiculous
	XChangeProperty(display, window, atoms._NET_WM_PID, XA_CARDINAL, 32, PropModeReplace, (uint8_t*)&pid, 1);
	
	XChangeProperty(display, window, atoms._NET_WM_NAME, atoms.UTF8_STRING, 8, PropModeReplace, title.bytes().ptr(), title.length());
	
	XMapWindow(window_x11.display, window);
	
	calc_keyboard_map();
	
	runloop2::await_read(ConnectionNumber(window_x11.display)).then(&wait);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %r8, -0x78(%rbp)
movq %r9, -0x70(%rbp)
movq %rdi, -0x80(%rbp)
movq %rsi, -0x88(%rbp)
movl %edx, -0x8c(%rbp)
movl %ecx, -0x90(%rbp)
movq -0x80(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0x130f0
movq -0x158(%rbp), %rdi
leaq 0x6d8d8(%rip), %rax     # 0x80620
addq $0x10, %rax
movq %rax, (%rdi)
movq -0x88(%rbp), %rax
movq %rax, 0x10(%rdi)
movb $0x1, 0x18(%rdi)
movb $0x0, 0x19(%rdi)
addq $0x50, %rdi
callq 0x13110
movq -0x158(%rbp), %rax
movq %rax, %rdi
addq $0x60, %rdi
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rsi
callq 0x13130
movq -0x158(%rbp), %rdi
addq $0x298, %rdi            # imm = 0x298
callq 0x13160
movq -0x158(%rbp), %rdi
addq $0x2b0, %rdi            # imm = 0x2B0
callq 0x13180
movl $0xd, %edi
movl $0x1, %esi
callq 0x4410
movq -0x158(%rbp), %rax
movq %rax, 0x6e5a9(%rip)     # 0x81378
movq 0x6e592(%rip), %rcx     # 0x81368
movq %rcx, 0x8(%rax)
movq 0x8(%rax), %rdi
movq -0x88(%rbp), %rsi
movl $0x20806f, %edx         # imm = 0x20806F
callq 0x41d0
movq -0x158(%rbp), %rax
movq $0x38, -0xf0(%rbp)
movl -0x8c(%rbp), %ecx
movl %ecx, -0xd0(%rbp)
movl %ecx, -0xd8(%rbp)
movl %ecx, -0xe0(%rbp)
movl -0x90(%rbp), %ecx
movl %ecx, -0xcc(%rbp)
movl %ecx, -0xd4(%rbp)
movl %ecx, -0xdc(%rbp)
movq 0x8(%rax), %rdi
movq -0x88(%rbp), %rsi
leaq -0xf0(%rbp), %rdx
callq 0x4130
movq -0x158(%rbp), %r8
movq 0x8(%r8), %rdi
addq $0x20, %r8
leaq 0x6d822(%rip), %rsi     # 0x80680
movl $0x6, %edx
xorl %ecx, %ecx
callq 0x44e0
movq -0x158(%rbp), %rax
movq 0x28(%rax), %rcx
movq %rcx, -0x100(%rbp)
movq 0x30(%rax), %rcx
movq %rcx, -0xf8(%rbp)
movq 0x8(%rax), %rdi
movq -0x88(%rbp), %rsi
leaq -0x100(%rbp), %rdx
movl $0x2, %ecx
callq 0x4190
callq 0x4500
movl %eax, %ecx
movq -0x158(%rbp), %rax
movslq %ecx, %rcx
movq %rcx, -0x108(%rbp)
movq 0x8(%rax), %rdi
movq -0x88(%rbp), %rsi
movq 0x38(%rax), %rdx
movl $0x6, %ecx
movl $0x20, %r8d
xorl %r9d, %r9d
leaq -0x108(%rbp), %rax
movq %rax, (%rsp)
movl $0x1, 0x8(%rsp)
callq 0x42b0
movq -0x158(%rbp), %rax
movq 0x8(%rax), %rcx
movq %rcx, -0x150(%rbp)
movq -0x88(%rbp), %rcx
movq %rcx, -0x148(%rbp)
movq 0x40(%rax), %rcx
movq %rcx, -0x140(%rbp)
movq 0x48(%rax), %rax
movq %rax, -0x138(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rax
leaq -0x68(%rbp), %rcx
movq %rcx, -0x28(%rbp)
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x130(%rbp)
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x12f8f
movq -0x130(%rbp), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5cc0
movq -0x130(%rbp), %rsi
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
callq 0x5ed0
jmp 0x12fa7
movq -0x130(%rbp), %rax
movq (%rax), %rsi
movl 0x8(%rax), %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
callq 0x5ed0
movups -0x68(%rbp), %xmm0
movaps %xmm0, -0x50(%rbp)
movq -0x50(%rbp), %rcx
movq -0x48(%rbp), %rax
movq %rcx, -0x118(%rbp)
movq %rax, -0x110(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x62c0
movq %rax, -0x168(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x160(%rbp)
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x13022
movq -0x160(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0x34(%rbp)
jmp 0x1302f
movq -0x160(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x168(%rbp), %r10
movq -0x138(%rbp), %rcx
movq -0x140(%rbp), %rdx
movq -0x148(%rbp), %rsi
movq -0x150(%rbp), %rdi
movl -0x34(%rbp), %eax
movl $0x8, %r8d
xorl %r9d, %r9d
movq %r10, (%rsp)
movl %eax, 0x8(%rsp)
callq 0x42b0
movq 0x6e2f6(%rip), %rdi     # 0x81368
movq -0x88(%rbp), %rsi
callq 0x43b0
movq -0x158(%rbp), %rdi
callq 0x131a0
movq 0x6e2d7(%rip), %rax     # 0x81368
movl 0x10(%rax), %esi
leaq -0x128(%rbp), %rdi
callq 0x13370
movq -0x158(%rbp), %rsi
addq $0x50, %rsi
leaq -0x128(%rbp), %rdi
callq 0x133a0
leaq -0x128(%rbp), %rdi
callq 0x13420
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
nopl (%rax)