static Status ti_init(UI *ui) { TUI *tui = (TUI *)ui; /* Termkey */ /* Create new termkey instance monitoring stdin with * the SIGINT behaviour of Ctrl-C disabled */ tui->termkey = termkey_new(STDIN_FILENO, TERMKEY_FLAG_SPACESYMBOL | TERMKEY_FLAG_CTRLC); if (tui->termkey == NULL) { return st_get_error(ERR_UNABLE_TO_INITIALISE_TERMEKEY, "Unable to create termkey instance"); } /* Represent ASCII DEL character as backspace */ termkey_set_canonflags(tui->termkey, TERMKEY_CANON_DELBS | TERMKEY_CANON_SPACESYMBOL); if (tui->sess->wed_opt.test_mode) { tui->rows = 24; tui->cols = 80; return STATUS_SUCCESS; } /* ncurses */ initscr(); tui->rows = LINES; tui->cols = COLS; tv_init(&tui->tv, tui->rows, tui->cols); ti_init_display(ui); refresh(); return STATUS_SUCCESS; }
void CoreManager::InputInit() { // init libtermkey TERMKEY_CHECK_VERSION; if (!(tk = termkey_new(STDIN_FILENO, TERMKEY_FLAG_NOTERMIOS))) { g_critical(_("Libtermkey initialization failed.")); exit(1); } utf8 = g_get_charset(NULL); io_input_channel = g_io_channel_unix_new(STDIN_FILENO); // set channel encoding to NULL so it can be unbuffered g_io_channel_set_encoding(io_input_channel, NULL, NULL); g_io_channel_set_buffered(io_input_channel, FALSE); g_io_channel_set_close_on_unref(io_input_channel, TRUE); io_input_channel_id = g_io_add_watch_full(io_input_channel, G_PRIORITY_HIGH, static_cast<GIOCondition>(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI), io_input_, this, NULL); g_io_add_watch_full(io_input_channel, G_PRIORITY_HIGH, G_IO_NVAL, io_input_error_, this, NULL); g_io_channel_unref(io_input_channel); // screen resizing if (!pipe(pipefd)) { pipe_valid = true; resize_channel = g_io_channel_unix_new(pipefd[0]); g_io_channel_set_encoding(resize_channel, NULL, NULL); g_io_channel_set_buffered(resize_channel, FALSE); g_io_channel_set_close_on_unref(resize_channel, TRUE); resize_channel_id = g_io_add_watch_full(resize_channel, G_PRIORITY_HIGH, G_IO_IN, resize_input_, this, NULL); } }
void input_init(void) { log_msg("INIT", "INPUT"); tk = termkey_new(0,0); termkey_set_flags(tk, TERMKEY_FLAG_UTF8 | TERMKEY_CANON_DELBS); uv_poll_init(eventloop(), &poll_handle, 0); uv_poll_start(&poll_handle, UV_READABLE, input_check); }
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); }
Ui *ui_curses_new(void) { UiCurses *uic = calloc(1, sizeof(UiCurses)); Ui *ui = (Ui*)uic; if (!uic) return NULL; if (!(uic->termkey = termkey_new(STDIN_FILENO, TERMKEY_FLAG_UTF8))) goto err; termkey_set_canonflags(uic->termkey, TERMKEY_CANON_DELBS); setlocale(LC_CTYPE, ""); if (!getenv("ESCDELAY")) set_escdelay(50); char *term = getenv("TERM"); if (!term) term = "xterm"; if (!newterm(term, stderr, stdin)) { snprintf(uic->info, sizeof(uic->info), "Warning: unknown term `%s'", term); if (!newterm(strstr(term, "-256color") ? "xterm-256color" : "xterm", stderr, stdin)) goto err; } start_color(); use_default_colors(); raw(); noecho(); nonl(); keypad(stdscr, TRUE); meta(stdscr, TRUE); curs_set(0); /* needed because we use getch() which implicitly calls refresh() which would clear the screen (overwrite it with an empty / unused stdscr */ refresh(); *ui = (Ui) { .init = ui_init, .start = ui_start, .free = ui_curses_free, .termkey_get = ui_termkey_get, .suspend = ui_suspend, .resize = ui_resize, .update = ui_update, .window_new = ui_window_new, .window_free = ui_window_free, .window_focus = ui_window_focus, .window_swap = ui_window_swap, .draw = ui_draw, .redraw = ui_redraw, .arrange = ui_arrange, .die = ui_die, .info = ui_info, .info_hide = ui_info_hide, .haskey = ui_haskey, .getkey = ui_getkey, .terminal_save = ui_terminal_save, .terminal_restore = ui_terminal_restore, }; struct sigaction sa; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sa.sa_handler = sigwinch_handler; sigaction(SIGWINCH, &sa, NULL); sigaction(SIGCONT, &sa, NULL); ui_resize(ui); return ui; err: ui_curses_free(ui); return NULL; }
static TermKey *ui_termkey_new(int fd) { TermKey *termkey = termkey_new(fd, TERMKEY_FLAG_UTF8); if (termkey) termkey_set_canonflags(termkey, TERMKEY_CANON_DELBS); return termkey; }
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); }