Merge lp:~mixxxdevelopers/mixxx/features_controllerAbstraction into lp:~mixxxdevelopers/mixxx/trunk

Proposed by Sean M. Pappalardo
Status: Merged
Approved by: Sean M. Pappalardo
Approved revision: 2856
Merged at revision: 3047
Proposed branch: lp:~mixxxdevelopers/mixxx/features_controllerAbstraction
Merge into: lp:~mixxxdevelopers/mixxx/trunk
Diff against target: 24123 lines (+13054/-8821)
171 files modified
mixxx/SConstruct (+1/-1)
mixxx/build/depends.py (+26/-23)
mixxx/build/features.py (+47/-33)
mixxx/lib/hidapi-0.7.0/LICENSE-orig.txt (+9/-0)
mixxx/lib/hidapi-0.7.0/LICENSE.txt (+13/-0)
mixxx/lib/hidapi-0.7.0/hidapi/hidapi.h (+383/-0)
mixxx/lib/hidapi-0.7.0/linux/.gitignore (+13/-0)
mixxx/lib/hidapi-0.7.0/linux/Makefile (+36/-0)
mixxx/lib/hidapi-0.7.0/linux/README.txt (+63/-0)
mixxx/lib/hidapi-0.7.0/linux/hid-libusb.c (+1386/-0)
mixxx/lib/hidapi-0.7.0/linux/hid.c (+595/-0)
mixxx/lib/hidapi-0.7.0/mac/.gitignore (+13/-0)
mixxx/lib/hidapi-0.7.0/mac/Makefile (+32/-0)
mixxx/lib/hidapi-0.7.0/mac/hid.c (+1122/-0)
mixxx/lib/hidapi-0.7.0/windows/.gitignore (+11/-0)
mixxx/lib/hidapi-0.7.0/windows/Makefile (+14/-0)
mixxx/lib/hidapi-0.7.0/windows/Makefile.mingw (+32/-0)
mixxx/lib/hidapi-0.7.0/windows/ddk_build/.gitignore (+2/-0)
mixxx/lib/hidapi-0.7.0/windows/ddk_build/hidapi.def (+17/-0)
mixxx/lib/hidapi-0.7.0/windows/ddk_build/makefile (+49/-0)
mixxx/lib/hidapi-0.7.0/windows/ddk_build/sources (+23/-0)
mixxx/lib/hidapi-0.7.0/windows/hid.c (+873/-0)
mixxx/lib/hidapi-0.7.0/windows/hidapi.sln (+29/-0)
mixxx/lib/hidapi-0.7.0/windows/hidapi.vcproj (+201/-0)
mixxx/lib/hidapi-0.7.0/windows/hidtest.vcproj (+196/-0)
mixxx/res/controllers/American Audio VMS4.midi.xml (+9/-2)
mixxx/res/controllers/American-Audio-VMS4-scripts.js (+1/-1)
mixxx/res/controllers/EKS Otus.js (+95/-0)
mixxx/res/controllers/Eks Otus.cntrlr.xml (+13/-0)
mixxx/res/controllers/Stanton-SCS1d-scripts.js (+87/-104)
mixxx/res/controllers/Stanton-SCS3d-scripts.js (+23/-26)
mixxx/res/controllers/common-controller-scripts.js (+47/-14)
mixxx/res/mixxx.qrc (+1/-0)
mixxx/src/SConscript (+230/-234)
mixxx/src/controlbeat.cpp (+2/-1)
mixxx/src/controlbeat.h (+1/-2)
mixxx/src/controlgroupdelegate.cpp (+2/-1)
mixxx/src/controllers/controller.cpp (+144/-0)
mixxx/src/controllers/controller.h (+160/-0)
mixxx/src/controllers/controllerengine.cpp (+1157/-0)
mixxx/src/controllers/controllerengine.h (+127/-0)
mixxx/src/controllers/controllerenumerator.cpp (+18/-0)
mixxx/src/controllers/controllerenumerator.h (+33/-0)
mixxx/src/controllers/controllermanager.cpp (+359/-0)
mixxx/src/controllers/controllermanager.h (+75/-0)
mixxx/src/controllers/controllerpreset.h (+40/-0)
mixxx/src/controllers/controllerpresetfilehandler.cpp (+121/-0)
mixxx/src/controllers/controllerpresetfilehandler.h (+63/-0)
mixxx/src/controllers/controllerpresetvisitor.h (+14/-0)
mixxx/src/controllers/defs_controllers.h (+13/-0)
mixxx/src/controllers/dlgcontrollerlearning.cpp (+209/-0)
mixxx/src/controllers/dlgcontrollerlearning.h (+47/-0)
mixxx/src/controllers/dlgcontrollerlearning.ui (+242/-0)
mixxx/src/controllers/dlgprefcontroller.cpp (+139/-0)
mixxx/src/controllers/dlgprefcontroller.h (+70/-0)
mixxx/src/controllers/dlgprefcontrollerdlg.ui (+127/-0)
mixxx/src/controllers/dlgprefmappablecontroller.cpp (+110/-0)
mixxx/src/controllers/dlgprefmappablecontroller.h (+45/-0)
mixxx/src/controllers/dlgprefmappablecontrollerdlg.ui (+247/-0)
mixxx/src/controllers/dlgprefnocontrollers.cpp (+11/-0)
mixxx/src/controllers/dlgprefnocontrollers.h (+23/-0)
mixxx/src/controllers/dlgprefnocontrollersdlg.ui (+46/-0)
mixxx/src/controllers/hid/hidcontroller.cpp (+242/-0)
mixxx/src/controllers/hid/hidcontroller.h (+92/-0)
mixxx/src/controllers/hid/hidcontrollerpreset.h (+23/-0)
mixxx/src/controllers/hid/hidcontrollerpresetfilehandler.cpp (+16/-0)
mixxx/src/controllers/hid/hidcontrollerpresetfilehandler.h (+21/-0)
mixxx/src/controllers/hid/hidenumerator.cpp (+47/-0)
mixxx/src/controllers/hid/hidenumerator.h (+24/-0)
mixxx/src/controllers/midi/hss1394controller.cpp (+193/-0)
mixxx/src/controllers/midi/hss1394controller.h (+69/-0)
mixxx/src/controllers/midi/hss1394enumerator.cpp (+51/-0)
mixxx/src/controllers/midi/hss1394enumerator.h (+25/-0)
mixxx/src/controllers/midi/midicontroller.cpp (+500/-0)
mixxx/src/controllers/midi/midicontroller.h (+82/-0)
mixxx/src/controllers/midi/midicontrollerpreset.h (+42/-0)
mixxx/src/controllers/midi/midicontrollerpresetfilehandler.cpp (+465/-0)
mixxx/src/controllers/midi/midicontrollerpresetfilehandler.h (+37/-0)
mixxx/src/controllers/midi/midienumerator.cpp (+15/-0)
mixxx/src/controllers/midi/midienumerator.h (+26/-0)
mixxx/src/controllers/midi/midimessage.h (+89/-0)
mixxx/src/controllers/midi/midioutputhandler.cpp (+72/-0)
mixxx/src/controllers/midi/midioutputhandler.h (+46/-0)
mixxx/src/controllers/midi/portmidicontroller.cpp (+221/-0)
mixxx/src/controllers/midi/portmidicontroller.h (+66/-0)
mixxx/src/controllers/midi/portmidienumerator.cpp (+125/-0)
mixxx/src/controllers/midi/portmidienumerator.h (+25/-0)
mixxx/src/controllers/mixxxcontrol.cpp (+17/-171)
mixxx/src/controllers/mixxxcontrol.h (+38/-67)
mixxx/src/controllers/pitchfilter.h (+7/-16)
mixxx/src/controllers/qtscript-bytearray/bytearrayclass.cpp (+284/-0)
mixxx/src/controllers/qtscript-bytearray/bytearrayclass.h (+91/-0)
mixxx/src/controllers/qtscript-bytearray/bytearrayprototype.cpp (+129/-0)
mixxx/src/controllers/qtscript-bytearray/bytearrayprototype.h (+76/-0)
mixxx/src/controllers/softtakeover.cpp (+57/-63)
mixxx/src/controllers/softtakeover.h (+23/-35)
mixxx/src/controllogpotmeter.cpp (+2/-1)
mixxx/src/controllogpotmeter.h (+1/-1)
mixxx/src/controlobject.cpp (+6/-8)
mixxx/src/controlobject.h (+4/-4)
mixxx/src/controlobjectthread.cpp (+2/-1)
mixxx/src/controlpotmeter.cpp (+2/-1)
mixxx/src/controlpotmeter.h (+1/-1)
mixxx/src/controlpushbutton.cpp (+4/-4)
mixxx/src/controlpushbutton.h (+2/-1)
mixxx/src/controlttrotary.cpp (+2/-1)
mixxx/src/controlttrotary.h (+2/-2)
mixxx/src/controlvaluedelegate.cpp (+1/-1)
mixxx/src/dlgmidilearning.cpp (+0/-210)
mixxx/src/dlgmidilearning.h (+0/-56)
mixxx/src/dlgmidilearning.ui (+0/-241)
mixxx/src/dlgpreferences.cpp (+92/-105)
mixxx/src/dlgpreferences.h (+16/-21)
mixxx/src/dlgprefmidibindings.cpp (+0/-508)
mixxx/src/dlgprefmidibindings.h (+0/-92)
mixxx/src/dlgprefmidibindingsdlg.ui (+0/-324)
mixxx/src/dlgprefnomidi.cpp (+0/-25)
mixxx/src/dlgprefnomidi.h (+0/-33)
mixxx/src/dlgprefnomididlg.ui (+0/-46)
mixxx/src/engine/ratecontrol.cpp (+2/-0)
mixxx/src/library/schemamanager.cpp (+2/-2)
mixxx/src/main.cpp (+2/-2)
mixxx/src/midi/hss1394enumerator.cpp (+0/-66)
mixxx/src/midi/hss1394enumerator.h (+0/-38)
mixxx/src/midi/midichanneldelegate.cpp (+0/-77)
mixxx/src/midi/midichanneldelegate.h (+0/-36)
mixxx/src/midi/mididevice.cpp (+0/-373)
mixxx/src/midi/mididevice.h (+0/-115)
mixxx/src/midi/mididevicedummy.h (+0/-42)
mixxx/src/midi/midideviceenumerator.cpp (+0/-28)
mixxx/src/midi/midideviceenumerator.h (+0/-38)
mixxx/src/midi/mididevicehss1394.cpp (+0/-233)
mixxx/src/midi/mididevicehss1394.h (+0/-66)
mixxx/src/midi/mididevicemanager.cpp (+0/-217)
mixxx/src/midi/mididevicemanager.h (+0/-54)
mixxx/src/midi/midideviceportmidi.cpp (+0/-325)
mixxx/src/midi/midideviceportmidi.h (+0/-60)
mixxx/src/midi/midiinputmapping.h (+0/-10)
mixxx/src/midi/midiinputmappingtablemodel.cpp (+0/-229)
mixxx/src/midi/midiinputmappingtablemodel.h (+0/-42)
mixxx/src/midi/midiledhandler.cpp (+0/-119)
mixxx/src/midi/midiledhandler.h (+0/-44)
mixxx/src/midi/midimapping.cpp (+0/-1178)
mixxx/src/midi/midimapping.h (+0/-175)
mixxx/src/midi/midimessage.cpp (+0/-111)
mixxx/src/midi/midimessage.h (+0/-114)
mixxx/src/midi/midinodelegate.cpp (+0/-75)
mixxx/src/midi/midinodelegate.h (+0/-36)
mixxx/src/midi/midioptiondelegate.cpp (+0/-158)
mixxx/src/midi/midioptiondelegate.h (+0/-39)
mixxx/src/midi/midioutputmapping.h (+0/-10)
mixxx/src/midi/midioutputmappingtablemodel.cpp (+0/-263)
mixxx/src/midi/midioutputmappingtablemodel.h (+0/-44)
mixxx/src/midi/midiscriptengine.cpp (+0/-1342)
mixxx/src/midi/midiscriptengine.h (+0/-145)
mixxx/src/midi/midistatusdelegate.cpp (+0/-121)
mixxx/src/midi/midistatusdelegate.h (+0/-39)
mixxx/src/midi/portmidienumerator.cpp (+0/-135)
mixxx/src/midi/portmidienumerator.h (+0/-37)
mixxx/src/mixxx.cpp (+26/-23)
mixxx/src/mixxx.h (+2/-3)
mixxx/src/mixxx.rc (+2/-2)
mixxx/src/mixxxkeyboard.cpp (+2/-2)
mixxx/src/recording/recordingmanager.cpp (+1/-0)
mixxx/src/skin/propertybinder.cpp (+2/-0)
mixxx/src/test/controllerengine_test.cpp (+104/-25)
mixxx/src/upgrade.cpp (+74/-25)
mixxx/src/widget/wwidget.cpp (+0/-20)
mixxx/src/widget/wwidget.h (+0/-2)
mixxx/src/xmlparse.cpp (+23/-0)
mixxx/src/xmlparse.h (+2/-0)
To merge this branch: bzr merge lp:~mixxxdevelopers/mixxx/features_controllerAbstraction
Reviewer Review Type Date Requested Status
RJ Skerry-Ryan Approve
Review via email: mp+101177@code.launchpad.net

This proposal supersedes a proposal from 2012-03-19.

Description of the change

Abstracts the controller subsystem to support multiple protocols. MIDI subsystem completely rewritten; directories, classes and threads reorganized. Adds full HID controller support. Removes GUI table for now since it was horribly broken.

To post a comment you must log in.
Revision history for this message
William Good (bkgood) wrote : Posted in a previous version of this proposal

Maybe I missed the discussion on this but while I agree that the GUI mapping support totally sucks do we really want to remove it without replacing it?

Revision history for this message
RJ Skerry-Ryan (rryan) wrote : Posted in a previous version of this proposal

For 1.11 I think we're just going to remove it. The general reasoning is
that since we haven't really had anyone complaining about how hard it is to
use like about 2 years of it being there.. the only conclusion must be that
close to nobody uses it.

Code-wise it was very complicated too and really screwed up the MIDI
mapping implementation since they were deeply intertwined. I'd rather nuke
it in 1.11 and come up with a really nice one for 1.12 or create an
external mapping program.

On Sun, Mar 18, 2012 at 9:26 PM, Bill Good <email address hidden> wrote:

> Maybe I missed the discussion on this but while I agree that the GUI
> mapping support totally sucks do we really want to remove it without
> replacing it?
> --
>
> https://code.launchpad.net/~mixxxdevelopers/mixxx/features_controllerAbstraction/+merge/98121
> You proposed lp:~mixxxdevelopers/mixxx/features_controllerAbstraction for
> merging.
>

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote : Posted in a previous version of this proposal

We could add back the MIDI mapping wizard if you like, since there's already a message signal and a learning flag in place in MidiController. MC just needs a slot to get data from the wizard, then the wizard would need to be adjusted to work with these signals/slots. I'll check into this further.

Revision history for this message
RJ Skerry-Ryan (rryan) wrote : Posted in a previous version of this proposal
Download full text (3.9 KiB)

Hey Sean,

I realize you haven't put the finishing touches on the branch yet. I'm sorry in advance because this review is going to seem a little brutal but I'm convinced that we need to do things right. The main problem I saw with the MIDI subsystem in 1.10 is that everything was totally commingled. This made it impossible to write unit tests for a given class because we had swiss-army-knife classes that did everything. We need to move away from this.

I want to get you this feedback as soon as possible, so here it is. I may have more comments to share later.

* Please make sure to remove commented code referring to non-existing stuff (e.g. all commented lines you left in build/depends.py that refer to old files that don't exist anymore)

* Make sure to delete "res/controllers/Stanton SCS.1m VUMETERS ONLY.xml"

* Please delete src/controllers/OLDmidi soon because it makes reviewing your branch hard (the diff is gigantic).

* In defs_controllers.h, make helper functions instead of these macros. (DEFAULT_DEVICE_PRESET, DEFAULT_MIDI_DEVICE_PRESET in particular. Ideally PRESETS_PATH and LPRESETS_PATH as well).

* Please add "virtual" to every destructor declaration.

* It's good that the old MidiMapping class died because it was a mutable-shared-state-logic-monster. That said, storing the mappings in the controller is almost as bad.
   - Let's pull all mappings out into a Mapping/MidiMapping class. This class should be plain-old-data, not a QObject (e.g. no signals/slots) and no methods except the minimum needed for Controller to look up the script files, MidiController to look up a mapped control in receive() and to look up output mappings where it creates the MidiOutputHandlers.

* Take all serialization/deserialization code and put it into a MappingParser/MidiMappingParser helper class which creates a Mapping/MidiMapping class from a given path.

* Delete loadPreset / savePreset / buildDomElement methods in Controller/MidiController .. replace them with a setMapping() command. A controller that doesn't have a mapping yet can be considered invalid.

* Controllers shouldn't be concerned with saving a preset. Instead that should be the controller preferences dialog or the ControllerManager's job to deserialize the mapping and call Controller::setMapping().

* Need to remove all methods that deal with a char*/length data buffer e.g. Controller::send, Controller::receivePointer. Instead we should be using QByteArray for everything.

* Remove length argument from Controller::sendBa since a QByteArray knows its own length. Also, rename sendBa to send(). Also make the argument a const QByteArray.

* I'd prefer we get rid of the Controller::send(QList<int>) method and instead require people to use QByteArrays but I'm not sure if this is easy from scripts so maybe we can let it be. Please change the implementation of Controller::send(QList<int>, int) to build a QByteArray and call send(QByteArray).

* In ControllerManager, please remove the m_pPMEnumerator, m_pHSSEnumerator, m_pHIDEnumerator, and m_pOSCEnumerator declarations. Instead use a QList of ControllerEnumerator* objects. Add an "addControllerEnumerator()" method to ControllerManager, an...

Read more...

review: Needs Fixing
Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote : Posted in a previous version of this proposal

> * Please make sure to remove commented code referring to non-existing stuff
> * Make sure to delete "res/controllers/Stanton SCS.1m VUMETERS ONLY.xml"
> * Please delete src/controllers/OLDmidi soon because it makes reviewing your

These are among the reasons why I didn't propose a merge yet, silly. :)

> * In defs_controllers.h, make helper functions instead of these macros.

What benefit would that give? The macros are straightforward and fast.

> * Please add "virtual" to every destructor declaration.

As a matter of course? What will that do? I only do it to destructors that will be reimplemented.

> shared-state-logic-monster. That said, storing the mappings in the controller
> is almost as bad.

I do this because I view a Controller as a device, with all properties particular to that device (e.g. a mapping) contained in the one object. Then due to the time-sensitive nature of MIDI processing, I imagine local object data is faster to access.

> * Take all serialization/deserialization code and put it into a
> MappingParser/MidiMappingParser helper class which creates a
> Mapping/MidiMapping class from a given path.

That sounds like overkill. If we do in fact make a MidiMapping class, shouldn't the same class that holds the data be responsible for all operations on that data?

> * Need to remove all methods that deal with a char*/length data buffer e.g.
> Controller::send, Controller::receivePointer. Instead we should be using
> QByteArray for everything.

Not all of them can be removed without duplicating code or complicating the classes, since the device APIs send and expect data this way. If we try, know that interfacing raw bytes between JS and C++ is a very delicate issue. I've had numerous problems with bytes getting mangled in the past.
Plus I imagine primitive data types to be slightly faster. Here again, what would be the benefit of switching to QByteArrays?

> * I'd prefer we get rid of the Controller::send(QList<int>) method and instead
> require people to use QByteArrays

We can't. This is how QtScript passes data byte arrays to C++.

> * In ControllerManager, please remove the m_pPMEnumerator, m_pHSSEnumerator,
> m_pHIDEnumerator, and m_pOSCEnumerator declarations.

Thanks for the suggested methods. This was bothering me too but I couldn't think of a neat way to fix it.

> * midi-mappings-scripts.js You're changing the names of a lot of common script
> stuff. Won't this break MIDI-scripts in the wild?

Yes, but the only things that will be incompatible are MidiButtonState and MidiLedState. A README telling to search-n-replace will fix them right up. I left backwards-compatible calls for everything else.

Revision history for this message
Owen Williams (ywwg) wrote : Posted in a previous version of this proposal

We should think long and hard before breaking people's scripts, even if a simple search-and-replace will fix it. I'd prefer to leave scripts stable and only limit breakage to major releases (2.0, etc). I'm not super-familiar with this branch, but is this breakage necessary?

Revision history for this message
RJ Skerry-Ryan (rryan) wrote : Posted in a previous version of this proposal
Download full text (5.5 KiB)

On Mon, Mar 19, 2012 at 4:14 PM, Sean M. Pappalardo
<email address hidden>wrote:

> > * Please make sure to remove commented code referring to non-existing
> stuff
> > * Make sure to delete "res/controllers/Stanton SCS.1m VUMETERS ONLY.xml"
> > * Please delete src/controllers/OLDmidi soon because it makes reviewing
> your
>
> These are among the reasons why I didn't propose a merge yet, silly. :)
>
> > * In defs_controllers.h, make helper functions instead of these macros.
>
> What benefit would that give? The macros are straightforward and fast.
>

Macros should only be used for constants. They're no faster than an inline
function. These particular macros refer to member variables of a class and
have logic in them. This obscures the logic within the class they are used
in because it is hidden behind what looks like a constant. If they have
member variables then they're specific to a class so why not make them a
private inline helper function of that class?

>
> > * Please add "virtual" to every destructor declaration.
>
> As a matter of course? What will that do? I only do it to destructors that
> will be reimplemented.
>
>
It's just something that every class should have. It should be as automatic
as making your member variables private. Some day in the future someone
will subclass your class -- guaranteed. Be nice to them -- it might even be
your future self. :)

> > shared-state-logic-monster. That said, storing the mappings in the
> controller
> > is almost as bad.
>
> I do this because I view a Controller as a device, with all properties
> particular to that device (e.g. a mapping) contained in the one object.
> Then due to the time-sensitive nature of MIDI processing, I imagine local
> object data is faster to access.
>
>
It's fine for the controller to have a mapping. That mapping should be
encapsulated into one class so you can pass them around and vary the
implementation of the mapping independent of the class that consumes the
mapping (the Controller).

Accessing a member variable versus the member variable of another class is
possibly the difference in 1 or two assembly instructions -- if any
difference. If you care about this in the name of speed then I'm surprised
you use so many virtual functions (they are about 2x slower than a regular
function call). This sort of difference is totally meaningless from a Mixxx
performance perspective. We're talking picoseconds here!

Don't let tens or hundreds of assembly instructions get in the way of
writing good code. For the long-term health of the project it's worth it to
structure the project in small, bite-sized and testable classes. This is
extra work, I know.

> > * Take all serialization/deserialization code and put it into a
> > MappingParser/MidiMappingParser helper class which creates a
> > Mapping/MidiMapping class from a given path.
>
> That sounds like overkill. If we do in fact make a MidiMapping class,
> shouldn't the same class that holds the data be responsible for all
> operations on that data?
>

The mapping parsing / serializing code is long enough to warrant living in
its own utility class. It is long and obscures the purpose of whatever
class it lives in (origin...

Read more...

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote : Posted in a previous version of this proposal

> Macros should only be used for constants.

My bad, I forgot there were member variables in two of them. Don't know what I was thinking when I did that.

> difference. If you care about this in the name of speed then I'm surprised
> you use so many virtual functions (they are about 2x slower than a regular
> function call).

:O I had no idea! Is that 2x slowdown a matter of picoseconds too?

> The mapping parsing / serializing code is long enough to warrant living in
> its own utility class. It is long and obscures the purpose of whatever
> class it lives in (originally MidiMapping, now *Controller).

What bothers me about separating it is that there is no generic ControllerMapping class (at least not yet,) so MidiMappings are specific to MIDI controllers, so why not make them an integral part? I see your point though and will make the change.

> 1.10. Can we keep the ButtonState and LedState as they are for MIDI and
> mark them deprecated?

Actually, leaving them without the "Midi" prefix should be fine, since non-MIDI controllers will need to set the values differently anyway.

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote : Posted in a previous version of this proposal

LOL, I removed the OLDmidi directory and the diff got bigger. (Not surprised.)

RJ, while I work on the rest, I need you to take a look at ControllerEngine::connectControl() and figure out why using COTs breaks automatic reactions. They work fine in trunk so I have no idea.

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote : Posted in a previous version of this proposal

Hello again. Looking at your class-separation requests more closely now, I still have reservations:

> - Let's pull all mappings out into a Mapping/MidiMapping class. This class
> should be plain-old-data, not a QObject (e.g. no signals/slots) and no methods

I'm sorry, I can't see the benefit of doing that. It would just add complexity and make it harder for *Controller to be expanded in the future such as when the GUI signals it to add/delete/change mappings. (Besides, a preset is just two QLists. Specific *Controllers add two QHashes for mappings.)

> * Take all serialization/deserialization code and put it into a
> MappingParser/MidiMappingParser helper class

Here too, moving those functions to another class would add needless complexity because they have to access data that's specific to a Controller. I understand the desire to have classes not be too long, but don't let that get in the way of logical design. In this case, everything that's in *Controller is specific to that type of device. (Yes, that includes the parsing code, since it's specific to the mapping storage format, which is specific to the device type.) Trying to separate it across multiple classes would cause us to have shared-state logic monsters again and make it much more difficult to follow for those new to the code. Since it sounds like you're interested in visual separation, I have moved the functions that handle XML into separate .cpp files and have simply #included these in the main *Controller ones.

> * Controllers shouldn't be concerned with saving a preset. Instead that should
> be the controller preferences dialog or the ControllerManager's job

The ControllerManager _is_ in charge of saving presets. The Controllers just contain the code to do so, because it's specific to each Controller type (as stated above,) and the Manager isn't concerned with those details (or it would be micro-managing.)

In short, there's nothing else that can be factored out.

If your concerns are motivated by unit-testing issues, why can't we just write separate tests for different aspects of a class?

Revision history for this message
RJ Skerry-Ryan (rryan) wrote : Posted in a previous version of this proposal
Download full text (3.4 KiB)

On Apr 7, 2012 7:38 AM, "Sean M. Pappalardo" <email address hidden> wrote:
>
> Hello again. Looking at your class-separation requests more closely now,
I still have reservations:
>
> > - Let's pull all mappings out into a Mapping/MidiMapping class. This
class
> > should be plain-old-data, not a QObject (e.g. no signals/slots) and no
methods
>
> I'm sorry, I can't see the benefit of doing that. It would just add
complexity and make it harder for *Controller to be expanded in the future
such as when the GUI signals it to add/delete/change mappings. (Besides, a
preset is just two QLists. Specific *Controllers add two QHashes for
mappings.)

The benefit is encapsulation of the mapping implementation.

Add/delete/change of a mapping from the GUI is the reason the old midi
system is so terrible. The only way to load a mapping going forward will be
to call Controller::loadMapping(ControllerMapping).

>
> > * Take all serialization/deserialization code and put it into a
> > MappingParser/MidiMappingParser helper class
>
> Here too, moving those functions to another class would add needless
complexity because they have to access data that's specific to a
Controller. I understand the desire to have classes not be too long, but
don't let that get in the way of logical design. In this case, everything
that's in *Controller is specific to that type of device. (Yes, that
includes the parsing code, since it's specific to the mapping storage
format, which is specific to the device type.) Trying to separate it across
multiple classes would cause us to have shared-state logic monsters again
and make it much more difficult to follow for those new to the code. Since
it sounds like you're interested in visual separation, I have moved the
functions that handle XML into separate .cpp files and have simply
#included these in the main *Controller ones.

Putting this in a separate helper will allow us to test it and it will
simplify the ControllerMapping class considerably.

>
> > * Controllers shouldn't be concerned with saving a preset. Instead that
should
> > be the controller preferences dialog or the ControllerManager's job
>
> The ControllerManager _is_ in charge of saving presets. The Controllers
just contain the code to do so, because it's specific to each Controller
type (as stated above,) and the Manager isn't concerned with those details
(or it would be micro-managi

I think saving presets should be owned by the controller preferences dialog
and not even be part of the controller manager actually.

>
> In short, there's nothing else that can be factored out.
>
> If your concerns are motivated by unit-testing issues, why can't we just
write separate tests for different aspects of a class?

Well the general reason why this is is that you can't isolate the logic you
aim to test when it's all mixed together in one class.

I feel bad coming down hard but these issues are non-negotiable. In the
past I haven't held the MIDI subsystem to as high a standard as say the
engine but now that you are rewriting it I'd like to change that. "It
works" is not good enough for infrastructure code (which this is going to
be more than ever as we add more controller types) so...

Read more...

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote :

> Add/delete/change of a mapping from the GUI is the reason the old midi
> system is so terrible. The only way to load a mapping going forward will be
> to call Controller::loadMapping(ControllerMapping).

I think we're getting terms confused. Let's define them again: a mapping is a translation of an individual hardware control onto a MixxxControl or script function (e.g. a <control/> block in the XML,) while a preset is a package of mappings and related script functions (e.g. an entire XML file & JS file(s).) That said, we will eventually add back a way for the GUI to add/change/delete individual mappings (but not presets.) The branch today already does what you say: the only way for the GUI to manipulate entire presets is to load them by signaling Controller::loadPreset(XMLFileName).

> I think saving presets should be owned by the controller preferences dialog
> and not even be part of the controller manager actually.

Why? Seems logical to me to have the Manager be in charge of telling its underlings when they should save themselves, and under what file name (so the convention is uniform across all Controller sub-classes.) If you feel strongly about this, it's a simple matter to make Controller::savePreset() a slot so that the dialog can signal it (but then the Controller would need to ask the Manager what file name to use.)

And don't worry about "coming down hard." I'm all about getting the design correct from the start, so it's fine and good to hash this out now. I just need to fully understand the reasons for the changes before I can effectively code them, you know?

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote :

Wow, you sure went to town! Thank you for doing that! Obviously since I was stuck getting it to build and needing the visitor thing, you fixed alot of stuff I would have eventually found. So thanks!

My comments:

- Dude, your editor doesn't like indented blank lines. Please disable it from removing those because it makes diffs longer than they need to be.

- Please get into the habit of using qWarning() for error conditions as we will eventually turn off qDebugs in release builds.

- Does HidController::visit(const MidiControllerPreset* preset) have to exist? That is, does every Controller type have to have those for every other type? That seems clunky, especially as we add more controller types (OSC, etc.)

- What's with your aversion to simple single-line if statements? ;) (I totally agree to parenthesize them when the action is too long for the line though.)

- How _do_ we get the global config object from the rest of Mixxx in the ControllerEngine?

- Nit-pick: I have a sort of convention for comments: if it's commented code, I put the // at the very start of the line (which kwrite does automatically.) If it's a comment to the reader, the // is at the indent level. This way, it's easy so scan for commented code by watching the left side as you scroll. (You changed a few of these.)

- Adding paths to all of the includes makes extra work if we ever rename directories or otherwise move code.

- does CPFH::buildRootWithScripts need to be virtual anymore since you created a different function to add things on?

- MidiController::destroyOutputHandlers(): I used a reverse-for for speed. Your replacement calls QList.size() every iteration. The code isn't really speed-sensitive, so it's not a big deal.

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote :

OK, final review please. I think this is done now, yes?

Revision history for this message
RJ Skerry-Ryan (rryan) wrote :
Download full text (3.5 KiB)

On Tue, Apr 10, 2012 at 3:19 AM, Sean M. Pappalardo
<email address hidden>wrote:

> Wow, you sure went to town! Thank you for doing that! Obviously since I
> was stuck getting it to build and needing the visitor thing, you fixed alot
> of stuff I would have eventually found. So thanks!
>
> My comments:
>
> - Dude, your editor doesn't like indented blank lines. Please disable it
> from removing those because it makes diffs longer than they need to be.
>
>
:) How about instead you set your editor to not put whitespace dangling at
the end of lines.

> - Please get into the habit of using qWarning() for error conditions as we
> will eventually turn off qDebugs in release builds.
>

oops -- noted.

>
> - Does HidController::visit(const MidiControllerPreset* preset) have to
> exist? That is, does every Controller type have to have those for every
> other type? That seems clunky, especially as we add more controller types
> (OSC, etc.)
>
> Yea, it does, but it doesn't need the full type information (can be
forward declared). It's good to be explicit about what happens if a preset
of the wrong type is loaded into a controller of the wrong type so I kind
of like how it's explicitly handled by the type system for us.

> - What's with your aversion to simple single-line if statements? ;) (I
> totally agree to parenthesize them when the action is too long for the line
> though.)
>
>
In my opinion they're very hard to read because you don't get the benefit
of indentation to tell you that "hey there's a control flow construct
here". When I read code I scan it based on the structure and then "zoom in"
for more detail after that so code on the same line as its conditional
slows me down.

> - How _do_ we get the global config object from the rest of Mixxx in the
> ControllerEngine?
>

Pass m_pConfig to ControllerManager when you create it, then pass m_pConfig
to every other thing that you create as you create it. That's how the rest
of Mixxx does it at least.

>
> - Nit-pick: I have a sort of convention for comments: if it's commented
> code, I put the // at the very start of the line (which kwrite does
> automatically.) If it's a comment to the reader, the // is at the indent
> level. This way, it's easy so scan for commented code by watching the left
> side as you scroll. (You changed a few of these.)
>

My convention is english comments have a space after the // and code
doesn't. I think that comments at the start of the line make it harder to
tell what indentation level a given commented line belongs to.

>
> - Adding paths to all of the includes makes extra work if we ever rename
> directories or otherwise move code.
>

We use absolute includes in most places in Mixxx and it prevents ambiguity
(for the reader) if there's a file with the same name in multiple
directories.

>
> - does CPFH::buildRootWithScripts need to be virtual anymore since you
> created a different function to add things on?
>

Nope, don't think so.

>
> - MidiController::destroyOutputHandlers(): I used a reverse-for for speed.
> Your replacement calls QList.size() every iteration. The code isn't really
> speed-sensitive, so it's not a big deal.
>
> Ah ok. QList::size() and QLi...

Read more...

Revision history for this message
Sean M. Pappalardo (pegasus-renegadetech) wrote :

The mapping wizard works fine as tested with a software MIDI controller on Linux.

2833. By Sean M. Pappalardo

Neatened up MIDI message display in learning dialog and prevented controls from affecting Mixxx during learning.

2834. By Sean M. Pappalardo

Made the Clear All button work.

2835. By RJ Skerry-Ryan

Get rid of fputs in upgrade.cpp

2836. By RJ Skerry-Ryan

Mass reformat tabs in SConscript to spaces. Remove all license blocks from src/controllers. Fix lint here and there. Remove unused code from MixxxControl.

2837. By RJ Skerry-Ryan

Rewrite SoftTakeover to use ControlObjects instead of MixxxControl.

2838. By RJ Skerry-Ryan

Move SoftTakeover to src/controllers/. Add more virtual destructors and fix style issues here and there.

2839. By RJ Skerry-Ryan

Remove more licenses. Fix more lint.

2840. By RJ Skerry-Ryan

Move HID code to src/controllers/hid.

2841. By RJ Skerry-Ryan

Move MixxxControl to src/controllers.

2842. By RJ Skerry-Ryan

Get rid of passing ControllerProcessor timerEvent()'s directly in favor of just calling a Controller::poll() method when the controller needs to poll (if it has polling enabled).

2843. By RJ Skerry-Ryan

Delete ControllerProcessor since it was just a wrapper around a QTimer essentially.

2844. By RJ Skerry-Ryan

Prevent changing visibility for a few methods. Mass re-format some class definitions to conform to mixxx indentation style.

2845. By RJ Skerry-Ryan

Comments, formatting.

2846. By RJ Skerry-Ryan

Remove redundant/unused code here and there.

2847. By RJ Skerry-Ryan

Parent DeviceChannelListener to its HSS1394Controller.

2848. By RJ Skerry-Ryan

* Remove polling declaration from ControllerEnumerator and put it in
  the Controllers themselves.

* Make all variables of Controller private and make sub-classes access
  via accessors.

2849. By RJ Skerry-Ryan

Remove dups option from MidiOutputHandler since it wasn't used anywhere.

2850. By RJ Skerry-Ryan

Add locking to protect m_controllers list in ControllerManager. Other misc cleanups.

2851. By RJ Skerry-Ryan

Make DlgPrefController vars private.

2852. By RJ Skerry-Ryan

Make DlgPrefController not call open() or close() on controllers anymore. Switch HidReader to use QAtomicInt instead of bool flag.

2853. By RJ Skerry-Ryan

Oops. Fix a bug I wrote in preset loading.

2854. By RJ Skerry-Ryan

Sean -- please take a look at the comment I left. Not sure what the desired behavior is here.

Revision history for this message
RJ Skerry-Ryan (rryan) wrote :

I have a few things I'd like to fix but they aren't 1.11 launch blocking in my opinion so I think we should get this merged.

Thanks a lot for this branch, Sean. It's a huge step forward for Mixxx's controller system!

review: Approve
Revision history for this message
Albert Santoni (gamegod) wrote :

Nice work Sean! :)

On Tue, Apr 24, 2012 at 5:46 PM, RJ Ryan <email address hidden> wrote:
> Review: Approve
>
> I have a few things I'd like to fix but they aren't 1.11 launch blocking in my opinion so I think we should get this merged.
>
> Thanks a lot for this branch, Sean. It's a huge step forward for Mixxx's controller system!
> --
> https://code.launchpad.net/~mixxxdevelopers/mixxx/features_controllerAbstraction/+merge/101177
> Your team Mixxx Development Team is subscribed to branch lp:mixxx.

--
Albert Santoni
Developer, Mixxx
http://www.mixxx.org
http://www.oscillicious.com

Revision history for this message
Phillip Whelan (pwhelan) wrote :

I'll finish up my work with the MIDI Script Improvements at the latest
for next monday. I'll propose it as a separate merge.

2855. By Sean M. Pappalardo

loadPreset takes a filename, not device name

2856. By RJ Skerry-Ryan

Clear pointer on delete.

2857. By Sean M. Pappalardo

Merging from trunk

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'mixxx/SConstruct'
2--- mixxx/SConstruct 2011-11-11 22:18:56 +0000
3+++ mixxx/SConstruct 2012-04-25 15:44:23 +0000
4@@ -33,11 +33,11 @@
5 toolchain = ARGUMENTS.get('toolchain', None)
6
7 available_features = [features.HifiEq,
8- features.MIDIScript,
9 features.Mad,
10 features.CoreAudio,
11 features.MediaFoundation,
12 features.HSS1394,
13+ features.HID,
14 features.VinylControl,
15 features.Shoutcast,
16 features.Profiling,
17
18=== modified file 'mixxx/build/depends.py'
19--- mixxx/build/depends.py 2012-04-22 17:33:25 +0000
20+++ mixxx/build/depends.py 2012-04-25 15:44:23 +0000
21@@ -29,7 +29,7 @@
22 raise Exception('Did not find PortMidi or its development headers.')
23
24 def sources(self, build):
25- return ['midi/portmidienumerator.cpp', 'midi/midideviceportmidi.cpp']
26+ return ['controllers/midi/portmidienumerator.cpp', 'controllers/midi/portmidicontroller.cpp']
27
28 class OpenGL(Dependence):
29
30@@ -158,6 +158,7 @@
31 build.env.Append(LIBS = 'QtSql4')
32 build.env.Append(LIBS = 'QtGui4')
33 build.env.Append(LIBS = 'QtCore4')
34+ build.env.Append(LIBS = 'QtScript4')
35 build.env.Append(LIBS = 'QtWebKit4')
36 build.env.Append(LIBS = 'QtNetwork4')
37 build.env.Append(LIBS = 'QtOpenGL4')
38@@ -168,6 +169,7 @@
39 '$QTDIR/include/QtGui',
40 '$QTDIR/include/QtXml',
41 '$QTDIR/include/QtNetwork',
42+ '$QTDIR/include/QtScript',
43 '$QTDIR/include/QtSql',
44 '$QTDIR/include/QtOpenGL',
45 '$QTDIR/include/QtWebKit',
46@@ -307,9 +309,11 @@
47 "dlgpreferences.cpp",
48 "dlgprefsound.cpp",
49 "dlgprefsounditem.cpp",
50- "dlgprefmidibindings.cpp",
51+ "controllers/dlgprefcontroller.cpp",
52+ "controllers/dlgprefmappablecontroller.cpp",
53+ "controllers/dlgcontrollerlearning.cpp",
54+ "controllers/dlgprefnocontrollers.cpp",
55 "dlgprefplaylist.cpp",
56- "dlgprefnomidi.cpp",
57 "dlgprefcontrols.cpp",
58 "dlgprefbpm.cpp",
59 "dlgprefreplaygain.cpp",
60@@ -318,7 +322,6 @@
61 "dlgabout.cpp",
62 "dlgprefeq.cpp",
63 "dlgprefcrossfader.cpp",
64- "dlgmidilearning.cpp",
65 "dlgtrackinfo.cpp",
66 "dlgprepare.cpp",
67 "dlgautodj.cpp",
68@@ -365,24 +368,21 @@
69 "analyserbpm.cpp",
70 "analyserwaveform.cpp",
71
72- "midi/mididevice.cpp",
73- "midi/mididevicemanager.cpp",
74- "midi/midideviceenumerator.cpp",
75- "midi/midimapping.cpp",
76- "midi/midiinputmappingtablemodel.cpp",
77- "midi/midioutputmappingtablemodel.cpp",
78- "midi/midichanneldelegate.cpp",
79- "midi/midistatusdelegate.cpp",
80- "midi/midinodelegate.cpp",
81- "midi/midioptiondelegate.cpp",
82- "midi/midimessage.cpp",
83- "midi/midiledhandler.cpp",
84- "softtakeover.cpp",
85+ "controllers/controller.cpp",
86+ "controllers/controllerengine.cpp",
87+ "controllers/controllerenumerator.cpp",
88+ "controllers/controllermanager.cpp",
89+ "controllers/controllerpresetfilehandler.cpp",
90+ "controllers/midi/midicontroller.cpp",
91+ "controllers/midi/midicontrollerpresetfilehandler.cpp",
92+ "controllers/midi/midienumerator.cpp",
93+ "controllers/midi/midioutputhandler.cpp",
94+ "controllers/mixxxcontrol.cpp",
95+ "controllers/qtscript-bytearray/bytearrayclass.cpp",
96+ "controllers/qtscript-bytearray/bytearrayprototype.cpp",
97+ "controllers/softtakeover.cpp",
98
99 "main.cpp",
100- "controlgroupdelegate.cpp",
101- "controlvaluedelegate.cpp",
102- "mixxxcontrol.cpp",
103 "mixxx.cpp",
104 "errordialoghandler.cpp",
105 "upgrade.cpp",
106@@ -592,9 +592,13 @@
107 # the code for the QT UI forms
108 build.env.Uic4('dlgpreferencesdlg.ui')
109 build.env.Uic4('dlgprefsounddlg.ui')
110- build.env.Uic4('dlgprefmidibindingsdlg.ui')
111+
112+ build.env.Uic4('controllers/dlgprefcontrollerdlg.ui')
113+ build.env.Uic4('controllers/dlgprefmappablecontrollerdlg.ui')
114+ build.env.Uic4('controllers/dlgcontrollerlearning.ui')
115+ build.env.Uic4('controllers/dlgprefnocontrollersdlg.ui')
116+
117 build.env.Uic4('dlgprefplaylistdlg.ui')
118- build.env.Uic4('dlgprefnomididlg.ui')
119 build.env.Uic4('dlgprefcontrolsdlg.ui')
120 build.env.Uic4('dlgprefeqdlg.ui')
121 build.env.Uic4('dlgprefcrossfaderdlg.ui')
122@@ -607,7 +611,6 @@
123 build.env.Uic4('dlgprefnovinyldlg.ui')
124 build.env.Uic4('dlgprefrecorddlg.ui')
125 build.env.Uic4('dlgaboutdlg.ui')
126- build.env.Uic4('dlgmidilearning.ui')
127 build.env.Uic4('dlgtrackinfo.ui')
128 build.env.Uic4('dlgprepare.ui')
129 build.env.Uic4('dlgautodj.ui')
130
131=== modified file 'mixxx/build/features.py'
132--- mixxx/build/features.py 2012-04-16 04:00:01 +0000
133+++ mixxx/build/features.py 2012-04-25 15:44:23 +0000
134@@ -44,12 +44,55 @@
135 build.env.Append(CPPDEFINES = '__HSS1394__')
136
137 def sources(self, build):
138- sources = SCons.Split("""midi/mididevicehss1394.cpp
139- midi/hss1394enumerator.cpp
140- """)
141+ return ['controllers/midi/hss1394controller.cpp',
142+ 'controllers/midi/hss1394enumerator.cpp']
143+
144+class HID(Feature):
145+ HIDAPI_INTERNAL_PATH = '#lib/hidapi-0.7.0'
146+ def description(self):
147+ return "HID controller support"
148+
149+ def enabled(self, build):
150+ build.flags['hid'] = util.get_flags(build.env, 'hid', 1)
151+ if int(build.flags['hid']):
152+ return True
153+ return False
154+
155+ def add_options(self, build, vars):
156+ vars.Add('hid', 'Set to 1 to enable HID controller support.', 1)
157+
158+ def configure(self, build, conf):
159+ if not self.enabled(build):
160+ return
161+ if build.platform_is_linux and (not conf.CheckLib(['libusb-1.0', 'usb-1.0']) or not conf.CheckHeader('libusb-1.0/libusb.h')):
162+ raise Exception('Did not find the libusb 1.0 development library or its header file, exiting!')
163+ return
164+ elif build.platform_is_windows:
165+ if not conf.CheckLib(['hidapi', 'libhidapi']):
166+ raise Exception('Did not find HIDAPI development library, exiting!')
167+ return
168+
169+ build.env.Append(CPPDEFINES = '__HID__')
170+
171+ def sources(self, build):
172+ build.env.Append(CPPPATH=[os.path.join(self.HIDAPI_INTERNAL_PATH, 'hidapi')])
173+ sources = ['controllers/hid/hidcontroller.cpp',
174+ 'controllers/hid/hidenumerator.cpp',
175+ 'controllers/hid/hidcontrollerpresetfilehandler.cpp']
176+
177+ if build.platform_is_windows:
178+ # This doesn't work. You need to build it in MSVS like all the other dependencies
179+ # sources.append("#lib/hidapi-0.7.0/windows/hid.c")
180+ return sources
181+ elif build.platform_is_linux:
182+ build.env.ParseConfig('pkg-config libusb-1.0 --silence-errors --cflags --libs')
183+ sources.append(os.path.join(self.HIDAPI_INTERNAL_PATH, 'linux/hid-libusb.c'))
184+ elif build.platform_is_osx:
185+ build.env.Append(LINKFLAGS='-framework IOKit')
186+ build.env.Append(LINKFLAGS='-framework CoreFoundation')
187+ sources.append(os.path.join(self.HIDAPI_INTERNAL_PATH, 'mac/hid.c'))
188 return sources
189
190-
191 class Mad(Feature):
192 def description(self):
193 return "MAD MP3 Decoder"
194@@ -138,35 +181,6 @@
195 build.env.Append(CPPDEFINES='__MEDIAFOUNDATION__')
196 return
197
198-class MIDIScript(Feature):
199- def description(self):
200- return "MIDI Scripting"
201-
202- def enabled(self, build):
203- build.flags['midiscript'] = util.get_flags(build.env, 'midiscript', 0)
204- if int(build.flags['midiscript']):
205- return True
206- return False
207-
208- def add_options(self, build, vars):
209- vars.Add('midiscript', 'Set to 1 to enable MIDI Scripting support.', 1)
210-
211- def configure(self, build, conf):
212- if not self.enabled(build):
213- return
214- if build.platform_is_windows:
215- build.env.Append(LIBS = 'QtScript4')
216- elif build.platform_is_linux:
217- build.env.Append(LIBS = 'QtScript')
218- elif build.platform_is_osx:
219- # TODO(XXX) put in logic here to add a -framework QtScript
220- pass
221- build.env.Append(CPPPATH = '$QTDIR/include/QtScript')
222- build.env.Append(CPPDEFINES = '__MIDISCRIPT__')
223-
224- def sources(self, build):
225- return ["midi/midiscriptengine.cpp"]
226-
227 class LADSPA(Feature):
228
229 def description(self):
230
231=== added directory 'mixxx/lib/hidapi-0.7.0'
232=== added file 'mixxx/lib/hidapi-0.7.0/LICENSE-orig.txt'
233--- mixxx/lib/hidapi-0.7.0/LICENSE-orig.txt 1970-01-01 00:00:00 +0000
234+++ mixxx/lib/hidapi-0.7.0/LICENSE-orig.txt 2012-04-25 15:44:23 +0000
235@@ -0,0 +1,9 @@
236+ HIDAPI - Multi-Platform library for
237+ communication with HID devices.
238+
239+ Copyright 2009, Alan Ott, Signal 11 Software.
240+ All Rights Reserved.
241+
242+ This software may be used by anyone for any reason so
243+ long as the copyright notice in the source files
244+ remains intact.
245
246=== added file 'mixxx/lib/hidapi-0.7.0/LICENSE.txt'
247--- mixxx/lib/hidapi-0.7.0/LICENSE.txt 1970-01-01 00:00:00 +0000
248+++ mixxx/lib/hidapi-0.7.0/LICENSE.txt 2012-04-25 15:44:23 +0000
249@@ -0,0 +1,13 @@
250+HIDAPI can be used under one of three licenses.
251+
252+1. The GNU Public License, version 3.0, in LICENSE-gpl3.txt
253+2. A BSD-Style License, in LICENSE-bsd.txt.
254+3. The more liberal original HIDAPI license. LICENSE-orig.txt
255+
256+The license chosen is at the discretion of the user of HIDAPI. For example:
257+1. An author of GPL software would likely use HIDAPI under the terms of the
258+GPL.
259+
260+2. An author of commercial closed-source software would likely use HIDAPI
261+under the terms of the BSD-style license or the original HIDAPI license.
262+
263
264=== added directory 'mixxx/lib/hidapi-0.7.0/hidapi'
265=== added file 'mixxx/lib/hidapi-0.7.0/hidapi/hidapi.h'
266--- mixxx/lib/hidapi-0.7.0/hidapi/hidapi.h 1970-01-01 00:00:00 +0000
267+++ mixxx/lib/hidapi-0.7.0/hidapi/hidapi.h 2012-04-25 15:44:23 +0000
268@@ -0,0 +1,383 @@
269+/*******************************************************
270+ HIDAPI - Multi-Platform library for
271+ communication with HID devices.
272+
273+ Alan Ott
274+ Signal 11 Software
275+
276+ 8/22/2009
277+
278+ Copyright 2009, All Rights Reserved.
279+
280+ At the discretion of the user of this library,
281+ this software may be licensed under the terms of the
282+ GNU Public License v3, a BSD-Style license, or the
283+ original HIDAPI license as outlined in the LICENSE.txt,
284+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
285+ files located at the root of the source distribution.
286+ These files may also be found in the public source
287+ code repository located at:
288+ http://github.com/signal11/hidapi .
289+********************************************************/
290+
291+/** @file
292+ * @defgroup API hidapi API
293+ */
294+
295+#ifndef HIDAPI_H__
296+#define HIDAPI_H__
297+
298+#include <wchar.h>
299+
300+#ifdef _WIN32
301+ #define HID_API_EXPORT __declspec(dllexport)
302+ #define HID_API_CALL
303+#else
304+ #define HID_API_EXPORT /**< API export macro */
305+ #define HID_API_CALL /**< API call macro */
306+#endif
307+
308+#define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/
309+
310+#ifdef __cplusplus
311+extern "C" {
312+#endif
313+ struct hid_device_;
314+ typedef struct hid_device_ hid_device; /**< opaque hidapi structure */
315+
316+ /** hidapi info structure */
317+ struct hid_device_info {
318+ /** Platform-specific device path */
319+ char *path;
320+ /** Device Vendor ID */
321+ unsigned short vendor_id;
322+ /** Device Product ID */
323+ unsigned short product_id;
324+ /** Serial Number */
325+ wchar_t *serial_number;
326+ /** Device Release Number in binary-coded decimal,
327+ also known as Device Version Number */
328+ unsigned short release_number;
329+ /** Manufacturer String */
330+ wchar_t *manufacturer_string;
331+ /** Product string */
332+ wchar_t *product_string;
333+ /** Usage Page for this Device/Interface
334+ (Windows/Mac only). */
335+ unsigned short usage_page;
336+ /** Usage for this Device/Interface
337+ (Windows/Mac only).*/
338+ unsigned short usage;
339+ /** The USB interface which this logical device
340+ represents. Valid on both Linux implementations
341+ in all cases, and valid on the Windows implementation
342+ only if the device contains more than one interface. */
343+ int interface_number;
344+
345+ /** Pointer to the next device */
346+ struct hid_device_info *next;
347+ };
348+
349+
350+ /** @brief Initialize the HIDAPI library.
351+
352+ This function initializes the HIDAPI library. Calling it is not
353+ strictly necessary, as it will be called automatically by
354+ hid_enumerate() and any of the hid_open_*() functions if it is
355+ needed. This function should be called at the beginning of
356+ execution however, if there is a chance of HIDAPI handles
357+ being opened by different threads simultaneously.
358+
359+ @ingroup API
360+
361+ @returns
362+ This function returns 0 on success and -1 on error.
363+ */
364+ int HID_API_EXPORT HID_API_CALL hid_init(void);
365+
366+ /** @brief Finalize the HIDAPI library.
367+
368+ This function frees all of the static data associated with
369+ HIDAPI. It should be called at the end of execution to avoid
370+ memory leaks.
371+
372+ @ingroup API
373+
374+ @returns
375+ This function returns 0 on success and -1 on error.
376+ */
377+ int HID_API_EXPORT HID_API_CALL hid_exit(void);
378+
379+ /** @brief Enumerate the HID Devices.
380+
381+ This function returns a linked list of all the HID devices
382+ attached to the system which match vendor_id and product_id.
383+ If @p vendor_id and @p product_id are both set to 0, then
384+ all HID devices will be returned.
385+
386+ @ingroup API
387+ @param vendor_id The Vendor ID (VID) of the types of device
388+ to open.
389+ @param product_id The Product ID (PID) of the types of
390+ device to open.
391+
392+ @returns
393+ This function returns a pointer to a linked list of type
394+ struct #hid_device, containing information about the HID devices
395+ attached to the system, or NULL in the case of failure. Free
396+ this linked list by calling hid_free_enumeration().
397+ */
398+ struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id);
399+
400+ /** @brief Free an enumeration Linked List
401+
402+ This function frees a linked list created by hid_enumerate().
403+
404+ @ingroup API
405+ @param devs Pointer to a list of struct_device returned from
406+ hid_enumerate().
407+ */
408+ void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs);
409+
410+ /** @brief Open a HID device using a Vendor ID (VID), Product ID
411+ (PID) and optionally a serial number.
412+
413+ If @p serial_number is NULL, the first device with the
414+ specified VID and PID is opened.
415+
416+ @ingroup API
417+ @param vendor_id The Vendor ID (VID) of the device to open.
418+ @param product_id The Product ID (PID) of the device to open.
419+ @param serial_number The Serial Number of the device to open
420+ (Optionally NULL).
421+
422+ @returns
423+ This function returns a pointer to a #hid_device object on
424+ success or NULL on failure.
425+ */
426+ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number);
427+
428+ /** @brief Open a HID device by its path name.
429+
430+ The path name be determined by calling hid_enumerate(), or a
431+ platform-specific path name can be used (eg: /dev/hidraw0 on
432+ Linux).
433+
434+ @ingroup API
435+ @param path The path name of the device to open
436+
437+ @returns
438+ This function returns a pointer to a #hid_device object on
439+ success or NULL on failure.
440+ */
441+ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path);
442+
443+ /** @brief Write an Output report to a HID device.
444+
445+ The first byte of @p data[] must contain the Report ID. For
446+ devices which only support a single report, this must be set
447+ to 0x0. The remaining bytes contain the report data. Since
448+ the Report ID is mandatory, calls to hid_write() will always
449+ contain one more byte than the report contains. For example,
450+ if a hid report is 16 bytes long, 17 bytes must be passed to
451+ hid_write(), the Report ID (or 0x0, for devices with a
452+ single report), followed by the report data (16 bytes). In
453+ this example, the length passed in would be 17.
454+
455+ hid_write() will send the data on the first OUT endpoint, if
456+ one exists. If it does not, it will send the data through
457+ the Control Endpoint (Endpoint 0).
458+
459+ @ingroup API
460+ @param device A device handle returned from hid_open().
461+ @param data The data to send, including the report number as
462+ the first byte.
463+ @param length The length in bytes of the data to send.
464+
465+ @returns
466+ This function returns the actual number of bytes written and
467+ -1 on error.
468+ */
469+ int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length);
470+
471+ /** @brief Read an Input report from a HID device with timeout.
472+
473+ Input reports are returned
474+ to the host through the INTERRUPT IN endpoint. The first byte will
475+ contain the Report number if the device uses numbered reports.
476+
477+ @ingroup API
478+ @param device A device handle returned from hid_open().
479+ @param data A buffer to put the read data into.
480+ @param length The number of bytes to read. For devices with
481+ multiple reports, make sure to read an extra byte for
482+ the report number.
483+ @param milliseconds timeout in milliseconds or -1 for blocking wait.
484+
485+ @returns
486+ This function returns the actual number of bytes read and
487+ -1 on error.
488+ */
489+ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds);
490+
491+ /** @brief Read an Input report from a HID device.
492+
493+ Input reports are returned
494+ to the host through the INTERRUPT IN endpoint. The first byte will
495+ contain the Report number if the device uses numbered reports.
496+
497+ @ingroup API
498+ @param device A device handle returned from hid_open().
499+ @param data A buffer to put the read data into.
500+ @param length The number of bytes to read. For devices with
501+ multiple reports, make sure to read an extra byte for
502+ the report number.
503+
504+ @returns
505+ This function returns the actual number of bytes read and
506+ -1 on error.
507+ */
508+ int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length);
509+
510+ /** @brief Set the device handle to be non-blocking.
511+
512+ In non-blocking mode calls to hid_read() will return
513+ immediately with a value of 0 if there is no data to be
514+ read. In blocking mode, hid_read() will wait (block) until
515+ there is data to read before returning.
516+
517+ Nonblocking can be turned on and off at any time.
518+
519+ @ingroup API
520+ @param device A device handle returned from hid_open().
521+ @param nonblock enable or not the nonblocking reads
522+ - 1 to enable nonblocking
523+ - 0 to disable nonblocking.
524+
525+ @returns
526+ This function returns 0 on success and -1 on error.
527+ */
528+ int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock);
529+
530+ /** @brief Send a Feature report to the device.
531+
532+ Feature reports are sent over the Control endpoint as a
533+ Set_Report transfer. The first byte of @p data[] must
534+ contain the Report ID. For devices which only support a
535+ single report, this must be set to 0x0. The remaining bytes
536+ contain the report data. Since the Report ID is mandatory,
537+ calls to hid_send_feature_report() will always contain one
538+ more byte than the report contains. For example, if a hid
539+ report is 16 bytes long, 17 bytes must be passed to
540+ hid_send_feature_report(): the Report ID (or 0x0, for
541+ devices which do not use numbered reports), followed by the
542+ report data (16 bytes). In this example, the length passed
543+ in would be 17.
544+
545+ @ingroup API
546+ @param device A device handle returned from hid_open().
547+ @param data The data to send, including the report number as
548+ the first byte.
549+ @param length The length in bytes of the data to send, including
550+ the report number.
551+
552+ @returns
553+ This function returns the actual number of bytes written and
554+ -1 on error.
555+ */
556+ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length);
557+
558+ /** @brief Get a feature report from a HID device.
559+
560+ Make sure to set the first byte of @p data[] to the Report
561+ ID of the report to be read. Make sure to allow space for
562+ this extra byte in @p data[].
563+
564+ @ingroup API
565+ @param device A device handle returned from hid_open().
566+ @param data A buffer to put the read data into, including
567+ the Report ID. Set the first byte of @p data[] to the
568+ Report ID of the report to be read.
569+ @param length The number of bytes to read, including an
570+ extra byte for the report ID. The buffer can be longer
571+ than the actual report.
572+
573+ @returns
574+ This function returns the number of bytes read and
575+ -1 on error.
576+ */
577+ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length);
578+
579+ /** @brief Close a HID device.
580+
581+ @ingroup API
582+ @param device A device handle returned from hid_open().
583+ */
584+ void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device);
585+
586+ /** @brief Get The Manufacturer String from a HID device.
587+
588+ @ingroup API
589+ @param device A device handle returned from hid_open().
590+ @param string A wide string buffer to put the data into.
591+ @param maxlen The length of the buffer in multiples of wchar_t.
592+
593+ @returns
594+ This function returns 0 on success and -1 on error.
595+ */
596+ int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen);
597+
598+ /** @brief Get The Product String from a HID device.
599+
600+ @ingroup API
601+ @param device A device handle returned from hid_open().
602+ @param string A wide string buffer to put the data into.
603+ @param maxlen The length of the buffer in multiples of wchar_t.
604+
605+ @returns
606+ This function returns 0 on success and -1 on error.
607+ */
608+ int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen);
609+
610+ /** @brief Get The Serial Number String from a HID device.
611+
612+ @ingroup API
613+ @param device A device handle returned from hid_open().
614+ @param string A wide string buffer to put the data into.
615+ @param maxlen The length of the buffer in multiples of wchar_t.
616+
617+ @returns
618+ This function returns 0 on success and -1 on error.
619+ */
620+ int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen);
621+
622+ /** @brief Get a string from a HID device, based on its string index.
623+
624+ @ingroup API
625+ @param device A device handle returned from hid_open().
626+ @param string_index The index of the string to get.
627+ @param string A wide string buffer to put the data into.
628+ @param maxlen The length of the buffer in multiples of wchar_t.
629+
630+ @returns
631+ This function returns 0 on success and -1 on error.
632+ */
633+ int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen);
634+
635+ /** @brief Get a string describing the last error which occurred.
636+
637+ @ingroup API
638+ @param device A device handle returned from hid_open().
639+
640+ @returns
641+ This function returns a string containing the last error
642+ which occurred or NULL if none has occurred.
643+ */
644+ HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device);
645+
646+#ifdef __cplusplus
647+}
648+#endif
649+
650+#endif
651+
652
653=== added directory 'mixxx/lib/hidapi-0.7.0/linux'
654=== added file 'mixxx/lib/hidapi-0.7.0/linux/.gitignore'
655--- mixxx/lib/hidapi-0.7.0/linux/.gitignore 1970-01-01 00:00:00 +0000
656+++ mixxx/lib/hidapi-0.7.0/linux/.gitignore 2012-04-25 15:44:23 +0000
657@@ -0,0 +1,13 @@
658+Debug
659+Release
660+*.exp
661+*.ilk
662+*.lib
663+*.suo
664+*.vcproj.*
665+*.ncb
666+*.suo
667+*.dll
668+*.pdb
669+*.o
670+hidtest
671\ No newline at end of file
672
673=== added file 'mixxx/lib/hidapi-0.7.0/linux/Makefile'
674--- mixxx/lib/hidapi-0.7.0/linux/Makefile 1970-01-01 00:00:00 +0000
675+++ mixxx/lib/hidapi-0.7.0/linux/Makefile 2012-04-25 15:44:23 +0000
676@@ -0,0 +1,36 @@
677+###########################################
678+# Simple Makefile for HIDAPI test program
679+#
680+# Alan Ott
681+# Signal 11 Software
682+# 2010-06-01
683+###########################################
684+
685+all: hidtest
686+
687+CC ?= gcc
688+CFLAGS ?= -Wall -g
689+
690+CXX ?= g++
691+CXXFLAGS ?= -Wall -g
692+
693+COBJS = hid-libusb.o
694+CPPOBJS = ../hidtest/hidtest.o
695+OBJS = $(COBJS) $(CPPOBJS)
696+LIBS = `pkg-config libusb-1.0 libudev --libs`
697+INCLUDES ?= -I../hidapi `pkg-config libusb-1.0 --cflags`
698+
699+
700+hidtest: $(OBJS)
701+ $(CXX) $(CXXFLAGS) $(LDFLAGS) $^ $(LIBS) -o hidtest
702+
703+$(COBJS): %.o: %.c
704+ $(CC) $(CFLAGS) -c $(INCLUDES) $< -o $@
705+
706+$(CPPOBJS): %.o: %.cpp
707+ $(CXX) $(CXXFLAGS) -c $(INCLUDES) $< -o $@
708+
709+clean:
710+ rm -f $(OBJS) hidtest
711+
712+.PHONY: clean
713
714=== added file 'mixxx/lib/hidapi-0.7.0/linux/README.txt'
715--- mixxx/lib/hidapi-0.7.0/linux/README.txt 1970-01-01 00:00:00 +0000
716+++ mixxx/lib/hidapi-0.7.0/linux/README.txt 2012-04-25 15:44:23 +0000
717@@ -0,0 +1,63 @@
718+
719+There are two implementations of HIDAPI for Linux. One (hid.c) uses the
720+Linux hidraw driver, and the other (hid-libusb.c) uses libusb. Which one you
721+use depends on your application. Complete functionality of the hidraw
722+version depends on patches to the Linux kernel which are not currently in
723+the mainline. These patches have to do with sending and receiving feature
724+reports. The libusb implementation uses libusb to talk directly to the
725+device, bypassing any Linux HID driver. The disadvantage of the libusb
726+version is that it will only work with USB devices, while the hidraw
727+implementation will work with Bluetooth devices as well.
728+
729+To use HIDAPI, simply drop either hid.c or hid-libusb.c into your
730+application and build using the build parameters in the Makefile.
731+
732+By default, on Linux, the Makefile in this directory is configured to use
733+the libusb implementation. To switch to the hidraw implementation, simply
734+change hid-libusb.c to hid.c in the Makefile.
735+
736+
737+Libusb Implementation notes
738+----------------------------
739+For the libusb implementation, libusb-1.0 must be installed. Libusb 1.0 is
740+different than the legacy libusb 0.1 which is installed on many systems. To
741+install libusb-1.0 on Ubuntu and other Debian-based systems, run:
742+ sudo apt-get install libusb-1.0-0-dev
743+
744+
745+Hidraw Implementation notes
746+----------------------------
747+For the hidraw implementation, libudev headers and libraries are required to
748+build hidapi programs. To install libudev libraries on Ubuntu,
749+and other Debian-based systems, run:
750+ sudo apt-get install libudev-dev
751+
752+On Redhat-based systems, run the following as root:
753+ yum install libudev-devel
754+
755+Unfortunately, the hidraw driver, which the linux version of hidapi is based
756+on, contains bugs in kernel versions < 2.6.36, which the client application
757+should be aware of.
758+
759+Bugs (hidraw implementation only):
760+-----------------------------------
761+On Kernel versions < 2.6.34, if your device uses numbered reports, an extra
762+byte will be returned at the beginning of all reports returned from read()
763+for hidraw devices. This is worked around in the libary. No action should be
764+necessary in the client library.
765+
766+On Kernel versions < 2.6.35, reports will only be sent using a Set_Report
767+transfer on the CONTROL endpoint. No data will ever be sent on an Interrupt
768+Out endpoint if one exists. This is fixed in 2.6.35. In 2.6.35, OUTPUT
769+reports will be sent to the device on the first INTERRUPT OUT endpoint if it
770+exists; If it does not exist, OUTPUT reports will be sent on the CONTROL
771+endpoint.
772+
773+On Kernel versions < 2.6.36, add an extra byte containing the report number
774+to sent reports if numbered reports are used, and the device does not
775+contain an INTERRPUT OUT endpoint for OUTPUT transfers. For example, if
776+your device uses numbered reports and wants to send {0x2 0xff 0xff 0xff} to
777+the device (0x2 is the report number), you must send {0x2 0x2 0xff 0xff
778+0xff}. If your device has the optional Interrupt OUT endpoint, this does not
779+apply (but really on 2.6.35 only, because 2.6.34 won't use the interrupt
780+out endpoint).
781
782=== added file 'mixxx/lib/hidapi-0.7.0/linux/hid-libusb.c'
783--- mixxx/lib/hidapi-0.7.0/linux/hid-libusb.c 1970-01-01 00:00:00 +0000
784+++ mixxx/lib/hidapi-0.7.0/linux/hid-libusb.c 2012-04-25 15:44:23 +0000
785@@ -0,0 +1,1386 @@
786+/*******************************************************
787+ HIDAPI - Multi-Platform library for
788+ communication with HID devices.
789+
790+ Alan Ott
791+ Signal 11 Software
792+
793+ 8/22/2009
794+ Linux Version - 6/2/2010
795+ Libusb Version - 8/13/2010
796+
797+ Copyright 2009, All Rights Reserved.
798+
799+ At the discretion of the user of this library,
800+ this software may be licensed under the terms of the
801+ GNU Public License v3, a BSD-Style license, or the
802+ original HIDAPI license as outlined in the LICENSE.txt,
803+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
804+ files located at the root of the source distribution.
805+ These files may also be found in the public source
806+ code repository located at:
807+ http://github.com/signal11/hidapi .
808+********************************************************/
809+
810+#define _GNU_SOURCE // needed for wcsdup() before glibc 2.10
811+
812+/* C */
813+#include <stdio.h>
814+#include <string.h>
815+#include <stdlib.h>
816+#include <ctype.h>
817+#include <locale.h>
818+#include <errno.h>
819+
820+/* Unix */
821+#include <unistd.h>
822+#include <sys/types.h>
823+#include <sys/stat.h>
824+#include <sys/ioctl.h>
825+#include <sys/utsname.h>
826+#include <fcntl.h>
827+#include <pthread.h>
828+#include <wchar.h>
829+
830+/* GNU / LibUSB */
831+#include "libusb.h"
832+#include "iconv.h"
833+
834+#include "hidapi.h"
835+
836+#ifdef __cplusplus
837+extern "C" {
838+#endif
839+
840+#ifdef DEBUG_PRINTF
841+#define LOG(...) fprintf(stderr, __VA_ARGS__)
842+#else
843+#define LOG(...) do {} while (0)
844+#endif
845+
846+
847+/* Uncomment to enable the retrieval of Usage and Usage Page in
848+hid_enumerate(). Warning, this is very invasive as it requires the detach
849+and re-attach of the kernel driver. See comments inside hid_enumerate().
850+Linux/libusb HIDAPI programs are encouraged to use the interface number
851+instead to differentiate between interfaces on a composite HID device. */
852+/*#define INVASIVE_GET_USAGE*/
853+
854+/* Linked List of input reports received from the device. */
855+struct input_report {
856+ uint8_t *data;
857+ size_t len;
858+ struct input_report *next;
859+};
860+
861+
862+struct hid_device_ {
863+ /* Handle to the actual device. */
864+ libusb_device_handle *device_handle;
865+
866+ /* Endpoint information */
867+ int input_endpoint;
868+ int output_endpoint;
869+ int input_ep_max_packet_size;
870+
871+ /* The interface number of the HID */
872+ int interface;
873+
874+ /* Indexes of Strings */
875+ int manufacturer_index;
876+ int product_index;
877+ int serial_index;
878+
879+ /* Whether blocking reads are used */
880+ int blocking; /* boolean */
881+
882+ /* Read thread objects */
883+ pthread_t thread;
884+ pthread_mutex_t mutex; /* Protects input_reports */
885+ pthread_cond_t condition;
886+ pthread_barrier_t barrier; /* Ensures correct startup sequence */
887+ int shutdown_thread;
888+ struct libusb_transfer *transfer;
889+
890+ /* List of received input reports. */
891+ struct input_report *input_reports;
892+};
893+
894+static int initialized = 0;
895+
896+uint16_t get_usb_code_for_current_locale(void);
897+static int return_data(hid_device *dev, unsigned char *data, size_t length);
898+
899+static hid_device *new_hid_device(void)
900+{
901+ hid_device *dev = calloc(1, sizeof(hid_device));
902+ dev->device_handle = NULL;
903+ dev->input_endpoint = 0;
904+ dev->output_endpoint = 0;
905+ dev->input_ep_max_packet_size = 0;
906+ dev->interface = 0;
907+ dev->manufacturer_index = 0;
908+ dev->product_index = 0;
909+ dev->serial_index = 0;
910+ dev->blocking = 1;
911+ dev->shutdown_thread = 0;
912+ dev->transfer = NULL;
913+ dev->input_reports = NULL;
914+
915+ pthread_mutex_init(&dev->mutex, NULL);
916+ pthread_cond_init(&dev->condition, NULL);
917+ pthread_barrier_init(&dev->barrier, NULL, 2);
918+
919+ return dev;
920+}
921+
922+static void free_hid_device(hid_device *dev)
923+{
924+ /* Clean up the thread objects */
925+ pthread_barrier_destroy(&dev->barrier);
926+ pthread_cond_destroy(&dev->condition);
927+ pthread_mutex_destroy(&dev->mutex);
928+
929+ /* Free the device itself */
930+ free(dev);
931+}
932+
933+#if 0
934+//TODO: Implement this funciton on Linux.
935+static void register_error(hid_device *device, const char *op)
936+{
937+
938+}
939+#endif
940+
941+#ifdef INVASIVE_GET_USAGE
942+/* Get bytes from a HID Report Descriptor.
943+ Only call with a num_bytes of 0, 1, 2, or 4. */
944+static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur)
945+{
946+ /* Return if there aren't enough bytes. */
947+ if (cur + num_bytes >= len)
948+ return 0;
949+
950+ if (num_bytes == 0)
951+ return 0;
952+ else if (num_bytes == 1) {
953+ return rpt[cur+1];
954+ }
955+ else if (num_bytes == 2) {
956+ return (rpt[cur+2] * 256 + rpt[cur+1]);
957+ }
958+ else if (num_bytes == 4) {
959+ return (rpt[cur+4] * 0x01000000 +
960+ rpt[cur+3] * 0x00010000 +
961+ rpt[cur+2] * 0x00000100 +
962+ rpt[cur+1] * 0x00000001);
963+ }
964+ else
965+ return 0;
966+}
967+
968+/* Retrieves the device's Usage Page and Usage from the report
969+ descriptor. The algorithm is simple, as it just returns the first
970+ Usage and Usage Page that it finds in the descriptor.
971+ The return value is 0 on success and -1 on failure. */
972+static int get_usage(uint8_t *report_descriptor, size_t size,
973+ unsigned short *usage_page, unsigned short *usage)
974+{
975+ int i = 0;
976+ int size_code;
977+ int data_len, key_size;
978+ int usage_found = 0, usage_page_found = 0;
979+
980+ while (i < size) {
981+ int key = report_descriptor[i];
982+ int key_cmd = key & 0xfc;
983+
984+ //printf("key: %02hhx\n", key);
985+
986+ if ((key & 0xf0) == 0xf0) {
987+ /* This is a Long Item. The next byte contains the
988+ length of the data section (value) for this key.
989+ See the HID specification, version 1.11, section
990+ 6.2.2.3, titled "Long Items." */
991+ if (i+1 < size)
992+ data_len = report_descriptor[i+1];
993+ else
994+ data_len = 0; /* malformed report */
995+ key_size = 3;
996+ }
997+ else {
998+ /* This is a Short Item. The bottom two bits of the
999+ key contain the size code for the data section
1000+ (value) for this key. Refer to the HID
1001+ specification, version 1.11, section 6.2.2.2,
1002+ titled "Short Items." */
1003+ size_code = key & 0x3;
1004+ switch (size_code) {
1005+ case 0:
1006+ case 1:
1007+ case 2:
1008+ data_len = size_code;
1009+ break;
1010+ case 3:
1011+ data_len = 4;
1012+ break;
1013+ default:
1014+ /* Can't ever happen since size_code is & 0x3 */
1015+ data_len = 0;
1016+ break;
1017+ };
1018+ key_size = 1;
1019+ }
1020+
1021+ if (key_cmd == 0x4) {
1022+ *usage_page = get_bytes(report_descriptor, size, data_len, i);
1023+ usage_page_found = 1;
1024+ //printf("Usage Page: %x\n", (uint32_t)*usage_page);
1025+ }
1026+ if (key_cmd == 0x8) {
1027+ *usage = get_bytes(report_descriptor, size, data_len, i);
1028+ usage_found = 1;
1029+ //printf("Usage: %x\n", (uint32_t)*usage);
1030+ }
1031+
1032+ if (usage_page_found && usage_found)
1033+ return 0; /* success */
1034+
1035+ /* Skip over this key and it's associated data */
1036+ i += data_len + key_size;
1037+ }
1038+
1039+ return -1; /* failure */
1040+}
1041+#endif // INVASIVE_GET_USAGE
1042+
1043+
1044+/* Get the first language the device says it reports. This comes from
1045+ USB string #0. */
1046+static uint16_t get_first_language(libusb_device_handle *dev)
1047+{
1048+ uint16_t buf[32];
1049+ int len;
1050+
1051+ /* Get the string from libusb. */
1052+ len = libusb_get_string_descriptor(dev,
1053+ 0x0, /* String ID */
1054+ 0x0, /* Language */
1055+ (unsigned char*)buf,
1056+ sizeof(buf));
1057+ if (len < 4)
1058+ return 0x0;
1059+
1060+ return buf[1]; // First two bytes are len and descriptor type.
1061+}
1062+
1063+static int is_language_supported(libusb_device_handle *dev, uint16_t lang)
1064+{
1065+ uint16_t buf[32];
1066+ int len;
1067+ int i;
1068+
1069+ /* Get the string from libusb. */
1070+ len = libusb_get_string_descriptor(dev,
1071+ 0x0, /* String ID */
1072+ 0x0, /* Language */
1073+ (unsigned char*)buf,
1074+ sizeof(buf));
1075+ if (len < 4)
1076+ return 0x0;
1077+
1078+
1079+ len /= 2; /* language IDs are two-bytes each. */
1080+ /* Start at index 1 because there are two bytes of protocol data. */
1081+ for (i = 1; i < len; i++) {
1082+ if (buf[i] == lang)
1083+ return 1;
1084+ }
1085+
1086+ return 0;
1087+}
1088+
1089+
1090+/* This function returns a newly allocated wide string containing the USB
1091+ device string numbered by the index. The returned string must be freed
1092+ by using free(). */
1093+static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
1094+{
1095+ char buf[512];
1096+ int len;
1097+ wchar_t *str = NULL;
1098+ wchar_t wbuf[256];
1099+
1100+ /* iconv variables */
1101+ iconv_t ic;
1102+ size_t inbytes;
1103+ size_t outbytes;
1104+ size_t res;
1105+ char *inptr;
1106+ char *outptr;
1107+
1108+ /* Determine which language to use. */
1109+ uint16_t lang;
1110+ lang = get_usb_code_for_current_locale();
1111+ if (!is_language_supported(dev, lang))
1112+ lang = get_first_language(dev);
1113+
1114+ /* Get the string from libusb. */
1115+ len = libusb_get_string_descriptor(dev,
1116+ idx,
1117+ lang,
1118+ (unsigned char*)buf,
1119+ sizeof(buf));
1120+ if (len < 0)
1121+ return NULL;
1122+
1123+ buf[sizeof(buf)-1] = '\0';
1124+
1125+ if (len+1 < sizeof(buf))
1126+ buf[len+1] = '\0';
1127+
1128+ /* Initialize iconv. */
1129+ ic = iconv_open("UTF-32", "UTF-16");
1130+ if (ic == (iconv_t)-1)
1131+ return NULL;
1132+
1133+ /* Convert to UTF-32 (wchar_t on glibc systems).
1134+ Skip the first character (2-bytes). */
1135+ inptr = buf+2;
1136+ inbytes = len-2;
1137+ outptr = (char*) wbuf;
1138+ outbytes = sizeof(wbuf);
1139+ res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
1140+ if (res == (size_t)-1)
1141+ goto err;
1142+
1143+ /* Write the terminating NULL. */
1144+ wbuf[sizeof(wbuf)/sizeof(wbuf[0])-1] = 0x00000000;
1145+ if (outbytes >= sizeof(wbuf[0]))
1146+ *((wchar_t*)outptr) = 0x00000000;
1147+
1148+ /* Allocate and copy the string. */
1149+ str = wcsdup(wbuf+1);
1150+
1151+err:
1152+ iconv_close(ic);
1153+
1154+ return str;
1155+}
1156+
1157+static char *make_path(libusb_device *dev, int interface_number)
1158+{
1159+ char str[64];
1160+ snprintf(str, sizeof(str), "%04x:%04x:%02x",
1161+ libusb_get_bus_number(dev),
1162+ libusb_get_device_address(dev),
1163+ interface_number);
1164+ str[sizeof(str)-1] = '\0';
1165+
1166+ return strdup(str);
1167+}
1168+
1169+
1170+int HID_API_EXPORT hid_init(void)
1171+{
1172+ if (!initialized) {
1173+ if (libusb_init(NULL))
1174+ return -1;
1175+ initialized = 1;
1176+ }
1177+
1178+ return 0;
1179+}
1180+
1181+int HID_API_EXPORT hid_exit(void)
1182+{
1183+ if (initialized) {
1184+ libusb_exit(NULL);
1185+ initialized = 0;
1186+ }
1187+
1188+ return 0;
1189+}
1190+
1191+struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
1192+{
1193+ libusb_device **devs;
1194+ libusb_device *dev;
1195+ libusb_device_handle *handle;
1196+ ssize_t num_devs;
1197+ int i = 0;
1198+
1199+ struct hid_device_info *root = NULL; // return object
1200+ struct hid_device_info *cur_dev = NULL;
1201+
1202+ setlocale(LC_ALL,"");
1203+
1204+ if (!initialized)
1205+ hid_init();
1206+
1207+ num_devs = libusb_get_device_list(NULL, &devs);
1208+ if (num_devs < 0)
1209+ return NULL;
1210+ while ((dev = devs[i++]) != NULL) {
1211+ struct libusb_device_descriptor desc;
1212+ struct libusb_config_descriptor *conf_desc = NULL;
1213+ int j, k;
1214+ int interface_num = 0;
1215+
1216+ int res = libusb_get_device_descriptor(dev, &desc);
1217+ unsigned short dev_vid = desc.idVendor;
1218+ unsigned short dev_pid = desc.idProduct;
1219+
1220+ /* HID's are defined at the interface level. */
1221+ if (desc.bDeviceClass != LIBUSB_CLASS_PER_INTERFACE)
1222+ continue;
1223+
1224+ res = libusb_get_active_config_descriptor(dev, &conf_desc);
1225+ if (res < 0)
1226+ libusb_get_config_descriptor(dev, 0, &conf_desc);
1227+ if (conf_desc) {
1228+ for (j = 0; j < conf_desc->bNumInterfaces; j++) {
1229+ const struct libusb_interface *intf = &conf_desc->interface[j];
1230+ for (k = 0; k < intf->num_altsetting; k++) {
1231+ const struct libusb_interface_descriptor *intf_desc;
1232+ intf_desc = &intf->altsetting[k];
1233+ if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID) {
1234+ interface_num = intf_desc->bInterfaceNumber;
1235+
1236+ /* Check the VID/PID against the arguments */
1237+ if ((vendor_id == 0x0 && product_id == 0x0) ||
1238+ (vendor_id == dev_vid && product_id == dev_pid)) {
1239+ struct hid_device_info *tmp;
1240+
1241+ /* VID/PID match. Create the record. */
1242+ tmp = calloc(1, sizeof(struct hid_device_info));
1243+ if (cur_dev) {
1244+ cur_dev->next = tmp;
1245+ }
1246+ else {
1247+ root = tmp;
1248+ }
1249+ cur_dev = tmp;
1250+
1251+ /* Fill out the record */
1252+ cur_dev->next = NULL;
1253+ cur_dev->path = make_path(dev, interface_num);
1254+
1255+ res = libusb_open(dev, &handle);
1256+
1257+ if (res >= 0) {
1258+ /* Serial Number */
1259+ if (desc.iSerialNumber > 0)
1260+ cur_dev->serial_number =
1261+ get_usb_string(handle, desc.iSerialNumber);
1262+
1263+ /* Manufacturer and Product strings */
1264+ if (desc.iManufacturer > 0)
1265+ cur_dev->manufacturer_string =
1266+ get_usb_string(handle, desc.iManufacturer);
1267+ if (desc.iProduct > 0)
1268+ cur_dev->product_string =
1269+ get_usb_string(handle, desc.iProduct);
1270+
1271+#ifdef INVASIVE_GET_USAGE
1272+ /*
1273+ This section is removed because it is too
1274+ invasive on the system. Getting a Usage Page
1275+ and Usage requires parsing the HID Report
1276+ descriptor. Getting a HID Report descriptor
1277+ involves claiming the interface. Claiming the
1278+ interface involves detaching the kernel driver.
1279+ Detaching the kernel driver is hard on the system
1280+ because it will unclaim interfaces (if another
1281+ app has them claimed) and the re-attachment of
1282+ the driver will sometimes change /dev entry names.
1283+ It is for these reasons that this section is
1284+ #if 0. For composite devices, use the interface
1285+ field in the hid_device_info struct to distinguish
1286+ between interfaces. */
1287+ int detached = 0;
1288+ unsigned char data[256];
1289+
1290+ /* Usage Page and Usage */
1291+ res = libusb_kernel_driver_active(handle, interface_num);
1292+ if (res == 1) {
1293+ res = libusb_detach_kernel_driver(handle, interface_num);
1294+ if (res < 0)
1295+ LOG("Couldn't detach kernel driver, even though a kernel driver was attached.");
1296+ else
1297+ detached = 1;
1298+ }
1299+ res = libusb_claim_interface(handle, interface_num);
1300+ if (res >= 0) {
1301+ /* Get the HID Report Descriptor. */
1302+ res = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_INTERFACE, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_REPORT << 8)|interface_num, 0, data, sizeof(data), 5000);
1303+ if (res >= 0) {
1304+ unsigned short page=0, usage=0;
1305+ /* Parse the usage and usage page
1306+ out of the report descriptor. */
1307+ get_usage(data, res, &page, &usage);
1308+ cur_dev->usage_page = page;
1309+ cur_dev->usage = usage;
1310+ }
1311+ else
1312+ LOG("libusb_control_transfer() for getting the HID report failed with %d\n", res);
1313+
1314+ /* Release the interface */
1315+ res = libusb_release_interface(handle, interface_num);
1316+ if (res < 0)
1317+ LOG("Can't release the interface.\n");
1318+ }
1319+ else
1320+ LOG("Can't claim interface %d\n", res);
1321+
1322+ /* Re-attach kernel driver if necessary. */
1323+ if (detached) {
1324+ res = libusb_attach_kernel_driver(handle, interface_num);
1325+ if (res < 0)
1326+ LOG("Couldn't re-attach kernel driver.\n");
1327+ }
1328+#endif /*******************/
1329+
1330+ libusb_close(handle);
1331+ }
1332+ /* VID/PID */
1333+ cur_dev->vendor_id = dev_vid;
1334+ cur_dev->product_id = dev_pid;
1335+
1336+ /* Release Number */
1337+ cur_dev->release_number = desc.bcdDevice;
1338+
1339+ /* Interface Number */
1340+ cur_dev->interface_number = interface_num;
1341+ }
1342+ }
1343+ } /* altsettings */
1344+ } /* interfaces */
1345+ libusb_free_config_descriptor(conf_desc);
1346+ }
1347+ }
1348+
1349+ libusb_free_device_list(devs, 1);
1350+
1351+ return root;
1352+}
1353+
1354+void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
1355+{
1356+ struct hid_device_info *d = devs;
1357+ while (d) {
1358+ struct hid_device_info *next = d->next;
1359+ free(d->path);
1360+ free(d->serial_number);
1361+ free(d->manufacturer_string);
1362+ free(d->product_string);
1363+ free(d);
1364+ d = next;
1365+ }
1366+}
1367+
1368+hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
1369+{
1370+ struct hid_device_info *devs, *cur_dev;
1371+ const char *path_to_open = NULL;
1372+ hid_device *handle = NULL;
1373+
1374+ devs = hid_enumerate(vendor_id, product_id);
1375+ cur_dev = devs;
1376+ while (cur_dev) {
1377+ if (cur_dev->vendor_id == vendor_id &&
1378+ cur_dev->product_id == product_id) {
1379+ if (serial_number) {
1380+ if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
1381+ path_to_open = cur_dev->path;
1382+ break;
1383+ }
1384+ }
1385+ else {
1386+ path_to_open = cur_dev->path;
1387+ break;
1388+ }
1389+ }
1390+ cur_dev = cur_dev->next;
1391+ }
1392+
1393+ if (path_to_open) {
1394+ /* Open the device */
1395+ handle = hid_open_path(path_to_open);
1396+ }
1397+
1398+ hid_free_enumeration(devs);
1399+
1400+ return handle;
1401+}
1402+
1403+static void read_callback(struct libusb_transfer *transfer)
1404+{
1405+ hid_device *dev = transfer->user_data;
1406+
1407+ if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
1408+
1409+ struct input_report *rpt = malloc(sizeof(*rpt));
1410+ rpt->data = malloc(transfer->actual_length);
1411+ memcpy(rpt->data, transfer->buffer, transfer->actual_length);
1412+ rpt->len = transfer->actual_length;
1413+ rpt->next = NULL;
1414+
1415+ pthread_mutex_lock(&dev->mutex);
1416+
1417+ /* Attach the new report object to the end of the list. */
1418+ if (dev->input_reports == NULL) {
1419+ /* The list is empty. Put it at the root. */
1420+ dev->input_reports = rpt;
1421+ pthread_cond_signal(&dev->condition);
1422+ }
1423+ else {
1424+ /* Find the end of the list and attach. */
1425+ struct input_report *cur = dev->input_reports;
1426+ int num_queued = 0;
1427+ while (cur->next != NULL) {
1428+ cur = cur->next;
1429+ num_queued++;
1430+ }
1431+ cur->next = rpt;
1432+
1433+ /* Pop one off if we've reached 30 in the queue. This
1434+ way we don't grow forever if the user never reads
1435+ anything from the device. */
1436+ if (num_queued > 30) {
1437+ return_data(dev, NULL, 0);
1438+ }
1439+ }
1440+ pthread_mutex_unlock(&dev->mutex);
1441+ }
1442+ else if (transfer->status == LIBUSB_TRANSFER_CANCELLED) {
1443+ dev->shutdown_thread = 1;
1444+ return;
1445+ }
1446+ else if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
1447+ dev->shutdown_thread = 1;
1448+ return;
1449+ }
1450+ else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
1451+ //LOG("Timeout (normal)\n");
1452+ }
1453+ else {
1454+ LOG("Unknown transfer code: %d\n", transfer->status);
1455+ }
1456+
1457+ /* Re-submit the transfer object. */
1458+ libusb_submit_transfer(transfer);
1459+}
1460+
1461+
1462+static void *read_thread(void *param)
1463+{
1464+ hid_device *dev = param;
1465+ unsigned char *buf;
1466+ const size_t length = dev->input_ep_max_packet_size;
1467+
1468+ /* Set up the transfer object. */
1469+ buf = malloc(length);
1470+ dev->transfer = libusb_alloc_transfer(0);
1471+ libusb_fill_interrupt_transfer(dev->transfer,
1472+ dev->device_handle,
1473+ dev->input_endpoint,
1474+ buf,
1475+ length,
1476+ read_callback,
1477+ dev,
1478+ 5000/*timeout*/);
1479+
1480+ /* Make the first submission. Further submissions are made
1481+ from inside read_callback() */
1482+ libusb_submit_transfer(dev->transfer);
1483+
1484+ // Notify the main thread that the read thread is up and running.
1485+ pthread_barrier_wait(&dev->barrier);
1486+
1487+ /* Handle all the events. */
1488+ while (!dev->shutdown_thread) {
1489+ int res;
1490+ res = libusb_handle_events(NULL);
1491+ if (res < 0) {
1492+ /* There was an error. Break out of this loop. */
1493+ break;
1494+ }
1495+ }
1496+
1497+ /* Cancel any transfer that may be pending. This call will fail
1498+ if no transfers are pending, but that's OK. */
1499+ if (libusb_cancel_transfer(dev->transfer) == 0) {
1500+ /* The transfer was cancelled, so wait for its completion. */
1501+ libusb_handle_events(NULL);
1502+ }
1503+
1504+ /* Now that the read thread is stopping, Wake any threads which are
1505+ waiting on data (in hid_read_timeout()). Do this under a mutex to
1506+ make sure that a thread which is about to go to sleep waiting on
1507+ the condition acutally will go to sleep before the condition is
1508+ signaled. */
1509+ pthread_mutex_lock(&dev->mutex);
1510+ pthread_cond_broadcast(&dev->condition);
1511+ pthread_mutex_unlock(&dev->mutex);
1512+
1513+ /* The dev->transfer->buffer and dev->transfer objects are cleaned up
1514+ in hid_close(). They are not cleaned up here because this thread
1515+ could end either due to a disconnect or due to a user
1516+ call to hid_close(). In both cases the objects can be safely
1517+ cleaned up after the call to pthread_join() (in hid_close()), but
1518+ since hid_close() calls libusb_cancel_transfer(), on these objects,
1519+ they can not be cleaned up here. */
1520+
1521+ return NULL;
1522+}
1523+
1524+
1525+hid_device * HID_API_EXPORT hid_open_path(const char *path)
1526+{
1527+ hid_device *dev = NULL;
1528+
1529+ dev = new_hid_device();
1530+
1531+ libusb_device **devs;
1532+ libusb_device *usb_dev;
1533+ ssize_t num_devs;
1534+ int res;
1535+ int d = 0;
1536+ int good_open = 0;
1537+
1538+ setlocale(LC_ALL,"");
1539+
1540+ if (!initialized)
1541+ hid_init();
1542+
1543+ num_devs = libusb_get_device_list(NULL, &devs);
1544+ while ((usb_dev = devs[d++]) != NULL) {
1545+ struct libusb_device_descriptor desc;
1546+ struct libusb_config_descriptor *conf_desc = NULL;
1547+ int i,j,k;
1548+ libusb_get_device_descriptor(usb_dev, &desc);
1549+
1550+ if (libusb_get_active_config_descriptor(usb_dev, &conf_desc) < 0)
1551+ continue;
1552+ for (j = 0; j < conf_desc->bNumInterfaces; j++) {
1553+ const struct libusb_interface *intf = &conf_desc->interface[j];
1554+ for (k = 0; k < intf->num_altsetting; k++) {
1555+ const struct libusb_interface_descriptor *intf_desc;
1556+ intf_desc = &intf->altsetting[k];
1557+ if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID) {
1558+ char *dev_path = make_path(usb_dev, intf_desc->bInterfaceNumber);
1559+ if (!strcmp(dev_path, path)) {
1560+ /* Matched Paths. Open this device */
1561+
1562+ // OPEN HERE //
1563+ res = libusb_open(usb_dev, &dev->device_handle);
1564+ if (res < 0) {
1565+ LOG("can't open device\n");
1566+ free(dev_path);
1567+ break;
1568+ }
1569+ good_open = 1;
1570+
1571+ /* Detach the kernel driver, but only if the
1572+ device is managed by the kernel */
1573+ if (libusb_kernel_driver_active(dev->device_handle, intf_desc->bInterfaceNumber) == 1) {
1574+ res = libusb_detach_kernel_driver(dev->device_handle, intf_desc->bInterfaceNumber);
1575+ if (res < 0) {
1576+ libusb_close(dev->device_handle);
1577+ LOG("Unable to detach Kernel Driver\n");
1578+ free(dev_path);
1579+ good_open = 0;
1580+ break;
1581+ }
1582+ }
1583+
1584+ res = libusb_claim_interface(dev->device_handle, intf_desc->bInterfaceNumber);
1585+ if (res < 0) {
1586+ LOG("can't claim interface %d: %d\n", intf_desc->bInterfaceNumber, res);
1587+ free(dev_path);
1588+ libusb_close(dev->device_handle);
1589+ good_open = 0;
1590+ break;
1591+ }
1592+
1593+ /* Store off the string descriptor indexes */
1594+ dev->manufacturer_index = desc.iManufacturer;
1595+ dev->product_index = desc.iProduct;
1596+ dev->serial_index = desc.iSerialNumber;
1597+
1598+ /* Store off the interface number */
1599+ dev->interface = intf_desc->bInterfaceNumber;
1600+
1601+ /* Find the INPUT and OUTPUT endpoints. An
1602+ OUTPUT endpoint is not required. */
1603+ for (i = 0; i < intf_desc->bNumEndpoints; i++) {
1604+ const struct libusb_endpoint_descriptor *ep
1605+ = &intf_desc->endpoint[i];
1606+
1607+ /* Determine the type and direction of this
1608+ endpoint. */
1609+ int is_interrupt =
1610+ (ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK)
1611+ == LIBUSB_TRANSFER_TYPE_INTERRUPT;
1612+ int is_output =
1613+ (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
1614+ == LIBUSB_ENDPOINT_OUT;
1615+ int is_input =
1616+ (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
1617+ == LIBUSB_ENDPOINT_IN;
1618+
1619+ /* Decide whether to use it for intput or output. */
1620+ if (dev->input_endpoint == 0 &&
1621+ is_interrupt && is_input) {
1622+ /* Use this endpoint for INPUT */
1623+ dev->input_endpoint = ep->bEndpointAddress;
1624+ dev->input_ep_max_packet_size = ep->wMaxPacketSize;
1625+ }
1626+ if (dev->output_endpoint == 0 &&
1627+ is_interrupt && is_output) {
1628+ /* Use this endpoint for OUTPUT */
1629+ dev->output_endpoint = ep->bEndpointAddress;
1630+ }
1631+ }
1632+
1633+ pthread_create(&dev->thread, NULL, read_thread, dev);
1634+
1635+ // Wait here for the read thread to be initialized.
1636+ pthread_barrier_wait(&dev->barrier);
1637+
1638+ }
1639+ free(dev_path);
1640+ }
1641+ }
1642+ }
1643+ libusb_free_config_descriptor(conf_desc);
1644+
1645+ }
1646+
1647+ libusb_free_device_list(devs, 1);
1648+
1649+ // If we have a good handle, return it.
1650+ if (good_open) {
1651+ return dev;
1652+ }
1653+ else {
1654+ // Unable to open any devices.
1655+ free_hid_device(dev);
1656+ return NULL;
1657+ }
1658+}
1659+
1660+
1661+int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
1662+{
1663+ int res;
1664+ int report_number = data[0];
1665+ int skipped_report_id = 0;
1666+
1667+ if (report_number == 0x0) {
1668+ data++;
1669+ length--;
1670+ skipped_report_id = 1;
1671+ }
1672+
1673+
1674+ if (dev->output_endpoint <= 0) {
1675+ /* No interrput out endpoint. Use the Control Endpoint */
1676+ res = libusb_control_transfer(dev->device_handle,
1677+ LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT,
1678+ 0x09/*HID Set_Report*/,
1679+ (2/*HID output*/ << 8) | report_number,
1680+ dev->interface,
1681+ (unsigned char *)data, length,
1682+ 1000/*timeout millis*/);
1683+
1684+ if (res < 0)
1685+ return -1;
1686+
1687+ if (skipped_report_id)
1688+ length++;
1689+
1690+ return length;
1691+ }
1692+ else {
1693+ /* Use the interrupt out endpoint */
1694+ int actual_length;
1695+ res = libusb_interrupt_transfer(dev->device_handle,
1696+ dev->output_endpoint,
1697+ (unsigned char*)data,
1698+ length,
1699+ &actual_length, 1000);
1700+
1701+ if (res < 0)
1702+ return -1;
1703+
1704+ if (skipped_report_id)
1705+ actual_length++;
1706+
1707+ return actual_length;
1708+ }
1709+}
1710+
1711+/* Helper function, to simplify hid_read().
1712+ This should be called with dev->mutex locked. */
1713+static int return_data(hid_device *dev, unsigned char *data, size_t length)
1714+{
1715+ /* Copy the data out of the linked list item (rpt) into the
1716+ return buffer (data), and delete the liked list item. */
1717+ struct input_report *rpt = dev->input_reports;
1718+ size_t len = (length < rpt->len)? length: rpt->len;
1719+ if (len > 0)
1720+ memcpy(data, rpt->data, len);
1721+ dev->input_reports = rpt->next;
1722+ free(rpt->data);
1723+ free(rpt);
1724+ return len;
1725+}
1726+
1727+static void cleanup_mutex(void *param)
1728+{
1729+ hid_device *dev = param;
1730+ pthread_mutex_unlock(&dev->mutex);
1731+}
1732+
1733+
1734+int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
1735+{
1736+ int bytes_read = -1;
1737+
1738+#if 0
1739+ int transferred;
1740+ int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
1741+ LOG("transferred: %d\n", transferred);
1742+ return transferred;
1743+#endif
1744+
1745+ pthread_mutex_lock(&dev->mutex);
1746+ pthread_cleanup_push(&cleanup_mutex, dev);
1747+
1748+ /* There's an input report queued up. Return it. */
1749+ if (dev->input_reports) {
1750+ /* Return the first one */
1751+ bytes_read = return_data(dev, data, length);
1752+ goto ret;
1753+ }
1754+
1755+ if (dev->shutdown_thread) {
1756+ /* This means the device has been disconnected.
1757+ An error code of -1 should be returned. */
1758+ bytes_read = -1;
1759+ goto ret;
1760+ }
1761+
1762+ if (milliseconds == -1) {
1763+ /* Blocking */
1764+ while (!dev->input_reports && !dev->shutdown_thread) {
1765+ pthread_cond_wait(&dev->condition, &dev->mutex);
1766+ }
1767+ if (dev->input_reports) {
1768+ bytes_read = return_data(dev, data, length);
1769+ }
1770+ }
1771+ else if (milliseconds > 0) {
1772+ /* Non-blocking, but called with timeout. */
1773+ int res;
1774+ struct timespec ts;
1775+ clock_gettime(CLOCK_REALTIME, &ts);
1776+ ts.tv_sec += milliseconds / 1000;
1777+ ts.tv_nsec += (milliseconds % 1000) * 1000000;
1778+ if (ts.tv_nsec >= 1000000000L) {
1779+ ts.tv_sec++;
1780+ ts.tv_nsec -= 1000000000L;
1781+ }
1782+
1783+ while (!dev->input_reports && !dev->shutdown_thread) {
1784+ res = pthread_cond_timedwait(&dev->condition, &dev->mutex, &ts);
1785+ if (res == 0) {
1786+ if (dev->input_reports) {
1787+ bytes_read = return_data(dev, data, length);
1788+ break;
1789+ }
1790+
1791+ /* If we're here, there was a spurious wake up
1792+ or the read thread was shutdown. Run the
1793+ loop again (ie: don't break). */
1794+ }
1795+ else if (res == ETIMEDOUT) {
1796+ /* Timed out. */
1797+ bytes_read = 0;
1798+ break;
1799+ }
1800+ else {
1801+ /* Error. */
1802+ bytes_read = -1;
1803+ break;
1804+ }
1805+ }
1806+ }
1807+ else {
1808+ /* Purely non-blocking */
1809+ bytes_read = 0;
1810+ }
1811+
1812+ret:
1813+ pthread_mutex_unlock(&dev->mutex);
1814+ pthread_cleanup_pop(0);
1815+
1816+ return bytes_read;
1817+}
1818+
1819+int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
1820+{
1821+ return hid_read_timeout(dev, data, length, dev->blocking ? -1 : 0);
1822+}
1823+
1824+int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
1825+{
1826+ dev->blocking = !nonblock;
1827+
1828+ return 0;
1829+}
1830+
1831+
1832+int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
1833+{
1834+ int res = -1;
1835+ int skipped_report_id = 0;
1836+ int report_number = data[0];
1837+
1838+ if (report_number == 0x0) {
1839+ data++;
1840+ length--;
1841+ skipped_report_id = 1;
1842+ }
1843+
1844+ res = libusb_control_transfer(dev->device_handle,
1845+ LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT,
1846+ 0x09/*HID set_report*/,
1847+ (3/*HID feature*/ << 8) | report_number,
1848+ dev->interface,
1849+ (unsigned char *)data, length,
1850+ 1000/*timeout millis*/);
1851+
1852+ if (res < 0)
1853+ return -1;
1854+
1855+ /* Account for the report ID */
1856+ if (skipped_report_id)
1857+ length++;
1858+
1859+ return length;
1860+}
1861+
1862+int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
1863+{
1864+ int res = -1;
1865+ int skipped_report_id = 0;
1866+ int report_number = data[0];
1867+
1868+ if (report_number == 0x0) {
1869+ /* Offset the return buffer by 1, so that the report ID
1870+ will remain in byte 0. */
1871+ data++;
1872+ length--;
1873+ skipped_report_id = 1;
1874+ }
1875+ res = libusb_control_transfer(dev->device_handle,
1876+ LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_IN,
1877+ 0x01/*HID get_report*/,
1878+ (3/*HID feature*/ << 8) | report_number,
1879+ dev->interface,
1880+ (unsigned char *)data, length,
1881+ 1000/*timeout millis*/);
1882+
1883+ if (res < 0)
1884+ return -1;
1885+
1886+ if (skipped_report_id)
1887+ res++;
1888+
1889+ return res;
1890+}
1891+
1892+
1893+void HID_API_EXPORT hid_close(hid_device *dev)
1894+{
1895+ if (!dev)
1896+ return;
1897+
1898+ /* Cause read_thread() to stop. */
1899+ dev->shutdown_thread = 1;
1900+ libusb_cancel_transfer(dev->transfer);
1901+
1902+ /* Wait for read_thread() to end. */
1903+ pthread_join(dev->thread, NULL);
1904+
1905+ /* Clean up the Transfer objects allocated in read_thread(). */
1906+ free(dev->transfer->buffer);
1907+ libusb_free_transfer(dev->transfer);
1908+
1909+ /* release the interface */
1910+ libusb_release_interface(dev->device_handle, dev->interface);
1911+
1912+ /* Close the handle */
1913+ libusb_close(dev->device_handle);
1914+
1915+ /* Clear out the queue of received reports. */
1916+ pthread_mutex_lock(&dev->mutex);
1917+ while (dev->input_reports) {
1918+ return_data(dev, NULL, 0);
1919+ }
1920+ pthread_mutex_unlock(&dev->mutex);
1921+
1922+ free_hid_device(dev);
1923+}
1924+
1925+
1926+int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
1927+{
1928+ return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
1929+}
1930+
1931+int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
1932+{
1933+ return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
1934+}
1935+
1936+int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
1937+{
1938+ return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
1939+}
1940+
1941+int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
1942+{
1943+ wchar_t *str;
1944+
1945+ str = get_usb_string(dev->device_handle, string_index);
1946+ if (str) {
1947+ wcsncpy(string, str, maxlen);
1948+ string[maxlen-1] = L'\0';
1949+ free(str);
1950+ return 0;
1951+ }
1952+ else
1953+ return -1;
1954+}
1955+
1956+
1957+HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
1958+{
1959+ return NULL;
1960+}
1961+
1962+
1963+struct lang_map_entry {
1964+ const char *name;
1965+ const char *string_code;
1966+ uint16_t usb_code;
1967+};
1968+
1969+#define LANG(name,code,usb_code) { name, code, usb_code }
1970+static struct lang_map_entry lang_map[] = {
1971+ LANG("Afrikaans", "af", 0x0436),
1972+ LANG("Albanian", "sq", 0x041C),
1973+ LANG("Arabic - United Arab Emirates", "ar_ae", 0x3801),
1974+ LANG("Arabic - Bahrain", "ar_bh", 0x3C01),
1975+ LANG("Arabic - Algeria", "ar_dz", 0x1401),
1976+ LANG("Arabic - Egypt", "ar_eg", 0x0C01),
1977+ LANG("Arabic - Iraq", "ar_iq", 0x0801),
1978+ LANG("Arabic - Jordan", "ar_jo", 0x2C01),
1979+ LANG("Arabic - Kuwait", "ar_kw", 0x3401),
1980+ LANG("Arabic - Lebanon", "ar_lb", 0x3001),
1981+ LANG("Arabic - Libya", "ar_ly", 0x1001),
1982+ LANG("Arabic - Morocco", "ar_ma", 0x1801),
1983+ LANG("Arabic - Oman", "ar_om", 0x2001),
1984+ LANG("Arabic - Qatar", "ar_qa", 0x4001),
1985+ LANG("Arabic - Saudi Arabia", "ar_sa", 0x0401),
1986+ LANG("Arabic - Syria", "ar_sy", 0x2801),
1987+ LANG("Arabic - Tunisia", "ar_tn", 0x1C01),
1988+ LANG("Arabic - Yemen", "ar_ye", 0x2401),
1989+ LANG("Armenian", "hy", 0x042B),
1990+ LANG("Azeri - Latin", "az_az", 0x042C),
1991+ LANG("Azeri - Cyrillic", "az_az", 0x082C),
1992+ LANG("Basque", "eu", 0x042D),
1993+ LANG("Belarusian", "be", 0x0423),
1994+ LANG("Bulgarian", "bg", 0x0402),
1995+ LANG("Catalan", "ca", 0x0403),
1996+ LANG("Chinese - China", "zh_cn", 0x0804),
1997+ LANG("Chinese - Hong Kong SAR", "zh_hk", 0x0C04),
1998+ LANG("Chinese - Macau SAR", "zh_mo", 0x1404),
1999+ LANG("Chinese - Singapore", "zh_sg", 0x1004),
2000+ LANG("Chinese - Taiwan", "zh_tw", 0x0404),
2001+ LANG("Croatian", "hr", 0x041A),
2002+ LANG("Czech", "cs", 0x0405),
2003+ LANG("Danish", "da", 0x0406),
2004+ LANG("Dutch - Netherlands", "nl_nl", 0x0413),
2005+ LANG("Dutch - Belgium", "nl_be", 0x0813),
2006+ LANG("English - Australia", "en_au", 0x0C09),
2007+ LANG("English - Belize", "en_bz", 0x2809),
2008+ LANG("English - Canada", "en_ca", 0x1009),
2009+ LANG("English - Caribbean", "en_cb", 0x2409),
2010+ LANG("English - Ireland", "en_ie", 0x1809),
2011+ LANG("English - Jamaica", "en_jm", 0x2009),
2012+ LANG("English - New Zealand", "en_nz", 0x1409),
2013+ LANG("English - Phillippines", "en_ph", 0x3409),
2014+ LANG("English - Southern Africa", "en_za", 0x1C09),
2015+ LANG("English - Trinidad", "en_tt", 0x2C09),
2016+ LANG("English - Great Britain", "en_gb", 0x0809),
2017+ LANG("English - United States", "en_us", 0x0409),
2018+ LANG("Estonian", "et", 0x0425),
2019+ LANG("Farsi", "fa", 0x0429),
2020+ LANG("Finnish", "fi", 0x040B),
2021+ LANG("Faroese", "fo", 0x0438),
2022+ LANG("French - France", "fr_fr", 0x040C),
2023+ LANG("French - Belgium", "fr_be", 0x080C),
2024+ LANG("French - Canada", "fr_ca", 0x0C0C),
2025+ LANG("French - Luxembourg", "fr_lu", 0x140C),
2026+ LANG("French - Switzerland", "fr_ch", 0x100C),
2027+ LANG("Gaelic - Ireland", "gd_ie", 0x083C),
2028+ LANG("Gaelic - Scotland", "gd", 0x043C),
2029+ LANG("German - Germany", "de_de", 0x0407),
2030+ LANG("German - Austria", "de_at", 0x0C07),
2031+ LANG("German - Liechtenstein", "de_li", 0x1407),
2032+ LANG("German - Luxembourg", "de_lu", 0x1007),
2033+ LANG("German - Switzerland", "de_ch", 0x0807),
2034+ LANG("Greek", "el", 0x0408),
2035+ LANG("Hebrew", "he", 0x040D),
2036+ LANG("Hindi", "hi", 0x0439),
2037+ LANG("Hungarian", "hu", 0x040E),
2038+ LANG("Icelandic", "is", 0x040F),
2039+ LANG("Indonesian", "id", 0x0421),
2040+ LANG("Italian - Italy", "it_it", 0x0410),
2041+ LANG("Italian - Switzerland", "it_ch", 0x0810),
2042+ LANG("Japanese", "ja", 0x0411),
2043+ LANG("Korean", "ko", 0x0412),
2044+ LANG("Latvian", "lv", 0x0426),
2045+ LANG("Lithuanian", "lt", 0x0427),
2046+ LANG("F.Y.R.O. Macedonia", "mk", 0x042F),
2047+ LANG("Malay - Malaysia", "ms_my", 0x043E),
2048+ LANG("Malay – Brunei", "ms_bn", 0x083E),
2049+ LANG("Maltese", "mt", 0x043A),
2050+ LANG("Marathi", "mr", 0x044E),
2051+ LANG("Norwegian - Bokml", "no_no", 0x0414),
2052+ LANG("Norwegian - Nynorsk", "no_no", 0x0814),
2053+ LANG("Polish", "pl", 0x0415),
2054+ LANG("Portuguese - Portugal", "pt_pt", 0x0816),
2055+ LANG("Portuguese - Brazil", "pt_br", 0x0416),
2056+ LANG("Raeto-Romance", "rm", 0x0417),
2057+ LANG("Romanian - Romania", "ro", 0x0418),
2058+ LANG("Romanian - Republic of Moldova", "ro_mo", 0x0818),
2059+ LANG("Russian", "ru", 0x0419),
2060+ LANG("Russian - Republic of Moldova", "ru_mo", 0x0819),
2061+ LANG("Sanskrit", "sa", 0x044F),
2062+ LANG("Serbian - Cyrillic", "sr_sp", 0x0C1A),
2063+ LANG("Serbian - Latin", "sr_sp", 0x081A),
2064+ LANG("Setsuana", "tn", 0x0432),
2065+ LANG("Slovenian", "sl", 0x0424),
2066+ LANG("Slovak", "sk", 0x041B),
2067+ LANG("Sorbian", "sb", 0x042E),
2068+ LANG("Spanish - Spain (Traditional)", "es_es", 0x040A),
2069+ LANG("Spanish - Argentina", "es_ar", 0x2C0A),
2070+ LANG("Spanish - Bolivia", "es_bo", 0x400A),
2071+ LANG("Spanish - Chile", "es_cl", 0x340A),
2072+ LANG("Spanish - Colombia", "es_co", 0x240A),
2073+ LANG("Spanish - Costa Rica", "es_cr", 0x140A),
2074+ LANG("Spanish - Dominican Republic", "es_do", 0x1C0A),
2075+ LANG("Spanish - Ecuador", "es_ec", 0x300A),
2076+ LANG("Spanish - Guatemala", "es_gt", 0x100A),
2077+ LANG("Spanish - Honduras", "es_hn", 0x480A),
2078+ LANG("Spanish - Mexico", "es_mx", 0x080A),
2079+ LANG("Spanish - Nicaragua", "es_ni", 0x4C0A),
2080+ LANG("Spanish - Panama", "es_pa", 0x180A),
2081+ LANG("Spanish - Peru", "es_pe", 0x280A),
2082+ LANG("Spanish - Puerto Rico", "es_pr", 0x500A),
2083+ LANG("Spanish - Paraguay", "es_py", 0x3C0A),
2084+ LANG("Spanish - El Salvador", "es_sv", 0x440A),
2085+ LANG("Spanish - Uruguay", "es_uy", 0x380A),
2086+ LANG("Spanish - Venezuela", "es_ve", 0x200A),
2087+ LANG("Southern Sotho", "st", 0x0430),
2088+ LANG("Swahili", "sw", 0x0441),
2089+ LANG("Swedish - Sweden", "sv_se", 0x041D),
2090+ LANG("Swedish - Finland", "sv_fi", 0x081D),
2091+ LANG("Tamil", "ta", 0x0449),
2092+ LANG("Tatar", "tt", 0X0444),
2093+ LANG("Thai", "th", 0x041E),
2094+ LANG("Turkish", "tr", 0x041F),
2095+ LANG("Tsonga", "ts", 0x0431),
2096+ LANG("Ukrainian", "uk", 0x0422),
2097+ LANG("Urdu", "ur", 0x0420),
2098+ LANG("Uzbek - Cyrillic", "uz_uz", 0x0843),
2099+ LANG("Uzbek – Latin", "uz_uz", 0x0443),
2100+ LANG("Vietnamese", "vi", 0x042A),
2101+ LANG("Xhosa", "xh", 0x0434),
2102+ LANG("Yiddish", "yi", 0x043D),
2103+ LANG("Zulu", "zu", 0x0435),
2104+ LANG(NULL, NULL, 0x0),
2105+};
2106+
2107+uint16_t get_usb_code_for_current_locale(void)
2108+{
2109+ char *locale;
2110+ char search_string[64];
2111+ char *ptr;
2112+
2113+ /* Get the current locale. */
2114+ locale = setlocale(0, NULL);
2115+ if (!locale)
2116+ return 0x0;
2117+
2118+ /* Make a copy of the current locale string. */
2119+ strncpy(search_string, locale, sizeof(search_string));
2120+ search_string[sizeof(search_string)-1] = '\0';
2121+
2122+ /* Chop off the encoding part, and make it lower case. */
2123+ ptr = search_string;
2124+ while (*ptr) {
2125+ *ptr = tolower(*ptr);
2126+ if (*ptr == '.') {
2127+ *ptr = '\0';
2128+ break;
2129+ }
2130+ ptr++;
2131+ }
2132+
2133+ /* Find the entry which matches the string code of our locale. */
2134+ struct lang_map_entry *lang = lang_map;
2135+ while (lang->string_code) {
2136+ if (!strcmp(lang->string_code, search_string)) {
2137+ return lang->usb_code;
2138+ }
2139+ lang++;
2140+ }
2141+
2142+ /* There was no match. Find with just the language only. */
2143+ /* Chop off the variant. Chop it off at the '_'. */
2144+ ptr = search_string;
2145+ while (*ptr) {
2146+ *ptr = tolower(*ptr);
2147+ if (*ptr == '_') {
2148+ *ptr = '\0';
2149+ break;
2150+ }
2151+ ptr++;
2152+ }
2153+
2154+#if 0 // TODO: Do we need this?
2155+ /* Find the entry which matches the string code of our language. */
2156+ lang = lang_map;
2157+ while (lang->string_code) {
2158+ if (!strcmp(lang->string_code, search_string)) {
2159+ return lang->usb_code;
2160+ }
2161+ lang++;
2162+ }
2163+#endif
2164+
2165+ /* Found nothing. */
2166+ return 0x0;
2167+}
2168+
2169+#ifdef __cplusplus
2170+}
2171+#endif
2172
2173=== added file 'mixxx/lib/hidapi-0.7.0/linux/hid.c'
2174--- mixxx/lib/hidapi-0.7.0/linux/hid.c 1970-01-01 00:00:00 +0000
2175+++ mixxx/lib/hidapi-0.7.0/linux/hid.c 2012-04-25 15:44:23 +0000
2176@@ -0,0 +1,595 @@
2177+/*******************************************************
2178+ HIDAPI - Multi-Platform library for
2179+ communication with HID devices.
2180+
2181+ Alan Ott
2182+ Signal 11 Software
2183+
2184+ 8/22/2009
2185+ Linux Version - 6/2/2009
2186+
2187+ Copyright 2009, All Rights Reserved.
2188+
2189+ At the discretion of the user of this library,
2190+ this software may be licensed under the terms of the
2191+ GNU Public License v3, a BSD-Style license, or the
2192+ original HIDAPI license as outlined in the LICENSE.txt,
2193+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
2194+ files located at the root of the source distribution.
2195+ These files may also be found in the public source
2196+ code repository located at:
2197+ http://github.com/signal11/hidapi .
2198+********************************************************/
2199+
2200+/* C */
2201+#include <stdio.h>
2202+#include <string.h>
2203+#include <stdlib.h>
2204+#include <locale.h>
2205+#include <errno.h>
2206+
2207+/* Unix */
2208+#include <unistd.h>
2209+#include <sys/types.h>
2210+#include <sys/stat.h>
2211+#include <sys/ioctl.h>
2212+#include <sys/utsname.h>
2213+#include <fcntl.h>
2214+#include <poll.h>
2215+
2216+/* Linux */
2217+#include <linux/hidraw.h>
2218+#include <linux/version.h>
2219+#include <libudev.h>
2220+
2221+#include "hidapi.h"
2222+
2223+/* Definitions from linux/hidraw.h. Since these are new, some distros
2224+ may not have header files which contain them. */
2225+#ifndef HIDIOCSFEATURE
2226+#define HIDIOCSFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x06, len)
2227+#endif
2228+#ifndef HIDIOCGFEATURE
2229+#define HIDIOCGFEATURE(len) _IOC(_IOC_WRITE|_IOC_READ, 'H', 0x07, len)
2230+#endif
2231+
2232+struct hid_device_ {
2233+ int device_handle;
2234+ int blocking;
2235+ int uses_numbered_reports;
2236+};
2237+
2238+
2239+static __u32 kernel_version = 0;
2240+
2241+hid_device *new_hid_device()
2242+{
2243+ hid_device *dev = calloc(1, sizeof(hid_device));
2244+ dev->device_handle = -1;
2245+ dev->blocking = 1;
2246+ dev->uses_numbered_reports = 0;
2247+
2248+ return dev;
2249+}
2250+
2251+static void register_error(hid_device *device, const char *op)
2252+{
2253+
2254+}
2255+
2256+/* Get an attribute value from a udev_device and return it as a whar_t
2257+ string. The returned string must be freed with free() when done.*/
2258+static wchar_t *copy_udev_string(struct udev_device *dev, const char *udev_name)
2259+{
2260+ const char *str;
2261+ wchar_t *ret = NULL;
2262+ str = udev_device_get_sysattr_value(dev, udev_name);
2263+ if (str) {
2264+ /* Convert the string from UTF-8 to wchar_t */
2265+ size_t wlen = mbstowcs(NULL, str, 0);
2266+ ret = calloc(wlen+1, sizeof(wchar_t));
2267+ mbstowcs(ret, str, wlen+1);
2268+ ret[wlen] = 0x0000;
2269+ }
2270+
2271+ return ret;
2272+}
2273+
2274+/* uses_numbered_reports() returns 1 if report_descriptor describes a device
2275+ which contains numbered reports. */
2276+static int uses_numbered_reports(__u8 *report_descriptor, __u32 size) {
2277+ int i = 0;
2278+ int size_code;
2279+ int data_len, key_size;
2280+
2281+ while (i < size) {
2282+ int key = report_descriptor[i];
2283+
2284+ /* Check for the Report ID key */
2285+ if (key == 0x85/*Report ID*/) {
2286+ /* This device has a Report ID, which means it uses
2287+ numbered reports. */
2288+ return 1;
2289+ }
2290+
2291+ //printf("key: %02hhx\n", key);
2292+
2293+ if ((key & 0xf0) == 0xf0) {
2294+ /* This is a Long Item. The next byte contains the
2295+ length of the data section (value) for this key.
2296+ See the HID specification, version 1.11, section
2297+ 6.2.2.3, titled "Long Items." */
2298+ if (i+1 < size)
2299+ data_len = report_descriptor[i+1];
2300+ else
2301+ data_len = 0; /* malformed report */
2302+ key_size = 3;
2303+ }
2304+ else {
2305+ /* This is a Short Item. The bottom two bits of the
2306+ key contain the size code for the data section
2307+ (value) for this key. Refer to the HID
2308+ specification, version 1.11, section 6.2.2.2,
2309+ titled "Short Items." */
2310+ size_code = key & 0x3;
2311+ switch (size_code) {
2312+ case 0:
2313+ case 1:
2314+ case 2:
2315+ data_len = size_code;
2316+ break;
2317+ case 3:
2318+ data_len = 4;
2319+ break;
2320+ default:
2321+ /* Can't ever happen since size_code is & 0x3 */
2322+ data_len = 0;
2323+ break;
2324+ };
2325+ key_size = 1;
2326+ }
2327+
2328+ /* Skip over this key and it's associated data */
2329+ i += data_len + key_size;
2330+ }
2331+
2332+ /* Didn't find a Report ID key. Device doesn't use numbered reports. */
2333+ return 0;
2334+}
2335+
2336+static int get_device_string(hid_device *dev, const char *key, wchar_t *string, size_t maxlen)
2337+{
2338+ struct udev *udev;
2339+ struct udev_device *udev_dev, *parent;
2340+ struct stat s;
2341+ int ret = -1;
2342+
2343+ setlocale(LC_ALL,"");
2344+
2345+ /* Create the udev object */
2346+ udev = udev_new();
2347+ if (!udev) {
2348+ printf("Can't create udev\n");
2349+ return -1;
2350+ }
2351+
2352+ /* Get the dev_t (major/minor numbers) from the file handle. */
2353+ fstat(dev->device_handle, &s);
2354+ /* Open a udev device from the dev_t. 'c' means character device. */
2355+ udev_dev = udev_device_new_from_devnum(udev, 'c', s.st_rdev);
2356+ if (udev_dev) {
2357+ const char *str;
2358+ /* Find the parent USB Device */
2359+ parent = udev_device_get_parent_with_subsystem_devtype(
2360+ udev_dev,
2361+ "usb",
2362+ "usb_device");
2363+ if (parent) {
2364+ str = udev_device_get_sysattr_value(parent, key);
2365+ if (str) {
2366+ /* Convert the string from UTF-8 to wchar_t */
2367+ ret = mbstowcs(string, str, maxlen);
2368+ goto end;
2369+ }
2370+ }
2371+ }
2372+
2373+end:
2374+ udev_device_unref(udev_dev);
2375+ // parent doesn't need to be (and can't be) unref'd.
2376+ // I'm not sure why, but it'll throw double-free() errors.
2377+ udev_unref(udev);
2378+
2379+ return ret;
2380+}
2381+
2382+int HID_API_EXPORT hid_init(void)
2383+{
2384+ /* Nothing to do for this in the Linux/hidraw implementation. */
2385+ return 0;
2386+}
2387+
2388+int HID_API_EXPORT hid_exit(void)
2389+{
2390+ /* Nothing to do for this in the Linux/hidraw implementation. */
2391+ return 0;
2392+}
2393+
2394+struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
2395+{
2396+ struct udev *udev;
2397+ struct udev_enumerate *enumerate;
2398+ struct udev_list_entry *devices, *dev_list_entry;
2399+
2400+ struct hid_device_info *root = NULL; // return object
2401+ struct hid_device_info *cur_dev = NULL;
2402+
2403+ setlocale(LC_ALL,"");
2404+
2405+ /* Create the udev object */
2406+ udev = udev_new();
2407+ if (!udev) {
2408+ printf("Can't create udev\n");
2409+ return NULL;
2410+ }
2411+
2412+ /* Create a list of the devices in the 'hidraw' subsystem. */
2413+ enumerate = udev_enumerate_new(udev);
2414+ udev_enumerate_add_match_subsystem(enumerate, "hidraw");
2415+ udev_enumerate_scan_devices(enumerate);
2416+ devices = udev_enumerate_get_list_entry(enumerate);
2417+ /* For each item, see if it matches the vid/pid, and if so
2418+ create a udev_device record for it */
2419+ udev_list_entry_foreach(dev_list_entry, devices) {
2420+ const char *sysfs_path;
2421+ const char *dev_path;
2422+ const char *str;
2423+ struct udev_device *hid_dev; // The device's HID udev node.
2424+ struct udev_device *dev; // The actual hardware device.
2425+ struct udev_device *intf_dev; // The device's interface (in the USB sense).
2426+ unsigned short dev_vid;
2427+ unsigned short dev_pid;
2428+
2429+ /* Get the filename of the /sys entry for the device
2430+ and create a udev_device object (dev) representing it */
2431+ sysfs_path = udev_list_entry_get_name(dev_list_entry);
2432+ hid_dev = udev_device_new_from_syspath(udev, sysfs_path);
2433+ dev_path = udev_device_get_devnode(hid_dev);
2434+
2435+ /* The device pointed to by hid_dev contains information about
2436+ the hidraw device. In order to get information about the
2437+ USB device, get the parent device with the
2438+ subsystem/devtype pair of "usb"/"usb_device". This will
2439+ be several levels up the tree, but the function will find
2440+ it.*/
2441+ dev = udev_device_get_parent_with_subsystem_devtype(
2442+ hid_dev,
2443+ "usb",
2444+ "usb_device");
2445+ if (!dev) {
2446+ /* Unable to find parent usb device. */
2447+ goto next;
2448+ }
2449+
2450+ /* Get the VID/PID of the device */
2451+ str = udev_device_get_sysattr_value(dev,"idVendor");
2452+ dev_vid = (str)? strtol(str, NULL, 16): 0x0;
2453+ str = udev_device_get_sysattr_value(dev, "idProduct");
2454+ dev_pid = (str)? strtol(str, NULL, 16): 0x0;
2455+
2456+ /* Check the VID/PID against the arguments */
2457+ if ((vendor_id == 0x0 && product_id == 0x0) ||
2458+ (vendor_id == dev_vid && product_id == dev_pid)) {
2459+ struct hid_device_info *tmp;
2460+ size_t len;
2461+
2462+ /* VID/PID match. Create the record. */
2463+ tmp = malloc(sizeof(struct hid_device_info));
2464+ if (cur_dev) {
2465+ cur_dev->next = tmp;
2466+ }
2467+ else {
2468+ root = tmp;
2469+ }
2470+ cur_dev = tmp;
2471+
2472+ /* Fill out the record */
2473+ cur_dev->next = NULL;
2474+ str = dev_path;
2475+ if (str) {
2476+ len = strlen(str);
2477+ cur_dev->path = calloc(len+1, sizeof(char));
2478+ strncpy(cur_dev->path, str, len+1);
2479+ cur_dev->path[len] = '\0';
2480+ }
2481+ else
2482+ cur_dev->path = NULL;
2483+
2484+ /* Serial Number */
2485+ cur_dev->serial_number
2486+ = copy_udev_string(dev, "serial");
2487+
2488+ /* Manufacturer and Product strings */
2489+ cur_dev->manufacturer_string
2490+ = copy_udev_string(dev, "manufacturer");
2491+ cur_dev->product_string
2492+ = copy_udev_string(dev, "product");
2493+
2494+ /* VID/PID */
2495+ cur_dev->vendor_id = dev_vid;
2496+ cur_dev->product_id = dev_pid;
2497+
2498+ /* Release Number */
2499+ str = udev_device_get_sysattr_value(dev, "bcdDevice");
2500+ cur_dev->release_number = (str)? strtol(str, NULL, 16): 0x0;
2501+
2502+ /* Interface Number */
2503+ cur_dev->interface_number = -1;
2504+ /* Get a handle to the interface's udev node. */
2505+ intf_dev = udev_device_get_parent_with_subsystem_devtype(
2506+ hid_dev,
2507+ "usb",
2508+ "usb_interface");
2509+ if (intf_dev) {
2510+ str = udev_device_get_sysattr_value(intf_dev, "bInterfaceNumber");
2511+ cur_dev->interface_number = (str)? strtol(str, NULL, 16): -1;
2512+ }
2513+ }
2514+ else
2515+ goto next;
2516+
2517+ next:
2518+ udev_device_unref(hid_dev);
2519+ /* dev and intf_dev don't need to be (and can't be)
2520+ unref()d. It will cause a double-free() error. I'm not
2521+ sure why. */
2522+ }
2523+ /* Free the enumerator and udev objects. */
2524+ udev_enumerate_unref(enumerate);
2525+ udev_unref(udev);
2526+
2527+ return root;
2528+}
2529+
2530+void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
2531+{
2532+ struct hid_device_info *d = devs;
2533+ while (d) {
2534+ struct hid_device_info *next = d->next;
2535+ free(d->path);
2536+ free(d->serial_number);
2537+ free(d->manufacturer_string);
2538+ free(d->product_string);
2539+ free(d);
2540+ d = next;
2541+ }
2542+}
2543+
2544+hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
2545+{
2546+ struct hid_device_info *devs, *cur_dev;
2547+ const char *path_to_open = NULL;
2548+ hid_device *handle = NULL;
2549+
2550+ devs = hid_enumerate(vendor_id, product_id);
2551+ cur_dev = devs;
2552+ while (cur_dev) {
2553+ if (cur_dev->vendor_id == vendor_id &&
2554+ cur_dev->product_id == product_id) {
2555+ if (serial_number) {
2556+ if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
2557+ path_to_open = cur_dev->path;
2558+ break;
2559+ }
2560+ }
2561+ else {
2562+ path_to_open = cur_dev->path;
2563+ break;
2564+ }
2565+ }
2566+ cur_dev = cur_dev->next;
2567+ }
2568+
2569+ if (path_to_open) {
2570+ /* Open the device */
2571+ handle = hid_open_path(path_to_open);
2572+ }
2573+
2574+ hid_free_enumeration(devs);
2575+
2576+ return handle;
2577+}
2578+
2579+hid_device * HID_API_EXPORT hid_open_path(const char *path)
2580+{
2581+ hid_device *dev = NULL;
2582+
2583+ dev = new_hid_device();
2584+
2585+ if (kernel_version == 0) {
2586+ struct utsname name;
2587+ int major, minor, release;
2588+ int ret;
2589+ uname(&name);
2590+ ret = sscanf(name.release, "%d.%d.%d", &major, &minor, &release);
2591+ if (ret == 3) {
2592+ kernel_version = major << 16 | minor << 8 | release;
2593+ //printf("Kernel Version: %d\n", kernel_version);
2594+ }
2595+ else {
2596+ printf("Couldn't sscanf() version string %s\n", name.release);
2597+ }
2598+ }
2599+
2600+ // OPEN HERE //
2601+ dev->device_handle = open(path, O_RDWR);
2602+
2603+ // If we have a good handle, return it.
2604+ if (dev->device_handle > 0) {
2605+
2606+ /* Get the report descriptor */
2607+ int res, desc_size = 0;
2608+ struct hidraw_report_descriptor rpt_desc;
2609+
2610+ memset(&rpt_desc, 0x0, sizeof(rpt_desc));
2611+
2612+ /* Get Report Descriptor Size */
2613+ res = ioctl(dev->device_handle, HIDIOCGRDESCSIZE, &desc_size);
2614+ if (res < 0)
2615+ perror("HIDIOCGRDESCSIZE");
2616+
2617+
2618+ /* Get Report Descriptor */
2619+ rpt_desc.size = desc_size;
2620+ res = ioctl(dev->device_handle, HIDIOCGRDESC, &rpt_desc);
2621+ if (res < 0) {
2622+ perror("HIDIOCGRDESC");
2623+ } else {
2624+ /* Determine if this device uses numbered reports. */
2625+ dev->uses_numbered_reports =
2626+ uses_numbered_reports(rpt_desc.value,
2627+ rpt_desc.size);
2628+ }
2629+
2630+ return dev;
2631+ }
2632+ else {
2633+ // Unable to open any devices.
2634+ free(dev);
2635+ return NULL;
2636+ }
2637+}
2638+
2639+
2640+int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
2641+{
2642+ int bytes_written;
2643+
2644+ bytes_written = write(dev->device_handle, data, length);
2645+
2646+ return bytes_written;
2647+}
2648+
2649+
2650+int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
2651+{
2652+ int bytes_read;
2653+
2654+ if (milliseconds != 0) {
2655+ /* milliseconds is -1 or > 0. In both cases, we want to
2656+ call poll() and wait for data to arrive. -1 means
2657+ INFINITE. */
2658+ int ret;
2659+ struct pollfd fds;
2660+
2661+ fds.fd = dev->device_handle;
2662+ fds.events = POLLIN;
2663+ fds.revents = 0;
2664+ ret = poll(&fds, 1, milliseconds);
2665+ if (ret == -1 || ret == 0)
2666+ /* Error or timeout */
2667+ return ret;
2668+ }
2669+
2670+ bytes_read = read(dev->device_handle, data, length);
2671+ if (bytes_read < 0 && errno == EAGAIN)
2672+ bytes_read = 0;
2673+
2674+ if (bytes_read >= 0 &&
2675+ kernel_version < KERNEL_VERSION(2,6,34) &&
2676+ dev->uses_numbered_reports) {
2677+ /* Work around a kernel bug. Chop off the first byte. */
2678+ memmove(data, data+1, bytes_read);
2679+ bytes_read--;
2680+ }
2681+
2682+ return bytes_read;
2683+}
2684+
2685+int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
2686+{
2687+ return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
2688+}
2689+
2690+int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
2691+{
2692+ int flags, res;
2693+
2694+ flags = fcntl(dev->device_handle, F_GETFL, 0);
2695+ if (flags >= 0) {
2696+ if (nonblock)
2697+ res = fcntl(dev->device_handle, F_SETFL, flags | O_NONBLOCK);
2698+ else
2699+ res = fcntl(dev->device_handle, F_SETFL, flags & ~O_NONBLOCK);
2700+ }
2701+ else
2702+ return -1;
2703+
2704+ if (res < 0) {
2705+ return -1;
2706+ }
2707+ else {
2708+ dev->blocking = !nonblock;
2709+ return 0; /* Success */
2710+ }
2711+}
2712+
2713+
2714+int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
2715+{
2716+ int res;
2717+
2718+ res = ioctl(dev->device_handle, HIDIOCSFEATURE(length), data);
2719+ if (res < 0)
2720+ perror("ioctl (SFEATURE)");
2721+
2722+ return res;
2723+}
2724+
2725+int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
2726+{
2727+ int res;
2728+
2729+ res = ioctl(dev->device_handle, HIDIOCGFEATURE(length), data);
2730+ if (res < 0)
2731+ perror("ioctl (GFEATURE)");
2732+
2733+
2734+ return res;
2735+}
2736+
2737+
2738+void HID_API_EXPORT hid_close(hid_device *dev)
2739+{
2740+ if (!dev)
2741+ return;
2742+ close(dev->device_handle);
2743+ free(dev);
2744+}
2745+
2746+
2747+int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
2748+{
2749+ return get_device_string(dev, "manufacturer", string, maxlen);
2750+}
2751+
2752+int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
2753+{
2754+ return get_device_string(dev, "product", string, maxlen);
2755+}
2756+
2757+int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
2758+{
2759+ return get_device_string(dev, "serial", string, maxlen);
2760+}
2761+
2762+int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
2763+{
2764+ return -1;
2765+}
2766+
2767+
2768+HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
2769+{
2770+ return NULL;
2771+}
2772
2773=== added directory 'mixxx/lib/hidapi-0.7.0/mac'
2774=== added file 'mixxx/lib/hidapi-0.7.0/mac/.gitignore'
2775--- mixxx/lib/hidapi-0.7.0/mac/.gitignore 1970-01-01 00:00:00 +0000
2776+++ mixxx/lib/hidapi-0.7.0/mac/.gitignore 2012-04-25 15:44:23 +0000
2777@@ -0,0 +1,13 @@
2778+Debug
2779+Release
2780+*.exp
2781+*.ilk
2782+*.lib
2783+*.suo
2784+*.vcproj.*
2785+*.ncb
2786+*.suo
2787+*.dll
2788+*.pdb
2789+*.o
2790+hidtest
2791\ No newline at end of file
2792
2793=== added file 'mixxx/lib/hidapi-0.7.0/mac/Makefile'
2794--- mixxx/lib/hidapi-0.7.0/mac/Makefile 1970-01-01 00:00:00 +0000
2795+++ mixxx/lib/hidapi-0.7.0/mac/Makefile 2012-04-25 15:44:23 +0000
2796@@ -0,0 +1,32 @@
2797+###########################################
2798+# Simple Makefile for HIDAPI test program
2799+#
2800+# Alan Ott
2801+# Signal 11 Software
2802+# 2010-07-03
2803+###########################################
2804+
2805+all: hidtest
2806+
2807+CC=gcc
2808+CXX=g++
2809+COBJS=hid.o
2810+CPPOBJS=../hidtest/hidtest.o
2811+OBJS=$(COBJS) $(CPPOBJS)
2812+CFLAGS+=-I../hidapi -Wall -g -c
2813+LIBS=-framework IOKit -framework CoreFoundation
2814+
2815+
2816+hidtest: $(OBJS)
2817+ g++ -Wall -g $^ $(LIBS) -o hidtest
2818+
2819+$(COBJS): %.o: %.c
2820+ $(CC) $(CFLAGS) $< -o $@
2821+
2822+$(CPPOBJS): %.o: %.cpp
2823+ $(CXX) $(CFLAGS) $< -o $@
2824+
2825+clean:
2826+ rm -f *.o hidtest $(CPPOBJS)
2827+
2828+.PHONY: clean
2829
2830=== added file 'mixxx/lib/hidapi-0.7.0/mac/hid.c'
2831--- mixxx/lib/hidapi-0.7.0/mac/hid.c 1970-01-01 00:00:00 +0000
2832+++ mixxx/lib/hidapi-0.7.0/mac/hid.c 2012-04-25 15:44:23 +0000
2833@@ -0,0 +1,1122 @@
2834+/*******************************************************
2835+ HIDAPI - Multi-Platform library for
2836+ communication with HID devices.
2837+
2838+ Alan Ott
2839+ Signal 11 Software
2840+
2841+ 2010-07-03
2842+
2843+ Copyright 2010, All Rights Reserved.
2844+
2845+ At the discretion of the user of this library,
2846+ this software may be licensed under the terms of the
2847+ GNU Public License v3, a BSD-Style license, or the
2848+ original HIDAPI license as outlined in the LICENSE.txt,
2849+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
2850+ files located at the root of the source distribution.
2851+ These files may also be found in the public source
2852+ code repository located at:
2853+ http://github.com/signal11/hidapi .
2854+********************************************************/
2855+
2856+/* See Apple Technical Note TN2187 for details on IOHidManager. */
2857+
2858+#include <IOKit/hid/IOHIDManager.h>
2859+#include <IOKit/hid/IOHIDKeys.h>
2860+#include <CoreFoundation/CoreFoundation.h>
2861+#include <wchar.h>
2862+#include <locale.h>
2863+#include <pthread.h>
2864+#include <sys/time.h>
2865+#include <unistd.h>
2866+
2867+#include "hidapi.h"
2868+
2869+/* Barrier implementation because Mac OSX doesn't have pthread_barrier.
2870+ It also doesn't have clock_gettime(). So much for POSIX and SUSv2.
2871+ This implementation came from Brent Priddy and was posted on
2872+ StackOverflow. It is used with his permission. */
2873+typedef int pthread_barrierattr_t;
2874+typedef struct pthread_barrier {
2875+ pthread_mutex_t mutex;
2876+ pthread_cond_t cond;
2877+ int count;
2878+ int trip_count;
2879+} pthread_barrier_t;
2880+
2881+static int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned int count)
2882+{
2883+ if(count == 0) {
2884+ errno = EINVAL;
2885+ return -1;
2886+ }
2887+
2888+ if(pthread_mutex_init(&barrier->mutex, 0) < 0) {
2889+ return -1;
2890+ }
2891+ if(pthread_cond_init(&barrier->cond, 0) < 0) {
2892+ pthread_mutex_destroy(&barrier->mutex);
2893+ return -1;
2894+ }
2895+ barrier->trip_count = count;
2896+ barrier->count = 0;
2897+
2898+ return 0;
2899+}
2900+
2901+static int pthread_barrier_destroy(pthread_barrier_t *barrier)
2902+{
2903+ pthread_cond_destroy(&barrier->cond);
2904+ pthread_mutex_destroy(&barrier->mutex);
2905+ return 0;
2906+}
2907+
2908+static int pthread_barrier_wait(pthread_barrier_t *barrier)
2909+{
2910+ pthread_mutex_lock(&barrier->mutex);
2911+ ++(barrier->count);
2912+ if(barrier->count >= barrier->trip_count)
2913+ {
2914+ barrier->count = 0;
2915+ pthread_cond_broadcast(&barrier->cond);
2916+ pthread_mutex_unlock(&barrier->mutex);
2917+ return 1;
2918+ }
2919+ else
2920+ {
2921+ pthread_cond_wait(&barrier->cond, &(barrier->mutex));
2922+ pthread_mutex_unlock(&barrier->mutex);
2923+ return 0;
2924+ }
2925+}
2926+
2927+static int return_data(hid_device *dev, unsigned char *data, size_t length);
2928+
2929+/* Linked List of input reports received from the device. */
2930+struct input_report {
2931+ uint8_t *data;
2932+ size_t len;
2933+ struct input_report *next;
2934+};
2935+
2936+struct hid_device_ {
2937+ IOHIDDeviceRef device_handle;
2938+ int blocking;
2939+ int uses_numbered_reports;
2940+ int disconnected;
2941+ CFStringRef run_loop_mode;
2942+ CFRunLoopRef run_loop;
2943+ CFRunLoopSourceRef source;
2944+ uint8_t *input_report_buf;
2945+ CFIndex max_input_report_len;
2946+ struct input_report *input_reports;
2947+
2948+ pthread_t thread;
2949+ pthread_mutex_t mutex; /* Protects input_reports */
2950+ pthread_cond_t condition;
2951+ pthread_barrier_t barrier; /* Ensures correct startup sequence */
2952+ pthread_barrier_t shutdown_barrier; /* Ensures correct shutdown sequence */
2953+ int shutdown_thread;
2954+
2955+ hid_device *next;
2956+};
2957+
2958+/* Static list of all the devices open. This way when a device gets
2959+ disconnected, its hid_device structure can be marked as disconnected
2960+ from hid_device_removal_callback(). */
2961+static hid_device *device_list = NULL;
2962+static pthread_mutex_t device_list_mutex = PTHREAD_MUTEX_INITIALIZER;
2963+
2964+static hid_device *new_hid_device(void)
2965+{
2966+ hid_device *dev = calloc(1, sizeof(hid_device));
2967+ dev->device_handle = NULL;
2968+ dev->blocking = 1;
2969+ dev->uses_numbered_reports = 0;
2970+ dev->disconnected = 0;
2971+ dev->run_loop_mode = NULL;
2972+ dev->run_loop = NULL;
2973+ dev->source = NULL;
2974+ dev->input_report_buf = NULL;
2975+ dev->input_reports = NULL;
2976+ dev->shutdown_thread = 0;
2977+ dev->next = NULL;
2978+
2979+ /* Thread objects */
2980+ pthread_mutex_init(&dev->mutex, NULL);
2981+ pthread_cond_init(&dev->condition, NULL);
2982+ pthread_barrier_init(&dev->barrier, NULL, 2);
2983+ pthread_barrier_init(&dev->shutdown_barrier, NULL, 2);
2984+
2985+ /* Add the new record to the device_list. */
2986+ pthread_mutex_lock(&device_list_mutex);
2987+ if (!device_list)
2988+ device_list = dev;
2989+ else {
2990+ hid_device *d = device_list;
2991+ while (d) {
2992+ if (!d->next) {
2993+ d->next = dev;
2994+ break;
2995+ }
2996+ d = d->next;
2997+ }
2998+ }
2999+ pthread_mutex_unlock(&device_list_mutex);
3000+
3001+ return dev;
3002+}
3003+
3004+static void free_hid_device(hid_device *dev)
3005+{
3006+ if (!dev)
3007+ return;
3008+
3009+ /* Delete any input reports still left over. */
3010+ struct input_report *rpt = dev->input_reports;
3011+ while (rpt) {
3012+ struct input_report *next = rpt->next;
3013+ free(rpt->data);
3014+ free(rpt);
3015+ rpt = next;
3016+ }
3017+
3018+ /* Free the string and the report buffer. The check for NULL
3019+ is necessary here as CFRelease() doesn't handle NULL like
3020+ free() and others do. */
3021+ if (dev->run_loop_mode)
3022+ CFRelease(dev->run_loop_mode);
3023+ if (dev->source)
3024+ CFRelease(dev->source);
3025+ free(dev->input_report_buf);
3026+
3027+ /* Clean up the thread objects */
3028+ pthread_barrier_destroy(&dev->shutdown_barrier);
3029+ pthread_barrier_destroy(&dev->barrier);
3030+ pthread_cond_destroy(&dev->condition);
3031+ pthread_mutex_destroy(&dev->mutex);
3032+
3033+ /* Remove it from the device list. */
3034+ pthread_mutex_lock(&device_list_mutex);
3035+ hid_device *d = device_list;
3036+ if (d == dev) {
3037+ device_list = d->next;
3038+ }
3039+ else {
3040+ while (d) {
3041+ if (d->next == dev) {
3042+ d->next = d->next->next;
3043+ break;
3044+ }
3045+
3046+ d = d->next;
3047+ }
3048+ }
3049+ pthread_mutex_unlock(&device_list_mutex);
3050+
3051+ /* Free the structure itself. */
3052+ free(dev);
3053+}
3054+
3055+static IOHIDManagerRef hid_mgr = 0x0;
3056+
3057+
3058+#if 0
3059+static void register_error(hid_device *device, const char *op)
3060+{
3061+
3062+}
3063+#endif
3064+
3065+
3066+static int32_t get_int_property(IOHIDDeviceRef device, CFStringRef key)
3067+{
3068+ CFTypeRef ref;
3069+ int32_t value;
3070+
3071+ ref = IOHIDDeviceGetProperty(device, key);
3072+ if (ref) {
3073+ if (CFGetTypeID(ref) == CFNumberGetTypeID()) {
3074+ CFNumberGetValue((CFNumberRef) ref, kCFNumberSInt32Type, &value);
3075+ return value;
3076+ }
3077+ }
3078+ return 0;
3079+}
3080+
3081+static unsigned short get_vendor_id(IOHIDDeviceRef device)
3082+{
3083+ return get_int_property(device, CFSTR(kIOHIDVendorIDKey));
3084+}
3085+
3086+static unsigned short get_product_id(IOHIDDeviceRef device)
3087+{
3088+ return get_int_property(device, CFSTR(kIOHIDProductIDKey));
3089+}
3090+
3091+
3092+static int32_t get_max_report_length(IOHIDDeviceRef device)
3093+{
3094+ return get_int_property(device, CFSTR(kIOHIDMaxInputReportSizeKey));
3095+}
3096+
3097+static int get_string_property(IOHIDDeviceRef device, CFStringRef prop, wchar_t *buf, size_t len)
3098+{
3099+ CFStringRef str = IOHIDDeviceGetProperty(device, prop);
3100+
3101+ buf[0] = 0x0000;
3102+
3103+ if (str) {
3104+ CFRange range;
3105+ range.location = 0;
3106+ range.length = len;
3107+ CFIndex used_buf_len;
3108+ CFStringGetBytes(str,
3109+ range,
3110+ kCFStringEncodingUTF32LE,
3111+ (char)'?',
3112+ FALSE,
3113+ (UInt8*)buf,
3114+ len,
3115+ &used_buf_len);
3116+ buf[len-1] = 0x00000000;
3117+ return used_buf_len;
3118+ }
3119+ else
3120+ return 0;
3121+
3122+}
3123+
3124+static int get_string_property_utf8(IOHIDDeviceRef device, CFStringRef prop, char *buf, size_t len)
3125+{
3126+ CFStringRef str = IOHIDDeviceGetProperty(device, prop);
3127+
3128+ buf[0] = 0x0000;
3129+
3130+ if (str) {
3131+ CFRange range;
3132+ range.location = 0;
3133+ range.length = len;
3134+ CFIndex used_buf_len;
3135+ CFStringGetBytes(str,
3136+ range,
3137+ kCFStringEncodingUTF8,
3138+ (char)'?',
3139+ FALSE,
3140+ (UInt8*)buf,
3141+ len,
3142+ &used_buf_len);
3143+ buf[len-1] = 0x00000000;
3144+ return used_buf_len;
3145+ }
3146+ else
3147+ return 0;
3148+
3149+}
3150+
3151+
3152+static int get_serial_number(IOHIDDeviceRef device, wchar_t *buf, size_t len)
3153+{
3154+ return get_string_property(device, CFSTR(kIOHIDSerialNumberKey), buf, len);
3155+}
3156+
3157+static int get_manufacturer_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
3158+{
3159+ return get_string_property(device, CFSTR(kIOHIDManufacturerKey), buf, len);
3160+}
3161+
3162+static int get_product_string(IOHIDDeviceRef device, wchar_t *buf, size_t len)
3163+{
3164+ return get_string_property(device, CFSTR(kIOHIDProductKey), buf, len);
3165+}
3166+
3167+
3168+/* Implementation of wcsdup() for Mac. */
3169+static wchar_t *dup_wcs(const wchar_t *s)
3170+{
3171+ size_t len = wcslen(s);
3172+ wchar_t *ret = malloc((len+1)*sizeof(wchar_t));
3173+ wcscpy(ret, s);
3174+
3175+ return ret;
3176+}
3177+
3178+
3179+static int make_path(IOHIDDeviceRef device, char *buf, size_t len)
3180+{
3181+ int res;
3182+ unsigned short vid, pid;
3183+ char transport[32];
3184+
3185+ buf[0] = '\0';
3186+
3187+ res = get_string_property_utf8(
3188+ device, CFSTR(kIOHIDTransportKey),
3189+ transport, sizeof(transport));
3190+
3191+ if (!res)
3192+ return -1;
3193+
3194+ vid = get_vendor_id(device);
3195+ pid = get_product_id(device);
3196+
3197+ res = snprintf(buf, len, "%s_%04hx_%04hx_%p",
3198+ transport, vid, pid, device);
3199+
3200+
3201+ buf[len-1] = '\0';
3202+ return res+1;
3203+}
3204+
3205+static int init_hid_manager(void)
3206+{
3207+ IOReturn res;
3208+
3209+ /* Initialize all the HID Manager Objects */
3210+ hid_mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
3211+ IOHIDManagerSetDeviceMatching(hid_mgr, NULL);
3212+ IOHIDManagerScheduleWithRunLoop(hid_mgr, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
3213+ res = IOHIDManagerOpen(hid_mgr, kIOHIDOptionsTypeNone);
3214+ return (res == kIOReturnSuccess)? 0: -1;
3215+}
3216+
3217+int HID_API_EXPORT hid_init(void)
3218+{
3219+ if (!hid_mgr) {
3220+ if (init_hid_manager() < 0) {
3221+ hid_exit();
3222+ return -1;
3223+ }
3224+ }
3225+ return 0;
3226+}
3227+
3228+int HID_API_EXPORT hid_exit(void)
3229+{
3230+ if (hid_mgr) {
3231+ /* Close the HID manager. */
3232+ IOHIDManagerClose(hid_mgr, kIOHIDOptionsTypeNone);
3233+ CFRelease(hid_mgr);
3234+ hid_mgr = NULL;
3235+ }
3236+
3237+ return 0;
3238+}
3239+
3240+struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
3241+{
3242+ struct hid_device_info *root = NULL; // return object
3243+ struct hid_device_info *cur_dev = NULL;
3244+ CFIndex num_devices;
3245+ int i;
3246+
3247+ setlocale(LC_ALL,"");
3248+
3249+ /* Set up the HID Manager if it hasn't been done */
3250+ hid_init();
3251+
3252+ /* Get a list of the Devices */
3253+ CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);
3254+
3255+ /* Convert the list into a C array so we can iterate easily. */
3256+ num_devices = CFSetGetCount(device_set);
3257+ IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
3258+ CFSetGetValues(device_set, (const void **) device_array);
3259+
3260+ /* Iterate over each device, making an entry for it. */
3261+ for (i = 0; i < num_devices; i++) {
3262+ unsigned short dev_vid;
3263+ unsigned short dev_pid;
3264+ #define BUF_LEN 256
3265+ wchar_t buf[BUF_LEN];
3266+ char cbuf[BUF_LEN];
3267+
3268+ IOHIDDeviceRef dev = device_array[i];
3269+
3270+ if (!dev) {
3271+ continue;
3272+ }
3273+ dev_vid = get_vendor_id(dev);
3274+ dev_pid = get_product_id(dev);
3275+
3276+ /* Check the VID/PID against the arguments */
3277+ if ((vendor_id == 0x0 && product_id == 0x0) ||
3278+ (vendor_id == dev_vid && product_id == dev_pid)) {
3279+ struct hid_device_info *tmp;
3280+ size_t len;
3281+
3282+ /* VID/PID match. Create the record. */
3283+ tmp = malloc(sizeof(struct hid_device_info));
3284+ if (cur_dev) {
3285+ cur_dev->next = tmp;
3286+ }
3287+ else {
3288+ root = tmp;
3289+ }
3290+ cur_dev = tmp;
3291+
3292+ // Get the Usage Page and Usage for this device.
3293+ cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
3294+ cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));
3295+
3296+ /* Fill out the record */
3297+ cur_dev->next = NULL;
3298+ len = make_path(dev, cbuf, sizeof(cbuf));
3299+ cur_dev->path = strdup(cbuf);
3300+
3301+ /* Serial Number */
3302+ get_serial_number(dev, buf, BUF_LEN);
3303+ cur_dev->serial_number = dup_wcs(buf);
3304+
3305+ /* Manufacturer and Product strings */
3306+ get_manufacturer_string(dev, buf, BUF_LEN);
3307+ cur_dev->manufacturer_string = dup_wcs(buf);
3308+ get_product_string(dev, buf, BUF_LEN);
3309+ cur_dev->product_string = dup_wcs(buf);
3310+
3311+ /* VID/PID */
3312+ cur_dev->vendor_id = dev_vid;
3313+ cur_dev->product_id = dev_pid;
3314+
3315+ /* Release Number */
3316+ cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey));
3317+
3318+ /* Interface Number (Unsupported on Mac)*/
3319+ cur_dev->interface_number = -1;
3320+ }
3321+ }
3322+
3323+ free(device_array);
3324+ CFRelease(device_set);
3325+
3326+ return root;
3327+}
3328+
3329+void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
3330+{
3331+ /* This function is identical to the Linux version. Platform independent. */
3332+ struct hid_device_info *d = devs;
3333+ while (d) {
3334+ struct hid_device_info *next = d->next;
3335+ free(d->path);
3336+ free(d->serial_number);
3337+ free(d->manufacturer_string);
3338+ free(d->product_string);
3339+ free(d);
3340+ d = next;
3341+ }
3342+}
3343+
3344+hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
3345+{
3346+ /* This function is identical to the Linux version. Platform independent. */
3347+ struct hid_device_info *devs, *cur_dev;
3348+ const char *path_to_open = NULL;
3349+ hid_device * handle = NULL;
3350+
3351+ devs = hid_enumerate(vendor_id, product_id);
3352+ cur_dev = devs;
3353+ while (cur_dev) {
3354+ if (cur_dev->vendor_id == vendor_id &&
3355+ cur_dev->product_id == product_id) {
3356+ if (serial_number) {
3357+ if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
3358+ path_to_open = cur_dev->path;
3359+ break;
3360+ }
3361+ }
3362+ else {
3363+ path_to_open = cur_dev->path;
3364+ break;
3365+ }
3366+ }
3367+ cur_dev = cur_dev->next;
3368+ }
3369+
3370+ if (path_to_open) {
3371+ /* Open the device */
3372+ handle = hid_open_path(path_to_open);
3373+ }
3374+
3375+ hid_free_enumeration(devs);
3376+
3377+ return handle;
3378+}
3379+
3380+static void hid_device_removal_callback(void *context, IOReturn result,
3381+ void *sender, IOHIDDeviceRef dev_ref)
3382+{
3383+ /* Stop the Run Loop for this device. */
3384+ pthread_mutex_lock(&device_list_mutex);
3385+ hid_device *d = device_list;
3386+ while (d) {
3387+ if (d->device_handle == dev_ref) {
3388+ d->disconnected = 1;
3389+ CFRunLoopStop(d->run_loop);
3390+ }
3391+
3392+ d = d->next;
3393+ }
3394+ pthread_mutex_unlock(&device_list_mutex);
3395+}
3396+
3397+/* The Run Loop calls this function for each input report received.
3398+ This function puts the data into a linked list to be picked up by
3399+ hid_read(). */
3400+static void hid_report_callback(void *context, IOReturn result, void *sender,
3401+ IOHIDReportType report_type, uint32_t report_id,
3402+ uint8_t *report, CFIndex report_length)
3403+{
3404+ struct input_report *rpt;
3405+ hid_device *dev = context;
3406+
3407+ /* Make a new Input Report object */
3408+ rpt = calloc(1, sizeof(struct input_report));
3409+ rpt->data = calloc(1, report_length);
3410+ memcpy(rpt->data, report, report_length);
3411+ rpt->len = report_length;
3412+ rpt->next = NULL;
3413+
3414+ /* Lock this section */
3415+ pthread_mutex_lock(&dev->mutex);
3416+
3417+ /* Attach the new report object to the end of the list. */
3418+ if (dev->input_reports == NULL) {
3419+ /* The list is empty. Put it at the root. */
3420+ dev->input_reports = rpt;
3421+ }
3422+ else {
3423+ /* Find the end of the list and attach. */
3424+ struct input_report *cur = dev->input_reports;
3425+ int num_queued = 0;
3426+ while (cur->next != NULL) {
3427+ cur = cur->next;
3428+ num_queued++;
3429+ }
3430+ cur->next = rpt;
3431+
3432+ /* Pop one off if we've reached 30 in the queue. This
3433+ way we don't grow forever if the user never reads
3434+ anything from the device. */
3435+ if (num_queued > 30) {
3436+ return_data(dev, NULL, 0);
3437+ }
3438+ }
3439+
3440+ /* Signal a waiting thread that there is data. */
3441+ pthread_cond_signal(&dev->condition);
3442+
3443+ /* Unlock */
3444+ pthread_mutex_unlock(&dev->mutex);
3445+
3446+}
3447+
3448+/* This gets called when the read_thred's run loop gets signaled by
3449+ hid_close(), and serves to stop the read_thread's run loop. */
3450+static void perform_signal_callback(void *context)
3451+{
3452+ hid_device *dev = context;
3453+ CFRunLoopStop(dev->run_loop); //TODO: CFRunLoopGetCurrent()
3454+}
3455+
3456+static void *read_thread(void *param)
3457+{
3458+ hid_device *dev = param;
3459+
3460+ /* Move the device's run loop to this thread. */
3461+ IOHIDDeviceScheduleWithRunLoop(dev->device_handle, CFRunLoopGetCurrent(), dev->run_loop_mode);
3462+
3463+ /* Create the RunLoopSource which is used to signal the
3464+ event loop to stop when hid_close() is called. */
3465+ CFRunLoopSourceContext ctx;
3466+ memset(&ctx, 0, sizeof(ctx));
3467+ ctx.version = 0;
3468+ ctx.info = dev;
3469+ ctx.perform = &perform_signal_callback;
3470+ dev->source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0/*order*/, &ctx);
3471+ CFRunLoopAddSource(CFRunLoopGetCurrent(), dev->source, dev->run_loop_mode);
3472+
3473+ /* Store off the Run Loop so it can be stopped from hid_close()
3474+ and on device disconnection. */
3475+ dev->run_loop = CFRunLoopGetCurrent();
3476+
3477+ /* Notify the main thread that the read thread is up and running. */
3478+ pthread_barrier_wait(&dev->barrier);
3479+
3480+ /* Run the Event Loop. CFRunLoopRunInMode() will dispatch HID input
3481+ reports into the hid_report_callback(). */
3482+ SInt32 code;
3483+ while (!dev->shutdown_thread && !dev->disconnected) {
3484+ code = CFRunLoopRunInMode(dev->run_loop_mode, 1000/*sec*/, FALSE);
3485+ /* Return if the device has been disconnected */
3486+ if (code == kCFRunLoopRunFinished) {
3487+ dev->disconnected = 1;
3488+ break;
3489+ }
3490+
3491+
3492+ /* Break if The Run Loop returns Finished or Stopped. */
3493+ if (code != kCFRunLoopRunTimedOut &&
3494+ code != kCFRunLoopRunHandledSource) {
3495+ /* There was some kind of error. Setting
3496+ shutdown seems to make sense, but
3497+ there may be something else more appropriate */
3498+ dev->shutdown_thread = 1;
3499+ break;
3500+ }
3501+ }
3502+
3503+ /* Now that the read thread is stopping, Wake any threads which are
3504+ waiting on data (in hid_read_timeout()). Do this under a mutex to
3505+ make sure that a thread which is about to go to sleep waiting on
3506+ the condition acutally will go to sleep before the condition is
3507+ signaled. */
3508+ pthread_mutex_lock(&dev->mutex);
3509+ pthread_cond_broadcast(&dev->condition);
3510+ pthread_mutex_unlock(&dev->mutex);
3511+
3512+ /* Close the OS handle to the device, but only if it's not
3513+ been unplugged. If it's been unplugged, then calling
3514+ IOHIDDeviceClose() will crash. */
3515+ if (!dev->disconnected) {
3516+ IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeNone);
3517+ }
3518+
3519+ /* Wait here until hid_close() is called and makes it past
3520+ the call to CFRunLoopWakeUp(). This thread still needs to
3521+ be valid when that function is called on the other thread. */
3522+ pthread_barrier_wait(&dev->shutdown_barrier);
3523+
3524+ return NULL;
3525+}
3526+
3527+hid_device * HID_API_EXPORT hid_open_path(const char *path)
3528+{
3529+ int i;
3530+ hid_device *dev = NULL;
3531+ CFIndex num_devices;
3532+
3533+ dev = new_hid_device();
3534+
3535+ /* Set up the HID Manager if it hasn't been done */
3536+ hid_init();
3537+
3538+ CFSetRef device_set = IOHIDManagerCopyDevices(hid_mgr);
3539+
3540+ num_devices = CFSetGetCount(device_set);
3541+ IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
3542+ CFSetGetValues(device_set, (const void **) device_array);
3543+ for (i = 0; i < num_devices; i++) {
3544+ char cbuf[BUF_LEN];
3545+ size_t len;
3546+ IOHIDDeviceRef os_dev = device_array[i];
3547+
3548+ len = make_path(os_dev, cbuf, sizeof(cbuf));
3549+ if (!strcmp(cbuf, path)) {
3550+ // Matched Paths. Open this Device.
3551+ IOReturn ret = IOHIDDeviceOpen(os_dev, kIOHIDOptionsTypeNone);
3552+ if (ret == kIOReturnSuccess) {
3553+ char str[32];
3554+
3555+ free(device_array);
3556+ CFRelease(device_set);
3557+ dev->device_handle = os_dev;
3558+
3559+ /* Create the buffers for receiving data */
3560+ dev->max_input_report_len = (CFIndex) get_max_report_length(os_dev);
3561+ dev->input_report_buf = calloc(dev->max_input_report_len, sizeof(uint8_t));
3562+
3563+ /* Create the Run Loop Mode for this device.
3564+ printing the reference seems to work. */
3565+ sprintf(str, "HIDAPI_%p", os_dev);
3566+ dev->run_loop_mode =
3567+ CFStringCreateWithCString(NULL, str, kCFStringEncodingASCII);
3568+
3569+ /* Attach the device to a Run Loop */
3570+ IOHIDDeviceRegisterInputReportCallback(
3571+ os_dev, dev->input_report_buf, dev->max_input_report_len,
3572+ &hid_report_callback, dev);
3573+ IOHIDManagerRegisterDeviceRemovalCallback(hid_mgr, hid_device_removal_callback, NULL);
3574+
3575+ /* Start the read thread */
3576+ pthread_create(&dev->thread, NULL, read_thread, dev);
3577+
3578+ /* Wait here for the read thread to be initialized. */
3579+ pthread_barrier_wait(&dev->barrier);
3580+
3581+ return dev;
3582+ }
3583+ else {
3584+ goto return_error;
3585+ }
3586+ }
3587+ }
3588+
3589+return_error:
3590+ free(device_array);
3591+ CFRelease(device_set);
3592+ free_hid_device(dev);
3593+ return NULL;
3594+}
3595+
3596+static int set_report(hid_device *dev, IOHIDReportType type, const unsigned char *data, size_t length)
3597+{
3598+ const unsigned char *data_to_send;
3599+ size_t length_to_send;
3600+ IOReturn res;
3601+
3602+ /* Return if the device has been disconnected. */
3603+ if (dev->disconnected)
3604+ return -1;
3605+
3606+ if (data[0] == 0x0) {
3607+ /* Not using numbered Reports.
3608+ Don't send the report number. */
3609+ data_to_send = data+1;
3610+ length_to_send = length-1;
3611+ }
3612+ else {
3613+ /* Using numbered Reports.
3614+ Send the Report Number */
3615+ data_to_send = data;
3616+ length_to_send = length;
3617+ }
3618+
3619+ if (!dev->disconnected) {
3620+ res = IOHIDDeviceSetReport(dev->device_handle,
3621+ type,
3622+ data[0], /* Report ID*/
3623+ data_to_send, length_to_send);
3624+
3625+ if (res == kIOReturnSuccess) {
3626+ return length;
3627+ }
3628+ else
3629+ return -1;
3630+ }
3631+
3632+ return -1;
3633+}
3634+
3635+int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
3636+{
3637+ return set_report(dev, kIOHIDReportTypeOutput, data, length);
3638+}
3639+
3640+/* Helper function, so that this isn't duplicated in hid_read(). */
3641+static int return_data(hid_device *dev, unsigned char *data, size_t length)
3642+{
3643+ /* Copy the data out of the linked list item (rpt) into the
3644+ return buffer (data), and delete the liked list item. */
3645+ struct input_report *rpt = dev->input_reports;
3646+ size_t len = (length < rpt->len)? length: rpt->len;
3647+ memcpy(data, rpt->data, len);
3648+ dev->input_reports = rpt->next;
3649+ free(rpt->data);
3650+ free(rpt);
3651+ return len;
3652+}
3653+
3654+static int cond_wait(const hid_device *dev, pthread_cond_t *cond, pthread_mutex_t *mutex)
3655+{
3656+ while (!dev->input_reports) {
3657+ int res = pthread_cond_wait(cond, mutex);
3658+ if (res != 0)
3659+ return res;
3660+
3661+ /* A res of 0 means we may have been signaled or it may
3662+ be a spurious wakeup. Check to see that there's acutally
3663+ data in the queue before returning, and if not, go back
3664+ to sleep. See the pthread_cond_timedwait() man page for
3665+ details. */
3666+
3667+ if (dev->shutdown_thread || dev->disconnected)
3668+ return -1;
3669+ }
3670+
3671+ return 0;
3672+}
3673+
3674+static int cond_timedwait(const hid_device *dev, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
3675+{
3676+ while (!dev->input_reports) {
3677+ int res = pthread_cond_timedwait(cond, mutex, abstime);
3678+ if (res != 0)
3679+ return res;
3680+
3681+ /* A res of 0 means we may have been signaled or it may
3682+ be a spurious wakeup. Check to see that there's acutally
3683+ data in the queue before returning, and if not, go back
3684+ to sleep. See the pthread_cond_timedwait() man page for
3685+ details. */
3686+
3687+ if (dev->shutdown_thread || dev->disconnected)
3688+ return -1;
3689+ }
3690+
3691+ return 0;
3692+
3693+}
3694+
3695+int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
3696+{
3697+ int bytes_read = -1;
3698+
3699+ /* Lock the access to the report list. */
3700+ pthread_mutex_lock(&dev->mutex);
3701+
3702+ /* There's an input report queued up. Return it. */
3703+ if (dev->input_reports) {
3704+ /* Return the first one */
3705+ bytes_read = return_data(dev, data, length);
3706+ goto ret;
3707+ }
3708+
3709+ /* Return if the device has been disconnected. */
3710+ if (dev->disconnected) {
3711+ bytes_read = -1;
3712+ goto ret;
3713+ }
3714+
3715+ if (dev->shutdown_thread) {
3716+ /* This means the device has been closed (or there
3717+ has been an error. An error code of -1 should
3718+ be returned. */
3719+ bytes_read = -1;
3720+ goto ret;
3721+ }
3722+
3723+ /* There is no data. Go to sleep and wait for data. */
3724+
3725+ if (milliseconds == -1) {
3726+ /* Blocking */
3727+ int res;
3728+ res = cond_wait(dev, &dev->condition, &dev->mutex);
3729+ if (res == 0)
3730+ bytes_read = return_data(dev, data, length);
3731+ else {
3732+ /* There was an error, or a device disconnection. */
3733+ bytes_read = -1;
3734+ }
3735+ }
3736+ else if (milliseconds > 0) {
3737+ /* Non-blocking, but called with timeout. */
3738+ int res;
3739+ struct timespec ts;
3740+ struct timeval tv;
3741+ gettimeofday(&tv, NULL);
3742+ TIMEVAL_TO_TIMESPEC(&tv, &ts);
3743+ ts.tv_sec += milliseconds / 1000;
3744+ ts.tv_nsec += (milliseconds % 1000) * 1000000;
3745+ if (ts.tv_nsec >= 1000000000L) {
3746+ ts.tv_sec++;
3747+ ts.tv_nsec -= 1000000000L;
3748+ }
3749+
3750+ res = cond_timedwait(dev, &dev->condition, &dev->mutex, &ts);
3751+ if (res == 0)
3752+ bytes_read = return_data(dev, data, length);
3753+ else if (res == ETIMEDOUT)
3754+ bytes_read = 0;
3755+ else
3756+ bytes_read = -1;
3757+ }
3758+ else {
3759+ /* Purely non-blocking */
3760+ bytes_read = 0;
3761+ }
3762+
3763+ret:
3764+ /* Unlock */
3765+ pthread_mutex_unlock(&dev->mutex);
3766+ return bytes_read;
3767+}
3768+
3769+int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
3770+{
3771+ return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
3772+}
3773+
3774+int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
3775+{
3776+ /* All Nonblocking operation is handled by the library. */
3777+ dev->blocking = !nonblock;
3778+
3779+ return 0;
3780+}
3781+
3782+int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
3783+{
3784+ return set_report(dev, kIOHIDReportTypeFeature, data, length);
3785+}
3786+
3787+int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
3788+{
3789+ CFIndex len = length;
3790+ IOReturn res;
3791+
3792+ /* Return if the device has been unplugged. */
3793+ if (dev->disconnected)
3794+ return -1;
3795+
3796+ res = IOHIDDeviceGetReport(dev->device_handle,
3797+ kIOHIDReportTypeFeature,
3798+ data[0], /* Report ID */
3799+ data, &len);
3800+ if (res == kIOReturnSuccess)
3801+ return len;
3802+ else
3803+ return -1;
3804+}
3805+
3806+
3807+void HID_API_EXPORT hid_close(hid_device *dev)
3808+{
3809+ if (!dev)
3810+ return;
3811+
3812+ /* Disconnect the report callback before close. */
3813+ if (!dev->disconnected) {
3814+ IOHIDDeviceRegisterInputReportCallback(
3815+ dev->device_handle, dev->input_report_buf, dev->max_input_report_len,
3816+ NULL, dev);
3817+ IOHIDManagerRegisterDeviceRemovalCallback(hid_mgr, NULL, dev);
3818+ IOHIDDeviceUnscheduleFromRunLoop(dev->device_handle, dev->run_loop, dev->run_loop_mode);
3819+ IOHIDDeviceScheduleWithRunLoop(dev->device_handle, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
3820+ }
3821+
3822+ /* Cause read_thread() to stop. */
3823+ dev->shutdown_thread = 1;
3824+
3825+ /* Wake up the run thread's event loop so that the thread can exit. */
3826+ CFRunLoopSourceSignal(dev->source);
3827+ CFRunLoopWakeUp(dev->run_loop);
3828+
3829+ /* Notify the read thread that it can shut down now. */
3830+ pthread_barrier_wait(&dev->shutdown_barrier);
3831+
3832+ /* Wait for read_thread() to end. */
3833+ pthread_join(dev->thread, NULL);
3834+
3835+ /* Close the OS handle to the device, but only if it's not
3836+ been unplugged. If it's been unplugged, then calling
3837+ IOHIDDeviceClose() will crash. */
3838+ if (!dev->disconnected) {
3839+ IOHIDDeviceClose(dev->device_handle, kIOHIDOptionsTypeNone);
3840+ }
3841+
3842+ /* Clear out the queue of received reports. */
3843+ pthread_mutex_lock(&dev->mutex);
3844+ while (dev->input_reports) {
3845+ return_data(dev, NULL, 0);
3846+ }
3847+ pthread_mutex_unlock(&dev->mutex);
3848+
3849+ free_hid_device(dev);
3850+}
3851+
3852+int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
3853+{
3854+ return get_manufacturer_string(dev->device_handle, string, maxlen);
3855+}
3856+
3857+int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
3858+{
3859+ return get_product_string(dev->device_handle, string, maxlen);
3860+}
3861+
3862+int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
3863+{
3864+ return get_serial_number(dev->device_handle, string, maxlen);
3865+}
3866+
3867+int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
3868+{
3869+ // TODO:
3870+
3871+ return 0;
3872+}
3873+
3874+
3875+HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
3876+{
3877+ // TODO:
3878+
3879+ return NULL;
3880+}
3881+
3882+
3883+
3884+
3885+
3886+
3887+#if 0
3888+static int32_t get_location_id(IOHIDDeviceRef device)
3889+{
3890+ return get_int_property(device, CFSTR(kIOHIDLocationIDKey));
3891+}
3892+
3893+static int32_t get_usage(IOHIDDeviceRef device)
3894+{
3895+ int32_t res;
3896+ res = get_int_property(device, CFSTR(kIOHIDDeviceUsageKey));
3897+ if (!res)
3898+ res = get_int_property(device, CFSTR(kIOHIDPrimaryUsageKey));
3899+ return res;
3900+}
3901+
3902+static int32_t get_usage_page(IOHIDDeviceRef device)
3903+{
3904+ int32_t res;
3905+ res = get_int_property(device, CFSTR(kIOHIDDeviceUsagePageKey));
3906+ if (!res)
3907+ res = get_int_property(device, CFSTR(kIOHIDPrimaryUsagePageKey));
3908+ return res;
3909+}
3910+
3911+static int get_transport(IOHIDDeviceRef device, wchar_t *buf, size_t len)
3912+{
3913+ return get_string_property(device, CFSTR(kIOHIDTransportKey), buf, len);
3914+}
3915+
3916+
3917+int main(void)
3918+{
3919+ IOHIDManagerRef mgr;
3920+ int i;
3921+
3922+ mgr = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
3923+ IOHIDManagerSetDeviceMatching(mgr, NULL);
3924+ IOHIDManagerOpen(mgr, kIOHIDOptionsTypeNone);
3925+
3926+ CFSetRef device_set = IOHIDManagerCopyDevices(mgr);
3927+
3928+ CFIndex num_devices = CFSetGetCount(device_set);
3929+ IOHIDDeviceRef *device_array = calloc(num_devices, sizeof(IOHIDDeviceRef));
3930+ CFSetGetValues(device_set, (const void **) device_array);
3931+
3932+ setlocale(LC_ALL, "");
3933+
3934+ for (i = 0; i < num_devices; i++) {
3935+ IOHIDDeviceRef dev = device_array[i];
3936+ printf("Device: %p\n", dev);
3937+ printf(" %04hx %04hx\n", get_vendor_id(dev), get_product_id(dev));
3938+
3939+ wchar_t serial[256], buf[256];
3940+ char cbuf[256];
3941+ get_serial_number(dev, serial, 256);
3942+
3943+
3944+ printf(" Serial: %ls\n", serial);
3945+ printf(" Loc: %ld\n", get_location_id(dev));
3946+ get_transport(dev, buf, 256);
3947+ printf(" Trans: %ls\n", buf);
3948+ make_path(dev, cbuf, 256);
3949+ printf(" Path: %s\n", cbuf);
3950+
3951+ }
3952+
3953+ return 0;
3954+}
3955+#endif
3956
3957=== added directory 'mixxx/lib/hidapi-0.7.0/windows'
3958=== added file 'mixxx/lib/hidapi-0.7.0/windows/.gitignore'
3959--- mixxx/lib/hidapi-0.7.0/windows/.gitignore 1970-01-01 00:00:00 +0000
3960+++ mixxx/lib/hidapi-0.7.0/windows/.gitignore 2012-04-25 15:44:23 +0000
3961@@ -0,0 +1,11 @@
3962+Debug
3963+Release
3964+*.exp
3965+*.ilk
3966+*.lib
3967+*.suo
3968+*.vcproj.*
3969+*.ncb
3970+*.suo
3971+*.dll
3972+*.pdb
3973
3974=== added file 'mixxx/lib/hidapi-0.7.0/windows/Makefile'
3975--- mixxx/lib/hidapi-0.7.0/windows/Makefile 1970-01-01 00:00:00 +0000
3976+++ mixxx/lib/hidapi-0.7.0/windows/Makefile 2012-04-25 15:44:23 +0000
3977@@ -0,0 +1,14 @@
3978+
3979+
3980+OS=$(shell uname)
3981+
3982+ifneq (,$(findstring MINGW,$(OS)))
3983+ FILE=Makefile.mingw
3984+endif
3985+
3986+ifeq ($(FILE), )
3987+all:
3988+ $(error Your platform ${OS} is not supported at this time.)
3989+endif
3990+
3991+include $(FILE)
3992
3993=== added file 'mixxx/lib/hidapi-0.7.0/windows/Makefile.mingw'
3994--- mixxx/lib/hidapi-0.7.0/windows/Makefile.mingw 1970-01-01 00:00:00 +0000
3995+++ mixxx/lib/hidapi-0.7.0/windows/Makefile.mingw 2012-04-25 15:44:23 +0000
3996@@ -0,0 +1,32 @@
3997+###########################################
3998+# Simple Makefile for HIDAPI test program
3999+#
4000+# Alan Ott
4001+# Signal 11 Software
4002+# 2010-06-01
4003+###########################################
4004+
4005+all: hidtest
4006+
4007+CC=gcc
4008+CXX=g++
4009+COBJS=hid.o
4010+CPPOBJS=../hidtest/hidtest.o
4011+OBJS=$(COBJS) $(CPPOBJS)
4012+CFLAGS=-I../hidapi -g -c
4013+LIBS= -lsetupapi
4014+
4015+
4016+hidtest: $(OBJS)
4017+ g++ -g $^ $(LIBS) -o hidtest
4018+
4019+$(COBJS): %.o: %.c
4020+ $(CC) $(CFLAGS) $< -o $@
4021+
4022+$(CPPOBJS): %.o: %.cpp
4023+ $(CXX) $(CFLAGS) $< -o $@
4024+
4025+clean:
4026+ rm *.o ../hidtest/*.o hidtest.exe
4027+
4028+.PHONY: clean
4029
4030=== added directory 'mixxx/lib/hidapi-0.7.0/windows/ddk_build'
4031=== added file 'mixxx/lib/hidapi-0.7.0/windows/ddk_build/.gitignore'
4032--- mixxx/lib/hidapi-0.7.0/windows/ddk_build/.gitignore 1970-01-01 00:00:00 +0000
4033+++ mixxx/lib/hidapi-0.7.0/windows/ddk_build/.gitignore 2012-04-25 15:44:23 +0000
4034@@ -0,0 +1,2 @@
4035+*.log
4036+obj*_*_*
4037\ No newline at end of file
4038
4039=== added file 'mixxx/lib/hidapi-0.7.0/windows/ddk_build/hidapi.def'
4040--- mixxx/lib/hidapi-0.7.0/windows/ddk_build/hidapi.def 1970-01-01 00:00:00 +0000
4041+++ mixxx/lib/hidapi-0.7.0/windows/ddk_build/hidapi.def 2012-04-25 15:44:23 +0000
4042@@ -0,0 +1,17 @@
4043+LIBRARY hidapi
4044+EXPORTS
4045+ hid_open @1
4046+ hid_write @2
4047+ hid_read @3
4048+ hid_close @4
4049+ hid_get_product_string @5
4050+ hid_get_manufacturer_string @6
4051+ hid_get_serial_number_string @7
4052+ hid_get_indexed_string @8
4053+ hid_error @9
4054+ hid_set_nonblocking @10
4055+ hid_enumerate @11
4056+ hid_open_path @12
4057+ hid_send_feature_report @13
4058+ hid_get_feature_report @14
4059+
4060\ No newline at end of file
4061
4062=== added file 'mixxx/lib/hidapi-0.7.0/windows/ddk_build/makefile'
4063--- mixxx/lib/hidapi-0.7.0/windows/ddk_build/makefile 1970-01-01 00:00:00 +0000
4064+++ mixxx/lib/hidapi-0.7.0/windows/ddk_build/makefile 2012-04-25 15:44:23 +0000
4065@@ -0,0 +1,49 @@
4066+#############################################################################
4067+#
4068+# Copyright (C) Microsoft Corporation 1995, 1996
4069+# All Rights Reserved.
4070+#
4071+# MAKEFILE for HID directory
4072+#
4073+#############################################################################
4074+
4075+!IFDEF WIN95_BUILD
4076+
4077+ROOT=..\..\..\..
4078+
4079+VERSIONLIST = debug retail
4080+IS_32 = TRUE
4081+IS_SDK = TRUE
4082+IS_PRIVATE = TRUE
4083+IS_SDK = TRUE
4084+IS_DDK = TRUE
4085+WIN32 = TRUE
4086+COMMONMKFILE = hidapi.mk
4087+
4088+!include $(ROOT)\dev\master.mk
4089+
4090+
4091+!ELSE
4092+
4093+#
4094+# DO NOT EDIT THIS FILE!!! Edit .\sources. if you want to add a new source
4095+# file to this component. This file merely indirects to the real make file
4096+# that is shared by all the driver components of the Windows NT DDK
4097+#
4098+
4099+!IF DEFINED(_NT_TARGET_VERSION)
4100+! IF $(_NT_TARGET_VERSION)>=0x501
4101+! INCLUDE $(NTMAKEENV)\makefile.def
4102+! ELSE
4103+# Only warn once per directory
4104+! INCLUDE $(NTMAKEENV)\makefile.plt
4105+! IF "$(BUILD_PASS)"=="PASS1"
4106+! message BUILDMSG: Warning : The sample "$(MAKEDIR)" is not valid for the current OS target.
4107+! ENDIF
4108+! ENDIF
4109+!ELSE
4110+! INCLUDE $(NTMAKEENV)\makefile.def
4111+!ENDIF
4112+
4113+!ENDIF
4114+
4115
4116=== added file 'mixxx/lib/hidapi-0.7.0/windows/ddk_build/sources'
4117--- mixxx/lib/hidapi-0.7.0/windows/ddk_build/sources 1970-01-01 00:00:00 +0000
4118+++ mixxx/lib/hidapi-0.7.0/windows/ddk_build/sources 2012-04-25 15:44:23 +0000
4119@@ -0,0 +1,23 @@
4120+TARGETNAME=hidapi
4121+TARGETTYPE=DYNLINK
4122+UMTYPE=console
4123+UMENTRY=main
4124+
4125+MSC_WARNING_LEVEL=/W3 /WX
4126+
4127+TARGETLIBS=$(SDK_LIB_PATH)\hid.lib \
4128+ $(SDK_LIB_PATH)\setupapi.lib \
4129+ $(SDK_LIB_PATH)\kernel32.lib \
4130+ $(SDK_LIB_PATH)\comdlg32.lib
4131+
4132+USE_MSVCRT=1
4133+
4134+INCLUDES= ..\..\hidapi
4135+SOURCES= ..\hid.c \
4136+
4137+
4138+TARGET_DESTINATION=retail
4139+
4140+MUI=0
4141+MUI_COMMENT="HID Interface DLL"
4142+
4143
4144=== added file 'mixxx/lib/hidapi-0.7.0/windows/hid.c'
4145--- mixxx/lib/hidapi-0.7.0/windows/hid.c 1970-01-01 00:00:00 +0000
4146+++ mixxx/lib/hidapi-0.7.0/windows/hid.c 2012-04-25 15:44:23 +0000
4147@@ -0,0 +1,873 @@
4148+/*******************************************************
4149+ HIDAPI - Multi-Platform library for
4150+ communication with HID devices.
4151+
4152+ Alan Ott
4153+ Signal 11 Software
4154+
4155+ 8/22/2009
4156+
4157+ Copyright 2009, All Rights Reserved.
4158+
4159+ At the discretion of the user of this library,
4160+ this software may be licensed under the terms of the
4161+ GNU Public License v3, a BSD-Style license, or the
4162+ original HIDAPI license as outlined in the LICENSE.txt,
4163+ LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt
4164+ files located at the root of the source distribution.
4165+ These files may also be found in the public source
4166+ code repository located at:
4167+ http://github.com/signal11/hidapi .
4168+********************************************************/
4169+
4170+#include <windows.h>
4171+
4172+#ifndef _NTDEF_
4173+typedef LONG NTSTATUS;
4174+#endif
4175+
4176+#ifdef __MINGW32__
4177+#include <ntdef.h>
4178+#include <winbase.h>
4179+#endif
4180+
4181+#ifdef __CYGWIN__
4182+#include <ntdef.h>
4183+#define _wcsdup wcsdup
4184+#endif
4185+
4186+//#define HIDAPI_USE_DDK
4187+
4188+#ifdef __cplusplus
4189+extern "C" {
4190+#endif
4191+ #include <setupapi.h>
4192+ #include <winioctl.h>
4193+ #ifdef HIDAPI_USE_DDK
4194+ #include <hidsdi.h>
4195+ #endif
4196+
4197+ // Copied from inc/ddk/hidclass.h, part of the Windows DDK.
4198+ #define HID_OUT_CTL_CODE(id) \
4199+ CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
4200+ #define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
4201+
4202+#ifdef __cplusplus
4203+} // extern "C"
4204+#endif
4205+
4206+#include <stdio.h>
4207+#include <stdlib.h>
4208+
4209+
4210+#include "hidapi.h"
4211+
4212+#ifdef _MSC_VER
4213+ // Thanks Microsoft, but I know how to use strncpy().
4214+ #pragma warning(disable:4996)
4215+#endif
4216+
4217+#ifdef __cplusplus
4218+extern "C" {
4219+#endif
4220+
4221+#ifndef HIDAPI_USE_DDK
4222+ // Since we're not building with the DDK, and the HID header
4223+ // files aren't part of the SDK, we have to define all this
4224+ // stuff here. In lookup_functions(), the function pointers
4225+ // defined below are set.
4226+ typedef struct _HIDD_ATTRIBUTES{
4227+ ULONG Size;
4228+ USHORT VendorID;
4229+ USHORT ProductID;
4230+ USHORT VersionNumber;
4231+ } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
4232+
4233+ typedef USHORT USAGE;
4234+ typedef struct _HIDP_CAPS {
4235+ USAGE Usage;
4236+ USAGE UsagePage;
4237+ USHORT InputReportByteLength;
4238+ USHORT OutputReportByteLength;
4239+ USHORT FeatureReportByteLength;
4240+ USHORT Reserved[17];
4241+ USHORT fields_not_used_by_hidapi[10];
4242+ } HIDP_CAPS, *PHIDP_CAPS;
4243+ typedef char* HIDP_PREPARSED_DATA;
4244+ #define HIDP_STATUS_SUCCESS 0x0
4245+
4246+ typedef BOOLEAN (__stdcall *HidD_GetAttributes_)(HANDLE device, PHIDD_ATTRIBUTES attrib);
4247+ typedef BOOLEAN (__stdcall *HidD_GetSerialNumberString_)(HANDLE device, PVOID buffer, ULONG buffer_len);
4248+ typedef BOOLEAN (__stdcall *HidD_GetManufacturerString_)(HANDLE handle, PVOID buffer, ULONG buffer_len);
4249+ typedef BOOLEAN (__stdcall *HidD_GetProductString_)(HANDLE handle, PVOID buffer, ULONG buffer_len);
4250+ typedef BOOLEAN (__stdcall *HidD_SetFeature_)(HANDLE handle, PVOID data, ULONG length);
4251+ typedef BOOLEAN (__stdcall *HidD_GetFeature_)(HANDLE handle, PVOID data, ULONG length);
4252+ typedef BOOLEAN (__stdcall *HidD_GetIndexedString_)(HANDLE handle, ULONG string_index, PVOID buffer, ULONG buffer_len);
4253+ typedef BOOLEAN (__stdcall *HidD_GetPreparsedData_)(HANDLE handle, HIDP_PREPARSED_DATA **preparsed_data);
4254+ typedef BOOLEAN (__stdcall *HidD_FreePreparsedData_)(HIDP_PREPARSED_DATA *preparsed_data);
4255+ typedef BOOLEAN (__stdcall *HidP_GetCaps_)(HIDP_PREPARSED_DATA *preparsed_data, HIDP_CAPS *caps);
4256+
4257+ static HidD_GetAttributes_ HidD_GetAttributes;
4258+ static HidD_GetSerialNumberString_ HidD_GetSerialNumberString;
4259+ static HidD_GetManufacturerString_ HidD_GetManufacturerString;
4260+ static HidD_GetProductString_ HidD_GetProductString;
4261+ static HidD_SetFeature_ HidD_SetFeature;
4262+ static HidD_GetFeature_ HidD_GetFeature;
4263+ static HidD_GetIndexedString_ HidD_GetIndexedString;
4264+ static HidD_GetPreparsedData_ HidD_GetPreparsedData;
4265+ static HidD_FreePreparsedData_ HidD_FreePreparsedData;
4266+ static HidP_GetCaps_ HidP_GetCaps;
4267+
4268+ static HMODULE lib_handle = NULL;
4269+ static BOOLEAN initialized = FALSE;
4270+#endif // HIDAPI_USE_DDK
4271+
4272+struct hid_device_ {
4273+ HANDLE device_handle;
4274+ BOOL blocking;
4275+ size_t input_report_length;
4276+ void *last_error_str;
4277+ DWORD last_error_num;
4278+ BOOL read_pending;
4279+ char *read_buf;
4280+ OVERLAPPED ol;
4281+};
4282+
4283+static hid_device *new_hid_device()
4284+{
4285+ hid_device *dev = (hid_device*) calloc(1, sizeof(hid_device));
4286+ dev->device_handle = INVALID_HANDLE_VALUE;
4287+ dev->blocking = TRUE;
4288+ dev->input_report_length = 0;
4289+ dev->last_error_str = NULL;
4290+ dev->last_error_num = 0;
4291+ dev->read_pending = FALSE;
4292+ dev->read_buf = NULL;
4293+ memset(&dev->ol, 0, sizeof(dev->ol));
4294+ dev->ol.hEvent = CreateEvent(NULL, FALSE, FALSE /*inital state f=nonsignaled*/, NULL);
4295+
4296+ return dev;
4297+}
4298+
4299+
4300+static void register_error(hid_device *device, const char *op)
4301+{
4302+ WCHAR *ptr, *msg;
4303+
4304+ FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
4305+ FORMAT_MESSAGE_FROM_SYSTEM |
4306+ FORMAT_MESSAGE_IGNORE_INSERTS,
4307+ NULL,
4308+ GetLastError(),
4309+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
4310+ (LPWSTR)&msg, 0/*sz*/,
4311+ NULL);
4312+
4313+ // Get rid of the CR and LF that FormatMessage() sticks at the
4314+ // end of the message. Thanks Microsoft!
4315+ ptr = msg;
4316+ while (*ptr) {
4317+ if (*ptr == '\r') {
4318+ *ptr = 0x0000;
4319+ break;
4320+ }
4321+ ptr++;
4322+ }
4323+
4324+ // Store the message off in the Device entry so that
4325+ // the hid_error() function can pick it up.
4326+ LocalFree(device->last_error_str);
4327+ device->last_error_str = msg;
4328+}
4329+
4330+#ifndef HIDAPI_USE_DDK
4331+static int lookup_functions()
4332+{
4333+ lib_handle = LoadLibraryA("hid.dll");
4334+ if (lib_handle) {
4335+#define RESOLVE(x) x = (x##_)GetProcAddress(lib_handle, #x); if (!x) return -1;
4336+ RESOLVE(HidD_GetAttributes);
4337+ RESOLVE(HidD_GetSerialNumberString);
4338+ RESOLVE(HidD_GetManufacturerString);
4339+ RESOLVE(HidD_GetProductString);
4340+ RESOLVE(HidD_SetFeature);
4341+ RESOLVE(HidD_GetFeature);
4342+ RESOLVE(HidD_GetIndexedString);
4343+ RESOLVE(HidD_GetPreparsedData);
4344+ RESOLVE(HidD_FreePreparsedData);
4345+ RESOLVE(HidP_GetCaps);
4346+#undef RESOLVE
4347+ }
4348+ else
4349+ return -1;
4350+
4351+ return 0;
4352+}
4353+#endif
4354+
4355+static HANDLE open_device(const char *path)
4356+{
4357+ HANDLE handle;
4358+
4359+ /* First, try to open with sharing mode turned off. This will make it so
4360+ that a HID device can only be opened once. This is to be consistent
4361+ with the behavior on the other platforms. */
4362+ handle = CreateFileA(path,
4363+ GENERIC_WRITE |GENERIC_READ,
4364+ 0, /*share mode*/
4365+ NULL,
4366+ OPEN_EXISTING,
4367+ FILE_FLAG_OVERLAPPED,//FILE_ATTRIBUTE_NORMAL,
4368+ 0);
4369+
4370+ if (handle == INVALID_HANDLE_VALUE) {
4371+ /* Couldn't open the device. Some devices must be opened
4372+ with sharing enabled (even though they are only opened once),
4373+ so try it here. */
4374+ handle = CreateFileA(path,
4375+ GENERIC_WRITE |GENERIC_READ,
4376+ FILE_SHARE_READ|FILE_SHARE_WRITE, /*share mode*/
4377+ NULL,
4378+ OPEN_EXISTING,
4379+ FILE_FLAG_OVERLAPPED,//FILE_ATTRIBUTE_NORMAL,
4380+ 0);
4381+ }
4382+
4383+ return handle;
4384+}
4385+
4386+int HID_API_EXPORT hid_init(void)
4387+{
4388+#ifndef HIDAPI_USE_DDK
4389+ if (!initialized) {
4390+ if (lookup_functions() < 0) {
4391+ hid_exit();
4392+ return -1;
4393+ }
4394+ initialized = TRUE;
4395+ }
4396+#endif
4397+ return 0;
4398+}
4399+
4400+int HID_API_EXPORT hid_exit(void)
4401+{
4402+#ifndef HIDAPI_USE_DDK
4403+ if (lib_handle)
4404+ FreeLibrary(lib_handle);
4405+ lib_handle = NULL;
4406+ initialized = FALSE;
4407+#endif
4408+ return 0;
4409+}
4410+
4411+struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
4412+{
4413+ BOOL res;
4414+ struct hid_device_info *root = NULL; // return object
4415+ struct hid_device_info *cur_dev = NULL;
4416+
4417+ // Windows objects for interacting with the driver.
4418+ GUID InterfaceClassGuid = {0x4d1e55b2, 0xf16f, 0x11cf, {0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30} };
4419+ SP_DEVINFO_DATA devinfo_data;
4420+ SP_DEVICE_INTERFACE_DATA device_interface_data;
4421+ SP_DEVICE_INTERFACE_DETAIL_DATA_A *device_interface_detail_data = NULL;
4422+ HDEVINFO device_info_set = INVALID_HANDLE_VALUE;
4423+ int device_index = 0;
4424+
4425+ if (hid_init() < 0)
4426+ return NULL;
4427+
4428+ // Initialize the Windows objects.
4429+ devinfo_data.cbSize = sizeof(SP_DEVINFO_DATA);
4430+ device_interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
4431+
4432+ // Get information for all the devices belonging to the HID class.
4433+ device_info_set = SetupDiGetClassDevsA(&InterfaceClassGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
4434+
4435+ // Iterate over each device in the HID class, looking for the right one.
4436+
4437+ for (;;) {
4438+ HANDLE write_handle = INVALID_HANDLE_VALUE;
4439+ DWORD required_size = 0;
4440+ HIDD_ATTRIBUTES attrib;
4441+
4442+ res = SetupDiEnumDeviceInterfaces(device_info_set,
4443+ NULL,
4444+ &InterfaceClassGuid,
4445+ device_index,
4446+ &device_interface_data);
4447+
4448+ if (!res) {
4449+ // A return of FALSE from this function means that
4450+ // there are no more devices.
4451+ break;
4452+ }
4453+
4454+ // Call with 0-sized detail size, and let the function
4455+ // tell us how long the detail struct needs to be. The
4456+ // size is put in &required_size.
4457+ res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
4458+ &device_interface_data,
4459+ NULL,
4460+ 0,
4461+ &required_size,
4462+ NULL);
4463+
4464+ // Allocate a long enough structure for device_interface_detail_data.
4465+ device_interface_detail_data = (SP_DEVICE_INTERFACE_DETAIL_DATA_A*) malloc(required_size);
4466+ device_interface_detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_A);
4467+
4468+ // Get the detailed data for this device. The detail data gives us
4469+ // the device path for this device, which is then passed into
4470+ // CreateFile() to get a handle to the device.
4471+ res = SetupDiGetDeviceInterfaceDetailA(device_info_set,
4472+ &device_interface_data,
4473+ device_interface_detail_data,
4474+ required_size,
4475+ NULL,
4476+ NULL);
4477+
4478+ if (!res) {
4479+ //register_error(dev, "Unable to call SetupDiGetDeviceInterfaceDetail");
4480+ // Continue to the next device.
4481+ goto cont;
4482+ }
4483+
4484+ //wprintf(L"HandleName: %s\n", device_interface_detail_data->DevicePath);
4485+
4486+ // Open a handle to the device
4487+ write_handle = open_device(device_interface_detail_data->DevicePath);
4488+
4489+ // Check validity of write_handle.
4490+ if (write_handle == INVALID_HANDLE_VALUE) {
4491+ // Unable to open the device.
4492+ //register_error(dev, "CreateFile");
4493+ goto cont_close;
4494+ }
4495+
4496+
4497+ // Get the Vendor ID and Product ID for this device.
4498+ attrib.Size = sizeof(HIDD_ATTRIBUTES);
4499+ HidD_GetAttributes(write_handle, &attrib);
4500+ //wprintf(L"Product/Vendor: %x %x\n", attrib.ProductID, attrib.VendorID);
4501+
4502+ // Check the VID/PID to see if we should add this
4503+ // device to the enumeration list.
4504+ if ((vendor_id == 0x0 && product_id == 0x0) ||
4505+ (attrib.VendorID == vendor_id && attrib.ProductID == product_id)) {
4506+
4507+ #define WSTR_LEN 512
4508+ const char *str;
4509+ struct hid_device_info *tmp;
4510+ HIDP_PREPARSED_DATA *pp_data = NULL;
4511+ HIDP_CAPS caps;
4512+ BOOLEAN res;
4513+ NTSTATUS nt_res;
4514+ wchar_t wstr[WSTR_LEN]; // TODO: Determine Size
4515+ size_t len;
4516+
4517+ /* VID/PID match. Create the record. */
4518+ tmp = (struct hid_device_info*) calloc(1, sizeof(struct hid_device_info));
4519+ if (cur_dev) {
4520+ cur_dev->next = tmp;
4521+ }
4522+ else {
4523+ root = tmp;
4524+ }
4525+ cur_dev = tmp;
4526+
4527+ // Get the Usage Page and Usage for this device.
4528+ res = HidD_GetPreparsedData(write_handle, &pp_data);
4529+ if (res) {
4530+ nt_res = HidP_GetCaps(pp_data, &caps);
4531+ if (nt_res == HIDP_STATUS_SUCCESS) {
4532+ cur_dev->usage_page = caps.UsagePage;
4533+ cur_dev->usage = caps.Usage;
4534+ }
4535+
4536+ HidD_FreePreparsedData(pp_data);
4537+ }
4538+
4539+ /* Fill out the record */
4540+ cur_dev->next = NULL;
4541+ str = device_interface_detail_data->DevicePath;
4542+ if (str) {
4543+ len = strlen(str);
4544+ cur_dev->path = (char*) calloc(len+1, sizeof(char));
4545+ strncpy(cur_dev->path, str, len+1);
4546+ cur_dev->path[len] = '\0';
4547+ }
4548+ else
4549+ cur_dev->path = NULL;
4550+
4551+ /* Serial Number */
4552+ res = HidD_GetSerialNumberString(write_handle, wstr, sizeof(wstr));
4553+ wstr[WSTR_LEN-1] = 0x0000;
4554+ if (res) {
4555+ cur_dev->serial_number = _wcsdup(wstr);
4556+ }
4557+
4558+ /* Manufacturer String */
4559+ res = HidD_GetManufacturerString(write_handle, wstr, sizeof(wstr));
4560+ wstr[WSTR_LEN-1] = 0x0000;
4561+ if (res) {
4562+ cur_dev->manufacturer_string = _wcsdup(wstr);
4563+ }
4564+
4565+ /* Product String */
4566+ res = HidD_GetProductString(write_handle, wstr, sizeof(wstr));
4567+ wstr[WSTR_LEN-1] = 0x0000;
4568+ if (res) {
4569+ cur_dev->product_string = _wcsdup(wstr);
4570+ }
4571+
4572+ /* VID/PID */
4573+ cur_dev->vendor_id = attrib.VendorID;
4574+ cur_dev->product_id = attrib.ProductID;
4575+
4576+ /* Release Number */
4577+ cur_dev->release_number = attrib.VersionNumber;
4578+
4579+ /* Interface Number. It can sometimes be parsed out of the path
4580+ on Windows if a device has multiple interfaces. See
4581+ http://msdn.microsoft.com/en-us/windows/hardware/gg487473 or
4582+ search for "Hardware IDs for HID Devices" at MSDN. If it's not
4583+ in the path, it's set to -1. */
4584+ cur_dev->interface_number = -1;
4585+ if (cur_dev->path) {
4586+ char *interface_component = strstr(cur_dev->path, "&mi_");
4587+ if (interface_component) {
4588+ char *hex_str = interface_component + 4;
4589+ char *endptr = NULL;
4590+ cur_dev->interface_number = strtol(hex_str, &endptr, 16);
4591+ if (endptr == hex_str) {
4592+ /* The parsing failed. Set interface_number to -1. */
4593+ cur_dev->interface_number = -1;
4594+ }
4595+ }
4596+ }
4597+ }
4598+
4599+cont_close:
4600+ CloseHandle(write_handle);
4601+cont:
4602+ // We no longer need the detail data. It can be freed
4603+ free(device_interface_detail_data);
4604+
4605+ device_index++;
4606+
4607+ }
4608+
4609+ // Close the device information handle.
4610+ SetupDiDestroyDeviceInfoList(device_info_set);
4611+
4612+ return root;
4613+
4614+}
4615+
4616+void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
4617+{
4618+ // TODO: Merge this with the Linux version. This function is platform-independent.
4619+ struct hid_device_info *d = devs;
4620+ while (d) {
4621+ struct hid_device_info *next = d->next;
4622+ free(d->path);
4623+ free(d->serial_number);
4624+ free(d->manufacturer_string);
4625+ free(d->product_string);
4626+ free(d);
4627+ d = next;
4628+ }
4629+}
4630+
4631+
4632+HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, wchar_t *serial_number)
4633+{
4634+ // TODO: Merge this functions with the Linux version. This function should be platform independent.
4635+ struct hid_device_info *devs, *cur_dev;
4636+ const char *path_to_open = NULL;
4637+ hid_device *handle = NULL;
4638+
4639+ devs = hid_enumerate(vendor_id, product_id);
4640+ cur_dev = devs;
4641+ while (cur_dev) {
4642+ if (cur_dev->vendor_id == vendor_id &&
4643+ cur_dev->product_id == product_id) {
4644+ if (serial_number) {
4645+ if (wcscmp(serial_number, cur_dev->serial_number) == 0) {
4646+ path_to_open = cur_dev->path;
4647+ break;
4648+ }
4649+ }
4650+ else {
4651+ path_to_open = cur_dev->path;
4652+ break;
4653+ }
4654+ }
4655+ cur_dev = cur_dev->next;
4656+ }
4657+
4658+ if (path_to_open) {
4659+ /* Open the device */
4660+ handle = hid_open_path(path_to_open);
4661+ }
4662+
4663+ hid_free_enumeration(devs);
4664+
4665+ return handle;
4666+}
4667+
4668+HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path)
4669+{
4670+ hid_device *dev;
4671+ HIDP_CAPS caps;
4672+ HIDP_PREPARSED_DATA *pp_data = NULL;
4673+ BOOLEAN res;
4674+ NTSTATUS nt_res;
4675+
4676+ if (hid_init() < 0) {
4677+ return NULL;
4678+ }
4679+
4680+ dev = new_hid_device();
4681+
4682+ // Open a handle to the device
4683+ dev->device_handle = open_device(path);
4684+
4685+ // Check validity of write_handle.
4686+ if (dev->device_handle == INVALID_HANDLE_VALUE) {
4687+ // Unable to open the device.
4688+ register_error(dev, "CreateFile");
4689+ goto err;
4690+ }
4691+
4692+ // Get the Input Report length for the device.
4693+ res = HidD_GetPreparsedData(dev->device_handle, &pp_data);
4694+ if (!res) {
4695+ register_error(dev, "HidD_GetPreparsedData");
4696+ goto err;
4697+ }
4698+ nt_res = HidP_GetCaps(pp_data, &caps);
4699+ if (nt_res != HIDP_STATUS_SUCCESS) {
4700+ register_error(dev, "HidP_GetCaps");
4701+ goto err_pp_data;
4702+ }
4703+ dev->input_report_length = caps.InputReportByteLength;
4704+ HidD_FreePreparsedData(pp_data);
4705+
4706+ dev->read_buf = (char*) malloc(dev->input_report_length);
4707+
4708+ return dev;
4709+
4710+err_pp_data:
4711+ HidD_FreePreparsedData(pp_data);
4712+err:
4713+ CloseHandle(dev->device_handle);
4714+ free(dev);
4715+ return NULL;
4716+}
4717+
4718+int HID_API_EXPORT HID_API_CALL hid_write(hid_device *dev, const unsigned char *data, size_t length)
4719+{
4720+ DWORD bytes_written;
4721+ BOOL res;
4722+
4723+ OVERLAPPED ol;
4724+ memset(&ol, 0, sizeof(ol));
4725+
4726+ res = WriteFile(dev->device_handle, data, length, NULL, &ol);
4727+
4728+ if (!res) {
4729+ if (GetLastError() != ERROR_IO_PENDING) {
4730+ // WriteFile() failed. Return error.
4731+ register_error(dev, "WriteFile");
4732+ return -1;
4733+ }
4734+ }
4735+
4736+ // Wait here until the write is done. This makes
4737+ // hid_write() synchronous.
4738+ res = GetOverlappedResult(dev->device_handle, &ol, &bytes_written, TRUE/*wait*/);
4739+ if (!res) {
4740+ // The Write operation failed.
4741+ register_error(dev, "WriteFile");
4742+ return -1;
4743+ }
4744+
4745+ return bytes_written;
4746+}
4747+
4748+
4749+int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
4750+{
4751+ DWORD bytes_read = 0;
4752+ BOOL res;
4753+
4754+ // Copy the handle for convenience.
4755+ HANDLE ev = dev->ol.hEvent;
4756+
4757+ if (!dev->read_pending) {
4758+ // Start an Overlapped I/O read.
4759+ dev->read_pending = TRUE;
4760+ ResetEvent(ev);
4761+ res = ReadFile(dev->device_handle, dev->read_buf, dev->input_report_length, &bytes_read, &dev->ol);
4762+
4763+ if (!res) {
4764+ if (GetLastError() != ERROR_IO_PENDING) {
4765+ // ReadFile() has failed.
4766+ // Clean up and return error.
4767+ CancelIo(dev->device_handle);
4768+ dev->read_pending = FALSE;
4769+ goto end_of_function;
4770+ }
4771+ }
4772+ }
4773+
4774+ if (milliseconds >= 0) {
4775+ // See if there is any data yet.
4776+ res = WaitForSingleObject(ev, milliseconds);
4777+ if (res != WAIT_OBJECT_0) {
4778+ // There was no data this time. Return zero bytes available,
4779+ // but leave the Overlapped I/O running.
4780+ return 0;
4781+ }
4782+ }
4783+
4784+ // Either WaitForSingleObject() told us that ReadFile has completed, or
4785+ // we are in non-blocking mode. Get the number of bytes read. The actual
4786+ // data has been copied to the data[] array which was passed to ReadFile().
4787+ res = GetOverlappedResult(dev->device_handle, &dev->ol, &bytes_read, TRUE/*wait*/);
4788+
4789+ // Set pending back to false, even if GetOverlappedResult() returned error.
4790+ dev->read_pending = FALSE;
4791+
4792+ if (res && bytes_read > 0) {
4793+ if (dev->read_buf[0] == 0x0) {
4794+ /* If report numbers aren't being used, but Windows sticks a report
4795+ number (0x0) on the beginning of the report anyway. To make this
4796+ work like the other platforms, and to make it work more like the
4797+ HID spec, we'll skip over this byte. */
4798+ bytes_read--;
4799+ memcpy(data, dev->read_buf+1, length);
4800+ }
4801+ else {
4802+ /* Copy the whole buffer, report number and all. */
4803+ memcpy(data, dev->read_buf, length);
4804+ }
4805+ }
4806+
4807+end_of_function:
4808+ if (!res) {
4809+ register_error(dev, "GetOverlappedResult");
4810+ return -1;
4811+ }
4812+
4813+ return bytes_read;
4814+}
4815+
4816+int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length)
4817+{
4818+ return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0);
4819+}
4820+
4821+int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *dev, int nonblock)
4822+{
4823+ dev->blocking = !nonblock;
4824+ return 0; /* Success */
4825+}
4826+
4827+int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
4828+{
4829+ BOOL res = HidD_SetFeature(dev->device_handle, (PVOID)data, length);
4830+ if (!res) {
4831+ register_error(dev, "HidD_SetFeature");
4832+ return -1;
4833+ }
4834+
4835+ return length;
4836+}
4837+
4838+
4839+int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
4840+{
4841+ BOOL res;
4842+#if 0
4843+ res = HidD_GetFeature(dev->device_handle, data, length);
4844+ if (!res) {
4845+ register_error(dev, "HidD_GetFeature");
4846+ return -1;
4847+ }
4848+ return 0; /* HidD_GetFeature() doesn't give us an actual length, unfortunately */
4849+#else
4850+ DWORD bytes_returned;
4851+
4852+ OVERLAPPED ol;
4853+ memset(&ol, 0, sizeof(ol));
4854+
4855+ res = DeviceIoControl(dev->device_handle,
4856+ IOCTL_HID_GET_FEATURE,
4857+ data, length,
4858+ data, length,
4859+ &bytes_returned, &ol);
4860+
4861+ if (!res) {
4862+ if (GetLastError() != ERROR_IO_PENDING) {
4863+ // DeviceIoControl() failed. Return error.
4864+ register_error(dev, "Send Feature Report DeviceIoControl");
4865+ return -1;
4866+ }
4867+ }
4868+
4869+ // Wait here until the write is done. This makes
4870+ // hid_get_feature_report() synchronous.
4871+ res = GetOverlappedResult(dev->device_handle, &ol, &bytes_returned, TRUE/*wait*/);
4872+ if (!res) {
4873+ // The operation failed.
4874+ register_error(dev, "Send Feature Report GetOverLappedResult");
4875+ return -1;
4876+ }
4877+ return bytes_returned;
4878+#endif
4879+}
4880+
4881+void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)
4882+{
4883+ if (!dev)
4884+ return;
4885+ CancelIo(dev->device_handle);
4886+ CloseHandle(dev->ol.hEvent);
4887+ CloseHandle(dev->device_handle);
4888+ LocalFree(dev->last_error_str);
4889+ free(dev->read_buf);
4890+ free(dev);
4891+}
4892+
4893+int HID_API_EXPORT_CALL HID_API_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
4894+{
4895+ BOOL res;
4896+
4897+ res = HidD_GetManufacturerString(dev->device_handle, string, 2 * maxlen);
4898+ if (!res) {
4899+ register_error(dev, "HidD_GetManufacturerString");
4900+ return -1;
4901+ }
4902+
4903+ return 0;
4904+}
4905+
4906+int HID_API_EXPORT_CALL HID_API_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
4907+{
4908+ BOOL res;
4909+
4910+ res = HidD_GetProductString(dev->device_handle, string, 2 * maxlen);
4911+ if (!res) {
4912+ register_error(dev, "HidD_GetProductString");
4913+ return -1;
4914+ }
4915+
4916+ return 0;
4917+}
4918+
4919+int HID_API_EXPORT_CALL HID_API_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
4920+{
4921+ BOOL res;
4922+
4923+ res = HidD_GetSerialNumberString(dev->device_handle, string, 2 * maxlen);
4924+ if (!res) {
4925+ register_error(dev, "HidD_GetSerialNumberString");
4926+ return -1;
4927+ }
4928+
4929+ return 0;
4930+}
4931+
4932+int HID_API_EXPORT_CALL HID_API_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
4933+{
4934+ BOOL res;
4935+
4936+ res = HidD_GetIndexedString(dev->device_handle, string_index, string, 2 * maxlen);
4937+ if (!res) {
4938+ register_error(dev, "HidD_GetIndexedString");
4939+ return -1;
4940+ }
4941+
4942+ return 0;
4943+}
4944+
4945+
4946+HID_API_EXPORT const wchar_t * HID_API_CALL hid_error(hid_device *dev)
4947+{
4948+ return (wchar_t*)dev->last_error_str;
4949+}
4950+
4951+
4952+//#define PICPGM
4953+//#define S11
4954+#define P32
4955+#ifdef S11
4956+ unsigned short VendorID = 0xa0a0;
4957+ unsigned short ProductID = 0x0001;
4958+#endif
4959+
4960+#ifdef P32
4961+ unsigned short VendorID = 0x04d8;
4962+ unsigned short ProductID = 0x3f;
4963+#endif
4964+
4965+
4966+#ifdef PICPGM
4967+ unsigned short VendorID = 0x04d8;
4968+ unsigned short ProductID = 0x0033;
4969+#endif
4970+
4971+
4972+#if 0
4973+int __cdecl main(int argc, char* argv[])
4974+{
4975+ int res;
4976+ unsigned char buf[65];
4977+
4978+ UNREFERENCED_PARAMETER(argc);
4979+ UNREFERENCED_PARAMETER(argv);
4980+
4981+ // Set up the command buffer.
4982+ memset(buf,0x00,sizeof(buf));
4983+ buf[0] = 0;
4984+ buf[1] = 0x81;
4985+
4986+
4987+ // Open the device.
4988+ int handle = open(VendorID, ProductID, L"12345");
4989+ if (handle < 0)
4990+ printf("unable to open device\n");
4991+
4992+
4993+ // Toggle LED (cmd 0x80)
4994+ buf[1] = 0x80;
4995+ res = write(handle, buf, 65);
4996+ if (res < 0)
4997+ printf("Unable to write()\n");
4998+
4999+ // Request state (cmd 0x81)
5000+ buf[1] = 0x81;
The diff has been truncated for viewing.

Subscribers

People subscribed via source and target branches