fixed loading games. crashed because a X-func was called.
[xboard.git] / xboard.c
index ead2202..d0345f1 100644 (file)
--- a/xboard.c
+++ b/xboard.c
@@ -245,13 +245,13 @@ typedef struct {
 int main P((int argc, char **argv));
 RETSIGTYPE CmailSigHandler P((int sig));
 RETSIGTYPE IntSigHandler P((int sig));
+RETSIGTYPE TermSizeSigHandler P((int sig));
 void CreateGCs P((void));
 void CreateXIMPieces P((void));
 void CreateXPMPieces P((void));
 void CreatePieces P((void));
 void CreatePieceMenus P((void));
 Widget CreateMenuBar P((Menu *mb));
-Widget CreateButtonBar P ((MenuItem *mi));
 char *FindFont P((char *pattern, int targetPxlSize));
 void PieceMenuPopup P((Widget w, XEvent *event,
                       String *params, Cardinal *num_params));
@@ -263,10 +263,6 @@ void HandleUserMove P((Widget w, XEvent *event,
                     String *prms, Cardinal *nprms));
 void AnimateUserMove P((Widget w, XEvent * event,
                     String * params, Cardinal * nParams));
-void WhiteClock P((Widget w, XEvent *event,
-                  String *prms, Cardinal *nprms));
-void BlackClock P((Widget w, XEvent *event,
-                  String *prms, Cardinal *nprms));
 void CommentPopUp P((char *title, char *label));
 void CommentPopDown P((void));
 void CommentCallback P((Widget w, XtPointer client_data,
@@ -326,10 +322,6 @@ void AnimateDraggingProc P((Widget w, XEvent *event, String *prms,
                         Cardinal *nprms));
 void AnimateMovingProc P((Widget w, XEvent *event, String *prms,
                         Cardinal *nprms));
-void AutocommProc P((Widget w, XEvent *event, String *prms,
-                    Cardinal *nprms));
-void AutoflagProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
-void AutoflipProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
 void AutobsProc P((Widget w, XEvent *event, String *prms,
                        Cardinal *nprms));
 void AutoraiseProc P((Widget w, XEvent *event, String *prms, Cardinal *nprms));
@@ -389,7 +381,8 @@ void TimeControlPopDown P(());
 void NewVariantPopDown P(());
 void SettingsPopDown P(());
 void SetMenuEnables P((Enables *enab));
-
+void update_ics_width P(());
+int get_term_width P(());
 /*
 * XBoard depends on Xt R4 or higher
 */
@@ -414,7 +407,7 @@ Pixmap iconPixmap, wIconPixmap, bIconPixmap, xMarkPixmap;
 Widget shellWidget, layoutWidget, formWidget, boardWidget, messageWidget,
   whiteTimerWidget, blackTimerWidget, titleWidget, widgetList[16],
   commentShell, promotionShell, whitePieceMenu, blackPieceMenu, dropMenu,
-  menuBarWidget, buttonBarWidget, editShell, errorShell, analysisShell,
+  menuBarWidget,  editShell, errorShell, analysisShell,
   ICSInputShell, fileNameShell, askQuestionShell;
 Font clockFontID, coordFontID, countFontID;
 XFontStruct *clockFontStruct, *coordFontStruct, *countFontStruct;
@@ -699,9 +692,9 @@ MenuItem optionsMenu[] = {
     {N_("Always Queen"), AlwaysQueenProc},
     {N_("Animate Dragging"), AnimateDraggingProc},
     {N_("Animate Moving"), AnimateMovingProc},
-    {N_("Auto Comment"), AutocommProc},
-    {N_("Auto Flag"), AutoflagProc},
-    {N_("Auto Flip View"), AutoflipProc},
+    //    {N_("Auto Comment"), AutocommProc},
+    //    {N_("Auto Flag"), AutoflagProc},
+    //    {N_("Auto Flip View"), AutoflipProc},
     {N_("Auto Observe"), AutobsProc},
     {N_("Auto Raise Board"), AutoraiseProc},
     {N_("Auto Save"), AutosaveProc},
@@ -733,16 +726,6 @@ Menu menuBar[] = {
     {NULL, NULL}
 };
 
-#define PAUSE_BUTTON N_("P")
-MenuItem buttonBar[] = {
-  //    {"<<", ToStartProc},
-    //    {"<", BackwardProc},
-    //    {PAUSE_BUTTON, PauseProc},
-    //    {">", ForwardProc},
-  //    {">>", ToEndProc},
-    {NULL, NULL}
-};
-
 #define PIECE_MENU_SIZE 18
 String pieceMenuStrings[2][PIECE_MENU_SIZE] = {
     { N_("White"), "----", N_("Pawn"), N_("Knight"), N_("Bishop"), N_("Rook"),
@@ -1423,6 +1406,9 @@ XtResource clientResources[] = {
     { "forceIllegalMoves", "forceIllegalMoves", XtRBoolean,
        sizeof(Boolean), XtOffset(AppDataPtr, forceIllegal),
        XtRImmediate, (XtPointer) False},
+    { "keepLineBreaksICS", "keepLineBreaksICS", XtRBoolean,
+       sizeof(Boolean), XtOffset(AppDataPtr, noJoin),
+       XtRImmediate, (XtPointer) True},
 };
 
 XrmOptionDescRec shellOptions[] = {
@@ -1792,6 +1778,7 @@ XrmOptionDescRec shellOptions[] = {
     { "-secondNeedsNoncompliantFEN", "secondNeedsNoncompliantFEN", XrmoptionSepArg, NULL },
     { "-keepAlive", "keepAlive", XrmoptionSepArg, NULL },
     { "-forceIllegalMoves", "forceIllegalMoves", XrmoptionNoArg, "True" },
+    { "-keepLineBreaksICS", "keepLineBreaksICS", XrmoptionSepArg, NULL },
 };
 
 
@@ -1802,8 +1789,8 @@ XtActionsRec boardActions[] = {
     { "AskQuestionProc", AskQuestionProc },
     { "AskQuestionReplyAction", AskQuestionReplyAction },
     { "PieceMenuPopup", PieceMenuPopup },
-    { "WhiteClock", WhiteClock },
-    { "BlackClock", BlackClock },
+    //    { "WhiteClock", WhiteClock },
+    //    { "BlackClock", BlackClock },
     { "Iconify", Iconify },
     { "LoadSelectedProc", LoadSelectedProc },
     { "LoadPositionProc", LoadPositionProc },
@@ -1859,8 +1846,8 @@ XtActionsRec boardActions[] = {
     { "AlwaysQueenProc", AlwaysQueenProc },
     { "AnimateDraggingProc", AnimateDraggingProc },
     { "AnimateMovingProc", AnimateMovingProc },
-    { "AutoflagProc", AutoflagProc },
-    { "AutoflipProc", AutoflipProc },
+    //    { "AutoflagProc", AutoflagProc },
+    //    { "AutoflipProc", AutoflipProc },
     { "AutobsProc", AutobsProc },
     { "AutoraiseProc", AutoraiseProc },
     { "AutosaveProc", AutosaveProc },
@@ -1896,7 +1883,7 @@ XtActionsRec boardActions[] = {
     { "TagsPopDown", (XtActionProc) TagsPopDown },
     { "ErrorPopDown", (XtActionProc) ErrorPopDown },
     { "ICSInputBoxPopDown", (XtActionProc) ICSInputBoxPopDown },
-    { "AnalysisPopDown", (XtActionProc) AnalysisPopDown },
+    { "EngineOutputPopDown", (XtActionProc) EngineOutputPopDown },
     { "FileNamePopDown", (XtActionProc) FileNamePopDown },
     { "AskQuestionPopDown", (XtActionProc) AskQuestionPopDown },
     { "GameListPopDown", (XtActionProc) GameListPopDown },
@@ -2211,6 +2198,9 @@ main(argc, argv)
     GUI_Window = GTK_WIDGET (gtk_builder_get_object (builder, "MainWindow"));
     if(!GUI_Window) printf("Error: gtk_builder didn't work!\n");
 
+    GUI_Aspect = GTK_WIDGET (gtk_builder_get_object (builder, "Aspectframe"));
+    if(!GUI_Aspect) printf("Error: gtk_builder didn't work!\n");
+
     GUI_History = GTK_WIDGET (gtk_builder_get_object (builder, "MoveHistory"));
     if(!GUI_History) printf("Error: gtk_builder didn't work!\n");
 
@@ -2744,6 +2734,11 @@ ShutDownFrontEnd()
     unlink(gamePasteFilename);
 }
 
+RETSIGTYPE TermSizeSigHandler(int sig)
+{
+    update_ics_width();
+}
+
 RETSIGTYPE
 IntSigHandler(sig)
      int sig;
@@ -3211,39 +3206,6 @@ Widget CreateMenuBar(mb)
     return menuBar;
 }
 
-Widget CreateButtonBar(mi)
-     MenuItem *mi;
-{
-    int j;
-    Widget button, buttonBar;
-    Arg args[16];
-
-    j = 0;
-    XtSetArg(args[j], XtNorientation, XtorientHorizontal); j++;
-    if (tinyLayout) {
-       XtSetArg(args[j], XtNhSpace, 0); j++;
-    }
-    XtSetArg(args[j], XtNborderWidth, 0); j++;
-    XtSetArg(args[j], XtNvSpace, 0);                        j++;
-    buttonBar = XtCreateWidget("buttonBar", boxWidgetClass,
-                              formWidget, args, j);
-
-    while (mi->string != NULL) {
-       j = 0;
-       if (tinyLayout) {
-           XtSetArg(args[j], XtNinternalWidth, 2); j++;
-           XtSetArg(args[j], XtNborderWidth, 0); j++;
-       }
-      XtSetArg(args[j], XtNlabel, XtNewString(_(mi->string))); j++;
-       button = XtCreateManagedWidget(mi->string, commandWidgetClass,
-                                      buttonBar, args, j);
-       XtAddCallback(button, XtNcallback,
-                     (XtCallbackProc) MenuBarSelect,
-                     (caddr_t) mi->proc);
-       mi++;
-    }
-    return buttonBar;
-}
 
 Widget
 CreatePieceMenu(name, color)
@@ -3401,33 +3363,6 @@ static void DropMenuSelect(w, piece, junk)
     DropMenuEvent(piece, pmFromX, pmFromY);
 }
 
-void WhiteClock(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-    if (gameMode == EditPosition || gameMode == IcsExamining) {
-       SetWhiteToPlayEvent();
-    } else if (gameMode == IcsPlayingBlack || gameMode == MachinePlaysWhite) {
-       CallFlagEvent();
-    }
-}
-
-void BlackClock(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-    if (gameMode == EditPosition || gameMode == IcsExamining) {
-       SetBlackToPlayEvent();
-    } else if (gameMode == IcsPlayingWhite || gameMode == MachinePlaysBlack) {
-       CallFlagEvent();
-    }
-}
-
-
 /*
  * If the user selects on a border boundary, return -1; if off the board,
  *   return -2.  Otherwise map the event coordinate to the square.
@@ -4693,37 +4628,6 @@ void CommentPopUp(title, text)
     commentUp = True;
 }
 
-void AnalysisPopUp(title, text)
-     char *title, *text;
-{
-    Arg args[16];
-    int j;
-    Widget edit;
-
-    if (analysisShell == NULL) {
-       analysisShell = MiscCreate(title, text, False, NULL, 4);
-       XtRealizeWidget(analysisShell);
-       CatchDeleteWindow(analysisShell, "AnalysisPopDown");
-
-    } else {
-       edit = XtNameToWidget(analysisShell, "*form.text");
-       j = 0;
-       XtSetArg(args[j], XtNstring, text); j++;
-       XtSetValues(edit, args, j);
-       j = 0;
-       XtSetArg(args[j], XtNiconName, (XtArgVal) title);   j++;
-       XtSetArg(args[j], XtNtitle, (XtArgVal) title);      j++;
-       XtSetValues(analysisShell, args, j);
-    }
-
-    if (!analysisUp) {
-       XtPopup(analysisShell, XtGrabNone);
-    }
-    XSync(xDisplay, False);
-
-    analysisUp = True;
-}
-
 void CommentCallback(w, client_data, call_data)
      Widget w;
      XtPointer client_data, call_data;
@@ -4762,16 +4666,6 @@ void CommentPopDown()
     commentUp = False;
 }
 
-void AnalysisPopDown()
-{
-    if (!analysisUp) return;
-    XtPopdown(analysisShell);
-    XSync(xDisplay, False);
-    analysisUp = False;
-    if (appData.icsEngineAnalyze) ExitAnalyzeMode();    /* [DM] icsEngineAnalyze */
-}
-
-
 void FileNamePopUp(label, def, proc, openMode)
      char *label;
      char *def;
@@ -5637,64 +5531,6 @@ void AnimateMovingProc(w, event, prms, nprms)
                args, 1);
 }
 
-void AutocommProc(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-    Arg args[16];
-
-    appData.autoComment = !appData.autoComment;
-
-    if (appData.autoComment) {
-       XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
-    } else {
-       XtSetArg(args[0], XtNleftBitmap, None);
-    }
-    XtSetValues(XtNameToWidget(menuBarWidget, "menuOptions.Auto Comment"),
-               args, 1);
-}
-
-
-void AutoflagProc(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-    Arg args[16];
-
-    appData.autoCallFlag = !appData.autoCallFlag;
-
-    if (appData.autoCallFlag) {
-       XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
-    } else {
-       XtSetArg(args[0], XtNleftBitmap, None);
-    }
-    XtSetValues(XtNameToWidget(menuBarWidget, "menuOptions.Auto Flag"),
-               args, 1);
-}
-
-void AutoflipProc(w, event, prms, nprms)
-     Widget w;
-     XEvent *event;
-     String *prms;
-     Cardinal *nprms;
-{
-    Arg args[16];
-
-    appData.autoFlipView = !appData.autoFlipView;
-
-    if (appData.autoFlipView) {
-       XtSetArg(args[0], XtNleftBitmap, xMarkPixmap);
-    } else {
-       XtSetArg(args[0], XtNleftBitmap, None);
-    }
-    XtSetValues(XtNameToWidget(menuBarWidget, "menuOptions.Auto Flip View"),
-               args, 1);
-}
-
 void AutobsProc(w, event, prms, nprms)
      Widget w;
      XEvent *event;
@@ -6841,6 +6677,10 @@ int StartChildProcess(cmdLine, dir, pr)
 
     SetUpChildIO(to_prog, from_prog);
 
+    #ifdef SIGWINCH
+    signal(SIGWINCH, TermSizeSigHandler);
+    #endif
+
     if ((pid = fork()) == 0) {
        /* Child process */
        // [HGM] PSWBTM: made order resistant against case where fd of created pipe was 0 or 1
@@ -7978,3 +7818,38 @@ SetProgramStats( FrontEndProgramStats * stats )
   // [HGM] done, but perhaps backend should call this directly?
     EngineOutputUpdate( stats );
 }
+
+#include <sys/ioctl.h>
+int get_term_width()
+{
+    int fd, default_width;
+
+    fd = STDIN_FILENO;
+    default_width = 79; // this is FICS default anyway...
+
+#if !defined(TIOCGWINSZ) && defined(TIOCGSIZE)
+    struct ttysize win;
+    if (!ioctl(fd, TIOCGSIZE, &win))
+        default_width = win.ts_cols;
+#elif defined(TIOCGWINSZ)
+    struct winsize win;
+    if (!ioctl(fd, TIOCGWINSZ, &win))
+        default_width = win.ws_col;
+#endif
+    return default_width;
+}
+
+void update_ics_width()
+{
+    static int old_width = 0;
+    int new_width = get_term_width();
+
+    if (old_width != new_width)
+       ics_printf("set width %d\n", new_width);
+    old_width = new_width;
+}
+
+void NotifyFrontendLogin()
+{
+    update_ics_width();
+}