A better and more flexible way of invoking Polyglot
[xboard.git] / xengineoutput.c
1 /*
2  * Engine output (PV)
3  *
4  * Author: Alessandro Scotti (Dec 2005)
5  *
6  * Copyright 2005 Alessandro Scotti
7  *
8  * Enhancements Copyright 2009 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 #include "config.h"
29
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <sys/types.h>
34
35 #if STDC_HEADERS
36 # include <stdlib.h>
37 # include <string.h>
38 #else /* not STDC_HEADERS */
39 extern char *getenv();
40 # if HAVE_STRING_H
41 #  include <string.h>
42 # else /* not HAVE_STRING_H */
43 #  include <strings.h>
44 # endif /* not HAVE_STRING_H */
45 #endif /* not STDC_HEADERS */
46
47 #if HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50
51 #include <X11/Intrinsic.h>
52 #include <X11/StringDefs.h>
53 #include <X11/Shell.h>
54 #include <X11/Xaw/Dialog.h>
55 #include <X11/Xaw/Form.h>
56 #include <X11/Xaw/List.h>
57 #include <X11/Xaw/Label.h>
58 #include <X11/Xaw/SimpleMenu.h>
59 #include <X11/Xaw/SmeBSB.h>
60 #include <X11/Xaw/SmeLine.h>
61 #include <X11/Xaw/Box.h>
62 #include <X11/Xaw/Paned.h>
63 #include <X11/Xaw/MenuButton.h>
64 #include <X11/cursorfont.h>
65 #include <X11/Xaw/Text.h>
66 #include <X11/Xaw/AsciiText.h>
67 #include <X11/Xaw/Viewport.h>
68
69 #include "common.h"
70 #include "frontend.h"
71 #include "backend.h"
72 #include "xboard.h"
73 #include "engineoutput.h"
74 #include "gettext.h"
75
76 #ifdef ENABLE_NLS
77 # define  _(s) gettext (s)
78 # define N_(s) gettext_noop (s)
79 #else
80 # define  _(s) (s)
81 # define N_(s)  s
82 #endif
83
84 #include <X11/xpm.h>
85
86 // [HGM] pixmaps of some ICONS used in the engine-outut window
87 #include "pixmaps/WHITE_14.xpm"
88 #include "pixmaps/BLACK_14.xpm"
89 #include "pixmaps/CLEAR_14.xpm"
90 #include "pixmaps/UNKNOWN_14.xpm"
91 #include "pixmaps/THINKING_14.xpm"
92 #include "pixmaps/PONDER_14.xpm"
93 #include "pixmaps/ANALYZING_14.xpm"
94
95 #ifdef SNAP
96 #include "wsnap.h"
97 #endif
98
99 #define _LL_ 100
100
101 // imports from xboard.c
102 extern Widget formWidget, shellWidget, boardWidget, menuBarWidget;
103 extern Display *xDisplay;
104 extern Window xBoardWindow;
105 extern int squareSize;
106 extern Pixmap xMarkPixmap, wIconPixmap, bIconPixmap;
107 extern char *layoutName;
108
109 Pixmap icons[8]; // [HGM] this front-end array translates back-end icon indicator to handle
110 Widget outputField[2][7]; // [HGM] front-end array to translate output field to window handle
111
112 void EngineOutputPopDown();
113 void engineOutputPopUp();
114 int  EngineOutputIsUp();
115 void SetEngineColorIcon( int which );
116
117 /* Imports from backend.c */
118 char * SavePart(char *str);
119 extern int opponentKibitzes;
120
121 /* Imports from xboard.c */
122 extern Arg layoutArgs[2], formArgs[2], messageArgs[4];
123
124 //extern WindowPlacement wpEngineOutput;
125
126 Position engineOutputX = -1, engineOutputY = -1;
127 Dimension engineOutputW, engineOutputH;
128 Widget engineOutputShell;
129 static int engineOutputDialogUp;
130
131 /* Module variables */
132 int  windowMode = 1;
133
134 typedef struct {
135     char * name;
136     int which;
137     int depth;
138     u64 nodes;
139     int score;
140     int time;
141     char * pv;
142     char * hint;
143     int an_move_index;
144     int an_move_count;
145 } EngineOutputData;
146
147 //static void UpdateControls( EngineOutputData * ed );
148
149 void ReadIcon(char *pixData[], int iconNr)
150 {
151     int r;
152
153         if ((r=XpmCreatePixmapFromData(xDisplay, XtWindow(outputField[0][nColorIcon]),
154                                        pixData,
155                                        &(icons[iconNr]),
156                                        NULL, NULL /*&attr*/)) != 0) {
157           fprintf(stderr, _("Error %d loading icon image\n"), r);
158           exit(1); 
159         }       
160 }
161
162 static void InitializeEngineOutput()
163 { int i;
164
165         ReadIcon(WHITE_14,   nColorWhite);
166         ReadIcon(BLACK_14,   nColorBlack);
167         ReadIcon(UNKNOWN_14, nColorUnknown);
168
169         ReadIcon(CLEAR_14,   nClear);
170         ReadIcon(PONDER_14,  nPondering);
171         ReadIcon(THINK_14,   nThinking);
172         ReadIcon(ANALYZE_14, nAnalyzing);
173 }
174
175 void DoSetWindowText(int which, int field, char *s_label)
176
177         Arg arg;
178
179         XtSetArg(arg, XtNlabel, (XtArgVal) s_label);
180         XtSetValues(outputField[which][field], &arg, 1);
181 }
182
183 void InsertIntoMemo( int which, char * text, int where )
184 {
185         Arg arg; XawTextBlock t; Widget edit;
186
187         /* the backend adds \r\n, which is needed for winboard, 
188          * for xboard we delete them again over here */
189         if(t.ptr = strchr(text, '\r')) *t.ptr = ' ';
190
191         t.ptr = text; t.firstPos = 0; t.length = strlen(text); t.format = XawFmt8Bit;
192         edit = XtNameToWidget(engineOutputShell, which ? "*form2.text" : "*form.text");
193         XawTextReplace(edit, where, where, &t);
194 //      XtSetArg(arg, XtNstring, (XtArgVal) text);
195 //      XtSetValues(outputField[which][nMemo], &arg, 1);
196 }
197
198 void SetIcon( int which, int field, int nIcon )
199 {
200     Arg arg;
201
202     if( nIcon != 0 ) {
203         XtSetArg(arg, XtNleftBitmap, (XtArgVal) icons[nIcon]);
204         XtSetValues(outputField[which][field], &arg, 1);
205     }
206 }
207
208 void DoClearMemo(int which)
209
210     Arg args[16];
211     int j;
212     Widget edit;
213
214         edit = XtNameToWidget(engineOutputShell, which ? "*form2.text" : "*form.text");
215         XtCallActionProc(edit, "select-all", NULL, NULL, 0);
216         XtCallActionProc(edit, "kill-selection", NULL, NULL, 0);
217 }
218
219 // The following routines are mutated clones of the commentPopUp routines
220
221 void PositionControlSet(which, form, bw_width)
222      int which;
223      Widget form;
224      Dimension bw_width;
225 {
226     Arg args[16];
227     Widget edit, NameWidget, ColorWidget, ModeWidget, MoveWidget, NodesWidget;
228     int j, mutable=1;
229     j = 0;
230     XtSetArg(args[j], XtNborderWidth, (XtArgVal) 0); j++;
231     XtSetArg(args[j], XtNlabel,     (XtArgVal) ""); j++;
232     XtSetArg(args[j], XtNtop,       XtChainTop); j++;
233     XtSetArg(args[j], XtNbottom,    XtChainTop); j++;
234     XtSetArg(args[j], XtNleft,      XtChainLeft); j++;
235     XtSetArg(args[j], XtNright,     XtChainLeft); j++;
236     XtSetArg(args[j], XtNheight,    (XtArgVal) 16); j++;
237     XtSetArg(args[j], XtNwidth,     (XtArgVal) 17); j++;
238     outputField[which][nColorIcon] = ColorWidget =
239       XtCreateManagedWidget("Color", labelWidgetClass,
240                      form, args, j);
241
242     j = 0;
243     XtSetArg(args[j], XtNborderWidth, (XtArgVal) 0); j++;
244     XtSetArg(args[j], XtNjustify,   (XtArgVal) XtJustifyLeft); j++;
245     XtSetArg(args[j], XtNfromHoriz, (XtArgVal) ColorWidget); j++;
246     XtSetArg(args[j], XtNtop,       XtChainTop); j++;
247     XtSetArg(args[j], XtNbottom,    XtChainTop); j++;
248     XtSetArg(args[j], XtNleft,      XtChainLeft); j++;
249     XtSetArg(args[j], XtNheight,    (XtArgVal) 16); j++;
250     XtSetArg(args[j], XtNwidth,     (XtArgVal) bw_width/2 - 57); j++;
251     outputField[which][nLabel] = NameWidget =
252       XtCreateManagedWidget("Engine", labelWidgetClass,
253                      form, args, j);
254
255     j = 0;
256     XtSetArg(args[j], XtNborderWidth, (XtArgVal) 0); j++;
257     XtSetArg(args[j], XtNlabel,     (XtArgVal) ""); j++;
258     XtSetArg(args[j], XtNfromHoriz, (XtArgVal) NameWidget); j++;
259     XtSetArg(args[j], XtNtop,       XtChainTop); j++;
260     XtSetArg(args[j], XtNbottom,    XtChainTop); j++;
261     XtSetArg(args[j], XtNheight,    (XtArgVal) 16); j++;
262     XtSetArg(args[j], XtNwidth,     (XtArgVal) 20); j++;
263     outputField[which][nStateIcon] = ModeWidget =
264       XtCreateManagedWidget("Mode", labelWidgetClass,
265                      form, args, j);
266
267     j = 0;
268     XtSetArg(args[j], XtNborderWidth, (XtArgVal) 0); j++;
269     XtSetArg(args[j], XtNjustify,   (XtArgVal) XtJustifyLeft); j++;
270     XtSetArg(args[j], XtNlabel,     (XtArgVal) ""); j++;
271     XtSetArg(args[j], XtNfromHoriz, (XtArgVal) ModeWidget); j++;
272     XtSetArg(args[j], XtNtop,       XtChainTop); j++;
273     XtSetArg(args[j], XtNbottom,    XtChainTop); j++;
274     XtSetArg(args[j], XtNright,     XtChainRight); j++;
275     XtSetArg(args[j], XtNheight,    (XtArgVal) 16); j++;
276     XtSetArg(args[j], XtNwidth,     (XtArgVal) bw_width/2 - 102); j++;
277     outputField[which][nStateData] = MoveWidget =
278       XtCreateManagedWidget("Move", labelWidgetClass,
279                      form, args, j);
280
281     j = 0;
282     XtSetArg(args[j], XtNborderWidth, (XtArgVal) 0); j++;
283     XtSetArg(args[j], XtNjustify,   (XtArgVal) XtJustifyRight); j++;
284     XtSetArg(args[j], XtNlabel,     (XtArgVal) _("NPS")); j++;
285     XtSetArg(args[j], XtNfromHoriz, (XtArgVal) MoveWidget); j++;
286     XtSetArg(args[j], XtNtop,       XtChainTop); j++;
287     XtSetArg(args[j], XtNbottom,    XtChainTop); j++;
288     XtSetArg(args[j], XtNleft,      XtChainRight); j++;
289     XtSetArg(args[j], XtNright,     XtChainRight); j++;
290     XtSetArg(args[j], XtNheight,    (XtArgVal) 16); j++;
291     XtSetArg(args[j], XtNwidth,     (XtArgVal) 100); j++;
292     outputField[which][nLabelNPS] = NodesWidget =
293       XtCreateManagedWidget("Nodes", labelWidgetClass,
294                      form, args, j);
295
296     // create "text" within "form"
297     j = 0;
298     if (mutable) {
299         XtSetArg(args[j], XtNeditType, XawtextEdit);  j++;
300         XtSetArg(args[j], XtNuseStringInPlace, False);  j++;
301     }
302     XtSetArg(args[j], XtNstring, "");  j++;
303     XtSetArg(args[j], XtNdisplayCaret, False);  j++;
304     XtSetArg(args[j], XtNtop, XtChainTop);  j++;
305     XtSetArg(args[j], XtNbottom, XtChainBottom);  j++;
306     XtSetArg(args[j], XtNleft, XtChainLeft);  j++;
307     XtSetArg(args[j], XtNright, XtChainRight);  j++;
308     XtSetArg(args[j], XtNresizable, True);  j++;
309     XtSetArg(args[j], XtNwidth, bw_width);  j++; /*force wider than buttons*/
310     /* !!Work around an apparent bug in XFree86 4.0.1 (X11R6.4.3) */
311     XtSetArg(args[j], XtNscrollVertical, XawtextScrollAlways);  j++;
312     XtSetArg(args[j], XtNscrollHorizontal, XawtextScrollWhenNeeded);  j++;
313 //    XtSetArg(args[j], XtNautoFill, True);  j++;
314 //    XtSetArg(args[j], XtNwrap, XawtextWrapWord); j++;
315     outputField[which][nMemo] = edit =
316       XtCreateManagedWidget("text", asciiTextWidgetClass, form, args, j);
317
318     j = 0;
319     XtSetArg(args[j], XtNfromVert, ColorWidget); j++;
320 //    XtSetArg(args[j], XtNresizable, (XtArgVal) True); j++;
321     XtSetValues(edit, args, j);
322 }
323
324 Widget EngineOutputCreate(name, text)
325      char *name, *text;
326 {
327     Arg args[16];
328     Widget shell, layout, form, form2, edit;
329     Dimension bw_width, bw_height;
330     int j;
331
332     // get board width
333     j = 0;
334     XtSetArg(args[j], XtNwidth,  &bw_width);  j++;
335     XtSetArg(args[j], XtNheight, &bw_height);  j++;
336     XtGetValues(boardWidget, args, j);
337
338     // define form within layout within shell.
339     j = 0;
340     XtSetArg(args[j], XtNresizable, True);  j++;
341     shell =
342 #if TOPLEVEL 
343      XtCreatePopupShell(name, topLevelShellWidgetClass,
344 #else
345       XtCreatePopupShell(name, transientShellWidgetClass,
346 #endif
347                          shellWidget, args, j);
348     layout =
349       XtCreateManagedWidget(layoutName, formWidgetClass, shell,
350                             layoutArgs, XtNumber(layoutArgs));
351     // divide window vertically into two equal parts, by creating two forms
352     form =
353       XtCreateManagedWidget("form", formWidgetClass, layout,
354                             formArgs, XtNumber(formArgs));
355     form2 =
356       XtCreateManagedWidget("form2", formWidgetClass, layout,
357                             formArgs, XtNumber(formArgs));
358     j = 0;
359     XtSetArg(args[j], XtNfromVert,  (XtArgVal) form); j++;
360     XtSetValues(form2, args, j);
361     // make sure width is known in advance, for better placement of child widgets
362     j = 0;
363     XtSetArg(args[j], XtNwidth,     (XtArgVal) bw_width-16); j++;
364     XtSetArg(args[j], XtNheight,    (XtArgVal) bw_height/2); j++;
365     XtSetValues(shell, args, j);
366
367     // fill up both forms with control elements
368     PositionControlSet(0, form,  bw_width);
369     PositionControlSet(1, form2, bw_width);
370
371     XtRealizeWidget(shell);
372
373     if(wpEngineOutput.width > 0) {
374       engineOutputW = wpEngineOutput.width;
375       engineOutputH = wpEngineOutput.height;
376       engineOutputX = wpEngineOutput.x;
377       engineOutputY = wpEngineOutput.y;
378     }
379
380     if (engineOutputX == -1) {
381         int xx, yy;
382         Window junk;
383         Dimension pw_height;
384         Dimension ew_height;
385         engineOutputH = bw_height/2;
386         engineOutputW = bw_width-16;
387
388         XSync(xDisplay, False);
389 #ifdef NOTDEF
390         /* This code seems to tickle an X bug if it is executed too soon
391            after xboard starts up.  The coordinates get transformed as if
392            the main window was positioned at (0, 0).
393            */
394         XtTranslateCoords(shellWidget,
395                           (bw_width - engineOutputW) / 2, 0 - engineOutputH / 2,
396                           &engineOutputX, &engineOutputY);
397 #else  /*!NOTDEF*/
398         XTranslateCoordinates(xDisplay, XtWindow(shellWidget),
399                               RootWindowOfScreen(XtScreen(shellWidget)),
400                               (bw_width - engineOutputW) / 2, 0 - engineOutputH / 2,
401                               &xx, &yy, &junk);
402         engineOutputX = xx;
403         engineOutputY = yy;
404 #endif /*!NOTDEF*/
405         if (engineOutputY < 0) engineOutputY = 0; /*avoid positioning top offscreen*/
406     }
407     j = 0;
408     XtSetArg(args[j], XtNheight, engineOutputH);  j++;
409     XtSetArg(args[j], XtNwidth, engineOutputW);  j++;
410     XtSetArg(args[j], XtNx, engineOutputX);  j++;
411     XtSetArg(args[j], XtNy, engineOutputY);  j++;
412     XtSetValues(shell, args, j);
413 //    XtSetKeyboardFocus(shell, edit);
414
415     return shell;
416 }
417
418 void ResizeWindowControls(mode)
419         int mode;
420 {
421     Widget form1, form2;
422     Arg args[16];
423     int j;
424     Dimension ew_height, tmp;
425     Widget shell = engineOutputShell;
426
427     form1 = XtNameToWidget(shell, "*form");
428     form2 = XtNameToWidget(shell, "*form2");
429
430     j = 0;
431     XtSetArg(args[j], XtNheight, (XtArgVal) &ew_height); j++;
432     XtGetValues(form1, args, j);
433     j = 0;
434     XtSetArg(args[j], XtNheight, (XtArgVal) &tmp); j++;
435     XtGetValues(form2, args, j);
436     ew_height += tmp; // total height
437
438     if(mode==0) {
439         j = 0;
440         XtSetArg(args[j], XtNheight, (XtArgVal) 5); j++;
441         XtSetValues(form2, args, j);
442         j = 0;
443         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height-5)); j++;
444         XtSetValues(form1, args, j);
445     } else {
446         j = 0;
447         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
448         XtSetValues(form1, args, j);
449         j = 0;
450         XtSetArg(args[j], XtNheight, (XtArgVal) (ew_height/2)); j++;
451         XtSetValues(form2, args, j);
452     }
453 }
454
455 void 
456 EngineOutputPopUp()
457 {
458     Arg args[16];
459     int j;
460     Widget edit;
461     static int  needInit = TRUE;
462     static char *title = _("Engine output"), *text = _("This feature is experimental");
463
464     if (engineOutputShell == NULL) {
465         engineOutputShell =
466           EngineOutputCreate(title, text);
467         XtRealizeWidget(engineOutputShell);
468         CatchDeleteWindow(engineOutputShell, "EngineOutputPopDown");
469         if( needInit ) {
470             InitializeEngineOutput();
471             needInit = FALSE;
472         }
473         SetEngineColorIcon( 0 );
474         SetEngineColorIcon( 1 );
475         SetEngineState( 0, STATE_IDLE, "" );
476         SetEngineState( 1, STATE_IDLE, "" );
477     } else {
478         edit = XtNameToWidget(engineOutputShell, "*form.text");
479         j = 0;
480         XtSetArg(args[j], XtNstring, text); j++;
481         XtSetValues(edit, args, j);
482         j = 0;
483         XtSetArg(args[j], XtNiconName, (XtArgVal) title);   j++;
484         XtSetArg(args[j], XtNtitle, (XtArgVal) title);      j++;
485         XtSetValues(engineOutputShell, args, j);
486     }
487
488     XtPopup(engineOutputShell, XtGrabNone);
489     XSync(xDisplay, False);
490
491     j=0;
492     XtSetArg(args[j], XtNleftBitmap, xMarkPixmap); j++;
493     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Engine Output"),
494                 args, j);
495
496     engineOutputDialogUp = True;
497     ShowThinkingEvent(); // [HGM] thinking: might need to prompt engine for thinking output
498 }
499
500 void EngineOutputPopDown()
501 {
502     Arg args[16];
503     int j;
504
505     if (!engineOutputDialogUp) return;
506     DoClearMemo(1);
507     j = 0;
508     XtSetArg(args[j], XtNx, &engineOutputX); j++;
509     XtSetArg(args[j], XtNy, &engineOutputY); j++;
510     XtSetArg(args[j], XtNwidth, &engineOutputW); j++;
511     XtSetArg(args[j], XtNheight, &engineOutputH); j++;
512     XtGetValues(engineOutputShell, args, j);
513     wpEngineOutput.x = engineOutputX - 4;
514     wpEngineOutput.y = engineOutputY - 23;
515     wpEngineOutput.width = engineOutputW;
516     wpEngineOutput.height = engineOutputH;
517     XtPopdown(engineOutputShell);
518     XSync(xDisplay, False);
519     j=0;
520     XtSetArg(args[j], XtNleftBitmap, None); j++;
521     XtSetValues(XtNameToWidget(menuBarWidget, "menuMode.Show Engine Output"),
522                 args, j);
523
524     engineOutputDialogUp = False;
525     ShowThinkingEvent(); // [HGM] thinking: might need to shut off thinking output
526 }
527
528 int EngineOutputIsUp()
529 {
530     return engineOutputDialogUp;
531 }
532
533 int EngineOutputDialogExists()
534 {
535     return engineOutputShell != NULL;
536 }
537
538 void
539 EngineOutputProc(w, event, prms, nprms)
540      Widget w;
541      XEvent *event;
542      String *prms;
543      Cardinal *nprms;
544 {
545   if (engineOutputDialogUp) {
546     EngineOutputPopDown();
547   } else {
548     EngineOutputPopUp();
549   }
550 }