// File: /workspace/github_cpp_makefile_star2_O2/nishalkulkarni[P]fluid-wm/fluid_wm.cpp
// Function: WindowManager::Run()
void WindowManager::Run()
{
    wm_detected_ = false;
    XSetErrorHandler(&WindowManager::OnWMDetected);

    XSelectInput(
        display_,
        root_,
        SubstructureRedirectMask | SubstructureNotifyMask);
    XSync(display_, false);
    if (wm_detected_) {
        std::cout << "Detected another window manager on display "
                  << XDisplayString(display_) << std::endl;
        return;
    }
    XSetErrorHandler(&WindowManager::OnXError);

    XGrabServer(display_);
    Window returned_root, returned_parent;
    Window* top_level_windows;
    unsigned int num_top_level_windows;
    assert(XQueryTree(
        display_,
        root_,
        &returned_root,
        &returned_parent,
        &top_level_windows,
        &num_top_level_windows));
    assert(returned_root == root_);
    for (unsigned int i = 0; i < num_top_level_windows; ++i) {
        Frame(top_level_windows[i], true);
    }
    XFree(top_level_windows);
    XUngrabServer(display_);

    for (;;) {
        XEvent e;
        XNextEvent(display_, &e);
        std::cout << "Received event: " << ToString(e) << std::endl;

        switch (e.type) {
        case CreateNotify:
            OnCreateNotify(e.xcreatewindow);
            break;
        case DestroyNotify:
            OnDestroyNotify(e.xdestroywindow);
            break;
        case ReparentNotify:
            OnReparentNotify(e.xreparent);
            break;
        case MapNotify:
            OnMapNotify(e.xmap);
            break;
        case UnmapNotify:
            OnUnmapNotify(e.xunmap);
            break;
        case ConfigureNotify:
            OnConfigureNotify(e.xconfigure);
            break;
        case MapRequest:
            OnMapRequest(e.xmaprequest);
            break;
        case ConfigureRequest:
            OnConfigureRequest(e.xconfigurerequest);
            break;
        case ButtonPress:
            OnButtonPress(e.xbutton);
            break;
        case ButtonRelease:
            OnButtonRelease(e.xbutton);
            break;
        case MotionNotify:
            // Skip any already pending motion events.
            while (XCheckTypedWindowEvent(
                display_, e.xmotion.window, MotionNotify, &e)) { }
            OnMotionNotify(e.xmotion);
            break;
        case KeyPress:
            OnKeyPress(e.xkey);
            break;
        case KeyRelease:
            OnKeyRelease(e.xkey);
            break;
        default:
            std::cout << "Ignored event" << std::endl;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
movb $0x0, 0x7d6a(%rip)      # 0xe26d
leaq 0x230(%rip), %rdi       # 0x673a
callq 0x2080
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl $0x180000, %edx         # imm = 0x180000
callq 0x2130
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x2470
cmpb $0x1, 0x7d3c(%rip)      # 0xe26d
jne 0x6575
movq 0x7a9e(%rip), %rdi      # 0xdfd8
leaq 0x28a3(%rip), %rsi      # 0x8de4
callq 0x2200
movq %rax, %r14
movq (%rbx), %rdi
callq 0x2170
movq %r14, %rdi
movq %rax, %rsi
callq 0x2200
movq %rax, %rdi
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20e0
leaq 0x1ee(%rip), %rdi       # 0x676a
callq 0x2080
movq (%rbx), %rdi
callq 0x2220
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x20(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x20a0
testl %eax, %eax
jne 0x65c6
leaq 0x285c(%rip), %rdi      # 0x8e10
leaq 0x28c0(%rip), %rsi      # 0x8e7b
leaq 0x28c6(%rip), %rcx      # 0x8e88
pushq $0x42
jmp 0x660f
movq 0x18(%rsp), %rax
cmpq 0x8(%rbx), %rax
jne 0x65f8
xorl %r14d, %r14d
pushq $0x1
popq %rbp
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rdi
cmpq %rax, %r14
jae 0x6615
movq (%rdi,%r14,8), %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x6890
incq %r14
jmp 0x65d7
leaq 0x28a3(%rip), %rdi      # 0x8ea2
leaq 0x2875(%rip), %rsi      # 0x8e7b
leaq 0x287b(%rip), %rcx      # 0x8e88
pushq $0x43
popq %rdx
callq 0x2140
callq 0x22a0
movq (%rbx), %rdi
callq 0x2110
leaq 0x48(%rsp), %r14
leaq 0x28(%rsp), %r13
leaq 0x2721(%rip), %r15      # 0x8d54
pushq $0x6
popq %rbp
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x2320
movq 0x7990(%rip), %rdi      # 0xdfd8
leaq 0x286a(%rip), %rsi      # 0x8eb9
callq 0x2200
movq %rax, %r12
movq %r13, %rdi
movq %r14, %rsi
callq 0x266c
movq %r12, %rdi
movq %r13, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x20e0
movq %r13, %rdi
callq 0x20d0
movl 0x48(%rsp), %eax
addl $-0x2, %eax
cmpl $0x15, %eax
ja 0x66f5
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x70ec
jmp 0x6636
movq (%rbx), %rdi
movq 0x68(%rsp), %rsi
movl %ebp, %edx
movq %r14, %rcx
callq 0x22e0
testl %eax, %eax
jne 0x669f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7012
jmp 0x6636
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6ddc
jmp 0x6636
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6e04
jmp 0x6636
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6d6c
jmp 0x6636
movq 0x78dc(%rip), %rdi      # 0xdfd8
leaq 0x27c7(%rip), %rsi      # 0x8eca
callq 0x2200
movq %rax, %rdi
callq 0x20e0
jmp 0x6636
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6f12
jmp 0x6636
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2420