diff src/video/windx5/SDL_dx5video.c @ 1854:2280e314a978

Closed bug #74 Added DirectInput joystick code, contributed by Glenn Maynard. This fixes a problem with the Windows Multimedia joystick driver not showing all 6 axes on a GameCube controller converter, which was donated by Jacob Kolding.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 21 May 2006 16:47:41 +0000
parents 8d3ca155c396
children 5ff2c01e475e
line wrap: on
line diff
--- a/src/video/windx5/SDL_dx5video.c	Sun May 21 02:50:05 2006 +0000
+++ b/src/video/windx5/SDL_dx5video.c	Sun May 21 16:47:41 2006 +0000
@@ -436,102 +436,98 @@
 extern int DIB_GetGammaRamp(_THIS, Uint16 *ramp);
 extern void DIB_QuitGamma(_THIS);
 
+/* Functions for loading the DirectX functions dynamically */
+static int DX5_loaded = 0;
+static HINSTANCE DDrawDLL = NULL;
+static HINSTANCE DInputDLL = NULL;
+
+void DX5_Unload(void)
+{
+	if ( --DX5_loaded == 0 ) {
+		if ( DDrawDLL != NULL ) {
+			FreeLibrary(DDrawDLL);
+			DDrawCreate = NULL;
+			DDrawDLL = NULL;
+		}
+		if ( DInputDLL != NULL ) {
+			FreeLibrary(DInputDLL);
+			DInputCreate = NULL;
+			DInputDLL = NULL;
+		}
+	}
+}
+int DX5_Load(void)
+{
+	int status = 0;
+
+	if ( ++DX5_loaded == 1 ) {
+		DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
+		if ( DDrawDLL != NULL ) {
+			DDrawCreate = (void *)GetProcAddress(DDrawDLL,
+						TEXT("DirectDrawCreate"));
+		}
+		DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
+		if ( DInputDLL != NULL ) {
+			DInputCreate = (void *)GetProcAddress(DInputDLL,
+						TEXT("DirectInputCreateA"));
+		}
+		if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
+			status = 0;
+		} else {
+			DX5_Unload();
+			status = -1;
+		}
+	}
+	return status;
+}
+
 /* DX5 driver bootstrap functions */
 
 static int DX5_Available(void)
 {
-	HINSTANCE DInputDLL;
-	HINSTANCE DDrawDLL;
-	int dinput_ok;
-	int ddraw_ok;
+	int ddraw_ok = 0;
+	HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
+	LPDIRECTDRAW DDraw;
 
 	/* Version check DINPUT.DLL and DDRAW.DLL (Is DirectX okay?) */
-	dinput_ok = 0;
-	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
-	if ( DInputDLL != NULL ) {
-		dinput_ok = 1;
-	  	FreeLibrary(DInputDLL);
+	if ( DX5_Load() < 0 ) {
+		return -1;
 	}
-	ddraw_ok = 0;
-	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
-	if ( DDrawDLL != NULL ) {
-	  HRESULT (WINAPI *DDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
-	  LPDIRECTDRAW DDraw;
 
-	  /* Try to create a valid DirectDraw object */
-	  DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
-	  if ( (DDrawCreate != NULL)
+	/* Try to create a valid DirectDraw object */
+	DDrawCreate = (void *)GetProcAddress(DDrawDLL, TEXT("DirectDrawCreate"));
+	if ( (DDrawCreate != NULL)
 			&& !FAILED(DDrawCreate(NULL, &DDraw, NULL)) ) {
-	    if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
+	  if ( !FAILED(IDirectDraw_SetCooperativeLevel(DDraw,
 							NULL, DDSCL_NORMAL)) ) {
-	      DDSURFACEDESC desc;
-	      LPDIRECTDRAWSURFACE  DDrawSurf;
-	      LPDIRECTDRAWSURFACE3 DDrawSurf3;
-
-	      /* Try to create a DirectDrawSurface3 object */
-	      SDL_memset(&desc, 0, sizeof(desc));
-	      desc.dwSize = sizeof(desc);
-	      desc.dwFlags = DDSD_CAPS;
-	      desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
-	      if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
-							&DDrawSurf, NULL)) ) {
-	        if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
-			&IID_IDirectDrawSurface3, (LPVOID *)&DDrawSurf3)) ) {
-	          /* Yay! */
-		  ddraw_ok = 1;
+	    DDSURFACEDESC desc;
+	    LPDIRECTDRAWSURFACE  DDrawSurf;
+	    LPDIRECTDRAWSURFACE3 DDrawSurf3;
 
-	          /* Clean up.. */
-	          IDirectDrawSurface3_Release(DDrawSurf3);
-	        }
-	        IDirectDrawSurface_Release(DDrawSurf);
-	      }
-	    }
-	    IDirectDraw_Release(DDraw);
-	  }
-	  FreeLibrary(DDrawDLL);
-	}
-	return(dinput_ok && ddraw_ok);
-}
-
-/* Functions for loading the DirectX functions dynamically */
-static HINSTANCE DDrawDLL = NULL;
-static HINSTANCE DInputDLL = NULL;
+	    /* Try to create a DirectDrawSurface3 object */
+	    SDL_memset(&desc, 0, sizeof(desc));
+	    desc.dwSize = sizeof(desc);
+	    desc.dwFlags = DDSD_CAPS;
+	    desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_VIDEOMEMORY;
+	    if ( !FAILED(IDirectDraw_CreateSurface(DDraw, &desc,
+							&DDrawSurf, NULL)) ) {
+	      if ( !FAILED(IDirectDrawSurface_QueryInterface(DDrawSurf,
+			&IID_IDirectDrawSurface3, (LPVOID *)&DDrawSurf3)) ) {
+	        /* Yay! */
+		ddraw_ok = 1;
 
-static void DX5_Unload(void)
-{
-	if ( DDrawDLL != NULL ) {
-		FreeLibrary(DDrawDLL);
-		DDrawCreate = NULL;
-		DDrawDLL = NULL;
+	        /* Clean up.. */
+	        IDirectDrawSurface3_Release(DDrawSurf3);
+	      }
+	      IDirectDrawSurface_Release(DDrawSurf);
+	    }
+	  }
+	  IDirectDraw_Release(DDraw);
 	}
-	if ( DInputDLL != NULL ) {
-		FreeLibrary(DInputDLL);
-		DInputCreate = NULL;
-		DInputDLL = NULL;
-	}
-}
-static int DX5_Load(void)
-{
-	int status;
 
 	DX5_Unload();
-	DDrawDLL = LoadLibrary(TEXT("DDRAW.DLL"));
-	if ( DDrawDLL != NULL ) {
-		DDrawCreate = (void *)GetProcAddress(DDrawDLL,
-					TEXT("DirectDrawCreate"));
-	}
-	DInputDLL = LoadLibrary(TEXT("DINPUT.DLL"));
-	if ( DInputDLL != NULL ) {
-		DInputCreate = (void *)GetProcAddress(DInputDLL,
-					TEXT("DirectInputCreateA"));
-	}
-	if ( DDrawDLL && DDrawCreate && DInputDLL && DInputCreate ) {
-		status = 0;
-	} else {
-		DX5_Unload();
-		status = -1;
-	}
-	return status;
+
+	return ddraw_ok;
 }
 
 static void DX5_DeleteDevice(SDL_VideoDevice *this)
@@ -541,6 +537,7 @@
 		IDirectDraw2_Release(ddraw2);
 	}
 	DX5_Unload();
+
 	if ( this ) {
 		if ( this->hidden ) {
 			SDL_free(this->hidden);