OSDN Git Service

update year to 2022
[jnethack/source.git] / win / win32 / mhmain.c
index bb58a6b..360c532 100644 (file)
@@ -4,7 +4,7 @@
 
 /* JNetHack Copyright */
 /* (c) Issei Numata, Naoki Hamada, Shigehiro Miyashita, 1994-2000  */
-/* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2016            */
+/* For 3.4-, Copyright (c) SHIRAKATA Kentaro, 2002-2022            */
 /* JNetHack may be freely redistributed.  See license for details. */
 
 #include "winMS.h"
@@ -190,6 +190,8 @@ static const char scanmap[] = {
     'b', 'n', 'm', ',', '.', '?' /* ... */
 };
 
+#define IDT_FUZZ_TIMER 100 
+
 /*
 //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
 //
@@ -198,7 +200,7 @@ static const char scanmap[] = {
 LRESULT CALLBACK
 MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    PNHMainWindow data;
+    PNHMainWindow data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
 #if 1 /*JP*/
     static int doublebyte = 0;
 #endif
@@ -231,7 +233,6 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
         break;
 
     case WM_KEYDOWN: {
-        data = (PNHMainWindow) GetWindowLongPtr(hWnd, GWLP_USERDATA);
 
         /* translate arrow keys into nethack commands */
         switch (wParam) {
@@ -338,6 +339,15 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
             NHEVENT_KBD(KEYTABLE(KEY_PLUS));
             return 0;
 
+#if defined(DEBUG) && defined(_MSC_VER)
+        case VK_PAUSE:
+            if (IsDebuggerPresent()) {
+                iflags.debug_fuzzer = !iflags.debug_fuzzer;
+                return 0;
+            }
+            break;
+#endif
+
         case VK_CLEAR: /* This is the '5' key */
             NHEVENT_KBD(KEYTABLE(KEY_GOINTERESTING));
             return 0;
@@ -481,10 +491,11 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
             bail((char *) 0);
         } else {
             /* prompt user for action */
-/*JP
+#if 0 /*JP*/
             switch (NHMessageBox(hWnd, TEXT("Save?"),
-*/
-           switch (NHMessageBox(hWnd, TEXT("\95Û\91\82µ\82Ä\8fI\97¹\82µ\82Ü\82·\82©\81H"),
+#else
+            switch (NHMessageBox(hWnd, TEXT("\95Û\91\82µ\82Ä\8fI\97¹\82µ\82Ü\82·\82©\81H"),
+#endif
                                  MB_YESNOCANCEL | MB_ICONQUESTION)) {
             case IDYES:
 #ifdef SAFERHANGUP
@@ -525,6 +536,10 @@ MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
         exit(1);
         break;
 
+    case WM_DPICHANGED: {
+        mswin_layout_main_window(NULL);
+    } break;
+
     default:
         return DefWindowProc(hWnd, message, wParam, lParam);
     }
@@ -549,6 +564,12 @@ onMSNHCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
 
         child = GetNHApp()->windowlist[msg_param->wid].win;
     } break;
+
+    case MSNH_MSG_RANDOM_INPUT: {
+        nhassert(iflags.debug_fuzzer);
+        NHEVENT_KBD(randomkey());
+    } break;
+
     }
 }
 
@@ -726,7 +747,7 @@ mswin_layout_main_window(HWND changed_child)
         GetNHApp()->rtInvenWindow.bottom = GetNHApp()->rtMenuWindow.bottom;
 
         /* adjust map window size only if perm_invent is set */
-        if (flags.perm_invent)
+        if (iflags.perm_invent)
             GetNHApp()->rtMapWindow.right = GetNHApp()->rtMenuWindow.left;
     }
 
@@ -738,7 +759,7 @@ mswin_layout_main_window(HWND changed_child)
             /* kludge - inventory window should have its own type (same as
                menu-text
                as a matter of fact) */
-            if (flags.perm_invent && i == WIN_INVEN) {
+            if (iflags.perm_invent && i == WIN_INVEN) {
                 mswin_get_window_placement(NHW_INVEN, &rt);
             } else {
                 mswin_get_window_placement(GetNHApp()->windowlist[i].type,
@@ -753,6 +774,62 @@ mswin_layout_main_window(HWND changed_child)
         SetForegroundWindow(changed_child);
 }
 
+VOID CALLBACK FuzzTimerProc(
+       _In_ HWND     hwnd,
+       _In_ UINT     uMsg,
+       _In_ UINT_PTR idEvent,
+       _In_ DWORD    dwTime
+       )
+{
+       INPUT input[16];
+       int i_pos = 0;
+       int c = randomkey();
+       SHORT k = VkKeyScanA(c);
+       BOOL gen_alt = (rn2(50) == 0) && isalpha(c);
+
+       if (!iflags.debug_fuzzer) {
+               KillTimer(hwnd, IDT_FUZZ_TIMER);
+               return;
+       }
+
+       if (!GetFocus())
+            return;
+
+       ZeroMemory(input, sizeof(input));
+       if (gen_alt) {
+               input[i_pos].type = INPUT_KEYBOARD;
+               input[i_pos].ki.dwFlags = KEYEVENTF_SCANCODE;
+               input[i_pos].ki.wScan = MapVirtualKey(VK_MENU, 0);
+               i_pos++;
+       }
+
+       if (HIBYTE(k) & 1) {
+               input[i_pos].type = INPUT_KEYBOARD;
+               input[i_pos].ki.dwFlags = KEYEVENTF_SCANCODE;
+               input[i_pos].ki.wScan = MapVirtualKey(VK_LSHIFT, 0);
+               i_pos++;
+       }
+
+       input[i_pos].type = INPUT_KEYBOARD;
+       input[i_pos].ki.dwFlags = KEYEVENTF_SCANCODE;
+       input[i_pos].ki.wScan = MapVirtualKey(LOBYTE(k), 0);
+       i_pos++;
+
+       if (HIBYTE(k) & 1) {
+               input[i_pos].type = INPUT_KEYBOARD;
+               input[i_pos].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
+               input[i_pos].ki.wScan = MapVirtualKey(VK_LSHIFT, 0);
+               i_pos++;
+       }
+       if (gen_alt) {
+               input[i_pos].type = INPUT_KEYBOARD;
+               input[i_pos].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
+               input[i_pos].ki.wScan = MapVirtualKey(VK_MENU, 0);
+               i_pos++;
+       }
+       SendInput(i_pos, input, sizeof(input[0]));
+}
+
 LRESULT
 onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
 {
@@ -771,11 +848,22 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
         mswin_display_splash_window(TRUE);
         break;
 
+    case IDM_FUZZ:
+        if (iflags.debug_fuzzer)
+            KillTimer(hWnd, IDT_FUZZ_TIMER);
+        else
+            SetTimer(hWnd, IDT_FUZZ_TIMER, 10, FuzzTimerProc);
+        iflags.debug_fuzzer = !iflags.debug_fuzzer;
+        break;
     case IDM_EXIT:
+        if (iflags.debug_fuzzer)
+            break;
         done2();
         break;
 
     case IDM_SAVE:
+        if (iflags.debug_fuzzer)
+            break;
         if (!program_state.gameover && !program_state.done_hup)
             dosave();
         else
@@ -821,6 +909,7 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
         if (!OpenClipboard(hWnd)) {
             NHMessageBox(hWnd, TEXT("Cannot open clipboard"),
                          MB_OK | MB_ICONERROR);
+            free(p);
             return 0;
         }
 
@@ -829,6 +918,7 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
         hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(char));
         if (hglbCopy == NULL) {
             CloseClipboard();
+            free(p);
             return FALSE;
         }
 
@@ -853,6 +943,9 @@ onWMCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
         wchar_t *wtext;
         int tlen = 0;
 
+        if (iflags.debug_fuzzer)
+            break;
+
         ZeroMemory(filename, sizeof(filename));
         ZeroMemory(&ofn, sizeof(ofn));
         ofn.lStructSize = sizeof(OPENFILENAME);