4
0
Fork
You've already forked hidapi
1
A Simple cross-platform library for communicating with HID devices https://libusb.info/hidapi/
  • C 78.1%
  • M4 6.4%
  • CMake 6.4%
  • C++ 3.8%
  • Meson 2.9%
  • Other 2.4%
2025年03月03日 10:27:00 +00:00
.builds ci: Docker build for Alpine ( #537 ) 2023年05月01日 13:22:19 +03:00
.github/workflows Weekly runs for Coverity Scan ( #565 ) 2023年05月22日 13:50:06 +03:00
android/jni libusb: Add Android support 2014年07月23日 16:40:51 -04:00
dist podspec: include darwin-specific header ( #412 ) 2022年05月31日 14:58:01 +03:00
documentation Documentation refactoring and update 2021年07月03日 20:38:58 +03:00
doxygen Automate doxygen builds ( #521 ) 2023年04月05日 12:41:14 +03:00
hidapi meson: Created logic for installing the API header 2024年03月02日 15:00:56 +00:00
hidtest hidtest: print Bus Name 2023年05月05日 12:11:18 +03:00
libusb meson: Added an option to control what kind of build will be done when in a cross-build environment 2024年03月07日 13:41:41 +00:00
linux meson: Added an option to control what kind of build will be done when in a cross-build environment 2024年03月07日 13:41:41 +00:00
m4 Exclude ax_pthread.m4 from .gitignore 2012年07月16日 00:43:04 -04:00
mac meson: Added an option to control what kind of build will be done when in a cross-build environment 2024年03月07日 13:41:41 +00:00
netbsd meson: Added an option to control what kind of build will be done when in a cross-build environment 2024年03月07日 13:41:41 +00:00
pc add CMake build system 2021年07月03日 20:38:58 +03:00
src Fix typos in comments ( #523 ) 2023年04月06日 17:57:04 +03:00
subprojects Add meson.build as a wrapper over CMake ( #410 ) 2022年09月10日 16:52:46 +03:00
testgui Ported code from SetupApi to CfgMgr32 ( #362 ) 2022年01月21日 18:43:08 +02:00
udev Update 69-hid.rules ( #337 ) 2021年09月25日 02:52:04 +03:00
windows windows: Fix a multiple definitions warning 2025年03月03日 10:27:00 +00:00
.appveyor.yml Fix Cygwin Appveyor build ( #423 ) 2022年05月31日 13:16:03 +03:00
.cirrus.yml Add .cirrus.yml config for Cirrus-CI ( #15 ) 2020年01月22日 16:01:51 +02:00
.gitattributes Add a .gitattributes giving autoconf files LF line endings 2012年07月16日 00:43:04 -04:00
.gitignore Automate doxygen builds ( #521 ) 2023年04月05日 12:41:14 +03:00
AUTHORS.txt update authors link 2021年09月18日 18:56:56 +03:00
bootstrap Add Autotools build system (automake, autconf) 2012年07月16日 00:43:04 -04:00
BUILD.autotools.md Add deprecation message for Autotools 2021年09月20日 16:06:27 +03:00
BUILD.cmake.md doc: Shared+Static build for CMake ( #517 ) 2023年03月13日 12:42:33 +02:00
BUILD.md Fix typos in README ( #479 ) 2022年11月04日 14:01:41 +02:00
CMakeLists.txt Fix typos in comments ( #523 ) 2023年04月06日 17:57:04 +03:00
configure.ac Link gcc helpers statically on MinGW ( #488 ) 2022年12月12日 20:27:20 +02:00
HACKING.txt Document Coverity/Checks before release step ( #558 ) 2023年05月21日 11:02:55 +03:00
LICENSE-bsd.txt License files showing modified, but they aren't. Probably a line-ending problem. 2010年11月21日 20:02:32 -05:00
LICENSE-gpl3.txt License files showing modified, but they aren't. Probably a line-ending problem. 2010年11月21日 20:02:32 -05:00
LICENSE-orig.txt Additional licenses. HIDAPI can now be used under the GPLv3, BSD, or original 2010年09月13日 11:17:45 -04:00
LICENSE.txt all: Fix license name 2013年10月06日 17:33:44 -04:00
Makefile.am Changes to Build System for Haiku 2020年02月12日 17:00:07 +02:00
meson.build meson: Made sure to pass a C standard with the project options to get consistent and sane build behaviour (non-gnu modes do not work!) 2024年07月23日 19:43:24 +01:00
meson_options.txt meson: Added an option to control what kind of build will be done when in a cross-build environment 2024年03月07日 13:41:41 +00:00
README.md Add Coverity check to README ( #564 ) 2023年05月22日 13:34:57 +03:00
VERSION hid_get_report_descriptor: bump version to 0.14.0 ( #516 ) 2023年03月12日 16:48:48 +02:00

HIDAPI library for Windows, Linux, FreeBSD and macOS

CI instance Status
Linux/macOS/Windows (master) GitHub Builds
Windows (master) Build status
BSD, last build (branch/PR) builds.sr.ht status
Coverity Scan (last) Coverity Scan

HIDAPI is a multi-platform library which allows an application to interface with USB and Bluetooth HID-Class devices on Windows, Linux, FreeBSD, and macOS. HIDAPI can be either built as a shared library (.so, .dll or .dylib) or can be embedded directly into a target application by adding a single source file (per platform) and a single header.
See remarks on embedding directly into your build system.

HIDAPI library was originally developed by Alan Ott (signal11).

It was moved to libusb/hidapi on June 4th, 2019, in order to merge important bugfixes and continue development of the library.

Table of Contents

About

HIDAPI has four back-ends:

  • Windows (using hid.dll)
  • Linux/hidraw (using the Kernel's hidraw driver)
  • libusb (using libusb-1.0 - Linux/BSD/other UNIX-like systems)
  • macOS (using IOHidManager)

On Linux, either the hidraw or the libusb back-end can be used. There are tradeoffs, and the functionality supported is slightly different. Both are built by default. It is up to the application linking to hidapi to choose the backend at link time by linking to either libhidapi-libusb or libhidapi-hidraw.

Note that you will need to install an udev rule file with your application for unprivileged users to be able to access HID devices with hidapi. Refer to the 69-hid.rules file in the udev directory for an example.

Linux/hidraw (linux/hid.c):

This back-end uses the hidraw interface in the Linux kernel, and supports both USB and Bluetooth HID devices. It requires kernel version at least 2.6.39 to build. In addition, it will only communicate with devices which have hidraw nodes associated with them. Keyboards, mice, and some other devices which are blacklisted from having hidraw nodes will not work. Fortunately, for nearly all the uses of hidraw, this is not a problem.

Linux/FreeBSD/libusb (libusb/hid.c):

This back-end uses libusb-1.0 to communicate directly to a USB device. This back-end will of course not work with Bluetooth devices.

Test GUI

HIDAPI also comes with a Test GUI. The Test GUI is cross-platform and uses Fox Toolkit http://www.fox-toolkit.org. It will build on every platform which HIDAPI supports. Since it relies on a 3rd party library, building it is optional but it is useful when debugging hardware.

NOTE: Test GUI based on Fox Toolkit is not actively developed nor supported by HIDAPI team. It is kept as a historical artifact. It may even work sometime or on some platforms, but it is not going to get any new features or bugfixes.

Instructions for installing Fox-Toolkit on each platform is not provided. Make sure to use Fox-Toolkit v1.6 if you choose to use it.

Console Test App

If you want to play around with your HID device before starting any development with HIDAPI and using a GUI app is not an option for you, you may try hidapitester.

This app has a console interface for most of the features supported by HIDAPI library.

What Does the API Look Like?

The API provides the most commonly used HID functions including sending and receiving of input, output, and feature reports. The sample program, which communicates with a heavily hacked up version of the Microchip USB Generic HID sample looks like this (with error checking removed for simplicity):

Warning: Only run the code you understand, and only when it conforms to the device spec. Writing data (hid_write) at random to your HID devices can break them.

#include <stdio.h> // printf#include <wchar.h> // wchar_t
#include <hidapi.h>
#define MAX_STR 255

int main(int argc, char* argv[])
{
	int res;
	unsigned char buf[65];
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	int i;
	// Initialize the hidapi library
	res = hid_init();
	// Open the device using the VID, PID,
	// and optionally the Serial number.
	handle = hid_open(0x4d8, 0x3f, NULL);
	if (!handle) {
		printf("Unable to open device\n");
		hid_exit();
 		return 1;
	}
	// Read the Manufacturer String
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	printf("Manufacturer String: %ls\n", wstr);
	// Read the Product String
	res = hid_get_product_string(handle, wstr, MAX_STR);
	printf("Product String: %ls\n", wstr);
	// Read the Serial Number String
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	printf("Serial Number String: (%d) %ls\n", wstr[0], wstr);
	// Read Indexed String 1
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	printf("Indexed String 1: %ls\n", wstr);
	// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
	buf[0] = 0x0;
	buf[1] = 0x80;
	res = hid_write(handle, buf, 65);
	// Request state (cmd 0x81). The first byte is the report number (0x0).
	buf[0] = 0x0;
	buf[1] = 0x81;
	res = hid_write(handle, buf, 65);
	// Read requested state
	res = hid_read(handle, buf, 65);
	// Print out the returned buffer.
	for (i = 0; i < 4; i++)
		printf("buf[%d]: %d\n", i, buf[i]);
	// Close the device
	hid_close(handle);
	// Finalize the hidapi library
	res = hid_exit();
	return 0;
}

You can also use hidtest/test.c as a starting point for your applications.

License

HIDAPI may be used by one of three licenses as outlined in LICENSE.txt.

Installing HIDAPI

If you want to build your own application that uses HID devices with HIDAPI, you need to get HIDAPI development package.

Depending on what your development environment is, HIDAPI likely to be provided by your package manager.

For instance on Ubuntu, HIDAPI is available via APT:

sudo apt install libhidapi-dev

HIDAPI package name for other systems/package managers may differ. Check the documentation/package list of your package manager.

Build from Source

Check BUILD.md for details.