2009-07-03 11:45:47 +00:00
|
|
|
/* 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
|
2010-07-04 22:49:00 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
2009-07-03 11:45:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "onepad.h"
|
2013-06-28 10:43:50 +00:00
|
|
|
#include "svnrev.h"
|
2016-07-24 15:06:28 +00:00
|
|
|
#include "state_management.h"
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2014-08-03 18:11:22 +00:00
|
|
|
#ifdef __linux__
|
2009-07-03 11:45:47 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2013-06-28 17:11:16 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define snprintf sprintf_s
|
|
|
|
#endif
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
PADconf *conf;
|
2015-04-16 21:14:12 +00:00
|
|
|
static char libraryName[256];
|
2009-07-03 11:45:47 +00:00
|
|
|
|
|
|
|
keyEvent event;
|
|
|
|
|
|
|
|
static keyEvent s_event;
|
2010-05-22 02:52:47 +00:00
|
|
|
std::string s_strIniPath("inis/");
|
2010-08-23 18:30:48 +00:00
|
|
|
std::string s_strLogPath("logs/");
|
2015-11-01 09:32:56 +00:00
|
|
|
bool toggleAutoRepeat = false;
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
const u32 version = PS2E_PAD_VERSION;
|
2011-06-17 20:44:46 +00:00
|
|
|
const u32 revision = 1;
|
2016-10-16 19:39:56 +00:00
|
|
|
const u32 build = 3; // increase that with each version
|
2016-07-24 16:21:19 +00:00
|
|
|
#define PAD_SAVE_STATE_VERSION ((revision << 8) | (build << 0))
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2009-07-03 11:45:47 +00:00
|
|
|
FILE *padLog = NULL;
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
pthread_spinlock_t mutex_KeyEvent;
|
2013-06-15 07:20:02 +00:00
|
|
|
bool mutex_WasInit = false;
|
2016-09-04 12:10:02 +00:00
|
|
|
KeyStatus *key_status = NULL;
|
2011-06-12 14:48:36 +00:00
|
|
|
|
|
|
|
queue<keyEvent> ev_fifo;
|
|
|
|
|
2009-07-03 11:45:47 +00:00
|
|
|
static void InitLibraryName()
|
|
|
|
{
|
2013-06-28 10:43:50 +00:00
|
|
|
#ifdef PUBLIC
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
// Public Release!
|
|
|
|
// Output a simplified string that's just our name:
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
strcpy(libraryName, "OnePAD");
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2013-06-28 10:43:50 +00:00
|
|
|
#else
|
2009-07-03 11:45:47 +00:00
|
|
|
|
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" : "");
|
2009-07-03 11:45:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(u32)
|
|
|
|
PS2EgetLibType()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
return PS2E_LT_PAD;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(char *)
|
|
|
|
PS2EgetLibName()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
InitLibraryName();
|
|
|
|
return libraryName;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(u32)
|
|
|
|
PS2EgetLibVersion2(u32 type)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
return (version << 16) | (revision << 8) | build;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
void __Log(const char *fmt, ...)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
va_list list;
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
if (padLog == NULL)
|
|
|
|
return;
|
|
|
|
va_start(list, fmt);
|
|
|
|
vfprintf(padLog, fmt, list);
|
|
|
|
va_end(list);
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
void __LogToConsole(const char *fmt, ...)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
va_list list;
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
va_start(list, fmt);
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
if (padLog != NULL)
|
|
|
|
vfprintf(padLog, fmt, list);
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
printf("OnePAD: ");
|
|
|
|
vprintf(fmt, list);
|
|
|
|
va_end(list);
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initLogging()
|
|
|
|
{
|
|
|
|
#ifdef PAD_LOG
|
2016-09-04 12:10:02 +00:00
|
|
|
if (padLog)
|
|
|
|
return;
|
2010-08-23 18:30:48 +00:00
|
|
|
|
|
|
|
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);
|
2010-08-23 18:30:48 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
PAD_LOG("PADinit\n");
|
2010-08-23 18:30:48 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseLogging()
|
|
|
|
{
|
|
|
|
#ifdef PAD_LOG
|
2016-09-04 12:10:02 +00:00
|
|
|
if (padLog) {
|
|
|
|
fclose(padLog);
|
|
|
|
padLog = NULL;
|
|
|
|
}
|
2009-07-03 11:45:47 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-06-12 14:48:36 +00:00
|
|
|
void clearPAD(int pad)
|
2009-07-10 00:05:05 +00:00
|
|
|
{
|
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);
|
2009-07-10 00:05:05 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(s32)
|
|
|
|
PADinit(u32 flags)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
initLogging();
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
LoadConfig();
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
key_status = new KeyStatus();
|
2009-07-03 11:45:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
Pad::reset_all();
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
query.reset();
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
for (int port = 0; port < 2; port++)
|
|
|
|
slots[port] = 0;
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
return 0;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(void)
|
|
|
|
PADshutdown()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
CloseLogging();
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
delete conf;
|
|
|
|
conf = nullptr;
|
2016-07-24 15:06:28 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
delete key_status;
|
|
|
|
key_status = nullptr;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(s32)
|
|
|
|
PADopen(void *pDsp)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
memset(&event, 0, sizeof(event));
|
|
|
|
key_status->Init();
|
2009-07-03 11:45:47 +00:00
|
|
|
|
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;
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2016-05-01 08:54:53 +00:00
|
|
|
#if defined(__unix__)
|
2016-09-04 12:10:02 +00:00
|
|
|
GamePad::EnumerateGamePads(s_vgamePad);
|
2009-07-03 11:45:47 +00:00
|
|
|
#endif
|
2016-09-04 12:10:02 +00:00
|
|
|
return _PADopen(pDsp);
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(void)
|
|
|
|
PADsetSettingsDir(const char *dir)
|
2010-05-22 02:52:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
// Get the path to the ini directory.
|
|
|
|
s_strIniPath = (dir == NULL) ? "inis/" : dir;
|
2010-05-22 02:52:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(void)
|
|
|
|
PADsetLogDir(const char *dir)
|
2010-08-23 18:30:48 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
// Get the path to the log directory.
|
|
|
|
s_strLogPath = (dir == NULL) ? "logs/" : dir;
|
2010-08-23 18:30:48 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
// Reload the log file after updated the path
|
2010-08-23 18:30:48 +00:00
|
|
|
CloseLogging();
|
|
|
|
initLogging();
|
|
|
|
}
|
2010-05-22 02:52:47 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(void)
|
|
|
|
PADclose()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
while (!ev_fifo.empty())
|
|
|
|
ev_fifo.pop();
|
|
|
|
mutex_WasInit = false;
|
|
|
|
pthread_spin_destroy(&mutex_KeyEvent);
|
|
|
|
_PADclose();
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(u32)
|
|
|
|
PADquery()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-10-16 19:39:56 +00:00
|
|
|
return 3; // both
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(s32)
|
|
|
|
PADsetSlot(u8 port, u8 slot)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
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;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(s32)
|
|
|
|
PADfreeze(int mode, freezeData *data)
|
2016-07-24 16:21:19 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
if (!data)
|
|
|
|
return -1;
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
if (mode == FREEZE_SIZE) {
|
|
|
|
data->size = sizeof(PadPluginFreezeData);
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
} else if (mode == FREEZE_LOAD) {
|
|
|
|
PadPluginFreezeData *pdata = (PadPluginFreezeData *)(data->data);
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
Pad::stop_vibrate_all();
|
2016-07-24 16:21:19 +00:00
|
|
|
|
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-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
query = pdata->query;
|
|
|
|
if (pdata->query.slot < 4) {
|
|
|
|
query = pdata->query;
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
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-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
if (mode != MODE_DIGITAL && mode != MODE_ANALOG && mode != MODE_DS2_NATIVE) {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
memcpy(&pads[port][slot], &pdata->padData[port][slot], sizeof(PadFreezeData));
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
if (pdata->slot[port] < 4)
|
|
|
|
slots[port] = pdata->slot[port];
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
} else if (mode == FREEZE_SAVE) {
|
|
|
|
if (data->size != sizeof(PadPluginFreezeData))
|
|
|
|
return 0;
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
PadPluginFreezeData *pdata = (PadPluginFreezeData *)(data->data);
|
2016-07-24 16:21:19 +00:00
|
|
|
|
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-07-24 16:21:19 +00:00
|
|
|
|
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-07-24 16:21:19 +00:00
|
|
|
|
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-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
pdata->slot[port] = slots[port];
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2016-07-24 16:21:19 +00:00
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
return 0;
|
2016-07-24 16:21:19 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(u8)
|
|
|
|
PADstartPoll(int pad)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
return pad_start_poll(pad);
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(u8)
|
|
|
|
PADpoll(u8 value)
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
return pad_poll(value);
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PADkeyEvent is called every vsync (return NULL if no event)
|
2016-09-04 12:10:02 +00:00
|
|
|
EXPORT_C_(keyEvent *)
|
|
|
|
PADkeyEvent()
|
2009-07-03 11:45:47 +00:00
|
|
|
{
|
2016-09-04 12:10:02 +00:00
|
|
|
s_event = event;
|
|
|
|
event.evt = 0;
|
|
|
|
event.key = 0;
|
|
|
|
return &s_event;
|
2009-07-03 11:45:47 +00:00
|
|
|
}
|
2011-06-12 14:48:36 +00:00
|
|
|
|
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)
|
2011-06-12 14:48:36 +00:00
|
|
|
{
|
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);
|
|
|
|
}
|
2011-06-12 14:48:36 +00:00
|
|
|
}
|
|
|
|
#endif
|