Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #define stdin 0
- #define stdout 1
- #define __KERNEL_NCCS 19
- #define ICANON 0000002
- #define ECHO 0000010
- typedef unsigned int tcflag_t;
- typedef unsigned char cc_t;
- int printf(const char*, ...);
- struct linux_termios {
- tcflag_t c_iflag; /* Input flags */
- tcflag_t c_oflag; /* Output flags */
- tcflag_t c_cflag; /* Control mode flags */
- tcflag_t c_lflag; /* Local mode flags */
- cc_t c_line; /* Line discipline */
- cc_t c_cc[__KERNEL_NCCS]; /* Control characters */
- };
- struct linux_termios termios_orig;
- void backup_termios() {
- struct linux_termios l_termios;
- ioctl(stdin, TCGETS, &l_termios);
- memcpy(&termios_orig, &l_termios, sizeof(struct linux_termios));
- memcpy(&termios_orig.c_cc[0], &l_termios.c_cc[0], __KERNEL_NCCS);
- }
- struct linux_termios get_termios() {
- struct linux_termios l_termios;
- ioctl(stdin, TCGETS, &l_termios);
- return l_termios;
- }
- void enable_raw_mode() {
- struct linux_termios termios = get_termios();
- termios.c_lflag &= ~(ECHO | ICANON);
- ioctl(stdin, TCSETSF, &termios);
- }
- void revert_termios() {
- ioctl(stdin, TCSETSF, &termios_orig);
- }
- int main() {
- backup_termios();
- enable_raw_mode();
- atexit(revert_termios);
- char buf;
- while(read(stdin, &buf, 1) == 1 && buf != 'q') {
- if (buf == 27) {
- // Arrow key pressed, skip '['
- read(stdin, &buf, 1);
- read(stdin, &buf, 1);
- switch (buf) {
- case 'A': {
- write(stdout, "U", 1);
- break;
- }
- case 'B': {
- write(stdout, "D", 1);
- break;
- }
- case 'C': {
- write(stdout, "L", 1);
- break;
- }
- case 'D': {
- write(stdout, "R", 1);
- break;
- }
- default: write(stdout, &buf, 1);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement