// File: /workspace/github_c_makefile_star2_O2/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 0x2804
movl %edx, %eax
cmpl $0xff, %esi
ja 0x2788
cmpl $0xe, %esi
ja 0x2804
movl %esi, %ecx
imulq $0xc, %rcx, %rdx
leaq 0x78da(%rip), %rcx      # 0xa060
jmp 0x27c2
cmpl $0x1ff, %esi            # imm = 0x1FF
ja 0x27aa
addl $0xffffff00, %esi       # imm = 0xFFFFFF00
cmpl $0xc, %esi
ja 0x2804
movl %esi, %ecx
imulq $0xc, %rcx, %rdx
leaq 0x7978(%rip), %rcx      # 0xa120
jmp 0x27c2
addl $0xfffffe00, %esi       # imm = 0xFFFFFE00
cmpl $0x11, %esi
ja 0x2804
movl %esi, %ecx
imulq $0xc, %rcx, %rdx
leaq 0x79fe(%rip), %rcx      # 0xa1c0
addq %rdx, %rcx
movl (%rcx), %esi
decl %esi
cmpl $0x7, %esi
ja 0x2804
movb 0x4(%rcx), %dl
movsbl %dl, %r9d
movl 0x8(%rcx), %ecx
leaq 0x7859(%rip), %r8       # 0xa038
movslq (%r8,%rsi,4), %rsi
addq %r8, %rsi
jmpq *%rsi
cmpl $0x1, %eax
jne 0x28b1
leaq 0x7ab2(%rip), %rdx      # 0xa2aa
movl $0x9b, %esi
xorl %eax, %eax
jmp 0x9800
retq
movq 0x128(%rdi), %rcx
testb $0x10, 0xac(%rcx)
je 0x28b5
leaq 0x7a8c(%rip), %rsi      # 0xa2ac
xorl %eax, %eax
jmp 0x977c
movq 0x128(%rdi), %rdx
testb $0x1, 0xac(%rdx)
je 0x28b5
movl %ecx, %edx
testl %eax, %eax
jne 0x286e
jmp 0x2856
testl %eax, %eax
jne 0x286a
movq 0x128(%rdi), %rcx
movzwl 0xac(%rcx), %ecx
andl $0x2, %ecx
je 0x286a
movsbl %dl, %ecx
leaq 0x7a50(%rip), %rdx      # 0xa2b0
movl $0x8f, %esi
jmp 0x2913
testl %eax, %eax
je 0x28a0
incl %eax
movsbl %dl, %r8d
leaq 0x7a38(%rip), %rdx      # 0xa2b3
movl $0x9b, %esi
movl %eax, %ecx
jmp 0x28fc
testl %eax, %eax
je 0x28ed
incl %eax
leaq 0x7a6b(%rip), %rdx      # 0xa2fc
movl $0x9b, %esi
movl %eax, %r8d
xorl %eax, %eax
jmp 0x9800
leaq 0x7a09(%rip), %rdx      # 0xa2b0
movl $0x9b, %esi
movl %r9d, %ecx
jmp 0x2913
testb $0x1, %al
jne 0x2903
testb $0x5, %al
je 0x28cf
incl %eax
leaq 0x79d6(%rip), %rdx      # 0xa298
movl $0x9b, %esi
movl %r9d, %ecx
movl %eax, %r8d
jmp 0x28fc
testb $0x2, %al
leaq 0x79d8(%rip), %rax      # 0xa2b0
leaq 0x79d0(%rip), %rsi      # 0xa2af
cmoveq %rax, %rsi
movl %r9d, %edx
xorl %eax, %eax
jmp 0x977c
leaq 0x79c1(%rip), %rdx      # 0xa2b5
movl $0x9b, %esi
movl %r9d, %r8d
xorl %eax, %eax
jmp 0x9800
incl %eax
leaq 0x799a(%rip), %rdx      # 0xa2a6
movl $0x9b, %esi
movl %eax, %ecx
xorl %eax, %eax
jmp 0x9800