// File: /workspace/github_cpp_makefile_star2_O3/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 0xd87b(%rip), %rdi      # 0x15b70
leaq 0x68f3(%rip), %rsi      # 0xebef
callq 0x3100
movq %rax, %rbx
testq %rax, %rax
jne 0x834b
movq 0xacb0(%rip), %r14      # 0x12fc0
leaq 0x62f3(%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 0x849c
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 0x848c
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 0x7dac
movq %r12, %rdi
movq %r15, %rsi
callq 0x8bae
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0x8407
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3390
movq 0x118(%rsp), %rdx
subq $0x1, %rdx
jb 0x8a89
movq %rdx, 0x118(%rsp)
movq 0x110(%rsp), %rax
movb $0x0, (%rax,%rdx)
movq %rbp, %rdi
leaq 0xc49(%rip), %rsi       # 0x9080
movq %r12, %rdx
leaq 0x8(%rsp), %rcx
leaq 0xb0(%rsp), %r8
callq 0x8c70
leaq 0xd75d(%rip), %rdi      # 0x15bb0
movq %rbp, %rsi
callq 0x8ee2
cmpq $0x0, 0x48(%rsp)
jne 0x8b7a
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x848c
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x3510
testl %eax, %eax
je 0x8386
movq %rbx, %rdi
callq 0x3480
movl $0x10, %edi
movl $0x3, %esi
movl $0xf, %edx
callq 0x30d0
testl %eax, %eax
js 0x89a8
movl %eax, %ebx
leaq 0x190(%rsp), %r14
movl $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 0x89ec
cmpb $0x0, 0xd6c8(%rip)      # 0x15bc8
movq %rbx, %r14
jne 0x8971
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 0x8964
testq %r15, 0x110(%rsp,%rbx,8)
je 0x8964
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 0x8964
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 0x7dac
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x8bae
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8664
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 0x7dac
movq %r14, %r12
movl $0x3, %ecx
movq %r13, %rdi
leaq 0x5f64(%rip), %rsi      # 0xe618
xorl %edx, %edx
callq 0x35c0
testq %rax, %rax
je 0x86e5
movl $0x6, %ecx
movq %r13, %rdi
leaq 0x5f4d(%rip), %rsi      # 0xe61c
xorl %edx, %edx
callq 0x35c0
cmpq $0x1, %rax
movl $0x0, %r14d
sbbl %r14d, %r14d
jmp 0x86eb
movl $0x1, %r14d
movq 0xf0(%rsp), %rdi
leaq 0x100(%rsp), %rax
cmpq %rax, %rdi
je 0x8710
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x3390
cmpl $-0x1, %r14d
je 0x8853
cmpl $0x1, %r14d
movq %r12, %r14
jne 0x8945
leaq 0xa0(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x90(%rsp), %rdi
callq 0x7e42
leaq 0x90(%rsp), %rdi
callq 0xcd94
movl %eax, %r13d
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x8786
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3390
cmpq $0x0, 0x10(%rsp)
sete %al
orb %al, %r13b
jne 0x8945
movq %rbp, %rdi
leaq 0xd3ee(%rip), %rsi      # 0x15b90
leaq 0x8(%rsp), %rdx
callq 0x8d0c
movq 0x28(%rsp), %rdi
leaq 0x6437(%rip), %rsi      # 0xebef
callq 0x3100
movq %rax, %r13
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x87dc
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x3390
testq %r13, %r13
je 0x88fb
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 0x8945
movq %rbp, %r12
movq %rbp, %rdi
leaq 0x867(%rip), %rsi       # 0x9080
leaq 0x8(%rsp), %rdx
leaq 0xe8(%rsp), %rcx
leaq 0xe0(%rsp), %r8
callq 0x8c70
leaq 0xd376(%rip), %rdi      # 0x15bb0
movq %r12, %rsi
callq 0x8ee2
cmpq $0x0, 0x28(%rsp)
je 0x8945
jmp 0x8b7a
leaq 0x80(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x70(%rsp), %rdi
callq 0x7e42
movq %r12, %r14
leaq 0x70(%rsp), %rdi
callq 0xcd94
movl %eax, %r13d
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x88a9
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x3390
testb %r13b, %r13b
je 0x8945
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0x7e42
leaq 0x50(%rsp), %rdi
callq 0xcd46
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x8945
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x8945
movl $0xd, %edx
movq 0xa6b9(%rip), %rdi      # 0x12fc0
leaq 0x5cfc(%rip), %rsi      # 0xe60a
callq 0x33d0
movq 0xa6a6(%rip), %rcx      # 0x12fc0
movq (%rcx), %rax
movq -0x18(%rax), %rdi
addq %rcx, %rdi
movl $0xa, %esi
callq 0x3340
movsbl %al, %esi
movq 0xa688(%rip), %rdi      # 0x12fc0
callq 0x3040
movq %rax, %rdi
callq 0x3230
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x8961
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x3390
movq %rbp, %r12
cmpb $0x1, 0xd25d(%rip)      # 0x15bc8
jne 0x8527
callq 0xcf1e
testl %r14d, %r14d
je 0x8983
movl %r14d, %edi
callq 0x3640
movq 0xd226(%rip), %rbx      # 0x15bb0
movq 0xd227(%rip), %r14      # 0x15bb8
cmpq %r14, %rbx
je 0x8a77
movq %rbx, %rdi
callq 0x35b0
addq $0x8, %rbx
jmp 0x8991
movq 0xa611(%rip), %rbx      # 0x12fc0
leaq 0x5c31(%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 0x8a35
movq 0xa5cd(%rip), %r14      # 0x12fc0
leaq 0x5bff(%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
movq 0xa584(%rip), %rbx      # 0x12fc0
leaq 0x5b4e(%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 0x5b20(%rip), %rdi      # 0xe5b0
leaq 0x5b05(%rip), %rsi      # 0xe59c
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x34f0
cmpq $0x0, 0x28(%rsp)
je 0x8b44
jmp 0x8b7a
jmp 0x8b44
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x8b47
movq 0x60(%rsp), %rsi
jmp 0x8b0e
movq %rax, %rbx
movq 0x70(%rsp), %rdi
leaq 0x80(%rsp), %rax
cmpq %rax, %rdi
je 0x8b47
movq 0x80(%rsp), %rsi
jmp 0x8b0e
movq %rax, %rbx
movq 0x90(%rsp), %rdi
leaq 0xa0(%rsp), %rax
cmpq %rax, %rdi
je 0x8b47
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x3390
jmp 0x8b47
movq %rax, %rbx
jmp 0x8ba6
jmp 0x8b44
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x8ba6
movq 0xd0(%rsp), %rsi
jmp 0x8b9e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x8ba6
movq 0x18(%rsp), %rsi
jmp 0x8b9e
movq %rax, %rbx
movq 0x28(%rsp), %rdi
movq %r13, (%rsp)
cmpq %r13, %rdi
jne 0x8b97
jmp 0x8ba6
jmp 0x8b7f
cmpq $0x0, 0x48(%rsp)
je 0x8b7f
callq 0x30f0
movq %rax, %rbx
movq 0x110(%rsp), %rdi
leaq 0x120(%rsp), %rax
cmpq %rax, %rdi
je 0x8ba6
movq (%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x3390
movq %rbx, %rdi
callq 0x35e0