improved async input + improved build system + added time values to move struct

Wed, 09 Apr 2014 09:34:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 09:34:07 +0200
changeset 32
8a0b85303ee8
parent 31
ed440bcd9740
child 33
866025982aa9

improved async input + improved build system + added time values to move struct

Makefile file | annotate | diff | comparison | revisions
conf.mk file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/chess/Makefile file | annotate | diff | comparison | revisions
src/chess/conf.mk file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
src/game.c file | annotate | diff | comparison | revisions
src/input.c file | annotate | diff | comparison | revisions
src/input.h file | annotate | diff | comparison | revisions
src/network.h file | annotate | diff | comparison | revisions
     1.1 --- a/Makefile	Tue Apr 08 21:13:28 2014 +0200
     1.2 +++ b/Makefile	Wed Apr 09 09:34:07 2014 +0200
     1.3 @@ -28,14 +28,17 @@
     1.4  
     1.5  include conf.mk
     1.6  
     1.7 -all: build chess
     1.8 +all: chess
     1.9  	cd src; $(MAKE)
    1.10  
    1.11 -build:
    1.12 -	$(MKDIR) build
    1.13 +debug: chess-debug
    1.14 +	cd src; $(MAKE) debug
    1.15  
    1.16  chess:
    1.17  	cd src/chess; $(MAKE) BUILDDIR=../../build CONFIG=../../conf.mk
    1.18 +
    1.19 +chess-debug:
    1.20 +	cd src/chess; $(MAKE) debug BUILDDIR=../../build CONFIG=../../conf.mk
    1.21  	
    1.22  clean:
    1.23 -	$(RM) -f -R build
    1.24 +	$(RM) $(RMFLAGS) build
     2.1 --- a/conf.mk	Tue Apr 08 21:13:28 2014 +0200
     2.2 +++ b/conf.mk	Wed Apr 09 09:34:07 2014 +0200
     2.3 @@ -32,7 +32,8 @@
     2.4  
     2.5  BIN        = terminal-chess
     2.6  CC         = gcc
     2.7 -CFLAGS     = -g -O2 -std=gnu99 -Wall -pedantic
     2.8 +CFLAGS     = -O2 -std=gnu99
     2.9 +CFLAGS_D   = -g -std=gnu99 -Wall -pedantic
    2.10  LD         = gcc
    2.11  LDFLAGS    = -lncurses
    2.12  ARFLAGS    = -r
     3.1 --- a/src/Makefile	Tue Apr 08 21:13:28 2014 +0200
     3.2 +++ b/src/Makefile	Wed Apr 09 09:34:07 2014 +0200
     3.3 @@ -35,12 +35,26 @@
     3.4  SRC += client.c
     3.5  SRC += game.c
     3.6  
     3.7 -OBJ = $(SRC:%.c=../build/%$(OBJ_EXT))
     3.8 +OBJ = $(SRC:%.c=../build/release/%$(OBJ_EXT))
     3.9 +OBJ_D = $(SRC:%.c=../build/debug/%$(OBJ_EXT))
    3.10  
    3.11  all: $(OBJ)
    3.12 -	$(LD) -o ../build/$(BIN) $^ ../build/chess$(LIB_EXT) $(LDFLAGS)
    3.13 -	
    3.14 +	$(LD) -o ../build/release/$(BIN) $^ \
    3.15 +	../build/release/chess$(LIB_EXT) $(LDFLAGS)
    3.16  
    3.17 -../build/%$(OBJ_EXT): %.c
    3.18 +debug: $(OBJ_D)
    3.19 +	$(LD) -o ../build/debug/$(BIN) $^ \
    3.20 +	../build/debug/chess$(LIB_EXT) $(LDFLAGS)	
    3.21 +
    3.22 +../build/release/%$(OBJ_EXT): %.c ../build/release
    3.23  	$(CC) -o $@ $(CFLAGS) -c $<
    3.24  
    3.25 +../build/debug/%$(OBJ_EXT): %.c ../build/debug
    3.26 +	$(CC) -o $@ $(CFLAGS_D) -c $<
    3.27 +	
    3.28 +../build/release:
    3.29 +	$(MKDIR) $(MKDIRFLAGS) ../build/release
    3.30 +	
    3.31 +../build/debug:
    3.32 +	$(MKDIR) $(MKDIRFLAGS) ../build/debug
    3.33 +
     4.1 --- a/src/chess/Makefile	Tue Apr 08 21:13:28 2014 +0200
     4.2 +++ b/src/chess/Makefile	Wed Apr 09 09:34:07 2014 +0200
     4.3 @@ -39,17 +39,29 @@
     4.4  SRC += king.c
     4.5  SRC += rules.c
     4.6  
     4.7 -OBJ = $(SRC:%.c=$(BUILDDIR)/%$(OBJ_EXT))
     4.8 +OBJ = $(SRC:%.c=$(BUILDDIR)/release/%$(OBJ_EXT))
     4.9 +OBJ_D = $(SRC:%.c=$(BUILDDIR)/debug/%$(OBJ_EXT))
    4.10  
    4.11  all: $(OBJ)
    4.12 -	$(AR) $(ARFLAGS) $(BUILDDIR)/chess$(LIB_EXT) $(OBJ)
    4.13 +	$(AR) $(ARFLAGS) $(BUILDDIR)/release/chess$(LIB_EXT) $(OBJ)
    4.14 +
    4.15 +debug: $(OBJ_D)
    4.16 +	$(AR) $(ARFLAGS) $(BUILDDIR)/debug/chess$(LIB_EXT) $(OBJ_D)	
    4.17 +
    4.18 +$(BUILDDIR)/release/%$(OBJ_EXT): %.c $(BUILDDIR)/release
    4.19 +	$(CC) -o $@ $(CFLAGS) -c $<
    4.20  	
    4.21 -
    4.22 -$(BUILDDIR)/%$(OBJ_EXT): %.c
    4.23 -	$(CC) -o $@ $(CFLAGS) -c $<
    4.24 +$(BUILDDIR)/debug/%$(OBJ_EXT): %.c $(BUILDDIR)/debug
    4.25 +	$(CC) -o $@ $(CFLAGS_D) -c $<
    4.26  
    4.27  $(BUILDDIR):
    4.28  	$(MKDIR) $(MKDIRFLAGS) $(BUILDDIR)
    4.29  	
    4.30  clear:
    4.31  	$(RM) $(RMFLAGS) $(BUILDDIR)
    4.32 +	
    4.33 + $(BUILDDIR)/release:
    4.34 +	$(MKDIR) $(MKDIRFLAGS)  $(BUILDDIR)/release
    4.35 +	
    4.36 + $(BUILDDIR)/debug:
    4.37 +	$(MKDIR) $(MKDIRFLAGS)  $(BUILDDIR)/debug
     5.1 --- a/src/chess/conf.mk	Tue Apr 08 21:13:28 2014 +0200
     5.2 +++ b/src/chess/conf.mk	Wed Apr 09 09:34:07 2014 +0200
     5.3 @@ -31,7 +31,8 @@
     5.4  AR      = ar
     5.5  
     5.6  CC         = gcc
     5.7 -CFLAGS     = -g -O2 -std=gnu99 -Wall -pedantic
     5.8 +CFLAGS_D   = -g -std=gnu99 -Wall -pedantic
     5.9 +CFLAGS     = -O2 -std=gnu99
    5.10  ARFLAGS    = -r
    5.11  MKDIRFLAGS = -p
    5.12  RMFLAGS    = -f -R
     6.1 --- a/src/chess/rules.h	Tue Apr 08 21:13:28 2014 +0200
     6.2 +++ b/src/chess/rules.h	Wed Apr 09 09:34:07 2014 +0200
     6.3 @@ -31,6 +31,7 @@
     6.4  #define	RULES_H
     6.5  
     6.6  #include <stdint.h>
     6.7 +#include <time.h>
     6.8  
     6.9  #define VALID_MOVE_SYNTAX   0
    6.10  #define INVALID_MOVE_SYNTAX 1
    6.11 @@ -76,6 +77,8 @@
    6.12      uint8_t tofile;
    6.13      uint8_t torow;
    6.14      uint8_t promotion;
    6.15 +    struct timespec timestamp;
    6.16 +    struct timespec movetime;
    6.17      _Bool check;
    6.18      _Bool capture;
    6.19  } Move;
     7.1 --- a/src/game.c	Tue Apr 08 21:13:28 2014 +0200
     7.2 +++ b/src/game.c	Wed Apr 09 09:34:07 2014 +0200
     7.3 @@ -153,8 +153,8 @@
     7.4  static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) {
     7.5      
     7.6      const size_t buflen = 8;
     7.7 +    size_t bufpos = 0;
     7.8      char movestr[buflen];
     7.9 -    movestr[0] = '\0';
    7.10      
    7.11      int inputy = getmaxy(stdscr) - 6;
    7.12      while (1) {
    7.13 @@ -167,7 +167,7 @@
    7.14          clrtoeol();
    7.15          refresh();
    7.16          
    7.17 -        if (asyncgetnstr(movestr, buflen)) {
    7.18 +        if (asyncgetnstr(movestr, &bufpos, buflen)) {
    7.19              if (strncmp(movestr, "surr", buflen) == 0) {
    7.20                  printw("%s surrendered!",
    7.21                      gamestate->mycolor==WHITE?"White":"Black");
    7.22 @@ -206,7 +206,6 @@
    7.23                  }
    7.24                  clrtoeol();
    7.25              }
    7.26 -            movestr[0] = '\0'; /* reset string for next input */
    7.27          }
    7.28      }
    7.29  }
     8.1 --- a/src/input.c	Tue Apr 08 21:13:28 2014 +0200
     8.2 +++ b/src/input.c	Wed Apr 09 09:34:07 2014 +0200
     8.3 @@ -28,6 +28,7 @@
     8.4   */
     8.5  
     8.6  #include "input.h"
     8.7 +#include <string.h>
     8.8  #include <ctype.h>
     8.9  
    8.10  void init_colorpairs() {
    8.11 @@ -49,49 +50,25 @@
    8.12      return ch == 'y';
    8.13  }
    8.14  
    8.15 -/**
    8.16 - * Asynchronous variant of getnstr().
    8.17 - * 
    8.18 - * Needs halfdelay mode enabled!
    8.19 - * 
    8.20 - * Warning: you must not call this function for reading into two separate
    8.21 - * buffers at the same time.
    8.22 - * 
    8.23 - * Attention: the first byte of the buffer must be zero at the first call, so
    8.24 - * the buffer pointer is initialized correctly.
    8.25 - * 
    8.26 - * @param w the window
    8.27 - * @param y the window y position
    8.28 - * @param x the window x position
    8.29 - * @param str the buffer for the read string
    8.30 - * @param len the length of the buffer
    8.31 - * @return 0 if reading is in progress and 1 when a complete line is read
    8.32 - */
    8.33 -int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len) {
    8.34 -    static size_t pos = 0;
    8.35 -    
    8.36 -    if (*str == '\0') {
    8.37 -        memset(str, 0, len);
    8.38 -        pos = 0;
    8.39 -    }
    8.40 -    
    8.41 -    mvwaddstr(w,y, x, str);
    8.42 +int mvwasyncgetnstr(WINDOW* w,int y,int x,char *str,size_t *pos,size_t len) {
    8.43 +    mvwaddnstr(w, y, x, str, *pos);
    8.44      wrefresh(w);
    8.45      int c = wgetch(w);
    8.46  
    8.47      if (c != ERR) {
    8.48          switch (c) {
    8.49 +        case KEY_DOWN:
    8.50          case '\n':
    8.51 -            str[pos] = '\0';
    8.52 -            pos = 0;
    8.53 +            str[*pos] = '\0';
    8.54 +            *pos = 0;
    8.55              return 1;
    8.56          case KEY_BACKSPACE:
    8.57          case KEY_LEFT:
    8.58 -            str[--pos] = '\0';
    8.59 +            str[--(*pos)] = '\0';
    8.60              break;
    8.61          default:
    8.62 -            if (isprint(c) && pos < len-1) {
    8.63 -                str[pos++] = (char) c;
    8.64 +            if (isprint(c) && *pos < len-1) {
    8.65 +                str[(*pos)++] = (char) c;
    8.66              }
    8.67          }
    8.68      }
     9.1 --- a/src/input.h	Tue Apr 08 21:13:28 2014 +0200
     9.2 +++ b/src/input.h	Wed Apr 09 09:34:07 2014 +0200
     9.3 @@ -45,10 +45,48 @@
     9.4  
     9.5  int prompt_yesno(char *msg);
     9.6  
     9.7 -int mvwasyncgetnstr(WINDOW* w, int y, int x, char *str, size_t len);
     9.8 -#define mvasyncgetnstr(y,x,str,len) mvwasyncgetnstr(stdscr,y,x,str,len)
     9.9 -#define asyncgetnstr(str,len) mvwasyncgetnstr(stdscr, stdscr->_cury, \
    9.10 -    stdscr->_curx, str, len)
    9.11 +
    9.12 +/**
    9.13 + * Asynchronous variant of mvwgetnstr().
    9.14 + * 
    9.15 + * Needs halfdelay mode enabled!
    9.16 + * 
    9.17 + * @param w the window
    9.18 + * @param y the window y position
    9.19 + * @param x the window x position
    9.20 + * @param str the buffer for the read string
    9.21 + * @param pos a pointer to the object containing the current buffer position
    9.22 + * @param len the length of the buffer
    9.23 + * @return 0 if reading is in progress and 1 when a complete line is read
    9.24 + */
    9.25 +int mvwasyncgetnstr(WINDOW* w,int y,int x,char *str,size_t *pos,size_t len);
    9.26 +
    9.27 +/**
    9.28 + * Asynchronous variant of mvgetnstr().
    9.29 + * 
    9.30 + * Needs halfdelay mode enabled!
    9.31 + * 
    9.32 + * @param y the window y position
    9.33 + * @param x the window x position
    9.34 + * @param str the buffer for the read string
    9.35 + * @param pos a pointer to the object containing the current buffer position
    9.36 + * @param len the length of the buffer
    9.37 + * @return 0 if reading is in progress and 1 when a complete line is read
    9.38 + */
    9.39 +#define mvasyncgetnstr(y,x,str,pos,len) mvwasyncgetnstr(stdscr,y,x,str,pos,len)
    9.40 +
    9.41 +/**
    9.42 + * Asynchronous variant of getnstr().
    9.43 + * 
    9.44 + * Needs halfdelay mode enabled!
    9.45 + * 
    9.46 + * @param str the buffer for the read string
    9.47 + * @param pos a pointer to the object containing the current buffer position
    9.48 + * @param len the length of the buffer
    9.49 + * @return 0 if reading is in progress and 1 when a complete line is read
    9.50 + */
    9.51 +#define asyncgetnstr(str,pos,len) mvwasyncgetnstr(stdscr, stdscr->_cury, \
    9.52 +    stdscr->_curx, str, pos, len)
    9.53  
    9.54  
    9.55  #ifdef	__cplusplus
    10.1 --- a/src/network.h	Tue Apr 08 21:13:28 2014 +0200
    10.2 +++ b/src/network.h	Wed Apr 09 09:34:07 2014 +0200
    10.3 @@ -47,7 +47,7 @@
    10.4  #define NETCODE_CHECKMATE 0x24
    10.5  #define NETCODE_STALEMATE 0x25
    10.6  
    10.7 -#define NETCODE_VERSION 7
    10.8 +#define NETCODE_VERSION 10
    10.9  
   10.10  typedef struct {
   10.11      int fd; /* -1, if we are the client */

mercurial