6502

minimal 6502 emulator written in C
Index Commits Files Refs README LICENSE
tui.c (995B)
   1 #include "tui.h"
   2 
   3 #include <stdio.h>
   4 #include <stdlib.h>
   5 #include <termios.h>
   6 #include <unistd.h>
   7 
   8 
   9 /* variable to remember original terminal attributes. */
  10 static struct termios default_attributes;
  11 
  12 void reset_input_mode (void) {
  13     tcsetattr(STDIN_FILENO, TCSANOW, &default_attributes);
  14 
  15     /* restore cursor */
  16     printf("\033[?25h");
  17 }
  18 
  19 void set_input_mode (void) {
  20     struct termios tattr;
  21 
  22     /* Make sure stdin is a terminal. */
  23     if (!isatty(STDIN_FILENO)) {
  24         fprintf (stderr, "Not a terminal.\n");
  25         exit (EXIT_FAILURE);
  26     }
  27 
  28     /* Save the terminal attributes so we can restore them later. */
  29     tcgetattr(STDIN_FILENO, &default_attributes);
  30     atexit(reset_input_mode);
  31 
  32     /* hide cursor */
  33     printf("\033[?25l");
  34 
  35     /* Set the terminal modes. */
  36     tcgetattr(STDIN_FILENO, &tattr);
  37     tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
  38     tattr.c_cc[VMIN] = 1;
  39     tattr.c_cc[VTIME] = 0;
  40     tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
  41 }