lua-users home
lua-l archive

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


thanks for great idea.
But I have problem with lua_State *  in my_callback fuction, because
it has only "int pos " as argument .
It's defined like :
typedef void (CV_CDECL *CvTrackbarCallback)(int pos);

And only way to pass stack to callback function is in global variable,
but this isn't so good technique as I hoped.

But yes sample code like this works.

lua_State *state;
char funcname[100];

void mycallback(int pos)

static int luacv_cvCreateTrackbar(lua_State *L)
  const char *trackbar_name=luaL_checkstring(L,1);
  const char *window_name=luaL_checkstring(L,2);
  int value=luaL_checkint(L,3);
  int count=luaL_checkint(L,4);

  if (lua_isfunction(L,-1))
    luaL_error(L,"luacv.cvCreateTrackbar(string trackbar_name,string
window_name,int value,int count, name of 'void func(int pos))'");

return 0;

if  state and funcname are global variables, then it works. But this
way is limited to only one Trackbar widget per script.

Unfortunately I can't use lua_State  in mycallback as you wrote with
gpointer, because
cvCreateTrackbar function is platform depended. Depends on GUI you
use, including WinAPI.

With regards,
Jiri Prymus

2009/10/18 Francesco Abbate <>:
> 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 <>
>> 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 <>:
>> > 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:
>> >
>> > 471: int MenuItem :: action(lua_State *L)
>> >
>> >
>> > // gtk callback system:
>> >
>> > 93: void MenuBarImpl :: dispatch(MenuItem *mi)
>> > 419: void MenuItem :: implEnableAction()
>> >
>> >
>> > wes
>> >