Redo Eval Graph with generic popup
[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 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 _BACKEND
55 #define _BACKEND
56
57 /* unsigned int 64 for engine nodes work and display */
58 #ifdef WIN32
59        /* I don't know the name for this type of other compiler
60         * If it not work, just modify here
61         * This is for MS Visual Studio
62         */
63        #ifdef _MSC_VER
64                #define u64 unsigned __int64
65                #define s64 signed __int64
66                #define u64Display "%I64u"
67                #define s64Display "%I64d"
68                #define u64Const(c) (c ## UI64)
69                #define s64Const(c) (c ## I64)
70        #else
71                /* place holder
72                 * or dummy types for other compiler
73                 * [HGM] seems that -mno-cygwin comple needs %I64?
74                 */
75                #define u64 unsigned long long
76                #define s64 signed long long
77                #ifdef USE_I64
78                   #define u64Display "%I64u"
79                   #define s64Display "%I64d"
80                #else
81                   #define u64Display "%llu"
82                   #define s64Display "%lld"
83                #endif
84                #define u64Const(c) (c ## ULL)
85                #define s64Const(c) (c ## LL)
86        #endif
87 #else
88        /* GNU gcc */
89        #define u64 unsigned long long
90        #define s64 signed long long
91        #define u64Display "%llu"
92        #define s64Display "%lld"
93        #define u64Const(c) (c ## ull)
94        #define s64Const(c) (c ## ll)
95 #endif
96
97 #include "lists.h"
98 #include "frontend.h"
99
100 extern char *wbOptions;
101 extern int gotPremove;
102 extern GameMode gameMode;
103 extern int matchMode;
104 extern int matchGame;
105 extern int pausing, cmailMsgLoaded, flipView, mute;
106 extern char white_holding[], black_holding[];
107 extern int currentMove, backwardMostMove, forwardMostMove;
108 extern int blackPlaysFirst;
109 extern FILE *debugFP;
110 extern char* programVersion;
111 extern ProcRef firstProgramPR, secondProgramPR;
112 extern Board boards[];
113 extern char marker[BOARD_RANKS][BOARD_FILES];
114 extern char lastMsg[MSG_SIZ];
115 extern Boolean bookUp;
116 extern int tinyLayout, smallLayout;
117
118 char *CmailMsg P((void));
119 /* Tord: Added the useFEN960 parameter in PositionToFEN() below */
120 char *PositionToFEN P((int move, char* useFEN960));
121 void AlphaRank P((char *s, int n)); /* [HGM] Shogi move preprocessor */
122 void EditPositionPasteFEN P((char *fen));
123 void TimeDelay P((long ms));
124 void SendMultiLineToICS P(( char *text ));
125 void AnalysisPeriodicEvent P((int force));
126 void SetWhiteToPlayEvent P((void));
127 void SetBlackToPlayEvent P((void));
128 void UploadGameEvent P((void));
129 void InitBackEnd1 P((void));
130 void InitBackEnd2 P((void));
131 int HasPromotionChoice P((int fromX, int fromY, int toX, int toY, char *choice, int sweep));
132 int InPalace P((int row, int column));
133 int PieceForSquare P((int x, int y));
134 int OKToStartUserMove P((int x, int y));
135 void Reset P((int redraw, int init));
136 void ResetGameEvent P((void));
137 Boolean HasPattern P(( const char * text, const char * pattern ));
138 Boolean SearchPattern P(( const char * text, const char * pattern ));
139 int LoadGame P((FILE *f, int n, char *title, int useList));
140 int LoadGameFromFile P((char *filename, int n, char *title, int useList));
141 int CmailLoadGame P((FILE *f, int n, char *title, int useList));
142 int ReloadGame P((int offset));
143 int SaveGame P((FILE *f, int dummy, char *dummy2));
144 int SaveGameToFile P((char *filename, int append));
145 int LoadPosition P((FILE *f, int n, char *title));
146 int ReloadPosition P((int offset));
147 int SavePosition P((FILE *f, int dummy, char *dummy2));
148 int DrawSeekGraph P(());
149 int SeekGraphClick P((ClickType click, int x, int y, int moving));
150 void EditPositionEvent P((void));
151 void FlipViewEvent P((void));
152 void MachineWhiteEvent P((void));
153 void MachineBlackEvent P((void));
154 void TwoMachinesEvent P((void));
155 void EditGameEvent P((void));
156 void TrainingEvent P((void));
157 void IcsClientEvent P((void));
158 void ForwardEvent P((void));
159 void BackwardEvent P((void));
160 void ToEndEvent P((void));
161 void ToStartEvent P((void));
162 void ToNrEvent P((int to));
163 void RevertEvent P((Boolean annotate));
164 void RetractMoveEvent P((void));
165 void MoveNowEvent P((void));
166 void TruncateGameEvent P((void));
167 void PauseEvent P((void));
168 void CallFlagEvent P((void));
169 void ClockClick P((int which));
170 void AcceptEvent P((void));
171 void DeclineEvent P((void));
172 void RematchEvent P((void));
173 void DrawEvent P((void));
174 void AbortEvent P((void));
175 void AdjournEvent P((void));
176 void ResignEvent P((void));
177 void UserAdjudicationEvent P((int result));
178 void StopObservingEvent P((void));
179 void StopExaminingEvent P((void));
180 void PonderNextMoveEvent P((int newState));
181 void ShowThinkingEvent P(());
182 void PeriodicUpdatesEvent P((int newState));
183 void HintEvent P((void));
184 void BookEvent P((void));
185 void AboutGameEvent P((void));
186 void ExitEvent P((int status));
187 char *DefaultFileName P((char *));
188 ChessMove UserMoveTest P((int fromX, int fromY, int toX, int toY, int promoChar, Boolean captureOwn));
189 void UserMoveEvent P((int fromX, int fromY, int toX, int toY, int promoChar));
190 void DecrementClocks P((void));
191 char *TimeString P((long millisec));
192 void AutoPlayGameLoop P((void));
193 void AdjustClock P((Boolean which, int dir));
194 void DisplayBothClocks P((void));
195 void EditPositionMenuEvent P((ChessSquare selection, int x, int y));
196 void DropMenuEvent P((ChessSquare selection, int x, int y));
197 int ParseTimeControl P((char *tc, float ti, int mps));
198 void EscapeExpand(char *p, char *q);
199 void ProcessICSInitScript P((FILE * f));
200 void EditCommentEvent P((void));
201 void ReplaceComment P((int index, char *text));
202 int ReplaceTags P((char *tags, GameInfo *gi));/* returns nonzero on error */
203 void AppendComment P((int index, char *text, Boolean addBraces));
204 void LoadVariation P((int index, char *text));
205 void ReloadCmailMsgEvent P((int unregister));
206 void MailMoveEvent P((void));
207 void EditTagsEvent P((void));
208 void GetMoveListEvent P((void));
209 void ExitAnalyzeMode P((void));
210 void AnalyzeModeEvent P((void));
211 void AnalyzeFileEvent P((void));
212 void MatchEvent P((int mode));
213 void RecentEngineEvent P((int nr));
214 void TypeInEvent P((char first));
215 void TypeInDoneEvent P((char *move));
216 void InitPosition P((int redraw));
217 void NewSettingEvent P((int option, int *feature, char *command, int value));
218 void SettingsMenuIfReady P((void));
219 void DoEcho P((void));
220 void DontEcho P((void));
221 void TidyProgramName P((char *prog, char *host, char *buf));
222 void SetGameInfo P((void));
223 void AskQuestionEvent P((char *title, char *question,
224                          char *replyPrefix, char *which));
225 Boolean ParseOneMove P((char *move, int moveNum,
226                         ChessMove *moveType, int *fromX, int *fromY,
227                         int *toX, int *toY, char *promoChar));
228 char *VariantName P((VariantClass v));
229 VariantClass StringToVariant P((char *e));
230 double u64ToDouble P((u64 value));
231 void OutputChatMessage P((int partner, char *mess));
232 void EditPositionDone P((Boolean fakeRights));
233 Boolean GetArgValue P((char *name));
234 Boolean LoadPV P((int x, int y));
235 Boolean LoadMultiPV P((int x, int y, char *buf, int index, int *start, int *end));
236 void UnLoadPV P(());
237 void MovePV P((int x, int y, int h));
238 int PromoScroll P((int x, int y));
239 void EditBookEvent P((void));
240 Boolean DisplayBook P((int moveNr));
241 void SaveToBook P((char *text));
242 int PackGame P((Board board));
243 Boolean ParseFEN P((Board board, int *blackPlaysFirst, char *fen));
244 void ApplyMove P((int fromX, int fromY, int toX, int toY, int promoChar, Board board));
245 void PackMove P((int fromX, int fromY, int toX, int toY, ChessSquare promoPiece));
246
247 char *StrStr P((char *string, char *match));
248 char *StrCaseStr P((char *string, char *match));
249 char *StrSave P((char *s));
250 char *StrSavePtr P((char *s, char **savePtr));
251 char *SavePart P((char *));
252 char* safeStrCpy P(( char *dst, const char *src, size_t count ));
253
254 #ifndef _amigados
255 int StrCaseCmp P((char *s1, char *s2));
256 int ToLower P((int c));
257 int ToUpper P((int c));
258 #else
259 #define StrCaseCmp Stricmp  /*  Use utility.library functions   */
260 #include <proto/utility.h>
261 #endif
262
263 extern GameInfo gameInfo;
264
265 /* ICS vars used with backend.c and zippy.c */
266 #define ICS_GENERIC 0
267 #define ICS_ICC 1
268 #define ICS_FICS 2
269 #define ICS_CHESSNET 3 /* not really supported */
270 int ics_type;
271
272  
273
274 /* pgntags.c prototypes
275  */
276 char *PGNTags P((GameInfo *));
277 void PrintPGNTags P((FILE *f, GameInfo *));
278 int ParsePGNTag P((char *, GameInfo *));
279 char *PGNResult P((ChessMove result));
280
281
282 /* gamelist.c prototypes
283  */
284 /* A game node in the double linked list of games.
285  */
286 typedef struct _ListGame {
287     ListNode node;
288     int number;
289     int position;
290     int moves;
291     unsigned long offset;   /*  Byte offset of game within file.     */
292     GameInfo gameInfo;      /*  Note that some entries may be NULL. */
293 } ListGame;
294  
295 extern int storedGames;
296 extern int opponentKibitzes;
297 extern ChessSquare gatingPiece;
298 extern List gameList;
299 extern int lastLoadGameNumber;
300 void ClearGameInfo P((GameInfo *));
301 int GameListBuild P((FILE *));
302 void GameListInitGameInfo P((GameInfo *));
303 char *GameListLine P((int, GameInfo *));
304 char * GameListLineFull P(( int, GameInfo *));
305 void InitSearch P((void));
306 int GameContainsPosition P((FILE *f, ListGame *lg));
307 void GLT_TagsToList P(( char * tags ));
308 void GLT_ParseList P((void));
309 int NamesToList P((char *name, char **engines, char **mnemonics, char *group));
310 int CreateTourney P((char *name));
311 char *MakeName P((char *templ));
312 void SwapEngines P((int n));
313 void Substitute P((char *participants, int expunge));
314
315 extern char* StripHighlight P((char *));  /* returns static data */
316 extern char* StripHighlightAndTitle P((char *));  /* returns static data */
317 extern void ics_update_width P((int new_width));
318 extern Boolean set_cont_sequence P((char *new_seq));
319 extern int wrap P((char *dest, char *src, int count, int width, int *lp));
320 int Explode P((Board board, int fromX, int fromY, int toX, int toY));
321
322 typedef enum { CheckBox, ComboBox, TextBox, Button, Spin, ResetButton, SaveButton, ListBox, Graph, PopUp,
323                  FileName, PathName, Slider, Message, Fractional, Label, BoxBegin, BoxEnd, DropDown, Break, EndMark } Control;
324
325 typedef struct _OPT {   // [HGM] options: descriptor of UCI-style option
326     int value;          // current setting, starts as default
327     int min;            // Also used for flags
328     int max;
329     void *handle;       // for use by front end
330     void *target;       // for use by front end
331     char *textValue;    // points to beginning of text value in name field
332     char **choice;      // points to array of combo choices in cps->combo
333     Control type;
334     char *name;         // holds both option name and text value (in allocated memory)
335 } Option;
336
337 typedef struct _CPS {
338     char *which;
339     int maybeThinking;
340     ProcRef pr;
341     InputSourceRef isr;
342     char *twoMachinesColor; /* "white\n" or "black\n" */
343     char *program;
344     char *host;
345     char *dir;
346     struct _CPS *other;
347     char *initString;
348     char *computerString;
349     int sendTime; /* 0=don't, 1=do, 2=test */
350     int sendDrawOffers;
351     int useSigint;
352     int useSigterm;
353     int offeredDraw; /* countdown */
354     int reuse;
355     int useSetboard; /* 0=use "edit"; 1=use "setboard" */
356     int extendedEdit;/* 1=also set holdings with "edit" */
357     int useSAN;      /* 0=use coordinate notation; 1=use SAN */
358     int usePing;     /* 0=not OK to use ping; 1=OK */
359     int lastPing;
360     int lastPong;
361     int usePlayother;/* 0=not OK to use playother; 1=OK */
362     int useColors;   /* 0=avoid obsolete white/black commands; 1=use them */
363     int useUsermove; /* 0=just send move; 1=send "usermove move" */
364     int sendICS;     /* 0=don't use "ics" command; 1=do */
365     int sendName;    /* 0=don't use "name" command; 1=do */
366     int sdKludge;    /* 0=use "sd DEPTH" command; 1=use "depth\nDEPTH" */
367     int stKludge;    /* 0=use "st TIME" command; 1=use "level 1 TIME" */
368     int excludeMoves;/* 0=don't use "exclude" command; 1=do */
369     char tidy[MSG_SIZ];
370     int matchWins;
371     char variants[MSG_SIZ];
372     int analysisSupport;
373     int analyzing;
374     int protocolVersion;
375     int initDone;
376
377     /* Added by Tord: */
378     int useFEN960;   /* 0=use "KQkq" style FENs, 1=use "HAha" style FENs */
379     int useOOCastle; /* 0="O-O" notation for castling, 1="king capture rook" notation */
380     /* End of additions by Tord */
381
382     int scoreIsAbsolute; /* [AS] 0=don't know (standard), 1=score is always from white side */
383     int isUCI;           /* [AS] 0=no (Winboard), 1=UCI (requires Polyglot) */
384     int hasOwnBookUCI;   /* [AS] 0=use GUI or Polyglot book, 1=has own book */
385
386     /* [HGM] time odds */
387     float timeOdds; /* factor through which we divide time for this engine  */
388     int debug;      /* [HGM] ignore engine debug lines starting with '#'    */
389     int maxNrOfSessions; /* [HGM] secondary TC: max args in 'level' command */
390     int accumulateTC; /* [HGM] secondary TC: how to handle extra sessions   */
391     int nps;          /* [HGM] nps: factor for node count to replace time   */
392     int supportsNPS;
393     int alphaRank;    /* [HGM] shogi: engine uses shogi-type coordinates    */
394     int maxCores;     /* [HGM] SMP: engine understands cores command        */
395     int memSize;      /* [HGM] memsize: engine understands memory command   */
396     char egtFormats[MSG_SIZ];     /* [HGM] EGT: supported tablebase formats */
397     int bookSuspend;  /* [HGM] book: go was deferred because of book hit    */
398     int nrOptions;    /* [HGM] options: remembered option="..." features    */
399 #define MAX_OPTIONS 200
400     Option option[MAX_OPTIONS];
401     int comboCnt;
402     char *comboList[20*MAX_OPTIONS];
403     char *optionSettings;
404     void *programLogo; /* [HGM] logo: bitmap of the logo                    */
405     char *fenOverride; /* [HGM} FRC: force FEN casling & ep fields by hand  */
406     char userError;    /* [HGM] crash: flag to suppress fatal-error messages*/
407 } ChessProgramState;
408
409 extern ChessProgramState first, second;
410
411 /* Search stats from chessprogram */
412 typedef struct {
413   char movelist[2*MSG_SIZ]; /* Last PV we were sent */
414   int depth;              /* Current search depth */
415   int nr_moves;           /* Total nr of root moves */
416   int moves_left;         /* Moves remaining to be searched */
417   char move_name[MOVE_LEN];  /* Current move being searched, if provided */
418   u64 nodes;    /* # of nodes searched */
419   int time;               /* Search time (centiseconds) */
420   int score;              /* Score (centipawns) */
421   int got_only_move;      /* If last msg was "(only move)" */
422   int got_fail;           /* 0 - nothing, 1 - got "--", 2 - got "++" */
423   int ok_to_send;         /* handshaking between send & recv */
424   int line_is_book;       /* 1 if movelist is book moves */
425   int seen_stat;          /* 1 if we've seen the stat01: line */
426 } ChessProgramStats;
427
428 extern ChessProgramStats_Move pvInfoList[MAX_MOVES];
429 extern Boolean shuffleOpenings;
430 extern ChessProgramStats programStats;
431 extern int opponentKibitzes; // used by wengineo.c
432 extern int errorExitStatus;
433 extern char *recentEngines;
434 extern char *comboLine;
435 extern Boolean partnerUp, twoBoards;
436 void SettingsPopUp P((ChessProgramState *cps)); // [HGM] really in front-end, but CPS not known in frontend.h
437 int WaitForEngine P((ChessProgramState *cps, DelayedEventCallback x));
438 void Load P((ChessProgramState *cps, int n));
439 int MultiPV P((ChessProgramState *cps));
440 void MoveHistorySet P(( char movelist[][2*MOVE_LEN], int first, int last, int current, ChessProgramStats_Move * pvInfo ));
441 void EvalGraphSet P(( int first, int last, int current, ChessProgramStats_Move * pvInfo ));
442 void MakeEngineOutputTitle P((void));
443
444 /* A point in time */
445 typedef struct {
446     long sec;  /* Assuming this is >= 32 bits */
447     int ms;    /* Assuming this is >= 16 bits */
448 } TimeMark;
449
450 void GetTimeMark P((TimeMark *));
451 long SubtractTimeMarks P((TimeMark *, TimeMark *));
452
453 #endif /* _BACKEND */