]> 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 {
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 void keyhandler(buf b, hist h) 
69
70     
71   int a = getch();    
72   
73   int t = type(a); 
74     
75   switch(t)
76   { 
77           
78         case (1):   break;     /*  Ctrl a  */   
79         case (2):   break;     /*  Ctrl b  */   
80         case (3):   getch();  spec(h); break;   /*  Ctrl c  */     
81         case (4):   set(b, a);   break;    /*  Printable chars.  */    
82         case (5):   delch(b);  break;  
83         case (6):   break;  
84         default:    break; 
85                      
86   }   
87    
88 }    
89
90
91
92
93
94