neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

termkey-internal.h (2640B)


      1 #pragma once
      2 
      3 #include <stdint.h>
      4 
      5 #include "nvim/tui/termkey/termkey_defs.h"
      6 
      7 #define HAVE_TERMIOS
      8 #ifdef _WIN32
      9 # undef HAVE_TERMIOS
     10 #endif
     11 
     12 #ifdef HAVE_TERMIOS
     13 # include <termios.h>
     14 #endif
     15 
     16 #ifdef _MSC_VER
     17 # include <BaseTsd.h>
     18 typedef SSIZE_T ssize_t;
     19 #endif
     20 
     21 struct TermKeyDriver {
     22  const char *name;
     23  void *(*new_driver)(TermKey *tk, TerminfoEntry *term);
     24  void (*free_driver)(void *info);
     25  int (*start_driver)(TermKey *tk, void *info);
     26  int (*stop_driver)(TermKey *tk, void *info);
     27  TermKeyResult (*peekkey)(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytes);
     28 };
     29 
     30 struct keyinfo {
     31  TermKeyType type;
     32  TermKeySym sym;
     33  int modifier_mask;
     34  int modifier_set;
     35 };
     36 
     37 struct TermKeyDriverNode;
     38 struct TermKeyDriverNode {
     39  struct TermKeyDriver *driver;
     40  void *info;
     41  struct TermKeyDriverNode *next;
     42 };
     43 
     44 struct TermKey {
     45  int fd;
     46  int flags;
     47  int canonflags;
     48  unsigned char *buffer;
     49  size_t buffstart;  // First offset in buffer
     50  size_t buffcount;  // NUMBER of entries valid in buffer
     51  size_t buffsize;   // Total malloc'ed size
     52  size_t hightide;   // Position beyond buffstart at which peekkey() should next start
     53                     // normally 0, but see also termkey_interpret_csi
     54 
     55 #ifdef HAVE_TERMIOS
     56  struct termios restore_termios;
     57  char restore_termios_valid;
     58 #endif
     59 
     60  TermKey_Terminfo_Getstr_Hook *ti_getstr_hook;
     61  void *ti_getstr_hook_data;
     62 
     63  int waittime;  // msec
     64 
     65  char is_closed;
     66  char is_started;
     67 
     68  int nkeynames;
     69  const char **keynames;
     70 
     71  // There are 32 C0 codes
     72  struct keyinfo c0[32];
     73 
     74  struct TermKeyDriverNode *drivers;
     75 
     76  // Now some "protected" methods for the driver to call but which we don't
     77  // want exported as real symbols in the library
     78  struct {
     79    void (*emit_codepoint)(TermKey *tk, int codepoint, TermKeyKey *key);
     80    TermKeyResult (*peekkey_simple)(TermKey *tk, TermKeyKey *key, int force, size_t *nbytes);
     81    TermKeyResult (*peekkey_mouse)(TermKey *tk, TermKeyKey *key, size_t *nbytes);
     82  } method;
     83 };
     84 
     85 static inline void termkey_key_get_linecol(const TermKeyKey *key, int *line, int *col)
     86 {
     87  if (col) {
     88    *col = (unsigned char)key->code.mouse[1] | ((unsigned char)key->code.mouse[3] & 0x0f) << 8;
     89  }
     90 
     91  if (line) {
     92    *line = (unsigned char)key->code.mouse[2] | ((unsigned char)key->code.mouse[3] & 0x70) << 4;
     93  }
     94 }
     95 
     96 static inline void termkey_key_set_linecol(TermKeyKey *key, int line, int col)
     97 {
     98  if (line > 0xfff) {
     99    line = 0xfff;
    100  }
    101 
    102  if (col > 0x7ff) {
    103    col = 0x7ff;
    104  }
    105 
    106  key->code.mouse[1] = (char)(line & 0x0ff);
    107  key->code.mouse[2] = (char)(col & 0x0ff);
    108  key->code.mouse[3] = (line & 0xf00) >> 8 | (col & 0x300) >> 4;
    109 }