// File: /workspace/github_cpp_makefile_star2_O1/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, 0x702e(%rip)      # 0xf26d
leaq 0x2f8(%rip), %rdi       # 0x853e
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, 0x7000(%rip)      # 0xf26d
jne 0x830a
movq 0x6d5e(%rip), %r14      # 0xefd8
leaq 0x2acf(%rip), %rsi      # 0xad50
movl $0x2b, %edx
movq %r14, %rdi
callq 0x32a0
movq (%rbx), %rdi
callq 0x31a0
testq %rax, %rax
je 0x82ba
movq %rax, %rbx
movq %rax, %rdi
callq 0x30e0
movq 0x6d2b(%rip), %rdi      # 0xefd8
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32a0
jmp 0x82d2
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       # 0x856e
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 0x8361
leaq 0x2a33(%rip), %rdi      # 0xad7c
leaq 0x2a97(%rip), %rsi      # 0xade7
leaq 0x2a9d(%rip), %rcx      # 0xadf4
movl $0x42, %edx
callq 0x3160
movq 0x38(%rsp), %rax
cmpq 0x8(%rbx), %rax
jne 0x84f7
cmpl $0x0, 0xc(%rsp)
je 0x839c
xorl %r14d, %r14d
movq 0x10(%rsp), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0x8702
incq %r14
movl 0xc(%rsp), %eax
cmpq %rax, %r14
jb 0x837a
movq 0x10(%rsp), %rdi
callq 0x32c0
movq (%rbx), %rdi
callq 0x3130
leaq 0x48(%rsp), %r14
movq 0x6c1e(%rip), %r15      # 0xefd8
leaq 0x18(%rsp), %r13
leaq 0x28fa(%rip), %r12      # 0xacc0
movq (%rbx), %rdi
movq %r14, %rsi
callq 0x3320
movl $0x10, %edx
movq %r15, %rdi
leaq 0x2a45(%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 0x8448
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
movl 0x48(%rsp), %eax
addl $-0x2, %eax
cmpl $0x15, %eax
ja 0x84c6
movslq (%r12,%rax,4), %rax
addq %r12, %rax
jmpq *%rax
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8fea
jmp 0x83c6
movq (%rbx), %rdi
movq 0x68(%rsp), %rsi
movl $0x6, %edx
movq %r14, %rcx
callq 0x32f0
testl %eax, %eax
jne 0x846d
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8f10
jmp 0x83c6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8ca8
jmp 0x83c6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8cd0
jmp 0x83c6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8c2c
jmp 0x83c6
movl $0xd, %edx
movq %r15, %rdi
leaq 0x2961(%rip), %rsi      # 0xae36
callq 0x32a0
movq %r15, %rdi
callq 0x30f0
jmp 0x83c6
movq %rbx, %rdi
movq %r14, %rsi
callq 0x8e10
jmp 0x83c6
leaq 0x2910(%rip), %rdi      # 0xae0e
leaq 0x28e2(%rip), %rsi      # 0xade7
leaq 0x28e8(%rip), %rcx      # 0xadf4
movl $0x43, %edx
callq 0x3160
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x8535
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x3430
nop