~vcs-imports-ii/gnuchess/trunk

« back to all changes in this revision

Viewing changes to src/frontend/engine.cc

  • Committer: aceballos
  • Date: 2017-06-12 21:52:45 UTC
  • Revision ID: svn-v4:6006247a-2d13-4e52-b961-62b44c6c37c8:trunk:157
Tags: v6.2.5-rc2
merge --reintegrate branches/readline; release 6.2.5-rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* engine.c
2
 
 
 
2
 
3
3
   GNU Chess frontend
4
4
 
5
5
   Copyright (C) 2001-2015 Free Software Foundation, Inc.
52
52
/* A BUF_SIZE long char string made up of null characters */
53
53
static char zerochar[BUF_SIZE];
54
54
 
55
 
/* Whether the prompt must be displayed or not. */
56
 
static int showprompt=1;
57
 
 
58
55
/* Buffer storing the input entered by the user */
59
56
char userinputbuf[BUF_SIZE]="";
60
57
 
79
76
static int GetDataToEngine( char data[] );
80
77
static int AnswerFromEngineExpected( void );
81
78
static int UserInputIsAValidMove( void );
 
79
void InitInputThread(void);
 
80
void input_wakeup( void );
82
81
 
83
82
/*
84
83
 * Initializes data used in the frontend.
89
88
  int i;
90
89
  for ( i=0; i<BUF_SIZE; ++i )
91
90
    zerochar[i] = '\0';
 
91
  InitInput();
 
92
  InitInputThread();
92
93
}
93
94
 
94
95
/*
174
175
 
175
176
  /* Poll input from user in non-blocking mode */
176
177
  FD_ZERO(set);
177
 
  FD_SET(STDIN_FILENO,set);
 
178
  FD_SET(pipefd_i2f[0],set);
178
179
  time_val->tv_sec = 0;
179
180
  time_val->tv_usec = 0;
180
 
  userinputready = select( STDIN_FILENO+1, set, NULL, NULL, time_val );
 
181
  userinputready = select( pipefd_i2f[0]+1, set, NULL, NULL, time_val );
181
182
 
182
183
  if ( userinputready == -1 ) {
183
184
    printf( "Error reading user input.\n" );
184
185
  } else if ( userinputready > 0 ) {
185
186
    /* There are some data from the user. Store it in buffer */
186
187
    strncpy( userinputaux, zerochar, BUF_SIZE );
187
 
    nread = read( STDIN_FILENO, userinputaux, BUF_SIZE );
 
188
    nread = read( pipefd_i2f[0], userinputaux, BUF_SIZE );
188
189
    strcat( userinputbuf, userinputaux );
189
190
    userinputbuf[strlen( userinputbuf ) + nread] = '\0';
190
191
  }
236
237
}
237
238
 
238
239
/*
239
 
 * If the prompt must be displayed on the standard output, according to
240
 
 * the current state, it is displayed.
241
 
 */
242
 
void ShowPrompt( void )
243
 
{
244
 
  char prompt[MAXSTR] = "";
245
 
  if ( showprompt && !(flags & XBOARD) ) {
246
 
    sprintf(prompt,"%s (%d) : ",
247
 
            RealSide ? _("Black") : _("White"),
248
 
            (RealGameCnt+1)/2 + 1 );
249
 
    fprintf( stdout, "%s", prompt );
250
 
    fflush( stdout );
251
 
    showprompt = 0;
252
 
  }
253
 
}
254
 
 
255
 
/*
256
240
 * Extracts a command from the user input buffer.
257
241
 *
258
242
 * The command is removed from the buffer.
281
265
            SetAutoGo( false );
282
266
        }
283
267
      }
284
 
      showprompt = !AnswerFromEngineExpected();
285
268
      /* Check if command was entered in manual mode */
286
269
      if ( (flags & MANUAL) && UserInputIsAValidMove() ) {
287
270
        RealGameCnt = GameCnt;
288
271
        RealSide = board.side;
289
 
        showprompt = 1;
290
272
      }
291
273
      /* Check if the color must be changed, e.g. after an undo command. */
292
274
      if ( changeColor ) {
293
275
        RealGameCnt = GameCnt;
294
276
        RealSide = board.side;
295
277
      }
 
278
      if ( !AnswerFromEngineExpected() || (flags & MANUAL) ) {
 
279
        input_wakeup();
 
280
      }
296
281
    }
297
282
  }
298
283
}
339
324
                  fflush( stdout );
340
325
          }
341
326
          RealGameCnt = GameCnt;
342
 
          showprompt = 1;
343
327
          /* Check if the color must be changed, e.g. after a go command. */
344
328
          if ( changeColor ) {
345
329
            RealGameCnt = GameCnt;
346
330
            RealSide = board.side;
347
331
          }
 
332
          input_wakeup();
348
333
        }
349
334
      } else {
350
335
        dbg_printf( "USER <: %s\n",engineinput );
352
337
        if ( flags & XBOARD ) {
353
338
          fflush( stdout );
354
339
        }
 
340
        if ( AnswerFromEngineExpected() ) {
 
341
          input_wakeup();
 
342
        }
355
343
      }
356
344
    }
357
345
  }