wrap_Joystick.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * Copyright (c) 2006-2016 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. Joystick *luax_checkjoystick(lua_State *L, int idx)
  29. {
  30. return luax_checktype<Joystick>(L, idx);
  31. }
  32. int w_Joystick_isConnected(lua_State *L)
  33. {
  34. Joystick *j = luax_checkjoystick(L, 1);
  35. luax_pushboolean(L, j->isConnected());
  36. return 1;
  37. }
  38. int w_Joystick_getName(lua_State *L)
  39. {
  40. Joystick *j = luax_checkjoystick(L, 1);
  41. lua_pushstring(L, j->getName());
  42. return 1;
  43. }
  44. int w_Joystick_getID(lua_State *L)
  45. {
  46. Joystick *j = luax_checkjoystick(L, 1);
  47. // IDs are 1-based in Lua.
  48. lua_pushinteger(L, j->getID() + 1);
  49. int instanceid = j->getInstanceID();
  50. if (instanceid >= 0)
  51. lua_pushinteger(L, instanceid + 1);
  52. else
  53. lua_pushnil(L);
  54. return 2;
  55. }
  56. int w_Joystick_getGUID(lua_State *L)
  57. {
  58. Joystick *j = luax_checkjoystick(L, 1);
  59. luax_pushstring(L, j->getGUID());
  60. return 1;
  61. }
  62. int w_Joystick_getAxisCount(lua_State *L)
  63. {
  64. Joystick *j = luax_checkjoystick(L, 1);
  65. lua_pushinteger(L, j->getAxisCount());
  66. return 1;
  67. }
  68. int w_Joystick_getButtonCount(lua_State *L)
  69. {
  70. Joystick *j = luax_checkjoystick(L, 1);
  71. lua_pushinteger(L, j->getButtonCount());
  72. return 1;
  73. }
  74. int w_Joystick_getHatCount(lua_State *L)
  75. {
  76. Joystick *j = luax_checkjoystick(L, 1);
  77. lua_pushinteger(L, j->getHatCount());
  78. return 1;
  79. }
  80. int w_Joystick_getAxis(lua_State *L)
  81. {
  82. Joystick *j = luax_checkjoystick(L, 1);
  83. int axisindex = (int) luaL_checknumber(L, 2) - 1;
  84. lua_pushnumber(L, j->getAxis(axisindex));
  85. return 1;
  86. }
  87. int w_Joystick_getAxes(lua_State *L)
  88. {
  89. Joystick *j = luax_checkjoystick(L, 1);
  90. std::vector<float> axes = j->getAxes();
  91. for (float value : axes)
  92. lua_pushnumber(L, value);
  93. return (int) axes.size();
  94. }
  95. int w_Joystick_getHat(lua_State *L)
  96. {
  97. Joystick *j = luax_checkjoystick(L, 1);
  98. int hatindex = (int) luaL_checknumber(L, 2) - 1;
  99. Joystick::Hat h = j->getHat(hatindex);
  100. const char *direction = "";
  101. Joystick::getConstant(h, direction);
  102. lua_pushstring(L, direction);
  103. return 1;
  104. }
  105. int w_Joystick_isDown(lua_State *L)
  106. {
  107. Joystick *j = luax_checkjoystick(L, 1);
  108. bool istable = lua_istable(L, 2);
  109. int num = istable ? (int) luax_objlen(L, 2) : (lua_gettop(L) - 1);
  110. if (num == 0)
  111. luaL_checkinteger(L, 2);
  112. std::vector<int> buttons;
  113. buttons.reserve(num);
  114. if (istable)
  115. {
  116. for (int i = 0; i < num; i++)
  117. {
  118. lua_rawgeti(L, 2, i + 1);
  119. buttons.push_back((int) luaL_checknumber(L, -1) - 1);
  120. lua_pop(L, 1);
  121. }
  122. }
  123. else
  124. {
  125. for (int i = 0; i < num; i++)
  126. buttons.push_back((int) luaL_checknumber(L, i + 2) - 1);
  127. }
  128. luax_pushboolean(L, j->isDown(buttons));
  129. return 1;
  130. }
  131. int w_Joystick_isGamepad(lua_State *L)
  132. {
  133. Joystick *j = luax_checkjoystick(L, 1);
  134. luax_pushboolean(L, j->isGamepad());
  135. return 1;
  136. }
  137. int w_Joystick_getGamepadAxis(lua_State *L)
  138. {
  139. Joystick *j = luax_checkjoystick(L, 1);
  140. const char *str = luaL_checkstring(L, 2);
  141. Joystick::GamepadAxis axis;
  142. if (!joystick::Joystick::getConstant(str, axis))
  143. return luaL_error(L, "Invalid gamepad axis: %s", str);
  144. lua_pushnumber(L, j->getGamepadAxis(axis));
  145. return 1;
  146. }
  147. int w_Joystick_isGamepadDown(lua_State *L)
  148. {
  149. Joystick *j = luax_checkjoystick(L, 1);
  150. bool istable = lua_istable(L, 2);
  151. int num = istable ? (int) luax_objlen(L, 2) : (lua_gettop(L) - 1);
  152. if (num == 0)
  153. luaL_checkstring(L, 2);
  154. std::vector<Joystick::GamepadButton> buttons;
  155. buttons.reserve(num);
  156. Joystick::GamepadButton button;
  157. if (istable)
  158. {
  159. for (int i = 0; i < num; i++)
  160. {
  161. lua_rawgeti(L, 2, i + 1);
  162. const char *str = luaL_checkstring(L, -1);
  163. if (!joystick::Joystick::getConstant(str, button))
  164. return luaL_error(L, "Invalid gamepad button: %s", str);
  165. buttons.push_back(button);
  166. lua_pop(L, 1);
  167. }
  168. }
  169. else
  170. {
  171. for (int i = 0; i < num; i++)
  172. {
  173. const char *str = luaL_checkstring(L, i + 2);
  174. if (!joystick::Joystick::getConstant(str, button))
  175. return luaL_error(L, "Invalid gamepad button: %s", str);
  176. buttons.push_back(button);
  177. }
  178. }
  179. luax_pushboolean(L, j->isGamepadDown(buttons));
  180. return 1;
  181. }
  182. int w_Joystick_isVibrationSupported(lua_State *L)
  183. {
  184. Joystick *j = luax_checkjoystick(L, 1);
  185. luax_pushboolean(L, j->isVibrationSupported());
  186. return 1;
  187. }
  188. int w_Joystick_setVibration(lua_State *L)
  189. {
  190. Joystick *j = luax_checkjoystick(L, 1);
  191. bool success = false;
  192. if (lua_isnoneornil(L, 2))
  193. {
  194. // Disable joystick vibration if no argument is given.
  195. success = j->setVibration();
  196. }
  197. else
  198. {
  199. float left = (float) luaL_checknumber(L, 2);
  200. float right = (float) luaL_optnumber(L, 3, left);
  201. float duration = (float) luaL_optnumber(L, 4, -1.0); // -1 is infinite.
  202. success = j->setVibration(left, right, duration);
  203. }
  204. luax_pushboolean(L, success);
  205. return 1;
  206. }
  207. int w_Joystick_getVibration(lua_State *L)
  208. {
  209. Joystick *j = luax_checkjoystick(L, 1);
  210. float left, right;
  211. j->getVibration(left, right);
  212. lua_pushnumber(L, left);
  213. lua_pushnumber(L, right);
  214. return 2;
  215. }
  216. // List of functions to wrap.
  217. static const luaL_Reg w_Joystick_functions[] =
  218. {
  219. { "isConnected", w_Joystick_isConnected },
  220. { "getName", w_Joystick_getName },
  221. { "getID", w_Joystick_getID },
  222. { "getGUID", w_Joystick_getGUID },
  223. { "getAxisCount", w_Joystick_getAxisCount },
  224. { "getButtonCount", w_Joystick_getButtonCount },
  225. { "getHatCount", w_Joystick_getHatCount },
  226. { "getAxis", w_Joystick_getAxis },
  227. { "getAxes", w_Joystick_getAxes },
  228. { "getHat", w_Joystick_getHat },
  229. { "isDown", w_Joystick_isDown },
  230. { "isGamepad", w_Joystick_isGamepad },
  231. { "getGamepadAxis", w_Joystick_getGamepadAxis },
  232. { "isGamepadDown", w_Joystick_isGamepadDown },
  233. { "isVibrationSupported", w_Joystick_isVibrationSupported },
  234. { "setVibration", w_Joystick_setVibration },
  235. { "getVibration", w_Joystick_getVibration },
  236. // From wrap_JoystickModule.
  237. { "getConnectedIndex", w_getIndex },
  238. { "getGamepadMapping", w_getGamepadMapping },
  239. { 0, 0 },
  240. };
  241. extern "C" int luaopen_joystick(lua_State *L)
  242. {
  243. return luax_register_type(L, Joystick::type, w_Joystick_functions, nullptr);
  244. }
  245. } // joystick
  246. } // love