We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4601

Summary: SDL HIDAPI Error error when running the 'Rectangles' example on Android
Product: SDL Reporter: Steve Robinson <ssrobins>
Component: buildAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: registrirayme, sylvain.becker, tolstenko
Version: 2.0.9   
Hardware: ARM   
OS: Android (All)   
Attachments: Error screen shot
Error screen shot
Error screen shot
CMakeLists-hidapi.txt
CMakeLists.diff
HIDDeviceManager.diff
SDL_config.h.diff
SDL_config_iphoneos.diff

Description Steve Robinson 2019-04-19 01:43:36 UTC
Created attachment 3753 [details]
Error screen shot

1. Build SDL 2.0.9 with CMake for Android
2. Build the 'Rectangles' example included in SDL2 source
3. Run the APK

I get an 'SDL HIDAPI Error' when running the apk (see the attached screen shot).

Upon further investigation, it's clear that the newly-added hidapi functionality was not added to the CMake build system included with SDL2.  The android-project/app/src/main/java/org/libsdl/app/HIDDeviceManager.java file used in the 'Rectangles' example is trying to load a hidapi shared library that doesn't exist.

So for users of CMake, SDL2 for Android is completely unusable and all other platforms do not include the new hidapi functionality.
Comment 1 Steve Robinson 2019-04-19 01:44:51 UTC
Created attachment 3754 [details]
Error screen shot
Comment 2 Steve Robinson 2019-04-19 01:46:47 UTC
Created attachment 3755 [details]
Error screen shot
Comment 3 Steve Robinson 2019-04-19 01:52:29 UTC
Created attachment 3756 [details]
CMakeLists-hidapi.txt
Comment 4 Steve Robinson 2019-04-19 01:53:08 UTC
Created attachment 3757 [details]
CMakeLists.diff
Comment 5 Steve Robinson 2019-04-19 01:53:45 UTC
Created attachment 3758 [details]
HIDDeviceManager.diff
Comment 6 Steve Robinson 2019-04-19 01:54:17 UTC
Created attachment 3759 [details]
SDL_config.h.diff
Comment 7 Steve Robinson 2019-04-19 01:54:42 UTC
Created attachment 3760 [details]
SDL_config_iphoneos.diff
Comment 8 Steve Robinson 2019-04-19 02:00:32 UTC
The work to add hidapi to the SDL2 CMake builds is here:
https://gitlab.com/ssrobins/conan-sdl2/tree/test_patch
...and the key files have been attached to this bug.

CMakeLists-hidapi.txt
Rename this file to CMakeLists.txt and put it in src/hidapi.  It defines the hidapi static library.  For linux, I chose the hidraw hid back-end instead of libusb since it supports USB and Bluetooth.  More info: https://github.com/signal11/hidapi

When using CMake to build, set BUILD_SHARED_LIBS to ON to build hidapi as a shared library.  Otherwise, it'll build as a static library.

The rest of the changes are defined in diff files from existing files from the 2.0.9 source zip.  The file path is defined in the first line of each file.

CMakeLists.diff
This is a patch of the main SDL2 CMake file.  This adds the joystick/hidapi/*.c files to SDL2 and references the CMake file that defines the hidapi build.  There are also several build changes to allow a sample program to build and run with SDL2.

HIDDeviceManager.diff
The code to load a hidapi shared library is removed since the SDL2 CMake file may be configured to build hidapi as a static library so loading the SDL2 library or a program that statically linked to the SDL2 library will be sufficient to load the hid native functions.  Instead of an unhandled exception if the functions aren't found, I now log to logcat and continue. 

SDL_config.h.diff
Add SDL_JOYSTICK_HIDAPI so the appropriate code is included when this feature is enabled.

SDL_config_iphoneos.diff
Enable SDL_JOYSTICK_HIDAPI for iOS


To test these changes, I took the rectangles example and built it with the above changes on Windows, Linux, Mac, iOS, and Android:
https://gitlab.com/ssrobins/sdl2-example/tree/test_sdl2.0.9

For all platforms except iOS you can download the built programs here:
https://gitlab.com/ssrobins/sdl2-example/pipelines/47924886
Just click on the platform, then either 'Download' or 'Browse'.

I also used the patched SDL 2.0.9 in a closed source project.

I ran the programs on:
Windows 10 Pro ver. 1809
Ubuntu 16.04 LTS 64-bit
macOS High Sierra 10.13.6
Moto X Pure Edition (Android)
iPhone 4S

Programs loaded and ran fine.
Comment 9 ZZ 2019-05-01 16:39:03 UTC
It looks like that I have been bitten hard by this issue today on Shield TV/AS3.4. I don't use the standard app layout and because of various reasons it took me half a day until I found this bug report. Will this be committed to a working repository any time soon?
Comment 10 ZZ 2019-08-24 09:57:57 UTC
There is still no support for hidapi with CMake builds in 2.0.10 I guess?
Comment 11 Steve Robinson 2019-08-24 21:43:21 UTC
Yeah, I didn't try it out, but looking through the code it looks like it's still not fixed.

Hey SDL2 authors, please make CMake your only build system. It's the dominant build system for multiplatform C/C++ work. It already mostly works to build SDL2, just keep it up-to-date. If you still want to provide Xcode, make, and Visual Studio projects just use what CMake generates.

Also, while we're on the topic of best practices, using GitHub/GitLab and a CI would help formalize the collaboration process. I bet it would speed up development for you as well.

Build, CI, and dev workflows are my professional specialty. I'm happy to help out because I think SDL2 is fantastically useful. So hey, stop ignoring me and let's make this happen :)
Comment 12 ZZ 2019-08-25 08:55:54 UTC
Steve, this patches above have submitted them? Do you have any ideas why aren't there in?
Every time I try to upgrade SDL2 have to manually fix the build and everytime I don't remember anything and I am wasting time researching the causes.

This patch should disable hidapi (somewhat): https://github.com/spurious/SDL-mirror/commit/79da113ed7eed61b97deb32d02834ccaa60bb0c6

I don't get why hidapi is necessary for Android and even if it is at all. And this README doesn't even state that it is supported on Android: https://github.com/spurious/SDL-mirror/tree/master/src/hidapi

So I don't know even if I should bother.
Comment 14 Sylvain 2019-08-25 14:49:37 UTC
hidapi is *not* necessary for Android, since you can use the SDL android port without it.

I mean, if you have a patch to make sure sdl cmake compile on android with hiapi, I'll add it. I just haven't tried to do it.
Comment 15 Sylvain 2019-08-25 14:51:35 UTC
indeed building with cmake needs some attention... there are 30 open bugs with cmake keyword.

https://bugzilla.libsdl.org/buglist.cgi?bug_status=NEW&bug_status=ASSIGNED&bug_status=REOPENED&list_id=38107&query_format=advanced&short_desc=cmake&short_desc_type=allwordssubstr
Comment 17 Steve Robinson 2020-04-19 08:06:12 UTC
CMake builds hidapi as of SDL 2.0.12 so I'm resolving this, thanks!
Comment 18 ZZ 2020-07-09 15:59:42 UTC
I don't see this happening on Android. Is there any trick?
Comment 19 ZZ 2020-07-09 16:01:33 UTC
Previously I used to do something like:

    // Setup
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // In SDLActivity.java define:
        //   protected static boolean mHIDAPIEnabled = true;
        // In SDLActivity.java in:
        //   protected void onCreate(Bundle savedInstanceState)
        // modify:
        //   mHIDDeviceManager = HIDDeviceManager.acquire(this);
        // to:
        //   if (SDLActivity.mHIDAPIEnabled) {
        //        mHIDDeviceManager = HIDDeviceManager.acquire(this);
        //   }

        // Disable HIDAPI library usage
        SDLActivity.mHIDAPIEnabled = false;

        super.onCreate(savedInstanceState);
    }
Comment 20 Steve Robinson 2020-07-09 16:05:47 UTC
I resolved this bug months ago, it's fixed as of SDL 2.0.12.
Comment 21 ZZ 2020-07-09 16:24:57 UTC
Unfortunately I still get this:
https://bugzilla.libsdl.org/show_bug.cgi?id=4776
about missing libhdapi.so.

I upgraded my sources to 2.0.12 recently.
And I don't see any libhdapi.so in the apk.
I build all the libraries with gradle via Android Studio.
Comment 22 ZZ 2020-07-09 16:28:58 UTC
Is it fixed for CMake?
Comment 23 ZZ 2020-07-09 17:36:51 UTC
Thanx. I figured it out.
Comment 24 Alexandre Tolstenko 2020-07-16 02:33:32 UTC
(In reply to ZZ from comment #21)
> Unfortunately I still get this:
> https://bugzilla.libsdl.org/show_bug.cgi?id=4776
> about missing libhdapi.so.
> 
> I upgraded my sources to 2.0.12 recently.
> And I don't see any libhdapi.so in the apk.
> I build all the libraries with gradle via Android Studio.

Hey man, I am facing the same issues. I've tried the master branch and the 2.0.12, and I am facing the same problem. How did you solve it?

Btw I am using the cmake approach too without clues.
Comment 25 Steve Robinson 2020-07-16 04:30:39 UTC
You have to copy the libhidapi.so alongside the Rectangles.so or do what I did and change hidapi to a static lib in CMakeLists.txt:
https://gitlab.com/ssrobins/conan-sdl2/-/blob/sdl2.0.12/CMakeLists.diff

..then remove the hidapi load command from HIDDeviceManager.java:
https://gitlab.com/ssrobins/conan-sdl2/-/blob/sdl2.0.12/HIDDeviceManager.diff

Here's my copy of the Rectangles example with the build files to make it all work:
https://gitlab.com/ssrobins/sdl2-example/-/tree/sdl2.0.12