wrap_Joystick.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * Copyright (c) 2006-2009 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. #include "wrap_Joystick.h"
  21. namespace love
  22. {
  23. namespace joystick
  24. {
  25. namespace sdl
  26. {
  27. static Joystick * instance = 0;
  28. int w_getNumJoysticks(lua_State * L)
  29. {
  30. lua_pushinteger(L, instance->getNumJoysticks());
  31. return 1;
  32. }
  33. int w_getName(lua_State * L)
  34. {
  35. int index = luaL_checkint(L, 1);
  36. lua_pushstring(L, instance->getName(index));
  37. return 1;
  38. }
  39. int w_open(lua_State * L)
  40. {
  41. int index = luaL_checkint(L, 1);
  42. luax_pushboolean(L, instance->open(index));
  43. return 1;
  44. }
  45. int w_isOpen(lua_State * L)
  46. {
  47. int index = luaL_checkint(L, 1);
  48. luax_pushboolean(L, instance->isOpen(index));
  49. return 1;
  50. }
  51. int w_getNumAxes(lua_State * L)
  52. {
  53. int index = luaL_checkint(L, 1);
  54. lua_pushinteger(L, instance->getNumAxes(index));
  55. return 1;
  56. }
  57. int w_getNumBalls(lua_State * L)
  58. {
  59. int index = luaL_checkint(L, 1);
  60. lua_pushinteger(L, instance->getNumBalls(index));
  61. return 1;
  62. }
  63. int w_getNumButtons(lua_State * L)
  64. {
  65. int index = luaL_checkint(L, 1);
  66. lua_pushinteger(L, instance->getNumButtons(index));
  67. return 1;
  68. }
  69. int w_getNumHats(lua_State * L)
  70. {
  71. int index = luaL_checkint(L, 1);
  72. lua_pushinteger(L, instance->getNumHats(index));
  73. return 1;
  74. }
  75. int w_getAxis(lua_State * L)
  76. {
  77. int index = luaL_checkint(L, 1);
  78. int axis = luaL_checkint(L, 2);
  79. lua_pushnumber(L, instance->getAxis(index, axis));
  80. return 1;
  81. }
  82. int w_getAxes(lua_State * L)
  83. {
  84. return instance->getAxes(L);
  85. }
  86. int w_getBall(lua_State * L)
  87. {
  88. return instance->getBall(L);
  89. }
  90. int w_isDown(lua_State * L)
  91. {
  92. int index = luaL_checkint(L, 1);
  93. int button = luaL_checkint(L, 2);
  94. luax_pushboolean(L, instance->isDown(index, button));
  95. return 1;
  96. }
  97. int w_getHat(lua_State * L)
  98. {
  99. int index = luaL_checkint(L, 1);
  100. int hat = luaL_checkint(L, 2);
  101. lua_pushinteger(L, instance->getHat(index, hat));
  102. return 1;
  103. }
  104. int w_close(lua_State * L)
  105. {
  106. int index = luaL_checkint(L, 1);
  107. instance->close(index);
  108. return 0;
  109. }
  110. // List of functions to wrap.
  111. static const luaL_Reg functions[] = {
  112. { "getNumJoysticks", w_getNumJoysticks },
  113. { "getName", w_getName },
  114. { "open", w_open },
  115. { "isOpen", w_isOpen },
  116. { "getNumAxes", w_getNumAxes },
  117. { "getNumBalls", w_getNumBalls },
  118. { "getNumButtons", w_getNumButtons },
  119. { "getNumHats", w_getNumHats },
  120. { "getAxis", w_getAxis },
  121. { "getAxes", w_getAxes },
  122. { "getBall", w_getBall },
  123. { "isDown", w_isDown },
  124. { "getHat", w_getHat },
  125. { "close", w_close },
  126. { 0, 0 }
  127. };
  128. // List of constants.
  129. static const Constant constants[] = {
  130. { "joystick_axis_horizontal", Joystick::JOYSTICK_AXIS_HORIZONTAL },
  131. { "joystick_axis_vertical", Joystick::JOYSTICK_AXIS_VERITCAL },
  132. { "joystick_hat_centered", Joystick::JOYSTICK_HAT_CENTERED },
  133. { "joystick_hat_up", Joystick::JOYSTICK_HAT_UP },
  134. { "joystick_hat_right", Joystick::JOYSTICK_HAT_RIGHT },
  135. { "joystick_hat_down", Joystick::JOYSTICK_HAT_DOWN },
  136. { "joystick_hat_left", Joystick::JOYSTICK_HAT_LEFT },
  137. { "joystick_hat_rightup", Joystick::JOYSTICK_HAT_RIGHTUP },
  138. { "joystick_hat_rightdown", Joystick::JOYSTICK_HAT_RIGHTDOWN },
  139. { "joystick_hat_leftup", Joystick::JOYSTICK_HAT_LEFTUP },
  140. { "joystick_hat_leftdown", Joystick::JOYSTICK_HAT_LEFTDOWN },
  141. { 0, 0 }
  142. };
  143. int luaopen_love_joystick(lua_State * L)
  144. {
  145. if(instance == 0)
  146. {
  147. try
  148. {
  149. instance = new Joystick();
  150. }
  151. catch(Exception & e)
  152. {
  153. return luaL_error(L, e.what());
  154. }
  155. }
  156. WrappedModule w;
  157. w.module = instance;
  158. w.name = "joystick";
  159. w.flags = MODULE_T;
  160. w.functions = functions;
  161. w.types = 0;
  162. w.constants = constants;
  163. return luax_register_module(L, w);
  164. }
  165. } // sdl
  166. } // joystick
  167. } // love