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 1159

Summary: Patch for Android: protecting threads and multitouch
Product: SDL Reporter: Gueniffey <pierre-yves>
Component: videoAssignee: Sam Lantinga <slouken>
Status: RESOLVED DUPLICATE QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: gabomdq, jspradlin, tim
Version: HG 2.0   
Hardware: ARM   
OS: Android (All)   
Attachments: Android multitouch + thread protection
Android multitouch + thread protection
Patch for Android multitouch + thread protection

Description Gueniffey 2011-03-02 09:22:30 UTC
Attached is a patch that enables multitouch support for Android and fixes issues when the JNI environment is not accessible anymore(when calling from other threads or if it has been released)
Comment 1 Gueniffey 2011-03-02 09:23:24 UTC
Created attachment 593 [details]
Android multitouch + thread protection
Comment 2 Gueniffey 2011-03-03 00:34:10 UTC
Created attachment 594 [details]
Android multitouch + thread protection

The previous patch was messed out, sorry. Here is the right one.
Comment 3 Gueniffey 2011-03-03 03:28:11 UTC
Created attachment 595 [details]
Patch for Android multitouch + thread protection

New clean patch with everything working (sorry for the two first, which were messed up)
Comment 4 Sam Lantinga 2011-03-05 09:58:27 UTC
Thank you very much for your patch for SDL 1.3!

Do you give me permission to release your code with SDL 1.3 and future
versions of SDL under both the LGPL and a closed-source commercial
license?
Comment 5 Gueniffey 2011-03-05 10:00:33 UTC
(In reply to comment #4)
> Thank you very much for your patch for SDL 1.3!
> 
> Do you give me permission to release your code with SDL 1.3 and future
> versions of SDL under both the LGPL and a closed-source commercial
> license?

Yes of course, if you give me the permission to use your library under LGPL for our commercial Android application
Comment 6 Sam Lantinga 2011-03-05 10:05:24 UTC
Of course, as long as you comply with the LGPL license by dynamically linking, etc.
Comment 7 Sam Lantinga 2011-03-05 10:07:24 UTC
A few comments:

I noticed you updated the android target from android-4 (1.6) to android-7.  This breaks compatibility with older Android phones.  Was this intentional?

Your patch to attach and detach threads from the VM adds some overhead to the SDL calls.  What was the problem you were fixing by doing this?

Thanks!
Comment 8 Gueniffey 2011-03-07 00:21:48 UTC
The multitouch APIs are only available since API level 5 (Android 2.1). You can downgrade the target to this (I should have done it). For older versions, the old code with mouse pointers will still work and is still available, developers will need to make a few changes in the java code if they need to support Android 1.6.

The thread attachment code doesn't add any overhead, since it's only called for when the calling thread is different from the main SDL thread. With the current code, it would crash, as the JNI environmemnt cannot be accessed from other threads. Moreover, it could be released from the main thread and this would also lead to a crash.
For more info, check this out:
http://www.google.fr/url?sa=t&source=web&cd=1&ved=0CBgQFjAA&url=http%3A%2F%2Fandroid.wooyd.org%2FJNIExample%2Ffiles%2FJNIExample.pdf&rct=j&q=android%20jni%20attachcurrentthread&ei=ZZR0TZyiCIaMswb2qZSEDg&usg=AFQjCNHwjbm09fLt2PAgFYDRjSKlL3mQTA&sig2=N7PRNFcRbEuoqhSWOdmNfA&cad=rja
Comment 9 Gueniffey 2011-03-11 05:44:44 UTC
API level 5 is android 2.0, forgive my mistake.
Comment 10 Sam Lantinga 2011-03-11 15:58:31 UTC
In the multi-touch code, any touch up will send touch up for all current touches.  Is that intentional?

"The thread attachment code doesn't add any overhead, since it's only called for
when the calling thread is different from the main SDL thread."

Can any number of threads be attached to the VM?  If so, is there any reason to call this after each time you attach?
mJVM->DetachCurrentThread();

Thanks!
Comment 11 Gueniffey 2011-03-11 22:47:38 UTC
(In reply to comment #10)
> In the multi-touch code, any touch up will send touch up for all current
> touches.  Is that intentional?

Nope. Maybe there's a bug in my code.

> "The thread attachment code doesn't add any overhead, since it's only called
> for
> when the calling thread is different from the main SDL thread."
> 
> Can any number of threads be attached to the VM?  If so, is there any reason to
> call this after each time you attach?
> mJVM->DetachCurrentThread();

You need to detach any attached thread before it ends. How can you detach it anywhere else ?
Comment 12 Sam Lantinga 2011-03-11 23:50:56 UTC
You could keep track of attached threads add maybe add code to the SDL thread cleanup to take care of it.

What is the original case that you were trying to solve?
Comment 13 Gueniffey 2011-03-11 23:56:13 UTC
Well, that's possible, but you can use threads that are not SDL threads, right? In that case the app would crash when the thread ends.
I need to call a SDL function that triggered Android code from a separate thread, and my app crashed, since the JNI environment is not available for other threads.
Comment 14 Gueniffey 2011-03-19 14:14:55 UTC
I don't think there's a need to modify the patch (except if there's a bug in the multitouch handling code). So I suggest that you add it to the trunk, as there is still a crash that can occur in the current android code. Maybe somebody could make changes later if the fix is faulty.
Comment 15 Gabriel Jacobo 2012-06-21 07:55:40 UTC
Multitouch for Android is now fully functional, there's a threading issue with RWops that's being dealt with in bug #1422

*** This bug has been marked as a duplicate of bug 1422 ***