// File: /workspace/github_c_makefile_star2_O2/comfies[P]custard/src/wm/handlers.c
// Function: handle_window_click
void handle_window_click(xcb_generic_event_t *generic_event) {
    xcb_button_press_event_t *event;
    event = (xcb_button_press_event_t*)generic_event;

    xcb_window_t window_id = event->event;
    log_debug("Window(%08x) clicked", window_id);

    window_t *window;
    if (windows) {
        while ((window = vector_iterator(windows))) {
            // Redirect border window click as necessary
            if (window->parent == window_id) {
                window_id = window->id;
                break;
            }
        }

        reset_vector_iterator(windows);
    }

    if (focused_window == window_id)
        return;

    xcb_window_t previous_window = focused_window;
    focused_window = window_id;

    /* Is the newly focused window managed? */
    if (window_is_managed(focused_window)) {
        window = get_window_by_id(focused_window);
        xcb_ungrab_button(xcb_connection,
            XCB_BUTTON_INDEX_ANY, window_id, XCB_MOD_MASK_ANY);
        raise_window(window->parent);
        decorate(window);
    } else {
        raise_window(window_id);
    }
    focus_window(window_id);

    /* Is the previously focused window managed? */
    if (window_is_managed(previous_window)) {
        window = get_window_by_id(previous_window);
        xcb_grab_button(xcb_connection, 0, previous_window,
            XCB_EVENT_MASK_BUTTON_PRESS,
            XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
            XCB_NONE, XCB_NONE,
            XCB_BUTTON_INDEX_ANY ^ XCB_BUTTON_INDEX_4 ^ XCB_BUTTON_INDEX_5,
            XCB_MOD_MASK_ANY);
        decorate(window);
    }

    apply();
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
subq $0x20, %rsp
movl 0xc(%rdi), %ebx
leaq 0x25a2(%rip), %rsi      # 0x86a0
leaq 0x25ad(%rip), %rdx      # 0x86b2
leaq 0x25ba(%rip), %r8       # 0x86c6
pushq $0x3
popq %rdi
movl $0x85, %ecx
movl %ebx, %r9d
xorl %eax, %eax
callq 0x50dd
leaq 0x67b3(%rip), %r14      # 0xc8d8
cmpq $0x0, (%r14)
je 0x6147
movq (%r14), %rdi
callq 0x383d
testq %rax, %rax
je 0x613f
cmpl %ebx, 0x4(%rax)
jne 0x612b
movl (%rax), %ebx
movq (%r14), %rdi
callq 0x3871
leaq 0x6792(%rip), %r14      # 0xc8e0
movl (%r14), %ebp
cmpl %ebx, %ebp
jne 0x615e
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
retq
movl %ebx, (%r14)
movl %ebx, %edi
callq 0x6904
testw %ax, %ax
je 0x61a3
movl (%r14), %edi
callq 0x6916
movq %rax, %r14
leaq 0x6769(%rip), %rax      # 0xc8e8
movq (%rax), %rdi
xorl %esi, %esi
movl %ebx, %edx
movl $0x8000, %ecx           # imm = 0x8000
callq 0x34f0
movl 0x4(%r14), %edi
callq 0x7348
movq %r14, %rdi
callq 0x5726
jmp 0x61aa
movl %ebx, %edi
callq 0x7348
movl %ebx, %edi
callq 0x72b8
movl %ebp, %edi
callq 0x6904
testw %ax, %ax
je 0x6205
movl %ebp, %edi
callq 0x6916
movq %rax, %rbx
leaq 0x671a(%rip), %rax      # 0xc8e8
movq (%rax), %rdi
movl $0x8000, 0x18(%rsp)     # imm = 0x8000
andl $0x0, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
andl $0x0, (%rsp)
pushq $0x4
popq %rcx
pushq $0x1
popq %r8
xorl %esi, %esi
movl %ebp, %edx
movl %r8d, %r9d
callq 0x3360
movq %rbx, %rdi
callq 0x5726
addq $0x20, %rsp
popq %rbx
popq %r14
popq %rbp
jmp 0x6fd5