src/main.c

changeset 225
6e39f53127e9
parent 219
24b866230dd4
child 226
341080f6bf34
equal deleted inserted replaced
224:93e0e89496fe 225:6e39f53127e9
31 31
32 #include <curses.h> 32 #include <curses.h>
33 #include <string.h> 33 #include <string.h>
34 #include <time.h> 34 #include <time.h>
35 #include <locale.h> 35 #include <locale.h>
36 #include <ctype.h>
36 #include <sys/stat.h> 37 #include <sys/stat.h>
37 #include <signal.h> 38 #include <signal.h>
38 #include <errno.h> 39 #include <errno.h>
39 #include <unistd.h> 40 #include <unistd.h>
40 #include <getopt.h> 41 #include <getopt.h>
41 42
42 #include "chess/rules.h" 43 #include "chess/rules.h"
43 #include "chess/pgn.h" 44 #include "chess/pgn.h"
44 #include "input.h"
45 #include "network.h" 45 #include "network.h"
46 #include "colors.h"
47 46
48 typedef struct { 47 typedef struct {
49 /** 48 /**
50 * Server host address. 49 * Server host address.
51 * TCP: server address or \c NULL when we are the server 50 * TCP: server address or \c NULL when we are the server
246 } else { 245 } else {
247 settings.ishost = !settings.serverhost; 246 settings.ishost = !settings.serverhost;
248 } 247 }
249 248
250 return 0; 249 return 0;
250 }
251
252 #ifdef USE_POSIX_CURSES
253 #define COL_APP 0
254 #else
255 #define COL_APP 1
256 #endif
257 #define COL_BW 2
258 #define COL_WB 3
259 #define COL_BB 4
260 #define COL_WW 5
261
262 static void init_colorpairs(void) {
263 #ifndef USE_POSIX_CURSES
264 use_default_colors();
265 init_pair(COL_APP, -1, -1);
266 #endif
267 init_pair(COL_BW, COLOR_BLACK, COLOR_CYAN);
268 init_pair(COL_BB, COLOR_BLACK, COLOR_BLUE);
269 init_pair(COL_WB, COLOR_WHITE, COLOR_BLUE);
270 init_pair(COL_WW, COLOR_WHITE, COLOR_CYAN);
271 }
272
273 static int prompt_yesno(const char *msg) {
274 addstr(msg);
275 addstr(" (y/n)? ");
276 clrtoeol();
277 refresh();
278 noecho();
279 int ch;
280 do {
281 ch = getch();
282 } while (ch != 'y' && ch != 'n');
283 echo();
284
285 return ch == 'y';
286 }
287
288 static int asyncgetnstr(char *str, size_t *pos, size_t len) {
289 WINDOW* w = stdscr;
290 int y, x;
291 getyx(w, y, x);
292 mvwaddnstr(w, y, x, str, *pos);
293 wrefresh(w);
294 int c = wgetch(w);
295
296 if (c != ERR) {
297 switch (c) {
298 case KEY_DOWN:
299 case '\n':
300 str[*pos] = '\0';
301 *pos = 0;
302 waddch(w,'\n');
303 return 1;
304 case 127: /* ASCII DEL (important for macOS) */
305 case KEY_DC: /* DEL key (for other OS) */
306 case KEY_BACKSPACE:
307 case KEY_LEFT:
308 if ((*pos) > 0) {
309 str[--(*pos)] = '\0';
310 }
311 break;
312 default:
313 if (isprint(c) && *pos < len-1) {
314 str[(*pos)++] = (char) c;
315 }
316 }
317 }
318
319 return 0;
320 }
321
322 static int input_getmaxy(void) {
323 /* POSIX-curses-compliant wrapper for getmaxy() */
324 int x, y;
325 getmaxyx(stdscr, y, x);
326 (void)x;
327 return y;
251 } 328 }
252 329
253 static const uint8_t boardx = 4, boardy = 10; 330 static const uint8_t boardx = 4, boardy = 10;
254 static int inputy = 21; /* should be overridden on game startup */ 331 static int inputy = 21; /* should be overridden on game startup */
255 332

mercurial