Fix exclusion header fix
[xboard.git] / engineoutput.c
1 /*
2  * engineoutput.c - split-off backe-end from Engine output (PV) by HGM
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  *
6  * Copyright 2005 Alessandro Scotti
7  *
8  * Enhancements Copyright 1995, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
9  *
10  * ------------------------------------------------------------------------
11  *
12  * GNU XBoard is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or (at
15  * your option) any later version.
16  *
17  * GNU XBoard is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see http://www.gnu.org/licenses/.  *
24  *
25  *------------------------------------------------------------------------
26  ** See the file ChangeLog for a revision history.  */
27
28 #define SHOW_PONDERING
29
30 #include "config.h"
31
32 #include <stdio.h>
33 #include <ctype.h>
34
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 # include <string.h>
38 #else /* not STDC_HEADERS */
39 # if HAVE_STRING_H
40 #  include <string.h>
41 # else /* not HAVE_STRING_H */
42 #  include <strings.h>
43 # endif /* not HAVE_STRING_H */
44 #endif /* not STDC_HEADERS */
45
46 #include "common.h"
47 #include "frontend.h"
48 #include "backend.h"
49 #include "moves.h"
50 #include "engineoutput.h"
51 #include "gettext.h"
52
53 #ifdef ENABLE_NLS
54 # define  _(s) gettext (s)
55 # define N_(s) gettext_noop (s)
56 #else
57 # ifdef WIN32
58 #  define  _(s) T_(s)
59 #  undef  ngettext
60 #  define  ngettext(s,p,n) T_(p)
61 # else
62 #  define  _(s) (s)
63 # endif
64 # define N_(s)  s
65 #endif
66
67 typedef struct {
68     char * name;
69     int which;
70     int depth;
71     u64 nodes;
72     int score;
73     int time;
74     char * pv;
75     char * hint;
76     int an_move_index;
77     int an_move_count;
78     int moveKey;
79 } EngineOutputData;
80
81 // called by other front-end
82 void EngineOutputUpdate( FrontEndProgramStats * stats );
83 void OutputKibitz(int window, char *text);
84
85 // module back-end routines
86 static void VerifyDisplayMode();
87 static void UpdateControls( EngineOutputData * ed );
88
89 static int  lastDepth[2] = { -1, -1 };
90 static int  lastForwardMostMove[2] = { -1, -1 };
91 static int  engineState[2] = { -1, -1 };
92 static char lastLine[2][MSG_SIZ];
93 static char header[2][MSG_SIZ];
94 static char columnHeader[MSG_SIZ] = "dep\tscore\tnodes\ttime\t(not shown:  tbhits\tknps\tseldep)\n";
95 static int  columnMask = 0xF0;
96
97 #define MAX_VAR 400
98 static int scores[MAX_VAR], textEnd[MAX_VAR], keys[MAX_VAR], curDepth[2], nrVariations[2];
99 static char fail[MAX_VAR];
100
101 extern int initialRulePlies;
102
103 void
104 MakeEngineOutputTitle ()
105 {
106         static char buf[MSG_SIZ];
107         static char oldTitle[MSG_SIZ];
108         char title[MSG_SIZ];
109         int count, rule = 2*appData.ruleMoves;
110
111         snprintf(title, MSG_SIZ, _("Engine Output") );
112
113         if(!EngineOutputIsUp()) return;
114         // figure out value of 50-move counter
115         count = currentMove;
116         while( (signed char)boards[count][EP_STATUS] <= EP_NONE && count > backwardMostMove ) count--;
117         if( count == backwardMostMove ) count -= initialRulePlies;
118         count = currentMove - count;
119         if(!rule) rule = 100;
120         if(count >= rule - 40 && (!appData.icsActive || gameMode == IcsObserving || appData.zippyPlay)) {
121                 snprintf(buf, MSG_SIZ, ngettext("%s (%d reversible ply)", "%s (%d reversible plies)", count), title, count);
122                 safeStrCpy(title, buf, MSG_SIZ);
123         }
124         if(!strcmp(oldTitle, title)) return;
125         safeStrCpy(oldTitle, title, MSG_SIZ);
126         SetEngineOutputTitle(title);
127 }
128
129 // back end, due to front-end wrapper for SetWindowText, and new SetIcon arguments
130 void
131 SetEngineState (int which, enum ENGINE_STATE state, char * state_data)
132 {
133     int x_which = 1 - which;
134
135     if( engineState[ which ] != state ) {
136         engineState[ which ] = state;
137
138         switch( state ) {
139         case STATE_THINKING:
140             SetIcon( which, nStateIcon, nThinking );
141             if( engineState[ x_which ] == STATE_THINKING ) {
142                 SetEngineState( x_which, STATE_IDLE, "" );
143             }
144             break;
145         case STATE_PONDERING:
146             SetIcon( which, nStateIcon, nPondering );
147             break;
148         case STATE_ANALYZING:
149             SetIcon( which, nStateIcon, nAnalyzing );
150             break;
151         default:
152             SetIcon( which, nStateIcon, nClear );
153             break;
154         }
155     }
156
157     if( state_data != 0 ) {
158         DoSetWindowText( which, nStateData, state_data );
159     }
160 }
161
162 // back end, now the front-end wrapper ClearMemo is used, and ed no longer contains handles.
163 void
164 SetProgramStats (FrontEndProgramStats * stats) // now directly called by back-end
165 {
166     EngineOutputData ed;
167     int clearMemo = FALSE;
168     int which, depth, multi;
169     ChessMove moveType;
170     int ff, ft, rf, rt;
171     char pc;
172
173     if( stats == 0 ) {
174         SetEngineState( 0, STATE_IDLE, "" );
175         SetEngineState( 1, STATE_IDLE, "" );
176         return;
177     }
178
179     if(gameMode == IcsObserving && !appData.icsEngineAnalyze)
180         return; // [HGM] kibitz: shut up engine if we are observing an ICS game
181
182     which = stats->which;
183     depth = stats->depth;
184
185     if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
186         return;
187     }
188
189     if( !EngineOutputDialogExists() ) {
190         return;
191     }
192
193     VerifyDisplayMode();
194
195     ed.which = which;
196     ed.depth = depth;
197     ed.nodes = stats->nodes;
198     ed.score = stats->score;
199     ed.time = stats->time;
200     ed.pv = stats->pv;
201     ed.hint = stats->hint;
202     ed.an_move_index = stats->an_move_index;
203     ed.an_move_count = stats->an_move_count;
204
205     /* Get target control. [HGM] this is moved to front end, which get them from a table */
206     if( which == 0 ) {
207         ed.name = first.tidy;
208     }
209     else {
210         ed.name = second.tidy;
211     }
212
213     if( ed.pv != 0 && ed.pv[0] == ' ' ) {
214         if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
215             ed.pv = "";
216         }
217     }
218
219     /* Clear memo if needed */
220     if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1 && ed.pv[0]) ) { // no reason to clear if we won't add line
221         clearMemo = TRUE;
222     }
223
224     if( lastForwardMostMove[which] != forwardMostMove ) {
225         clearMemo = TRUE;
226     }
227
228     if( clearMemo ) {
229         if(!appData.headers) columnHeader[0] = NULLCHAR;
230         DoClearMemo(which); nrVariations[which] = 0;
231         header[which][0] = NULLCHAR;
232         if(gameMode == AnalyzeMode) {
233           ChessProgramState *cps = (which ? &second : &first);
234           char *exclu = cps->excludeMoves ? exclusionHeader : "";
235           if((multi = MultiPV(cps)) >= 0) {
236             snprintf(header[which], MSG_SIZ, "\t%s viewpoint\t\tfewer / Multi-PV setting = %d / more\n",
237                                        appData.whitePOV || appData.scoreWhite ? "white" : "mover", cps->option[multi].value);
238           }
239           if(!which) snprintf(header[which]+strlen(header[which]), MSG_SIZ-strlen(header[which]), "%s%s", exclu, columnHeader);
240           InsertIntoMemo( which, header[which], 0);
241         } else {
242           snprintf(header[which], MSG_SIZ, "%s", columnHeader);
243           if(appData.ponderNextMove && lastLine[which][0]) {
244             InsertIntoMemo( which, lastLine[which], 0 );
245             InsertIntoMemo( which, "\n", 0 );
246           }
247           InsertIntoMemo( which, header[which], 0);
248         }
249     }
250
251     if(ed.pv && ed.pv[0] && ParseOneMove(ed.pv, currentMove, &moveType, &ff, &rf, &ft, &rt, &pc))
252         ed.moveKey = (ff<<24 | rf << 16 | ft << 8 | rt) ^ pc*87161;
253     else ed.moveKey = ed.nodes; // kludge to get unique key unlikely to match any move
254
255     /* Update */
256     lastDepth[which] = depth == 1 && ed.nodes == 0 ? 0 : depth; // [HGM] info-line kudge
257     lastForwardMostMove[which] = forwardMostMove;
258
259     UpdateControls( &ed );
260 }
261
262 #define ENGINE_COLOR_WHITE      'w'
263 #define ENGINE_COLOR_BLACK      'b'
264 #define ENGINE_COLOR_UNKNOWN    ' '
265
266 // pure back end
267 static char
268 GetEngineColor (int which)
269 {
270     char result = ENGINE_COLOR_UNKNOWN;
271
272     if( which == 0 || which == 1 ) {
273         ChessProgramState * cps;
274
275         switch (gameMode) {
276         case MachinePlaysBlack:
277         case IcsPlayingBlack:
278             result = ENGINE_COLOR_BLACK;
279             break;
280         case MachinePlaysWhite:
281         case IcsPlayingWhite:
282             result = ENGINE_COLOR_WHITE;
283             break;
284         case AnalyzeMode:
285         case AnalyzeFile:
286             result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
287             break;
288         case TwoMachinesPlay:
289             cps = (which == 0) ? &first : &second;
290             result = cps->twoMachinesColor[0];
291             result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
292             break;
293         default: ; // does not happen, but suppresses pedantic warnings
294         }
295     }
296
297     return result;
298 }
299
300 // pure back end
301 static char
302 GetActiveEngineColor ()
303 {
304     char result = ENGINE_COLOR_UNKNOWN;
305
306     if( gameMode == TwoMachinesPlay ) {
307         result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
308     }
309
310     return result;
311 }
312
313 // pure back end
314 static int
315 IsEnginePondering (int which)
316 {
317     int result = FALSE;
318
319     switch (gameMode) {
320     case MachinePlaysBlack:
321     case IcsPlayingBlack:
322         if( WhiteOnMove(forwardMostMove) ) result = TRUE;
323         break;
324     case MachinePlaysWhite:
325     case IcsPlayingWhite:
326         if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
327         break;
328     case TwoMachinesPlay:
329         if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
330             if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
331         }
332         break;
333     default: ; // does not happen, but suppresses pedantic warnings
334     }
335
336     return result;
337 }
338
339 // back end
340 static void
341 SetDisplayMode (int mode)
342 {
343     if( windowMode != mode ) {
344         windowMode = mode;
345
346         ResizeWindowControls( mode );
347     }
348 }
349
350 // pure back end
351 static void
352 VerifyDisplayMode ()
353 {
354     int mode;
355
356     /* Get proper mode for current game */
357     switch( gameMode ) {
358     case IcsObserving:    // [HGM] ICS analyze
359         if(!appData.icsEngineAnalyze) return;
360     case AnalyzeFile:
361     case MachinePlaysWhite:
362     case MachinePlaysBlack:
363         mode = 0;
364         break;
365     case AnalyzeMode:
366         mode = second.analyzing;
367         break;
368     case IcsPlayingWhite:
369     case IcsPlayingBlack:
370         mode = appData.zippyPlay && opponentKibitzes; // [HGM] kibitz
371         break;
372     case TwoMachinesPlay:
373         mode = 1;
374         break;
375     default:
376         /* Do not change */
377         return;
378     }
379
380     SetDisplayMode( mode );
381 }
382
383 // back end. Determine what icon to set in the color-icon field, and print it
384 void
385 SetEngineColorIcon (int which)
386 {
387     char color = GetEngineColor(which);
388     int nicon = 0;
389
390     if( color == ENGINE_COLOR_BLACK )
391         nicon = nColorBlack;
392     else if( color == ENGINE_COLOR_WHITE )
393         nicon = nColorWhite;
394     else
395         nicon = nColorUnknown;
396
397     SetIcon( which, nColorIcon, nicon );
398 }
399
400 #define MAX_NAME_LENGTH 32
401
402 // [HGM] multivar: sort Thinking Output within one depth on score
403
404 static int
405 InsertionPoint (int len, EngineOutputData *ed)
406 {
407         int i, offs = 0, newScore = ed->score, n = ed->which;
408         char failType;
409
410         if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
411                 newScore = 1e6; // info lines inserted on top
412         if(ed->depth != curDepth[n]) { // depth has changed
413                 curDepth[n] = ed->depth;
414                 nrVariations[n] = 0; // throw away everything we had
415         }
416         i = strlen(ed->pv); if(i > 0) i--;
417         failType = ed->pv[i];
418         if(failType != '?' && failType != '!') failType = ' ';
419         // loop through all lines. Note even / odd used for different panes
420         for(i=nrVariations[n]-2; i>=0; i-=2) {
421                 // put new item behind those we haven't looked at
422                 offs = textEnd[i+n];
423                 textEnd[i+n+2] = offs + len;
424                 scores[i+n+2] = newScore;
425                 keys[i+n+2] = ed->moveKey;
426                 fail[i+n+2] = failType;
427                 if(ed->moveKey != keys[i+n] && // same move always tops previous one (as a higher score must be a fail low)
428                    newScore < scores[i+n] && fail[i+n] == ' ') break;
429                 // if it had higher score as previous, move previous in stead
430                 scores[i+n+2] = ed->moveKey == keys[i+n] ? newScore : scores[i+n]; // correct scores of fail-low/high searches
431                 textEnd[i+n+2] = textEnd[i+n] + len;
432                 keys[i+n+2] = keys[i+n];
433                 fail[i+n+2] = fail[i+n];
434         }
435         if(i<0) {
436                 offs = 0;
437                 textEnd[n] = offs + len;
438                 scores[n] = newScore;
439                 keys[n] = ed->moveKey;
440                 fail[n] = failType;
441         }
442         nrVariations[n] += 2;
443       return offs + strlen(header[ed->which]);
444 }
445
446 #define MATE_SCORE 100000
447 static char spaces[] = "            "; // [HGM] align: spaces for padding
448
449 static void
450 Format(char *buf, int val)
451 { // [HGM] tbhits: print a positive integer with trailing whitespace to give it fixed width
452         if( val < 1000000 ) {
453             int h = val, i=0;
454             while(h > 0) h /= 10, i++;
455             snprintf( buf, 24, "%d%s\t", val, spaces + 2*i);
456         }
457         else {
458             char unit = 'M';
459             if(val >= 1e9) val /= 1e3, unit = 'G';
460             snprintf( buf, 24, "%.*f%c%s\t", 1 + (val < 1e7), val/1e6, unit, spaces + 10 + 2*(val >= 1e8));
461         }
462 }
463
464 // pure back end, now SetWindowText is called via wrapper DoSetWindowText
465 static void
466 UpdateControls (EngineOutputData *ed)
467 {
468 //    int isPondering = FALSE;
469
470     char s_label[MAX_NAME_LENGTH + 32];
471     int h;
472     char * name = ed->name;
473     char *q, *pvStart = ed->pv;
474
475     /* Label */
476     if( name == 0 || *name == '\0' ) {
477         name = "?";
478     }
479
480     strncpy( s_label, name, MAX_NAME_LENGTH );
481     s_label[ MAX_NAME_LENGTH-1 ] = '\0';
482
483     if(pvStart) { // [HGM] tbhits: plit up old PV into extra infos and real PV
484         while(strchr(pvStart, '\t')) { // locate last tab before non-int (real PV starts after that)
485             for(q=pvStart; isdigit(*q) || *q == ' '; q++);
486             if(*q != '\t') break;
487             pvStart = q + 1;
488         }
489     }
490
491 #ifdef SHOW_PONDERING
492     if( IsEnginePondering( ed->which ) ) {
493         char buf[12];
494
495         buf[0] = '\0';
496
497         if( ed->hint != 0 && *ed->hint != '\0' ) {
498             strncpy( buf, ed->hint, sizeof(buf) );
499             buf[sizeof(buf)-1] = '\0';
500         }
501         else if( pvStart != 0 && *pvStart != '\0' ) {
502             char * sep;
503             int buflen = sizeof(buf);
504
505             sep = strchr( pvStart, ' ' );
506             if( sep != NULL ) {
507                 buflen = sep - pvStart + 1;
508                 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
509             }
510
511             strncpy( buf, pvStart, buflen );
512             buf[ buflen-1 ] = '\0';
513         }
514
515         SetEngineState( ed->which, STATE_PONDERING, buf );
516     }
517     else if( gameMode == TwoMachinesPlay ) {
518         SetEngineState( ed->which, STATE_THINKING, "" );
519     }
520     else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile
521           || (gameMode == IcsObserving && appData.icsEngineAnalyze)) { // [HGM] ICS-analyze
522         char buf[64];
523         int time_secs = ed->time / 100;
524         int time_mins = time_secs / 60;
525
526         buf[0] = '\0';
527
528         if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
529             char mov[16];
530
531             strncpy( mov, ed->hint, sizeof(mov) );
532             mov[ sizeof(mov)-1 ] = '\0';
533
534             snprintf( buf, sizeof(buf)/sizeof(buf[0]), "[%d] %d/%d: %s [%02d:%02d:%02d]", ed->depth, ed->an_move_index,
535                         ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
536         }
537
538         SetEngineState( ed->which, STATE_ANALYZING, buf );
539     }
540     else {
541         SetEngineState( ed->which, STATE_IDLE, "" );
542     }
543 #endif
544
545     DoSetWindowText( ed->which, nLabel, s_label );
546
547     s_label[0] = '\0';
548
549     if( ed->time > 0 && ed->nodes > 0 ) {
550         unsigned long nps_100 = ed->nodes / ed->time;
551
552         if( nps_100 < 100000 ) {
553           snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %lu", _("NPS"), nps_100 * 100 );
554         }
555         else {
556           snprintf( s_label, sizeof(s_label)/sizeof(s_label[0]), "%s: %.1fk", _("NPS"), nps_100 / 10.0 );
557         }
558     }
559
560     DoSetWindowText( ed->which, nLabelNPS, s_label );
561
562     /* Memo */
563     if( pvStart != 0 && *pvStart != '\0' ) {
564         char s_nodes[24];
565         char s_score[16];
566         char s_time[24];
567         char s_hits[24];
568         char s_seld[24];
569         char s_knps[24];
570         char buf[256], fail;
571         int buflen, hits, i, params[5], extra;
572         int time_secs = ed->time / 100;
573         int time_cent = ed->time % 100;
574
575         /* Nodes */
576         if( ed->nodes < 1000000 ) {
577             int h = ed->nodes, i=0;
578             while(h > 0) h /= 10, i++; // [HGM] align: count digits; pad with 2 spaces for every missing digit
579             snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), u64Display "%s\t", ed->nodes, spaces + 2*i);
580         }
581         else {
582             double x = u64ToDouble(ed->nodes);
583             char unit = 'M';
584             if(x >= 1e9) x /= 1e3, unit = 'G';
585             snprintf( s_nodes, sizeof(s_nodes)/sizeof(s_nodes[0]), "%.*f%c%s\t", 1 + (x < 1e7), x / 1e6,
586                       unit, spaces + 10 + 2*(ed->nodes >= 1e8));
587         }
588
589         /* TB Hits etc. */
590         for(i=hits=0; i<5; i++) params[i] = 0;
591 //fprintf(stderr, "%s\n%s\n", ed->pv, pvStart);
592         if(pvStart != ed->pv) { // check if numbers before PV
593             strncpy(buf, ed->pv, 256); buf[pvStart - ed->pv] = NULLCHAR;
594             extra = sscanf(buf, "%d %d %d %d %d", params, params+1, params+2, params+3, params+4);
595 //fprintf(stderr, "extra=%d len=%d\n", extra, pvStart - ed->pv);
596             if(extra) hits = params[extra-1], params[extra-1] = 0; // last one is tbhits
597         }
598         Format(s_seld, params[0]); Format(s_knps, params[1]); Format(s_hits, hits); 
599
600         if(*ed->pv) fail = ed->pv[strlen(ed->pv)-1]; else fail = ' ';
601         if(fail != '?' && fail != '!') fail = ' ';
602
603         /* Score */
604         h = ((gameMode == AnalyzeMode && appData.whitePOV || appData.scoreWhite) && !WhiteOnMove(currentMove) ? -1 : 1) * ed->score;
605         if( h == 0 ) {
606           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "  0.00%c\t", fail );
607         } else
608         if( h >= MATE_SCORE) snprintf(s_score, 16, "  %s#%d%c\t", ( h > MATE_SCORE+9 ? "" : "  "),  h - MATE_SCORE, fail ); else
609         if(-h >= MATE_SCORE) snprintf(s_score, 16, " %s#-%d%c\t", (-h > MATE_SCORE+9 ? "" : "  "), -h - MATE_SCORE, fail ); else
610         if( h > 0 ) {
611           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), "+%.2f%c\t", h / 100.0, fail );
612         }
613         else {
614           snprintf( s_score, sizeof(s_score)/sizeof(s_score[0]), " %.2f%c\t", h / 100.0, fail );
615         }
616
617         /* Time */
618         if(time_secs >= 3600)
619             snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d:%02d\t", time_secs / 3600, (time_secs / 60) % 60, time_secs % 60 );
620         else
621         snprintf( s_time, sizeof(s_time)/sizeof(s_time[0]), "%d:%02d.%02d\t", time_secs / 60, time_secs % 60, time_cent );
622
623         if(columnMask & 2) s_score[0] = NULLCHAR; // [HGM] hide: erase columns the user has hidden
624         if(columnMask & 4) s_nodes[0] = NULLCHAR;
625         if(columnMask & 8) s_time[0]  = NULLCHAR;
626         if(columnMask & 16) s_hits[0]  = NULLCHAR;
627         if(columnMask & 32) s_knps[0]  = NULLCHAR;
628         if(columnMask & 64) s_seld[0]  = NULLCHAR;
629
630         /* Put all together... */
631         if(ed->nodes == 0 && ed->score == 0 && ed->time == 0)
632           snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t", ed->depth );
633         else
634           snprintf( buf, sizeof(buf)/sizeof(buf[0]), "%3d\t%s%s%s%s%s%s", ed->depth, s_score, s_nodes, s_time, s_hits, s_knps, s_seld );
635
636         /* Add PV */
637         buflen = strlen(buf);
638
639         strncpy( buf + buflen, pvStart, sizeof(buf) - buflen );
640
641         buf[ sizeof(buf) - 3 ] = '\0';
642
643         strcat( buf + buflen, "\r\n" );
644
645         /* Update memo */
646         InsertIntoMemo( ed->which, buf, InsertionPoint(strlen(buf), ed) );
647         strncpy(lastLine[ed->which], buf, MSG_SIZ);
648     }
649
650     /* Colors */
651     SetEngineColorIcon( ed->which );
652 }
653
654 static char *titles[] = { "score\t", "nodes\t", "time\t", "tbhits\t", "knps\t", "seldep\t" };
655
656 void
657 Collapse(int n)
658 {   // handle click on column headers, to hide / show them
659     int i, j, nr=0, m=~columnMask, Ncol=7;
660     for(i=0; columnHeader[i] && i<n; i++) nr += (columnHeader[i] == '\t');
661     if(!nr) return; // depth always shown, so clicks on it ignored
662     for(i=j=0; i<Ncol; i++) if(m & 1<<i) j++; // count hidden columns
663     if(nr < j) { // shown column clicked: hide it
664         for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
665         columnMask |= 1<<i;
666     } else { // hidden column clicked: show it
667         m = ~m; nr -= j;
668         for(i=j=0; i<Ncol; i++) if(m & 1<<i && j++ == nr) break;
669         columnMask &= ~(1<<i);
670     }
671     // create new header line
672     strcpy(columnHeader, "dep\t");
673     m = ~columnMask;
674     for(i=j=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]), j++;
675     if(j != Ncol) { // list hidden columns, so user ca click them
676         m = ~m; strcat(columnHeader, "(not shown:  ");
677         for(i=1; i<Ncol; i++) if(m & 1<<i) strcat(columnHeader, titles[i-1]);
678         strcat(columnHeader, ")");
679     }
680     strcat(columnHeader, "\n");
681 }
682
683 // [HGM] kibitz: write kibitz line; split window for it if necessary
684 void
685 OutputKibitz (int window, char *text)
686 {
687         static int currentLineEnd[2];
688         int where = 0;
689         if(!EngineOutputIsUp()) return;
690         if(!opponentKibitzes) { // on first kibitz of game, clear memos
691             DoClearMemo(1); currentLineEnd[1] = 0;
692             if(gameMode == IcsObserving) { DoClearMemo(0); currentLineEnd[0] = 0; }
693         }
694         opponentKibitzes = TRUE; // this causes split window DisplayMode in ICS modes.
695         VerifyDisplayMode();
696         strncpy(text+strlen(text)-1, "\r\n",sizeof(text+strlen(text)-1)); // to not lose line breaks on copying
697         if(gameMode == IcsObserving) {
698             DoSetWindowText(0, nLabel, gameInfo.white);
699             SetIcon( 0, nColorIcon,  nColorWhite);
700             SetIcon( 0, nStateIcon,  nClear);
701         }
702         DoSetWindowText(1, nLabel, gameMode == IcsPlayingBlack ? gameInfo.white : gameInfo.black); // opponent name
703         SetIcon( 1, nColorIcon,  gameMode == IcsPlayingBlack ? nColorWhite : nColorBlack);
704         SetIcon( 1, nStateIcon,  nClear);
705         if(strstr(text, "\\  ") == text) where = currentLineEnd[window-1]; // continuation line
706 //if(appData.debugMode) fprintf(debugFP, "insert '%s' at %d (end = %d,%d)\n", text, where, currentLineEnd[0], currentLineEnd[1]);
707         InsertIntoMemo(window-1, text, where); // [HGM] multivar: always at top
708         currentLineEnd[window-1] = where + strlen(text);
709 }