summaryrefslogtreecommitdiff
path: root/cInput.cpp
diff options
context:
space:
mode:
authorGravatar jonas <himself@jonasgunz.de> 2018-12-27 00:26:34 +0100
committerGravatar jonas <himself@jonasgunz.de> 2018-12-27 00:26:34 +0100
commit7015245de4af673af3e6fa62d5d0f9c2e0c2a8a1 (patch)
tree8bec5d1716eebb0fedc75dcc036385443b1ecea5 /cInput.cpp
parent616951a353caf398908e6e6ce7dddf34a65513e5 (diff)
downloadtermgl-7015245de4af673af3e6fa62d5d0f9c2e0c2a8a1.tar.gz
cleaned working tree
Diffstat (limited to 'cInput.cpp')
-rw-r--r--cInput.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/cInput.cpp b/cInput.cpp
new file mode 100644
index 0000000..e0aa233
--- /dev/null
+++ b/cInput.cpp
@@ -0,0 +1,74 @@
+#include "cInput.h"
+
+cInput::cInput()
+{
+ // Save original serial communication configuration for stdin
+ tcgetattr( STDIN_FILENO, &original);
+
+ // Put stdin in raw mode so keys get through directly without
+ // requiring pressing enter.
+ cfmakeraw (&raw);
+ tcsetattr (STDIN_FILENO, TCSANOW, &raw);
+
+ // Switch to the alternate buffer screen
+ write (STDOUT_FILENO, "\e[?47h", 6);
+
+ // Enable mouse tracking
+ write (STDOUT_FILENO, "\e[?9h", 5);
+}
+
+cInput::~cInput()
+{
+ //revert changes to console
+ write (STDOUT_FILENO, "\e[?9l", 5);
+ write (STDOUT_FILENO, "\e[?47l", 6);
+ tcsetattr (STDIN_FILENO, TCSANOW, &original);
+}
+
+sInputEvent cInput::poll()
+{
+ sInputEvent ret;
+ unsigned char buff [6];
+
+ //setup for select
+ fd_set rfds;
+ struct timeval tv;
+ FD_ZERO(&rfds);
+ FD_SET(STDIN_FILENO, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 0;
+
+ ret.type = _EVENT_NULL;
+
+ //Check for Input. return of none
+ if(!select(1, &rfds, NULL, NULL, &tv))
+ return ret;
+
+ read (STDIN_FILENO, &buff, 1);
+
+ if (buff[0] == '\x1B') //Escape sequence
+ {
+ read (STDIN_FILENO, &buff, 5);
+ if(buff[0] == '[')
+ {
+ if(buff[1] == 'M') //Mouse Event
+ {
+ ret.b = buff[2] - 32;
+ ret.x = buff[3] - 32;
+ ret.y = buff[4] - 32;
+ ret.type = _EVENT_MOUSE;
+ }
+ else //e.g. Arrow Keys
+ {
+ ret.c = buff[1];
+ ret.type = _EVENT_KEY;
+ }
+ }
+ }
+ else
+ {
+ ret.type = _EVENT_CHAR;
+ ret.c = buff[0];
+ }
+ return ret;
+}