Skip to content

Commit

Permalink
Make loading/saving dollar gesture templates endian clean (thanks, Ma…
Browse files Browse the repository at this point in the history
…rtin!).

Fixes Bugzilla #2674.
  • Loading branch information
icculus committed Apr 5, 2015
1 parent 05a9206 commit 2f3f328
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions src/events/SDL_gesture.c
Expand Up @@ -24,6 +24,7 @@
/* General mouse handling code for SDL */

#include "SDL_events.h"
#include "SDL_endian.h"
#include "SDL_events_c.h"
#include "SDL_gesture_c.h"

Expand Down Expand Up @@ -114,14 +115,34 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points)

static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops *dst)
{
if (dst == NULL) return 0;
if (dst == NULL) {
return 0;
}

/* No Longer storing the Hash, rehash on load */
/* if (SDL_RWops.write(dst, &(templ->hash), sizeof(templ->hash), 1) != 1) return 0; */

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
if (SDL_RWwrite(dst, templ->path,
sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS)
sizeof(templ->path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
return 0;
}
#else
{
SDL_DollarTemplate copy = *templ;
SDL_FloatPoint *p = copy.path;
int i;
for (i = 0; i < DOLLARNPOINTS; i++, p++) {
p->x = SDL_SwapFloatLE(p->x);
p->y = SDL_SwapFloatLE(p->y);
}

if (SDL_RWwrite(dst, copy.path,
sizeof(copy.path[0]),DOLLARNPOINTS) != DOLLARNPOINTS) {
return 0;
}
}
#endif

return 1;
}
Expand Down Expand Up @@ -203,17 +224,33 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
SDL_GestureTouch *touch = NULL;
if (src == NULL) return 0;
if (touchId >= 0) {
for (i = 0; i < SDL_numGestureTouches; i++)
if (SDL_gestureTouch[i].id == touchId)
for (i = 0; i < SDL_numGestureTouches; i++) {
if (SDL_gestureTouch[i].id == touchId) {
touch = &SDL_gestureTouch[i];
if (touch == NULL) return SDL_SetError("given touch id not found");
}
}
if (touch == NULL) {
return SDL_SetError("given touch id not found");
}
}

while (1) {
SDL_DollarTemplate templ;

if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) <
DOLLARNPOINTS) break;
if (SDL_RWread(src,templ.path,sizeof(templ.path[0]),DOLLARNPOINTS) < DOLLARNPOINTS) {
if (loaded == 0) {
return SDL_SetError("could not read any dollar gesture from rwops");
}
break;
}

#if SDL_BYTEORDER != SDL_LIL_ENDIAN
for (i = 0; i < DOLLARNPOINTS; i++) {
SDL_FloatPoint *p = &templ.path[i];
p->x = SDL_SwapFloatLE(p->x);
p->y = SDL_SwapFloatLE(p->y);
}
#endif

if (touchId >= 0) {
/* printf("Adding loaded gesture to 1 touch\n"); */
Expand Down

0 comments on commit 2f3f328

Please sign in to comment.