Use explict libusb_context

This commit is contained in:
Alexander Ponyatykh committed 2019-11-18 02:50:24 +03:00
1 parent aa2cf02029
commit 80dac3d2d6
1 file changed
+35 -35
@@ -7,11 +7,9 @@ Subject: Migrate from legacy libusb-0.1 to libusb-1.0
libg15.c | 476 +++++++++++++++++++++++++++++------------------------------ libg15.c | 476 +++++++++++++++++++++++++++++------------------------------
2 files changed, 234 insertions(+), 247 deletions(-) 2 files changed, 234 insertions(+), 247 deletions(-)
diff --git a/configure.in b/configure.in
index 074d98e..b533d2c 100644
--- a/configure.in --- a/configure.in
+++ b/configure.in +++ b/configure.in
@@ -31,7 +31,8 @@ case $host_os in @@ -31,7 +31,8 @@
esac esac
# Checks for libraries. # Checks for libraries.
@@ -21,7 +19,7 @@ index 074d98e..b533d2c 100644
dnl Some versions of libusb do not honour timeout and block. Suns' version is one of them. dnl Some versions of libusb do not honour timeout and block. Suns' version is one of them.
if test "x$libusb_blocks" == "xtrue"; then if test "x$libusb_blocks" == "xtrue"; then
@@ -41,7 +42,7 @@ fi @@ -41,7 +42,7 @@
# Checks for header files. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([string.h]) AC_CHECK_HEADERS([string.h])
@@ -30,11 +28,9 @@ index 074d98e..b533d2c 100644
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
diff --git a/libg15.c b/libg15.c
index ba1c94b..435e396 100644
--- a/libg15.c --- a/libg15.c
+++ b/libg15.c +++ b/libg15.c
@@ -24,14 +24,15 @@ @@ -24,20 +24,21 @@
#include "libg15.h" #include "libg15.h"
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
@@ -49,22 +45,28 @@ index ba1c94b..435e396 100644
+static libusb_device_handle *keyboard_device = 0; +static libusb_device_handle *keyboard_device = 0;
static int libg15_debugging_enabled = 0; static int libg15_debugging_enabled = 0;
-static int enospc_slowdown = 0; -static int enospc_slowdown = 0;
+static int libusb_initialized = 0;
static int found_devicetype = -1; static int found_devicetype = -1;
static int shared_device = 0; static int shared_device = 0;
@@ -67,7 +68,9 @@ int g15DeviceCapabilities() { static int g15_keys_endpoint = 0;
static int g15_lcd_endpoint = 0;
static pthread_mutex_t libusb_mutex;
+static libusb_context *libg15_libusb_context = NULL;
/* to add a new device, simply create a new DEVICE() in this list */
/* Fields are: "Name",VendorID,ProductID,Capabilities */
@@ -67,7 +68,9 @@
void libg15Debug(int option) { void libg15Debug(int option) {
libg15_debugging_enabled = option; libg15_debugging_enabled = option;
- usb_set_debug(option); - usb_set_debug(option);
+ if (option && libusb_initialized) { + if (option && libg15_libusb_context) {
+ libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING); + libusb_set_option(libg15_libusb_context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_WARNING);
+ } + }
} }
/* debugging wrapper */ /* debugging wrapper */
@@ -86,191 +89,209 @@ static int g15_log (FILE *fd, unsigned int level, const char *fmt, ...) { @@ -86,191 +89,207 @@
/* return number of connected and supported devices */ /* return number of connected and supported devices */
int g15NumberOfConnectedDevices() { int g15NumberOfConnectedDevices() {
@@ -77,7 +79,7 @@ index ba1c94b..435e396 100644
- for (i=0; g15_devices[i].name !=NULL;i++) - for (i=0; g15_devices[i].name !=NULL;i++)
- for (bus = usb_busses; bus; bus = bus->next) - for (bus = usb_busses; bus; bus = bus->next)
+ ssize_t cnt = libusb_get_device_list(NULL, &list); + ssize_t cnt = libusb_get_device_list(libg15_libusb_context, &list);
+ +
+ if (cnt < 0) + if (cnt < 0)
+ { + {
@@ -108,13 +110,13 @@ index ba1c94b..435e396 100644
static int initLibUsb() static int initLibUsb()
{ {
- usb_init(); - usb_init();
- + int errcode = libusb_init(&libg15_libusb_context);
- /** - /**
- * usb_find_busses and usb_find_devices both report the number of devices - * usb_find_busses and usb_find_devices both report the number of devices
- * / busses added / removed since the last call. since this is the first - * / busses added / removed since the last call. since this is the first
- * call we have to return values != 0 or else we didnt find anything */ - * call we have to return values != 0 or else we didnt find anything */
+ int errcode = libusb_init(NULL); -
- if (!usb_find_busses()) - if (!usb_find_busses())
+ if (errcode != 0) + if (errcode != 0)
+ { + {
@@ -124,8 +126,6 @@ index ba1c94b..435e396 100644
- if (!usb_find_devices()) - if (!usb_find_devices())
- return G15_ERROR_OPENING_USB_DEVICE; - return G15_ERROR_OPENING_USB_DEVICE;
+ libusb_initialized = 1;
+
+ libg15Debug(libg15_debugging_enabled); + libg15Debug(libg15_debugging_enabled);
return G15_NO_ERROR; return G15_NO_ERROR;
@@ -147,7 +147,7 @@ index ba1c94b..435e396 100644
+ int retries = 0; + int retries = 0;
+ int ret; + int ret;
+ +
+ ssize_t cnt = libusb_get_device_list(NULL, &list); + ssize_t cnt = libusb_get_device_list(libg15_libusb_context, &list);
- for (bus = usb_busses; bus; bus = bus->next) - for (bus = usb_busses; bus; bus = bus->next)
+ if (cnt < 0) + if (cnt < 0)
@@ -418,7 +418,7 @@ index ba1c94b..435e396 100644
int i; int i;
for (i=0; g15_devices[i].name !=NULL ;i++){ for (i=0; g15_devices[i].name !=NULL ;i++){
g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name); g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
@@ -288,19 +309,13 @@ static int closeKeyboardDevice() @@ -288,19 +307,13 @@
{ {
int retval = G15_NO_ERROR; int retval = G15_NO_ERROR;
@@ -443,7 +443,7 @@ index ba1c94b..435e396 100644
keyboard_device=0; keyboard_device=0;
return retval; return retval;
} }
@@ -311,9 +326,6 @@ int re_initLibG15() @@ -311,9 +324,6 @@
{ {
closeKeyboardDevice(); closeKeyboardDevice();
@@ -453,7 +453,7 @@ index ba1c94b..435e396 100644
keyboard_device = findAndOpenG15(); keyboard_device = findAndOpenG15();
if (!keyboard_device) if (!keyboard_device)
return G15_ERROR_OPENING_USB_DEVICE; return G15_ERROR_OPENING_USB_DEVICE;
@@ -330,10 +342,6 @@ int initLibG15() @@ -330,10 +340,6 @@
g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING); g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING);
@@ -464,18 +464,18 @@ index ba1c94b..435e396 100644
g15NumberOfConnectedDevices(); g15NumberOfConnectedDevices();
keyboard_device = findAndOpenG15(); keyboard_device = findAndOpenG15();
@@ -347,7 +355,9 @@ int initLibG15() @@ -347,7 +353,9 @@
int exitLibG15() int exitLibG15()
{ {
pthread_mutex_destroy(&libusb_mutex); pthread_mutex_destroy(&libusb_mutex);
- return closeKeyboardDevice(); - return closeKeyboardDevice();
+ int ret = closeKeyboardDevice(); + int ret = closeKeyboardDevice();
+ libusb_exit(NULL); + libusb_exit(libg15_libusb_context);
+ libusb_initialized = 0; + libg15_libusb_context = NULL;
} }
static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data) static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data)
@@ -428,36 +438,6 @@ static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char con @@ -428,36 +436,6 @@
} }
} }
@@ -512,7 +512,7 @@ index ba1c94b..435e396 100644
int writePixmapToLCD(unsigned char const *data) int writePixmapToLCD(unsigned char const *data)
{ {
int ret = 0; int ret = 0;
@@ -474,43 +454,27 @@ int writePixmapToLCD(unsigned char const *data) @@ -474,43 +452,27 @@
/* the keyboard needs this magic byte */ /* the keyboard needs this magic byte */
lcd_buffer[0] = 0x03; lcd_buffer[0] = 0x03;
@@ -574,7 +574,7 @@ index ba1c94b..435e396 100644
return 0; return 0;
} }
@@ -535,7 +499,9 @@ int setLCDContrast(unsigned int level) @@ -535,7 +497,9 @@
usb_data[3] = 18; usb_data[3] = 18;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -585,7 +585,7 @@ index ba1c94b..435e396 100644
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -553,7 +519,9 @@ int setLEDs(unsigned int leds) @@ -553,7 +517,9 @@
return G15_ERROR_UNSUPPORTED; return G15_ERROR_UNSUPPORTED;
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -596,7 +596,7 @@ index ba1c94b..435e396 100644
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -578,7 +546,9 @@ int setLCDBrightness(unsigned int level) @@ -578,7 +544,9 @@
usb_data[2] = 0x00; usb_data[2] = 0x00;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -607,7 +607,7 @@ index ba1c94b..435e396 100644
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -604,7 +574,9 @@ int setKBBrightness(unsigned int level) @@ -604,7 +572,9 @@
usb_data[2] = 0x0; usb_data[2] = 0x0;
} }
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -618,7 +618,7 @@ index ba1c94b..435e396 100644
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -619,7 +591,9 @@ int setG510LEDColor(unsigned char r, unsigned char g, unsigned char b) @@ -619,7 +589,9 @@
usb_data[3] = b; usb_data[3] = b;
pthread_mutex_lock(&libusb_mutex); pthread_mutex_lock(&libusb_mutex);
@@ -629,7 +629,7 @@ index ba1c94b..435e396 100644
pthread_mutex_unlock(&libusb_mutex); pthread_mutex_unlock(&libusb_mutex);
return retval; return retval;
} }
@@ -1033,18 +1007,30 @@ static void processKeyEvent4Byte(unsigned int *pressed_keys, unsigned char *buff @@ -1033,18 +1005,30 @@
int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout) int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout)
{ {
unsigned char buffer[G15_KEY_READ_LENGTH]; unsigned char buffer[G15_KEY_READ_LENGTH];
@@ -667,7 +667,7 @@ index ba1c94b..435e396 100644
return G15_ERROR_TRY_AGAIN; return G15_ERROR_TRY_AGAIN;
} }
@@ -1054,7 +1040,7 @@ int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout) @@ -1054,7 +1038,7 @@
return G15_NO_ERROR; return G15_NO_ERROR;
} }
@@ -676,7 +676,7 @@ index ba1c94b..435e396 100644
case 4: case 4:
processKeyEvent4Byte(pressed_keys, buffer); processKeyEvent4Byte(pressed_keys, buffer);
return G15_NO_ERROR; return G15_NO_ERROR;
@@ -1065,6 +1051,6 @@ int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout) @@ -1065,6 +1049,6 @@
processKeyEvent9Byte(pressed_keys, buffer); processKeyEvent9Byte(pressed_keys, buffer);
return G15_NO_ERROR; return G15_NO_ERROR;
default: default: