-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbncli_unix.c
46 lines (37 loc) · 1 KB
/
rbncli_unix.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
#include "rbncli.h"
#include <pthread.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void rbncli_platform_init() {
pthread_mutex_init(&mutex, NULL);
}
uint64_t rbncli_get_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t)tv.tv_usec + (uint64_t)tv.tv_sec * 1000000;
}
void rbncli_sleep(uint32_t ms) {
usleep(ms * 1000);
}
void rbncli_lock() {
pthread_mutex_lock(&mutex);
}
void rbncli_unlock() {
pthread_mutex_unlock(&mutex);
}
void rbncli_clear_screen() {
system("clear");
}
int rbncli_getch() {
struct termios old, current;
tcgetattr(0, &old); /* grab old terminal i/o settings */
current = old; /* make new settings same as old settings */
current.c_lflag &= ~ICANON; /* disable buffered i/o */
current.c_lflag &= ~ECHO; /* set no echo mode */
tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */
int ch = getchar();
tcsetattr(0, TCSANOW, &old);
return ch;
}