Exemplo n.º 1
0
static const char *ui_getkey(Ui *ui) {
    UiCurses *uic = (UiCurses*)ui;
    TermKeyKey key;
    TermKeyResult ret = termkey_getkey(uic->termkey, &key);

    if (ret == TERMKEY_RES_EOF) {
        int tty = open("/dev/tty", O_RDWR);
        if (tty == -1)
            goto fatal;
        if (tty != STDIN_FILENO && dup2(tty, STDIN_FILENO) == -1)
            goto fatal;
        termkey_destroy(uic->termkey);
        if (!(uic->termkey = ui_termkey_new(STDIN_FILENO)))
            goto fatal;
        return NULL;
    }

    if (ret == TERMKEY_RES_AGAIN) {
        struct pollfd fd;
        fd.fd = STDIN_FILENO;
        fd.events = POLLIN;
        if (poll(&fd, 1, termkey_get_waittime(uic->termkey)) == 0)
            ret = termkey_getkey_force(uic->termkey, &key);
    }

    if (ret != TERMKEY_RES_KEY)
        return NULL;
    termkey_strfkey(uic->termkey, uic->key, sizeof(uic->key), &key, TERMKEY_FORMAT_VIM);
    return uic->key;

fatal:
    ui_die_msg(ui, "Failed to re-open stdin as /dev/tty\n");
    return NULL;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
  TermKey   *tk;
  TermKeyKey key;
  int        line, col;

  plan_tests(8);

  tk = termkey_new_abstract("vt100", 0);

  termkey_push_bytes(tk, "\e[?15;7R", 8);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY for position report");

  is_int(key.type, TERMKEY_TYPE_POSITION, "key.type for position report");

  is_int(termkey_interpret_position(tk, &key, &line, &col), TERMKEY_RES_KEY, "interpret_position yields RES_KEY");

  is_int(line, 15, "line for position report");
  is_int(col,   7, "column for position report");

  /* A plain CSI R is likely to be <F3> though.
   * This is tricky :/
   */
  termkey_push_bytes(tk, "\e[R", 3);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY for <F3>");

  is_int(key.type, TERMKEY_TYPE_FUNCTION, "key.type for <F3>");
  is_int(key.code.number, 3, "key.code.number for <F3>");

  termkey_destroy(tk);

  return exit_status();
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
  TermKey   *tk;

  plan_tests(6);

  tk = termkey_new_abstract("vt100", 0);

  ok(!!tk, "termkey_new_abstract");

  is_int(termkey_get_buffer_size(tk), 256, "termkey_get_buffer_size");
  ok(termkey_is_started(tk), "termkey_is_started true after construction");

  termkey_stop(tk);

  ok(!termkey_is_started(tk), "termkey_is_started false after termkey_stop()");

  termkey_start(tk);

  ok(termkey_is_started(tk), "termkey_is_started true after termkey_start()");

  termkey_destroy(tk);

  ok(1, "termkey_free");

  return exit_status();
}
Exemplo n.º 4
0
void ui_curses_free(Ui *ui) {
	UiCurses *uic = (UiCurses*)ui;
	if (!uic)
		return;
	while (uic->windows)
		ui_window_free((UiWin*)uic->windows);
	endwin();
	if (uic->termkey)
		termkey_destroy(uic->termkey);
	free(uic);
}
Exemplo n.º 5
0
void ui_curses_free(Ui *ui) {
    UiCurses *uic = (UiCurses*)ui;
    if (!uic)
        return;
    while (uic->windows)
        ui_window_free((UiWin*)uic->windows);
    endwin();
    if (uic->termkey)
        termkey_destroy(uic->termkey);
    tcsetattr(STDERR_FILENO, TCSANOW, &uic->tio);
    free(uic);
}
Exemplo n.º 6
0
void input_cleanup(void)
{
  for (int j = 0; j < 2; j++)
  for (int i = 0; i < 256; i++) {
    if (!key_maps[j][i])
      continue;
    free(key_maps[j][i]->lhs);
    free(key_maps[j][i]->rhs);
    free(key_maps[j][i]);
  }
  termkey_destroy(tk);
}
Exemplo n.º 7
0
int main(int argc, char *argv[])
{
  TERMKEY_CHECK_VERSION;

  TermKey *tk = termkey_new(0, 0);

  if(!tk) {
    fprintf(stderr, "Cannot allocate termkey instance\n");
    exit(1);
  }

  struct pollfd fd;

  fd.fd = 0; /* the file descriptor we passed to termkey_new() */
  fd.events = POLLIN;

  TermKeyResult ret;
  TermKeyKey key;

  int running = 1;
  int nextwait = -1;

  while(running) {
    if(poll(&fd, 1, nextwait) == 0) {
      // Timed out
      if(termkey_getkey_force(tk, &key) == TERMKEY_RES_KEY)
        on_key(tk, &key);
    }

    if(fd.revents & (POLLIN|POLLHUP|POLLERR))
      termkey_advisereadable(tk);

    while((ret = termkey_getkey(tk, &key)) == TERMKEY_RES_KEY) {
      on_key(tk, &key);

      if(key.type == TERMKEY_TYPE_UNICODE &&
         key.modifiers & TERMKEY_KEYMOD_CTRL &&
         (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
        running = 0;
    }

    if(ret == TERMKEY_RES_AGAIN)
      nextwait = termkey_get_waittime(tk);
    else
      nextwait = -1;
  }

  termkey_destroy(tk);
}
Exemplo n.º 8
0
void CoreManager::InputUnInit()
{
  termkey_destroy(tk);
  tk = NULL;

  g_source_remove(io_input_channel_id);
  io_input_channel_id = 0;
  g_io_channel_unref(io_input_channel);
  io_input_channel = NULL;

  if (pipe_valid) {
    g_source_remove(resize_channel_id);
    resize_channel_id = 0;
    g_io_channel_unref(resize_channel);
    resize_channel = NULL;
    close(pipefd[0]);
    close(pipefd[1]);
  }
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
  TermKey   *tk;
  TermKeyKey key;

  plan_tests(57);

  tk = termkey_new_abstract("vt100", TERMKEY_FLAG_UTF8);

  termkey_push_bytes(tk, "a", 1);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY low ASCII");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type low ASCII");
  is_int(key.code.number, 'a',                  "key.code.number low ASCII");

  /* 2-byte UTF-8 range is U+0080 to U+07FF (0xDF 0xBF) */
  /* However, we'd best avoid the C1 range, so we'll start at U+00A0 (0xC2 0xA0) */

  termkey_push_bytes(tk, "\xC2\xA0", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 low");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 2 low");
  is_int(key.code.number, 0x00A0,               "key.code.number UTF-8 2 low");

  termkey_push_bytes(tk, "\xDF\xBF", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 high");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 2 high");
  is_int(key.code.number, 0x07FF,               "key.code.number UTF-8 2 high");

  /* 3-byte UTF-8 range is U+0800 (0xE0 0xA0 0x80) to U+FFFD (0xEF 0xBF 0xBD) */

  termkey_push_bytes(tk, "\xE0\xA0\x80", 3);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 low");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 3 low");
  is_int(key.code.number, 0x0800,               "key.code.number UTF-8 3 low");

  termkey_push_bytes(tk, "\xEF\xBF\xBD", 3);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 high");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 3 high");
  is_int(key.code.number, 0xFFFD,               "key.code.number UTF-8 3 high");

  /* 4-byte UTF-8 range is U+10000 (0xF0 0x90 0x80 0x80) to U+10FFFF (0xF4 0x8F 0xBF 0xBF) */

  termkey_push_bytes(tk, "\xF0\x90\x80\x80", 4);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 low");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 4 low");
  is_int(key.code.number, 0x10000,              "key.code.number UTF-8 4 low");

  termkey_push_bytes(tk, "\xF4\x8F\xBF\xBF", 4);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 high");
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type UTF-8 4 high");
  is_int(key.code.number, 0x10FFFF,             "key.code.number UTF-8 4 high");

  /* Invalid continuations */

  termkey_push_bytes(tk, "\xC2!", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 invalid cont");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 2 invalid cont");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 2 invalid after");

  termkey_push_bytes(tk, "\xE0!", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid cont");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 3 invalid cont");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 3 invalid after");

  termkey_push_bytes(tk, "\xE0\xA0!", 3);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid cont 2");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 3 invalid cont 2");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 3 invalid after");

  termkey_push_bytes(tk, "\xF0!", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after");

  termkey_push_bytes(tk, "\xF0\x90!", 3);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont 2");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont 2");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after");

  termkey_push_bytes(tk, "\xF0\x90\x80!", 4);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid cont 3");
  is_int(key.code.number, 0xFFFD, "key.code.number UTF-8 4 invalid cont 3");
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 invalid after");
  is_int(key.code.number, '!', "key.code.number UTF-8 4 invalid after");

  /* Partials */

  termkey_push_bytes(tk, "\xC2", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 2 partial");

  termkey_push_bytes(tk, "\xA0", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 2 partial");
  is_int(key.code.number, 0x00A0, "key.code.number UTF-8 2 partial");

  termkey_push_bytes(tk, "\xE0", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 3 partial");

  termkey_push_bytes(tk, "\xA0", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 3 partial");

  termkey_push_bytes(tk, "\x80", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 3 partial");
  is_int(key.code.number, 0x0800, "key.code.number UTF-8 3 partial");

  termkey_push_bytes(tk, "\xF0", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial");

  termkey_push_bytes(tk, "\x90", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial");

  termkey_push_bytes(tk, "\x80", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN UTF-8 4 partial");

  termkey_push_bytes(tk, "\x80", 1);
  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY UTF-8 4 partial");
  is_int(key.code.number, 0x10000, "key.code.number UTF-8 4 partial");

  termkey_destroy(tk);

  return exit_status();
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
  TermKey   *tk;
  TermKeyKey key;
  char       buffer[16];
  size_t     len;

  plan_tests(44);

  tk = termkey_new_abstract("vt100", 0);

  key.type = TERMKEY_TYPE_UNICODE;
  key.code.codepoint = 'A';
  key.modifiers = 0;
  key.utf8[0] = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 1, "length for unicode/A/0");
  is_str(buffer, "A", "buffer for unicode/A/0");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_WRAPBRACKET);
  is_int(len, 1, "length for unicode/A/0 wrapbracket");
  is_str(buffer, "A", "buffer for unicode/A/0 wrapbracket");

  key.type = TERMKEY_TYPE_UNICODE;
  key.code.codepoint = 'b';
  key.modifiers = TERMKEY_KEYMOD_CTRL;
  key.utf8[0] = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 3, "length for unicode/b/CTRL");
  is_str(buffer, "C-b", "buffer for unicode/b/CTRL");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_LONGMOD);
  is_int(len, 6, "length for unicode/b/CTRL longmod");
  is_str(buffer, "Ctrl-b", "buffer for unicode/b/CTRL longmod");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key,
      TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_SPACEMOD);
  is_int(len, 6, "length for unicode/b/CTRL longmod|spacemod");
  is_str(buffer, "Ctrl b", "buffer for unicode/b/CTRL longmod|spacemod");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key,
      TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_LOWERMOD);
  is_int(len, 6, "length for unicode/b/CTRL longmod|lowermod");
  is_str(buffer, "ctrl-b", "buffer for unicode/b/CTRL longmod|lowermod");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key,
      TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_SPACEMOD|TERMKEY_FORMAT_LOWERMOD);
  is_int(len, 6, "length for unicode/b/CTRL longmod|spacemod|lowermode");
  is_str(buffer, "ctrl b", "buffer for unicode/b/CTRL longmod|spacemod|lowermode");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_CARETCTRL);
  is_int(len, 2, "length for unicode/b/CTRL caretctrl");
  is_str(buffer, "^B", "buffer for unicode/b/CTRL caretctrl");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_WRAPBRACKET);
  is_int(len, 5, "length for unicode/b/CTRL wrapbracket");
  is_str(buffer, "<C-b>", "buffer for unicode/b/CTRL wrapbracket");

  key.type = TERMKEY_TYPE_UNICODE;
  key.code.codepoint = 'c';
  key.modifiers = TERMKEY_KEYMOD_ALT;
  key.utf8[0] = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 3, "length for unicode/c/ALT");
  is_str(buffer, "A-c", "buffer for unicode/c/ALT");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_LONGMOD);
  is_int(len, 5, "length for unicode/c/ALT longmod");
  is_str(buffer, "Alt-c", "buffer for unicode/c/ALT longmod");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_ALTISMETA);
  is_int(len, 3, "length for unicode/c/ALT altismeta");
  is_str(buffer, "M-c", "buffer for unicode/c/ALT altismeta");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_LONGMOD|TERMKEY_FORMAT_ALTISMETA);
  is_int(len, 6, "length for unicode/c/ALT longmod|altismeta");
  is_str(buffer, "Meta-c", "buffer for unicode/c/ALT longmod|altismeta");

  key.type = TERMKEY_TYPE_KEYSYM;
  key.code.sym = TERMKEY_SYM_UP;
  key.modifiers = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 2, "length for sym/Up/0");
  is_str(buffer, "Up", "buffer for sym/Up/0");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_WRAPBRACKET);
  is_int(len, 4, "length for sym/Up/0 wrapbracket");
  is_str(buffer, "<Up>", "buffer for sym/Up/0 wrapbracket");

  key.type = TERMKEY_TYPE_KEYSYM;
  key.code.sym = TERMKEY_SYM_PAGEUP;
  key.modifiers = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 6, "length for sym/PageUp/0");
  is_str(buffer, "PageUp", "buffer for sym/PageUp/0");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_LOWERSPACE);
  is_int(len, 7, "length for sym/PageUp/0 lowerspace");
  is_str(buffer, "page up", "buffer for sym/PageUp/0 lowerspace");

  /* If size of buffer is too small, strfkey should return something consistent */
  len = termkey_strfkey(tk, buffer, 4, &key, 0);
  is_int(len, 6, "length for sym/PageUp/0");
  is_str(buffer, "Pag", "buffer of len 4 for sym/PageUp/0");

  len = termkey_strfkey(tk, buffer, 4, &key, TERMKEY_FORMAT_LOWERSPACE);
  is_int(len, 7, "length for sym/PageUp/0 lowerspace");
  is_str(buffer, "pag", "buffer of len 4 for sym/PageUp/0 lowerspace");

  key.type = TERMKEY_TYPE_FUNCTION;
  key.code.number = 5;
  key.modifiers = 0;

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, 0);
  is_int(len, 2, "length for func/5/0");
  is_str(buffer, "F5", "buffer for func/5/0");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_WRAPBRACKET);
  is_int(len, 4, "length for func/5/0 wrapbracket");
  is_str(buffer, "<F5>", "buffer for func/5/0 wrapbracket");

  len = termkey_strfkey(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_LOWERSPACE);
  is_int(len, 2, "length for func/5/0 lowerspace");
  is_str(buffer, "f5", "buffer for func/5/0 lowerspace");

  termkey_destroy(tk);

  return exit_status();
}
Exemplo n.º 11
0
int main(int argc, char *argv[])
{
  TermKey   *tk;
  TermKeyKey key;
  const char *str;

  plan_tests(23);

  tk = termkey_new_abstract("xterm", 0);

  // 7bit DCS
  termkey_push_bytes(tk, "\eP1$r1 q\e\\", 10);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY for DCS");

  is_int(key.type,        TERMKEY_TYPE_DCS, "key.type for DCS");
  is_int(key.modifiers,   0,                "key.modifiers for DCS");

  is_int(termkey_interpret_string(tk, &key, &str), TERMKEY_RES_KEY, "termkey_interpret_string() gives string");
  is_str(str, "1$r1 q", "termkey_interpret_string() yields correct string");

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_NONE, "getkey again yields RES_NONE");

  // 8bit DCS
  termkey_push_bytes(tk, "\x90""1$r2 q""\x9c", 8);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY for DCS");

  is_int(key.type,        TERMKEY_TYPE_DCS, "key.type for DCS");
  is_int(key.modifiers,   0,                "key.modifiers for DCS");

  is_int(termkey_interpret_string(tk, &key, &str), TERMKEY_RES_KEY, "termkey_interpret_string() gives string");
  is_str(str, "1$r2 q", "termkey_interpret_string() yields correct string");

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_NONE, "getkey again yields RES_NONE");

  // 7bit OSC
  termkey_push_bytes(tk, "\e]15;abc\e\\", 10);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_KEY, "getkey yields RES_KEY for OSC");

  is_int(key.type,        TERMKEY_TYPE_OSC, "key.type for OSC");
  is_int(key.modifiers,   0,                "key.modifiers for OSC");

  is_int(termkey_interpret_string(tk, &key, &str), TERMKEY_RES_KEY, "termkey_interpret_string() gives string");
  is_str(str, "15;abc", "termkey_interpret_string() yields correct string");

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_NONE, "getkey again yields RES_NONE");

  // False alarm
  termkey_push_bytes(tk, "\eP", 2);

  is_int(termkey_getkey(tk, &key), TERMKEY_RES_AGAIN, "getkey yields RES_AGAIN for false alarm");

  is_int(termkey_getkey_force(tk, &key), TERMKEY_RES_KEY, "getkey_force yields RES_KEY for false alarm");

  is_int(key.type,           TERMKEY_TYPE_UNICODE, "key.type for false alarm");
  is_int(key.code.codepoint, 'P',                  "key.code.codepoint for false alarm");
  is_int(key.modifiers,      TERMKEY_KEYMOD_ALT,   "key.modifiers for false alarm");

  termkey_destroy(tk);

  return exit_status();
}
Exemplo n.º 12
0
int main(int argc, char *argv[])
{
  TermKey      *tk;
  TermKeyKey    key;
  char         *endp;

#define CLEAR_KEY do { key.type = -1; key.code.codepoint = -1; key.modifiers = -1; key.utf8[0] = 0; } while(0)

  plan_tests(53);

  tk = termkey_new_abstract("vt100", 0);

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "A", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/A/0");
  is_int(key.code.codepoint, 'A',               "key.code.codepoint for unicode/A/0");
  is_int(key.modifiers,   0,                    "key.modifiers for unicode/A/0");
  is_str(key.utf8,        "A",                  "key.utf8 for unicode/A/0");
  is_str(endp, "", "consumed entire input for unicode/A/0");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "A and more", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/A/0 trailing");
  is_int(key.code.codepoint, 'A',               "key.code.codepoint for unicode/A/0 trailing");
  is_int(key.modifiers,   0,                    "key.modifiers for unicode/A/0 trailing");
  is_str(key.utf8,        "A",                  "key.utf8 for unicode/A/0 trailing");
  is_str(endp, " and more", "points at string tail for unicode/A/0 trailing");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "C-b", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL");
  is_int(key.code.codepoint, 'b',               "key.code.codepoint for unicode/b/CTRL");
  is_int(key.modifiers,   TERMKEY_KEYMOD_CTRL,  "key.modifiers for unicode/b/CTRL");
  is_str(key.utf8,        "b",                  "key.utf8 for unicode/b/CTRL");
  is_str(endp, "", "consumed entire input for unicode/b/CTRL");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "Ctrl-b", &key, TERMKEY_FORMAT_LONGMOD);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL longmod");
  is_int(key.code.codepoint, 'b',               "key.code.codepoint for unicode/b/CTRL longmod");
  is_int(key.modifiers,   TERMKEY_KEYMOD_CTRL,  "key.modifiers for unicode/b/CTRL longmod");
  is_str(key.utf8,        "b",                  "key.utf8 for unicode/b/CTRL longmod");
  is_str(endp, "", "consumed entire input for unicode/b/CTRL longmod");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "^B", &key, TERMKEY_FORMAT_CARETCTRL);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/b/CTRL caretctrl");
  is_int(key.code.codepoint, 'b',               "key.code.codepoint for unicode/b/CTRL caretctrl");
  is_int(key.modifiers,   TERMKEY_KEYMOD_CTRL,  "key.modifiers for unicode/b/CTRL caretctrl");
  is_str(key.utf8,        "b",                  "key.utf8 for unicode/b/CTRL caretctrl");
  is_str(endp, "", "consumed entire input for unicode/b/CTRL caretctrl");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "A-c", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT");
  is_int(key.code.codepoint, 'c',               "key.code.codepoint for unicode/c/ALT");
  is_int(key.modifiers,   TERMKEY_KEYMOD_ALT,   "key.modifiers for unicode/c/ALT");
  is_str(key.utf8,        "c",                  "key.utf8 for unicode/c/ALT");
  is_str(endp, "", "consumed entire input for unicode/c/ALT");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "Alt-c", &key, TERMKEY_FORMAT_LONGMOD);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT longmod");
  is_int(key.code.codepoint, 'c',               "key.code.codepoint for unicode/c/ALT longmod");
  is_int(key.modifiers,   TERMKEY_KEYMOD_ALT,   "key.modifiers for unicode/c/ALT longmod");
  is_str(key.utf8,        "c",                  "key.utf8 for unicode/c/ALT longmod");
  is_str(endp, "", "consumed entire input for unicode/c/ALT longmod");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "M-c", &key, TERMKEY_FORMAT_ALTISMETA);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta");
  is_int(key.code.codepoint, 'c',               "key.code.codepoint for unicode/c/ALT altismeta");
  is_int(key.modifiers,   TERMKEY_KEYMOD_ALT,   "key.modifiers for unicode/c/ALT altismeta");
  is_str(key.utf8,        "c",                  "key.utf8 for unicode/c/ALT altismeta");
  is_str(endp, "", "consumed entire input for unicode/c/ALT altismeta");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "Meta-c", &key, TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_LONGMOD);
  is_int(key.type,        TERMKEY_TYPE_UNICODE, "key.type for unicode/c/ALT altismeta+longmod");
  is_int(key.code.codepoint, 'c',               "key.code.codepoint for unicode/c/ALT altismeta+longmod");
  is_int(key.modifiers,   TERMKEY_KEYMOD_ALT,   "key.modifiers for unicode/c/ALT altismeta+longmod");
  is_str(key.utf8,        "c",                  "key.utf8 for unicode/c/ALT altismeta+longmod");
  is_str(endp, "", "consumed entire input for unicode/c/ALT altismeta+longmod");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "Up", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_KEYSYM, "key.type for sym/Up/0");
  is_int(key.code.sym,    TERMKEY_SYM_UP,      "key.code.codepoint for sym/Up/0");
  is_int(key.modifiers,   0,                   "key.modifiers for sym/Up/0");
  is_str(endp, "", "consumed entire input for sym/Up/0");

  CLEAR_KEY;
  endp = termkey_strpkey(tk, "F5", &key, 0);
  is_int(key.type,        TERMKEY_TYPE_FUNCTION, "key.type for func/5/0");
  is_int(key.code.number, 5,                     "key.code.number for func/5/0");
  is_int(key.modifiers,   0,                     "key.modifiers for func/5/0");
  is_str(endp, "", "consumed entire input for func/5/0");

  termkey_destroy(tk);

  return exit_status();
}
Exemplo n.º 13
0
Arquivo: demo.c Projeto: zonbrisad/ore
int main(int argc, char *argv[])
{
  TERMKEY_CHECK_VERSION;

  int mouse = 0;
  int mouse_proto = 0;
  TermKeyFormat format = TERMKEY_FORMAT_VIM;

  char buffer[50];
  TermKey *tk;

  int opt;
  while((opt = getopt(argc, argv, "m::p:")) != -1) {
    switch(opt) {
    case 'm':
      if(optarg)
        mouse = atoi(optarg);
      else
        mouse = 1000;

      break;

    case 'p':
      mouse_proto = atoi(optarg);
      break;

    default:
      fprintf(stderr, "Usage: %s [-m]\n", argv[0]);
      return 1;
    }
  }

  tk = termkey_new(0, TERMKEY_FLAG_SPACESYMBOL|TERMKEY_FLAG_CTRLC);

  if(!tk) {
    fprintf(stderr, "Cannot allocate termkey instance\n");
    exit(1);
  }

  TermKeyResult ret;
  TermKeyKey key;

  if(mouse) {
    printf("\033[?%dhMouse mode active\n", mouse);
    if(mouse_proto)
      printf("\033[?%dh", mouse_proto);
  }

  while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
    if(ret == TERMKEY_RES_KEY) {
      termkey_strfkey(tk, buffer, sizeof buffer, &key, format);
      if(key.type == TERMKEY_TYPE_MOUSE) {
        int line, col;
        termkey_interpret_mouse(tk, &key, NULL, NULL, &line, &col);
        printf("%s at line=%d, col=%d)\n", buffer, line, col);
      }
      else if(key.type == TERMKEY_TYPE_POSITION) {
        int line, col;
        termkey_interpret_position(tk, &key, &line, &col);
        printf("Cursor position report at line=%d, col=%d)\n", line, col);
      }
      else {
        printf("%s\n", buffer);
      }

      if(key.type == TERMKEY_TYPE_UNICODE &&
         key.modifiers & TERMKEY_KEYMOD_CTRL &&
         (key.code.codepoint == 'C' || key.code.codepoint == 'c'))
        break;

      if(key.type == TERMKEY_TYPE_UNICODE &&
         key.modifiers == 0 &&
         key.code.codepoint == '?') {
        printf("\033[6n");
        fflush(stdout);
      }
    }
    else if(ret == TERMKEY_RES_ERROR) {
      if(errno != EINTR) {
        perror("termkey_waitkey");
        break;
      }
      printf("Interrupted by signal\n");
    }
  }

  if(mouse)
    printf("\033[?%dlMouse mode deactivated\n", mouse);

  termkey_destroy(tk);
}