Allow adding played move to book
[xboard.git] / backend.h
1 /*
2  * backend.h -- Interface exported by XBoard back end
3  *
4  * Copyright 1991 by Digital Equipment Corporation, Maynard,
5  * Massachusetts.
6  *
7  * Enhancements Copyright 1992-2001, 2002, 2003, 2004, 2005, 2006,
8  * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
9  *
10  * Enhancements Copyright 2005 Alessandro Scotti
11  *
12  * The following terms apply to Digital Equipment Corporation's copyright
13  * interest in XBoard:
14  * ------------------------------------------------------------------------
15  * All Rights Reserved
16  *
17  * Permission to use, copy, modify, and distribute this software and its
18  * documentation for any purpose and without fee is hereby granted,
19  * provided that the above copyright notice appear in all copies and that
20  * both that copyright notice and this permission notice appear in
21  * supporting documentation, and that the name of Digital not be
22  * used in advertising or publicity pertaining to distribution of the
23  * software without specific, written prior permission.
24  *
25  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
27  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
28  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
29  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
30  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31  * SOFTWARE.
32  * ------------------------------------------------------------------------
33  *
34  * The following terms apply to the enhanced version of XBoard
35  * distributed by the Free Software Foundation:
36  * ------------------------------------------------------------------------
37  *
38  * GNU XBoard is free software: you can redistribute it and/or modify
39  * it under the terms of the GNU General Public License as published by
40  * the Free Software Foundation, either version 3 of the License, or (at
41  * your option) any later version.
42  *
43  * GNU XBoard is distributed in the hope that it will be useful, but
44  * WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
46  * General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program. If not, see http://www.gnu.org/licenses/.  *
50  *
51  *------------------------------------------------------------------------
52  ** See the file ChangeLog for a revision history.  */
53
54 #ifndef XB_BACKEND
55 #define XB_BACKEND
56
57 #include "lists.h"
58
59 typedef int (*FileProc) P((FILE *f, int n, char *title));
60
61 extern char *wbOptions;
62 extern int gotPremove;
63 extern GameMode gameMode;
64 extern int matchMode;
65 extern int matchGame;
66 extern int pausing, cmailMsgLoaded, flipView, mute;
67 extern char white_holding[], black_holding[];
68 extern int currentMove, backwardMostMove, forwardMostMove;
69 extern int blackPlaysFirst;
70 extern FILE *debugFP;
71 extern char* programVersion;
72 extern Board boards[];
73 extern char marker[BOARD_RANKS][BOARD_FILES];
74 extern char lastMsg[MSG_SIZ];
75 extern Boolean bookUp;
76 extern Boolean addToBookFlag;
77 extern int tinyLayout, smallLayout;
78 extern Boolean mcMode;
79 extern int dragging;
80 extern char variantError[];
81 extern char lastTalker[];
82
83 void MarkMenuItem P((char *menuRef, int state));
84 char *CmailMsg P((void));
85 /* Tord: Added the useFEN960 parameter in PositionToFEN() below */
86 char *PositionToFEN P((int move, char* useFEN960, int moveCounts));
87 void AlphaRank P((char *s, int n)); /* [HGM] Shogi move preprocessor */
88 void EditPositionPasteFEN P((char *fen));
89 void TimeDelay P((long ms));
90 void SendMultiLineToICS P(( char *text ));
91 void AnalysisPeriodicEvent P((int force));
92 void SetWhiteToPlayEvent P((void));
93 void SetBlackToPlayEvent P((void));
94 void UploadGameEvent P((void));
95 void InitBackEnd1 P((void));
96 void InitBackEnd2 P((void));
97 int HasPromotionChoice P((int fromX, int fromY, int toX, int toY, char *choice, int sweep));
98 int InPalace P((int row, int column));
99 int PieceForSquare P((int x, int y));
100 int OKToStartUserMove P((int x, int y));
101 void Reset P((int redraw, int init));
102 void ResetGameEvent P((void));
103 Boolean HasPattern P(( const char * text, const char * pattern ));
104 Boolean SearchPattern P(( const char * text, const char * pattern ));
105 int LoadGame P((FILE *f, int n, char *title, int useList));
106 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
107 int CmailLoadGame P((FILE *f, int n, char *title, int useList));
108 int ReloadGame P((int offset));
109 int SaveGame P((FILE *f, int dummy, char *dummy2));
110 int SaveGameToFile P((char *filename, int append));
111 int LoadPosition P((FILE *f, int n, char *title));
112 int ReloadPosition P((int offset));
113 int SavePosition P((FILE *f, int dummy, char *dummy2));
114 int DrawSeekGraph P(());
115 int SeekGraphClick P((ClickType click, int x, int y, int moving));
116 void EditPositionEvent P((void));
117 void FlipViewEvent P((void));
118 void MachineWhiteEvent P((void));
119 void MachineBlackEvent P((void));
120 void TwoMachinesEvent P((void));
121 void EditGameEvent P((void));
122 void TrainingEvent P((void));
123 void IcsClientEvent P((void));
124 void ForwardEvent P((void));
125 void BackwardEvent P((void));
126 void ToEndEvent P((void));
127 void ToStartEvent P((void));
128 void ToNrEvent P((int to));
129 void RevertEvent P((Boolean annotate));
130 void RetractMoveEvent P((void));
131 void MoveNowEvent P((void));
132 void TruncateGameEvent P((void));
133 void PauseEvent P((void));
134 void CallFlagEvent P((void));
135 void ClockClick P((int which));
136 void AcceptEvent P((void));
137 void DeclineEvent P((void));
138 void RematchEvent P((void));
139 void DrawEvent P((void));
140 void AbortEvent P((void));
141 void AdjournEvent P((void));
142 void ResignEvent P((void));
143 void UserAdjudicationEvent P((int result));
144 void StopObservingEvent P((void));
145 void StopExaminingEvent P((void));
146 void PonderNextMoveEvent P((int newState));
147 void ShowThinkingEvent P(());
148 void PeriodicUpdatesEvent P((int newState));
149 void HintEvent P((void));
150 void BookEvent P((void));
151 void AboutGameEvent P((void));
152 void ExitEvent P((int status));
153 char *DefaultFileName P((char *));
154 ChessMove UserMoveTest P((int fromX, int fromY, int toX, int toY, int promoChar, Boolean captureOwn));
155 void UserMoveEvent P((int fromX, int fromY, int toX, int toY, int promoChar));
156 void DecrementClocks P((void));
157 char *TimeString P((long millisec));
158 void AutoPlayGameLoop P((void));
159 void AdjustClock P((Boolean which, int dir));
160 void DisplayBothClocks P((void));
161 void EditPositionMenuEvent P((ChessSquare selection, int x, int y));
162 void DropMenuEvent P((ChessSquare selection, int x, int y));
163 int ParseTimeControl P((char *tc, float ti, int mps));
164 void EscapeExpand(char *p, char *q);
165 void ProcessICSInitScript P((FILE * f));
166 void EditCommentEvent P((void));
167 void ReplaceComment P((int index, char *text));
168 int ReplaceTags P((char *tags, GameInfo *gi));/* returns nonzero on error */
169 void AppendComment P((int index, char *text, Boolean addBraces));
170 void LoadVariation P((int index, char *text));
171 void ReloadCmailMsgEvent P((int unregister));
172 void MailMoveEvent P((void));
173 void EditTagsEvent P((void));
174 void GetMoveListEvent P((void));
175 void ExitAnalyzeMode P((void));
176 int  AnalyzeModeEvent P((void));
177 void AnalyzeFileEvent P((void));
178 void MatchEvent P((int mode));
179 void RecentEngineEvent P((int nr));
180 void TypeInEvent P((char first));
181 void TypeInDoneEvent P((char *move));
182 void InitPosition P((int redraw));
183 void NewSettingEvent P((int option, int *feature, char *command, int value));
184 void SettingsMenuIfReady P((void));
185 void DoEcho P((void));
186 void DontEcho P((void));
187 void TidyProgramName P((char *prog, char *host, char *buf));
188 void SetGameInfo P((void));
189 void AskQuestionEvent P((char *title, char *question,
190                          char *replyPrefix, char *which));
191 Boolean ParseOneMove P((char *move, int moveNum,
192                         ChessMove *moveType, int *fromX, int *fromY,
193                         int *toX, int *toY, char *promoChar));
194 char *VariantName P((VariantClass v));
195 VariantClass StringToVariant P((char *e));
196 double u64ToDouble P((u64 value));
197 void OutputChatMessage P((int partner, char *mess));
198 void EditPositionDone P((Boolean fakeRights));
199 Boolean GetArgValue P((char *name));
200 Boolean LoadPV P((int x, int y));
201 Boolean LoadMultiPV P((int x, int y, char *buf, int index, int *start, int *end, int pane));
202 void UnLoadPV P(());
203 void MovePV P((int x, int y, int h));
204 int PromoScroll P((int x, int y));
205 void EditBookEvent P((void));
206 Boolean DisplayBook P((int moveNr));
207 void SaveToBook P((char *text));
208 void AddBookMove P((char *text));
209 void PlayBookMove P((char *text, int index));
210 void HoverEvent P((int hiX, int hiY, int x, int y));
211 int PackGame P((Board board));
212 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen, Boolean autoSize));
213 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar, Board board));
214 void PackMove P((int fromX, int fromY, int toX, int toY, ChessSquare promoPiece));
215 void ics_printf P((char *format, ...));
216 int GetEngineLine P((char *nick, int engine));
217 void AddGameToBook P((int always));
218 void FlushBook P((void));
219
220 char *StrStr P((char *string, char *match));
221 char *StrCaseStr P((char *string, char *match));
222 char *StrSave P((char *s));
223 char *StrSavePtr P((char *s, char **savePtr));
224 char *SavePart P((char *));
225 char* safeStrCpy P(( char *dst, const char *src, size_t count ));
226
227 #ifndef _amigados
228 int StrCaseCmp P((char *s1, char *s2));
229 int ToLower P((int c));
230 int ToUpper P((int c));
231 #else
232 #define StrCaseCmp Stricmp  /*  Use utility.library functions   */
233 #include <proto/utility.h>
234 #endif
235
236 extern GameInfo gameInfo;
237
238 /* ICS vars used with backend.c and zippy.c */
239 enum ICS_TYPE { ICS_GENERIC, ICS_ICC, ICS_FICS, ICS_CHESSNET /* not really supported */ };
240 enum ICS_TYPE ics_type;
241
242 /* pgntags.c prototypes
243  */
244 char *PGNTags P((GameInfo *));
245 void PrintPGNTags P((FILE *f, GameInfo *));
246 int ParsePGNTag P((char *, GameInfo *));
247 char *PGNResult P((ChessMove result));
248
249
250 /* gamelist.c prototypes
251  */
252 /* A game node in the double linked list of games.
253  */
254 typedef struct XB_ListGame {
255     ListNode node;
256     int number;
257     int position;
258     int moves;
259     unsigned long offset;   /*  Byte offset of game within file.     */
260     GameInfo gameInfo;      /*  Note that some entries may be NULL. */
261 } ListGame;
262
263 extern int doubleClick;
264 extern int storedGames;
265 extern int opponentKibitzes;
266 extern ChessSquare gatingPiece;
267 extern List gameList;
268 extern int lastLoadGameNumber;
269 void ClearGameInfo P((GameInfo *));
270 int GameListBuild P((FILE *));
271 void GameListInitGameInfo P((GameInfo *));
272 char *GameListLine P((int, GameInfo *));
273 char * GameListLineFull P(( int, GameInfo *));
274 void InitSearch P((void));
275 int GameContainsPosition P((FILE *f, ListGame *lg));
276 void GLT_TagsToList P(( char * tags ));
277 void GLT_ParseList P((void));
278 int NamesToList P((char *name, char **engines, char **mnemonics, char *group));
279 int CreateTourney P((char *name));
280 char *MakeName P((char *templ));
281 void SwapEngines P((int n));
282 void Substitute P((char *participants, int expunge));
283
284 extern char* StripHighlight P((char *));  /* returns static data */
285 extern char* StripHighlightAndTitle P((char *));  /* returns static data */
286 extern void ics_update_width P((int new_width));
287 extern Boolean set_cont_sequence P((char *new_seq));
288 extern int wrap P((char *dest, char *src, int count, int width, int *lp));
289 int Explode P((Board board, int fromX, int fromY, int toX, int toY));
290
291 typedef enum { CheckBox, ComboBox, TextBox, Button, Spin, ResetButton, SaveButton, ListBox, Graph, PopUp,
292                  FileName, PathName, Slider, Message, Fractional, Label, Icon,
293                  BoxBegin, BoxEnd, BarBegin, BarEnd, DropDown, Break, EndMark, Skip } Control;
294
295 typedef struct XB_OPT {   // [HGM] options: descriptor of UCI-style option
296     int value;          // current setting, starts as default
297     int min;            // Also used for flags
298     int max;
299     void *handle;       // for use by front end
300     void *target;       // for use by front end
301     char *textValue;    // points to beginning of text value in name field
302     char **choice;      // points to array of combo choices in cps->combo
303     Control type;
304     char *name;         // holds both option name and text value (in allocated memory)
305 } Option;
306
307 typedef struct XB_CPS {
308     char *which;
309     int maybeThinking;
310     ProcRef pr;
311     InputSourceRef isr;
312     char *twoMachinesColor; /* "white\n" or "black\n" */
313     char *program;
314     char *host;
315     char *dir;
316     struct XB_CPS *other;
317     char *initString;
318     char *computerString;
319     int sendTime; /* 0=don't, 1=do, 2=test */
320     int sendDrawOffers;
321     int useSigint;
322     int useSigterm;
323     int offeredDraw; /* countdown */
324     int reuse;
325     int useSetboard; /* 0=use "edit"; 1=use "setboard" */
326     int extendedEdit;/* 1=also set holdings with "edit" */
327     int useSAN;      /* 0=use coordinate notation; 1=use SAN */
328     int usePing;     /* 0=not OK to use ping; 1=OK */
329     int lastPing;
330     int lastPong;
331     int usePlayother;/* 0=not OK to use playother; 1=OK */
332     int useColors;   /* 0=avoid obsolete white/black commands; 1=use them */
333     int useUsermove; /* 0=just send move; 1=send "usermove move" */
334     int sendICS;     /* 0=don't use "ics" command; 1=do */
335     int sendName;    /* 0=don't use "name" command; 1=do */
336     int sdKludge;    /* 0=use "sd DEPTH" command; 1=use "depth\nDEPTH" */
337     int stKludge;    /* 0=use "st TIME" command; 1=use "level 1 TIME" */
338     int excludeMoves;/* 0=don't use "exclude" command; 1=do */
339     char *tidy;
340     int matchWins;
341     char *variants;
342     int analysisSupport;
343     int analyzing;
344     int protocolVersion;
345     int initDone;
346
347     /* Added by Tord: */
348     int useFEN960;   /* 0=use "KQkq" style FENs, 1=use "HAha" style FENs */
349     int useOOCastle; /* 0="O-O" notation for castling, 1="king capture rook" notation */
350     /* End of additions by Tord */
351
352     int scoreIsAbsolute; /* [AS] 0=don't know (standard), 1=score is always from white side */
353     int isUCI;           /* [AS] 0=no (Winboard), 1=UCI (requires Polyglot) */
354     int hasOwnBookUCI;   /* [AS] 0=use GUI or Polyglot book, 1=has own book */
355
356     /* [HGM] time odds */
357     float timeOdds; /* factor through which we divide time for this engine  */
358     int debug;      /* [HGM] ignore engine debug lines starting with '#'    */
359     int maxNrOfSessions; /* [HGM] secondary TC: max args in 'level' command */
360     int accumulateTC; /* [HGM] secondary TC: how to handle extra sessions   */
361     int drawDepth;    /* [HGM] egbb: search depth to play egbb draws        */
362     int nps;          /* [HGM] nps: factor for node count to replace time   */
363     int supportsNPS;
364     int alphaRank;    /* [HGM] shogi: engine uses shogi-type coordinates    */
365     int maxCores;     /* [HGM] SMP: engine understands cores command        */
366     int memSize;      /* [HGM] memsize: engine understands memory command   */
367     char *egtFormats; /* [HGM] EGT: supported tablebase formats             */
368     int bookSuspend;  /* [HGM] book: go was deferred because of book hit    */
369     int pause;        /* [HGM] pause: 1=supports it, 2=actually paused      */
370     int highlight;    /* [HGM] engine wants to get lift and put commands    */
371     int nrOptions;    /* [HGM] options: remembered option="..." features    */
372 #define MAX_OPTIONS 200
373     Option option[MAX_OPTIONS];
374     int comboCnt;
375     char *comboList[20*MAX_OPTIONS];
376     char *optionSettings;
377     void *programLogo; /* [HGM] logo: bitmap of the logo                    */
378     char *fenOverride; /* [HGM} FRC: force FEN casling & ep fields by hand  */
379     char userError;    /* [HGM] crash: flag to suppress fatal-error messages*/
380     char reload;       /* [HGM] options: flag to resend options with xreuse */
381 } ChessProgramState;
382
383 extern ChessProgramState first, second;
384
385 /* Search stats from chessprogram */
386 typedef struct {
387   char movelist[2*MSG_SIZ]; /* Last PV we were sent */
388   int depth;              /* Current search depth */
389   int nr_moves;           /* Total nr of root moves */
390   int moves_left;         /* Moves remaining to be searched */
391   char move_name[MOVE_LEN];  /* Current move being searched, if provided */
392   u64 nodes;    /* # of nodes searched */
393   int time;               /* Search time (centiseconds) */
394   int score;              /* Score (centipawns) */
395   int got_only_move;      /* If last msg was "(only move)" */
396   int got_fail;           /* 0 - nothing, 1 - got "--", 2 - got "++" */
397   int ok_to_send;         /* handshaking between send & recv */
398   int line_is_book;       /* 1 if movelist is book moves */
399   int seen_stat;          /* 1 if we've seen the stat01: line */
400 } ChessProgramStats;
401
402 extern ChessProgramStats_Move pvInfoList[MAX_MOVES];
403 extern Boolean shuffleOpenings;
404 extern ChessProgramStats programStats;
405 extern int opponentKibitzes; // used by wengineo.c
406 extern int errorExitStatus;
407 extern char *recentEngines;
408 extern char *comboLine;
409 extern Boolean partnerUp, twoBoards;
410 extern char engineVariant[];
411 char *EngineDefinedVariant P((ChessProgramState *cps, int n));
412 void SettingsPopUp P((ChessProgramState *cps)); // [HGM] really in front-end, but CPS not known in frontend.h
413 int WaitForEngine P((ChessProgramState *cps, DelayedEventCallback x));
414 void Load P((ChessProgramState *cps, int n));
415 int MultiPV P((ChessProgramState *cps));
416 void MoveHistorySet P(( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo ));
417 void MakeEngineOutputTitle P((void));
418 void LoadTheme P((void));
419 void CreateBookEvent P((void));
420 char *SupportedVariant P((char *list, VariantClass v, int w, int h, int s, int proto, char *engine));
421
422 /* A point in time */
423 typedef struct {
424     long sec;  /* Assuming this is >= 32 bits */
425     int ms;    /* Assuming this is >= 16 bits */
426 } TimeMark;
427
428 void GetTimeMark P((TimeMark *));
429 long SubtractTimeMarks P((TimeMark *, TimeMark *));
430
431 #endif /* XB_BACKEND */