pcsx2/plugins/onepad/onepad.cpp

365 lines
7.4 KiB
C++
Raw Normal View History

/* OnePAD - author: arcum42(@gmail.com)
* Copyright (C) 2009
*
* Based on ZeroPAD, author zerofrog@gmail.com
* Copyright (C) 2006-2007
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include "onepad.h"
#include "svnrev.h"
#include "state_management.h"
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef _MSC_VER
#define snprintf sprintf_s
#endif
2016-09-04 12:10:02 +00:00
PADconf *conf;
static char libraryName[256];
keyEvent event;
static keyEvent s_event;
std::string s_strIniPath("inis/");
std::string s_strLogPath("logs/");
bool toggleAutoRepeat = false;
2016-09-04 12:10:02 +00:00
const u32 version = PS2E_PAD_VERSION;
const u32 revision = 1;
const u32 build = 3; // increase that with each version
#define PAD_SAVE_STATE_VERSION ((revision << 8) | (build << 0))
FILE *padLog = NULL;
2016-09-04 12:10:02 +00:00
pthread_spinlock_t mutex_KeyEvent;
bool mutex_WasInit = false;
2016-09-04 12:10:02 +00:00
KeyStatus *key_status = NULL;
queue<keyEvent> ev_fifo;
static void InitLibraryName()
{
#ifdef PUBLIC
2016-09-04 12:10:02 +00:00
// Public Release!
// Output a simplified string that's just our name:
2016-09-04 12:10:02 +00:00
strcpy(libraryName, "OnePAD");
#else
2016-09-04 12:10:02 +00:00
// Use TortoiseSVN's SubWCRev utility's output
// to label the specific revision:
snprintf(libraryName, 255, "OnePAD %lld%s"
#ifdef PCSX2_DEBUG
"-Debug"
#elif defined(PCSX2_DEVBUILD)
"-Dev"
#endif
,
SVN_REV,
SVN_MODS ? "m" : "");
#endif
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(u32)
PS2EgetLibType()
{
2016-09-04 12:10:02 +00:00
return PS2E_LT_PAD;
}
EXPORT_C_(const char *)
2016-09-04 12:10:02 +00:00
PS2EgetLibName()
{
2016-09-04 12:10:02 +00:00
InitLibraryName();
return libraryName;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(u32)
PS2EgetLibVersion2(u32 type)
{
2016-09-04 12:10:02 +00:00
return (version << 16) | (revision << 8) | build;
}
void __Log(const char *fmt, ...)
{
2016-09-04 12:10:02 +00:00
va_list list;
2016-09-04 12:10:02 +00:00
if (padLog == NULL)
return;
va_start(list, fmt);
vfprintf(padLog, fmt, list);
va_end(list);
}
void __LogToConsole(const char *fmt, ...)
{
2016-09-04 12:10:02 +00:00
va_list list;
2016-09-04 12:10:02 +00:00
va_start(list, fmt);
2016-09-04 12:10:02 +00:00
if (padLog != NULL)
vfprintf(padLog, fmt, list);
2016-09-04 12:10:02 +00:00
printf("OnePAD: ");
vprintf(fmt, list);
va_end(list);
}
void initLogging()
{
#ifdef PAD_LOG
2016-09-04 12:10:02 +00:00
if (padLog)
return;
const std::string LogFile(s_strLogPath + "padLog.txt");
padLog = fopen(LogFile.c_str(), "w");
if (padLog)
2016-09-04 12:10:02 +00:00
setvbuf(padLog, NULL, _IONBF, 0);
2016-09-04 12:10:02 +00:00
PAD_LOG("PADinit\n");
#endif
}
void CloseLogging()
{
#ifdef PAD_LOG
2016-09-04 12:10:02 +00:00
if (padLog) {
fclose(padLog);
padLog = NULL;
}
#endif
}
void clearPAD(int pad)
{
2016-09-04 12:10:02 +00:00
conf->keysym_map[pad].clear();
for (int key = 0; key < MAX_KEYS; ++key)
set_key(pad, key, 0);
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(s32)
PADinit(u32 flags)
{
2016-09-04 12:10:02 +00:00
initLogging();
2016-09-04 12:10:02 +00:00
LoadConfig();
2016-09-04 12:10:02 +00:00
key_status = new KeyStatus();
2016-09-04 12:10:02 +00:00
Pad::reset_all();
2016-09-04 12:10:02 +00:00
query.reset();
2016-09-04 12:10:02 +00:00
for (int port = 0; port < 2; port++)
slots[port] = 0;
2016-09-04 12:10:02 +00:00
return 0;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(void)
PADshutdown()
{
2016-09-04 12:10:02 +00:00
CloseLogging();
2016-09-04 12:10:02 +00:00
delete conf;
conf = nullptr;
2016-09-04 12:10:02 +00:00
delete key_status;
key_status = nullptr;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(s32)
PADopen(void *pDsp)
{
2016-09-04 12:10:02 +00:00
memset(&event, 0, sizeof(event));
key_status->Init();
2016-09-04 12:10:02 +00:00
while (!ev_fifo.empty())
ev_fifo.pop();
pthread_spin_init(&mutex_KeyEvent, PTHREAD_PROCESS_PRIVATE);
mutex_WasInit = true;
2016-05-01 08:54:53 +00:00
#if defined(__unix__)
2016-09-04 12:10:02 +00:00
GamePad::EnumerateGamePads(s_vgamePad);
#endif
2016-09-04 12:10:02 +00:00
return _PADopen(pDsp);
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(void)
PADsetSettingsDir(const char *dir)
{
2016-09-04 12:10:02 +00:00
// Get the path to the ini directory.
s_strIniPath = (dir == NULL) ? "inis/" : dir;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(void)
PADsetLogDir(const char *dir)
{
2016-09-04 12:10:02 +00:00
// Get the path to the log directory.
s_strLogPath = (dir == NULL) ? "logs/" : dir;
2016-09-04 12:10:02 +00:00
// Reload the log file after updated the path
CloseLogging();
initLogging();
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(void)
PADclose()
{
2016-09-04 12:10:02 +00:00
while (!ev_fifo.empty())
ev_fifo.pop();
mutex_WasInit = false;
pthread_spin_destroy(&mutex_KeyEvent);
_PADclose();
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(u32)
PADquery()
{
return 3; // both
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(s32)
PADsetSlot(u8 port, u8 slot)
{
2016-09-04 12:10:02 +00:00
port--;
slot--;
if (port > 1 || slot > 3) {
return 0;
}
// Even if no pad there, record the slot, as it is the active slot regardless.
slots[port] = slot;
return 1;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(s32)
PADfreeze(int mode, freezeData *data)
{
2016-09-04 12:10:02 +00:00
if (!data)
return -1;
2016-09-04 12:10:02 +00:00
if (mode == FREEZE_SIZE) {
data->size = sizeof(PadPluginFreezeData);
2016-09-04 12:10:02 +00:00
} else if (mode == FREEZE_LOAD) {
PadPluginFreezeData *pdata = (PadPluginFreezeData *)(data->data);
2016-09-04 12:10:02 +00:00
Pad::stop_vibrate_all();
2016-09-04 12:10:02 +00:00
if (data->size != sizeof(PadPluginFreezeData) || pdata->version != PAD_SAVE_STATE_VERSION ||
strncmp(pdata->format, "OnePad", sizeof(pdata->format)))
return 0;
2016-09-04 12:10:02 +00:00
query = pdata->query;
if (pdata->query.slot < 4) {
query = pdata->query;
}
2016-09-04 12:10:02 +00:00
// Tales of the Abyss - pad fix
// - restore data for both ports
for (int port = 0; port < 2; port++) {
for (int slot = 0; slot < 4; slot++) {
u8 mode = pdata->padData[port][slot].mode;
2016-09-04 12:10:02 +00:00
if (mode != MODE_DIGITAL && mode != MODE_ANALOG && mode != MODE_DS2_NATIVE) {
break;
}
2016-09-04 12:10:02 +00:00
memcpy(&pads[port][slot], &pdata->padData[port][slot], sizeof(PadFreezeData));
}
2016-09-04 12:10:02 +00:00
if (pdata->slot[port] < 4)
slots[port] = pdata->slot[port];
}
2016-09-04 12:10:02 +00:00
} else if (mode == FREEZE_SAVE) {
if (data->size != sizeof(PadPluginFreezeData))
return 0;
2016-09-04 12:10:02 +00:00
PadPluginFreezeData *pdata = (PadPluginFreezeData *)(data->data);
2016-09-04 12:10:02 +00:00
// Tales of the Abyss - pad fix
// - PCSX2 only saves port0 (save #1), then port1 (save #2)
2016-09-04 12:10:02 +00:00
memset(pdata, 0, data->size);
strncpy(pdata->format, "OnePad", sizeof(pdata->format));
pdata->version = PAD_SAVE_STATE_VERSION;
pdata->query = query;
2016-09-04 12:10:02 +00:00
for (int port = 0; port < 2; port++) {
for (int slot = 0; slot < 4; slot++) {
pdata->padData[port][slot] = pads[port][slot];
}
2016-09-04 12:10:02 +00:00
pdata->slot[port] = slots[port];
}
2016-09-04 12:10:02 +00:00
} else {
return -1;
}
2016-09-04 12:10:02 +00:00
return 0;
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(u8)
PADstartPoll(int pad)
{
2016-09-04 12:10:02 +00:00
return pad_start_poll(pad);
}
2016-09-04 12:10:02 +00:00
EXPORT_C_(u8)
PADpoll(u8 value)
{
2016-09-04 12:10:02 +00:00
return pad_poll(value);
}
// PADkeyEvent is called every vsync (return NULL if no event)
2016-09-04 12:10:02 +00:00
EXPORT_C_(keyEvent *)
PADkeyEvent()
{
2016-09-04 12:10:02 +00:00
s_event = event;
event.evt = 0;
event.key = 0;
return &s_event;
}
2016-05-01 08:54:53 +00:00
#if defined(__unix__)
2016-09-04 12:10:02 +00:00
EXPORT_C_(void)
PADWriteEvent(keyEvent &evt)
{
2016-09-04 12:10:02 +00:00
// This function call be called before PADopen. Therefore we cann't
// guarantee that the spin lock was initialized
if (mutex_WasInit) {
pthread_spin_lock(&mutex_KeyEvent);
ev_fifo.push(evt);
pthread_spin_unlock(&mutex_KeyEvent);
}
}
#endif