// File: nishalkulkarni[P]fluid-wm/fluid_wm.cpp
// Function: WindowManager::OnKeyPress(XKeyEvent const&)
void WindowManager::OnKeyPress(const XKeyEvent& e)
{
    if ((e.state & MODKEY) && (e.keycode == XKeysymToKeycode(display_, XK_F4))) {
        // alt + f4: Close window.
        std::cout << "fewnlkewlhntkerjnhtrkjhntkrjnhktrnhj\n\n\n";
        Atom* supported_protocols;
        int num_supported_protocols;
        if (XGetWMProtocols(display_,
                e.window,
                &supported_protocols,
                &num_supported_protocols)
            && (::std::find(supported_protocols,
                    supported_protocols + num_supported_protocols,
                    WM_DELETE_WINDOW)
                != supported_protocols + num_supported_protocols)) {
            std::cout << "Gracefully deleting window " << e.window;

            XEvent msg;
            memset(&msg, 0, sizeof(msg));
            msg.xclient.type = ClientMessage;
            msg.xclient.message_type = WM_PROTOCOLS;
            msg.xclient.window = e.window;
            msg.xclient.format = 32;
            msg.xclient.data.l[0] = WM_DELETE_WINDOW;

            assert(XSendEvent(display_, e.window, false, 0, &msg));
        } else {
            std::cout << "Killing window " << e.window;
            XKillClient(display_, e.window);
        }
    } else if ((e.state & MODKEY) && (e.keycode == XKeysymToKeycode(display_, XK_Tab))) {
        // alt + tab: Switch window.
        auto i = clients_.find(e.window);
        assert(i != clients_.end());
        ++i;
        if (i == clients_.end()) {
            i = clients_.begin();
        }

        XRaiseWindow(display_, i->second);
        XSetInputFocus(display_, i->first, RevertToPointerRoot, CurrentTime);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x12e35
movq -0x108(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x54(%rcx), %ecx
movl %ecx, -0x10c(%rbp)
movq (%rax), %rdi
movl $0xffc1, %esi           # imm = 0xFFC1
callq 0x4140
movb %al, %cl
movl -0x10c(%rbp), %eax
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x12e35
movq 0x10304(%rip), %rdi     # 0x22fd0
leaq 0x524a(%rip), %rsi      # 0x17f1d
callq 0x4240
movq -0x108(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
leaq -0x18(%rbp), %rdx
leaq -0x1c(%rbp), %rcx
callq 0x42e0
cmpl $0x0, %eax
je 0x12df6
movq -0x108(%rbp), %rdx
movq -0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x1c(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
addq $0x70, %rdx
callq 0x135f0
movq -0x18(%rbp), %rcx
movslq -0x1c(%rbp), %rdx
shlq $0x3, %rdx
addq %rdx, %rcx
cmpq %rcx, %rax
je 0x12df6
movq 0x1028e(%rip), %rdi     # 0x22fd0
leaq 0x51fc(%rip), %rsi      # 0x17f45
callq 0x4240
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x4050
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0xc0, %edx
callq 0x4110
movq -0x108(%rbp), %rax
movl $0x21, -0xe0(%rbp)
movq 0x68(%rax), %rcx
movq %rcx, -0xb8(%rbp)
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq %rcx, -0xc0(%rbp)
movl $0x20, -0xb0(%rbp)
movq 0x70(%rax), %rcx
movq %rcx, -0xa8(%rbp)
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
xorl %edx, %edx
xorl %eax, %eax
movl %eax, %ecx
leaq -0xe0(%rbp), %r8
callq 0x4070
cmpl $0x0, %eax
je 0x12dd5
jmp 0x12df4
leaq 0x5185(%rip), %rdi      # 0x17f61
leaq 0x4e2c(%rip), %rsi      # 0x17c0f
movl $0x18e, %edx            # imm = 0x18E
leaq 0x51a1(%rip), %rcx      # 0x17f90
callq 0x4170
jmp 0x12e30
movq 0x101d3(%rip), %rdi     # 0x22fd0
leaq 0x51be(%rip), %rsi      # 0x17fc2
callq 0x4240
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x4050
movq -0x108(%rbp), %rax
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x44a0
jmp 0x12fb1
movq -0x10(%rbp), %rax
movl 0x50(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x12faf
movq -0x108(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x54(%rcx), %ecx
movl %ecx, -0x110(%rbp)
movq (%rax), %rdi
movl $0xff09, %esi           # imm = 0xFF09
callq 0x4140
movb %al, %cl
movl -0x110(%rbp), %eax
movzbl %cl, %ecx
cmpl %ecx, %eax
jne 0x12faf
movq -0x108(%rbp), %rdi
addq $0x10, %rdi
movq -0x10(%rbp), %rsi
addq $0x20, %rsi
callq 0x13640
movq -0x108(%rbp), %rdi
movq %rax, -0xe8(%rbp)
addq $0x10, %rdi
callq 0x13370
movq %rax, -0xf0(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x133a0
testb $0x1, %al
jne 0x12ecb
jmp 0x12ecd
jmp 0x12eec
leaq 0x50fe(%rip), %rdi      # 0x17fd2
leaq 0x4d34(%rip), %rsi      # 0x17c0f
movl $0x196, %edx            # imm = 0x196
leaq 0x50a9(%rip), %rcx      # 0x17f90
callq 0x4170
leaq -0xe8(%rbp), %rdi
callq 0x13400
movq -0x108(%rbp), %rdi
addq $0x10, %rdi
callq 0x13370
movq %rax, -0xf8(%rbp)
leaq -0xe8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0x13670
testb $0x1, %al
jne 0x12f28
jmp 0x12f4d
movq -0x108(%rbp), %rdi
addq $0x10, %rdi
callq 0x13340
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x108(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x120(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x136a0
movq -0x120(%rbp), %rdi
movq 0x8(%rax), %rsi
callq 0x4310
movq -0x108(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x118(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x136a0
movq -0x118(%rbp), %rdi
movq (%rax), %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %ecx
callq 0x41c0
jmp 0x12fb1
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw (%rax,%rax)