// File: /workspace/github_cpp_makefile_star2_O2/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 0x29c0
movq 0x20(%rbx), %rax
movzbl (%rax), %ebp
movb 0x1(%rax), %r12b
movzbl %r12b, %edx
leaq 0x1894(%rip), %rdi      # 0x4136
xorl %r14d, %r14d
movl %ebp, %esi
movl %edx, 0xc(%rsp)
xorl %eax, %eax
callq 0x2577
leaq 0x39cf(%rip), %rax      # 0x6288
movq (%rax), %r15
testq %r15, %r15
je 0x28d3
cmpb %r12b, (%r15)
je 0x297f
leaq 0x8(%r15), %rax
movq %r15, %r14
jmp 0x28b9
cmpl $0x5, %ebp
je 0x29b9
cmpl $0x4, %ebp
je 0x29b0
cmpl $0x3, %ebp
jne 0x2917
leaq 0x18(%rsp), %r15
movq %r15, %rdi
xorl %esi, %esi
callq 0x2240
movl 0xc(%rsp), %edi
callq 0x2eca
movq %rax, -0x8(%r15)
leaq 0x3963(%rip), %rdi      # 0x6270
leaq 0x10(%rsp), %rsi
callq 0x2edc
movq 0x393a(%rip), %r13      # 0x6258
movq 0x393b(%rip), %r14      # 0x6260
cmpq %r14, %r13
je 0x29c0
movq (%r13), %r15
cmpb $0x0, (%r15)
je 0x2940
cmpb 0x1(%r15), %r12b
je 0x294f
jmp 0x2968
movl 0xc(%rsp), %edi
callq 0x2eca
cmpq 0x8(%r15), %rax
jne 0x2962
movzbl 0x3952(%rip), %esi      # 0x62a8
movq %r15, %rdi
movl %ebp, %edx
callq 0x29d7
jmp 0x2979
testb $0x1, (%r15)
jne 0x2938
cmpl $0x2, %ebp
jne 0x2979
cmpb $0x0, 0x29(%r15)
je 0x2979
movb $0x1, 0x28(%r15)
addq $0x8, %r13
jmp 0x2925
leaq 0x17d7(%rip), %rdi      # 0x415d
xorl %eax, %eax
callq 0x2577
leaq 0x8(%r14), %rax
testq %r14, %r14
movq 0x8(%r15), %rcx
leaq 0x38e9(%rip), %rdx      # 0x6288
cmovneq %rax, %rdx
movq %rcx, (%rdx)
movq %r15, %rdi
callq 0x2180
jmp 0x29c0
movb $0x1, 0x38f1(%rip)      # 0x62a8
jmp 0x29c0
movb $0x0, 0x38e8(%rip)      # 0x62a8
movq %rbx, %rdi
callq 0x20a0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq