Commits

Jérôme Vuarand committed 52f1825

Binding for get_pollfds and pollfds_handle_timeouts.

Comments (0)

Files changed (1)

 #include <lauxlib.h>
 #include <libusb.h>
 #include <string.h>
+#include <poll.h>
+#include <stdlib.h>
 #include "compat.h"
 #include "enums.h"
 #include "structs.h"
 #define BINDING(f) static int lua__libusb_##f(lua_State* L)
 #define BIND(f) {#f, lua__libusb_##f},
 
+struct int_name_t
+{
+	const char* name;
+	int value;
+};
+
+/****************************************************************************/
+
 BINDING(init)
 {
 	int alloc_context;
 	return 1;
 }
 
+static struct int_name_t poll_events[] = {
+	{"POLLIN", POLLIN},
+	{"POLLRDNORM", POLLRDNORM},
+	{"POLLRDBAND", POLLRDBAND},
+	{"POLLPRI", POLLPRI},
+	{"POLLOUT", POLLOUT},
+	{"POLLWRNORM", POLLWRNORM},
+	{"POLLWRBAND", POLLWRBAND},
+	{"POLLERR", POLLERR},
+	{"POLLHUP", POLLHUP},
+	{"POLLNVAL", POLLNVAL},
+	{0, 0},
+};
+
+BINDING(get_pollfds)
+{
+	libusb_context* ctx;
+	const struct libusb_pollfd** result;
+	const struct libusb_pollfd** it;
+	
+	ctx = luausb_opt_context(L, 1, NULL);
+	
+	result = libusb_get_pollfds(ctx);
+	if (!result)
+	{
+		lua_pushnil(L);
+		return 1;
+	}
+	
+	lua_newtable(L);
+	it = result;
+	while (*it)
+	{
+		int fd;
+		short events;
+		fd = (*it)->fd;
+		events = (*it)->events;
+		if (events != 0)
+		{
+			struct int_name_t* p;
+			lua_pushnumber(L, fd);
+			lua_newtable(L);
+			for (p=poll_events; p->name; ++p)
+			{
+				if (events & p->value)
+				{
+					lua_pushboolean(L, 1);
+					lua_setfield(L, -2, p->name);
+				}
+			}
+			lua_settable(L, -3);
+		}
+		++it;
+	}
+	free(result);
+	return 1;
+}
+
+BINDING(pollfds_handle_timeouts)
+{
+	libusb_context* ctx;
+	int result;
+	
+	ctx = luausb_opt_context(L, 1, NULL);
+	
+	result = libusb_pollfds_handle_timeouts(ctx);
+	
+	lua_pushboolean(L, result);
+	return 1;
+}
+
 /****************************************************************************/
 
 #define GETTER(c, f) int luausb_get_##c##_##f(lua_State* L)
 	BIND(open_device_with_vid_pid)
 	BIND(handle_events)
 	BIND(handle_events_completed)
+	BIND(get_pollfds)
+	BIND(pollfds_handle_timeouts)
 	{0, 0},
 };
 /*
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.