]> pd.if.org Git - pd_readline/blob - keyhandler.c
2fc6ee5dd974f2dae10f8ebdcb9400d5505726b9
[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 {
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 */
31 }
32
33
34 /* Restore old terminal i/o settings */
35 void resetTermios(void) 
36 {
37     tcsetattr(0, TCSANOW, &old);
38 }
39
40
41 /* Read 1 character - echo defines echo mode */
42 char getch_(int echo) {
43     char ch;
44     initTermios(echo);
45     ch = getchar();
46     resetTermios();
47     return ch;
48 }
49
50
51 /* Read 1 character without echo */
52 char getch(void) {
53     return getch_(0);
54 }
55
56
57 /* Read 1 character with echo */
58 char getche(void) {
59     return getch_(1);
60
61
62
63
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        */ 
67
68 /* TO DO: Use the helper function range                     */  
69 /*  (  range(rstart, rend, val).  )                         */ 
70 /* to handle entire ranges at once.                         */        
71
72
73 void keyhandler(buf b) 
74
75     
76   int i = getch(); 
77   
78   int t = type(i); 
79     
80   switch(t)
81   { 
82           
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;  
88         case (6):   break;  
89         default:    break; 
90                      
91   }   
92   
93 }    
94
95
96
97
98
99