#include #include #include #include #include static struct termios old, current; #ifndef CTRL #define CTRL(c) ((c) & 037) #endif /* Initialize new terminal i/o settings */ void initTermios() { tcgetattr(0, &old); /* grab old terminal i/o settings */ current = old; /* make new settings same as old settings */ current.c_lflag &= ~ICANON; /* disable buffered i/o */ //current.c_lflag |= ECHO; /* set echo mode */ current.c_lflag &= ~ECHO; /* set no echo mode */ tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */ } /* Restore old terminal i/o settings */ void resetTermios(void) { tcsetattr(0, TCSANOW, &old); } /* Read 1 character*/ char getch() { char ch=0; initTermios(); ch = getchar(); resetTermios(); return ch; } int main (void) { char ch; char extCH; char chMOD; // ch apres les ESCAPE char chF; // ch des F key char chWaste; // ch de fermeture de certaines sequences, pas encore trouve a quoi ca sert. system("stty raw"); int fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY | O_SYNC); struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); tcsetattr(fd, TCSANOW, &options); if (fd == -1) { perror("unable to open port"); exit(0); } inf_loop: //while() //{ //write(fd,ch,1); extCH = 0; ch = getch(); unsigned char gch[]=""; gch[0] = ch; //write(fd,gch,sizeof(gch)); printf("(%d",ch); printf(")"); if (ch==27){ /* '\033' ESCAPE extended press */ printf("[%d",ch); chMOD = getch(); printf(",%d", chMOD); switch(chMOD){ case 27: printf(",{ESCAPE}"); extCH=26; break; case '`': printf("[_exit_]"); system("stty cooked"); exit(0); break; case 93: printf(",{ALT-]}"); break; case 92: printf(",{ALT-\\n}" ) ; /* alt-\ */ break; case 52: printf(",{ALT_4}"); break; default: ch = getch(); printf(",%d",ch); if (chMOD == 79) { /* Mouse move */ switch(ch){ case 65: printf(",{MOUSE_UP}"); // ctrl-up extCH=1; break; case 66: printf(",{MOUSE_DOWN}"); //ctrl-down extCH=2; break; case 67: printf(",{MOUSE_RIGHT}"); //ctrl-right extCH=3; break; case 68: printf(",{MOUSE_LEFT}"); //ctrl-left extCH=4; break; case 71: printf(",{MOUSE_BTN1}"); //ctrl-mid extCH=5; break; } } if (chMOD == 91) { /* Normal */ switch(ch){ case 65: printf(",{UP}"); extCH=11; break; case 66: printf(",{DOWN}"); extCH=12; break; case 67: printf(",{RIGHT}"); extCH=13; break; case 68: printf(",{LEFT}"); extCH=14; break; case 71: printf(",{MID}"); break; case 52: printf(",{END}"); chWaste = getch(); extCH=22; break; case 54: printf(",{PG_DOWN}"); chWaste = getch(); extCH=24; break; case 53: printf(",{PG_UP}"); chWaste = getch(); extCH=23; break; case 51: printf(",{DEL}"); chWaste = getch(); extCH=20; break; case 50: /* F-Key[8-12] */ chF = getch(); printf(",%d",chF); switch(chF){ case 48: printf(",{F9}"); chWaste = getch(); extCH=35; break; case 49: printf(",{F10}"); chWaste = getch(); extCH=36; break; case 51: printf(",{F11}"); chWaste = getch(); extCH=37; break; case 52: printf(",{F12}"); chWaste = getch(); extCH=38; break; case 56: printf(",{MOUSE_BTN2}"); // SHIFT-F5 extCH = 6; chWaste = getch(); break; case 126: printf(",{INS}"); extCH=19; //chWaste = getch(); break {ins}, ok pour autre cases break; } break; case 49: /* F-Key[1-7] */ chF = getch(); printf(",%d",chF); switch(chF){ case 126: printf(",{HOME}"); extCH=21; break; case 49: printf(",{F1}"); extCH=27; chWaste = getch(); break; case 50: printf(",{F2}"); extCH=28; chWaste = getch(); break; case 51: printf(",{F3}"); extCH=29; chWaste = getch(); break; case 52: printf(",{F4}"); extCH=30; chWaste = getch(); break; case 53: printf(",{F5}"); extCH=31; chWaste = getch(); break; case 55: printf(",{F6}"); extCH=32; chWaste = getch(); break; case 56: printf(",{F7}"); extCH=33; chWaste = getch(); break; case 57: printf(",{F8}"); extCH=34; chWaste = getch(); break; } break; case 27: printf(",{ALT-[}"); break; } printf("]"); } break; } } else { switch(ch) { case 27: printf("{esc}"); extCH=26; // esc break; case 1: printf(",{ALT}"); // CTRL-A extCH=7; break; case 19: printf(",{SHIFT}"); // CTRL-S extCH=8; break; case 3: printf(",{CTRL}"); //CTRL-C extCH=9; break; case 23: printf(",{WIN}"); //CTRL-W extCH=10; break; case 9: printf(",{TAB}"); extCH=15; // TAB break; case 12: printf(",{CAPS_LOCK}"); extCH=16; //CTRL-L break; case 13: printf(",{RETURN}"); extCH=17; // RETURN break; case 127: printf(",{BACKSPACE}"); extCH=18; // BACKSPACE break; case 14: printf(",{NUM_LOCK}"); extCH=25; // ctrl-n break; case 16: printf(",{PRT_SCRN}"); extCH=39; // ctrl-p break; case 18: printf(",{SCRL_LOCK}"); extCH=40; // ctrl-r break; case 2: printf(",{PAUSE_BRK}"); extCH=41; // ctrl-b break; case 5: printf(",{MENU}"); extCH=42; // ctrl-e break; case 17: printf(",{release all}"); extCH=43; // ctrl-q ------------------------ LAST ONE ------------------- break; case 8: printf("DISPLAY HELP!!!"); break; } } if (extCH >= 1){ gch[0] = extCH + 127; printf("!(_%d_)", gch[0]); } else { gch[0] = ch; printf(":(%d)", gch[0]); } write(fd,gch,sizeof(gch)); printf("\r\n"); tcdrain(fd); goto inf_loop; return 0; }