X-Git-Url: https://pd.if.org/git/?p=pd_readline;a=blobdiff_plain;f=pdmenu2.c;fp=pdmenu2.c;h=746eb3102aa380da6eaff7d3c8a3dc2db548636c;hp=0000000000000000000000000000000000000000;hb=15155262dff7689ef3601df89e70d42ec3c127ca;hpb=a9843085ec916c175bd245a8398f30e6cc03f984 diff --git a/pdmenu2.c b/pdmenu2.c new file mode 100644 index 0000000..746eb31 --- /dev/null +++ b/pdmenu2.c @@ -0,0 +1,170 @@ + + +/* pdmenu.c */ +/* This code is aimed at helping those who would like to */ +/* create menus without using the curses library. */ +/* It creates a count variable (which could be used to */ +/* keep track of which item is highlighted). Using the */ +/* up and down arrow keys increments and decrements the */ +/* count variable. Finally, when you press Enter, the */ +/* value of the count variable is printed and the program */ +/* exits. */ +/* This code is released to the public domain. */ +/* "Share and enjoy...." ;) */ + + +#include +#include +#include /* For getch() */ + +/* This implementation of getch() is from here - */ +/* http://wesley.vidiqatch.org/ */ +/* Thanks, Wesley! */ +static struct termios old, new; + +/* Initialize new terminal i/o settings */ +void initTermios(int echo) { + tcgetattr(0, &old); /* grab old terminal i/o settings */ + new = old; /* make new settings same as old settings */ + new.c_lflag &= ~ICANON; /* disable buffered i/o */ + new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ + tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ +} + + +/* Restore old terminal i/o settings */ +void resetTermios(void) { + tcsetattr(0, TCSANOW, &old); +} + + +/* Read 1 character - echo defines echo mode */ +char getch_(int echo) { + char ch; + initTermios(echo); + ch = getchar(); + resetTermios(); + return ch; +} + + +/* Read 1 character without echo */ +char getch(void) { + return getch_(0); +} + + +/* Read 1 character with echo */ +char getche(void) { + return getch_(1); +} + + + +/* Helper function, to let us see if a .history file */ +/* exists in the current directory. */ +int fexists(char *fname) +{ + + FILE *fptr ; + + fptr = fopen(fname, "r") ; + + if ( !fptr ) return -1 ; /* File does not exist in dir. */ + + fclose(fptr); + return 0; /* File DOES exist in dir. */ + +} + + + +int main(void) +{ + + printf("Public Domain Menu Program \n"); + printf("\nUse the up and down arrow keys then press Enter\n"); + + int menunum = 0; + + /* Buffer */ + char buffer[80] ; + + /* Buffer "pointer" */ + int bufpnt = 0; + + + /* Test for existence of history file. */ + int exists; + exists = fexists(".history"); + printf("Result: %d \n", exists); + + while(1) + { + + int key = getch(); + + /* Printable chars. */ + if ( (key >= 32) && (key <= 126) ) + { + buffer[bufpnt] = key; + bufpnt += 1; + } + + /* Up arrow is 27, 91, 65. ( ESC [ A ) */ + /* Down arrow is 27, 91, 66. ( ESC [ B ) */ + /* Right arrow is 27, 91, 67. ( ESC [ C ) */ + /* Left arrow is 27, 91, 68. ( ESC [ D ) */ + else if(key == 27) + { key = getch(); + if(key == 91) + key = getch(); + if(key == 65) + { puts("You pressed up arrow! \n"); + menunum-=1; + printf("Menunum is now %d \n", menunum); + } + else if(key == 66) + { puts("You pressed down arrow! \n"); + menunum+=1; + printf("Menunum is now %d \n", menunum); + } + else if(key == 67) + { puts("You pressed right arrow! \n"); + } + else if(key == 68) + { puts("You pressed left arrow! \n"); + } + } + + /* A few other keys (for testing purposes ) */ + + /* + else if(key == 97) + { puts("You pressed the \"a\" key! \n"); + } + else if(key == 98) + { puts("You pressed the \"b\" key! \n"); + } + else if(key == 99) + { puts("You pressed the \"c\" key! \n"); + } + else if(key == 100) + { puts("You pressed the \"d\" key! \n"); + } + */ + + + /* The Enter key exits. Enter is 10 decimal */ + else if(key == 10) + { printf("You pressed ENTER! You chose item %d \n", menunum); + printf("Buffer is %s \n", buffer) ; + break; } + } + + return 0; +} + + + +