/*************************************************************************** Cjoystick.cpp (c) 2011 Laurent Carlier This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ***************************************************************************/ #define __CJOYSTICK_CPP #include "Cjoystick.h" #include #include // store joysticks infos typedef struct { Uint8 Axes; Uint8 Balls; Uint8 Buttons; Uint8 Hats; std::string Name; } JOY_info; static std::map joyinfos; static std::map joyobjects; static int joyindex = 0; CJOY_INFO CJOY_info = { 0 }; #define CHECK_VALID() \ if (UNLIKELY(!CJOY_info.valid)) \ { \ GB.Error("No joystick event data"); \ return; \ } static void filljoyinfos() { int numjoy = SDL_NumJoysticks(); JOY_info myinfo; if (!numjoy) return; for (int i=0; i=numjoy || index<0) { GB.Error("Joystick &1 not available !", VARG(index)); return; } joyindex = index; if (!joyinfos.size()) filljoyinfos(); RETURN_SELF(); END_METHOD BEGIN_PROPERTY(JOYSTICKS_count) GB.ReturnInteger(SDL_NumJoysticks()); END_METHOD BEGIN_PROPERTY(JOYSTICK_device) CHECK_VALID(); GB.ReturnInteger(CJOY_info.device); END_METHOD BEGIN_PROPERTY(JOYSTICK_id) CHECK_VALID(); GB.ReturnInteger(CJOY_info.id); END_PROPERTY BEGIN_PROPERTY(JOYSTICK_axisvalue) CHECK_VALID(); GB.ReturnInteger(CJOY_info.value1); END_PROPERTY BEGIN_PROPERTY(JOYSTICK_hatvalue) CHECK_VALID(); GB.ReturnInteger(CJOY_info.value1); END_PROPERTY BEGIN_PROPERTY(JOYSTICK_ballx) CHECK_VALID(); GB.ReturnInteger(CJOY_info.value1); END_PROPERTY BEGIN_PROPERTY(JOYSTICK_bally) CHECK_VALID(); GB.ReturnInteger(CJOY_info.value2); END_PROPERTY /***************************************************************************/ GB_DESC CJoyInfos[] = { GB_DECLARE(".Joystick", 0), GB_VIRTUAL_CLASS(), GB_STATIC_PROPERTY("Enable", "b", JOYINFOS_enable), GB_STATIC_PROPERTY_READ("Axes", "i", JOYINFOS_numofaxes), GB_STATIC_PROPERTY_READ("Balls", "i", JOYINFOS_numofballs), GB_STATIC_PROPERTY_READ("Buttons", "i", JOYINFOS_numofbuts), GB_STATIC_PROPERTY_READ("Hats", "i", JOYINFOS_numofhats), GB_STATIC_PROPERTY_READ("Name", "s", JOYINFOS_name), GB_END_DECLARE }; GB_DESC CQueryJoys[] = { GB_DECLARE("Joysticks", 0), GB_VIRTUAL_CLASS(), GB_STATIC_METHOD("_get", ".Joystick", JOYSTICKS_get, "(Index)i"), GB_STATIC_PROPERTY_READ("Count", "i", JOYSTICKS_count), GB_END_DECLARE }; GB_DESC CJoystick[] = { GB_DECLARE("Joystick", 0), GB_VIRTUAL_CLASS(), // TODO close all opened joysticks on exit ? // GB_STATIC_METHOD("_exit", NULL, JOYSTICK_exit, NULL), GB_STATIC_PROPERTY_READ("Device", "i", JOYSTICK_device), GB_STATIC_PROPERTY_READ("Id", "i", JOYSTICK_id), GB_STATIC_PROPERTY_READ("Axis", "i", JOYSTICK_axisvalue), GB_STATIC_PROPERTY_READ("Hat", "i", JOYSTICK_hatvalue), GB_STATIC_PROPERTY_READ("BallX", "i", JOYSTICK_ballx), GB_STATIC_PROPERTY_READ("BallY", "i", JOYSTICK_bally), GB_CONSTANT("LeftUp", "i", SDL_HAT_LEFTUP), GB_CONSTANT("Left", "i", SDL_HAT_LEFT), GB_CONSTANT("LeftDown", "i", SDL_HAT_LEFTDOWN), GB_CONSTANT("Up", "i", SDL_HAT_UP), GB_CONSTANT("Centered", "i", SDL_HAT_CENTERED), GB_CONSTANT("Down", "i", SDL_HAT_DOWN), GB_CONSTANT("RightUp", "i", SDL_HAT_RIGHTUP), GB_CONSTANT("Right", "i", SDL_HAT_RIGHT), GB_CONSTANT("RightDown", "i", SDL_HAT_RIGHTDOWN), GB_END_DECLARE };