-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.c
104 lines (94 loc) · 2.97 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <windows.h>
#include <conio.h>
#include "utils.h"
uint64_t get_tick_count(void)
{
#ifdef WIN32
return GetTickCount64();
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
}
static pthread_mutex_t s_lock = (pthread_mutex_t)NULL;
static pthread_t s_hthread = (pthread_t )NULL;
#define MAXBUFZIE 256
#define FLAG_EXIT (1 << 0)
#define FLAG_PAUSE (1 << 1)
static int s_flags = 0;
static int s_head, s_tail, s_size;
static int s_buff[MAXBUFZIE];
static void* console_thread_proc(void *arg)
{
while (!(s_flags & FLAG_EXIT)) {
if (s_flags & FLAG_PAUSE) { usleep(100 * 1000); continue; }
int c = fgetc(stdin);
pthread_mutex_lock(&s_lock);
if (!(s_flags & FLAG_PAUSE)) {
s_buff[s_tail++] = c; s_tail %= MAXBUFZIE;
if (s_size < MAXBUFZIE) s_size++;
else s_head = s_tail;
}
pthread_mutex_unlock(&s_lock);
}
return NULL;
}
static void console_thread_signal(void)
{
DWORD n = 0;
INPUT_RECORD rec = {};
rec.EventType = KEY_EVENT;
rec.Event.KeyEvent.bKeyDown = TRUE;
rec.Event.KeyEvent.wRepeatCount = 1;
rec.Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
rec.Event.KeyEvent.uChar.AsciiChar = '\r';
WriteConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &rec, 1, &n);
rec.Event.KeyEvent.uChar.AsciiChar = '\n';
WriteConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &rec, 1, &n);
}
static void console_thread_pause(void)
{
if (!(s_flags & FLAG_PAUSE)) {
pthread_mutex_lock(&s_lock);
s_flags |= FLAG_PAUSE;
console_thread_signal();
pthread_mutex_unlock(&s_lock);
}
}
void console_init(void)
{
if (!s_lock) pthread_mutex_init(&s_lock, NULL);
if (!s_hthread) { s_flags = 0; pthread_create(&s_hthread, NULL, console_thread_proc, NULL); }
}
void console_exit(void)
{
pthread_mutex_lock(&s_lock);
s_flags |= FLAG_EXIT;
console_thread_signal();
pthread_mutex_unlock(&s_lock);
if (s_hthread) { pthread_join(s_hthread, NULL); s_hthread = (pthread_t )NULL; }
if (s_lock ) { pthread_mutex_destroy(&s_lock); s_lock = (pthread_mutex_t)NULL; }
}
int console_getc(void)
{
int c = EOF;
pthread_mutex_lock(&s_lock);
s_flags &= ~FLAG_PAUSE;
if (s_size) { c = s_buff[s_head++]; s_head %= MAXBUFZIE; s_size--; }
pthread_mutex_unlock(&s_lock);
return c;
}
int console_getch (void) { console_thread_pause(); return getch(); }
int console_kbhit (void) { console_thread_pause(); return kbhit(); }
void console_clrscr(void) { system("cls"); }
void console_gotoxy(int x, int y)
{
COORD coord = { .X = x, .Y = y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}