// File: /workspace/github_c_makefile_star2_O1/neovim[P]libvterm/src/keyboard.c
// Function: vterm_keyboard_key
void vterm_keyboard_key(VTerm *vt, VTermKey key, VTermModifier mod)
{
  if(key == VTERM_KEY_NONE)
    return;

  keycodes_s k;
  if(key < VTERM_KEY_FUNCTION_0) {
    if(key >= sizeof(keycodes)/sizeof(keycodes[0]))
      return;
    k = keycodes[key];
  }
  else if(key >= VTERM_KEY_FUNCTION_0 && key <= VTERM_KEY_FUNCTION_MAX) {
    if((key - VTERM_KEY_FUNCTION_0) >= sizeof(keycodes_fn)/sizeof(keycodes_fn[0]))
      return;
    k = keycodes_fn[key - VTERM_KEY_FUNCTION_0];
  }
  else if(key >= VTERM_KEY_KP_0) {
    if((key - VTERM_KEY_KP_0) >= sizeof(keycodes_kp)/sizeof(keycodes_kp[0]))
      return;
    k = keycodes_kp[key - VTERM_KEY_KP_0];
  }

  switch(k.type) {
  case KEYCODE_NONE:
    break;

  case KEYCODE_TAB:
    /* Shift-Tab is CSI Z but plain Tab is 0x09 */
    if(mod == VTERM_MOD_SHIFT)
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "Z");
    else if(mod & VTERM_MOD_SHIFT)
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "1;%dZ", mod+1);
    else
      goto case_LITERAL;
    break;

  case KEYCODE_ENTER:
    /* Enter is CRLF in newline mode, but just LF in linefeed */
    if(vt->state->mode.newline)
      vterm_push_output_sprintf(vt, "\r\n");
    else
      goto case_LITERAL;
    break;

  case KEYCODE_LITERAL: case_LITERAL:
    if(mod & (VTERM_MOD_SHIFT|VTERM_MOD_CTRL))
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d;%du", k.literal, mod+1);
    else
      vterm_push_output_sprintf(vt, mod & VTERM_MOD_ALT ? ESC_S "%c" : "%c", k.literal);
    break;

  case KEYCODE_SS3: case_SS3:
    if(mod == 0)
      vterm_push_output_sprintf_ctrl(vt, C1_SS3, "%c", k.literal);
    else
      goto case_CSI;
    break;

  case KEYCODE_CSI: case_CSI:
    if(mod == 0)
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%c", k.literal);
    else
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "1;%d%c", mod + 1, k.literal);
    break;

  case KEYCODE_CSINUM:
    if(mod == 0)
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d%c", k.csinum, k.literal);
    else
      vterm_push_output_sprintf_ctrl(vt, C1_CSI, "%d;%d%c", k.csinum, mod + 1, k.literal);
    break;

  case KEYCODE_CSI_CURSOR:
    if(vt->state->mode.cursor)
      goto case_SS3;
    else
      goto case_CSI;

  case KEYCODE_KEYPAD:
    if(vt->state->mode.keypad) {
      k.literal = k.csinum;
      goto case_SS3;
    }
    else
      goto case_LITERAL;
  }
}

// Assembly:
testl %esi, %esi
je 0x2914
movl %edx, %eax
cmpl $0xff, %esi
ja 0x2897
cmpl $0xe, %esi
ja 0x2914
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x87cb(%rip), %rdx      # 0xb060
jmp 0x28d1
cmpl $0x1ff, %esi            # imm = 0x1FF
ja 0x28b9
addl $0xffffff00, %esi       # imm = 0xFFFFFF00
cmpl $0xc, %esi
ja 0x2914
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x8869(%rip), %rdx      # 0xb120
jmp 0x28d1
addl $0xfffffe00, %esi       # imm = 0xFFFFFE00
cmpl $0x11, %esi
ja 0x2914
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x88ef(%rip), %rdx      # 0xb1c0
leaq (%rdx,%rcx,4), %rcx
movl (%rcx), %esi
decl %esi
cmpl $0x7, %esi
ja 0x2914
movb 0x4(%rcx), %dl
movsbl %dl, %r9d
movl 0x8(%rcx), %ecx
leaq 0x8749(%rip), %r8       # 0xb038
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
cmpl $0x1, %eax
jne 0x29c1
leaq 0x89a2(%rip), %rdx      # 0xb2aa
movl $0x9b, %esi
xorl %eax, %eax
jmp 0xa5f7
retq
movq 0x128(%rdi), %rcx
testb $0x10, 0xac(%rcx)
je 0x29c5
leaq 0x897c(%rip), %rsi      # 0xb2ac
xorl %eax, %eax
jmp 0xa573
movq 0x128(%rdi), %rdx
testb $0x1, 0xac(%rdx)
je 0x29c5
movl %ecx, %edx
testl %eax, %eax
jne 0x297a
jmp 0x2966
testl %eax, %eax
jne 0x297a
movq 0x128(%rdi), %rcx
movzwl 0xac(%rcx), %ecx
andl $0x2, %ecx
je 0x297a
movsbl %dl, %ecx
leaq 0x8940(%rip), %rdx      # 0xb2b0
movl $0x8f, %esi
jmp 0x2a23
testl %eax, %eax
je 0x29b0
incl %eax
movsbl %dl, %r8d
leaq 0x8928(%rip), %rdx      # 0xb2b3
movl $0x9b, %esi
movl %eax, %ecx
jmp 0x2a0c
testl %eax, %eax
je 0x29fd
incl %eax
leaq 0x895b(%rip), %rdx      # 0xb2fc
movl $0x9b, %esi
movl %eax, %r8d
xorl %eax, %eax
jmp 0xa5f7
movsbl %dl, %ecx
leaq 0x88f6(%rip), %rdx      # 0xb2b0
movl $0x9b, %esi
jmp 0x2a23
testb $0x1, %al
jne 0x2a13
testb $0x5, %al
je 0x29df
incl %eax
leaq 0x88c6(%rip), %rdx      # 0xb298
movl $0x9b, %esi
movl %r9d, %ecx
movl %eax, %r8d
jmp 0x2a0c
testb $0x2, %al
leaq 0x88c8(%rip), %rax      # 0xb2b0
leaq 0x88c0(%rip), %rsi      # 0xb2af
cmoveq %rax, %rsi
movl %r9d, %edx
xorl %eax, %eax
jmp 0xa573
leaq 0x88b1(%rip), %rdx      # 0xb2b5
movl $0x9b, %esi
movl %r9d, %r8d
xorl %eax, %eax
jmp 0xa5f7
incl %eax
leaq 0x888a(%rip), %rdx      # 0xb2a6
movl $0x9b, %esi
movl %eax, %ecx
xorl %eax, %eax
jmp 0xa5f7