4 * Author: Alessandro Scotti (Dec 2005)
\r
6 * ------------------------------------------------------------------------
\r
7 * This program is free software; you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation; either version 2 of the License, or
\r
10 * (at your option) any later version.
\r
12 * This program is distributed in the hope that it will be useful,
\r
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 * GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with this program; if not, write to the Free Software
\r
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
\r
20 * ------------------------------------------------------------------------
\r
24 #include <windows.h> /* required for all Windows applications */
\r
25 #include <richedit.h>
\r
29 #include <commdlg.h>
\r
33 #include "winboard.h"
\r
34 #include "frontend.h"
\r
35 #include "backend.h"
\r
39 VOID EngineOutputPopUp();
\r
40 VOID EngineOutputPopDown();
\r
41 BOOL EngineOutputIsUp();
\r
43 #define SHOW_PONDERING
\r
45 /* Imports from backend.c */
\r
46 char * SavePart(char *str);
\r
48 /* Imports from winboard.c */
\r
49 extern HWND engineOutputDialog;
\r
50 extern BOOLEAN engineOutputDialogUp;
\r
52 extern HINSTANCE hInst;
\r
53 extern HWND hwndMain;
\r
55 extern WindowPlacement wpEngineOutput;
\r
57 /* Module variables */
\r
60 #define LABEL_V_DISTANCE 1 /* Distance between label and memo */
\r
61 #define SPLITTER_SIZE 4 /* Distance between first memo and second label */
\r
63 #define ICON_SIZE 14
\r
65 #define STATE_UNKNOWN -1
\r
66 #define STATE_THINKING 0
\r
67 #define STATE_IDLE 1
\r
68 #define STATE_PONDERING 2
\r
69 #define STATE_ANALYZING 3
\r
71 static int windowMode = 1;
\r
73 static BOOL needInit = TRUE;
\r
75 static HICON hiColorBlack = NULL;
\r
76 static HICON hiColorWhite = NULL;
\r
77 static HICON hiColorUnknown = NULL;
\r
78 static HICON hiClear = NULL;
\r
79 static HICON hiPondering = NULL;
\r
80 static HICON hiThinking = NULL;
\r
81 static HICON hiAnalyzing = NULL;
\r
83 static int lastDepth[2] = { -1, -1 };
\r
84 static int lastForwardMostMove[2] = { -1, -1 };
\r
85 static int engineState[2] = { -1, -1 };
\r
97 unsigned long nodes;
\r
104 } EngineOutputData;
\r
106 static HICON LoadIconEx( int id )
\r
108 return LoadImage( hInst, MAKEINTRESOURCE(id), IMAGE_ICON, ICON_SIZE, ICON_SIZE, 0 );
\r
111 static VOID InitializeEngineOutput()
\r
114 hiColorBlack = LoadIconEx( IDI_BLACK_14 );
\r
115 hiColorWhite = LoadIconEx( IDI_WHITE_14 );
\r
116 hiColorUnknown = LoadIconEx( IDI_UNKNOWN_14 );
\r
117 hiClear = LoadIconEx( IDI_TRANS_14 );
\r
118 hiPondering = LoadIconEx( IDI_PONDER_14 );
\r
119 hiThinking = LoadIconEx( IDI_CLOCK_14 );
\r
120 hiAnalyzing = LoadIconEx( IDI_ANALYZE2_14 );
\r
125 static VOID SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
\r
127 HWND hControl = GetDlgItem( hDlg, id );
\r
129 SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
\r
132 #define HIDDEN_X 20000
\r
133 #define HIDDEN_Y 20000
\r
135 static VOID HideControl( HWND hDlg, int id )
\r
137 HWND hControl = GetDlgItem( hDlg, id );
\r
140 GetWindowRect( hControl, &rc );
\r
143 Avoid hiding an already hidden control, because that causes many
\r
144 unnecessary WM_ERASEBKGND messages!
\r
146 if( rc.left != HIDDEN_X || rc.top != HIDDEN_Y ) {
\r
147 SetControlPos( hDlg, id, 20000, 20000, 100, 100 );
\r
151 static int GetControlWidth( HWND hDlg, int id )
\r
155 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
157 return rc.right - rc.left;
\r
160 static int GetControlHeight( HWND hDlg, int id )
\r
164 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
\r
166 return rc.bottom - rc.top;
\r
169 static int GetHeaderHeight()
\r
171 int result = GetControlHeight( engineOutputDialog, IDC_EngineLabel1 );
\r
173 if( result < ICON_SIZE ) result = ICON_SIZE;
\r
178 #define ENGINE_COLOR_WHITE 'w'
\r
179 #define ENGINE_COLOR_BLACK 'b'
\r
180 #define ENGINE_COLOR_UNKNOWN ' '
\r
182 char GetEngineColor( int which )
\r
184 char result = ENGINE_COLOR_UNKNOWN;
\r
186 if( which == 0 || which == 1 ) {
\r
187 ChessProgramState * cps;
\r
189 switch (gameMode) {
\r
190 case MachinePlaysBlack:
\r
191 case IcsPlayingBlack:
\r
192 result = ENGINE_COLOR_BLACK;
\r
194 case MachinePlaysWhite:
\r
195 case IcsPlayingWhite:
\r
196 result = ENGINE_COLOR_WHITE;
\r
200 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
\r
202 case TwoMachinesPlay:
\r
203 cps = (which == 0) ? &first : &second;
\r
204 result = cps->twoMachinesColor[0];
\r
205 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
\r
213 char GetActiveEngineColor()
\r
215 char result = ENGINE_COLOR_UNKNOWN;
\r
217 if( gameMode == TwoMachinesPlay ) {
\r
218 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
\r
224 static int IsEnginePondering( int which )
\r
226 int result = FALSE;
\r
228 switch (gameMode) {
\r
229 case MachinePlaysBlack:
\r
230 case IcsPlayingBlack:
\r
231 if( WhiteOnMove(forwardMostMove) ) result = TRUE;
\r
233 case MachinePlaysWhite:
\r
234 case IcsPlayingWhite:
\r
235 if( ! WhiteOnMove(forwardMostMove) ) result = TRUE;
\r
237 case TwoMachinesPlay:
\r
238 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
\r
239 if( GetEngineColor( which ) != GetActiveEngineColor() ) result = TRUE;
\r
247 static VOID PositionControlSet( HWND hDlg, int x, int y, int clientWidth, int memoHeight, int idColor, int idEngineLabel, int idNPS, int idMemo, int idStateIcon, int idStateData )
\r
249 int label_x = x + ICON_SIZE + H_MARGIN;
\r
250 int label_h = GetControlHeight( hDlg, IDC_EngineLabel1 );
\r
251 int label_y = y + ICON_SIZE - label_h;
\r
252 int nps_w = GetControlWidth( hDlg, IDC_Engine1_NPS );
\r
253 int nps_x = clientWidth - H_MARGIN - nps_w;
\r
254 int state_data_w = GetControlWidth( hDlg, IDC_StateData1 );
\r
255 int state_data_x = nps_x - H_MARGIN - state_data_w;
\r
256 int state_icon_x = state_data_x - ICON_SIZE - 2;
\r
257 int max_w = clientWidth - 2*H_MARGIN;
\r
258 int memo_y = y + ICON_SIZE + LABEL_V_DISTANCE;
\r
260 SetControlPos( hDlg, idColor, x, y, ICON_SIZE, ICON_SIZE );
\r
261 SetControlPos( hDlg, idEngineLabel, label_x, label_y, state_icon_x - label_x, label_h );
\r
262 SetControlPos( hDlg, idStateIcon, state_icon_x, y, ICON_SIZE, ICON_SIZE );
\r
263 SetControlPos( hDlg, idStateData, state_data_x, label_y, state_data_w, label_h );
\r
264 SetControlPos( hDlg, idNPS, nps_x, label_y, nps_w, label_h );
\r
265 SetControlPos( hDlg, idMemo, x, memo_y, max_w, memoHeight );
\r
268 static VOID ResizeWindowControls( HWND hDlg, int mode )
\r
271 int headerHeight = GetHeaderHeight();
\r
272 int labelHeight = GetControlHeight( hDlg, IDC_EngineLabel1 );
\r
273 int labelOffset = H_MARGIN + ICON_SIZE + H_MARGIN;
\r
274 int labelDeltaY = ICON_SIZE - labelHeight;
\r
277 int maxControlWidth;
\r
280 /* Initialize variables */
\r
281 GetClientRect( hDlg, &rc );
\r
283 clientWidth = rc.right - rc.left;
\r
284 clientHeight = rc.bottom - rc.top;
\r
286 maxControlWidth = clientWidth - 2*H_MARGIN;
\r
288 npsWidth = GetControlWidth( hDlg, IDC_Engine1_NPS );
\r
290 /* Resize controls */
\r
293 PositionControlSet( hDlg, H_MARGIN, V_MARGIN,
\r
295 clientHeight - V_MARGIN - LABEL_V_DISTANCE - headerHeight- V_MARGIN,
\r
296 IDC_Color1, IDC_EngineLabel1, IDC_Engine1_NPS, IDC_EngineMemo1, IDC_StateIcon1, IDC_StateData1 );
\r
298 /* Hide controls for the second engine */
\r
299 HideControl( hDlg, IDC_Color2 );
\r
300 HideControl( hDlg, IDC_EngineLabel2 );
\r
301 HideControl( hDlg, IDC_StateIcon2 );
\r
302 HideControl( hDlg, IDC_StateData2 );
\r
303 HideControl( hDlg, IDC_Engine2_NPS );
\r
304 HideControl( hDlg, IDC_EngineMemo2 );
\r
305 SendDlgItemMessage( hDlg, IDC_EngineMemo2, WM_SETTEXT, 0, (LPARAM) "" );
\r
306 /* TODO: we should also hide/disable them!!! what about tab stops?!?! */
\r
310 int memo_h = (clientHeight - headerHeight*2 - V_MARGIN*2 - LABEL_V_DISTANCE*2 - SPLITTER_SIZE) / 2;
\r
311 int header1_y = V_MARGIN;
\r
312 int header2_y = V_MARGIN + headerHeight + LABEL_V_DISTANCE + memo_h + SPLITTER_SIZE;
\r
314 PositionControlSet( hDlg, H_MARGIN, header1_y, clientWidth, memo_h,
\r
315 IDC_Color1, IDC_EngineLabel1, IDC_Engine1_NPS, IDC_EngineMemo1, IDC_StateIcon1, IDC_StateData1 );
\r
317 PositionControlSet( hDlg, H_MARGIN, header2_y, clientWidth, memo_h,
\r
318 IDC_Color2, IDC_EngineLabel2, IDC_Engine2_NPS, IDC_EngineMemo2, IDC_StateIcon2, IDC_StateData2 );
\r
321 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
\r
322 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
\r
325 static VOID SetDisplayMode( int mode )
\r
327 if( windowMode != mode ) {
\r
330 ResizeWindowControls( engineOutputDialog, mode );
\r
334 static VOID VerifyDisplayMode()
\r
338 /* Get proper mode for current game */
\r
339 switch( gameMode ) {
\r
342 case MachinePlaysWhite:
\r
343 case MachinePlaysBlack:
\r
344 case IcsPlayingWhite:
\r
345 case IcsPlayingBlack:
\r
348 case TwoMachinesPlay:
\r
352 /* Do not change */
\r
356 SetDisplayMode( mode );
\r
359 static VOID InsertIntoMemo( HWND hMemo, char * text )
\r
361 SendMessage( hMemo, EM_SETSEL, 0, 0 );
\r
363 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
\r
366 static VOID SetIcon( HWND hControl, HICON hIcon )
\r
368 if( hIcon != NULL ) {
\r
369 SendMessage( hControl, STM_SETICON, (WPARAM) hIcon, 0 );
\r
373 static VOID SetEngineColorIcon( HWND hControl, int which )
\r
375 char color = GetEngineColor(which);
\r
376 HICON hicon = NULL;
\r
378 if( color == ENGINE_COLOR_BLACK )
\r
379 hicon = hiColorBlack;
\r
380 else if( color == ENGINE_COLOR_WHITE )
\r
381 hicon = hiColorWhite;
\r
383 hicon = hiColorUnknown;
\r
385 SetIcon( hControl, hicon );
\r
388 static SetEngineState( int which, int state, char * state_data )
\r
390 int x_which = 1 - which;
\r
391 HWND hStateIcon = GetDlgItem( engineOutputDialog, which == 0 ? IDC_StateIcon1 : IDC_StateIcon2 );
\r
392 HWND hStateData = GetDlgItem( engineOutputDialog, which == 0 ? IDC_StateData1 : IDC_StateData2 );
\r
394 if( engineState[ which ] != state ) {
\r
395 engineState[ which ] = state;
\r
398 case STATE_THINKING:
\r
399 SetIcon( hStateIcon, hiThinking );
\r
400 if( engineState[ x_which ] == STATE_THINKING ) {
\r
401 SetEngineState( x_which, STATE_IDLE, "" );
\r
404 case STATE_PONDERING:
\r
405 SetIcon( hStateIcon, hiPondering );
\r
407 case STATE_ANALYZING:
\r
408 SetIcon( hStateIcon, hiAnalyzing );
\r
411 SetIcon( hStateIcon, hiClear );
\r
416 if( state_data != 0 ) {
\r
417 SetWindowText( hStateData, state_data );
\r
421 #define MAX_NAME_LENGTH 32
\r
423 static VOID UpdateControls( EngineOutputData * ed )
\r
425 BOOL isPondering = FALSE;
\r
427 char s_label[MAX_NAME_LENGTH + 32];
\r
429 char * name = ed->name;
\r
432 if( name == 0 || *name == '\0' ) {
\r
436 strncpy( s_label, name, MAX_NAME_LENGTH );
\r
437 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
\r
439 #ifdef SHOW_PONDERING
\r
440 if( IsEnginePondering( ed->which ) ) {
\r
445 if( ed->hint != 0 && *ed->hint != '\0' ) {
\r
446 strncpy( buf, ed->hint, sizeof(buf) );
\r
447 buf[sizeof(buf)-1] = '\0';
\r
449 else if( ed->pv != 0 && *ed->pv != '\0' ) {
\r
450 char * sep = strchr( ed->pv, ' ' );
\r
451 int buflen = sizeof(buf);
\r
453 if( sep != NULL ) {
\r
454 buflen = sep - ed->pv + 1;
\r
455 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
\r
458 strncpy( buf, ed->pv, buflen );
\r
459 buf[ buflen-1 ] = '\0';
\r
462 SetEngineState( ed->which, STATE_PONDERING, buf );
\r
464 else if( gameMode == TwoMachinesPlay ) {
\r
465 SetEngineState( ed->which, STATE_THINKING, "" );
\r
467 else if( gameMode == AnalyzeMode || gameMode == AnalyzeFile ) {
\r
469 int time_secs = ed->time / 100;
\r
470 int time_mins = time_secs / 60;
\r
474 if( ed->an_move_index != 0 && ed->an_move_count != 0 && *ed->hint != '\0' ) {
\r
477 strncpy( mov, ed->hint, sizeof(mov) );
\r
478 mov[ sizeof(mov)-1 ] = '\0';
\r
480 sprintf( buf, "%d/%d: %s [%02d:%02d:%02d]", ed->an_move_index, ed->an_move_count, mov, time_mins / 60, time_mins % 60, time_secs % 60 );
\r
483 SetEngineState( ed->which, STATE_ANALYZING, buf );
\r
486 SetEngineState( ed->which, STATE_IDLE, "" );
\r
490 SetWindowText( ed->hLabel, s_label );
\r
494 if( ed->time > 0 && ed->nodes > 0 ) {
\r
495 unsigned long nps_100 = ed->nodes / ed->time;
\r
497 if( nps_100 < 100000 ) {
\r
498 sprintf( s_label, "NPS: %lu", nps_100 * 100 );
\r
501 sprintf( s_label, "NPS: %.1fk", nps_100 / 10.0 );
\r
505 SetWindowText( ed->hLabelNPS, s_label );
\r
508 if( ed->pv != 0 && *ed->pv != '\0' ) {
\r
514 int time_secs = ed->time / 100;
\r
515 int time_cent = ed->time % 100;
\r
518 if( ed->nodes < 1000000 ) {
\r
519 sprintf( s_nodes, "%lu", ed->nodes );
\r
522 sprintf( s_nodes, "%.1fM", ed->nodes / 1000000.0 );
\r
526 if( ed->score > 0 ) {
\r
527 sprintf( s_score, "+%.2f", ed->score / 100.0 );
\r
530 sprintf( s_score, "%.2f", ed->score / 100.0 );
\r
534 sprintf( s_time, "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
\r
536 /* Put all together... */
\r
537 sprintf( buf, "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
\r
540 buflen = strlen(buf);
\r
542 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
\r
544 buf[ sizeof(buf) - 3 ] = '\0';
\r
546 strcat( buf + buflen, "\r\n" );
\r
549 InsertIntoMemo( ed->hMemo, buf );
\r
553 SetEngineColorIcon( ed->hColorIcon, ed->which );
\r
556 LRESULT CALLBACK EngineOutputProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
\r
558 static SnapData sd;
\r
561 case WM_INITDIALOG:
\r
562 if( engineOutputDialog == NULL ) {
\r
563 engineOutputDialog = hDlg;
\r
565 RestoreWindowPlacement( hDlg, &wpEngineOutput ); /* Restore window placement */
\r
567 ResizeWindowControls( hDlg, windowMode );
\r
569 SetEngineState( 0, STATE_IDLE, "" );
\r
570 SetEngineState( 1, STATE_IDLE, "" );
\r
576 switch (LOWORD(wParam)) {
\r
578 EndDialog(hDlg, TRUE);
\r
582 EndDialog(hDlg, FALSE);
\r
591 case WM_GETMINMAXINFO:
\r
593 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
\r
595 mmi->ptMinTrackSize.x = 100;
\r
596 mmi->ptMinTrackSize.y = 160;
\r
601 EngineOutputPopDown();
\r
605 ResizeWindowControls( hDlg, windowMode );
\r
608 case WM_ENTERSIZEMOVE:
\r
609 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
\r
612 return OnSizing( &sd, hDlg, wParam, lParam );
\r
615 return OnMoving( &sd, hDlg, wParam, lParam );
\r
617 case WM_EXITSIZEMOVE:
\r
618 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
\r
624 VOID EngineOutputPopUp()
\r
629 InitializeEngineOutput();
\r
632 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_CHECKED);
\r
634 if( engineOutputDialog ) {
\r
635 SendMessage( engineOutputDialog, WM_INITDIALOG, 0, 0 );
\r
637 if( ! engineOutputDialogUp ) {
\r
638 ShowWindow(engineOutputDialog, SW_SHOW);
\r
642 lpProc = MakeProcInstance( (FARPROC) EngineOutputProc, hInst );
\r
644 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
\r
645 CreateDialog( hInst, MAKEINTRESOURCE(DLG_EngineOutput), hwndMain, (DLGPROC)lpProc );
\r
647 FreeProcInstance(lpProc);
\r
650 engineOutputDialogUp = TRUE;
\r
651 ShowThinkingEvent(); // [HGM] thinking: might need to prompt engine for thinking output
654 VOID EngineOutputPopDown()
\r
656 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_UNCHECKED);
\r
658 if( engineOutputDialog ) {
\r
659 ShowWindow(engineOutputDialog, SW_HIDE);
\r
662 engineOutputDialogUp = FALSE;
\r
663 ShowThinkingEvent(); // [HGM] thinking: might need to shut off thinking output
666 BOOL EngineOutputIsUp()
\r
668 return engineOutputDialogUp;
\r
671 VOID EngineOutputUpdate( FrontEndProgramStats * stats )
\r
673 EngineOutputData ed;
\r
674 BOOL clearMemo = FALSE;
\r
679 SetEngineState( 0, STATE_IDLE, "" );
\r
680 SetEngineState( 1, STATE_IDLE, "" );
\r
684 which = stats->which;
\r
685 depth = stats->depth;
\r
687 if( which < 0 || which > 1 || depth < 0 || stats->time < 0 || stats->pv == 0 ) {
\r
691 if( engineOutputDialog == NULL ) {
\r
695 VerifyDisplayMode();
\r
699 ed.nodes = stats->nodes;
\r
700 ed.score = stats->score;
\r
701 ed.time = stats->time;
\r
703 ed.hint = stats->hint;
\r
704 ed.an_move_index = stats->an_move_index;
\r
705 ed.an_move_count = stats->an_move_count;
\r
707 /* Get target control */
\r
709 ed.hColorIcon = GetDlgItem( engineOutputDialog, IDC_Color1 );
\r
710 ed.hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel1 );
\r
711 ed.hStateIcon = GetDlgItem( engineOutputDialog, IDC_StateIcon1 );
\r
712 ed.hStateData = GetDlgItem( engineOutputDialog, IDC_StateData1 );
\r
713 ed.hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine1_NPS );
\r
714 ed.hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo1 );
\r
715 ed.name = first.tidy;
\r
718 ed.hColorIcon = GetDlgItem( engineOutputDialog, IDC_Color2 );
\r
719 ed.hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel2 );
\r
720 ed.hStateIcon = GetDlgItem( engineOutputDialog, IDC_StateIcon2 );
\r
721 ed.hStateData = GetDlgItem( engineOutputDialog, IDC_StateData2 );
\r
722 ed.hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine2_NPS );
\r
723 ed.hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo2 );
\r
724 ed.name = second.tidy;
\r
727 /* Clear memo if needed */
\r
728 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
\r
732 if( lastForwardMostMove[which] != forwardMostMove ) {
\r
737 SendMessage( ed.hMemo, WM_SETTEXT, 0, (LPARAM) "" );
\r
741 lastDepth[which] = depth;
\r
742 lastForwardMostMove[which] = forwardMostMove;
\r
744 if( ed.pv != 0 && ed.pv[0] == ' ' ) {
\r
745 if( strncmp( ed.pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
\r
750 UpdateControls( &ed );
\r