Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F125942
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
69 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/util/input/allegro/allegro-joystick.cpp b/util/input/allegro/allegro-joystick.cpp
index 09e95ba1..42ba090e 100644
--- a/util/input/allegro/allegro-joystick.cpp
+++ b/util/input/allegro/allegro-joystick.cpp
@@ -1,56 +1,58 @@
#ifdef USE_ALLEGRO
#include <allegro.h>
#include "allegro-joystick.h"
void AllegroJoystick::poll(){
::poll_joystick();
}
+/*
JoystickInput AllegroJoystick::readAll(){
JoystickInput input;
JOYSTICK_INFO * info = &joy[0];
switch(info->num_buttons > 4 ? 4 : info->num_buttons){
case 4: input.button4 = info->button[3].b;
case 3: input.button3 = info->button[2].b;
case 2: input.button2 = info->button[1].b;
case 1: input.button1 = info->button[0].b;
case 0: {
break;
}
}
for (int stick = 0; stick < info->num_sticks; stick++){
JOYSTICK_STICK_INFO * this_stick = &info->stick[stick];
if (this_stick->num_axis > 0){
int position = this_stick->axis[0].pos;
if (position < 0){
input.left = true;
} else if (position > 0){
input.right = true;
}
}
if (this_stick->num_axis > 1){
int position = this_stick->axis[1].pos;
if (position < 0){
input.up = true;
} else if (position > 0){
input.down = true;
}
}
}
return input;
}
+*/
AllegroJoystick::~AllegroJoystick(){
}
AllegroJoystick::AllegroJoystick(){
}
int AllegroJoystick::getDeviceId() const {
return 0;
}
#endif
diff --git a/util/input/allegro/allegro-joystick.h b/util/input/allegro/allegro-joystick.h
index bbecff48..d0fcc463 100644
--- a/util/input/allegro/allegro-joystick.h
+++ b/util/input/allegro/allegro-joystick.h
@@ -1,19 +1,18 @@
#ifndef _paintown_allegro_joystick_h
#define _paintown_allegro_joystick_h
#include "../joystick.h"
class AllegroJoystick: public Joystick {
public:
virtual void poll();
- virtual JoystickInput readAll();
virtual int getDeviceId() const;
virtual ~AllegroJoystick();
friend class Joystick;
protected:
AllegroJoystick();
};
#endif
diff --git a/util/input/input-manager.cpp b/util/input/input-manager.cpp
index de380ff5..dbf8a4f7 100644
--- a/util/input/input-manager.cpp
+++ b/util/input/input-manager.cpp
@@ -1,238 +1,238 @@
#include "input-manager.h"
#include "configuration.h"
/* FIXME: break this dependancy. this is used for PaintownInput */
#include "paintown-engine/object/object.h"
#include "joystick.h"
#include "util/events.h"
#include "globals.h"
#include "util/debug.h"
#include <stdlib.h>
#include <vector>
using namespace std;
InputManager * InputManager::manager = 0;
InputManager::InputManager():
capture(0),
joystick(NULL){
manager = this;
if (Configuration::isJoystickEnabled()){
joystick = Joystick::create();
}
}
InputManager::~InputManager(){
delete joystick;
}
bool InputManager::anyInput(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_anyInput();
}
bool InputManager::_anyInput(){
if (keyboard.keypressed()){
return true;
}
if (joystick){
- return joystick->getEvents().size() > 0;
+ return joystick->pressed();
/*
JoystickInput all_joystick = joystick->readAll();
if (all_joystick.pressed()){
return true;
}
*/
}
return false;
}
vector<Input::PaintownInput> InputManager::getInput(const Configuration & configuration, const int facing){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_getInput(configuration, facing);
}
/*
void InputManager::enableBufferInput(){
manager->bufferKeys = true;
}
void InputManager::disableBufferInput(){
manager->bufferKeys = false;
}
*/
void InputManager::waitForClear(){
manager->keyboard.clear();
while (anyInput()){
poll();
Util::rest(1);
}
}
void InputManager::waitForKeys(int key1, int key2){
InputMap<int> wait;
wait.set(key1, 0, false, 1);
wait.set(key2, 0, false, 1);
InputManager::waitForRelease(wait, 1);
InputManager::waitForPress(wait, 1);
InputManager::waitForRelease(wait, 1);
}
void InputManager::waitForKeys(int key){
InputMap<int> wait;
wait.set(key, 0, false, 1);
InputManager::waitForRelease(wait, 1);
InputManager::waitForPress(wait, 1);
InputManager::waitForRelease(wait, 1);
}
/* these mappings should agree with configuration.cpp:defaultJoystick1Keys,
* but otherwise they are completely arbitrary
*/
static Input::PaintownInput convertJoystickKey(const Joystick::Key key, const int facing){
switch (key){
case Joystick::Up : return Input::Up;
case Joystick::Down : return Input::Down;
case Joystick::Left : {
if (facing == Paintown::Object::FACING_RIGHT){
return Input::Back;
} else {
return Input::Forward;
}
}
case Joystick::Right : {
if (facing == Paintown::Object::FACING_RIGHT){
return Input::Forward;
} else {
return Input::Back;
}
}
case Joystick::Button1 : return Input::Attack1;
case Joystick::Button2 : return Input::Attack2;
case Joystick::Button3 : return Input::Attack3;
case Joystick::Button4 : return Input::Jump;
default : return Input::Unknown;
}
}
static vector<Input::PaintownInput> convertJoystick(const Configuration & configuration, JoystickInput input, const int facing){
vector<Input::PaintownInput> all;
if (input.up){
all.push_back(convertJoystickKey(configuration.getJoystickUp(), facing));
}
if (input.right){
all.push_back(convertJoystickKey(configuration.getJoystickRight(), facing));
}
if (input.left){
all.push_back(convertJoystickKey(configuration.getJoystickLeft(), facing));
}
if (input.down){
all.push_back(convertJoystickKey(configuration.getJoystickDown(), facing));
}
if (input.button1){
all.push_back(convertJoystickKey(configuration.getJoystickAttack1(), facing));
}
if (input.button2){
all.push_back(convertJoystickKey(configuration.getJoystickAttack2(), facing));
}
if (input.button3){
all.push_back(convertJoystickKey(configuration.getJoystickAttack3(), facing));
}
if (input.button4){
all.push_back(convertJoystickKey(configuration.getJoystickJump(), facing));
}
return all;
}
void InputManager::poll(){
if (manager == 0){
Global::debug(0) << "*BUG* Input manager not set up" << endl;
exit(0);
}
return manager->_poll();
}
int InputManager::readKey(){
return manager->_readKey();
}
int InputManager::_readKey(){
std::vector<int> keys;
do{
keyboard.readKeys(keys);
if (keys.size() == 0){
Util::rest(1);
poll();
}
} while (keys.size() == 0);
return keys.front();
}
void InputManager::_poll(){
/* FIXME: not sure if its a good idea to put the event manager here */
/*
if (bufferKeys){
eventManager.enableKeyBuffer();
}
*/
eventManager.run(keyboard, joystick);
// keyboard.poll();
/*
if (joystick != NULL){
joystick->poll();
}
*/
// const vector<Util::EventManager::KeyType> & keys = eventManager.getBufferedKeys();
// bufferedKeys.insert(bufferedKeys.end(), keys.begin(), keys.end());
}
vector<Input::PaintownInput> InputManager::_getInput(const Configuration & configuration, const int facing){
InputMap<Input::PaintownInput> input;
enum Input::PaintownInput all[] = {Input::Forward, Input::Back, Input::Up, Input::Down, Input::Attack1, Input::Attack2, Input::Attack3, Input::Jump, Input::Grab};
for (unsigned int i = 0; i < sizeof(all) / sizeof(Input::PaintownInput); i++){
input.set(configuration.getKey(all[i], facing), 0, false, all[i]);
input.set(configuration.getJoystickKey(all[i], facing), 0, false, all[i]);
}
InputMap<Input::PaintownInput>::Output state = InputManager::getMap(input);
return InputMap<Input::PaintownInput>::getAllPressed(state);
/*
vector<int> all_keys;
keyboard.readKeys( all_keys );
vector<Input::PaintownInput> real_input = Input::convertKeyboard(configuration, facing, all_keys);
if (joystick != NULL){
vector<Input::PaintownInput> joystick_keys = convertJoystick(configuration, joystick->readAll(), facing);
real_input.insert(real_input.begin(), joystick_keys.begin(), joystick_keys.end());
}
return real_input;
*/
}
diff --git a/util/input/input-manager.h b/util/input/input-manager.h
index dda353f8..11d0802d 100644
--- a/util/input/input-manager.h
+++ b/util/input/input-manager.h
@@ -1,287 +1,291 @@
#ifndef _paintown_input_manager
#define _paintown_input_manager
#include <vector>
#include <algorithm>
#include "input.h"
#include "input-map.h"
#include "util/funcs.h"
#include "util/events.h"
#include "keyboard.h"
#include "exceptions/exception.h"
class Configuration;
class Joystick;
template <class Output>
class InputHandler{
public:
InputHandler(){
}
virtual ~InputHandler(){
}
virtual void press(const Output & out, Keyboard::unicode_t unicode) = 0;
virtual void release(const Output & out, Keyboard::unicode_t unicode) = 0;
};
/* handles keyboard/joystick/whatever input during the game */
class InputManager{
public:
/* main has one instance of this and thats it.
* should the janitor have the reference instead?
*/
friend int paintown_main(int, char**);
friend int main(int, char **);
/* returns true if any input device is activated (keys pressed, joystick button */
static bool anyInput();
static std::vector<Input::PaintownInput> getInput(const Configuration & configuration, const int facing);
static void poll();
/*
static void enableBufferInput();
static void disableBufferInput();
*/
static void waitForKeys(int key1, int key2);
static void waitForKeys(int key1);
static int readKey();
static void waitForClear();
/*
template <class X>
static void observeKeyboard(InputMap<X> & input){
manager->keyboard.addObserver(InputMap<X>::observeKey, &input);
}
template <class X>
static void unObserveKeyboard(InputMap<X> & input){
manager->keyboard.removeObserver(InputMap<X>::observeKey, &input);
}
*/
static std::vector<Keyboard::unicode_t> readText(){
return manager->keyboard.readText();
}
template <typename X>
static std::vector<Keyboard::unicode_t> readText(InputMap<X> & input, typename InputMap<X>::Output & output){
return manager->_readText(input, output);
}
template <typename X>
static typename InputMap<X>::Output getMap(InputMap<X> & input){
if (manager){
return manager->_getMap(input);
}
/* just crash hard.. who cares */
throw Exception::Base(__FILE__, __LINE__);
/* make the compiler happy about returning something */
return *(typename InputMap<X>::Output*)1;
}
template <typename X>
static typename std::vector<typename InputMap<X>::InputEvent> getEvents(InputMap<X> & input){
return manager->_getEvents(input);
}
template <typename X>
static void handleEvents(InputMap<X> & input, InputHandler<X> & handler){
typename std::vector<typename InputMap<X>::InputEvent> events = getEvents(input);
for (typename std::vector<typename InputMap<X>::InputEvent>::iterator it = events.begin(); it != events.end(); it++){
const typename InputMap<X>::InputEvent & event = *it;
if (event.enabled){
handler.press(event.out, event.unicode);
} else {
handler.release(event.out, event.unicode);
}
}
}
template <typename X>
static bool pressed(InputMap<X> & input, X out){
if (manager){
return manager->_pressed(input, out);
}
return false;
}
/* wait for a key to be released
* really this waits for all inputs that would result in `out'
* being generated to stop.
*/
template <typename X>
static void waitForRelease(InputMap<X> & input, X out){
while (InputManager::pressed(input, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void waitForPress(InputMap<X> & input, X out){
while (!InputManager::pressed(input, out)){
Util::rest(1);
InputManager::poll();
}
}
template <typename X>
static void captureInput(InputMap<X> & input){
manager->_captureInput(input);
}
template <typename X>
static void releaseInput(InputMap<X> & input){
manager->_releaseInput(input);
}
protected:
InputManager();
virtual ~InputManager();
virtual bool _anyInput();
virtual int _readKey();
virtual std::vector<Input::PaintownInput> _getInput(const Configuration & configuration, const int facing);
template <typename X>
void _captureInput(InputMap<X> & input){
capture = (void*) &input;
}
template <typename X>
void _releaseInput(InputMap<X> & input){
if (capture == (void*) &input){
capture = 0;
}
}
template <typename X>
std::vector<Keyboard::unicode_t> _readText(InputMap<X> & input, typename InputMap<X>::Output & output){
std::vector<Keyboard::unicode_t> text;
std::vector<Keyboard::KeyData> all = keyboard.readData();
for (std::vector<Keyboard::KeyData>::iterator it = all.begin(); it != all.end(); it++){
const Keyboard::KeyData & data = *it;
KeyState<X> * state = input.getState(data.key);
if (state != NULL && output[state->out]){
text.push_back(data.unicode);
}
}
return text;
}
void removeDuplicates(std::vector<int> & storage){
std::vector<int> output;
int last = -1;
for (std::vector<int>::iterator it = storage.begin(); it != storage.end(); it++){
if (*it != last){
output.push_back(*it);
last = *it;
}
}
storage = output;
}
template <typename X>
typename std::vector<typename InputMap<X>::InputEvent> _getEvents(InputMap<X> & input){
std::vector<typename InputMap<X>::InputEvent> events;
if (capture != NULL && capture != &input){
return events;
}
const std::vector<typename Keyboard::KeyData> & buffer = keyboard.getBufferedKeys();
for (std::vector<Keyboard::KeyData>::const_iterator it = buffer.begin(); it != buffer.end(); it++){
const Keyboard::KeyData & data = *it;
KeyState<X> * state = input.getState(data.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, data.unicode, data.enabled));
}
}
if (joystick){
const std::vector<typename Joystick::Event> & joystickEvents = joystick->getEvents();
for (std::vector<Joystick::Event>::const_iterator it = joystickEvents.begin(); it != joystickEvents.end(); it++){
Joystick::Event event = *it;
JoystickState<X> * state = input.getJoystickState(event.key);
if (state != NULL){
events.push_back(typename InputMap<X>::InputEvent(state->out, -1, event.enabled));
}
}
}
return events;
}
template <typename X>
typename InputMap<X>::Output _getMap(InputMap<X> & input){
typename InputMap<X>::Output output;
if (capture != 0 && capture != &input){
return output;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
keyboard.readBufferedKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
// all_keys.insert(all_keys.end(), keyboard.currentKeys().begin(), keyboard.currentKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// std::unique(all_keys.begin(), all_keys.end());
// bufferedKeys.clear();
input.read(all_keys, &output);
+ /*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
input.read(all_joystick, &output);
}
+ */
/* just bumps an internal counter */
input.update();
return output;
}
template <typename X>
bool _pressed(InputMap<X> & input, X result){
if (capture != 0 && capture != &input){
return false;
}
std::vector<int> all_keys;
keyboard.readKeys(all_keys);
// all_keys.insert(all_keys.end(), bufferedKeys.begin(), bufferedKeys.end());
std::sort(all_keys.begin(), all_keys.end());
removeDuplicates(all_keys);
// bufferedKeys.clear();
bool out = false;
out = input.pressed(all_keys, result);
+ /*
if (joystick != NULL){
JoystickInput all_joystick = joystick->readAll();
out |= input.pressed(all_joystick, result);
}
+ */
return out;
}
virtual void _poll();
private:
static InputManager * manager;
void * capture;
Joystick * joystick;
Keyboard keyboard;
Util::EventManager eventManager;
// std::vector<int> bufferedKeys;
// bool bufferKeys;
};
#endif
diff --git a/util/input/joystick.cpp b/util/input/joystick.cpp
index 78744046..cdba0e4e 100644
--- a/util/input/joystick.cpp
+++ b/util/input/joystick.cpp
@@ -1,88 +1,85 @@
#include <stdlib.h>
#include "joystick.h"
/*
#ifdef LINUX
#include "linux_joystick.h"
#endif
*/
#ifdef USE_ALLEGRO
#include "allegro/allegro-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
Joystick * Joystick::create(){
#ifdef USE_ALLEGRO
return new AllegroJoystick();
#endif
#ifdef USE_SDL
#ifdef WII
return new SDLJoystick();
// return new WiiJoystick();
#elif MINPSPW
return new PSPJoystick();
#else
return new SDLJoystick();
#endif
#endif
/*
#ifdef LINUX
return new LinuxJoystick();
#endif
return NULL;
*/
return NULL;
}
Joystick::Joystick(){
}
Joystick::~Joystick(){
}
-
-bool Joystick::pressed(){
- JoystickInput input = readAll();
- return input.up || input.down || input.left || input.right ||
- input.button1 || input.button2 || input.button3 || input.button4 ||
- input.quit;
-}
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 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){
}
diff --git a/util/input/joystick.h b/util/input/joystick.h
index 4d8802a1..a3f7e01a 100644
--- a/util/input/joystick.h
+++ b/util/input/joystick.h
@@ -1,108 +1,108 @@
#ifndef _paintown_joystick_h
#define _paintown_joystick_h
#include <vector>
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){
}
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){
}
/* true if something is pressed */
bool pressed(){
return left || right || up || down ||
button1 || button2 || button3 || button4 ||
button5 || button6 ||
quit;
}
bool left;
bool right;
bool up;
bool down;
bool button1;
bool button2;
bool button3;
bool button4;
bool button5;
bool button6;
bool quit;
};
class Joystick{
public:
virtual void poll() = 0;
- virtual JoystickInput readAll() = 0;
- virtual bool pressed();
+ // 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);
static Joystick * create();
enum Key{
Invalid = -1,
Up = 0,
Down,
Left,
Right,
Button1,
Button2,
Button3,
Button4,
Button5,
Button6,
Quit,
};
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;
}
static const char * keyToName(Key key);
protected:
std::vector<Event> events;
Joystick();
};
#endif
diff --git a/util/input/linux_joystick.cpp b/util/input/linux_joystick.cpp
index ef26c57f..76551d42 100644
--- a/util/input/linux_joystick.cpp
+++ b/util/input/linux_joystick.cpp
@@ -1,406 +1,390 @@
#ifdef LINUX
/* based on
* jstest.c Version 1.2
*
* Copyright (c) 1996-1999 Vojtech Pavlik
*
* Sponsored by SuSE
*/
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <sstream>
#include <linux/input.h>
#include <linux/joystick.h>
#include "linux_joystick.h"
#include "util/debug.h"
using namespace std;
static const char *axis_names[ABS_MAX + 1] = {
"X", "Y", "Z", "Rx", "Ry", "Rz", "Throttle", "Rudder",
"Wheel", "Gas", "Brake", "?", "?", "?", "?", "?",
"Hat0X", "Hat0Y", "Hat1X", "Hat1Y", "Hat2X", "Hat2Y", "Hat3X", "Hat3Y",
"?", "?", "?", "?", "?", "?", "?",
};
static const char *button_names[KEY_MAX - BTN_MISC + 1] = {
"Btn0", "Btn1", "Btn2", "Btn3", "Btn4", "Btn5", "Btn6", "Btn7", "Btn8", "Btn9", "?", "?", "?", "?", "?", "?",
"LeftBtn", "RightBtn", "MiddleBtn", "SideBtn", "ExtraBtn", "ForwardBtn", "BackBtn", "TaskBtn", "?", "?", "?", "?", "?", "?", "?", "?",
"Trigger", "ThumbBtn", "ThumbBtn2", "TopBtn", "TopBtn2", "PinkieBtn", "BaseBtn", "BaseBtn2", "BaseBtn3", "BaseBtn4", "BaseBtn5", "BaseBtn6", "BtnDead",
"BtnA", "BtnB", "BtnC", "BtnX", "BtnY", "BtnZ", "BtnTL", "BtnTR", "BtnTL2", "BtnTR2", "BtnSelect", "BtnStart", "BtnMode", "BtnThumbL", "BtnThumbR", "?",
"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?",
"WheelBtn", "Gear up",
};
/* tries to open one /dev/input/jsX and return an open file descriptor */
static int read_joystick(){
for (int num = 0; num < 100; num++){
ostringstream name;
name << "/dev/input/js" << num;
const char * cname = name.str().c_str();
int fd = open(cname, O_RDONLY);
if (fd >= 0){
return fd;
}
}
return -1;
}
LinuxJoystick::LinuxJoystick():
button(0),
axis(0){
file = read_joystick();
if (file == -1){
Global::debug(0) << "Could not open joystick" << endl;
} else {
Global::debug(1) << "Opened joystick " << endl;
/* grab useful info */
ioctl(file, JSIOCGVERSION, &version);
ioctl(file, JSIOCGAXES, &axes);
ioctl(file, JSIOCGBUTTONS, &buttons);
ioctl(file, JSIOCGNAME(NAME_LENGTH), name);
ioctl(file, JSIOCGAXMAP, axmap);
ioctl(file, JSIOCGBTNMAP, btnmap);
/* put joystick in nonblocking mode */
fcntl(file, F_SETFL, O_NONBLOCK);
axis = new int[axes];
memset(axis, 0, sizeof(int) * axes);
button = new char[buttons];
memset(button, 0, sizeof(char) * buttons);
Global::debug(1) << "Joystick axis: " << (int)axes << " buttons: " << (int)buttons << endl;
}
}
LinuxJoystick::~LinuxJoystick(){
delete[] axis;
delete[] button;
if (file != -1){
close(file);
}
}
void LinuxJoystick::poll(){
struct js_event js;
if (file == -1){
return;
}
/* from an email by Vojtech Pavlik:
* "if you #include <linux/joystick.h>, the struct is defined in such a way
* that gcc doesn't add padding."
*/
int bytes = read(file, &js, sizeof(struct js_event));
if (bytes == sizeof(struct js_event)){
Global::debug(4) << "Event: type " << (int)js.type << " time " << (int)js.time << " number " << (int)js.number << " value " << (int)js.value << endl;
if (errno != EAGAIN) {
perror("joystick: error reading");
return;
}
switch (js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
axis[js.number] = js.value;
break;
}
}
}
-bool LinuxJoystick::pressed(){
- if (file == -1){
- return false;
- }
-
- for (int i = 0; i < axes; i++){
- if (axis[i]){
- return true;
- }
- }
- for (int i = 0; i < buttons; i++){
- if (button[i]){
- return true;
- }
- }
- return false;
-}
-
+/*
JoystickInput LinuxJoystick::readAll(){
JoystickInput input;
if (file == -1){
return input;
}
if (axes > 0){
if (axis[0] < 0){
input.left = true;
}
if (axis[0] > 0){
input.right = true;
}
if (axes > 1){
if (axis[1] < 0){
input.up = true;
}
if (axis[1] > 0){
input.down = true;
}
}
}
if (buttons > 0 && button[0]){
input.button1 = true;
}
if (buttons > 1 && button[1]){
input.button2 = true;
}
if (buttons > 2 && button[2]){
input.button3 = true;
}
if (buttons > 3 && button[3]){
input.button4 = true;
}
Global::debug(1) << "joystick input up " << input.up
<< " down " << input.down
<< " left " << input.left
<< " right " << input.right
<< " button1 " << input.button1
<< " button2 " << input.button2
<< " button3 " << input.button3
<< " button4 " << input.button4
<< endl;
return input;
}
+*/
#if 0
int main (int argc, char **argv)
{
int fd, i;
unsigned char axes = 2;
unsigned char buttons = 2;
int version = 0x000800;
char name[NAME_LENGTH] = "Unknown";
uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
uint8_t axmap[ABS_MAX + 1];
if (argc < 2 || argc > 3 || !strcmp("--help", argv[1])) {
puts("");
puts("Usage: jstest [<mode>] <device>");
puts("");
puts("Modes:");
puts(" --normal One-line mode showing immediate status");
puts(" --old Same as --normal, using 0.x interface");
puts(" --event Prints events as they come in");
puts(" --nonblock Same as --event, in nonblocking mode");
puts(" --select Same as --event, using select() call");
puts("");
return 1;
}
if ((fd = open(argv[argc - 1], O_RDONLY)) < 0) {
perror("jstest");
return 1;
}
ioctl(fd, JSIOCGVERSION, &version);
ioctl(fd, JSIOCGAXES, &axes);
ioctl(fd, JSIOCGBUTTONS, &buttons);
ioctl(fd, JSIOCGNAME(NAME_LENGTH), name);
ioctl(fd, JSIOCGAXMAP, axmap);
ioctl(fd, JSIOCGBTNMAP, btnmap);
printf("Driver version is %d.%d.%d.\n",
version >> 16, (version >> 8) & 0xff, version & 0xff);
printf("Joystick (%s) has %d axes (", name, axes);
for (i = 0; i < axes; i++)
printf("%s%s", i > 0 ? ", " : "", axis_names[axmap[i]]);
puts(")");
printf("and %d buttons (", buttons);
for (i = 0; i < buttons; i++)
printf("%s%s", i > 0 ? ", " : "", button_names[btnmap[i] - BTN_MISC]);
puts(").");
printf("Testing ... (interrupt to exit)\n");
/*
* Old (0.x) interface.
*/
if ((argc == 2 && version < 0x010000) || !strcmp("--old", argv[1])) {
struct JS_DATA_TYPE js;
while (1) {
if (read(fd, &js, JS_RETURN) != JS_RETURN) {
perror("\njstest: error reading");
return 1;
}
printf("Axes: X:%3d Y:%3d Buttons: A:%s B:%s\r",
js.x, js.y, (js.buttons & 1) ? "on " : "off", (js.buttons & 2) ? "on " : "off");
fflush(stdout);
usleep(10000);
}
}
/*
* Event interface, single line readout.
*/
if (argc == 2 || !strcmp("--normal", argv[1])) {
int *axis;
char *button;
int i;
struct js_event js;
axis = calloc(axes, sizeof(int));
button = calloc(buttons, sizeof(char));
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
switch(js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
button[js.number] = js.value;
break;
case JS_EVENT_AXIS:
axis[js.number] = js.value;
break;
}
printf("\r");
if (axes) {
printf("Axes: ");
for (i = 0; i < axes; i++)
printf("%2d:%6d ", i, axis[i]);
}
if (buttons) {
printf("Buttons: ");
for (i = 0; i < buttons; i++)
printf("%2d:%s ", i, button[i] ? "on " : "off");
}
fflush(stdout);
}
}
/*
* Event interface, events being printed.
*/
if (!strcmp("--event", argv[1])) {
struct js_event js;
while (1) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
fflush(stdout);
}
}
/*
* Reading in nonblocking mode.
*/
if (!strcmp("--nonblock", argv[1])) {
struct js_event js;
fcntl(fd, F_SETFL, O_NONBLOCK);
while (1) {
while (read(fd, &js, sizeof(struct js_event)) == sizeof(struct js_event)) {
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
if (errno != EAGAIN) {
perror("\njstest: error reading");
return 1;
}
usleep(10000);
}
}
/*
* Using select() on joystick fd.
*/
if (!strcmp("--select", argv[1])) {
struct js_event js;
struct timeval tv;
fd_set set;
tv.tv_sec = 1;
tv.tv_usec = 0;
while (1) {
FD_ZERO(&set);
FD_SET(fd, &set);
if (select(fd+1, &set, NULL, NULL, &tv)) {
if (read(fd, &js, sizeof(struct js_event)) != sizeof(struct js_event)) {
perror("\njstest: error reading");
return 1;
}
printf("Event: type %d, time %d, number %d, value %d\n",
js.type, js.time, js.number, js.value);
}
}
}
printf("jstest: unknown mode: %s\n", argv[1]);
return -1;
}
#endif
#endif
diff --git a/util/input/linux_joystick.h b/util/input/linux_joystick.h
index 8c528628..6cd2709c 100644
--- a/util/input/linux_joystick.h
+++ b/util/input/linux_joystick.h
@@ -1,36 +1,34 @@
#ifdef LINUX
#include "joystick.h"
#include <stdint.h>
#include <vector>
#include <linux/input.h>
#include <linux/joystick.h>
#define NAME_LENGTH 1024
class LinuxJoystick: public Joystick {
public:
virtual void poll();
- virtual JoystickInput readAll();
- virtual bool pressed();
virtual ~LinuxJoystick();
friend class Joystick;
protected:
LinuxJoystick();
/* file descriptor for /dev/input/jsX */
int file;
char name[NAME_LENGTH];
unsigned char axes;
unsigned char buttons;
int version;
uint16_t btnmap[KEY_MAX - BTN_MISC + 1];
uint8_t axmap[ABS_MAX + 1];
char * button;
int * axis;
};
#endif
diff --git a/util/input/psp/joystick.cpp b/util/input/psp/joystick.cpp
index d5e5e206..8b8499cd 100644
--- a/util/input/psp/joystick.cpp
+++ b/util/input/psp/joystick.cpp
@@ -1,305 +1,301 @@
#ifdef MINPSPW
// Assumes SDL
#include <SDL.h>
#include "joystick.h"
#include "util/debug.h"
void PSPJoystick::poll(){
events.clear();
sceCtrlPeekBufferPositive(&joystick, 1);
doKeys();
}
int PSPJoystick::getDeviceId() const {
/* Only one available on psp, return that */
return 0;
}
-JoystickInput PSPJoystick::readAll(){
- return buffer;
-}
-
void PSPJoystick::pressButton(int button){
/* NOTE This can easily be done in doKeys instead of feeding it back into SDL to run this */
Event event = Event(Joystick::Invalid, false);
switch (button){
case 0: event = Event(Joystick::Button1, true); break;
case 1: event = Event(Joystick::Button2, true); break;
case 2: event = Event(Joystick::Button3, true); break;
case 3: event = Event(Joystick::Button4, true); break;
case 4: event = Event(Joystick::Quit, true); break;
}
events.push_back(event);
}
void PSPJoystick::releaseButton(int button){
}
void PSPJoystick::axisMotion(int axis, int motion){
if (axis == 0){
if (motion < 0){
events.push_back(Event(Joystick::Left, true));
} else if (motion > 0){
events.push_back(Event(Joystick::Right, true));
}
} else if (axis == 1){
if (motion < 0){
events.push_back(Event(Joystick::Up, true));
} else if (motion > 0){
events.push_back(Event(Joystick::Down, true));
}
}
}
void PSPJoystick::doKeys(){
if(joystick.Buttons != 0){
/*
if(!(buffer.button7) && (joystick.Buttons & PSP_CTRL_START)){
} else if((buffer.button7) && !(joystick.Buttons & PSP_CTRL_START)){
} */
if(!(buffer.quit) && (joystick.Buttons & PSP_CTRL_SELECT)){
buffer.quit = true;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 4;
event.jbutton.type = SDL_JOYBUTTONDOWN;
event.jbutton.state = SDL_PRESSED;
SDL_PushEvent(&event);
} else if((buffer.quit) && !(joystick.Buttons & PSP_CTRL_SELECT)){
buffer.quit = false;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 4;
event.jbutton.type = SDL_JOYBUTTONUP;
event.jbutton.state = SDL_RELEASED;
SDL_PushEvent(&event);
}
if(!(buffer.button1) && (joystick.Buttons & PSP_CTRL_SQUARE)){
buffer.button1 = true;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 0;
event.jbutton.type = SDL_JOYBUTTONDOWN;
event.jbutton.state = SDL_PRESSED;
SDL_PushEvent(&event);
} else if((buffer.button1) && !(joystick.Buttons & PSP_CTRL_SQUARE)){
buffer.button1 = false;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 0;
event.jbutton.type = SDL_JOYBUTTONUP;
event.jbutton.state = SDL_RELEASED;
SDL_PushEvent(&event);
}
if(!(buffer.button2) && (joystick.Buttons & PSP_CTRL_CROSS)){
buffer.button2 = true;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 1;
event.jbutton.type = SDL_JOYBUTTONDOWN;
event.jbutton.state = SDL_PRESSED;
SDL_PushEvent(&event);
} else if((buffer.button2) && !(joystick.Buttons & PSP_CTRL_CROSS)){
buffer.button2 = false;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 1;
event.jbutton.type = SDL_JOYBUTTONUP;
event.jbutton.state = SDL_RELEASED;
SDL_PushEvent(&event);
}
if(!(buffer.button3) && (joystick.Buttons & PSP_CTRL_TRIANGLE)){
buffer.button3 = true;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 2;
event.jbutton.type = SDL_JOYBUTTONDOWN;
event.jbutton.state = SDL_PRESSED;
SDL_PushEvent(&event);
} else if((buffer.button3) && !(joystick.Buttons & PSP_CTRL_TRIANGLE)){
buffer.button3 = false;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 2;
event.jbutton.type = SDL_JOYBUTTONUP;
event.jbutton.state = SDL_RELEASED;
SDL_PushEvent(&event);
}
if(!(buffer.button4) && (joystick.Buttons & PSP_CTRL_CIRCLE)){
buffer.button4 = true;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 3;
event.jbutton.type = SDL_JOYBUTTONDOWN;
event.jbutton.state = SDL_PRESSED;
SDL_PushEvent(&event);
} else if((buffer.button4) && !(joystick.Buttons & PSP_CTRL_CIRCLE)){
buffer.button4 = false;
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jbutton.which = 0;
event.jbutton.button = 3;
event.jbutton.type = SDL_JOYBUTTONUP;
event.jbutton.state = SDL_RELEASED;
SDL_PushEvent(&event);
}
/*
if(!(buffer.button5) && (joystick.Buttons & PSP_CTRL_RTRIGGER)){
} else if((buffer.button5) && !(joystick.Buttons & PSP_CTRL_RTRIGGER)){
}
if(!(buffer.button6) && (joystick.Buttons & PSP_CTRL_LTRIGGER)){
} else if((buffer.button6) && !(joystick.Buttons & PSP_CTRL_LTRIGGER)){
}
*/
if(!(buffer.left) && (joystick.Buttons & PSP_CTRL_LEFT)){
buffer.left = true;
events.push_back(Event(Joystick::Left, true));
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 0;
event.jaxis.value = -1;
SDL_PushEvent(&event);
#endif
} else if((buffer.left) && !(joystick.Buttons & PSP_CTRL_LEFT)){
buffer.left = false;
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 0;
event.jaxis.value = 0;
SDL_PushEvent(&event);
#endif
}
if(!(buffer.right) && (joystick.Buttons & PSP_CTRL_RIGHT)){
buffer.right = true;
events.push_back(Event(Joystick::Right, true));
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 0;
event.jaxis.value = 1;
SDL_PushEvent(&event);
#endif
} else if((buffer.right) && !(joystick.Buttons & PSP_CTRL_RIGHT)){
buffer.right = false;
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 0;
event.jaxis.value = 0;
SDL_PushEvent(&event);
#endif
}
if(!(buffer.down) && (joystick.Buttons & PSP_CTRL_DOWN)){
buffer.down = true;
events.push_back(Event(Joystick::Down, true));
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 1;
event.jaxis.value = -1;
SDL_PushEvent(&event);
#endif
} else if((buffer.down) && !(joystick.Buttons & PSP_CTRL_DOWN)){
buffer.down = false;
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 1;
event.jaxis.value = 0;
SDL_PushEvent(&event);
#endif
}
if(!(buffer.up) && (joystick.Buttons & PSP_CTRL_UP)){
buffer.up = true;
events.push_back(Event(Joystick::Up, true));
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 1;
event.jaxis.value = 1;
SDL_PushEvent(&event);
#endif
} else if((buffer.up) && !(joystick.Buttons & PSP_CTRL_UP)){
buffer.up = false;
#if 0
// Create SDL joy button event
SDL_Event event;
// Default to 0
event.jaxis.which = 0;
event.jaxis.type = SDL_JOYAXISMOTION;
event.jaxis.axis = 1;
event.jaxis.value = 0;
SDL_PushEvent(&event);
#endif
}
}
}
PSPJoystick::~PSPJoystick(){
// no cleanup required
}
PSPJoystick::PSPJoystick(){
sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
}
#endif
diff --git a/util/input/psp/joystick.h b/util/input/psp/joystick.h
index 49c62e4a..71b0a5ed 100644
--- a/util/input/psp/joystick.h
+++ b/util/input/psp/joystick.h
@@ -1,34 +1,33 @@
#ifndef _paintown_psp_joystick_h
#define _paintown_psp_joystick_h
#ifdef MINPSPW
#include "../joystick.h"
#include <pspctrl.h>
class PSPJoystick: public Joystick {
public:
virtual void poll();
- virtual JoystickInput readAll();
virtual int getDeviceId() const;
virtual void pressButton(int button);
virtual void releaseButton(int button);
virtual void axisMotion(int axis, int motion);
virtual ~PSPJoystick();
friend class Joystick;
protected:
void doKeys();
JoystickInput buffer;
PSPJoystick();
// joystick
SceCtrlData joystick;
};
#endif
#endif
diff --git a/util/input/sdl/joystick.cpp b/util/input/sdl/joystick.cpp
index ff15a4d9..d756b14a 100644
--- a/util/input/sdl/joystick.cpp
+++ b/util/input/sdl/joystick.cpp
@@ -1,678 +1,680 @@
#ifdef USE_SDL
#include <SDL.h>
#include "joystick.h"
#include "util/debug.h"
#include <string>
#include <vector>
using std::string;
using std::vector;
class ButtonMapping{
public:
ButtonMapping(){
}
virtual ~ButtonMapping(){
}
virtual int toNative(int button) = 0;
virtual int fromNative(int button) = 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(int button){
return button;
}
int fromNative(int button){
return button;
}
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){
/* FIXME */
/*
if (axis == 0){
if (motion < 0){
return Joystick::Left;
} else if (motion > 0){
return Joystick::Right;
}
} else if (axis == 1){
if (motion < 0){
return Joystick::Up;
} else if (motion > 0){
return Joystick::Down;
}
}
*/
}
};
/* 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(int button){
switch (button){
case 0: return Square;
case 1: return Cross;
case 2: return Circle;
case 3: return Triangle;
case 4: return Start;
}
return button;
}
int fromNative(int button){
switch (button){
case Square: return 0;
case Cross: return 1;
case Circle: return 2;
case Triangle: return 3;
case Start: return Start;
default: return 5;
}
return button;
}
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::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(int button){
return -1;
}
int fromNative(int button){
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::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 < -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(int button){
switch (button){
case 0: return Square;
case 1: return Cross;
case 2: return Circle;
case 3: return Triangle;
case 4: return Start;
}
return button;
}
int fromNative(int button){
switch (button){
case Square: return 0;
case Cross: return 1;
case Circle: return 2;
case Triangle: return 3;
case Start: return Start;
default: return 5;
}
return button;
}
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::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(int button){
return 0;
}
int fromNative(int button){
return 0;
}
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::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(int button){
return 0;
}
int fromNative(int button){
return 0;
}
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(int button){
return 0;
}
int fromNative(int button){
return 0;
}
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::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();
}
return new DefaultButtonMapping();
}
void SDLJoystick::poll(){
events.clear();
}
static bool read_button(SDL_Joystick * joystick, int button){
return SDL_JoystickGetButton(joystick, button);
}
+/*
JoystickInput SDLJoystick::readAll(){
JoystickInput input;
return input;
if (joystick){
int buttons = SDL_JoystickNumButtons(joystick);
switch (buttons > 5 ? 5 : buttons){
case 5: input.quit = read_button(joystick, buttonMapping->toNative(4));
case 4: input.button4 = read_button(joystick, buttonMapping->toNative(3));
case 3: input.button3 = read_button(joystick, buttonMapping->toNative(2));
case 2: input.button2 = read_button(joystick, buttonMapping->toNative(1));
case 1: input.button1 = read_button(joystick, buttonMapping->toNative(0));
case 0: {
break;
}
}
}
int axis = SDL_JoystickNumAxes(joystick);
if (axis > 0){
int position = SDL_JoystickGetAxis(joystick, 0);
if (position < 0){
input.left = true;
} else if (position > 0){
input.right = true;
}
}
if (axis > 1){
int position = SDL_JoystickGetAxis(joystick, 1);
if (position < 0){
input.up = true;
} else if (position > 0){
input.down = true;
}
}
int hats = SDL_JoystickNumHats(joystick);
if (hats > 0){
int hat = SDL_JoystickGetHat(joystick, 0);
if ((hat & SDL_HAT_UP) == SDL_HAT_UP){
input.up = true;
}
if ((hat & SDL_HAT_DOWN) == SDL_HAT_DOWN){
input.down = true;
}
if ((hat & SDL_HAT_LEFT) == SDL_HAT_LEFT){
input.left = true;
}
if ((hat & SDL_HAT_RIGHT) == SDL_HAT_RIGHT){
input.right = true;
}
if ((hat & SDL_HAT_RIGHTUP) == SDL_HAT_RIGHTUP){
input.right = true;
input.up = true;
}
if ((hat & SDL_HAT_RIGHTDOWN) == SDL_HAT_RIGHTDOWN){
input.right = true;
input.down = true;
}
if ((hat & SDL_HAT_LEFTDOWN) == SDL_HAT_LEFTDOWN){
input.left = true;
input.down = true;
}
if ((hat & SDL_HAT_LEFTUP) == SDL_HAT_LEFTUP){
input.left = true;
input.up = true;
}
}
return input;
}
+*/
SDLJoystick::~SDLJoystick(){
if (joystick){
SDL_JoystickClose(joystick);
}
}
SDLJoystick::SDLJoystick():
joystick(NULL){
/* TODO: don't always open joystick 0, try to support all of them */
if (SDL_NumJoysticks() > 0){
Global::debug(1) << "Opened joystick '" << SDL_JoystickName(0) << "'" << std::endl;
joystick = SDL_JoystickOpen(0);
// printf("Opened joystick '%s'\n", SDL_JoystickName(4));
buttonMapping = makeButtonMapping(SDL_JoystickName(0));
}
}
void SDLJoystick::pressButton(int button){
// Global::debug(0) << "Pressed button " << button << std::endl;
if (joystick){
Key event = buttonMapping->toKey(button);
if (event != Invalid){
events.push_back(Event(event, true));
}
}
}
void SDLJoystick::releaseButton(int button){
if (joystick){
Key event = buttonMapping->toKey(button);
if (event != Invalid){
events.push_back(Event(event, false));
}
}
}
void SDLJoystick::hatMotion(int motion){
if (joystick){
buttonMapping->hatMotionEvents(motion, events);
}
}
void SDLJoystick::axisMotion(int axis, int motion){
// 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);
}
*/
}
}
int SDLJoystick::getDeviceId() const {
if (joystick){
return SDL_JoystickIndex(joystick);
}
return -1;
}
#endif
diff --git a/util/input/sdl/joystick.h b/util/input/sdl/joystick.h
index 380f7a15..43df47fa 100644
--- a/util/input/sdl/joystick.h
+++ b/util/input/sdl/joystick.h
@@ -1,37 +1,37 @@
#ifndef _paintown_sdl_joystick_h
#define _paintown_sdl_joystick_h
#ifdef USE_SDL
#include <SDL.h>
#include "../joystick.h"
#include "util/pointer.h"
class ButtonMapping;
class SDLJoystick: public Joystick {
public:
virtual void poll();
- virtual JoystickInput readAll();
+ // virtual JoystickInput readAll();
virtual int getDeviceId() const;
virtual void pressButton(int button);
virtual void releaseButton(int button);
virtual void axisMotion(int axis, int motion);
virtual void hatMotion(int motion);
virtual ~SDLJoystick();
friend class Joystick;
protected:
/* convert buttons between what paintown wants and what sdl wants */
int to_native_button(int button);
int from_native_button(int button);
SDLJoystick();
SDL_Joystick * joystick;
Util::ReferenceCount<ButtonMapping> buttonMapping;
};
#endif
#endif
diff --git a/util/input/wii/joystick.cpp b/util/input/wii/joystick.cpp
index 256110b3..f43659f8 100644
--- a/util/input/wii/joystick.cpp
+++ b/util/input/wii/joystick.cpp
@@ -1,139 +1,141 @@
#ifdef WII
#include "joystick.h"
#include <ogc/pad.h>
#include <wiiuse/wpad.h>
void WiiJoystick::poll(){
PAD_ScanPads();
WPAD_ScanPads();
}
+#if 0
JoystickInput WiiJoystick::readAll(){
JoystickInput input;
// Number passed should be a port 1-4 I believe same with gc pad this can be modified later for multiplayer
// Wii data
WPADData * wpad;
wpad = WPAD_Data(0);
// Gamecube
u16 gcheld = PAD_ButtonsHeld(0);
// Wii controller
if (wpad->exp.type == WPAD_EXP_CLASSIC){
// Classic controller
if(wpad->btns_h & WPAD_CLASSIC_BUTTON_UP){
input.up = true;
}
if(wpad->btns_h & WPAD_CLASSIC_BUTTON_DOWN){
input.down = true;
}
if(wpad->btns_h & WPAD_CLASSIC_BUTTON_LEFT){
input.left = true;
}
if(wpad->btns_h & WPAD_CLASSIC_BUTTON_RIGHT){
input.right = true;
}
} else if (wpad->exp.type == WPAD_EXP_NUNCHUK){
// With nunchuck
if(wpad->exp.nunchuk.js.pos.y >= 0xB0){
input.up = true;
}
if(wpad->exp.nunchuk.js.pos.y <= 0x40){
input.down = true;
}
if(wpad->exp.nunchuk.js.pos.x <= 0x40){
input.left = true;
}
if(wpad->exp.nunchuk.js.pos.x >= 0xB0){
input.right = true;
}
} else {
// No attachment
if(wpad->btns_h & WPAD_BUTTON_UP){
if(wpad->ir.valid){
input.up = true;
} else {
input.left = true;
}
}
if(wpad->btns_h & WPAD_BUTTON_DOWN){
if(wpad->ir.valid){
input.down = true;
} else {
input.right = true;
}
}
if(wpad->btns_h & WPAD_BUTTON_LEFT){
if(wpad->ir.valid){
input.left = true;
} else {
input.down = true;
}
}
if(wpad->btns_h & WPAD_BUTTON_RIGHT){
if(wpad->ir.valid){
input.right = true;
} else {
input.up = true;
}
}
}
// Gamecube controller
if(gcheld & PAD_BUTTON_UP){
input.up = true;
}
if(gcheld & PAD_BUTTON_DOWN){
input.down = true;
}
if(gcheld & PAD_BUTTON_LEFT){
input.left = true;
}
if(gcheld & PAD_BUTTON_RIGHT){
input.right = true;
}
// Check all input buttons
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_X || wpad->btns_h & WPAD_BUTTON_1 || gcheld & PAD_BUTTON_X){
input.button1 = true;
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_Y || wpad->btns_h & WPAD_BUTTON_2 || gcheld & PAD_BUTTON_Y){
input.button2 = true;
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_A || wpad->btns_h & WPAD_BUTTON_A || gcheld & PAD_BUTTON_A){
input.button3 = true;
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_B || wpad->btns_h & WPAD_BUTTON_B || gcheld & PAD_BUTTON_B){
input.button4 = true;
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_PLUS || wpad->btns_h & WPAD_BUTTON_PLUS || gcheld & PAD_BUTTON_START){
// enter
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_HOME || wpad->btns_h & WPAD_BUTTON_HOME || gcheld & PAD_BUTTON_MENU){
// esc
}
if (wpad->btns_h & WPAD_CLASSIC_BUTTON_MINUS || wpad->btns_h & WPAD_BUTTON_MINUS){
// ?
}
return input;
}
+#endif
int WiiJoystick::getDeviceId() const {
/* FIXME! Figure out the device id */
return 0;
}
WiiJoystick::~WiiJoystick(){
WPAD_Shutdown();
}
WiiJoystick::WiiJoystick(){
WPAD_Init();
PAD_Init();
}
#endif
diff --git a/util/input/wii/joystick.h b/util/input/wii/joystick.h
index 6c3a9ca5..515fa714 100644
--- a/util/input/wii/joystick.h
+++ b/util/input/wii/joystick.h
@@ -1,23 +1,22 @@
#ifndef _paintown_wii_joystick_h
#define _paintown_wii_joystick_h
#ifdef WII
#include "../joystick.h"
class WiiJoystick: public Joystick {
public:
virtual void poll();
- virtual JoystickInput readAll();
virtual int getDeviceId() const;
virtual ~WiiJoystick();
friend class Joystick;
protected:
WiiJoystick();
};
#endif
#endif
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jun 11, 10:11 AM (3 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
68314
Default Alt Text
(69 KB)
Attached To
Mode
R75 R-Tech1
Attached
Detach File
Event Timeline