// File: fgheng[P]keysound/src/KeyDetect.cc
// Function: key_detect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, Audio*, Mixer*)
void key_detect(std::string str_event_id, Audio *audio, Mixer *mixer) {
    input_event ie;
    // int tid = std::stoi(event_id);

    int fd;
    fd_set fds;
    struct timeval tv;

    // 如果使用suid的话，不需要sleep即可正常读取/dev/input/event文件				If you use suid, you can read /dev/input/event files normally without using sleep
    // 如果使用input组的方式，那么需要加上sleep，否则新插入的键盘会读取失败			If you use the "input" group method, you need to add sleep, otherwise the newly inserted keyboard will fail to read
    // 不清楚是为什么，是因为文件创建的慢了？还是权限不够了？					Not sure why, is it because the file creation is slow? Still not enough permissions?
    // 也不是文件不存在的问题，我尝试判断文件是否存在，文件是存在的				It's not because the file doesn't exist, I tried to see if it does and it does in fact exist.
    // 那是因为权限没有跟上？尝试了一下确实是不可读，这是为什么呢？因为权限管理的延迟？		Is that because the permissions didn't carry over? After one  try, it becomes  unreadable. Why is this? Because
    // 使用open的阻塞方式 O_NONBLOCK 打开也不行							of the delay in rights management?
    // add_event_id(str_event_id);								Use O_NONBLOCK method when opening
    // while (event_id_exists(str_event_id) && access((input_file_header + str_event_id).c_str(), R_OK) == -1) {
        // usleep(100 * 1000);
        // std::cout << "判断是否有权限" << std::endl;
    // }
    // 经过搜索，我找到了原因，是因为程序读取太快，在udev正确设置权限之前就读取了		After searching, I found the reason, because the program reads too fast, it reads before udev sets the permissions correctly
    // 有两个解决方案，一个是等待一段时间，比如sleep(1)						There are two solutions, one is to wait for a period of time, such as with  sleep(1)
    // 第二个是使用inotify监控文件的权限更改							The second choice is to use "inotify" to monitor file permission changes
    sleep(1);
    fd = open((input_file_header + str_event_id).c_str(), O_RDONLY);

    if (fd == -1) {
        std::cout << "Error occured while attempting to open event file: " << input_file_header + str_event_id;
        std::cout << " you may not have access to the file " << std::endl;
        return;
    }

    // 成功运行，添加到key_detect_threads中		It has successfully run, add to key_detect_threads
    add_event_id(str_event_id);
    while(event_id_exists(str_event_id)) {
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        tv.tv_sec = 0;
        tv.tv_usec = 100 * 1000;

        int ret = select(fd+1, &fds, NULL, NULL, &tv);

        if (ret < 0 ) continue;
        if (!(ret > 0 && FD_ISSET(fd, &fds))) continue;

        ssize_t num = read(fd, (void *)&ie, sizeof(struct input_event));

        if (num == -1 || num != sizeof(struct input_event)) continue;

        // 键盘事件	Keyboard events
        if (ie.type == EV_KEY) {
            switch (ie.value) {
                case KEY_PRESS:
                {
                    WAV_DATA wd = audio->get_wav_by_code(ie.code);
                    std::cout << " [" << ie.code << "] is pressed " << std::endl;
                    mixer->mix(wd.data, wd.len, wd.bits_per_sample);
                    break;
                }
                case KEY_RELEASE:
                {
                    // std::cout << "some key release" << std::endl;
                    break;
                }
                case KEY_REPEAT:
                {
                    // std::cout << "some key repeat" << std::endl;
                    break;
                }
                default: break;
            }
        }
    }

    if (fd>0) close(fd);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x1a0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x1, %edi
callq 0x42a0
movq -0x1a0(%rbp), %rdx
leaq 0x147aa(%rip), %rsi     # 0x21c40
leaq -0xe8(%rbp), %rdi
movq %rdi, -0x198(%rbp)
callq 0xbdf0
movq -0x198(%rbp), %rdi
callq 0x4170
movq %rax, %rdi
xorl %esi, %esi
movb %sil, %al
callq 0x4280
movl %eax, -0x18c(%rbp)
jmp 0xd4ca
movl -0x18c(%rbp), %eax
movl %eax, -0x34(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x4728
cmpl $-0x1, -0x34(%rbp)
jne 0xd5b2
movq 0x11ac8(%rip), %rdi     # 0x1efb8
leaq 0x92a4(%rip), %rsi      # 0x1679b
callq 0x43a0
movq -0x1a0(%rbp), %rdx
movq %rax, -0x1b0(%rbp)
leaq 0x1472f(%rip), %rsi     # 0x21c40
leaq -0x118(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0xbdf0
movq -0x1b0(%rbp), %rdi
movq -0x1a8(%rbp), %rsi
callq 0x4310
jmp 0xd539
leaq -0x118(%rbp), %rdi
callq 0x4728
movq 0x11a6c(%rip), %rdi     # 0x1efb8
leaq 0x927c(%rip), %rsi      # 0x167cf
callq 0x43a0
movq %rax, %rdi
movq 0x11a26(%rip), %rsi     # 0x1ef88
callq 0x4400
jmp 0xd893
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf0(%rbp)
movl %eax, -0xf4(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x4728
jmp 0xd89c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf0(%rbp)
movl %eax, -0xf4(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x4728
jmp 0xd89c
movq -0x1a0(%rbp), %rsi
leaq -0x138(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x4180
movq -0x1b8(%rbp), %rdi
callq 0x141a0
jmp 0xd5da
leaq -0x138(%rbp), %rdi
callq 0x4728
movq -0x1a0(%rbp), %rsi
leaq -0x158(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x4180
movq -0x1c8(%rbp), %rdi
callq 0x14230
movb %al, -0x1b9(%rbp)
jmp 0xd614
leaq -0x158(%rbp), %rdi
callq 0x4728
movb -0x1b9(%rbp), %al
testb $0x1, %al
jne 0xd62f
jmp 0xd885
jmp 0xd631
leaq -0xb8(%rbp), %rax
movq %rax, -0x168(%rbp)
movl $0x0, -0x15c(%rbp)
movl -0x15c(%rbp), %eax
cmpq $0x10, %rax
jae 0xd6c1
movq -0x168(%rbp), %rax
movl -0x15c(%rbp), %ecx
movq $0x0, (%rax,%rcx,8)
movl -0x15c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x15c(%rbp)
jmp 0xd649
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf0(%rbp)
movl %eax, -0xf4(%rbp)
leaq -0x138(%rbp), %rdi
callq 0x4728
jmp 0xd89c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xf0(%rbp)
movl %eax, -0xf4(%rbp)
leaq -0x158(%rbp), %rdi
callq 0x4728
jmp 0xd89c
jmp 0xd6c3
movl -0x34(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x34(%rbp), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq -0xb8(%rbp,%rax,8), %rcx
movq %rcx, -0xb8(%rbp,%rax,8)
movq $0x0, -0xc8(%rbp)
movq $0x186a0, -0xc0(%rbp) # imm = 0x186A0
movl -0x34(%rbp), %edi
addl $0x1, %edi
leaq -0xb8(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0xc8(%rbp), %r8
movq %rcx, %rdx
callq 0x4360
movl %eax, -0x16c(%rbp)
cmpl $0x0, -0x16c(%rbp)
jge 0xd744
jmp 0xd5e6
cmpl $0x0, -0x16c(%rbp)
jle 0xd791
movl -0x34(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq -0xb8(%rbp,%rax,8), %rax
movq %rax, -0x1d0(%rbp)
movl -0x34(%rbp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq -0x1d0(%rbp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
jne 0xd796
jmp 0xd5e6
movl -0x34(%rbp), %edi
leaq -0x30(%rbp), %rsi
movl $0x18, %edx
callq 0x4530
movq %rax, -0x178(%rbp)
cmpq $-0x1, -0x178(%rbp)
je 0xd7c2
cmpq $0x18, -0x178(%rbp)
je 0xd7c7
jmp 0xd5e6
movzwl -0x20(%rbp), %eax
cmpl $0x1, %eax
jne 0xd880
movl -0x1c(%rbp), %eax
movl %eax, -0x1d4(%rbp)
testl %eax, %eax
je 0xd878
jmp 0xd7e7
movl -0x1d4(%rbp), %eax
subl $0x1, %eax
je 0xd801
jmp 0xd7f4
movl -0x1d4(%rbp), %eax
subl $0x2, %eax
je 0xd87a
jmp 0xd87c
movq -0x10(%rbp), %rdi
movzwl -0x1e(%rbp), %esi
callq 0x88e0
movq %rax, -0x188(%rbp)
movq %rdx, -0x180(%rbp)
movq 0x11795(%rip), %rdi     # 0x1efb8
leaq 0x8fcb(%rip), %rsi      # 0x167f5
callq 0x43a0
movq %rax, %rdi
movzwl -0x1e(%rbp), %esi
callq 0x4630
movq %rax, %rdi
leaq 0x8fb3(%rip), %rsi      # 0x167f8
callq 0x43a0
movq %rax, %rdi
movq 0x11734(%rip), %rsi     # 0x1ef88
callq 0x4400
movq -0x18(%rbp), %rdi
movq -0x188(%rbp), %rsi
movl -0x180(%rbp), %edx
movzwl -0x17c(%rbp), %ecx
callq 0xd960
jmp 0xd87e
jmp 0xd87e
jmp 0xd87e
jmp 0xd87e
jmp 0xd880
jmp 0xd5e6
cmpl $0x0, -0x34(%rbp)
jle 0xd893
movl -0x34(%rbp), %edi
callq 0x46f0
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0xf0(%rbp), %rdi
callq 0x4670
nopl (%rax,%rax)