# HG changeset patch # User Mike Becker # Date 1397028847 -7200 # Node ID 8a0b85303ee8887ff6ce4f890f69a5b05853ce0b # Parent ed440bcd9740305841cf7004087c9a3eb82acc11 improved async input + improved build system + added time values to move struct diff -r ed440bcd9740 -r 8a0b85303ee8 Makefile --- a/Makefile Tue Apr 08 21:13:28 2014 +0200 +++ b/Makefile Wed Apr 09 09:34:07 2014 +0200 @@ -28,14 +28,17 @@ include conf.mk -all: build chess +all: chess cd src; $(MAKE) -build: - $(MKDIR) build +debug: chess-debug + cd src; $(MAKE) debug chess: cd src/chess; $(MAKE) BUILDDIR=../../build CONFIG=../../conf.mk + +chess-debug: + cd src/chess; $(MAKE) debug BUILDDIR=../../build CONFIG=../../conf.mk clean: - $(RM) -f -R build + $(RM) $(RMFLAGS) build diff -r ed440bcd9740 -r 8a0b85303ee8 conf.mk --- a/conf.mk Tue Apr 08 21:13:28 2014 +0200 +++ b/conf.mk Wed Apr 09 09:34:07 2014 +0200 @@ -32,7 +32,8 @@ BIN = terminal-chess CC = gcc -CFLAGS = -g -O2 -std=gnu99 -Wall -pedantic +CFLAGS = -O2 -std=gnu99 +CFLAGS_D = -g -std=gnu99 -Wall -pedantic LD = gcc LDFLAGS = -lncurses ARFLAGS = -r diff -r ed440bcd9740 -r 8a0b85303ee8 src/Makefile --- a/src/Makefile Tue Apr 08 21:13:28 2014 +0200 +++ b/src/Makefile Wed Apr 09 09:34:07 2014 +0200 @@ -35,12 +35,26 @@ SRC += client.c SRC += game.c -OBJ = $(SRC:%.c=../build/%$(OBJ_EXT)) +OBJ = $(SRC:%.c=../build/release/%$(OBJ_EXT)) +OBJ_D = $(SRC:%.c=../build/debug/%$(OBJ_EXT)) all: $(OBJ) - $(LD) -o ../build/$(BIN) $^ ../build/chess$(LIB_EXT) $(LDFLAGS) - + $(LD) -o ../build/release/$(BIN) $^ \ + ../build/release/chess$(LIB_EXT) $(LDFLAGS) -../build/%$(OBJ_EXT): %.c +debug: $(OBJ_D) + $(LD) -o ../build/debug/$(BIN) $^ \ + ../build/debug/chess$(LIB_EXT) $(LDFLAGS) + +../build/release/%$(OBJ_EXT): %.c ../build/release $(CC) -o $@ $(CFLAGS) -c $< +../build/debug/%$(OBJ_EXT): %.c ../build/debug + $(CC) -o $@ $(CFLAGS_D) -c $< + +../build/release: + $(MKDIR) $(MKDIRFLAGS) ../build/release + +../build/debug: + $(MKDIR) $(MKDIRFLAGS) ../build/debug + diff -r ed440bcd9740 -r 8a0b85303ee8 src/chess/Makefile --- a/src/chess/Makefile Tue Apr 08 21:13:28 2014 +0200 +++ b/src/chess/Makefile Wed Apr 09 09:34:07 2014 +0200 @@ -39,17 +39,29 @@ SRC += king.c SRC += rules.c -OBJ = $(SRC:%.c=$(BUILDDIR)/%$(OBJ_EXT)) +OBJ = $(SRC:%.c=$(BUILDDIR)/release/%$(OBJ_EXT)) +OBJ_D = $(SRC:%.c=$(BUILDDIR)/debug/%$(OBJ_EXT)) all: $(OBJ) - $(AR) $(ARFLAGS) $(BUILDDIR)/chess$(LIB_EXT) $(OBJ) + $(AR) $(ARFLAGS) $(BUILDDIR)/release/chess$(LIB_EXT) $(OBJ) + +debug: $(OBJ_D) + $(AR) $(ARFLAGS) $(BUILDDIR)/debug/chess$(LIB_EXT) $(OBJ_D) + +$(BUILDDIR)/release/%$(OBJ_EXT): %.c $(BUILDDIR)/release + $(CC) -o $@ $(CFLAGS) -c $< - -$(BUILDDIR)/%$(OBJ_EXT): %.c - $(CC) -o $@ $(CFLAGS) -c $< +$(BUILDDIR)/debug/%$(OBJ_EXT): %.c $(BUILDDIR)/debug + $(CC) -o $@ $(CFLAGS_D) -c $< $(BUILDDIR): $(MKDIR) $(MKDIRFLAGS) $(BUILDDIR) clear: $(RM) $(RMFLAGS) $(BUILDDIR) + + $(BUILDDIR)/release: + $(MKDIR) $(MKDIRFLAGS) $(BUILDDIR)/release + + $(BUILDDIR)/debug: + $(MKDIR) $(MKDIRFLAGS) $(BUILDDIR)/debug diff -r ed440bcd9740 -r 8a0b85303ee8 src/chess/conf.mk --- a/src/chess/conf.mk Tue Apr 08 21:13:28 2014 +0200 +++ b/src/chess/conf.mk Wed Apr 09 09:34:07 2014 +0200 @@ -31,7 +31,8 @@ AR = ar CC = gcc -CFLAGS = -g -O2 -std=gnu99 -Wall -pedantic +CFLAGS_D = -g -std=gnu99 -Wall -pedantic +CFLAGS = -O2 -std=gnu99 ARFLAGS = -r MKDIRFLAGS = -p RMFLAGS = -f -R diff -r ed440bcd9740 -r 8a0b85303ee8 src/chess/rules.h --- a/src/chess/rules.h Tue Apr 08 21:13:28 2014 +0200 +++ b/src/chess/rules.h Wed Apr 09 09:34:07 2014 +0200 @@ -31,6 +31,7 @@ #define RULES_H #include +#include #define VALID_MOVE_SYNTAX 0 #define INVALID_MOVE_SYNTAX 1 @@ -76,6 +77,8 @@ uint8_t tofile; uint8_t torow; uint8_t promotion; + struct timespec timestamp; + struct timespec movetime; _Bool check; _Bool capture; } Move; diff -r ed440bcd9740 -r 8a0b85303ee8 src/game.c --- a/src/game.c Tue Apr 08 21:13:28 2014 +0200 +++ b/src/game.c Wed Apr 09 09:34:07 2014 +0200 @@ -153,8 +153,8 @@ static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) { const size_t buflen = 8; + size_t bufpos = 0; char movestr[buflen]; - movestr[0] = '\0'; int inputy = getmaxy(stdscr) - 6; while (1) { @@ -167,7 +167,7 @@ clrtoeol(); refresh(); - if (asyncgetnstr(movestr, buflen)) { + if (asyncgetnstr(movestr, &bufpos, buflen)) { if (strncmp(movestr, "surr", buflen) == 0) { printw("%s surrendered!", gamestate->mycolor==WHITE?"White":"Black"); @@ -206,7 +206,6 @@ } clrtoeol(); } - movestr[0] = '\0'; /* reset string for next input */ } } } diff -r ed440bcd9740 -r 8a0b85303ee8 src/input.c --- a/src/input.c Tue Apr 08 21:13:28 2014 +0200 +++ b/src/input.c Wed Apr 09 09:34:07 2014 +0200 @@ -28,6 +28,7 @@ */ #include "input.h" +#include #include void init_colorpairs() { @@ -49,49 +50,25 @@ return ch == 'y'; } -/** - * Asynchronous variant of getnstr(). - * - * Needs halfdelay mode enabled! - * - * Warning: you must not call this function for reading into two separate - * buffers at the same time. - * - * Attention: the first byte of the buffer must be zero at the first call, so - * the buffer pointer is initialized correctly. - * - * @param w the window - * @param y the window y position - * @param x the window x position - * @param str the buffer for the read string - * @param len the length of the buffer - * @return 0 if reading is in progress and 1 when a complete line is read - */ -int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) { - static size_t pos = 0; - - if (*str == '\0') { - memset(str, 0, len); - pos = 0; - } - - mvwaddstr(w,y, x, str); +int mvwasyncgetnstr(WINDOW* w,int y,int x,char *str,size_t *pos,size_t len) { + mvwaddnstr(w, y, x, str, *pos); wrefresh(w); int c = wgetch(w); if (c != ERR) { switch (c) { + case KEY_DOWN: case '\n': - str[pos] = '\0'; - pos = 0; + str[*pos] = '\0'; + *pos = 0; return 1; case KEY_BACKSPACE: case KEY_LEFT: - str[--pos] = '\0'; + str[--(*pos)] = '\0'; break; default: - if (isprint(c) && pos < len-1) { - str[pos++] = (char) c; + if (isprint(c) && *pos < len-1) { + str[(*pos)++] = (char) c; } } } diff -r ed440bcd9740 -r 8a0b85303ee8 src/input.h --- a/src/input.h Tue Apr 08 21:13:28 2014 +0200 +++ b/src/input.h Wed Apr 09 09:34:07 2014 +0200 @@ -45,10 +45,48 @@ int prompt_yesno(char *msg); -int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len); -#define mvasyncgetnstr(y,x,str,len) mvwasyncgetnstr(stdscr,y,x,str,len) -#define asyncgetnstr(str,len) mvwasyncgetnstr(stdscr, stdscr->_cury, \ - stdscr->_curx, str, len) + +/** + * Asynchronous variant of mvwgetnstr(). + * + * Needs halfdelay mode enabled! + * + * @param w the window + * @param y the window y position + * @param x the window x position + * @param str the buffer for the read string + * @param pos a pointer to the object containing the current buffer position + * @param len the length of the buffer + * @return 0 if reading is in progress and 1 when a complete line is read + */ +int mvwasyncgetnstr(WINDOW* w,int y,int x,char *str,size_t *pos,size_t len); + +/** + * Asynchronous variant of mvgetnstr(). + * + * Needs halfdelay mode enabled! + * + * @param y the window y position + * @param x the window x position + * @param str the buffer for the read string + * @param pos a pointer to the object containing the current buffer position + * @param len the length of the buffer + * @return 0 if reading is in progress and 1 when a complete line is read + */ +#define mvasyncgetnstr(y,x,str,pos,len) mvwasyncgetnstr(stdscr,y,x,str,pos,len) + +/** + * Asynchronous variant of getnstr(). + * + * Needs halfdelay mode enabled! + * + * @param str the buffer for the read string + * @param pos a pointer to the object containing the current buffer position + * @param len the length of the buffer + * @return 0 if reading is in progress and 1 when a complete line is read + */ +#define asyncgetnstr(str,pos,len) mvwasyncgetnstr(stdscr, stdscr->_cury, \ + stdscr->_curx, str, pos, len) #ifdef __cplusplus diff -r ed440bcd9740 -r 8a0b85303ee8 src/network.h --- a/src/network.h Tue Apr 08 21:13:28 2014 +0200 +++ b/src/network.h Wed Apr 09 09:34:07 2014 +0200 @@ -47,7 +47,7 @@ #define NETCODE_CHECKMATE 0x24 #define NETCODE_STALEMATE 0x25 -#define NETCODE_VERSION 7 +#define NETCODE_VERSION 10 typedef struct { int fd; /* -1, if we are the client */