index — thing @ a9bf0f76676ecdd38e8cb896255da56e18185e80

A little something I'm working on

learning: use polling for stdin
crispy-caesus crispy@crispy-caesus.eu
Mon, 27 Apr 2026 21:39:57 +0200
commit

a9bf0f76676ecdd38e8cb896255da56e18185e80

parent

380805578271e79be47c50674a5fe0013e818d8b

1 files changed, 18 insertions(+), 9 deletions(-)

jump to
M main.cmain.c

@@ -1,6 +1,7 @@

#include <stdint.h> #include <stdio.h> +#include <poll.h> #include <termios.h> #include <unistd.h>

@@ -18,21 +19,29 @@ new.c_cc[VTIME] = 0; // time before return

tcsetattr(STDIN_FILENO, TCSANOW, &new); + struct pollfd poller[1]; + + poller[0].fd = STDIN_FILENO; + poller[0].events = POLLIN; + 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; + int events = poll(poller, 1, -1); + if (events > 0 && poller[0].revents & POLLIN) { + 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"); } - printf("\n"); } }