]> pd.if.org Git - pd_readline/blob - keyhandler.c
More work.
[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 #include "pd_readline.h"  
16
17
18 /* This implementation of getch() is from here - */ 
19 /* http://wesley.vidiqatch.org/                  */ 
20 /* Thanks, Wesley!                               */  
21 static struct termios old, new;
22
23 /* Initialize new terminal i/o settings */
24 void initTermios(int echo) {
25     tcgetattr(0, &old); /* grab old terminal i/o settings */
26     new = old; /* make new settings same as old settings */
27     new.c_lflag &= ~ICANON; /* disable buffered i/o */
28     new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
29     tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
30 }
31
32
33 /* Restore old terminal i/o settings */
34 void resetTermios(void) {
35     tcsetattr(0, TCSANOW, &old);
36 }
37
38
39 /* Read 1 character - echo defines echo mode */
40 char getch_(int echo) {
41     char ch;
42     initTermios(echo);
43     ch = getchar();
44     resetTermios();
45     return ch;
46 }
47
48
49 /* Read 1 character without echo */
50 char getch(void) {
51     return getch_(0);
52 }
53
54
55 /* Read 1 character with echo */
56 char getche(void) {
57     return getch_(1);
58
59
60
61
62 /*  Arrow keys are esc [ A to esc [ D                       */ 
63 /*  Alt keys are just esc then key (e.g. Alt-g is esc g ).  */   
64 /*  Ctrl (then letter) keys are just Dec 1 to Dec 26        */ 
65
66 void keyhandler(buf b) 
67
68     
69   int i = getch(); 
70   
71   switch(i)
72   { 
73         case (1):   break;     /*  Ctrl a  */   
74         case (2):   break;     /*  Ctrl b  */   
75         case (3):   break;     /*  Ctrl c  */     
76           
77         case (10):  enter();     /* Enter    */   
78           
79     case (27):  spec(i);   break;   /*  esc  */ 
80     
81     case (32):  printf("%c", i);  break;      /*  Printable chars.  */    
82     case (33):  printf("%c", i);  break;      /*  Printable chars.  */    
83     case (34):  printf("%c", i);  break;      /*  Printable chars.  */    
84     
85     case (65):  printf("%c", i);  break;      /*  "A"  */    
86     case (66):  printf("%c", i);  break;      /*  "B"  */    
87     case (67):  printf("%c", i);  break;      /*  "C"  */   
88     case (68):  printf("%c", i);  break;      /*  "D"  */    
89     case (69):  printf("%c", i);  break;      /*  "E"  */    
90     case (70):  printf("%c", i);  break;      /*  "F"  */    
91       
92     case (97):  printf("%c", i);  break;       /*  "a"  */    
93     case (98):  printf("%c", i);  break;       /*  "b"  */    
94     case (99):  printf("%c", i);  break;       /*  "c"  */    
95     case (100):  printf("%c", i);  break;      /*  "d"  */    
96     case (101):  printf("%c", i);  break;      /*  "e"  */    
97     case (102):  printf("%c", i);  break;      /*  "f"  */    
98             
99     case (126):  printf("%c", i);  break;      /*  "~"  */    
100     
101     case (127):  delch(b);  break;  
102    
103     default:   break;                 /*  stuff();         */ 
104   }   
105   
106
107
108
109
110