-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal_read.c
75 lines (70 loc) · 2.03 KB
/
terminal_read.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @file terminal_read.c
* @author Lukas Nejezchleb ([email protected])
* @brief Module housing the thread function for reading the terminal in nonblocking mode and setting the last key pressed variable
* @version 0.1
* @date 2021-05-04
*
* @copyright Copyright (c) 2021
*
*/
#include "terminal_read.h"
#include "data_structures.h"
#include <stdbool.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#define TERMINAL_TIMEOUT_MS 100
void *input_thread(void *d)
{
read_thread_data_type *data = (read_thread_data_type *)d;
bool end = false;
// put terminal into raw mode
struct termios tio, tioOld;
tcgetattr(STDIN_FILENO, &tio);
tioOld = tio; // backup
cfmakeraw(&tio);
tio.c_oflag |= OPOST;
tcsetattr(STDIN_FILENO, TCSANOW, &tio);
// start reading terminal
unsigned char c;
while (!end)
{
int r = serial_getc_timeout(STDIN_FILENO, TERMINAL_TIMEOUT_MS, &c);
if (r > 0)
{
pthread_mutex_lock(&mtx);
data->last_read = c;
pthread_mutex_unlock(&mtx);
pthread_cond_broadcast(&character_has_been_read);
}
else if (r < 0)
{
// error reading from stdin
fprintf(stderr, "Error occured when trying to read from stdin\n");
end = true;
}
pthread_mutex_lock(&mtx);
end = end || data->quit; // check for quit
pthread_mutex_unlock(&mtx);
}
// reset terminal to original mode
tcsetattr(STDIN_FILENO, TCSANOW, &tioOld);
return NULL;
}
// inside function to read terminal in nonblocking regime
int serial_getc_timeout(int fd, int timeout_ms, unsigned char *c)
{
struct pollfd ufdr[1];
int r = 0;
ufdr[0].fd = fd;
ufdr[0].events = POLLIN | POLLRDNORM;
if ((poll(&ufdr[0], 1, timeout_ms) > 0) &&
(ufdr[0].revents & (POLLIN | POLLRDNORM)))
{
r = read(fd, c, 1);
}
return r;
}