Mercurial > sdl-ios-xcode
changeset 4641:49a97daea6ec
Added touch event definitions. Heavily modified events/SDL_touch*.
author | Jim Grandpre <jim.tla@gmail.com> |
---|---|
date | Thu, 27 May 2010 01:21:37 -0400 |
parents | f068a6dfc858 |
children | 057e8762d2a1 |
files | include/SDL_events.h src/events/SDL_touch.c src/events/SDL_touch_c.h touchTest/touchPong touchTest/touchSimp |
diffstat | 5 files changed, 255 insertions(+), 333 deletions(-) [+] |
line wrap: on
line diff
--- a/include/SDL_events.h Tue May 25 23:23:23 2010 -0400 +++ b/include/SDL_events.h Thu May 27 01:21:37 2010 -0400 @@ -86,11 +86,19 @@ SDL_JOYBUTTONDOWN, /**< Joystick button pressed */ SDL_JOYBUTTONUP, /**< Joystick button released */ + /*Touch events - is 0x700 the correct place?*/ + SDL_FINGERDOWN = 0x700, + SDL_FINGERUP, + SDL_FINGERMOTION, + SDL_TOUCHBUTTONDOWN, + SDL_TOUCHBUTTONUP, + /* Obsolete events */ SDL_EVENT_COMPAT1 = 0x7000, /**< SDL 1.2 events for compatibility */ SDL_EVENT_COMPAT2, SDL_EVENT_COMPAT3, + /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use, * and should be allocated with SDL_RegisterEvents() */ @@ -289,6 +297,37 @@ Uint8 padding1; } SDL_JoyButtonEvent; + +/** + * \brief Touch finger motion/finger event structure (event.tmotion.*) + */ +typedef struct SDL_TouchFingerEvent +{ + Uint32 type; /**< ::SDL_FINGERMOTION OR + SDL_FINGERDOWN OR SDL_FINGERUP*/ + Uint32 windowID; /**< The window with mouse focus, if any */ + Uint8 touchId; /**< The touch device id */ + Uint8 state; /**< The current button state */ + Uint8 fingerId; + Uint8 padding1; +} SDL_TouchFingerEvent; + + +/** + * \brief Touch finger motion/finger event structure (event.tmotion.*) + */ +typedef struct SDL_TouchButtonEvent +{ + Uint32 type; /**< ::SDL_TOUCHBUTTONUP OR SDL_TOUCHBUTTONDOWN */ + Uint32 windowID; /**< The window with mouse focus, if any */ + Uint8 touchId; /**< The touch device index */ + Uint8 state; /**< The current button state */ + Uint8 button; /**< The button changing state */ + Uint8 padding1; + +} SDL_TouchButtonEvent; + + /** * \brief The "quit requested" event */ @@ -372,6 +411,8 @@ SDL_UserEvent user; /**< Custom event data */ SDL_SysWMEvent syswm; /**< System dependent window event data */ SDL_ProximityEvent proximity; /**< Proximity In or Out event */ + SDL_TouchFingerEvent tfinger; /**< Touch finger event data */ + SDL_TouchButtonEvent tbutton; /**< Touch button event data */ /** Temporarily here for backwards compatibility */ /*@{*/
--- a/src/events/SDL_touch.c Tue May 25 23:23:23 2010 -0400 +++ b/src/events/SDL_touch.c Thu May 27 01:21:37 2010 -0400 @@ -29,8 +29,7 @@ static int SDL_num_touch = 0; -static int SDL_current_touch = -1; -static SDL_Touch **SDL_touch = NULL; +static SDL_Touch **SDL_touchPads = NULL; /* Public functions */ @@ -39,24 +38,43 @@ { return (0); } - SDL_Touch * -SDL_GetTouch(int index) +SDL_GetTouch(int id) { + int index = SDL_GetTouchIndexId(id); if (index < 0 || index >= SDL_num_touch) { return NULL; } - return SDL_touch[index]; + return SDL_touchPads[index]; +} + +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]; } -static int +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; +} + +int SDL_GetTouchIndexId(int id) { int index; SDL_Touch *touch; for (index = 0; index < SDL_num_touch; ++index) { - touch = SDL_GetTouch(index); + touch = SDL_touchPads[index]; if (touch->id == id) { return index; } @@ -68,7 +86,7 @@ SDL_AddTouch(const SDL_Touch * touch, char *name, int pressure_max, int pressure_min, int ends) { - SDL_Touch **touch; + SDL_Touch **touchPads; int selected_touch; int index; size_t length; @@ -78,58 +96,47 @@ } /* Add the touch to the list of touch */ - touch = (SDL_Touch **) SDL_realloc(SDL_touch, + touchPads = (SDL_Touch **) SDL_realloc(SDL_touchPads, (SDL_num_touch + 1) * sizeof(*touch)); - if (!touch) { + if (!touchPads) { SDL_OutOfMemory(); return -1; } - SDL_touch = touch; + SDL_touchPads = touchPads; index = SDL_num_touch++; - SDL_touch[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touch[index])); - if (!SDL_touch[index]) { + SDL_touchPads[index] = (SDL_Touch *) SDL_malloc(sizeof(*SDL_touchPads[index])); + if (!SDL_touchPads[index]) { SDL_OutOfMemory(); return -1; } - *SDL_touch[index] = *touch; + *SDL_touchPads[index] = *touch; /* we're setting the touch properties */ length = 0; length = SDL_strlen(name); - SDL_touch[index]->focus = 0; - SDL_touch[index]->name = SDL_malloc((length + 2) * sizeof(char)); - SDL_strlcpy(SDL_touch[index]->name, name, length + 1); - SDL_touch[index]->pressure_max = pressure_max; - SDL_touch[index]->pressure_min = pressure_min; - SDL_touch[index]->cursor_shown = SDL_TRUE; - selected_touch = SDL_SelectTouch(index); - SDL_touch[index]->cur_cursor = NULL; - SDL_touch[index]->def_cursor = - /* we're assuming that all touch are in the computer sensing zone */ - SDL_touch[index]->proximity = SDL_TRUE; - /* we're assuming that all touch are working in the absolute position mode - thanx to that, the users that don't want to use many touch don't have to - worry about anything */ - SDL_touch[index]->relative_mode = SDL_FALSE; - SDL_touch[index]->current_end = 0; - SDL_touch[index]->total_ends = ends; - SDL_SelectTouch(selected_touch); + 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]->pressure_max = pressure_max; + SDL_touchPads[index]->pressure_min = pressure_min; + return index; } void -SDL_DelTouch(int index) +SDL_DelTouch(int id) { - SDL_Touch *touch = SDL_GetTouch(index); + int index = SDL_GetTouchIndexId(id); + SDL_Touch *touch = SDL_GetTouch(id); if (!touch) { return; } - touch->def_cursor = NULL; + SDL_free(touch->name); if (touch->FreeTouch) { @@ -137,19 +144,8 @@ } SDL_free(touch); - SDL_touch[index] = NULL; -} - -void -SDL_ResetTouch(int index) -{ - SDL_Touch *touch = SDL_GetTouch(index); - - if (!touch) { - return; - } - - /* FIXME */ + SDL_num_touch--; + SDL_touchPads[index] = SDL_touchPads[SDL_num_touch]; } void @@ -157,15 +153,14 @@ { int i; - for (i = 0; i < SDL_num_touch; ++i) { + for (i = SDL_num_touch-1; i > 0 ; --i) { SDL_DelTouch(i); } SDL_num_touch = 0; - SDL_current_touch = -1; - if (SDL_touch) { - SDL_free(SDL_touch); - SDL_touch = NULL; + if (SDL_touchPads) { + SDL_free(SDL_touchPads); + SDL_touchPads = NULL; } } @@ -174,20 +169,10 @@ { return SDL_num_touch; } - -int -SDL_SelectTouch(int index) +SDL_Window * +SDL_GetTouchFocusWindow(int id) { - if (index >= 0 && index < SDL_num_touch) { - SDL_current_touch = index; - } - return SDL_current_touch; -} - -SDL_Window * -SDL_GetTouchFocusWindow(int index) -{ - SDL_Touch *touch = SDL_GetTouch(index); + SDL_Touch *touch = SDL_GetTouch(id); if (!touch) { return 0; @@ -195,110 +180,11 @@ return touch->focus; } -static int SDLCALL -FlushTouchMotion(void *param, SDL_Event * event) -{ - if (event->type == SDL_TOUCHMOTION - && event->motion.which == (Uint8) SDL_current_touch) { - return 0; - } else { - return 1; - } -} - -int -SDL_SetRelativeTouchMode(int index, SDL_bool enabled) -{ - SDL_Touch *touch = SDL_GetTouch(index); - - if (!touch) { - return -1; - } - - /* Flush pending touch motion */ - touch->flush_motion = SDL_TRUE; - SDL_PumpEvents(); - touch->flush_motion = SDL_FALSE; - SDL_FilterEvents(FlushTouchMotion, touch); - - /* Set the relative mode */ - touch->relative_mode = enabled; - - - - if (!enabled) { - /* Restore the expected touch position */ - SDL_WarpTouchInWindow(touch->focus, touch->x, touch->y); - } - return 0; -} - -SDL_bool -SDL_GetRelativeTouchMode(int index) -{ - SDL_Touch *touch = SDL_GetTouch(index); - - if (!touch) { - return SDL_FALSE; - } - return touch->relative_mode; -} - -Uint8 -SDL_GetTouchState(int *x, int *y) -{ - SDL_Touch *touch = SDL_GetTouch(SDL_current_touch); - - if (!touch) { - if (x) { - *x = 0; - } - if (y) { - *y = 0; - } - return 0; - } - - if (x) { - *x = touch->x; - } - if (y) { - *y = touch->y; - } - return touch->buttonstate; -} - -Uint8 -SDL_GetRelativeTouchState(int index, int *x, int *y) -{ - SDL_Touch *touch = SDL_GetTouch(index); - - if (!touch) { - if (x) { - *x = 0; - } - if (y) { - *y = 0; - } - return 0; - } - - if (x) { - *x = touch->xdelta; - } - if (y) { - *y = touch->ydelta; - } - touch->xdelta = 0; - touch->ydelta = 0; - return touch->buttonstate; -} - void SDL_SetTouchFocus(int id, SDL_Window * window) { int index = SDL_GetTouchIndexId(id); - SDL_Touch *touch = SDL_GetTouch(index); + SDL_Touch *touch = SDL_GetTouch(id); int i; SDL_bool focus; @@ -312,7 +198,7 @@ for (i = 0; i < SDL_num_touch; ++i) { SDL_Touch *check; if (i != index) { - check = SDL_GetTouch(i); + check = SDL_touchPads[i]; if (check && check->focus == touch->focus) { focus = SDL_TRUE; break; @@ -331,7 +217,7 @@ for (i = 0; i < SDL_num_touch; ++i) { SDL_Touch *check; if (i != index) { - check = SDL_GetTouch(i); + check = SDL_touchPads[i]; if (check && check->focus == touch->focus) { focus = SDL_TRUE; break; @@ -344,43 +230,106 @@ } } -int -SDL_SendProximity(int id, int x, int y, int type) +int +SDL_AddFinger(SDL_Touch* touch,SDL_Finger* finger) { - int index = SDL_GetTouchIndexId(id); - SDL_Touch *touch = SDL_GetTouch(index); - int posted = 0; - - if (!touch) { - return 0; + int index; + SDL_Finger **fingers; + size_t length; + + if (SDL_GetFingerIndexId(touch,finger->id) != -1) { + SDL_SetError("Finger ID already in use"); } - touch->last_x = x; - touch->last_y = y; - if (SDL_GetEventState(type) == SDL_ENABLE) { - SDL_Event event; - event.proximity.which = (Uint8) index; - event.proximity.x = x; - event.proximity.y = y; - event.proximity.cursor = touch->current_end; - event.proximity.type = type; - /* FIXME: is this right? */ - event.proximity.windowID = touch->focus ? touch->focus->id : 0; - posted = (SDL_PushEvent(&event) > 0); - if (type == SDL_PROXIMITYIN) { - touch->proximity = SDL_TRUE; - } else { - touch->proximity = SDL_FALSE; - } + /* 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; } - return posted; + + touch->fingers = fingers; + index = SDL_num_touch++; + + 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_SendTouchMotion(int id, int relative, int x, int y, int pressure) +SDL_DelFinger(SDL_Touch* touch,int fingerid) +{ + int index = SLD_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) +{ + 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.state = touch->buttonstate; + event.tfinger.windowID = touch->focus ? touch->focus->id : 0; + event.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.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(index); + SDL_Touch *touch = SDL_GetTouch(id); + SDL_Finger *finger = SDL_GetFinger(touch,fingerid); int posted; int xrel; int yrel; @@ -390,22 +339,15 @@ return 0; } - /* if the touch is out of proximity we don't to want to have any motion from it */ - if (touch->proximity == SDL_FALSE) { - touch->last_x = x; - touch->last_y = y; - return 0; - } - /* the relative motion is calculated regarding the system cursor last position */ if (relative) { xrel = x; yrel = y; - x = (touch->last_x + x); - y = (touch->last_y + y); + x = (finger->last_x + x); + y = (finger->last_y + y); } else { - xrel = x - touch->last_x; - yrel = y - touch->last_y; + xrel = x - finger->last_x; + yrel = y - finger->last_y; } /* Drop events that don't change state */ @@ -417,18 +359,16 @@ } /* Update internal touch coordinates */ - if (touch->relative_mode == SDL_FALSE) { - touch->x = x; - touch->y = y; - } else { - touch->x += xrel; - touch->y += yrel; - } - SDL_GetWindowSize(touch->focus, &x_max, &y_max); + 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) { @@ -440,46 +380,32 @@ } else if (touch->y < 0) { touch->y = 0; } - - touch->xdelta += xrel; - touch->ydelta += yrel; - touch->pressure = pressure; + */ + finger->xdelta += xrel; + finger->ydelta += yrel; + finger->pressure = pressure; /* Post the event, if desired */ posted = 0; - if (SDL_GetEventState(SDL_TOUCHMOTION) == SDL_ENABLE && - touch->proximity == SDL_TRUE) { + if (SDL_GetEventState(SDL_FINGERMOTION) == SDL_ENABLE) { SDL_Event event; - event.motion.type = SDL_TOUCHMOTION; - event.motion.which = (Uint8) index; - event.motion.state = touch->buttonstate; - event.motion.x = touch->x; - event.motion.y = touch->y; - event.motion.z = touch->z; - event.motion.pressure = touch->pressure; - event.motion.pressure_max = touch->pressure_max; - event.motion.pressure_min = touch->pressure_min; - event.motion.rotation = 0; - event.motion.tilt_x = 0; - event.motion.tilt_y = 0; - event.motion.cursor = touch->current_end; - event.motion.xrel = xrel; - event.motion.yrel = yrel; - event.motion.windowID = touch->focus ? touch->focus->id : 0; + event.tfinger.type = SDL_FINGERMOTION; + event.tfinger.which = (Uint8) index; + event.tfinger.state = touch->buttonstate; + event.tfinger.windowID = touch->focus ? touch->focus->id : 0; posted = (SDL_PushEvent(&event) > 0); } - touch->last_x = touch->x; - touch->last_y = touch->y; + finger->last_x = finger->x; + finger->last_y = finger->y; return posted; } int SDL_SendTouchButton(int id, Uint8 state, Uint8 button) { - int index = SDL_GetTouchIndexId(id); - SDL_Touch *touch = SDL_GetTouch(index); + SDL_Touch *touch = SDL_GetTouch(id); int posted; Uint32 type; @@ -515,61 +441,23 @@ if (SDL_GetEventState(type) == SDL_ENABLE) { SDL_Event event; event.type = type; - event.button.which = (Uint8) index; - event.button.state = state; - event.button.button = button; - event.button.x = touch->x; - event.button.y = touch->y; - event.button.windowID = touch->focus ? touch->focus->id : 0; + event.tbutton.which = (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; } -int -SDL_SendTouchWheel(int index, int x, int y) +char * +SDL_GetTouchName(int id) { - SDL_Touch *touch = SDL_GetTouch(index); - int posted; - - if (!touch || (!x && !y)) { - return 0; - } - - /* Post the event, if desired */ - posted = 0; - if (SDL_GetEventState(SDL_TOUCHWHEEL) == SDL_ENABLE) { - SDL_Event event; - event.type = SDL_TOUCHWHEEL; - event.wheel.which = (Uint8) index; - event.wheel.x = x; - event.wheel.y = y; - event.wheel.windowID = touch->focus ? touch->focus->id : 0; - posted = (SDL_PushEvent(&event) > 0); - } - return posted; -} - - -char * -SDL_GetTouchName(int index) -{ - SDL_Touch *touch = SDL_GetTouch(index); + SDL_Touch *touch = SDL_GetTouch(id); if (!touch) { return NULL; } return touch->name; } -void -SDL_ChangeEnd(int id, int end) -{ - int index = SDL_GetTouchIndexId(id); - SDL_Touch *touch = SDL_GetTouch(index); - - if (touch) { - touch->current_end = end; - } -} - /* vi: set ts=4 sw=4 expandtab: */
--- a/src/events/SDL_touch_c.h Tue May 25 23:23:23 2010 -0400 +++ b/src/events/SDL_touch_c.h Thu May 27 01:21:37 2010 -0400 @@ -25,49 +25,50 @@ #define _SDL_touch_c_h typedef struct SDL_Touch SDL_Touch; +typedef struct SDL_Finger SDL_Finger; + +struct SDL_Finger { + int id; + int x; + int y; + int z; /* for future use */ + int xdelta; + int ydelta; + int last_x, last_y; /* the last reported x and y coordinates */ + int pressure; +}; struct SDL_Touch { - /* Warp the touch to (x,y) */ - void (*WarpTouch) (SDL_Touch * touch, SDL_Window * window, int x, - int y); - - /* Free the touch when it's time */ - void (*FreeTouch) (SDL_Touch * touch); - - /* data common for tablets */ - int pressure; - int pressure_max; - int pressure_min; - int tilt; /* for future use */ - int rotation; /* for future use */ - int total_ends; - int current_end; + + /* Free the touch when it's time */ + void (*FreeTouch) (SDL_Touch * touch); + + /* data common for tablets */ + int pressure_max; + int pressure_min; + int tilt; /* for future use */ + int rotation; /* for future use */ + + int total_ends; + int current_end; + + /* Data common to all touch */ + int id; + SDL_Window *focus; + + char *name; + Uint8 buttonstate; + SDL_bool relative_mode; + SDL_bool flush_motion; - /* Data common to all touch */ - int id; - SDL_Window *focus; - int which; - int x; - int y; - int z; /* for future use */ - int xdelta; - int ydelta; - int last_x, last_y; /* the last reported x and y coordinates */ - char *name; - Uint8 buttonstate; - SDL_bool relative_mode; - SDL_bool proximity; - SDL_bool flush_motion; + int num_fingers; + SDL_Finger** fingers; + + void *driverdata; +}; - SDL_Cursor *cursors; - SDL_Cursor *def_cursor; - SDL_Cursor *cur_cursor; - SDL_bool cursor_shown; - - void *driverdata; -}; /* Initialize the touch subsystem */ extern int SDL_TouchInit(void); @@ -84,24 +85,16 @@ /* Remove a touch at an index, clearing the slot for later */ extern void SDL_DelTouch(int index); -/* Clear the button state of a touch at an index */ -extern void SDL_ResetTouch(int index); - /* Set the touch focus window */ extern void SDL_SetTouchFocus(int id, SDL_Window * window); /* Send a touch motion event for a touch */ -extern int SDL_SendTouchMotion(int id, int relative, int x, int y, int z); +extern int SDL_SendTouchMotion(int id, int fingerid, + int relative, int x, int y, int z); /* Send a touch button event for a touch */ extern int SDL_SendTouchButton(int id, Uint8 state, Uint8 button); -/* Send a touch wheel event for a touch */ -extern int SDL_SendTouchWheel(int id, int x, int y); - -/* Send a proximity event for a touch */ -extern int SDL_SendProximity(int id, int x, int y, int type); - /* Shutdown the touch subsystem */ extern void SDL_TouchQuit(void);