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 1294

Summary: Multitouch for Android
Product: SDL Reporter: Gabriel Jacobo <gabomdq>
Component: eventsAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus, tim
Version: HG 2.0   
Hardware: ARM   
OS: Android (All)   
Attachments: Multitouch for Android, includes debug stuff, dont use in production
Multitouch for Android
Multitouch for Android
Multitouch for Android

Description Gabriel Jacobo 2011-08-29 16:52:52 UTC
Created attachment 691 [details]
Multitouch for Android, includes debug stuff, dont use in production

I've tried to adapt the patch for bug #1159 and this http://forums.libsdl.org/viewtopic.php?t=7474&sid=3f2c638f7321bdcd6007e525a735456d into a working patch that we can apply to main.

It's mostly working except a big problem I've found (that happens at least in my setup)...while I'm getting perfectly valid float coordinates in x,y and pressure from Java (up until Android_OnTouch is called), calls from there on get their float parameters completely corrupted, like there's a different calling convention at work, or different float precision, something that I can't put my finger on...

I've left some debug printfs in the files so it's easier to look at this for anyone wanting to.
The printf inside Android_OnTouch prints valid, logic (as in they make sense) coordinates. But the printfs in SDL_SendFingerDown and SDL_SendTouchMotion print gibberish.

A sample:

Android_OnTouch: touch_device_id_in: 3, pointer_finger_id_in 0, action 2, x 937.267761, y 238.000000, p 1.600000
SDL_SendTouchMotion: id: 3, fingerid 0d, relative 0, xin 4.415300, yin 36893488147419103232.000000, pressurein 4.415300
Android_OnTouch: touch_device_id_in: 3, pointer_finger_id_in 0, action 2, x 979.234985, y 239.000000, p 1.200000
SDL_SendTouchMotion: id: 3, fingerid 0d, relative 0, xin 4.456284, yin 2.000000, pressurein 4.456284
Comment 1 Gabriel Jacobo 2011-08-29 18:32:45 UTC
Created attachment 692 [details]
Multitouch for Android

That wasn't so hard (I was missing a header) but it took me hours to figure out where the problem was! Multitouch should work with this patch.
Comment 2 Gabriel Jacobo 2011-08-30 11:40:57 UTC
Created attachment 694 [details]
Multitouch for Android

Adding a new version that accounts for two action types that are deprecated but are still being emitted by Android.
Comment 3 Tim Angus 2011-08-31 05:21:09 UTC
Looks pretty good, though the values of touch.native_xres and yres are wrong. The correct settings are actually commented out. It seems the original author has missed the point that the values that come out of the touch events are normalised to 0<=x<=32768. This is explained, though not very prominently, in README.touch. The code should look like this:

    if (!SDL_GetTouch(touchDeviceId)) {
        SDL_Touch touch;
        memset( &touch, 0, sizeof(touch) );
        touch.id = touchDeviceId;
        touch.x_min = 0.0f;
        touch.x_max = (float)Android_ScreenWidth;
        touch.native_xres = touch.x_max - touch.x_min;
        touch.y_min = 0.0f;
        touch.y_max = (float)Android_ScreenHeight;
        touch.native_yres = touch.y_max - touch.y_min;
        touch.pressure_min = 0.0f;
        touch.pressure_max = 1.0f;
        touch.native_pressureres = touch.pressure_max - touch.pressure_min;
        if (SDL_AddTouch(&touch, "") < 0) {
             SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
        }
    }

This brings it inline with how the iOS and X11 touch versions do things. We don't really want to deviate from what other platforms do as that defeats the point of SDL. It's probably also worth removing the commented code in the same function that was responsible for emulating mouse clicks.

To be fair the SDL touch API is quite confusing and perhaps deserves a little bit of a review before it gets deployed/used too much.
Comment 4 Gabriel Jacobo 2011-08-31 05:59:21 UTC
I understand, though I'd like to ask (out of curiosity) why was this decision made to normalize the coordinates to 0->32768 instead of a more natural 0->screen resolution or 0->window size, for example, which makes the coordinates in line with the values you get out of mouse events.
Comment 5 Tim Angus 2011-08-31 06:16:19 UTC
I don't know. My guess is the original author of the touch API (it was a GsoC project right?) was initially developing for pen based graphics tablets which don't have a screen resolution per se. That's just conjecture though.
Comment 6 Gabriel Jacobo 2011-08-31 07:07:14 UTC
Created attachment 695 [details]
Multitouch for Android

New patch following Tim's advice.
Comment 7 Ryan C. Gordon 2011-10-12 22:28:20 UTC
This patch is now hg changeset f324bd81b52c, thanks!

--ryan.