Weekend Hacks #2: Understanding HIDs


Mouses, keyboards, game controllers are every day devices we expect to work with our systems seamlessly. We don’t even have patience to download and install their drivers. We want them to work magically. Magical is convenient. But what is the main ingredient of magic?

I have a Ps4 lying around for the past couple of weeks and honestly I am not excited about it at all. The controller, DualShock 4, is another story. I must give kudos to Sony for equipping this controller with various tricks and believing in developer’s creativity. As the platform owner, they have made the controller as interactive as they can and challenged developers to utilize these features in most creative ways. My weekend goal is to get to know Dualshock 4, particularly it’s touch-pad, without using a Playstation SDK (Because I don’t have one).

I've started my search with Tested’s article on using Ps4 as a PC controller. DS4Tool is an impressive tool that hides your DS4 behind a ,Windows-supported, Xbox 360 controller driver and as a result make it possible for you to use DS4 with PC games. Unfortunately I wasn't able to find any source code so I ventured forth to a similar project; PS4PC.

PS4PC is a lightweight executable that maps PS4 touch-pad to your mouse. After playing with it for a while, now that I knew it is actually possible to use touch-pad on a PC, I was excited to learn more about USB devices.

Couple of hours of quick-and-dirty Google searches and going over partially helpful sources, wikis and code pieces, I knew good enough to say a couple of lines about USB devices and HIDs. The interaction between a USB device and a PC depends on simply polling. PC frequently “reads” the device and as a result the device returns a report, represented as a byte array, which holds information about device state. Of course it is a two-way street, PC can create a report and “write” to device to trigger features such as haptic feedback.

OK, now that we know how communication works, how do we specify content? That’s where our best friend HID comes in. HID is simply a contract between device producer and it’s future owners. It is, in a way, a map that explains meaning of each byte in the report. 6th byte may be the state of your left mouse button; either clicked or not. Of course, HIDs are pretty short and predictable for simpler USB devices such as regular mouses, but they may become beasts with devices such as DualShock 4. Realizing Sony has not released a public HID, my hopes for touch-pad integration were failing as I found this project. Apparently, some geniuses decided to discover HID on their own, great!

The rest was pretty simple; pick up an HID library project for C#, Java or your favorite language, find vendor and product ids for your device through Control Panel, write a simple program that reads your device in short time intervals and get the information you want by mapping bytes into booleans, integers, etc. using HID. One interesting characteristic with USB interaction is the concept of continuous information flow. Working mostly on mobile platforms nowadays, I tend to think event based interaction, someone tapping on a button, swiping, etc. However with USB devices there is always information coming in and it’s your job as the developer to decide what constitutes an event and react to it.

That’s about it; 5-6 hours, 2 pots of coffee, couple of mutterings and frustrations here there resulted in a DualShock 4 talking to a PC. If you are looking for an interesting weekend, pick your favorite USB device and try to see what lies under the driver, you may be suprised.

Email me when silly experiments publishes stories