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 1664

Summary: Accelerometer name returns constant value
Product: SDL Reporter: Joseba García Echebarria <joseba.gar>
Component: joystickAssignee: Sam Lantinga <slouken>
Status: RESOLVED INVALID QA Contact: Sam Lantinga <slouken>
Severity: enhancement    
Priority: P2 CC: philipp.wiesemann
Version: HG 2.0   
Hardware: All   
OS: Android (All)   
Attachments: Update accelerometer name.

Description Joseba García Echebarria 2012-12-15 14:15:13 UTC
Created attachment 998 [details]
Update accelerometer name.

In Android the accelerometer name is always returned as "Android accelerometer".

The attached patch will update the accelerometer name with the one given by Android for the sensor that called the onNativeAccel function.
Comment 1 Philipp Wiesemann 2012-12-16 02:58:16 UTC
This implementation sets the name again for every change from sensor. Maybe it could only be set once and then only changed if the name is different (what should happen very often :). Else there will be a lot of not needed string allocations.

Also the last string allocated with strndup() is always leaked when the name is set again.

In the old implementation the value of accelerometerName was always a pointer to a string ("Android accelerometer") but in this implementation it has an undefined value until a change from sensor. It may be used before which may be unwanted.

Maybe SDL_strdup() could be used instead of strndup() because the whole string will be copied anyway. Using strlen() does work which is not needed then.
Comment 2 Sam Lantinga 2012-12-31 16:35:37 UTC
Is it typical for there to be more than one accelerometer in an Android device?  What does the name typically look like?  Is it expensive to pass a Java string across the JNI interface?
Comment 3 Joseba García Echebarria 2013-01-01 08:40:09 UTC
Yesterday I finished a working joystick implementation for Android. It's based on an older version of SDL2 HG.
That code sets the accelerometer (and joystick) names when the joystick subsystem is initialized and avoids the leaks mentioned by freeing everything when the subsystem is closed.

I'll adapt the code for the latest version of SDL and will submit it to bugzilla for consideration so I'm closing this bug report.

Regards

(In reply to comment #2)
> Is it typical for there to be more than one accelerometer in an Android device?
>  What does the name typically look like?  Is it expensive to pass a Java string
> across the JNI interface?