// File: Alcaro[P]bridges/arlib/opengl/ctx-x11.cpp
// Function: (anonymous namespace)::aropengl_x11::init(unsigned int, unsigned int, unsigned long, unsigned long*, unsigned int)
bool init(uint32_t width, uint32_t height, uintptr_t parent, uintptr_t* window_, uint32_t flags)
	{
		ctx = None;
		win = None;
		current = False;
		
		if (!libLoad()) return false;
		if (glx.GetCurrentContext()) return false;
		
		bool debug = (flags & aropengl::t_debug_context);
		uint32_t version = (flags & 0xFFF);
		
		int glx_major = 0;
		int glx_minor = 0;
		if (!glx.QueryVersion(window_x11.display, &glx_major, &glx_minor)) return false;
		if (glx_major != 1 || glx_minor < 3) return false;
		
		int visual_attribs[] = {
			GLX_X_RENDERABLE,  True,
			GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
			GLX_RED_SIZE,      8,
			GLX_GREEN_SIZE,    8,
			GLX_BLUE_SIZE,     8,
			//GLX_DEPTH_SIZE,    0,
			//GLX_STENCIL_SIZE,  0,
			GLX_DOUBLEBUFFER,  True,
			None
		};
		
		int fbcount;
		GLXFBConfig* fbcs = glx.ChooseFBConfig(window_x11.display, window_x11.screen, visual_attribs, &fbcount);
		if (!fbcs) return false;
		GLXFBConfig fbc = fbcs[0];
		XFree(fbcs);
		
		XVisualInfo* vi = glx.GetVisualFromFBConfig(window_x11.display, fbc);
		
		XSetWindowAttributes swa;
		swa.colormap = XCreateColormap(window_x11.display, parent, vi->visual, AllocNone);
		swa.background_pixmap = None;
		swa.border_pixel      = 0;
		swa.event_mask        = ExposureMask; // to make runloop->step(true) break if someone moves a window on top of ours
		
		win = XCreateWindow(window_x11.display, parent, 0, 0, width, height, 0,
		                    vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
		if (!win) return false;
		
		*window_ = win;
		XFreeColormap(window_x11.display, swa.colormap);
		XFree(vi);
		
		PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB =
			(PFNGLXCREATECONTEXTATTRIBSARBPROC)glx.GetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
		if (!glXCreateContextAttribsARB) return false;
		
		int context_attribs[] = {
			GLX_CONTEXT_MAJOR_VERSION_ARB, (int)version/100,
			GLX_CONTEXT_MINOR_VERSION_ARB, (int)version/10%10,
			GLX_CONTEXT_FLAGS_ARB, debug ? GLX_CONTEXT_DEBUG_BIT_ARB : 0,
			//GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
			None
		};
		
		ctx = glXCreateContextAttribsARB(window_x11.display, fbc, 0, true, context_attribs);
		if (!ctx) return false;
		makeCurrent(true);
		
		glx.SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glx.GetProcAddress((const GLubyte*)"glXSwapIntervalSGI");
		
		return true;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x138(%rbp)
movq $0x0, 0x8(%rax)
movq $0x0, 0x10(%rax)
movb $0x0, 0x18(%rax)
callq 0x2f8c0
testb $0x1, %al
jne 0x2f3c2
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
callq *0x52030(%rip)          # 0x813f8
cmpq $0x0, %rax
je 0x2f3d7
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
movl -0x2c(%rbp), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x2d(%rbp)
movl -0x2c(%rbp), %eax
andl $0xfff, %eax            # imm = 0xFFF
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq 0x51fe6(%rip), %rax     # 0x813f0
leaq 0x51f57(%rip), %rcx     # 0x81368
movq (%rcx), %rdi
leaq -0x38(%rbp), %rsi
leaq -0x3c(%rbp), %rdx
callq *%rax
cmpl $0x0, %eax
jne 0x2f42c
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
cmpl $0x1, -0x38(%rbp)
jne 0x2f438
cmpl $0x3, -0x3c(%rbp)
jge 0x2f441
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
leaq -0x70(%rbp), %rdi
leaq 0x346c4(%rip), %rsi     # 0x63b10
movl $0x34, %edx
callq 0x4280
movq 0x51fa3(%rip), %rax     # 0x81400
leaq 0x51f04(%rip), %rcx     # 0x81368
movq (%rcx), %rdi
leaq 0x51efa(%rip), %rcx     # 0x81368
movl 0x8(%rcx), %esi
leaq -0x70(%rbp), %rdx
leaq -0x74(%rbp), %rcx
callq *%rax
movq %rax, -0x80(%rbp)
cmpq $0x0, -0x80(%rbp)
jne 0x2f48f
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
movq -0x80(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x80(%rbp), %rdi
callq 0x4470
movq 0x51f5b(%rip), %rax     # 0x81408
leaq 0x51eb4(%rip), %rcx     # 0x81368
movq (%rcx), %rdi
movq -0x88(%rbp), %rsi
callq *%rax
movq %rax, -0x90(%rbp)
leaq 0x51e9a(%rip), %rax     # 0x81368
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movq -0x90(%rbp), %rax
movq (%rax), %rdx
xorl %ecx, %ecx
callq 0x41e0
movq %rax, -0xa0(%rbp)
movq $0x0, -0x100(%rbp)
movq $0x0, -0xe8(%rbp)
movq $0x8000, -0xb8(%rbp) # imm = 0x8000
leaq 0x51e53(%rip), %rax     # 0x81368
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movl -0x14(%rbp), %r8d
movl -0x18(%rbp), %r9d
movq -0x90(%rbp), %rax
movl 0x14(%rax), %r11d
movq -0x90(%rbp), %rax
movq (%rax), %r10
xorl %ecx, %ecx
leaq -0x100(%rbp), %rax
movl %ecx, %edx
movl $0x0, (%rsp)
movl %r11d, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq $0x2808, 0x20(%rsp)     # imm = 0x2808
movq %rax, 0x28(%rsp)
callq 0x4590
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x10(%rax)
cmpq $0x0, 0x10(%rax)
jne 0x2f58e
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
movq -0x138(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
leaq 0x51dc1(%rip), %rax     # 0x81368
movq (%rax), %rdi
movq -0xa0(%rbp), %rsi
callq 0x40a0
movq -0x90(%rbp), %rdi
callq 0x4470
leaq 0x3457b(%rip), %rdi     # 0x63b44
callq *0x51e09(%rip)          # 0x813d8
movq %rax, -0x108(%rbp)
cmpq $0x0, -0x108(%rbp)
jne 0x2f5e9
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
movl $0x2091, -0x130(%rbp)   # imm = 0x2091
movl -0x34(%rbp), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, -0x12c(%rbp)
movl $0x2092, -0x128(%rbp)   # imm = 0x2092
movl -0x34(%rbp), %eax
movl $0xa, %ecx
cltd
idivl %ecx
movl $0xa, %ecx
cltd
idivl %ecx
movl %edx, -0x124(%rbp)
movl $0x2094, -0x120(%rbp)   # imm = 0x2094
movb -0x2d(%rbp), %dl
xorl %eax, %eax
movl $0x1, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x11c(%rbp)
movl $0x0, -0x118(%rbp)
movq -0x108(%rbp), %rax
leaq 0x51d09(%rip), %rcx     # 0x81368
movq (%rcx), %rdi
movq -0x88(%rbp), %rsi
leaq -0x130(%rbp), %r8
xorl %ecx, %ecx
movl %ecx, %edx
movl $0x1, %ecx
callq *%rax
movq %rax, %rcx
movq -0x138(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x8(%rax)
jne 0x2f696
movb $0x0, -0x1(%rbp)
jmp 0x2f6bf
movq -0x138(%rbp), %rdi
movq (%rdi), %rax
movl $0x1, %esi
callq *(%rax)
leaq 0x344b1(%rip), %rdi     # 0x63b5f
callq *0x51d24(%rip)          # 0x813d8
movq %rax, 0x51d65(%rip)     # 0x81420
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
nopl (%rax)