Bugfix draw offer to engine if color zippy enable.
[xboard.git] / winboard / winboard.c
index 455c483..b1aa04b 100644 (file)
 \r
 #include <windows.h>\r
 #include <winuser.h>\r
+#include <winsock.h>\r
 \r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <malloc.h>\r
-#include <io.h>\r
 #include <sys/stat.h>\r
 #include <fcntl.h>\r
 #include <math.h>\r
 #include <commdlg.h>\r
 #include <dlgs.h>\r
 #include <richedit.h>\r
+#include <mmsystem.h>\r
 \r
 #if __GNUC__\r
 #include <errno.h>\r
@@ -168,6 +169,7 @@ static BOOL paletteChanged = FALSE;
 static HICON iconWhite, iconBlack, iconCurrent;\r
 static int doingSizing = FALSE;\r
 static int lastSizing = 0;\r
+static int prevStderrPort;\r
 \r
 #if __GNUC__ && !defined(_winmajor)\r
 #define oldDialog 0 /* cygwin doesn't define _winmajor; mingw does */\r
@@ -335,6 +337,7 @@ HWND analysisDialog = NULL;
 BOOLEAN analysisDialogUp = FALSE;\r
 static int analysisX, analysisY, analysisH, analysisW;\r
 \r
+char errorTitle[MSG_SIZ];\r
 char errorMessage[2*MSG_SIZ];\r
 HWND errorDialog = NULL;\r
 BOOLEAN moveErrorMessageUp = FALSE;\r
@@ -344,8 +347,6 @@ COLORREF consoleBackgroundColor;
 \r
 char *programVersion;\r
 \r
-#include <winsock.h>\r
-\r
 #define CPReal 1\r
 #define CPComm 2\r
 #define CPSock 3\r
@@ -593,11 +594,6 @@ InitInstance(HINSTANCE hInstance, int nCmdShow, LPSTR lpCmdLine)
   InitMenuChecks();\r
   buttonCount = GetSystemMetrics(SM_CMOUSEBUTTONS);\r
 \r
-  /* Make a console window if needed */\r
-  if (appData.icsActive) {\r
-    ConsoleCreate();\r
-  }\r
-\r
   InitBackEnd2();\r
 \r
   /* Make the window visible; update its client area; and return "success" */\r
@@ -3107,7 +3103,19 @@ MouseEvent(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
       dragInfo.lastpos = dragInfo.pos;\r
     }\r
     break;\r
-\r
+  case WM_MOUSEWHEEL:\r
+       /* Mouse Wheel is being rolled forward \r
+        * Play moves forward\r
+        */\r
+       if ((short)HIWORD(wParam) > 0) \r
+          if (forwardMostMove > 0 && currentMove != forwardMostMove)\r
+                  ForwardEvent();\r
+          /* Mouse Wheel is being rolled backward \r
+           * Play moves backward\r
+           */\r
+       if ((short)HIWORD(wParam) < 0) \r
+          if (currentMove > 0) BackwardEvent();\r
+       break;\r
   case WM_MBUTTONDOWN:\r
   case WM_RBUTTONDOWN:\r
     ErrorPopDown();\r
@@ -3246,7 +3254,8 @@ Promotion(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
     /* Center the dialog over the application window */\r
     CenterWindow(hDlg, GetWindow(hDlg, GW_OWNER));\r
     ShowWindow(GetDlgItem(hDlg, PB_King), \r
-      (!appData.testLegality || gameInfo.variant == VariantSuicide) ?\r
+      (!appData.testLegality || gameInfo.variant == VariantSuicide ||\r
+       gameInfo.variant == VariantGiveaway) ?\r
               SW_SHOW : SW_HIDE);\r
     return TRUE;\r
 \r
@@ -3413,6 +3422,7 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
   case WM_MBUTTONUP:\r
   case WM_RBUTTONUP:\r
   case WM_MOUSEMOVE:\r
+  case WM_MOUSEWHEEL:\r
     MouseEvent(hwnd, message, wParam, lParam);\r
     break;\r
 \r
@@ -4070,6 +4080,26 @@ WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  *\r
 \*---------------------------------------------------------------------------*/\r
 \r
+/*\r
+ * Decent random number generator, at least not as bad as Windows\r
+ * standard rand, which returns a value in the range 0 to 0x7fff.\r
+ */\r
+unsigned int randstate;\r
+\r
+int\r
+myrandom(void)\r
+{\r
+  randstate = randstate * 1664525 + 1013904223;\r
+  return (int) randstate & 0x7fffffff;\r
+}\r
+\r
+void\r
+mysrandom(unsigned int seed)\r
+{\r
+  randstate = seed;\r
+}\r
+\r
+\r
 /* \r
  * returns TRUE if user selects a different color, FALSE otherwise \r
  */\r
@@ -4317,7 +4347,7 @@ OpenFileDialog(HWND hwnd, BOOL write, char *defName, char *defExt,
   if (write ? GetSaveFileName(&openFileName) : \r
               GetOpenFileName(&openFileName)) {\r
     /* open the file */\r
-    f = fopen(openFileName.lpstrFile, write ? "a" : "r");\r
+    f = fopen(openFileName.lpstrFile, write ? "a" : "rb");\r
     if (f == NULL) {\r
       MessageBox(hwnd, "File open failed", NULL,\r
                 MB_OK|MB_ICONEXCLAMATION);\r
@@ -4941,6 +4971,45 @@ PopUpMoveDialog(char firstchar)
 \*---------------------------------------------------------------------------*/\r
 \r
 /* Nonmodal error box */\r
+LRESULT CALLBACK ErrorDialog(HWND hDlg, UINT message,\r
+                            WPARAM wParam, LPARAM lParam);\r
+\r
+VOID\r
+ErrorPopUp(char *title, char *content)\r
+{\r
+  FARPROC lpProc;\r
+  char *p, *q;\r
+  BOOLEAN modal = hwndMain == NULL;\r
+\r
+  p = content;\r
+  q = errorMessage;\r
+  while (*p) {\r
+    if (*p == '\n') {\r
+      if (modal) {\r
+       *q++ = ' ';\r
+       p++;\r
+      } else {\r
+       *q++ = '\r';\r
+       *q++ = *p++;\r
+      }\r
+    } else {\r
+      *q++ = *p++;\r
+    }\r
+  }\r
+  *q = NULLCHAR;\r
+  strncpy(errorTitle, title, sizeof(errorTitle));\r
+  errorTitle[sizeof(errorTitle) - 1] = '\0';\r
+  \r
+  if (modal) {\r
+    MessageBox(NULL, errorMessage, errorTitle, MB_OK|MB_ICONEXCLAMATION);\r
+  } else {\r
+    lpProc = MakeProcInstance((FARPROC)ErrorDialog, hInst);\r
+    CreateDialog(hInst, MAKEINTRESOURCE(DLG_Error),\r
+                hwndMain, (DLGPROC)lpProc);\r
+    FreeProcInstance(lpProc);\r
+  }\r
+}\r
+\r
 VOID\r
 ErrorPopDown()\r
 {\r
@@ -4963,6 +5032,7 @@ ErrorDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
       rChild.top + boardRect.top - (rChild.bottom - rChild.top), \r
       0, 0, SWP_NOZORDER|SWP_NOSIZE);\r
     errorDialog = hDlg;\r
+    SetWindowText(hDlg, errorTitle);\r
     hwndText = GetDlgItem(hDlg, OPT_ErrorText);\r
     SetDlgItemText(hDlg, OPT_ErrorText, errorMessage);\r
     return FALSE;\r
@@ -4971,7 +5041,7 @@ ErrorDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
     switch (LOWORD(wParam)) {\r
     case IDOK:\r
     case IDCANCEL:\r
-      if (errorDialog = hDlg) errorDialog = NULL;\r
+      if (errorDialog == hDlg) errorDialog = NULL;\r
       DestroyWindow(hDlg);\r
       return TRUE;\r
 \r
@@ -6292,11 +6362,9 @@ DisplayMessage(char *str1, char *str2)
 VOID\r
 DisplayError(char *str, int error)\r
 {\r
-  FARPROC lpProc;\r
   char buf[MSG_SIZ*2], buf2[MSG_SIZ];\r
   int len;\r
-  char *p, *q;\r
-\r
\r
   if (error == 0) {\r
     strcpy(buf, str);\r
   } else {\r
@@ -6315,31 +6383,8 @@ DisplayError(char *str, int error)
       }\r
     }\r
   }\r
-  p = buf;\r
-  q = errorMessage;\r
-  while (*p) {\r
-    if (*p == '\n') {\r
-      if (hwndMain != NULL /*!!?*/) {\r
-        *q++ = '\r';\r
-        *q++ = *p++;\r
-      } else {\r
-       *q++ = ' ';\r
-        p++;\r
-      }\r
-    } else {\r
-      *q++ = *p++;\r
-    }\r
-  }\r
-  *q = NULLCHAR;\r
   \r
-  if (hwndMain == NULL) {\r
-    MessageBox(NULL, errorMessage, "Error", MB_OK|MB_ICONEXCLAMATION);\r
-  } else {\r
-    lpProc = MakeProcInstance((FARPROC)ErrorDialog, hInst);\r
-    CreateDialog(hInst, MAKEINTRESOURCE(DLG_Error),\r
-      hwndMain, (DLGPROC)lpProc);\r
-    FreeProcInstance(lpProc);\r
-  }\r
+  ErrorPopUp("Error", buf);\r
 }\r
 \r
 \r
@@ -6350,7 +6395,7 @@ DisplayMoveError(char *str)
   ClearHighlights();\r
   DrawPosition(FALSE, NULL);\r
   if (appData.popupMoveErrors) {\r
-    DisplayError(str, 0);\r
+    ErrorPopUp("Error", str);\r
   } else {\r
     DisplayMessage(str, "");\r
     moveErrorMessageUp = TRUE;\r
@@ -6399,6 +6444,13 @@ DisplayInformation(char *str)
 }\r
 \r
 \r
+VOID\r
+DisplayNote(char *str)\r
+{\r
+  ErrorPopUp("Note", str);\r
+}\r
+\r
+\r
 typedef struct {\r
   char *title, *question, *replyPrefix;\r
   ProcRef pr;\r
@@ -7202,6 +7254,7 @@ OpenRcmd(char* host, char* user, char* cmd, ProcRef* pr)
   mysa.sin_family = AF_INET;\r
   mysa.sin_addr.s_addr = INADDR_ANY;\r
   for (fromPort = 1023;; fromPort--) {\r
+    if (fromPort == prevStderrPort) continue; // don't reuse port\r
     if (fromPort < 0) {\r
       (void) closesocket(s);\r
       WSACleanup();\r
@@ -7239,6 +7292,7 @@ OpenRcmd(char* host, char* user, char* cmd, ProcRef* pr)
     }\r
     break;\r
   }\r
+  prevStderrPort = fromPort; // remember port used\r
   sprintf(stderrPortStr, "%d", fromPort);\r
 \r
   if (send(s, stderrPortStr, strlen(stderrPortStr) + 1, 0) == SOCKET_ERROR) {\r