// File: /workspace/github_cpp_makefile_star2_O2/nishalkulkarni[P]fluid-wm/fluid_wm.cpp
// Function: WindowManager::Frame(unsigned long, bool)
void WindowManager::Frame(Window w, bool was_created_before_window_manager)
{
    assert(!clients_.count(w));

    XWindowAttributes x_window_attrs;
    assert(XGetWindowAttributes(display_, w, &x_window_attrs));

    if (was_created_before_window_manager) {
        if (x_window_attrs.override_redirect || x_window_attrs.map_state != IsViewable) {
            return;
        }
    }

    Node* parent_window;
    Node* new_window;
    if (clients_.size() == 0) {
        Window returned_root;
        int root_x, root_y;
        unsigned root_width, root_height, root_border_width, root_depth;

        assert(XGetGeometry(
            display_,
            root_,
            &returned_root,
            &root_x, &root_y,
            &root_width, &root_height,
            &root_border_width,
            &root_depth));

        std::tie(parent_window, new_window) = bsp->init_root(w, root_width, root_height, root_x, root_y);
    } else {
        std::tie(parent_window, new_window) = bsp->create_node(w);
    }

    int window_width, window_height, window_x, window_y;
    std::tie(window_width, window_height, window_x, window_y) = new_window->get_dimensions();
    std::cout << "\n\nhello" << window_width << " 0 " << window_height << "\n";
    std::cout << "bye" << window_x << " 0 " << window_y << "\n\n";

    bsp->display_tree();

    if (parent_window != nullptr) {
        int parent_width, parent_height, parent_x, parent_y;
        std::tie(parent_width, parent_height, parent_x, parent_y) = parent_window->get_dimensions();
        XResizeWindow(
            display_,
            clients_[parent_window->get_window()],
            parent_width - 2 * (BORDER_WIDTH + GAP), parent_height - 2 * (BORDER_WIDTH + GAP));
        XResizeWindow(
            display_,
            parent_window->get_window(),
            parent_width - 2 * (BORDER_WIDTH + GAP), parent_height - 2 * (BORDER_WIDTH + GAP));
    }

    XResizeWindow(
        display_,
        w,
        window_width - 2 * (BORDER_WIDTH + GAP), window_height - 2 * (BORDER_WIDTH + GAP));

    const Window frame = XCreateSimpleWindow(
        display_,
        root_,
        window_x + GAP,
        window_y + GAP,
        window_width - 2 * (BORDER_WIDTH + GAP),
        window_height - 2 * (BORDER_WIDTH + GAP),
        BORDER_WIDTH,
        BORDER_ACTIVE_COLOR,
        BG_COLOR);

    XSelectInput(
        display_,
        frame,
        SubstructureRedirectMask | SubstructureNotifyMask);

    XAddToSaveSet(display_, w);

    XReparentWindow(
        display_,
        w,
        frame,
        0, 0);

    XMapWindow(display_, frame);

    clients_[w] = frame;
    std::cout << " Map size " << clients_.size() << std::endl;
    for (auto i : clients_) {
        std::cout << i.first << " " << i.second << std::endl;
    }

    XSync(display_,false);
    //   Kill windows with alt + f4.
    XGrabKey(
        display_,
        XKeysymToKeycode(display_, XK_F4),
        MODKEY,
        w,
        false,
        GrabModeAsync,
        GrabModeAsync);
    //   Switch windows with alt + tab.
    XGrabKey(
        display_,
        XKeysymToKeycode(display_, XK_Tab),
        MODKEY,
        w,
        false,
        GrabModeAsync,
        GrabModeAsync);

    std::cout << "Framed window " << w << " [" << frame << "]\n\n";
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movl %edx, %ebp
movq %rdi, %rbx
leaq 0x28(%rsp), %rax
movq %rsi, (%rax)
leaq 0x10(%rdi), %r14
movq %r14, %rdi
movq %rax, %rsi
callq 0x74dc
testq %rax, %rax
jne 0x6d07
movq (%rbx), %rdi
movq 0x28(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x2430
testl %eax, %eax
je 0x6d26
testb %bpl, %bpl
je 0x6904
cmpl $0x0, 0xf8(%rsp)
jne 0x6cf5
cmpl $0x2, 0xdc(%rsp)
jne 0x6cf5
cmpq $0x0, 0x28(%rbx)
movq %r14, 0x40(%rsp)
je 0x6928
movq 0x48(%rbx), %rsi
movq 0x28(%rsp), %rdx
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x54e4
jmp 0x6998
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x34(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x3c(%rsp), %rax
movq %rax, 0x8(%rsp)
leaq 0x1c(%rsp), %rax
movq %rax, (%rsp)
leaq 0x50(%rsp), %rdx
leaq 0x24(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x38(%rsp), %r9
callq 0x2360
testl %eax, %eax
je 0x6d45
movq 0x48(%rbx), %rsi
movq 0x28(%rsp), %rdx
movl 0x38(%rsp), %ecx
movl 0x1c(%rsp), %r8d
movl 0x24(%rsp), %r9d
movl 0x20(%rsp), %eax
movl %eax, (%rsp)
leaq 0x60(%rsp), %r15
movq %r15, %rdi
callq 0x5414
movq (%r15), %rsi
movq 0x8(%r15), %r15
leaq 0x50(%rsp), %r12
movq %r12, %rdi
callq 0x528e
leaq 0x1c(%rsp), %rax
leaq 0x60(%rsp), %rdi
movq %rax, (%rdi)
leaq 0x38(%rsp), %r14
movq %r14, 0x8(%rdi)
leaq 0x20(%rsp), %rbp
movq %rbp, 0x10(%rdi)
leaq 0x24(%rsp), %r13
movq %r13, 0x18(%rdi)
movq %r12, %rsi
callq 0x73ce
movq 0x75f5(%rip), %r12      # 0xdfd8
leaq 0x25d9(%rip), %rsi      # 0x8fc3
movq %r12, %rdi
callq 0x2200
movl (%r13), %esi
movq %rax, %rdi
callq 0x2400
leaq 0x25c6(%rip), %r13      # 0x8fcb
movq %rax, %rdi
movq %r13, %rsi
callq 0x2200
movl (%rbp), %esi
movq %rax, %rdi
callq 0x2400
leaq 0x278d(%rip), %rsi      # 0x91af
movq %rax, %rdi
callq 0x2200
leaq 0x259e(%rip), %rsi      # 0x8fcf
movq %r12, %rdi
callq 0x2200
movl (%r14), %esi
movq %rax, %rdi
callq 0x2400
movq %rax, %rdi
movq %r13, %rsi
callq 0x2200
leaq 0x1c(%rsp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2400
leaq 0x2749(%rip), %rsi      # 0x91ae
movq %rax, %rdi
callq 0x2200
movq 0x48(%rbx), %rdi
callq 0x56de
testq %r15, %r15
movq 0x40(%rsp), %r14
je 0x6b25
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x528e
leaq 0x48(%rsp), %rax
leaq 0x60(%rsp), %r13
movq %rax, (%r13)
leaq 0x4c(%rsp), %rax
movq %rax, 0x8(%r13)
leaq 0x34(%rsp), %rax
movq %rax, 0x10(%r13)
leaq 0x3c(%rsp), %rbp
movq %rbp, 0x18(%r13)
movq %r13, %rdi
movq %r12, %rsi
callq 0x73ce
movq (%rbx), %r12
movq %r15, %rdi
callq 0x52a0
movq %rax, (%r13)
leaq 0x60(%rsp), %rsi
movq %r14, %rdi
callq 0x7594
movq (%rax), %rsi
movl (%rbp), %edx
addl $-0xe, %edx
leaq 0x34(%rsp), %rax
movl (%rax), %ecx
addl $-0xe, %ecx
movq %r12, %rdi
callq 0x2350
movq (%rbx), %r12
movq %r15, %rdi
callq 0x52a0
movl (%rbp), %edx
addl $-0xe, %edx
leaq 0x34(%rsp), %rcx
movl (%rcx), %ecx
addl $-0xe, %ecx
movq %r12, %rdi
movq %rax, %rsi
callq 0x2350
movq (%rbx), %rdi
leaq 0x28(%rsp), %r12
movq (%r12), %rsi
movl 0x24(%rsp), %edx
addl $-0xe, %edx
movl 0x20(%rsp), %ecx
addl $-0xe, %ecx
callq 0x2350
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl 0x38(%rsp), %edx
addl $0x5, %edx
movl 0x1c(%rsp), %ecx
addl $0x5, %ecx
movl 0x24(%rsp), %r8d
addl $-0xe, %r8d
movl 0x20(%rsp), %r9d
addl $-0xe, %r9d
movq $0x2e3440, 0x10(%rsp)   # imm = 0x2E3440
movq $0x81a1c1, 0x8(%rsp)    # imm = 0x81A1C1
movl $0x2, (%rsp)
callq 0x2070
movq %rax, %r15
movq (%rbx), %rdi
movl $0x180000, %edx         # imm = 0x180000
movq %rax, %rsi
callq 0x2130
movq (%rbx), %rdi
movq (%r12), %rsi
callq 0x22c0
movq (%rbx), %rdi
movq (%r12), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2030
movq (%rbx), %rdi
movq %r15, %rsi
callq 0x2260
movq %r14, %rdi
movq %r12, %rsi
callq 0x787e
movq %r15, (%rax)
movq 0x73fc(%rip), %r14      # 0xdfd8
leaq 0x23f0(%rip), %rsi      # 0x8fd3
movq %r14, %rdi
callq 0x2200
movq 0x28(%rbx), %rsi
movq %rax, %rdi
callq 0x2150
movq %rax, %rdi
callq 0x20e0
leaq 0x20(%rbx), %rbp
leaq 0x1908(%rip), %r12      # 0x8512
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x6c43
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %r13
movq %r14, %rdi
callq 0x2150
movq %rax, %rdi
movq %r12, %rsi
callq 0x2200
movq %rax, %rdi
movq %r13, %rsi
callq 0x2150
movq %rax, %rdi
callq 0x20e0
jmp 0x6c0a
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x2470
movq (%rbx), %r14
movl $0xffc1, %esi           # imm = 0xFFC1
movq %r14, %rdi
callq 0x2120
movzbl %al, %esi
movq 0x28(%rsp), %rcx
pushq $0x1
popq %rbp
movl %ebp, (%rsp)
pushq $0x8
popq %r12
movq %r14, %rdi
movl %r12d, %edx
xorl %r8d, %r8d
movl %ebp, %r9d
callq 0x21f0
movq (%rbx), %rbx
movl $0xff09, %esi           # imm = 0xFF09
movq %rbx, %rdi
callq 0x2120
movzbl %al, %esi
movq 0x28(%rsp), %rcx
movl %ebp, (%rsp)
movq %rbx, %rdi
movl %r12d, %edx
xorl %r8d, %r8d
movl %ebp, %r9d
callq 0x21f0
movq 0x7325(%rip), %rdi      # 0xdfd8
leaq 0x2324(%rip), %rsi      # 0x8fde
callq 0x2200
movq 0x28(%rsp), %rsi
movq %rax, %rdi
callq 0x2150
leaq 0x23bc(%rip), %rsi      # 0x908f
movq %rax, %rdi
callq 0x2200
movq %rax, %rdi
movq %r15, %rsi
callq 0x2150
leaq 0x2300(%rip), %rsi      # 0x8fed
movq %rax, %rdi
callq 0x2200
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21ca(%rip), %rdi      # 0x8ed8
leaq 0x2166(%rip), %rsi      # 0x8e7b
leaq 0x21cf(%rip), %rcx      # 0x8eeb
movl $0x82, %edx
callq 0x2140
leaq 0x21e6(%rip), %rdi      # 0x8f13
leaq 0x2147(%rip), %rsi      # 0x8e7b
leaq 0x21b0(%rip), %rcx      # 0x8eeb
movl $0x85, %edx
callq 0x2140
leaq 0x21fa(%rip), %rdi      # 0x8f46
leaq 0x2128(%rip), %rsi      # 0x8e7b
leaq 0x2191(%rip), %rcx      # 0x8eeb
movl $0x9b, %edx
callq 0x2140