Add an attach debugger dialog.

Bug 15759123

Change-Id: I363524a191d319e03ce2b874cfc853c7330b684f
Reviewed-on: https://swiftshader-review.googlesource.com/3735
Reviewed-by: Nicolas Capens <capn@google.com>
Tested-by: Nicolas Capens <capn@google.com>
diff --git a/src/D3D9/D3D9.cpp b/src/D3D9/D3D9.cpp
index 9a11bd4..6a37094 100644
--- a/src/D3D9/D3D9.cpp
+++ b/src/D3D9/D3D9.cpp
@@ -99,6 +99,43 @@
 	}
 }
 
+static INT_PTR CALLBACK DebuggerWaitDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+	RECT rect;
+
+    switch(uMsg)
+    {
+    case WM_INITDIALOG:
+		GetWindowRect(GetDesktopWindow(), &rect);
+		SetWindowPos(hwnd, HWND_TOP, rect.right / 2, rect.bottom / 2, 0, 0, SWP_NOSIZE);
+		SetTimer(hwnd, 1, 100, NULL);
+		return TRUE;
+    case WM_COMMAND:
+        if(LOWORD(wParam) == IDCANCEL)
+		{
+			EndDialog(hwnd, 0);
+		}
+        break;
+    case WM_TIMER:
+		if(IsDebuggerPresent())
+		{
+			EndDialog(hwnd, 0);
+		}
+    }
+
+    return FALSE;
+}
+
+static void WaitForDebugger(HINSTANCE instance)
+{
+    if(!IsDebuggerPresent())
+    {
+        HRSRC dialog = FindResource(instance, MAKEINTRESOURCE(IDD_DIALOG1), RT_DIALOG);
+		DLGTEMPLATE *dialogTemplate = (DLGTEMPLATE*)LoadResource(instance, dialog);
+		DialogBoxIndirect(instance, dialogTemplate, NULL, DebuggerWaitDialogProc);
+    }
+}
+
 using namespace D3D9;
 
 extern "C"
@@ -111,7 +148,7 @@
 			if(dllInstance == 0)
 			{
 				FILE *file = fopen("debug.txt", "w");   // Clear debug log
-				fclose(file);
+				if(file) fclose(file);
 			}
 		#endif
 
@@ -124,6 +161,9 @@
 		case DLL_PROCESS_DETACH:
 			break;
 		case DLL_PROCESS_ATTACH:
+			#ifndef NDEBUG
+				WaitForDebugger(instance);
+			#endif
 			DisableThreadLibraryCalls(instance);
 			break;
 		case DLL_THREAD_ATTACH:
diff --git a/src/D3D9/D3D9.rc b/src/D3D9/D3D9.rc
index a0d69e6..fbd29af 100644
--- a/src/D3D9/D3D9.rc
+++ b/src/D3D9/D3D9.rc
@@ -84,6 +84,40 @@
     END
 END
 
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// Dialog
+//
+
+IDD_DIALOG1 DIALOGEX 0, 0, 129, 47
+STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
+CAPTION "Waiting for debugger"
+FONT 8, "MS Shell Dlg", 400, 0, 0x1
+BEGIN
+    PUSHBUTTON      "Cancel",IDCANCEL,72,26,50,14
+    LTEXT           "Attach a debugger or ESC to cancel",IDC_STATIC,7,7,115,8
+END
+
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// DESIGNINFO
+//
+
+#ifdef APSTUDIO_INVOKED
+GUIDELINES DESIGNINFO
+BEGIN
+    IDD_DIALOG1, DIALOG
+    BEGIN
+        LEFTMARGIN, 7
+        RIGHTMARGIN, 122
+        TOPMARGIN, 7
+        BOTTOMMARGIN, 40
+    END
+END
+#endif    // APSTUDIO_INVOKED
+
 #endif    // English (United States) resources
 /////////////////////////////////////////////////////////////////////////////
 
diff --git a/src/D3D9/resource.h b/src/D3D9/resource.h
index 4eae621..7779600 100644
--- a/src/D3D9/resource.h
+++ b/src/D3D9/resource.h
@@ -1,6 +1,8 @@
 //{{NO_DEPENDENCIES}}
 // Microsoft Visual C++ generated include file.
 // Used by D3D9.rc
+#define IDD_DIALOG1                     101
+#define IDC_STATIC                      -1
 
 // Next default values for new objects
 //