OnePAD: All around my hat...

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1482 96395faa-99c1-11dd-bbfe-3dabce05a288
This commit is contained in:
arcum42 2009-07-10 06:07:32 +00:00
parent cae05180f5
commit b0861c6d81
10 changed files with 327 additions and 204 deletions

View File

@ -64,9 +64,9 @@ void populate_tree_view()
gtk_tree_store_clear(treestore);
for (int pad = 0; pad < 2 * PADSUBKEYS; pad++)
for (int pad = 0; pad < 2 * MAX_SUB_KEYS; pad++)
{
for (int key = 0; key < PADKEYS; key++)
for (int key = 0; key < MAX_KEYS; key++)
{
if (get_key(pad, key) != 0)
{
@ -168,79 +168,6 @@ void delete_tree_row()
{
}*/
void SaveConfig()
{
FILE *f;
char cfg[255];
strcpy(cfg, s_strIniPath.c_str());
f = fopen(cfg, "w");
if (f == NULL)
{
printf("ZeroPAD: failed to save ini %s\n", s_strIniPath.c_str());
return;
}
for (int pad = 0; pad < 2 * PADSUBKEYS; pad++)
{
for (int key = 0; key < PADKEYS; key++)
{
fprintf(f, "[%d][%d] = 0x%lx\n", pad, key, get_key(pad,key));
}
}
fprintf(f, "log = %d\n", conf.log);
fprintf(f, "options = %d\n", conf.options);
fclose(f);
}
void LoadConfig()
{
FILE *f;
char str[256];
char cfg[255];
memset(&conf, 0, sizeof(conf));
set_key(0, PAD_L2, XK_a);
set_key(0, PAD_R2, XK_semicolon);
set_key(0, PAD_L1, XK_w);
set_key(0, PAD_R1, XK_p);
set_key(0, PAD_TRIANGLE, XK_i);
set_key(0, PAD_CIRCLE, XK_l);
set_key(0, PAD_CROSS, XK_k);
set_key(0, PAD_SQUARE, XK_j);
set_key(0, PAD_SELECT, XK_v);
set_key(0, PAD_START, XK_n);
set_key(0, PAD_UP, XK_e);
set_key(0, PAD_RIGHT, XK_f);
set_key(0, PAD_DOWN, XK_d);
set_key(0, PAD_LEFT, XK_s);
conf.log = 0;
strcpy(cfg, s_strIniPath.c_str());
f = fopen(cfg, "r");
if (f == NULL)
{
printf("OnePAD: failed to load ini %s\n", s_strIniPath.c_str());
SaveConfig(); //save and return
return;
}
for (int pad = 0; pad < 2 * PADSUBKEYS; pad++)
{
for (int key = 0; key < PADKEYS; key++)
{
sprintf(str, "[%d][%d] = 0x%%x\n", pad, key);
u32 temp;
if (fscanf(f, str, &temp) == 0) temp = 0;
set_key(pad, key, temp);
}
}
fscanf(f, "log = %d\n", &conf.log);
fscanf(f, "options = %d\n", &conf.options);
fclose(f);
}
void OnMsg_Ok()
{
gtk_widget_destroy(MsgDlg);

View File

@ -0,0 +1,179 @@
/* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <gtk/gtk.h>
#include <pthread.h>
#include "joystick.h"
#include "onepad.h"
#include "linux.h"
extern "C"
{
#include "interface.h"
#include "support.h"
#include "callbacks.h"
}
string KeyName(int pad, int key)
{
string tmp;
KeyType k = type_of_key(pad, key);
switch (k)
{
case PAD_KEYBOARD:
{
char* pstr = KeysymToChar(pad_to_key(pad, key));
if (pstr != NULL) tmp = pstr;
break;
}
case PAD_JOYBUTTONS:
{
int button = key_to_button(pad, key);
tmp.resize(28);
sprintf(&tmp[0], "JBut %d", button);
break;
}
case PAD_JOYSTICK:
{
int axis = key_to_axis(pad, key);
tmp.resize(28);
sprintf(&tmp[0], "JAxis %d", axis);
break;
}
case PAD_HAT:
{
int axis = key_to_axis(pad, key);
tmp.resize(28);
switch(key_to_hat_dir(pad, key))
{
case SDL_HAT_UP:
sprintf(&tmp[0], "JPOVU-%d", axis);
break;
case SDL_HAT_RIGHT:
sprintf(&tmp[0], "JPOVR-%d", axis);
break;
case SDL_HAT_DOWN:
sprintf(&tmp[0], "JPOVD-%d", axis);
break;
case SDL_HAT_LEFT:
sprintf(&tmp[0], "JPOVL-%d", axis);
break;
}
break;
}
case PAD_POV:
{
tmp.resize(28);
sprintf(&tmp[0], "JPOV %d%s", key_to_axis(pad, key), key_to_pov_sign(pad, key) ? "-" : "+");
break;
}
default: break;
}
return tmp;
}
void DefaultValues()
{
set_key(0, PAD_L2, XK_a);
set_key(0, PAD_R2, XK_semicolon);
set_key(0, PAD_L1, XK_w);
set_key(0, PAD_R1, XK_p);
set_key(0, PAD_TRIANGLE, XK_i);
set_key(0, PAD_CIRCLE, XK_l);
set_key(0, PAD_CROSS, XK_k);
set_key(0, PAD_SQUARE, XK_j);
set_key(0, PAD_SELECT, XK_v);
set_key(0, PAD_START, XK_n);
set_key(0, PAD_UP, XK_e);
set_key(0, PAD_RIGHT, XK_f);
set_key(0, PAD_DOWN, XK_d);
set_key(0, PAD_LEFT, XK_s);
}
void SaveConfig()
{
FILE *f;
char cfg[255];
strcpy(cfg, s_strIniPath.c_str());
f = fopen(cfg, "w");
if (f == NULL)
{
printf("ZeroPAD: failed to save ini %s\n", s_strIniPath.c_str());
return;
}
for (int pad = 0; pad < 2 * MAX_SUB_KEYS; pad++)
{
for (int key = 0; key < MAX_KEYS; key++)
{
fprintf(f, "[%d][%d] = 0x%lx\n", pad, key, get_key(pad,key));
}
}
fprintf(f, "log = %d\n", conf.log);
fprintf(f, "options = %d\n", conf.options);
fclose(f);
}
void LoadConfig()
{
FILE *f;
char str[256];
char cfg[255];
memset(&conf, 0, sizeof(conf));
DefaultValues();
conf.log = 0;
strcpy(cfg, s_strIniPath.c_str());
f = fopen(cfg, "r");
if (f == NULL)
{
printf("OnePAD: failed to load ini %s\n", s_strIniPath.c_str());
SaveConfig(); //save and return
return;
}
for (int pad = 0; pad < 2 * MAX_SUB_KEYS; pad++)
{
for (int key = 0; key < MAX_KEYS; key++)
{
sprintf(str, "[%d][%d] = 0x%%x\n", pad, key);
u32 temp;
if (fscanf(f, str, &temp) == 0) temp = 0;
set_key(pad, key, temp);
}
}
fscanf(f, "log = %d\n", &conf.log);
fscanf(f, "options = %d\n", &conf.options);
fclose(f);
}

View File

@ -34,6 +34,8 @@ const char* s_pGuiKeyMap[] =
"R_Up", "R_Right", "R_Down", "R_Left"
};
extern string KeyName(int pad, int key);
s32 _PADopen(void *pDsp)
{
GSdsp = *(Display**)pDsp;
@ -63,9 +65,9 @@ int _GetJoystickIdFromPAD(int pad)
// select the right joystick id
int joyid = -1;
for (int p = 0; p < PADSUBKEYS; p++)
for (int p = 0; p < MAX_SUB_KEYS; p++)
{
for (int i = 0; i < PADKEYS; ++i)
for (int i = 0; i < MAX_KEYS; ++i)
{
KeyType k = type_of_key(PadEnum[pad][p],i);
@ -97,7 +99,7 @@ EXPORT_C_(void) PADupdate(int pad)
// joystick info
SDL_JoystickUpdate();
for (int i = 0; i < PADKEYS; i++)
for (int i = 0; i < MAX_KEYS; i++)
{
int cpad = PadEnum[pad][0];
@ -136,33 +138,22 @@ EXPORT_C_(void) PADupdate(int pad)
}
break;
}
#ifdef EXPERIMENTAL_POV_CODE
case PAD_HAT:
{
int value = SDL_JoystickGetHat((pjoy)->GetJoy(), key_to_axis(cpad, i));
if ((value != SDL_HAT_CENTERED) && (key_to_hat_dir(cpad, i) == value))
if (key_to_hat_dir(cpad, i) == value)
{
if ((value == SDL_HAT_UP) ||
(value == SDL_HAT_RIGHT) ||
(value == SDL_HAT_DOWN) ||
(value == SDL_HAT_LEFT))
{
set_bit(status[pad], i);
PAD_LOG("Registered %s. Set (%d)\n", HatName(value), i);
}
else
{
clear_bit(status[pad], i);
}
clear_bit(status[pad], i);
//PAD_LOG("Registered %s\n", HatName(value), i);
//PAD_LOG("%s\n", KeyName(cpad, i).c_str());
}
else
{
clear_bit(status[pad], i);
set_bit(status[pad], i);
}
break;
}
#endif
case PAD_POV:
{
int value = SDL_JoystickGetAxis((pjoy)->GetJoy(), key_to_axis(cpad, i));
@ -181,73 +172,6 @@ EXPORT_C_(void) PADupdate(int pad)
}
}
string KeyName(int pad, int key)
{
string tmp;
KeyType k = type_of_key(pad, key);
switch (k)
{
case PAD_KEYBOARD:
{
char* pstr = KeysymToChar(pad_to_key(pad, key));
if (pstr != NULL) tmp = pstr;
break;
}
case PAD_JOYBUTTONS:
{
int button = key_to_button(pad, key);
tmp.resize(28);
sprintf(&tmp[0], "JBut %d", button);
break;
}
case PAD_JOYSTICK:
{
int axis = key_to_axis(pad, key);
tmp.resize(28);
sprintf(&tmp[0], "JAxis %d", axis);
break;
}
#ifdef EXPERIMENTAL_POV_CODE
case PAD_HAT:
{
int axis = key_to_axis(pad, key);
tmp.resize(28);
switch(key_to_hat_dir(pad, key))
{
case SDL_HAT_UP:
sprintf(&tmp[0], "JPOVU-%d", axis);
break;
case SDL_HAT_RIGHT:
sprintf(&tmp[0], "JPOVR-%d", axis);
break;
case SDL_HAT_DOWN:
sprintf(&tmp[0], "JPOVD-%d", axis);
break;
case SDL_HAT_LEFT:
sprintf(&tmp[0], "JPOVL-%d", axis);
break;
}
break;
}
#endif
case PAD_POV:
{
tmp.resize(28);
sprintf(&tmp[0], "JPOV %d%s", key_to_axis(pad, key), key_to_pov_sign(pad, key) ? "-" : "+");
break;
}
default: break;
}
return tmp;
}
void UpdateConf(int pad)
{
initLogging();
@ -267,7 +191,7 @@ void UpdateConf(int pad)
continue;
}
gtk_object_set_user_data(GTK_OBJECT(Btn), (void*)(PADKEYS * pad + i));
gtk_object_set_user_data(GTK_OBJECT(Btn), (void*)(MAX_KEYS * pad + i));
}
// check bounds
@ -309,8 +233,8 @@ void OnConf_Key(GtkButton *button, gpointer user_data)
if (id == -1) return;
int pad = id / PADKEYS;
int key = id % PADKEYS;
int pad = id / MAX_KEYS;
int key = id % MAX_KEYS;
// save the joystick states
UpdateJoysticks();
@ -324,6 +248,7 @@ void OnConf_Key(GtkButton *button, gpointer user_data)
if (PollX11Keyboard(tmp, pkey))
{
set_key(pad, key, pkey);
PAD_LOG("%s\n", KeyName(pad, key).c_str());
captured = true;
break;
}
@ -333,34 +258,50 @@ void OnConf_Key(GtkButton *button, gpointer user_data)
itjoy = s_vjoysticks.begin();
while ((itjoy != s_vjoysticks.end()) && (!captured))
{
int jbutton, direction;
int button_id, direction;
pkey = get_key(pad, key);
if ((*itjoy)->PollButtons(jbutton, pkey))
if ((*itjoy)->PollButtons(button_id, pkey))
{
set_key(pad, key, pkey);
PAD_LOG("%s\n", KeyName(pad, key).c_str());
captured = true;
break;
}
bool negative = false;
bool sign = false;
bool pov = (!((key == PAD_RY) || (key == PAD_LY) || (key == PAD_RX) || (key == PAD_LX)));
if ((*itjoy)->PollAxes(pov, jbutton, negative, pkey))
int axis_id;
if (pov)
{
set_key(pad, key, pkey);
captured = true;
break;
if ((*itjoy)->PollPOV(axis_id, sign, pkey))
{
set_key(pad, key, pkey);
PAD_LOG("%s\n", KeyName(pad, key).c_str());
captured = true;
break;
}
}
else
{
if ((*itjoy)->PollAxes(axis_id, pkey))
{
set_key(pad, key, pkey);
PAD_LOG("%s\n", KeyName(pad, key).c_str());
captured = true;
break;
}
}
#ifdef EXPERIMENTAL_POV_CODE
if ((*itjoy)->PollHats(jbutton, direction, pkey))
if ((*itjoy)->PollHats(axis_id, direction, pkey))
{
set_key(pad, key, pkey);
PAD_LOG("%s\n", KeyName(pad, key).c_str());
captured = true;
break;
}
#endif
itjoy++;
}
}

View File

@ -23,4 +23,4 @@ libOnePAD_LDFLAGS+=-Wl,-soname,@ZEROPAD_SONAME@
libOnePAD_LDADD=$(libOnePAD_a_OBJECTS)
libOnePAD_a_SOURCES = joystick.cpp analog.cpp analog.h onepad.cpp onepad.h controller.cpp controller.h \
Linux/gui.cpp Linux/linux.cpp Linux/support.c Linux/interface.c keyboard.cpp keyboard.h
Linux/gui.cpp Linux/linux.cpp Linux/support.c Linux/interface.c Linux/ini.cpp keyboard.cpp keyboard.h

View File

@ -22,6 +22,8 @@
#include "onepad.h"
#include "controller.h"
HatPins hat_position = {false, false, false, false};
__forceinline int set_key(int pad, int index, int value)
{
conf.keys[pad][index] = value;

View File

@ -23,15 +23,15 @@
#define __CONTROLLER_H__
#ifdef __LINUX__
#define PADKEYS 28
#define MAX_KEYS 28
#else
#define PADKEYS 20
#define MAX_KEYS 20
#endif
#ifdef _WIN32
#define PADSUBKEYS 1
#define MAX_SUB_KEYS 1
#else
#define PADSUBKEYS 2
#define MAX_SUB_KEYS 2
#endif
enum KeyType
@ -60,12 +60,60 @@ extern int joystick_to_key(int joy_id, int axis_id);
extern int pov_to_key(int joy_id, int sign, int axis_id);
extern int hat_to_key(int joy_id, int dir, int axis_id);
//#define EXPERIMENTAL_POV_CODE
extern int PadEnum[2][2];
typedef struct
{
u32 keys[2 * PADSUBKEYS][PADKEYS];
bool left, right, up, down;
} HatPins;
extern HatPins hat_position;
static __forceinline void set_hat_pins(int tilt_o_the_hat)
{
hat_position.left = false;
hat_position.right = false;
hat_position.up = false;
hat_position.down = false;
switch (tilt_o_the_hat)
{
case SDL_HAT_UP:
hat_position.up = true;
break;
case SDL_HAT_RIGHT:
hat_position.right= true;
break;
case SDL_HAT_DOWN:
hat_position.down = true;
break;
case SDL_HAT_LEFT:
hat_position.left = true;
break;
case SDL_HAT_LEFTUP:
hat_position.left = true;
hat_position.up = true;
break;
case SDL_HAT_RIGHTUP:
hat_position.right= true;
hat_position.up = true;
break;
case SDL_HAT_LEFTDOWN:
hat_position.left = true;
hat_position.down = true;
break;
case SDL_HAT_RIGHTDOWN:
hat_position.right= true;
hat_position.down = true;
break;
default:
break;
}
}
typedef struct
{
u32 keys[2 * MAX_SUB_KEYS][MAX_KEYS];
u32 log;
u32 options; // upper 16 bits are for pad2
} PADconf;

View File

@ -100,7 +100,7 @@ void JoystickInfo::EnumerateJoysticks(vector<JoystickInfo*>& vjoysticks)
// select the right joystick id
int joyid = -1;
for (int i = 0; i < PADKEYS; ++i)
for (int i = 0; i < MAX_KEYS; ++i)
{
KeyType k = type_of_key(pad,i);
if (k == PAD_JOYSTICK || k == PAD_JOYBUTTONS)
@ -167,7 +167,7 @@ void JoystickInfo::Assign(int newpad)
if (pad >= 0)
{
for (int i = 0; i < PADKEYS; ++i)
for (int i = 0; i < MAX_KEYS; ++i)
{
KeyType k = type_of_key(pad,i);
@ -221,7 +221,7 @@ bool JoystickInfo::PollButtons(int &jbutton, u32 &pkey)
return false;
}
bool JoystickInfo::PollAxes(bool pov, int &jbutton, bool &negative, u32 &pkey)
bool JoystickInfo::PollPOV(int &axis_id, bool &sign, u32 &pkey)
{
for (int i = 0; i < GetNumAxes(); ++i)
{
@ -240,17 +240,40 @@ bool JoystickInfo::PollAxes(bool pov, int &jbutton, bool &negative, u32 &pkey)
if (abs(value) > 0x3fff)
{
jbutton = i;
axis_id = i;
sign = (value < 0);
pkey = pov_to_key(GetId(), sign, i);
return true;
}
}
}
return false;
}
bool JoystickInfo::PollAxes(int &axis_id, u32 &pkey)
{
for (int i = 0; i < GetNumAxes(); ++i)
{
int value = SDL_JoystickGetAxis(GetJoy(), i);
if (value != GetAxisState(i))
{
PAD_LOG("Change in joystick %d: %d.\n", i, value);
if (abs(value) <= GetAxisState(i)) // we don't want this
{
// released, we don't really want this
SetAxisState(i, value);
break;
}
if (abs(value) > 0x3fff)
{
axis_id = i;
pkey = joystick_to_key(GetId(), i);
if (pov)
{
negative = (value < 0);
pkey = pov_to_key(GetId(), negative, i);
}
else // axis
{
pkey = joystick_to_key(GetId(), i);
}
return true;
}
}
@ -260,12 +283,11 @@ bool JoystickInfo::PollAxes(bool pov, int &jbutton, bool &negative, u32 &pkey)
bool JoystickInfo::PollHats(int &jbutton, int &dir, u32 &pkey)
{
#ifdef EXPERIMENTAL_POV_CODE
for (int i = 0; i < GetNumHats(); ++i)
{
int value = SDL_JoystickGetHat(GetJoy(), i);
if (value != SDL_HAT_CENTERED)
if ((value != GetHatState(i)) && (value != SDL_HAT_CENTERED))
{
switch (value)
{
@ -283,6 +305,5 @@ bool JoystickInfo::PollHats(int &jbutton, int &dir, u32 &pkey)
}
}
}
#endif
return false;
}

View File

@ -29,6 +29,7 @@
#endif
#include "onepad.h"
#include "controller.h"
// holds all joystick info
class JoystickInfo
@ -51,10 +52,12 @@ class JoystickInfo
bool PollButtons(int &jbutton, u32 &pkey);
bool PollAxes(bool pov, int &jbutton, bool &negative, u32 &pkey);
bool PollAxes(int &axis_id, u32 &pkey);
bool PollHats(int &jbutton, int &dir, u32 &pkey);
bool PollPOV(int &axis_id, bool &sign, u32 &pkey);
const string& GetName()
{
return devname;
@ -105,6 +108,7 @@ class JoystickInfo
int GetHatState(int i)
{
//PAD_LOG("Getting POV State of %d.\n", i);
set_hat_pins(vhatstate[i]);
return vhatstate[i];
}
@ -121,6 +125,7 @@ class JoystickInfo
void SetHatState(int i, int value)
{
//PAD_LOG("We should set %d to %d.\n", i, value);
set_hat_pins(i);
vhatstate[i] = value;
}

View File

@ -29,8 +29,8 @@
__forceinline int FindKey(int key, int pad)
{
for (int p = 0; p < PADSUBKEYS; p++)
for (int i = 0; i < PADKEYS; i++)
for (int p = 0; p < MAX_SUB_KEYS; p++)
for (int i = 0; i < MAX_KEYS; i++)
if (key == get_key(PadEnum[pad][p], i)) return i;
return -1;
}
@ -189,7 +189,7 @@ LRESULT WINAPI PADwndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
for (int pad = 0; pad < 2; ++pad)
{
for (int i = 0; i < PADKEYS; i++)
for (int i = 0; i < MAX_KEYS; i++)
{
if (wParam == get_key(pad, i))
{
@ -207,7 +207,7 @@ LRESULT WINAPI PADwndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_KEYUP:
for (int pad = 0; pad < 2; ++pad)
{
for (int i = 0; i < PADKEYS; i++)
for (int i = 0; i < MAX_KEYS; i++)
{
if (wParam == get_key(pad,i))
{

View File

@ -229,9 +229,9 @@ void initLogging()
void clearPAD()
{
for (int pad = 0; pad < PADSUBKEYS; pad++)
for (int pad = 0; pad < MAX_SUB_KEYS; pad++)
{
for (int key= 0; key < PADKEYS; ++key)
for (int key= 0; key < MAX_KEYS; ++key)
{
set_key(pad, key, 0);
}