Browse Source

utils/faux-getch utility

Serj Kalichev 3 years ago
parent
commit
553a689d0a
3 changed files with 34 additions and 1 deletions
  1. 1 0
      utils/.gitignore
  2. 5 1
      utils/Makefile.am
  3. 28 0
      utils/faux-getch.c

+ 1 - 0
utils/.gitignore

@@ -1 +1,2 @@
 /faux-file2c
+/faux-getch

+ 5 - 1
utils/Makefile.am

@@ -1,10 +1,14 @@
 ## Process this file with automake to produce Makefile.in
 bin_PROGRAMS += \
-	utils/faux-file2c
+	utils/faux-file2c \
+	utils/faux-getch
 
 utils_faux_file2c_SOURCES = \
 	utils/faux-file2c.c
 
+utils_faux_getch_SOURCES = \
+	utils/faux-getch.c
+
 utils_faux_file2c_LDADD = \
 	libfaux.la \
 	$(LIBOBJS)

+ 28 - 0
utils/faux-getch.c

@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <termios.h>
+
+int main(void)
+{
+	while (1) {
+		int c = 0;
+		static struct termios oldt = {};
+		static struct termios newt = {};
+
+		// Don't wait for Enter (buffer)
+		tcgetattr(STDIN_FILENO, &oldt);
+		newt = oldt;
+		newt.c_lflag &= ~(ICANON);
+		tcsetattr( STDIN_FILENO, TCSANOW, &newt);
+
+		c = getchar();
+		printf("%u 0x%x\n", (unsigned char)c, (unsigned char)c);
+
+		// Restore terminal
+		tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
+	}
+
+	return 0;
+}
+