// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movq %rsi, %rbx
cmpl $0x0, 0x18(%rsi)
jne 0x29f6
movq 0x20(%rbx), %rax
movzbl (%rax), %ebp
movb 0x1(%rax), %r12b
movzbl %r12b, %edx
leaq 0x1820(%rip), %rdi      # 0x4136
movl %ebp, %esi
movl %edx, 0xc(%rsp)
xorl %eax, %eax
callq 0x25d3
movq 0x3956(%rip), %r15      # 0x6280
testq %r15, %r15
je 0x2981
cmpb %r12b, (%r15)
jne 0x2939
xorl %r14d, %r14d
jmp 0x2950
movq %r15, %rax
movq 0x8(%rax), %r15
testq %r15, %r15
je 0x2981
movq %rax, %r14
movq %r15, %rax
cmpb %r12b, (%r15)
jne 0x293c
leaq 0x1806(%rip), %rdi      # 0x415d
xorl %eax, %eax
callq 0x25d3
leaq 0x8(%r14), %rax
testq %r14, %r14
movq 0x8(%r15), %rcx
leaq 0x3910(%rip), %rdx      # 0x6280
cmovneq %rax, %rdx
movq %rcx, (%rdx)
movq %r15, %rdi
callq 0x2170
jmp 0x29f6
cmpl $0x5, %ebp
je 0x29ef
cmpl $0x4, %ebp
je 0x29e6
cmpl $0x3, %ebp
jne 0x2a1e
leaq 0x18(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2230
movq 0x3806(%rip), %rdi      # 0x61b0
movl 0xc(%rsp), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x20b0
movq %rax, -0x8(%r15)
movq 0x38ae(%rip), %rsi      # 0x6270
cmpq 0x38af(%rip), %rsi      # 0x6278
je 0x2a0d
movq 0x20(%rsp), %rax
movq %rax, 0x10(%rsi)
movups 0x10(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x388c(%rip)     # 0x6270
jmp 0x2a1e
movb $0x1, 0x38b3(%rip)      # 0x62a0
jmp 0x29f6
movb $0x0, 0x38aa(%rip)      # 0x62a0
movq %rbx, %rdi
callq 0x2090
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x3854(%rip), %rdi      # 0x6268
leaq 0x10(%rsp), %rdx
callq 0x32ea
movq 0x382b(%rip), %r13      # 0x6250
movq 0x382c(%rip), %r14      # 0x6258
cmpq %r14, %r13
je 0x29f6
movq (%r13), %r15
cmpb $0x0, (%r15)
je 0x2a43
cmpb 0x1(%r15), %r12b
je 0x2a5d
jmp 0x2a76
movq 0x3766(%rip), %rdi      # 0x61b0
movl 0xc(%rsp), %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x20b0
cmpq 0x8(%r15), %rax
jne 0x2a70
movzbl 0x383c(%rip), %esi      # 0x62a0
movq %r15, %rdi
movl %ebp, %edx
callq 0x2a8d
jmp 0x2a87
testb $0x1, (%r15)
jne 0x2a3b
cmpl $0x2, %ebp
jne 0x2a87
cmpb $0x0, 0x29(%r15)
je 0x2a87
movb $0x1, 0x28(%r15)
addq $0x8, %r13
jmp 0x2a2c