--- vd_win32.org.c	2003-01-22 12:52:07.000000000 +0100
+++ vd_win32.c	2003-01-22 16:17:32.000000000 +0100
@@ -63,6 +63,10 @@
 #define GRXWINDOW_TITLE "GRX"
 #endif
 
+static HANDLE window_thread = INVALID_HANDLE_VALUE;
+static volatile int hValid = 0;
+static DWORD WINAPI WndThread(void *param);
+
 HINSTANCE hGlobInst;
 HWND hGRXWnd = NULL;
 HDC hDCMem = NULL;
@@ -78,9 +82,9 @@
 static int maxScreenWidth, maxScreenHeight;
 static int maxWindowWidth, maxWindowHeight;
 
-static DWORD mainThreadId; /* Identifier of the main thread */
+// static DWORD mainThreadId; /* Identifier of the main thread */
 HANDLE hThread;            /* Handle of the worker thread (GRXMain ) */
-static int isGRXMainworking = 0;  /* True if hThread is running */
+// static int isGRXMainworking = 0;  /* True if hThread is running */
 
 static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam,
 				LPARAM lParam);
@@ -140,6 +144,15 @@
     HBRUSH hBrush;
     int inipos;
 
+    if (window_thread == INVALID_HANDLE_VALUE) {
+      DWORD thread_id;
+      /* Start the window thread */
+      window_thread = CreateThread(0,0,WndThread, NULL,0,&thread_id);
+      /* Wait for thread creating the window. This is a busy waiting
+	 loop (bad), but only for a short time (good) 
+      */
+      while(hValid == 0){/* wait for window to be created  Sleep()? */ Sleep(1); }
+    }
     if (mp->extinfo->mode != GR_frameText) {
 	inipos = 50;
 	if (mp->width == maxScreenWidth && mp->height == maxScreenHeight)
@@ -172,7 +185,7 @@
 	DeleteObject(hBrush);
 
 	UpdateWindow(hGRXWnd);
-
+	SetFocus(hGRXWnd);
 	ColorList_destroy();
     } else {
         /* If changing to text-mode, hide the graphics window. */
@@ -184,7 +197,28 @@
 
 static int detect(void)
 {
-    return TRUE;
+  /* Register the window class for graphics window */
+  WNDCLASSEX wndclass;
+
+  ColorList = NULL;
+  hBitmapScreen = NULL;
+  InitializeCriticalSection(&_csEventQueue);
+
+  wndclass.cbSize = sizeof(wndclass);
+  wndclass.style = CS_HREDRAW | CS_VREDRAW;
+  wndclass.lpfnWndProc = WndProc;
+  wndclass.cbClsExtra = 0;
+  wndclass.cbWndExtra = 0;
+  wndclass.hInstance = GetModuleHandle(NULL);
+  wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
+  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
+  wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
+  wndclass.lpszMenuName = NULL;
+  wndclass.lpszClassName = "GRXCLASS";
+  wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
+  RegisterClassEx(&wndclass);
+  hGRXWnd = INVALID_HANDLE_VALUE;
+  return TRUE;
 }
 
 static void setbank_dummy(int bk)
@@ -333,106 +367,43 @@
     GR_DRIVERF_USER_RESOLUTION  /* arbitrary resolution possible */
 };
 
-DWORD WINAPI GRXLocalMain(LPVOID pCmdLine)
-{
-    int argc;
-    char **argv;
-    char **envp;
-    char *pszCmdLine;
-    char *psz;
-    char **ppargv;
-    int i;
-
-    isGRXMainworking = 1;
-
-    pszCmdLine = GetCommandLine();
-    psz = pszCmdLine;
-    argc = 1;
-    while (*psz) {
-	if (*psz == ' ') {
-	    argc++;
-	    while (*psz == ' ') {
-		*psz = 0x00;
-		psz++;
-	    }
-	    if (*psz == 0x00) {
-		argc--;
-		break;
-	    }
-	    psz--;
-	}
-	psz++;
+static DWORD WINAPI WndThread(void *param) {
+  MSG msg;
+  /* Create the window */
+  hGRXWnd = CreateWindow("GRXCLASS", GRXWINDOW_TITLE,
+                   WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
+                   | WS_THICKFRAME | WS_MINIMIZEBOX, 0, 0,
+                   CW_USEDEFAULT, CW_USEDEFAULT, NULL,
+                   NULL,GetModuleHandle(NULL), NULL);
+  ShowWindow(hGRXWnd, SW_HIDE);
+  SetFocus(hGRXWnd);
+  /* Let main task procede */
+  hValid = 1;
+  /* Enter messaging loop */
+  while(1){
+    switch (GetMessage(&msg,hGRXWnd,0,0)){
+    case  -1:
+      /* 
+	 Here we end up if somebody close hGRXWnd without telling us, i.e.
+	 from the system menu. Is there a way how we can signal this to the
+	 main thread?
+	 In any case, we set our thread handle to invalid and terminate.
+      */
+      window_thread = INVALID_HANDLE_VALUE;
+      ExitThread(-1);
+      /* NOTREACHED */
+      return -1;
+    case TRUE:
+      TranslateMessage(&msg);
+      DispatchMessage(&msg);
+      break;
+    case FALSE:
+      /* Got a WM_QUIT */
+      ExitThread(0);
+      /* NOTREACHED */
+      return 0;
     }
-    psz = pszCmdLine;
-    argv = malloc(argc * sizeof(char *));
-    ppargv = argv;
-    for (i = 0; i < argc; i++) {
-	*ppargv = _strdup(psz);
-	ppargv++;
-	psz += strlen(psz);
-	while (*psz == 0x00) {
-	    psz++;
-	}
-    }
-    envp = (char **) GetEnvironmentStrings();
-    Sleep(1000);
-    /* Execute the main program */
-    GRXMain(argc, argv, envp);
-    /* Send a message to the main thread */
-    isGRXMainworking = 0;
-    PostMessage(hGRXWnd, WM_CLOSE, 0, 0);
-    ExitThread(0);
-    return 0;
-}
-
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
-		   PSTR szCmdLine, int nCmdShow)
-{
-    WNDCLASSEX wndclass;
-    DWORD tid;
-    MSG msg;
-
-    hGlobInst = hInstance;
-    tid = 0;
-    mainThreadId = GetCurrentThreadId();
-    ColorList = NULL;
-    hBitmapScreen = NULL;
-    InitializeCriticalSection(&_csEventQueue);
-
-    wndclass.cbSize = sizeof(wndclass);
-    wndclass.style = CS_HREDRAW | CS_VREDRAW;
-    wndclass.lpfnWndProc = WndProc;
-    wndclass.cbClsExtra = 0;
-    wndclass.cbWndExtra = 0;
-    wndclass.hInstance = hGlobInst;
-    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
-    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
-    wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
-    wndclass.lpszMenuName = NULL;
-    wndclass.lpszClassName = "GRXCLASS";
-    wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
-    RegisterClassEx(&wndclass);
-
-    hGRXWnd = CreateWindow("GRXCLASS", GRXWINDOW_TITLE,
-                           WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
-                           | WS_THICKFRAME | WS_MINIMIZEBOX, 0, 0,
-                           CW_USEDEFAULT, CW_USEDEFAULT, NULL,
-                           NULL, hGlobInst, NULL);
-    ShowWindow(hGRXWnd, SW_HIDE);
-    UpdateWindow(hGRXWnd);
-    SetFocus(hGRXWnd);
-
-    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) GRXLocalMain,
-			   (LPVOID) szCmdLine, 0, &tid);
-    while (GetMessage(&msg, NULL, 0, 0)) {
-	TranslateMessage(&msg);
-	DispatchMessage(&msg);
-    }
-    DeleteCriticalSection(&_csEventQueue);
-    /* Before terminating, stop the worker thread if running */
-    if (isGRXMainworking) TerminateThread(hThread, 0);
-
-    return 0;
+  }
 }
 
 static int convertwin32keystate(void)
@@ -504,18 +475,18 @@
 	}
 
     case WM_CLOSE:
-        if ( isGRXMainworking && MessageBox(hWnd,
-            "This will abort the program\nare you sure?", "Abort",
-             MB_APPLMODAL | MB_ICONQUESTION | MB_YESNO ) != IDYES)
-             return 0;
-        /* While terminating suspend the worker thread if running */
-        if (isGRXMainworking ) SuspendThread(hThread);
+      /* We destroy the backing storage and the contexts.
+	 Any attempts to draw regardless will probably segfault. Do we really
+	 want this ?
+      */
 	if (hBitmapScreen != NULL) {
 	    DeleteObject(hBitmapScreen);
 	    hBitmapScreen = NULL;
 	}
 	DeleteDC(hDCMem);
+	hDCMem = NULL;
         DestroyWindow(hWnd);
+	/* Should we also end WndThread? */
 	break;
 
     case WM_DESTROY:
@@ -527,7 +498,6 @@
 	    LPMINMAXINFO lpmmi = (LPMINMAXINFO) lParam;
 
 	    lpmmi->ptMaxSize.x = lpmmi->ptMaxTrackSize.x = maxWindowWidth;
-
 	    lpmmi->ptMaxSize.y = lpmmi->ptMaxTrackSize.y = maxWindowHeight;
 	}
         return 0;
