// File: fulhax[P]ncpamixer/src/pa.cpp
// Function: Pa::pa_connect()
bool Pa::pa_connect()
{
    if (pa_ctx) {
        return false;
    }

    pa_threaded_mainloop_lock(pa_ml);

    pa_proplist *proplist = pa_proplist_new();
    pa_proplist_sets(proplist, PA_PROP_APPLICATION_NAME, "ncpamixer");
    pa_proplist_sets(proplist, PA_PROP_APPLICATION_ID, "ncpamixer");
    pa_proplist_sets(proplist, PA_PROP_APPLICATION_ICON_NAME, "audio-card");
    pa_ctx = pa_context_new_with_proplist(pa_api, NULL, proplist);
    pa_proplist_free(proplist);

    if (!pa_ctx) {
        fprintf(stderr, "Unable to create PA context.\n");
        pa_threaded_mainloop_unlock(pa_ml);
        pa_ctx = nullptr;
        exit(EXIT_FAILURE);
        return false;
    }

    pa_context_set_state_callback(pa_ctx, &Pa::ctx_state_cb, this);
    pa_context_set_subscribe_callback(pa_ctx, &Pa::subscribe_cb, this);

    if (pa_context_connect(pa_ctx, NULL, PA_CONTEXT_NOFAIL, NULL) < 0) {
        pa_context_disconnect(pa_ctx);
        pa_context_unref(pa_ctx); // Tror ej den behövs?
        pa_threaded_mainloop_unlock(pa_ml);
        pa_ctx = nullptr;

        connectionMtx.unlock();
        reconnect();
        return false;
    }

    pa_threaded_mainloop_unlock(pa_ml);

    connectionMtx.unlock();
    return true;

}

// Assembly:
subq $0x28, %rsp
movq %rdi, 0x18(%rsp)
movq 0x18(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x150(%rax)
je 0xad07
movb $0x0, 0x27(%rsp)
jmp 0xaec8
movq 0x8(%rsp), %rax
movq 0x158(%rax), %rdi
callq 0x70e0
callq 0x7240
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rdi
leaq 0x15566(%rip), %rsi     # 0x20294
leaq 0x15570(%rip), %rdx     # 0x202a5
callq 0x7c90
movq 0x10(%rsp), %rdi
leaq 0x15569(%rip), %rsi     # 0x202af
leaq 0x15558(%rip), %rdx     # 0x202a5
callq 0x7c90
movq 0x10(%rsp), %rdi
leaq 0x15560(%rip), %rsi     # 0x202be
leaq 0x1556f(%rip), %rdx     # 0x202d4
callq 0x7c90
movq 0x8(%rsp), %rax
movq 0x160(%rax), %rdi
movq 0x10(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x7940
movq %rax, %rcx
movq 0x8(%rsp), %rax
movq %rcx, 0x150(%rax)
movq 0x10(%rsp), %rdi
callq 0x78c0
movq 0x8(%rsp), %rax
cmpq $0x0, 0x150(%rax)
jne 0xadef
movq 0x201f5(%rip), %rax     # 0x2afa8
movq (%rax), %rdi
leaq 0x15522(%rip), %rsi     # 0x202df
movb $0x0, %al
callq 0x79b0
movq 0x8(%rsp), %rax
movq 0x158(%rax), %rdi
callq 0x71c0
movq 0x8(%rsp), %rax
movq $0x0, 0x150(%rax)
movl $0x1, %edi
callq 0x7840
movq 0x8(%rsp), %rdx
movq 0x150(%rdx), %rdi
leaq 0x16e(%rip), %rsi       # 0xaf70
callq 0x7c00
movq 0x8(%rsp), %rdx
movq 0x150(%rdx), %rdi
leaq 0x286(%rip), %rsi       # 0xb0a0
callq 0x7030
movq 0x8(%rsp), %rax
movq 0x150(%rax), %rdi
xorl %eax, %eax
movl %eax, %ecx
movl $0x2, %edx
movq %rcx, %rsi
callq 0x7d30
cmpl $0x0, %eax
jge 0xaea1
movq 0x8(%rsp), %rax
movq 0x150(%rax), %rdi
callq 0x7c70
movq 0x8(%rsp), %rax
movq 0x150(%rax), %rdi
callq 0x7a40
movq 0x8(%rsp), %rax
movq 0x158(%rax), %rdi
callq 0x71c0
movq 0x8(%rsp), %rdi
movq $0x0, 0x150(%rdi)
addq $0x128, %rdi            # imm = 0x128
callq 0xd170
movq 0x8(%rsp), %rdi
callq 0xaee0
movb $0x0, 0x27(%rsp)
jmp 0xaec8
movq 0x8(%rsp), %rax
movq 0x158(%rax), %rdi
callq 0x71c0
movq 0x8(%rsp), %rdi
addq $0x128, %rdi            # imm = 0x128
callq 0xd170
movb $0x1, 0x27(%rsp)
movb 0x27(%rsp), %al
andb $0x1, %al
addq $0x28, %rsp
retq
nopw %cs:(%rax,%rax)