Skip to content

Commit

Permalink
Fix another hidapi releated issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
pbludov committed Apr 23, 2017
1 parent 3e702d3 commit bfb2da3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
18 changes: 15 additions & 3 deletions libqhid/qhiddevice_hidapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,24 @@ static void resetDevice(int vendorId, int deviceId)
}
#endif

static int hidapiUsed = 0;

QHIDDevicePrivate::QHIDDevicePrivate(QHIDDevice *q_ptr, int vendorId, int deviceId, int usagePage, int usage)
: device(nullptr)
, vendorId(vendorId)
, deviceId(deviceId)
, q_ptr(q_ptr)
{
if (hid_init() != 0)
// Make sure we call hid_init() only once.
if (hidapiUsed == 0 && hid_init() != 0)
{
qWarning() << "hid_init failed, error" << errno;
return;
}

// Increment hidapi library usage counter.
++hidapiUsed;

int interfaceNumber = -1;
#ifdef WITH_LIBUSB_1_0
hidapiMissingFeatures(
Expand Down Expand Up @@ -292,12 +298,18 @@ QHIDDevicePrivate::~QHIDDevicePrivate()
device = nullptr;

#ifdef WITH_LIBUSB_1_0
// Until reset the keyboard interface will be ignored by the kernel.
// Until reset the keyboard interface will be ignored by the kernel
// since the hidapi library does detach the kernel driver and does
// not re-attach it back.
resetDevice(vendorId, deviceId);
#endif
}

hid_exit();
// Only the last one should call hid_exit(),
if (--hidapiUsed == 0)
{
hid_exit();
}
}

bool QHIDDevicePrivate::isValid() const
Expand Down
1 change: 0 additions & 1 deletion src/ms735.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ MS735::MS735(QObject *parent)
, eventDevice(new QHIDDevice(VENDOR, PRODUCT, EVENT_USAGE_PAGE, EVENT_USAGE, this))
, monitor(new QHIDMonitor(VENDOR, PRODUCT, this))
, timerId(0)

{
connect(monitor, SIGNAL(deviceArrival(QString)), this, SLOT(deviceArrival(QString)));
connect(monitor, SIGNAL(deviceRemove()), this, SLOT(deviceRemove()));
Expand Down
1 change: 1 addition & 0 deletions src/ms735.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class MS735 : public QObject
CmdControl = 0x0C,
CmdButtons = 0x0D,
CmdMacro = 0x0F,
CmdVersion = 0x10, // 0x91 => 01 01, this may be 1.1 version.
CmdFlagGet = 0x80,
CmdPing = CmdBlink | CmdFlagGet,
CmdGetProfile = CmdProfile | CmdFlagGet,
Expand Down

0 comments on commit bfb2da3

Please sign in to comment.