// File: /workspace/github_c_makefile_star2_O3/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 0x28e2
movl %edx, %eax
cmpl $0xff, %esi
ja 0x2865
cmpl $0xe, %esi
ja 0x28e2
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x97fd(%rip), %rdx      # 0xc060
jmp 0x289f
cmpl $0x1ff, %esi            # imm = 0x1FF
ja 0x2887
addl $0xffffff00, %esi       # imm = 0xFFFFFF00
cmpl $0xc, %esi
ja 0x28e2
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x989b(%rip), %rdx      # 0xc120
jmp 0x289f
addl $0xfffffe00, %esi       # imm = 0xFFFFFE00
cmpl $0x11, %esi
ja 0x28e2
movl %esi, %ecx
leaq (%rcx,%rcx,2), %rcx
leaq 0x9921(%rip), %rdx      # 0xc1c0
leaq (%rdx,%rcx,4), %rcx
movl (%rcx), %esi
decl %esi
cmpl $0x7, %esi
ja 0x28e2
movb 0x4(%rcx), %dl
movsbl %dl, %r9d
movl 0x8(%rcx), %ecx
leaq 0x9777(%rip), %r8       # 0xc034
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
cmpl $0x1, %eax
jne 0x298f
leaq 0x99d4(%rip), %rdx      # 0xc2aa
movl $0x9b, %esi
xorl %eax, %eax
jmp 0xaf11
retq
movq 0x128(%rdi), %rcx
testb $0x10, 0xac(%rcx)
je 0x2993
leaq 0x99ae(%rip), %rsi      # 0xc2ac
xorl %eax, %eax
jmp 0xae8d
movq 0x128(%rdi), %rdx
testb $0x1, 0xac(%rdx)
je 0x2993
movl %ecx, %edx
testl %eax, %eax
jne 0x294c
jmp 0x2934
testl %eax, %eax
jne 0x2948
movq 0x128(%rdi), %rcx
movzwl 0xac(%rcx), %ecx
andl $0x2, %ecx
je 0x2948
movsbl %dl, %ecx
leaq 0x9972(%rip), %rdx      # 0xc2b0
movl $0x8f, %esi
jmp 0x29f1
testl %eax, %eax
je 0x297e
incl %eax
movsbl %dl, %r8d
leaq 0x995a(%rip), %rdx      # 0xc2b3
movl $0x9b, %esi
movl %eax, %ecx
jmp 0x29da
testl %eax, %eax
je 0x29cb
incl %eax
leaq 0x998d(%rip), %rdx      # 0xc2fc
movl $0x9b, %esi
movl %eax, %r8d
xorl %eax, %eax
jmp 0xaf11
leaq 0x992b(%rip), %rdx      # 0xc2b0
movl $0x9b, %esi
movl %r9d, %ecx
jmp 0x29f1
testb $0x1, %al
jne 0x29e1
testb $0x5, %al
je 0x29ad
incl %eax
leaq 0x98f8(%rip), %rdx      # 0xc298
movl $0x9b, %esi
movl %r9d, %ecx
movl %eax, %r8d
jmp 0x29da
testb $0x2, %al
leaq 0x98fa(%rip), %rax      # 0xc2b0
leaq 0x98f2(%rip), %rsi      # 0xc2af
cmoveq %rax, %rsi
movl %r9d, %edx
xorl %eax, %eax
jmp 0xae8d
leaq 0x98e3(%rip), %rdx      # 0xc2b5
movl $0x9b, %esi
movl %r9d, %r8d
xorl %eax, %eax
jmp 0xaf11
incl %eax
leaq 0x98bc(%rip), %rdx      # 0xc2a6
movl $0x9b, %esi
movl %eax, %ecx
xorl %eax, %eax
jmp 0xaf11