changeset 12:af71ea70b7f4

Fixed keyboard control.
author samer
date Sat, 18 Feb 2012 13:36:01 +0000
parents a8995f4a5793
children b82da07b6800
files termapp/Makefile termapp/termapp termapp/termapp.cpp
diffstat 3 files changed, 50 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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
-
Binary file termapp/termapp has changed
--- 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 <XnCppWrapper.h>
 #include <lo/lo.h>
 #include <math.h>
-#include <curses.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <string.h>
+#include <sys/select.h>
+#include <termios.h>
+#include <stdio.h>
+
+
 
 #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;
 }