Fix Seirawan gating at Rook square in PGN castling moves
[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     if(*engineListFile) ParseSettingsFile(engineListFile, &engineListFile);
391     EditAnyPopUp(firstChessProgramNames, &firstChessProgramNames, _("Registered Engines"));
392 }
393
394 void
395 EditThemesProc ()
396 {
397     EditAnyPopUp(appData.themeNames, &appData.themeNames, _("Predefined Themes"));
398 }
399
400 void
401 EditMenuProc ()
402 {
403     EditAnyPopUp(icsTextMenuString, &icsTextMenuString, _("ICS Text-Menu Definition"));
404 }
405
406 void
407 MuteProc ()
408 {
409     mute = !mute;
410     MarkMenuItem("Options.Mute", mute);
411 }
412
413 void
414 NothingProc ()
415 {
416     return;
417 }
418
419 #ifdef OPTIONSDIALOG
420 #   define MARK_MENU_ITEM(X,Y)
421 #else
422 #   define MARK_MENU_ITEM(X,Y) MarkMenuItem(X, Y)
423 #endif
424
425 void
426 PonderNextMoveProc ()
427 {
428   PonderNextMoveEvent(!appData.ponderNextMove);
429   MARK_MENU_ITEM("Options.PonderNextMove", appData.ponderNextMove);
430 }
431
432 void
433 AlwaysQueenProc ()
434 {
435     appData.alwaysPromoteToQueen = !appData.alwaysPromoteToQueen;
436     MARK_MENU_ITEM("Options.AlwaysQueen", appData.alwaysPromoteToQueen);
437 }
438
439 void
440 AnimateDraggingProc ()
441 {
442     appData.animateDragging = !appData.animateDragging;
443
444     if (appData.animateDragging) CreateAnimVars();
445     MARK_MENU_ITEM("Options.AnimateDragging", appData.animateDragging);
446 }
447
448 void
449 AnimateMovingProc ()
450 {
451     appData.animate = !appData.animate;
452     if (appData.animate) CreateAnimVars();
453     MARK_MENU_ITEM("Options.AnimateMoving", appData.animate);
454 }
455
456 void
457 AutoflagProc ()
458 {
459     appData.autoCallFlag = !appData.autoCallFlag;
460     MARK_MENU_ITEM("Options.AutoFlag", appData.autoCallFlag);
461 }
462
463 void
464 AutoflipProc ()
465 {
466     appData.autoFlipView = !appData.autoFlipView;
467     MARK_MENU_ITEM("Options.AutoFlipView", appData.autoFlipView);
468 }
469
470 void
471 BlindfoldProc ()
472 {
473     appData.blindfold = !appData.blindfold;
474     MARK_MENU_ITEM("Options.Blindfold", appData.blindfold);
475     DrawPosition(True, NULL);
476 }
477
478 void
479 TestLegalityProc ()
480 {
481     appData.testLegality = !appData.testLegality;
482     MARK_MENU_ITEM("Options.TestLegality", appData.testLegality);
483 }
484
485
486 void
487 FlashMovesProc ()
488 {
489     if (appData.flashCount == 0) {
490         appData.flashCount = 3;
491     } else {
492         appData.flashCount = -appData.flashCount;
493     }
494     MARK_MENU_ITEM("Options.FlashMoves", appData.flashCount > 0);
495 }
496
497 #if HIGHDRAG
498 void
499 HighlightDraggingProc ()
500 {
501     appData.highlightDragging = !appData.highlightDragging;
502     MARK_MENU_ITEM("Options.HighlightDragging", appData.highlightDragging);
503 }
504 #endif
505
506 void
507 HighlightLastMoveProc ()
508 {
509     appData.highlightLastMove = !appData.highlightLastMove;
510     MARK_MENU_ITEM("Options.HighlightLastMove", appData.highlightLastMove);
511 }
512
513 void
514 HighlightArrowProc ()
515 {
516     appData.highlightMoveWithArrow = !appData.highlightMoveWithArrow;
517     MARK_MENU_ITEM("Options.HighlightWithArrow", appData.highlightMoveWithArrow);
518 }
519
520 void
521 IcsAlarmProc ()
522 {
523     appData.icsAlarm = !appData.icsAlarm;
524 //    MARK_MENU_ITEM("Options.ICSAlarm", appData.icsAlarm);
525 }
526
527 void
528 MoveSoundProc ()
529 {
530     appData.ringBellAfterMoves = !appData.ringBellAfterMoves;
531     MARK_MENU_ITEM("Options.MoveSound", appData.ringBellAfterMoves);
532 }
533
534 void
535 OneClickProc ()
536 {
537     appData.oneClick = !appData.oneClick;
538     MARK_MENU_ITEM("Options.OneClickMoving", appData.oneClick);
539 }
540
541 void
542 PeriodicUpdatesProc ()
543 {
544     PeriodicUpdatesEvent(!appData.periodicUpdates);
545     MARK_MENU_ITEM("Options.PeriodicUpdates", appData.periodicUpdates);
546 }
547
548 void
549 PopupExitMessageProc ()
550 {
551     appData.popupExitMessage = !appData.popupExitMessage;
552     MARK_MENU_ITEM("Options.PopupExitMessage", appData.popupExitMessage);
553 }
554
555 void
556 PopupMoveErrorsProc ()
557 {
558     appData.popupMoveErrors = !appData.popupMoveErrors;
559     MARK_MENU_ITEM("Options.PopupMoveErrors", appData.popupMoveErrors);
560 }
561
562 void
563 PremoveProc ()
564 {
565     appData.premove = !appData.premove;
566 //    MARK_MENU_ITEM("Options.Premove", appData.premove);
567 }
568
569 void
570 ShowCoordsProc ()
571 {
572     appData.showCoords = !appData.showCoords;
573     MARK_MENU_ITEM("Options.ShowCoords", appData.showCoords);
574     DrawPosition(True, NULL);
575 }
576
577 void
578 ShowThinkingProc ()
579 {
580     appData.showThinking = !appData.showThinking; // [HGM] thinking: taken out of ShowThinkingEvent
581     ShowThinkingEvent();
582 }
583
584 void
585 HideThinkingProc ()
586 {
587   appData.hideThinkingFromHuman = !appData.hideThinkingFromHuman; // [HGM] thinking: taken out of ShowThinkingEvent
588   ShowThinkingEvent();
589
590   MARK_MENU_ITEM("Options.HideThinking", appData.hideThinkingFromHuman);
591 }
592
593 void
594 CreateBookDelayed ()
595 {
596   ScheduleDelayedEvent(CreateBookEvent, 50);
597 }
598
599 void
600 SaveSelectedProc ()
601 {
602   FileNamePopUp(_("Save game file name?"),
603                   "",
604                   ".pgn",
605                   SaveSelected, "a");
606 }
607
608 /*
609  *  Menu definition tables
610  */
611
612 MenuItem fileMenu[] = {
613   {N_("New Game"),             "<Ctrl>n",          "NewGame",              ResetGameEvent},
614   {N_("New Shuffle Game..."),   NULL,              "NewShuffleGame",       ShuffleMenuProc},
615   {N_("New Variant..."),       "<Alt><Shift>v",    "NewVariant",           NewVariantProc},// [HGM] variant: not functional yet
616   {"----",                      NULL,               NULL,                  NothingProc},
617   {N_("Load Game"),            "<Ctrl>o",          "LoadGame",             LoadGameProc,           CHECK},
618   {N_("Load Position"),        "<Ctrl><Shift>o",   "LoadPosition",         LoadPositionProc},
619   {N_("Next Position"),        "<Shift>Page_Down", "LoadNextPosition",     LoadNextPositionProc},
620   {N_("Prev Position"),        "<Shift>Page_Up",   "LoadPreviousPosition", LoadPrevPositionProc},
621   {"----",                      NULL,               NULL,                  NothingProc},
622   {N_("Save Game"),            "<Ctrl>s",          "SaveGame",             SaveGameProc},
623   {N_("Save Position"),        "<Ctrl><Shift>s",   "SavePosition",         SavePositionProc},
624   {N_("Save Selected Games"),   NULL,              "SaveSelected",         SaveSelectedProc},
625   {N_("Save Games as Book"),    NULL,              "CreateBook",           CreateBookDelayed},
626   {"----",                      NULL,               NULL,                  NothingProc},
627   {N_("Mail Move"),             NULL,              "MailMove",             MailMoveEvent},
628   {N_("Reload CMail Message"),  NULL,              "ReloadCMailMessage",   ReloadCmailMsgProc},
629   {"----",                      NULL,               NULL,                  NothingProc},
630   {N_("Quit "),                "<Ctrl>q",          "Quit",                 QuitProc},
631   {NULL,                        NULL,               NULL,                  NULL}
632 };
633
634 MenuItem editMenu[] = {
635   {N_("Copy Game"),      "<Ctrl>c",        "CopyGame",      CopyGameProc},
636   {N_("Copy Position"),  "<Ctrl><Shift>c", "CopyPosition",  CopyPositionProc},
637   {N_("Copy Game List"),  NULL,            "CopyGameList",  CopyGameListProc},
638   {"----",                NULL,             NULL,           NothingProc},
639   {N_("Paste Game"),     "<Ctrl>v",        "PasteGame",     PasteGameProc},
640   {N_("Paste Position"), "<Ctrl><Shift>v", "PastePosition", PastePositionProc},
641   {"----",                NULL,             NULL,           NothingProc},
642   {N_("Edit Game"),      "<Ctrl>e",        "EditGame",      EditGameEvent},
643   {N_("Edit Position"),  "<Ctrl><Shift>e", "EditPosition",  EditPositionEvent},
644   {N_("Edit Tags"),       NULL,            "EditTags",      EditTagsProc},
645   {N_("Edit Comment"),    NULL,            "EditComment",   EditCommentProc},
646   {N_("Edit Book"),       NULL,            "EditBook",      EditBookEvent},
647   {"----",                NULL,             NULL,           NothingProc},
648   {N_("Revert"),         "Home",           "Revert",        RevertProc},
649   {N_("Annotate"),        NULL,            "Annotate",      AnnotateProc},
650   {N_("Truncate Game"),  "End",            "TruncateGame",  TruncateGameEvent},
651   {"----",                NULL,             NULL,           NothingProc},
652   {N_("Backward"),       "<Alt>Left",      "Backward",      BackwardEvent},
653   {N_("Forward"),        "<Alt>Right",     "Forward",       ForwardEvent},
654   {N_("Back to Start"),  "<Alt>Home",      "BacktoStart",   ToStartEvent},
655   {N_("Forward to End"), "<Alt>End",       "ForwardtoEnd",  ToEndEvent},
656   {NULL,                  NULL,             NULL,           NULL}
657 };
658
659 MenuItem viewMenu[] = {
660   {N_("Flip View"),         "F2",            "FlipView",        FlipViewProc,           CHECK},
661   {"----",                   NULL,            NULL,             NothingProc},
662   {N_("Engine Output"),     "<Alt><Shift>o", "EngineOutput",    EngineOutputProc,       CHECK},
663   {N_("Move History"),      "<Alt><Shift>h", "MoveHistory",     HistoryShowProc,        CHECK}, // [HGM] hist: activate 4.2.7 code
664   {N_("Evaluation Graph"),  "<Alt><Shift>e", "EvaluationGraph", EvalGraphProc,          CHECK},
665   {N_("Game List"),         "<Alt><Shift>g", "GameList",        ShowGameListProc,       CHECK},
666   {"----",                   NULL,            NULL,             NothingProc},
667   {N_("Tags"),               NULL,           "Tags",            EditTagsProc,           CHECK},
668   {N_("Comments"),           NULL,           "Comments",        EditCommentProc,        CHECK},
669   {N_("ICS Input Box"),      NULL,           "ICSInputBox",     IcsInputBoxProc,        CHECK},
670   {N_("ICS/Chat Console"),   NULL,           "OpenChatWindow",  ChatProc,               CHECK},
671   {N_("ICS text menu"),      NULL,           "ICStextmenu",     IcsTextProc,            CHECK},
672   {N_("Edit ICS menu..."),   NULL,           "EditTextMenu",    EditMenuProc},
673   {"----",                   NULL,            NULL,             NothingProc},
674   {N_("Edit Theme List..."), NULL,           "EditThemeList",   EditThemesProc},
675   {N_("Board..."),           NULL,           "Board",           BoardOptionsProc},
676   {N_("Fonts..."),           NULL,           "Fonts",           FontsProc},
677   {N_("Game List Tags..."),  NULL,           "GameListTags",    GameListOptionsProc},
678   {NULL,                     NULL,            NULL,             NULL}
679 };
680
681 MenuItem modeMenu[] = {
682   {N_("Machine White"),  "<Ctrl>w",        "MachineWhite",  MachineWhiteEvent,              RADIO },
683   {N_("Machine Black"),  "<Ctrl>b",        "MachineBlack",  MachineBlackEvent,              RADIO },
684   {N_("Two Machines"),   "<Ctrl>t",        "TwoMachines",   TwoMachinesEvent,               RADIO },
685   {N_("Analysis Mode"),  "<Ctrl>a",        "AnalysisMode",  (MenuProc*) AnalyzeModeEvent,   RADIO },
686   {N_("Analyze Game"),   "<Ctrl>g",        "AnalyzeFile",   AnalyzeFileEvent,               RADIO },
687   {N_("Edit Game"),      "<Ctrl>e",        "EditGame",      EditGameEvent,                  RADIO },
688   {N_("Edit Position"),  "<Ctrl><Shift>e", "EditPosition",  EditPositionEvent,              RADIO },
689   {N_("Training"),        NULL,            "Training",      TrainingEvent,                  RADIO },
690   {N_("ICS Client"),      NULL,            "ICSClient",     IcsClientEvent,                 RADIO },
691   {"----",                NULL,             NULL,           NothingProc},
692   {N_("Machine Match"),   NULL,            "MachineMatch",  MatchProc,                      CHECK },
693   {N_("Pause"),          "Pause",          "Pause",         PauseEvent,                     CHECK },
694   {NULL,                  NULL,             NULL,           NULL}
695 };
696
697 MenuItem actionMenu[] = {
698   {N_("Accept"),             "F3",   "Accept",             AcceptEvent},
699   {N_("Decline"),            "F4",   "Decline",            DeclineEvent},
700   {N_("Rematch"),            "F12",  "Rematch",            RematchEvent},
701   {"----",                    NULL,   NULL,                NothingProc},
702   {N_("Call Flag"),          "F5",   "CallFlag",           CallFlagEvent},
703   {N_("Draw"),               "F6",   "Draw",               DrawEvent},
704   {N_("Adjourn"),            "F7",   "Adjourn",            AdjournEvent},
705   {N_("Abort"),              "F8",   "Abort",              AbortEvent},
706   {N_("Resign"),             "F9",   "Resign",             ResignEvent},
707   {"----",                    NULL,   NULL,                NothingProc},
708   {N_("Stop Observing"),     "F10",  "StopObserving",      StopObservingEvent},
709   {N_("Stop Examining"),     "F11",  "StopExamining",      StopExaminingEvent},
710   {N_("Upload to Examine"),   NULL,  "UploadtoExamine",    UploadGameEvent},
711   {"----",                    NULL,   NULL,                NothingProc},
712   {N_("Adjudicate to White"), NULL,  "AdjudicatetoWhite",  AdjuWhiteProc},
713   {N_("Adjudicate to Black"), NULL,  "AdjudicatetoBlack",  AdjuBlackProc},
714   {N_("Adjudicate Draw"),     NULL,  "AdjudicateDraw",     AdjuDrawProc},
715   {NULL,                      NULL,   NULL,                NULL}
716 };
717
718 MenuItem engineMenu[100] = {
719   {N_("Edit Engine List..."),      NULL,     "EditEngList",      EditEngineProc},
720   {"----",                         NULL,      NULL,              NothingProc},
721   {N_("Load New 1st Engine..."),   NULL,     "LoadNew1stEngine", LoadEngine1Proc},
722   {N_("Load New 2nd Engine..."),   NULL,     "LoadNew2ndEngine", LoadEngine2Proc},
723   {"----",                         NULL,      NULL,              NothingProc},
724   {N_("Engine #1 Settings..."),    NULL,     "Engine#1Settings", FirstSettingsProc},
725   {N_("Engine #2 Settings..."),    NULL,     "Engine#2Settings", SecondSettingsProc},
726   {N_("Common Settings..."), "<Alt><Shift>u","CommonEngine",     UciMenuProc},
727   {"----",                         NULL,      NULL,              NothingProc},
728   {N_("Hint"),                     NULL,     "Hint",             HintEvent},
729   {N_("Book"),                     NULL,     "Book",             BookEvent},
730   {"----",                         NULL,      NULL,              NothingProc},
731   {N_("Move Now"),                "<Ctrl>m", "MoveNow",          MoveNowEvent},
732   {N_("Retract Move"),            "<Ctrl>x", "RetractMove",      RetractMoveEvent},
733   {NULL,                           NULL,      NULL,              NULL}
734 };
735
736 MenuItem optionsMenu[] = {
737   {N_("Mute all Sounds"),         NULL,             "Mute",                MuteProc,         CHECK},
738   {"----",                        NULL,              NULL,                 NothingProc},
739 #ifdef OPTIONSDIALOG
740   {N_("General..."),              NULL,             "General",             OptionsProc},
741 #endif
742   {N_("Time Control..."),        "<Alt><Shift>t",   "TimeControl",         TimeControlProc},
743   {N_("Adjudications..."),       "<Alt><Shift>j",   "Adjudications",       EngineMenuProc},
744   {N_("ICS..."),                  NULL,             "ICS",                 IcsOptionsProc},
745   {N_("Tournament..."),           NULL,             "Match",               MatchOptionsProc},
746   {N_("Load Game..."),            NULL,             "LoadGame",            LoadOptionsProc},
747   {N_("Save Game..."),            NULL,             "SaveGame",            SaveOptionsProc},
748   {N_("Game List..."),            NULL,             "GameList",            GameListOptionsProc},
749   {N_("Sounds..."),               NULL,             "Sounds",              SoundOptionsProc},
750   {"----",                        NULL,              NULL,                 NothingProc},
751 #ifndef OPTIONSDIALOG
752   {N_("Always Queen"),           "<Ctrl><Shift>q",  "AlwaysQueen",         AlwaysQueenProc},
753   {N_("Animate Dragging"),        NULL,             "AnimateDragging",     AnimateDraggingProc},
754   {N_("Animate Moving"),         "<Ctrl><Shift>a",  "AnimateMoving",       AnimateMovingProc},
755   {N_("Auto Flag"),              "<Ctrl><Shift>f",  "AutoFlag",            AutoflagProc},
756   {N_("Auto Flip View"),          NULL,             "AutoFlipView",        AutoflipProc},
757   {N_("Blindfold"),               NULL,             "Blindfold",           BlindfoldProc},
758   {N_("Flash Moves"),             NULL,             "FlashMoves",          FlashMovesProc},
759 #if HIGHDRAG
760   {N_("Highlight Dragging"),      NULL,             "HighlightDragging",   HighlightDraggingProc},
761 #endif
762   {N_("Highlight Last Move"),     NULL,             "HighlightLastMove",   HighlightLastMoveProc},
763   {N_("Highlight With Arrow"),    NULL,             "HighlightWithArrow",  HighlightArrowProc},
764   {N_("Move Sound"),              NULL,             "MoveSound",           MoveSoundProc},
765   {N_("One-Click Moving"),        NULL,             "OneClickMoving",      OneClickProc},
766   {N_("Periodic Updates"),        NULL,             "PeriodicUpdates",     PeriodicUpdatesProc},
767   {N_("Ponder Next Move"),       "<Ctrl><Shift>p",  "PonderNextMove",      PonderNextMoveProc},
768   {N_("Popup Exit Message"),      NULL,             "PopupExitMessage",    PopupExitMessageProc},
769   {N_("Popup Move Errors"),       NULL,             "PopupMoveErrors",     PopupMoveErrorsProc},
770   {N_("Show Coords"),             NULL,             "ShowCoords",          ShowCoordsProc},
771   {N_("Hide Thinking"),          "<Ctrl><Shift>h",  "HideThinking",        HideThinkingProc},
772   {N_("Test Legality"),          "<Ctrl><Shift>l",  "TestLegality",        TestLegalityProc},
773   {"----",                        NULL,              NULL,                 NothingProc},
774 #endif
775   {N_("Save Settings Now"),       NULL,             "SaveSettingsNow",     SaveSettingsProc},
776   {N_("Save Settings on Exit"),   NULL,             "SaveSettingsonExit",  SaveOnExitProc,         CHECK },
777   {NULL,                          NULL,              NULL,                 NULL}
778 };
779
780 MenuItem helpMenu[] = {
781   {N_("Info XBoard"),            NULL,   "InfoXBoard",           InfoProc},
782   {N_("Man XBoard"),            "F1",    "ManXBoard",            ManProc},
783   {"----",                       NULL,    NULL,                  NothingProc},
784   {N_("XBoard Home Page"),       NULL,   "XBoardHomePage",       HomePageProc},
785   {N_("On-line User Guide"),     NULL,   "On-lineUserGuide",     GuideProc},
786   {N_("Development News"),       NULL,   "DevelopmentNews",      NewsPageProc},
787   {N_("e-Mail Bug Report"),      NULL,   "e-MailBugReport",      BugReportProc},
788   {"----",                       NULL,    NULL,                  NothingProc},
789   {N_("About XBoard"),           NULL,   "AboutXBoard",          AboutProc},
790   {NULL,                         NULL,    NULL,                  NULL}
791 };
792
793 MenuItem noMenu[] = {
794   { "", "<Alt>Next" ,"LoadNextGame", LoadNextGameProc },
795   { "", "<Alt>Prior" ,"LoadPrevGame", LoadPrevGameProc },
796   { "", NULL,"ReloadGame", ReloadGameProc },
797   { "", NULL,"ReloadPosition", ReloadPositionProc },
798 #ifndef OPTIONSDIALOG
799   { "", NULL,"AlwaysQueen", AlwaysQueenProc },
800   { "", NULL,"AnimateDragging", AnimateDraggingProc },
801   { "", NULL,"AnimateMoving", AnimateMovingProc },
802   { "", NULL,"Autoflag", AutoflagProc },
803   { "", NULL,"Autoflip", AutoflipProc },
804   { "", NULL,"Blindfold", BlindfoldProc },
805   { "", NULL,"FlashMoves", FlashMovesProc },
806 #if HIGHDRAG
807   { "", NULL,"HighlightDragging", HighlightDraggingProc },
808 #endif
809   { "", NULL,"HighlightLastMove", HighlightLastMoveProc },
810   { "", NULL,"MoveSound", MoveSoundProc },
811   { "", NULL,"PeriodicUpdates", PeriodicUpdatesProc },
812   { "", NULL,"PopupExitMessage", PopupExitMessageProc },
813   { "", NULL,"PopupMoveErrors", PopupMoveErrorsProc },
814   { "", NULL,"ShowCoords", ShowCoordsProc },
815   { "", NULL,"ShowThinking", ShowThinkingProc },
816   { "", NULL,"HideThinking", HideThinkingProc },
817   { "", NULL,"TestLegality", TestLegalityProc },
818 #endif
819   { "", NULL,"AboutGame", AboutGameEvent },
820   { "", "<Ctrl>d" ,"DebugProc", DebugProc },
821   { "", NULL,"Nothing", NothingProc },
822   {NULL, NULL, NULL, NULL}
823 };
824
825 Menu menuBar[] = {
826     {N_("File"),    "File", fileMenu},
827     {N_("Edit"),    "Edit", editMenu},
828     {N_("View"),    "View", viewMenu},
829     {N_("Mode"),    "Mode", modeMenu},
830     {N_("Action"),  "Action", actionMenu},
831     {N_("Engine"),  "Engine", engineMenu},
832     {N_("Options"), "Options", optionsMenu},
833     {N_("Help"),    "Help", helpMenu},
834     {NULL, NULL, NULL},
835     {   "",         "None", noMenu}
836 };
837
838 MenuItem *
839 MenuNameToItem (char *menuName)
840 {
841     int i=0;
842     char buf[MSG_SIZ], *p;
843     MenuItem *menuTab;
844     static MenuItem a = { NULL, NULL, NULL, NothingProc };
845     extern Option mainOptions[];
846     safeStrCpy(buf, menuName, MSG_SIZ);
847     p = strchr(buf, '.');
848     if(!p) menuTab = noMenu, p = menuName; else {
849         *p++ = NULLCHAR;
850         for(i=0; menuBar[i].name; i++)
851             if(!strcmp(buf, menuBar[i].name)) break;
852         if(!menuBar[i].name) return NULL; // main menu not found
853         menuTab = menuBar[i].mi;
854     }
855     if(*p == NULLCHAR) { a.handle = mainOptions[i+1].handle; return &a; } // main menu bar
856     for(i=0; menuTab[i].string; i++)
857         if(menuTab[i].ref && !strcmp(p, menuTab[i].ref)) return menuTab + i;
858     return NULL; // item not found
859 }
860
861 int firstEngineItem;
862
863 void
864 AppendEnginesToMenu (char *list)
865 {
866     int i=0;
867     char *p;
868     if(appData.icsActive || appData.recentEngines <= 0) return;
869     for(firstEngineItem=0; engineMenu[firstEngineItem].string; firstEngineItem++);
870     recentEngines = strdup(list);
871     while (*list) {
872         p = strchr(list, '\n'); if(p == NULL) break;
873         if(i == 0) engineMenu[firstEngineItem++].string = "----"; // at least one valid item to add
874         *p = 0;
875         if(firstEngineItem + i < 99)
876             engineMenu[firstEngineItem+i].string = strdup(list); // just set name; MenuProc stays NULL
877         i++; *p = '\n'; list = p + 1;
878     }
879 }
880
881 Enables icsEnables[] = {
882     { "File.MailMove", False },
883     { "File.ReloadCMailMessage", False },
884     { "Mode.MachineBlack", False },
885     { "Mode.MachineWhite", False },
886     { "Mode.AnalysisMode", False },
887     { "Mode.AnalyzeFile", False },
888     { "Mode.TwoMachines", False },
889     { "Mode.MachineMatch", False },
890 #if !ZIPPY
891     { "Engine.Hint", False },
892     { "Engine.Book", False },
893     { "Engine.MoveNow", False },
894 #ifndef OPTIONSDIALOG
895     { "PeriodicUpdates", False },
896     { "HideThinking", False },
897     { "PonderNextMove", False },
898 #endif
899 #endif
900     { "Engine.Engine#1Settings", False },
901     { "Engine.Engine#2Settings", False },
902     { "Engine.Load1stEngine", False },
903     { "Engine.Load2ndEngine", False },
904     { "Edit.Annotate", False },
905     { "Options.Match", False },
906     { NULL, False }
907 };
908
909 Enables ncpEnables[] = {
910     { "File.MailMove", False },
911     { "File.ReloadCMailMessage", False },
912     { "Mode.MachineWhite", False },
913     { "Mode.MachineBlack", False },
914     { "Mode.AnalysisMode", False },
915     { "Mode.AnalyzeFile", False },
916     { "Mode.TwoMachines", False },
917     { "Mode.MachineMatch", False },
918     { "Mode.ICSClient", False },
919     { "View.ICStextmenu", False },
920     { "View.ICSInputBox", False },
921     { "View.OpenChatWindow", False },
922     { "Action.", False },
923     { "Edit.Revert", False },
924     { "Edit.Annotate", False },
925     { "Engine.Engine#1Settings", False },
926     { "Engine.Engine#2Settings", False },
927     { "Engine.MoveNow", False },
928     { "Engine.RetractMove", False },
929     { "Options.ICS", False },
930 #ifndef OPTIONSDIALOG
931     { "Options.AutoFlag", False },
932     { "Options.AutoFlip View", False },
933 //    { "Options.ICSAlarm", False },
934     { "Options.MoveSound", False },
935     { "Options.HideThinking", False },
936     { "Options.PeriodicUpdates", False },
937     { "Options.PonderNextMove", False },
938 #endif
939     { "Engine.Hint", False },
940     { "Engine.Book", False },
941     { NULL, False }
942 };
943
944 Enables gnuEnables[] = {
945     { "Mode.ICSClient", False },
946     { "View.ICStextmenu", False },
947     { "View.ICSInputBox", False },
948     { "View.OpenChatWindow", False },
949     { "Action.Accept", False },
950     { "Action.Decline", False },
951     { "Action.Rematch", False },
952     { "Action.Adjourn", False },
953     { "Action.StopExamining", False },
954     { "Action.StopObserving", False },
955     { "Action.UploadtoExamine", False },
956     { "Edit.Revert", False },
957     { "Edit.Annotate", False },
958     { "Options.ICS", False },
959
960     /* The next two options rely on SetCmailMode being called *after*    */
961     /* SetGNUMode so that when GNU is being used to give hints these     */
962     /* menu options are still available                                  */
963
964     { "File.MailMove", False },
965     { "File.ReloadCMailMessage", False },
966     // [HGM] The following have been added to make a switch from ncp to GNU mode possible
967     { "Mode.MachineWhite", True },
968     { "Mode.MachineBlack", True },
969     { "Mode.AnalysisMode", True },
970     { "Mode.AnalyzeFile", True },
971     { "Mode.TwoMachines", True },
972     { "Mode.MachineMatch", True },
973     { "Engine.Engine#1Settings", True },
974     { "Engine.Engine#2Settings", True },
975     { "Engine.Hint", True },
976     { "Engine.Book", True },
977     { "Engine.MoveNow", True },
978     { "Engine.RetractMove", True },
979     { "Action.", True },
980     { NULL, False }
981 };
982
983 Enables cmailEnables[] = {
984     { "Action.", True },
985     { "Action.CallFlag", False },
986     { "Action.Draw", True },
987     { "Action.Adjourn", False },
988     { "Action.Abort", False },
989     { "Action.StopObserving", False },
990     { "Action.StopExamining", False },
991     { "File.MailMove", True },
992     { "File.ReloadCMailMessage", True },
993     { NULL, False }
994 };
995
996 Enables trainingOnEnables[] = {
997   { "Edit.EditComment", False },
998   { "Mode.Pause", False },
999   { "Edit.Forward", False },
1000   { "Edit.Backward", False },
1001   { "Edit.ForwardtoEnd", False },
1002   { "Edit.BacktoStart", False },
1003   { "Engine.MoveNow", False },
1004   { "Edit.TruncateGame", False },
1005   { NULL, False }
1006 };
1007
1008 Enables trainingOffEnables[] = {
1009   { "Edit.EditComment", True },
1010   { "Mode.Pause", True },
1011   { "Edit.Forward", True },
1012   { "Edit.Backward", True },
1013   { "Edit.ForwardtoEnd", True },
1014   { "Edit.BacktoStart", True },
1015   { "Engine.MoveNow", True },
1016   { "Engine.TruncateGame", True },
1017   { NULL, False }
1018 };
1019
1020 Enables machineThinkingEnables[] = {
1021   { "File.LoadGame", False },
1022 //  { "LoadNextGame", False },
1023 //  { "LoadPreviousGame", False },
1024 //  { "ReloadSameGame", False },
1025   { "Edit.PasteGame", False },
1026   { "File.LoadPosition", False },
1027 //  { "LoadNextPosition", False },
1028 //  { "LoadPreviousPosition", False },
1029 //  { "ReloadSamePosition", False },
1030   { "Edit.PastePosition", False },
1031   { "Mode.MachineWhite", False },
1032   { "Mode.MachineBlack", False },
1033   { "Mode.TwoMachines", False },
1034 //  { "MachineMatch", False },
1035   { "Engine.RetractMove", False },
1036   { NULL, False }
1037 };
1038
1039 Enables userThinkingEnables[] = {
1040   { "File.LoadGame", True },
1041 //  { "LoadNextGame", True },
1042 //  { "LoadPreviousGame", True },
1043 //  { "ReloadSameGame", True },
1044   { "Edit.PasteGame", True },
1045   { "File.LoadPosition", True },
1046 //  { "LoadNextPosition", True },
1047 //  { "LoadPreviousPosition", True },
1048 //  { "ReloadSamePosition", True },
1049   { "Edit.PastePosition", True },
1050   { "Mode.MachineWhite", True },
1051   { "Mode.MachineBlack", True },
1052   { "Mode.TwoMachines", True },
1053 //  { "MachineMatch", True },
1054   { "Engine.RetractMove", True },
1055   { NULL, False }
1056 };
1057
1058 void
1059 SetICSMode ()
1060 {
1061   SetMenuEnables(icsEnables);
1062
1063 #if ZIPPY
1064   if (appData.zippyPlay && !appData.noChessProgram) { /* [DM] icsEngineAnalyze */
1065      EnableNamedMenuItem("Mode.AnalysisMode", True);
1066      EnableNamedMenuItem("Engine.Engine#1Settings", True);
1067   }
1068 #endif
1069 }
1070
1071 void
1072 SetNCPMode ()
1073 {
1074   SetMenuEnables(ncpEnables);
1075 }
1076
1077 void
1078 SetGNUMode ()
1079 {
1080   SetMenuEnables(gnuEnables);
1081 }
1082
1083 void
1084 SetCmailMode ()
1085 {
1086   SetMenuEnables(cmailEnables);
1087 }
1088
1089 void
1090 SetTrainingModeOn ()
1091 {
1092   SetMenuEnables(trainingOnEnables);
1093   if (appData.showButtonBar) {
1094     EnableButtonBar(False);
1095   }
1096   CommentPopDown();
1097 }
1098
1099 void
1100 SetTrainingModeOff ()
1101 {
1102   SetMenuEnables(trainingOffEnables);
1103   if (appData.showButtonBar) {
1104     EnableButtonBar(True);
1105   }
1106 }
1107
1108 void
1109 SetUserThinkingEnables ()
1110 {
1111   if (appData.noChessProgram) return;
1112   SetMenuEnables(userThinkingEnables);
1113 }
1114
1115 void
1116 SetMachineThinkingEnables ()
1117 {
1118   if (appData.noChessProgram) return;
1119   SetMenuEnables(machineThinkingEnables);
1120   switch (gameMode) {
1121   case MachinePlaysBlack:
1122   case MachinePlaysWhite:
1123   case TwoMachinesPlay:
1124     EnableNamedMenuItem(ModeToWidgetName(gameMode), True);
1125     break;
1126   default:
1127     break;
1128   }
1129 }
1130
1131 void
1132 GreyRevert (Boolean grey)
1133 {
1134     EnableNamedMenuItem("Edit.Revert", !grey);
1135     EnableNamedMenuItem("Edit.Annotate", !grey);
1136 }
1137
1138 char *
1139 ModeToWidgetName (GameMode mode)
1140 {
1141     switch (mode) {
1142       case BeginningOfGame:
1143         if (appData.icsActive)
1144           return "Mode.ICSClient";
1145         else if (appData.noChessProgram ||
1146                  *appData.cmailGameName != NULLCHAR)
1147           return "Mode.EditGame";
1148         else
1149           return "Mode.MachineBlack";
1150       case MachinePlaysBlack:
1151         return "Mode.MachineBlack";
1152       case MachinePlaysWhite:
1153         return "Mode.MachineWhite";
1154       case AnalyzeMode:
1155         return "Mode.AnalysisMode";
1156       case AnalyzeFile:
1157         return "Mode.AnalyzeFile";
1158       case TwoMachinesPlay:
1159         return "Mode.TwoMachines";
1160       case EditGame:
1161         return "Mode.EditGame";
1162       case PlayFromGameFile:
1163         return "File.LoadGame";
1164       case EditPosition:
1165         return "Mode.EditPosition";
1166       case Training:
1167         return "Mode.Training";
1168       case IcsPlayingWhite:
1169       case IcsPlayingBlack:
1170       case IcsObserving:
1171       case IcsIdle:
1172       case IcsExamining:
1173         return "Mode.ICSClient";
1174       default:
1175       case EndOfGame:
1176         return NULL;
1177     }
1178 }
1179
1180 static void
1181 InstallNewEngine (char *command, char *dir, char *variants, char *protocol)
1182 { // install the given engine in XBoard's -firstChessProgramNames
1183     char buf[MSG_SIZ], *quote = "";
1184     if(strchr(command, ' ')) { // quoting needed
1185         if(!strchr(command, '"')) quote = "\""; else
1186         if(!strchr(command, '\'')) quote = "'"; else {
1187             printf("Could not auto-install %s\n", command); // too complex
1188         }
1189     }
1190     // construct engine line, with optional -fd and -fUCI arguments
1191     snprintf(buf, MSG_SIZ, "%s%s%s", quote, command, quote);
1192     if(strcmp(dir, "") && strcmp(dir, "."))
1193         snprintf(buf + strlen(buf), MSG_SIZ - strlen(buf), " -fd %s", dir);
1194     if(!strcmp(protocol, "uci"))
1195         snprintf(buf + strlen(buf), MSG_SIZ - strlen(buf), " -fUCI");
1196     if(strstr(firstChessProgramNames, buf)) return; // avoid duplicats
1197     // append line
1198     quote = malloc(strlen(firstChessProgramNames) + strlen(buf) + 2);
1199     sprintf(quote, "%s%s\n", firstChessProgramNames, buf);
1200     FREE(firstChessProgramNames); firstChessProgramNames = quote;
1201 }
1202
1203 #ifdef HAVE_DIRENT_H
1204 #include <dirent.h>
1205 #else
1206 #include <sys/dir.h>
1207 #define dirent direct
1208 #endif
1209
1210 static void
1211 InstallFromDir (char *dirName, char *protocol, char *settingsFile)
1212 {   // scan system for new plugin specs in given directory
1213     DIR *dir;
1214     struct dirent *dp;
1215     struct stat statBuf;
1216     time_t lastSaved = 0;
1217     char buf[1024];
1218
1219     if(!stat(settingsFile, &statBuf)) lastSaved = statBuf.st_mtime;
1220     snprintf(buf, 1024, "%s/%s", dirName, protocol);
1221
1222     if(!(dir = opendir(buf))) return;
1223     while( (dp = readdir(dir))) {
1224         time_t installed = 0;
1225         if(!strstr(dp->d_name, ".eng")) continue; // to suppress . and ..
1226         snprintf(buf, 1024, "%s/%s/%s", dirName, protocol, dp->d_name);
1227         if(!stat(buf, &statBuf)) installed = statBuf.st_mtime;
1228         if(lastSaved == 0 || (int) (installed - lastSaved) > 0) { // first time we see it
1229             FILE *f = fopen(buf, "r");
1230             if(f) { // read the plugin-specs
1231                 char engineCommand[1024], engineDir[1024], variants[1024];
1232                 char bad=0, dummy, *engineCom = engineCommand;
1233                 int major, minor;
1234                 if(fscanf(f, "plugin spec %d.%d%c", &major, &minor, &dummy) != 3 ||
1235                    fscanf(f, "%[^\n]%c", engineCommand, &dummy) != 2 ||
1236                    fscanf(f, "%[^\n]%c", variants, &dummy) != 2) bad = 1;
1237                 fclose(f);
1238                 if(bad) continue;
1239                 // uncomment following two lines for chess-only installs
1240 //              if(!(p = strstr(variants, "chess")) ||
1241 //                   p != variants && p[-1] != ',' || p[5] && p[5] != ',') continue;
1242                 // split off engine working directory (if any)
1243                 strcpy(engineDir, "");
1244                 if(sscanf(engineCommand, "cd %[^;];%c", engineDir, &dummy) == 2)
1245                     engineCom = engineCommand + strlen(engineDir) + 4;
1246                 InstallNewEngine(engineCom, engineDir, variants, protocol);
1247             }
1248         }
1249     }
1250     closedir(dir);
1251 }
1252
1253 static void
1254 AutoInstallProtocol (char *settingsFile, char *protocol)
1255 {   // install new engines for given protocol (both from package and source)
1256     InstallFromDir("/usr/local/share/games/plugins", protocol, settingsFile);
1257     InstallFromDir("/usr/share/games/plugins", protocol, settingsFile);
1258 }
1259
1260 void
1261 AutoInstall (char *settingsFile)
1262 {   // install all new XBoard and UCI engines
1263     AutoInstallProtocol(settingsFile, "xboard");
1264     AutoInstallProtocol(settingsFile, "uci");
1265 }
1266
1267 void
1268 InitMenuMarkers()
1269 {
1270 #ifndef OPTIONSDIALOG
1271     if (appData.alwaysPromoteToQueen) {
1272         MarkMenuItem("Options.Always Queen", True);
1273     }
1274     if (appData.animateDragging) {
1275         MarkMenuItem("Options.Animate Dragging", True);
1276     }
1277     if (appData.animate) {
1278         MarkMenuItem("Options.Animate Moving", True);
1279     }
1280     if (appData.autoCallFlag) {
1281         MarkMenuItem("Options.Auto Flag", True);
1282     }
1283     if (appData.autoFlipView) {
1284         XtSetValues(XtNameToWidget(menuBarWidget,"Options.Auto Flip View", True);
1285     }
1286     if (appData.blindfold) {
1287         MarkMenuItem("Options.Blindfold", True);
1288     }
1289     if (appData.flashCount > 0) {
1290         MarkMenuItem("Options.Flash Moves", True);
1291     }
1292 #if HIGHDRAG
1293     if (appData.highlightDragging) {
1294         MarkMenuItem("Options.Highlight Dragging", True);
1295     }
1296 #endif
1297     if (appData.highlightLastMove) {
1298         MarkMenuItem("Options.Highlight Last Move", True);
1299     }
1300     if (appData.highlightMoveWithArrow) {
1301         MarkMenuItem("Options.Arrow", True);
1302     }
1303 //    if (appData.icsAlarm) {
1304 //      MarkMenuItem("Options.ICS Alarm", True);
1305 //    }
1306     if (appData.ringBellAfterMoves) {
1307         MarkMenuItem("Options.Move Sound", True);
1308     }
1309     if (appData.oneClick) {
1310         MarkMenuItem("Options.OneClick", True);
1311     }
1312     if (appData.periodicUpdates) {
1313         MarkMenuItem("Options.Periodic Updates", True);
1314     }
1315     if (appData.ponderNextMove) {
1316         MarkMenuItem("Options.Ponder Next Move", True);
1317     }
1318     if (appData.popupExitMessage) {
1319         MarkMenuItem("Options.Popup Exit Message", True);
1320     }
1321     if (appData.popupMoveErrors) {
1322         MarkMenuItem("Options.Popup Move Errors", True);
1323     }
1324 //    if (appData.premove) {
1325 //      MarkMenuItem("Options.Premove", True);
1326 //    }
1327     if (appData.showCoords) {
1328         MarkMenuItem("Options.Show Coords", True);
1329     }
1330     if (appData.hideThinkingFromHuman) {
1331         MarkMenuItem("Options.Hide Thinking", True);
1332     }
1333     if (appData.testLegality) {
1334         MarkMenuItem("Options.Test Legality", True);
1335     }
1336 #endif
1337     if (saveSettingsOnExit) {
1338         MarkMenuItem("Options.SaveSettingsonExit", True);
1339     }
1340     EnableNamedMenuItem("File.SaveSelected", False);
1341
1342     // all XBoard builds get here, but not WinBoard...
1343     if(*appData.autoInstall) AutoInstall(settingsFileName);
1344 }