// File: /workspace/github_cpp_makefile_star2_O1/utkukaratas[P]xkeylayers/xkeylayers.cpp
// Function: intercept(char*, XRecordInterceptData*)
void intercept(XPointer user_data, XRecordInterceptData* data)
{
    static bool mouse_pressed = False;

    if (data->category == XRecordFromServer)
    {
        xEvent* event = reinterpret_cast<xEvent*>(data->data);
        int     key_event = event->u.u.type;
        KeyCode key_code  = event->u.u.detail;
        debug("Intercepted key event %d, key code %d\n", key_event, key_code);

        TKey *g, *g_prev = NULL;

        for (g = app.generated; g != NULL; g = g->next)
        {
            if (g->key == key_code)
            {
                debug("Ignoring generated event.\n");
                if (g_prev != NULL)
                {
                    g_prev->next = g->next;
                }
                else
                {
                    app.generated = g->next;
                }
                free (g);
                goto exit;
            }
            g_prev = g;
        }

        // log last N keys for heuristics
        // todo: make this a circular buffer or something
        if (key_event == KeyRelease) {
            timeval now;
            gettimeofday(&now, NULL);
            auto p = make_pair(sym_from_code(key_code), now);
            app.last_presses.push_back(p);
        }

        if (key_event == ButtonPress)
        {
            mouse_pressed = True;
        }
        else if (key_event == ButtonRelease)
        {
            mouse_pressed = False;
        }
        else
        {
            for (TModifierKey* mod : app.modifiers) {
                if ((mod->use_keycode == False && sym_from_code(key_code) == mod->keysym)
                    ||
                    (mod->use_keycode == True && key_code == mod->keycode))
                {
                    handle_modifier(mod, mouse_pressed, key_event);
                }
                else if (mod->is_down && key_event == KeyPress)
                {
                    mod->used = True;
                }
            }
        }
    }

exit:
    XRecordFreeData(data);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
cmpl $0x0, 0x18(%rsi)
jne 0x2a14
movq 0x20(%rbx), %rax
movzbl (%rax), %ebp
movb 0x1(%rax), %r12b
movzbl %r12b, %edx
leaq 0x17fc(%rip), %rdi      # 0x4126
movl %ebp, %esi
movl %edx, 0xc(%rsp)
xorl %eax, %eax
callq 0x25eb
movq 0x3942(%rip), %r15      # 0x6280
testq %r15, %r15
je 0x2995
cmpb %r12b, (%r15)
jne 0x294d
xorl %r14d, %r14d
jmp 0x2964
movq %r15, %rax
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x2995
movq %rax, %r14
movq %r15, %rax
cmpb %r12b, (%r15)
jne 0x2950
leaq 0x17e2(%rip), %rdi      # 0x414d
xorl %eax, %eax
callq 0x25eb
leaq 0x8(%r14), %rax
testq %r14, %r14
movq 0x8(%r15), %rcx
leaq 0x38fc(%rip), %rdx      # 0x6280
cmovneq %rax, %rdx
movq %rcx, (%rdx)
movq %r15, %rdi
callq 0x2170
jmp 0x2a14
cmpl $0x5, %ebp
je 0x2a0d
cmpl $0x4, %ebp
je 0x2a04
cmpl $0x3, %ebp
jne 0x2a3c
leaq 0x28(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2230
movq 0x37f2(%rip), %rdi      # 0x61b0
movl 0xc(%rsp), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x20b0
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, 0x18(%rsp)
movq 0x3890(%rip), %rsi      # 0x6270
cmpq 0x3891(%rip), %rsi      # 0x6278
je 0x2a2b
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x386e(%rip)     # 0x6270
jmp 0x2a3c
movb $0x1, 0x3895(%rip)      # 0x62a0
jmp 0x2a14
movb $0x0, 0x388c(%rip)      # 0x62a0
movq %rbx, %rdi
callq 0x2090
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3836(%rip), %rdi      # 0x6268
leaq 0x10(%rsp), %rdx
callq 0x32be
movq 0x380d(%rip), %r13      # 0x6250
movq 0x380e(%rip), %r14      # 0x6258
cmpq %r14, %r13
je 0x2a14
movq (%r13), %r15
cmpb $0x0, (%r15)
jne 0x2a73
movq 0x3750(%rip), %rdi      # 0x61b0
movl 0xc(%rsp), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x20b0
cmpq 0x8(%r15), %rax
je 0x2a7f
cmpb $0x1, (%r15)
jne 0x2a92
cmpb 0x1(%r15), %r12b
jne 0x2a92
movzbl 0x381a(%rip), %esi      # 0x62a0
movq %r15, %rdi
movl %ebp, %edx
callq 0x2aa9
jmp 0x2aa3
cmpl $0x2, %ebp
jne 0x2aa3
cmpb $0x0, 0x29(%r15)
je 0x2aa3
movb $0x1, 0x28(%r15)
addq $0x8, %r13
jmp 0x2a4a