project64/Source/Project64-core/Plugins/ControllerPlugin.cpp

185 lines
6.3 KiB
C++
Raw Normal View History

2012-12-19 09:30:18 +00:00
/****************************************************************************
* *
2015-11-10 05:21:49 +00:00
* Project64 - A Nintendo 64 emulator. *
2012-12-19 09:30:18 +00:00
* http://www.pj64-emu.com/ *
* Copyright (C) 2012 Project64. All rights reserved. *
* *
* License: *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
* *
****************************************************************************/
#include "stdafx.h"
2015-12-06 09:59:58 +00:00
#include <Project64-core/N64System/SystemGlobals.h>
#include <Project64-core/N64System/N64RomClass.h>
#include <Project64-core/N64System/Mips/RegisterClass.h>
#include "ControllerPlugin.h"
#include <Windows.h>
CControl_Plugin::CControl_Plugin(void) :
WM_KeyDown(NULL),
WM_KeyUp(NULL),
RumbleCommand(NULL),
GetKeys(NULL),
ReadController(NULL),
ControllerCommand(NULL),
m_AllocatedControllers(false)
2012-10-04 01:45:30 +00:00
{
2015-11-08 06:08:15 +00:00
memset(&m_PluginControllers, 0, sizeof(m_PluginControllers));
memset(&m_Controllers, 0, sizeof(m_Controllers));
2012-10-04 01:45:30 +00:00
}
CControl_Plugin::~CControl_Plugin()
2012-10-04 01:45:30 +00:00
{
2015-11-08 06:08:15 +00:00
Close();
UnloadPlugin();
}
2015-11-08 06:08:15 +00:00
bool CControl_Plugin::LoadFunctions(void)
{
2015-11-08 06:08:15 +00:00
// Find entries for functions in DLL
void(__cdecl *InitiateControllers)(void);
LoadFunction(InitiateControllers);
LoadFunction(ControllerCommand);
LoadFunction(GetKeys);
LoadFunction(ReadController);
LoadFunction(WM_KeyDown);
LoadFunction(WM_KeyUp);
LoadFunction(RumbleCommand);
//Make sure dll had all needed functions
if (InitiateControllers == NULL) { UnloadPlugin(); return false; }
if (m_PluginInfo.Version >= 0x0102)
{
if (PluginOpened == NULL) { UnloadPlugin(); return false; }
}
// Allocate our own controller
m_AllocatedControllers = true;
for (int32_t i = 0; i < 4; i++)
{
m_Controllers[i] = new CCONTROL(m_PluginControllers[i].Present, m_PluginControllers[i].RawData, m_PluginControllers[i].Plugin);
}
return true;
}
bool CControl_Plugin::Initiate(CN64System * System, RenderWindow * Window)
{
CONTROL_INFO ControlInfo;
uint8_t Buffer[100];
2015-11-08 06:08:15 +00:00
for (int32_t i = 0; i < 4; i++)
{
m_PluginControllers[i].Present = FALSE;
m_PluginControllers[i].RawData = FALSE;
m_PluginControllers[i].Plugin = PLUGIN_NONE;
}
if (m_PluginInfo.Version >= 0x0101)
{
ControlInfo.Controls = m_PluginControllers;
ControlInfo.HEADER = (System == NULL ? Buffer : g_Rom->GetRomAddress());
ControlInfo.hinst = GetModuleHandle(NULL);
ControlInfo.hMainWindow = Window ? (HWND)Window->GetWindowHandle() : NULL;
ControlInfo.MemoryBswaped = TRUE;
}
2015-11-08 06:08:15 +00:00
// Test Plugin version
if (m_PluginInfo.Version == 0x0100)
{
//Get Function from DLL
void(__cdecl *InitiateControllers_1_0)(HWND hMainWindow, CONTROL Controls[4]);
InitiateControllers_1_0 = (void(__cdecl *)(HWND, CONTROL *))GetProcAddress((HMODULE)m_hDll, "InitiateControllers");
if (InitiateControllers_1_0 == NULL) { return false; }
InitiateControllers_1_0((HWND)Window->GetWindowHandle(), m_PluginControllers);
2015-11-08 06:08:15 +00:00
m_Initialized = true;
}
else if (m_PluginInfo.Version == 0x0101)
2015-11-08 06:08:15 +00:00
{
//Get Function from DLL
void(__cdecl *InitiateControllers_1_1)(CONTROL_INFO ControlInfo);
InitiateControllers_1_1 = (void(__cdecl *)(CONTROL_INFO))GetProcAddress((HMODULE)m_hDll, "InitiateControllers");
2015-11-08 06:08:15 +00:00
if (InitiateControllers_1_1 == NULL) { return false; }
InitiateControllers_1_1(ControlInfo);
m_Initialized = true;
}
else if (m_PluginInfo.Version >= 0x0102)
{
//Get Function from DLL
void(__cdecl *InitiateControllers_1_2)(CONTROL_INFO * ControlInfo);
InitiateControllers_1_2 = (void(__cdecl *)(CONTROL_INFO *))GetProcAddress((HMODULE)m_hDll, "InitiateControllers");
if (InitiateControllers_1_2 == NULL) { return false; }
InitiateControllers_1_2(&ControlInfo);
2015-11-08 06:08:15 +00:00
m_Initialized = true;
}
2015-11-08 06:08:15 +00:00
//jabo had a bug so I call CreateThread so his dllmain gets called again
DWORD ThreadID;
HANDLE hthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DummyFunction, NULL, 0, &ThreadID);
CloseHandle(hthread);
2015-11-08 06:08:15 +00:00
return m_Initialized;
}
void CControl_Plugin::UnloadPluginDetails(void)
{
2015-11-08 06:08:15 +00:00
if (m_AllocatedControllers)
{
for (int32_t count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++)
{
delete m_Controllers[count];
m_Controllers[count] = NULL;
}
}
m_AllocatedControllers = false;
ControllerCommand = NULL;
GetKeys = NULL;
ReadController = NULL;
WM_KeyDown = NULL;
WM_KeyUp = NULL;
}
2015-11-08 06:08:15 +00:00
void CControl_Plugin::UpdateKeys(void)
{
2015-11-08 06:08:15 +00:00
if (!m_AllocatedControllers) { return; }
for (int32_t cont = 0; cont < sizeof(m_Controllers) / sizeof(m_Controllers[0]); cont++)
{
if (!m_Controllers[cont]->m_Present) { continue; }
if (!m_Controllers[cont]->m_RawData)
{
GetKeys(cont, &m_Controllers[cont]->m_Buttons);
}
else
{
g_Notify->BreakPoint(__FILE__, __LINE__);
2015-11-08 06:08:15 +00:00
}
}
if (ReadController) { ReadController(-1, NULL); }
}
void CControl_Plugin::SetControl(CControl_Plugin const * const Plugin)
{
2015-11-08 06:08:15 +00:00
if (m_AllocatedControllers)
{
for (int32_t count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++)
{
delete m_Controllers[count];
m_Controllers[count] = NULL;
}
}
m_AllocatedControllers = false;
for (int32_t count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++)
{
m_Controllers[count] = Plugin->m_Controllers[count];
}
}
CCONTROL::CCONTROL(int32_t &Present, int32_t &RawData, int32_t &PlugType) :
m_Present(Present), m_RawData(RawData), m_PlugType(PlugType)
{
2015-11-08 06:08:15 +00:00
m_Buttons.Value = 0;
}