From 9e1d956e98f0c3f5c5c18ef8a8c99a252a8bfdfd Mon Sep 17 00:00:00 2001
From: Jordan Woyak <jordan.woyak@gmail.com>
Date: Fri, 2 Apr 2010 01:17:40 +0000
Subject: [PATCH] GCPadNew now compiles with SDL 1.2, disabling haptic
 features.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5267 8ced0084-cf51-0410-be5f-012b33b47a6e
---
 .../Src/ControllerInterface/SDL/SDL.cpp       | 63 +++++++------------
 .../Src/ControllerInterface/SDL/SDL.h         | 34 +++++++---
 2 files changed, 49 insertions(+), 48 deletions(-)

diff --git a/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.cpp b/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.cpp
index 90b7f886da..fa636fa461 100644
--- a/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.cpp
+++ b/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.cpp
@@ -5,12 +5,13 @@
 #include "SDL.h"
 
 #ifdef _WIN32
-	#pragma comment(lib, "SDL.1.3.lib")
+	#if SDL_VERSION_ATLEAST(1, 3, 0)
+		#pragma comment(lib, "SDL.1.3.lib")
+	#else
+		#pragma comment(lib, "SDL.lib")
+	#endif
 #endif
 
-// temp for debuggin
-//#include <fstream>
-
 namespace ciface
 {
 namespace SDL
@@ -18,7 +19,7 @@ namespace SDL
 
 void Init( std::vector<ControllerInterface::Device*>& devices )
 {	
-	if ( SDL_Init( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC ) >= 0 )
+	if ( SDL_Init( SDL_INIT_FLAGS ) >= 0 )
     {
 		// joysticks
 		for( int i = 0; i < SDL_NumJoysticks(); ++i )
@@ -60,15 +61,16 @@ Joystick::Joystick( SDL_Joystick* const joystick, const unsigned int index ) : m
 		inputs.push_back( new Axis( i, -32768 ) );
 		inputs.push_back( new Axis( i, 32767 ) );
 	}
-	m_haptic = SDL_HapticOpenFromJoystick( m_joystick );
+
+#ifdef USE_SDL_HAPTIC
 	// try to get supported ff effects
+	m_haptic = SDL_HapticOpenFromJoystick( m_joystick );
 	if ( m_haptic  )
 	{
-
 		//SDL_HapticSetGain( m_haptic, 1000 );
 		//SDL_HapticSetAutocenter( m_haptic, 0 );
 
-		const unsigned int supported_effects = SDL_HapticQuery( m_haptic ); // use this later
+		const unsigned int supported_effects = SDL_HapticQuery( m_haptic );
 
 		// constant effect
 		if ( supported_effects & SDL_HAPTIC_CONSTANT )
@@ -84,15 +86,17 @@ Joystick::Joystick( SDL_Joystick* const joystick, const unsigned int index ) : m
 			m_state_out.push_back( EffectIDState() );
 		}
 	}
+#endif
 
 }
 
 Joystick::~Joystick()
 {
+#ifdef USE_SDL_HAPTIC
 	if ( m_haptic )
 	{	
 		// stop/destroy all effects
-		//SDL_HapticStopAll( m_haptic ); // ControllerInterface handles this
+		SDL_HapticStopAll( m_haptic );
 		std::vector<EffectIDState>::iterator i = m_state_out.begin(),
 			e = m_state_out.end();
 		for ( ; i!=e; ++i )
@@ -101,16 +105,13 @@ Joystick::~Joystick()
 		// close haptic first
 		SDL_HapticClose( m_haptic );
 	}
+#endif
 
 	// close joystick
 	SDL_JoystickClose( m_joystick );
 }
 
-//std::string Joystick::Effect::GetName() const
-//{
-//	return haptic_named_effects[m_index].name;
-//}
-
+#ifdef USE_SDL_HAPTIC
 std::string Joystick::ConstantEffect::GetName() const
 {
 	return "Constant";
@@ -125,14 +126,8 @@ void Joystick::ConstantEffect::SetState( const ControlState state, Joystick::Eff
 {
 	if ( state )
 	{
-		//debuggin here ...
-		//memset( &effect->effect, 0, sizeof(effect->effect) );
 		effect->effect.type = SDL_HAPTIC_CONSTANT;
 		effect->effect.constant.length = SDL_HAPTIC_INFINITY;
-		//effect->effect.constant.attack_length = 3000;
-		//effect->effect.constant.direction.type = SDL_HAPTIC_CARTESIAN;
-		//effect->effect
-		//effect->effect.constant.button = 0xFFFFFFFF;
 	}
 	else
 		effect->effect.type = 0;
@@ -158,6 +153,7 @@ void Joystick::RampEffect::SetState( const ControlState state, Joystick::EffectI
 	if ( old != effect->effect.ramp.start )
 		effect->changed = true;
 }
+#endif
 
 ControlState Joystick::GetInputState(const ControllerInterface::Device::Input* input)
 {
@@ -166,18 +162,22 @@ ControlState Joystick::GetInputState(const ControllerInterface::Device::Input* i
 
 void Joystick::SetOutputState(const ControllerInterface::Device::Output* output, const ControlState state)
 {
+#ifdef USE_SDL_HAPTIC
 	((Output*)output)->SetState( state, &m_state_out[ ((Output*)output)->m_index ] );
+#endif
 }
 
 bool Joystick::UpdateInput()
 {
-	SDL_JoystickUpdate();		// each joystick is doin this, o well
+	// each joystick is doin this, o well
+	SDL_JoystickUpdate();
 	
 	return true;
 }
 
 bool Joystick::UpdateOutput()
 {
+#ifdef USE_SDL_HAPTIC
 	std::vector<EffectIDState>::iterator i = m_state_out.begin(),
 		e = m_state_out.end();
 	for ( ; i!=e; ++i )
@@ -187,25 +187,7 @@ bool Joystick::UpdateOutput()
 			{
 				if ( i->effect.type )		// if outputstate is >0  this would be true
 					if ( (i->id = SDL_HapticNewEffect( m_haptic, &i->effect )) > -1 )	// upload the effect
-					{
-						//std::ofstream file( "SDLgood.txt" );
-
-						/*if ( 0 == */SDL_HapticRunEffect( m_haptic, i->id, 1 );//)	// run the effect
-						//	file << "all good";
-						//else
-						//	file << "not good";
-						//file.close();
-
-					}
-					else
-					{
-						// DEBUG
-
-						//std::ofstream file( "SDLerror.txt" );
-						//file << SDL_GetError();
-						//file.close();
-
-					}
+						SDL_HapticRunEffect( m_haptic, i->id, 1 );	// run the effect
 			}
 			else	// effect is already uploaded
 			{
@@ -221,6 +203,7 @@ bool Joystick::UpdateOutput()
 
 			i->changed = false;
 		}
+#endif
 	return true;
 }
 
diff --git a/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.h b/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.h
index fdd2b18da6..135c8ac12c 100644
--- a/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.h
+++ b/Source/Plugins/Plugin_GCPadNew/Src/ControllerInterface/SDL/SDL.h
@@ -3,15 +3,25 @@
 
 #include "../ControllerInterface.h"
 
-// getting rid of warning, sdl/wxwidgets both define it
-#undef M_PI
-// really dum
 #ifdef _WIN32
 	#include <SDL.h>
-	#include <SDL_haptic.h>
 #else
 	#include <SDL/SDL.h>
-	#include <SDL/SDL_haptic.h>
+#endif
+
+#if SDL_VERSION_ATLEAST(1, 3, 0)
+	#define USE_SDL_HAPTIC
+#endif
+
+#ifdef USE_SDL_HAPTIC
+	#define SDL_INIT_FLAGS	SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC
+	#ifdef _WIN32
+		#include <SDL_haptic.h>
+	#else
+		#include <SDL/SDL_haptic.h>
+	#endif
+#else
+	#define SDL_INIT_FLAGS	SDL_INIT_JOYSTICK
 #endif
 
 namespace ciface
@@ -28,6 +38,7 @@ class Joystick : public ControllerInterface::Device
 
 protected:
 
+#ifdef USE_SDL_HAPTIC
 	class EffectIDState
 	{
 		friend class Joystick;
@@ -38,6 +49,7 @@ protected:
 		int					id;
 		bool				changed;
 	};
+#endif
 
 	class Input : public ControllerInterface::Device::Input
 	{
@@ -49,6 +61,7 @@ protected:
 		const unsigned int		m_index;
 	};
 
+#ifdef USE_SDL_HAPTIC
 	class Output : public ControllerInterface::Device::Output
 	{
 		friend class Joystick;
@@ -58,6 +71,7 @@ protected:
 		
 		const size_t		m_index;
 	};
+#endif
 
 	class Button : public Input
 	{
@@ -92,6 +106,7 @@ protected:
 		const unsigned int		m_direction;
 	};
 
+#ifdef USE_SDL_HAPTIC
 	class ConstantEffect : public Output
 	{
 		friend class Joystick;
@@ -111,6 +126,7 @@ protected:
 		RampEffect( const size_t index ) : Output(index) {}
 		void SetState( const ControlState state, EffectIDState* const effect );
 	};
+#endif
 
 	bool UpdateInput();
 	bool UpdateOutput();
@@ -127,11 +143,13 @@ public:
 	std::string GetSource() const;
 
 private:
-	std::vector<EffectIDState>	m_state_out;
-
-	SDL_Haptic*					m_haptic;
 	SDL_Joystick* const			m_joystick;
 	const unsigned int			m_index;
+
+#ifdef USE_SDL_HAPTIC
+	std::vector<EffectIDState>	m_state_out;
+	SDL_Haptic*					m_haptic;
+#endif
 };