Fix multi-leg promotions
[xboard.git] / menus.c
1 /*
2  * menus.c -- platform-indendent menu handling code for XBoard
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, 2013, 2014, 2015, 2016 Free
9  * Software Foundation, Inc.
10  *
11  * The following terms apply to Digital Equipment Corporation's copyright
12  * interest in XBoard:
13  * ------------------------------------------------------------------------
14  * All Rights Reserved
15  *
16  * Permission to use, copy, modify, and distribute this software and its
17  * documentation for any purpose and without fee is hereby granted,
18  * provided that the above copyright notice appear in all copies and that
19  * both that copyright notice and this permission notice appear in
20  * supporting documentation, and that the name of Digital not be
21  * used in advertising or publicity pertaining to distribution of the
22  * software without specific, written prior permission.
23  *
24  * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
25  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
26  * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
27  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
28  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
29  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
30  * SOFTWARE.
31  * ------------------------------------------------------------------------
32  *
33  * The following terms apply to the enhanced version of XBoard
34  * distributed by the Free Software Foundation:
35  * ------------------------------------------------------------------------
36  *
37  * GNU XBoard is free software: you can redistribute it and/or modify
38  * it under the terms of the GNU General Public License as published by
39  * the Free Software Foundation, either version 3 of the License, or (at
40  * your option) any later version.
41  *
42  * GNU XBoard is distributed in the hope that it will be useful, but
43  * WITHOUT ANY WARRANTY; without even the implied warranty of
44  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
45  * General Public License for more details.
46  *
47  * You should have received a copy of the GNU General Public License
48  * along with this program. If not, see http://www.gnu.org/licenses/.  *
49  *
50  *------------------------------------------------------------------------
51  ** See the file ChangeLog for a revision history.  */
52
53 #define HIGHDRAG 1
54
55 #include "config.h"
56
57 #include <stdio.h>
58 #include <ctype.h>
59 #include <signal.h>
60 #include <errno.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <pwd.h>
64 #include <math.h>
65
66 #if STDC_HEADERS
67 # include <stdlib.h>
68 # include <string.h>
69 #else /* not STDC_HEADERS */
70 extern char *getenv();
71 # if HAVE_STRING_H
72 #  include <string.h>
73 # else /* not HAVE_STRING_H */
74 #  include <strings.h>
75 # endif /* not HAVE_STRING_H */
76 #endif /* not STDC_HEADERS */
77
78 #if HAVE_UNISTD_H
79 # include <unistd.h>
80 #endif
81
82 #if ENABLE_NLS
83 #include <locale.h>
84 #endif
85
86 // [HGM] bitmaps: put before incuding the bitmaps / pixmaps, to know how many piece types there are.
87 #include "common.h"
88
89 #include "frontend.h"
90 #include "backend.h"
91 #include "menus.h"
92 #include "gettext.h"
93
94 #ifdef ENABLE_NLS
95 # define  _(s) gettext (s)
96 # define N_(s) gettext_noop (s)
97 #else
98 # define  _(s) (s)
99 # define N_(s)  s
100 #endif
101
102 /*
103  * Button/menu procedures
104  */
105
106 char  *gameCopyFilename, *gamePasteFilename;
107 Boolean saveSettingsOnExit;
108 char *settingsFileName;
109 char gamesDir[MSG_SIZ], positionsDir[MSG_SIZ], textureDir[MSG_SIZ], bookDir[MSG_SIZ], piecesDir[MSG_SIZ];
110
111 static int
112 LoadGamePopUp (FILE *f, int gameNumber, char *title)
113 {
114     cmailMsgLoaded = FALSE;
115     if (gameNumber == 0) {
116         int error = GameListBuild(f);
117         if (error) {
118             DisplayError(_("Cannot build game list"), error);
119         } else if (!ListEmpty(&gameList) &&
120                    ((ListGame *) gameList.tailPred)->number > 1) {
121             GameListPopUp(f, title);
122             return TRUE;
123         }
124         GameListDestroy();
125         gameNumber = 1;
126     }
127     return LoadGame(f, gameNumber, title, FALSE);
128 }
129
130 void
131 LoadGameProc ()
132 {
133     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
134         Reset(FALSE, TRUE);
135     }
136     FileNamePopUp(_("Load game file name?"), "", ".pgn .game", LoadGamePopUp, "rb");
137 }
138
139 void
140 LoadNextGameProc ()
141 {
142     ReloadGame(1);
143 }
144
145 void
146 LoadPrevGameProc ()
147 {
148     ReloadGame(-1);
149 }
150
151 void
152 ReloadGameProc ()
153 {
154     ReloadGame(0);
155 }
156
157 void
158 LoadNextPositionProc ()
159 {
160     ReloadPosition(1);
161 }
162
163 void
164 LoadPrevPositionProc ()
165 {
166     ReloadPosition(-1);
167 }
168
169 void
170 ReloadPositionProc ()
171 {
172     ReloadPosition(0);
173 }
174
175 void
176 LoadPositionProc()
177 {
178     static char buf[MSG_SIZ];
179     if (gameMode == AnalyzeMode || gameMode == AnalyzeFile) {
180         Reset(FALSE, TRUE);
181     }
182     snprintf(buf, MSG_SIZ, "%s/", appData.positionDir);
183     FileNamePopUp(_("Load position file name?"), buf, ".fen .epd .pos", LoadPosition, "rb");
184 }
185
186 void
187 SaveGameProc ()
188 {
189     FileNamePopUp(_("Save game file name?"),
190                   DefaultFileName(appData.oldSaveStyle ? "game" : "pgn"),
191                   appData.oldSaveStyle ? ".game" : ".pgn",
192                   SaveGame, "a");
193 }
194
195 void
196 SavePositionProc ()
197 {
198     FileNamePopUp(_("Save position file name?"),
199                   DefaultFileName(appData.oldSaveStyle ? "pos" : "fen"),
200                   appData.oldSaveStyle ? ".pos" : ".fen",
201                   SavePosition, "a");
202 }
203
204 void
205 ReloadCmailMsgProc ()
206 {
207     ReloadCmailMsgEvent(FALSE);
208 }
209
210 void
211 CopyFENToClipboard ()
212 { // wrapper to make call from back-end possible
213   CopyPositionProc();
214 }
215
216 void
217 CopyPositionProc ()
218 {
219     static char *selected_fen_position=NULL;
220     if(gameMode == EditPosition) EditPositionDone(TRUE);
221     if (selected_fen_position) free(selected_fen_position);
222     selected_fen_position = (char *)PositionToFEN(currentMove, NULL, 1);
223     if (!selected_fen_position) return;
224     CopySomething(selected_fen_position);
225 }
226
227 void
228 CopyGameProc ()
229 {
230   int ret;
231
232   ret = SaveGameToFile(gameCopyFilename, FALSE);
233   if (!ret) return;
234
235   CopySomething(NULL);
236 }
237
238 void
239 CopyGameListProc ()
240 {
241   if(!SaveGameListAsText(fopen(gameCopyFilename, "w"))) return;
242   CopySomething(NULL);
243 }
244
245 void
246 AutoSaveGame ()
247 {
248     SaveGameProc();
249 }
250
251
252 void
253 QuitProc ()
254 {
255     ExitEvent(0);
256 }
257
258 void
259 MatchProc ()
260 {
261     static Enables matchOff[] = { { "Mode.MachineMatch", False }, { NULL, False } };
262     if(matchMode) SetMenuEnables(matchOff);
263     MatchEvent(2);
264 }
265
266 void
267 AdjuWhiteProc ()
268 {
269     UserAdjudicationEvent(+1);
270 }
271
272 void
273 AdjuBlackProc ()
274 {
275     UserAdjudicationEvent(-1);
276 }
277
278 void
279 AdjuDrawProc ()
280 {
281     UserAdjudicationEvent(0);
282 }
283
284 void
285 RevertProc ()
286 {
287     RevertEvent(False);
288 }
289
290 void
291 AnnotateProc ()
292 {
293     RevertEvent(True);
294 }
295
296 void
297 FlipViewProc ()
298 {
299     if(twoBoards) { partnerUp = 1; DrawPosition(True, NULL); partnerUp = 0; }
300     flipView = !flipView;
301     DrawPosition(True, NULL);
302 }
303
304 void
305 SaveOnExitProc ()
306 {
307   saveSettingsOnExit = !saveSettingsOnExit;
308
309   MarkMenuItem("Options.SaveSettingsonExit", saveSettingsOnExit);
310 }
311
312 void
313 SaveSettingsProc ()
314 {
315   SaveSettings(settingsFileName);
316 }
317
318 void
319 BugReportProc ()
320 {
321     char buf[MSG_SIZ];
322     snprintf(buf, MSG_SIZ, "%s mailto:bug-xboard@gnu.org", appData.sysOpen);
323     system(buf);
324 }
325
326 void
327 GuideProc ()
328 {
329     char buf[MSG_SIZ];
330     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/user_guide/UserGuide.html", appData.sysOpen);
331     system(buf);
332 }
333
334 void
335 HomePageProc ()
336 {
337     char buf[MSG_SIZ];
338     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/", appData.sysOpen);
339     system(buf);
340 }
341
342 void
343 NewsPageProc ()
344 {
345     char buf[MSG_SIZ];
346     snprintf(buf, MSG_SIZ, "%s http://www.gnu.org/software/xboard/whats_new/portal.html", appData.sysOpen);
347     system(buf);
348 }
349
350 void
351 AboutProc ()
352 {
353     char buf[2 * MSG_SIZ];
354 #if ZIPPY
355     char *zippy = _(" (with Zippy code)");
356 #else
357     char *zippy = "";
358 #endif
359     snprintf(buf, sizeof(buf),
360 _("%s%s\n\n"
361 "Copyright 1991 Digital Equipment Corporation\n"
362 "Enhancements Copyright 1992-2016 Free Software Foundation\n"
363 "Enhancements Copyright 2005 Alessandro Scotti\n\n"
364 "%s is free software and carries NO WARRANTY;"
365 "see the file COPYING for more information.\n\n"
366 "Visit XBoard on the web at: http://www.gnu.org/software/xboard/\n"
367 "Check out the newest features at: http://www.gnu.org/software/xboard/whats_new.html\n\n"
368 "Report bugs via email at: <bug-xboard@gnu.org>\n\n"
369   ),
370             programVersion, zippy, PACKAGE);
371     ErrorPopUp(_("About XBoard"), buf, FALSE);
372 }
373
374 void
375 DebugProc ()
376 {
377     appData.debugMode = !appData.debugMode;
378     if(!strcmp(appData.nameOfDebugFile, "stderr")) return; // stderr is already open, and should never be closed
379     if(!appData.debugMode) fclose(debugFP);
380     else {
381         debugFP = fopen(appData.nameOfDebugFile, "w");
382         if(debugFP == NULL) debugFP = stderr;
383         else setbuf(debugFP, NULL);
384     }
385 }
386
387 void
388 EditEngineProc ()
389 {
390     EditAnyPopUp(firstChessProgramNames, &firstChessProgramNames, _("Registered Engines"));
391 }
392
393 void
394 EditThemesProc ()
395 {
396     EditAnyPopUp(appData.themeNames, &appData.themeNames, _("Predefined Themes"));
397 }
398
399 void
400 EditMenuProc ()
401 {
402     EditAnyPopUp(icsTextMenuString, &icsTextMenuString, _("ICS Text-Menu Definition"));
403 }
404
405 void
406 NothingProc ()
407 {
408     return;
409 }
410
411 #ifdef OPTIONSDIALOG
412 #   define MARK_MENU_ITEM(X,Y)
413 #else
414 #   define MARK_MENU_ITEM(X,Y) MarkMenuItem(X, Y)
415 #endif
416
417 void
418 PonderNextMoveProc ()
419 {
420   PonderNextMoveEvent(!appData.ponderNextMove);
421   MARK_MENU_ITEM("Options.PonderNextMove", appData.ponderNextMove);
422 }
423
424 void
425 AlwaysQueenProc ()
426 {
427     appData.alwaysPromoteToQueen = !appData.alwaysPromoteToQueen;
428     MARK_MENU_ITEM("Options.AlwaysQueen", appData.alwaysPromoteToQueen);
429 }
430
431 void
432 AnimateDraggingProc ()
433 {
434     appData.animateDragging = !appData.animateDragging;
435
436     if (appData.animateDragging) CreateAnimVars();
437     MARK_MENU_ITEM("Options.AnimateDragging", appData.animateDragging);
438 }
439
440 void
441 AnimateMovingProc ()
442 {
443     appData.animate = !appData.animate;
444     if (appData.animate) CreateAnimVars();
445     MARK_MENU_ITEM("Options.AnimateMoving", appData.animate);
446 }
447
448 void
449 AutoflagProc ()
450 {
451     appData.autoCallFlag = !appData.autoCallFlag;
452     MARK_MENU_ITEM("Options.AutoFlag", appData.autoCallFlag);
453 }
454
455 void
456 AutoflipProc ()
457 {
458     appData.autoFlipView = !appData.autoFlipView;
459     MARK_MENU_ITEM("Options.AutoFlipView", appData.autoFlipView);
460 }
461
462 void
463 BlindfoldProc ()
464 {
465     appData.blindfold = !appData.blindfold;
466     MARK_MENU_ITEM("Options.Blindfold", appData.blindfold);
467     DrawPosition(True, NULL);
468 }
469
470 void
471 TestLegalityProc ()
472 {
473     appData.testLegality = !appData.testLegality;
474     MARK_MENU_ITEM("Options.TestLegality", appData.testLegality);
475 }
476
477
478 void
479 FlashMovesProc ()
480 {
481     if (appData.flashCount == 0) {
482         appData.flashCount = 3;
483     } else {
484         appData.flashCount = -appData.flashCount;
485     }
486     MARK_MENU_ITEM("Options.FlashMoves", appData.flashCount > 0);
487 }
488
489 #if HIGHDRAG
490 void
491 HighlightDraggingProc ()
492 {
493     appData.highlightDragging = !appData.highlightDragging;
494     MARK_MENU_ITEM("Options.HighlightDragging", appData.highlightDragging);
495 }
496 #endif
497
498 void
499 HighlightLastMoveProc ()
500 {
501     appData.highlightLastMove = !appData.highlightLastMove;
502     MARK_MENU_ITEM("Options.HighlightLastMove", appData.highlightLastMove);
503 }
504
505 void
506 HighlightArrowProc ()
507 {
508     appData.highlightMoveWithArrow = !appData.highlightMoveWithArrow;
509     MARK_MENU_ITEM("Options.HighlightWithArrow", appData.highlightMoveWithArrow);
510 }
511
512 void
513 IcsAlarmProc ()
514 {
515     appData.icsAlarm = !appData.icsAlarm;
516 //    MARK_MENU_ITEM("Options.ICSAlarm", appData.icsAlarm);
517 }
518
519 void
520 MoveSoundProc ()
521 {
522     appData.ringBellAfterMoves = !appData.ringBellAfterMoves;
523     MARK_MENU_ITEM("Options.MoveSound", appData.ringBellAfterMoves);
524 }
525
526 void
527 OneClickProc ()
528 {
529     appData.oneClick = !appData.oneClick;
530     MARK_MENU_ITEM("Options.OneClickMoving", appData.oneClick);
531 }
532
533 void
534 PeriodicUpdatesProc ()
535 {
536     PeriodicUpdatesEvent(!appData.periodicUpdates);
537     MARK_MENU_ITEM("Options.PeriodicUpdates", appData.periodicUpdates);
538 }
539
540 void
541 PopupExitMessageProc ()
542 {
543     appData.popupExitMessage = !appData.popupExitMessage;
544     MARK_MENU_ITEM("Options.PopupExitMessage", appData.popupExitMessage);
545 }
546
547 void
548 PopupMoveErrorsProc ()
549 {
550     appData.popupMoveErrors = !appData.popupMoveErrors;
551     MARK_MENU_ITEM("Options.PopupMoveErrors", appData.popupMoveErrors);
552 }
553
554 void
555 PremoveProc ()
556 {
557     appData.premove = !appData.premove;
558 //    MARK_MENU_ITEM("Options.Premove", appData.premove);
559 }
560
561 void
562 ShowCoordsProc ()
563 {
564     appData.showCoords = !appData.showCoords;
565     MARK_MENU_ITEM("Options.ShowCoords", appData.showCoords);
566     DrawPosition(True, NULL);
567 }
568
569 void
570 ShowThinkingProc ()
571 {
572     appData.showThinking = !appData.showThinking; // [HGM] thinking: taken out of ShowThinkingEvent
573     ShowThinkingEvent();
574 }
575
576 void
577 HideThinkingProc ()
578 {
579   appData.hideThinkingFromHuman = !appData.hideThinkingFromHuman; // [HGM] thinking: taken out of ShowThinkingEvent
580   ShowThinkingEvent();
581
582   MARK_MENU_ITEM("Options.HideThinking", appData.hideThinkingFromHuman);
583 }
584
585 void
586 CreateBookDelayed ()
587 {
588   ScheduleDelayedEvent(CreateBookEvent, 50);
589 }
590
591 void
592 SaveSelectedProc ()
593 {
594   FileNamePopUp(_("Save game file name?"),
595                   "",
596                   ".pgn",
597                   SaveSelected, "a");
598 }
599
600 /*
601  *  Menu definition tables
602  */
603
604 MenuItem fileMenu[] = {
605   {N_("New Game"),             "<Ctrl>n",          "NewGame",              ResetGameEvent},
606   {N_("New Shuffle Game..."),   NULL,              "NewShuffleGame",       ShuffleMenuProc},
607   {N_("New Variant..."),       "<Alt><Shift>v",    "NewVariant",           NewVariantProc},// [HGM] variant: not functional yet
608   {"----",                      NULL,               NULL,                  NothingProc},
609   {N_("Load Game"),            "<Ctrl>o",          "LoadGame",             LoadGameProc,           CHECK},
610   {N_("Load Position"),        "<Ctrl><Shift>o",   "LoadPosition",         LoadPositionProc},
611   {N_("Next Position"),        "<Shift>Page_Down", "LoadNextPosition",     LoadNextPositionProc},
612   {N_("Prev Position"),        "<Shift>Page_Up",   "LoadPreviousPosition", LoadPrevPositionProc},
613   {"----",                      NULL,               NULL,                  NothingProc},
614   {N_("Save Game"),            "<Ctrl>s",          "SaveGame",             SaveGameProc},
615   {N_("Save Position"),        "<Ctrl><Shift>s",   "SavePosition",         SavePositionProc},
616   {N_("Save Selected Games"),   NULL,              "SaveSelected",         SaveSelectedProc},
617   {N_("Save Games as Book"),    NULL,              "CreateBook",           CreateBookDelayed},
618   {"----",                      NULL,               NULL,                  NothingProc},
619   {N_("Mail Move"),             NULL,              "MailMove",             MailMoveEvent},
620   {N_("Reload CMail Message"),  NULL,              "ReloadCMailMessage",   ReloadCmailMsgProc},
621   {"----",                      NULL,               NULL,                  NothingProc},
622   {N_("Quit "),                "<Ctrl>q",          "Quit",                 QuitProc},
623   {NULL,                        NULL,               NULL,                  NULL}
624 };
625
626 MenuItem editMenu[] = {
627   {N_("Copy Game"),      "<Ctrl>c",        "CopyGame",      CopyGameProc},
628   {N_("Copy Position"),  "<Ctrl><Shift>c", "CopyPosition",  CopyPositionProc},
629   {N_("Copy Game List"),  NULL,            "CopyGameList",  CopyGameListProc},
630   {"----",                NULL,             NULL,           NothingProc},
631   {N_("Paste Game"),     "<Ctrl>v",        "PasteGame",     PasteGameProc},
632   {N_("Paste Position"), "<Ctrl><Shift>v", "PastePosition", PastePositionProc},
633   {"----",                NULL,             NULL,           NothingProc},
634   {N_("Edit Game"),      "<Ctrl>e",        "EditGame",      EditGameEvent},
635   {N_("Edit Position"),  "<Ctrl><Shift>e", "EditPosition",  EditPositionEvent},
636   {N_("Edit Tags"),       NULL,            "EditTags",      EditTagsProc},
637   {N_("Edit Comment"),    NULL,            "EditComment",   EditCommentProc},
638   {N_("Edit Book"),       NULL,            "EditBook",      EditBookEvent},
639   {"----",                NULL,             NULL,           NothingProc},
640   {N_("Revert"),         "Home",           "Revert",        RevertProc},
641   {N_("Annotate"),        NULL,            "Annotate",      AnnotateProc},
642   {N_("Truncate Game"),  "End",            "TruncateGame",  TruncateGameEvent},
643   {"----",                NULL,             NULL,           NothingProc},
644   {N_("Backward"),       "<Alt>Left",      "Backward",      BackwardEvent},
645   {N_("Forward"),        "<Alt>Right",     "Forward",       ForwardEvent},
646   {N_("Back to Start"),  "<Alt>Home",      "BacktoStart",   ToStartEvent},
647   {N_("Forward to End"), "<Alt>End",       "ForwardtoEnd",  ToEndEvent},
648   {NULL,                  NULL,             NULL,           NULL}
649 };
650
651 MenuItem viewMenu[] = {
652   {N_("Flip View"),         "F2",            "FlipView",        FlipViewProc,           CHECK},
653   {"----",                   NULL,            NULL,             NothingProc},
654   {N_("Engine Output"),     "<Alt><Shift>o", "EngineOutput",    EngineOutputProc,       CHECK},
655   {N_("Move History"),      "<Alt><Shift>h", "MoveHistory",     HistoryShowProc,        CHECK}, // [HGM] hist: activate 4.2.7 code
656   {N_("Evaluation Graph"),  "<Alt><Shift>e", "EvaluationGraph", EvalGraphProc,          CHECK},
657   {N_("Game List"),         "<Alt><Shift>g", "GameList",        ShowGameListProc,       CHECK},
658   {"----",                   NULL,            NULL,             NothingProc},
659   {N_("Tags"),               NULL,           "Tags",            EditTagsProc,           CHECK},
660   {N_("Comments"),           NULL,           "Comments",        EditCommentProc,        CHECK},
661   {N_("ICS Input Box"),      NULL,           "ICSInputBox",     IcsInputBoxProc,        CHECK},
662   {N_("ICS/Chat Console"),   NULL,           "OpenChatWindow",  ChatProc,               CHECK},
663   {N_("ICS text menu"),      NULL,           "ICStextmenu",     IcsTextProc,            CHECK},
664   {N_("Edit ICS menu..."),   NULL,           "EditTextMenu",    EditMenuProc},
665   {"----",                   NULL,            NULL,             NothingProc},
666   {N_("Edit Theme List..."), NULL,           "EditThemeList",   EditThemesProc},
667   {N_("Board..."),           NULL,           "Board",           BoardOptionsProc},
668   {N_("Fonts..."),           NULL,           "Fonts",           FontsProc},
669   {N_("Game List Tags..."),  NULL,           "GameListTags",    GameListOptionsProc},
670   {NULL,                     NULL,            NULL,             NULL}
671 };
672
673 MenuItem modeMenu[] = {
674   {N_("Machine White"),  "<Ctrl>w",        "MachineWhite",  MachineWhiteEvent,              RADIO },
675   {N_("Machine Black"),  "<Ctrl>b",        "MachineBlack",  MachineBlackEvent,              RADIO },
676   {N_("Two Machines"),   "<Ctrl>t",        "TwoMachines",   TwoMachinesEvent,               RADIO },
677   {N_("Analysis Mode"),  "<Ctrl>a",        "AnalysisMode",  (MenuProc*) AnalyzeModeEvent,   RADIO },
678   {N_("Analyze Game"),   "<Ctrl>g",        "AnalyzeFile",   AnalyzeFileEvent,               RADIO },
679   {N_("Edit Game"),      "<Ctrl>e",        "EditGame",      EditGameEvent,                  RADIO },
680   {N_("Edit Position"),  "<Ctrl><Shift>e", "EditPosition",  EditPositionEvent,              RADIO },
681   {N_("Training"),        NULL,            "Training",      TrainingEvent,                  RADIO },
682   {N_("ICS Client"),      NULL,            "ICSClient",     IcsClientEvent,                 RADIO },
683   {"----",                NULL,             NULL,           NothingProc},
684   {N_("Machine Match"),   NULL,            "MachineMatch",  MatchProc,                      CHECK },
685   {N_("Pause"),          "Pause",          "Pause",         PauseEvent,                     CHECK },
686   {NULL,                  NULL,             NULL,           NULL}
687 };
688
689 MenuItem actionMenu[] = {
690   {N_("Accept"),             "F3",   "Accept",             AcceptEvent},
691   {N_("Decline"),            "F4",   "Decline",            DeclineEvent},
692   {N_("Rematch"),            "F12",  "Rematch",            RematchEvent},
693   {"----",                    NULL,   NULL,                NothingProc},
694   {N_("Call Flag"),          "F5",   "CallFlag",           CallFlagEvent},
695   {N_("Draw"),               "F6",   "Draw",               DrawEvent},
696   {N_("Adjourn"),            "F7",   "Adjourn",            AdjournEvent},
697   {N_("Abort"),              "F8",   "Abort",              AbortEvent},
698   {N_("Resign"),             "F9",   "Resign",             ResignEvent},
699   {"----",                    NULL,   NULL,                NothingProc},
700   {N_("Stop Observing"),     "F10",  "StopObserving",      StopObservingEvent},
701   {N_("Stop Examining"),     "F11",  "StopExamining",      StopExaminingEvent},
702   {N_("Upload to Examine"),   NULL,  "UploadtoExamine",    UploadGameEvent},
703   {"----",                    NULL,   NULL,                NothingProc},
704   {N_("Adjudicate to White"), NULL,  "AdjudicatetoWhite",  AdjuWhiteProc},
705   {N_("Adjudicate to Black"), NULL,  "AdjudicatetoBlack",  AdjuBlackProc},
706   {N_("Adjudicate Draw"),     NULL,  "AdjudicateDraw",     AdjuDrawProc},
707   {NULL,                      NULL,   NULL,                NULL}
708 };
709
710 MenuItem engineMenu[100] = {
711   {N_("Edit Engine List..."),      NULL,     "EditEngList",      EditEngineProc},
712   {"----",                         NULL,      NULL,              NothingProc},
713   {N_("Load New 1st Engine..."),   NULL,     "LoadNew1stEngine", LoadEngine1Proc},
714   {N_("Load New 2nd Engine..."),   NULL,     "LoadNew2ndEngine", LoadEngine2Proc},
715   {"----",                         NULL,      NULL,              NothingProc},
716   {N_("Engine #1 Settings..."),    NULL,     "Engine#1Settings", FirstSettingsProc},
717   {N_("Engine #2 Settings..."),    NULL,     "Engine#2Settings", SecondSettingsProc},
718   {N_("Common Settings..."), "<Alt><Shift>u","CommonEngine",     UciMenuProc},
719   {"----",                         NULL,      NULL,              NothingProc},
720   {N_("Hint"),                     NULL,     "Hint",             HintEvent},
721   {N_("Book"),                     NULL,     "Book",             BookEvent},
722   {"----",                         NULL,      NULL,              NothingProc},
723   {N_("Move Now"),                "<Ctrl>m", "MoveNow",          MoveNowEvent},
724   {N_("Retract Move"),            "<Ctrl>x", "RetractMove",      RetractMoveEvent},
725   {NULL,                           NULL,      NULL,              NULL}
726 };
727
728 MenuItem optionsMenu[] = {
729 #ifdef OPTIONSDIALOG
730   {N_("General..."),              NULL,             "General",             OptionsProc},
731 #endif
732   {N_("Time Control..."),        "<Alt><Shift>t",   "TimeControl",         TimeControlProc},
733   {N_("Adjudications..."),       "<Alt><Shift>j",   "Adjudications",       EngineMenuProc},
734   {N_("ICS..."),                  NULL,             "ICS",                 IcsOptionsProc},
735   {N_("Tournament..."),           NULL,             "Match",               MatchOptionsProc},
736   {N_("Load Game..."),            NULL,             "LoadGame",            LoadOptionsProc},
737   {N_("Save Game..."),            NULL,             "SaveGame",            SaveOptionsProc},
738   {N_("Game List..."),            NULL,             "GameList",            GameListOptionsProc},
739   {N_("Sounds..."),               NULL,             "Sounds",              SoundOptionsProc},
740   {"----",                        NULL,              NULL,                 NothingProc},
741 #ifndef OPTIONSDIALOG
742   {N_("Always Queen"),           "<Ctrl><Shift>q",  "AlwaysQueen",         AlwaysQueenProc},
743   {N_("Animate Dragging"),        NULL,             "AnimateDragging",     AnimateDraggingProc},
744   {N_("Animate Moving"),         "<Ctrl><Shift>a",  "AnimateMoving",       AnimateMovingProc},
745   {N_("Auto Flag"),              "<Ctrl><Shift>f",  "AutoFlag",            AutoflagProc},
746   {N_("Auto Flip View"),          NULL,             "AutoFlipView",        AutoflipProc},
747   {N_("Blindfold"),               NULL,             "Blindfold",           BlindfoldProc},
748   {N_("Flash Moves"),             NULL,             "FlashMoves",          FlashMovesProc},
749 #if HIGHDRAG
750   {N_("Highlight Dragging"),      NULL,             "HighlightDragging",   HighlightDraggingProc},
751 #endif
752   {N_("Highlight Last Move"),     NULL,             "HighlightLastMove",   HighlightLastMoveProc},
753   {N_("Highlight With Arrow"),    NULL,             "HighlightWithArrow",  HighlightArrowProc},
754   {N_("Move Sound"),              NULL,             "MoveSound",           MoveSoundProc},
755   {N_("One-Click Moving"),        NULL,             "OneClickMoving",      OneClickProc},
756   {N_("Periodic Updates"),        NULL,             "PeriodicUpdates",     PeriodicUpdatesProc},
757   {N_("Ponder Next Move"),       "<Ctrl><Shift>p",  "PonderNextMove",      PonderNextMoveProc},
758   {N_("Popup Exit Message"),      NULL,             "PopupExitMessage",    PopupExitMessageProc},
759   {N_("Popup Move Errors"),       NULL,             "PopupMoveErrors",     PopupMoveErrorsProc},
760   {N_("Show Coords"),             NULL,             "ShowCoords",          ShowCoordsProc},
761   {N_("Hide Thinking"),          "<Ctrl><Shift>h",  "HideThinking",        HideThinkingProc},
762   {N_("Test Legality"),          "<Ctrl><Shift>l",  "TestLegality",        TestLegalityProc},
763   {"----",                        NULL,              NULL,                 NothingProc},
764 #endif
765   {N_("Save Settings Now"),       NULL,             "SaveSettingsNow",     SaveSettingsProc},
766   {N_("Save Settings on Exit"),   NULL,             "SaveSettingsonExit",  SaveOnExitProc,         CHECK },
767   {NULL,                          NULL,              NULL,                 NULL}
768 };
769
770 MenuItem helpMenu[] = {
771   {N_("Info XBoard"),            NULL,   "InfoXBoard",           InfoProc},
772   {N_("Man XBoard"),            "F1",    "ManXBoard",            ManProc},
773   {"----",                       NULL,    NULL,                  NothingProc},
774   {N_("XBoard Home Page"),       NULL,   "XBoardHomePage",       HomePageProc},
775   {N_("On-line User Guide"),     NULL,   "On-lineUserGuide",     GuideProc},
776   {N_("Development News"),       NULL,   "DevelopmentNews",      NewsPageProc},
777   {N_("e-Mail Bug Report"),      NULL,   "e-MailBugReport",      BugReportProc},
778   {"----",                       NULL,    NULL,                  NothingProc},
779   {N_("About XBoard"),           NULL,   "AboutXBoard",          AboutProc},
780   {NULL,                         NULL,    NULL,                  NULL}
781 };
782
783 MenuItem noMenu[] = {
784   { "", "<Alt>Next" ,"LoadNextGame", LoadNextGameProc },
785   { "", "<Alt>Prior" ,"LoadPrevGame", LoadPrevGameProc },
786   { "", NULL,"ReloadGame", ReloadGameProc },
787   { "", NULL,"ReloadPosition", ReloadPositionProc },
788 #ifndef OPTIONSDIALOG
789   { "", NULL,"AlwaysQueen", AlwaysQueenProc },
790   { "", NULL,"AnimateDragging", AnimateDraggingProc },
791   { "", NULL,"AnimateMoving", AnimateMovingProc },
792   { "", NULL,"Autoflag", AutoflagProc },
793   { "", NULL,"Autoflip", AutoflipProc },
794   { "", NULL,"Blindfold", BlindfoldProc },
795   { "", NULL,"FlashMoves", FlashMovesProc },
796 #if HIGHDRAG
797   { "", NULL,"HighlightDragging", HighlightDraggingProc },
798 #endif
799   { "", NULL,"HighlightLastMove", HighlightLastMoveProc },
800   { "", NULL,"MoveSound", MoveSoundProc },
801   { "", NULL,"PeriodicUpdates", PeriodicUpdatesProc },
802   { "", NULL,"PopupExitMessage", PopupExitMessageProc },
803   { "", NULL,"PopupMoveErrors", PopupMoveErrorsProc },
804   { "", NULL,"ShowCoords", ShowCoordsProc },
805   { "", NULL,"ShowThinking", ShowThinkingProc },
806   { "", NULL,"HideThinking", HideThinkingProc },
807   { "", NULL,"TestLegality", TestLegalityProc },
808 #endif
809   { "", NULL,"AboutGame", AboutGameEvent },
810   { "", "<Ctrl>d" ,"DebugProc", DebugProc },
811   { "", NULL,"Nothing", NothingProc },
812   {NULL, NULL, NULL, NULL}
813 };
814
815 Menu menuBar[] = {
816     {N_("File"),    "File", fileMenu},
817     {N_("Edit"),    "Edit", editMenu},
818     {N_("View"),    "View", viewMenu},
819     {N_("Mode"),    "Mode", modeMenu},
820     {N_("Action"),  "Action", actionMenu},
821     {N_("Engine"),  "Engine", engineMenu},
822     {N_("Options"), "Options", optionsMenu},
823     {N_("Help"),    "Help", helpMenu},
824     {NULL, NULL, NULL},
825     {   "",         "None", noMenu}
826 };
827
828 MenuItem *
829 MenuNameToItem (char *menuName)
830 {
831     int i=0;
832     char buf[MSG_SIZ], *p;
833     MenuItem *menuTab;
834     static MenuItem a = { NULL, NULL, NULL, NothingProc };
835     extern Option mainOptions[];
836     safeStrCpy(buf, menuName, MSG_SIZ);
837     p = strchr(buf, '.');
838     if(!p) menuTab = noMenu, p = menuName; else {
839         *p++ = NULLCHAR;
840         for(i=0; menuBar[i].name; i++)
841             if(!strcmp(buf, menuBar[i].name)) break;
842         if(!menuBar[i].name) return NULL; // main menu not found
843         menuTab = menuBar[i].mi;
844     }
845     if(*p == NULLCHAR) { a.handle = mainOptions[i+1].handle; return &a; } // main menu bar
846     for(i=0; menuTab[i].string; i++)
847         if(menuTab[i].ref && !strcmp(p, menuTab[i].ref)) return menuTab + i;
848     return NULL; // item not found
849 }
850
851 int firstEngineItem;
852
853 void
854 AppendEnginesToMenu (char *list)
855 {
856     int i=0;
857     char *p;
858     if(appData.icsActive || appData.recentEngines <= 0) return;
859     for(firstEngineItem=0; engineMenu[firstEngineItem].string; firstEngineItem++);
860     recentEngines = strdup(list);
861     while (*list) {
862         p = strchr(list, '\n'); if(p == NULL) break;
863         if(i == 0) engineMenu[firstEngineItem++].string = "----"; // at least one valid item to add
864         *p = 0;
865         if(firstEngineItem + i < 99)
866             engineMenu[firstEngineItem+i].string = strdup(list); // just set name; MenuProc stays NULL
867         i++; *p = '\n'; list = p + 1;
868     }
869 }
870
871 Enables icsEnables[] = {
872     { "File.MailMove", False },
873     { "File.ReloadCMailMessage", False },
874     { "Mode.MachineBlack", False },
875     { "Mode.MachineWhite", False },
876     { "Mode.AnalysisMode", False },
877     { "Mode.AnalyzeFile", False },
878     { "Mode.TwoMachines", False },
879     { "Mode.MachineMatch", False },
880 #if !ZIPPY
881     { "Engine.Hint", False },
882     { "Engine.Book", False },
883     { "Engine.MoveNow", False },
884 #ifndef OPTIONSDIALOG
885     { "PeriodicUpdates", False },
886     { "HideThinking", False },
887     { "PonderNextMove", False },
888 #endif
889 #endif
890     { "Engine.Engine#1Settings", False },
891     { "Engine.Engine#2Settings", False },
892     { "Engine.Load1stEngine", False },
893     { "Engine.Load2ndEngine", False },
894     { "Edit.Annotate", False },
895     { "Options.Match", False },
896     { NULL, False }
897 };
898
899 Enables ncpEnables[] = {
900     { "File.MailMove", False },
901     { "File.ReloadCMailMessage", False },
902     { "Mode.MachineWhite", False },
903     { "Mode.MachineBlack", False },
904     { "Mode.AnalysisMode", False },
905     { "Mode.AnalyzeFile", False },
906     { "Mode.TwoMachines", False },
907     { "Mode.MachineMatch", False },
908     { "Mode.ICSClient", False },
909     { "View.ICStextmenu", False },
910     { "View.ICSInputBox", False },
911     { "View.OpenChatWindow", False },
912     { "Action.", False },
913     { "Edit.Revert", False },
914     { "Edit.Annotate", False },
915     { "Engine.Engine#1Settings", False },
916     { "Engine.Engine#2Settings", False },
917     { "Engine.MoveNow", False },
918     { "Engine.RetractMove", False },
919     { "Options.ICS", False },
920 #ifndef OPTIONSDIALOG
921     { "Options.AutoFlag", False },
922     { "Options.AutoFlip View", False },
923 //    { "Options.ICSAlarm", False },
924     { "Options.MoveSound", False },
925     { "Options.HideThinking", False },
926     { "Options.PeriodicUpdates", False },
927     { "Options.PonderNextMove", False },
928 #endif
929     { "Engine.Hint", False },
930     { "Engine.Book", False },
931     { NULL, False }
932 };
933
934 Enables gnuEnables[] = {
935     { "Mode.ICSClient", False },
936     { "View.ICStextmenu", False },
937     { "View.ICSInputBox", False },
938     { "View.OpenChatWindow", False },
939     { "Action.Accept", False },
940     { "Action.Decline", False },
941     { "Action.Rematch", False },
942     { "Action.Adjourn", False },
943     { "Action.StopExamining", False },
944     { "Action.StopObserving", False },
945     { "Action.UploadtoExamine", False },
946     { "Edit.Revert", False },
947     { "Edit.Annotate", False },
948     { "Options.ICS", False },
949
950     /* The next two options rely on SetCmailMode being called *after*    */
951     /* SetGNUMode so that when GNU is being used to give hints these     */
952     /* menu options are still available                                  */
953
954     { "File.MailMove", False },
955     { "File.ReloadCMailMessage", False },
956     // [HGM] The following have been added to make a switch from ncp to GNU mode possible
957     { "Mode.MachineWhite", True },
958     { "Mode.MachineBlack", True },
959     { "Mode.AnalysisMode", True },
960     { "Mode.AnalyzeFile", True },
961     { "Mode.TwoMachines", True },
962     { "Mode.MachineMatch", True },
963     { "Engine.Engine#1Settings", True },
964     { "Engine.Engine#2Settings", True },
965     { "Engine.Hint", True },
966     { "Engine.Book", True },
967     { "Engine.MoveNow", True },
968     { "Engine.RetractMove", True },
969     { "Action.", True },
970     { NULL, False }
971 };
972
973 Enables cmailEnables[] = {
974     { "Action.", True },
975     { "Action.CallFlag", False },
976     { "Action.Draw", True },
977     { "Action.Adjourn", False },
978     { "Action.Abort", False },
979     { "Action.StopObserving", False },
980     { "Action.StopExamining", False },
981     { "File.MailMove", True },
982     { "File.ReloadCMailMessage", True },
983     { NULL, False }
984 };
985
986 Enables trainingOnEnables[] = {
987   { "Edit.EditComment", False },
988   { "Mode.Pause", False },
989   { "Edit.Forward", False },
990   { "Edit.Backward", False },
991   { "Edit.ForwardtoEnd", False },
992   { "Edit.BacktoStart", False },
993   { "Engine.MoveNow", False },
994   { "Edit.TruncateGame", False },
995   { NULL, False }
996 };
997
998 Enables trainingOffEnables[] = {
999   { "Edit.EditComment", True },
1000   { "Mode.Pause", True },
1001   { "Edit.Forward", True },
1002   { "Edit.Backward", True },
1003   { "Edit.ForwardtoEnd", True },
1004   { "Edit.BacktoStart", True },
1005   { "Engine.MoveNow", True },
1006   { "Engine.TruncateGame", True },
1007   { NULL, False }
1008 };
1009
1010 Enables machineThinkingEnables[] = {
1011   { "File.LoadGame", False },
1012 //  { "LoadNextGame", False },
1013 //  { "LoadPreviousGame", False },
1014 //  { "ReloadSameGame", False },
1015   { "Edit.PasteGame", False },
1016   { "File.LoadPosition", False },
1017 //  { "LoadNextPosition", False },
1018 //  { "LoadPreviousPosition", False },
1019 //  { "ReloadSamePosition", False },
1020   { "Edit.PastePosition", False },
1021   { "Mode.MachineWhite", False },
1022   { "Mode.MachineBlack", False },
1023   { "Mode.TwoMachines", False },
1024 //  { "MachineMatch", False },
1025   { "Engine.RetractMove", False },
1026   { NULL, False }
1027 };
1028
1029 Enables userThinkingEnables[] = {
1030   { "File.LoadGame", True },
1031 //  { "LoadNextGame", True },
1032 //  { "LoadPreviousGame", True },
1033 //  { "ReloadSameGame", True },
1034   { "Edit.PasteGame", True },
1035   { "File.LoadPosition", True },
1036 //  { "LoadNextPosition", True },
1037 //  { "LoadPreviousPosition", True },
1038 //  { "ReloadSamePosition", True },
1039   { "Edit.PastePosition", True },
1040   { "Mode.MachineWhite", True },
1041   { "Mode.MachineBlack", True },
1042   { "Mode.TwoMachines", True },
1043 //  { "MachineMatch", True },
1044   { "Engine.RetractMove", True },
1045   { NULL, False }
1046 };
1047
1048 void
1049 SetICSMode ()
1050 {
1051   SetMenuEnables(icsEnables);
1052
1053 #if ZIPPY
1054   if (appData.zippyPlay && !appData.noChessProgram) { /* [DM] icsEngineAnalyze */
1055      EnableNamedMenuItem("Mode.AnalysisMode", True);
1056      EnableNamedMenuItem("Engine.Engine#1Settings", True);
1057   }
1058 #endif
1059 }
1060
1061 void
1062 SetNCPMode ()
1063 {
1064   SetMenuEnables(ncpEnables);
1065 }
1066
1067 void
1068 SetGNUMode ()
1069 {
1070   SetMenuEnables(gnuEnables);
1071 }
1072
1073 void
1074 SetCmailMode ()
1075 {
1076   SetMenuEnables(cmailEnables);
1077 }
1078
1079 void
1080 SetTrainingModeOn ()
1081 {
1082   SetMenuEnables(trainingOnEnables);
1083   if (appData.showButtonBar) {
1084     EnableButtonBar(False);
1085   }
1086   CommentPopDown();
1087 }
1088
1089 void
1090 SetTrainingModeOff ()
1091 {
1092   SetMenuEnables(trainingOffEnables);
1093   if (appData.showButtonBar) {
1094     EnableButtonBar(True);
1095   }
1096 }
1097
1098 void
1099 SetUserThinkingEnables ()
1100 {
1101   if (appData.noChessProgram) return;
1102   SetMenuEnables(userThinkingEnables);
1103 }
1104
1105 void
1106 SetMachineThinkingEnables ()
1107 {
1108   if (appData.noChessProgram) return;
1109   SetMenuEnables(machineThinkingEnables);
1110   switch (gameMode) {
1111   case MachinePlaysBlack:
1112   case MachinePlaysWhite:
1113   case TwoMachinesPlay:
1114     EnableNamedMenuItem(ModeToWidgetName(gameMode), True);
1115     break;
1116   default:
1117     break;
1118   }
1119 }
1120
1121 void
1122 GreyRevert (Boolean grey)
1123 {
1124     EnableNamedMenuItem("Edit.Revert", !grey);
1125     EnableNamedMenuItem("Edit.Annotate", !grey);
1126 }
1127
1128 char *
1129 ModeToWidgetName (GameMode mode)
1130 {
1131     switch (mode) {
1132       case BeginningOfGame:
1133         if (appData.icsActive)
1134           return "Mode.ICSClient";
1135         else if (appData.noChessProgram ||
1136                  *appData.cmailGameName != NULLCHAR)
1137           return "Mode.EditGame";
1138         else
1139           return "Mode.MachineBlack";
1140       case MachinePlaysBlack:
1141         return "Mode.MachineBlack";
1142       case MachinePlaysWhite:
1143         return "Mode.MachineWhite";
1144       case AnalyzeMode:
1145         return "Mode.AnalysisMode";
1146       case AnalyzeFile:
1147         return "Mode.AnalyzeFile";
1148       case TwoMachinesPlay:
1149         return "Mode.TwoMachines";
1150       case EditGame:
1151         return "Mode.EditGame";
1152       case PlayFromGameFile:
1153         return "File.LoadGame";
1154       case EditPosition:
1155         return "Mode.EditPosition";
1156       case Training:
1157         return "Mode.Training";
1158       case IcsPlayingWhite:
1159       case IcsPlayingBlack:
1160       case IcsObserving:
1161       case IcsIdle:
1162       case IcsExamining:
1163         return "Mode.ICSClient";
1164       default:
1165       case EndOfGame:
1166         return NULL;
1167     }
1168 }
1169
1170 static void
1171 InstallNewEngine (char *command, char *dir, char *variants, char *protocol)
1172 { // install the given engine in XBoard's -firstChessProgramNames
1173     char buf[MSG_SIZ], *quote = "";
1174     if(strchr(command, ' ')) { // quoting needed
1175         if(!strchr(command, '"')) quote = "\""; else
1176         if(!strchr(command, '\'')) quote = "'"; else {
1177             printf("Could not auto-install %s\n", command); // too complex
1178         }
1179     }
1180     // construct engine line, with optional -fd and -fUCI arguments
1181     snprintf(buf, MSG_SIZ, "%s%s%s", quote, command, quote);
1182     if(strcmp(dir, "") && strcmp(dir, "."))
1183         snprintf(buf + strlen(buf), MSG_SIZ - strlen(buf), " -fd %s", dir);
1184     if(!strcmp(protocol, "uci"))
1185         snprintf(buf + strlen(buf), MSG_SIZ - strlen(buf), " -fUCI");
1186     if(strstr(firstChessProgramNames, buf)) return; // avoid duplicats
1187     // append line
1188     quote = malloc(strlen(firstChessProgramNames) + strlen(buf) + 2);
1189     sprintf(quote, "%s%s\n", firstChessProgramNames, buf);
1190     FREE(firstChessProgramNames); firstChessProgramNames = quote;
1191 }
1192
1193 #ifdef HAVE_DIRENT_H
1194 #include <dirent.h>
1195 #else
1196 #include <sys/dir.h>
1197 #define dirent direct
1198 #endif
1199
1200 static void
1201 InstallFromDir (char *dirName, char *protocol, char *settingsFile)
1202 {   // scan system for new plugin specs in given directory
1203     DIR *dir;
1204     struct dirent *dp;
1205     struct stat statBuf;
1206     time_t lastSaved = 0;
1207     char buf[1024];
1208
1209     if(!stat(settingsFile, &statBuf)) lastSaved = statBuf.st_mtime;
1210     snprintf(buf, 1024, "%s/%s", dirName, protocol);
1211
1212     if(!(dir = opendir(buf))) return;
1213     while( (dp = readdir(dir))) {
1214         time_t installed = 0;
1215         if(!strstr(dp->d_name, ".eng")) continue; // to suppress . and ..
1216         snprintf(buf, 1024, "%s/%s/%s", dirName, protocol, dp->d_name);
1217         if(!stat(buf, &statBuf)) installed = statBuf.st_mtime;
1218         if(lastSaved == 0 || (int) (installed - lastSaved) > 0) { // first time we see it
1219             FILE *f = fopen(buf, "r");
1220             if(f) { // read the plugin-specs
1221                 char engineCommand[1024], engineDir[1024], variants[1024];
1222                 char bad=0, dummy, *engineCom = engineCommand;
1223                 int major, minor;
1224                 if(fscanf(f, "plugin spec %d.%d%c", &major, &minor, &dummy) != 3 ||
1225                    fscanf(f, "%[^\n]%c", engineCommand, &dummy) != 2 ||
1226                    fscanf(f, "%[^\n]%c", variants, &dummy) != 2) bad = 1;
1227                 fclose(f);
1228                 if(bad) continue;
1229                 // uncomment following two lines for chess-only installs
1230 //              if(!(p = strstr(variants, "chess")) ||
1231 //                   p != variants && p[-1] != ',' || p[5] && p[5] != ',') continue;
1232                 // split off engine working directory (if any)
1233                 strcpy(engineDir, "");
1234                 if(sscanf(engineCommand, "cd %[^;];%c", engineDir, &dummy) == 2)
1235                     engineCom = engineCommand + strlen(engineDir) + 4;
1236                 InstallNewEngine(engineCom, engineDir, variants, protocol);
1237             }
1238         }
1239     }
1240     closedir(dir);
1241 }
1242
1243 static void
1244 AutoInstallProtocol (char *settingsFile, char *protocol)
1245 {   // install new engines for given protocol (both from package and source)
1246     InstallFromDir("/usr/local/share/games/plugins", protocol, settingsFile);
1247     InstallFromDir("/usr/share/games/plugins", protocol, settingsFile);
1248 }
1249
1250 void
1251 AutoInstall (char *settingsFile)
1252 {   // install all new XBoard and UCI engines
1253     AutoInstallProtocol(settingsFile, "xboard");
1254     AutoInstallProtocol(settingsFile, "uci");
1255 }
1256
1257 void
1258 InitMenuMarkers()
1259 {
1260 #ifndef OPTIONSDIALOG
1261     if (appData.alwaysPromoteToQueen) {
1262         MarkMenuItem("Options.Always Queen", True);
1263     }
1264     if (appData.animateDragging) {
1265         MarkMenuItem("Options.Animate Dragging", True);
1266     }
1267     if (appData.animate) {
1268         MarkMenuItem("Options.Animate Moving", True);
1269     }
1270     if (appData.autoCallFlag) {
1271         MarkMenuItem("Options.Auto Flag", True);
1272     }
1273     if (appData.autoFlipView) {
1274         XtSetValues(XtNameToWidget(menuBarWidget,"Options.Auto Flip View", True);
1275     }
1276     if (appData.blindfold) {
1277         MarkMenuItem("Options.Blindfold", True);
1278     }
1279     if (appData.flashCount > 0) {
1280         MarkMenuItem("Options.Flash Moves", True);
1281     }
1282 #if HIGHDRAG
1283     if (appData.highlightDragging) {
1284         MarkMenuItem("Options.Highlight Dragging", True);
1285     }
1286 #endif
1287     if (appData.highlightLastMove) {
1288         MarkMenuItem("Options.Highlight Last Move", True);
1289     }
1290     if (appData.highlightMoveWithArrow) {
1291         MarkMenuItem("Options.Arrow", True);
1292     }
1293 //    if (appData.icsAlarm) {
1294 //      MarkMenuItem("Options.ICS Alarm", True);
1295 //    }
1296     if (appData.ringBellAfterMoves) {
1297         MarkMenuItem("Options.Move Sound", True);
1298     }
1299     if (appData.oneClick) {
1300         MarkMenuItem("Options.OneClick", True);
1301     }
1302     if (appData.periodicUpdates) {
1303         MarkMenuItem("Options.Periodic Updates", True);
1304     }
1305     if (appData.ponderNextMove) {
1306         MarkMenuItem("Options.Ponder Next Move", True);
1307     }
1308     if (appData.popupExitMessage) {
1309         MarkMenuItem("Options.Popup Exit Message", True);
1310     }
1311     if (appData.popupMoveErrors) {
1312         MarkMenuItem("Options.Popup Move Errors", True);
1313     }
1314 //    if (appData.premove) {
1315 //      MarkMenuItem("Options.Premove", True);
1316 //    }
1317     if (appData.showCoords) {
1318         MarkMenuItem("Options.Show Coords", True);
1319     }
1320     if (appData.hideThinkingFromHuman) {
1321         MarkMenuItem("Options.Hide Thinking", True);
1322     }
1323     if (appData.testLegality) {
1324         MarkMenuItem("Options.Test Legality", True);
1325     }
1326 #endif
1327     if (saveSettingsOnExit) {
1328         MarkMenuItem("Options.SaveSettingsonExit", True);
1329     }
1330     EnableNamedMenuItem("File.SaveSelected", False);
1331
1332     // all XBoard builds get here, but not WinBoard...
1333     if(*appData.autoInstall) AutoInstall(settingsFileName);
1334 }