/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2010 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

/* General touch handling code for SDL */

#include "SDL_events.h"
#include "SDL_events_c.h"
#include "../video/SDL_sysvideo.h"


static int SDL_num_touch = 0;
static SDL_Touch **SDL_touchPads = NULL;


/* Public functions */
int
SDL_TouchInit(void)
{
    SDL_Touch touch;
    touch.pressure_max = 0;
    touch.pressure_min = 0;
    touch.id = 0; //Should be function? 
    
    SDL_AddTouch(&touch, "Touch1");
    return (0);
}

SDL_Touch *
SDL_GetTouch(int id)
{
    int index = SDL_GetTouchIndexId(id);
    if (index < 0 || index >= SDL_num_touch) {
        return NULL;
    }
    return SDL_touchPads[index];
}

SDL_Touch *
SDL_GetTouchIndex(int index)
{
    if (index < 0 || index >= SDL_num_touch) {
        return NULL;
    }
    return SDL_touchPads[index];
}

int
SDL_GetFingerIndexId(SDL_Touch* touch,int fingerid)
{
    int i;
    for(i = 0;i < touch->num_fingers;i++)
	if(touch->fingers[i]->id == fingerid)
	    return i;
    return -1;
}


SDL_Finger *
SDL_GetFinger(SDL_Touch* touch,int id)
{
    int index = SDL_GetFingerIndexId(touch,id);
    if(index < 0 || index >= touch->num_fingers)
	return NULL;
    return touch->fingers[index];
}


int
SDL_GetTouchIndexId(int id)
{
    int index;
    SDL_Touch *touch;

    for (index = 0; index < SDL_num_touch; ++index) {
        touch = SDL_touchPads[index];
        if (touch->id == id) {
            return index;
        }
    }
    return -1;
}

int
SDL_AddTouch(const SDL_Touch * touch, char *name)
{
    SDL_Touch **touchPads;
    int selected_touch;
    int index;
    size_t length;

    if (SDL_GetTouchIndexId(touch->id) != -1) {
        SDL_SetError("Touch ID already in use");
    }

    /* Add the touch to the list of touch */
    touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads,
                                      (SDL_num_touch + 1) * sizeof(*touch));
    if (!touchPads) {
        SDL_OutOfMemory();
        return -1;
    }

    SDL_touchPads = touchPads;
    index = SDL_num_touch++;

    SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index]));
    if (!SDL_touchPads[index]) {
        SDL_OutOfMemory();
        return -1;
    }
    *SDL_touchPads[index] = *touch;

    /* we're setting the touch properties */
    length = 0;
    length = SDL_strlen(name);
    SDL_touchPads[index]->focus = 0;
    SDL_touchPads[index]->name = SDL_malloc((length + 2) * sizeof(char));
    SDL_strlcpy(SDL_touchPads[index]->name, name, length + 1);   

    SDL_touchPads[index]->num_fingers = 0;
    SDL_touchPads[index]->buttonstate = 0;
    SDL_touchPads[index]->relative_mode = SDL_FALSE;
    SDL_touchPads[index]->flush_motion = SDL_FALSE;
    
    return index;
}

void
SDL_DelTouch(int id)
{
    int index = SDL_GetTouchIndexId(id);
    SDL_Touch *touch = SDL_GetTouch(id);

    if (!touch) {
        return;
    }

    
    SDL_free(touch->name);
 
    if (touch->FreeTouch) {
        touch->FreeTouch(touch);
    }
    SDL_free(touch);

    SDL_num_touch--;
    SDL_touchPads[index] = SDL_touchPads[SDL_num_touch];
}

void
SDL_TouchQuit(void)
{
    int i;

    for (i = SDL_num_touch-1; i > 0 ; --i) {
        SDL_DelTouch(i);
    }
    SDL_num_touch = 0;

    if (SDL_touchPads) {
        SDL_free(SDL_touchPads);
        SDL_touchPads = NULL;
    }
}

int
SDL_GetNumTouch(void)
{
    return SDL_num_touch;
}
SDL_Window *
SDL_GetTouchFocusWindow(int id)
{
    SDL_Touch *touch = SDL_GetTouch(id);

    if (!touch) {
        return 0;
    }
    return touch->focus;
}

void
SDL_SetTouchFocus(int id, SDL_Window * window)
{
    int index = SDL_GetTouchIndexId(id);
    SDL_Touch *touch = SDL_GetTouch(id);
    int i;
    SDL_bool focus;

    if (!touch || (touch->focus == window)) {
        return;
    }

    /* See if the current window has lost focus */
    if (touch->focus) {
        focus = SDL_FALSE;
        for (i = 0; i < SDL_num_touch; ++i) {
            SDL_Touch *check;
            if (i != index) {
                check = SDL_touchPads[i];
                if (check && check->focus == touch->focus) {
                    focus = SDL_TRUE;
                    break;
                }
            }
        }
        if (!focus) {
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
        }
    }

    touch->focus = window;

    if (touch->focus) {
        focus = SDL_FALSE;
        for (i = 0; i < SDL_num_touch; ++i) {
            SDL_Touch *check;
            if (i != index) {
                check = SDL_touchPads[i];
                if (check && check->focus == touch->focus) {
                    focus = SDL_TRUE;
                    break;
                }
            }
        }
        if (!focus) {
            SDL_SendWindowEvent(touch->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
        }
    }
}

int 
SDL_AddFinger(SDL_Touch* touch,SDL_Finger* finger)
{
    int index;
    SDL_Finger **fingers;
    size_t length;
    
    if (SDL_GetFingerIndexId(touch,finger->id) != -1) {
        SDL_SetError("Finger ID already in use");
	}

    /* Add the touch to the list of touch */
    fingers = (SDL_Finger **) SDL_realloc(touch->fingers,
					  (touch->num_fingers + 1) * sizeof(*touch));
    if (!fingers) {
        SDL_OutOfMemory();
        return -1;
    }

    touch->fingers = fingers;
    index = touch->num_fingers++;

    touch->fingers[index] = (SDL_Finger *) SDL_malloc(sizeof(*(touch->fingers[index])));
    if (!touch->fingers[index]) {
        SDL_OutOfMemory();
        return -1;
    }
    *touch->fingers[index] = *finger;    

    return index;
}

int
SDL_DelFinger(SDL_Touch* touch,int fingerid)
{
    int index = SDL_GetFingerIndexId(touch,fingerid);
    SDL_Finger* finger = SDL_GetFinger(touch,fingerid);

    if (!finger) {
        return;
    }
 

    SDL_free(finger);
    touch->num_fingers--;
    touch->fingers[index] = touch->fingers[touch->num_fingers];
}


int
SDL_SendFingerDown(int id, int fingerid, SDL_bool down, int x, int y, int pressure)
{
  int posted;
    SDL_Touch* touch = SDL_GetTouch(id);
    if(down) {
	SDL_Finger nf;
	nf.id = id;
	nf.x = x;
	nf.y = y;
	nf.pressure = pressure;
	nf.xdelta = 0;
	nf.ydelta = 0;
	nf.last_x = x;
	nf.last_y = y;
	SDL_AddFinger(touch,&nf);

	posted = 0;
	if (SDL_GetEventState(SDL_FINGERDOWN) == SDL_ENABLE) {
	    SDL_Event event;
	    event.tfinger.type = SDL_FINGERDOWN;
	    event.tfinger.touchId = (Uint8) id;
	    event.tfinger.x = x;
	    event.tfinger.y = y;
	    event.tfinger.state = touch->buttonstate;
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
	    event.tfinger.fingerId = id;
	    posted = (SDL_PushEvent(&event) > 0);
	}
	return posted;
    }
    else {
	SDL_DelFinger(touch,id);
	posted = 0;
	if (SDL_GetEventState(SDL_FINGERUP) == SDL_ENABLE) {
	    SDL_Event event;
	    event.tfinger.type = SDL_FINGERUP;
	    event.tfinger.touchId = (Uint8) id;
	    event.tfinger.state = touch->buttonstate;
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
	    event.tfinger.fingerId = id;
	    posted = (SDL_PushEvent(&event) > 0);
	}
	return posted;
    }
}

int
SDL_SendTouchMotion(int id, int fingerid, int relative, 
		    int x, int y, int pressure)
{
    int index = SDL_GetTouchIndexId(id);
    SDL_Touch *touch = SDL_GetTouch(id);
    SDL_Finger *finger = SDL_GetFinger(touch,fingerid);
    int posted;
    int xrel;
    int yrel;
    int x_max = 0, y_max = 0;

    if (!touch || touch->flush_motion) {
        return 0;
    }

    if(finger == NULL)
	SDL_SendFingerDown(id,fingerid,SDL_TRUE,x,y,pressure);
    else {
	/* the relative motion is calculated regarding the last position */
	if (relative) {
	    xrel = x;
	    yrel = y;
	    x = (finger->last_x + x);
	    y = (finger->last_y + y);
	} else {
	    if(x < 0) x = finger->last_x; /*If movement is only in one axis,*/
	    if(y < 0) y = finger->last_y; /*The other is marked as -1*/
	    xrel = x - finger->last_x;
	    yrel = y - finger->last_y;
	}
	
	/* Drop events that don't change state */
	if (!xrel && !yrel) {
#if 0
	    printf("Touch event didn't change state - dropped!\n");
#endif
	    return 0;
	}
	
	/* Update internal touch coordinates */
	
	finger->x = x;
	finger->y = y;
	
	/*Should scale to window? Normalize? Maintain Aspect?*/
	//SDL_GetWindowSize(touch->focus, &x_max, &y_max);
	
	/* make sure that the pointers find themselves inside the windows */
	/* only check if touch->xmax is set ! */
	/*
	  if (x_max && touch->x > x_max) {
	  touch->x = x_max;
	  } else if (touch->x < 0) {
	  touch->x = 0;
	  }
	  
	  if (y_max && touch->y > y_max) {
	  touch->y = y_max;
	  } else if (touch->y < 0) {
	  touch->y = 0;
	  }
	*/
	finger->xdelta += xrel;
	finger->ydelta += yrel;
	finger->pressure = pressure;
	
	
	
	/* Post the event, if desired */
	posted = 0;
	if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) {
	    SDL_Event event;
	    event.tfinger.type = SDL_FINGERMOTION;
	    event.tfinger.touchId = (Uint8) index;
	    event.tfinger.x = x;
	    event.tfinger.y = y;
	    event.tfinger.state = touch->buttonstate;
	    event.tfinger.windowID = touch->focus ? touch->focus->id : 0;
	    posted = (SDL_PushEvent(&event) > 0);
	}
	finger->last_x = finger->x;
	finger->last_y = finger->y;
	return posted;
    }
}
int
SDL_SendTouchButton(int id, Uint8 state, Uint8 button)
{
    SDL_Touch *touch = SDL_GetTouch(id);
    int posted;
    Uint32 type;

    if (!touch) {
        return 0;
    }

    /* Figure out which event to perform */
    switch (state) {
    case SDL_PRESSED:
        if (touch->buttonstate & SDL_BUTTON(button)) {
            /* Ignore this event, no state change */
            return 0;
        }
        type = SDL_TOUCHBUTTONDOWN;
        touch->buttonstate |= SDL_BUTTON(button);
        break;
    case SDL_RELEASED:
        if (!(touch->buttonstate & SDL_BUTTON(button))) {
            /* Ignore this event, no state change */
            return 0;
        }
        type = SDL_TOUCHBUTTONUP;
        touch->buttonstate &= ~SDL_BUTTON(button);
        break;
    default:
        /* Invalid state -- bail */
        return 0;
    }

    /* Post the event, if desired */
    posted = 0;
    if (SDL_GetEventState(type) == SDL_ENABLE) {
        SDL_Event event;
        event.type = type;
        event.tbutton.touchId = (Uint8) index;
        event.tbutton.state = state;
        event.tbutton.button = button;
        event.tbutton.windowID = touch->focus ? touch->focus->id : 0;
        posted = (SDL_PushEvent(&event) > 0);
    }
    return posted;
}

char *
SDL_GetTouchName(int id)
{
    SDL_Touch *touch = SDL_GetTouch(id);
    if (!touch) {
        return NULL;
    }
    return touch->name;
}

/* vi: set ts=4 sw=4 expandtab: */
