// File: MatthewZMD[P]TetrisDual/window.cc
// Function: Xwindow::Xwindow(int, int)
Xwindow::Xwindow(int width, int height): width{width}, height{height} {

	d = XOpenDisplay(NULL);
	if (d == NULL) {
		cerr << "Cannot open display" << endl;
		exit(1);
	}
	s = DefaultScreen(d);
	w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, width, height, 1,
			BlackPixel(d, s), WhitePixel(d, s));
	XSelectInput(d, w, ExposureMask | KeyPressMask);
	XMapRaised(d, w);

	Pixmap pix = XCreatePixmap(d,w,width,
			height,DefaultDepth(d,DefaultScreen(d)));
	gc = XCreateGC(d, pix, 0,(XGCValues *)0);

	Font thefont = XLoadFont(d, "*-fixed-*-*-*-*-*-*-*-*-*-*-*-*");
	XFlush(d);
	XFlush(d);


	// Set up colours.
	XColor xcolour;
	Colormap cmap;
	char color_vals[11][11] = {"white",  "black", "red",      "green",
		"blue",   "cyan",  "yellow",   "magenta",
		"orange", "brown", "darkgreen"};

	cmap=DefaultColormap(d,DefaultScreen(d));
	for(int i=0; i < 11; ++i) {
		XParseColor(d,cmap,color_vals[i],&xcolour);
		XAllocColor(d,cmap,&xcolour);
		colours[i]=xcolour.pixel;
	}

	XSetForeground(d,gc,colours[Black]);

	// Make window non-resizeable.
	XSizeHints hints;
	hints.flags = (USPosition | PSize | PMinSize | PMaxSize );
	hints.height = hints.base_height = hints.min_height = hints.max_height = height;
	hints.width = hints.base_width = hints.min_width = hints.max_width = width;
	XSetNormalHints(d, w, &hints);

	XSynchronize(d, True);

	XSelectInput(d, w, ExposureMask);
	XFlush(d);
	XEvent event;
	XNextEvent(d, &event); // Hang until the window is ready.
	XSelectInput(d, w, 0);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x200, %rsp            # imm = 0x200
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, 0x78(%rax)
movl -0x10(%rbp), %ecx
movl %ecx, 0x7c(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x64e0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x2bcf4
movq 0x18319(%rip), %rdi     # 0x43fe8
leaq 0x1db2(%rip), %rsi      # 0x2da88
callq 0x6350
movq %rax, %rdi
movq 0x1829b(%rip), %rsi     # 0x43f80
callq 0x63a0
movl $0x1, %edi
callq 0x6450
movq -0x1e0(%rbp), %rcx
movq (%rcx), %rax
movl 0xe0(%rax), %eax
movl %eax, 0x10(%rcx)
movq (%rcx), %rdi
movq (%rcx), %rax
movq 0xe8(%rax), %rax
movslq 0x10(%rcx), %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movq 0x10(%rax), %rsi
movl -0xc(%rbp), %r8d
movl -0x10(%rbp), %r9d
movq (%rcx), %rax
movq 0xe8(%rax), %rax
movslq 0x10(%rcx), %rdx
shlq $0x7, %rdx
addq %rdx, %rax
movq 0x60(%rax), %r10
movq (%rcx), %rax
movq 0xe8(%rax), %rax
movslq 0x10(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x58(%rax), %rax
movl $0xa, %ecx
movl %ecx, %edx
movl $0x1, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x60f0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, 0x8(%rax)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movl $0x8001, %edx           # imm = 0x8001
callq 0x6240
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x66b0
movq -0x1e0(%rbp), %r8
movq (%r8), %rdi
movq 0x8(%r8), %rsi
movl -0xc(%rbp), %edx
movl -0x10(%rbp), %ecx
movq (%r8), %rax
movq 0xe8(%rax), %rax
movq (%r8), %r8
movslq 0xe0(%r8), %r8
shlq $0x7, %r8
addq %r8, %rax
movl 0x38(%rax), %r8d
callq 0x60b0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, -0x18(%rbp)
movq (%rax), %rdi
movq -0x18(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x61c0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, 0x18(%rax)
movq (%rax), %rdi
leaq 0x1c7f(%rip), %rsi      # 0x2da9c
callq 0x66e0
movq %rax, %rcx
movq -0x1e0(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq (%rax), %rdi
callq 0x6590
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
callq 0x6590
leaq -0xc0(%rbp), %rdi
leaq 0x1c8b(%rip), %rsi      # 0x2dae0
movl $0x79, %edx
callq 0x62e0
movq -0x1e0(%rbp), %rcx
movq (%rcx), %rax
movq 0xe8(%rax), %rax
movq (%rcx), %rcx
movslq 0xe0(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x50(%rax), %rax
movq %rax, -0x38(%rbp)
movl $0x0, -0xc4(%rbp)
cmpl $0xb, -0xc4(%rbp)
jge 0x2bf07
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movslq -0xc4(%rbp), %rax
leaq -0xc0(%rbp), %rdx
imulq $0xb, %rax, %rax
addq %rax, %rdx
leaq -0x30(%rbp), %rcx
callq 0x62a0
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x62f0
movq -0x1e0(%rbp), %rax
movq -0x30(%rbp), %rdx
movslq -0xc4(%rbp), %rcx
movq %rdx, 0x20(%rax,%rcx,8)
movl -0xc4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc4(%rbp)
jmp 0x2be93
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rax), %rsi
movq 0x28(%rax), %rdx
callq 0x6130
movq -0x1e0(%rbp), %rax
movq $0x39, -0x118(%rbp)
movl -0x10(%rbp), %ecx
movl %ecx, -0xf4(%rbp)
movl %ecx, -0xfc(%rbp)
movl %ecx, -0xd4(%rbp)
movl %ecx, -0x104(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, -0xf8(%rbp)
movl %ecx, -0x100(%rbp)
movl %ecx, -0xd8(%rbp)
movl %ecx, -0x108(%rbp)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq -0x118(%rbp), %rdx
callq 0x6290
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movl $0x1, %esi
callq 0x6110
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movl $0x8000, %edx           # imm = 0x8000
callq 0x6240
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
callq 0x6590
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
leaq -0x1d8(%rbp), %rsi
callq 0x6530
movq -0x1e0(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x6240
addq $0x200, %rsp            # imm = 0x200
popq %rbp
retq
nopw (%rax,%rax)