robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <ctype.h> 2#include <errno.h> 3#include <stdio.h> 4#include <string.h> 5 6#include "nvim/macros_defs.h" 7#include "nvim/mbyte.h" 8#include "nvim/memory.h" 9#include "nvim/tui/termkey/driver-csi.h" 10#include "nvim/tui/termkey/driver-ti.h" 11#include "nvim/tui/termkey/termkey-internal.h" 12#include "nvim/tui/termkey/termkey.h" 13#include "nvim/tui/termkey/termkey_defs.h" 14 15#ifndef _WIN32 16// Include these directly instead of <termios.h> which is system-dependent. #31704 17# include <poll.h> // IWYU pragma: keep 18# include <strings.h> // IWYU pragma: keep 19# include <unistd.h> // IWYU pragma: keep 20#else 21# include <io.h> 22#endif 23 24#include "tui/termkey/termkey.c.generated.h" 25 26#ifdef _MSC_VER 27# define strcaseeq (a ,b ) (_stricmp(a, b) == 0) 28#else 29# define strcaseeq (a ,b ) (strcasecmp(a, b) == 0) 30#endif 31 32static struct TermKeyDriver termkey_driver_ti = { 33 .name = "terminfo" , 34 35 .new_driver = new_driver_ti , 36 .free_driver = free_driver_ti , 37 38 .start_driver = start_driver_ti , 39 .stop_driver = stop_driver_ti , 40 41 .peekkey = peekkey_ti , 42}; 43 44static struct TermKeyDriver termkey_driver_csi = { 45 .name = "CSI" , 46 47 .new_driver = new_driver_csi , 48 .free_driver = free_driver_csi , 49 50 .peekkey = peekkey_csi , 51}; 52 53static struct TermKeyDriver * drivers []= { 54& termkey_driver_ti , 55& termkey_driver_csi , 56NULL , 57}; 58 59static struct { 60TermKeySym sym ; 61const char * name ; 62}keynames []= { 63 {TERMKEY_SYM_NONE ,"NONE" }, 64 {TERMKEY_SYM_BACKSPACE ,"Backspace" }, 65 {TERMKEY_SYM_TAB ,"Tab" }, 66 {TERMKEY_SYM_ENTER ,"Enter" }, 67 {TERMKEY_SYM_ESCAPE ,"Escape" }, 68 {TERMKEY_SYM_SPACE ,"Space" }, 69 {TERMKEY_SYM_DEL ,"DEL" }, 70 {TERMKEY_SYM_UP ,"Up" }, 71 {TERMKEY_SYM_DOWN ,"Down" }, 72 {TERMKEY_SYM_LEFT ,"Left" }, 73 {TERMKEY_SYM_RIGHT ,"Right" }, 74 {TERMKEY_SYM_BEGIN ,"Begin" }, 75 {TERMKEY_SYM_FIND ,"Find" }, 76 {TERMKEY_SYM_INSERT ,"Insert" }, 77 {TERMKEY_SYM_DELETE ,"Delete" }, 78 {TERMKEY_SYM_SELECT ,"Select" }, 79 {TERMKEY_SYM_PAGEUP ,"PageUp" }, 80 {TERMKEY_SYM_PAGEDOWN ,"PageDown" }, 81 {TERMKEY_SYM_HOME ,"Home" }, 82 {TERMKEY_SYM_END ,"End" }, 83 {TERMKEY_SYM_CANCEL ,"Cancel" }, 84 {TERMKEY_SYM_CLEAR ,"Clear" }, 85 {TERMKEY_SYM_CLOSE ,"Close" }, 86 {TERMKEY_SYM_COMMAND ,"Command" }, 87 {TERMKEY_SYM_COPY ,"Copy" }, 88 {TERMKEY_SYM_EXIT ,"Exit" }, 89 {TERMKEY_SYM_HELP ,"Help" }, 90 {TERMKEY_SYM_MARK ,"Mark" }, 91 {TERMKEY_SYM_MESSAGE ,"Message" }, 92 {TERMKEY_SYM_MOVE ,"Move" }, 93 {TERMKEY_SYM_OPEN ,"Open" }, 94 {TERMKEY_SYM_OPTIONS ,"Options" }, 95 {TERMKEY_SYM_PRINT ,"Print" }, 96 {TERMKEY_SYM_REDO ,"Redo" }, 97 {TERMKEY_SYM_REFERENCE ,"Reference" }, 98 {TERMKEY_SYM_REFRESH ,"Refresh" }, 99 {TERMKEY_SYM_REPLACE ,"Replace" }, 100 {TERMKEY_SYM_RESTART ,"Restart" }, 101 {TERMKEY_SYM_RESUME ,"Resume" }, 102 {TERMKEY_SYM_SAVE ,"Save" }, 103 {TERMKEY_SYM_SUSPEND ,"Suspend" }, 104 {TERMKEY_SYM_UNDO ,"Undo" }, 105 {TERMKEY_SYM_KP0 ,"KP0" }, 106 {TERMKEY_SYM_KP1 ,"KP1" }, 107 {TERMKEY_SYM_KP2 ,"KP2" }, 108 {TERMKEY_SYM_KP3 ,"KP3" }, 109 {TERMKEY_SYM_KP4 ,"KP4" }, 110 {TERMKEY_SYM_KP5 ,"KP5" }, 111 {TERMKEY_SYM_KP6 ,"KP6" }, 112 {TERMKEY_SYM_KP7 ,"KP7" }, 113 {TERMKEY_SYM_KP8 ,"KP8" }, 114 {TERMKEY_SYM_KP9 ,"KP9" }, 115 {TERMKEY_SYM_KPENTER ,"KPEnter" }, 116 {TERMKEY_SYM_KPPLUS ,"KPPlus" }, 117 {TERMKEY_SYM_KPMINUS ,"KPMinus" }, 118 {TERMKEY_SYM_KPMULT ,"KPMult" }, 119 {TERMKEY_SYM_KPDIV ,"KPDiv" }, 120 {TERMKEY_SYM_KPCOMMA ,"KPComma" }, 121 {TERMKEY_SYM_KPPERIOD ,"KPPeriod" }, 122 {TERMKEY_SYM_KPEQUALS ,"KPEquals" }, 123 {0 ,NULL }, 124}; 125 126// Mouse event names 127static const char * evnames []= {"Unknown" ,"Press" ,"Drag" ,"Release" }; 128 129#define CHARAT (i ) (tk->buffer[tk->buffstart + (i)]) 130 131#ifdef DEBUG 132// Some internal debugging functions 133 134static void print_buffer (TermKey * tk ) 135{ 136int i ; 137for (i = 0 ;i < tk -> buffcount && i < 20 ;i ++ ) { 138fprintf (stderr ,"%02x " ,CHARAT (i )); 139 } 140if (tk -> buffcount > 20 ) { 141fprintf (stderr ,"..." ); 142 } 143} 144 145static void print_key (TermKey * tk ,TermKeyKey * key ) 146{ 147switch (key -> type ) { 148case TERMKEY_TYPE_UNICODE : 149fprintf (stderr ,"Unicode codepoint=U+%04lx utf8='%s'" ,key -> code .codepoint ,key -> utf8 ); 150break ; 151case TERMKEY_TYPE_FUNCTION : 152fprintf (stderr ,"Function F%d" ,key -> code .number ); 153break ; 154case TERMKEY_TYPE_KEYSYM : 155fprintf (stderr ,"Keysym sym=%d(%s)" ,key -> code .sym ,termkey_get_keyname (tk ,key -> code .sym )); 156break ; 157case TERMKEY_TYPE_MOUSE : { 158TermKeyMouseEvent ev ; 159int button ,line ,col ; 160termkey_interpret_mouse (tk ,key ,& ev ,& button ,& line ,& col ); 161fprintf (stderr ,"Mouse ev=%d button=%d pos=(%d,%d)\n" ,ev ,button ,line ,col ); 162 } 163break ; 164case TERMKEY_TYPE_POSITION : { 165int line ,col ; 166termkey_interpret_position (tk ,key ,& line ,& col ); 167fprintf (stderr ,"Position report pos=(%d,%d)\n" ,line ,col ); 168 } 169break ; 170case TERMKEY_TYPE_MODEREPORT : { 171int initial ,mode ,value ; 172termkey_interpret_modereport (tk ,key ,& initial ,& mode ,& value ); 173fprintf (stderr ,"Mode report mode=%s %d val=%d\n" ,initial == '?' ?"DEC" :"ANSI" ,mode , 174value ); 175 } 176break ; 177case TERMKEY_TYPE_DCS : 178fprintf (stderr ,"Device Control String" ); 179break ; 180case TERMKEY_TYPE_OSC : 181fprintf (stderr ,"Operating System Control" ); 182break ; 183case TERMKEY_TYPE_APC : 184fprintf (stderr ,"Application Program Command" ); 185break ; 186case TERMKEY_TYPE_UNKNOWN_CSI : 187fprintf (stderr ,"unknown CSI\n" ); 188break ; 189 } 190 191int m = key -> modifiers ; 192fprintf (stderr ," mod=%s%s%s+%02x" , 193 (m & TERMKEY_KEYMOD_CTRL ?"C" :"" ), 194 (m & TERMKEY_KEYMOD_ALT ?"A" :"" ), 195 (m & TERMKEY_KEYMOD_SHIFT ?"S" :"" ), 196m & ~(TERMKEY_KEYMOD_CTRL |TERMKEY_KEYMOD_ALT |TERMKEY_KEYMOD_SHIFT )); 197} 198 199static const char * res2str (TermKeyResult res ) 200{ 201static char errorbuffer [256 ]; 202 203switch (res ) { 204case TERMKEY_RES_KEY : 205return "TERMKEY_RES_KEY" ; 206case TERMKEY_RES_EOF : 207return "TERMKEY_RES_EOF" ; 208case TERMKEY_RES_AGAIN : 209return "TERMKEY_RES_AGAIN" ; 210case TERMKEY_RES_NONE : 211return "TERMKEY_RES_NONE" ; 212case TERMKEY_RES_ERROR : 213snprintf (errorbuffer ,sizeof errorbuffer ,"TERMKEY_RES_ERROR(errno=%d)\n" ,errno ); 214return (const char * )errorbuffer ; 215 } 216 217return "unknown" ; 218} 219#endif 220 221TermKeyResult termkey_interpret_string (TermKey * tk ,const TermKeyKey * key ,const char ** strp ) 222{ 223struct TermKeyDriverNode * p ; 224for (p = tk -> drivers ;p ;p = p -> next ) { 225if (p -> driver == & termkey_driver_csi ) { 226break ; 227 } 228 } 229 230if (!p ) { 231return TERMKEY_RES_NONE ; 232 } 233 234if (key -> type != TERMKEY_TYPE_DCS 235&& key -> type != TERMKEY_TYPE_OSC 236&& key -> type != TERMKEY_TYPE_APC ) { 237return TERMKEY_RES_NONE ; 238 } 239 240TermKeyCsi * csi = p -> info ; 241 242if (csi -> saved_string_id != key -> code .number ) { 243return TERMKEY_RES_NONE ; 244 } 245 246* strp = csi -> saved_string ; 247 248return TERMKEY_RES_KEY ; 249} 250 251/// Similar to snprintf(str, size, "%s", src) except it turns CamelCase into 252/// space separated values 253static int snprint_cameltospaces (char * str ,size_t size ,const char * src ) 254{ 255int prev_lower = 0 ; 256size_t l = 0 ; 257while (* src && l < size - 1 ) { 258if (isupper (* src )&& prev_lower ) { 259if (str ) { 260str [l ++ ]= ' ' ; 261 } 262if (l >=size - 1 ) { 263break ; 264 } 265 } 266prev_lower = islower (* src ); 267str [l ++ ]= (char )tolower (* src ++ ); 268 } 269str [l ]= 0 ; 270// For consistency with snprintf, return the number of bytes that would have 271// been written, excluding '\0' 272while (* src ) { 273if (isupper (* src )&& prev_lower ) { 274l ++ ; 275 } 276prev_lower = islower (* src ); 277src ++ ;l ++ ; 278 } 279return (int )l ; 280} 281 282/// Similar to strcmp(str, strcamel, n) except that: 283/// it compares CamelCase in strcamel with space separated values in str; 284/// it takes char**s and updates them 285/// n counts bytes of strcamel, not str 286static int strpncmp_camel (const char ** strp ,const char ** strcamelp ,size_t n ) 287{ 288const char * str = * strp ,* strcamel = * strcamelp ; 289int prev_lower = 0 ; 290 291for (; (* str || * strcamel )&& n ;n -- ) { 292char b = (char )tolower (* strcamel ); 293if (isupper (* strcamel )&& prev_lower ) { 294if (* str != ' ' ) { 295break ; 296 } 297str ++ ; 298if (* str != b ) { 299break ; 300 } 301 }else if (* str != b ) { 302break ; 303 } 304 305prev_lower = islower (* strcamel ); 306 307str ++ ; 308strcamel ++ ; 309 } 310 311* strp = str ; 312* strcamelp = strcamel ; 313return * str - * strcamel ; 314} 315 316static TermKey * termkey_alloc (void ) 317{ 318TermKey * tk = xmalloc (sizeof (TermKey )); 319 320// Default all the object fields but don't allocate anything 321 322tk -> fd = -1 ; 323tk -> flags = 0 ; 324tk -> canonflags = 0 ; 325 326tk -> buffer = NULL ; 327tk -> buffstart = 0 ; 328tk -> buffcount = 0 ; 329tk -> buffsize = 256 ;// bytes 330tk -> hightide = 0 ; 331 332#ifdef HAVE_TERMIOS 333tk -> restore_termios_valid = 0 ; 334#endif 335 336tk -> ti_getstr_hook = NULL ; 337tk -> ti_getstr_hook_data = NULL ; 338 339tk -> waittime = 50 ;// msec 340 341tk -> is_closed = 0 ; 342tk -> is_started = 0 ; 343 344tk -> nkeynames = 64 ; 345tk -> keynames = NULL ; 346 347for (int i = 0 ;i < 32 ;i ++ ) { 348tk -> c0 [i ].sym = TERMKEY_SYM_NONE ; 349 } 350 351tk -> drivers = NULL ; 352 353tk -> method .emit_codepoint = & emit_codepoint ; 354tk -> method .peekkey_simple = & peekkey_simple ; 355tk -> method .peekkey_mouse = & peekkey_mouse ; 356 357return tk ; 358} 359 360static int termkey_init (TermKey * tk ,TerminfoEntry * term ) 361{ 362tk -> buffer = xmalloc (tk -> buffsize ); 363tk -> keynames = xmalloc (sizeof (tk -> keynames [0 ])* (size_t )tk -> nkeynames ); 364 365int i ; 366for (i = 0 ;i < tk -> nkeynames ;i ++ ) { 367tk -> keynames [i ]= NULL ; 368 } 369 370for (i = 0 ;keynames [i ].name ;i ++ ) { 371if (termkey_register_keyname (tk ,keynames [i ].sym ,keynames [i ].name )== -1 ) { 372 gotoabort_free_keynames ; 373 } 374 } 375 376register_c0 (tk ,TERMKEY_SYM_TAB ,0x09 ,NULL ); 377register_c0 (tk ,TERMKEY_SYM_ENTER ,0x0d ,NULL ); 378register_c0 (tk ,TERMKEY_SYM_ESCAPE ,0x1b ,NULL ); 379 380struct TermKeyDriverNode * tail = NULL ; 381 382for (i = 0 ;drivers [i ];i ++ ) { 383void * info = (* drivers [i ]-> new_driver )(tk ,term ); 384if (!info ) { 385continue ; 386 } 387 388#ifdef DEBUG 389fprintf (stderr ,"Loading the %s driver...\n" ,drivers [i ]-> name ); 390#endif 391 392struct TermKeyDriverNode * thisdrv = xmalloc (sizeof (* thisdrv )); 393if (!thisdrv ) { 394 gotoabort_free_drivers ; 395 } 396 397thisdrv -> driver = drivers [i ]; 398thisdrv -> info = info ; 399thisdrv -> next = NULL ; 400 401if (!tail ) { 402tk -> drivers = thisdrv ; 403 }else { 404tail -> next = thisdrv ; 405 } 406 407tail = thisdrv ; 408 409#ifdef DEBUG 410fprintf (stderr ,"Loaded %s driver\n" ,drivers [i ]-> name ); 411#endif 412 } 413 414if (!tk -> drivers ) { 415errno = ENOENT ; 416 gotoabort_free_keynames ; 417 } 418 419return 1 ; 420 421abort_free_drivers : 422for (struct TermKeyDriverNode * p = tk -> drivers ;p ;) { 423 (* p -> driver -> free_driver )(p -> info ); 424struct TermKeyDriverNode * next = p -> next ; 425xfree (p ); 426p = next ; 427 } 428 429abort_free_keynames : 430xfree (tk -> keynames ); 431xfree (tk -> buffer ); 432 433return 0 ; 434} 435 436TermKey * termkey_new_abstract (TerminfoEntry * term ,int flags ) 437{ 438TermKey * tk = termkey_alloc (); 439if (!tk ) { 440return NULL ; 441 } 442 443tk -> fd = -1 ; 444 445termkey_set_flags (tk ,flags ); 446 447if (!termkey_init (tk ,term )) { 448xfree (tk ); 449return NULL ; 450 } 451 452if (!(flags & TERMKEY_FLAG_NOSTART )&& !termkey_start (tk )) { 453 gotoabort ; 454 } 455 456return tk ; 457 458abort : 459xfree (tk ); 460return NULL ; 461} 462 463void termkey_free (TermKey * tk ) 464{ 465xfree (tk -> buffer );tk -> buffer = NULL ; 466xfree (tk -> keynames );tk -> keynames = NULL ; 467 468struct TermKeyDriverNode * p ; 469for (p = tk -> drivers ;p ;) { 470 (* p -> driver -> free_driver )(p -> info ); 471struct TermKeyDriverNode * next = p -> next ; 472xfree (p ); 473p = next ; 474 } 475 476xfree (tk ); 477} 478 479void termkey_destroy (TermKey * tk ) 480{ 481if (tk -> is_started ) { 482termkey_stop (tk ); 483 } 484 485termkey_free (tk ); 486} 487 488void termkey_hook_terminfo_getstr (TermKey * tk ,TermKey_Terminfo_Getstr_Hook * hookfn ,void * data ) 489{ 490tk -> ti_getstr_hook = hookfn ; 491tk -> ti_getstr_hook_data = data ; 492} 493 494int termkey_start (TermKey * tk ) 495{ 496if (tk -> is_started ) { 497return 1 ; 498 } 499 500#ifdef HAVE_TERMIOS 501if (tk -> fd != -1 && !(tk -> flags & TERMKEY_FLAG_NOTERMIOS )) { 502struct termios termios ; 503if (tcgetattr (tk -> fd ,& termios )== 0 ) { 504tk -> restore_termios = termios ; 505tk -> restore_termios_valid = 1 ; 506 507termios .c_iflag &= (tcflag_t ) ~(IXON |INLCR |ICRNL ); 508termios .c_lflag &= (tcflag_t ) ~(ICANON |ECHO 509# ifdef IEXTEN 510 |IEXTEN 511# endif 512 ); 513termios .c_cc [VMIN ]= 1 ; 514termios .c_cc [VTIME ]= 0 ; 515 516if (tk -> flags & TERMKEY_FLAG_CTRLC ) { 517// want no signal keys at all, so just disable ISIG 518termios .c_lflag &= (tcflag_t ) ~ISIG ; 519 }else { 520// Disable Ctrl-\==VQUIT and Ctrl-D==VSUSP but leave Ctrl-C as SIGINT 521termios .c_cc [VQUIT ]= _POSIX_VDISABLE ; 522termios .c_cc [VSUSP ]= _POSIX_VDISABLE ; 523// Some OSes have Ctrl-Y==VDSUSP 524# ifdef VDSUSP 525termios .c_cc [VDSUSP ]= _POSIX_VDISABLE ; 526# endif 527 } 528 529# ifdef DEBUG 530fprintf (stderr ,"Setting termios(3) flags\n" ); 531# endif 532tcsetattr (tk -> fd ,TCSANOW ,& termios ); 533 } 534 } 535#endif 536 537struct TermKeyDriverNode * p ; 538for (p = tk -> drivers ;p ;p = p -> next ) { 539if (p -> driver -> start_driver ) { 540if (!(* p -> driver -> start_driver )(tk ,p -> info )) { 541return 0 ; 542 } 543 } 544 } 545 546#ifdef DEBUG 547fprintf (stderr ,"Drivers started; termkey instance %p is ready\n" ,tk ); 548#endif 549 550tk -> is_started = 1 ; 551return 1 ; 552} 553 554int termkey_stop (TermKey * tk ) 555{ 556if (!tk -> is_started ) { 557return 1 ; 558 } 559 560struct TermKeyDriverNode * p ; 561for (p = tk -> drivers ;p ;p = p -> next ) { 562if (p -> driver -> stop_driver ) { 563 (* p -> driver -> stop_driver )(tk ,p -> info ); 564 } 565 } 566 567#ifdef HAVE_TERMIOS 568if (tk -> restore_termios_valid ) { 569tcsetattr (tk -> fd ,TCSANOW ,& tk -> restore_termios ); 570 } 571#endif 572 573tk -> is_started = 0 ; 574 575return 1 ; 576} 577 578void termkey_set_flags (TermKey * tk ,int newflags ) 579{ 580tk -> flags = newflags ; 581 582if (tk -> flags & TERMKEY_FLAG_SPACESYMBOL ) { 583tk -> canonflags |=TERMKEY_CANON_SPACESYMBOL ; 584 }else { 585tk -> canonflags &= ~TERMKEY_CANON_SPACESYMBOL ; 586 } 587} 588 589int termkey_get_canonflags (TermKey * tk ) 590{ 591return tk -> canonflags ; 592} 593 594void termkey_set_canonflags (TermKey * tk ,int flags ) 595{ 596tk -> canonflags = flags ; 597 598if (tk -> canonflags & TERMKEY_CANON_SPACESYMBOL ) { 599tk -> flags |=TERMKEY_FLAG_SPACESYMBOL ; 600 }else { 601tk -> flags &= ~TERMKEY_FLAG_SPACESYMBOL ; 602 } 603} 604 605size_t termkey_get_buffer_size (TermKey * tk ) 606{ 607return tk -> buffsize ; 608} 609 610int termkey_set_buffer_size (TermKey * tk ,size_t size ) 611{ 612unsigned char * buffer = xrealloc (tk -> buffer ,size ); 613 614tk -> buffer = buffer ; 615tk -> buffsize = size ; 616 617return 1 ; 618} 619 620size_t termkey_get_buffer_remaining (TermKey * tk ) 621{ 622// Return the total number of free bytes in the buffer, because that's what 623// is available to the user. 624return tk -> buffsize - tk -> buffcount ; 625} 626 627static void eat_bytes (TermKey * tk ,size_t count ) 628{ 629if (count >=tk -> buffcount ) { 630tk -> buffstart = 0 ; 631tk -> buffcount = 0 ; 632return ; 633 } 634 635tk -> buffstart += count ; 636tk -> buffcount -= count ; 637} 638 639int fill_utf8 (int codepoint ,char * str ) 640{ 641int nbytes = utf_char2bytes (codepoint ,str ); 642str [nbytes ]= 0 ; 643return nbytes ; 644} 645 646static TermKeyResult parse_utf8 (const unsigned char * bytes ,size_t len ,int * cp ,size_t * nbytep ) 647{ 648unsigned nbytes ; 649 650unsigned char b0 = bytes [0 ]; 651 652if (b0 < 0x80 ) { 653// Single byte ASCII 654* cp = b0 ; 655* nbytep = 1 ; 656return TERMKEY_RES_KEY ; 657 }else if (b0 < 0xc0 ) { 658// Starts with a continuation byte - that's not right 659* cp = UNICODE_INVALID ; 660* nbytep = 1 ; 661return TERMKEY_RES_KEY ; 662 }else if (b0 < 0xe0 ) { 663nbytes = 2 ; 664* cp = b0 & 0x1f ; 665 }else if (b0 < 0xf0 ) { 666nbytes = 3 ; 667* cp = b0 & 0x0f ; 668 }else if (b0 < 0xf8 ) { 669nbytes = 4 ; 670* cp = b0 & 0x07 ; 671 }else if (b0 < 0xfc ) { 672nbytes = 5 ; 673* cp = b0 & 0x03 ; 674 }else if (b0 < 0xfe ) { 675nbytes = 6 ; 676* cp = b0 & 0x01 ; 677 }else { 678* cp = UNICODE_INVALID ; 679* nbytep = 1 ; 680return TERMKEY_RES_KEY ; 681 } 682 683for (unsigned b = 1 ;b < nbytes ;b ++ ) { 684unsigned char cb ; 685 686if (b >=len ) { 687return TERMKEY_RES_AGAIN ; 688 } 689 690cb = bytes [b ]; 691if (cb < 0x80 || cb >=0xc0 ) { 692* cp = UNICODE_INVALID ; 693* nbytep = b ; 694return TERMKEY_RES_KEY ; 695 } 696 697* cp <<=6 ; 698* cp |=cb & 0x3f ; 699 } 700 701// Check for overlong sequences 702if ((int )nbytes > utf_char2len (* cp )) { 703* cp = UNICODE_INVALID ; 704 } 705 706// Check for UTF-16 surrogates or invalid *cps 707if ((* cp >=0xD800 && * cp <=0xDFFF ) 708|| * cp == 0xFFFE 709|| * cp == 0xFFFF ) { 710* cp = UNICODE_INVALID ; 711 } 712 713* nbytep = nbytes ; 714return TERMKEY_RES_KEY ; 715} 716 717static void emit_codepoint (TermKey * tk ,int codepoint ,TermKeyKey * key ) 718{ 719if (codepoint == 0 ) { 720// ASCII NUL = Ctrl-Space 721key -> type = TERMKEY_TYPE_KEYSYM ; 722key -> code .sym = TERMKEY_SYM_SPACE ; 723key -> modifiers = TERMKEY_KEYMOD_CTRL ; 724 }else if (codepoint < 0x20 && !(tk -> flags & TERMKEY_FLAG_KEEPC0 )) { 725// C0 range 726key -> code .codepoint = 0 ; 727key -> modifiers = 0 ; 728 729if (!(tk -> flags & TERMKEY_FLAG_NOINTERPRET )&& tk -> c0 [codepoint ].sym != TERMKEY_SYM_UNKNOWN ) { 730key -> code .sym = tk -> c0 [codepoint ].sym ; 731key -> modifiers |=tk -> c0 [codepoint ].modifier_set ; 732 } 733 734if (!key -> code .sym ) { 735key -> type = TERMKEY_TYPE_UNICODE ; 736// Generically modified Unicode ought not report the SHIFT state, or else 737// we get into complications trying to report Shift-; vs : and so on... 738// In order to be able to represent Ctrl-Shift-A as CTRL modified 739// unicode A, we need to call Ctrl-A simply 'a', lowercase 740if (codepoint + 0x40 >='A' && codepoint + 0x40 <='Z' ) { 741// it's a letter - use lowercase instead 742key -> code .codepoint = codepoint + 0x60 ; 743 }else { 744key -> code .codepoint = codepoint + 0x40 ; 745 } 746key -> modifiers = TERMKEY_KEYMOD_CTRL ; 747 }else { 748key -> type = TERMKEY_TYPE_KEYSYM ; 749 } 750 }else if (codepoint == 0x7f && !(tk -> flags & TERMKEY_FLAG_NOINTERPRET )) { 751// ASCII DEL 752key -> type = TERMKEY_TYPE_KEYSYM ; 753key -> code .sym = TERMKEY_SYM_DEL ; 754key -> modifiers = 0 ; 755 }else if (codepoint > 0 && codepoint < 0x80 ) { 756// ASCII lowbyte range 757key -> type = TERMKEY_TYPE_UNICODE ; 758key -> code .codepoint = codepoint ; 759key -> modifiers = 0 ; 760 }else if (codepoint >=0x80 && codepoint < 0xa0 ) { 761// UTF-8 never starts with a C1 byte. So we can be sure of these 762key -> type = TERMKEY_TYPE_UNICODE ; 763key -> code .codepoint = codepoint - 0x40 ; 764key -> modifiers = TERMKEY_KEYMOD_CTRL |TERMKEY_KEYMOD_ALT ; 765 }else { 766// UTF-8 codepoint 767key -> type = TERMKEY_TYPE_UNICODE ; 768key -> code .codepoint = codepoint ; 769key -> modifiers = 0 ; 770 } 771 772termkey_canonicalise (tk ,key ); 773 774if (key -> type == TERMKEY_TYPE_UNICODE ) { 775fill_utf8 (key -> code .codepoint ,key -> utf8 ); 776 } 777} 778 779void termkey_canonicalise (TermKey * tk ,TermKeyKey * key ) 780{ 781int flags = tk -> canonflags ; 782 783if (flags & TERMKEY_CANON_SPACESYMBOL ) { 784if (key -> type == TERMKEY_TYPE_UNICODE && key -> code .codepoint == 0x20 ) { 785key -> type = TERMKEY_TYPE_KEYSYM ; 786key -> code .sym = TERMKEY_SYM_SPACE ; 787 } 788 }else { 789if (key -> type == TERMKEY_TYPE_KEYSYM && key -> code .sym == TERMKEY_SYM_SPACE ) { 790key -> type = TERMKEY_TYPE_UNICODE ; 791key -> code .codepoint = 0x20 ; 792fill_utf8 (key -> code .codepoint ,key -> utf8 ); 793 } 794 } 795 796if (flags & TERMKEY_CANON_DELBS ) { 797if (key -> type == TERMKEY_TYPE_KEYSYM && key -> code .sym == TERMKEY_SYM_DEL ) { 798key -> code .sym = TERMKEY_SYM_BACKSPACE ; 799 } 800 } 801} 802 803static TermKeyResult peekkey (TermKey * tk ,TermKeyKey * key ,int force ,size_t * nbytep ) 804{ 805int again = 0 ; 806 807if (!tk -> is_started ) { 808errno = EINVAL ; 809return TERMKEY_RES_ERROR ; 810 } 811 812// Press is the default event type. 813key -> event = TERMKEY_EVENT_PRESS ; 814 815#ifdef DEBUG 816fprintf (stderr ,"getkey(force=%d): buffer " ,force ); 817print_buffer (tk ); 818fprintf (stderr ,"\n" ); 819#endif 820 821if (tk -> hightide ) { 822tk -> buffstart += tk -> hightide ; 823tk -> buffcount -= tk -> hightide ; 824tk -> hightide = 0 ; 825 } 826 827TermKeyResult ret ; 828struct TermKeyDriverNode * p ; 829for (p = tk -> drivers ;p ;p = p -> next ) { 830ret = (p -> driver -> peekkey )(tk ,p -> info ,key ,force ,nbytep ); 831 832#ifdef DEBUG 833fprintf (stderr ,"Driver %s yields %s\n" ,p -> driver -> name ,res2str (ret )); 834#endif 835 836switch (ret ) { 837case TERMKEY_RES_KEY : 838#ifdef DEBUG 839print_key (tk ,key );fprintf (stderr ,"\n" ); 840#endif 841// Slide the data down to stop it running away 842 { 843size_t halfsize = tk -> buffsize /2 ; 844 845if (tk -> buffstart > halfsize ) { 846memcpy (tk -> buffer ,tk -> buffer + halfsize ,halfsize ); 847tk -> buffstart -= halfsize ; 848 } 849 } 850FALLTHROUGH ; 851case TERMKEY_RES_EOF : 852case TERMKEY_RES_ERROR : 853return ret ; 854 855case TERMKEY_RES_AGAIN : 856if (!force ) { 857again = 1 ; 858 } 859FALLTHROUGH ; 860case TERMKEY_RES_NONE : 861break ; 862 } 863 } 864 865if (again ) { 866return TERMKEY_RES_AGAIN ; 867 } 868 869ret = peekkey_simple (tk ,key ,force ,nbytep ); 870 871#ifdef DEBUG 872fprintf (stderr ,"getkey_simple(force=%d) yields %s\n" ,force ,res2str (ret )); 873if (ret == TERMKEY_RES_KEY ) { 874print_key (tk ,key );fprintf (stderr ,"\n" ); 875 } 876#endif 877 878return ret ; 879} 880 881static TermKeyResult peekkey_simple (TermKey * tk ,TermKeyKey * key ,int force ,size_t * nbytep ) 882{ 883if (tk -> buffcount == 0 ) { 884return tk -> is_closed ?TERMKEY_RES_EOF :TERMKEY_RES_NONE ; 885 } 886 887unsigned char b0 = CHARAT (0 ); 888 889if (b0 == 0x1b ) { 890// Escape-prefixed value? Might therefore be Alt+key 891if (tk -> buffcount == 1 ) { 892// This might be an <Esc> press, or it may want to be part of a longer 893// sequence 894if (!force ) { 895return TERMKEY_RES_AGAIN ; 896 } 897 898 (* tk -> method .emit_codepoint )(tk ,b0 ,key ); 899* nbytep = 1 ; 900return TERMKEY_RES_KEY ; 901 } 902 903// Try another key there 904tk -> buffstart ++ ; 905tk -> buffcount -- ; 906 907// Run the full driver 908TermKeyResult metakey_result = peekkey (tk ,key ,force ,nbytep ); 909 910tk -> buffstart -- ; 911tk -> buffcount ++ ; 912 913switch (metakey_result ) { 914case TERMKEY_RES_KEY : 915if (key -> event == TERMKEY_EVENT_REPEAT || key -> event == TERMKEY_EVENT_RELEASE ) { 916// A repeat or release event's modifiers are encoded in its escape sequence, so 917// a preceding Esc cannot be its Alt modifier, and can only be an <Esc> press. 918key -> event = TERMKEY_EVENT_PRESS ; 919 (* tk -> method .emit_codepoint )(tk ,b0 ,key ); 920* nbytep = 1 ; 921return TERMKEY_RES_KEY ; 922 } 923key -> modifiers |=TERMKEY_KEYMOD_ALT ; 924 (* nbytep )++ ; 925break ; 926 927case TERMKEY_RES_NONE : 928case TERMKEY_RES_EOF : 929case TERMKEY_RES_AGAIN : 930case TERMKEY_RES_ERROR : 931break ; 932 } 933 934return metakey_result ; 935 }else if (b0 < 0xa0 ) { 936// Single byte C0, G0 or C1 - C1 is never UTF-8 initial byte 937 (* tk -> method .emit_codepoint )(tk ,b0 ,key ); 938* nbytep = 1 ; 939return TERMKEY_RES_KEY ; 940 }else if (tk -> flags & TERMKEY_FLAG_UTF8 ) { 941// Some UTF-8 942int codepoint ; 943TermKeyResult res = parse_utf8 (tk -> buffer + tk -> buffstart ,tk -> buffcount ,& codepoint ,nbytep ); 944 945if (res == TERMKEY_RES_AGAIN && force ) { 946// There weren't enough bytes for a complete UTF-8 sequence but caller 947// demands an answer. About the best thing we can do here is eat as many 948// bytes as we have, and emit a UNICODE_INVALID. If the remaining bytes 949// arrive later, they'll be invalid too. 950codepoint = UNICODE_INVALID ; 951* nbytep = tk -> buffcount ; 952res = TERMKEY_RES_KEY ; 953 } 954 955key -> type = TERMKEY_TYPE_UNICODE ; 956key -> modifiers = 0 ; 957 (* tk -> method .emit_codepoint )(tk ,codepoint ,key ); 958return res ; 959 }else { 960// Non UTF-8 case - just report the raw byte 961key -> type = TERMKEY_TYPE_UNICODE ; 962key -> code .codepoint = b0 ; 963key -> modifiers = 0 ; 964 965key -> utf8 [0 ]= (char )key -> code .codepoint ; 966key -> utf8 [1 ]= 0 ; 967 968* nbytep = 1 ; 969 970return TERMKEY_RES_KEY ; 971 } 972} 973 974static TermKeyResult peekkey_mouse (TermKey * tk ,TermKeyKey * key ,size_t * nbytep ) 975{ 976if (tk -> buffcount < 3 ) { 977return TERMKEY_RES_AGAIN ; 978 } 979 980key -> type = TERMKEY_TYPE_MOUSE ; 981key -> code .mouse [0 ]= (char )CHARAT (0 )- 0x20 ; 982key -> code .mouse [1 ]= (char )CHARAT (1 )- 0x20 ; 983key -> code .mouse [2 ]= (char )CHARAT (2 )- 0x20 ; 984key -> code .mouse [3 ]= 0 ; 985 986key -> modifiers = (key -> code .mouse [0 ]& 0x1c ) >>2 ; 987key -> code .mouse [0 ] &= ~0x1c ; 988 989* nbytep = 3 ; 990return TERMKEY_RES_KEY ; 991} 992 993TermKeyResult termkey_getkey (TermKey * tk ,TermKeyKey * key ) 994{ 995size_t nbytes = 0 ; 996TermKeyResult ret = peekkey (tk ,key ,0 ,& nbytes ); 997 998if (ret == TERMKEY_RES_KEY ) { 999eat_bytes (tk ,nbytes ); 1000 } 1001 1002if (ret == TERMKEY_RES_AGAIN ) { 1003// Call peekkey() again in force mode to obtain whatever it can 1004 (void )peekkey (tk ,key ,1 ,& nbytes ); 1005 } 1006// Don't eat it yet though 1007 1008return ret ; 1009} 1010 1011TermKeyResult termkey_getkey_force (TermKey * tk ,TermKeyKey * key ) 1012{ 1013size_t nbytes = 0 ; 1014TermKeyResult ret = peekkey (tk ,key ,1 ,& nbytes ); 1015 1016if (ret == TERMKEY_RES_KEY ) { 1017eat_bytes (tk ,nbytes ); 1018 } 1019 1020return ret ; 1021} 1022 1023size_t termkey_push_bytes (TermKey * tk ,const char * bytes ,size_t len ) 1024{ 1025if (tk -> buffstart ) { 1026memmove (tk -> buffer ,tk -> buffer + tk -> buffstart ,tk -> buffcount ); 1027tk -> buffstart = 0 ; 1028 } 1029 1030// Not expecting it ever to be greater but doesn't hurt to handle that 1031if (tk -> buffcount >=tk -> buffsize ) { 1032errno = ENOMEM ; 1033return (size_t )-1 ; 1034 } 1035 1036if (len > tk -> buffsize - tk -> buffcount ) { 1037len = tk -> buffsize - tk -> buffcount ; 1038 } 1039 1040// memcpy(), not strncpy() in case of null bytes in input 1041memcpy (tk -> buffer + tk -> buffcount ,bytes ,len ); 1042tk -> buffcount += len ; 1043 1044return len ; 1045} 1046 1047TermKeySym termkey_register_keyname (TermKey * tk ,TermKeySym sym ,const char * name ) 1048{ 1049if (!sym ) { 1050sym = tk -> nkeynames ; 1051 } 1052 1053if (sym >=tk -> nkeynames ) { 1054const char ** new_keynames = xrealloc (tk -> keynames ,sizeof (new_keynames [0 ])* ((size_t )sym + 1 )); 1055 1056tk -> keynames = new_keynames ; 1057 1058// Fill in the hole 1059for (int i = tk -> nkeynames ;i < sym ;i ++ ) { 1060tk -> keynames [i ]= NULL ; 1061 } 1062 1063tk -> nkeynames = sym + 1 ; 1064 } 1065 1066tk -> keynames [sym ]= name ; 1067 1068return sym ; 1069} 1070 1071const char * termkey_get_keyname (TermKey * tk ,TermKeySym sym ) 1072{ 1073if (sym == TERMKEY_SYM_UNKNOWN ) { 1074return "UNKNOWN" ; 1075 } 1076 1077if (sym < tk -> nkeynames ) { 1078return tk -> keynames [sym ]; 1079 } 1080 1081return "UNKNOWN" ; 1082} 1083 1084static const char * termkey_lookup_keyname_format (TermKey * tk ,const char * str ,TermKeySym * sym , 1085TermKeyFormat format ) 1086{ 1087// We store an array, so we can't do better than a linear search. Doesn't 1088// matter because user won't be calling this too often 1089 1090for (* sym = 0 ;* sym < tk -> nkeynames ; (* sym )++ ) { 1091const char * thiskey = tk -> keynames [* sym ]; 1092if (!thiskey ) { 1093continue ; 1094 } 1095size_t len = strlen (thiskey ); 1096if (format & TERMKEY_FORMAT_LOWERSPACE ) { 1097const char * thisstr = str ; 1098if (strpncmp_camel (& thisstr ,& thiskey ,len )== 0 ) { 1099return thisstr ; 1100 } 1101 }else { 1102if (strncmp (str ,thiskey ,len )== 0 ) { 1103return (char * )str + len ; 1104 } 1105 } 1106 } 1107 1108return NULL ; 1109} 1110 1111const char * termkey_lookup_keyname (TermKey * tk ,const char * str ,TermKeySym * sym ) 1112{ 1113return termkey_lookup_keyname_format (tk ,str ,sym ,0 ); 1114} 1115 1116static TermKeySym register_c0 (TermKey * tk ,TermKeySym sym ,unsigned char ctrl ,const char * name ) 1117{ 1118return register_c0_full (tk ,sym ,0 ,0 ,ctrl ,name ); 1119} 1120 1121static TermKeySym register_c0_full (TermKey * tk ,TermKeySym sym ,int modifier_set ,int modifier_mask , 1122unsigned char ctrl ,const char * name ) 1123{ 1124if (ctrl >=0x20 ) { 1125errno = EINVAL ; 1126return -1 ; 1127 } 1128 1129if (name ) { 1130sym = termkey_register_keyname (tk ,sym ,name ); 1131 } 1132 1133tk -> c0 [ctrl ].sym = sym ; 1134tk -> c0 [ctrl ].modifier_set = modifier_set ; 1135tk -> c0 [ctrl ].modifier_mask = modifier_mask ; 1136 1137return sym ; 1138} 1139 1140static struct modnames { 1141const char * shift ,* alt ,* ctrl ; 1142} 1143modnames []= { 1144 {"S" ,"A" ,"C" },// 0 1145 {"Shift" ,"Alt" ,"Ctrl" },// LONGMOD 1146 {"S" ,"M" ,"C" },// ALTISMETA 1147 {"Shift" ,"Meta" ,"Ctrl" },// ALTISMETA+LONGMOD 1148 {"s" ,"a" ,"c" },// LOWERMOD 1149 {"shift" ,"alt" ,"ctrl" },// LOWERMOD+LONGMOD 1150 {"s" ,"m" ,"c" },// LOWERMOD+ALTISMETA 1151 {"shift" ,"meta" ,"ctrl" },// LOWERMOD+ALTISMETA+LONGMOD 1152}; 1153 1154size_t termkey_strfkey (TermKey * tk ,char * buffer ,size_t len ,TermKeyKey * key ,TermKeyFormat format ) 1155{ 1156size_t pos = 0 ; 1157size_t l = 0 ; 1158 1159struct modnames * mods = & modnames [!!(format & TERMKEY_FORMAT_LONGMOD )+ 1160 !!(format & TERMKEY_FORMAT_ALTISMETA )* 2 + 1161 !!(format & TERMKEY_FORMAT_LOWERMOD )* 4 ]; 1162 1163int wrapbracket = (format & TERMKEY_FORMAT_WRAPBRACKET ) 1164&& (key -> type != TERMKEY_TYPE_UNICODE || key -> modifiers != 0 ); 1165 1166char sep = (format & TERMKEY_FORMAT_SPACEMOD ) ?' ' :'-' ; 1167 1168if (format & TERMKEY_FORMAT_CARETCTRL 1169&& key -> type == TERMKEY_TYPE_UNICODE 1170&& key -> modifiers == TERMKEY_KEYMOD_CTRL ) { 1171long codepoint = key -> code .codepoint ; 1172 1173// Handle some of the special cases first 1174if (codepoint >='a' && codepoint <='z' ) { 1175l = (size_t )snprintf (buffer + pos ,len - pos ,wrapbracket ?"<^%c>" :"^%c" , 1176 (char )codepoint - 0x20 ); 1177if (l <=0 ) { 1178return pos ; 1179 } 1180pos += l ; 1181return pos ; 1182 }else if ((codepoint >='@' && codepoint < 'A' ) 1183|| (codepoint > 'Z' && codepoint <='_' )) { 1184l = (size_t )snprintf (buffer + pos ,len - pos ,wrapbracket ?"<^%c>" :"^%c" , (char )codepoint ); 1185if (l <=0 ) { 1186return pos ; 1187 } 1188pos += l ; 1189return pos ; 1190 } 1191 } 1192 1193if (wrapbracket ) { 1194l = (size_t )snprintf (buffer + pos ,len - pos ,"<" ); 1195if (l <=0 ) { 1196return pos ; 1197 } 1198pos += l ; 1199 } 1200 1201if (key -> modifiers & TERMKEY_KEYMOD_ALT ) { 1202l = (size_t )snprintf (buffer + pos ,len - pos ,"%s%c" ,mods -> alt ,sep ); 1203if (l <=0 ) { 1204return pos ; 1205 } 1206pos += l ; 1207 } 1208 1209if (key -> modifiers & TERMKEY_KEYMOD_CTRL ) { 1210l = (size_t )snprintf (buffer + pos ,len - pos ,"%s%c" ,mods -> ctrl ,sep ); 1211if (l <=0 ) { 1212return pos ; 1213 } 1214pos += l ; 1215 } 1216 1217if (key -> modifiers & TERMKEY_KEYMOD_SHIFT ) { 1218l = (size_t )snprintf (buffer + pos ,len - pos ,"%s%c" ,mods -> shift ,sep ); 1219if (l <=0 ) { 1220return pos ; 1221 } 1222pos += l ; 1223 } 1224 1225switch (key -> type ) { 1226case TERMKEY_TYPE_UNICODE : 1227if (!key -> utf8 [0 ]) {// In case of user-supplied key structures 1228fill_utf8 (key -> code .codepoint ,key -> utf8 ); 1229 } 1230l = (size_t )snprintf (buffer + pos ,len - pos ,"%s" ,key -> utf8 ); 1231break ; 1232case TERMKEY_TYPE_KEYSYM : { 1233const char * name = termkey_get_keyname (tk ,key -> code .sym ); 1234if (format & TERMKEY_FORMAT_LOWERSPACE ) { 1235l = (size_t )snprint_cameltospaces (buffer + pos ,len - pos ,name ); 1236 }else { 1237l = (size_t )snprintf (buffer + pos ,len - pos ,"%s" ,name ); 1238 } 1239 } 1240break ; 1241case TERMKEY_TYPE_FUNCTION : 1242l = (size_t )snprintf (buffer + pos ,len - pos ,"%c%d" , 1243 (format & TERMKEY_FORMAT_LOWERSPACE ?'f' :'F' ),key -> code .number ); 1244break ; 1245case TERMKEY_TYPE_MOUSE : { 1246TermKeyMouseEvent ev ; 1247int button ; 1248int line ,col ; 1249termkey_interpret_mouse (tk ,key ,& ev ,& button ,& line ,& col ); 1250 1251l = (size_t )snprintf (buffer + pos ,len - pos ,"Mouse%s(%d)" , 1252evnames [ev ],button ); 1253 1254if (format & TERMKEY_FORMAT_MOUSE_POS ) { 1255if (l <=0 ) { 1256return pos ; 1257 } 1258pos += l ; 1259 1260l = (size_t )snprintf (buffer + pos ,len - pos ," @ (%u,%u)" ,col ,line ); 1261 } 1262 } 1263break ; 1264case TERMKEY_TYPE_POSITION : 1265l = (size_t )snprintf (buffer + pos ,len - pos ,"Position" ); 1266break ; 1267case TERMKEY_TYPE_MODEREPORT : { 1268int initial ,mode ,value ; 1269termkey_interpret_modereport (tk ,key ,& initial ,& mode ,& value ); 1270if (initial ) { 1271l = (size_t )snprintf (buffer + pos ,len - pos ,"Mode(%c%d=%d)" ,initial ,mode ,value ); 1272 }else { 1273l = (size_t )snprintf (buffer + pos ,len - pos ,"Mode(%d=%d)" ,mode ,value ); 1274 } 1275 } 1276break ; 1277case TERMKEY_TYPE_DCS : 1278l = (size_t )snprintf (buffer + pos ,len - pos ,"DCS" ); 1279break ; 1280case TERMKEY_TYPE_OSC : 1281l = (size_t )snprintf (buffer + pos ,len - pos ,"OSC" ); 1282break ; 1283case TERMKEY_TYPE_APC : 1284l = (size_t )snprintf (buffer + pos ,len - pos ,"APC" ); 1285break ; 1286case TERMKEY_TYPE_UNKNOWN_CSI : 1287l = (size_t )snprintf (buffer + pos ,len - pos ,"CSI %c" ,key -> code .number & 0xff ); 1288break ; 1289 } 1290 1291if (l <=0 ) { 1292return pos ; 1293 } 1294pos += l ; 1295 1296if (wrapbracket ) { 1297l = (size_t )snprintf (buffer + pos ,len - pos ,">" ); 1298if (l <=0 ) { 1299return pos ; 1300 } 1301pos += l ; 1302 } 1303 1304return pos ; 1305}