• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.builds/H27-Sep-2021-9894

.github/workflows/H27-Sep-2021-161141

android/jni/H27-Sep-2021-2012

dist/H27-Sep-2021-3220

documentation/H03-May-2022-

doxygen/H27-Sep-2021-2,4831,920

hidapi/H27-Sep-2021-49966

hidtest/H03-May-2022-242163

libusb/H03-May-2022-1,9361,342

linux/H03-May-2022-1,204785

m4/H27-Sep-2021-474422

mac/H03-May-2022-1,329912

pc/H27-Sep-2021-3831

src/cmake/H03-May-2022-5342

testgui/H27-Sep-2021-1,262965

udev/H27-Sep-2021-3729

windows/H03-May-2022-2,1531,762

.appveyor.ymlH A D27-Sep-20211.1 KiB3429

.cirrus.ymlH A D27-Sep-20211.1 KiB3431

.gitattributesH A D27-Sep-2021104 85

.gitignoreH A D27-Sep-2021318 2924

BUILD.autotools.mdH A D27-Sep-20213.5 KiB11583

BUILD.cmake.mdH A D27-Sep-202112.7 KiB228171

BUILD.mdH A D27-Sep-20214 KiB11179

Makefile.amH A D27-Sep-20211.1 KiB8668

README.mdH A D27-Sep-20216.2 KiB181130

VERSIONH A D27-Sep-20217 21

bootstrapH A D27-Sep-202152 31

configure.acH A D03-May-20227.4 KiB253224

README.md

1## HIDAPI library for Windows, Linux, FreeBSD and macOS
2
3| CI instance          | Status |
4|----------------------|--------|
5| `Linux/macOS/Windows master` | [![GitHub Builds](https://github.com/libusb/hidapi/workflows/GitHub%20Builds/badge.svg?branch=master)](https://github.com/libusb/hidapi/actions/workflows/builds.yml?query=branch%3Amaster) |
6| `Windows master` | [![Build status](https://ci.appveyor.com/api/projects/status/xfmr5fo8w0re8ded/branch/master?svg=true)](https://ci.appveyor.com/project/libusb/hidapi/branch/master) |
7| `Linux/BSD, last build (branch/PR)` | [![builds.sr.ht status](https://builds.sr.ht/~z3ntu/hidapi.svg)](https://builds.sr.ht/~z3ntu/hidapi) |
8
9HIDAPI is a multi-platform library which allows an application to interface
10with USB and Bluetooth HID-Class devices on Windows, Linux, FreeBSD, and macOS.
11HIDAPI can be either built as a shared library (`.so`, `.dll` or `.dylib`) or
12can be embedded directly into a target application by adding a single source
13file (per platform) and a single header.
14
15HIDAPI library was originally developed by Alan Ott ([signal11](https://github.com/signal11)).
16
17It was moved to [libusb/hidapi](https://github.com/libusb/hidapi) on June 4th, 2019, in order to merge important bugfixes and continue development of the library.
18
19## Table of Contents
20
21* [About](#about)
22    * [Test GUI](#test-gui)
23* [What Does the API Look Like?](#what-does-the-api-look-like)
24* [License](#license)
25* [Installing HIDAPI](#installing-hidapi)
26* [Build from Source](#build-from-source)
27
28
29## About
30
31HIDAPI has four back-ends:
32* Windows (using `hid.dll`)
33* Linux/hidraw (using the Kernel's hidraw driver)
34* libusb (using libusb-1.0 - Linux/BSD/other UNIX-like systems)
35* macOS (using IOHidManager)
36
37On Linux, either the hidraw or the libusb back-end can be used. There are
38tradeoffs, and the functionality supported is slightly different. Both are
39built by default. It is up to the application linking to hidapi to choose
40the backend at link time by linking to either `libhidapi-libusb` or
41`libhidapi-hidraw`.
42
43Note that you will need to install an udev rule file with your application
44for unprivileged users to be able to access HID devices with hidapi. Refer
45to the [69-hid.rules](udev/69-hid.rules) file in the `udev` directory
46for an example.
47
48__Linux/hidraw__ (`linux/hid.c`):
49
50This back-end uses the hidraw interface in the Linux kernel, and supports
51both USB and Bluetooth HID devices. It requires kernel version at least 2.6.39
52to build. In addition, it will only communicate with devices which have hidraw
53nodes associated with them.
54Keyboards, mice, and some other devices which are blacklisted from having
55hidraw nodes will not work. Fortunately, for nearly all the uses of hidraw,
56this is not a problem.
57
58__Linux/FreeBSD/libusb__ (`libusb/hid.c`):
59
60This back-end uses libusb-1.0 to communicate directly to a USB device. This
61back-end will of course not work with Bluetooth devices.
62
63### Test GUI
64
65HIDAPI also comes with a Test GUI. The Test GUI is cross-platform and uses
66Fox Toolkit <http://www.fox-toolkit.org>.  It will build on every platform
67which HIDAPI supports.  Since it relies on a 3rd party library, building it
68is optional but recommended because it is so useful when debugging hardware.
69
70NOTE: Test GUI based on Fox Toolkit is not actively developed nor supported
71by HIDAPI team. It is kept as a historical artifact. It may even work sometime
72or on some platforms, but it is not going to get any new features or bugfixes.
73
74Instructions for installing Fox-Toolkit on each platform is not provided.
75Make sure to use Fox-Toolkit v1.6 if you choose to use it.
76
77## What Does the API Look Like?
78
79The API provides the most commonly used HID functions including sending
80and receiving of input, output, and feature reports. The sample program,
81which communicates with a heavily hacked up version of the Microchip USB
82Generic HID sample looks like this (with error checking removed for
83simplicity):
84
85**Warning: Only run the code you understand, and only when it conforms to the
86device spec. Writing data (`hid_write`) at random to your HID devices can break them.**
87
88```c
89#include <stdio.h> // printf
90#include <wchar.h> // wprintf
91
92#include <hidapi.h>
93
94#define MAX_STR 255
95
96int main(int argc, char* argv[])
97{
98	int res;
99	unsigned char buf[65];
100	wchar_t wstr[MAX_STR];
101	hid_device *handle;
102	int i;
103
104	// Initialize the hidapi library
105	res = hid_init();
106
107	// Open the device using the VID, PID,
108	// and optionally the Serial number.
109	handle = hid_open(0x4d8, 0x3f, NULL);
110
111	// Read the Manufacturer String
112	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
113	wprintf(L"Manufacturer String: %s\n", wstr);
114
115	// Read the Product String
116	res = hid_get_product_string(handle, wstr, MAX_STR);
117	wprintf(L"Product String: %s\n", wstr);
118
119	// Read the Serial Number String
120	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
121	wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);
122
123	// Read Indexed String 1
124	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
125	wprintf(L"Indexed String 1: %s\n", wstr);
126
127	// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
128	buf[0] = 0x0;
129	buf[1] = 0x80;
130	res = hid_write(handle, buf, 65);
131
132	// Request state (cmd 0x81). The first byte is the report number (0x0).
133	buf[0] = 0x0;
134	buf[1] = 0x81;
135	res = hid_write(handle, buf, 65);
136
137	// Read requested state
138	res = hid_read(handle, buf, 65);
139
140	// Print out the returned buffer.
141	for (i = 0; i < 4; i++)
142		printf("buf[%d]: %d\n", i, buf[i]);
143
144	// Close the device
145	hid_close(handle);
146
147	// Finalize the hidapi library
148	res = hid_exit();
149
150	return 0;
151}
152```
153
154You can also use [hidtest/test.c](hidtest/test.c)
155as a starting point for your applications.
156
157
158## License
159
160HIDAPI may be used by one of three licenses as outlined in [LICENSE.txt](LICENSE.txt).
161
162## Installing HIDAPI
163
164If you want to build your own application that uses HID devices with HIDAPI,
165you need to get HIDAPI development package.
166
167Depending on what your development environment is, HIDAPI likely to be provided
168by your package manager.
169
170For instance on Ubuntu, HIDAPI is available via APT:
171```sh
172sudo apt install libhidapi-dev
173```
174
175HIDAPI package name for other systems/package managers may differ.
176Check the documentation/package list of your package manager.
177
178## Build from Source
179
180Check [BUILD.md](BUILD.md) for details.
181