// File: /workspace/github_cpp_makefile_star2_O1/fgheng[P]keysound/src/DeviceDetect.cc
// Function: device_detect(Audio*, Mixer*)
void device_detect(Audio *audio, Mixer *mixer) {
    // 首先启动已有的设备	Start the existing equipment first
    start_exists_device(audio, mixer);
    fd_set fds;
    struct timeval tv;

    int sock_fd = init_socket();
    if (sock_fd < 0) {
        std::cout << "init error" << std::endl;
        return;
    }

    char buf[UEVENT_BUFFER_SIZE];

    // while的条件要更改，本身监控是一个线程，所以要安全退出
    // The condition of while needs to be changed, the monitoring itself is a thread,
    // so it is necessary to exit safely
    while (detect) {
        FD_ZERO(&fds);
        FD_SET(sock_fd, &fds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;

        int ret = select(sock_fd + 1, &fds, NULL, NULL, &tv);
        if (ret < 0) continue;
        if (!(ret > 0 && FD_ISSET(sock_fd, &fds))) continue;

        std::memset(buf, '\0', UEVENT_BUFFER_SIZE);
        if (recv(sock_fd, buf, UEVENT_BUFFER_SIZE, 0) > 0) {
            std::string str_event_id = get_event_id(buf);
            // int int_event_id = atoi(str_event_id.c_str());

            // 判断状态，增加设备还是删除设备
	    // Determine the status, add or delete equipment
            int state = device_state(buf);
            switch (state) {
                case 1:
                {
                    // 若是增加设备		If you add equipment
                    // 判断设备线程是否已经存在 Determine whether the device thread already exists
                    if (event_id_exists(str_event_id)) break;
                    else {
                        // 判断是不是键盘设备	Determine if it is a keyboard device
                        if (is_keyboard(str_event_id)) {
                            // 新建线程		Create new thread
                            all_threads.push_back(std::thread(key_detect, str_event_id, audio, mixer));
                        }
                    }
                    break;
                }
                case -1:
                {
                    // 删除					Delete
                    // 判断是否已经存在于key_detect_threads中	Determine whether it already exists in key_detect_threads
                    if (event_id_exists(str_event_id)) {
                        del_event_id(str_event_id);
                    }
                    break;
                }
                default:
                    break;

            }
        }
    }

    clear_all_key_detect_threads();
    if (sock_fd > 0) close(sock_fd);

    // 等待子线程结束	Wait for the child thread
    for (auto &th: all_threads) {
        th.join();
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x398, %rsp            # imm = 0x398
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsi, 0xb0(%rsp)
movq 0xd81f(%rip), %rdi      # 0x15b70
leaq 0x68a7(%rip), %rsi      # 0xebff
callq 0x3100
movq %rax, %rbx
testq %rax, %rax
jne 0x83a7
movq 0xac54(%rip), %r14      # 0x12fc0
leaq 0x6297(%rip), %rsi      # 0xe60a
movl $0xd, %edx
movq %r14, %rdi
callq 0x33d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
movq %rbx, %rdi
callq 0x3510
testl %eax, %eax
jne 0x84fb
leaq 0x38(%rsp), %r13
leaq 0x120(%rsp), %rax
movq %rax, (%rsp)
leaq 0x190(%rsp), %r14
leaq 0x28(%rsp), %r15
leaq 0x110(%rsp), %r12
leaq 0x48(%rsp), %rbp
xorps %xmm0, %xmm0
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x190(%rsp)
movq %r14, %rdi
movl $0x40, %esi
movq %rbx, %rdx
callq 0x3520
testq %rax, %rax
je 0x84eb
movq %r13, 0x28(%rsp)
movq %r14, %rdi
callq 0x3170
leaq (%rsp,%rax), %rdx
addq $0x190, %rdx            # imm = 0x190
movq %r15, %rdi
movq %r14, %rsi
callq 0x7e16
movq %r12, %rdi
movq %r15, %rsi
callq 0x8c21
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x8463
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3390
movq 0x118(%rsp), %rcx
movq %rcx, %rdx
subq $0x1, %rdx
jb 0x8afe
movq %rdx, 0x118(%rsp)
movq 0x110(%rsp), %rax
movb $0x0, (%rax,%rdx)
movq %rbp, %rdi
leaq 0xc6e(%rip), %rsi       # 0x9104
movq %r12, %rdx
leaq 0x8(%rsp), %rcx
leaq 0xb0(%rsp), %r8
callq 0x8ce4
leaq 0xd6fe(%rip), %rdi      # 0x15bb0
movq %rbp, %rsi
callq 0x8f66
cmpq $0x0, 0x48(%rsp)
jne 0x8bed
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x84eb
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x3510
testl %eax, %eax
je 0x83e2
movq %rbx, %rdi
callq 0x3480
movl $0x10, %edi
movl $0x3, %esi
movl $0xf, %edx
callq 0x30d0
testl %eax, %eax
js 0x8566
movl %eax, %ebx
leaq 0x190(%rsp), %r14
movq $0x0, (%r14)
movl $0x0, 0x8(%r14)
movw $0x10, (%r14)
callq 0x34d0
movl %eax, 0x4(%r14)
movl $0x1, 0x8(%r14)
movl %ebx, %edi
movq %r14, %rsi
movl $0xc, %edx
callq 0x3070
testl %eax, %eax
js 0x85aa
movq %rbx, %r14
jmp 0x85f9
movq 0xaa53(%rip), %rbx      # 0x12fc0
leaq 0x6073(%rip), %rsi      # 0xe5e7
movl $0x11, %edx
movq %rbx, %rdi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
jmp 0x85f3
movq 0xaa0f(%rip), %r14      # 0x12fc0
leaq 0x6041(%rip), %rsi      # 0xe5f9
movl $0x1e, %edx
movq %r14, %rdi
callq 0x33d0
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %r14, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
movl %ebx, %edi
callq 0x3640
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testl %r14d, %r14d
js 0x8aaa
cmpb $0x0, 0xd5bf(%rip)      # 0x15bc8
jne 0x8a77
movl $0x1, %r15d
movl %r14d, %ecx
shlq %cl, %r15
movl %r14d, %ebx
shrl $0x6, %ebx
leal 0x1(%r14), %eax
movl %eax, (%rsp)
leaq 0x28(%rsp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x180(%rsp)
movaps %xmm0, 0x170(%rsp)
movaps %xmm0, 0x160(%rsp)
movaps %xmm0, 0x150(%rsp)
movaps %xmm0, 0x140(%rsp)
movaps %xmm0, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movaps %xmm0, 0x110(%rsp)
orq %r15, 0x110(%rsp,%rbx,8)
movq $0x0, 0xb0(%rsp)
movq $0x186a0, 0xb8(%rsp) # imm = 0x186A0
movl (%rsp), %edi
leaq 0x110(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0xb0(%rsp), %r8
callq 0x3330
testl %eax, %eax
jle 0x8a6a
testq %r15, 0x110(%rsp,%rbx,8)
je 0x8a6a
movl $0x200, %edx            # imm = 0x200
movq %r12, %rbp
leaq 0x190(%rsp), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x31d0
movl $0x200, %edx            # imm = 0x200
movl %r14d, %edi
movq %r12, %rsi
movq %rbp, %r12
xorl %ecx, %ecx
callq 0x3120
testq %rax, %rax
jle 0x8a6a
leaq 0xd0(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x190(%rsp), %r12
movq %r12, %rdi
callq 0x3170
leaq (%rsp,%rax), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xc0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x7e16
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x8c21
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x876a
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x3390
leaq 0x100(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x190(%rsp), %r12
movq %r12, %rdi
callq 0x3170
leaq (%rsp,%rax), %rdx
addq $0x190, %rdx            # imm = 0x190
leaq 0xf0(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x7e16
movq %r14, %r12
movl $0x3, %ecx
movq %r13, %rdi
leaq 0x5e5e(%rip), %rsi      # 0xe618
xorl %edx, %edx
callq 0x35c0
testq %rax, %rax
je 0x87eb
movl $0x6, %ecx
movq %r13, %rdi
leaq 0x5e47(%rip), %rsi      # 0xe61c
xorl %edx, %edx
callq 0x35c0
cmpq $0x1, %rax
movl $0x0, %r14d
sbbl %r14d, %r14d
jmp 0x87f1
movl $0x1, %r14d
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x8816
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x3390
cmpl $-0x1, %r14d
je 0x8959
cmpl $0x1, %r14d
movq %r12, %r14
jne 0x8a4b
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x7ea6
leaq 0x90(%rsp), %rdi
callq 0xcf48
movl %eax, %r13d
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x888c
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3390
cmpq $0x0, 0x10(%rsp)
sete %al
orb %al, %r13b
jne 0x8a4b
movq %rbp, %rdi
leaq 0xd2e8(%rip), %rsi      # 0x15b90
leaq 0x8(%rsp), %rdx
callq 0x8d90
movq 0x28(%rsp), %rdi
leaq 0x6341(%rip), %rsi      # 0xebff
callq 0x3100
movq %rax, %r13
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x88e2
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3390
testq %r13, %r13
je 0x8a01
movq %rbp, %rdi
movl $0x8, %esi
movq %r13, %rdx
callq 0x3520
movq %rax, %r14
movq %r13, %rdi
callq 0x3480
testq %r14, %r14
movq %r12, %r14
je 0x8a4b
movq %rbp, %r12
movq %rbp, %rdi
leaq 0x7e5(%rip), %rsi       # 0x9104
leaq 0x8(%rsp), %rdx
leaq 0xe8(%rsp), %rcx
leaq 0xe0(%rsp), %r8
callq 0x8ce4
leaq 0xd270(%rip), %rdi      # 0x15bb0
movq %r12, %rsi
callq 0x8f66
cmpq $0x0, 0x28(%rsp)
je 0x8a4b
jmp 0x8bed
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x7ea6
movq %r12, %r14
leaq 0x70(%rsp), %rdi
callq 0xcf48
movl %eax, %r13d
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x89af
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x3390
testb %r13b, %r13b
je 0x8a4b
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x7ea6
leaq 0x50(%rsp), %rdi
callq 0xcefa
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x8a4b
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x8a4b
movl $0xd, %edx
movq 0xa5b3(%rip), %rdi      # 0x12fc0
leaq 0x5bf6(%rip), %rsi      # 0xe60a
callq 0x33d0
movq 0xa5a0(%rip), %rcx      # 0x12fc0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq 0xa582(%rip), %rdi      # 0x12fc0
callq 0x3040
movq %rax, %rdi
callq 0x3230
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x8a67
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbp, %r12
cmpb $0x1, 0xd157(%rip)      # 0x15bc8
jne 0x862d
callq 0xd0d2
testl %r14d, %r14d
je 0x8a89
movl %r14d, %edi
callq 0x3640
movq 0xd120(%rip), %rbx      # 0x15bb0
movq 0xd121(%rip), %r14      # 0x15bb8
cmpq %r14, %rbx
je 0x8aec
movq %rbx, %rdi
callq 0x35b0
addq $0x8, %rbx
jmp 0x8a97
movq 0xa50f(%rip), %rbx      # 0x12fc0
leaq 0x5ad9(%rip), %rsi      # 0xe591
movl $0xa, %edx
movq %rbx, %rdi
callq 0x33d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3040
movq %rax, %rdi
callq 0x3230
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5aab(%rip), %rdi      # 0xe5b0
leaq 0x5a90(%rip), %rsi      # 0xe59c
xorl %eax, %eax
callq 0x34f0
cmpq $0x0, 0x28(%rsp)
je 0x8bb7
jmp 0x8bed
jmp 0x8bb7
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x8bba
movq 0x60(%rsp), %rsi
jmp 0x8b81
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x8bba
movq 0x80(%rsp), %rsi
jmp 0x8b81
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x8bba
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x8bba
movq %rax, %rbx
jmp 0x8c19
jmp 0x8bb7
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8c19
movq 0xd0(%rsp), %rsi
jmp 0x8c11
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x8c19
movq 0x18(%rsp), %rsi
jmp 0x8c11
movq %rax, %rbx
movq 0x28(%rsp), %rdi
movq %r13, (%rsp)
cmpq %r13, %rdi
jne 0x8c0a
jmp 0x8c19
jmp 0x8bf2
cmpq $0x0, 0x48(%rsp)
je 0x8bf2
callq 0x30f0
movq %rax, %rbx
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x8c19
movq (%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x35e0