]> pd.if.org Git - pd_readline/blob - keyhandler.c
Starting cleanup of code.
[pd_readline] / keyhandler.c
1
2
3
4 /*  keyhandler.c                                       */ 
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.                */ 
9
10
11 #include <string.h>   
12 #include <stdio.h> 
13 #include <stdlib.h> 
14 #include <termios.h>  
15
16
17 /* This implementation of getch() is from here - */ 
18 /* http://wesley.vidiqatch.org/                  */ 
19 /* Thanks, Wesley!                               */  
20 static struct termios old, new;
21
22 /* Initialize new terminal i/o settings */
23 void initTermios(int echo) {
24     tcgetattr(0, &old); /* grab old terminal i/o settings */
25     new = old; /* make new settings same as old settings */
26     new.c_lflag &= ~ICANON; /* disable buffered i/o */
27     new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
28     tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
29 }
30
31
32 /* Restore old terminal i/o settings */
33 void resetTermios(void) {
34     tcsetattr(0, TCSANOW, &old);
35 }
36
37
38 /* Read 1 character - echo defines echo mode */
39 char getch_(int echo) {
40     char ch;
41     initTermios(echo);
42     ch = getchar();
43     resetTermios();
44     return ch;
45 }
46
47
48 /* Read 1 character without echo */
49 char getch(void) {
50     return getch_(0);
51 }
52
53
54 /* Read 1 character with echo */
55 char getche(void) {
56     return getch_(1);
57
58
59
60
61 int keyhandler(void) 
62
63   int i = getch(); 
64   
65   switch(i); 
66   { 
67     case (27):  escape() ; 
68     case (33):  dosomething();  
69     case (42):  something();
70     default:    stuff(): 
71   }   
72   
73
74
75
76
77