lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I think you can do it cleanly. When you use gtk_signal_connect you
should pass lua_State * as a gpointer func_data. As a callback
function you should use a simple C function that you define in your
code, lets call it 'my_callback'.

Then in my_callback you will have access the lua_State * through the
func_data gpointer (you need just a cast). The you need to use
Lua_State to put the Lua function you want in the Lua stack. The
function could be in the Environment, or in the registry or in the
global namespace of even it could already be in the stack. Then you
push the arguments of the Lua functions in the Lua stack and you use
lua_call or lua_pcall.

Anyway, to use this schema the condition is that the GTK main event
loop run  from a C function that was called from Lua. So the schema
could be:
- gtk_signal_connect will be used with some C functions callback that
you prepare on your source code. These functions will look for
particular Lua function and call them
with lua_call
- a Lua function is called to run the GTK main loop, some Lua
functions have been
  prepared in advance in the Environment or in the Lua stack to be
used as callbacks

So we can summarize the call sequence like that:
Lua main code => GTK main loop (C code called from Lua, a Lua stack
will be available) => a C callback is called by GTK main loop (we
still have access to the same Lua stack available for GTK main loop)
=> a lua function is called.

So we have closed the sequence Lua => C code => Lua.

Of course it will be important to clean the stack before the C
callback terminates.

I hope this is clear and pertinent with your problem.

Regards,
Francesco

2009/10/17 Jiří Prymus <jiri.prymus@gmail.com>
>
> So if I understand that, I can't wrapper cvCreateTrackBar function
> without re-implementing it in my wrapper, because i'm not able to get
> proper Callback function pointer from lua and pass it to opencv
> library as a C function pointer.
>
> But it leads to the bypassing original library function and that isn't
> too great.
>
> And using hacks isn't proper for me, because it's part of my Bachelor
> thesis and I want clear code.
>
> Thanks for help.
>
> With regards,
> Jiri Prymus
>
> 2009/10/17 Wesley Smith <wesley.hoke@gmail.com>:
> > I've got a system that allows me to set Lua functions as callback
> > actions from GtkMenuItems.  You can find he code here:
> >
> > // generic interface:
> > http://www.mat.ucsb.edu/projects/luaAV/browser/trunk/LuaAV/library/src/LuaAVMenu.cpp
> > 471: int MenuItem :: action(lua_State *L)
> >
> >
> > // gtk callback system:
> > http://www.mat.ucsb.edu/projects/luaAV/browser/trunk/LuaAV/library/linux/LuaAVMenuLinux.cpp
> > 93: void MenuBarImpl :: dispatch(MenuItem *mi)
> > 419: void MenuItem :: implEnableAction()
> >
> >
> > wes
> >