// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movl %ecx, %r15d
movl %edx, %r14d
movl %esi, %ebp
movq %rdi, %rbx
xorl %edi, %edi
callq 0x5370
movq %rax, (%rbx)
testq %rax, %rax
je 0x108ab
movslq 0xe0(%rax), %r10
movl %r10d, 0x10(%rbx)
shlq $0x7, %r10
cmpl $0x1, %r15d
movq 0xe8(%rax), %r11
movq 0x10(%r11,%r10), %rsi
pushq $0xa
popq %rcx
movl $0x320, %edx            # imm = 0x320
cmovel %ecx, %edx
subq $0x8, %rsp
movq %rax, %rdi
movl %ebp, %r8d
movl %r14d, %r9d
pushq 0x58(%r11,%r10)
pushq 0x60(%r11,%r10)
pushq $0x1
callq 0x50e0
addq $0x20, %rsp
movq %rax, 0x8(%rbx)
movq (%rbx), %rdi
movl $0x8001, %edx           # imm = 0x8001
movq %rax, %rsi
callq 0x51e0
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movq 0xe8(%rdi), %rax
movslq 0xe0(%rdi), %rcx
shlq $0x7, %rcx
movl 0x38(%rax,%rcx), %r8d
movl %ebp, 0xc(%rsp)
movl %ebp, %edx
movl %r14d, %ecx
callq 0x5090
movq (%rbx), %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x51a0
movq %rax, 0x18(%rbx)
leaq 0x1163(%rip), %rsi      # 0x11930
leaq 0x70(%rsp), %r15
movl $0xf0, %edx
movq %r15, %rdi
callq 0x5250
movq (%rbx), %rax
movq 0xe8(%rax), %rcx
movslq 0xe0(%rax), %rax
shlq $0x7, %rax
movq 0x50(%rcx,%rax), %r12
pushq $0x4
popq %rbp
leaq 0x10(%rsp), %r13
movq (%rbx), %rdi
cmpq $0x10, %rbp
je 0x10838
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %rcx
callq 0x5230
movq (%rbx), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x5260
movq 0x10(%rsp), %rax
movq %rax, (%rbx,%rbp,8)
incq %rbp
addq $0x14, %r15
jmp 0x10801
movq 0x18(%rbx), %rsi
movq 0x28(%rbx), %rdx
callq 0x50f0
leaq 0x20(%rsp), %rdx
movq $0x39, (%rdx)
movl %r14d, 0x24(%rdx)
movl %r14d, 0x1c(%rdx)
movl %r14d, 0x44(%rdx)
movl %r14d, 0x14(%rdx)
movl 0xc(%rsp), %eax
movl %eax, 0x20(%rdx)
movl %eax, 0x18(%rdx)
movl %eax, 0x40(%rdx)
movl %eax, 0x10(%rdx)
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x5210
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
callq 0x5490
movq (%rbx), %rdi
callq 0x53e0
pushq $0x1
popq %rdi
callq 0x5220
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x973e(%rip), %rdi      # 0x19ff0
leaq 0x1055(%rip), %rsi      # 0x1190e
callq 0x52b0
movq %rax, %rdi
callq 0x5160
pushq $0x1
popq %rdi
callq 0x5320