Skip to content

Commit fcea632

Browse files
committed
Make libusb mandatory.
We will need it after hidapi device path stabilization. libusb/hidapi#291 USBInit class now have its own source files.
1 parent 81e5176 commit fcea632

File tree

9 files changed

+189
-113
lines changed

9 files changed

+189
-113
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ Build Dependencies
3333
- sys-fs/udev
3434
- sys-apps/systemd
3535
* dev-libs/libevdev >= 1.5.7
36-
* a libusb provider :
37-
- dev-libs/hidapi (libusb backend) >= 0.10.0
38-
- dev-libs/libusb >= 1.0.19
36+
* dev-libs/libusb >= 1.0.19
3937

4038
Optional Build Dependencies
4139
===========================
4240

41+
* dev-libs/hidapi (libusb backend) >= 0.10.0
4342
* sys-apps/dbus >= 1.10.18
4443
* x11-libs/libICE
4544
* x11-libs/libSM

configure.ac

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,12 @@ dnl -------------------------------------------------------------------------
317317

318318
PKG_CHECK_MODULES([LIBUDEV], [libudev])
319319
PKG_CHECK_MODULES([LIBEVDEV], [libevdev])
320+
PKG_CHECK_MODULES([LIBUSB], [libusb-1.0 >= 1.0.19])
320321

321322
AS_IF([test "x$enable_hidapi" = "xyes"],
322323
[
323324
PKG_CHECK_MODULES([LIBHIDAPI], [hidapi-libusb >= 0.10.0])
324-
], [
325-
PKG_CHECK_MODULES([LIBUSB], [libusb-1.0 >= 1.0.19])
326-
])
325+
], [])
327326

328327
AS_IF([test "x$enable_dbus" = "xyes"],
329328
[

src/bin/daemon/Makefile.am

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sbin_PROGRAMS = GLogiKd
44
GLogiKd_CXXFLAGS = \
55
@LIBEVDEV_CFLAGS@ \
66
@BOOST_CPPFLAGS@ \
7+
@LIBUSB_CFLAGS@ \
78
@PTHREAD_CFLAGS@ \
89
@AM_CXXFLAGS@
910

@@ -18,6 +19,7 @@ GLogiKd_LDADD = \
1819
@BOOST_FILESYSTEM_LIB@ \
1920
@BOOST_IOSTREAMS_LIB@ \
2021
@LIBUDEV_LIBS@ \
22+
@LIBUSB_LIBS@ \
2123
@PTHREAD_LIBS@ \
2224
libGKUtils.la \
2325
libGKShared.la
@@ -33,6 +35,8 @@ GLogiKd_SOURCES = \
3335
%D%/USBAPIenums.hpp \
3436
%D%/keyboardDriver.cpp \
3537
%D%/keyboardDriver.hpp \
38+
%D%/usbinit.cpp \
39+
%D%/usbinit.hpp \
3640
%D%/USBDevice.cpp \
3741
%D%/USBDevice.hpp \
3842
%D%/USBDeviceID.cpp \
@@ -48,12 +52,6 @@ GLogiKd_SOURCES = \
4852

4953

5054
if WITH_LIBUSB
51-
GLogiKd_CXXFLAGS += \
52-
@LIBUSB_CFLAGS@
53-
54-
GLogiKd_LDADD += \
55-
@LIBUSB_LIBS@
56-
5755
GLogiKd_SOURCES += \
5856
%D%/libusb.cpp \
5957
%D%/libusb.hpp

src/bin/daemon/USBDevice.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141

4242
#include <config.h>
4343

44-
#if GKLIBUSB
4544
#include <libusb-1.0/libusb.h>
46-
#elif GKHIDAPI
45+
46+
#if GKHIDAPI
4747
#include <hidapi.h>
4848
#endif
4949

@@ -66,7 +66,6 @@ class USBDevice
6666

6767
#if GKLIBUSB
6868
private:
69-
friend class USBInit;
7069
friend class libusb;
7170

7271
std::mutex _libUSBMutex;
@@ -90,11 +89,14 @@ class USBDevice
9089
uint64_t _LCDPluginsMask1;
9190

9291
private:
92+
friend class USBInit;
93+
9394
MacrosManager* _pMacrosManager;
9495
LCDScreenPluginsManager* _pLCDPluginsManager;
9596

96-
#if GKLIBUSB
9797
libusb_device* _pUSBDevice;
98+
99+
#if GKLIBUSB
98100
libusb_device_handle* _pUSBDeviceHandle;
99101
#elif GKHIDAPI
100102
friend class hidapi;

src/bin/daemon/libusb.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ namespace GLogiK
3737

3838
using namespace NSGKUtils;
3939

40-
libusb_context * USBInit::pContext = nullptr;
41-
uint8_t USBInit::counter = 0;
42-
bool USBInit::status = false;
43-
4440
/*
4541
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
4642
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
@@ -51,83 +47,6 @@ bool USBInit::status = false;
5147
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
5248
*/
5349

54-
USBInit::USBInit(void)
55-
{
56-
if( ! USBInit::status ) {
57-
#if DEBUGGING_ON
58-
LOG(DEBUG3) << "initializing libusb";
59-
#endif
60-
int ret = libusb_init( &(USBInit::pContext) );
61-
if ( this->USBError(ret) ) {
62-
throw GLogiKExcept("libusb initialization failure");
63-
}
64-
65-
USBInit::status = true;
66-
}
67-
68-
USBInit::counter++;
69-
}
70-
71-
USBInit::~USBInit(void)
72-
{
73-
USBInit::counter--;
74-
75-
if (USBInit::status and USBInit::counter == 0) {
76-
#if DEBUGGING_ON
77-
LOG(DEBUG3) << "closing libusb";
78-
#endif
79-
libusb_exit(USBInit::pContext);
80-
USBInit::status = false;
81-
}
82-
}
83-
84-
int USBInit::USBError(int errorCode) noexcept
85-
{
86-
switch(errorCode) {
87-
case LIBUSB_SUCCESS:
88-
break;
89-
default:
90-
std::ostringstream buffer(std::ios_base::app);
91-
buffer << "libusb error (" << libusb_error_name(errorCode) << ") : "
92-
<< libusb_strerror( (libusb_error)errorCode );
93-
GKSysLog(LOG_ERR, ERROR, buffer.str());
94-
break;
95-
}
96-
97-
return errorCode;
98-
}
99-
100-
void USBInit::seekUSBDevice(USBDevice & device)
101-
{
102-
libusb_device **list;
103-
int numDevices = libusb_get_device_list(USBInit::pContext, &(list));
104-
if( numDevices < 0 ) {
105-
this->USBError(numDevices);
106-
throw GLogiKExcept("error getting USB devices list");
107-
}
108-
109-
for (int i = 0; i < numDevices; ++i) {
110-
device._pUSBDevice = list[i];
111-
if( libusb_get_bus_number(device._pUSBDevice) == device.getBus() and
112-
libusb_get_device_address(device._pUSBDevice) == device.getNum() ) {
113-
break;
114-
}
115-
device._pUSBDevice = nullptr;
116-
}
117-
118-
if( device._pUSBDevice == nullptr ) {
119-
std::ostringstream buffer(std::ios_base::app);
120-
buffer << "libusb cannot find device " << device.getNum()
121-
<< " on bus " << device.getBus();
122-
libusb_free_device_list(list, 1);
123-
throw GLogiKExcept(buffer.str());
124-
}
125-
126-
libusb_free_device_list(list, 1);
127-
}
128-
129-
/* -- -- -- */
130-
13150
void libusb::openUSBDevice(USBDevice & device)
13251
{
13352
/* throws on failure */

src/bin/daemon/libusb.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,6 @@
3131
namespace GLogiK
3232
{
3333

34-
class USBInit
35-
{
36-
public:
37-
protected:
38-
USBInit(void);
39-
~USBInit(void);
40-
41-
int USBError(int errorCode) noexcept;
42-
void seekUSBDevice(USBDevice & device);
43-
44-
private:
45-
static libusb_context * pContext;
46-
static uint8_t counter; /* initialized drivers counter */
47-
static bool status; /* is libusb initialized ? */
48-
};
49-
5034
class libusb
5135
: private USBInit
5236
{

src/bin/daemon/usbinit.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
*
3+
* This file is part of GLogiK project.
4+
* GLogiK, daemon to handle special features on gaming keyboards
5+
* Copyright (C) 2016-2021 Fabrice Delliaux <[email protected]>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#include "lib/utils/utils.hpp"
23+
24+
#include "usbinit.hpp"
25+
26+
namespace GLogiK
27+
{
28+
29+
using namespace NSGKUtils;
30+
31+
libusb_context * USBInit::pContext = nullptr;
32+
uint8_t USBInit::counter = 0;
33+
bool USBInit::status = false;
34+
35+
/*
36+
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
37+
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
38+
*
39+
* === protected === protected === protected === protected ===
40+
*
41+
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
42+
* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
43+
*/
44+
45+
USBInit::USBInit(void)
46+
{
47+
if( ! USBInit::status ) {
48+
#if DEBUGGING_ON
49+
LOG(DEBUG3) << "initializing libusb";
50+
#endif
51+
int ret = libusb_init( &(USBInit::pContext) );
52+
if ( this->USBError(ret) ) {
53+
throw GLogiKExcept("libusb initialization failure");
54+
}
55+
56+
USBInit::status = true;
57+
}
58+
59+
USBInit::counter++;
60+
}
61+
62+
USBInit::~USBInit(void)
63+
{
64+
USBInit::counter--;
65+
66+
if (USBInit::status and USBInit::counter == 0) {
67+
#if DEBUGGING_ON
68+
LOG(DEBUG3) << "closing libusb";
69+
#endif
70+
libusb_exit(USBInit::pContext);
71+
USBInit::status = false;
72+
}
73+
}
74+
75+
int USBInit::USBError(int errorCode) noexcept
76+
{
77+
switch(errorCode) {
78+
case LIBUSB_SUCCESS:
79+
break;
80+
default:
81+
std::ostringstream buffer(std::ios_base::app);
82+
buffer << "libusb error (" << libusb_error_name(errorCode) << ") : "
83+
<< libusb_strerror( (libusb_error)errorCode );
84+
GKSysLog(LOG_ERR, ERROR, buffer.str());
85+
break;
86+
}
87+
88+
return errorCode;
89+
}
90+
91+
void USBInit::seekUSBDevice(USBDevice & device)
92+
{
93+
libusb_device **list;
94+
int numDevices = libusb_get_device_list(USBInit::pContext, &(list));
95+
if( numDevices < 0 ) {
96+
this->USBError(numDevices);
97+
throw GLogiKExcept("error getting USB devices list");
98+
}
99+
100+
for (int i = 0; i < numDevices; ++i) {
101+
device._pUSBDevice = list[i];
102+
if( libusb_get_bus_number(device._pUSBDevice) == device.getBus() and
103+
libusb_get_device_address(device._pUSBDevice) == device.getNum() ) {
104+
break;
105+
}
106+
device._pUSBDevice = nullptr;
107+
}
108+
109+
if( device._pUSBDevice == nullptr ) {
110+
std::ostringstream buffer(std::ios_base::app);
111+
buffer << "libusb cannot find device " << device.getNum()
112+
<< " on bus " << device.getBus();
113+
libusb_free_device_list(list, 1);
114+
throw GLogiKExcept(buffer.str());
115+
}
116+
117+
libusb_free_device_list(list, 1);
118+
}
119+
120+
} // namespace GLogiK
121+

src/bin/daemon/usbinit.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
*
3+
* This file is part of GLogiK project.
4+
* GLogiK, daemon to handle special features on gaming keyboards
5+
* Copyright (C) 2016-2021 Fabrice Delliaux <[email protected]>
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef SRC_BIN_DAEMON_USBINIT_HPP_
23+
#define SRC_BIN_DAEMON_USBINIT_HPP_
24+
25+
#include <cstdint>
26+
27+
#include <libusb-1.0/libusb.h>
28+
29+
#include "USBDevice.hpp"
30+
31+
namespace GLogiK
32+
{
33+
34+
class USBInit
35+
{
36+
public:
37+
protected:
38+
USBInit(void);
39+
~USBInit(void);
40+
41+
int USBError(int errorCode) noexcept;
42+
void seekUSBDevice(USBDevice & device);
43+
44+
private:
45+
static libusb_context * pContext;
46+
static uint8_t counter; /* initialized drivers counter */
47+
static bool status; /* is libusb initialized ? */
48+
};
49+
50+
} // namespace GLogiK
51+
52+
#endif

0 commit comments

Comments
 (0)