#include #include #include #include #include "console.h" #include "helpers.h" int console_init(struct console *cons) { int master = open("/dev/ptmx", O_RDWR | O_NOCTTY); if (master < 0) { return -1; } if (grantpt(master) || unlockpt(master)) { close(master); return -1; } int flags = fcntl(master, F_GETFL, 0); if (flags < 0) { close(master); return -1; } flags |= O_NONBLOCK; if (fcntl(master, F_SETFL, flags) < 0) { close(master); return -1; } cons->master = master; cons->isesc = 0; return 0; } int console_deinit(struct console *cons) { close(cons->master); return 0; } void console_enter(struct console *cons, PANEL *pan) { (void)cons; keypad(stdscr, FALSE); curs_set(TRUE); raw(); top_panel(pan); } void console_leave(struct console *cons, PANEL *pan) { (void)cons; (void)pan; keypad(stdscr, TRUE); curs_set(FALSE); cbreak(); } int console_update(struct console *cons, PANEL *pan) { char c; int did_read = 0; while (read(cons->master, &c, 1) > 0) { did_read = 1; if (!cons->isesc) { if (c == 0x1b) { cons->isesc = 1; } else if (c != 0x0d) { pprintw(pan, "%c", c); } } else { if (c == 'm') { cons->isesc = 0; } } } return did_read; } void console_input(struct console *cons, int ch) { write(cons->master, &ch, 1); } int console_configslave(struct console *cons) { if (setsid() < 0) { return -1; } int slave = open(ptsname(cons->master), O_RDWR); if (slave < 0) { return -1; } if (ioctl(slave, TIOCSCTTY, 0) < 0) { close(slave); return -1; } dup2(slave, STDIN_FILENO); dup2(slave, STDOUT_FILENO); dup2(slave, STDERR_FILENO); return slave; }