// File: EstherJin[P]TetrisRemake/window.cc
// Function: Xwindow::Xwindow(int, int, int)
Xwindow::Xwindow(int width, int height, int player) {
    d = XOpenDisplay(NULL);
    if (d == NULL) {
        cerr << "Cannot open display" << endl;
        exit(1);
    }

    s = DefaultScreen(d);
    if (player == 1) {
        w = XCreateSimpleWindow(
                d, RootWindow(d, s), 10, 10, width, height,
                1, BlackPixel(d, s), WhitePixel(d, s)
        );
    } else {
        w = XCreateSimpleWindow(
                d, RootWindow(d, s), 800, 10, width, height,
                1, BlackPixel(d, s), WhitePixel(d, s)
        );
    }

    XSelectInput(d, w, ExposureMask | KeyPressMask);

    Pixmap pix = XCreatePixmap(
            d, w, width, height,
            DefaultDepth(d, DefaultScreen(d))
    );

    gc = XCreateGC(d, pix, 0, 0);

    // Set up colours.
    XColor xcolour;
    Colormap cmap;

    const size_t numColours = 12;
    char color_vals[numColours][20] = {
            "PapayaWhip", "gray20", "MediumPurple3",
            "RoyalBlue4", "tomato4", "goldenrod1",
            "SeaGreen", "sienna1", "turquoise", "SaddleBrown", "White", "Brown"
    };

    cmap = DefaultColormap(d, DefaultScreen(d));

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

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

    // 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);

    // map window and flush
    XMapRaised(d, w);
    XFlush(d);

    // wait 1 second for setup
    sleep(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x190(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x64b0
movq %rax, %rcx
movq -0x190(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x2c62b
movq 0x159ea(%rip), %rdi     # 0x41ff0
leaq 0x1249(%rip), %rsi      # 0x2d856
callq 0x6360
movq %rax, %rdi
movq 0x15984(%rip), %rsi     # 0x41fa0
callq 0x63c0
movl $0x1, %edi
callq 0x6420
movq -0x190(%rbp), %rax
movq (%rax), %rcx
movl 0xe0(%rcx), %ecx
movl %ecx, 0x10(%rax)
cmpl $0x1, -0x14(%rbp)
jne 0x2c6d5
movq -0x190(%rbp), %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 0x6100
movq %rax, %rcx
movq -0x190(%rbp), %rax
movq %rcx, 0x8(%rax)
jmp 0x2c760
movq -0x190(%rbp), %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 $0x320, %edx            # imm = 0x320
movl $0xa, %ecx
movl $0x1, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x6100
movq %rax, %rcx
movq -0x190(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x190(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
movl $0x8001, %edx           # imm = 0x8001
callq 0x6240
movq -0x190(%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 -0x190(%rbp), %rax
movq %rcx, -0x20(%rbp)
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x61c0
movq %rax, %rcx
movq -0x190(%rbp), %rax
movq %rcx, 0x18(%rax)
movq $0xc, -0x40(%rbp)
leaq -0x130(%rbp), %rdi
leaq 0x107b(%rip), %rsi      # 0x2d870
movl $0xf0, %edx
callq 0x62f0
movq -0x190(%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, -0x134(%rbp)
movl -0x134(%rbp), %eax
cmpq $0xc, %rax
jae 0x2c8a8
movq -0x190(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
movl -0x134(%rbp), %eax
leaq -0x130(%rbp), %rdx
imulq $0x14, %rax, %rax
addq %rax, %rdx
leaq -0x30(%rbp), %rcx
callq 0x62c0
movq -0x190(%rbp), %rax
movq (%rax), %rdi
movq -0x38(%rbp), %rsi
leaq -0x30(%rbp), %rdx
callq 0x6300
movq -0x190(%rbp), %rax
movq -0x30(%rbp), %rdx
movl -0x134(%rbp), %ecx
movq %rdx, 0x20(%rax,%rcx,8)
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x2c833
movq -0x190(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rax), %rsi
movq 0x28(%rax), %rdx
callq 0x6110
movq -0x190(%rbp), %rax
movq $0x39, -0x188(%rbp)
movl -0x10(%rbp), %ecx
movl %ecx, -0x164(%rbp)
movl %ecx, -0x16c(%rbp)
movl %ecx, -0x144(%rbp)
movl %ecx, -0x174(%rbp)
movl -0xc(%rbp), %ecx
movl %ecx, -0x168(%rbp)
movl %ecx, -0x170(%rbp)
movl %ecx, -0x148(%rbp)
movl %ecx, -0x178(%rbp)
movq (%rax), %rdi
movq 0x8(%rax), %rsi
leaq -0x188(%rbp), %rdx
callq 0x62a0
movq -0x190(%rbp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x6630
movq -0x190(%rbp), %rax
movq (%rax), %rdi
callq 0x6540
movl $0x1, %edi
callq 0x62b0
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
nop