5 /* Handle keystrokes for pd_readline. */
6 /* This code is released to the public domain. */
7 /* "Share and enjoy...." ;) */
8 /* See the UNLICENSE file for details. */
15 #include "pd_readline.h"
18 /* This implementation of getch() is from here - */
19 /* http://wesley.vidiqatch.org/ */
21 static struct termios old, new;
23 /* Initialize new terminal i/o settings */
24 void initTermios(int echo)
26 tcgetattr(0, &old); /* grab old terminal i/o settings */
27 new = old; /* make new settings same as old settings */
28 new.c_lflag &= ~ICANON; /* disable buffered i/o */
29 new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
30 tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
34 /* Restore old terminal i/o settings */
35 void resetTermios(void)
37 tcsetattr(0, TCSANOW, &old);
41 /* Read 1 character - echo defines echo mode */
42 char getch_(int echo) {
51 /* Read 1 character without echo */
57 /* Read 1 character with echo */
64 /* Arrow keys are esc [ A to esc [ D */
65 /* Alt keys are just esc then key (e.g. Alt-g is esc g ). */
66 /* Ctrl (then letter) keys are just Dec 1 to Dec 26 */
68 /* TO DO: Use the helper function range */
69 /* ( range(rstart, rend, val). ) */
70 /* to handle entire ranges at once. */
73 void keyhandler(buf b)
83 case (1): break; /* Ctrl a */
84 case (2): break; /* Ctrl b */
85 case (3): break; /* Ctrl c */
86 case (4): printf("%c", i); break; /* Printable chars. */
87 case (5): delch(b); break;