-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathUtils.cpp
232 lines (205 loc) · 5.2 KB
/
Utils.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "config.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <thread>
#include <mutex>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/select.h>
#endif
#include "Utils.h"
#include "GTP.h"
Utils::ThreadPool thread_pool;
bool Utils::input_causes_stop() {
return true;
}
bool Utils::input_pending(void) {
#ifdef HAVE_SELECT
fd_set read_fds;
struct timeval timeout;
FD_ZERO(&read_fds);
FD_SET(0,&read_fds);
timeout.tv_sec = timeout.tv_usec = 0;
select(1,&read_fds,NULL,NULL,&timeout);
if (FD_ISSET(0,&read_fds)) {
return input_causes_stop();
} else {
return false;
}
#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)) {
myprintf("Nothing at other end - exiting\n");
exit(EXIT_FAILURE);
}
if (dw) {
return input_causes_stop();
} else {
return false;
}
} else {
if (!GetNumberOfConsoleInputEvents(inh, &dw)) {
myprintf("Nothing at other end - exiting\n");
exit(EXIT_FAILURE);
}
if (dw <= 1) {
return false;
} else {
return input_causes_stop();
}
}
#endif
return false;
}
#ifndef _CONSOLE
static wxEvtHandler * GUIQ = nullptr;
static wxEvtHandler * ANALQ = nullptr;
static int GUIQ_T = 0;
static int ANALQ_T_ANAL = 0;
static int ANALQ_T_MOVES = 0;
std::mutex GUImutex;
void Utils::setGUIQueue(wxEvtHandler * evt, int evt_type) {
std::lock_guard<std::mutex> guard(GUImutex);
GUIQ = evt;
GUIQ_T = evt_type;
}
void Utils::setAnalysisQueue(wxEvtHandler * evt, int an_evt_type,
int mv_evt_type) {
std::lock_guard<std::mutex> guard(GUImutex);
ANALQ = evt;
ANALQ_T_ANAL = an_evt_type;
ANALQ_T_MOVES = mv_evt_type;
}
#else
std::mutex IOmutex;
#endif
void Utils::GUIAnalysis(void* data) {
#ifndef _CONSOLE
std::lock_guard<std::mutex> guard(GUImutex);
if (ANALQ != NULL) {
wxCommandEvent* myevent = new wxCommandEvent(ANALQ_T_ANAL);
myevent->SetClientData(data);
::wxQueueEvent(ANALQ, myevent);
}
#endif
}
void Utils::GUIBestMoves(void* data) {
#ifndef _CONSOLE
std::lock_guard<std::mutex> guard(GUImutex);
if (ANALQ != NULL) {
wxCommandEvent* myevent = new wxCommandEvent(ANALQ_T_MOVES);
myevent->SetClientData(data);
::wxQueueEvent(ANALQ, myevent);
}
#endif
}
void Utils::GUIprintf(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
#ifndef _CONSOLE
std::lock_guard<std::mutex> guard(GUImutex);
if (GUIQ != nullptr) {
char buffer[512];
#ifdef WIN32
vsprintf_s(buffer, 512, fmt, ap);
#else
vsnprintf(buffer, 512, fmt, ap);
#endif
wxCommandEvent* myevent = new wxCommandEvent(GUIQ_T);
myevent->SetString(wxString(buffer));
::wxQueueEvent(GUIQ, myevent);
}
#endif
va_end(ap);
}
void Utils::myprintf(const char *fmt, ...) {
if (cfg_quiet) return;
#ifdef _CONSOLE
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (cfg_logfile_handle) {
std::lock_guard<std::mutex> lock(IOmutex);
va_start(ap, fmt);
vfprintf(cfg_logfile_handle, fmt, ap);
va_end(ap);
}
#endif
}
void Utils::gtp_printf(int id, const char *fmt, ...) {
va_list ap;
if (id != -1) {
fprintf(stdout, "=%d ", id);
} else {
fprintf(stdout, "= ");
}
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
printf("\n\n");
#ifdef _CONSOLE
if (cfg_logfile_handle) {
std::lock_guard<std::mutex> lock(IOmutex);
if (id != -1) {
fprintf(cfg_logfile_handle, "=%d ", id);
} else {
fprintf(cfg_logfile_handle, "= ");
}
va_start(ap, fmt);
vfprintf(cfg_logfile_handle, fmt, ap);
va_end(ap);
fprintf(cfg_logfile_handle, "\n\n");
}
#endif
}
void Utils::gtp_fail_printf(int id, const char *fmt, ...) {
va_list ap;
if (id != -1) {
fprintf(stdout, "?%d ", id);
} else {
fprintf(stdout, "? ");
}
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
printf("\n\n");
#ifdef _CONSOLE
if (cfg_logfile_handle) {
std::lock_guard<std::mutex> lock(IOmutex);
if (id != -1) {
fprintf(cfg_logfile_handle, "?%d ", id);
} else {
fprintf(cfg_logfile_handle, "? ");
}
va_start(ap, fmt);
vfprintf(cfg_logfile_handle, fmt, ap);
va_end(ap);
fprintf(cfg_logfile_handle, "\n\n");
}
#endif
}
void Utils::log_input(std::string input) {
#ifdef _CONSOLE
if (cfg_logfile_handle) {
std::lock_guard<std::mutex> lock(IOmutex);
fprintf(cfg_logfile_handle, ">>%s\n", input.c_str());
}
#endif
}