/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* u_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #include #include #include "c_init.h" t_caps *c_get_struct(int mode, t_caps *src) { static t_caps *caps; if (mode == 1) { caps = src; } return (caps); } void c_process_key(t_caps *tcaps) { char nread[64] = {0}; int bread; while ((bread = read(STDIN_FILENO, nread, sizeof(nread)) != 1)) { if (bread == -1) { printf("error on read"); return ; } } if (*nread == 'q') exit(0); else if ((strncmp(nread, "l", 1) == 0) || strncmp(nread, tcaps->kl, strlen(tcaps->kl)) == 0) { tputs(tgoto(tcaps->nd, 1, 1), 1, putchar); } else if (strncmp(nread, "c", 1) == 0) write(1, tcaps->cl, strlen(tcaps->cl)); else if (strncmp(nread, "j", 1) == 0) write(1, tcaps->DO, strlen(tcaps->DO)); else if (strncmp(nread, "k", 1) == 0) { write(1, tcaps->up, strlen(tcaps->kl)); } else if (strncmp(nread, "h", 1) == 0) { tputs(tgoto(tcaps->le, 1, 1), 1, putchar); } else write(1, nread, 1); } int16_t c_init_tcaps(void) { t_caps tcaps; char *bp; char *term; bp = NULL; term = getenv("TERM"); if (!tgetent(bp, term)) return (-1); tcaps.cl = tgetstr("cl", &term); tcaps.ks = tgetstr("ks", &term); tcaps.kl = tgetstr("kl", &term); tcaps.ke = tgetstr("kr", &term); tcaps.kr = tgetstr("kr", &term); tcaps.pc = tgetstr("pc", &term); tcaps.bc = tgetstr("bc", &term); tcaps.up = tgetstr("up", &term); tcaps.nd = tgetstr("nd", &term); tcaps.le = tgetstr("le", &term); tcaps.DO = tgetstr("do", &term); tcgetattr(STDIN_FILENO, &tcaps.tios); printf("%d\n", KEY_LEFT); tputs(tgoto(tcaps.ks, 1, 1), 1, putchar); tcaps.tios.c_lflag &= ~(ECHO | ICANON); tcaps.tios.c_cc[VMIN] = 0; tcaps.tios.c_cc[VTIME] = 1; tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcaps.tios); c_get_struct(1, &tcaps); while (1) { c_process_key(&tcaps); } return (1); }