00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/input/joystickinputdevice.hpp> 00019 #include <ptf/core/ptfcore.hpp> 00020 #include <SDL.h> 00021 #include <stdexcept> 00022 00026 JoystickInputDevice::JoystickInputDevice() { 00027 SDL_Joystick* joystick; 00028 SDL_InitSubSystem(SDL_INIT_JOYSTICK); 00029 SDL_ShowCursor(0); 00030 if (SDL_NumJoysticks() < 1){ 00031 PtfCore::getSingleton().getMessageLog() 00032 << "ERROR: At least one joystick is needed for joystick as input device.\n"; 00033 throw std::runtime_error("At least one joystick is needed for joystick as input device."); 00034 } 00035 PtfCore::getSingleton().getMessageLog() 00036 << "MESSAGE: Using joystick 0 (" << SDL_JoystickName(0) << ").\n"; 00037 SDL_JoystickEventState(SDL_ENABLE); 00038 joystick = SDL_JoystickOpen(0); 00039 } 00040 00044 JoystickInputDevice::~JoystickInputDevice() { 00045 // nothing to do here 00046 } 00047 00051 bool JoystickInputDevice::poll() { 00052 SDL_Event event; 00053 if (SDL_PollEvent(&event)){ 00054 if (event.type == SDL_QUIT){ 00055 PtfCore::getSingleton().getScheduler().quit(); 00056 } 00057 else if (event.type == SDL_JOYBUTTONDOWN) { 00058 lastButton = event.jbutton.button; 00059 return true; 00060 } 00061 } 00062 return false; 00063 } 00064 00069 int JoystickInputDevice::getLastButton() const { 00070 return lastButton; 00071 } 00072 00076 bool JoystickInputDevice::handle(vector<bool>& states) { 00077 return false; 00078 }