index — thing @ 380805578271e79be47c50674a5fe0013e818d8b

A little something I'm working on

main.c (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
#include <stdint.h>
#include <stdio.h>

#include <termios.h>
#include <unistd.h>

int main() {

  struct termios old, new;

  tcgetattr(STDIN_FILENO, &old);

  new = old;

  new.c_lflag &= ~(ECHO | ICANON);
  new.c_cc[VMIN] = 0;  // bytes read to return
  new.c_cc[VTIME] = 0; // time before return

  tcsetattr(STDIN_FILENO, TCSANOW, &new);

  printf("Pressed characters:\n");

  uint8_t run = 1;

  while (run) {
    char buffer[10];
    uint16_t length = read(STDIN_FILENO, buffer, sizeof(buffer));
    if (length > 0) {
      uint8_t i;
      for (i = 0; i < length; i++) {
        printf(" %d", buffer[i]);
        if (buffer[i] == 'Q')
          run = 0;
      }
      printf("\n");
    }
  }

  tcsetattr(STDIN_FILENO, TCSANOW, &old);

  return 0;
}