// File: flightlessmango[P]macro-recorder/main.cpp
// Function: main
int main()
{
    // while(1) {
        // xdo_send_keysequence_window_down(x, CURRENTWINDOW, "A", 0);
    // }

    Window window;
    XEvent event;
    Window root = DefaultRootWindow(display);
    Window curFocus;
    int revert;
    /* open connection with the server */
 
    XGetInputFocus (display, &curFocus, &revert);
    XSelectInput(display, curFocus, KeyPressMask | KeyReleaseMask | FocusChangeMask);
 
    
    /* event loop */
    while (1)
    {   
        keyArray_prev = keyArray;
    // auto elapsed = std::chrono::high_resolution_clock::now() - start;

    // long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();

        XNextEvent(display, &event);
        /* keyboard events */
        if (event.type == KeyPress)
        {
            keyArray.insert(event.xkey.keycode);
            printf( "KeyPress: %x\n", event.xkey.keycode );
            writeToFile();
        }
        else if (event.type == KeyRelease)
        {
            keyArray.erase(event.xkey.keycode);
            printf( "KeyRelease: %x\n", event.xkey.keycode );
            writeToFile();
        }
        else if (event.type == FocusOut)
        {
                // printf("%s\n", "Focus changed!");
                if (curFocus != root)
                    XSelectInput(display, curFocus, 0);
                XGetInputFocus (display, &curFocus, &revert);

                if (curFocus == PointerRoot)
                    curFocus = root;
                XSelectInput(display, curFocus, KeyPressMask|KeyReleaseMask|FocusChangeMask);
                
        }
    }

    /* close connection to server */
    XCloseDisplay(display);
 
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movl $0x0, -0x4(%rbp)
movq 0x5b3f(%rip), %rax      # 0x7188
movq 0xe8(%rax), %rax
movq 0x5b31(%rip), %rcx      # 0x7188
movslq 0xe0(%rcx), %rcx
shlq $0x7, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0xd8(%rbp)
movq 0x5b11(%rip), %rdi      # 0x7188
leaq -0xe0(%rbp), %rsi
leaq -0xe4(%rbp), %rdx
callq 0x11b0
movq 0x5af7(%rip), %rdi      # 0x7188
movq -0xe0(%rbp), %rsi
movl $0x200003, %edx         # imm = 0x200003
callq 0x10b0
leaq 0x5aaf(%rip), %rdi      # 0x7158
leaq 0x5a78(%rip), %rsi      # 0x7128
callq 0x19c0
movq 0x5acc(%rip), %rdi      # 0x7188
leaq -0xd0(%rbp), %rsi
callq 0x1160
cmpl $0x2, -0xd0(%rbp)
jne 0x1710
leaq -0xd0(%rbp), %rsi
addq $0x54, %rsi
leaq 0x5a45(%rip), %rdi      # 0x7128
callq 0x19f0
movq %rax, -0xf8(%rbp)
movb %dl, -0xf0(%rbp)
movl -0x7c(%rbp), %esi
leaq 0x292a(%rip), %rdi      # 0x4029
movb $0x0, %al
callq 0x1030
callq 0x1410
jmp 0x17c6
cmpl $0x3, -0xd0(%rbp)
jne 0x1748
leaq -0xd0(%rbp), %rsi
addq $0x54, %rsi
leaq 0x59fd(%rip), %rdi      # 0x7128
callq 0x1a40
movl -0x7c(%rbp), %esi
leaq 0x28fd(%rip), %rdi      # 0x4037
movb $0x0, %al
callq 0x1030
callq 0x1410
jmp 0x17c4
cmpl $0xa, -0xd0(%rbp)
jne 0x17c2
movq -0xe0(%rbp), %rax
cmpq -0xd8(%rbp), %rax
je 0x1778
movq 0x5a20(%rip), %rdi      # 0x7188
movq -0xe0(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x10b0
movq 0x5a09(%rip), %rdi      # 0x7188
leaq -0xe0(%rbp), %rsi
leaq -0xe4(%rbp), %rdx
callq 0x11b0
cmpq $0x1, -0xe0(%rbp)
jne 0x17aa
movq -0xd8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq 0x59d7(%rip), %rdi      # 0x7188
movq -0xe0(%rbp), %rsi
movl $0x200003, %edx         # imm = 0x200003
callq 0x10b0
jmp 0x17c4
jmp 0x17c6
jmp 0x16a2
nopl (%rax,%rax)