00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "configuration.h"
00023 #include "joystick.h"
00024 #include "log.h"
00025
00026 #include <cassert>
00027
00028 int Joystick::joystickCount = 0;
00029
00030 void Joystick::init()
00031 {
00032 SDL_InitSubSystem(SDL_INIT_JOYSTICK);
00033
00034
00035 SDL_JoystickEventState(SDL_ENABLE);
00036
00037 joystickCount = SDL_NumJoysticks();
00038 logger->log("%i joysticks/gamepads found", joystickCount);
00039 for (int i = 0; i < joystickCount; i++)
00040 logger->log("- %s", SDL_JoystickName(i));
00041 }
00042
00043 Joystick::Joystick(int no):
00044 mDirection(0),
00045 mCalibrating(false),
00046 mEnabled(false)
00047 {
00048 assert(no < joystickCount);
00049
00050 mJoystick = SDL_JoystickOpen(no);
00051
00052
00053 if (!mJoystick)
00054 {
00055 logger->log("Couldn't open joystick: %s", SDL_GetError());
00056 return;
00057 }
00058
00059 logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick));
00060 logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick));
00061 logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick));
00062 logger->log("Buttons: %i", SDL_JoystickNumButtons(mJoystick));
00063
00064 mEnabled = (int) config.getValue("joystickEnabled", 0) != 0;
00065 mUpTolerance = (int) config.getValue("upTolerance", 100);
00066 mDownTolerance = (int) config.getValue("downTolerance", 100);
00067 mLeftTolerance = (int) config.getValue("leftTolerance", 100);
00068 mRightTolerance = (int) config.getValue("rightTolerance", 100);
00069 }
00070
00071 Joystick::~Joystick()
00072 {
00073 SDL_JoystickClose(mJoystick);
00074 }
00075
00076 void Joystick::update()
00077 {
00078 mDirection = 0;
00079
00080
00081 if (mCalibrating) {
00082 doCalibration();
00083 return;
00084 };
00085
00086 if (!mEnabled)
00087 return;
00088
00089
00090 int position = SDL_JoystickGetAxis(mJoystick, 0);
00091 if (position >= mRightTolerance)
00092 mDirection |= RIGHT;
00093 else if (position <= mLeftTolerance)
00094 mDirection |= LEFT;
00095
00096
00097 position = SDL_JoystickGetAxis(mJoystick, 1);
00098 if (position <= mUpTolerance)
00099 mDirection |= UP;
00100 else if (position >= mDownTolerance)
00101 mDirection |= DOWN;
00102
00103
00104 for (int i = 0; i < MAX_BUTTONS; i++)
00105 mButtons[i] = (SDL_JoystickGetButton(mJoystick, i) == 1);
00106 }
00107
00108 void Joystick::startCalibration()
00109 {
00110 mUpTolerance = 0;
00111 mDownTolerance = 0;
00112 mLeftTolerance = 0;
00113 mRightTolerance = 0;
00114 mCalibrating = true;
00115 }
00116
00117 void Joystick::doCalibration()
00118 {
00119
00120 int position = SDL_JoystickGetAxis(mJoystick, 0);
00121 if (position > mRightTolerance)
00122 mRightTolerance = position;
00123 else if (position < mLeftTolerance)
00124 mLeftTolerance = position;
00125
00126
00127 position = SDL_JoystickGetAxis(mJoystick, 1);
00128 if (position > mDownTolerance)
00129 mDownTolerance = position;
00130 else if (position < mUpTolerance)
00131 mUpTolerance = position;
00132 }
00133
00134 void Joystick::finishCalibration()
00135 {
00136 config.setValue("leftTolerance", mLeftTolerance);
00137 config.setValue("rightTolerance", mRightTolerance);
00138 config.setValue("upTolerance", mUpTolerance);
00139 config.setValue("downTolerance", mDownTolerance);
00140 mCalibrating = false;
00141 }
00142
00143 bool Joystick::buttonPressed(unsigned char no) const
00144 {
00145 return (mEnabled && no < MAX_BUTTONS) ? mButtons[no] : false;
00146 }