4 * Author: Alessandro Scotti (Dec 2005)
6 * ------------------------------------------------------------------------
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * ------------------------------------------------------------------------
24 #include <windows.h> /* required for all Windows applications */
39 VOID EngineOutputPopUp();
40 VOID EngineOutputPopDown();
41 BOOL EngineOutputIsUp();
43 #define SHOW_PONDERING
45 /* Imports from backend.c */
46 char * SavePart(char *str);
48 /* Imports from winboard.c */
49 extern HWND engineOutputDialog;
50 extern BOOLEAN engineOutputDialogUp;
52 extern HINSTANCE hInst;
55 extern WindowPlacement wpEngineOutput;
57 /* Module variables */
60 #define LABEL_V_DISTANCE 1 /* Distance between label and memo */
61 #define SPLITTER_SIZE 4 /* Distance between first memo and second label */
65 #define STATE_UNKNOWN -1
66 #define STATE_THINKING 0
68 #define STATE_PONDERING 2
70 static int windowMode = 1;
72 static BOOL needInit = TRUE;
74 static HICON hiColorBlack = NULL;
75 static HICON hiColorWhite = NULL;
76 static HICON hiColorUnknown = NULL;
77 static HICON hiClear = NULL;
78 static HICON hiPondering = NULL;
79 static HICON hiThinking = NULL;
81 static int lastDepth[2] = { -1, -1 };
82 static int lastForwardMostMove[2] = { -1, -1 };
83 static int engineState[2] = { -1, -1 };
102 static HICON LoadIconEx( int id )
104 return LoadImage( hInst, MAKEINTRESOURCE(id), IMAGE_ICON, ICON_SIZE, ICON_SIZE, 0 );
107 static VOID InitializeEngineOutput()
110 hiColorBlack = LoadIconEx( IDI_BLACK_14 );
111 hiColorWhite = LoadIconEx( IDI_WHITE_14 );
112 hiColorUnknown = LoadIconEx( IDI_UNKNOWN_14 );
113 hiClear = LoadIconEx( IDI_TRANS_14 );
114 hiPondering = LoadIconEx( IDI_PONDER_14 );
115 hiThinking = LoadIconEx( IDI_CLOCK_14 );
120 static VOID SetControlPos( HWND hDlg, int id, int x, int y, int width, int height )
122 HWND hControl = GetDlgItem( hDlg, id );
124 SetWindowPos( hControl, HWND_TOP, x, y, width, height, SWP_NOZORDER );
127 #define HIDDEN_X 20000
128 #define HIDDEN_Y 20000
130 static VOID HideControl( HWND hDlg, int id )
132 HWND hControl = GetDlgItem( hDlg, id );
135 GetWindowRect( hControl, &rc );
138 Avoid hiding an already hidden control, because that causes many
139 unnecessary WM_ERASEBKGND messages!
141 if( rc.left != HIDDEN_X || rc.top != HIDDEN_Y ) {
142 SetControlPos( hDlg, id, 20000, 20000, 100, 100 );
146 static int GetControlWidth( HWND hDlg, int id )
150 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
152 return rc.right - rc.left;
155 static int GetControlHeight( HWND hDlg, int id )
159 GetWindowRect( GetDlgItem( hDlg, id ), &rc );
161 return rc.bottom - rc.top;
164 static int GetHeaderHeight()
166 int result = GetControlHeight( engineOutputDialog, IDC_EngineLabel1 );
168 if( result < ICON_SIZE ) result = ICON_SIZE;
173 #define ENGINE_COLOR_WHITE 'w'
174 #define ENGINE_COLOR_BLACK 'b'
175 #define ENGINE_COLOR_UNKNOWN ' '
177 char GetEngineColor( int which )
179 char result = ENGINE_COLOR_UNKNOWN;
181 if( which == 0 || which == 1 ) {
182 ChessProgramState * cps;
185 case MachinePlaysBlack:
186 case IcsPlayingBlack:
187 result = ENGINE_COLOR_BLACK;
189 case MachinePlaysWhite:
190 case IcsPlayingWhite:
191 result = ENGINE_COLOR_WHITE;
195 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
197 case TwoMachinesPlay:
198 cps = (which == 0) ? &first : &second;
199 result = cps->twoMachinesColor[0];
200 result = result == 'w' ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
208 char GetActiveEngineColor()
210 char result = ENGINE_COLOR_UNKNOWN;
212 if( gameMode == TwoMachinesPlay ) {
213 result = WhiteOnMove(forwardMostMove) ? ENGINE_COLOR_WHITE : ENGINE_COLOR_BLACK;
219 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 )
221 int label_x = x + ICON_SIZE + H_MARGIN;
222 int label_h = GetControlHeight( hDlg, IDC_EngineLabel1 );
223 int label_y = y + ICON_SIZE - label_h;
224 int nps_w = GetControlWidth( hDlg, IDC_Engine1_NPS );
225 int nps_x = clientWidth - H_MARGIN - nps_w;
226 int state_data_w = GetControlWidth( hDlg, IDC_StateData1 );
227 int state_data_x = nps_x - H_MARGIN - state_data_w;
228 int state_icon_x = state_data_x - ICON_SIZE - 2;
229 int max_w = clientWidth - 2*H_MARGIN;
230 int memo_y = y + ICON_SIZE + LABEL_V_DISTANCE;
232 SetControlPos( hDlg, idColor, x, y, ICON_SIZE, ICON_SIZE );
233 SetControlPos( hDlg, idEngineLabel, label_x, label_y, max_w / 2, label_h );
234 SetControlPos( hDlg, idStateIcon, state_icon_x, y, ICON_SIZE, ICON_SIZE );
235 SetControlPos( hDlg, idStateData, state_data_x, label_y, state_data_w, label_h );
236 SetControlPos( hDlg, idNPS, nps_x, label_y, nps_w, label_h );
237 SetControlPos( hDlg, idMemo, x, memo_y, max_w, memoHeight );
240 static VOID ResizeWindowControls( HWND hDlg, int mode )
243 int headerHeight = GetHeaderHeight();
244 int labelHeight = GetControlHeight( hDlg, IDC_EngineLabel1 );
245 int labelOffset = H_MARGIN + ICON_SIZE + H_MARGIN;
246 int labelDeltaY = ICON_SIZE - labelHeight;
252 /* Initialize variables */
253 GetClientRect( hDlg, &rc );
255 clientWidth = rc.right - rc.left;
256 clientHeight = rc.bottom - rc.top;
258 maxControlWidth = clientWidth - 2*H_MARGIN;
260 npsWidth = GetControlWidth( hDlg, IDC_Engine1_NPS );
262 /* Resize controls */
265 PositionControlSet( hDlg, H_MARGIN, V_MARGIN,
267 clientHeight - V_MARGIN - LABEL_V_DISTANCE - headerHeight- V_MARGIN,
268 IDC_Color1, IDC_EngineLabel1, IDC_Engine1_NPS, IDC_EngineMemo1, IDC_StateIcon1, IDC_StateData1 );
270 /* Hide controls for the second engine */
271 HideControl( hDlg, IDC_Color2 );
272 HideControl( hDlg, IDC_EngineLabel2 );
273 HideControl( hDlg, IDC_StateIcon2 );
274 HideControl( hDlg, IDC_StateData2 );
275 HideControl( hDlg, IDC_Engine2_NPS );
276 HideControl( hDlg, IDC_EngineMemo2 );
277 SendDlgItemMessage( hDlg, IDC_EngineMemo2, WM_SETTEXT, 0, (LPARAM) "" );
278 /* TODO: we should also hide/disable them!!! what about tab stops?!?! */
282 int memo_h = (clientHeight - headerHeight*2 - V_MARGIN*2 - LABEL_V_DISTANCE*2 - SPLITTER_SIZE) / 2;
283 int header1_y = V_MARGIN;
284 int header2_y = V_MARGIN + headerHeight + LABEL_V_DISTANCE + memo_h + SPLITTER_SIZE;
286 PositionControlSet( hDlg, H_MARGIN, header1_y, clientWidth, memo_h,
287 IDC_Color1, IDC_EngineLabel1, IDC_Engine1_NPS, IDC_EngineMemo1, IDC_StateIcon1, IDC_StateData1 );
289 PositionControlSet( hDlg, H_MARGIN, header2_y, clientWidth, memo_h,
290 IDC_Color2, IDC_EngineLabel2, IDC_Engine2_NPS, IDC_EngineMemo2, IDC_StateIcon2, IDC_StateData2 );
293 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo1), NULL, FALSE );
294 InvalidateRect( GetDlgItem(hDlg,IDC_EngineMemo2), NULL, FALSE );
297 static VOID SetDisplayMode( int mode )
299 if( windowMode != mode ) {
302 ResizeWindowControls( engineOutputDialog, mode );
306 static VOID VerifyDisplayMode()
310 /* Get proper mode for current game */
314 case MachinePlaysWhite:
315 case MachinePlaysBlack:
316 case IcsPlayingWhite:
317 case IcsPlayingBlack:
320 case TwoMachinesPlay:
328 SetDisplayMode( mode );
331 static VOID InsertIntoMemo( HWND hMemo, char * text )
333 SendMessage( hMemo, EM_SETSEL, 0, 0 );
335 SendMessage( hMemo, EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) text );
338 static VOID SetIcon( HWND hControl, HICON hIcon )
340 if( hIcon != NULL ) {
341 SendMessage( hControl, STM_SETICON, (WPARAM) hIcon, 0 );
345 static VOID SetEngineColorIcon( HWND hControl, int which )
347 char color = GetEngineColor(which);
350 if( color == ENGINE_COLOR_BLACK )
351 hicon = hiColorBlack;
352 else if( color == ENGINE_COLOR_WHITE )
353 hicon = hiColorWhite;
355 hicon = hiColorUnknown;
357 SetIcon( hControl, hicon );
360 static SetEngineState( int which, int state, char * state_data )
362 int x_which = 1 - which;
363 HWND hStateIcon = GetDlgItem( engineOutputDialog, which == 0 ? IDC_StateIcon1 : IDC_StateIcon2 );
364 HWND hStateData = GetDlgItem( engineOutputDialog, which == 0 ? IDC_StateData1 : IDC_StateData2 );
366 if( engineState[ which ] != state ) {
367 engineState[ which ] = state;
371 SetIcon( hStateIcon, hiThinking );
372 if( engineState[ x_which ] == STATE_THINKING ) {
373 SetEngineState( x_which, STATE_IDLE, "" );
376 case STATE_PONDERING:
377 SetIcon( hStateIcon, hiPondering );
380 SetIcon( hStateIcon, hiClear );
385 if( state_data != 0 ) {
386 SetWindowText( hStateData, state_data );
390 #define MAX_NAME_LENGTH 32
392 static VOID UpdateControls( EngineOutputData * ed )
394 char s_label[MAX_NAME_LENGTH + 32];
396 char * name = ed->name;
399 if( name == 0 || *name == '\0' ) {
403 strncpy( s_label, name, MAX_NAME_LENGTH );
404 s_label[ MAX_NAME_LENGTH-1 ] = '\0';
406 #ifdef SHOW_PONDERING
407 if( GetActiveEngineColor() != ENGINE_COLOR_UNKNOWN ) {
408 if( GetEngineColor(ed->which) != GetActiveEngineColor() ) {
413 if( ed->hint != 0 && *ed->hint != '\0' ) {
414 strncpy( buf, ed->hint, sizeof(buf) );
415 buf[sizeof(buf)-1] = '\0';
417 else if( ed->pv != 0 && *ed->pv != '\0' ) {
418 char * sep = strchr( ed->pv, ' ' );
419 int buflen = sizeof(buf);
422 buflen = sep - ed->pv + 1;
423 if( buflen > sizeof(buf) ) buflen = sizeof(buf);
426 strncpy( buf, ed->pv, buflen );
427 buf[ buflen-1 ] = '\0';
430 SetEngineState( ed->which, STATE_PONDERING, buf );
433 SetEngineState( ed->which, STATE_THINKING, "" );
437 SetEngineState( ed->which, STATE_IDLE, "" );
441 SetWindowText( ed->hLabel, s_label );
445 if( ed->time > 0 && ed->nodes > 0 ) {
446 unsigned long nps_100 = ed->nodes / ed->time;
448 if( nps_100 < 100000 ) {
449 sprintf( s_label, "NPS: %lu", nps_100 * 100 );
452 sprintf( s_label, "NPS: %.1fk", nps_100 / 10.0 );
456 SetWindowText( ed->hLabelNPS, s_label );
459 if( ed->pv != 0 && *ed->pv != '\0' ) {
465 int time_secs = ed->time / 100;
466 int time_cent = ed->time % 100;
469 if( ed->nodes < 1000000 ) {
470 sprintf( s_nodes, "%lu", ed->nodes );
473 sprintf( s_nodes, "%.1fM", ed->nodes / 1000000.0 );
477 if( ed->score > 0 ) {
478 sprintf( s_score, "+%.2f", ed->score / 100.0 );
481 sprintf( s_score, "%.2f", ed->score / 100.0 );
485 sprintf( s_time, "%d:%02d.%02d", time_secs / 60, time_secs % 60, time_cent );
487 /* Put all together... */
488 sprintf( buf, "%3d\t%s\t%s\t%s\t", ed->depth, s_score, s_nodes, s_time );
491 buflen = strlen(buf);
493 strncpy( buf + buflen, ed->pv, sizeof(buf) - buflen );
495 buf[ sizeof(buf) - 3 ] = '\0';
497 strcat( buf + buflen, "\r\n" );
500 InsertIntoMemo( ed->hMemo, buf );
504 SetEngineColorIcon( ed->hColorIcon, ed->which );
507 LRESULT CALLBACK EngineOutputProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
513 if( engineOutputDialog == NULL ) {
514 engineOutputDialog = hDlg;
516 RestoreWindowPlacement( hDlg, &wpEngineOutput ); /* Restore window placement */
518 ResizeWindowControls( hDlg, windowMode );
520 SetEngineState( 0, STATE_IDLE, "" );
521 SetEngineState( 1, STATE_IDLE, "" );
527 switch (LOWORD(wParam)) {
529 EndDialog(hDlg, TRUE);
533 EndDialog(hDlg, FALSE);
542 case WM_GETMINMAXINFO:
544 MINMAXINFO * mmi = (MINMAXINFO *) lParam;
546 mmi->ptMinTrackSize.x = 100;
547 mmi->ptMinTrackSize.y = 160;
552 EngineOutputPopDown();
556 ResizeWindowControls( hDlg, windowMode );
559 case WM_ENTERSIZEMOVE:
560 return OnEnterSizeMove( &sd, hDlg, wParam, lParam );
563 return OnSizing( &sd, hDlg, wParam, lParam );
566 return OnMoving( &sd, hDlg, wParam, lParam );
568 case WM_EXITSIZEMOVE:
569 return OnExitSizeMove( &sd, hDlg, wParam, lParam );
575 VOID EngineOutputPopUp()
580 InitializeEngineOutput();
583 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_CHECKED);
585 if( engineOutputDialog ) {
586 SendMessage( engineOutputDialog, WM_INITDIALOG, 0, 0 );
588 if( ! engineOutputDialogUp ) {
589 ShowWindow(engineOutputDialog, SW_SHOW);
593 lpProc = MakeProcInstance( (FARPROC) EngineOutputProc, hInst );
595 /* Note to self: dialog must have the WS_VISIBLE style set, otherwise it's not shown! */
596 CreateDialog( hInst, MAKEINTRESOURCE(DLG_EngineOutput), hwndMain, (DLGPROC)lpProc );
598 FreeProcInstance(lpProc);
601 engineOutputDialogUp = TRUE;
604 VOID EngineOutputPopDown()
606 CheckMenuItem(GetMenu(hwndMain), IDM_ShowEngineOutput, MF_UNCHECKED);
608 if( engineOutputDialog ) {
609 ShowWindow(engineOutputDialog, SW_HIDE);
612 engineOutputDialogUp = FALSE;
615 BOOL EngineOutputIsUp()
617 return engineOutputDialogUp;
620 VOID EngineOutputUpdate( int which, int depth, unsigned long nodes, int score, int time, char * pv, char * hint )
623 BOOL clearMemo = FALSE;
625 if( which < 0 || which > 1 || depth < 0 || time < 0 || pv == 0 || *pv == '\0' ) {
629 if( engineOutputDialog == NULL ) {
643 /* Get target control */
645 ed.hColorIcon = GetDlgItem( engineOutputDialog, IDC_Color1 );
646 ed.hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel1 );
647 ed.hStateIcon = GetDlgItem( engineOutputDialog, IDC_StateIcon1 );
648 ed.hStateData = GetDlgItem( engineOutputDialog, IDC_StateData1 );
649 ed.hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine1_NPS );
650 ed.hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo1 );
651 ed.name = first.tidy;
654 ed.hColorIcon = GetDlgItem( engineOutputDialog, IDC_Color2 );
655 ed.hLabel = GetDlgItem( engineOutputDialog, IDC_EngineLabel2 );
656 ed.hStateIcon = GetDlgItem( engineOutputDialog, IDC_StateIcon2 );
657 ed.hStateData = GetDlgItem( engineOutputDialog, IDC_StateData2 );
658 ed.hLabelNPS = GetDlgItem( engineOutputDialog, IDC_Engine2_NPS );
659 ed.hMemo = GetDlgItem( engineOutputDialog, IDC_EngineMemo2 );
660 ed.name = second.tidy;
663 /* Clear memo if needed */
664 if( lastDepth[which] > depth || (lastDepth[which] == depth && depth <= 1) ) {
668 if( lastForwardMostMove[which] != forwardMostMove ) {
673 SendMessage( ed.hMemo, WM_SETTEXT, 0, (LPARAM) "" );
677 lastDepth[which] = depth;
678 lastForwardMostMove[which] = forwardMostMove;
681 if( strncmp( pv, " no PV", 6 ) == 0 ) { /* Hack on hack! :-O */
686 UpdateControls( &ed );