Guest User

Untitled

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. /** Calls the callback function for the GIO channel
  2.  *    Returns FALSE if the event should be removed
  3.  *
  4.  * \see luaH_luakit_idle_add_socket
  5.  *
  6.  * \param source The channel that triggered the event
  7.  * \param condition The condition that triggered the event
  8.  * \param func Lua callback function
  9.  */
  10. static gboolean
  11. idle_socket_cb(GIOChannel *source, GIOCondition condition, gpointer func)
  12. {
  13.     lua_State *L = globalconf.L;
  14.  
  15.     gint top = lua_gettop(L);
  16.     gboolean keep = FALSE;
  17.  
  18.     /* setup stack for call, first the function to call*/
  19.     luaH_object_push(L, func);
  20.     /* now come the arguments*/
  21.     int fd = g_io_channel_unix_get_fd(source);
  22.     lua_pushnumber(L, fd);
  23.     lua_pushnumber(L, condition);
  24.     /*make the jump to light speed...*/
  25.     if (lua_pcall(L, 2, 1, 0))
  26.         warn("error in socket callback: %s", lua_tostring(L, -1));
  27.     else
  28.         keep = lua_toboolean(L, -1);
  29.  
  30.     if (!keep)
  31.         luaH_object_unref(L, func);
  32.  
  33.     lua_settop(L, top);
  34.  
  35.     return keep;
  36. }
  37.  
  38. /** Adds the GIOChannel into the default main loop context with the default
  39.  * priority.  
  40.  *
  41.  * \param  L The lua VM state.
  42.  * \return   The number of elements pushed on stack
  43.  *
  44.  * \luastack
  45.  * \lparam fd  The file descriptor to create a watch for
  46.  * \lparam condition The conditions for the watch to trigger an event
  47.  * \lparam func The callback function
  48.  */
  49. static gint
  50. luaH_luakit_idle_add_socket(lua_State *L)
  51. {
  52.     int fd = luaL_checkint(L, 1);
  53.     GIOCondition condition = luaL_checkint(L, 2);
  54.     luaH_checkfunction(L, 3);
  55.     gpointer func = luaH_object_ref(L, 3);
  56.     GIOChannel *channel = g_io_channel_unix_new(fd);
  57.     g_io_add_watch(channel, condition, idle_socket_cb, func);
  58.     return 0;
  59. }
Add Comment
Please, Sign In to add comment