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 "common/config.h" |
22 | |
23 | // LOVE |
24 | #include "wrap_Touch.h" |
25 | |
26 | #include "sdl/Touch.h" |
27 | |
28 | namespace love |
29 | { |
30 | namespace touch |
31 | { |
32 | |
33 | #define instance() (Module::getInstance<Touch>(Module::M_TOUCH)) |
34 | |
35 | int64 luax_checktouchid(lua_State *L, int idx) |
36 | { |
37 | if (!lua_islightuserdata(L, idx)) |
38 | return luax_typerror(L, idx, "touch id" ); |
39 | |
40 | return (int64) (intptr_t) lua_touserdata(L, 1); |
41 | } |
42 | |
43 | int w_getTouches(lua_State *L) |
44 | { |
45 | const std::vector<Touch::TouchInfo> &touches = instance()->getTouches(); |
46 | |
47 | lua_createtable(L, (int) touches.size(), 0); |
48 | |
49 | for (size_t i = 0; i < touches.size(); i++) |
50 | { |
51 | // This is a bit hackish and we lose the higher 32 bits of the id on |
52 | // 32-bit systems, but SDL only ever gives id's that at most use as many |
53 | // bits as can fit in a pointer (for now.) |
54 | // We use lightuserdata instead of a lua_Number (double) because doubles |
55 | // can't represent all possible id values on 64-bit systems. |
56 | lua_pushlightuserdata(L, (void *) (intptr_t) touches[i].id); |
57 | lua_rawseti(L, -2, (int) i + 1); |
58 | } |
59 | |
60 | return 1; |
61 | } |
62 | |
63 | int w_getPosition(lua_State *L) |
64 | { |
65 | int64 id = luax_checktouchid(L, 1); |
66 | |
67 | Touch::TouchInfo touch = {}; |
68 | luax_catchexcept(L, [&]() { touch = instance()->getTouch(id); }); |
69 | |
70 | lua_pushnumber(L, touch.x); |
71 | lua_pushnumber(L, touch.y); |
72 | |
73 | return 2; |
74 | } |
75 | |
76 | int w_getPressure(lua_State *L) |
77 | { |
78 | int64 id = luax_checktouchid(L, 1); |
79 | |
80 | Touch::TouchInfo touch = {}; |
81 | luax_catchexcept(L, [&](){ touch = instance()->getTouch(id); }); |
82 | |
83 | lua_pushnumber(L, touch.pressure); |
84 | return 1; |
85 | } |
86 | |
87 | static const luaL_Reg functions[] = |
88 | { |
89 | { "getTouches" , w_getTouches }, |
90 | { "getPosition" , w_getPosition }, |
91 | { "getPressure" , w_getPressure }, |
92 | { 0, 0 } |
93 | }; |
94 | |
95 | extern "C" int luaopen_love_touch(lua_State *L) |
96 | { |
97 | Touch *instance = instance(); |
98 | if (instance == nullptr) |
99 | { |
100 | luax_catchexcept(L, [&](){ instance = new love::touch::sdl::Touch(); }); |
101 | } |
102 | else |
103 | instance->retain(); |
104 | |
105 | WrappedModule w; |
106 | w.module = instance; |
107 | w.name = "touch" ; |
108 | w.type = &Module::type; |
109 | w.functions = functions; |
110 | w.types = nullptr; |
111 | |
112 | return luax_register_module(L, w); |
113 | } |
114 | |
115 | } // touch |
116 | } // love |
117 | |