diff --git a/plugins/zeropad/Linux/gui.cpp b/plugins/zeropad/Linux/gui.cpp index d2dd512a7d..b7b6bccfb2 100644 --- a/plugins/zeropad/Linux/gui.cpp +++ b/plugins/zeropad/Linux/gui.cpp @@ -20,10 +20,7 @@ #include #include -#define JOYSTICK_SUPPORT -#ifdef JOYSTICK_SUPPORT #include "joystick.h" -#endif #include "zeropad.h" #include "linux.h" diff --git a/plugins/zeropad/Linux/linux.cpp b/plugins/zeropad/Linux/linux.cpp index 8378fe5295..250e956fd1 100644 --- a/plugins/zeropad/Linux/linux.cpp +++ b/plugins/zeropad/Linux/linux.cpp @@ -48,9 +48,7 @@ s32 _PADopen(void *pDsp) s_keyRelease[0] = s_keyRelease[1] = 0; XAutoRepeatOff(GSdsp); -#ifdef JOYSTICK_SUPPORT JoystickInfo::EnumerateJoysticks(s_vjoysticks); -#endif return 0; } @@ -100,6 +98,16 @@ int _GetJoystickIdFromPAD(int pad) } } } + + if ((joyid < 0) || (joyid >= (int)s_vjoysticks.size())) + { + // get first unused joystick + for (joyid = 0; joyid < s_vjoysticks.size(); ++joyid) + { + if (s_vjoysticks[joyid]->GetPAD() < 0) break; + } + } + return joyid; } @@ -186,8 +194,6 @@ void CALLBACK PADupdate(int pad) } // joystick info -#ifdef JOYSTICK_SUPPORT - SDL_JoystickUpdate(); for (int i = 0; i < PADKEYS; i++) @@ -202,22 +208,24 @@ void CALLBACK PADupdate(int pad) if ((joyid >= 0) && (joyid < (int)s_vjoysticks.size())) { pjoy = s_vjoysticks[joyid]; + int pad = (pjoy)->GetPAD(); + int value = SDL_JoystickGetButton((pjoy)->GetJoy(), PAD_GETJOYBUTTON(key)); - if (SDL_JoystickGetButton((pjoy)->GetJoy(), PAD_GETJOYBUTTON(key))) - clear_bit(status[(pjoy)->GetPAD()], i); // pressed + if (value) + clear_bit(status[pad], i); // pressed else - set_bit(status[(pjoy)->GetPAD()], i); // pressed + set_bit(status[pad], i); // pressed } } else if (IS_JOYSTICK(key)) { int joyid = PAD_GETJOYID(key); + if ((joyid >= 0) && (joyid < (int)s_vjoysticks.size())) { - pjoy = s_vjoysticks[joyid]; - int value = SDL_JoystickGetAxis((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); int pad = (pjoy)->GetPAD(); + int value = SDL_JoystickGetAxis((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); switch (i) { @@ -233,34 +241,16 @@ void CALLBACK PADupdate(int pad) } } } - else if (IS_POV(key)) - { - int joyid = PAD_GETJOYID(key); - if (joyid >= 0 && (joyid < (int)s_vjoysticks.size())) - { - pjoy = s_vjoysticks[joyid]; - - int value = SDL_JoystickGetAxis((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); - int pad = (pjoy)->GetPAD(); - - if (PAD_GETPOVSIGN(key) && (value < -2048)) - clear_bit(status[pad], i); - else if (!PAD_GETPOVSIGN(key) && (value > 2048)) - clear_bit(status[pad], i); - else - set_bit(status[pad], i); - } - } #ifdef EXPERIMENTAL_POV_CODE else if (IS_HAT(key)) { int joyid = PAD_GETJOYID(key); + if ((joyid >= 0) && (joyid < (int)s_vjoysticks.size())) { pjoy = s_vjoysticks[joyid]; - - int value = SDL_JoystickGetHat((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); int pad = (pjoy)->GetPAD(); + int value = SDL_JoystickGetHat((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); //PAD_LOG("Hat = %d for key %d\n", PAD_GETPOVDIR(key), key); if ((value != SDL_HAT_CENTERED) && (PAD_GETHATDIR(key) == value)) @@ -268,45 +258,12 @@ void CALLBACK PADupdate(int pad) 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); } - switch (i) - { - case PAD_LEFT: - PAD_LOG("Hat Left!\n"); - break; - case PAD_DOWN: - PAD_LOG("Hat Down!\n"); - break; - case PAD_RIGHT: - PAD_LOG("Hat Right!\n"); - break; - case PAD_UP: - PAD_LOG("Hat Up!\n"); - break; - } - - switch (value) - { - case SDL_HAT_UP: - PAD_LOG("D-pad Up!\n"); - break; - - case SDL_HAT_RIGHT: - PAD_LOG("D-pad Right!\n"); - break; - - case SDL_HAT_DOWN: - PAD_LOG("D-pad Down!\n"); - break; - - case SDL_HAT_LEFT: - PAD_LOG("D-pad Left!\n"); - break; - } } else { @@ -315,8 +272,27 @@ void CALLBACK PADupdate(int pad) } } #endif + else if (IS_POV(key)) + { + int joyid = PAD_GETJOYID(key); + + if (joyid >= 0 && (joyid < (int)s_vjoysticks.size())) + { + pjoy = s_vjoysticks[joyid]; + int pad = (pjoy)->GetPAD(); + int value = SDL_JoystickGetAxis((pjoy)->GetJoy(), PAD_GETJOYSTICK_AXIS(key)); + + if (PAD_GETPOVSIGN(key) && (value < -2048)) + clear_bit(status[pad], i); + else if (!PAD_GETPOVSIGN(key) && (value > 2048)) + clear_bit(status[pad], i); + else + set_bit(status[pad], i); + + + } + } } -#endif pthread_spin_lock(&s_mutexStatus); s_keyPress[pad] |= keyPress; @@ -335,6 +311,7 @@ void UpdateConf(int pad) GtkWidget *Btn; for (i = 0; i < ArraySize(s_pGuiKeyMap); i++) { + string tmp; if (s_pGuiKeyMap[i] == NULL) continue; @@ -344,8 +321,7 @@ void UpdateConf(int pad) PAD_LOG("ZeroPAD: cannot find key %s\n", s_pGuiKeyMap[i]); continue; } - - string tmp; + if (IS_KEYBOARD(conf.keys[pad][i])) { char* pstr = XKeysymToString(PAD_GETKEY(conf.keys[pad][i])); @@ -353,44 +329,49 @@ void UpdateConf(int pad) } else if (IS_JOYBUTTONS(conf.keys[pad][i])) { + int button = PAD_GETJOYBUTTON(conf.keys[pad][i]); tmp.resize(28); - sprintf(&tmp[0], "JBut %d", PAD_GETJOYBUTTON(conf.keys[pad][i])); + + sprintf(&tmp[0], "JBut %d", button); } else if (IS_JOYSTICK(conf.keys[pad][i])) { + int axis = PAD_GETJOYSTICK_AXIS(conf.keys[pad][i]); tmp.resize(28); - sprintf(&tmp[0], "JAxis %d", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i])); + + sprintf(&tmp[0], "JAxis %d", axis); } #ifdef EXPERIMENTAL_POV_CODE else if (IS_HAT(conf.keys[pad][i])) { + int axis = PAD_GETJOYSTICK_AXIS(conf.keys[pad][i]); tmp.resize(28); + switch(PAD_GETHATDIR(conf.keys[pad][i])) { case SDL_HAT_UP: - sprintf(&tmp[0], "JPOVU-%d", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i])); + sprintf(&tmp[0], "JPOVU-%d", axis); break; case SDL_HAT_RIGHT: - sprintf(&tmp[0], "JPOVR-%d", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i])); + sprintf(&tmp[0], "JPOVR-%d", axis); break; case SDL_HAT_DOWN: - sprintf(&tmp[0], "JPOVD-%d", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i])); + sprintf(&tmp[0], "JPOVD-%d", axis); break; case SDL_HAT_LEFT: - sprintf(&tmp[0], "JPOVL-%d", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i])); + sprintf(&tmp[0], "JPOVL-%d", axis); break; } } -#else +#endif else if (IS_POV(conf.keys[pad][i])) { tmp.resize(28); sprintf(&tmp[0], "JPOV %d%s", PAD_GETJOYSTICK_AXIS(conf.keys[pad][i]), PAD_GETPOVSIGN(conf.keys[pad][i]) ? "-" : "+"); } -#endif if (tmp.size() > 0) { @@ -404,17 +385,8 @@ void UpdateConf(int pad) // check bounds int joyid = _GetJoystickIdFromPAD(pad); - - if ((joyid < 0) || (joyid >= (int)s_vjoysticks.size())) - { - // get first unused joystick - for (joyid = 0; joyid < s_vjoysticks.size(); ++joyid) - { - if (s_vjoysticks[joyid]->GetPAD() < 0) break; - } - } - if (joyid >= 0 && joyid < (int)s_vjoysticks.size()) + if ((joyid >= 0) && (joyid < (int)s_vjoysticks.size())) gtk_combo_box_set_active(GTK_COMBO_BOX(s_devicecombo), joyid); // select the combo else gtk_combo_box_set_active(GTK_COMBO_BOX(s_devicecombo), s_vjoysticks.size()); // no gamepad @@ -446,21 +418,7 @@ int GetLabelId(GtkWidget *label) return (int)(uptr)gtk_object_get_user_data(GTK_OBJECT(label)); } -void UpdateJoysticks(vector::iterator itjoy) -{ - itjoy = s_vjoysticks.begin(); - - SDL_JoystickUpdate(); - - // Save everything in the vector s_vjoysticks. - while (itjoy != s_vjoysticks.end()) - { - (*itjoy)->SaveState(); - itjoy++; - } -} - -bool PollKeyboard(char* &temp, u32* &pkey) +bool PollKeyboard(char* &temp, u32 &pkey) { GdkEvent *ev = gdk_event_get(); @@ -472,15 +430,14 @@ bool PollKeyboard(char* &temp, u32* &pkey) if (ev->key.keyval == GDK_Escape) { temp = "Unknown"; - *pkey = NULL; + pkey = NULL; } else { temp = XKeysymToString(ev->key.keyval); - *pkey = ev->key.keyval; + pkey = ev->key.keyval; } - return true; } } @@ -488,170 +445,64 @@ bool PollKeyboard(char* &temp, u32* &pkey) return false; } -bool PollButtons(vector::iterator itjoy, int &jbutton, u32* &pkey) -{ - // MAKE sure to look for changes in the state!! - for (int i = 0; i < (*itjoy)->GetNumButtons(); ++i) - { - int but = SDL_JoystickGetButton((*itjoy)->GetJoy(), i); - - if (but != (*itjoy)->GetButtonState(i)) - { - if (!but) // released, we don't really want this - { - (*itjoy)->SetButtonState(i, 0); - break; - } - - *pkey = PAD_JOYBUTTON((*itjoy)->GetId(), i); - jbutton = i; - return true; - } - } - - return false; -} - -bool PollAxes(vector::iterator itjoy, bool pov, int &jbutton, bool &negative, u32* &pkey) -{ - for (int i = 0; i < (*itjoy)->GetNumAxes(); ++i) - { - int value = SDL_JoystickGetAxis((*itjoy)->GetJoy(), i); - - if (value != (*itjoy)->GetAxisState(i)) - { - PAD_LOG("Change in joystick %d: %d.\n", i, value); - - if (abs(value) <= (*itjoy)->GetAxisState(i)) // we don't want this - { - // released, we don't really want this - (*itjoy)->SetAxisState(i, value); - break; - } - - if (abs(value) > 0x3fff) - { - jbutton = i; - - if (pov) - { - negative = (value < 0); - *pkey = PAD_POV((*itjoy)->GetId(), negative, i); - } - else // axis - { - *pkey = PAD_JOYSTICK((*itjoy)->GetId(), i); - } - return true; - } - } - } - return false; -} - -/*SDL_HAT_CENTERED, -SDL_HAT_UP, SDL_HAT_RIGHT, -SDL_HAT_DOWN, SDL_HAT_LEFT, -SDL_HAT_RIGHTUP, SDL_HAT_RIGHTDOWN, -SDL_HAT_LEFTUP, SDL_HAT_LEFTDOWN*/ - -bool PollHAT(vector::iterator itjoy, int &jbutton, int &dir, u32* &pkey) -{ -#ifdef EXPERIMENTAL_POV_CODE - for (int i = 0; i < (*itjoy)->GetNumPOV(); ++i) - { - int value = SDL_JoystickGetHat((*itjoy)->GetJoy(), i); - - if (value != SDL_HAT_CENTERED) - { - switch (value) - { - case SDL_HAT_UP: - case SDL_HAT_RIGHT: - case SDL_HAT_DOWN: - case SDL_HAT_LEFT: - *pkey = PAD_HAT((*itjoy)->GetId(), value, i); - jbutton = i; - dir = value; - PAD_LOG("Hat Pressed!"); - return true; - default: - break; - } - } - } -#endif - return false; -} - void OnConf_Key(GtkButton *button, gpointer user_data) { - u32 *pkey; GtkWidget* label = GetLabelWidget(button); + bool captured = false; + char str[32]; int id = GetLabelId(label); if (id == -1) return; int pad = id / PADKEYS; int key = id % PADKEYS; - pkey = &conf.keys[pad][key]; - // save the states -#ifdef JOYSTICK_SUPPORT - vector::iterator itjoy; + // save the joystick states + UpdateJoysticks(); - UpdateJoysticks(itjoy); -#endif - - for (;;) + while (!captured) { + vector::iterator itjoy; char *tmp; - if (PollKeyboard(tmp, pkey)) + if (PollKeyboard(tmp, conf.keys[pad][key])) { - gtk_entry_set_text(GTK_ENTRY(label), tmp); - return; + strcpy(str, tmp); + captured = true; + break; } -#ifdef JOYSTICK_SUPPORT - itjoy = s_vjoysticks.begin(); - SDL_JoystickUpdate(); - while (itjoy != s_vjoysticks.end()) + itjoy = s_vjoysticks.begin(); + while ((itjoy != s_vjoysticks.end()) && (!captured)) { int jbutton, direction; - if (PollButtons(itjoy, jbutton, pkey)) + if ((*itjoy)->PollButtons(jbutton, conf.keys[pad][key])) { - char str[32]; sprintf(str, "JBut %d", jbutton); - gtk_entry_set_text(GTK_ENTRY(label), str); - return; + captured = true; + break; } - bool negative = false, pov = (key < 16); + bool negative = false; + bool pov = (!((key == PAD_RY) || (key == PAD_LY) || (key == PAD_RX) || (key == PAD_LX))); - if (PollAxes(itjoy, pov, jbutton, negative, pkey)) + if ((*itjoy)->PollAxes(pov, jbutton, negative, conf.keys[pad][key])) { - char str[32]; - if (pov) sprintf(str, "JPOV %d%s", jbutton, (negative) ? "-" : "+"); else sprintf(str, "JAxis %d", jbutton); - - gtk_entry_set_text(GTK_ENTRY(label), str); - - return; + captured = true; + break; } #ifdef EXPERIMENTAL_POV_CODE - if (PollHAT(itjoy, jbutton, direction, pkey)) + if ((*itjoy)->PollHats(jbutton, direction, conf.keys[pad][key])) { - char str[32]; - switch (direction) { case SDL_HAT_UP: sprintf(str, "JPOVU-%d", jbutton); break; @@ -659,16 +510,15 @@ void OnConf_Key(GtkButton *button, gpointer user_data) case SDL_HAT_DOWN: sprintf(str, "JPOVD-%d", jbutton); break; case SDL_HAT_LEFT: sprintf(str, "JPOVL-%d", jbutton); break; } - - gtk_entry_set_text(GTK_ENTRY(label), str); - return; + captured = true; + break; } #endif - itjoy++; } -#endif } + + gtk_entry_set_text(GTK_ENTRY(label), str); } void CALLBACK PADconfigure() @@ -683,9 +533,7 @@ void CALLBACK PADconfigure() Conf = create_Conf(); // recreate -#ifdef JOYSTICK_SUPPORT JoystickInfo::EnumerateJoysticks(s_vjoysticks); -#endif s_devicecombo = lookup_widget(Conf, "joydevicescombo"); @@ -696,8 +544,8 @@ void CALLBACK PADconfigure() // Delete everything in the vector vjoysticks. while (it != s_vjoysticks.end()) { - sprintf(str, "%d: %s - but: %d, axes: %d, pov: %d", (*it)->GetId(), (*it)->GetName().c_str(), - (*it)->GetNumButtons(), (*it)->GetNumAxes(), (*it)->GetNumPOV()); + sprintf(str, "%d: %s - but: %d, axes: %d, hats: %d", (*it)->GetId(), (*it)->GetName().c_str(), + (*it)->GetNumButtons(), (*it)->GetNumAxes(), (*it)->GetNumHats()); gtk_combo_box_append_text(GTK_COMBO_BOX(s_devicecombo), str); it++; } diff --git a/plugins/zeropad/Linux/linux.h b/plugins/zeropad/Linux/linux.h index 0dedcde2f1..5a12f8c145 100644 --- a/plugins/zeropad/Linux/linux.h +++ b/plugins/zeropad/Linux/linux.h @@ -21,10 +21,7 @@ #include #include -#define JOYSTICK_SUPPORT -#ifdef JOYSTICK_SUPPORT #include "joystick.h" -#endif #include "zeropad.h" diff --git a/plugins/zeropad/Makefile.am b/plugins/zeropad/Makefile.am index 987d3f3dd2..3937ec3c90 100644 --- a/plugins/zeropad/Makefile.am +++ b/plugins/zeropad/Makefile.am @@ -22,5 +22,5 @@ libZeroPAD_LDFLAGS= @SHARED_LDFLAGS@ libZeroPAD_LDFLAGS+=-Wl,-soname,@ZEROPAD_SONAME@ libZeroPAD_LDADD=$(libZeroPAD_a_OBJECTS) -libZeroPAD_a_SOURCES = analog.cpp analog.h zeropad.cpp zeropad.h \ -Linux/gui.cpp Linux/linux.cpp Linux/joystick.cpp Linux/support.c Linux/interface.c +libZeroPAD_a_SOURCES = joystick.cpp analog.cpp analog.h zeropad.cpp zeropad.h \ +Linux/gui.cpp Linux/linux.cpp Linux/support.c Linux/interface.c diff --git a/plugins/zeropad/analog.cpp b/plugins/zeropad/analog.cpp index e0fd490eb9..009f45c8db 100644 --- a/plugins/zeropad/analog.cpp +++ b/plugins/zeropad/analog.cpp @@ -126,7 +126,7 @@ namespace Analog if (RevertPad(padvalue)) InvertPad(padvalue, i); SetPad(padvalue, i, Pad(padvalue, i) + 0x80); - PAD_LOG("Setting pad[%d]@%d to %d from %d\n", padvalue, i, value, temp); + //PAD_LOG("Setting pad[%d]@%d to %d from %d\n", padvalue, i, value, temp); } int AnalogToPad(int padvalue) { diff --git a/plugins/zeropad/configure.ac b/plugins/zeropad/configure.ac index db2f1a4f10..89e5c48c0a 100644 --- a/plugins/zeropad/configure.ac +++ b/plugins/zeropad/configure.ac @@ -29,14 +29,14 @@ AC_ARG_ENABLE(debug, AC_HELP_STRING([--enable-debug], [debug build]), if test "x$debug" == xyes then AC_DEFINE(_DEBUG,1,[_DEBUG]) - CFLAGS+="-g -m32 " - CXXFLAGS+="-g -m32 " - CCASFLAGS+=" -m32 " + CFLAGS+="-g -m32 -fpic " + CXXFLAGS+="-g -m32 -fpic " + CCASFLAGS+=" -m32 -fpic " else AC_DEFINE(NDEBUG,1,[NDEBUG]) - CFLAGS+="-O2 -fomit-frame-pointer -m32 " - CXXFLAGS+="-O2 -fomit-frame-pointer -m32 " - CCASFLAGS+=" -m32 " + CFLAGS+="-O2 -fomit-frame-pointer -m32 -fpic " + CXXFLAGS+="-O2 -fomit-frame-pointer -m32 -fpic " + CCASFLAGS+=" -m32 -fpic " fi AM_CONDITIONAL(DEBUGBUILD, test x$debug = xyes) AC_MSG_RESULT($debug) diff --git a/plugins/zeropad/Linux/joystick.cpp b/plugins/zeropad/joystick.cpp similarity index 54% rename from plugins/zeropad/Linux/joystick.cpp rename to plugins/zeropad/joystick.cpp index f1d05204e7..579490b694 100644 --- a/plugins/zeropad/Linux/joystick.cpp +++ b/plugins/zeropad/joystick.cpp @@ -26,6 +26,49 @@ int s_selectedpad = 0; vector s_vjoysticks; static u32 s_bSDLInit = false; +void UpdateJoysticks() +{ + vector::iterator itjoy = s_vjoysticks.begin(); + + SDL_JoystickUpdate(); + + // Save everything in the vector s_vjoysticks. + while (itjoy != s_vjoysticks.end()) + { + (*itjoy)->SaveState(); + itjoy++; + } +} + +const char *HatName(int value) +{ + switch(value) + { + case SDL_HAT_CENTERED: + return "SDL_HAT_CENTERED"; + case SDL_HAT_UP: + return "SDL_HAT_UP"; + case SDL_HAT_RIGHT: + return "SDL_HAT_RIGHT"; + case SDL_HAT_DOWN: + return "SDL_HAT_DOWN"; + case SDL_HAT_LEFT: + return "SDL_HAT_LEFT"; + case SDL_HAT_RIGHTUP: + return "SDL_HAT_RIGHTUP"; + case SDL_HAT_RIGHTDOWN: + return "SDL_HAT_RIGHTDOWN"; + case SDL_HAT_LEFTUP: + return "SDL_HAT_LEFTUP"; + case SDL_HAT_LEFTDOWN: + return "SDL_HAT_LEFTDOWN"; + default: + return "Unknown"; + } + + return "Unknown"; +} + // opens handles to all possible joysticks void JoystickInfo::EnumerateJoysticks(vector& vjoysticks) { @@ -107,14 +150,14 @@ bool JoystickInfo::Init(int id, bool bStartThread) numaxes = SDL_JoystickNumAxes(joy); numbuttons = SDL_JoystickNumButtons(joy); - numpov = SDL_JoystickNumHats(joy); + numhats = SDL_JoystickNumHats(joy); devname = SDL_JoystickName(id); vaxisstate.resize(numaxes); - vbutstate.resize(numbuttons); - vpovstate.resize(numpov); + vbuttonstate.resize(numbuttons); + vhatstate.resize(numhats); - //PAD_LOG("There are %d buttons, %d axises, and %d povs.\n", numbuttons, numaxes, numpov); + //PAD_LOG("There are %d buttons, %d axises, and %d hats.\n", numbuttons, numaxes, numhats); return true; } @@ -146,10 +189,100 @@ void JoystickInfo::SaveState() SetButtonState(i, SDL_JoystickGetButton(joy, i)); for (int i = 0; i < numaxes; ++i) SetAxisState(i, SDL_JoystickGetAxis(joy, i)); - for (int i = 0; i < numpov; ++i) - SetPOVState(i, SDL_JoystickGetHat(joy, i)); + for (int i = 0; i < numhats; ++i) + SetHatState(i, SDL_JoystickGetHat(joy, i)); } void JoystickInfo::TestForce() { } + +bool JoystickInfo::PollButtons(int &jbutton, u32 &pkey) +{ + // MAKE sure to look for changes in the state!! + for (int i = 0; i < GetNumButtons(); ++i) + { + int but = SDL_JoystickGetButton(GetJoy(), i); + + if (but != GetButtonState(i)) + { + if (!but) // released, we don't really want this + { + SetButtonState(i, 0); + break; + } + + pkey = PAD_JOYBUTTON(GetId(), i); + jbutton = i; + return true; + } + } + + return false; +} + +bool JoystickInfo::PollAxes(bool pov, int &jbutton, bool &negative, 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) + { + jbutton = i; + + if (pov) + { + negative = (value < 0); + pkey = PAD_POV(GetId(), negative, i); + } + else // axis + { + pkey = PAD_JOYSTICK(GetId(), i); + } + return true; + } + } + } + return false; +} + +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) + { + switch (value) + { + case SDL_HAT_UP: + case SDL_HAT_RIGHT: + case SDL_HAT_DOWN: + case SDL_HAT_LEFT: + pkey = PAD_HAT(GetId(), value, i); + jbutton = i; + dir = value; + PAD_LOG("Hat Pressed!"); + return true; + default: + break; + } + } + } +#endif + return false; +} diff --git a/plugins/zeropad/Linux/joystick.h b/plugins/zeropad/joystick.h similarity index 80% rename from plugins/zeropad/Linux/joystick.h rename to plugins/zeropad/joystick.h index 43cbce9f90..4f9d5b0052 100644 --- a/plugins/zeropad/Linux/joystick.h +++ b/plugins/zeropad/joystick.h @@ -41,6 +41,12 @@ class JoystickInfo void TestForce(); + bool PollButtons(int &jbutton, u32 &pkey); + + bool PollAxes(bool pov, int &jbutton, bool &negative, u32 &pkey); + + bool PollHats(int &jbutton, int &dir, u32 &pkey); + const string& GetName() { return devname; @@ -56,9 +62,9 @@ class JoystickInfo return numaxes; } - int GetNumPOV() + int GetNumHats() { - return numpov; + return numhats; } int GetId() @@ -80,7 +86,7 @@ class JoystickInfo int GetButtonState(int i) { - return vbutstate[i]; + return vbuttonstate[i]; } int GetAxisState(int i) @@ -88,15 +94,15 @@ class JoystickInfo return vaxisstate[i]; } - int GetPOVState(int i) + int GetHatState(int i) { //PAD_LOG("Getting POV State of %d.\n", i); - return vpovstate[i]; + return vhatstate[i]; } void SetButtonState(int i, int state) { - vbutstate[i] = state; + vbuttonstate[i] = state; } void SetAxisState(int i, int value) @@ -104,10 +110,10 @@ class JoystickInfo vaxisstate[i] = value; } - void SetPOVState(int i, int value) + void SetHatState(int i, int value) { //PAD_LOG("We should set %d to %d.\n", i, value); - vpovstate[i] = value; + vhatstate[i] = value; } SDL_Joystick* GetJoy() @@ -119,11 +125,11 @@ class JoystickInfo string devname; // pretty device name int _id; - int numbuttons, numaxes, numpov; + int numbuttons, numaxes, numhats; int axisrange, deadzone; int pad; - vector vbutstate, vaxisstate, vpovstate; + vector vbuttonstate, vaxisstate, vhatstate; SDL_Joystick* joy; }; @@ -131,4 +137,6 @@ class JoystickInfo extern int s_selectedpad; extern vector s_vjoysticks; +extern void UpdateJoysticks(); +extern const char *HatName(int value); #endif