login

<     >

2020-10-14 00:24:27 (UTC-03:00)

Marcel Rodrigues <marcelgmr@gmail.com>

input handling skeleton code

diff --git a/seqt.c b/seqt.c
index 8db0f93..70b181d 100644
--- a/seqt.c
+++ b/seqt.c
@@ -100,6 +100,9 @@ main(int argc, char *argv[])
 {
     FILE *fin;
     char *fname;
+    struct termios term_prev;
+    int running;
+    char key;
     if (argc < 2) {
         fprintf(stderr, "usage:\n  %s file\n", argv[0]);
         return 1;
@@ -111,6 +114,17 @@ main(int argc, char *argv[])
         return 1;
     }
     fclose(fin);
+    setup_terminal(&term_prev);
     print_tracks(0, 10, 0, 3);
+    running = 1;
+    while (running) {
+        key = getchar();
+        switch (key) {
+        case 'q':
+            running = 0;
+            break;
+        }
+    }
+    restore_terminal(&term_prev);
     return 0;
 }

diff --git a/seqt.h b/seqt.h
index 235d9c1..b02c796 100644
--- a/seqt.h
+++ b/seqt.h
@@ -2,6 +2,7 @@
 #define SEQT_H
 
 #include <stdio.h>
+#include <termios.h>
 
 /* ============================= Matrix ============================= */
 
@@ -51,6 +52,11 @@ typedef enum TxtStt {MTDT, TKNM, EVNT} TxtStt;
 int load_txt(FILE *fp);
 int save_txt(FILE *fp);
 
+/* ============================ terminal =======]==================== */
+
+void setup_terminal(struct termios *term_prev);
+void restore_terminal(struct termios *term_prev);
+
 /* ====================[=====+=====  =====+=====]==================== */
 
 extern int ntracks;

diff --git a/term.c b/term.c
new file mode 100644
index 0000000..d4fcb04
--- /dev/null
+++ b/term.c
@@ -0,0 +1,21 @@
+#include "seqt.h"
+
+void
+setup_terminal(struct termios *term_prev)
+{
+    struct termios term_raw;
+
+    tcgetattr(0, term_prev);
+    term_raw = *term_prev;
+    term_raw.c_lflag &= ~(ECHO | ICANON);
+    /* blocking read */
+    term_raw.c_cc[VMIN] = 1;
+    term_raw.c_cc[VTIME] = 0;
+    tcsetattr(0, TCSAFLUSH, &term_raw);
+}
+
+void
+restore_terminal(struct termios *term_prev)
+{
+    tcsetattr(0, TCSAFLUSH, term_prev);
+}