From 33d7c46a44af411d10274a9481c98fdac4ef2b27 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sun, 10 Apr 2022 08:00:12 +0900 Subject: [PATCH] yutani-tty-pipe: Dumb tool to act as a keyboard over a tty --- apps/yutani-tty-pipe.c | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 apps/yutani-tty-pipe.c diff --git a/apps/yutani-tty-pipe.c b/apps/yutani-tty-pipe.c new file mode 100644 index 00000000..2560bf6e --- /dev/null +++ b/apps/yutani-tty-pipe.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include + +#include +#include + +yutani_t * yctx; +int quiet = 0; + +int show_resolution(void) { + if (!yctx) { + if (!quiet) printf("(not connected)\n"); + return 1; + } + printf("%dx%d\n", (int)yctx->display_width, (int)yctx->display_height); + return 0; +} + +int reload(void) { + if (!yctx) { + if (!quiet) printf("(not connected)\n"); + return 1; + } + yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_RELOAD); + return 0; +} + +struct termios old; + +void set_unbuffered() { + tcgetattr(fileno(stdin), &old); + struct termios new = old; + new.c_lflag &= (~ICANON & ~ECHO); + tcsetattr(fileno(stdin), TCSAFLUSH, &new); +} + + +int main(int argc, char * argv[]) { + yctx = yutani_init(); + + if (!yctx) { + fprintf(stderr, "not connected; did you set $DISPLAY?\n"); + return 1; + } + + set_unbuffered(); + + int c; + while ((c = fgetc(stdin))) { + key_event_t event = {0}; + key_event_state_t state = {0}; + + event.keycode = c; + event.key = c; + + switch (c) { + case 27: + event.keycode = KEY_ESCAPE; + event.key = KEY_ESCAPE; + break; + /* Either of the backspace keys */ + case 8: + case 0x7f: + event.keycode = 8; + event.key = 8; + break; + /* Any of \r or \n */ + case '\r': + case '\n': + event.keycode = '\n'; + event.key = '\n'; + break; + } + + event.action = KEY_ACTION_DOWN; + + yutani_msg_buildx_key_event_alloc(m_); + yutani_msg_buildx_key_event(m_, 0, &event, &state); + yutani_msg_send(yctx, m_); + + event.action = KEY_ACTION_UP; + yutani_msg_buildx_key_event(m_, 0, &event, &state); + yutani_msg_send(yctx, m_); + } + + return 0; +} +