4 Copyright (c) 2007 Mauro Iazzi
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation
8 files (the "Software"), to deal in the Software without
9 restriction, including without limitation the rights to use,
10 copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 OTHER DEALINGS IN THE SOFTWARE.
30 HEADER_DEFINE
= tostring(os
.getenv'HEADER_DEFINE' or '__LQT_DUMMY')
31 ARG_MAX
= tonumber(os
.getenv'ARG_MAX' or 2)
32 TYPES
= { bool
='lua_pushboolean', int
='lua_pushinteger', double
='lua_pushnumber', ['const char *']='lua_pushstring' }
35 io
.write("#ifndef "..HEADER_DEFINE
.."\n")
36 io
.write("#define "..HEADER_DEFINE
.."\n")
50 #define LUA_DUMMY_REGISTRY "LuaDummy"
51 #define SEE_STACK(L, j) for (int j=1;j<=lua_gettop(L);j++) { qDebug() << j << '=' << luaL_typename(L, j) << '@' << lua_topointer (L, j); }
53 class LuaDummy: public QObject {
57 LuaDummy(lua_State *state):L(state) {
58 int dummyTable = lua_gettop(L);
59 qDebug() << "Dummy" << this << "is born";
60 lua_getfield(L, LUA_REGISTRYINDEX, LUA_DUMMY_REGISTRY);
61 if (lua_isnil(L, -1)) {
65 lua_setfield(L, LUA_REGISTRYINDEX, LUA_DUMMY_REGISTRY);
70 lua_gettable(L, dummyTable);
72 if (!lua_isqobject(L, -1)) {
73 qDebug() << "not QObject* is" << luaL_typename(L, -1);
75 // top of stack is the function I want
76 qDebug() << "to be bound is" << luaL_typename(L, -1);
77 lua_pushlightuserdata(L, this);
79 lua_settable(L, dummyTable);
80 // registry this is associated to this function
81 lua_pushqobject(L, this);
84 lua_settable(L, dummyTable);
86 // leave the qobject on top;
87 qDebug() << "Dummy" << this << "scheduled for deletion";
90 lua_replace(L, dummyTable);
91 lua_settop(L, dummyTable);
94 lua_pushcfunction(L, __gc);
95 lua_setfield(L, -2, "__gc");
99 qDebug() << "Dummy" << this << "is dead";
105 static int __gc (lua_State *L) {
106 QPointer<QObject> *qp = (QPointer<QObject> *)lua_touserdata(L, 1);
107 if (*qp) (*qp)->deleteLater();
108 qDebug() << "LuaDummy" << *qp << "scheduled for deletion";
114 virtual bool eventFilter (QObject * watched, QEvent * event) {
116 int dummyTable = lua_gettop(L) + 1;
117 lua_getfield(L, LUA_REGISTRYINDEX, LUA_DUMMY_REGISTRY);
118 qDebug() << "dummyTable =" << dummyTable;
119 SEE_STACK(L, k);qDebug() << "====";
120 if (!lua_istable(L, -1)) {
121 lua_settop(L, dummyTable-1);
124 SEE_STACK(L, j);qDebug() << "====";
125 lua_pushlightuserdata(L, this);
126 SEE_STACK(L, j);qDebug() << "====";
127 lua_gettable(L, dummyTable);
128 SEE_STACK(L, j);qDebug() << "====";
129 lua_pushqobject(L, watched);
130 SEE_STACK(L, j);qDebug() << "====";
132 switch (event->type()) {
133 case QEvent::None: lua_pushnil(L); break;
134 case QEvent::AccessibilityDescription: break;
135 case QEvent::AccessibilityHelp: break;
136 case QEvent::AccessibilityPrepare: lua_pushstring(L, "AccessibilityPrepare"); break;
137 case QEvent::ActionAdded: break;
138 case QEvent::ActionChanged: break;
139 case QEvent::ActionRemoved: break;
140 case QEvent::ActivationChange: break;
141 case QEvent::ApplicationActivate: break;
142 case QEvent::ApplicationDeactivate: break;
143 case QEvent::ApplicationFontChange: break;
144 case QEvent::ApplicationLayoutDirectionChange: break;
145 case QEvent::ApplicationPaletteChange: break;
146 case QEvent::ApplicationWindowIconChange: break;
147 case QEvent::ChildAdded: break;
148 case QEvent::ChildPolished: break;
149 case QEvent::ChildRemoved: break;
150 case QEvent::Clipboard: break;
151 case QEvent::Close: break;
152 case QEvent::ContextMenu: break;
153 case QEvent::DeferredDelete: break;
154 case QEvent::DragEnter: break;
155 case QEvent::DragLeave: break;
156 case QEvent::DragMove: break;
157 case QEvent::Drop: break;
158 case QEvent::EnabledChange: break;
159 case QEvent::Enter: lua_pushstring(L, "Enter"); break;
160 //case QEvent::EnterEditFocus: break;
161 case QEvent::EnterWhatsThisMode: break;
162 case QEvent::FileOpen: break;
163 case QEvent::FocusIn: break;
164 case QEvent::FocusOut: break;
165 case QEvent::FontChange: break;
166 case QEvent::GraphicsSceneContextMenu: break;
167 case QEvent::GraphicsSceneDragEnter: break;
168 case QEvent::GraphicsSceneDragLeave: break;
169 case QEvent::GraphicsSceneDragMove: break;
170 case QEvent::GraphicsSceneDrop: break;
171 case QEvent::GraphicsSceneHelp: break;
172 case QEvent::GraphicsSceneHoverEnter: break;
173 case QEvent::GraphicsSceneHoverLeave: break;
174 case QEvent::GraphicsSceneHoverMove: break;
175 case QEvent::GraphicsSceneMouseDoubleClick: break;
176 case QEvent::GraphicsSceneMouseMove: break;
177 case QEvent::GraphicsSceneMousePress: break;
178 case QEvent::GraphicsSceneMouseRelease: break;
179 case QEvent::GraphicsSceneWheel: break;
180 case QEvent::Hide: break;
181 case QEvent::HideToParent: break;
182 case QEvent::HoverEnter: break;
183 case QEvent::HoverLeave: break;
184 case QEvent::HoverMove:
185 lua_pushstring(L, "HoverMove");
187 lua_pushinteger(L, static_cast<QHoverEvent*>(event)->oldPos().x()); lua_setfield(L, -2, "oldx");
188 lua_pushinteger(L, static_cast<QHoverEvent*>(event)->oldPos().y()); lua_setfield(L, -2, "oldy");
189 lua_pushinteger(L, static_cast<QHoverEvent*>(event)->pos().x()); lua_setfield(L, -2, "x");
190 lua_pushinteger(L, static_cast<QHoverEvent*>(event)->pos().y()); lua_setfield(L, -2, "y");
192 case QEvent::IconDrag: break;
193 case QEvent::IconTextChange: break;
194 case QEvent::InputMethod: break;
195 case QEvent::KeyPress: break;
196 case QEvent::KeyRelease: break;
197 case QEvent::LanguageChange: break;
198 case QEvent::LayoutDirectionChange: break;
199 case QEvent::LayoutRequest: break;
200 case QEvent::Leave: lua_pushstring(L, "Leave"); break;
201 //case QEvent::LeaveEditFocus: break;
202 case QEvent::LeaveWhatsThisMode: break;
203 case QEvent::LocaleChange: break;
204 case QEvent::MenubarUpdated: break;
205 case QEvent::MetaCall: break;
206 case QEvent::ModifiedChange: break;
207 case QEvent::MouseButtonDblClick: break;
208 case QEvent::MouseButtonPress: break;
209 case QEvent::MouseButtonRelease: break;
210 case QEvent::MouseMove: break;
211 case QEvent::MouseTrackingChange: break;
213 lua_pushstring(L, "Move");
215 lua_pushinteger(L, static_cast<QMoveEvent*>(event)->oldPos().x()); lua_setfield(L, -2, "oldx");
216 lua_pushinteger(L, static_cast<QMoveEvent*>(event)->oldPos().y()); lua_setfield(L, -2, "oldy");
217 lua_pushinteger(L, static_cast<QMoveEvent*>(event)->pos().x()); lua_setfield(L, -2, "x");
218 lua_pushinteger(L, static_cast<QMoveEvent*>(event)->pos().y()); lua_setfield(L, -2, "y");
220 case QEvent::Paint: break;
221 case QEvent::PaletteChange: break;
222 case QEvent::ParentAboutToChange: break;
223 case QEvent::ParentChange: break;
224 case QEvent::Polish: break;
225 case QEvent::PolishRequest: break;
226 case QEvent::QueryWhatsThis: break;
227 case QEvent::Resize: break;
228 case QEvent::Shortcut: break;
229 case QEvent::ShortcutOverride: break;
230 case QEvent::Show: break;
231 case QEvent::ShowToParent: break;
232 case QEvent::SockAct: break;
233 case QEvent::StatusTip: break;
234 case QEvent::StyleChange: break;
235 case QEvent::TabletMove: break;
236 case QEvent::TabletPress: break;
237 case QEvent::TabletRelease: break;
238 case QEvent::TabletEnterProximity: break;
239 case QEvent::TabletLeaveProximity: break;
240 case QEvent::Timer: break;
241 case QEvent::ToolBarChange: break;
242 case QEvent::ToolTip: break;
243 case QEvent::UpdateLater: break;
244 case QEvent::UpdateRequest: break;
245 case QEvent::WhatsThis: break;
246 case QEvent::WhatsThisClicked: break;
247 case QEvent::Wheel: break;
248 case QEvent::WinEventAct: break;
249 case QEvent::WindowActivate: break;
250 case QEvent::WindowBlocked: break;
251 case QEvent::WindowDeactivate: break;
253 lua_pushstring(L, "<unknown>");
256 while (lua_gettop(L) < dummyTable+4) { lua_pushnil(L); }
258 qDebug() << "try to call event filter; type =" << event->type();
260 qDebug() << "called event filter";
261 ret = (bool)lua_toboolean(L, -1);
262 lua_settop(L, dummyTable-1);
263 SEE_STACK(L, k);qDebug() << "====";
266 // DO NOT ENABLE THIS: IT IS FATAL! however, guess why?
267 //virtual bool event ( QEvent * event) { return eventFilter(this, event); }
278 for nargs
= 1,ARG_MAX
do
279 signatures
[nargs
] = {}
280 for oldsig
, oldbody
in pairs(signatures
[nargs
-1]) do
281 for argtype
, argpush
in pairs(TYPES
) do
282 signatures
[nargs
][oldsig
..((nargs
==1) and '' or ', ')..argtype
..' arg'..tostring(nargs
)] = oldbody
..argpush
.."(L, arg"..tostring(nargs
)..');'
288 for s
, b
in pairs(signatures
[i
]) do
289 io
.write' void generic_slot ('
292 int dummyTable = lua_gettop(L) + 1;
293 lua_getfield(L, LUA_REGISTRYINDEX, LUA_DUMMY_REGISTRY);
294 if (!lua_istable(L, -1)) return;
295 lua_pushlightuserdata(L, this);
296 lua_gettable(L, dummyTable);
302 io
.write(tostring(i
))
316 io
.write("#endif // "..HEADER_DEFINE
.."\n")