1 | /** |
2 | * Copyright (c) 2006-2023 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 | |
21 | #include "wrap_Event.h" |
22 | |
23 | // LOVE |
24 | #include "common/runtime.h" |
25 | |
26 | #include "sdl/Event.h" |
27 | |
28 | // Shove the wrap_Event.lua code directly into a raw string literal. |
29 | static const char event_lua[] = |
30 | #include "wrap_Event.lua" |
31 | ; |
32 | |
33 | namespace love |
34 | { |
35 | namespace event |
36 | { |
37 | |
38 | #define instance() (Module::getInstance<Event>(Module::M_EVENT)) |
39 | |
40 | static int w_poll_i(lua_State *L) |
41 | { |
42 | Message *m = nullptr; |
43 | |
44 | if (instance()->poll(m)) |
45 | { |
46 | int args = m->toLua(L); |
47 | m->release(); |
48 | return args; |
49 | } |
50 | |
51 | // No pending events. |
52 | return 0; |
53 | } |
54 | |
55 | int w_pump(lua_State *L) |
56 | { |
57 | luax_catchexcept(L, [&]() { instance()->pump(); }); |
58 | return 0; |
59 | } |
60 | |
61 | int w_wait(lua_State *L) |
62 | { |
63 | Message *m = nullptr; |
64 | luax_catchexcept(L, [&]() { m = instance()->wait(); }); |
65 | if (m) |
66 | { |
67 | int args = m->toLua(L); |
68 | m->release(); |
69 | return args; |
70 | } |
71 | |
72 | return 0; |
73 | } |
74 | |
75 | int w_push(lua_State *L) |
76 | { |
77 | StrongRef<Message> m; |
78 | luax_catchexcept(L, [&]() { m.set(Message::fromLua(L, 1), Acquire::NORETAIN); }); |
79 | |
80 | luax_pushboolean(L, m.get() != nullptr); |
81 | |
82 | if (m.get() == nullptr) |
83 | return 1; |
84 | |
85 | instance()->push(m); |
86 | return 1; |
87 | } |
88 | |
89 | int w_clear(lua_State *L) |
90 | { |
91 | luax_catchexcept(L, [&]() { instance()->clear(); }); |
92 | return 0; |
93 | } |
94 | |
95 | int w_quit(lua_State *L) |
96 | { |
97 | luax_catchexcept(L, [&]() { |
98 | std::vector<Variant> args = {Variant::fromLua(L, 1)}; |
99 | |
100 | StrongRef<Message> m(new Message("quit" , args), Acquire::NORETAIN); |
101 | instance()->push(m); |
102 | }); |
103 | |
104 | luax_pushboolean(L, true); |
105 | return 1; |
106 | } |
107 | |
108 | // List of functions to wrap. |
109 | static const luaL_Reg functions[] = |
110 | { |
111 | { "pump" , w_pump }, |
112 | { "poll_i" , w_poll_i }, |
113 | { "wait" , w_wait }, |
114 | { "push" , w_push }, |
115 | { "clear" , w_clear }, |
116 | { "quit" , w_quit }, |
117 | { 0, 0 } |
118 | }; |
119 | |
120 | extern "C" int luaopen_love_event(lua_State *L) |
121 | { |
122 | Event *instance = instance(); |
123 | if (instance == nullptr) |
124 | { |
125 | luax_catchexcept(L, [&](){ instance = new love::event::sdl::Event(); }); |
126 | } |
127 | else |
128 | instance->retain(); |
129 | |
130 | WrappedModule w; |
131 | w.module = instance; |
132 | w.name = "event" ; |
133 | w.type = &Module::type; |
134 | w.functions = functions; |
135 | w.types = nullptr; |
136 | |
137 | int ret = luax_register_module(L, w); |
138 | |
139 | if (luaL_loadbuffer(L, (const char *)event_lua, sizeof(event_lua), "=[love \"wrap_Event.lua\"]" ) == 0) |
140 | lua_call(L, 0, 0); |
141 | else |
142 | lua_error(L); |
143 | |
144 | return ret; |
145 | } |
146 | |
147 | } // event |
148 | } // love |
149 | |