// File: /workspace/github_cpp_makefile_star2_O2/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, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsi, 0x28(%rsp)
movq 0xd610(%rip), %rdi      # 0x14b50
leaq 0x4cf4(%rip), %rsi      # 0xc23b
callq 0x3100
movq %rax, %rbx
testq %rax, %rax
jne 0x756f
movq 0xaa5d(%rip), %rdi      # 0x11fb8
leaq 0x5170(%rip), %rsi      # 0xc6d2
callq 0x3310
movq %rax, %rdi
callq 0x3180
leaq 0x190(%rsp), %r14
pushq $0x40
popq %rbp
leaq 0x38(%rsp), %r15
leaq 0x68(%rsp), %r12
leaq 0xf0(%rsp), %r13
movq %rbx, %rdi
callq 0x34b0
testl %eax, %eax
jne 0x7648
xorps %xmm0, %xmm0
movaps %xmm0, 0x1c0(%rsp)
movaps %xmm0, 0x1b0(%rsp)
movaps %xmm0, 0x1a0(%rsp)
movaps %xmm0, 0x190(%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %rbx, %rdx
callq 0x34c0
testq %rax, %rax
je 0x758c
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x6efa
movq %r13, %rdi
movq %r15, %rsi
callq 0x7a81
movq %r15, %rdi
callq 0x3618
movq 0xf8(%rsp), %rsi
decq %rsi
movq %r13, %rdi
pushq $-0x1
popq %rdx
callq 0x30b0
movq %r12, %rdi
leaq 0x926(%rip), %rsi       # 0x7f38
movq %r13, %rdx
leaq 0x8(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq 0x7b24
leaq 0xd565(%rip), %rdi      # 0x14b90
movq %r12, %rsi
callq 0x7d62
movq %r12, %rdi
callq 0x7bba
movq %r13, %rdi
callq 0x3618
jmp 0x758c
movq %rbx, %rdi
callq 0x3430
pushq $0x10
popq %rdi
pushq $0x3
popq %rsi
pushq $0xf
popq %rdx
callq 0x30d0
testl %eax, %eax
js 0x7987
movl %eax, %r12d
leaq 0x190(%rsp), %r14
movl $0x10, (%r14)
callq 0x3480
movl %eax, 0x4(%r14)
movl $0x1, 0x8(%r14)
pushq $0xc
popq %rdx
movl %r12d, %edi
movq %r14, %rsi
callq 0x3060
testl %eax, %eax
js 0x79a4
pushq $0x1
popq %r15
movl %r12d, %ecx
shlq %cl, %r15
movl %r12d, %ebx
shrl $0x6, %ebx
leal 0x1(%r12), %eax
movl %eax, 0x4(%rsp)
leaq 0xf0(%rsp), %r14
cmpb $0x0, 0xd4e1(%rip)      # 0x14ba8
jne 0x7954
xorl %eax, %eax
cmpq $0x10, %rax
je 0x76e3
andq $0x0, 0xf0(%rsp,%rax,8)
incq %rax
jmp 0x76cf
orq %r15, 0xf0(%rsp,%rbx,8)
andq $0x0, 0x28(%rsp)
movq $0x186a0, 0x30(%rsp)    # imm = 0x186A0
movl 0x4(%rsp), %edi
movq %r14, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x28(%rsp), %r8
callq 0x32f0
testl %eax, %eax
jle 0x76c0
testq %r15, 0xf0(%rsp,%rbx,8)
je 0x76c0
movl $0x200, %edx            # imm = 0x200
leaq 0x190(%rsp), %r13
movq %r13, %rdi
xorl %esi, %esi
callq 0x31b0
movl $0x200, %edx            # imm = 0x200
movl %r12d, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x3110
testq %rax, %rax
jle 0x76c0
leaq 0xd0(%rsp), %r13
movq %r13, %rdi
leaq 0x190(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x6efa
movq %r12, %rbp
leaq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x7a81
movq %r13, %rdi
callq 0x3618
leaq 0x170(%rsp), %r12
movq %r12, %rdi
leaq 0x190(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x6efa
movq %r12, %rdi
leaq 0x4f35(%rip), %rsi      # 0xc6e0
xorl %edx, %edx
callq 0x3600
testq %rax, %rax
je 0x7842
movq %r12, %rdi
leaq 0x4f1f(%rip), %rsi      # 0xc6e4
xorl %edx, %edx
callq 0x3600
movq %rax, %r13
movq %r12, %rdi
callq 0x3618
testq %r13, %r13
movq %rbp, %r12
jne 0x7945
leaq 0x90(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3410
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
callq 0xb1a3
movl %eax, %r13d
movq %rbp, %rdi
callq 0x3618
testb %r13b, %r13b
je 0x7945
leaq 0x70(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3410
leaq 0x70(%rsp), %r13
movq %r13, %rdi
callq 0xb16f
movq %r13, %rdi
callq 0x3618
jmp 0x7945
movq %r12, %rdi
callq 0x3618
leaq 0xb0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3410
movq %rbp, %r12
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
callq 0xb1a3
movl %eax, %r13d
movq %rbp, %rdi
callq 0x3618
cmpq $0x0, 0x10(%rsp)
sete %al
orb %al, %r13b
jne 0x7945
leaq 0x38(%rsp), %rdi
leaq 0xd2d8(%rip), %rsi      # 0x14b70
leaq 0x8(%rsp), %rdx
callq 0x7bc7
movq 0x38(%rsp), %rdi
leaq 0x498d(%rip), %rsi      # 0xc23b
callq 0x3100
movq %rax, %r13
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
callq 0x3618
testq %r13, %r13
je 0x792a
movq %rbp, %rdi
pushq $0x8
popq %rsi
movq %r13, %rdx
callq 0x34c0
movq %rax, %r14
movq %r13, %rdi
callq 0x3430
testq %r14, %r14
leaq 0xf0(%rsp), %r14
je 0x7945
leaq 0x38(%rsp), %r13
movq %r13, %rdi
leaq 0x63b(%rip), %rsi       # 0x7f38
leaq 0x8(%rsp), %rdx
leaq 0x60(%rsp), %rcx
leaq 0x58(%rsp), %r8
callq 0x7b24
leaq 0xd278(%rip), %rdi      # 0x14b90
movq %r13, %rsi
callq 0x7d62
movq %r13, %rdi
callq 0x7bba
jmp 0x7945
movq 0xa687(%rip), %rdi      # 0x11fb8
leaq 0x4d9a(%rip), %rsi      # 0xc6d2
callq 0x3310
movq %rax, %rdi
callq 0x3180
leaq 0x8(%rsp), %rdi
callq 0x3618
jmp 0x76c0
callq 0xb2ba
testl %r12d, %r12d
je 0x7966
movl %r12d, %edi
callq 0x35f0
movq 0xd223(%rip), %rbx      # 0x14b90
movq 0xd224(%rip), %r14      # 0x14b98
cmpq %r14, %rbx
je 0x79e2
movq %rbx, %rdi
callq 0x3550
addq $0x8, %rbx
jmp 0x7974
movq 0xa62a(%rip), %rdi      # 0x11fb8
leaq 0x4d1a(%rip), %rsi      # 0xc6af
callq 0x3310
movq %rax, %rdi
callq 0x3180
jmp 0x79c7
movq 0xa60d(%rip), %rdi      # 0x11fb8
leaq 0x4d0f(%rip), %rsi      # 0xc6c1
callq 0x3310
movq %rax, %rdi
callq 0x3180
movl %r12d, %edi
callq 0x35f0
movq 0xa5ea(%rip), %rdi      # 0x11fb8
leaq 0x4ccf(%rip), %rsi      # 0xc6a4
callq 0x3310
movq %rax, %rdi
callq 0x3180
addq $0x398, %rsp            # imm = 0x398
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7bba
jmp 0x7a42
jmp 0x7a3f
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
jmp 0x7a27
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x7a27
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
callq 0x3618
jmp 0x7a42
jmp 0x7a76
jmp 0x7a3f
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x7a6f
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x7a6f
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x7bba
jmp 0x7a67
jmp 0x7a64
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
jmp 0x7a6f
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x3618
jmp 0x7a79
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3570