// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %ecx, %ebp
movl %edx, %r12d
movq %rsi, %r15
movq %rdi, %r14
movq %r8, 0x8(%rsp)
movq %r9, 0x10(%rsp)
leaq 0x3b73b(%rip), %rax     # 0x45710
movq %rax, (%rdi)
movq %rsi, 0x10(%rdi)
movw $0x1, 0x18(%rdi)
leaq 0x50(%rdi), %rbx
xorl %r13d, %r13d
movq %r13, 0x50(%rdi)
leaq 0x3ec(%rip), %rax       # 0xa3e0
movq %rax, 0x58(%rdi)
leaq 0x70f(%rip), %rax       # 0xa70e
movq %rax, 0x68(%rdi)
movq %rdi, 0x60(%rdi)
movq %r13, 0x70(%rdi)
leaq 0x702(%rip), %rax       # 0xa714
movq %rax, 0x2a0(%rdi)
movq %rax, 0x298(%rdi)
movq %r13, 0x2a8(%rdi)
leaq 0x6e8(%rip), %rax       # 0xa716
movq %rax, 0x2b8(%rdi)
movq %rax, 0x2b0(%rdi)
movq %r13, 0x2c0(%rdi)
movl $0x1, %esi
movl $0xd, %edi
callq 0x3420
movq %r14, 0x3c2ff(%rip)     # 0x46358
movq 0x3c2e8(%rip), %rdi     # 0x46348
movq %rdi, 0x8(%r14)
movl $0x20806f, %edx         # imm = 0x20806F
movq %r15, %rsi
callq 0x31d0
leaq 0x48(%rsp), %rdx
movq $0x38, (%rdx)
movl %r12d, 0x20(%rdx)
movl %r12d, 0x18(%rdx)
movl %r12d, 0x10(%rdx)
movl %ebp, 0x24(%rdx)
movl %ebp, 0x1c(%rdx)
movl %ebp, 0x14(%rdx)
movq 0x8(%r14), %rdi
movq %r15, %rsi
callq 0x3130
movq 0x8(%r14), %rdi
leaq 0x20(%r14), %r8
leaq 0x3b6b3(%rip), %rsi     # 0x45760
movl $0x6, %edx
xorl %ecx, %ecx
callq 0x34f0
movq 0x28(%r14), %rax
leaq 0x30(%rsp), %rdx
movq %rax, (%rdx)
movq 0x30(%r14), %rax
movq %rax, 0x8(%rdx)
movq 0x8(%r14), %rdi
movq %r15, %rsi
movl $0x2, %ecx
callq 0x3190
callq 0x3510
cltq
leaq 0x28(%rsp), %r10
movq %rax, (%r10)
movq 0x8(%r14), %rdi
movq 0x38(%r14), %rdx
movl $0x6, %ecx
movq %r15, %rsi
movl $0x20, %r8d
xorl %r9d, %r9d
pushq $0x1
pushq %r10
callq 0x32b0
addq $0x10, %rsp
movq 0x8(%r14), %rdi
movq 0x40(%r14), %rdx
movq 0x48(%r14), %rcx
movzbl 0x17(%rsp), %eax
testb %al, %al
jns 0xa133
movq 0x8(%rsp), %r10
movl 0x10(%rsp), %eax
jmp 0xa13b
xorl $0xf, %eax
leaq 0x8(%rsp), %r10
movq %r15, %rsi
movl $0x8, %r8d
xorl %r9d, %r9d
pushq %rax
pushq %r10
callq 0x32b0
addq $0x10, %rsp
movq 0x3c1ee(%rip), %rdi     # 0x46348
movq %r15, %rsi
callq 0x33c0
movq %r14, %rdi
callq 0xa1ba
movq 0x3c1d7(%rip), %rax     # 0x46348
movl 0x10(%rax), %esi
leaq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x16edc
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa29c
cmpq %r13, 0x8(%r14)
je 0xa19c
cmpq $0x0, 0x18(%rsp)
je 0xa1a8
leaq 0x254a8(%rip), %rdi     # 0x2f64b
callq 0x80fb
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq