wrap_Event.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "wrap_Event.h"
  21. // LOVE
  22. #include "common/runtime.h"
  23. #include "sdl/Event.h"
  24. namespace love
  25. {
  26. namespace event
  27. {
  28. #define instance() (Module::getInstance<Event>(Module::M_EVENT))
  29. static int w_poll_i(lua_State *L)
  30. {
  31. Message *m = nullptr;
  32. if (instance()->poll(m))
  33. {
  34. int args = m->toLua(L);
  35. m->release();
  36. return args;
  37. }
  38. // No pending events.
  39. return 0;
  40. }
  41. int w_poll(lua_State *L)
  42. {
  43. lua_pushcclosure(L, &w_poll_i, 0);
  44. return 1;
  45. }
  46. int w_pump(lua_State *)
  47. {
  48. instance()->pump();
  49. return 0;
  50. }
  51. int w_wait(lua_State *L)
  52. {
  53. Message *m = instance()->wait();
  54. if (m)
  55. {
  56. int args = m->toLua(L);
  57. m->release();
  58. return args;
  59. }
  60. return 0;
  61. }
  62. int w_push(lua_State *L)
  63. {
  64. StrongRef<Message> m(Message::fromLua(L, 1), Acquire::NORETAIN);
  65. luax_pushboolean(L, m.get() != nullptr);
  66. if (m.get() == nullptr)
  67. return 1;
  68. instance()->push(m);
  69. return 1;
  70. }
  71. int w_clear(lua_State *)
  72. {
  73. instance()->clear();
  74. return 0;
  75. }
  76. int w_quit(lua_State *L)
  77. {
  78. std::vector<Variant> args = {Variant::fromLua(L, 1)};
  79. StrongRef<Message> m(new Message("quit", args), Acquire::NORETAIN);
  80. instance()->push(m);
  81. luax_pushboolean(L, true);
  82. return 1;
  83. }
  84. // List of functions to wrap.
  85. static const luaL_Reg functions[] =
  86. {
  87. { "pump", w_pump },
  88. { "poll", w_poll },
  89. { "wait", w_wait },
  90. { "push", w_push },
  91. { "clear", w_clear },
  92. { "quit", w_quit },
  93. { 0, 0 }
  94. };
  95. extern "C" int luaopen_love_event(lua_State *L)
  96. {
  97. Event *instance = instance();
  98. if (instance == nullptr)
  99. {
  100. luax_catchexcept(L, [&](){ instance = new love::event::sdl::Event(); });
  101. }
  102. else
  103. instance->retain();
  104. WrappedModule w;
  105. w.module = instance;
  106. w.name = "event";
  107. w.type = MODULE_ID;
  108. w.functions = functions;
  109. w.types = nullptr;
  110. int ret = luax_register_module(L, w);
  111. return ret;
  112. }
  113. } // event
  114. } // love