예제 #1
0
파일: ui-curses.c 프로젝트: Armavica/vis
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;
}
예제 #2
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);
}
예제 #3
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_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;
}
예제 #4
0
gboolean CoreManager::io_input(GIOChannel *source, GIOCondition cond)
{
  if (io_input_timeout_conn.connected())
    io_input_timeout_conn.disconnect();

  termkey_advisereadable(tk);

  TermKeyKey key;
  TermKeyResult ret;
  while ((ret = termkey_getkey(tk, &key)) == TERMKEY_RES_KEY) {
    if (key.type == TERMKEY_TYPE_UNICODE && !utf8) {
      gsize bwritten;
      GError *err = NULL;
      char *utf8;

      // convert data from user charset to UTF-8
      if (!(utf8 = g_locale_to_utf8(key.utf8, -1, NULL, &bwritten, &err))) {
        if (err) {
          g_warning(_("Error converting input to UTF-8 (%s)."),
              err->message);
          g_error_free(err);
          err = NULL;
        }
        else
          g_warning(_("Error converting input to UTF-8."));
        continue;
      }

      memcpy(key.utf8, utf8, bwritten + 1);
      g_free(utf8);

      key.code.codepoint = g_utf8_get_char(key.utf8);
    }

    ProcessInput(key);
  }
  if (ret == TERMKEY_RES_AGAIN) {
    int wait = termkey_get_waittime(tk);
    io_input_timeout_conn = TimeoutOnceConnect(sigc::mem_fun(this,
          &CoreManager::io_input_timeout), wait);
  }

  return TRUE;
}