-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
78 lines (70 loc) · 1.51 KB
/
misc.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
#include "stdio.h"
#include "defs.h"
#include "string.h"
#ifdef WIN32
#include <windows.h>
#include <io.h>
#else
#include <sys/time.h>
#include <unistd.h>
#endif
unsigned long GetTimeMs() {
#ifdef WIN32
return GetTickCount();
#else
struct timeval t;
gettimeofday(&t, NULL);
return t.tv_sec*1000 + t.tv_usec/1000;
#endif
}
// http://home.arcor.de/dreamlike/chess/
int InputWaiting()
{
#ifndef WIN32
fd_set readfds;
struct timeval tv;
FD_ZERO (&readfds);
FD_SET (fileno(stdin), &readfds);
tv.tv_sec=0; tv.tv_usec=0;
select(16, &readfds, 0, 0, &tv);
return (FD_ISSET(fileno(stdin), &readfds));
#else
static int init = 0, pipe;
static HANDLE inh;
DWORD dw;
if (!init) {
init = 1;
inh = GetStdHandle(STD_INPUT_HANDLE);
pipe = !GetConsoleMode(inh, &dw);
if (!pipe) {
SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT|ENABLE_WINDOW_INPUT));
FlushConsoleInputBuffer(inh);
}
}
if (pipe) {
if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL)) return 1;
return dw;
} else {
GetNumberOfConsoleInputEvents(inh, &dw);
return dw <= 1 ? 0 : dw;
}
#endif
}
void ReadInput(S_SEARCHINFO *info) {
int bytes;
char input[256] = "", *endc;
if (InputWaiting()) {
info->stopped = TRUE;
do {
bytes = read(fileno(stdin), input, 256);
} while (bytes<0);
endc = strchr(input,'\n');
if (endc) *endc=0;
if (strlen(input) > 0) {
if (!strncmp(input, "quit", 4)) {
info->quit = TRUE;
}
}
return;
}
}