Пример #1
0
int load_termcap(char *termtype){
#if HAVE_TERMINFO || HAVE_TERMCAP

#if HAVE_TERMINFO
    use_env(TRUE);
    int ret;
    if (setupterm(termtype, 1, &ret) != OK) {
        /* try again, with with "ansi" terminal if it was unset before */
        if (!termtype)
            termtype = getenv("TERM");
        if (!termtype || *termtype == '\0')
            termtype = "ansi";

        if (setupterm(termtype, 1, &ret) != OK) {
            if (ret < 0) {
                printf("Could not access the 'terminfo' data base.\n");
                return 0;
            } else {
                printf("Couldn't use terminal `%s' for input.\n", termtype);
                return 0;
            }
        }
    }
#else
    static char term_buffer[2048];
    if (!termtype) termtype = getenv("TERM");
    if (!termtype) termtype = "ansi";
    int success = tgetent(term_buffer, termtype);
    if (success < 0) {
        printf("Could not access the 'termcap' data base.\n");
        return 0;
    } else if (success == 0) {
        printf("Terminal type `%s' is not defined.\n", termtype);
        return 0;
    }
#endif
    ensure_cap(&termcap_buf, 2048);

    static char term_buf[64];
    char *buf_ptr = &term_buf[0];

    erase_to_end_of_line = tgetstr("ce", &buf_ptr);

    screen_width  = tgetnum("co");
    screen_height = tgetnum("li");
    if (screen_width < 1 || screen_width > 255)
        screen_width = 80;
    if (screen_height < 1 || screen_height > 255)
        screen_height = 24;

    term_smkx = tgetstr("ks", &buf_ptr);
    term_rmkx = tgetstr("ke", &buf_ptr);

    cap_key_pair keys[] = {
        {"kP", MP_KEY_PGUP}, {"kN", MP_KEY_PGDWN}, {"kh", MP_KEY_HOME}, {"kH", MP_KEY_END},
        {"kI", MP_KEY_INS},  {"kD", MP_KEY_DEL},  /* on PC keyboards */ {"@7", MP_KEY_END},

        {"kl", MP_KEY_LEFT}, {"kd", MP_KEY_DOWN}, {"ku", MP_KEY_UP}, {"kr", MP_KEY_RIGHT},

        {"do", MP_KEY_ENTER},
        {"kb", MP_KEY_BS},

        {"k1", MP_KEY_F+1},  {"k2", MP_KEY_F+2},  {"k3", MP_KEY_F+3},
        {"k4", MP_KEY_F+4},  {"k5", MP_KEY_F+5},  {"k6", MP_KEY_F+6},
        {"k7", MP_KEY_F+7},  {"k8", MP_KEY_F+8},  {"k9", MP_KEY_F+9},
        {"k;", MP_KEY_F+10}, {"k0", MP_KEY_F+0},

        /* K2 is the keypad center */
        {"K2", MP_KEY_KP5},

        /* EOL */
        {NULL},
    };
    for (int i = 0; keys[i].id; i++) {
        termcap_add(keys[i]);
    }
    termcap_add_extra_f_keys();
#endif

    /* special cases (hardcoded, no need for HAVE_TERMCAP) */

    /* it's important to use keys_push_once as we can't have duplicates */

    /* many terminals, for emacs compatibility, use 0x7f instead of ^H
       when typing backspace, even when the 'kb' cap says otherwise. */
    keys_push_once("\177", MP_KEY_BS);

    /* mintty always sends these when using the numpad arrows,
       even in application mode, for telling them from regular arrows. */
    keys_push_once("\033[A", MP_KEY_UP);
    keys_push_once("\033[B", MP_KEY_DOWN);
    keys_push_once("\033[C", MP_KEY_RIGHT);
    keys_push_once("\033[D", MP_KEY_LEFT);

    /* mintty uses this instead of the "K2" cap for keypad center */
    keys_push_once("\033OE", MP_KEY_KP5);

    return getch2_keys.len;
}
Пример #2
0
int load_termcap(char *termtype){
  if(!termtype) termtype=getenv("TERM");
  if(!termtype) termtype="unknown";
  success=tgetent(term_buffer, termtype);
  if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
  if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}

  screen_width=tgetnum("co");
  screen_height=tgetnum("li");
  if(screen_width<1 || screen_width>255) screen_width=80;
  if(screen_height<1 || screen_height>255) screen_height=24;
  erase_to_end_of_line= tgetstr("cd",&term_p);

  termcap_add("kP",KEY_PGUP);
  termcap_add("kN",KEY_PGDWN);
  termcap_add("kh",KEY_HOME);
  termcap_add("kH",KEY_END);
  termcap_add("kI",KEY_INS);
  termcap_add("kD",KEY_DEL);
  termcap_add("kb",KEY_BS);
  termcap_add("kl",KEY_LEFT);
  termcap_add("kd",KEY_DOWN);
  termcap_add("ku",KEY_UP);
  termcap_add("kr",KEY_RIGHT);
  termcap_add("k0",KEY_F+0);
  termcap_add("k1",KEY_F+1);
  termcap_add("k2",KEY_F+2);
  termcap_add("k3",KEY_F+3);
  termcap_add("k4",KEY_F+4);
  termcap_add("k5",KEY_F+5);
  termcap_add("k6",KEY_F+6);
  termcap_add("k7",KEY_F+7);
  termcap_add("k8",KEY_F+8);
  termcap_add("k9",KEY_F+9);
  termcap_add("k;",KEY_F+10);
  return getch2_key_db;
}