Commits

Jérôme Vuarand committed d026f68

Added the possibility to use the default libusb context.

Comments (0)

Files changed (1)

 	return ctx;
 }
 
+libusb_context* luausb_opt_context(lua_State* L, int index, libusb_context* d)
+{
+	if (lua_isnoneornil(L, index))
+		return d;
+	else
+		return luausb_check_context(L, index);
+}
+
 void luausb_push_context(lua_State* L, libusb_context* value)
 {
 	libusb_context** udata;
 
 BINDING(init)
 {
+	int alloc_context;
 	libusb_context* context;
 	int result;
 	
-	result = libusb_init(&context);
-	if (result != LIBUSB_SUCCESS)
-		return lua__usberror(L, result);
+	alloc_context = lua_toboolean(L, 1);
 	
-	luausb_push_context(L, context);
-	return 1;
+	if (alloc_context)
+	{
+		result = libusb_init(&context);
+		if (result != LIBUSB_SUCCESS)
+			return lua__usberror(L, result);
+		
+		luausb_push_context(L, context);
+		return 1;
+	}
+	else
+	{
+		result = libusb_init(NULL);
+		if (result != LIBUSB_SUCCESS)
+			return lua__usberror(L, result);
+		
+		lua_pushboolean(L, 1);
+		return 1;
+	}
 }
 
 int luausb_context_gc(lua_State* L)
 
 BINDING(exit)
 {
-	luausb_check_context(L, 1);
-	return luausb_context_gc(L);
+	if (luausb_opt_context(L, 1, NULL))
+		return luausb_context_gc(L);
+	else
+	{
+		libusb_exit(NULL);
+		return 0;
+	}
 }
 
 BINDING(get_device_list)
 	libusb_device** list;
 	ssize_t result, i;
 	
-	ctx = luausb_check_context(L, 1);
+	ctx = luausb_opt_context(L, 1, NULL);
 	
 	result = libusb_get_device_list(ctx, &list);
 	if (result < 0)
 	uint16_t product_id;
 	libusb_device_handle* result;
 	
-	ctx = luausb_check_context(L, 1);
+	ctx = luausb_opt_context(L, 1, NULL);
 	vendor_id = (uint16_t)luaL_checknumber(L, 2); /* :FIXME: handle overflow */
 	product_id = (uint16_t)luaL_checknumber(L, 3); /* :FIXME: handle overflow */
 	
 	libusb_context* ctx;
 	int result;
 	
-	ctx = luausb_check_context(L, 1);
+	ctx = luausb_opt_context(L, 1, NULL);
 	
 	result = libusb_handle_events_completed(ctx, NULL);
 	if (result != LIBUSB_SUCCESS)
 	/* module functions */
 	lua_pushvalue(L, 1);
 	setfuncs(L, functions, 1);
+	lua_pushvalue(L, 1);
+	setfuncs(L, libusb_context__methods, 1);
 	return 1;
 }
 
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.