robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
a89d7dcb91
master
1#include <errno.h> 2#include <stdbool.h> 3#include <stdio.h> 4#include <stdlib.h> 5#include <string.h> 6#include <sys/stat.h> 7#include <uv.h> 8 9#include "nvim/charset.h" 10#include "nvim/memory.h" 11#include "nvim/tui/terminfo.h" 12#include "nvim/tui/termkey/driver-ti.h" 13#include "nvim/tui/termkey/termkey-internal.h" 14#include "nvim/tui/termkey/termkey_defs.h" 15 16#ifndef _WIN32 17# include <unistd.h> 18#else 19# include <io.h> 20#endif 21 22#include "tui/termkey/driver-ti.c.generated.h" 23 24#define streq (a ,b ) (!strcmp(a, b)) 25 26#define MAX_FUNCNAME 9 27 28static struct { 29TerminfoKey ti_key ; 30const char * funcname ; 31TermKeyType type ; 32TermKeySym sym ; 33int mods ; 34}funcs []= { 35// THIS LIST MUST REMAIN SORTED! 36// nvim note: entries commented out without further note are not recognized by nvim. 37#define KDEF (x )kTermKey_ ##x ,#x 38 {KDEF (backspace ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_BACKSPACE ,0 }, 39 {KDEF (beg ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_BEGIN ,0 }, 40 {KDEF (btab ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_TAB ,TERMKEY_KEYMOD_SHIFT }, 41// { KDEF(cancel), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_CANCEL, 0 }, 42 {KDEF (clear ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_CLEAR ,0 }, 43// { KDEF(close), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_CLOSE, 0 }, 44// { KDEF(command), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_COMMAND, 0 }, 45// { KDEF(copy), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_COPY, 0 }, 46 {KDEF (dc ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_DELETE ,0 }, 47// { KDEF(down), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_DOWN, 0 }, // redundant, driver-csi 48 {KDEF (end ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_END ,0 }, 49// { KDEF(enter), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_ENTER, 0 }, 50// { KDEF(exit), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_EXIT, 0 }, 51 {KDEF (find ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_FIND ,0 }, 52// { KDEF(help), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_HELP, 0 }, 53 {KDEF (home ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_HOME ,0 }, 54 {KDEF (ic ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_INSERT ,0 }, 55 {KDEF (left ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_LEFT ,0 }, 56// { KDEF(mark), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_MARK, 0 }, 57// { KDEF(message), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_MESSAGE, 0 }, 58// { KDEF(move), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_MOVE, 0 }, 59// { KDEF(next), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEDOWN, 0 }, // use "npage" right below 60 {KDEF (npage ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_PAGEDOWN ,0 }, 61// { KDEF(open), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_OPEN, 0 }, 62// { KDEF(options), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_OPTIONS, 0 }, 63 {KDEF (ppage ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_PAGEUP ,0 }, 64// { KDEF(previous), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEUP, 0 }, // use "ppage" right above 65// { KDEF(print), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PRINT, 0 }, 66// { KDEF(redo), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_REDO, 0 }, 67// { KDEF(reference), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_REFERENCE, 0 }, 68// { KDEF(refresh), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_REFRESH, 0 }, 69// { KDEF(replace), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_REPLACE, 0 }, 70// { KDEF(restart), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_RESTART, 0 }, 71// { KDEF(resume), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_RESUME, 0 }, 72 {KDEF (right ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_RIGHT ,0 }, 73// { KDEF(save), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_SAVE, 0 }, 74 {KDEF (select ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_SELECT ,0 }, 75 {KDEF (suspend ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_SUSPEND ,0 }, 76 {KDEF (undo ),TERMKEY_TYPE_KEYSYM ,TERMKEY_SYM_UNDO ,0 }, 77// { KDEF(up), TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_UP, 0 }, // redundant, driver-csi 78 {0 ,NULL ,0 ,0 ,0 }, 79}; 80 81// To be efficient at lookups, we store the byte sequence => keyinfo mapping 82// in a trie. This avoids a slow linear search through a flat list of 83// sequences. Because it is likely most nodes will be very sparse, we optimise 84// vector to store an extent map after the database is loaded. 85 86typedef enum { 87TYPE_KEY , 88TYPE_ARR , 89}trie_nodetype ; 90 91struct trie_node { 92trie_nodetype type ; 93}; 94 95struct trie_node_key { 96trie_nodetype type ; 97struct keyinfo key ; 98}; 99 100struct trie_node_arr { 101trie_nodetype type ; 102unsigned char min ,max ;// INCLUSIVE endpoints of the extent range 103struct trie_node * arr [];// dynamic size at allocation time 104}; 105 106static int insert_seq (TermKeyTI * ti ,const char * seq ,struct trie_node * node ); 107 108static struct trie_node * new_node_key (TermKeyType type ,TermKeySym sym ,int modmask ,int modset ) 109{ 110struct trie_node_key * n = xmalloc (sizeof (* n )); 111 112n -> type = TYPE_KEY ; 113 114n -> key .type = type ; 115n -> key .sym = sym ; 116n -> key .modifier_mask = modmask ; 117n -> key .modifier_set = modset ; 118 119return (struct trie_node * )n ; 120} 121 122static struct trie_node * new_node_arr (unsigned char min ,unsigned char max ) 123{ 124struct trie_node_arr * n = xmalloc (sizeof (* n )+ (max - min + 1 )* sizeof (n -> arr [0 ])); 125 126n -> type = TYPE_ARR ; 127n -> min = min ;n -> max = max ; 128 129int i ; 130for (i = min ;i <=max ;i ++ ) { 131n -> arr [i - min ]= NULL ; 132 } 133 134return (struct trie_node * )n ; 135} 136 137static struct trie_node * lookup_next (struct trie_node * n ,unsigned char b ) 138{ 139switch (n -> type ) { 140case TYPE_KEY : 141fprintf (stderr ,"ABORT: lookup_next within a TYPE_KEY node\n" ); 142abort (); 143case TYPE_ARR : { 144struct trie_node_arr * nar = (struct trie_node_arr * )n ; 145if (b < nar -> min || b > nar -> max ) { 146return NULL ; 147 } 148return nar -> arr [b - nar -> min ]; 149 } 150 } 151 152return NULL ;// Never reached but keeps compiler happy 153} 154 155static void free_trie (struct trie_node * n ) 156{ 157switch (n -> type ) { 158case TYPE_KEY : 159break ; 160case TYPE_ARR : { 161struct trie_node_arr * nar = (struct trie_node_arr * )n ; 162int i ; 163for (i = nar -> min ;i <=nar -> max ;i ++ ) { 164if (nar -> arr [i - nar -> min ]) { 165free_trie (nar -> arr [i - nar -> min ]); 166 } 167 } 168break ; 169 } 170 } 171 172xfree (n ); 173} 174 175static struct trie_node * compress_trie (struct trie_node * n ) 176{ 177if (!n ) { 178return NULL ; 179 } 180 181switch (n -> type ) { 182case TYPE_KEY : 183return n ; 184case TYPE_ARR : { 185struct trie_node_arr * nar = (struct trie_node_arr * )n ; 186unsigned char min ,max ; 187// Find the real bounds 188for (min = 0 ; !nar -> arr [min ];min ++ ) { 189if (min == 255 && !nar -> arr [min ]) { 190xfree (nar ); 191return new_node_arr (1 ,0 ); 192 } 193 } 194 195for (max = 0xff ; !nar -> arr [max ];max -- ) {} 196 197struct trie_node_arr * new = (struct trie_node_arr * )new_node_arr (min ,max ); 198int i ; 199for (i = min ;i <=max ;i ++ ) { 200new -> arr [i - min ]= compress_trie (nar -> arr [i ]); 201 } 202 203xfree (nar ); 204return (struct trie_node * )new ; 205 } 206 } 207 208return n ; 209} 210 211static bool try_load_terminfo_key (TermKeyTI * ti ,bool fn_nr ,int key ,bool shift ,const char * name , 212struct keyinfo * info ) 213{ 214const char * value = NULL ; 215 216if (ti -> ti ) { 217if (!fn_nr ) { 218value = ti -> ti -> keys [key ][shift ?1 :0 ]; 219 }else { 220assert (!shift ); 221value = ti -> ti -> f_keys [key ]; 222 } 223 } 224 225if (ti -> tk -> ti_getstr_hook ) { 226value = (ti -> tk -> ti_getstr_hook )(name ,value ,ti -> tk -> ti_getstr_hook_data ); 227 } 228 229if (!value || value == (char * )-1 || !value [0 ]) { 230return false; 231 } 232 233struct trie_node * node = new_node_key (info -> type ,info -> sym ,info -> modifier_mask , 234info -> modifier_set ); 235insert_seq (ti ,value ,node ); 236 237return true; 238} 239 240static int load_terminfo (TermKeyTI * ti ) 241{ 242int i ; 243 244ti -> root = new_node_arr (0 ,0xff ); 245if (!ti -> root ) { 246return 0 ; 247 } 248 249// First the regular key strings 250for (i = 0 ;funcs [i ].funcname ;i ++ ) { 251char name [MAX_FUNCNAME + 5 + 1 ]; 252 253sprintf (name ,"key_%s" ,funcs [i ].funcname );// NOLINT(runtime/printf) 254if (!try_load_terminfo_key (ti , false, (int )funcs [i ].ti_key , false,name ,& (struct keyinfo ){ 255 .type = funcs [i ].type , 256 .sym = funcs [i ].sym , 257 .modifier_mask = funcs [i ].mods , 258 .modifier_set = funcs [i ].mods , 259 })) { 260continue ; 261 } 262 263// Maybe it has a shifted version 264sprintf (name ,"key_s%s" ,funcs [i ].funcname );// NOLINT(runtime/printf) 265try_load_terminfo_key (ti , false, (int )funcs [i ].ti_key , true,name ,& (struct keyinfo ){ 266 .type = funcs [i ].type , 267 .sym = funcs [i ].sym , 268 .modifier_mask = funcs [i ].mods |TERMKEY_KEYMOD_SHIFT , 269 .modifier_set = funcs [i ].mods |TERMKEY_KEYMOD_SHIFT , 270 }); 271 } 272 273// Now the F<digit> keys 274for (i = 1 ;i <=kTerminfoFuncKeyMax ;i ++ ) { 275char name [9 ]; 276sprintf (name ,"key_f%d" ,i );// NOLINT(runtime/printf) 277if (!try_load_terminfo_key (ti , true, (i - 1 ), false,name ,& (struct keyinfo ){ 278 .type = TERMKEY_TYPE_FUNCTION , 279 .sym = i , 280 .modifier_mask = 0 , 281 .modifier_set = 0 , 282 })) { 283break ; 284 } 285 } 286 287// Finally mouse mode 288// This is overridden in nvim: we only want driver-csi mouse support 289if (false) { 290const char * value = NULL ; 291 292if (ti -> ti ) { 293// value = ti->ti->keys[kTermKey_mouse][0]; 294 } 295 296if (ti -> tk -> ti_getstr_hook ) { 297value = (ti -> tk -> ti_getstr_hook )("key_mouse" ,value ,ti -> tk -> ti_getstr_hook_data ); 298 } 299 300// Some terminfos (e.g. xterm-1006) claim a different key_mouse that won't 301// give X10 encoding. We'll only accept this if it's exactly "\e[M" 302if (value && streq (value ,"\x1b[M" )) { 303struct trie_node * node = new_node_key (TERMKEY_TYPE_MOUSE ,0 ,0 ,0 ); 304insert_seq (ti ,value ,node ); 305 } 306 } 307 308// Take copies of these terminfo strings, in case we build multiple termkey 309// instances for multiple different termtypes, and it's different by the 310// time we want to use it 311const char * keypad_xmit = ti -> ti 312 ?ti -> ti -> defs [kTerm_keypad_xmit ] 313 :NULL ; 314 315if (keypad_xmit ) { 316ti -> start_string = xstrdup (keypad_xmit ); 317 }else { 318ti -> start_string = NULL ; 319 } 320 321const char * keypad_local = ti -> ti 322 ?ti -> ti -> defs [kTerm_keypad_local ] 323 :NULL ; 324 325if (keypad_local ) { 326ti -> stop_string = xstrdup (keypad_local ); 327 }else { 328ti -> stop_string = NULL ; 329 } 330 331ti -> root = compress_trie (ti -> root ); 332 333return 1 ; 334} 335 336void * new_driver_ti (TermKey * tk ,TerminfoEntry * term ) 337{ 338TermKeyTI * ti = xmalloc (sizeof * ti ); 339 340ti -> tk = tk ; 341ti -> root = NULL ; 342ti -> start_string = NULL ; 343ti -> stop_string = NULL ; 344 345ti -> ti = term ; 346 347// ti->ti may be NULL because reasons. That means the terminal wasn't 348// known. Lets keep going because if we get getstr hook that might invent 349// new strings for us 350 351return ti ; 352} 353 354int start_driver_ti (TermKey * tk ,void * info ) 355{ 356TermKeyTI * ti = info ; 357struct stat statbuf ; 358char * start_string ; 359size_t len ; 360 361if (!ti -> root ) { 362load_terminfo (ti ); 363 } 364 365start_string = ti -> start_string ; 366 367if (tk -> fd == -1 || !start_string ) { 368return 1 ; 369 } 370 371// The terminfo database will contain keys in application cursor key mode. 372// We may need to enable that mode 373 374// There's no point trying to write() to a pipe 375if (fstat (tk -> fd ,& statbuf )== -1 ) { 376return 0 ; 377 } 378 379#ifndef _WIN32 380if (S_ISFIFO (statbuf .st_mode )) { 381return 1 ; 382 } 383#endif 384 385// Can't call putp or tputs because they suck and don't give us fd control 386len = strlen (start_string ); 387while (len ) { 388ssize_t result = write (tk -> fd ,start_string , (unsigned )len ); 389if (result < 0 ) { 390return 0 ; 391 } 392size_t written = (size_t )result ; 393start_string += written ; 394len -= written ; 395 } 396return 1 ; 397} 398 399int stop_driver_ti (TermKey * tk ,void * info ) 400{ 401TermKeyTI * ti = info ; 402struct stat statbuf ; 403char * stop_string = ti -> stop_string ; 404size_t len ; 405 406if (tk -> fd == -1 || !stop_string ) { 407return 1 ; 408 } 409 410// There's no point trying to write() to a pipe 411if (fstat (tk -> fd ,& statbuf )== -1 ) { 412return 0 ; 413 } 414 415#ifndef _WIN32 416if (S_ISFIFO (statbuf .st_mode )) { 417return 1 ; 418 } 419#endif 420 421// The terminfo database will contain keys in application cursor key mode. 422// We may need to enable that mode 423 424// Can't call putp or tputs because they suck and don't give us fd control 425len = strlen (stop_string ); 426while (len ) { 427ssize_t result = write (tk -> fd ,stop_string , (unsigned )len ); 428if (result < 0 ) { 429return 0 ; 430 } 431size_t written = (size_t )result ; 432stop_string += written ; 433len -= written ; 434 } 435return 1 ; 436} 437 438void free_driver_ti (void * info ) 439{ 440TermKeyTI * ti = info ; 441 442free_trie (ti -> root ); 443 444if (ti -> start_string ) { 445xfree (ti -> start_string ); 446 } 447 448if (ti -> stop_string ) { 449xfree (ti -> stop_string ); 450 } 451 452xfree (ti ); 453} 454 455#define CHARAT (i ) (tk->buffer[tk->buffstart + (i)]) 456 457TermKeyResult peekkey_ti (TermKey * tk ,void * info ,TermKeyKey * key ,int force ,size_t * nbytep ) 458{ 459TermKeyTI * ti = info ; 460 461if (tk -> buffcount == 0 ) { 462return tk -> is_closed ?TERMKEY_RES_EOF :TERMKEY_RES_NONE ; 463 } 464 465struct trie_node * p = ti -> root ; 466 467unsigned pos = 0 ; 468while (pos < tk -> buffcount ) { 469p = lookup_next (p ,CHARAT (pos )); 470if (!p ) { 471break ; 472 } 473 474pos ++ ; 475 476if (p -> type != TYPE_KEY ) { 477continue ; 478 } 479 480struct trie_node_key * nk = (struct trie_node_key * )p ; 481if (nk -> key .type == TERMKEY_TYPE_MOUSE ) { 482tk -> buffstart += pos ; 483tk -> buffcount -= pos ; 484 485TermKeyResult mouse_result = (* tk -> method .peekkey_mouse )(tk ,key ,nbytep ); 486 487tk -> buffstart -= pos ; 488tk -> buffcount += pos ; 489 490if (mouse_result == TERMKEY_RES_KEY ) { 491* nbytep += pos ; 492 } 493 494return mouse_result ; 495 } 496 497key -> type = nk -> key .type ; 498key -> code .sym = nk -> key .sym ; 499key -> modifiers = nk -> key .modifier_set ; 500* nbytep = pos ; 501return TERMKEY_RES_KEY ; 502 } 503 504// If p is not NULL then we hadn't walked off the end yet, so we have a 505// partial match 506if (p && !force ) { 507return TERMKEY_RES_AGAIN ; 508 } 509 510return TERMKEY_RES_NONE ; 511} 512 513static int insert_seq (TermKeyTI * ti ,const char * seq ,struct trie_node * node ) 514{ 515int pos = 0 ; 516struct trie_node * p = ti -> root ; 517 518// Unsigned because we'll be using it as an array subscript 519unsigned char b ; 520 521while ((b = (unsigned char )seq [pos ])) { 522struct trie_node * next = lookup_next (p ,b ); 523if (!next ) { 524break ; 525 } 526p = next ; 527pos ++ ; 528 } 529 530while ((b = (unsigned char )seq [pos ])) { 531struct trie_node * next ; 532if (seq [pos + 1 ]) { 533// Intermediate node 534next = new_node_arr (0 ,0xff ); 535 }else { 536// Final key node 537next = node ; 538 } 539 540if (!next ) { 541return 0 ; 542 } 543 544switch (p -> type ) { 545case TYPE_ARR : { 546struct trie_node_arr * nar = (struct trie_node_arr * )p ; 547if (b < nar -> min || b > nar -> max ) { 548fprintf (stderr , 549"ASSERT FAIL: Trie insert at 0x%02x is outside of extent bounds (0x%02x..0x%02x)\n" , 550b ,nar -> min ,nar -> max ); 551abort (); 552 } 553nar -> arr [b - nar -> min ]= next ; 554p = next ; 555break ; 556 } 557case TYPE_KEY : 558fprintf (stderr ,"ASSERT FAIL: Tried to insert child node in TYPE_KEY\n" ); 559abort (); 560 } 561 562pos ++ ; 563 } 564 565return 1 ; 566}