Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
71 KB
Referenced Files
None
Subscribers
None
diff --git a/util/input/allegro5/joystick.cpp b/util/input/allegro5/joystick.cpp
index 3bd22c3c..8f08b34b 100644
--- a/util/input/allegro5/joystick.cpp
+++ b/util/input/allegro5/joystick.cpp
@@ -1,389 +1,389 @@
#ifdef USE_ALLEGRO5
#include "../joystick.h"
#include "joystick.h"
#include "util/debug.h"
#include <allegro5/allegro.h>
using std::vector;
using std::string;
using std::map;
class ButtonMapping{
public:
ButtonMapping(){
}
virtual ~ButtonMapping(){
}
virtual int toNative(Joystick::Key key) = 0;
virtual Joystick::Key toKey(int button) = 0;
virtual void axisMotionEvents(int stick, int axis, float position, vector<Joystick::Event> & events) = 0;
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events) = 0;
};
class DefaultMapping: public ButtonMapping {
public:
DefaultMapping(){
}
virtual int toNative(Joystick::Key key){
switch (key){
case Joystick::Button1: return 0;
case Joystick::Button2: return 1;
case Joystick::Button3: return 2;
case Joystick::Button4: return 3;
case Joystick::Button5: return 4;
case Joystick::Button6: return 5;
case Joystick::Start: return 6;
case Joystick::Quit: return 7;
case Joystick::Up: return 8;
case Joystick::Down: return 9;
case Joystick::Left: return 10;
case Joystick::Right: return 11;
case Joystick::Invalid: return -1;
}
return -1;
}
virtual Joystick::Key toKey(int button){
switch (button){
case 0: return Joystick::Button1;
case 1: return Joystick::Button2;
case 2: return Joystick::Button3;
case 3: return Joystick::Button4;
case 4: return Joystick::Button5;
case 5: return Joystick::Button6;
case 6: return Joystick::Start;
case 7: return Joystick::Quit;
case 8: return Joystick::Up;
case 9: return Joystick::Down;
case 10: return Joystick::Left;
case 11: return Joystick::Right;
}
return Joystick::Invalid;
}
virtual void axisMotionEvents(int stick, int axis, float position, vector<Joystick::Event> & events){
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
}
virtual ~DefaultMapping(){
}
};
class LogitechPrecision: public ButtonMapping {
public:
enum Buttons{
Button1 = 0,
Button2 = 1,
Button3 = 2,
Button4 = 3,
Start = 8,
Select = 9,
R2 = 7,
R1 = 5,
L2 = 6,
L1 = 4
};
int toNative(Joystick::Key key){
switch (key){
case Joystick::Button1: return Button1;
case Joystick::Button2: return Button2;
case Joystick::Button3: return Button3;
case Joystick::Button4: return Button4;
case Joystick::Button5: return L1;
case Joystick::Button6: return R1;
case Joystick::Start: return Start;
case Joystick::Quit: return Select;
case Joystick::Invalid: return -1;
case Joystick::Left:
case Joystick::Right:
case Joystick::Up:
case Joystick::Down: return -1;
}
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case Button1: return Joystick::Button1;
case Button2: return Joystick::Button2;
case Button3: return Joystick::Button3;
case Button4: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
}
return Joystick::Invalid;
}
/* axis 1. negative up, positive down
* axis 0, negative left, positive right
*/
void axisMotionEvents(int stick, int axis, float position, vector<Joystick::Event> & events){
int tolerance = 10;
const int LeftRightAxis = 0;
const int UpDownAxis = 1;
switch (stick){
case 0: {
switch (axis){
case UpDownAxis: {
if (position == -1){
events.push_back(Joystick::Event(Joystick::Up, true));
events.push_back(Joystick::Event(Joystick::Down, false));
} else if (position == 1){
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, true));
} else if (position == 0){
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
}
break;
}
case LeftRightAxis: {
if (position == -1){
events.push_back(Joystick::Event(Joystick::Left, true));
events.push_back(Joystick::Event(Joystick::Right, false));
} else if (position == 1){
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, true));
} else if (position == 0){
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
}
break;
}
default: {
}
}
}
default: {
break;
}
}
#if 0
if (axis == 0){
if (position == 0){
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
} else if (motion == -32768){
events.push_back(Joystick::Event(Joystick::Left, true));
} else if (motion == 32767){
events.push_back(Joystick::Event(Joystick::Right, true));
} else if (motion == 128){
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
} else if (motion == 1){
events.push_back(Joystick::Event(Joystick::Up, true));
} else if (motion == 255){
events.push_back(Joystick::Event(Joystick::Down, true));
}
/*
if (motion < -tolerance){
events.push_back(Joystick::Event(Joystick::Left, true));
} else if (motion > tolerance){
events.push_back(Joystick::Event(Joystick::Right, true));
} else {
/ * fake a release for left and right * /
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
}
*/
} else if (axis == 1){
if (motion < -tolerance){
events.push_back(Joystick::Event(Joystick::Up, true));
} else if (motion > tolerance){
events.push_back(Joystick::Event(Joystick::Down, true));
} else {
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
}
}
#endif
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
}
};
static Util::ReferenceCount<ButtonMapping> createMapping(ALLEGRO_JOYSTICK * joystick){
string name = al_get_joystick_name(joystick);
if (name.find("Logitech(R) Precision(TM) Gamepad") != string::npos){
return Util::ReferenceCount<ButtonMapping>(new LogitechPrecision());
}
return Util::ReferenceCount<ButtonMapping>(new DefaultMapping());
}
Allegro5Joystick::Allegro5Joystick(int id):
id(id){
queue = al_create_event_queue();
if (al_is_joystick_installed()){
al_register_event_source(queue, al_get_joystick_event_source());
}
buttons = createMapping(al_get_joystick(id));
readCustomButtons();
readCustomAxes();
}
void Allegro5Joystick::axis(int stick, int axis, float position){
/*
if (stick == 9){
Global::debug(0) << "stick " << stick << " axis " << axis << " position " << position << std::endl;
}
*/
- std::set<JoystickListener*> listeners = getListeners();
- for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ const std::set<JoystickListener*> & listeners = getListeners();
+ for (std::set<JoystickListener*>::const_iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->axisMotion(this, stick, axis, position);
}
bool handled = false;
for (std::map<Key, Axis>::iterator it = customAxis.begin(); it != customAxis.end(); it++){
Key key = it->first;
Axis & use = it->second;
if (use.stick == stick && use.axis == axis){
handled = true;
bool output = false;
/* Only output an event if the new axis position is different from the last one */
if (position >= use.low && position <= use.high){
if (use.on == false){
use.on = true;
output = true;
}
} else {
/* Not in range, so if we output an event before then output a false event */
if (use.on == true){
use.on = false;
output = true;
}
}
if (output){
events.push_back(Joystick::Event(key, use.on));
}
}
}
/* If no custom axis then use the default joystick handler */
if (!handled){
buttons->axisMotionEvents(stick, axis, position, events);
}
}
Joystick::Key Allegro5Joystick::getKey(int button){
if (customButton.find(button) != customButton.end()){
return customButton[button];
}
return buttons->toKey(button);
}
int Allegro5Joystick::getButton(Key key){
for (std::map<int, Key>::iterator it = customButton.begin(); it != customButton.end(); it++){
if (it->second == key){
return it->first;
}
}
return buttons->toNative(key);
}
void Allegro5Joystick::buttonDown(int button){
// Global::debug(0) << "Button down " << button << std::endl;
- std::set<JoystickListener*> listeners = getListeners();
- for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ const std::set<JoystickListener*> & listeners = getListeners();
+ for (std::set<JoystickListener*>::const_iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->pressButton(this, button);
}
Key event = getKey(button);
if (event != Invalid){
events.push_back(Event(event, true));
}
}
void Allegro5Joystick::buttonUp(int button){
// Global::debug(0) << "Button up " << button << std::endl;
for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->releaseButton(this, button);
}
Key event = getKey(button);
if (event != Invalid){
events.push_back(Event(event, false));
}
}
void Allegro5Joystick::poll(){
events.clear();
ALLEGRO_EVENT event;
while (al_get_next_event(queue, &event)){
switch (event.type){
case ALLEGRO_EVENT_JOYSTICK_AXIS: {
if (event.joystick.id == al_get_joystick(id)){
axis(event.joystick.stick, event.joystick.axis, event.joystick.pos);
}
break;
}
case ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN: {
if (event.joystick.id == al_get_joystick(id)){
buttonDown(event.joystick.button);
}
break;
}
case ALLEGRO_EVENT_JOYSTICK_BUTTON_UP: {
if (event.joystick.id == al_get_joystick(id)){
buttonUp(event.joystick.button);
}
break;
}
}
}
}
std::map<int, std::map<int, double> > Allegro5Joystick::getCurrentAxisValues() const {
ALLEGRO_JOYSTICK_STATE state;
ALLEGRO_JOYSTICK * joystick = al_get_joystick(id);
al_get_joystick_state(joystick, &state);
map<int, map<int, double> > out;
int sticks = al_get_joystick_num_sticks(joystick);
for (int stick = 0; stick < sticks; stick++){
int axis = al_get_joystick_num_axes(joystick, stick);
for (int i = 0; i < axis; i++){
out[stick][i] = state.stick[stick].axis[i];
}
}
return out;
}
int Allegro5Joystick::getDeviceId() const {
return id;
}
std::string Allegro5Joystick::getName() const {
return al_get_joystick_name(al_get_joystick(id));
}
Allegro5Joystick::~Allegro5Joystick(){
al_destroy_event_queue(queue);
queue = NULL;
}
int Joystick::numberOfJoysticks(){
return al_get_num_joysticks();
}
#endif
diff --git a/util/input/joystick.cpp b/util/input/joystick.cpp
index c4c73f83..0421952d 100644
--- a/util/input/joystick.cpp
+++ b/util/input/joystick.cpp
@@ -1,222 +1,222 @@
#include <stdlib.h>
#include "util/configuration.h"
#include "joystick.h"
/*
#ifdef LINUX
#include "linux_joystick.h"
#endif
*/
#ifdef USE_ALLEGRO
#include "allegro/allegro-joystick.h"
#endif
#ifdef USE_ALLEGRO5
#include "allegro5/joystick.h"
#endif
#ifdef USE_SDL
#ifdef WII
#include "wii/joystick.h"
#include "sdl/joystick.h"
#elif MINPSPW
#include "psp/joystick.h"
#else
#include "sdl/joystick.h"
#endif
#endif
JoystickListener::JoystickListener(){
}
JoystickListener::~JoystickListener(){
}
Joystick * Joystick::create(int i){
#ifdef USE_ALLEGRO
return new AllegroJoystick();
#endif
#ifdef USE_SDL
#ifdef WII
return new SDLJoystick(i);
// return new WiiJoystick();
#elif MINPSPW
return new PSPJoystick();
#else
return new SDLJoystick(i);
#endif
#endif
#ifdef USE_ALLEGRO5
return new Allegro5Joystick(i);
#endif
/* TODO: support allegro5 joystick */
/*
#ifdef LINUX
return new LinuxJoystick();
#endif
return NULL;
*/
return NULL;
}
Joystick::Joystick(){
}
Joystick::~Joystick(){
}
void Joystick::readCustomButton(Key key){
int button;
if (Configuration::getCustomButton(key, getDeviceId(), getName(), button)){
customButton[button] = key;
if (customAxis.find(key) != customAxis.end()){
customAxis.erase(key);
}
}
}
void Joystick::readCustomButtons(){
readCustomButton(Up);
readCustomButton(Down);
readCustomButton(Left);
readCustomButton(Right);
readCustomButton(Button1);
readCustomButton(Button2);
readCustomButton(Button3);
readCustomButton(Button4);
readCustomButton(Button5);
readCustomButton(Button6);
readCustomButton(Quit);
readCustomButton(Start);
}
void Joystick::readCustomAxis(Key key){
int stick, axis;
double low, high;
if (Configuration::getCustomAxis(key, getDeviceId(), getName(), stick, axis, low, high)){
Axis & use = customAxis[key];
use.stick = stick;
use.axis = axis;
use.low = low;
use.high = high;
use.on = false;
eraseCustomButton(key);
}
}
void Joystick::readCustomAxes(){
readCustomAxis(Up);
readCustomAxis(Down);
readCustomAxis(Left);
readCustomAxis(Right);
readCustomAxis(Button1);
readCustomAxis(Button2);
readCustomAxis(Button3);
readCustomAxis(Button4);
readCustomAxis(Button5);
readCustomAxis(Button6);
readCustomAxis(Quit);
readCustomAxis(Start);
}
const char * Joystick::keyToName(Key key){
switch (key){
case Invalid: return "Invalid";
case Up: return "Up";
case Down: return "Down";
case Left: return "Left";
case Right: return "Right";
case Button1: return "Button1";
case Button2: return "Button2";
case Button3: return "Button3";
case Button4: return "Button4";
case Button5: return "Button5";
case Button6: return "Button6";
case Start: return "Start";
case Quit: return "Quit";
}
return "Unknown";
}
bool Joystick::pressed() const {
return events.size() > 0;
}
void Joystick::pressButton(int button){
}
void Joystick::releaseButton(int button){
}
void Joystick::axisMotion(int axis, int motion){
}
void Joystick::hatMotion(int motion){
}
void Joystick::setCustomButton(int button, Key key){
Configuration::setCustomButton(key, getDeviceId(), getName(), button);
customButton[button] = key;
/* Can only have one unique button/axis */
if (customAxis.find(key) != customAxis.end()){
customAxis.erase(key);
}
}
void Joystick::eraseCustomButton(Key key){
std::vector<int> keys;
for (std::map<int, Key>::iterator it = customButton.begin(); it != customButton.end(); it++){
if (it->second == key){
keys.push_back(it->first);
}
}
for (std::vector<int>::iterator it = keys.begin(); it != keys.end(); it++){
customButton.erase(*it);
}
}
void Joystick::setCustomAxis(Key key, int stick, int axis, double low, double high){
Configuration::setCustomAxis(key, getDeviceId(), getName(), stick, axis, low, high);
Axis & use = customAxis[key];
use.stick = stick;
use.axis = axis;
use.low = low;
use.high = high;
use.on = false;
/* Can only have one unique button/axis */
eraseCustomButton(key);
}
bool Joystick::getAxis(Key key, int & stick, int & axis, double & low, double & high) const {
std::map<Key, Axis>::const_iterator find = customAxis.find(key);
if (find != customAxis.end()){
const Axis & use = find->second;
stick = use.stick;
axis = use.axis;
low = use.low;
high = use.high;
return true;
} else {
return false;
}
}
void Joystick::addListener(JoystickListener * listener){
listeners.insert(listener);
}
void Joystick::removeListener(JoystickListener * listener){
listeners.erase(listener);
}
std::set<JoystickListener*> Joystick::listeners;
-std::set<JoystickListener*> Joystick::getListeners(){
+const std::set<JoystickListener*> & Joystick::getListeners(){
return listeners;
}
diff --git a/util/input/joystick.h b/util/input/joystick.h
index 75d7a225..0b91efa2 100644
--- a/util/input/joystick.h
+++ b/util/input/joystick.h
@@ -1,167 +1,167 @@
#ifndef _paintown_joystick_h
#define _paintown_joystick_h
#include <vector>
#include <set>
#include <string>
#include <map>
struct JoystickInput{
JoystickInput():
left(false),
right(false),
up(false),
down(false),
button1(false),
button2(false),
button3(false),
button4(false),
button5(false),
button6(false),
quit(false),
start(false){
}
JoystickInput(const JoystickInput & copy):
left(copy.left),
right(copy.right),
up(copy.up),
down(copy.down),
button1(copy.button1),
button2(copy.button2),
button3(copy.button3),
button4(copy.button4),
button5(copy.button5),
button6(copy.button6),
quit(copy.quit),
start(copy.start){
}
/* true if something is pressed */
bool pressed(){
return left || right || up || down ||
button1 || button2 || button3 || button4 ||
button5 || button6 ||
quit || start;
}
bool left;
bool right;
bool up;
bool down;
bool button1;
bool button2;
bool button3;
bool button4;
bool button5;
bool button6;
bool quit;
bool start;
};
/* Callback for joystick actions like button presses, axis motions, etc.
*/
class Joystick;
class JoystickListener{
public:
JoystickListener();
virtual ~JoystickListener();
virtual void pressButton(Joystick * from, int button) = 0;
virtual void releaseButton(Joystick * from, int button) = 0;
virtual void axisMotion(Joystick * from, int stick, int axis, double motion) = 0;
virtual void hatMotion(Joystick * from, int motion) = 0;
};
class Joystick{
public:
virtual void poll() = 0;
// virtual JoystickInput readAll() = 0;
virtual bool pressed() const;
virtual ~Joystick();
virtual int getDeviceId() const = 0;
virtual void pressButton(int button);
virtual void releaseButton(int button);
virtual void axisMotion(int axis, int motion);
virtual void hatMotion(int motion);
virtual std::string getName() const = 0;
/* create the ith joystick */
static Joystick * create(int i);
static int numberOfJoysticks();
enum Key{
Invalid = -1,
Up = 0,
Down,
Left,
Right,
Button1,
Button2,
Button3,
Button4,
Button5,
Button6,
Quit,
Start
};
struct Event{
Event(Key key, bool enabled):
key(key), enabled(enabled){
}
Key key;
bool enabled;
};
virtual inline const std::vector<Event> & getEvents() const {
return events;
}
virtual void setCustomButton(int button, Key key);
virtual void setCustomAxis(Key key, int stick, int axis, double low, double high);
virtual Key getKey(int button) = 0;
virtual int getButton(Key key) = 0;
/* returns true if there is an axis motion for the requested key */
virtual bool getAxis(Key key, int & stick, int & axis, double & low, double & high) const;
virtual std::map<int, std::map<int, double> > getCurrentAxisValues() const = 0;
static const char * keyToName(Key key);
static void addListener(JoystickListener * listener);
static void removeListener(JoystickListener * listener);
protected:
void readCustomButtons();
void readCustomButton(Key key);
void readCustomAxis(Key key);
void readCustomAxes();
void eraseCustomButton(Key key);
- static std::set<JoystickListener*> getListeners();
+ static const std::set<JoystickListener*> & getListeners();
static std::set<JoystickListener*> listeners;
std::vector<Event> events;
std::map<int, Key> customButton;
struct Axis{
int stick;
int axis;
double low;
double high;
/* true if this axis was in range and should not output an event */
bool on;
};
std::map<Key, Axis> customAxis;
Joystick();
};
#endif
diff --git a/util/input/keyboard.cpp b/util/input/keyboard.cpp
index b2f4b5e2..5598b3c0 100644
--- a/util/input/keyboard.cpp
+++ b/util/input/keyboard.cpp
@@ -1,439 +1,469 @@
#ifdef USE_ALLEGRO
#include "allegro/keyboard.cpp"
#endif
#ifdef USE_SDL
#include "sdl/keyboard.cpp"
#endif
#ifdef USE_ALLEGRO5
#include "allegro5/keyboard.cpp"
#endif
std::vector<bool> Keyboard::repeatState;
+std::set<KeyboardListener*> Keyboard::listeners;
bool Keyboard::isNumber( int key ){
return key == Key_0 ||
key == Key_1 ||
key == Key_2 ||
key == Key_3 ||
key == Key_4 ||
key == Key_5 ||
key == Key_6 ||
key == Key_7 ||
key == Key_8 ||
key == Key_9;
}
bool Keyboard::isChar( int key ){
return key == Key_A ||
key == Key_B ||
key == Key_C ||
key == Key_D ||
key == Key_E ||
key == Key_F ||
key == Key_G ||
key == Key_H ||
key == Key_I ||
key == Key_J ||
key == Key_K ||
key == Key_L ||
key == Key_M ||
key == Key_N ||
key == Key_O ||
key == Key_P ||
key == Key_Q ||
key == Key_R ||
key == Key_S ||
key == Key_T ||
key == Key_U ||
key == Key_V ||
key == Key_W ||
key == Key_X ||
key == Key_Y ||
key == Key_Z ||
key == Key_MINUS;
}
bool Keyboard::isAlpha( int key ){
return isNumber( key ) || isChar( key );
}
const char * Keyboard::keyToName( int key ){
switch ( key ){
case Keyboard::Key_A : return "A";
case Keyboard::Key_B : return "B";
case Keyboard::Key_C : return "C";
case Keyboard::Key_D : return "D";
case Keyboard::Key_E : return "E";
case Keyboard::Key_F : return "F";
case Keyboard::Key_G : return "G";
case Keyboard::Key_H : return "H";
case Keyboard::Key_I : return "I";
case Keyboard::Key_J : return "J";
case Keyboard::Key_K : return "K";
case Keyboard::Key_L : return "L";
case Keyboard::Key_M : return "M";
case Keyboard::Key_N : return "N";
case Keyboard::Key_O : return "O";
case Keyboard::Key_P : return "P";
case Keyboard::Key_Q : return "Q";
case Keyboard::Key_R : return "R";
case Keyboard::Key_S : return "S";
case Keyboard::Key_T : return "T";
case Keyboard::Key_U : return "U";
case Keyboard::Key_V : return "V";
case Keyboard::Key_W : return "W";
case Keyboard::Key_X : return "X";
case Keyboard::Key_Y : return "Y";
case Keyboard::Key_Z : return "Z";
case Keyboard::Key_0 : return "0";
case Keyboard::Key_1 : return "1";
case Keyboard::Key_2 : return "2";
case Keyboard::Key_3 : return "3";
case Keyboard::Key_4 : return "4";
case Keyboard::Key_5 : return "5";
case Keyboard::Key_6 : return "6";
case Keyboard::Key_7 : return "7";
case Keyboard::Key_8 : return "8";
case Keyboard::Key_9 : return "9";
case Keyboard::Key_0_PAD : return "0_PAD";
case Keyboard::Key_1_PAD : return "1_PAD";
case Keyboard::Key_2_PAD : return "2_PAD";
case Keyboard::Key_3_PAD : return "3_PAD";
case Keyboard::Key_4_PAD : return "4_PAD";
case Keyboard::Key_5_PAD : return "5_PAD";
case Keyboard::Key_6_PAD : return "6_PAD";
case Keyboard::Key_7_PAD : return "7_PAD";
case Keyboard::Key_8_PAD : return "8_PAD";
case Keyboard::Key_9_PAD : return "9_PAD";
case Keyboard::Key_F1 : return "F1";
case Keyboard::Key_F2 : return "F2";
case Keyboard::Key_F3 : return "F3";
case Keyboard::Key_F4 : return "F4";
case Keyboard::Key_F5 : return "F5";
case Keyboard::Key_F6 : return "F6";
case Keyboard::Key_F7 : return "F7";
case Keyboard::Key_F8 : return "F8";
case Keyboard::Key_F9 : return "F9";
case Keyboard::Key_F10 : return "F10";
case Keyboard::Key_F11 : return "F11";
case Keyboard::Key_F12 : return "F12";
case Keyboard::Key_ESC : return "ESC";
case Keyboard::Key_TILDE : return "~";
case Keyboard::Key_MINUS : return "-";
case Keyboard::Key_EQUALS : return "=";
case Keyboard::Key_BACKSPACE : return "BACKSPACE";
case Keyboard::Key_TAB : return "TAB";
case Keyboard::Key_OPENBRACE : return "OPENBRACE";
case Keyboard::Key_CLOSEBRACE : return "CLOSEBRACE";
case Keyboard::Key_ENTER : return "ENTER";
case Keyboard::Key_COLON : return "COLON";
case Keyboard::Key_QUOTE : return "QUOTE";
case Keyboard::Key_BACKSLASH : return "BACKSLASH";
case Keyboard::Key_BACKSLASH2 : return "BACKSLASH2";
case Keyboard::Key_COMMA : return "COMMA";
case Keyboard::Key_STOP : return ".";
case Keyboard::Key_SLASH : return "SLASH";
case Keyboard::Key_SPACE : return "SPACE";
case Keyboard::Key_INSERT : return "INSERT";
case Keyboard::Key_DEL : return "DEL";
case Keyboard::Key_HOME : return "HOME";
case Keyboard::Key_END : return "END";
case Keyboard::Key_PGUP : return "PGUP";
case Keyboard::Key_PGDN : return "PGDN";
case Keyboard::Key_LEFT : return "LEFT";
case Keyboard::Key_RIGHT : return "RIGHT";
case Keyboard::Key_UP : return "UP";
case Keyboard::Key_DOWN : return "DOWN";
case Keyboard::Key_SLASH_PAD : return "SLASH_PAD";
case Keyboard::Key_ASTERISK : return "ASTERISK";
case Keyboard::Key_MINUS_PAD : return "MINUS_PAD";
case Keyboard::Key_PLUS_PAD : return "PLUS_PAD";
case Keyboard::Key_DEL_PAD : return "DEL_PAD";
case Keyboard::Key_ENTER_PAD : return "ENTER_PAD";
case Keyboard::Key_PRTSCR : return "PRTSCR";
case Keyboard::Key_PAUSE : return "PAUSE";
case Keyboard::Key_ABNT_C1 : return "ABNT_C1";
case Keyboard::Key_YEN : return "YEN";
case Keyboard::Key_KANA : return "KANA";
case Keyboard::Key_CONVERT : return "CONVERT";
case Keyboard::Key_NOCONVERT : return "NOCONVERT";
case Keyboard::Key_AT : return "AT";
case Keyboard::Key_CIRCUMFLEX : return "CIRCUMFLEX";
case Keyboard::Key_COLON2 : return "COLON2";
case Keyboard::Key_KANJI : return "KANJI";
case Keyboard::Key_EQUALS_PAD : return "EQUALS_PAD";
case Keyboard::Key_BACKQUOTE : return "BACKQUOTE";
case Keyboard::Key_SEMICOLON : return "SEMICOLON";
case Keyboard::Key_COMMAND : return "COMMAND";
/*
case Keyboard::Key_UNKNOWN1 : return "UNKNOWN1";
case Keyboard::Key_UNKNOWN2 : return "UNKNOWN2";
case Keyboard::Key_UNKNOWN3 : return "UNKNOWN3";
case Keyboard::Key_UNKNOWN4 : return "UNKNOWN4";
case Keyboard::Key_UNKNOWN5 : return "UNKNOWN5";
case Keyboard::Key_UNKNOWN6 : return "UNKNOWN6";
case Keyboard::Key_UNKNOWN7 : return "UNKNOWN7";
case Keyboard::Key_UNKNOWN8 : return "UNKNOWN8";
*/
// case Keyboard::Key_MODIFIERS : return "MODIFIERS";
case Keyboard::Key_LSHIFT : return "LSHIFT";
case Keyboard::Key_RSHIFT : return "RSHIFT";
case Keyboard::Key_LCONTROL : return "LCONTROL";
case Keyboard::Key_RCONTROL : return "RCONTROL";
case Keyboard::Key_ALT : return "ALT";
case Keyboard::Key_ALTGR : return "ALTGR";
case Keyboard::Key_LWIN : return "LWIN";
case Keyboard::Key_RWIN : return "RWIN";
case Keyboard::Key_MENU : return "MENU";
case Keyboard::Key_SCRLOCK : return "SCRLOCK";
case Keyboard::Key_NUMLOCK : return "NUMLOCK";
case Keyboard::Key_CAPSLOCK : return "CAPSLOCK";
default : return "Unknown key";
}
}
void Keyboard::setDelay( const int key, const int delay ){
key_delay[ key ] = delay;
}
void Keyboard::setAllDelay( const int delay ){
setDelay( Key_A, delay );
setDelay( Key_B, delay );
setDelay( Key_C, delay );
setDelay( Key_D, delay );
setDelay( Key_E, delay );
setDelay( Key_F, delay );
setDelay( Key_G, delay );
setDelay( Key_H, delay );
setDelay( Key_I, delay );
setDelay( Key_J, delay );
setDelay( Key_K, delay );
setDelay( Key_L, delay );
setDelay( Key_M, delay );
setDelay( Key_N, delay );
setDelay( Key_O, delay );
setDelay( Key_P, delay );
setDelay( Key_Q, delay );
setDelay( Key_R, delay );
setDelay( Key_S, delay );
setDelay( Key_T, delay );
setDelay( Key_U, delay );
setDelay( Key_V, delay );
setDelay( Key_W, delay );
setDelay( Key_X, delay );
setDelay( Key_Y, delay );
setDelay( Key_Z, delay );
setDelay( Key_0, delay );
setDelay( Key_1, delay );
setDelay( Key_2, delay );
setDelay( Key_3, delay );
setDelay( Key_4, delay );
setDelay( Key_5, delay );
setDelay( Key_6, delay );
setDelay( Key_7, delay );
setDelay( Key_8, delay );
setDelay( Key_9, delay );
setDelay( Key_0_PAD, delay );
setDelay( Key_1_PAD, delay );
setDelay( Key_2_PAD, delay );
setDelay( Key_3_PAD, delay );
setDelay( Key_4_PAD, delay );
setDelay( Key_5_PAD, delay );
setDelay( Key_6_PAD, delay );
setDelay( Key_7_PAD, delay );
setDelay( Key_8_PAD, delay );
setDelay( Key_9_PAD, delay );
setDelay( Key_F1, delay );
setDelay( Key_F2, delay );
setDelay( Key_F3, delay );
setDelay( Key_F4, delay );
setDelay( Key_F5, delay );
setDelay( Key_F6, delay );
setDelay( Key_F7, delay );
setDelay( Key_F8, delay );
setDelay( Key_F9, delay );
setDelay( Key_F10, delay );
setDelay( Key_F11, delay );
setDelay( Key_F12, delay );
setDelay( Key_ESC, delay );
setDelay( Key_TILDE, delay );
setDelay( Key_MINUS, delay );
setDelay( Key_EQUALS, delay );
setDelay( Key_BACKSPACE, delay );
setDelay( Key_TAB, delay );
setDelay( Key_OPENBRACE, delay );
setDelay( Key_CLOSEBRACE, delay );
setDelay( Key_ENTER, delay );
setDelay( Key_COLON, delay );
setDelay( Key_QUOTE, delay );
setDelay( Key_BACKSLASH, delay );
setDelay( Key_BACKSLASH2, delay );
setDelay( Key_COMMA, delay );
setDelay( Key_STOP, delay );
setDelay( Key_SLASH, delay );
setDelay( Key_SPACE, delay );
setDelay( Key_INSERT, delay );
setDelay( Key_DEL, delay );
setDelay( Key_HOME, delay );
setDelay( Key_END, delay );
setDelay( Key_PGUP, delay );
setDelay( Key_PGDN, delay );
setDelay( Key_LEFT, delay );
setDelay( Key_RIGHT, delay );
setDelay( Key_UP, delay );
setDelay( Key_DOWN, delay );
setDelay( Key_SLASH_PAD, delay );
setDelay( Key_ASTERISK, delay );
setDelay( Key_MINUS_PAD, delay );
setDelay( Key_PLUS_PAD, delay );
setDelay( Key_DEL_PAD, delay );
setDelay( Key_ENTER_PAD, delay );
setDelay( Key_PRTSCR, delay );
setDelay( Key_PAUSE, delay );
setDelay( Key_ABNT_C1, delay );
setDelay( Key_YEN, delay );
setDelay( Key_KANA, delay );
setDelay( Key_CONVERT, delay );
setDelay( Key_NOCONVERT, delay );
setDelay( Key_AT, delay );
setDelay( Key_CIRCUMFLEX, delay );
setDelay( Key_COLON2, delay );
setDelay( Key_KANJI, delay );
setDelay( Key_EQUALS_PAD, delay );
setDelay( Key_BACKQUOTE, delay );
setDelay( Key_SEMICOLON, delay );
setDelay( Key_COMMAND, delay );
/*
setDelay( Key_UNKNOWN1, delay );
setDelay( Key_UNKNOWN2, delay );
setDelay( Key_UNKNOWN3, delay );
setDelay( Key_UNKNOWN4, delay );
setDelay( Key_UNKNOWN5, delay );
setDelay( Key_UNKNOWN6, delay );
setDelay( Key_UNKNOWN7, delay );
setDelay( Key_UNKNOWN8, delay );
*/
setDelay( Key_MODIFIERS, delay );
setDelay( Key_LSHIFT, delay );
setDelay( Key_RSHIFT, delay );
setDelay( Key_LCONTROL, delay );
setDelay( Key_RCONTROL, delay );
setDelay( Key_ALT, delay );
setDelay( Key_ALTGR, delay );
setDelay( Key_LWIN, delay );
setDelay( Key_RWIN, delay );
setDelay( Key_MENU, delay );
setDelay( Key_SCRLOCK, delay );
setDelay( Key_NUMLOCK, delay );
setDelay( Key_CAPSLOCK, delay );
}
void Keyboard::press(KeyType key, unicode_t unicode){
+ std::set<KeyboardListener*> listeners = getListeners();
+ for (std::set<KeyboardListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ (*it)->press(this, key, unicode);
+ }
+
+
keyState[key].key = key;
keyState[key].unicode = unicode;
keyState[key].enabled = true;
// if (enableBuffer){
buffer.push_back(keyState[key]);
// }
/*
KeyData data(key, unicode, true);
for (std::vector<Observer>::iterator it = observers.begin(); it != observers.end(); it++){
Observer observer = (*it);
observer.callback(data, observer.extra);
}
*/
}
void Keyboard::release(KeyType key){
+ std::set<KeyboardListener*> listeners = getListeners();
+ for (std::set<KeyboardListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ (*it)->release(this, key);
+ }
+
keyState[key].enabled = false;
buffer.push_back(keyState[key]);
/*
KeyData data(key, 0, false);
for (std::vector<Observer>::iterator it = observers.begin(); it != observers.end(); it++){
Observer observer = (*it);
observer.callback(data, observer.extra);
}
*/
}
void Keyboard::readBufferedKeys(std::vector<int> & keys){
for (std::vector<KeyData>::iterator it = buffer.begin(); it != buffer.end(); it++){
const KeyData & data = *it;
keys.push_back(data.key);
}
}
std::vector<Keyboard::KeyData> Keyboard::readData(){
std::vector<Keyboard::KeyData> out;
for (std::map<KeyType, KeyData>::iterator it = keyState.begin(); it != keyState.end(); it++){
KeyType key = it->first;
const KeyData & data = it->second;
if (data.enabled){
out.push_back(data);
}
}
return out;
}
std::vector<Keyboard::unicode_t> Keyboard::readText(){
std::vector<Keyboard::unicode_t> out;
for (std::map<KeyType, KeyData>::iterator it = keyState.begin(); it != keyState.end(); it++){
KeyType key = it->first;
const KeyData & data = it->second;
if (data.enabled){
out.push_back(data.unicode);
}
}
return out;
}
void Keyboard::pushRepeatState(bool enabled){
if (enabled){
enableKeyRepeat();
} else {
disableKeyRepeat();
}
repeatState.push_back(enabled);
}
void Keyboard::popRepeatState(){
if (repeatState.size() > 0){
bool last = repeatState.back();
repeatState.pop_back();
bool enabled = true;
if (repeatState.size() > 0){
enabled = repeatState.back();
}
if (enabled != last){
if (enabled){
enableKeyRepeat();
} else {
disableKeyRepeat();
}
}
}
}
bool Keyboard::haveKeyboard(){
#if defined(WII) || defined(PS3) || defined(XENON)
return false;
#endif
return true;
}
+
+KeyboardListener::KeyboardListener(){
+}
+
+KeyboardListener::~KeyboardListener(){
+}
+
+void Keyboard::addListener(KeyboardListener * listener){
+ listeners.insert(listener);
+}
+
+void Keyboard::removeListener(KeyboardListener * listener){
+ listeners.erase(listener);
+}
+
+const std::set<KeyboardListener*> & Keyboard::getListeners(){
+ return listeners;
+}
/*
void Keyboard::addObserver(ObserverCallback observer, void * data){
observers.push_back(Observer(observer, data));
}
void Keyboard::removeObserver(ObserverCallback observer, void * data){
for (std::vector<Observer>::iterator it = observers.begin(); it != observers.end(); it++){
const Observer & what = *it;
if (what.callback == observer && what.extra == data){
observers.erase(it);
break;
}
}
}
*/
diff --git a/util/input/keyboard.h b/util/input/keyboard.h
index c5ce0c94..7fb0198f 100644
--- a/util/input/keyboard.h
+++ b/util/input/keyboard.h
@@ -1,274 +1,296 @@
#ifndef _paintown_keyboard_h
#define _paintown_keyboard_h
#include <map>
#include <vector>
+#include <set>
#include <stdint.h>
+/* FIXME: use a namespace here. Input maybe? */
+
+class KeyboardListener;
+
/* handles allegro key[] array better than keypressed()
* and readkey()
*/
class Keyboard{
private:
Keyboard();
public:
friend class InputManager;
typedef const int KeyType;
typedef uint32_t unicode_t;
struct KeyData{
KeyData():
key(-1),
unicode(-1),
enabled(false){
}
KeyData(int key, unicode_t unicode, bool enabled):
key(key),
unicode(unicode),
enabled(enabled){
}
int key;
/* Converted to a unicode character in UTF-32 */
unicode_t unicode;
/* whether the key is being pressed */
bool enabled;
};
/*
typedef void (*ObserverCallback)(const KeyData & data, void * extra);
struct Observer{
Observer():
callback(NULL),
extra(NULL){
}
Observer(ObserverCallback callback, void * extra):
callback(callback),
extra(extra){
}
ObserverCallback callback;
void * extra;
};
*/
/* poll:
* Put the keys in Allegro's key[] array into our map of int -> bool
*/
void poll();
/* wait for all keys to be released */
void wait();
/* []:
* Extract a boolean value given a key number
*/
#if 0
inline bool operator[] ( const int i ){
/* if the key has been pressed for the first time return true */
if ( my_keys[ i ] < 0 ){
my_keys[ i ] = 1;
return true;
}
bool b = my_keys[ i ] > key_delay[ i ];
if ( b ){
my_keys[ i ] = 1;
}
return b;
}
#endif
/* keypressed:
* Returns true if a key is pressed
*/
bool keypressed();
/* readKeys:
* Store all pressed keys in a user supplied vector
*/
void readKeys(std::vector< int > & all_keys);
void readBufferedKeys(std::vector<int> & keys);
std::vector<KeyData> readData();
std::vector<unicode_t> readText();
// int readKey();
void clear();
/* sets the latest repeat state. popping the state restores the previous state */
static void pushRepeatState(bool enabled);
static void popRepeatState();
/* true on systems that probably have a keyboard, like pc.
* false on ps3, wii, etc.
*/
static bool haveKeyboard();
void setDelay( const int key, const int delay );
void setAllDelay( const int delay );
inline const std::vector<KeyData> & getBufferedKeys() const {
return buffer;
}
static const char * keyToName( int key );
static bool isNumber( int key );
static bool isChar( int key );
static bool isAlpha( int key );
static KeyType Key_A;
static KeyType Key_B;
static KeyType Key_C;
static KeyType Key_D;
static KeyType Key_E;
static KeyType Key_F;
static KeyType Key_G;
static KeyType Key_H;
static KeyType Key_I;
static KeyType Key_J;
static KeyType Key_K;
static KeyType Key_L;
static KeyType Key_M;
static KeyType Key_N;
static KeyType Key_O;
static KeyType Key_P;
static KeyType Key_Q;
static KeyType Key_R;
static KeyType Key_S;
static KeyType Key_T;
static KeyType Key_U;
static KeyType Key_V;
static KeyType Key_W;
static KeyType Key_X;
static KeyType Key_Y;
static KeyType Key_Z;
static KeyType Key_0;
static KeyType Key_1;
static KeyType Key_2;
static KeyType Key_3;
static KeyType Key_4;
static KeyType Key_5;
static KeyType Key_6;
static KeyType Key_7;
static KeyType Key_8;
static KeyType Key_9;
static KeyType Key_0_PAD;
static KeyType Key_1_PAD;
static KeyType Key_2_PAD;
static KeyType Key_3_PAD;
static KeyType Key_4_PAD;
static KeyType Key_5_PAD;
static KeyType Key_6_PAD;
static KeyType Key_7_PAD;
static KeyType Key_8_PAD;
static KeyType Key_9_PAD;
static KeyType Key_F1;
static KeyType Key_F2;
static KeyType Key_F3;
static KeyType Key_F4;
static KeyType Key_F5;
static KeyType Key_F6;
static KeyType Key_F7;
static KeyType Key_F8;
static KeyType Key_F9;
static KeyType Key_F10;
static KeyType Key_F11;
static KeyType Key_F12;
static KeyType Key_ESC;
static KeyType Key_TILDE;
static KeyType Key_MINUS;
static KeyType Key_EQUALS;
static KeyType Key_BACKSPACE;
static KeyType Key_TAB;
static KeyType Key_OPENBRACE;
static KeyType Key_CLOSEBRACE;
static KeyType Key_ENTER;
static KeyType Key_COLON;
static KeyType Key_QUOTE;
static KeyType Key_BACKSLASH;
static KeyType Key_BACKSLASH2;
static KeyType Key_COMMA;
static KeyType Key_STOP;
static KeyType Key_SLASH;
static KeyType Key_SPACE;
static KeyType Key_INSERT;
static KeyType Key_DEL;
static KeyType Key_HOME;
static KeyType Key_END;
static KeyType Key_PGUP;
static KeyType Key_PGDN;
static KeyType Key_LEFT;
static KeyType Key_RIGHT;
static KeyType Key_UP;
static KeyType Key_DOWN;
static KeyType Key_SLASH_PAD;
static KeyType Key_ASTERISK;
static KeyType Key_MINUS_PAD;
static KeyType Key_PLUS_PAD;
static KeyType Key_DEL_PAD;
static KeyType Key_ENTER_PAD;
static KeyType Key_PRTSCR;
static KeyType Key_PAUSE;
static KeyType Key_ABNT_C1;
static KeyType Key_YEN;
static KeyType Key_KANA;
static KeyType Key_CONVERT;
static KeyType Key_NOCONVERT;
static KeyType Key_AT;
static KeyType Key_CIRCUMFLEX;
static KeyType Key_COLON2;
static KeyType Key_KANJI;
static KeyType Key_EQUALS_PAD;
static KeyType Key_BACKQUOTE;
static KeyType Key_SEMICOLON;
static KeyType Key_COMMAND;
/*
static KeyType Key_UNKNOWN1;
static KeyType Key_UNKNOWN2;
static KeyType Key_UNKNOWN3;
static KeyType Key_UNKNOWN4;
static KeyType Key_UNKNOWN5;
static KeyType Key_UNKNOWN6;
static KeyType Key_UNKNOWN7;
static KeyType Key_UNKNOWN8;
*/
static KeyType Key_MODIFIERS;
static KeyType Key_LSHIFT;
static KeyType Key_RSHIFT;
static KeyType Key_LCONTROL;
static KeyType Key_RCONTROL;
static KeyType Key_ALT;
static KeyType Key_ALTGR;
static KeyType Key_LWIN;
static KeyType Key_RWIN;
static KeyType Key_MENU;
static KeyType Key_SCRLOCK;
static KeyType Key_NUMLOCK;
static KeyType Key_CAPSLOCK;
void press(KeyType key, unicode_t unicode);
void release(KeyType key);
/*
virtual void addObserver(ObserverCallback observer, void * extra);
virtual void removeObserver(ObserverCallback observer, void * extra);
*/
+
+ static void addListener(KeyboardListener * listener);
+ static void removeListener(KeyboardListener * listener);
+
+ const std::set<KeyboardListener*> & getListeners();
protected:
static void disableKeyRepeat();
static void enableKeyRepeat();
+ static std::set<KeyboardListener*> listeners;
+
// std::map<int,int> my_keys;
std::map<int,int> key_delay;
// std::vector<Observer> observers;
std::map<KeyType, KeyData> keyState;
std::vector<KeyData> buffer;
bool enableBuffer;
static std::vector<bool> repeatState;
};
+class KeyboardListener{
+public:
+ KeyboardListener();
+ virtual ~KeyboardListener();
+ virtual void press(Keyboard * from, Keyboard::KeyType key, Keyboard::unicode_t unicode) = 0;
+ virtual void release(Keyboard * from, Keyboard::KeyType key) = 0;
+};
+
+
+
#endif
diff --git a/util/input/sdl/joystick.cpp b/util/input/sdl/joystick.cpp
index 1ae908a9..baa49b32 100644
--- a/util/input/sdl/joystick.cpp
+++ b/util/input/sdl/joystick.cpp
@@ -1,774 +1,774 @@
#ifdef USE_SDL
#include <SDL.h>
#include "joystick.h"
#include "util/debug.h"
#include <string>
#include <vector>
#include <exception>
using std::string;
using std::vector;
using std::map;
class ButtonMapping{
public:
ButtonMapping(){
}
virtual ~ButtonMapping(){
}
virtual int toNative(Joystick::Key key) = 0;
virtual Joystick::Key toKey(int button) = 0;
virtual void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events) = 0;
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events) = 0;
};
class DefaultButtonMapping: public ButtonMapping {
public:
int toNative(Joystick::Key key){
switch (key){
case Joystick::Button1: return 0;
case Joystick::Button2: return 1;
case Joystick::Button3: return 2;
case Joystick::Button4: return 3;
case Joystick::Quit: return 4;
case Joystick::Button5: return 5;
case Joystick::Button6: return 6;
default: return -1;
}
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case 0: return Joystick::Button1;
case 1: return Joystick::Button2;
case 2: return Joystick::Button3;
case 3: return Joystick::Button4;
case 4: return Joystick::Quit;
case 5: return Joystick::Button5;
case 6: return Joystick::Button6;
default: return Joystick::Invalid;
}
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
bool up = false;
bool down = false;
bool left = false;
bool right = false;
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: up = true; break;
case SDL_HAT_RIGHT: right = true; break;
case SDL_HAT_DOWN: down = true; break;
case SDL_HAT_LEFT: left = true; break;
case SDL_HAT_RIGHTUP: right = true; up = true; break;
case SDL_HAT_RIGHTDOWN: right = true; down = true; break;
case SDL_HAT_LEFTUP: left = true; up = true; break;
case SDL_HAT_LEFTDOWN: left = true; down = true; break;
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
}
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
bool up = false;
bool down = false;
bool left = false;
bool right = false;
if (axis == 0){
if (motion < 0){
left = true;
} else if (motion > 0){
right = true;
}
} else if (axis == 1){
if (motion < 0){
up = true;
} else if (motion > 0){
down = true;
}
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
}
};
/* used when a ps3 controller is plugged into a usb port of a normal pc */
class Playstation3Controller: public ButtonMapping {
public:
enum Buttons{
Cross = 14,
Circle = 13,
Triangle = 12,
Square = 15,
Start = 3,
Select = 0,
Up = 4,
Left = 7,
Down = 6,
Right = 5,
Stick1 = 1,
Stick2 = 2,
L2 = 8,
L1 = 10,
R2 = 9,
R1 = 11,
/* the middle ps3 button */
Ps3 = 16
};
int toNative(Joystick::Key key){
switch (key){
case Joystick::Button1: return Square;
case Joystick::Button2: return Cross;
case Joystick::Button3: return Circle;
case Joystick::Button4: return Triangle;
case Joystick::Start: return Start;
default: return -1;
}
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case Square: return Joystick::Button1;
case Cross: return Joystick::Button2;
case Circle: return Joystick::Button3;
case Triangle: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
case Up: return Joystick::Up;
case Down: return Joystick::Down;
case Left: return Joystick::Left;
case Right: return Joystick::Right;
default: return Joystick::Invalid;
}
}
/* TODO */
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
}
};
class LogitechPrecision: public ButtonMapping {
public:
enum Buttons{
Button1 = 0,
Button2 = 1,
Button3 = 2,
Button4 = 3,
Start = 8,
Select = 9,
R2 = 7,
R1 = 5,
L2 = 6,
L1 = 4
};
int toNative(Joystick::Key key){
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case Button1: return Joystick::Button1;
case Button2: return Joystick::Button2;
case Button3: return Joystick::Button3;
case Button4: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
}
return Joystick::Invalid;
}
/* axis 1. negative up, positive down
* axis 0, negative left, positive right
*/
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
int tolerance = 10;
if (axis == 0){
if (motion == 0){
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
} else if (motion == -32768){
events.push_back(Joystick::Event(Joystick::Left, true));
} else if (motion == 32767){
events.push_back(Joystick::Event(Joystick::Right, true));
} else if (motion == 128){
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
} else if (motion == 1){
events.push_back(Joystick::Event(Joystick::Up, true));
} else if (motion == 255){
events.push_back(Joystick::Event(Joystick::Down, true));
}
/*
if (motion < -tolerance){
events.push_back(Joystick::Event(Joystick::Left, true));
} else if (motion > tolerance){
events.push_back(Joystick::Event(Joystick::Right, true));
} else {
/ * fake a release for left and right * /
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
}
*/
} else if (axis == 1){
if (motion < -tolerance){
events.push_back(Joystick::Event(Joystick::Up, true));
} else if (motion > tolerance){
events.push_back(Joystick::Event(Joystick::Down, true));
} else {
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
}
}
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
}
};
/* used for the ps3 controller with psl1ght's SDL version */
class Ps3Controller: public ButtonMapping {
public:
enum Buttons{
Left = 0,
Down = 1,
Right = 2,
Up = 3,
Select = 7,
Start = 4,
Square = 8,
Cross = 9,
Circle = 10,
Triangle = 11,
L1 = 13,
R1 = 12,
L2 = 15,
R2 = 14,
L3 = 6,
R3 = 5
};
int toNative(Joystick::Key key){
switch (key){
case Joystick::Button1: return Square;
case Joystick::Button2: return Cross;
case Joystick::Button3: return Circle;
case Joystick::Button4: return Triangle;
case Joystick::Start: return Start;
default: return -1;
}
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case Square: return Joystick::Button1;
case Cross: return Joystick::Button2;
case Circle: return Joystick::Button3;
case Triangle: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
case Up: return Joystick::Up;
case Down: return Joystick::Down;
case Left: return Joystick::Left;
case Right: return Joystick::Right;
default: return Joystick::Invalid;
}
}
/* TODO */
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
}
};
class XBox360Controller: public ButtonMapping {
public:
enum Buttons{
A = 0,
B = 1,
X = 2,
Y = 3,
L1 = 4,
R1 = 5,
Start = 6,
Xbox = 7,
L3 = 8,
R3 = 9,
Select = 10
};
int toNative(Joystick::Key key){
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case A: return Joystick::Button1;
case B: return Joystick::Button2;
case X: return Joystick::Button3;
case Y: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
}
return Joystick::Invalid;
}
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
/* axis 6 and 7 are the hats. sdl passes them as hat events */
#if 0
if (axis == 6){
if (motion < 0){
events.push_back(Joystick::Event(Joystick::Left, true));
} else if (motion > 0){
events.push_back(Joystick::Event(Joystick::Right, true));
} else if (motion == 0){
/* fake a release for left and right */
events.push_back(Joystick::Event(Joystick::Left, false));
events.push_back(Joystick::Event(Joystick::Right, false));
}
} else if (axis == 7){
if (motion < 0){
events.push_back(Joystick::Event(Joystick::Up, true));
} else if (motion > 0){
events.push_back(Joystick::Event(Joystick::Down, true));
} else if (motion == 0){
events.push_back(Joystick::Event(Joystick::Up, false));
events.push_back(Joystick::Event(Joystick::Down, false));
}
}
#endif
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
bool up = false;
bool down = false;
bool left = false;
bool right = false;
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: up = true; break;
case SDL_HAT_RIGHT: right = true; break;
case SDL_HAT_DOWN: down = true; break;
case SDL_HAT_LEFT: left = true; break;
case SDL_HAT_RIGHTUP: right = true; up = true; break;
case SDL_HAT_RIGHTDOWN: right = true; down = true; break;
case SDL_HAT_LEFTUP: left = true; up = true; break;
case SDL_HAT_LEFTDOWN: left = true; down = true; break;
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
}
};
class Wiimote: public ButtonMapping {
public:
enum Buttons{
A = 0,
B = 1,
Button1 = 2,
Button2 = 3,
Minus = 4,
Plus = 5,
Home = 6
};
int toNative(Joystick::Key key){
return -1;
}
/* FIXME: need a start key */
Joystick::Key toKey(int button){
switch (button){
case A: return Joystick::Button1;
case B: return Joystick::Button2;
case Button1: return Joystick::Button3;
case Button2: return Joystick::Button4;
case Minus: return Joystick::Button5;
case Plus: return Joystick::Button6;
case Home: return Joystick::Quit;
}
return Joystick::Invalid;
}
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
/* rotate all the directions 90 degrees */
bool up = false; // right
bool down = false; // left
bool left = false; // up
bool right = false; // down
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: up = true; break;
case SDL_HAT_RIGHT: right = true; break;
case SDL_HAT_DOWN: down = true; break;
case SDL_HAT_LEFT: left = true; break;
case SDL_HAT_RIGHTUP: up = true; right = true; break;
case SDL_HAT_RIGHTDOWN: down = true; right = true; break;
case SDL_HAT_LEFTUP: up = true; left = true; break;
case SDL_HAT_LEFTDOWN: down = true; left = true; break;
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
}
};
class GamecubePad: public ButtonMapping {
public:
enum Buttons{
A = 0,
B = 1,
X = 2,
Y = 3,
Z = 4,
Start = 7
};
int toNative(Joystick::Key key){
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case A: return Joystick::Button1;
case B: return Joystick::Button2;
case X: return Joystick::Button3;
case Y: return Joystick::Button4;
case Z: return Joystick::Button5;
case Start: return Joystick::Start;
}
return Joystick::Invalid;
}
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
// printf("axis %d motion %d\n", axis, motion);
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
bool up = false;
bool down = false;
bool left = false;
bool right = false;
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: up = true; break;
case SDL_HAT_RIGHT: right = true; break;
case SDL_HAT_DOWN: down = true; break;
case SDL_HAT_LEFT: left = true; break;
case SDL_HAT_RIGHTUP: right = true; up = true; break;
case SDL_HAT_RIGHTDOWN: right = true; down = true; break;
case SDL_HAT_LEFTUP: left = true; up = true; break;
case SDL_HAT_LEFTDOWN: left = true; down = true; break;
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
}
};
class IControlPad: public ButtonMapping {
public:
enum Buttons{
A = 11,
B = 13,
X = 12,
Y = 10,
Start = 9,
Select = 8,
Up = 0,
Down = 3,
Left = 2,
Right = 1,
L1 = 4,
R1 = 14
};
int toNative(Joystick::Key key){
return -1;
}
Joystick::Key toKey(int button){
switch (button){
case A: return Joystick::Button1;
case B: return Joystick::Button2;
case X: return Joystick::Button3;
case Y: return Joystick::Button4;
case L1: return Joystick::Button5;
case R1: return Joystick::Button6;
case Up: return Joystick::Up;
case Down: return Joystick::Down;
case Left: return Joystick::Left;
case Right: return Joystick::Right;
case Start: return Joystick::Start;
case Select: return Joystick::Quit;
}
return Joystick::Invalid;
}
void axisMotionEvents(int axis, int motion, vector<Joystick::Event> & events){
// printf("axis %d motion %d\n", axis, motion);
}
virtual void hatMotionEvents(int motion, vector<Joystick::Event> & events){
/*
bool up = false;
bool down = false;
bool left = false;
bool right = false;
switch (motion){
case SDL_HAT_CENTERED: break;
case SDL_HAT_UP: up = true; break;
case SDL_HAT_RIGHT: right = true; break;
case SDL_HAT_DOWN: down = true; break;
case SDL_HAT_LEFT: left = true; break;
case SDL_HAT_RIGHTUP: right = true; up = true; break;
case SDL_HAT_RIGHTDOWN: right = true; down = true; break;
case SDL_HAT_LEFTUP: left = true; up = true; break;
case SDL_HAT_LEFTDOWN: left = true; down = true; break;
}
events.push_back(Joystick::Event(Joystick::Left, left));
events.push_back(Joystick::Event(Joystick::Right, right));
events.push_back(Joystick::Event(Joystick::Down, down));
events.push_back(Joystick::Event(Joystick::Up, up));
*/
}
};
ButtonMapping * makeButtonMapping(string name){
#ifdef PS3
return new Ps3Controller();
#endif
if (name == "Sony PLAYSTATION(R)3 Controller"){
return new Playstation3Controller();
}
if (name.find("Logitech(R) Precision(TM) Gamepad") != string::npos){
return new LogitechPrecision();
}
if (name == "Microsoft X-Box 360 pad"){
return new XBox360Controller();
}
if (name.find("Wiimote") != string::npos){
return new Wiimote();
}
if (name.find("Gamecube") != string::npos){
return new GamecubePad();
}
if (name.find("iControlPad") != string::npos){
return new IControlPad();
}
Global::debug(0) << "Unknown controller '" << name << "'. Using default mapping" << std::endl;
return new DefaultButtonMapping();
}
void SDLJoystick::poll(){
events.clear();
}
static bool read_button(SDL_Joystick * joystick, int button){
return SDL_JoystickGetButton(joystick, button);
}
SDLJoystick::~SDLJoystick(){
if (joystick){
SDL_JoystickClose(joystick);
}
}
#if 0
#include <io/pad.h>
#include <fstream>
void hack(){
padInfo padinfo;
int ok = ioPadGetInfo(&padinfo);
if (ok == 0){
std::ofstream out("/dev_hdd0/tmp/p.txt");
out << "PS3 Pad Info" << std::endl;
out << " max " << padinfo.max << std::endl;
out << " connected " << padinfo.connected << std::endl;
out << " status 0 " << (int) padinfo.status[0] << std::endl;
out << " status 1 " << (int) padinfo.status[1] << std::endl;
out << " status 2 " << (int) padinfo.status[2] << std::endl;
out << " status 3 " << (int) padinfo.status[3] << std::endl;
out << " status 4 " << (int) padinfo.status[4] << std::endl;
out.close();
} else {
Global::debug(0) << "Could not get pad info" << std::endl;
}
}
#endif
SDLJoystick::SDLJoystick(int id):
joystick(NULL){
if (SDL_NumJoysticks() > id){
joystick = SDL_JoystickOpen(id);
if (joystick == NULL){
Global::debug(0) << "Could not open joystick at index " << id << std::endl;
} else {
Global::debug(1) << "Opened joystick '" << SDL_JoystickName(id) << "'" << std::endl;
}
// printf("Opened joystick '%s'\n", SDL_JoystickName(4));
buttonMapping = makeButtonMapping(SDL_JoystickName(id));
readCustomButtons();
readCustomAxes();
}
}
Joystick::Key SDLJoystick::getKey(int button){
if (customButton.find(button) != customButton.end()){
return customButton[button];
}
return buttonMapping->toKey(button);
}
int SDLJoystick::getButton(Key key){
for (std::map<int, Key>::iterator it = customButton.begin(); it != customButton.end(); it++){
if (it->second == key){
return it->first;
}
}
return buttonMapping->toNative(key);
}
void SDLJoystick::pressButton(int button){
- std::set<JoystickListener*> listeners = getListeners();
- for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ const std::set<JoystickListener*> & listeners = getListeners();
+ for (std::set<JoystickListener*>::const_iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->pressButton(this, button);
}
// Global::debug(0) << "Pressed button " << button << std::endl;
if (joystick){
Key event = getKey(button);
if (event != Invalid){
events.push_back(Event(event, true));
}
}
}
void SDLJoystick::releaseButton(int button){
- std::set<JoystickListener*> listeners = getListeners();
- for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ const std::set<JoystickListener*> & listeners = getListeners();
+ for (std::set<JoystickListener*>::const_iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->releaseButton(this, button);
}
if (joystick){
Key event = getKey(button);
if (event != Invalid){
events.push_back(Event(event, false));
}
}
}
void SDLJoystick::hatMotion(int motion){
std::set<JoystickListener*> listeners = getListeners();
for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
(*it)->hatMotion(this, motion);
}
// Global::debug(0) << "Hat motion " << motion << std::endl;
if (joystick){
buttonMapping->hatMotionEvents(motion, events);
}
}
void SDLJoystick::axisMotion(int axis, int motion){
- std::set<JoystickListener*> listeners = getListeners();
- for (std::set<JoystickListener*>::iterator it = listeners.begin(); it != listeners.end(); it++){
+ const std::set<JoystickListener*> & listeners = getListeners();
+ for (std::set<JoystickListener*>::const_iterator it = listeners.begin(); it != listeners.end(); it++){
/* Stick is always 0.
* Motions should always fit inside a short, [-32767, 32767]
*/
(*it)->axisMotion(this, 0, axis, (double) motion / 32768.0);
}
// Global::debug(0) << "Axis motion on " << axis << " motion " << motion << std::endl;
if (joystick){
buttonMapping->axisMotionEvents(axis, motion, events);
/*
Event move = buttonMapping->axisMotionToEvent(axis, motion);
if (move.key != Invalid){
events.push_back(move);
}
*/
}
}
string SDLJoystick::getName() const {
if (joystick){
return SDL_JoystickName(getDeviceId());
}
return "";
}
int SDLJoystick::getDeviceId() const {
if (joystick){
return SDL_JoystickIndex(joystick);
}
return -1;
}
std::map<int, std::map<int, double> > SDLJoystick::getCurrentAxisValues() const {
map<int, map<int, double> > out;
if (joystick){
int axis = SDL_JoystickNumAxes(joystick);
for (int i = 0; i < axis; i++){
/* always use stick 0 */
out[0][i] = (double) SDL_JoystickGetAxis(joystick, i) / 32768.0;
}
}
return out;
}
int Joystick::numberOfJoysticks(){
return SDL_NumJoysticks();
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jun 16, 12:19 AM (2 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70398
Default Alt Text
(71 KB)

Event Timeline