wrap_Joystick.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Joystick.h"
  22. #include "wrap_JoystickModule.h"
  23. #include <vector>
  24. namespace love
  25. {
  26. namespace joystick
  27. {
  28. namespace sdl
  29. {
  30. Joystick *luax_checkjoystick(lua_State *L, int idx)
  31. {
  32. return luax_checktype<Joystick>(L, idx, "Joystick", JOYSTICK_JOYSTICK_T);
  33. }
  34. int w_Joystick_isConnected(lua_State *L)
  35. {
  36. Joystick *j = luax_checkjoystick(L, 1);
  37. luax_pushboolean(L, j->isConnected());
  38. return 1;
  39. }
  40. int w_Joystick_getName(lua_State *L)
  41. {
  42. Joystick *j = luax_checkjoystick(L, 1);
  43. lua_pushstring(L, j->getName());
  44. return 1;
  45. }
  46. int w_Joystick_getID(lua_State *L)
  47. {
  48. Joystick *j = luax_checkjoystick(L, 1);
  49. // IDs are 1-based in Lua.
  50. lua_pushinteger(L, j->getID() + 1);
  51. int instanceid = j->getInstanceID();
  52. if (instanceid >= 0)
  53. lua_pushinteger(L, instanceid + 1);
  54. else
  55. lua_pushnil(L);
  56. return 2;
  57. }
  58. int w_Joystick_getGUID(lua_State *L)
  59. {
  60. Joystick *j = luax_checkjoystick(L, 1);
  61. luax_pushstring(L, j->getGUID());
  62. return 1;
  63. }
  64. int w_Joystick_getAxisCount(lua_State *L)
  65. {
  66. Joystick *j = luax_checkjoystick(L, 1);
  67. lua_pushinteger(L, j->getAxisCount());
  68. return 1;
  69. }
  70. int w_Joystick_getButtonCount(lua_State *L)
  71. {
  72. Joystick *j = luax_checkjoystick(L, 1);
  73. lua_pushinteger(L, j->getButtonCount());
  74. return 1;
  75. }
  76. int w_Joystick_getHatCount(lua_State *L)
  77. {
  78. Joystick *j = luax_checkjoystick(L, 1);
  79. lua_pushinteger(L, j->getHatCount());
  80. return 1;
  81. }
  82. int w_Joystick_getAxis(lua_State *L)
  83. {
  84. Joystick *j = luax_checkjoystick(L, 1);
  85. int axisindex = luaL_checkint(L, 2) - 1;
  86. lua_pushnumber(L, j->getAxis(axisindex));
  87. return 1;
  88. }
  89. int w_Joystick_getAxes(lua_State *L)
  90. {
  91. Joystick *j = luax_checkjoystick(L, 1);
  92. std::vector<float> axes = j->getAxes();
  93. for (size_t i = 0; i < axes.size(); i++)
  94. lua_pushnumber(L, axes[i]);
  95. return (int) axes.size();
  96. }
  97. int w_Joystick_getHat(lua_State *L)
  98. {
  99. Joystick *j = luax_checkjoystick(L, 1);
  100. int hatindex = luaL_checkint(L, 2) - 1;
  101. Joystick::Hat h = j->getHat(hatindex);
  102. const char *direction = "";
  103. love::joystick::Joystick::getConstant(h, direction);
  104. lua_pushstring(L, direction);
  105. return 1;
  106. }
  107. int w_Joystick_isDown(lua_State *L)
  108. {
  109. Joystick *j = luax_checkjoystick(L, 1);
  110. luaL_checkinteger(L, 2);
  111. std::vector<int> buttons;
  112. for (int i = 2; i <= lua_gettop(L); i++)
  113. buttons.push_back(luaL_checkint(L, i));
  114. luax_pushboolean(L, j->isDown(buttons));
  115. return 1;
  116. }
  117. int w_Joystick_isGamepad(lua_State *L)
  118. {
  119. Joystick *j = luax_checkjoystick(L, 1);
  120. luax_pushboolean(L, j->isGamepad());
  121. return 1;
  122. }
  123. int w_Joystick_getGamepadAxis(lua_State *L)
  124. {
  125. Joystick *j = luax_checkjoystick(L, 1);
  126. const char *str = luaL_checkstring(L, 2);
  127. Joystick::GamepadAxis axis;
  128. if (!joystick::Joystick::getConstant(str, axis))
  129. return luaL_error(L, "Invalid gamepad axis: %s", str);
  130. lua_pushnumber(L, j->getGamepadAxis(axis));
  131. return 1;
  132. }
  133. int w_Joystick_isGamepadDown(lua_State *L)
  134. {
  135. Joystick *j = luax_checkjoystick(L, 1);
  136. std::vector<Joystick::GamepadButton> buttons;
  137. buttons.reserve(lua_gettop(L) - 1);
  138. luaL_checkstring(L, 2);
  139. for (int i = 2; i <= lua_gettop(L); i++)
  140. {
  141. const char *str = luaL_checkstring(L, i);
  142. Joystick::GamepadButton button;
  143. if (!joystick::Joystick::getConstant(str, button))
  144. return luaL_error(L, "Invalid gamepad button: %s", str);
  145. buttons.push_back(button);
  146. }
  147. luax_pushboolean(L, j->isGamepadDown(buttons));
  148. return 1;
  149. }
  150. int w_Joystick_isVibrationSupported(lua_State *L)
  151. {
  152. Joystick *j = luax_checkjoystick(L, 1);
  153. luax_pushboolean(L, j->isVibrationSupported());
  154. return 1;
  155. }
  156. int w_Joystick_setVibration(lua_State *L)
  157. {
  158. Joystick *j = luax_checkjoystick(L, 1);
  159. bool success = false;
  160. if (lua_isnoneornil(L, 2))
  161. {
  162. // Disable joystick vibration if no argument is given.
  163. success = j->setVibration();
  164. }
  165. else
  166. {
  167. float left = (float) luaL_checknumber(L, 2);
  168. float right = (float) luaL_optnumber(L, 3, left);
  169. success = j->setVibration(left, right);
  170. }
  171. luax_pushboolean(L, success);
  172. return 1;
  173. }
  174. int w_Joystick_getVibration(lua_State *L)
  175. {
  176. Joystick *j = luax_checkjoystick(L, 1);
  177. float left, right;
  178. j->getVibration(left, right);
  179. lua_pushnumber(L, left);
  180. lua_pushnumber(L, right);
  181. return 2;
  182. }
  183. // List of functions to wrap.
  184. static const luaL_Reg functions[] =
  185. {
  186. { "isConnected", w_Joystick_isConnected },
  187. { "getName", w_Joystick_getName },
  188. { "getID", w_Joystick_getID },
  189. { "getGUID", w_Joystick_getGUID },
  190. { "getAxisCount", w_Joystick_getAxisCount },
  191. { "getButtonCount", w_Joystick_getButtonCount },
  192. { "getHatCount", w_Joystick_getHatCount },
  193. { "getAxis", w_Joystick_getAxis },
  194. { "getAxes", w_Joystick_getAxes },
  195. { "getHat", w_Joystick_getHat },
  196. { "isDown", w_Joystick_isDown },
  197. { "isGamepad", w_Joystick_isGamepad },
  198. { "getGamepadAxis", w_Joystick_getGamepadAxis },
  199. { "isGamepadDown", w_Joystick_isGamepadDown },
  200. { "isVibrationSupported", w_Joystick_isVibrationSupported },
  201. { "setVibration", w_Joystick_setVibration },
  202. { "getVibration", w_Joystick_getVibration },
  203. // From wrap_JoystickModule.
  204. { "getConnectedIndex", w_getIndex },
  205. { "getGamepadMapping", w_getGamepadMapping },
  206. { 0, 0 },
  207. };
  208. extern "C" int luaopen_joystick(lua_State *L)
  209. {
  210. return luax_register_type(L, "Joystick", functions);
  211. }
  212. } // sdl
  213. } // joystick
  214. } // love