joystickCtrl.cpp
00001
00002
00003
00004
00005
00006
00007 #include "joystickCtrl.h"
00008 #include <SDL.h>
00009
00010
00011 bool JoystickCtrl::joystick_initialized = false;
00012 JoystickCtrl::JoystickResource JoystickCtrl::jsr;
00013
00014
00015 void JoystickCtrl::initJoystickSystem(void) {
00016 if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) {
00017 throw SDL_Exception();
00018 }
00019 SDL_JoystickEventState(SDL_ENABLE);
00020 jsr.num_max = SDL_NumJoysticks();
00021 jsr.js.resize(jsr.num_max);
00022 }
00023
00024
00025 void JoystickCtrl::init(void) {
00026 if (!joystick_initialized) {
00027 initJoystickSystem();
00028 joystick_initialized = true;
00029 }
00030 }
00031
00032
00033 JoystickCtrl::JoystickCtrl(void) : own_index(-1) {
00034 init();
00035 }
00036
00037
00038 JoystickCtrl::~JoystickCtrl(void) {
00039 if (own_index >= 0) {
00040 SDL_JoystickClose(jsr.js[own_index].ptr);
00041 jsr.js[own_index].ptr = NULL;
00042 jsr.js[own_index].used = false;
00043 own_index = -1;
00044 }
00045 }
00046
00047
00048 const char* JoystickCtrl::what(void) {
00049 return "";
00050 }
00051
00052
00053 int JoystickCtrl::size(void) {
00054 init();
00055 return (jsr.num_max - jsr.used_num);
00056 }
00057
00058
00059 bool JoystickCtrl::activate(int index) {
00060
00061
00062 if (index == FreeIndex) {
00063 int free_index = 0;
00064 for (std::vector<EachJoystick>::iterator it = jsr.js.begin();
00065 it != jsr.js.end(); ++it) {
00066 if (!it->used) {
00067 index = free_index;
00068 break;
00069 }
00070 ++free_index;
00071 }
00072 }
00073
00074 if ((index < 0) || (index >= static_cast<int>(jsr.js.size())) ||
00075 jsr.js[index].used) {
00076 return false;
00077 }
00078 own_index = index;
00079 EachJoystick* js = &jsr.js[own_index];
00080
00081
00082 if (!js->ptr) {
00083 js->ptr = SDL_JoystickOpen(own_index);
00084 }
00085 js->used = true;
00086
00087 js->num_axis = SDL_JoystickNumAxes(js->ptr);
00088
00089
00090 js->num_buttons = SDL_JoystickNumButtons(js->ptr);
00091 js->axis_value.resize(js->num_axis);
00092 js->buttons_value.resize(js->num_buttons);
00093
00094 for (int i = 0; i < js->num_axis; ++i) {
00095 js->axis_value[i] = 0;
00096 }
00097
00098
00099 for (int i = 0; i < js->num_buttons; ++i) {
00100 js->buttons_value[i] = false;
00101 }
00102
00103 return true;
00104 }
00105
00106
00107 void JoystickCtrl::joystickEventHandler(SDL_Event& event) {
00108
00109 switch (event.type) {
00110
00111 case SDL_JOYAXISMOTION:
00112 JoystickCtrl::joyAxisEventHandler(event.jaxis);
00113 break;
00114
00115 case SDL_JOYBALLMOTION:
00116 JoystickCtrl::joyBallEventHandler(event.jball);
00117 break;
00118
00119 case SDL_JOYHATMOTION:
00120 JoystickCtrl::joyHatEventHandler(event.jhat);
00121 break;
00122
00123 case SDL_JOYBUTTONDOWN:
00124 case SDL_JOYBUTTONUP:
00125 JoystickCtrl::joyButtonEventHandler(event.jbutton);
00126 break;
00127 }
00128 }
00129
00130
00131 void JoystickCtrl::joyAxisEventHandler(SDL_JoyAxisEvent& event) {
00132 jsr.js[event.which].axis_value[event.axis] = event.value;
00133 }
00134
00135
00136 void JoystickCtrl::joyBallEventHandler(SDL_JoyBallEvent& event) {
00137 fprintf(stderr, "ball event: Not implemented\n");
00138 }
00139
00140
00141 void JoystickCtrl::joyHatEventHandler(SDL_JoyHatEvent& event) {
00142 fprintf(stderr, "hat event: Not implemented\n");
00143 }
00144
00145
00146 void JoystickCtrl::joyButtonEventHandler(SDL_JoyButtonEvent& event) {
00147 jsr.js[event.which].buttons_value[event.button] =
00148 (event.state == SDL_PRESSED) ? true : false;
00149 }
00150
00151
00152 bool JoystickCtrl::isActivated(void) const {
00153 return (own_index >= 0) ? true : false;
00154 }
00155
00156 int JoystickCtrl::getNumAxis(void) const {
00157 return jsr.js[own_index].num_axis;
00158 }
00159
00160
00161 int JoystickCtrl::getNumButtons(void) const {
00162 return jsr.js[own_index].num_buttons;
00163 }
00164
00165
00166 short JoystickCtrl::getAxisValue(int index) const {
00167 return jsr.js[own_index].axis_value[index];
00168 }
00169
00170
00171 bool JoystickCtrl::getButtonValue(int index) const {
00172 return jsr.js[own_index].buttons_value[index];
00173 }
00174