// File: /workspace/github_cpp_makefile_star2_O3/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, 0x707a(%rip)      # 0xf26d
leaq 0x2f8(%rip), %rdi       # 0x84f2
callq 0x3080
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
movl $0x180000, %edx         # imm = 0x180000
callq 0x3150
movq (%rbx), %rdi
xorl %esi, %esi
callq 0x3480
cmpb $0x1, 0x704c(%rip)      # 0xf26d
jne 0x82be
movq 0x6daa(%rip), %r14      # 0xefd8
leaq 0x2b1b(%rip), %rsi      # 0xad50
movl $0x2b, %edx
movq %r14, %rdi
callq 0x32a0
movq (%rbx), %rdi
callq 0x31a0
testq %rax, %rax
je 0x826e
movq %rax, %rbx
movq %rax, %rdi
callq 0x30e0
movq 0x6d77(%rip), %rdi      # 0xefd8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32a0
jmp 0x8286
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x3400
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3180
leaq 0x25d(%rip), %rdi       # 0x8522
callq 0x3080
movq (%rbx), %rdi
callq 0x3240
movq (%rbx), %rdi
movq 0x8(%rbx), %rsi
leaq 0x38(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0xc(%rsp), %r9
callq 0x30c0
testl %eax, %eax
jne 0x8315
leaq 0x2a7f(%rip), %rdi      # 0xad7c
leaq 0x2ae3(%rip), %rsi      # 0xade7
leaq 0x2ae9(%rip), %rcx      # 0xadf4
movl $0x42, %edx
callq 0x3160
movq 0x38(%rsp), %rax
cmpq 0x8(%rbx), %rax
jne 0x84ab
cmpl $0x0, 0xc(%rsp)
je 0x8350
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x86b6
incq %r14
movl 0xc(%rsp), %eax
cmpq %rax, %r14
jb 0x832e
movq 0x10(%rsp), %rdi
callq 0x32c0
movq (%rbx), %rdi
callq 0x3130
leaq 0x48(%rsp), %r14
movq 0x6c6a(%rip), %r15      # 0xefd8
leaq 0x18(%rsp), %r13
leaq 0x2946(%rip), %r12      # 0xacc0
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x3320
movl $0x10, %edx
movq %r15, %rdi
leaq 0x2a91(%rip), %rsi      # 0xae25
callq 0x32a0
movq %r13, %rdi
movq %r14, %rsi
callq 0x36cc
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %r15, %rdi
callq 0x32a0
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %rbp, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3180
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x83fc
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
movl 0x48(%rsp), %eax
addl $-0x2, %eax
cmpl $0x15, %eax
ja 0x847a
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8f8c
jmp 0x837a
movq (%rbx), %rdi
movq 0x68(%rsp), %rsi
movl $0x6, %edx
movq %r14, %rcx
callq 0x32f0
testl %eax, %eax
jne 0x8421
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8eb2
jmp 0x837a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8c5c
jmp 0x837a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8c84
jmp 0x837a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8be0
jmp 0x837a
movl $0xd, %edx
movq %r15, %rdi
leaq 0x29ad(%rip), %rsi      # 0xae36
callq 0x32a0
movq %r15, %rdi
callq 0x30f0
jmp 0x837a
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8db2
jmp 0x837a
leaq 0x295c(%rip), %rdi      # 0xae0e
leaq 0x292e(%rip), %rsi      # 0xade7
leaq 0x2934(%rip), %rcx      # 0xadf4
movl $0x43, %edx
callq 0x3160
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x84e9
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x3430
nop