+/* Takes a { fd : events } table as first arg and modifies it to { fd : revents } */
+static int vlclua_net_poll( lua_State *L )
+{
+ luaL_checktype( L, 1, LUA_TTABLE );
+ double f_timeout = luaL_optnumber( L, 2, -1. );
+
+ int i_fds = 0;
+ lua_pushnil( L );
+ while( lua_next( L, 1 ) )
+ {
+ i_fds++;
+ lua_pop( L, 1 );
+ }
+ struct pollfd *p_fds = malloc( i_fds * sizeof( struct pollfd ) );
+ lua_pushnil( L );
+ int i = 0;
+ while( lua_next( L, 1 ) )
+ {
+ p_fds[i].fd = luaL_checkinteger( L, -2 );
+ p_fds[i].events = luaL_checkinteger( L, -1 );
+ p_fds[i].revents = 0;
+ lua_pop( L, 1 );
+ i++;
+ }
+
+ int i_ret = poll( p_fds, i_fds, f_timeout < 0. ? -1 : (int)(f_timeout*1000) );
+ for( i = 0; i < i_fds; i++ )
+ {
+ lua_pushinteger( L, p_fds[i].fd );
+ lua_pushinteger( L, p_fds[i].revents );
+ lua_settable( L, 1 );
+ }
+ free( p_fds );
+ lua_pushinteger( L, i_ret );
+ return 1;
+}
+