# HG changeset patch # User samer # Date 1329572161 0 # Node ID af71ea70b7f425d02a7ff28383158fae44acd083 # Parent a8995f4a57936badf7960791d5ab4cab890dcf2a Fixed keyboard control. diff -r a8995f4a5793 -r af71ea70b7f4 termapp/Makefile --- a/termapp/Makefile Fri Feb 17 23:08:53 2012 +0000 +++ b/termapp/Makefile Sat Feb 18 13:36:01 2012 +0000 @@ -7,11 +7,7 @@ clean: rm $(TARGET) - rm $(TARGET).o -$(TARGET).o: $(TARGET).cpp - g++ $(CFLAGS) -c $(INCLUDES) $(TARGET).cpp +$(TARGET): $(TARGET).cpp + g++ $(CFLAGS) $(INCLUDES) -o $(TARGET) $(LIBS) $(TARGET).cpp -$(TARGET): $(TARGET).o - g++ $(CFLAGS) -o $(TARGET) $(LIBS) $(TARGET).o - diff -r a8995f4a5793 -r af71ea70b7f4 termapp/termapp Binary file termapp/termapp has changed diff -r a8995f4a5793 -r af71ea70b7f4 termapp/termapp.cpp --- a/termapp/termapp.cpp Fri Feb 17 23:08:53 2012 +0000 +++ b/termapp/termapp.cpp Sat Feb 18 13:36:01 2012 +0000 @@ -1,9 +1,14 @@ #include #include #include -#include #include #include +#include +#include +#include +#include + + #define IMGWIDTH 640 #define IMGHEIGHT 480 @@ -260,6 +265,7 @@ printf("\n floor plane: (%f,%f,%f), <%f,%f,%f>\n", floor_pie.ptPoint.X, floor_pie.ptPoint.Y, floor_pie.ptPoint.Z, floor_pie.vNormal.X, floor_pie.vNormal.Y, floor_pie.vNormal.Z); + fflush(stdout); break; } } @@ -303,6 +309,43 @@ } //======================================================================== +struct termios orig_termios; + +void reset_terminal_mode() +{ + printf("Resetting terminal...\n"); + tcsetattr(0, TCSANOW, &orig_termios); +} + +void set_conio_terminal_mode() +{ + struct termios new_termios; + + /* take two copies - one for now, one for later */ + tcgetattr(0, &orig_termios); + memcpy(&new_termios, &orig_termios, sizeof(new_termios)); + atexit(reset_terminal_mode); + new_termios.c_lflag &= ~ECHO; + new_termios.c_lflag &= ~ICANON; + tcsetattr(0, TCSANOW, &new_termios); +} + +int kbhit() +{ + struct timeval tv = { 0L, 0L }; + fd_set fds; + FD_ZERO(&fds); + FD_SET(0, &fds); + return select(1, &fds, NULL, NULL, &tv); +} + +int getch() +{ + unsigned char c; + int r=read(0, &c, sizeof(c)); + return (r<0) ? r : c; +} + int main(int argc, const char **argv) { int i,ch; @@ -316,18 +359,15 @@ printf("Setting up...\n"); app->setup(); printf("Running...\n"); + set_conio_terminal_mode(); for (;;) { app->update(); app->draw(); - ch=getch(); - if (ch!=ERR) { - if (ch=='Q') break; - printf("\n got key: %d.\n",ch); - app->keyPressed(ch); + while (kbhit()) { + ch = getch(); + if (ch>0) app->keyPressed(ch); } } - delete app; - app=NULL; }