]> 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 void keyhandler(void) 
63
64   int i = getch(); 
65   
66   switch(i)
67   { 
68     case (27):  puts("1");    /*  escape() ;       */ 
69     case (33):  puts("2");    /*  dosomething();   */ 
70     case (42):  puts("3");    /*  something();     */ 
71     default:    puts("4");    /*  stuff();         */ 
72   }   
73   
74
75
76
77
78